Enhance e820 memory map generation - include files missing from last commit.
[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
10
11 /****************************************************************
12  * Memory interface
13  ****************************************************************/
14
15 struct cb_header {
16     u32 signature;
17     u32 header_bytes;
18     u32 header_checksum;
19     u32 table_bytes;
20     u32 table_checksum;
21     u32 table_entries;
22 };
23
24 #define CB_SIGNATURE 0x4f49424C // "LBIO"
25
26 struct cb_memory_range {
27     u64 start;
28     u64 size;
29     u32 type;
30 };
31
32 #define CB_MEM_TABLE    16
33
34 struct cb_memory {
35     u32 tag;
36     u32 size;
37     struct cb_memory_range map[0];
38 };
39
40 #define CB_TAG_MEMORY 0x01
41
42 #define MEM_RANGE_COUNT(_rec) \
43         (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
44
45 static u16
46 ipchksum(char *buf, int count)
47 {
48     u16 *p = (u16*)buf;
49     u32 sum = 0;
50     while (count > 1) {
51         sum += *p++;
52         count -= 2;
53     }
54     if (count)
55         sum += *(u8*)p;
56     sum = (sum >> 16) + (sum & 0xffff);
57     sum += (sum >> 16);
58     return ~sum;
59 }
60
61 // Try to locate the coreboot header in a given address range.
62 static struct cb_header *
63 find_cb_header(char *addr, int len)
64 {
65     char *end = addr + len;
66     for (; addr < end; addr += 16) {
67         struct cb_header *cbh = (struct cb_header *)addr;
68         if (cbh->signature != CB_SIGNATURE)
69             continue;
70         dprintf(1, "sig %p=%x\n", addr, cbh->signature);
71         if (! cbh->table_bytes)
72             continue;
73         if (ipchksum(addr, sizeof(*cbh)) != 0)
74             continue;
75         if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
76             != cbh->table_checksum)
77             continue;
78         return cbh;
79     }
80     return NULL;
81 }
82
83 // Try to find the coreboot memory table in the given coreboot table.
84 static struct cb_memory *
85 find_cb_memory(struct cb_header *cbh)
86 {
87     char *tbl = (char *)cbh + sizeof(*cbh);
88     int i;
89     for (i=0; i<cbh->table_entries; i++) {
90         struct cb_memory *cbm = (struct cb_memory *)tbl;
91         tbl += cbm->size;
92         if (cbm->tag == CB_TAG_MEMORY)
93             return cbm;
94     }
95     return NULL;
96 }
97
98 // Populate max ram and e820 map info by scanning for a coreboot table.
99 void
100 coreboot_fill_map()
101 {
102     dprintf(3, "Attempting to find coreboot table\n");
103     struct cb_header *cbh = find_cb_header(0, 0x1000);
104     if (!cbh)
105         goto fail;
106     struct cb_memory *cbm = find_cb_memory(cbh);
107     if (!cbm)
108         goto fail;
109
110     u64 maxram = 0;
111     int i, count = MEM_RANGE_COUNT(cbm);
112     for (i=0; i<count; i++) {
113         struct cb_memory_range *m = &cbm->map[i];
114         u32 type = m->type;
115         if (type == CB_MEM_TABLE)
116             type = E820_RESERVED;
117         if ((type == E820_ACPI || type == E820_RAM)
118             && (m->start + m->size) > maxram)
119             maxram = m->start + m->size;
120         add_e820(m->start, m->size, type);
121     }
122
123     // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
124     // confuses grub.  So, override it.
125     add_e820(0, 16*1024, E820_RAM);
126
127     SET_EBDA(ram_size, maxram);
128     return;
129
130 fail:
131     // No table found..  Use 16Megs as a dummy value.
132     dprintf(1, "Unable to find coreboot table!\n");
133     SET_EBDA(ram_size, 16*1024*1024);
134     add_e820(0, 16*1024*1024, E820_RAM);
135     return;
136 }