input_file.c
1 /* SPDX-License-Identifier: GPL-2.0-only */ 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 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[]); 13 14 /* matches either a blank line or a comment line */ 15 static const char blank_or_comment_regex[] = 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 "#.*$)"; 22 23 /* matches an assignment line */ 24 const char assignment_regex[] = 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:]]*$"; 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 ****************************************************************************/ 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; 59 60 list = NULL; 61 p = &list; 62 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); 65 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; 70 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 } 80 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]; 88 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 } 97 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 */ 102 103 if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL) 104 out_of_memory(); 105 106 item->bit = e->bit; 107 item->length = e->length; 108 item->config = e->config; 109 item->value = try_prepare_cmos_write(e, value); 110 111 /* Append write operation to pending write list. */ 112 item->next = NULL; 113 *p = item; 114 p = &item->next; 115 } 116 117 regfree(&blank_or_comment); 118 regfree(&assignment); 119 return list; 120 } 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 ****************************************************************************/ 129 void do_cmos_writes(cmos_write_t * list) 130 { 131 cmos_write_t *item; 132 133 set_iopl(3); 134 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 } 145 146 cmos_checksum_write(cmos_checksum_compute()); 147 set_iopl(0); 148 } 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 ****************************************************************************/ 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; 162 163 case LINE_EOF: 164 return LINE_EOF; 165 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; 172 173 default: 174 BUG(); 175 } 176 177 exit(1); 178 return 1; /* keep compiler happy */ 179 } 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 ****************************************************************************/ 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; 192 193 switch (prepare_cmos_write(e, value_str, &value)) { 194 case OK: 195 return value; 196 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; 202 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; 209 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; 215 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; 222 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; 229 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; 235 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; 242 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; 249 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; 256 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 } 263 264 fprintf(stderr, " No CMOS writes performed.\n"); 265 exit(1); 266 return 0; /* keep compiler happy */ 267 }