Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * input_file.c |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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>. |
| 7 | * UCRL-CODE-2003-012 |
| 8 | * All rights reserved. |
| 9 | * |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 10 | * This file is part of nvramtool, a utility for reading/writing coreboot |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 11 | * parameters and displaying information from the coreboot table. |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 12 | * For details, see http://coreboot.org/nvramtool. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 13 | * |
| 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 25 | \*****************************************************************************/ |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 34 | static int get_input_file_line(FILE * f, char line[], int line_buf_size); |
| 35 | static unsigned long long try_prepare_cmos_write(const cmos_entry_t * e, |
| 36 | const char value_str[]); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 37 | |
| 38 | /* matches either a blank line or a comment line */ |
| 39 | static const char blank_or_comment_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 40 | /* 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 46 | |
| 47 | /* matches an assignment line */ |
| 48 | const char assignment_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 49 | /* 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 63 | |
| 64 | static 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 73 | cmos_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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 83 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 84 | list = NULL; |
| 85 | p = &list; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 86 | |
Patrick Georgi | bf64985 | 2011-01-28 07:40:08 +0000 | [diff] [blame] | 87 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 89 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 90 | /* 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 94 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 95 | /* 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 104 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 105 | /* 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 112 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 113 | /* 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 121 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 122 | /* 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 126 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 127 | if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL) |
| 128 | out_of_memory(); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 129 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 130 | item->bit = e->bit; |
| 131 | item->length = e->length; |
| 132 | item->config = e->config; |
| 133 | item->value = try_prepare_cmos_write(e, value); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 134 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 135 | /* Append write operation to pending write list. */ |
| 136 | item->next = NULL; |
| 137 | *p = item; |
| 138 | p = &item->next; |
| 139 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 140 | |
Patrick Georgi | bf64985 | 2011-01-28 07:40:08 +0000 | [diff] [blame] | 141 | regfree(&blank_or_comment); |
| 142 | regfree(&assignment); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 143 | return list; |
| 144 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 145 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 153 | void do_cmos_writes(cmos_write_t * list) |
| 154 | { |
| 155 | cmos_write_t *item; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 156 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 157 | set_iopl(3); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 158 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 159 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 169 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 170 | cmos_checksum_write(cmos_checksum_compute()); |
| 171 | set_iopl(0); |
| 172 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 173 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 181 | static 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 186 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 187 | case LINE_EOF: |
| 188 | return LINE_EOF; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 189 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 190 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 196 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 197 | default: |
| 198 | BUG(); |
| 199 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 200 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 201 | exit(1); |
| 202 | return 1; /* keep compiler happy */ |
| 203 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 204 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 212 | static unsigned long long try_prepare_cmos_write(const cmos_entry_t * e, |
| 213 | const char value_str[]) |
| 214 | { |
| 215 | unsigned long long value; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 216 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 217 | switch (prepare_cmos_write(e, value_str, &value)) { |
| 218 | case OK: |
| 219 | return value; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 220 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 221 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 226 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 227 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 233 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 234 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 239 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 240 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 246 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 247 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 253 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 254 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 259 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 260 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 266 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 267 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 273 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 274 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 280 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 281 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 287 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 288 | fprintf(stderr, " No CMOS writes performed.\n"); |
| 289 | exit(1); |
| 290 | return 0; /* keep compiler happy */ |
| 291 | } |