use byteorder.h instead of implementing another byte swap function
[coreboot.git] / src / boot / selfboot.c
1 /*
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
21 #include <arch/byteorder.h>
22 #include <console/console.h>
23 #include <fallback.h>
24 #include <boot/elf.h>
25 #include <boot/elf_boot.h>
26 #include <boot/coreboot_tables.h>
27 #include <ip_checksum.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <cbfs.h>
32 #include <lib.h>
33
34 /* Maximum physical address we can use for the coreboot bounce buffer.
35  */
36 #ifndef MAX_ADDR
37 #define MAX_ADDR -1UL
38 #endif
39
40 extern unsigned char _ram_seg;
41 extern unsigned char _eram_seg;
42
43 struct segment {
44         struct segment *next;
45         struct segment *prev;
46         struct segment *phdr_next;
47         struct segment *phdr_prev;
48         unsigned long s_dstaddr;
49         unsigned long s_srcaddr;
50         unsigned long s_memsz;
51         unsigned long s_filesz;
52         int compression;
53 };
54
55 struct verify_callback {
56         struct verify_callback *next;
57         int (*callback)(struct verify_callback *vcb,
58                 Elf_ehdr *ehdr, Elf_phdr *phdr, struct segment *head);
59         unsigned long desc_offset;
60         unsigned long desc_addr;
61 };
62
63 struct ip_checksum_vcb {
64         struct verify_callback data;
65         unsigned short ip_checksum;
66 };
67
68 static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload);
69
70 void * cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
71 {
72         struct cbfs_payload *payload;
73
74         payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
75         if (payload == NULL)
76                 return (void *) -1;
77         printk(BIOS_DEBUG, "Got a payload\n");
78
79         selfboot(lb_mem, payload);
80         printk(BIOS_EMERG, "SELFBOOT RETURNED!\n");
81
82         return (void *) -1;
83 }
84
85 /* The problem:
86  * Static executables all want to share the same addresses
87  * in memory because only a few addresses are reliably present on
88  * a machine, and implementing general relocation is hard.
89  *
90  * The solution:
91  * - Allocate a buffer the size of the coreboot image plus additional
92  *   required space.
93  * - Anything that would overwrite coreboot copy into the lower part of
94  *   the buffer.
95  * - After loading an ELF image copy coreboot to the top of the buffer.
96  * - Then jump to the loaded image.
97  *
98  * Benefits:
99  * - Nearly arbitrary standalone executables can be loaded.
100  * - Coreboot is preserved, so it can be returned to.
101  * - The implementation is still relatively simple,
102  *   and much simpler than the general case implemented in kexec.
103  *
104  */
105
106 static unsigned long bounce_size, bounce_buffer;
107
108 static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
109 {
110         unsigned long lb_size;
111         unsigned long mem_entries;
112         unsigned long buffer;
113         int i;
114         lb_size = (unsigned long)(&_eram_seg - &_ram_seg);
115         /* Double coreboot size so I have somewhere to place a copy to return to */
116         lb_size = req_size + lb_size;
117         mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
118         buffer = 0;
119         for(i = 0; i < mem_entries; i++) {
120                 unsigned long mstart, mend;
121                 unsigned long msize;
122                 unsigned long tbuffer;
123                 if (mem->map[i].type != LB_MEM_RAM)
124                         continue;
125                 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
126                         continue;
127                 if (unpack_lb64(mem->map[i].size) < lb_size)
128                         continue;
129                 mstart = unpack_lb64(mem->map[i].start);
130                 msize = MAX_ADDR - mstart +1;
131                 if (msize > unpack_lb64(mem->map[i].size))
132                         msize = unpack_lb64(mem->map[i].size);
133                 mend = mstart + msize;
134                 tbuffer = mend - lb_size;
135                 if (tbuffer < buffer)
136                         continue;
137                 buffer = tbuffer;
138         }
139         bounce_buffer = buffer;
140         bounce_size = req_size;
141 }
142
143 static int valid_area(struct lb_memory *mem, unsigned long buffer,
144         unsigned long start, unsigned long len)
145 {
146         /* Check through all of the memory segments and ensure
147          * the segment that was passed in is completely contained
148          * in RAM.
149          */
150         int i;
151         unsigned long end = start + len;
152         unsigned long mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
153
154         /* See if I conflict with the bounce buffer */
155         if (end >= buffer) {
156                 return 0;
157         }
158
159         /* Walk through the table of valid memory ranges and see if I
160          * have a match.
161          */
162         for(i = 0; i < mem_entries; i++) {
163                 uint64_t mstart, mend;
164                 uint32_t mtype;
165                 mtype = mem->map[i].type;
166                 mstart = unpack_lb64(mem->map[i].start);
167                 mend = mstart + unpack_lb64(mem->map[i].size);
168                 if ((mtype == LB_MEM_RAM) && (start < mend) && (end > mstart)) {
169                         break;
170                 }
171                 if ((mtype == LB_MEM_TABLE) && (start < mend) && (end > mstart)) {
172                         printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
173                         break;
174                 }
175         }
176         if (i == mem_entries) {
177                 printk(BIOS_ERR, "No matching ram area found for range:\n");
178                 printk(BIOS_ERR, "  [0x%016lx, 0x%016lx)\n", start, end);
179                 printk(BIOS_ERR, "Ram areas\n");
180                 for(i = 0; i < mem_entries; i++) {
181                         uint64_t mstart, mend;
182                         uint32_t mtype;
183                         mtype = mem->map[i].type;
184                         mstart = unpack_lb64(mem->map[i].start);
185                         mend = mstart + unpack_lb64(mem->map[i].size);
186                         printk(BIOS_ERR, "  [0x%016lx, 0x%016lx) %s\n",
187                                 (unsigned long)mstart,
188                                 (unsigned long)mend,
189                                 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
190
191                 }
192                 return 0;
193         }
194         return 1;
195 }
196
197 static const unsigned long lb_start = (unsigned long)&_ram_seg;
198 static const unsigned long lb_end = (unsigned long)&_eram_seg;
199
200 static int overlaps_coreboot(struct segment *seg)
201 {
202         unsigned long start, end;
203         start = seg->s_dstaddr;
204         end = start + seg->s_memsz;
205         return !((end <= lb_start) || (start >= lb_end));
206 }
207
208 static int relocate_segment(unsigned long buffer, struct segment *seg)
209 {
210         /* Modify all segments that want to load onto coreboot
211          * to load onto the bounce buffer instead.
212          */
213         /* ret:  1 : A new segment is inserted before the seg.
214          *       0 : A new segment is inserted after the seg, or no new one. */
215         unsigned long start, middle, end, ret = 0;
216
217         printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
218                 lb_start, lb_end);
219
220         /* I don't conflict with coreboot so get out of here */
221         if (!overlaps_coreboot(seg))
222                 return 0;
223
224         start = seg->s_dstaddr;
225         middle = start + seg->s_filesz;
226         end = start + seg->s_memsz;
227
228         printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
229                 start, middle, end);
230
231         if (seg->compression == CBFS_COMPRESS_NONE) {
232                 /* Slice off a piece at the beginning
233                  * that doesn't conflict with coreboot.
234                  */
235                 if (start < lb_start) {
236                         struct segment *new;
237                         unsigned long len = lb_start - start;
238                         new = malloc(sizeof(*new));
239                         *new = *seg;
240                         new->s_memsz = len;
241                         seg->s_memsz -= len;
242                         seg->s_dstaddr += len;
243                         seg->s_srcaddr += len;
244                         if (seg->s_filesz > len) {
245                                 new->s_filesz = len;
246                                 seg->s_filesz -= len;
247                         } else {
248                                 seg->s_filesz = 0;
249                         }
250
251                         /* Order by stream offset */
252                         new->next = seg;
253                         new->prev = seg->prev;
254                         seg->prev->next = new;
255                         seg->prev = new;
256                         /* Order by original program header order */
257                         new->phdr_next = seg;
258                         new->phdr_prev = seg->phdr_prev;
259                         seg->phdr_prev->phdr_next = new;
260                         seg->phdr_prev = new;
261
262                         /* compute the new value of start */
263                         start = seg->s_dstaddr;
264
265                         printk(BIOS_SPEW, "   early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
266                                 new->s_dstaddr,
267                                 new->s_dstaddr + new->s_filesz,
268                                 new->s_dstaddr + new->s_memsz);
269
270                         ret = 1;
271                 }
272
273                 /* Slice off a piece at the end
274                  * that doesn't conflict with coreboot
275                  */
276                 if (end > lb_end) {
277                         unsigned long len = lb_end - start;
278                         struct segment *new;
279                         new = malloc(sizeof(*new));
280                         *new = *seg;
281                         seg->s_memsz = len;
282                         new->s_memsz -= len;
283                         new->s_dstaddr += len;
284                         new->s_srcaddr += len;
285                         if (seg->s_filesz > len) {
286                                 seg->s_filesz = len;
287                                 new->s_filesz -= len;
288                         } else {
289                                 new->s_filesz = 0;
290                         }
291                         /* Order by stream offset */
292                         new->next = seg->next;
293                         new->prev = seg;
294                         seg->next->prev = new;
295                         seg->next = new;
296                         /* Order by original program header order */
297                         new->phdr_next = seg->phdr_next;
298                         new->phdr_prev = seg;
299                         seg->phdr_next->phdr_prev = new;
300                         seg->phdr_next = new;
301
302                         printk(BIOS_SPEW, "   late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
303                                 new->s_dstaddr,
304                                 new->s_dstaddr + new->s_filesz,
305                                 new->s_dstaddr + new->s_memsz);
306                 }
307         }
308
309         /* Now retarget this segment onto the bounce buffer */
310         /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
311          * so you will make the dstaddr be this buffer, and it will get copied
312          * later to where coreboot lives.
313          */
314         seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
315
316         printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
317                 seg->s_dstaddr,
318                 seg->s_dstaddr + seg->s_filesz,
319                 seg->s_dstaddr + seg->s_memsz);
320
321         return ret;
322 }
323
324
325 static int build_self_segment_list(
326         struct segment *head,
327         struct lb_memory *mem,
328         struct cbfs_payload *payload, u32 *entry)
329 {
330         struct segment *new;
331         struct segment *ptr;
332         struct cbfs_payload_segment *segment, *first_segment;
333         memset(head, 0, sizeof(*head));
334         head->phdr_next = head->phdr_prev = head;
335         head->next = head->prev = head;
336         first_segment = segment = &payload->segments;
337
338         while(1) {
339                 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
340                 switch(segment->type) {
341                 case PAYLOAD_SEGMENT_PARAMS:
342                         printk(BIOS_DEBUG, "  parameter section (skipped)\n");
343                         segment++;
344                         continue;
345
346                 case PAYLOAD_SEGMENT_CODE:
347                 case PAYLOAD_SEGMENT_DATA:
348                         printk(BIOS_DEBUG, "  %s (compression=%x)\n",
349                                         segment->type == PAYLOAD_SEGMENT_CODE ?  "code" : "data",
350                                         ntohl(segment->compression));
351                         new = malloc(sizeof(*new));
352                         new->s_dstaddr = ntohl((u32) segment->load_addr);
353                         new->s_memsz = ntohl(segment->mem_len);
354                         new->compression = ntohl(segment->compression);
355
356                         new->s_srcaddr = (u32) ((unsigned char *) first_segment) + ntohl(segment->offset);
357                         new->s_filesz = ntohl(segment->len);
358                         printk(BIOS_DEBUG, "  New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
359                                 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
360                         /* Clean up the values */
361                         if (new->s_filesz > new->s_memsz)  {
362                                 new->s_filesz = new->s_memsz;
363                         }
364                         printk(BIOS_DEBUG, "  (cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n",
365                                 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
366                         break;
367
368                 case PAYLOAD_SEGMENT_BSS:
369                         printk(BIOS_DEBUG, "  BSS 0x%p (%d byte)\n", (void *) ntohl((u32) segment->load_addr),
370                                  ntohl(segment->mem_len));
371                         new = malloc(sizeof(*new));
372                         new->s_filesz = 0;
373                         new->s_dstaddr = ntohl((u32) segment->load_addr);
374                         new->s_memsz = ntohl(segment->mem_len);
375                         break;
376
377                 case PAYLOAD_SEGMENT_ENTRY:
378                         printk(BIOS_DEBUG, "  Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
379                         *entry =  ntohl((u32) segment->load_addr);
380                         /* Per definition, a payload always has the entry point
381                          * as last segment. Thus, we use the occurence of the
382                          * entry point as break condition for the loop.
383                          * Can we actually just look at the number of section?
384                          */
385                         return 1;
386
387                 default:
388                         /* We found something that we don't know about. Throw
389                          * hands into the sky and run away!
390                          */
391                         printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
392                         return -1;
393                 }
394
395                 segment++;
396
397                 // FIXME: Explain what this is
398                 for(ptr = head->next; ptr != head; ptr = ptr->next) {
399                         if (new->s_srcaddr < ntohl((u32) segment->load_addr))
400                                 break;
401                 }
402
403                 /* Order by stream offset */
404                 new->next = ptr;
405                 new->prev = ptr->prev;
406                 ptr->prev->next = new;
407                 ptr->prev = new;
408
409                 /* Order by original program header order */
410                 new->phdr_next = head;
411                 new->phdr_prev = head->phdr_prev;
412                 head->phdr_prev->phdr_next  = new;
413                 head->phdr_prev = new;
414         }
415
416         return 1;
417 }
418
419 static int load_self_segments(
420         struct segment *head,
421         struct lb_memory *mem,
422         struct cbfs_payload *payload)
423 {
424         struct segment *ptr;
425
426         unsigned long bounce_high = lb_end;
427         for(ptr = head->next; ptr != head; ptr = ptr->next) {
428                 if (!overlaps_coreboot(ptr)) continue;
429                 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
430                         bounce_high = ptr->s_dstaddr + ptr->s_memsz;
431         }
432         get_bounce_buffer(mem, bounce_high - lb_start);
433         if (!bounce_buffer) {
434                 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
435                 return 0;
436         }
437         for(ptr = head->next; ptr != head; ptr = ptr->next) {
438                 /* Verify the memory addresses in the segment are valid */
439                 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
440                         return 0;
441         }
442         for(ptr = head->next; ptr != head; ptr = ptr->next) {
443                 unsigned char *dest, *src;
444                 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
445                         ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
446
447                 /* Modify the segment to load onto the bounce_buffer if necessary.
448                  */
449                 if (relocate_segment(bounce_buffer, ptr)) {
450                         ptr = (ptr->prev)->prev;
451                         continue;
452                 }
453
454                 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
455                         ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
456
457                 /* Compute the boundaries of the segment */
458                 dest = (unsigned char *)(ptr->s_dstaddr);
459                 src = (unsigned char *)(ptr->s_srcaddr);
460
461                 /* Copy data from the initial buffer */
462                 if (ptr->s_filesz) {
463                         unsigned char *middle, *end;
464                         size_t len;
465                         len = ptr->s_filesz;
466                         switch(ptr->compression) {
467                                 case CBFS_COMPRESS_LZMA: {
468                                         printk(BIOS_DEBUG, "using LZMA\n");
469                                         len = ulzma(src, dest);
470                                         if (!len) /* Decompression Error. */
471                                                 return 0;
472                                         break;
473                                 }
474 #if CONFIG_COMPRESSED_PAYLOAD_NRV2B
475                                 case CBFS_COMPRESS_NRV2B: {
476                                         printk(BIOS_DEBUG, "using NRV2B\n");
477                                         unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
478                                         unsigned long tmp;
479                                         len = unrv2b(src, dest, &tmp);
480                                         break;
481                                 }
482 #endif
483                                 case CBFS_COMPRESS_NONE: {
484                                         printk(BIOS_DEBUG, "it's not compressed!\n");
485                                         memcpy(dest, src, len);
486                                         break;
487                                 }
488                                 default:
489                                         printk(BIOS_INFO,  "CBFS:  Unknown compression type %d\n", ptr->compression);
490                                         return -1;
491                         }
492                         end = dest + ptr->s_memsz;
493                         middle = dest + len;
494                         printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
495                                 (unsigned long)dest,
496                                 (unsigned long)middle,
497                                 (unsigned long)end,
498                                 (unsigned long)src);
499
500                         /* Zero the extra bytes between middle & end */
501                         if (middle < end) {
502                                 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
503                                         (unsigned long)middle, (unsigned long)(end - middle));
504
505                                 /* Zero the extra bytes */
506                                 memset(middle, 0, end - middle);
507                         }
508                         /* Copy the data that's outside the area that shadows coreboot_ram */
509                         printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
510                         if ((unsigned long)end > bounce_buffer) {
511                                 if ((unsigned long)dest < bounce_buffer) {
512                                         unsigned char *from = dest;
513                                         unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
514                                         unsigned long amount = bounce_buffer-(unsigned long)dest;
515                                         printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
516                                         memcpy(to, from, amount);
517                                 }
518                                 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
519                                         unsigned long from = bounce_buffer + (lb_end - lb_start);
520                                         unsigned long to = lb_end;
521                                         unsigned long amount = (unsigned long)end - from;
522                                         printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
523                                         memcpy((char*)to, (char*)from, amount);
524                                 }
525                         }
526                 }
527         }
528         return 1;
529 }
530
531 static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
532 {
533         u32 entry=0;
534         struct segment head;
535
536         /* Preprocess the self segments */
537         if (!build_self_segment_list(&head, mem, payload, &entry))
538                 goto out;
539
540         /* Load the segments */
541         if (!load_self_segments(&head, mem, payload))
542                 goto out;
543
544         printk(BIOS_SPEW, "Loaded segments\n");
545
546         /* Reset to booting from this image as late as possible */
547         boot_successful();
548
549         printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
550         post_code(POST_ENTER_ELF_BOOT);
551
552         /* Jump to kernel */
553         jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
554         return 1;
555
556  out:
557         return 0;
558 }
559