1 // Coreboot interface support.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU GPLv3 license.
7 #include "memmap.h" // add_e820
8 #include "util.h" // dprintf
11 /****************************************************************
13 ****************************************************************/
24 #define CB_SIGNATURE 0x4f49424C // "LBIO"
26 struct cb_memory_range {
32 #define CB_MEM_TABLE 16
37 struct cb_memory_range map[0];
40 #define CB_TAG_MEMORY 0x01
42 #define MEM_RANGE_COUNT(_rec) \
43 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
46 ipchksum(char *buf, int count)
56 sum = (sum >> 16) + (sum & 0xffff);
61 // Try to locate the coreboot header in a given address range.
62 static struct cb_header *
63 find_cb_header(char *addr, int len)
65 char *end = addr + len;
66 for (; addr < end; addr += 16) {
67 struct cb_header *cbh = (struct cb_header *)addr;
68 if (cbh->signature != CB_SIGNATURE)
70 dprintf(1, "sig %p=%x\n", addr, cbh->signature);
71 if (! cbh->table_bytes)
73 if (ipchksum(addr, sizeof(*cbh)) != 0)
75 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
76 != cbh->table_checksum)
83 // Try to find the coreboot memory table in the given coreboot table.
84 static struct cb_memory *
85 find_cb_memory(struct cb_header *cbh)
87 char *tbl = (char *)cbh + sizeof(*cbh);
89 for (i=0; i<cbh->table_entries; i++) {
90 struct cb_memory *cbm = (struct cb_memory *)tbl;
92 if (cbm->tag == CB_TAG_MEMORY)
98 // Populate max ram and e820 map info by scanning for a coreboot table.
102 dprintf(3, "Attempting to find coreboot table\n");
103 struct cb_header *cbh = find_cb_header(0, 0x1000);
106 struct cb_memory *cbm = find_cb_memory(cbh);
111 int i, count = MEM_RANGE_COUNT(cbm);
112 for (i=0; i<count; i++) {
113 struct cb_memory_range *m = &cbm->map[i];
115 if (type == CB_MEM_TABLE)
116 type = E820_RESERVED;
117 if ((type == E820_ACPI || type == E820_RAM)
118 && (m->start + m->size) > maxram)
119 maxram = m->start + m->size;
120 add_e820(m->start, m->size, type);
123 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
124 // confuses grub. So, override it.
125 add_e820(0, 16*1024, E820_RAM);
127 SET_EBDA(ram_size, maxram);
131 // No table found.. Use 16Megs as a dummy value.
132 dprintf(1, "Unable to find coreboot table!\n");
133 SET_EBDA(ram_size, 16*1024*1024);
134 add_e820(0, 16*1024*1024, E820_RAM);