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 David S. 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 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 \*****************************************************************************/
32 #include "input_file.h"
35 #include "cmos_lowlevel.h"
38 static int get_input_file_line (FILE *f, char line[], int line_buf_size);
39 static unsigned long long try_prepare_cmos_write (const cmos_entry_t *e,
40 const char value_str[]);
42 /* matches either a blank line or a comment line */
43 static const char blank_or_comment_regex[] =
49 /* a line consisting of: optional whitespace followed by */
51 /* a '#' character and optionally, additional characters */
54 /* matches an assignment line */
55 const char assignment_regex[] =
56 /* optional whitespace */
58 /* followed by a coreboot parameter name */
60 /* followed by optional whitespace */
62 /* followed by an '=' character */
64 /* followed by optional whitespace */
66 /* followed by a value that may contain embedded whitespace */
67 "([^[:space:]]+([[:space:]]+[^[:space:]]+)*)+"
68 /* followed by optional whitespace */
73 /****************************************************************************
76 * Read the contents of file 'f' and return a pointer to a list of pending
77 * write operations. Perform sanity checking on all write operations and
78 * exit with an error message if there is a problem.
79 ****************************************************************************/
80 cmos_write_t * process_input_file (FILE *f)
82 static const int LINE_BUF_SIZE = 256;
83 static const size_t N_MATCHES = 4;
84 char line[LINE_BUF_SIZE];
85 const char *name, *value;
86 cmos_write_t *list, *item, **p;
87 regex_t blank_or_comment, assignment;
88 regmatch_t match[N_MATCHES];
89 const cmos_entry_t *e;
94 compile_reg_exprs(REG_EXTENDED | REG_NEWLINE, 2, blank_or_comment_regex,
95 &blank_or_comment, assignment_regex, &assignment);
97 /* each iteration processes one line from input file */
99 get_input_file_line(f, line, LINE_BUF_SIZE) == OK;
101 { /* skip comments and blank lines */
102 if (!regexec(&blank_or_comment, line, 0, NULL, 0))
105 /* Is this a valid assignment line? If not, then it's a syntax
108 if (regexec(&assignment, line, N_MATCHES, match, 0))
109 { fprintf(stderr, "%s: Syntax error on line %d of input file.\n",
110 prog_name, line_num);
114 /* OK, we found an assignment. Break the line into substrings
115 * representing the lefthand and righthand sides of the assignment.
117 line[match[1].rm_eo] = '\0';
118 line[match[2].rm_eo] = '\0';
119 name = &line[match[1].rm_so];
120 value = &line[match[2].rm_so];
122 /* now look up the coreboot parameter name */
123 if (is_checksum_name(name) || (e = find_cmos_entry(name)) == NULL)
124 { fprintf(stderr, "%s: Error on line %d of input file: CMOS parameter "
125 "%s not found.\n", prog_name, line_num, name);
129 /* At this point, we figure out what numeric value needs to be written
130 * to which location. At the same time, we perform sanity checking on
131 * the write operation.
134 if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL)
138 item->length = e->length;
139 item->config = e->config;
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;
168 e.length = item->length;
169 e.config = item->config;
171 cmos_write(&e, item->value);
175 cmos_checksum_write(cmos_checksum_compute());
179 /****************************************************************************
180 * get_input_file_line
182 * Get a line of input from file 'f'. Store result in 'line' which is an
183 * array of 'line_buf_size' bytes. Return OK on success or an error code on
185 ****************************************************************************/
186 static int get_input_file_line (FILE *f, char line[], int line_buf_size)
187 { switch (get_line_from_file(f, line, line_buf_size))
195 fprintf(stderr, "%s: Error on line %d of input file: Maximum line "
196 "length exceeded. Max is %d characters.\n", prog_name,
197 line_num, line_buf_size - 2);
205 return 1; /* keep compiler happy */
208 /****************************************************************************
209 * try_prepare_cmos_write
211 * Attempt to convert 'value_str' to an integer representation for storage in
212 * CMOS memory. On success, return the converted value. On error, exit with
214 ****************************************************************************/
215 static unsigned long long try_prepare_cmos_write (const cmos_entry_t *e,
216 const char value_str[])
217 { unsigned long long value;
219 switch (prepare_cmos_write(e, value_str, &value))
223 case CMOS_OP_BAD_ENUM_VALUE:
224 fprintf(stderr, "%s: Error on line %d of input file: Bad value for "
225 "parameter %s.", prog_name, line_num, e->name);
228 case CMOS_OP_NEGATIVE_INT:
229 fprintf(stderr, "%s: Error on line %d of input file: This program "
230 "does not support assignment of negative numbers to "
231 "coreboot parameters.", prog_name, line_num);
234 case CMOS_OP_INVALID_INT:
235 fprintf(stderr, "%s: Error on line %d of input file: %s is not a "
236 "valid integer.", prog_name, line_num, value_str);
239 case CMOS_OP_RESERVED:
240 fprintf(stderr, "%s: Error on line %d of input file: Can not modify "
241 "reserved coreboot parameter %s.", prog_name, line_num,
245 case CMOS_OP_VALUE_TOO_WIDE:
246 fprintf(stderr, "%s: Error on line %d of input file: Can not write "
247 "value %s to CMOS parameter %s that is only %d bits wide.",
248 prog_name, line_num, value_str, e->name, e->length);
251 case CMOS_OP_NO_MATCHING_ENUM:
252 fprintf(stderr, "%s: coreboot parameter %s has no matching enums.",
256 case CMOS_AREA_OUT_OF_RANGE:
257 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
258 "coreboot parameter %s is out of range.", prog_name,
262 case CMOS_AREA_OVERLAPS_RTC:
263 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
264 "coreboot parameter %s overlaps the realtime clock area.",
268 case CMOS_AREA_TOO_WIDE:
269 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
270 "coreboot parameter %s is too wide.",
276 "%s: Unknown error encountered while attempting to modify "
277 "coreboot parameter %s.", prog_name, e->name);
281 fprintf(stderr, " No CMOS writes performed.\n");
283 return 0; /* keep compiler happy */