Use ntohll where appropriate.
[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 = ntohll(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)
357                                                 + ntohl(segment->offset);
358                         new->s_filesz = ntohl(segment->len);
359                         printk(BIOS_DEBUG, "  New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
360                                 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
361                         /* Clean up the values */
362                         if (new->s_filesz > new->s_memsz)  {
363                                 new->s_filesz = new->s_memsz;
364                         }
365                         printk(BIOS_DEBUG, "  (cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n",
366                                 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
367                         break;
368
369                 case PAYLOAD_SEGMENT_BSS:
370                         printk(BIOS_DEBUG, "  BSS 0x%p (%d byte)\n", (void *)
371                                         (intptr_t)ntohll(segment->load_addr),
372                                         ntohl(segment->mem_len));
373                         new = malloc(sizeof(*new));
374                         new->s_filesz = 0;
375                         new->s_dstaddr = ntohll(segment->load_addr);
376                         new->s_memsz = ntohl(segment->mem_len);
377                         break;
378
379                 case PAYLOAD_SEGMENT_ENTRY:
380                         printk(BIOS_DEBUG, "  Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
381                         *entry =  ntohll(segment->load_addr);
382                         /* Per definition, a payload always has the entry point
383                          * as last segment. Thus, we use the occurence of the
384                          * entry point as break condition for the loop.
385                          * Can we actually just look at the number of section?
386                          */
387                         return 1;
388
389                 default:
390                         /* We found something that we don't know about. Throw
391                          * hands into the sky and run away!
392                          */
393                         printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
394                         return -1;
395                 }
396
397                 segment++;
398
399                 // FIXME: Explain what this is
400                 for(ptr = head->next; ptr != head; ptr = ptr->next) {
401                         if (new->s_srcaddr < ntohll(segment->load_addr))
402                                 break;
403                 }
404
405                 /* Order by stream offset */
406                 new->next = ptr;
407                 new->prev = ptr->prev;
408                 ptr->prev->next = new;
409                 ptr->prev = new;
410
411                 /* Order by original program header order */
412                 new->phdr_next = head;
413                 new->phdr_prev = head->phdr_prev;
414                 head->phdr_prev->phdr_next  = new;
415                 head->phdr_prev = new;
416         }
417
418         return 1;
419 }
420
421 static int load_self_segments(
422         struct segment *head,
423         struct lb_memory *mem,
424         struct cbfs_payload *payload)
425 {
426         struct segment *ptr;
427
428         unsigned long bounce_high = lb_end;
429         for(ptr = head->next; ptr != head; ptr = ptr->next) {
430                 if (!overlaps_coreboot(ptr)) continue;
431                 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
432                         bounce_high = ptr->s_dstaddr + ptr->s_memsz;
433         }
434         get_bounce_buffer(mem, bounce_high - lb_start);
435         if (!bounce_buffer) {
436                 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
437                 return 0;
438         }
439         for(ptr = head->next; ptr != head; ptr = ptr->next) {
440                 /* Verify the memory addresses in the segment are valid */
441                 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
442                         return 0;
443         }
444         for(ptr = head->next; ptr != head; ptr = ptr->next) {
445                 unsigned char *dest, *src;
446                 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
447                         ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
448
449                 /* Modify the segment to load onto the bounce_buffer if necessary.
450                  */
451                 if (relocate_segment(bounce_buffer, ptr)) {
452                         ptr = (ptr->prev)->prev;
453                         continue;
454                 }
455
456                 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
457                         ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
458
459                 /* Compute the boundaries of the segment */
460                 dest = (unsigned char *)(ptr->s_dstaddr);
461                 src = (unsigned char *)(ptr->s_srcaddr);
462
463                 /* Copy data from the initial buffer */
464                 if (ptr->s_filesz) {
465                         unsigned char *middle, *end;
466                         size_t len;
467                         len = ptr->s_filesz;
468                         switch(ptr->compression) {
469                                 case CBFS_COMPRESS_LZMA: {
470                                         printk(BIOS_DEBUG, "using LZMA\n");
471                                         len = ulzma(src, dest);
472                                         if (!len) /* Decompression Error. */
473                                                 return 0;
474                                         break;
475                                 }
476 #if CONFIG_COMPRESSED_PAYLOAD_NRV2B
477                                 case CBFS_COMPRESS_NRV2B: {
478                                         printk(BIOS_DEBUG, "using NRV2B\n");
479                                         unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
480                                         unsigned long tmp;
481                                         len = unrv2b(src, dest, &tmp);
482                                         break;
483                                 }
484 #endif
485                                 case CBFS_COMPRESS_NONE: {
486                                         printk(BIOS_DEBUG, "it's not compressed!\n");
487                                         memcpy(dest, src, len);
488                                         break;
489                                 }
490                                 default:
491                                         printk(BIOS_INFO,  "CBFS:  Unknown compression type %d\n", ptr->compression);
492                                         return -1;
493                         }
494                         end = dest + ptr->s_memsz;
495                         middle = dest + len;
496                         printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
497                                 (unsigned long)dest,
498                                 (unsigned long)middle,
499                                 (unsigned long)end,
500                                 (unsigned long)src);
501
502                         /* Zero the extra bytes between middle & end */
503                         if (middle < end) {
504                                 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
505                                         (unsigned long)middle, (unsigned long)(end - middle));
506
507                                 /* Zero the extra bytes */
508                                 memset(middle, 0, end - middle);
509                         }
510                         /* Copy the data that's outside the area that shadows coreboot_ram */
511                         printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
512                         if ((unsigned long)end > bounce_buffer) {
513                                 if ((unsigned long)dest < bounce_buffer) {
514                                         unsigned char *from = dest;
515                                         unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
516                                         unsigned long amount = bounce_buffer-(unsigned long)dest;
517                                         printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
518                                         memcpy(to, from, amount);
519                                 }
520                                 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
521                                         unsigned long from = bounce_buffer + (lb_end - lb_start);
522                                         unsigned long to = lb_end;
523                                         unsigned long amount = (unsigned long)end - from;
524                                         printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
525                                         memcpy((char*)to, (char*)from, amount);
526                                 }
527                         }
528                 }
529         }
530         return 1;
531 }
532
533 static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
534 {
535         u32 entry=0;
536         struct segment head;
537
538         /* Preprocess the self segments */
539         if (!build_self_segment_list(&head, mem, payload, &entry))
540                 goto out;
541
542         /* Load the segments */
543         if (!load_self_segments(&head, mem, payload))
544                 goto out;
545
546         printk(BIOS_SPEW, "Loaded segments\n");
547
548         /* Reset to booting from this image as late as possible */
549         boot_successful();
550
551         printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
552         post_code(POST_ENTER_ELF_BOOT);
553
554         /* Jump to kernel */
555         jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
556         return 1;
557
558  out:
559         return 0;
560 }
561