2 * This file is part of the coreboot project.
4 * Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com>
5 * Copyright (C) 2009 Ron Minnich <rminnich@gmail.com>
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.
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.
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
21 #include <arch/byteorder.h>
22 #include <console/console.h>
25 #include <boot/elf_boot.h>
26 #include <boot/coreboot_tables.h>
33 /* Maximum physical address we can use for the coreboot bounce buffer. */
38 /* from coreboot_ram.ld: */
39 extern unsigned char _ram_seg;
40 extern unsigned char _eram_seg;
42 static const unsigned long lb_start = (unsigned long)&_ram_seg;
43 static const unsigned long lb_end = (unsigned long)&_eram_seg;
48 unsigned long s_dstaddr;
49 unsigned long s_srcaddr;
50 unsigned long s_memsz;
51 unsigned long s_filesz;
56 * Static executables all want to share the same addresses
57 * in memory because only a few addresses are reliably present on
58 * a machine, and implementing general relocation is hard.
61 * - Allocate a buffer the size of the coreboot image plus additional
63 * - Anything that would overwrite coreboot copy into the lower part of
65 * - After loading an ELF image copy coreboot to the top of the buffer.
66 * - Then jump to the loaded image.
69 * - Nearly arbitrary standalone executables can be loaded.
70 * - Coreboot is preserved, so it can be returned to.
71 * - The implementation is still relatively simple,
72 * and much simpler than the general case implemented in kexec.
75 static unsigned long bounce_size, bounce_buffer;
77 static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
79 unsigned long lb_size;
80 unsigned long mem_entries;
83 lb_size = lb_end - lb_start;
84 /* Plus coreboot size so I have somewhere
85 * to place a copy to return to.
87 lb_size = req_size + lb_size;
88 mem_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]);
90 for(i = 0; i < mem_entries; i++) {
91 unsigned long mstart, mend;
93 unsigned long tbuffer;
94 if (mem->map[i].type != LB_MEM_RAM)
96 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
98 if (unpack_lb64(mem->map[i].size) < lb_size)
100 mstart = unpack_lb64(mem->map[i].start);
101 msize = MAX_ADDR - mstart +1;
102 if (msize > unpack_lb64(mem->map[i].size))
103 msize = unpack_lb64(mem->map[i].size);
104 mend = mstart + msize;
105 tbuffer = mend - lb_size;
106 if (tbuffer < buffer)
110 bounce_buffer = buffer;
111 bounce_size = req_size;
114 static int valid_area(struct lb_memory *mem, unsigned long buffer,
115 unsigned long start, unsigned long len)
117 /* Check through all of the memory segments and ensure
118 * the segment that was passed in is completely contained
122 unsigned long end = start + len;
123 unsigned long mem_entries = (mem->size - sizeof(*mem)) /
126 /* See if I conflict with the bounce buffer */
131 /* Walk through the table of valid memory ranges and see if I
134 for(i = 0; i < mem_entries; i++) {
135 uint64_t mstart, mend;
137 mtype = mem->map[i].type;
138 mstart = unpack_lb64(mem->map[i].start);
139 mend = mstart + unpack_lb64(mem->map[i].size);
140 if ((mtype == LB_MEM_RAM) && (start >= mstart) && (end < mend)) {
143 if ((mtype == LB_MEM_TABLE) && (start >= mstart) && (end < mend)) {
144 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
148 if (i == mem_entries) {
149 if (start < (1024*1024) && end <=(1024*1024)) {
150 printk(BIOS_DEBUG, "Payload (probably SeaBIOS) loaded"
151 " into a reserved area in the lower 1MB\n");
154 printk(BIOS_ERR, "No matching ram area found for range:\n");
155 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
156 printk(BIOS_ERR, "Ram areas\n");
157 for(i = 0; i < mem_entries; i++) {
158 uint64_t mstart, mend;
160 mtype = mem->map[i].type;
161 mstart = unpack_lb64(mem->map[i].start);
162 mend = mstart + unpack_lb64(mem->map[i].size);
163 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
164 (unsigned long)mstart,
166 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
175 static int overlaps_coreboot(struct segment *seg)
177 unsigned long start, end;
178 start = seg->s_dstaddr;
179 end = start + seg->s_memsz;
180 return !((end <= lb_start) || (start >= lb_end));
183 static int relocate_segment(unsigned long buffer, struct segment *seg)
185 /* Modify all segments that want to load onto coreboot
186 * to load onto the bounce buffer instead.
188 /* ret: 1 : A new segment is inserted before the seg.
189 * 0 : A new segment is inserted after the seg, or no new one.
191 unsigned long start, middle, end, ret = 0;
193 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
196 /* I don't conflict with coreboot so get out of here */
197 if (!overlaps_coreboot(seg))
200 start = seg->s_dstaddr;
201 middle = start + seg->s_filesz;
202 end = start + seg->s_memsz;
204 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
207 if (seg->compression == CBFS_COMPRESS_NONE) {
208 /* Slice off a piece at the beginning
209 * that doesn't conflict with coreboot.
211 if (start < lb_start) {
213 unsigned long len = lb_start - start;
214 new = malloc(sizeof(*new));
218 seg->s_dstaddr += len;
219 seg->s_srcaddr += len;
220 if (seg->s_filesz > len) {
222 seg->s_filesz -= len;
227 /* Order by stream offset */
229 new->prev = seg->prev;
230 seg->prev->next = new;
233 /* compute the new value of start */
234 start = seg->s_dstaddr;
236 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
238 new->s_dstaddr + new->s_filesz,
239 new->s_dstaddr + new->s_memsz);
244 /* Slice off a piece at the end
245 * that doesn't conflict with coreboot
248 unsigned long len = lb_end - start;
250 new = malloc(sizeof(*new));
254 new->s_dstaddr += len;
255 new->s_srcaddr += len;
256 if (seg->s_filesz > len) {
258 new->s_filesz -= len;
262 /* Order by stream offset */
263 new->next = seg->next;
265 seg->next->prev = new;
268 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
270 new->s_dstaddr + new->s_filesz,
271 new->s_dstaddr + new->s_memsz);
275 /* Now retarget this segment onto the bounce buffer */
276 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
277 * so you will make the dstaddr be this buffer, and it will get copied
278 * later to where coreboot lives.
280 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
282 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
284 seg->s_dstaddr + seg->s_filesz,
285 seg->s_dstaddr + seg->s_memsz);
291 static int build_self_segment_list(
292 struct segment *head,
293 struct lb_memory *mem,
294 struct cbfs_payload *payload, u32 *entry)
298 struct cbfs_payload_segment *segment, *first_segment;
299 memset(head, 0, sizeof(*head));
300 head->next = head->prev = head;
301 first_segment = segment = &payload->segments;
304 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
305 switch(segment->type) {
306 case PAYLOAD_SEGMENT_PARAMS:
307 printk(BIOS_DEBUG, " parameter section (skipped)\n");
311 case PAYLOAD_SEGMENT_CODE:
312 case PAYLOAD_SEGMENT_DATA:
313 printk(BIOS_DEBUG, " %s (compression=%x)\n",
314 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
315 ntohl(segment->compression));
316 new = malloc(sizeof(*new));
317 new->s_dstaddr = ntohll(segment->load_addr);
318 new->s_memsz = ntohl(segment->mem_len);
319 new->compression = ntohl(segment->compression);
321 new->s_srcaddr = (u32) ((unsigned char *)first_segment)
322 + ntohl(segment->offset);
323 new->s_filesz = ntohl(segment->len);
324 printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
325 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
326 /* Clean up the values */
327 if (new->s_filesz > new->s_memsz) {
328 new->s_filesz = new->s_memsz;
330 printk(BIOS_DEBUG, " (cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n",
331 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
334 case PAYLOAD_SEGMENT_BSS:
335 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
336 (intptr_t)ntohll(segment->load_addr),
337 ntohl(segment->mem_len));
338 new = malloc(sizeof(*new));
340 new->s_dstaddr = ntohll(segment->load_addr);
341 new->s_memsz = ntohl(segment->mem_len);
344 case PAYLOAD_SEGMENT_ENTRY:
345 printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
346 *entry = ntohll(segment->load_addr);
347 /* Per definition, a payload always has the entry point
348 * as last segment. Thus, we use the occurence of the
349 * entry point as break condition for the loop.
350 * Can we actually just look at the number of section?
355 /* We found something that we don't know about. Throw
356 * hands into the sky and run away!
358 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
362 /* We have found another CODE, DATA or BSS segment */
365 /* Find place where to insert our segment */
366 for(ptr = head->next; ptr != head; ptr = ptr->next) {
367 if (new->s_srcaddr < ntohll(segment->load_addr))
371 /* Order by stream offset */
373 new->prev = ptr->prev;
374 ptr->prev->next = new;
381 static int load_self_segments(
382 struct segment *head,
383 struct lb_memory *mem,
384 struct cbfs_payload *payload)
388 unsigned long bounce_high = lb_end;
389 for(ptr = head->next; ptr != head; ptr = ptr->next) {
390 if (!overlaps_coreboot(ptr))
392 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
393 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
395 get_bounce_buffer(mem, bounce_high - lb_start);
396 if (!bounce_buffer) {
397 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
400 for(ptr = head->next; ptr != head; ptr = ptr->next) {
401 /* Verify the memory addresses in the segment are valid */
402 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
405 for(ptr = head->next; ptr != head; ptr = ptr->next) {
406 unsigned char *dest, *src;
407 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
408 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
410 /* Modify the segment to load onto the bounce_buffer if necessary.
412 if (relocate_segment(bounce_buffer, ptr)) {
413 ptr = (ptr->prev)->prev;
417 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
418 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
420 /* Compute the boundaries of the segment */
421 dest = (unsigned char *)(ptr->s_dstaddr);
422 src = (unsigned char *)(ptr->s_srcaddr);
424 /* Copy data from the initial buffer */
426 unsigned char *middle, *end;
429 switch(ptr->compression) {
430 case CBFS_COMPRESS_LZMA: {
431 printk(BIOS_DEBUG, "using LZMA\n");
432 len = ulzma(src, dest);
433 if (!len) /* Decompression Error. */
437 #if CONFIG_COMPRESSED_PAYLOAD_NRV2B
438 case CBFS_COMPRESS_NRV2B: {
439 printk(BIOS_DEBUG, "using NRV2B\n");
440 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
442 len = unrv2b(src, dest, &tmp);
446 case CBFS_COMPRESS_NONE: {
447 printk(BIOS_DEBUG, "it's not compressed!\n");
448 memcpy(dest, src, len);
452 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
455 end = dest + ptr->s_memsz;
457 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
459 (unsigned long)middle,
463 /* Zero the extra bytes between middle & end */
465 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
466 (unsigned long)middle, (unsigned long)(end - middle));
468 /* Zero the extra bytes */
469 memset(middle, 0, end - middle);
471 /* Copy the data that's outside the area that shadows coreboot_ram */
472 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
473 if ((unsigned long)end > bounce_buffer) {
474 if ((unsigned long)dest < bounce_buffer) {
475 unsigned char *from = dest;
476 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
477 unsigned long amount = bounce_buffer-(unsigned long)dest;
478 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
479 memcpy(to, from, amount);
481 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
482 unsigned long from = bounce_buffer + (lb_end - lb_start);
483 unsigned long to = lb_end;
484 unsigned long amount = (unsigned long)end - from;
485 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
486 memcpy((char*)to, (char*)from, amount);
494 static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
499 /* Preprocess the self segments */
500 if (!build_self_segment_list(&head, mem, payload, &entry))
503 /* Load the segments */
504 if (!load_self_segments(&head, mem, payload))
507 printk(BIOS_SPEW, "Loaded segments\n");
509 /* Reset to booting from this image as late as possible */
512 printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
513 post_code(POST_ENTER_ELF_BOOT);
516 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
523 void *cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
525 struct cbfs_payload *payload;
527 payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
530 printk(BIOS_DEBUG, "Got a payload\n");
532 selfboot(lb_mem, payload);
533 printk(BIOS_EMERG, "SELFBOOT RETURNED!\n");