Copy mptable floater and config tables when on coreboot.
[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 static u16
147 ipchksum(char *buf, int count)
148 {
149     u16 *p = (u16*)buf;
150     u32 sum = 0;
151     while (count > 1) {
152         sum += *p++;
153         count -= 2;
154     }
155     if (count)
156         sum += *(u8*)p;
157     sum = (sum >> 16) + (sum & 0xffff);
158     sum += (sum >> 16);
159     return ~sum;
160 }
161
162 // Try to locate the coreboot header in a given address range.
163 static struct cb_header *
164 find_cb_header(char *addr, int len)
165 {
166     char *end = addr + len;
167     for (; addr < end; addr += 16) {
168         struct cb_header *cbh = (struct cb_header *)addr;
169         if (cbh->signature != CB_SIGNATURE)
170             continue;
171         if (! cbh->table_bytes)
172             continue;
173         if (ipchksum(addr, sizeof(*cbh)) != 0)
174             continue;
175         if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
176             != cbh->table_checksum)
177             continue;
178         return cbh;
179     }
180     return NULL;
181 }
182
183 // Try to find the coreboot memory table in the given coreboot table.
184 static void *
185 find_cb_subtable(struct cb_header *cbh, u32 tag)
186 {
187     char *tbl = (char *)cbh + sizeof(*cbh);
188     int i;
189     for (i=0; i<cbh->table_entries; i++) {
190         struct cb_memory *cbm = (struct cb_memory *)tbl;
191         tbl += cbm->size;
192         if (cbm->tag == tag)
193             return cbm;
194     }
195     return NULL;
196 }
197
198 // Populate max ram and e820 map info by scanning for a coreboot table.
199 void
200 coreboot_fill_map()
201 {
202     dprintf(3, "Attempting to find coreboot table\n");
203
204     // Init variables set in coreboot table memory scan.
205     PirOffset = 0;
206     RsdpAddr = 0;
207
208     // Find coreboot table.
209     struct cb_header *cbh = find_cb_header(0, 0x1000);
210     if (!cbh)
211         goto fail;
212     struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY);
213     if (!cbm)
214         goto fail;
215
216     u64 maxram = 0, maxram_over4G = 0;
217     int i, count = MEM_RANGE_COUNT(cbm);
218     for (i=0; i<count; i++) {
219         struct cb_memory_range *m = &cbm->map[i];
220         u32 type = m->type;
221         if (type == CB_MEM_TABLE) {
222             type = E820_RESERVED;
223             scan_tables(m->start, m->size);
224         } else if (type == E820_ACPI || type == E820_RAM) {
225             u64 end = m->start + m->size;
226             if (end > 0x100000000ull) {
227                 end -= 0x100000000ull;
228                 if (end > maxram_over4G)
229                     maxram_over4G = end;
230             } else if (end > maxram)
231                 maxram = end;
232         }
233         add_e820(m->start, m->size, type);
234     }
235
236     RamSize = maxram;
237     RamSizeOver4G = maxram_over4G;
238
239     // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
240     // confuses grub.  So, override it.
241     add_e820(0, 16*1024, E820_RAM);
242
243     // XXX - just create dummy smbios table for now - should detect if
244     // smbios/dmi table is found from coreboot and use that instead.
245     smbios_init();
246
247     return;
248
249 fail:
250     // No table found..  Use 16Megs as a dummy value.
251     dprintf(1, "Unable to find coreboot table!\n");
252     RamSize = 16*1024*1024;
253     RamSizeOver4G = 0;
254     add_e820(0, 16*1024*1024, E820_RAM);
255     return;
256 }