1 /*****************************************************************************\
4 *****************************************************************************
5 * Copyright (C) 2002-2005 The Regents of the University of California.
6 * Produced at the Lawrence Livermore National Laboratory.
7 * Written by David S. Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
11 * This file is part of nvramtool, a utility for reading/writing coreboot
12 * parameters and displaying information from the coreboot table.
13 * For details, see http://coreboot.org/nvramtool.
15 * Please also read the file DISCLAIMER which is included in this software
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License (as published by the
20 * Free Software Foundation) version 2, dated June 1991.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
25 * conditions of the GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30 \*****************************************************************************/
33 #include "input_file.h"
36 #include "cmos_lowlevel.h"
39 static int get_input_file_line (FILE *f, char line[], int line_buf_size);
40 static unsigned long long try_prepare_cmos_write (const cmos_entry_t *e,
41 const char value_str[]);
43 /* matches either a blank line or a comment line */
44 static const char blank_or_comment_regex[] =
50 /* a line consisting of: optional whitespace followed by */
52 /* a '#' character and optionally, additional characters */
55 /* matches an assignment line */
56 const char assignment_regex[] =
57 /* optional whitespace */
59 /* followed by a coreboot parameter name */
61 /* followed by optional whitespace */
63 /* followed by an '=' character */
65 /* followed by optional whitespace */
67 /* followed by a value that may contain embedded whitespace */
68 "([^[:space:]]+([[:space:]]+[^[:space:]]+)*)+"
69 /* followed by optional whitespace */
74 /****************************************************************************
77 * Read the contents of file 'f' and return a pointer to a list of pending
78 * write operations. Perform sanity checking on all write operations and
79 * exit with an error message if there is a problem.
80 ****************************************************************************/
81 cmos_write_t * process_input_file (FILE *f)
83 static const int LINE_BUF_SIZE = 256;
84 static const size_t N_MATCHES = 4;
85 char line[LINE_BUF_SIZE];
86 const char *name, *value;
87 cmos_write_t *list, *item, **p;
88 regex_t blank_or_comment, assignment;
89 regmatch_t match[N_MATCHES];
90 const cmos_entry_t *e;
95 compile_reg_exprs(REG_EXTENDED | REG_NEWLINE, 2, blank_or_comment_regex,
96 &blank_or_comment, assignment_regex, &assignment);
98 /* each iteration processes one line from input file */
100 get_input_file_line(f, line, LINE_BUF_SIZE) == OK;
102 { /* skip comments and blank lines */
103 if (!regexec(&blank_or_comment, line, 0, NULL, 0))
106 /* Is this a valid assignment line? If not, then it's a syntax
109 if (regexec(&assignment, line, N_MATCHES, match, 0))
110 { fprintf(stderr, "%s: Syntax error on line %d of input file.\n",
111 prog_name, line_num);
115 /* OK, we found an assignment. Break the line into substrings
116 * representing the lefthand and righthand sides of the assignment.
118 line[match[1].rm_eo] = '\0';
119 line[match[2].rm_eo] = '\0';
120 name = &line[match[1].rm_so];
121 value = &line[match[2].rm_so];
123 /* now look up the coreboot parameter name */
124 if (is_checksum_name(name) || (e = find_cmos_entry(name)) == NULL)
125 { fprintf(stderr, "%s: Error on line %d of input file: CMOS parameter "
126 "%s not found.\n", prog_name, line_num, name);
130 /* At this point, we figure out what numeric value needs to be written
131 * to which location. At the same time, we perform sanity checking on
132 * the write operation.
135 if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL)
139 item->length = e->length;
140 item->value = try_prepare_cmos_write(e, value);
142 /* Append write operation to pending write list. */
148 free_reg_exprs(2, &blank_or_comment, &assignment);
152 /****************************************************************************
155 * 'list' is a linked list of pending CMOS write operations that have passed
156 * all sanity checks. Perform all write operations, destroying the list as
158 ****************************************************************************/
159 void do_cmos_writes (cmos_write_t *list)
160 { cmos_write_t *item;
167 cmos_write(item->bit, item->length, item->value);
171 cmos_checksum_write(cmos_checksum_compute());
175 /****************************************************************************
176 * get_input_file_line
178 * Get a line of input from file 'f'. Store result in 'line' which is an
179 * array of 'line_buf_size' bytes. Return OK on success or an error code on
181 ****************************************************************************/
182 static int get_input_file_line (FILE *f, char line[], int line_buf_size)
183 { switch (get_line_from_file(f, line, line_buf_size))
191 fprintf(stderr, "%s: Error on line %d of input file: Maximum line "
192 "length exceeded. Max is %d characters.\n", prog_name,
193 line_num, line_buf_size - 2);
201 return 1; /* keep compiler happy */
204 /****************************************************************************
205 * try_prepare_cmos_write
207 * Attempt to convert 'value_str' to an integer representation for storage in
208 * CMOS memory. On success, return the converted value. On error, exit with
210 ****************************************************************************/
211 static unsigned long long try_prepare_cmos_write (const cmos_entry_t *e,
212 const char value_str[])
213 { unsigned long long value;
215 switch (prepare_cmos_write(e, value_str, &value))
219 case CMOS_OP_BAD_ENUM_VALUE:
220 fprintf(stderr, "%s: Error on line %d of input file: Bad value for "
221 "parameter %s.", prog_name, line_num, e->name);
224 case CMOS_OP_NEGATIVE_INT:
225 fprintf(stderr, "%s: Error on line %d of input file: This program "
226 "does not support assignment of negative numbers to "
227 "coreboot parameters.", prog_name, line_num);
230 case CMOS_OP_INVALID_INT:
231 fprintf(stderr, "%s: Error on line %d of input file: %s is not a "
232 "valid integer.", prog_name, line_num, value_str);
235 case CMOS_OP_RESERVED:
236 fprintf(stderr, "%s: Error on line %d of input file: Can not modify "
237 "reserved coreboot parameter %s.", prog_name, line_num,
241 case CMOS_OP_VALUE_TOO_WIDE:
242 fprintf(stderr, "%s: Error on line %d of input file: Can not write "
243 "value %s to CMOS parameter %s that is only %d bits wide.",
244 prog_name, line_num, value_str, e->name, e->length);
247 case CMOS_OP_NO_MATCHING_ENUM:
248 fprintf(stderr, "%s: coreboot parameter %s has no matching enums.",
252 case CMOS_AREA_OUT_OF_RANGE:
253 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
254 "coreboot parameter %s is out of range.", prog_name,
258 case CMOS_AREA_OVERLAPS_RTC:
259 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
260 "coreboot parameter %s overlaps the realtime clock area.",
264 case CMOS_AREA_TOO_WIDE:
265 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
266 "coreboot parameter %s is too wide.",
272 "%s: Unknown error encountered while attempting to modify "
273 "coreboot parameter %s.", prog_name, e->name);
277 fprintf(stderr, " No CMOS writes performed.\n");
279 return 0; /* keep compiler happy */