blob: de059b0faa996a2d4822d24a9373d9ce02d8da03 [file] [log] [blame]
ebiedermc7798892009-04-01 11:03:32 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com>
5 * Copyright (C) 2009 Ron Minnich <rminnich@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
19 */
20
Stefan Reinauer13774912011-10-14 15:19:21 -070021#include <arch/byteorder.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000022#include <console/console.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050023#include <cpu/cpu.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000024#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000027#include <cbfs.h>
Myles Watson58170782009-10-28 16:13:28 +000028#include <lib.h>
Aaron Durbinceebc052014-02-25 00:21:10 -060029#include <bootmem.h>
Aaron Durbinbdf913a2014-02-24 14:56:34 -060030#include <payload_loader.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000031
Furquan Shaikh20f25dd2014-04-22 10:41:05 -070032/* from ramstage.ld: */
Ronald G. Minnichae631262009-04-01 10:48:39 +000033extern unsigned char _ram_seg;
34extern unsigned char _eram_seg;
35
Stefan Reinauer19436292011-11-07 12:43:03 -080036static const unsigned long lb_start = (unsigned long)&_ram_seg;
37static const unsigned long lb_end = (unsigned long)&_eram_seg;
38
Ronald G. Minnichae631262009-04-01 10:48:39 +000039struct segment {
40 struct segment *next;
41 struct segment *prev;
Ronald G. Minnichae631262009-04-01 10:48:39 +000042 unsigned long s_dstaddr;
43 unsigned long s_srcaddr;
44 unsigned long s_memsz;
45 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000046 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000047};
48
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000049/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000050 * Static executables all want to share the same addresses
51 * in memory because only a few addresses are reliably present on
52 * a machine, and implementing general relocation is hard.
53 *
54 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000055 * - Allocate a buffer the size of the coreboot image plus additional
56 * required space.
57 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000058 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000059 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000060 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000061 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000062 * Benefits:
63 * - Nearly arbitrary standalone executables can be loaded.
64 * - Coreboot is preserved, so it can be returned to.
65 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +000066 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000067 */
68
Patrick Georgi5eceb322009-05-13 16:27:25 +000069static unsigned long bounce_size, bounce_buffer;
70
Aaron Durbinceebc052014-02-25 00:21:10 -060071static void get_bounce_buffer(unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000072{
73 unsigned long lb_size;
Aaron Durbinceebc052014-02-25 00:21:10 -060074 void *buffer;
Aaron Durbine58a24b2014-02-24 22:11:45 -060075
76 /* When the ramstage is relocatable there is no need for a bounce
77 * buffer. All payloads should not overlap the ramstage.
78 */
79 if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
80 bounce_buffer = ~0UL;
81 bounce_size = 0;
82 return;
83 }
84
Stefan Reinauer19436292011-11-07 12:43:03 -080085 lb_size = lb_end - lb_start;
86 /* Plus coreboot size so I have somewhere
87 * to place a copy to return to.
88 */
Myles Watson92027982009-09-23 20:32:21 +000089 lb_size = req_size + lb_size;
Aaron Durbinceebc052014-02-25 00:21:10 -060090
91 buffer = bootmem_allocate_buffer(lb_size);
92
93 printk(BIOS_SPEW, "Bounce Buffer at %p, %lu bytes\n", buffer, lb_size);
94
95 bounce_buffer = (uintptr_t)buffer;
Myles Watson92027982009-09-23 20:32:21 +000096 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +000097}
98
Patrick Georgi5eceb322009-05-13 16:27:25 +000099static int overlaps_coreboot(struct segment *seg)
100{
101 unsigned long start, end;
102 start = seg->s_dstaddr;
103 end = start + seg->s_memsz;
104 return !((end <= lb_start) || (start >= lb_end));
105}
106
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000107static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000108{
109 /* Modify all segments that want to load onto coreboot
110 * to load onto the bounce buffer instead.
111 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000112 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800113 * 0 : A new segment is inserted after the seg, or no new one.
114 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000115 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000116
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000117 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000118 lb_start, lb_end);
119
Patrick Georgi5eceb322009-05-13 16:27:25 +0000120 /* I don't conflict with coreboot so get out of here */
121 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000122 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000123
Ronald G. Minnichae631262009-04-01 10:48:39 +0000124 start = seg->s_dstaddr;
125 middle = start + seg->s_filesz;
126 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000127
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000128 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000129 start, middle, end);
130
Patrick Georgi369bc782009-04-25 07:32:24 +0000131 if (seg->compression == CBFS_COMPRESS_NONE) {
132 /* Slice off a piece at the beginning
133 * that doesn't conflict with coreboot.
134 */
135 if (start < lb_start) {
136 struct segment *new;
137 unsigned long len = lb_start - start;
138 new = malloc(sizeof(*new));
139 *new = *seg;
140 new->s_memsz = len;
141 seg->s_memsz -= len;
142 seg->s_dstaddr += len;
143 seg->s_srcaddr += len;
144 if (seg->s_filesz > len) {
145 new->s_filesz = len;
146 seg->s_filesz -= len;
147 } else {
148 seg->s_filesz = 0;
149 }
150
151 /* Order by stream offset */
152 new->next = seg;
153 new->prev = seg->prev;
154 seg->prev->next = new;
155 seg->prev = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000156
157 /* compute the new value of start */
158 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000159
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000160 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000161 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000162 new->s_dstaddr + new->s_filesz,
163 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000164
165 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000166 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000167
168 /* Slice off a piece at the end
169 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000170 */
171 if (end > lb_end) {
172 unsigned long len = lb_end - start;
173 struct segment *new;
174 new = malloc(sizeof(*new));
175 *new = *seg;
176 seg->s_memsz = len;
177 new->s_memsz -= len;
178 new->s_dstaddr += len;
179 new->s_srcaddr += len;
180 if (seg->s_filesz > len) {
181 seg->s_filesz = len;
182 new->s_filesz -= len;
183 } else {
184 new->s_filesz = 0;
185 }
186 /* Order by stream offset */
187 new->next = seg->next;
188 new->prev = seg;
189 seg->next->prev = new;
190 seg->next = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000191
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000192 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000193 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000194 new->s_dstaddr + new->s_filesz,
195 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000196 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000197 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000198
Ronald G. Minnichae631262009-04-01 10:48:39 +0000199 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000200 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000201 * so you will make the dstaddr be this buffer, and it will get copied
202 * later to where coreboot lives.
203 */
204 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
205
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000206 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000207 seg->s_dstaddr,
208 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000209 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000210
211 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000212}
213
214
215static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000216 struct segment *head,
Aaron Durbin6086e632014-02-24 21:50:24 -0600217 struct payload *payload, uintptr_t *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000218{
219 struct segment *new;
220 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000221 struct cbfs_payload_segment *segment, *first_segment;
Aaron Durbin6086e632014-02-24 21:50:24 -0600222 struct cbfs_payload *cbfs_payload;
223 cbfs_payload = payload->backing_store.data;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000224 memset(head, 0, sizeof(*head));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000225 head->next = head->prev = head;
Aaron Durbin6086e632014-02-24 21:50:24 -0600226 first_segment = segment = &cbfs_payload->segments;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000227
228 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000229 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000230 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000231 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000232 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000233 segment++;
234 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000235
Ronald G. Minnichae631262009-04-01 10:48:39 +0000236 case PAYLOAD_SEGMENT_CODE:
237 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000238 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000239 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
240 ntohl(segment->compression));
241 new = malloc(sizeof(*new));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700242 new->s_dstaddr = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000243 new->s_memsz = ntohl(segment->mem_len);
244 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000245
Ronald G. Minnichef402092013-11-11 10:36:28 -0800246 new->s_srcaddr = (uintptr_t)
247 ((unsigned char *)first_segment)
248 + ntohl(segment->offset);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000249 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000250 printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000251 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
252 /* Clean up the values */
253 if (new->s_filesz > new->s_memsz) {
254 new->s_filesz = new->s_memsz;
Vadim Bendebury9e208bc2014-05-13 15:43:58 -0700255 printk(BIOS_DEBUG,
256 " cleaned up filesize 0x%lx\n",
257 new->s_filesz);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000258 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000259 break;
260
Ronald G. Minnichae631262009-04-01 10:48:39 +0000261 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700262 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
263 (intptr_t)ntohll(segment->load_addr),
264 ntohl(segment->mem_len));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000265 new = malloc(sizeof(*new));
266 new->s_filesz = 0;
Edward O'Callaghan4bab5822014-03-01 09:27:37 +1100267 new->s_srcaddr = (uintptr_t)
268 ((unsigned char *)first_segment)
269 + ntohl(segment->offset);
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700270 new->s_dstaddr = ntohll(segment->load_addr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000271 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000272 break;
273
274 case PAYLOAD_SEGMENT_ENTRY:
Hung-Te Linfdfd89f2013-02-27 16:38:38 +0800275 printk(BIOS_DEBUG, " Entry Point 0x%p\n",
276 (void *)(intptr_t)ntohll(segment->load_addr));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700277 *entry = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000278 /* Per definition, a payload always has the entry point
Martin Rothcbf2bd72013-07-09 21:51:14 -0600279 * as last segment. Thus, we use the occurrence of the
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000280 * entry point as break condition for the loop.
281 * Can we actually just look at the number of section?
282 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000283 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000284
285 default:
286 /* We found something that we don't know about. Throw
287 * hands into the sky and run away!
288 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000289 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000290 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000291 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000292
Stefan Reinauerc44de492012-01-11 14:07:39 -0800293 /* We have found another CODE, DATA or BSS segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000294 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000295
Stefan Reinauerc44de492012-01-11 14:07:39 -0800296 /* Find place where to insert our segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000297 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700298 if (new->s_srcaddr < ntohll(segment->load_addr))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000299 break;
300 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000301
Ronald G. Minnichae631262009-04-01 10:48:39 +0000302 /* Order by stream offset */
303 new->next = ptr;
304 new->prev = ptr->prev;
305 ptr->prev->next = new;
306 ptr->prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000307 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000308
Ronald G. Minnichae631262009-04-01 10:48:39 +0000309 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000310}
311
312static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000313 struct segment *head,
Aaron Durbin6086e632014-02-24 21:50:24 -0600314 struct payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000315{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000316 struct segment *ptr;
Aaron Durbinceebc052014-02-25 00:21:10 -0600317 const unsigned long one_meg = (1UL << 20);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000318 unsigned long bounce_high = lb_end;
Aaron Durbinceebc052014-02-25 00:21:10 -0600319
Patrick Georgi5eceb322009-05-13 16:27:25 +0000320 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Aaron Durbinceebc052014-02-25 00:21:10 -0600321 if (bootmem_region_targets_usable_ram(ptr->s_dstaddr,
322 ptr->s_memsz))
323 continue;
324
325 if (ptr->s_dstaddr < one_meg &&
326 (ptr->s_dstaddr + ptr->s_memsz) <= one_meg) {
327 printk(BIOS_DEBUG,
328 "Payload being loaded below 1MiB "
329 "without region being marked as RAM usable.\n");
330 continue;
331 }
332
333 /* Payload segment not targeting RAM. */
334 printk(BIOS_ERR, "SELF Payload doesn't target RAM:\n");
335 printk(BIOS_ERR, "Failed Segment: 0x%lx, %lu bytes\n",
336 ptr->s_dstaddr, ptr->s_memsz);
337 bootmem_dump_ranges();
338 return 0;
339 }
340
341 for(ptr = head->next; ptr != head; ptr = ptr->next) {
342 /*
343 * Add segments to bootmem memory map before a bounce buffer is
344 * allocated so that there aren't conflicts with the actual
345 * payload.
346 */
347 bootmem_add_range(ptr->s_dstaddr, ptr->s_memsz,
348 LB_MEM_UNUSABLE);
349
Stefan Reinauerc44de492012-01-11 14:07:39 -0800350 if (!overlaps_coreboot(ptr))
351 continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000352 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
353 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000354 }
Aaron Durbinceebc052014-02-25 00:21:10 -0600355 get_bounce_buffer(bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000356 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000357 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000358 return 0;
359 }
Aaron Durbine58a24b2014-02-24 22:11:45 -0600360
361 /* Update the payload's bounce buffer data used when loading. */
362 payload->bounce.data = (void *)(uintptr_t)bounce_buffer;
363 payload->bounce.size = bounce_size;
364
Patrick Georgi5eceb322009-05-13 16:27:25 +0000365 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000366 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000367 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000368 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000369
Patrick Georgi5eceb322009-05-13 16:27:25 +0000370 /* Modify the segment to load onto the bounce_buffer if necessary.
371 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000372 if (relocate_segment(bounce_buffer, ptr)) {
373 ptr = (ptr->prev)->prev;
374 continue;
375 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000376
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000377 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000378 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
379
Ronald G. Minnichae631262009-04-01 10:48:39 +0000380 /* Compute the boundaries of the segment */
381 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000382 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000383
Ronald G. Minnichae631262009-04-01 10:48:39 +0000384 /* Copy data from the initial buffer */
385 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000386 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000387 size_t len;
388 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000389 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000390 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000391 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000392 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000393 if (!len) /* Decompression Error. */
394 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000395 break;
396 }
Patrick Georgi369bc782009-04-25 07:32:24 +0000397 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000398 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000399 memcpy(dest, src, len);
400 break;
401 }
402 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000403 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000404 return -1;
405 }
406 end = dest + ptr->s_memsz;
407 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000408 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000409 (unsigned long)dest,
410 (unsigned long)middle,
411 (unsigned long)end,
412 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000413
414 /* Zero the extra bytes between middle & end */
415 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000416 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000417 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000418
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000419 /* Zero the extra bytes */
420 memset(middle, 0, end - middle);
421 }
Furquan Shaikh20f25dd2014-04-22 10:41:05 -0700422 /* Copy the data that's outside the area that shadows ramstage */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000423 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000424 if ((unsigned long)end > bounce_buffer) {
425 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000426 unsigned char *from = dest;
427 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000428 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000429 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000430 memcpy(to, from, amount);
431 }
432 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
433 unsigned long from = bounce_buffer + (lb_end - lb_start);
434 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000435 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000436 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000437 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000438 }
439 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000440 }
441 }
442 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000443}
444
Aaron Durbinceebc052014-02-25 00:21:10 -0600445void *selfload(struct payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000446{
Ronald G. Minnichef402092013-11-11 10:36:28 -0800447 uintptr_t entry = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000448 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000449
450 /* Preprocess the self segments */
Aaron Durbinceebc052014-02-25 00:21:10 -0600451 if (!build_self_segment_list(&head, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000452 goto out;
453
454 /* Load the segments */
Aaron Durbinceebc052014-02-25 00:21:10 -0600455 if (!load_self_segments(&head, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000456 goto out;
457
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000458 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000459
Aaron Durbin001de1a2013-04-24 22:59:45 -0500460 return (void *)entry;
461
462out:
463 return NULL;
464}