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