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 \*****************************************************************************/
39 #include "accessors/layout-text.h"
40 #include "input_file.h"
42 #include "cmos_lowlevel.h"
47 typedef void (*op_fn_t) (void);
49 static void op_show_version(void);
50 static void op_show_usage(void);
51 static void op_lbtable_show_info(void);
52 static void op_lbtable_dump(void);
53 static void op_show_param_values(void);
54 static void op_cmos_show_one_param(void);
55 static void op_cmos_show_all_params(void);
56 static void op_cmos_set_one_param(void);
57 static void op_cmos_set_params_stdin(void);
58 static void op_cmos_set_params_file(void);
59 static void op_cmos_checksum(void);
60 static void op_show_layout(void);
61 static void op_write_cmos_dump(void);
62 static void op_read_cmos_dump(void);
63 static void op_show_cmos_hex_dump(void);
64 static void op_show_cmos_dumpfile(void);
65 static int list_one_param(const char name[], int show_name);
66 static int list_all_params(void);
67 static void list_param_enums(const char name[]);
68 static void set_one_param(const char name[], const char value[]);
69 static void set_params(FILE * f);
70 static void parse_assignment(char arg[], const char **name, const char **value);
71 static int list_cmos_entry(const cmos_entry_t * e, int show_name);
72 static uint16_t convert_checksum_value(const char value[]);
74 static const op_fn_t op_fns[] = { op_show_version,
79 op_cmos_show_one_param,
80 op_cmos_show_all_params,
81 op_cmos_set_one_param,
82 op_cmos_set_params_stdin,
83 op_cmos_set_params_file,
88 op_show_cmos_hex_dump,
92 static const hexdump_format_t cmos_dump_format =
93 { 16, 2, "", " | ", " ", " | ", '.' };
95 /****************************************************************************
97 ****************************************************************************/
98 int main(int argc, char *argv[])
100 void *cmos_default = NULL;
101 cmos_layout_get_fn_t fn = get_layout_from_cmos_table;
103 parse_nvramtool_args(argc, argv);
105 /* If we should operate on a CBFS file default to reading the layout
106 * and CMOS contents from it. */
107 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CBFS_FILE].found) {
108 open_cbfs(nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CBFS_FILE].param);
109 if (!nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].found) {
110 cmos_default = cbfs_find_file("cmos.default", CBFS_COMPONENT_CMOS_DEFAULT, NULL);
111 if (cmos_default == NULL) {
112 fprintf(stderr, "Need a cmos.default in the CBFS image or separate cmos file (-D).\n");
116 fn = get_layout_from_cbfs_file;
119 /* If the user wants to use a specific layout file or explicitly use
120 * the coreboot option table allow him to override previous settings. */
121 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found) {
122 set_layout_filename(nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].param);
123 fn = get_layout_from_file;
124 } else if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) {
125 fn = get_layout_from_cmos_table;
128 /* Allow the user to use a file for the CMOS contents, possibly
129 * overriding a previously opened "cmos.default" file from the CBFS. */
130 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].found) {
134 if ((fd = open(nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param, O_RDWR | O_CREAT, 0666)) < 0) {
135 fprintf(stderr, "Couldn't open '%s'\n", nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param);
139 if (fstat(fd, &fd_stat) == -1) {
140 fprintf(stderr, "Couldn't stat '%s'\n", nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param);
144 if (fd_stat.st_size < 128) {
145 lseek(fd, 127, SEEK_SET);
146 if (write(fd, "\0", 1) != 1) {
147 fprintf(stderr, "Unable to extended '%s' to its full size.\n",
148 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param);
154 cmos_default = mmap(NULL, 128, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
155 if (cmos_default == MAP_FAILED) {
156 fprintf(stderr, "Couldn't map '%s'\n", nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param);
161 /* Switch to memory based CMOS access. */
163 select_hal(HAL_MEMORY, cmos_default);
166 register_cmos_layout_get_fn(fn);
167 op_fns[nvramtool_op.op]();
172 /****************************************************************************
177 * Show version information for this program.
178 ****************************************************************************/
179 static void op_show_version(void)
181 printf("This is %s version %s.\n", prog_name, prog_version);
184 /****************************************************************************
189 * Show a usage message for this program.
190 ****************************************************************************/
191 static void op_show_usage(void)
196 /****************************************************************************
197 * op_lbtable_show_info
201 * If ARG is present, show coreboot table information specified by ARG.
202 * Else show all possible values for ARG.
203 ****************************************************************************/
204 static void op_lbtable_show_info(void)
206 if (nvramtool_op.param == NULL)
207 list_lbtable_choices();
210 list_lbtable_item(nvramtool_op.param);
214 /****************************************************************************
219 * Do low-level dump of coreboot table.
220 ****************************************************************************/
221 static void op_lbtable_dump(void)
227 /****************************************************************************
228 * op_show_param_values
232 * Show all possible values for parameter NAME.
233 ****************************************************************************/
234 static void op_show_param_values(void)
237 list_param_enums(nvramtool_op.param);
240 /****************************************************************************
241 * op_cmos_show_one_param
245 * Show parameter NAME. If -n is specified, show value only. Else show name
247 ****************************************************************************/
248 static void op_cmos_show_one_param(void)
253 result = list_one_param(nvramtool_op.param,
254 !nvramtool_op_modifiers
255 [NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found);
256 cmos_checksum_verify();
262 /****************************************************************************
263 * op_cmos_show_all_params
267 * Show names and values for all parameters.
268 ****************************************************************************/
269 static void op_cmos_show_all_params(void)
274 result = list_all_params();
275 cmos_checksum_verify();
281 /****************************************************************************
282 * op_cmos_set_one_param
286 * Set parameter NAME to VALUE.
287 ****************************************************************************/
288 static void op_cmos_set_one_param(void)
290 const char *name, *value;
294 /* Separate 'NAME=VALUE' syntax into two strings representing NAME and
297 parse_assignment(nvramtool_op.param, &name, &value);
299 set_one_param(name, value);
302 /****************************************************************************
303 * op_cmos_set_params_stdin
307 * Set parameters according to standard input.
308 ****************************************************************************/
309 static void op_cmos_set_params_stdin(void)
315 /****************************************************************************
316 * op_cmos_set_params_file
320 * Set parameters according to INPUT_FILE.
321 ****************************************************************************/
322 static void op_cmos_set_params_file(void)
326 if ((f = fopen(nvramtool_op.param, "r")) == NULL) {
327 fprintf(stderr, "%s: Can not open file %s for reading: %s\n",
328 prog_name, nvramtool_op.param, strerror(errno));
337 /****************************************************************************
342 * If VALUE is present, set coreboot CMOS checksum to VALUE. Else show
344 ****************************************************************************/
345 static void op_cmos_checksum(void)
351 if (nvramtool_op.param == NULL) {
353 checksum = cmos_checksum_read();
355 printf("0x%x\n", checksum);
357 checksum = convert_checksum_value(nvramtool_op.param);
359 cmos_checksum_write(checksum);
364 /****************************************************************************
369 * Write CMOS layout information to standard output.
370 ****************************************************************************/
371 static void op_show_layout(void)
374 write_cmos_layout(stdout);
377 /****************************************************************************
382 * Write the contents of CMOS memory to a binary file.
383 ****************************************************************************/
384 static void op_write_cmos_dump(void)
386 unsigned char data[CMOS_SIZE];
389 if ((f = fopen(nvramtool_op.param, "w")) == NULL) {
390 fprintf(stderr, "%s: Can not open file %s for writing: %s\n",
391 prog_name, nvramtool_op.param, strerror(errno));
399 if (fwrite(data, 1, CMOS_SIZE, f) != CMOS_SIZE) {
400 fprintf(stderr, "%s: Error writing CMOS data to file %s: %s\n",
401 prog_name, nvramtool_op.param, strerror(errno));
408 /****************************************************************************
413 * Read binary data from a file and write the data to CMOS memory.
414 ****************************************************************************/
415 static void op_read_cmos_dump(void)
417 unsigned char data[CMOS_SIZE];
421 if ((f = fopen(nvramtool_op.param, "r")) == NULL) {
422 fprintf(stderr, "%s: Can not open file %s for reading: %s\n",
423 prog_name, nvramtool_op.param, strerror(errno));
427 if ((nr_bytes = fread(data, 1, CMOS_SIZE, f)) != CMOS_SIZE) {
429 "%s: Error: Only able to read %d bytes of CMOS data "
430 "from file %s. CMOS data is unchanged.\n", prog_name,
431 (int)nr_bytes, nvramtool_op.param);
437 cmos_write_all(data);
441 /****************************************************************************
442 * op_show_cmos_hex_dump
446 * Write a hex dump of CMOS memory to standard output.
447 ****************************************************************************/
448 static void op_show_cmos_hex_dump(void)
450 unsigned char data[CMOS_SIZE];
455 hexdump(data, CMOS_SIZE, 0, stdout, &cmos_dump_format);
458 /****************************************************************************
459 * op_show_cmos_dumpfile
463 * Read binary data from a file (presumably a CMOS dump file) and display a
464 * hex dump of the CMOS data from the file.
465 ****************************************************************************/
466 static void op_show_cmos_dumpfile(void)
468 unsigned char data[CMOS_SIZE];
472 if ((f = fopen(nvramtool_op.param, "r")) == NULL) {
473 fprintf(stderr, "%s: Can not open file %s for reading: %s\n",
474 prog_name, nvramtool_op.param, strerror(errno));
478 nr_bytes = fread(data, 1, CMOS_SIZE, f);
480 hexdump(data, nr_bytes, 0, stdout, &cmos_dump_format);
483 /****************************************************************************
486 * Attempt to list one CMOS parameter given by 'name'. 'show_name' is a
487 * boolean value indicating whether the parameter name should be displayed
488 * along with its value. Return 1 if error was encountered. Else return OK.
489 ****************************************************************************/
490 static int list_one_param(const char name[], int show_name)
492 const cmos_entry_t *e;
494 if (is_checksum_name(name) || ((e = find_cmos_entry(name)) == NULL)) {
495 fprintf(stderr, "%s: CMOS parameter %s not found.\n", prog_name,
500 if (e->config == CMOS_ENTRY_RESERVED) {
501 fprintf(stderr, "%s: Parameter %s is reserved.\n", prog_name,
506 return (list_cmos_entry(e, show_name) != 0);
509 /****************************************************************************
512 * Attempt to list all CMOS parameters. Return 1 if error was encountered.
514 ****************************************************************************/
515 static int list_all_params(void)
517 const cmos_entry_t *e;
522 for (e = first_cmos_entry(); e != NULL; e = next_cmos_entry(e)) {
523 if ((e->config == CMOS_ENTRY_RESERVED)
524 || is_checksum_name(e->name))
527 if (list_cmos_entry(e, TRUE))
534 /****************************************************************************
537 * List all possible values for CMOS parameter given by 'name'.
538 ****************************************************************************/
539 static void list_param_enums(const char name[])
541 const cmos_entry_t *e;
542 const cmos_enum_t *p;
544 if (is_checksum_name(name) || (e = find_cmos_entry(name)) == NULL) {
545 fprintf(stderr, "%s: CMOS parameter %s not found.\n", prog_name,
551 case CMOS_ENTRY_ENUM:
552 for (p = first_cmos_enum_id(e->config_id);
553 p != NULL; p = next_cmos_enum_id(p))
554 printf("%s\n", p->text);
559 printf("Parameter %s requires a %u-bit unsigned integer.\n",
563 case CMOS_ENTRY_STRING:
564 printf("Parameter %s requires a %u-byte string.\n", name,
568 case CMOS_ENTRY_RESERVED:
569 printf("Parameter %s is reserved.\n", name);
577 /****************************************************************************
580 * Set the CMOS parameter given by 'name' to 'value'. The 'name' parameter
581 * is case-sensitive. If we are setting an enum parameter, then 'value' is
582 * interpreted as a case-sensitive string that must match the option name
583 * exactly. If we are setting a 'hex' parameter, then 'value' is treated as
584 * a string representation of an unsigned integer that may be specified in
585 * decimal, hex, or octal.
586 ****************************************************************************/
587 static void set_one_param(const char name[], const char value[])
589 const cmos_entry_t *e;
590 unsigned long long n;
592 if (is_checksum_name(name) || (e = find_cmos_entry(name)) == NULL) {
593 fprintf(stderr, "%s: CMOS parameter %s not found.", prog_name,
598 switch (prepare_cmos_write(e, value, &n)) {
602 case CMOS_OP_BAD_ENUM_VALUE:
603 fprintf(stderr, "%s: Bad value for parameter %s.", prog_name,
607 case CMOS_OP_NEGATIVE_INT:
609 "%s: This program does not support assignment of negative "
610 "numbers to coreboot parameters.", prog_name);
613 case CMOS_OP_INVALID_INT:
614 fprintf(stderr, "%s: %s is not a valid integer.", prog_name,
618 case CMOS_OP_RESERVED:
620 "%s: Can not modify reserved coreboot parameter %s.",
624 case CMOS_OP_VALUE_TOO_WIDE:
626 "%s: Can not write value %s to CMOS parameter %s that is "
627 "only %d bits wide.", prog_name, value, name,
631 case CMOS_OP_NO_MATCHING_ENUM:
633 "%s: coreboot parameter %s has no matching enums.",
637 case CMOS_AREA_OUT_OF_RANGE:
639 "%s: The CMOS area specified by the layout info for "
640 "coreboot parameter %s is out of range.", prog_name,
644 case CMOS_AREA_OVERLAPS_RTC:
646 "%s: The CMOS area specified by the layout info for "
647 "coreboot parameter %s overlaps the realtime clock area.",
651 case CMOS_AREA_TOO_WIDE:
653 "%s: The CMOS area specified by the layout info for "
654 "coreboot parameter %s is too wide.", prog_name, name);
659 "%s: Unknown error encountered while attempting to modify "
660 "coreboot parameter %s.", prog_name, name);
664 /* write the value to nonvolatile RAM */
667 cmos_checksum_write(cmos_checksum_compute());
672 fprintf(stderr, " CMOS write not performed.\n");
676 /****************************************************************************
679 * Set coreboot parameters according to the contents of file 'f'.
680 ****************************************************************************/
681 static void set_params(FILE * f)
682 { /* First process the input file. Then perform writes only if there were
683 * no problems processing the input. Either all values will be written
684 * successfully or no values will be written.
686 do_cmos_writes(process_input_file(f));
689 /****************************************************************************
692 * Parse the string 'arg' (which supposedly represents an assignment) into a
693 * NAME and a VALUE. If 'arg' does not conform to the proper assignment
694 * syntax, exit with a usage message. Otherwise, on return, 'arg' is broken
695 * into substrings representing NAME and VALUE, and *name and *value are set
696 * to point to these two substrings.
697 ****************************************************************************/
698 static void parse_assignment(char arg[], const char **name, const char **value)
700 static const size_t N_MATCHES = 4;
701 regmatch_t match[N_MATCHES];
704 compile_reg_expr(REG_EXTENDED | REG_NEWLINE, assignment_regex, &assignment);
706 /* Does 'arg' conform to proper assignment syntax? If not, exit with a
709 if (regexec(&assignment, arg, N_MATCHES, match, 0))
712 /* Ok, we found a valid assignment. Break it into two strings
713 * representing NAME and VALUE.
715 arg[match[1].rm_eo] = '\0';
716 arg[match[2].rm_eo] = '\0';
717 *name = &arg[match[1].rm_so];
718 *value = &arg[match[2].rm_so];
720 regfree(&assignment);
723 /****************************************************************************
726 * Attempt to list the CMOS entry represented by 'e'. 'show_name' is a
727 * boolean value indicating whether the parameter name should be displayed
728 * along with its value. On success, return OK. On error, print an error
729 * message and return 1.
730 ****************************************************************************/
731 static int list_cmos_entry(const cmos_entry_t * e, int show_name)
733 const cmos_enum_t *p;
734 unsigned long long value;
737 /* sanity check CMOS entry */
738 switch (prepare_cmos_read(e)) {
742 case CMOS_OP_RESERVED:
745 case CMOS_AREA_OUT_OF_RANGE:
747 "%s: Can not read coreboot parameter %s because "
748 "layout info specifies out of range CMOS area.\n",
752 case CMOS_AREA_OVERLAPS_RTC:
754 "%s: Can not read coreboot parameter %s because "
755 "layout info specifies CMOS area that overlaps realtime "
756 "clock area.\n", prog_name, e->name);
759 case CMOS_AREA_TOO_WIDE:
761 "%s: Can not read coreboot parameter %s because "
762 "layout info specifies CMOS area that is too wide.\n",
768 "%s: Unknown error encountered while attempting to "
769 "read coreboot parameter %s\n", prog_name, e->name);
773 /* read the value from CMOS */
775 value = cmos_read(e);
778 /* display the value */
780 case CMOS_ENTRY_ENUM:
781 if ((p = find_cmos_enum(e->config_id, value)) == NULL) {
783 printf("# Bad value -> %s = 0x%llx\n", e->name,
786 printf("Bad value -> 0x%llx\n", value);
789 printf("%s = %s\n", e->name, p->text);
791 printf("%s\n", p->text);
798 printf("%s = 0x%llx\n", e->name, value);
800 printf("0x%llx\n", value);
804 case CMOS_ENTRY_STRING:
805 w = (char *)(unsigned long)value;
807 if(!isprint((int)(unsigned char)*w)) {
809 printf("# Bad value -> %s\n", e->name);
811 printf("Bad value\n");
820 printf("%s = %s\n", e->name,
821 (char *)(unsigned long)value);
823 printf("%s\n", (char *)(unsigned long)value);
826 free((void *)(unsigned long)value);
830 case CMOS_ENTRY_RESERVED:
838 /****************************************************************************
839 * convert_checksum_value
841 * 'value' is the string representation of a checksum value that the user
842 * wishes to set using the -c option. Convert the string to a 16-bit
843 * unsigned integer and return the result. Exit with an error message if
844 * 'value' is invalid.
845 ****************************************************************************/
846 static uint16_t convert_checksum_value(const char value[])
853 for (p = value; isspace((int)(unsigned char)*p); p++) ;
855 negative = (*p == '-');
856 n = strtoul(value, (char **)&p, 0);
860 "%s: Checksum value %s is not a valid integer.\n",
867 "%s: Checksum must be an unsigned integer.\n",
872 result = (uint16_t) n;
876 "%s: Checksum value must fit within 16 bits.\n",