blob: b910e358b6f10f388942fe66722419e40691dc68 [file] [log] [blame]
Patrick Georgi36ade672011-01-28 07:56:39 +00001/*****************************************************************************\
2 * lbtable.c
3 *****************************************************************************
Vikram Narayanana8111cf2012-04-14 15:25:13 +05304 * Copyright (C) 2012, Vikram Narayanan
5 * Unified build_opt_tbl and nvramtool
6 * build_opt_tbl.c
7 * Copyright (C) 2003 Eric Biederman (ebiederm@xmission.com)
8 * Copyright (C) 2007-2010 coresystems GmbH
9 *
Patrick Georgi36ade672011-01-28 07:56:39 +000010 * Copyright (C) 2002-2005 The Regents of the University of California.
11 * Produced at the Lawrence Livermore National Laboratory.
12 * Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>
13 * and Stefan Reinauer <stepan@openbios.org>.
14 * UCRL-CODE-2003-012
15 * All rights reserved.
16 *
17 * This file is part of nvramtool, a utility for reading/writing coreboot
18 * parameters and displaying information from the coreboot table.
19 * For details, see http://coreboot.org/nvramtool.
20 *
21 * Please also read the file DISCLAIMER which is included in this software
22 * distribution.
23 *
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License (as published by the
26 * Free Software Foundation) version 2, dated June 1991.
27 *
28 * This program is distributed in the hope that it will be useful, but
29 * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
31 * conditions of the GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License along
34 * with this program; if not, write to the Free Software Foundation, Inc.,
35 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36\*****************************************************************************/
37
38#include <arpa/inet.h>
39#include <string.h>
40#include <sys/mman.h>
41#include "common.h"
42#include "coreboot_tables.h"
43#include "ip_checksum.h"
44#include "lbtable.h"
45#include "layout.h"
46#include "cmos_lowlevel.h"
47#include "hexdump.h"
48#include "cbfs.h"
Vikram Narayanana8111cf2012-04-14 15:25:13 +053049#include "layout-text.h"
Patrick Georgi36ade672011-01-28 07:56:39 +000050
51static void process_cmos_table(void);
52static void get_cmos_checksum_info(void);
53static void try_convert_checksum_layout(cmos_checksum_layout_t * layout);
54static void try_add_cmos_table_enum(cmos_enum_t * cmos_enum);
55static void try_add_cmos_table_entry(cmos_entry_t * cmos_entry);
56static const struct cmos_entries *first_cmos_table_entry(void);
57static const struct cmos_entries *next_cmos_table_entry(const struct
58 cmos_entries *last);
59static const struct cmos_enums *first_cmos_table_enum(void);
60static const struct cmos_enums *next_cmos_table_enum
61 (const struct cmos_enums *last);
62static const struct lb_record *first_cmos_rec(uint32_t tag);
63static const struct lb_record *next_cmos_rec(const struct lb_record *last,
64 uint32_t tag);
65
66/* The CMOS option table is located within the coreboot table. It tells us
67 * where the CMOS parameters are located in the nonvolatile RAM.
68 */
69static const struct cmos_option_table *cmos_table = NULL;
70
Vikram Narayanana8111cf2012-04-14 15:25:13 +053071#define ROUNDUP4(x) (x += (4 - (x % 4)))
72
Patrick Georgi36ade672011-01-28 07:56:39 +000073void process_layout(void)
74{
75 if ((cmos_table) == NULL) {
76 fprintf(stderr,
77 "%s: CMOS option table not found in coreboot table. "
78 "Apparently, the coreboot installed on this system was "
79 "built without specifying CONFIG_HAVE_OPTION_TABLE.\n",
80 prog_name);
81 exit(1);
82 }
83
84 process_cmos_table();
85 get_cmos_checksum_info();
86}
87
88void get_layout_from_cbfs_file(void)
89{
90 uint32_t len;
91 cmos_table = cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT, &len);
92 process_layout();
93}
94
Vikram Narayanana8111cf2012-04-14 15:25:13 +053095int write_cmos_layout_bin(FILE *f)
96{
97 const cmos_entry_t *cmos_entry;
98 const cmos_enum_t *cmos_enum;
99 cmos_checksum_layout_t layout;
100 struct cmos_option_table table;
101 struct cmos_entries entry;
102 struct cmos_enums cenum;
103 struct cmos_checksum csum;
104 size_t sum = 0;
105 int len;
106
107 for (cmos_entry = first_cmos_entry(); cmos_entry != NULL;
108 cmos_entry = next_cmos_entry(cmos_entry)) {
109
110 if (cmos_entry == first_cmos_entry()) {
111 sum += sizeof(table);
112 table.header_length = sizeof(table);
113 table.tag = LB_TAG_CMOS_OPTION_TABLE;
114
115 if (fwrite((char *)&table, sizeof(table), 1, f) != 1) {
116 perror("Error writing image file");
117 goto err;
118 }
119 }
120
121 memset(&entry, 0, sizeof(entry));
122 entry.tag = LB_TAG_OPTION;
123 entry.config = cmos_entry->config;
124 entry.config_id = (uint32_t)cmos_entry->config_id;
125 entry.bit = cmos_entry->bit;
126 entry.length = cmos_entry->length;
127
128 if (!is_ident((char *)cmos_entry->name)) {
129 fprintf(stderr,
130 "Error - Name %s is an invalid identifier\n",
131 cmos_entry->name);
132 goto err;
133 }
134
135 memcpy(entry.name, cmos_entry->name, strlen(cmos_entry->name));
136 entry.name[strlen(cmos_entry->name)] = '\0';
137 len = strlen(cmos_entry->name) + 1;
138
139 if (len % 4)
140 ROUNDUP4(len);
141
142 entry.size = sizeof(entry) - CMOS_MAX_NAME_LENGTH + len;
143 sum += entry.size;
144 if (fwrite((char *)&entry, entry.size, 1, f) != 1) {
145 perror("Error writing image file");
146 goto err;
147 }
148 }
149
150 for (cmos_enum = first_cmos_enum();
151 cmos_enum != NULL; cmos_enum = next_cmos_enum(cmos_enum)) {
152 memset(&cenum, 0, sizeof(cenum));
153 cenum.tag = LB_TAG_OPTION_ENUM;
154 memcpy(cenum.text, cmos_enum->text, strlen(cmos_enum->text));
155 cenum.text[strlen(cmos_enum->text)] = '\0';
156 len = strlen((char *)cenum.text) + 1;
157
158 if (len % 4)
159 ROUNDUP4(len);
160
161 cenum.config_id = cmos_enum->config_id;
162 cenum.value = cmos_enum->value;
163 cenum.size = sizeof(cenum) - CMOS_MAX_TEXT_LENGTH + len;
164 sum += cenum.size;
165 if (fwrite((char *)&cenum, cenum.size, 1, f) != 1) {
166 perror("Error writing image file");
167 goto err;
168 }
169 }
170
171 layout.summed_area_start = cmos_checksum_start;
172 layout.summed_area_end = cmos_checksum_end;
173 layout.checksum_at = cmos_checksum_index;
174 checksum_layout_to_bits(&layout);
175
176 csum.tag = LB_TAG_OPTION_CHECKSUM;
177 csum.size = sizeof(csum);
178 csum.range_start = layout.summed_area_start;
179 csum.range_end = layout.summed_area_end;
180 csum.location = layout.checksum_at;
181 csum.type = CHECKSUM_PCBIOS;
182 sum += csum.size;
183
184 if (fwrite((char *)&csum, csum.size, 1, f) != 1) {
185 perror("Error writing image file");
186 goto err;
187 }
188
189 if (fseek(f, sizeof(table.tag), SEEK_SET) != 0) {
190 perror("Error while seeking");
191 goto err;
192 }
193
194 if (fwrite((char *)&sum, sizeof(table.tag), 1, f) != 1) {
195 perror("Error writing image file");
196 goto err;
197 }
198 return sum;
199
200err:
201 fclose(f);
202 exit(1);
203}
204
205void write_cmos_output_bin(const char *binary_filename)
206{
207 FILE *fp;
208
209 if ((fp = fopen(binary_filename, "wb")) == NULL) {
210 fprintf(stderr,
211 "%s: Can not open file %s for writing: "
212 "%s\n", prog_name, binary_filename, strerror(errno));
213 exit(1);
214 }
215 write_cmos_layout_bin(fp);
216 fclose(fp);
217}
218
Patrick Georgi36ade672011-01-28 07:56:39 +0000219/****************************************************************************
Mathias Krause155c3792011-03-10 07:52:02 +0000220 * get_layout_from_cmos_table
221 *
222 * Find the CMOS table which is stored within the coreboot table and set the
223 * global variable cmos_table to point to it.
224 ****************************************************************************/
225void get_layout_from_cmos_table(void)
226{
227 get_lbtable();
228 cmos_table = (const struct cmos_option_table *)
229 find_lbrec(LB_TAG_CMOS_OPTION_TABLE);
230 process_layout();
231}
232
233/****************************************************************************
Patrick Georgi36ade672011-01-28 07:56:39 +0000234 * process_cmos_table
235 *
236 * Extract layout information from the CMOS option table and store it in our
237 * internal repository.
238 ****************************************************************************/
239static void process_cmos_table(void)
240{
241 const struct cmos_enums *p;
242 const struct cmos_entries *q;
243 cmos_enum_t cmos_enum;
244 cmos_entry_t cmos_entry;
245
246 /* First add the enums. */
247 for (p = first_cmos_table_enum(); p != NULL;
248 p = next_cmos_table_enum(p)) {
249 cmos_enum.config_id = p->config_id;
250 cmos_enum.value = p->value;
251 strncpy(cmos_enum.text, (char *)p->text, CMOS_MAX_TEXT_LENGTH);
252 cmos_enum.text[CMOS_MAX_TEXT_LENGTH] = '\0';
253 try_add_cmos_table_enum(&cmos_enum);
254 }
255
256 /* Now add the entries. We must add the entries after the enums because
257 * the entries are sanity checked against the enums as they are added.
258 */
259 for (q = first_cmos_table_entry(); q != NULL;
260 q = next_cmos_table_entry(q)) {
261 cmos_entry.bit = q->bit;
262 cmos_entry.length = q->length;
263
264 switch (q->config) {
265 case 'e':
266 cmos_entry.config = CMOS_ENTRY_ENUM;
267 break;
268
269 case 'h':
270 cmos_entry.config = CMOS_ENTRY_HEX;
271 break;
272
273 case 'r':
274 cmos_entry.config = CMOS_ENTRY_RESERVED;
275 break;
276
277 case 's':
278 cmos_entry.config = CMOS_ENTRY_STRING;
279 break;
280
281 default:
282 fprintf(stderr,
283 "%s: Entry in CMOS option table has unknown config "
284 "value.\n", prog_name);
285 exit(1);
286 }
287
288 cmos_entry.config_id = q->config_id;
289 strncpy(cmos_entry.name, (char *)q->name, CMOS_MAX_NAME_LENGTH);
290 cmos_entry.name[CMOS_MAX_NAME_LENGTH] = '\0';
291 try_add_cmos_table_entry(&cmos_entry);
292 }
293}
294
295/****************************************************************************
296 * get_cmos_checksum_info
297 *
298 * Get layout information for CMOS checksum.
299 ****************************************************************************/
300static void get_cmos_checksum_info(void)
301{
302 const cmos_entry_t *e;
303 struct cmos_checksum *checksum;
304 cmos_checksum_layout_t layout;
305 unsigned index, index2;
306
307 checksum = (struct cmos_checksum *)next_cmos_rec((const struct lb_record *)first_cmos_table_enum(), LB_TAG_OPTION_CHECKSUM);
308
309 if (checksum != NULL) { /* We are lucky. The coreboot table hints us to the checksum.
310 * We might have to check the type field here though.
311 */
312 layout.summed_area_start = checksum->range_start;
313 layout.summed_area_end = checksum->range_end;
314 layout.checksum_at = checksum->location;
315 try_convert_checksum_layout(&layout);
316 cmos_checksum_start = layout.summed_area_start;
317 cmos_checksum_end = layout.summed_area_end;
318 cmos_checksum_index = layout.checksum_at;
319 return;
320 }
321
322 if ((e = find_cmos_entry(checksum_param_name)) == NULL)
323 return;
324
325 /* If we get here, we are unlucky. The CMOS option table contains the
326 * location of the CMOS checksum. However, there is no information
327 * regarding which bytes of the CMOS area the checksum is computed over.
328 * Thus we have to hope our presets will be fine.
329 */
330
331 if (e->bit % 8) {
332 fprintf(stderr,
333 "%s: Error: CMOS checksum is not byte-aligned.\n",
334 prog_name);
335 exit(1);
336 }
337
338 index = e->bit / 8;
339 index2 = index + 1; /* The CMOS checksum occupies 16 bits. */
340
341 if (verify_cmos_byte_index(index) || verify_cmos_byte_index(index2)) {
342 fprintf(stderr,
343 "%s: Error: CMOS checksum location out of range.\n",
344 prog_name);
345 exit(1);
346 }
347
348 if (((index >= cmos_checksum_start) && (index <= cmos_checksum_end)) ||
349 (((index2) >= cmos_checksum_start)
350 && ((index2) <= cmos_checksum_end))) {
351 fprintf(stderr,
352 "%s: Error: CMOS checksum overlaps checksummed area.\n",
353 prog_name);
354 exit(1);
355 }
356
357 cmos_checksum_index = index;
358}
359
360/****************************************************************************
361 * try_convert_checksum_layout
362 *
363 * Perform sanity checking on CMOS checksum layout information and attempt to
364 * convert information from bit positions to byte positions. Return OK on
365 * success or an error code on failure.
366 ****************************************************************************/
367static void try_convert_checksum_layout(cmos_checksum_layout_t * layout)
368{
369 switch (checksum_layout_to_bytes(layout)) {
370 case OK:
371 return;
372
373 case LAYOUT_SUMMED_AREA_START_NOT_ALIGNED:
374 fprintf(stderr,
375 "%s: CMOS checksummed area start is not byte-aligned.\n",
376 prog_name);
377 break;
378
379 case LAYOUT_SUMMED_AREA_END_NOT_ALIGNED:
380 fprintf(stderr,
381 "%s: CMOS checksummed area end is not byte-aligned.\n",
382 prog_name);
383 break;
384
385 case LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED:
386 fprintf(stderr,
387 "%s: CMOS checksum location is not byte-aligned.\n",
388 prog_name);
389 break;
390
391 case LAYOUT_INVALID_SUMMED_AREA:
392 fprintf(stderr,
393 "%s: CMOS checksummed area end must be greater than "
394 "CMOS checksummed area start.\n", prog_name);
395 break;
396
397 case LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA:
398 fprintf(stderr,
399 "%s: CMOS checksum overlaps checksummed area.\n",
400 prog_name);
401 break;
402
403 case LAYOUT_SUMMED_AREA_OUT_OF_RANGE:
404 fprintf(stderr,
405 "%s: CMOS checksummed area out of range.\n", prog_name);
406 break;
407
408 case LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE:
409 fprintf(stderr,
410 "%s: CMOS checksum location out of range.\n",
411 prog_name);
412 break;
413
414 default:
415 BUG();
416 }
417
418 exit(1);
419}
420
421/****************************************************************************
422 * try_add_cmos_table_enum
423 *
424 * Attempt to add a CMOS enum to our internal repository. Exit with an error
425 * message on failure.
426 ****************************************************************************/
427static void try_add_cmos_table_enum(cmos_enum_t * cmos_enum)
428{
429 switch (add_cmos_enum(cmos_enum)) {
430 case OK:
431 return;
432
433 case LAYOUT_DUPLICATE_ENUM:
434 fprintf(stderr, "%s: Duplicate enum %s found in CMOS option "
435 "table.\n", prog_name, cmos_enum->text);
436 break;
437
438 default:
439 BUG();
440 }
441
442 exit(1);
443}
444
445/****************************************************************************
446 * try_add_cmos_table_entry
447 *
448 * Attempt to add a CMOS entry to our internal repository. Exit with an
449 * error message on failure.
450 ****************************************************************************/
451static void try_add_cmos_table_entry(cmos_entry_t * cmos_entry)
452{
453 const cmos_entry_t *conflict;
454
455 switch (add_cmos_entry(cmos_entry, &conflict)) {
456 case OK:
457 return;
458
459 case CMOS_AREA_OUT_OF_RANGE:
460 fprintf(stderr,
461 "%s: Bad CMOS option layout in CMOS option table entry "
462 "%s.\n", prog_name, cmos_entry->name);
463 break;
464
465 case CMOS_AREA_TOO_WIDE:
466 fprintf(stderr,
467 "%s: Area too wide for CMOS option table entry %s.\n",
468 prog_name, cmos_entry->name);
469 break;
470
471 case LAYOUT_ENTRY_OVERLAP:
472 fprintf(stderr,
473 "%s: CMOS option table entries %s and %s have overlapping "
474 "layouts.\n", prog_name, cmos_entry->name,
475 conflict->name);
476 break;
477
478 case LAYOUT_ENTRY_BAD_LENGTH:
479 /* Silently ignore entries with zero length. Although this should
480 * never happen in practice, we should handle the case in a
481 * reasonable manner just to be safe.
482 */
483 return;
484
485 default:
486 BUG();
487 }
488
489 exit(1);
490}
491
492/****************************************************************************
493 * first_cmos_table_entry
494 *
495 * Return a pointer to the first entry in the CMOS table that represents a
496 * CMOS parameter. Return NULL if CMOS table is empty.
497 ****************************************************************************/
498static const struct cmos_entries *first_cmos_table_entry(void)
499{
500 return (const struct cmos_entries *)first_cmos_rec(LB_TAG_OPTION);
501}
502
503/****************************************************************************
504 * next_cmos_table_entry
505 *
506 * Return a pointer to the next entry after 'last' in the CMOS table that
507 * represents a CMOS parameter. Return NULL if there are no more parameters.
508 ****************************************************************************/
509static const struct cmos_entries *next_cmos_table_entry(const struct
510 cmos_entries *last)
511{
512 return (const struct cmos_entries *)
513 next_cmos_rec((const struct lb_record *)last, LB_TAG_OPTION);
514}
515
516/****************************************************************************
517 * first_cmos_table_enum
518 *
519 * Return a pointer to the first entry in the CMOS table that represents a
520 * possible CMOS parameter value. Return NULL if the table does not contain
521 * any such entries.
522 ****************************************************************************/
523static const struct cmos_enums *first_cmos_table_enum(void)
524{
525 return (const struct cmos_enums *)first_cmos_rec(LB_TAG_OPTION_ENUM);
526}
527
528/****************************************************************************
529 * next_cmos_table_enum
530 *
531 * Return a pointer to the next entry after 'last' in the CMOS table that
532 * represents a possible CMOS parameter value. Return NULL if there are no
533 * more parameter values.
534 ****************************************************************************/
535static const struct cmos_enums *next_cmos_table_enum
536 (const struct cmos_enums *last) {
537 return (const struct cmos_enums *)
538 next_cmos_rec((const struct lb_record *)last, LB_TAG_OPTION_ENUM);
539}
540
541/****************************************************************************
542 * first_cmos_rec
543 *
544 * Return a pointer to the first entry in the CMOS table whose type matches
545 * 'tag'. Return NULL if CMOS table contains no such entry.
546 *
547 * Possible values for 'tag' are as follows:
548 *
549 * LB_TAG_OPTION: The entry represents a CMOS parameter.
550 * LB_TAG_OPTION_ENUM: The entry represents a possible value for a CMOS
551 * parameter of type 'enum'.
552 *
553 * The CMOS table tells us where in the nonvolatile RAM to look for CMOS
554 * parameter values and specifies their types as 'enum', 'hex', or
555 * 'reserved'.
556 ****************************************************************************/
557static const struct lb_record *first_cmos_rec(uint32_t tag)
558{
559 const char *p;
560 uint32_t bytes_processed, bytes_for_entries;
561 const struct lb_record *lbrec;
562
563 p = ((const char *)cmos_table) + cmos_table->header_length;
564 bytes_for_entries = cmos_table->size - cmos_table->header_length;
565
566 for (bytes_processed = 0;
567 bytes_processed < bytes_for_entries;
568 bytes_processed += lbrec->size) {
569 lbrec = (const struct lb_record *)&p[bytes_processed];
570
571 if (lbrec->tag == tag)
572 return lbrec;
573 }
574
575 return NULL;
576}
577
578/****************************************************************************
579 * next_cmos_rec
580 *
581 * Return a pointer to the next entry after 'last' in the CMOS table whose
582 * type matches 'tag'. Return NULL if the table contains no more entries of
583 * this type.
584 ****************************************************************************/
585static const struct lb_record *next_cmos_rec(const struct lb_record *last,
586 uint32_t tag)
587{
588 const char *p;
589 uint32_t bytes_processed, bytes_for_entries, last_offset;
590 const struct lb_record *lbrec;
591
592 p = ((const char *)cmos_table) + cmos_table->header_length;
593 bytes_for_entries = cmos_table->size - cmos_table->header_length;
594 last_offset = ((const char *)last) - p;
595
596 for (bytes_processed = last_offset + last->size;
597 bytes_processed < bytes_for_entries;
598 bytes_processed += lbrec->size) {
599 lbrec = (const struct lb_record *)&p[bytes_processed];
600
601 if (lbrec->tag == tag)
602 return lbrec;
603 }
604
605 return NULL;
606}
607