blob: cce9a10894c8f86ddf69efbc3715697ec3657218 [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.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000025\*****************************************************************************/
26
27#include "common.h"
28#include "input_file.h"
29#include "layout.h"
30#include "cmos_ops.h"
31#include "cmos_lowlevel.h"
32#include "reg_expr.h"
33
Stefan Reinauer90b96b62010-01-13 21:00:23 +000034static int get_input_file_line(FILE * f, char line[], int line_buf_size);
35static unsigned long long try_prepare_cmos_write(const cmos_entry_t * e,
36 const char value_str[]);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000037
38/* matches either a blank line or a comment line */
39static const char blank_or_comment_regex[] =
Stefan Reinauer90b96b62010-01-13 21:00:23 +000040 /* a blank line */
41 "(^[[:space:]]+$)" "|" /* or ... */
42 /* a line consisting of: optional whitespace followed by */
43 "(^[[:space:]]*"
44 /* a '#' character and optionally, additional characters */
45 "#.*$)";
Stefan Reinauer6540ae52007-07-12 16:35:42 +000046
47/* matches an assignment line */
48const char assignment_regex[] =
Stefan Reinauer90b96b62010-01-13 21:00:23 +000049 /* optional whitespace */
50 "^[[:space:]]*"
51 /* followed by a coreboot parameter name */
52 "([^[:space:]]+)"
53 /* followed by optional whitespace */
54 "[[:space:]]*"
55 /* followed by an '=' character */
56 "="
57 /* followed by optional whitespace */
58 "[[:space:]]*"
59 /* followed by a value that may contain embedded whitespace */
60 "([^[:space:]]+([[:space:]]+[^[:space:]]+)*)+"
61 /* followed by optional whitespace */
62 "[[:space:]]*$";
Stefan Reinauer6540ae52007-07-12 16:35:42 +000063
64static int line_num;
65
66/****************************************************************************
67 * process_input_file
68 *
69 * Read the contents of file 'f' and return a pointer to a list of pending
70 * write operations. Perform sanity checking on all write operations and
71 * exit with an error message if there is a problem.
72 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000073cmos_write_t *process_input_file(FILE * f)
74{
75 static const int LINE_BUF_SIZE = 256;
76 static const size_t N_MATCHES = 4;
77 char line[LINE_BUF_SIZE];
78 const char *name, *value;
79 cmos_write_t *list, *item, **p;
80 regex_t blank_or_comment, assignment;
81 regmatch_t match[N_MATCHES];
82 const cmos_entry_t *e;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000083
Stefan Reinauer90b96b62010-01-13 21:00:23 +000084 list = NULL;
85 p = &list;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000086
Patrick Georgibf649852011-01-28 07:40:08 +000087 compile_reg_expr(REG_EXTENDED | REG_NEWLINE, blank_or_comment_regex, &blank_or_comment);
88 compile_reg_expr(REG_EXTENDED | REG_NEWLINE, assignment_regex, &assignment);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000089
Stefan Reinauer90b96b62010-01-13 21:00:23 +000090 /* each iteration processes one line from input file */
91 for (line_num = 1; get_input_file_line(f, line, LINE_BUF_SIZE) == OK; line_num++) { /* skip comments and blank lines */
92 if (!regexec(&blank_or_comment, line, 0, NULL, 0))
93 continue;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000094
Stefan Reinauer90b96b62010-01-13 21:00:23 +000095 /* Is this a valid assignment line? If not, then it's a syntax
96 * error.
97 */
98 if (regexec(&assignment, line, N_MATCHES, match, 0)) {
99 fprintf(stderr,
100 "%s: Syntax error on line %d of input file.\n",
101 prog_name, line_num);
102 exit(1);
103 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000104
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000105 /* OK, we found an assignment. Break the line into substrings
106 * representing the lefthand and righthand sides of the assignment.
107 */
108 line[match[1].rm_eo] = '\0';
109 line[match[2].rm_eo] = '\0';
110 name = &line[match[1].rm_so];
111 value = &line[match[2].rm_so];
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000112
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000113 /* now look up the coreboot parameter name */
114 if (is_checksum_name(name)
115 || (e = find_cmos_entry(name)) == NULL) {
116 fprintf(stderr,
117 "%s: Error on line %d of input file: CMOS parameter "
118 "%s not found.\n", prog_name, line_num, name);
119 exit(1);
120 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000121
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000122 /* At this point, we figure out what numeric value needs to be written
123 * to which location. At the same time, we perform sanity checking on
124 * the write operation.
125 */
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000126
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000127 if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL)
128 out_of_memory();
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000129
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000130 item->bit = e->bit;
131 item->length = e->length;
132 item->config = e->config;
133 item->value = try_prepare_cmos_write(e, value);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000134
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000135 /* Append write operation to pending write list. */
136 item->next = NULL;
137 *p = item;
138 p = &item->next;
139 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000140
Patrick Georgibf649852011-01-28 07:40:08 +0000141 regfree(&blank_or_comment);
142 regfree(&assignment);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000143 return list;
144}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000145
146/****************************************************************************
147 * do_cmos_writes
148 *
149 * 'list' is a linked list of pending CMOS write operations that have passed
150 * all sanity checks. Perform all write operations, destroying the list as
151 * we go.
152 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000153void do_cmos_writes(cmos_write_t * list)
154{
155 cmos_write_t *item;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000156
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000157 set_iopl(3);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000158
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000159 while (list != NULL) {
160 cmos_entry_t e;
161 item = list;
162 e.bit = item->bit;
163 e.length = item->length;
164 e.config = item->config;
165 list = item->next;
166 cmos_write(&e, item->value);
167 free(item);
168 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000169
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000170 cmos_checksum_write(cmos_checksum_compute());
171 set_iopl(0);
172}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000173
174/****************************************************************************
175 * get_input_file_line
176 *
177 * Get a line of input from file 'f'. Store result in 'line' which is an
178 * array of 'line_buf_size' bytes. Return OK on success or an error code on
179 * error.
180 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000181static int get_input_file_line(FILE * f, char line[], int line_buf_size)
182{
183 switch (get_line_from_file(f, line, line_buf_size)) {
184 case OK:
185 return OK;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000186
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000187 case LINE_EOF:
188 return LINE_EOF;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000189
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000190 case LINE_TOO_LONG:
191 fprintf(stderr,
192 "%s: Error on line %d of input file: Maximum line "
193 "length exceeded. Max is %d characters.\n", prog_name,
194 line_num, line_buf_size - 2);
195 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000196
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000197 default:
198 BUG();
199 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000200
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000201 exit(1);
202 return 1; /* keep compiler happy */
203}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000204
205/****************************************************************************
206 * try_prepare_cmos_write
207 *
208 * Attempt to convert 'value_str' to an integer representation for storage in
209 * CMOS memory. On success, return the converted value. On error, exit with
210 * an error message.
211 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000212static unsigned long long try_prepare_cmos_write(const cmos_entry_t * e,
213 const char value_str[])
214{
215 unsigned long long value;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000216
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000217 switch (prepare_cmos_write(e, value_str, &value)) {
218 case OK:
219 return value;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000220
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000221 case CMOS_OP_BAD_ENUM_VALUE:
222 fprintf(stderr,
223 "%s: Error on line %d of input file: Bad value for "
224 "parameter %s.", prog_name, line_num, e->name);
225 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000226
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000227 case CMOS_OP_NEGATIVE_INT:
228 fprintf(stderr,
229 "%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);
232 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000233
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000234 case CMOS_OP_INVALID_INT:
235 fprintf(stderr,
236 "%s: Error on line %d of input file: %s is not a "
237 "valid integer.", prog_name, line_num, value_str);
238 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000239
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000240 case CMOS_OP_RESERVED:
241 fprintf(stderr,
242 "%s: Error on line %d of input file: Can not modify "
243 "reserved coreboot parameter %s.", prog_name, line_num,
244 e->name);
245 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000246
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000247 case CMOS_OP_VALUE_TOO_WIDE:
248 fprintf(stderr,
249 "%s: Error on line %d of input file: Can not write "
250 "value %s to CMOS parameter %s that is only %d bits wide.",
251 prog_name, line_num, value_str, e->name, e->length);
252 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000253
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000254 case CMOS_OP_NO_MATCHING_ENUM:
255 fprintf(stderr,
256 "%s: coreboot parameter %s has no matching enums.",
257 prog_name, e->name);
258 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000259
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000260 case CMOS_AREA_OUT_OF_RANGE:
261 fprintf(stderr,
262 "%s: The CMOS area specified by the layout info for "
263 "coreboot parameter %s is out of range.", prog_name,
264 e->name);
265 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000266
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000267 case CMOS_AREA_OVERLAPS_RTC:
268 fprintf(stderr,
269 "%s: The CMOS area specified by the layout info for "
270 "coreboot parameter %s overlaps the realtime clock area.",
271 prog_name, e->name);
272 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000273
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000274 case CMOS_AREA_TOO_WIDE:
275 fprintf(stderr,
276 "%s: The CMOS area specified by the layout info for "
277 "coreboot parameter %s is too wide.", prog_name,
278 e->name);
279 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000280
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000281 default:
282 fprintf(stderr,
283 "%s: Unknown error encountered while attempting to modify "
284 "coreboot parameter %s.", prog_name, e->name);
285 break;
286 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000287
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000288 fprintf(stderr, " No CMOS writes performed.\n");
289 exit(1);
290 return 0; /* keep compiler happy */
291}