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 <console/console.h>
22 #include <part/fallback_boot.h>
24 #include <boot/elf_boot.h>
25 #include <boot/coreboot_tables.h>
26 #include <ip_checksum.h>
27 #include <stream/read_bytes.h>
33 #ifndef CONFIG_BIG_ENDIAN
34 #define ntohl(x) ( ((x&0xff)<<24) | ((x&0xff00)<<8) | \
35 ((x&0xff0000) >> 8) | ((x&0xff000000) >> 24) )
40 /* Maximum physical address we can use for the coreboot bounce buffer.
46 extern unsigned char _ram_seg;
47 extern unsigned char _eram_seg;
52 struct segment *phdr_next;
53 struct segment *phdr_prev;
54 unsigned long s_dstaddr;
55 unsigned long s_srcaddr;
56 unsigned long s_memsz;
57 unsigned long s_filesz;
60 struct verify_callback {
61 struct verify_callback *next;
62 int (*callback)(struct verify_callback *vcb,
63 Elf_ehdr *ehdr, Elf_phdr *phdr, struct segment *head);
64 unsigned long desc_offset;
65 unsigned long desc_addr;
68 struct ip_checksum_vcb {
69 struct verify_callback data;
70 unsigned short ip_checksum;
73 int cbfs_self_decompress(int algo, void *src,struct segment *new)
77 /* for uncompressed, it's easy: just point at the area in ROM */
78 if (algo == CBFS_COMPRESS_NONE) {
79 new->s_srcaddr = (u32) src;
80 new->s_filesz = new->s_memsz;
84 /* for compression, let's keep it simple. We'll malloc the destination
85 * area and decompress to there. The compression overhead far outweighs
86 * any overhead for an extra copy.
88 dst = malloc(new->s_memsz);
93 #ifdef CONFIG_COMPRESSION_LZMA
94 case CBFS_COMPRESS_LZMA: {
95 unsigned long ulzma(unsigned char *src, unsigned char *dst);
100 #ifdef CONFIG_COMPRESSION_NRV2B
101 case CBFS_COMPRESS_NRV2B: {
102 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
104 unrv2b(src, dst, &tmp);
108 printk_info( "CBFS: Unknown compression type %d\n",
113 new->s_srcaddr = (u32) dst;
114 new->s_filesz = new->s_memsz;
120 * Static executables all want to share the same addresses
121 * in memory because only a few addresses are reliably present on
122 * a machine, and implementing general relocation is hard.
125 * - Allocate a buffer twice the size of the coreboot image.
126 * - Anything that would overwrite coreboot copy into the lower half of
128 * - After loading an ELF image copy coreboot to the upper half of the
130 * - Then jump to the loaded image.
133 * - Nearly arbitrary standalone executables can be loaded.
134 * - Coreboot is preserved, so it can be returned to.
135 * - The implementation is still relatively simple,
136 * and much simpler then the general case implemented in kexec.
140 static unsigned long get_bounce_buffer(struct lb_memory *mem)
142 unsigned long lb_size;
143 unsigned long mem_entries;
144 unsigned long buffer;
146 lb_size = (unsigned long)(&_eram_seg - &_ram_seg);
147 /* Double coreboot size so I have somewhere to place a copy to return to */
148 lb_size = lb_size + lb_size;
149 mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
151 for(i = 0; i < mem_entries; i++) {
152 unsigned long mstart, mend;
154 unsigned long tbuffer;
155 if (mem->map[i].type != LB_MEM_RAM)
157 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
159 if (unpack_lb64(mem->map[i].size) < lb_size)
161 mstart = unpack_lb64(mem->map[i].start);
162 msize = MAX_ADDR - mstart +1;
163 if (msize > unpack_lb64(mem->map[i].size))
164 msize = unpack_lb64(mem->map[i].size);
165 mend = mstart + msize;
166 tbuffer = mend - lb_size;
167 if (tbuffer < buffer)
174 static int valid_area(struct lb_memory *mem, unsigned long buffer,
175 unsigned long start, unsigned long len)
177 /* Check through all of the memory segments and ensure
178 * the segment that was passed in is completely contained
182 unsigned long end = start + len;
183 unsigned long mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
185 /* See if I conflict with the bounce buffer */
190 /* Walk through the table of valid memory ranges and see if I
193 for(i = 0; i < mem_entries; i++) {
194 uint64_t mstart, mend;
196 mtype = mem->map[i].type;
197 mstart = unpack_lb64(mem->map[i].start);
198 mend = mstart + unpack_lb64(mem->map[i].size);
199 if ((mtype == LB_MEM_RAM) && (start < mend) && (end > mstart)) {
202 if ((mtype == LB_MEM_TABLE) && (start < mend) && (end > mstart)) {
203 printk_err("Payload is overwriting Coreboot tables.\n");
207 if (i == mem_entries) {
208 printk_err("No matching ram area found for range:\n");
209 printk_err(" [0x%016lx, 0x%016lx)\n", start, end);
210 printk_err("Ram areas\n");
211 for(i = 0; i < mem_entries; i++) {
212 uint64_t mstart, mend;
214 mtype = mem->map[i].type;
215 mstart = unpack_lb64(mem->map[i].start);
216 mend = mstart + unpack_lb64(mem->map[i].size);
217 printk_err(" [0x%016lx, 0x%016lx) %s\n",
218 (unsigned long)mstart,
220 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
228 static void relocate_segment(unsigned long buffer, struct segment *seg)
230 /* Modify all segments that want to load onto coreboot
231 * to load onto the bounce buffer instead.
233 unsigned long lb_start = (unsigned long)&_ram_seg;
234 unsigned long lb_end = (unsigned long)&_eram_seg;
235 unsigned long start, middle, end;
237 printk_spew("lb: [0x%016lx, 0x%016lx)\n",
240 start = seg->s_dstaddr;
241 middle = start + seg->s_filesz;
242 end = start + seg->s_memsz;
243 /* I don't conflict with coreboot so get out of here */
244 if ((end <= lb_start) || (start >= lb_end))
247 printk_spew("segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
250 /* Slice off a piece at the beginning
251 * that doesn't conflict with coreboot.
253 if (start < lb_start) {
255 unsigned long len = lb_start - start;
256 new = malloc(sizeof(*new));
260 seg->s_dstaddr += len;
261 seg->s_srcaddr += len;
262 if (seg->s_filesz > len) {
264 seg->s_filesz -= len;
269 /* Order by stream offset */
271 new->prev = seg->prev;
272 seg->prev->next = new;
274 /* Order by original program header order */
275 new->phdr_next = seg;
276 new->phdr_prev = seg->phdr_prev;
277 seg->phdr_prev->phdr_next = new;
278 seg->phdr_prev = new;
280 /* compute the new value of start */
281 start = seg->s_dstaddr;
283 printk_spew(" early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
285 new->s_dstaddr + new->s_filesz,
286 new->s_dstaddr + new->s_memsz);
289 /* Slice off a piece at the end
290 * that doesn't conflict with coreboot
293 unsigned long len = lb_end - start;
295 new = malloc(sizeof(*new));
299 new->s_dstaddr += len;
300 new->s_srcaddr += len;
301 if (seg->s_filesz > len) {
303 new->s_filesz -= len;
307 /* Order by stream offset */
308 new->next = seg->next;
310 seg->next->prev = new;
312 /* Order by original program header order */
313 new->phdr_next = seg->phdr_next;
314 new->phdr_prev = seg;
315 seg->phdr_next->phdr_prev = new;
316 seg->phdr_next = new;
318 /* compute the new value of end */
321 printk_spew(" late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
323 new->s_dstaddr + new->s_filesz,
324 new->s_dstaddr + new->s_memsz);
327 /* Now retarget this segment onto the bounce buffer */
328 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
329 * so you will make the dstaddr be this buffer, and it will get copied
330 * later to where coreboot lives.
332 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
334 printk_spew(" bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
336 seg->s_dstaddr + seg->s_filesz,
337 seg->s_dstaddr + seg->s_memsz);
341 static int build_self_segment_list(
342 struct segment *head,
343 unsigned long bounce_buffer, struct lb_memory *mem,
344 struct cbfs_payload *payload, u32 *entry)
350 struct cbfs_payload_segment *segment, *first_segment;
351 memset(head, 0, sizeof(*head));
352 head->phdr_next = head->phdr_prev = head;
353 head->next = head->prev = head;
354 first_segment = segment = &payload->segments;
357 printk_debug("Segment %p\n", segment);
358 switch(segment->type) {
359 default: printk_emerg("Bad segment type %x\n", segment->type);
361 case PAYLOAD_SEGMENT_PARAMS:
362 printk_info("found param section\n");
365 case PAYLOAD_SEGMENT_CODE:
366 case PAYLOAD_SEGMENT_DATA:
367 printk_info( "%s: ", segment->type == PAYLOAD_SEGMENT_CODE ?
369 new = malloc(sizeof(*new));
370 new->s_dstaddr = ntohl((u32) segment->load_addr);
371 new->s_memsz = ntohl(segment->mem_len);
373 datasize = ntohl(segment->len);
374 /* figure out decompression, do it, get pointer to the area */
375 if (cbfs_self_decompress(ntohl(segment->compression),
376 ((unsigned char *) first_segment) +
377 ntohl(segment->offset), new)) {
378 printk_emerg("cbfs_self_decompress failed\n");
381 printk_debug("New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
382 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
383 /* Clean up the values */
384 if (new->s_filesz > new->s_memsz) {
385 new->s_filesz = new->s_memsz;
387 printk_debug("(cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n",
388 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
390 case PAYLOAD_SEGMENT_BSS:
391 printk_info("BSS %p/%d\n", (void *) ntohl((u32) segment->load_addr),
392 ntohl(segment->mem_len));
393 new = malloc(sizeof(*new));
395 new->s_dstaddr = ntohl((u32) segment->load_addr);
396 new->s_memsz = ntohl(segment->mem_len);
400 case PAYLOAD_SEGMENT_ENTRY:
401 printk_info("Entry %p\n", (void *) ntohl((u32) segment->load_addr));
402 *entry = (void *) ntohl((u32) segment->load_addr);
406 for(ptr = head->next; ptr != head; ptr = ptr->next) {
407 if (new->s_srcaddr < ntohl((u32) segment->load_addr))
410 /* Order by stream offset */
412 new->prev = ptr->prev;
413 ptr->prev->next = new;
415 /* Order by original program header order */
416 new->phdr_next = head;
417 new->phdr_prev = head->phdr_prev;
418 head->phdr_prev->phdr_next = new;
419 head->phdr_prev = new;
421 /* Verify the memory addresses in the segment are valid */
422 if (!valid_area(mem, bounce_buffer, new->s_dstaddr, new->s_memsz))
425 /* Modify the segment to load onto the bounce_buffer if necessary.
427 relocate_segment(bounce_buffer, new);
434 static int load_self_segments(
435 struct segment *head, struct cbfs_payload *payload)
437 unsigned long offset;
441 for(ptr = head->next; ptr != head; ptr = ptr->next) {
442 unsigned long skip_bytes, read_bytes;
443 unsigned char *dest, *middle, *end, *src;
444 byte_offset_t result;
445 printk_debug("Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
446 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
448 /* Compute the boundaries of the segment */
449 dest = (unsigned char *)(ptr->s_dstaddr);
450 end = dest + ptr->s_memsz;
451 middle = dest + ptr->s_filesz;
452 src = ptr->s_srcaddr;
453 printk_spew("[ 0x%016lx, %016lx, 0x%016lx) <- %016lx\n",
455 (unsigned long)middle,
459 /* Copy data from the initial buffer */
463 memcpy(dest, src, len);
467 /* Zero the extra bytes between middle & end */
469 printk_debug("Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
470 (unsigned long)middle, (unsigned long)(end - middle));
472 /* Zero the extra bytes */
473 memset(middle, 0, end - middle);
481 int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
485 unsigned long bounce_buffer;
487 /* Find a bounce buffer so I can load to coreboot's current location */
488 bounce_buffer = get_bounce_buffer(mem);
489 if (!bounce_buffer) {
490 printk_err("Could not find a bounce buffer...\n");
494 /* Preprocess the self segments */
495 if (!build_self_segment_list(&head, bounce_buffer, mem, payload, &entry))
498 /* Load the segments */
499 if (!load_self_segments(&head, payload))
502 printk_spew("Loaded segments\n");
504 /* Reset to booting from this image as late as possible */
507 printk_debug("Jumping to boot code at %p\n", entry);
511 jmp_to_elf_entry(entry, bounce_buffer);