82c9f8a5499ebf6e9cda11424582e97ff63d926c
[seabios.git] / src / coreboot.c
1 // Coreboot interface support.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "memmap.h" // add_e820
8 #include "util.h" // dprintf
9 #include "pci.h" // struct pir_header
10 #include "acpi.h" // struct rsdp_descriptor
11 #include "mptable.h" // MPTABLE_SIGNATURE
12 #include "biosvar.h" // GET_EBDA
13 #include "lzmadecode.h" // LzmaDecode
14
15
16 /****************************************************************
17  * Memory map
18  ****************************************************************/
19
20 struct cb_header {
21     u32 signature;
22     u32 header_bytes;
23     u32 header_checksum;
24     u32 table_bytes;
25     u32 table_checksum;
26     u32 table_entries;
27 };
28
29 #define CB_SIGNATURE 0x4f49424C // "LBIO"
30
31 struct cb_memory_range {
32     u64 start;
33     u64 size;
34     u32 type;
35 };
36
37 #define CB_MEM_TABLE    16
38
39 struct cb_memory {
40     u32 tag;
41     u32 size;
42     struct cb_memory_range map[0];
43 };
44
45 #define CB_TAG_MEMORY 0x01
46
47 #define MEM_RANGE_COUNT(_rec) \
48         (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
49
50 struct cb_mainboard {
51     u32 tag;
52     u32 size;
53     u8  vendor_idx;
54     u8  part_idx;
55     char  strings[0];
56 };
57
58 #define CB_TAG_MAINBOARD 0x0003
59
60 struct cb_forward {
61     u32 tag;
62     u32 size;
63     u64 forward;
64 };
65
66 #define CB_TAG_FORWARD 0x11
67
68 static u16
69 ipchksum(char *buf, int count)
70 {
71     u16 *p = (u16*)buf;
72     u32 sum = 0;
73     while (count > 1) {
74         sum += *p++;
75         count -= 2;
76     }
77     if (count)
78         sum += *(u8*)p;
79     sum = (sum >> 16) + (sum & 0xffff);
80     sum += (sum >> 16);
81     return ~sum;
82 }
83
84 // Try to locate the coreboot header in a given address range.
85 static struct cb_header *
86 find_cb_header(char *addr, int len)
87 {
88     char *end = addr + len;
89     for (; addr < end; addr += 16) {
90         struct cb_header *cbh = (struct cb_header *)addr;
91         if (cbh->signature != CB_SIGNATURE)
92             continue;
93         if (! cbh->table_bytes)
94             continue;
95         if (ipchksum(addr, sizeof(*cbh)) != 0)
96             continue;
97         if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
98             != cbh->table_checksum)
99             continue;
100         return cbh;
101     }
102     return NULL;
103 }
104
105 // Try to find the coreboot memory table in the given coreboot table.
106 static void *
107 find_cb_subtable(struct cb_header *cbh, u32 tag)
108 {
109     char *tbl = (char *)cbh + sizeof(*cbh);
110     int i;
111     for (i=0; i<cbh->table_entries; i++) {
112         struct cb_memory *cbm = (struct cb_memory *)tbl;
113         tbl += cbm->size;
114         if (cbm->tag == tag)
115             return cbm;
116     }
117     return NULL;
118 }
119
120 static struct cb_memory *CBMemTable;
121
122 // Populate max ram and e820 map info by scanning for a coreboot table.
123 static void
124 coreboot_fill_map()
125 {
126     dprintf(3, "Attempting to find coreboot table\n");
127
128     CBMemTable = NULL;
129
130     // Find coreboot table.
131     struct cb_header *cbh = find_cb_header(0, 0x1000);
132     if (!cbh)
133         goto fail;
134     struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
135     if (cbf) {
136         dprintf(3, "Found coreboot table forwarder.\n");
137         cbh = find_cb_header((char *)((u32)cbf->forward), 0x100);
138         if (!cbh)
139             goto fail;
140     }
141     dprintf(3, "Now attempting to find coreboot memory map\n");
142     struct cb_memory *cbm = CBMemTable = find_cb_subtable(cbh, CB_TAG_MEMORY);
143     if (!cbm)
144         goto fail;
145
146     u64 maxram = 0, maxram_over4G = 0;
147     int i, count = MEM_RANGE_COUNT(cbm);
148     for (i=0; i<count; i++) {
149         struct cb_memory_range *m = &cbm->map[i];
150         u32 type = m->type;
151         if (type == CB_MEM_TABLE) {
152             type = E820_RESERVED;
153         } else if (type == E820_ACPI || type == E820_RAM) {
154             u64 end = m->start + m->size;
155             if (end > 0x100000000ull) {
156                 end -= 0x100000000ull;
157                 if (end > maxram_over4G)
158                     maxram_over4G = end;
159             } else if (end > maxram)
160                 maxram = end;
161         }
162         add_e820(m->start, m->size, type);
163     }
164
165     RamSize = maxram;
166     RamSizeOver4G = maxram_over4G;
167
168     // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
169     // confuses grub.  So, override it.
170     add_e820(0, 16*1024, E820_RAM);
171
172     struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD);
173     if (cbmb) {
174         const char *vendor = &cbmb->strings[cbmb->vendor_idx];
175         const char *part = &cbmb->strings[cbmb->part_idx];
176         dprintf(1, "Found mainboard %s %s\n", vendor, part);
177
178         vgahook_setup(vendor, part);
179     }
180
181     return;
182
183 fail:
184     // No table found..  Use 16Megs as a dummy value.
185     dprintf(1, "Unable to find coreboot table!\n");
186     RamSize = 16*1024*1024;
187     RamSizeOver4G = 0;
188     add_e820(0, 16*1024*1024, E820_RAM);
189     return;
190 }
191
192
193 /****************************************************************
194  * BIOS table copying
195  ****************************************************************/
196
197 static void
198 copy_pir(void *pos)
199 {
200     struct pir_header *p = pos;
201     if (p->signature != PIR_SIGNATURE)
202         return;
203     if (PirOffset)
204         return;
205     if (p->size < sizeof(*p))
206         return;
207     if (checksum(pos, p->size) != 0)
208         return;
209     void *newpos = malloc_fseg(p->size);
210     if (!newpos) {
211         dprintf(1, "No room to copy PIR table!\n");
212         return;
213     }
214     dprintf(1, "Copying PIR from %p to %p\n", pos, newpos);
215     memcpy(newpos, pos, p->size);
216     PirOffset = (u32)newpos - BUILD_BIOS_ADDR;
217 }
218
219 static void
220 copy_mptable(void *pos)
221 {
222     struct mptable_floating_s *p = pos;
223     if (p->signature != MPTABLE_SIGNATURE)
224         return;
225     if (!p->physaddr)
226         return;
227     if (checksum(pos, sizeof(*p)) != 0)
228         return;
229     u32 length = p->length * 16;
230     u16 mpclength = ((struct mptable_config_s *)p->physaddr)->length;
231     struct mptable_floating_s *newpos = malloc_fseg(length + mpclength);
232     if (!newpos) {
233         dprintf(1, "No room to copy MPTABLE!\n");
234         return;
235     }
236     dprintf(1, "Copying MPTABLE from %p/%x to %p\n", pos, p->physaddr, newpos);
237     memcpy(newpos, pos, length);
238     newpos->physaddr = (u32)newpos + length;
239     newpos->checksum -= checksum(newpos, sizeof(*newpos));
240     memcpy((void*)newpos + length, (void*)p->physaddr, mpclength);
241 }
242
243 static void
244 copy_acpi_rsdp(void *pos)
245 {
246     if (RsdpAddr)
247         return;
248     struct rsdp_descriptor *p = pos;
249     if (p->signature != RSDP_SIGNATURE)
250         return;
251     u32 length = 20;
252     if (checksum(pos, length) != 0)
253         return;
254     if (p->revision > 1) {
255         length = p->length;
256         if (checksum(pos, length) != 0)
257             return;
258     }
259     void *newpos = malloc_fseg(length);
260     if (!newpos) {
261         dprintf(1, "No room to copy ACPI RSDP table!\n");
262         return;
263     }
264     dprintf(1, "Copying ACPI RSDP from %p to %p\n", pos, newpos);
265     memcpy(newpos, pos, length);
266     RsdpAddr = newpos;
267 }
268
269 // Attempt to find (and relocate) any standard bios tables found in a
270 // given address range.
271 static void
272 scan_tables(u32 start, u32 size)
273 {
274     void *p = (void*)ALIGN(start, 16);
275     void *end = (void*)start + size;
276     for (; p<end; p += 16) {
277         copy_pir(p);
278         copy_mptable(p);
279         copy_acpi_rsdp(p);
280     }
281 }
282
283 void
284 coreboot_copy_biostable()
285 {
286     struct cb_memory *cbm = CBMemTable;
287     if (! CONFIG_COREBOOT || !cbm)
288         return;
289
290     dprintf(3, "Relocating coreboot bios tables\n");
291
292     // Init variables set in coreboot table memory scan.
293     PirOffset = 0;
294     RsdpAddr = 0;
295
296     // Scan CB_MEM_TABLE areas for bios tables.
297     int i, count = MEM_RANGE_COUNT(cbm);
298     for (i=0; i<count; i++) {
299         struct cb_memory_range *m = &cbm->map[i];
300         if (m->type == CB_MEM_TABLE)
301             scan_tables(m->start, m->size);
302     }
303
304     // XXX - just create dummy smbios table for now - should detect if
305     // smbios/dmi table is found from coreboot and use that instead.
306     smbios_init();
307 }
308
309
310 /****************************************************************
311  * ulzma
312  ****************************************************************/
313
314 static int
315 ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
316 {
317     dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
318     CLzmaDecoderState state;
319     int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
320     if (ret != LZMA_RESULT_OK) {
321         dprintf(1, "LzmaDecodeProperties error - %d\n", ret);
322         return -1;
323     }
324     u8 scratch[15980];
325     int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
326     if (need > sizeof(scratch)) {
327         dprintf(1, "LzmaDecode need %d have %d\n", need, sizeof(scratch));
328         return -1;
329     }
330     state.Probs = (CProb *)scratch;
331
332     u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
333     if (dstlen > maxlen) {
334         dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
335         return -1;
336     }
337     u32 inProcessed, outProcessed;
338     ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
339                      , &inProcessed, dst, dstlen, &outProcessed);
340     if (ret) {
341         dprintf(1, "LzmaDecode returned %d\n", ret);
342         return -1;
343     }
344     return dstlen;
345 }
346
347
348 /****************************************************************
349  * Coreboot flash format
350  ****************************************************************/
351
352 // XXX - optimize
353 #define ntohl(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | \
354                   (((x)&0xff0000) >> 8) | (((x)&0xff000000) >> 24))
355 #define htonl(x) ntohl(x)
356
357 #define CBFS_HEADER_MAGIC 0x4F524243
358 #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
359 #define CBFS_VERSION1 0x31313131
360
361 struct cbfs_header {
362     u32 magic;
363     u32 version;
364     u32 romsize;
365     u32 bootblocksize;
366     u32 align;
367     u32 offset;
368     u32 pad[2];
369 } PACKED;
370
371 static struct cbfs_header *CBHDR;
372
373 static void
374 cbfs_setup()
375 {
376     if (! CONFIG_COREBOOT_FLASH)
377         return;
378
379     CBHDR = *(void **)CBFS_HEADPTR_ADDR;
380     if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
381         dprintf(1, "Unable to find CBFS (got %x not %x)\n"
382                 , CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
383         CBHDR = NULL;
384         return;
385     }
386
387     dprintf(1, "Found CBFS header at %p\n", CBHDR);
388 }
389
390 #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
391
392 struct cbfs_file {
393     u64 magic;
394     u32 len;
395     u32 type;
396     u32 checksum;
397     u32 offset;
398     char filename[0];
399 } PACKED;
400
401 static struct cbfs_file *
402 cbfs_search(struct cbfs_file *file)
403 {
404     for (;;) {
405         if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
406             return NULL;
407         u64 magic = file->magic;
408         if (magic == CBFS_FILE_MAGIC) {
409             dprintf(5, "Found CBFS file %s\n", file->filename);
410             return file;
411         }
412         if (magic == 0)
413             return NULL;
414         file = (void*)file + ntohl(CBHDR->align);
415     }
416 }
417
418 static struct cbfs_file *
419 cbfs_getfirst()
420 {
421     if (! CBHDR)
422         return NULL;
423     return cbfs_search((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
424 }
425
426 static struct cbfs_file *
427 cbfs_getnext(struct cbfs_file *file)
428 {
429     file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
430     return cbfs_search(file);
431 }
432
433 static struct cbfs_file *
434 cbfs_findfile(const char *fname)
435 {
436     dprintf(3, "Searching CBFS for %s\n", fname);
437     struct cbfs_file *file;
438     for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
439         if (strcmp(fname, file->filename) == 0)
440             return file;
441     }
442     return NULL;
443 }
444
445 static int
446 data_copy(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
447 {
448     dprintf(3, "Copying data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
449     if (srclen > maxlen) {
450         dprintf(1, "File too big to copy\n");
451         return -1;
452     }
453     memcpy(dst, src, srclen);
454     return srclen;
455 }
456
457 // Copy a file to memory (uncompressing if necessary)
458 static int
459 cbfs_copyfile(void *dst, u32 maxlen, const char *fname)
460 {
461     dprintf(3, "Searching CBFS for data file %s\n", fname);
462     int fnlen = strlen(fname);
463     struct cbfs_file *file;
464     for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
465         if (memcmp(fname, file->filename, fnlen) != 0)
466             continue;
467         u32 size = ntohl(file->len);
468         void *src = (void*)file + ntohl(file->offset);
469         if (file->filename[fnlen] == '\0')
470             return data_copy(dst, maxlen, src, size);
471         if (strcmp(&file->filename[fnlen], ".lzma") == 0)
472             return ulzma(dst, maxlen, src, size);
473     }
474     return -1;
475 }
476
477 const char *
478 cbfs_findNprefix(const char *prefix, int n)
479 {
480     if (! CONFIG_COREBOOT_FLASH)
481         return NULL;
482
483     dprintf(3, "Searching CBFS for prefix %s\n", prefix);
484     int len = strlen(prefix);
485     struct cbfs_file *file;
486     for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
487         if (memcmp(prefix, file->filename, len) == 0) {
488             if (n <= 0)
489                 return file->filename;
490             n--;
491         }
492     }
493     return NULL;
494 }
495
496 static char
497 getHex(u8 x)
498 {
499     if (x <= 9)
500         return '0' + x;
501     return 'a' + x - 10;
502 }
503
504 static u32
505 hexify4(u16 x)
506 {
507     return ((getHex(x&0xf) << 24)
508             | (getHex((x>>4)&0xf) << 16)
509             | (getHex((x>>8)&0xf) << 8)
510             | (getHex(x>>12)));
511 }
512
513 int
514 cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev)
515 {
516     if (! CONFIG_COREBOOT_FLASH)
517         return -1;
518
519     char fname[17];
520     // Ughh - poor man's sprintf of "pci%04x,%04x.rom"
521     *(u32*)fname = 0x20696370; // "pci "
522     *(u32*)&fname[3] = hexify4(vendev);
523     fname[7] = ',';
524     *(u32*)&fname[8] = hexify4(vendev >> 16);
525     *(u32*)&fname[12] = 0x6d6f722e; // ".rom"
526     fname[16] = '\0';
527
528     return cbfs_copyfile(dst, maxlen, fname);
529 }
530
531 // Copy the next file with the given prefix - starting at pos 'last'.
532 struct cbfs_file *
533 cbfs_copyfile_prefix(void *dst, u32 maxlen, const char *prefix
534                      , struct cbfs_file *last)
535 {
536     if (! CONFIG_COREBOOT_FLASH)
537         return NULL;
538     dprintf(3, "Searching CBFS for data file prefix %s\n", prefix);
539     struct cbfs_file *file;
540     if (! last)
541         file = cbfs_getfirst();
542     else
543         file = cbfs_getnext(last);
544     int prefixlen = strlen(prefix);
545     for (; file; file = cbfs_getnext(file)) {
546         if (memcmp(prefix, file->filename, prefixlen) != 0)
547             continue;
548         u32 size = ntohl(file->len);
549         void *src = (void*)file + ntohl(file->offset);
550         int fnamelen = strlen(file->filename);
551         int rv;
552         if (fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0)
553             rv = ulzma(dst, maxlen, src, size);
554         else
555             rv = data_copy(dst, maxlen, src, size);
556         if (rv >= 0)
557             return file;
558     }
559     return NULL;
560 }
561
562 struct cbfs_payload_segment {
563     u32 type;
564     u32 compression;
565     u32 offset;
566     u64 load_addr;
567     u32 len;
568     u32 mem_len;
569 } PACKED;
570
571 #define PAYLOAD_SEGMENT_BSS    0x20535342
572 #define PAYLOAD_SEGMENT_ENTRY  0x52544E45
573
574 #define CBFS_COMPRESS_NONE  0
575 #define CBFS_COMPRESS_LZMA  1
576
577 struct cbfs_payload {
578     struct cbfs_payload_segment segments[1];
579 };
580
581 void
582 cbfs_run_payload(const char *filename)
583 {
584     if (! CONFIG_COREBOOT_FLASH)
585         return;
586     dprintf(1, "Run %s\n", filename);
587     struct cbfs_file *file = cbfs_findfile(filename);
588     if (!file)
589         return;
590     struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
591     struct cbfs_payload_segment *seg = pay->segments;
592     for (;;) {
593         void *src = (void*)pay + ntohl(seg->offset);
594         void *dest = (void*)ntohl((u32)seg->load_addr);
595         u32 src_len = ntohl(seg->len);
596         u32 dest_len = ntohl(seg->mem_len);
597         switch (seg->type) {
598         case PAYLOAD_SEGMENT_BSS:
599             dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
600             memset(dest, 0, dest_len);
601             break;
602         case PAYLOAD_SEGMENT_ENTRY: {
603             dprintf(1, "Calling addr %p\n", dest);
604             void (*func)() = dest;
605             func();
606             return;
607         }
608         default:
609             dprintf(3, "Segment %x %d@%p -> %d@%p\n"
610                     , seg->type, src_len, src, dest_len, dest);
611             if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
612                 if (src_len > dest_len)
613                     src_len = dest_len;
614                 memcpy(dest, src, src_len);
615             } else if (CONFIG_LZMA
616                        && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
617                 int ret = ulzma(dest, dest_len, src, src_len);
618                 if (ret < 0)
619                     return;
620                 src_len = ret;
621             } else {
622                 dprintf(1, "No support for compression type %x\n"
623                         , seg->compression);
624                 return;
625             }
626             if (dest_len > src_len)
627                 memset(dest + src_len, 0, dest_len - src_len);
628             break;
629         }
630         seg++;
631     }
632 }
633
634 void
635 coreboot_setup(void)
636 {
637     coreboot_fill_map();
638     cbfs_setup();
639 }