blob: 99b580e9e57e61e71714359f38f03202829829a5 [file] [log] [blame]
Stefan Reinauer6540ae52007-07-12 16:35:42 +00001/*****************************************************************************\
2 * input_file.c
Stefan Reinauer6540ae52007-07-12 16:35:42 +00003 *****************************************************************************
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>.
7 * UCRL-CODE-2003-012
8 * All rights reserved.
9 *
Uwe Hermann6e565942008-03-01 19:06:32 +000010 * This file is part of nvramtool, a utility for reading/writing coreboot
Stefan Reinauerf527e702008-01-18 15:33:49 +000011 * parameters and displaying information from the coreboot table.
Uwe Hermann6e565942008-03-01 19:06:32 +000012 * For details, see http://coreboot.org/nvramtool.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000013 *
14 * Please also read the file DISCLAIMER which is included in this software
15 * distribution.
16 *
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.
20 *
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.
25 *
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.,
Stefan Reinauerac7a2d22009-09-23 21:53:25 +000028 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000029\*****************************************************************************/
30
31#include "common.h"
32#include "input_file.h"
33#include "layout.h"
34#include "cmos_ops.h"
35#include "cmos_lowlevel.h"
36#include "reg_expr.h"
37
38static int get_input_file_line (FILE *f, char line[], int line_buf_size);
39static unsigned long long try_prepare_cmos_write (const cmos_entry_t *e,
40 const char value_str[]);
41
42/* matches either a blank line or a comment line */
43static const char blank_or_comment_regex[] =
44 /* a blank line */
45 "(^[[:space:]]+$)"
46
47 "|" /* or ... */
48
49 /* a line consisting of: optional whitespace followed by */
50 "(^[[:space:]]*"
51 /* a '#' character and optionally, additional characters */
52 "#.*$)";
53
54/* matches an assignment line */
55const char assignment_regex[] =
56 /* optional whitespace */
57 "^[[:space:]]*"
Stefan Reinauerf527e702008-01-18 15:33:49 +000058 /* followed by a coreboot parameter name */
Stefan Reinauer6540ae52007-07-12 16:35:42 +000059 "([^[:space:]]+)"
60 /* followed by optional whitespace */
61 "[[:space:]]*"
62 /* followed by an '=' character */
63 "="
64 /* followed by optional whitespace */
65 "[[:space:]]*"
66 /* followed by a value that may contain embedded whitespace */
67 "([^[:space:]]+([[:space:]]+[^[:space:]]+)*)+"
68 /* followed by optional whitespace */
69 "[[:space:]]*$";
70
71static int line_num;
72
73/****************************************************************************
74 * process_input_file
75 *
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 ****************************************************************************/
80cmos_write_t * process_input_file (FILE *f)
81 {
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;
90
91 list = NULL;
92 p = &list;
93
94 compile_reg_exprs(REG_EXTENDED | REG_NEWLINE, 2, blank_or_comment_regex,
95 &blank_or_comment, assignment_regex, &assignment);
96
97 /* each iteration processes one line from input file */
98 for (line_num = 1;
99 get_input_file_line(f, line, LINE_BUF_SIZE) == OK;
100 line_num++)
101 { /* skip comments and blank lines */
102 if (!regexec(&blank_or_comment, line, 0, NULL, 0))
103 continue;
104
105 /* Is this a valid assignment line? If not, then it's a syntax
106 * error.
107 */
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);
111 exit(1);
112 }
113
114 /* OK, we found an assignment. Break the line into substrings
115 * representing the lefthand and righthand sides of the assignment.
116 */
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];
121
Stefan Reinauerf527e702008-01-18 15:33:49 +0000122 /* now look up the coreboot parameter name */
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000123 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);
126 exit(1);
127 }
128
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.
132 */
133
134 if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL)
135 out_of_memory();
136
137 item->bit = e->bit;
138 item->length = e->length;
Stefan Reinauera67aab72008-09-27 10:08:28 +0000139 item->config = e->config;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000140 item->value = try_prepare_cmos_write(e, value);
141
142 /* Append write operation to pending write list. */
143 item->next = NULL;
144 *p = item;
145 p = &item->next;
146 }
147
148 free_reg_exprs(2, &blank_or_comment, &assignment);
149 return list;
150 }
151
152/****************************************************************************
153 * do_cmos_writes
154 *
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
157 * we go.
158 ****************************************************************************/
159void do_cmos_writes (cmos_write_t *list)
160 { cmos_write_t *item;
161
162 set_iopl(3);
163
164 while (list != NULL)
Stefan Reinauera67aab72008-09-27 10:08:28 +0000165 { cmos_entry_t e;
166 item = list;
167 e.bit = item->bit;
168 e.length = item->length;
169 e.config = item->config;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000170 list = item->next;
Stefan Reinauera67aab72008-09-27 10:08:28 +0000171 cmos_write(&e, item->value);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000172 free(item);
173 }
174
175 cmos_checksum_write(cmos_checksum_compute());
176 set_iopl(0);
177 }
178
179/****************************************************************************
180 * get_input_file_line
181 *
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
184 * error.
185 ****************************************************************************/
186static int get_input_file_line (FILE *f, char line[], int line_buf_size)
187 { switch (get_line_from_file(f, line, line_buf_size))
188 { case OK:
189 return OK;
190
191 case LINE_EOF:
192 return LINE_EOF;
193
194 case LINE_TOO_LONG:
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);
198 break;
199
200 default:
201 BUG();
202 }
203
204 exit(1);
205 return 1; /* keep compiler happy */
206 }
207
208/****************************************************************************
209 * try_prepare_cmos_write
210 *
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
213 * an error message.
214 ****************************************************************************/
215static unsigned long long try_prepare_cmos_write (const cmos_entry_t *e,
216 const char value_str[])
217 { unsigned long long value;
218
219 switch (prepare_cmos_write(e, value_str, &value))
220 { case OK:
221 return value;
222
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);
226 break;
227
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 "
Stefan Reinauerf527e702008-01-18 15:33:49 +0000231 "coreboot parameters.", prog_name, line_num);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000232 break;
233
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);
237 break;
238
239 case CMOS_OP_RESERVED:
240 fprintf(stderr, "%s: Error on line %d of input file: Can not modify "
Stefan Reinauerf527e702008-01-18 15:33:49 +0000241 "reserved coreboot parameter %s.", prog_name, line_num,
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000242 e->name);
243 break;
244
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);
249 break;
250
251 case CMOS_OP_NO_MATCHING_ENUM:
Stefan Reinauerf527e702008-01-18 15:33:49 +0000252 fprintf(stderr, "%s: coreboot parameter %s has no matching enums.",
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000253 prog_name, e->name);
254 break;
255
256 case CMOS_AREA_OUT_OF_RANGE:
257 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
Stefan Reinauerf527e702008-01-18 15:33:49 +0000258 "coreboot parameter %s is out of range.", prog_name,
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000259 e->name);
260 break;
261
262 case CMOS_AREA_OVERLAPS_RTC:
263 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
Stefan Reinauerf527e702008-01-18 15:33:49 +0000264 "coreboot parameter %s overlaps the realtime clock area.",
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000265 prog_name, e->name);
266 break;
267
268 case CMOS_AREA_TOO_WIDE:
269 fprintf(stderr, "%s: The CMOS area specified by the layout info for "
Stefan Reinauerf527e702008-01-18 15:33:49 +0000270 "coreboot parameter %s is too wide.",
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000271 prog_name, e->name);
272 break;
273
274 default:
275 fprintf(stderr,
276 "%s: Unknown error encountered while attempting to modify "
Stefan Reinauerf527e702008-01-18 15:33:49 +0000277 "coreboot parameter %s.", prog_name, e->name);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000278 break;
279 }
280
281 fprintf(stderr, " No CMOS writes performed.\n");
282 exit(1);
283 return 0; /* keep compiler happy */
284 }