Cosmetic changes and coding style fixes by running 'indent', with some
[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 #define MAX_MEMORY_COUNT 5
24
25 static struct {
26         int mem_count;
27         int mem_actual;
28
29         struct cb_memory_range range[MAX_MEMORY_COUNT];
30
31         char vendor[32];
32         char part[32];
33
34         char strings[10][64];
35
36         struct cb_serial serial;
37         struct cb_console console;
38 } cb_info;
39
40 static int tables_good = 0;
41
42 int coreboot_module_redraw(WINDOW *win)
43 {
44         int row = 2;
45         int i;
46
47         print_module_title(win, "Coreboot Tables");
48
49         if (tables_good) {
50                 mvwprintw(win, row++, 2, "No Coreboot tables were found");
51                 return 0;
52         }
53
54         mvwprintw(win, row++, 2, "Vendor: %s", cb_info.vendor);
55         mvwprintw(win, row++, 2, "Part: %s", cb_info.part);
56
57         mvwprintw(win, row++, 2, "Version: %s%s",
58                   cb_info.strings[CB_TAG_VERSION - 0x4],
59                   cb_info.strings[CB_TAG_EXTRA_VERSION - 0x4]);
60
61         mvwprintw(win, row++, 2, "Built: %s (%s@%s.%s)",
62                   cb_info.strings[CB_TAG_BUILD - 0x4],
63                   cb_info.strings[CB_TAG_COMPILE_BY - 0x04],
64                   cb_info.strings[CB_TAG_COMPILE_HOST - 0x04],
65                   cb_info.strings[CB_TAG_COMPILE_DOMAIN - 0x04]);
66
67         if (cb_info.serial.tag != 0x0) {
68                 mvwprintw(win, row++, 2, "Serial Port I/O base: 0x%x",
69                           cb_info.serial.ioport);
70         }
71
72         if (cb_info.console.tag != 0x0) {
73                 mvwprintw(win, row++, 2, "Default Output Console: ");
74
75                 switch (cb_info.console.type) {
76                 case CB_TAG_CONSOLE_SERIAL8250:
77                         wprintw(win, "Serial Port");
78                         break;
79                 case CB_TAG_CONSOLE_VGA:
80                         wprintw(win, "VGA");
81                         break;
82                 case CB_TAG_CONSOLE_BTEXT:
83                         wprintw(win, "BTEXT");
84                         break;
85                 case CB_TAG_CONSOLE_LOGBUF:
86                         wprintw(win, "Log Buffer");
87                         break;
88                 case CB_TAG_CONSOLE_SROM:
89                         wprintw(win, "Serial ROM");
90                         break;
91                 case CB_TAG_CONSOLE_EHCI:
92                         wprintw(win, "USB Debug");
93                         break;
94                 }
95         }
96
97         row++;
98         mvwprintw(win, row++, 2, "-- Memory Map --");
99
100         for (i = 0; i < cb_info.mem_count; i++) {
101                 switch (cb_info.range[i].type) {
102                 case CB_MEM_RAM:
103                         mvwprintw(win, row++, 4, "     RAM: ");
104                         break;
105                 case CB_MEM_RESERVED:
106                         mvwprintw(win, row++, 4, "Reserved: ");
107                         break;
108                 case CB_MEM_TABLE:
109                         mvwprintw(win, row++, 4, "   Table: ");
110                 }
111
112                 wprintw(win, "%16.16llx - %16.16llx",
113                         UNPACK_CB64(cb_info.range[i].start),
114                         UNPACK_CB64(cb_info.range[i].start) +
115                         UNPACK_CB64(cb_info.range[i].size) - 1);
116         }
117 }
118
119 static void parse_memory(unsigned char *ptr)
120 {
121         struct cb_memory *mem = (struct cb_memory *)ptr;
122
123         int max = (MEM_RANGE_COUNT(mem) > MAX_MEMORY_COUNT)
124             ? MAX_MEMORY_COUNT : MEM_RANGE_COUNT(mem);
125         int i;
126
127         for (i = 0; i < max; i++) {
128                 struct cb_memory_range *range =
129                     (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
130
131                 memcpy(&cb_info.range[i], range, sizeof(*range));
132         }
133
134         cb_info.mem_count = max;
135         cb_info.mem_actual = MEM_RANGE_COUNT(mem);
136 }
137
138 static void parse_mainboard(unsigned char *ptr)
139 {
140         struct cb_mainboard *mb = (struct cb_mainboard *)ptr;
141
142         strncpy(cb_info.vendor, MB_VENDOR_STRING(mb), 31);
143         strncpy(cb_info.part, MB_PART_STRING(mb), 31);
144 }
145
146 static void parse_strings(unsigned char *ptr)
147 {
148         struct cb_string *string = (struct cb_string *)ptr;
149         int index = string->tag - CB_TAG_VERSION;
150
151         strncpy(cb_info.strings[index], string->string, 63);
152         cb_info.strings[index][63] = 0;
153 }
154
155 static void parse_serial(unsigned char *ptr)
156 {
157         memcpy(&cb_info.serial, (struct cb_serial *)ptr,
158                sizeof(struct cb_serial));
159 }
160
161 static void parse_console(unsigned char *ptr)
162 {
163         memcpy(&cb_info.console, (struct cb_console *)ptr,
164                sizeof(struct cb_console));
165 }
166
167 static int parse_header(void *addr, int len)
168 {
169         struct cb_header *header;
170         unsigned char *ptr = (unsigned char *)addr;
171         int i;
172
173         for (i = 0; i < len; i += 16, ptr += 16) {
174                 header = (struct cb_header *)ptr;
175
176                 if (!strncmp(header->signature, "LBIO", 4))
177                         break;
178         }
179
180         /* We walked the entire space and didn't find anything. */
181         if (i >= len)
182                 return -1;
183
184         if (!header->table_bytes)
185                 return 0;
186
187         /* FIXME: Check the checksum. */
188
189         if (ipchksum((uint16_t *) header, sizeof(*header)))
190                 return -1;
191
192         if (ipchksum((uint16_t *) (ptr + sizeof(*header)), header->table_bytes)
193             != header->table_checksum)
194                 return -1;
195
196         /* Now, walk the tables. */
197         ptr += header->header_bytes;
198
199         for (i = 0; i < header->table_entries; i++) {
200                 struct cb_record *rec = (struct cb_record *)ptr;
201
202                 switch (rec->tag) {
203                 case CB_TAG_MEMORY:
204                         parse_memory(ptr);
205                         break;
206                 case CB_TAG_MAINBOARD:
207                         parse_mainboard(ptr);
208                         break;
209                 case CB_TAG_VERSION:
210                 case CB_TAG_EXTRA_VERSION:
211                 case CB_TAG_BUILD:
212                 case CB_TAG_COMPILE_TIME:
213                 case CB_TAG_COMPILE_BY:
214                 case CB_TAG_COMPILE_HOST:
215                 case CB_TAG_COMPILE_DOMAIN:
216                 case CB_TAG_COMPILER:
217                 case CB_TAG_LINKER:
218                 case CB_TAG_ASSEMBLER:
219                         parse_strings(ptr);
220                         break;
221                 case CB_TAG_SERIAL:
222                         parse_serial(ptr);
223                         break;
224                 case CB_TAG_CONSOLE:
225                         parse_console(ptr);
226                         break;
227                 default:
228                         break;
229                 }
230
231                 ptr += rec->size;
232         }
233
234         return 1;
235 }
236
237 int coreboot_module_init(void)
238 {
239         int ret = parse_header((void *)0x00000, 0x1000);
240
241         if (ret != 1)
242                 ret = parse_header((void *)0xf0000, 0x1000);
243
244         /* Return error if we couldn't find it at either address. */
245         tables_good = (ret == 1) ? 0 : -1;
246         return tables_good;
247 }
248
249 struct coreinfo_module coreboot_module = {
250         .name = "Coreboot",
251         .init = coreboot_module_init,
252         .redraw = coreboot_module_redraw,
253 };