Replace memeq/streq functions with memcmp/strcmp.
[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
14
15 /****************************************************************
16  * BIOS table copying
17  ****************************************************************/
18
19 static void
20 copy_pir(void *pos)
21 {
22     struct pir_header *p = pos;
23     if (p->signature != PIR_SIGNATURE)
24         return;
25     if (PirOffset)
26         return;
27     if (p->size < sizeof(*p))
28         return;
29     if (checksum(pos, p->size) != 0)
30         return;
31     bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
32     if (bios_table_cur_addr + p->size > bios_table_end_addr) {
33         dprintf(1, "No room to copy PIR table!\n");
34         return;
35     }
36     dprintf(1, "Copying PIR from %p to %x\n", pos, bios_table_cur_addr);
37     memcpy((void*)bios_table_cur_addr, pos, p->size);
38     PirOffset = bios_table_cur_addr - BUILD_BIOS_ADDR;
39     bios_table_cur_addr += p->size;
40 }
41
42 static void
43 copy_mptable(void *pos)
44 {
45     struct mptable_floating_s *p = pos;
46     if (p->signature != MPTABLE_SIGNATURE)
47         return;
48     if (!p->physaddr)
49         return;
50     if (checksum(pos, sizeof(*p)) != 0)
51         return;
52     u32 length = p->length * 16;
53     bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
54     u16 mpclength = ((struct mptable_config_s *)p->physaddr)->length;
55     if (bios_table_cur_addr + length + mpclength > bios_table_end_addr) {
56         dprintf(1, "No room to copy MPTABLE!\n");
57         return;
58     }
59     dprintf(1, "Copying MPTABLE from %p/%x to %x\n"
60             , pos, p->physaddr, bios_table_cur_addr);
61     memcpy((void*)bios_table_cur_addr, pos, length);
62     struct mptable_floating_s *newp = (void*)bios_table_cur_addr;
63     newp->physaddr = bios_table_cur_addr + length;
64     newp->checksum = 0;
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_forward {
147     u32 tag;
148     u32 size;
149     u64 forward;
150 };
151
152 #define CB_TAG_FORWARD 0x11
153
154 static u16
155 ipchksum(char *buf, int count)
156 {
157     u16 *p = (u16*)buf;
158     u32 sum = 0;
159     while (count > 1) {
160         sum += *p++;
161         count -= 2;
162     }
163     if (count)
164         sum += *(u8*)p;
165     sum = (sum >> 16) + (sum & 0xffff);
166     sum += (sum >> 16);
167     return ~sum;
168 }
169
170 // Try to locate the coreboot header in a given address range.
171 static struct cb_header *
172 find_cb_header(char *addr, int len)
173 {
174     char *end = addr + len;
175     for (; addr < end; addr += 16) {
176         struct cb_header *cbh = (struct cb_header *)addr;
177         if (cbh->signature != CB_SIGNATURE)
178             continue;
179         if (! cbh->table_bytes)
180             continue;
181         if (ipchksum(addr, sizeof(*cbh)) != 0)
182             continue;
183         if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
184             != cbh->table_checksum)
185             continue;
186         return cbh;
187     }
188     return NULL;
189 }
190
191 // Try to find the coreboot memory table in the given coreboot table.
192 static void *
193 find_cb_subtable(struct cb_header *cbh, u32 tag)
194 {
195     char *tbl = (char *)cbh + sizeof(*cbh);
196     int i;
197     for (i=0; i<cbh->table_entries; i++) {
198         struct cb_memory *cbm = (struct cb_memory *)tbl;
199         tbl += cbm->size;
200         if (cbm->tag == tag)
201             return cbm;
202     }
203     return NULL;
204 }
205
206 // Populate max ram and e820 map info by scanning for a coreboot table.
207 static void
208 coreboot_fill_map()
209 {
210     dprintf(3, "Attempting to find coreboot table\n");
211
212     // Init variables set in coreboot table memory scan.
213     PirOffset = 0;
214     RsdpAddr = 0;
215
216     // Find coreboot table.
217     struct cb_header *cbh = find_cb_header(0, 0x1000);
218     if (!cbh)
219         goto fail;
220     struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
221     if (cbf) {
222         dprintf(3, "Found coreboot table forwarder.\n");
223         cbh = find_cb_header((char *)((u32)cbf->forward), 0x100);
224         if (!cbh)
225             goto fail;
226     }
227     dprintf(3, "Now attempting to find coreboot memory map\n");
228     struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY);
229     if (!cbm)
230         goto fail;
231
232     u64 maxram = 0, maxram_over4G = 0;
233     int i, count = MEM_RANGE_COUNT(cbm);
234     for (i=0; i<count; i++) {
235         struct cb_memory_range *m = &cbm->map[i];
236         u32 type = m->type;
237         if (type == CB_MEM_TABLE) {
238             type = E820_RESERVED;
239             scan_tables(m->start, m->size);
240         } else if (type == E820_ACPI || type == E820_RAM) {
241             u64 end = m->start + m->size;
242             if (end > 0x100000000ull) {
243                 end -= 0x100000000ull;
244                 if (end > maxram_over4G)
245                     maxram_over4G = end;
246             } else if (end > maxram)
247                 maxram = end;
248         }
249         add_e820(m->start, m->size, type);
250     }
251
252     RamSize = maxram;
253     RamSizeOver4G = maxram_over4G;
254
255     // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
256     // confuses grub.  So, override it.
257     add_e820(0, 16*1024, E820_RAM);
258
259     // XXX - just create dummy smbios table for now - should detect if
260     // smbios/dmi table is found from coreboot and use that instead.
261     smbios_init();
262
263     return;
264
265 fail:
266     // No table found..  Use 16Megs as a dummy value.
267     dprintf(1, "Unable to find coreboot table!\n");
268     RamSize = 16*1024*1024;
269     RamSizeOver4G = 0;
270     add_e820(0, 16*1024*1024, E820_RAM);
271     return;
272 }
273
274
275 /****************************************************************
276  * Coreboot flash format
277  ****************************************************************/
278
279 // XXX - optimize
280 #define ntohl(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | \
281                   (((x)&0xff0000) >> 8) | (((x)&0xff000000) >> 24))
282 #define htonl(x) ntohl(x)
283
284 #define CBFS_HEADER_MAGIC 0x4F524243
285 #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
286 #define CBFS_VERSION1 0x31313131
287
288 struct cbfs_header {
289     u32 magic;
290     u32 version;
291     u32 romsize;
292     u32 bootblocksize;
293     u32 align;
294     u32 offset;
295     u32 pad[2];
296 } PACKED;
297
298 static struct cbfs_header *CBHDR;
299
300 static void
301 cbfs_setup()
302 {
303     if (! CONFIG_COREBOOT_FLASH)
304         return;
305
306     CBHDR = *(void **)CBFS_HEADPTR_ADDR;
307     if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
308         dprintf(1, "Unable to find CBFS (got %x not %x)\n"
309                 , CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
310         CBHDR = NULL;
311         return;
312     }
313
314     dprintf(1, "Found CBFS header at %p\n", CBHDR);
315 }
316
317 #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
318
319 struct cbfs_file {
320     u64 magic;
321     u32 len;
322     u32 type;
323     u32 checksum;
324     u32 offset;
325     char filename[0];
326 } PACKED;
327
328 static struct cbfs_file *
329 cbfs_search(struct cbfs_file *file)
330 {
331     for (;;) {
332         if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
333             return NULL;
334         if (file->magic == CBFS_FILE_MAGIC)
335             return file;
336         file = (void*)file + ntohl(CBHDR->align);
337     }
338 }
339
340 static struct cbfs_file *
341 cbfs_getfirst()
342 {
343     if (! CBHDR)
344         return NULL;
345     return cbfs_search((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
346 }
347
348 static struct cbfs_file *
349 cbfs_getnext(struct cbfs_file *file)
350 {
351     file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
352     return cbfs_search(file);
353 }
354
355 static struct cbfs_file *
356 cbfs_findfile(const char *fname)
357 {
358     if (! CONFIG_COREBOOT_FLASH)
359         return NULL;
360
361     dprintf(3, "Searching CBFS for %s\n", fname);
362     struct cbfs_file *file;
363     for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
364         dprintf(3, "Found CBFS file %s\n", file->filename);
365         if (strcmp(fname, file->filename) == 0)
366             return file;
367     }
368     return NULL;
369 }
370
371 const char *
372 cbfs_findNprefix(const char *prefix, int n)
373 {
374     if (! CONFIG_COREBOOT_FLASH)
375         return NULL;
376
377     dprintf(3, "Searching CBFS for prefix %s\n", prefix);
378     int len = strlen(prefix);
379     struct cbfs_file *file;
380     for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
381         dprintf(3, "Found CBFS file %s\n", file->filename);
382         if (memcmp(prefix, file->filename, len) == 0) {
383             if (n <= 0)
384                 return file->filename;
385             n--;
386         }
387     }
388     return NULL;
389 }
390
391 static char
392 getHex(u8 x)
393 {
394     if (x <= 9)
395         return '0' + x;
396     return 'a' + x - 10;
397 }
398
399 static u32
400 hexify4(u16 x)
401 {
402     return ((getHex(x&0xf) << 24)
403             | (getHex((x>>4)&0xf) << 16)
404             | (getHex((x>>8)&0xf) << 8)
405             | (getHex(x>>12)));
406 }
407
408 void *
409 cb_find_optionrom(u32 vendev)
410 {
411     if (! CONFIG_COREBOOT_FLASH)
412         return NULL;
413
414     char fname[17];
415     // Ughh - poor man's sprintf of "pci%04x,%04x.rom"
416     *(u32*)fname = 0x20696370; // "pci "
417     *(u32*)&fname[3] = hexify4(vendev);
418     fname[7] = ',';
419     *(u32*)&fname[8] = hexify4(vendev >> 16);
420     *(u32*)&fname[12] = 0x6d6f722e; // ".rom"
421     fname[16] = '\0';
422
423     struct cbfs_file *file = cbfs_findfile(fname);
424     if (!file)
425         return NULL;
426     // Found it.
427     dprintf(3, "Found rom at %p\n", (void*)file + ntohl(file->offset));
428     return (void*)file + ntohl(file->offset);
429 }
430
431 struct cbfs_payload_segment {
432     u32 type;
433     u32 compression;
434     u32 offset;
435     u64 load_addr;
436     u32 len;
437     u32 mem_len;
438 } PACKED;
439
440 #define PAYLOAD_SEGMENT_BSS    0x20535342
441 #define PAYLOAD_SEGMENT_ENTRY  0x52544E45
442
443 #define CBFS_COMPRESS_NONE  0
444
445 struct cbfs_payload {
446     struct cbfs_payload_segment segments[1];
447 };
448
449 void
450 cbfs_run_payload(const char *filename)
451 {
452     dprintf(1, "Run %s\n", filename);
453     struct cbfs_file *file = cbfs_findfile(filename);
454     if (!file)
455         return;
456     struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
457     struct cbfs_payload_segment *seg = pay->segments;
458     for (;;) {
459         if (seg->compression != htonl(CBFS_COMPRESS_NONE)) {
460             dprintf(1, "No support for compressed payloads (%x)\n"
461                     , seg->compression);
462             return;
463         }
464         void *src = (void*)pay + ntohl(seg->offset);
465         void *dest = (void*)ntohl((u32)seg->load_addr);
466         u32 src_len = ntohl(seg->len);
467         u32 dest_len = ntohl(seg->mem_len);
468         switch (seg->type) {
469         case PAYLOAD_SEGMENT_BSS:
470             dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
471             memset(dest, 0, dest_len);
472             break;
473         case PAYLOAD_SEGMENT_ENTRY: {
474             dprintf(1, "Calling addr %p\n", dest);
475             void (*func)() = dest;
476             func();
477             return;
478         }
479         default:
480             dprintf(3, "Segment %x %d@%p -> %d@%p\n"
481                     , seg->type, src_len, src, dest_len, dest);
482             if (src_len > dest_len)
483                 src_len = dest_len;
484             memcpy(dest, src, src_len);
485             if (dest_len > src_len)
486                 memset(dest + src_len, 0, dest_len - src_len);
487             break;
488         }
489         seg++;
490     }
491 }
492
493 void
494 coreboot_setup(void)
495 {
496     coreboot_fill_map();
497     cbfs_setup();
498 }