Patrick Georgi | 55189c9 | 2020-05-10 20:09:31 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 2 | |
| 3 | #include "common.h" |
| 4 | #include "input_file.h" |
| 5 | #include "layout.h" |
| 6 | #include "cmos_ops.h" |
| 7 | #include "cmos_lowlevel.h" |
| 8 | #include "reg_expr.h" |
| 9 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 10 | static int get_input_file_line(FILE * f, char line[], int line_buf_size); |
| 11 | static unsigned long long try_prepare_cmos_write(const cmos_entry_t * e, |
| 12 | const char value_str[]); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 13 | |
| 14 | /* matches either a blank line or a comment line */ |
| 15 | static const char blank_or_comment_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 16 | /* a blank line */ |
| 17 | "(^[[:space:]]+$)" "|" /* or ... */ |
| 18 | /* a line consisting of: optional whitespace followed by */ |
| 19 | "(^[[:space:]]*" |
| 20 | /* a '#' character and optionally, additional characters */ |
| 21 | "#.*$)"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 22 | |
| 23 | /* matches an assignment line */ |
| 24 | const char assignment_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 25 | /* optional whitespace */ |
| 26 | "^[[:space:]]*" |
| 27 | /* followed by a coreboot parameter name */ |
| 28 | "([^[:space:]]+)" |
| 29 | /* followed by optional whitespace */ |
| 30 | "[[:space:]]*" |
| 31 | /* followed by an '=' character */ |
| 32 | "=" |
| 33 | /* followed by optional whitespace */ |
| 34 | "[[:space:]]*" |
| 35 | /* followed by a value that may contain embedded whitespace */ |
| 36 | "([^[:space:]]+([[:space:]]+[^[:space:]]+)*)+" |
| 37 | /* followed by optional whitespace */ |
| 38 | "[[:space:]]*$"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 39 | |
| 40 | static int line_num; |
| 41 | |
| 42 | /**************************************************************************** |
| 43 | * process_input_file |
| 44 | * |
| 45 | * Read the contents of file 'f' and return a pointer to a list of pending |
| 46 | * write operations. Perform sanity checking on all write operations and |
| 47 | * exit with an error message if there is a problem. |
| 48 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 49 | cmos_write_t *process_input_file(FILE * f) |
| 50 | { |
| 51 | static const int LINE_BUF_SIZE = 256; |
| 52 | static const size_t N_MATCHES = 4; |
| 53 | char line[LINE_BUF_SIZE]; |
| 54 | const char *name, *value; |
| 55 | cmos_write_t *list, *item, **p; |
| 56 | regex_t blank_or_comment, assignment; |
| 57 | regmatch_t match[N_MATCHES]; |
| 58 | const cmos_entry_t *e; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 59 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 60 | list = NULL; |
| 61 | p = &list; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 62 | |
Patrick Georgi | bf64985 | 2011-01-28 07:40:08 +0000 | [diff] [blame] | 63 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, blank_or_comment_regex, &blank_or_comment); |
| 64 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, assignment_regex, &assignment); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 65 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 66 | /* each iteration processes one line from input file */ |
| 67 | for (line_num = 1; get_input_file_line(f, line, LINE_BUF_SIZE) == OK; line_num++) { /* skip comments and blank lines */ |
| 68 | if (!regexec(&blank_or_comment, line, 0, NULL, 0)) |
| 69 | continue; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 70 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 71 | /* Is this a valid assignment line? If not, then it's a syntax |
| 72 | * error. |
| 73 | */ |
| 74 | if (regexec(&assignment, line, N_MATCHES, match, 0)) { |
| 75 | fprintf(stderr, |
| 76 | "%s: Syntax error on line %d of input file.\n", |
| 77 | prog_name, line_num); |
| 78 | exit(1); |
| 79 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 80 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 81 | /* OK, we found an assignment. Break the line into substrings |
| 82 | * representing the lefthand and righthand sides of the assignment. |
| 83 | */ |
| 84 | line[match[1].rm_eo] = '\0'; |
| 85 | line[match[2].rm_eo] = '\0'; |
| 86 | name = &line[match[1].rm_so]; |
| 87 | value = &line[match[2].rm_so]; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 88 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 89 | /* now look up the coreboot parameter name */ |
| 90 | if (is_checksum_name(name) |
| 91 | || (e = find_cmos_entry(name)) == NULL) { |
| 92 | fprintf(stderr, |
| 93 | "%s: Error on line %d of input file: CMOS parameter " |
| 94 | "%s not found.\n", prog_name, line_num, name); |
| 95 | exit(1); |
| 96 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 97 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 98 | /* At this point, we figure out what numeric value needs to be written |
| 99 | * to which location. At the same time, we perform sanity checking on |
| 100 | * the write operation. |
| 101 | */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 102 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 103 | if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL) |
| 104 | out_of_memory(); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 105 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 106 | item->bit = e->bit; |
| 107 | item->length = e->length; |
| 108 | item->config = e->config; |
| 109 | item->value = try_prepare_cmos_write(e, value); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 110 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 111 | /* Append write operation to pending write list. */ |
| 112 | item->next = NULL; |
| 113 | *p = item; |
| 114 | p = &item->next; |
| 115 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 116 | |
Patrick Georgi | bf64985 | 2011-01-28 07:40:08 +0000 | [diff] [blame] | 117 | regfree(&blank_or_comment); |
| 118 | regfree(&assignment); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 119 | return list; |
| 120 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 121 | |
| 122 | /**************************************************************************** |
| 123 | * do_cmos_writes |
| 124 | * |
| 125 | * 'list' is a linked list of pending CMOS write operations that have passed |
| 126 | * all sanity checks. Perform all write operations, destroying the list as |
| 127 | * we go. |
| 128 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 129 | void do_cmos_writes(cmos_write_t * list) |
| 130 | { |
| 131 | cmos_write_t *item; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 132 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 133 | set_iopl(3); |
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 | while (list != NULL) { |
| 136 | cmos_entry_t e; |
| 137 | item = list; |
| 138 | e.bit = item->bit; |
| 139 | e.length = item->length; |
| 140 | e.config = item->config; |
| 141 | list = item->next; |
| 142 | cmos_write(&e, item->value); |
| 143 | free(item); |
| 144 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 145 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 146 | cmos_checksum_write(cmos_checksum_compute()); |
| 147 | set_iopl(0); |
| 148 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 149 | |
| 150 | /**************************************************************************** |
| 151 | * get_input_file_line |
| 152 | * |
| 153 | * Get a line of input from file 'f'. Store result in 'line' which is an |
| 154 | * array of 'line_buf_size' bytes. Return OK on success or an error code on |
| 155 | * error. |
| 156 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 157 | static int get_input_file_line(FILE * f, char line[], int line_buf_size) |
| 158 | { |
| 159 | switch (get_line_from_file(f, line, line_buf_size)) { |
| 160 | case OK: |
| 161 | return OK; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 162 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 163 | case LINE_EOF: |
| 164 | return LINE_EOF; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 165 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 166 | case LINE_TOO_LONG: |
| 167 | fprintf(stderr, |
| 168 | "%s: Error on line %d of input file: Maximum line " |
| 169 | "length exceeded. Max is %d characters.\n", prog_name, |
| 170 | line_num, line_buf_size - 2); |
| 171 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 172 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 173 | default: |
| 174 | BUG(); |
| 175 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 176 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 177 | exit(1); |
| 178 | return 1; /* keep compiler happy */ |
| 179 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 180 | |
| 181 | /**************************************************************************** |
| 182 | * try_prepare_cmos_write |
| 183 | * |
| 184 | * Attempt to convert 'value_str' to an integer representation for storage in |
| 185 | * CMOS memory. On success, return the converted value. On error, exit with |
| 186 | * an error message. |
| 187 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 188 | static unsigned long long try_prepare_cmos_write(const cmos_entry_t * e, |
| 189 | const char value_str[]) |
| 190 | { |
| 191 | unsigned long long value; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 192 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 193 | switch (prepare_cmos_write(e, value_str, &value)) { |
| 194 | case OK: |
| 195 | return value; |
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 | case CMOS_OP_BAD_ENUM_VALUE: |
| 198 | fprintf(stderr, |
| 199 | "%s: Error on line %d of input file: Bad value for " |
| 200 | "parameter %s.", prog_name, line_num, e->name); |
| 201 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 202 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 203 | case CMOS_OP_NEGATIVE_INT: |
| 204 | fprintf(stderr, |
| 205 | "%s: Error on line %d of input file: This program " |
| 206 | "does not support assignment of negative numbers to " |
| 207 | "coreboot parameters.", prog_name, line_num); |
| 208 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 209 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 210 | case CMOS_OP_INVALID_INT: |
| 211 | fprintf(stderr, |
| 212 | "%s: Error on line %d of input file: %s is not a " |
| 213 | "valid integer.", prog_name, line_num, value_str); |
| 214 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 215 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 216 | case CMOS_OP_RESERVED: |
| 217 | fprintf(stderr, |
| 218 | "%s: Error on line %d of input file: Can not modify " |
| 219 | "reserved coreboot parameter %s.", prog_name, line_num, |
| 220 | e->name); |
| 221 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 222 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 223 | case CMOS_OP_VALUE_TOO_WIDE: |
| 224 | fprintf(stderr, |
| 225 | "%s: Error on line %d of input file: Can not write " |
| 226 | "value %s to CMOS parameter %s that is only %d bits wide.", |
| 227 | prog_name, line_num, value_str, e->name, e->length); |
| 228 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 229 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 230 | case CMOS_OP_NO_MATCHING_ENUM: |
| 231 | fprintf(stderr, |
| 232 | "%s: coreboot parameter %s has no matching enums.", |
| 233 | prog_name, e->name); |
| 234 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 235 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 236 | case CMOS_AREA_OUT_OF_RANGE: |
| 237 | fprintf(stderr, |
| 238 | "%s: The CMOS area specified by the layout info for " |
| 239 | "coreboot parameter %s is out of range.", prog_name, |
| 240 | e->name); |
| 241 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 242 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 243 | case CMOS_AREA_OVERLAPS_RTC: |
| 244 | fprintf(stderr, |
| 245 | "%s: The CMOS area specified by the layout info for " |
| 246 | "coreboot parameter %s overlaps the realtime clock area.", |
| 247 | prog_name, e->name); |
| 248 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 249 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 250 | case CMOS_AREA_TOO_WIDE: |
| 251 | fprintf(stderr, |
| 252 | "%s: The CMOS area specified by the layout info for " |
| 253 | "coreboot parameter %s is too wide.", prog_name, |
| 254 | e->name); |
| 255 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 256 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 257 | default: |
| 258 | fprintf(stderr, |
| 259 | "%s: Unknown error encountered while attempting to modify " |
| 260 | "coreboot parameter %s.", prog_name, e->name); |
| 261 | break; |
| 262 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 263 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 264 | fprintf(stderr, " No CMOS writes performed.\n"); |
| 265 | exit(1); |
| 266 | return 0; /* keep compiler happy */ |
| 267 | } |