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