blob: 53f0530c4075c15341976ddd8ec8e8fe535d9d6b [file] [log] [blame]
Patrick Georgi36ade672011-01-28 07:56:39 +00001/*****************************************************************************\
2 * lbtable.c
3 *****************************************************************************
4 * Copyright (C) 2002-2005 The Regents of the University of California.
5 * Produced at the Lawrence Livermore National Laboratory.
6 * Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>
7 * and Stefan Reinauer <stepan@openbios.org>.
8 * UCRL-CODE-2003-012
9 * All rights reserved.
10 *
11 * This file is part of nvramtool, a utility for reading/writing coreboot
12 * parameters and displaying information from the coreboot table.
13 * For details, see http://coreboot.org/nvramtool.
14 *
15 * Please also read the file DISCLAIMER which is included in this software
16 * distribution.
17 *
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License (as published by the
20 * Free Software Foundation) version 2, dated June 1991.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
25 * conditions of the GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30\*****************************************************************************/
31
32#include <arpa/inet.h>
33#include <string.h>
34#include <sys/mman.h>
35#include "common.h"
36#include "coreboot_tables.h"
37#include "ip_checksum.h"
38#include "lbtable.h"
39#include "layout.h"
40#include "cmos_lowlevel.h"
41#include "hexdump.h"
42#include "cbfs.h"
43
44static void process_cmos_table(void);
45static void get_cmos_checksum_info(void);
46static void try_convert_checksum_layout(cmos_checksum_layout_t * layout);
47static void try_add_cmos_table_enum(cmos_enum_t * cmos_enum);
48static void try_add_cmos_table_entry(cmos_entry_t * cmos_entry);
49static const struct cmos_entries *first_cmos_table_entry(void);
50static const struct cmos_entries *next_cmos_table_entry(const struct
51 cmos_entries *last);
52static const struct cmos_enums *first_cmos_table_enum(void);
53static const struct cmos_enums *next_cmos_table_enum
54 (const struct cmos_enums *last);
55static const struct lb_record *first_cmos_rec(uint32_t tag);
56static const struct lb_record *next_cmos_rec(const struct lb_record *last,
57 uint32_t tag);
58
59/* The CMOS option table is located within the coreboot table. It tells us
60 * where the CMOS parameters are located in the nonvolatile RAM.
61 */
62static const struct cmos_option_table *cmos_table = NULL;
63
64void process_layout(void)
65{
66 if ((cmos_table) == NULL) {
67 fprintf(stderr,
68 "%s: CMOS option table not found in coreboot table. "
69 "Apparently, the coreboot installed on this system was "
70 "built without specifying CONFIG_HAVE_OPTION_TABLE.\n",
71 prog_name);
72 exit(1);
73 }
74
75 process_cmos_table();
76 get_cmos_checksum_info();
77}
78
79void get_layout_from_cbfs_file(void)
80{
81 uint32_t len;
82 cmos_table = cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT, &len);
83 process_layout();
84}
85
86/****************************************************************************
87 * process_cmos_table
88 *
89 * Extract layout information from the CMOS option table and store it in our
90 * internal repository.
91 ****************************************************************************/
92static void process_cmos_table(void)
93{
94 const struct cmos_enums *p;
95 const struct cmos_entries *q;
96 cmos_enum_t cmos_enum;
97 cmos_entry_t cmos_entry;
98
99 /* First add the enums. */
100 for (p = first_cmos_table_enum(); p != NULL;
101 p = next_cmos_table_enum(p)) {
102 cmos_enum.config_id = p->config_id;
103 cmos_enum.value = p->value;
104 strncpy(cmos_enum.text, (char *)p->text, CMOS_MAX_TEXT_LENGTH);
105 cmos_enum.text[CMOS_MAX_TEXT_LENGTH] = '\0';
106 try_add_cmos_table_enum(&cmos_enum);
107 }
108
109 /* Now add the entries. We must add the entries after the enums because
110 * the entries are sanity checked against the enums as they are added.
111 */
112 for (q = first_cmos_table_entry(); q != NULL;
113 q = next_cmos_table_entry(q)) {
114 cmos_entry.bit = q->bit;
115 cmos_entry.length = q->length;
116
117 switch (q->config) {
118 case 'e':
119 cmos_entry.config = CMOS_ENTRY_ENUM;
120 break;
121
122 case 'h':
123 cmos_entry.config = CMOS_ENTRY_HEX;
124 break;
125
126 case 'r':
127 cmos_entry.config = CMOS_ENTRY_RESERVED;
128 break;
129
130 case 's':
131 cmos_entry.config = CMOS_ENTRY_STRING;
132 break;
133
134 default:
135 fprintf(stderr,
136 "%s: Entry in CMOS option table has unknown config "
137 "value.\n", prog_name);
138 exit(1);
139 }
140
141 cmos_entry.config_id = q->config_id;
142 strncpy(cmos_entry.name, (char *)q->name, CMOS_MAX_NAME_LENGTH);
143 cmos_entry.name[CMOS_MAX_NAME_LENGTH] = '\0';
144 try_add_cmos_table_entry(&cmos_entry);
145 }
146}
147
148/****************************************************************************
149 * get_cmos_checksum_info
150 *
151 * Get layout information for CMOS checksum.
152 ****************************************************************************/
153static void get_cmos_checksum_info(void)
154{
155 const cmos_entry_t *e;
156 struct cmos_checksum *checksum;
157 cmos_checksum_layout_t layout;
158 unsigned index, index2;
159
160 checksum = (struct cmos_checksum *)next_cmos_rec((const struct lb_record *)first_cmos_table_enum(), LB_TAG_OPTION_CHECKSUM);
161
162 if (checksum != NULL) { /* We are lucky. The coreboot table hints us to the checksum.
163 * We might have to check the type field here though.
164 */
165 layout.summed_area_start = checksum->range_start;
166 layout.summed_area_end = checksum->range_end;
167 layout.checksum_at = checksum->location;
168 try_convert_checksum_layout(&layout);
169 cmos_checksum_start = layout.summed_area_start;
170 cmos_checksum_end = layout.summed_area_end;
171 cmos_checksum_index = layout.checksum_at;
172 return;
173 }
174
175 if ((e = find_cmos_entry(checksum_param_name)) == NULL)
176 return;
177
178 /* If we get here, we are unlucky. The CMOS option table contains the
179 * location of the CMOS checksum. However, there is no information
180 * regarding which bytes of the CMOS area the checksum is computed over.
181 * Thus we have to hope our presets will be fine.
182 */
183
184 if (e->bit % 8) {
185 fprintf(stderr,
186 "%s: Error: CMOS checksum is not byte-aligned.\n",
187 prog_name);
188 exit(1);
189 }
190
191 index = e->bit / 8;
192 index2 = index + 1; /* The CMOS checksum occupies 16 bits. */
193
194 if (verify_cmos_byte_index(index) || verify_cmos_byte_index(index2)) {
195 fprintf(stderr,
196 "%s: Error: CMOS checksum location out of range.\n",
197 prog_name);
198 exit(1);
199 }
200
201 if (((index >= cmos_checksum_start) && (index <= cmos_checksum_end)) ||
202 (((index2) >= cmos_checksum_start)
203 && ((index2) <= cmos_checksum_end))) {
204 fprintf(stderr,
205 "%s: Error: CMOS checksum overlaps checksummed area.\n",
206 prog_name);
207 exit(1);
208 }
209
210 cmos_checksum_index = index;
211}
212
213/****************************************************************************
214 * try_convert_checksum_layout
215 *
216 * Perform sanity checking on CMOS checksum layout information and attempt to
217 * convert information from bit positions to byte positions. Return OK on
218 * success or an error code on failure.
219 ****************************************************************************/
220static void try_convert_checksum_layout(cmos_checksum_layout_t * layout)
221{
222 switch (checksum_layout_to_bytes(layout)) {
223 case OK:
224 return;
225
226 case LAYOUT_SUMMED_AREA_START_NOT_ALIGNED:
227 fprintf(stderr,
228 "%s: CMOS checksummed area start is not byte-aligned.\n",
229 prog_name);
230 break;
231
232 case LAYOUT_SUMMED_AREA_END_NOT_ALIGNED:
233 fprintf(stderr,
234 "%s: CMOS checksummed area end is not byte-aligned.\n",
235 prog_name);
236 break;
237
238 case LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED:
239 fprintf(stderr,
240 "%s: CMOS checksum location is not byte-aligned.\n",
241 prog_name);
242 break;
243
244 case LAYOUT_INVALID_SUMMED_AREA:
245 fprintf(stderr,
246 "%s: CMOS checksummed area end must be greater than "
247 "CMOS checksummed area start.\n", prog_name);
248 break;
249
250 case LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA:
251 fprintf(stderr,
252 "%s: CMOS checksum overlaps checksummed area.\n",
253 prog_name);
254 break;
255
256 case LAYOUT_SUMMED_AREA_OUT_OF_RANGE:
257 fprintf(stderr,
258 "%s: CMOS checksummed area out of range.\n", prog_name);
259 break;
260
261 case LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE:
262 fprintf(stderr,
263 "%s: CMOS checksum location out of range.\n",
264 prog_name);
265 break;
266
267 default:
268 BUG();
269 }
270
271 exit(1);
272}
273
274/****************************************************************************
275 * try_add_cmos_table_enum
276 *
277 * Attempt to add a CMOS enum to our internal repository. Exit with an error
278 * message on failure.
279 ****************************************************************************/
280static void try_add_cmos_table_enum(cmos_enum_t * cmos_enum)
281{
282 switch (add_cmos_enum(cmos_enum)) {
283 case OK:
284 return;
285
286 case LAYOUT_DUPLICATE_ENUM:
287 fprintf(stderr, "%s: Duplicate enum %s found in CMOS option "
288 "table.\n", prog_name, cmos_enum->text);
289 break;
290
291 default:
292 BUG();
293 }
294
295 exit(1);
296}
297
298/****************************************************************************
299 * try_add_cmos_table_entry
300 *
301 * Attempt to add a CMOS entry to our internal repository. Exit with an
302 * error message on failure.
303 ****************************************************************************/
304static void try_add_cmos_table_entry(cmos_entry_t * cmos_entry)
305{
306 const cmos_entry_t *conflict;
307
308 switch (add_cmos_entry(cmos_entry, &conflict)) {
309 case OK:
310 return;
311
312 case CMOS_AREA_OUT_OF_RANGE:
313 fprintf(stderr,
314 "%s: Bad CMOS option layout in CMOS option table entry "
315 "%s.\n", prog_name, cmos_entry->name);
316 break;
317
318 case CMOS_AREA_TOO_WIDE:
319 fprintf(stderr,
320 "%s: Area too wide for CMOS option table entry %s.\n",
321 prog_name, cmos_entry->name);
322 break;
323
324 case LAYOUT_ENTRY_OVERLAP:
325 fprintf(stderr,
326 "%s: CMOS option table entries %s and %s have overlapping "
327 "layouts.\n", prog_name, cmos_entry->name,
328 conflict->name);
329 break;
330
331 case LAYOUT_ENTRY_BAD_LENGTH:
332 /* Silently ignore entries with zero length. Although this should
333 * never happen in practice, we should handle the case in a
334 * reasonable manner just to be safe.
335 */
336 return;
337
338 default:
339 BUG();
340 }
341
342 exit(1);
343}
344
345/****************************************************************************
346 * first_cmos_table_entry
347 *
348 * Return a pointer to the first entry in the CMOS table that represents a
349 * CMOS parameter. Return NULL if CMOS table is empty.
350 ****************************************************************************/
351static const struct cmos_entries *first_cmos_table_entry(void)
352{
353 return (const struct cmos_entries *)first_cmos_rec(LB_TAG_OPTION);
354}
355
356/****************************************************************************
357 * next_cmos_table_entry
358 *
359 * Return a pointer to the next entry after 'last' in the CMOS table that
360 * represents a CMOS parameter. Return NULL if there are no more parameters.
361 ****************************************************************************/
362static const struct cmos_entries *next_cmos_table_entry(const struct
363 cmos_entries *last)
364{
365 return (const struct cmos_entries *)
366 next_cmos_rec((const struct lb_record *)last, LB_TAG_OPTION);
367}
368
369/****************************************************************************
370 * first_cmos_table_enum
371 *
372 * Return a pointer to the first entry in the CMOS table that represents a
373 * possible CMOS parameter value. Return NULL if the table does not contain
374 * any such entries.
375 ****************************************************************************/
376static const struct cmos_enums *first_cmos_table_enum(void)
377{
378 return (const struct cmos_enums *)first_cmos_rec(LB_TAG_OPTION_ENUM);
379}
380
381/****************************************************************************
382 * next_cmos_table_enum
383 *
384 * Return a pointer to the next entry after 'last' in the CMOS table that
385 * represents a possible CMOS parameter value. Return NULL if there are no
386 * more parameter values.
387 ****************************************************************************/
388static const struct cmos_enums *next_cmos_table_enum
389 (const struct cmos_enums *last) {
390 return (const struct cmos_enums *)
391 next_cmos_rec((const struct lb_record *)last, LB_TAG_OPTION_ENUM);
392}
393
394/****************************************************************************
395 * first_cmos_rec
396 *
397 * Return a pointer to the first entry in the CMOS table whose type matches
398 * 'tag'. Return NULL if CMOS table contains no such entry.
399 *
400 * Possible values for 'tag' are as follows:
401 *
402 * LB_TAG_OPTION: The entry represents a CMOS parameter.
403 * LB_TAG_OPTION_ENUM: The entry represents a possible value for a CMOS
404 * parameter of type 'enum'.
405 *
406 * The CMOS table tells us where in the nonvolatile RAM to look for CMOS
407 * parameter values and specifies their types as 'enum', 'hex', or
408 * 'reserved'.
409 ****************************************************************************/
410static const struct lb_record *first_cmos_rec(uint32_t tag)
411{
412 const char *p;
413 uint32_t bytes_processed, bytes_for_entries;
414 const struct lb_record *lbrec;
415
416 p = ((const char *)cmos_table) + cmos_table->header_length;
417 bytes_for_entries = cmos_table->size - cmos_table->header_length;
418
419 for (bytes_processed = 0;
420 bytes_processed < bytes_for_entries;
421 bytes_processed += lbrec->size) {
422 lbrec = (const struct lb_record *)&p[bytes_processed];
423
424 if (lbrec->tag == tag)
425 return lbrec;
426 }
427
428 return NULL;
429}
430
431/****************************************************************************
432 * next_cmos_rec
433 *
434 * Return a pointer to the next entry after 'last' in the CMOS table whose
435 * type matches 'tag'. Return NULL if the table contains no more entries of
436 * this type.
437 ****************************************************************************/
438static const struct lb_record *next_cmos_rec(const struct lb_record *last,
439 uint32_t tag)
440{
441 const char *p;
442 uint32_t bytes_processed, bytes_for_entries, last_offset;
443 const struct lb_record *lbrec;
444
445 p = ((const char *)cmos_table) + cmos_table->header_length;
446 bytes_for_entries = cmos_table->size - cmos_table->header_length;
447 last_offset = ((const char *)last) - p;
448
449 for (bytes_processed = last_offset + last->size;
450 bytes_processed < bytes_for_entries;
451 bytes_processed += lbrec->size) {
452 lbrec = (const struct lb_record *)&p[bytes_processed];
453
454 if (lbrec->tag == tag)
455 return lbrec;
456 }
457
458 return NULL;
459}
460