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
9 #include "pci.h" // struct pir_header
10 #include "acpi.h" // struct rsdp_descriptor
11 #include "biosvar.h" // GET_EBDA
14 /****************************************************************
16 ****************************************************************/
21 struct pir_header *p = pos;
22 if (p->signature != PIR_SIGNATURE)
26 if (p->size < sizeof(*p))
28 if (checksum(pos, p->size) != 0)
30 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
31 if (bios_table_cur_addr + p->size > bios_table_end_addr) {
32 dprintf(1, "No room to copy PIR table!\n");
35 dprintf(1, "Copying PIR from %p to %x\n", pos, bios_table_cur_addr);
36 memcpy((void*)bios_table_cur_addr, pos, p->size);
37 PirOffset = bios_table_cur_addr - BUILD_BIOS_ADDR;
38 bios_table_cur_addr += p->size;
42 copy_mptable(void *pos)
44 struct mptable_floating_s *p = pos;
45 if (p->signature != MPTABLE_SIGNAURE)
49 if (checksum(pos, sizeof(*p)) != 0)
51 u32 length = p->length * 16;
52 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
53 if (bios_table_cur_addr + length > bios_table_end_addr) {
54 dprintf(1, "No room to copy MPTABLE!\n");
57 dprintf(1, "Copying MPTABLE from %p to %x\n", pos, bios_table_cur_addr);
58 memcpy((void*)bios_table_cur_addr, pos, length);
59 bios_table_cur_addr += length;
63 copy_acpi_rsdp(void *pos)
67 struct rsdp_descriptor *p = pos;
68 if (p->signature != RSDP_SIGNATURE)
71 if (checksum(pos, length) != 0)
73 if (p->revision > 1) {
75 if (checksum(pos, length) != 0)
78 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
79 if (bios_table_cur_addr + length > bios_table_end_addr) {
80 dprintf(1, "No room to copy ACPI RSDP table!\n");
83 dprintf(1, "Copying ACPI RSDP from %p to %x\n", pos, bios_table_cur_addr);
84 RsdpAddr = (void*)bios_table_cur_addr;
85 memcpy(RsdpAddr, pos, length);
86 bios_table_cur_addr += length;
89 // Attempt to find (and relocate) any standard bios tables found in a
90 // given address range.
92 scan_tables(u32 start, u32 size)
94 void *p = (void*)ALIGN(start, 16);
95 void *end = (void*)start + size;
96 for (; p<end; p += 16) {
104 /****************************************************************
106 ****************************************************************/
117 #define CB_SIGNATURE 0x4f49424C // "LBIO"
119 struct cb_memory_range {
125 #define CB_MEM_TABLE 16
130 struct cb_memory_range map[0];
133 #define CB_TAG_MEMORY 0x01
135 #define MEM_RANGE_COUNT(_rec) \
136 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
139 ipchksum(char *buf, int count)
149 sum = (sum >> 16) + (sum & 0xffff);
154 // Try to locate the coreboot header in a given address range.
155 static struct cb_header *
156 find_cb_header(char *addr, int len)
158 char *end = addr + len;
159 for (; addr < end; addr += 16) {
160 struct cb_header *cbh = (struct cb_header *)addr;
161 if (cbh->signature != CB_SIGNATURE)
163 if (! cbh->table_bytes)
165 if (ipchksum(addr, sizeof(*cbh)) != 0)
167 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
168 != cbh->table_checksum)
175 // Try to find the coreboot memory table in the given coreboot table.
177 find_cb_subtable(struct cb_header *cbh, u32 tag)
179 char *tbl = (char *)cbh + sizeof(*cbh);
181 for (i=0; i<cbh->table_entries; i++) {
182 struct cb_memory *cbm = (struct cb_memory *)tbl;
190 // Populate max ram and e820 map info by scanning for a coreboot table.
194 dprintf(3, "Attempting to find coreboot table\n");
195 struct cb_header *cbh = find_cb_header(0, 0x1000);
198 struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY);
202 u64 maxram = 0, maxram_over4G = 0;
203 int i, count = MEM_RANGE_COUNT(cbm);
204 for (i=0; i<count; i++) {
205 struct cb_memory_range *m = &cbm->map[i];
207 if (type == CB_MEM_TABLE) {
208 type = E820_RESERVED;
209 scan_tables(m->start, m->size);
210 } else if (type == E820_ACPI || type == E820_RAM) {
211 u64 end = m->start + m->size;
212 if (end > 0x100000000ull) {
213 end -= 0x100000000ull;
214 if (end > maxram_over4G)
216 } else if (end > maxram)
219 add_e820(m->start, m->size, type);
223 RamSizeOver4G = maxram_over4G;
225 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
226 // confuses grub. So, override it.
227 add_e820(0, 16*1024, E820_RAM);
229 // XXX - just create dummy smbios table for now - should detect if
230 // smbios/dmi table is found from coreboot and use that instead.
236 // No table found.. Use 16Megs as a dummy value.
237 dprintf(1, "Unable to find coreboot table!\n");
238 RamSize = 16*1024*1024;
240 add_e820(0, 16*1024*1024, E820_RAM);