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
13 /****************************************************************
15 ****************************************************************/
20 struct pir_header *p = pos;
21 if (p->signature != PIR_SIGNATURE)
23 if (GET_EBDA(pir_loc))
25 if (p->size < sizeof(*p))
27 if (checksum(pos, p->size) != 0)
29 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
30 if (bios_table_cur_addr + p->size > bios_table_end_addr) {
31 dprintf(1, "No room to copy PIR table!\n");
34 dprintf(1, "Copying PIR from %p to %x\n", pos, bios_table_cur_addr);
35 memcpy((void*)bios_table_cur_addr, pos, p->size);
36 SET_EBDA(pir_loc, bios_table_cur_addr);
37 bios_table_cur_addr += p->size;
41 copy_mptable(void *pos)
43 struct mptable_floating_s *p = pos;
44 if (p->signature != MPTABLE_SIGNAURE)
48 if (checksum(pos, sizeof(*p)) != 0)
50 u32 length = p->length * 16;
51 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
52 if (bios_table_cur_addr + length > bios_table_end_addr) {
53 dprintf(1, "No room to copy MPTABLE!\n");
56 dprintf(1, "Copying MPTABLE from %p to %x\n", pos, bios_table_cur_addr);
57 memcpy((void*)bios_table_cur_addr, pos, length);
58 SET_EBDA(pir_loc, bios_table_cur_addr);
59 bios_table_cur_addr += length;
63 copy_acpi_rsdp(void *pos)
65 if (*(u64*)pos != RSDP_SIGNATURE)
67 struct rsdp_descriptor *p = pos;
69 if (checksum(pos, length) != 0)
71 if (p->revision > 1) {
73 if (checksum(pos, length) != 0)
76 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
77 if (bios_table_cur_addr + length > bios_table_end_addr) {
78 dprintf(1, "No room to copy ACPI RSDP table!\n");
81 dprintf(1, "Copying ACPI RSDP from %p to %x\n", pos, bios_table_cur_addr);
82 memcpy((void*)bios_table_cur_addr, pos, length);
83 bios_table_cur_addr += length;
86 // Attempt to find (and relocate) any standard bios tables found in a
87 // given address range.
89 scan_tables(u32 start, u32 size)
91 void *p = (void*)ALIGN(start, 16);
92 void *end = (void*)start + size;
93 for (; p<end; p += 16) {
101 /****************************************************************
103 ****************************************************************/
114 #define CB_SIGNATURE 0x4f49424C // "LBIO"
116 struct cb_memory_range {
122 #define CB_MEM_TABLE 16
127 struct cb_memory_range map[0];
130 #define CB_TAG_MEMORY 0x01
132 #define MEM_RANGE_COUNT(_rec) \
133 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
136 ipchksum(char *buf, int count)
146 sum = (sum >> 16) + (sum & 0xffff);
151 // Try to locate the coreboot header in a given address range.
152 static struct cb_header *
153 find_cb_header(char *addr, int len)
155 char *end = addr + len;
156 for (; addr < end; addr += 16) {
157 struct cb_header *cbh = (struct cb_header *)addr;
158 if (cbh->signature != CB_SIGNATURE)
160 if (! cbh->table_bytes)
162 if (ipchksum(addr, sizeof(*cbh)) != 0)
164 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
165 != cbh->table_checksum)
172 // Try to find the coreboot memory table in the given coreboot table.
174 find_cb_subtable(struct cb_header *cbh, u32 tag)
176 char *tbl = (char *)cbh + sizeof(*cbh);
178 for (i=0; i<cbh->table_entries; i++) {
179 struct cb_memory *cbm = (struct cb_memory *)tbl;
187 // Populate max ram and e820 map info by scanning for a coreboot table.
191 dprintf(3, "Attempting to find coreboot table\n");
192 struct cb_header *cbh = find_cb_header(0, 0x1000);
195 struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY);
200 int i, count = MEM_RANGE_COUNT(cbm);
201 for (i=0; i<count; i++) {
202 struct cb_memory_range *m = &cbm->map[i];
204 if (type == CB_MEM_TABLE) {
205 type = E820_RESERVED;
206 scan_tables(m->start, m->size);
208 if ((type == E820_ACPI || type == E820_RAM)
209 && (m->start + m->size) > maxram)
210 maxram = m->start + m->size;
211 add_e820(m->start, m->size, type);
214 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
215 // confuses grub. So, override it.
216 add_e820(0, 16*1024, E820_RAM);
218 SET_EBDA(ram_size, maxram);
222 // No table found.. Use 16Megs as a dummy value.
223 dprintf(1, "Unable to find coreboot table!\n");
224 SET_EBDA(ram_size, 16*1024*1024);
225 add_e820(0, 16*1024*1024, E820_RAM);