1 /*****************************************************************************\
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>.
10 * This file is part of nvramtool, a utility for reading/writing coreboot
11 * parameters and displaying information from the coreboot table.
12 * For details, see http://coreboot.org/nvramtool.
14 * Please also read the file DISCLAIMER which is included in this software
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License (as published by the
19 * Free Software Foundation) version 2, dated June 1991.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
24 * conditions of the GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 \*****************************************************************************/
35 #include "layout_file.h"
36 #include "input_file.h"
38 #include "cmos_lowlevel.h"
42 typedef void (*op_fn_t) (void);
44 static void op_show_version(void);
45 static void op_show_usage(void);
46 static void op_lbtable_show_info(void);
47 static void op_lbtable_dump(void);
48 static void op_show_param_values(void);
49 static void op_cmos_show_one_param(void);
50 static void op_cmos_show_all_params(void);
51 static void op_cmos_set_one_param(void);
52 static void op_cmos_set_params_stdin(void);
53 static void op_cmos_set_params_file(void);
54 static void op_cmos_checksum(void);
55 static void op_show_layout(void);
56 static void op_write_cmos_dump(void);
57 static void op_read_cmos_dump(void);
58 static void op_show_cmos_hex_dump(void);
59 static void op_show_cmos_dumpfile(void);
60 static int list_one_param(const char name[], int show_name);
61 static int list_all_params(void);
62 static void list_param_enums(const char name[]);
63 static void set_one_param(const char name[], const char value[]);
64 static void set_params(FILE * f);
65 static void parse_assignment(char arg[], const char **name, const char **value);
66 static int list_cmos_entry(const cmos_entry_t * e, int show_name);
67 static uint16_t convert_checksum_value(const char value[]);
69 static const op_fn_t op_fns[] = { op_show_version,
74 op_cmos_show_one_param,
75 op_cmos_show_all_params,
76 op_cmos_set_one_param,
77 op_cmos_set_params_stdin,
78 op_cmos_set_params_file,
83 op_show_cmos_hex_dump,
87 static const hexdump_format_t cmos_dump_format =
88 { 16, 2, "", " | ", " ", " | ", '.', NULL };
90 /****************************************************************************
92 ****************************************************************************/
93 int main(int argc, char *argv[])
95 cmos_layout_get_fn_t fn;
97 parse_nvramtool_args(argc, argv);
99 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found) {
100 set_layout_filename(nvramtool_op_modifiers
101 [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].param);
102 fn = get_layout_from_file;
104 fn = get_layout_from_cmos_table;
106 register_cmos_layout_get_fn(fn);
107 op_fns[nvramtool_op.op] ();
111 /****************************************************************************
116 * Show version information for this program.
117 ****************************************************************************/
118 static void op_show_version(void)
120 printf("This is %s version %s.\n", prog_name, prog_version);
123 /****************************************************************************
128 * Show a usage message for this program.
129 ****************************************************************************/
130 static void op_show_usage(void)
135 /****************************************************************************
136 * op_lbtable_show_info
140 * If ARG is present, show coreboot table information specified by ARG.
141 * Else show all possible values for ARG.
142 ****************************************************************************/
143 static void op_lbtable_show_info(void)
145 if (nvramtool_op.param == NULL)
146 list_lbtable_choices();
149 list_lbtable_item(nvramtool_op.param);
153 /****************************************************************************
158 * Do low-level dump of coreboot table.
159 ****************************************************************************/
160 static void op_lbtable_dump(void)
166 /****************************************************************************
167 * op_show_param_values
171 * Show all possible values for parameter NAME.
172 ****************************************************************************/
173 static void op_show_param_values(void)
176 list_param_enums(nvramtool_op.param);
179 /****************************************************************************
180 * op_cmos_show_one_param
184 * Show parameter NAME. If -n is specified, show value only. Else show name
186 ****************************************************************************/
187 static void op_cmos_show_one_param(void)
192 result = list_one_param(nvramtool_op.param,
193 !nvramtool_op_modifiers
194 [NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found);
195 cmos_checksum_verify();
201 /****************************************************************************
202 * op_cmos_show_all_params
206 * Show names and values for all parameters.
207 ****************************************************************************/
208 static void op_cmos_show_all_params(void)
213 result = list_all_params();
214 cmos_checksum_verify();
220 /****************************************************************************
221 * op_cmos_set_one_param
225 * Set parameter NAME to VALUE.
226 ****************************************************************************/
227 static void op_cmos_set_one_param(void)
229 const char *name, *value;
233 /* Separate 'NAME=VALUE' syntax into two strings representing NAME and
236 parse_assignment(nvramtool_op.param, &name, &value);
238 set_one_param(name, value);
241 /****************************************************************************
242 * op_cmos_set_params_stdin
246 * Set parameters according to standard input.
247 ****************************************************************************/
248 static void op_cmos_set_params_stdin(void)
254 /****************************************************************************
255 * op_cmos_set_params_file
259 * Set parameters according to INPUT_FILE.
260 ****************************************************************************/
261 static void op_cmos_set_params_file(void)
265 if ((f = fopen(nvramtool_op.param, "r")) == NULL) {
266 fprintf(stderr, "%s: Can not open file %s for reading: %s\n",
267 prog_name, nvramtool_op.param, strerror(errno));
276 /****************************************************************************
281 * If VALUE is present, set coreboot CMOS checksum to VALUE. Else show
283 ****************************************************************************/
284 static void op_cmos_checksum(void)
290 if (nvramtool_op.param == NULL) {
292 checksum = cmos_checksum_read();
294 printf("0x%x\n", checksum);
296 checksum = convert_checksum_value(nvramtool_op.param);
298 cmos_checksum_write(checksum);
303 /****************************************************************************
308 * Write CMOS layout information to standard output.
309 ****************************************************************************/
310 static void op_show_layout(void)
313 write_cmos_layout(stdout);
316 /****************************************************************************
321 * Write the contents of CMOS memory to a binary file.
322 ****************************************************************************/
323 static void op_write_cmos_dump(void)
325 unsigned char data[CMOS_SIZE];
328 if ((f = fopen(nvramtool_op.param, "w")) == NULL) {
329 fprintf(stderr, "%s: Can not open file %s for writing: %s\n",
330 prog_name, nvramtool_op.param, strerror(errno));
338 if (fwrite(data, 1, CMOS_SIZE, f) != CMOS_SIZE) {
339 fprintf(stderr, "%s: Error writing CMOS data to file %s: %s\n",
340 prog_name, nvramtool_op.param, strerror(errno));
347 /****************************************************************************
352 * Read binary data from a file and write the data to CMOS memory.
353 ****************************************************************************/
354 static void op_read_cmos_dump(void)
356 unsigned char data[CMOS_SIZE];
360 if ((f = fopen(nvramtool_op.param, "r")) == NULL) {
361 fprintf(stderr, "%s: Can not open file %s for reading: %s\n",
362 prog_name, nvramtool_op.param, strerror(errno));
366 if ((nr_bytes = fread(data, 1, CMOS_SIZE, f)) != CMOS_SIZE) {
368 "%s: Error: Only able to read %d bytes of CMOS data "
369 "from file %s. CMOS data is unchanged.\n", prog_name,
370 (int)nr_bytes, nvramtool_op.param);
376 cmos_write_all(data);
380 /****************************************************************************
381 * op_show_cmos_hex_dump
385 * Write a hex dump of CMOS memory to standard output.
386 ****************************************************************************/
387 static void op_show_cmos_hex_dump(void)
389 unsigned char data[CMOS_SIZE];
394 hexdump(data, CMOS_SIZE, 0, stdout, &cmos_dump_format);
397 /****************************************************************************
398 * op_show_cmos_dumpfile
402 * Read binary data from a file (presumably a CMOS dump file) and display a
403 * hex dump of the CMOS data from the file.
404 ****************************************************************************/
405 static void op_show_cmos_dumpfile(void)
407 unsigned char data[CMOS_SIZE];
411 if ((f = fopen(nvramtool_op.param, "r")) == NULL) {
412 fprintf(stderr, "%s: Can not open file %s for reading: %s\n",
413 prog_name, nvramtool_op.param, strerror(errno));
417 nr_bytes = fread(data, 1, CMOS_SIZE, f);
419 hexdump(data, nr_bytes, 0, stdout, &cmos_dump_format);
422 /****************************************************************************
425 * Attempt to list one CMOS parameter given by 'name'. 'show_name' is a
426 * boolean value indicating whether the parameter name should be displayed
427 * along with its value. Return 1 if error was encountered. Else return OK.
428 ****************************************************************************/
429 static int list_one_param(const char name[], int show_name)
431 const cmos_entry_t *e;
433 if (is_checksum_name(name) || ((e = find_cmos_entry(name)) == NULL)) {
434 fprintf(stderr, "%s: CMOS parameter %s not found.\n", prog_name,
439 if (e->config == CMOS_ENTRY_RESERVED) {
440 fprintf(stderr, "%s: Parameter %s is reserved.\n", prog_name,
445 return (list_cmos_entry(e, show_name) != 0);
448 /****************************************************************************
451 * Attempt to list all CMOS parameters. Return 1 if error was encountered.
453 ****************************************************************************/
454 static int list_all_params(void)
456 const cmos_entry_t *e;
461 for (e = first_cmos_entry(); e != NULL; e = next_cmos_entry(e)) {
462 if ((e->config == CMOS_ENTRY_RESERVED)
463 || is_checksum_name(e->name))
466 if (list_cmos_entry(e, TRUE))
473 /****************************************************************************
476 * List all possible values for CMOS parameter given by 'name'.
477 ****************************************************************************/
478 static void list_param_enums(const char name[])
480 const cmos_entry_t *e;
481 const cmos_enum_t *p;
483 if (is_checksum_name(name) || (e = find_cmos_entry(name)) == NULL) {
484 fprintf(stderr, "%s: CMOS parameter %s not found.\n", prog_name,
490 case CMOS_ENTRY_ENUM:
491 for (p = first_cmos_enum_id(e->config_id);
492 p != NULL; p = next_cmos_enum_id(p))
493 printf("%s\n", p->text);
498 printf("Parameter %s requires a %u-bit unsigned integer.\n",
502 case CMOS_ENTRY_STRING:
503 printf("Parameter %s requires a %u-byte string.\n", name,
507 case CMOS_ENTRY_RESERVED:
508 printf("Parameter %s is reserved.\n", name);
516 /****************************************************************************
519 * Set the CMOS parameter given by 'name' to 'value'. The 'name' parameter
520 * is case-sensitive. If we are setting an enum parameter, then 'value' is
521 * interpreted as a case-sensitive string that must match the option name
522 * exactly. If we are setting a 'hex' parameter, then 'value' is treated as
523 * a string representation of an unsigned integer that may be specified in
524 * decimal, hex, or octal.
525 ****************************************************************************/
526 static void set_one_param(const char name[], const char value[])
528 const cmos_entry_t *e;
529 unsigned long long n;
531 if (is_checksum_name(name) || (e = find_cmos_entry(name)) == NULL) {
532 fprintf(stderr, "%s: CMOS parameter %s not found.", prog_name,
537 switch (prepare_cmos_write(e, value, &n)) {
541 case CMOS_OP_BAD_ENUM_VALUE:
542 fprintf(stderr, "%s: Bad value for parameter %s.", prog_name,
546 case CMOS_OP_NEGATIVE_INT:
548 "%s: This program does not support assignment of negative "
549 "numbers to coreboot parameters.", prog_name);
552 case CMOS_OP_INVALID_INT:
553 fprintf(stderr, "%s: %s is not a valid integer.", prog_name,
557 case CMOS_OP_RESERVED:
559 "%s: Can not modify reserved coreboot parameter %s.",
563 case CMOS_OP_VALUE_TOO_WIDE:
565 "%s: Can not write value %s to CMOS parameter %s that is "
566 "only %d bits wide.", prog_name, value, name,
570 case CMOS_OP_NO_MATCHING_ENUM:
572 "%s: coreboot parameter %s has no matching enums.",
576 case CMOS_AREA_OUT_OF_RANGE:
578 "%s: The CMOS area specified by the layout info for "
579 "coreboot parameter %s is out of range.", prog_name,
583 case CMOS_AREA_OVERLAPS_RTC:
585 "%s: The CMOS area specified by the layout info for "
586 "coreboot parameter %s overlaps the realtime clock area.",
590 case CMOS_AREA_TOO_WIDE:
592 "%s: The CMOS area specified by the layout info for "
593 "coreboot parameter %s is too wide.", prog_name, name);
598 "%s: Unknown error encountered while attempting to modify "
599 "coreboot parameter %s.", prog_name, name);
603 /* write the value to nonvolatile RAM */
606 cmos_checksum_write(cmos_checksum_compute());
611 fprintf(stderr, " CMOS write not performed.\n");
615 /****************************************************************************
618 * Set coreboot parameters according to the contents of file 'f'.
619 ****************************************************************************/
620 static void set_params(FILE * f)
621 { /* First process the input file. Then perform writes only if there were
622 * no problems processing the input. Either all values will be written
623 * successfully or no values will be written.
625 do_cmos_writes(process_input_file(f));
628 /****************************************************************************
631 * Parse the string 'arg' (which supposedly represents an assignment) into a
632 * NAME and a VALUE. If 'arg' does not conform to the proper assignment
633 * syntax, exit with a usage message. Otherwise, on return, 'arg' is broken
634 * into substrings representing NAME and VALUE, and *name and *value are set
635 * to point to these two substrings.
636 ****************************************************************************/
637 static void parse_assignment(char arg[], const char **name, const char **value)
639 static const size_t N_MATCHES = 4;
640 regmatch_t match[N_MATCHES];
643 compile_reg_exprs(REG_EXTENDED | REG_NEWLINE, 1, assignment_regex,
646 /* Does 'arg' conform to proper assignment syntax? If not, exit with a
649 if (regexec(&assignment, arg, N_MATCHES, match, 0))
652 /* Ok, we found a valid assignment. Break it into two strings
653 * representing NAME and VALUE.
655 arg[match[1].rm_eo] = '\0';
656 arg[match[2].rm_eo] = '\0';
657 *name = &arg[match[1].rm_so];
658 *value = &arg[match[2].rm_so];
660 free_reg_exprs(1, &assignment);
663 /****************************************************************************
666 * Attempt to list the CMOS entry represented by 'e'. 'show_name' is a
667 * boolean value indicating whether the parameter name should be displayed
668 * along with its value. On success, return OK. On error, print an error
669 * message and return 1.
670 ****************************************************************************/
671 static int list_cmos_entry(const cmos_entry_t * e, int show_name)
673 const cmos_enum_t *p;
674 unsigned long long value;
677 /* sanity check CMOS entry */
678 switch (prepare_cmos_read(e)) {
682 case CMOS_OP_RESERVED:
685 case CMOS_AREA_OUT_OF_RANGE:
687 "%s: Can not read coreboot parameter %s because "
688 "layout info specifies out of range CMOS area.\n",
692 case CMOS_AREA_OVERLAPS_RTC:
694 "%s: Can not read coreboot parameter %s because "
695 "layout info specifies CMOS area that overlaps realtime "
696 "clock area.\n", prog_name, e->name);
699 case CMOS_AREA_TOO_WIDE:
701 "%s: Can not read coreboot parameter %s because "
702 "layout info specifies CMOS area that is too wide.\n",
708 "%s: Unknown error encountered while attempting to "
709 "read coreboot parameter %s\n", prog_name, e->name);
713 /* read the value from CMOS */
715 value = cmos_read(e);
718 /* display the value */
720 case CMOS_ENTRY_ENUM:
721 if ((p = find_cmos_enum(e->config_id, value)) == NULL) {
723 printf("# Bad value -> %s = 0x%llx\n", e->name,
726 printf("Bad value -> 0x%llx\n", value);
729 printf("%s = %s\n", e->name, p->text);
731 printf("%s\n", p->text);
738 printf("%s = 0x%llx\n", e->name, value);
740 printf("0x%llx\n", value);
744 case CMOS_ENTRY_STRING:
745 w = (char *)(unsigned long)value;
749 printf("# Bad value -> %s\n", e->name);
751 printf("Bad value\n");
760 printf("%s = %s\n", e->name,
761 (char *)(unsigned long)value);
763 printf("%s\n", (char *)(unsigned long)value);
766 free((void *)(unsigned long)value);
770 case CMOS_ENTRY_RESERVED:
778 /****************************************************************************
779 * convert_checksum_value
781 * 'value' is the string representation of a checksum value that the user
782 * wishes to set using the -c option. Convert the string to a 16-bit
783 * unsigned integer and return the result. Exit with an error message if
784 * 'value' is invalid.
785 ****************************************************************************/
786 static uint16_t convert_checksum_value(const char value[])
793 for (p = value; isspace(*p); p++) ;
795 negative = (*p == '-');
796 n = strtoul(value, (char **)&p, 0);
800 "%s: Checksum value %s is not a valid integer.\n",
807 "%s: Checksum must be an unsigned integer.\n",
812 result = (uint16_t) n;
816 "%s: Checksum value must fit within 16 bits.\n",