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