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