| 1 |
|
| 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 |
|
| 15 |
static const char blank_or_comment_regex[] = |
| 16 |
|
| 17 |
"(^[[:space:]]+$)" "|" |
| 18 |
|
| 19 |
"(^[[:space:]]*" |
| 20 |
|
| 21 |
"#.*$)"; |
| 22 |
|
| 23 |
|
| 24 |
const char assignment_regex[] = |
| 25 |
|
| 26 |
"^[[:space:]]*" |
| 27 |
|
| 28 |
"([^[:space:]]+)" |
| 29 |
|
| 30 |
"[[:space:]]*" |
| 31 |
|
| 32 |
"=" |
| 33 |
|
| 34 |
"[[:space:]]*" |
| 35 |
|
| 36 |
"([^[:space:]]+([[:space:]]+[^[:space:]]+)*)+" |
| 37 |
|
| 38 |
"[[:space:]]*$"; |
| 39 |
|
| 40 |
static int line_num; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 67 |
for (line_num = 1; get_input_file_line(f, line, LINE_BUF_SIZE) == OK; line_num++) { |
| 68 |
if (!regexec(&blank_or_comment, line, 0, NULL, 0)) |
| 69 |
continue; |
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 82 |
|
| 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 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 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; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 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; |
| 267 |
} |