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