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 LGPLv3 license.
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
15 /****************************************************************
17 ****************************************************************/
22 struct pir_header *p = pos;
23 if (p->signature != PIR_SIGNATURE)
27 if (p->size < sizeof(*p))
29 if (checksum(pos, p->size) != 0)
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");
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;
43 copy_mptable(void *pos)
45 struct mptable_floating_s *p = pos;
46 if (p->signature != MPTABLE_SIGNATURE)
50 if (checksum(pos, sizeof(*p)) != 0)
52 u32 length = p->length * 16;
53 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
54 if (bios_table_cur_addr + length > bios_table_end_addr) {
55 dprintf(1, "No room to copy MPTABLE!\n");
58 dprintf(1, "Copying MPTABLE from %p to %x\n", pos, bios_table_cur_addr);
59 memcpy((void*)bios_table_cur_addr, pos, length);
60 bios_table_cur_addr += length;
64 copy_acpi_rsdp(void *pos)
68 struct rsdp_descriptor *p = pos;
69 if (p->signature != RSDP_SIGNATURE)
72 if (checksum(pos, length) != 0)
74 if (p->revision > 1) {
76 if (checksum(pos, length) != 0)
79 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
80 if (bios_table_cur_addr + length > bios_table_end_addr) {
81 dprintf(1, "No room to copy ACPI RSDP table!\n");
84 dprintf(1, "Copying ACPI RSDP from %p to %x\n", pos, bios_table_cur_addr);
85 RsdpAddr = (void*)bios_table_cur_addr;
86 memcpy(RsdpAddr, pos, length);
87 bios_table_cur_addr += length;
90 // Attempt to find (and relocate) any standard bios tables found in a
91 // given address range.
93 scan_tables(u32 start, u32 size)
95 void *p = (void*)ALIGN(start, 16);
96 void *end = (void*)start + size;
97 for (; p<end; p += 16) {
105 /****************************************************************
107 ****************************************************************/
118 #define CB_SIGNATURE 0x4f49424C // "LBIO"
120 struct cb_memory_range {
126 #define CB_MEM_TABLE 16
131 struct cb_memory_range map[0];
134 #define CB_TAG_MEMORY 0x01
136 #define MEM_RANGE_COUNT(_rec) \
137 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
140 ipchksum(char *buf, int count)
150 sum = (sum >> 16) + (sum & 0xffff);
155 // Try to locate the coreboot header in a given address range.
156 static struct cb_header *
157 find_cb_header(char *addr, int len)
159 char *end = addr + len;
160 for (; addr < end; addr += 16) {
161 struct cb_header *cbh = (struct cb_header *)addr;
162 if (cbh->signature != CB_SIGNATURE)
164 if (! cbh->table_bytes)
166 if (ipchksum(addr, sizeof(*cbh)) != 0)
168 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
169 != cbh->table_checksum)
176 // Try to find the coreboot memory table in the given coreboot table.
178 find_cb_subtable(struct cb_header *cbh, u32 tag)
180 char *tbl = (char *)cbh + sizeof(*cbh);
182 for (i=0; i<cbh->table_entries; i++) {
183 struct cb_memory *cbm = (struct cb_memory *)tbl;
191 // Populate max ram and e820 map info by scanning for a coreboot table.
195 dprintf(3, "Attempting to find coreboot table\n");
197 // Init variables set in coreboot table memory scan.
201 // Find coreboot table.
202 struct cb_header *cbh = find_cb_header(0, 0x1000);
205 struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY);
209 u64 maxram = 0, maxram_over4G = 0;
210 int i, count = MEM_RANGE_COUNT(cbm);
211 for (i=0; i<count; i++) {
212 struct cb_memory_range *m = &cbm->map[i];
214 if (type == CB_MEM_TABLE) {
215 type = E820_RESERVED;
216 scan_tables(m->start, m->size);
217 } else if (type == E820_ACPI || type == E820_RAM) {
218 u64 end = m->start + m->size;
219 if (end > 0x100000000ull) {
220 end -= 0x100000000ull;
221 if (end > maxram_over4G)
223 } else if (end > maxram)
226 add_e820(m->start, m->size, type);
230 RamSizeOver4G = maxram_over4G;
232 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
233 // confuses grub. So, override it.
234 add_e820(0, 16*1024, E820_RAM);
236 // XXX - just create dummy smbios table for now - should detect if
237 // smbios/dmi table is found from coreboot and use that instead.
243 // No table found.. Use 16Megs as a dummy value.
244 dprintf(1, "Unable to find coreboot table!\n");
245 RamSize = 16*1024*1024;
247 add_e820(0, 16*1024*1024, E820_RAM);