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