|
|
1.1 root 1: // Coreboot interface support.
2: //
3: // Copyright (C) 2008,2009 Kevin O'Connor <[email protected]>
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
1.1.1.5 ! root 15: #include "boot.h" // boot_add_cbfs
1.1 root 16:
17:
18: /****************************************************************
19: * Memory map
20: ****************************************************************/
21:
22: struct cb_header {
23: u32 signature;
24: u32 header_bytes;
25: u32 header_checksum;
26: u32 table_bytes;
27: u32 table_checksum;
28: u32 table_entries;
29: };
30:
31: #define CB_SIGNATURE 0x4f49424C // "LBIO"
32:
33: struct cb_memory_range {
34: u64 start;
35: u64 size;
36: u32 type;
37: };
38:
39: #define CB_MEM_TABLE 16
40:
41: struct cb_memory {
42: u32 tag;
43: u32 size;
44: struct cb_memory_range map[0];
45: };
46:
47: #define CB_TAG_MEMORY 0x01
48:
49: #define MEM_RANGE_COUNT(_rec) \
50: (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
51:
52: struct cb_mainboard {
53: u32 tag;
54: u32 size;
55: u8 vendor_idx;
56: u8 part_idx;
57: char strings[0];
58: };
59:
60: #define CB_TAG_MAINBOARD 0x0003
61:
62: struct cb_forward {
63: u32 tag;
64: u32 size;
65: u64 forward;
66: };
67:
68: #define CB_TAG_FORWARD 0x11
69:
70: static u16
71: ipchksum(char *buf, int count)
72: {
73: u16 *p = (u16*)buf;
74: u32 sum = 0;
75: while (count > 1) {
76: sum += *p++;
77: count -= 2;
78: }
79: if (count)
80: sum += *(u8*)p;
81: sum = (sum >> 16) + (sum & 0xffff);
82: sum += (sum >> 16);
83: return ~sum;
84: }
85:
86: // Try to locate the coreboot header in a given address range.
87: static struct cb_header *
88: find_cb_header(char *addr, int len)
89: {
90: char *end = addr + len;
91: for (; addr < end; addr += 16) {
92: struct cb_header *cbh = (struct cb_header *)addr;
93: if (cbh->signature != CB_SIGNATURE)
94: continue;
95: if (! cbh->table_bytes)
96: continue;
97: if (ipchksum(addr, sizeof(*cbh)) != 0)
98: continue;
99: if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
100: != cbh->table_checksum)
101: continue;
102: return cbh;
103: }
104: return NULL;
105: }
106:
107: // Try to find the coreboot memory table in the given coreboot table.
108: static void *
109: find_cb_subtable(struct cb_header *cbh, u32 tag)
110: {
111: char *tbl = (char *)cbh + sizeof(*cbh);
112: int i;
113: for (i=0; i<cbh->table_entries; i++) {
114: struct cb_memory *cbm = (struct cb_memory *)tbl;
115: tbl += cbm->size;
116: if (cbm->tag == tag)
117: return cbm;
118: }
119: return NULL;
120: }
121:
122: static struct cb_memory *CBMemTable;
123:
124: // Populate max ram and e820 map info by scanning for a coreboot table.
125: static void
1.1.1.2 root 126: coreboot_fill_map(void)
1.1 root 127: {
128: dprintf(3, "Attempting to find coreboot table\n");
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) {
1.1.1.3 root 211: warn_noalloc();
1.1 root 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) {
1.1.1.3 root 233: warn_noalloc();
1.1 root 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) {
1.1.1.3 root 261: warn_noalloc();
1.1 root 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
1.1.1.2 root 284: coreboot_copy_biostable(void)
1.1 root 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: // Scan CB_MEM_TABLE areas for bios tables.
293: int i, count = MEM_RANGE_COUNT(cbm);
294: for (i=0; i<count; i++) {
295: struct cb_memory_range *m = &cbm->map[i];
296: if (m->type == CB_MEM_TABLE)
297: scan_tables(m->start, m->size);
298: }
299:
300: // XXX - just create dummy smbios table for now - should detect if
301: // smbios/dmi table is found from coreboot and use that instead.
302: smbios_init();
303: }
304:
305:
306: /****************************************************************
307: * ulzma
308: ****************************************************************/
309:
310: // Uncompress data in flash to an area of memory.
311: static int
312: ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
313: {
314: dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
315: CLzmaDecoderState state;
316: int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
317: if (ret != LZMA_RESULT_OK) {
318: dprintf(1, "LzmaDecodeProperties error - %d\n", ret);
319: return -1;
320: }
321: u8 scratch[15980];
322: int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
323: if (need > sizeof(scratch)) {
1.1.1.4 root 324: dprintf(1, "LzmaDecode need %d have %d\n", need, (unsigned int)sizeof(scratch));
1.1 root 325: return -1;
326: }
327: state.Probs = (CProb *)scratch;
328:
329: u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
330: if (dstlen > maxlen) {
331: dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
332: return -1;
333: }
334: u32 inProcessed, outProcessed;
335: ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
336: , &inProcessed, dst, dstlen, &outProcessed);
337: if (ret) {
338: dprintf(1, "LzmaDecode returned %d\n", ret);
339: return -1;
340: }
341: return dstlen;
342: }
343:
344:
345: /****************************************************************
346: * Coreboot flash format
347: ****************************************************************/
348:
349: #define CBFS_HEADER_MAGIC 0x4F524243
350: #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
351: #define CBFS_VERSION1 0x31313131
352:
353: struct cbfs_header {
354: u32 magic;
355: u32 version;
356: u32 romsize;
357: u32 bootblocksize;
358: u32 align;
359: u32 offset;
360: u32 pad[2];
361: } PACKED;
362:
363: static struct cbfs_header *CBHDR;
364:
365: static void
1.1.1.2 root 366: cbfs_setup(void)
1.1 root 367: {
1.1.1.3 root 368: if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
1.1 root 369: return;
370:
371: CBHDR = *(void **)CBFS_HEADPTR_ADDR;
372: if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
1.1.1.3 root 373: dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n"
374: , CBHDR, CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
1.1 root 375: CBHDR = NULL;
376: return;
377: }
378:
379: dprintf(1, "Found CBFS header at %p\n", CBHDR);
380: }
381:
382: #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
383:
384: struct cbfs_file {
385: u64 magic;
386: u32 len;
387: u32 type;
388: u32 checksum;
389: u32 offset;
390: char filename[0];
391: } PACKED;
392:
393: // Verify a cbfs entry looks valid.
394: static struct cbfs_file *
395: cbfs_verify(struct cbfs_file *file)
396: {
397: if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
398: return NULL;
399: u64 magic = file->magic;
400: if (magic == CBFS_FILE_MAGIC) {
401: dprintf(5, "Found CBFS file %s\n", file->filename);
402: return file;
403: }
404: return NULL;
405: }
406:
407: // Return the first file in the CBFS archive
408: static struct cbfs_file *
1.1.1.2 root 409: cbfs_getfirst(void)
1.1 root 410: {
411: if (! CBHDR)
412: return NULL;
413: return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
414: }
415:
416: // Return the file after the given file.
417: static struct cbfs_file *
418: cbfs_getnext(struct cbfs_file *file)
419: {
420: file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
421: return cbfs_verify(file);
422: }
423:
424: // Find the file with the given filename.
425: struct cbfs_file *
426: cbfs_findfile(const char *fname)
427: {
428: dprintf(3, "Searching CBFS for %s\n", fname);
429: struct cbfs_file *file;
430: for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
431: if (strcmp(fname, file->filename) == 0)
432: return file;
433: return NULL;
434: }
435:
436: // Find next file with the given filename prefix.
437: struct cbfs_file *
438: cbfs_findprefix(const char *prefix, struct cbfs_file *last)
439: {
1.1.1.3 root 440: if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
1.1 root 441: return NULL;
442:
443: dprintf(3, "Searching CBFS for prefix %s\n", prefix);
444: int len = strlen(prefix);
445: struct cbfs_file *file;
446: if (! last)
447: file = cbfs_getfirst();
448: else
449: file = cbfs_getnext(last);
450: for (; file; file = cbfs_getnext(file))
451: if (memcmp(prefix, file->filename, len) == 0)
452: return file;
453: return NULL;
454: }
455:
456: // Find a file with the given filename (possibly with ".lzma" extension).
1.1.1.3 root 457: struct cbfs_file *
1.1 root 458: cbfs_finddatafile(const char *fname)
459: {
460: int fnlen = strlen(fname);
461: struct cbfs_file *file = NULL;
462: for (;;) {
463: file = cbfs_findprefix(fname, file);
464: if (!file)
465: return NULL;
466: if (file->filename[fnlen] == '\0'
467: || strcmp(&file->filename[fnlen], ".lzma") == 0)
468: return file;
469: }
470: }
471:
472: // Determine whether the file has a ".lzma" extension.
473: static int
474: cbfs_iscomp(struct cbfs_file *file)
475: {
476: int fnamelen = strlen(file->filename);
477: return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
478: }
479:
480: // Return the filename of a given file.
481: const char *
482: cbfs_filename(struct cbfs_file *file)
483: {
484: return file->filename;
485: }
486:
487: // Determine the uncompressed size of a datafile.
488: u32
489: cbfs_datasize(struct cbfs_file *file)
490: {
491: void *src = (void*)file + ntohl(file->offset);
492: if (cbfs_iscomp(file))
493: return *(u32*)(src + LZMA_PROPERTIES_SIZE);
494: return ntohl(file->len);
495: }
496:
497: // Copy a file to memory (uncompressing if necessary)
498: int
499: cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
500: {
1.1.1.3 root 501: if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
1.1 root 502: return -1;
503:
504: u32 size = ntohl(file->len);
505: void *src = (void*)file + ntohl(file->offset);
506: if (cbfs_iscomp(file)) {
507: // Compressed - copy to temp ram and uncompress it.
1.1.1.4 root 508: void *temp = malloc_tmphigh(size);
1.1 root 509: if (!temp)
510: return -1;
1.1.1.4 root 511: iomemcpy(temp, src, size);
1.1 root 512: int ret = ulzma(dst, maxlen, temp, size);
513: yield();
514: free(temp);
515: return ret;
516: }
517:
518: // Not compressed.
519: dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
520: if (size > maxlen) {
1.1.1.3 root 521: warn_noalloc();
1.1 root 522: return -1;
523: }
524: iomemcpy(dst, src, size);
525: return size;
526: }
527:
528: struct cbfs_payload_segment {
529: u32 type;
530: u32 compression;
531: u32 offset;
532: u64 load_addr;
533: u32 len;
534: u32 mem_len;
535: } PACKED;
536:
537: #define PAYLOAD_SEGMENT_BSS 0x20535342
538: #define PAYLOAD_SEGMENT_ENTRY 0x52544E45
539:
540: #define CBFS_COMPRESS_NONE 0
541: #define CBFS_COMPRESS_LZMA 1
542:
543: struct cbfs_payload {
544: struct cbfs_payload_segment segments[1];
545: };
546:
547: void
548: cbfs_run_payload(struct cbfs_file *file)
549: {
1.1.1.3 root 550: if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
1.1 root 551: return;
552: dprintf(1, "Run %s\n", file->filename);
553: struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
554: struct cbfs_payload_segment *seg = pay->segments;
555: for (;;) {
556: void *src = (void*)pay + ntohl(seg->offset);
557: void *dest = (void*)ntohl((u32)seg->load_addr);
558: u32 src_len = ntohl(seg->len);
559: u32 dest_len = ntohl(seg->mem_len);
560: switch (seg->type) {
561: case PAYLOAD_SEGMENT_BSS:
562: dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
563: memset(dest, 0, dest_len);
564: break;
565: case PAYLOAD_SEGMENT_ENTRY: {
566: dprintf(1, "Calling addr %p\n", dest);
567: void (*func)() = dest;
568: func();
569: return;
570: }
571: default:
572: dprintf(3, "Segment %x %d@%p -> %d@%p\n"
573: , seg->type, src_len, src, dest_len, dest);
574: if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
575: if (src_len > dest_len)
576: src_len = dest_len;
577: memcpy(dest, src, src_len);
578: } else if (CONFIG_LZMA
579: && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
580: int ret = ulzma(dest, dest_len, src, src_len);
581: if (ret < 0)
582: return;
583: src_len = ret;
584: } else {
585: dprintf(1, "No support for compression type %x\n"
586: , seg->compression);
587: return;
588: }
589: if (dest_len > src_len)
590: memset(dest + src_len, 0, dest_len - src_len);
591: break;
592: }
593: seg++;
594: }
595: }
596:
1.1.1.5 ! root 597: // Register payloads in "img/" directory with boot system.
! 598: void
! 599: cbfs_payload_setup(void)
! 600: {
! 601: struct cbfs_file *file = NULL;
! 602: for (;;) {
! 603: file = cbfs_findprefix("img/", file);
! 604: if (!file)
! 605: break;
! 606: const char *filename = cbfs_filename(file);
! 607: char *desc = znprintf(MAXDESCSIZE, "Payload [%s]", &filename[4]);
! 608: boot_add_cbfs(file, desc, bootprio_find_named_rom(filename, 0));
! 609: }
! 610: }
! 611:
1.1 root 612: void
613: coreboot_setup(void)
614: {
615: coreboot_fill_map();
616: cbfs_setup();
617: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.