dd589acee2b823a1b458b61cb393103d287deee7
[coreboot.git] / payloads / coreinfo / coreboot_module.c
1 /*
2  * This file is part of the coreinfo project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <coreboot_tables.h>
21 #include "coreinfo.h"
22
23 #ifdef CONFIG_MODULE_COREBOOT
24
25 #define MAX_MEMORY_COUNT 5
26
27 static struct {
28         int mem_count;
29         int mem_actual;
30
31         struct cb_memory_range range[MAX_MEMORY_COUNT];
32
33         char vendor[32];
34         char part[32];
35
36         char strings[10][64];
37
38         struct cb_serial serial;
39         struct cb_console console;
40 } cb_info;
41
42 static int tables_good = 0;
43
44 int coreboot_module_redraw(WINDOW *win)
45 {
46         int row = 2;
47         int i;
48
49         print_module_title(win, "Coreboot Tables");
50
51         if (tables_good) {
52                 mvwprintw(win, row++, 1, "No Coreboot tables were found");
53                 return 0;
54         }
55
56         mvwprintw(win, row++, 1, "Vendor: %s", cb_info.vendor);
57         mvwprintw(win, row++, 1, "Part: %s", cb_info.part);
58
59         mvwprintw(win, row++, 1, "Version: %s%s",
60                   cb_info.strings[CB_TAG_VERSION - 0x4],
61                   cb_info.strings[CB_TAG_EXTRA_VERSION - 0x4]);
62
63         mvwprintw(win, row++, 1, "Built: %s (%s@%s.%s)",
64                   cb_info.strings[CB_TAG_BUILD - 0x4],
65                   cb_info.strings[CB_TAG_COMPILE_BY - 0x04],
66                   cb_info.strings[CB_TAG_COMPILE_HOST - 0x04],
67                   cb_info.strings[CB_TAG_COMPILE_DOMAIN - 0x04]);
68
69         if (cb_info.serial.tag != 0x0) {
70                 mvwprintw(win, row++, 1, "Serial Port I/O base: 0x%x",
71                           cb_info.serial.baseaddr);
72         }
73
74         if (cb_info.console.tag != 0x0) {
75                 mvwprintw(win, row++, 1, "Default Output Console: ");
76
77                 switch (cb_info.console.type) {
78                 case CB_TAG_CONSOLE_SERIAL8250:
79                         wprintw(win, "Serial Port");
80                         break;
81                 case CB_TAG_CONSOLE_VGA:
82                         wprintw(win, "VGA");
83                         break;
84                 case CB_TAG_CONSOLE_BTEXT:
85                         wprintw(win, "BTEXT");
86                         break;
87                 case CB_TAG_CONSOLE_LOGBUF:
88                         wprintw(win, "Log Buffer");
89                         break;
90                 case CB_TAG_CONSOLE_SROM:
91                         wprintw(win, "Serial ROM");
92                         break;
93                 case CB_TAG_CONSOLE_EHCI:
94                         wprintw(win, "USB Debug");
95                         break;
96                 }
97         }
98
99         row++;
100         mvwprintw(win, row++, 1, "-- Memory Map --");
101
102         for (i = 0; i < cb_info.mem_count; i++) {
103                 switch (cb_info.range[i].type) {
104                 case CB_MEM_RAM:
105                         mvwprintw(win, row++, 3, "     RAM: ");
106                         break;
107                 case CB_MEM_RESERVED:
108                         mvwprintw(win, row++, 3, "Reserved: ");
109                         break;
110                 case CB_MEM_TABLE:
111                         mvwprintw(win, row++, 3, "   Table: ");
112                 }
113
114                 wprintw(win, "%16.16llx - %16.16llx",
115                         UNPACK_CB64(cb_info.range[i].start),
116                         UNPACK_CB64(cb_info.range[i].start) +
117                         UNPACK_CB64(cb_info.range[i].size) - 1);
118         }
119
120         return 0;
121 }
122
123 static void parse_memory(unsigned char *ptr)
124 {
125         struct cb_memory *mem = (struct cb_memory *)ptr;
126         int max = (MEM_RANGE_COUNT(mem) > MAX_MEMORY_COUNT)
127             ? MAX_MEMORY_COUNT : MEM_RANGE_COUNT(mem);
128         int i;
129
130         for (i = 0; i < max; i++) {
131                 struct cb_memory_range *range =
132                     (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
133
134                 memcpy(&cb_info.range[i], range, sizeof(*range));
135         }
136
137         cb_info.mem_count = max;
138         cb_info.mem_actual = MEM_RANGE_COUNT(mem);
139 }
140
141 static void parse_mainboard(unsigned char *ptr)
142 {
143         struct cb_mainboard *mb = (struct cb_mainboard *)ptr;
144
145         strncpy(cb_info.vendor, (const char *)MB_VENDOR_STRING(mb), 31);
146         strncpy(cb_info.part, (const char *)MB_PART_STRING(mb), 31);
147 }
148
149 static void parse_strings(unsigned char *ptr)
150 {
151         struct cb_string *string = (struct cb_string *)ptr;
152         int index = string->tag - CB_TAG_VERSION;
153
154         strncpy(cb_info.strings[index], (const char *)string->string, 63);
155         cb_info.strings[index][63] = 0;
156 }
157
158 static void parse_serial(unsigned char *ptr)
159 {
160         memcpy(&cb_info.serial, (struct cb_serial *)ptr,
161                sizeof(struct cb_serial));
162 }
163
164 static void parse_console(unsigned char *ptr)
165 {
166         memcpy(&cb_info.console, (struct cb_console *)ptr,
167                sizeof(struct cb_console));
168 }
169
170 static int parse_header(void *addr, int len)
171 {
172         struct cb_header *header;
173         unsigned char *ptr = (unsigned char *)addr;
174         int i;
175
176         for (i = 0; i < len; i += 16, ptr += 16) {
177                 header = (struct cb_header *)ptr;
178
179                 if (!strncmp((const char *)header->signature, "LBIO", 4))
180                         break;
181         }
182
183         /* We walked the entire space and didn't find anything. */
184         if (i >= len)
185                 return -1;
186
187         if (!header->table_bytes)
188                 return 0;
189
190         /* FIXME: Check the checksum. */
191
192         if (ipchksum((uint16_t *) header, sizeof(*header)))
193                 return -1;
194
195         if (ipchksum((uint16_t *) (ptr + sizeof(*header)), header->table_bytes)
196             != header->table_checksum)
197                 return -1;
198
199         /* Now, walk the tables. */
200         ptr += header->header_bytes;
201
202         for (i = 0; i < header->table_entries; i++) {
203                 struct cb_record *rec = (struct cb_record *)ptr;
204
205                 switch (rec->tag) {
206                 case CB_TAG_FORWARD:
207                         return parse_header((void *)(unsigned long)((struct cb_forward *)rec)->forward, 1);
208                         break;
209                 case CB_TAG_MEMORY:
210                         parse_memory(ptr);
211                         break;
212                 case CB_TAG_MAINBOARD:
213                         parse_mainboard(ptr);
214                         break;
215                 case CB_TAG_VERSION:
216                 case CB_TAG_EXTRA_VERSION:
217                 case CB_TAG_BUILD:
218                 case CB_TAG_COMPILE_TIME:
219                 case CB_TAG_COMPILE_BY:
220                 case CB_TAG_COMPILE_HOST:
221                 case CB_TAG_COMPILE_DOMAIN:
222                 case CB_TAG_COMPILER:
223                 case CB_TAG_LINKER:
224                 case CB_TAG_ASSEMBLER:
225                         parse_strings(ptr);
226                         break;
227                 case CB_TAG_SERIAL:
228                         parse_serial(ptr);
229                         break;
230                 case CB_TAG_CONSOLE:
231                         parse_console(ptr);
232                         break;
233                 default:
234                         break;
235                 }
236
237                 ptr += rec->size;
238         }
239
240         return 1;
241 }
242
243 static int coreboot_module_init(void)
244 {
245         int ret = parse_header((void *)0x00000, 0x1000);
246
247         if (ret != 1)
248                 ret = parse_header((void *)0xf0000, 0x1000);
249
250         /* Return error if we couldn't find it at either address. */
251         tables_good = (ret == 1) ? 0 : -1;
252         return tables_good;
253 }
254
255 struct coreinfo_module coreboot_module = {
256         .name = "Coreboot",
257         .init = coreboot_module_init,
258         .redraw = coreboot_module_redraw,
259 };
260
261 #else
262
263 struct coreinfo_module coreboot_module = {
264 };
265
266 #endif