Add support for more than 4Gig of ram.
[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 GPLv3 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 "biosvar.h" // GET_EBDA
12
13
14 /****************************************************************
15  * BIOS table copying
16  ****************************************************************/
17
18 static void
19 copy_pir(void *pos)
20 {
21     struct pir_header *p = pos;
22     if (p->signature != PIR_SIGNATURE)
23         return;
24     if (GET_EBDA(pir_loc))
25         return;
26     if (p->size < sizeof(*p))
27         return;
28     if (checksum(pos, p->size) != 0)
29         return;
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");
33         return;
34     }
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     SET_EBDA(pir_loc, bios_table_cur_addr);
38     bios_table_cur_addr += p->size;
39 }
40
41 static void
42 copy_mptable(void *pos)
43 {
44     struct mptable_floating_s *p = pos;
45     if (p->signature != MPTABLE_SIGNAURE)
46         return;
47     if (!p->physaddr)
48         return;
49     if (checksum(pos, sizeof(*p)) != 0)
50         return;
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");
55         return;
56     }
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     SET_EBDA(pir_loc, bios_table_cur_addr);
60     bios_table_cur_addr += length;
61 }
62
63 static void
64 copy_acpi_rsdp(void *pos)
65 {
66     if (*(u64*)pos != RSDP_SIGNATURE)
67         return;
68     struct rsdp_descriptor *p = pos;
69     u32 length = 20;
70     if (checksum(pos, length) != 0)
71         return;
72     if (p->revision > 1) {
73         length = p->length;
74         if (checksum(pos, length) != 0)
75             return;
76     }
77     bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
78     if (bios_table_cur_addr + length > bios_table_end_addr) {
79         dprintf(1, "No room to copy ACPI RSDP table!\n");
80         return;
81     }
82     dprintf(1, "Copying ACPI RSDP from %p to %x\n", pos, bios_table_cur_addr);
83     memcpy((void*)bios_table_cur_addr, pos, length);
84     bios_table_cur_addr += length;
85 }
86
87 // Attempt to find (and relocate) any standard bios tables found in a
88 // given address range.
89 static void
90 scan_tables(u32 start, u32 size)
91 {
92     void *p = (void*)ALIGN(start, 16);
93     void *end = (void*)start + size;
94     for (; p<end; p += 16) {
95         copy_pir(p);
96         copy_mptable(p);
97         copy_acpi_rsdp(p);
98     }
99 }
100
101
102 /****************************************************************
103  * Memory map
104  ****************************************************************/
105
106 struct cb_header {
107     u32 signature;
108     u32 header_bytes;
109     u32 header_checksum;
110     u32 table_bytes;
111     u32 table_checksum;
112     u32 table_entries;
113 };
114
115 #define CB_SIGNATURE 0x4f49424C // "LBIO"
116
117 struct cb_memory_range {
118     u64 start;
119     u64 size;
120     u32 type;
121 };
122
123 #define CB_MEM_TABLE    16
124
125 struct cb_memory {
126     u32 tag;
127     u32 size;
128     struct cb_memory_range map[0];
129 };
130
131 #define CB_TAG_MEMORY 0x01
132
133 #define MEM_RANGE_COUNT(_rec) \
134         (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
135
136 static u16
137 ipchksum(char *buf, int count)
138 {
139     u16 *p = (u16*)buf;
140     u32 sum = 0;
141     while (count > 1) {
142         sum += *p++;
143         count -= 2;
144     }
145     if (count)
146         sum += *(u8*)p;
147     sum = (sum >> 16) + (sum & 0xffff);
148     sum += (sum >> 16);
149     return ~sum;
150 }
151
152 // Try to locate the coreboot header in a given address range.
153 static struct cb_header *
154 find_cb_header(char *addr, int len)
155 {
156     char *end = addr + len;
157     for (; addr < end; addr += 16) {
158         struct cb_header *cbh = (struct cb_header *)addr;
159         if (cbh->signature != CB_SIGNATURE)
160             continue;
161         if (! cbh->table_bytes)
162             continue;
163         if (ipchksum(addr, sizeof(*cbh)) != 0)
164             continue;
165         if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
166             != cbh->table_checksum)
167             continue;
168         return cbh;
169     }
170     return NULL;
171 }
172
173 // Try to find the coreboot memory table in the given coreboot table.
174 static void *
175 find_cb_subtable(struct cb_header *cbh, u32 tag)
176 {
177     char *tbl = (char *)cbh + sizeof(*cbh);
178     int i;
179     for (i=0; i<cbh->table_entries; i++) {
180         struct cb_memory *cbm = (struct cb_memory *)tbl;
181         tbl += cbm->size;
182         if (cbm->tag == tag)
183             return cbm;
184     }
185     return NULL;
186 }
187
188 // Populate max ram and e820 map info by scanning for a coreboot table.
189 void
190 coreboot_fill_map()
191 {
192     dprintf(3, "Attempting to find coreboot table\n");
193     struct cb_header *cbh = find_cb_header(0, 0x1000);
194     if (!cbh)
195         goto fail;
196     struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY);
197     if (!cbm)
198         goto fail;
199
200     u64 maxram = 0, maxram_over4G = 0;
201     int i, count = MEM_RANGE_COUNT(cbm);
202     for (i=0; i<count; i++) {
203         struct cb_memory_range *m = &cbm->map[i];
204         u32 type = m->type;
205         if (type == CB_MEM_TABLE) {
206             type = E820_RESERVED;
207             scan_tables(m->start, m->size);
208         } else if (type == E820_ACPI || type == E820_RAM) {
209             u64 end = m->start + m->size;
210             if (end > 0x100000000ull) {
211                 end -= 0x100000000ull;
212                 if (end > maxram_over4G)
213                     maxram_over4G = end;
214             } else if (end > maxram)
215                 maxram = end;
216         }
217         add_e820(m->start, m->size, type);
218     }
219
220     SET_EBDA(ram_size, maxram);
221     SET_EBDA(ram_size_over4G, maxram_over4G);
222
223     // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
224     // confuses grub.  So, override it.
225     add_e820(0, 16*1024, E820_RAM);
226
227     // XXX - just create dummy smbios table for now - should detect if
228     // smbios/dmi table is found from coreboot and use that instead.
229     smbios_init();
230
231     return;
232
233 fail:
234     // No table found..  Use 16Megs as a dummy value.
235     dprintf(1, "Unable to find coreboot table!\n");
236     SET_EBDA(ram_size, 16*1024*1024);
237     add_e820(0, 16*1024*1024, E820_RAM);
238     return;
239 }