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