Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
Patrick Georgi | 49a7443 | 2011-01-28 07:50:33 +0000 | [diff] [blame] | 2 | * layout-text.c |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 3 | ***************************************************************************** |
Vikram Narayanan | a8111cf | 2012-04-14 15:25:13 +0530 | [diff] [blame] | 4 | * Copyright (C) 2012, Vikram Narayanan |
| 5 | * Unified build_opt_tbl and nvramtool |
| 6 | * build_opt_tbl.c |
| 7 | * Copyright (C) 2003 Eric Biederman (ebiederm@xmission.com) |
| 8 | * Copyright (C) 2007-2010 coresystems GmbH |
| 9 | * |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 10 | * Copyright (C) 2002-2005 The Regents of the University of California. |
| 11 | * Produced at the Lawrence Livermore National Laboratory. |
| 12 | * Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>. |
| 13 | * UCRL-CODE-2003-012 |
| 14 | * All rights reserved. |
| 15 | * |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 16 | * This file is part of nvramtool, a utility for reading/writing coreboot |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 17 | * parameters and displaying information from the coreboot table. |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 18 | * For details, see http://coreboot.org/nvramtool. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 19 | * |
| 20 | * Please also read the file DISCLAIMER which is included in this software |
| 21 | * distribution. |
| 22 | * |
| 23 | * This program is free software; you can redistribute it and/or modify it |
| 24 | * under the terms of the GNU General Public License (as published by the |
| 25 | * Free Software Foundation) version 2, dated June 1991. |
| 26 | * |
| 27 | * This program is distributed in the hope that it will be useful, but |
| 28 | * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF |
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and |
| 30 | * conditions of the GNU General Public License for more details. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 31 | \*****************************************************************************/ |
| 32 | |
| 33 | #include "common.h" |
Patrick Georgi | 49a7443 | 2011-01-28 07:50:33 +0000 | [diff] [blame] | 34 | #include "layout-text.h" |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 35 | #include "layout.h" |
| 36 | #include "cmos_lowlevel.h" |
| 37 | #include "reg_expr.h" |
| 38 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 39 | static void process_layout_file(FILE * f); |
| 40 | static void skip_past_start(FILE * f); |
| 41 | static int process_entry(FILE * f, int skip_add); |
| 42 | static int process_enum(FILE * f, int skip_add); |
| 43 | static void process_checksum_info(FILE * f); |
| 44 | static void skip_remaining_lines(FILE * f); |
| 45 | static void create_entry(cmos_entry_t * cmos_entry, |
| 46 | const char start_bit_str[], const char length_str[], |
| 47 | const char config_str[], const char config_id_str[], |
| 48 | const char name_str[]); |
| 49 | static void try_add_layout_file_entry(const cmos_entry_t * cmos_entry); |
| 50 | static void create_enum(cmos_enum_t * cmos_enum, const char id_str[], |
| 51 | const char value_str[], const char text_str[]); |
| 52 | static void try_add_cmos_enum(const cmos_enum_t * cmos_enum); |
| 53 | static void set_checksum_info(const char start_str[], const char end_str[], |
| 54 | const char index_str[]); |
| 55 | static char cmos_entry_char_value(cmos_entry_config_t config); |
| 56 | static int get_layout_file_line(FILE * f, char line[], int line_buf_size); |
| 57 | static unsigned string_to_unsigned(const char str[], const char str_name[]); |
| 58 | static unsigned long string_to_unsigned_long(const char str[], |
| 59 | const char str_name[]); |
| 60 | static unsigned long do_string_to_unsigned_long(const char str[], |
| 61 | const char str_name[], |
| 62 | const char blurb[]); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 63 | |
| 64 | /* matches either a blank line or a comment line */ |
| 65 | static const char blank_or_comment_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 66 | /* a blank line */ |
| 67 | "(^[[:space:]]+$)" "|" /* or ... */ |
| 68 | /* a line consisting of: optional whitespace followed by */ |
| 69 | "(^[[:space:]]*" |
| 70 | /* a '#' character and optionally, additional characters */ |
| 71 | "#.*$)"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 72 | |
| 73 | static regex_t blank_or_comment_expr; |
| 74 | |
| 75 | /* matches the line in a CMOS layout file indicating the start of the |
| 76 | * "entries" section. |
| 77 | */ |
| 78 | static const char start_entries_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 79 | /* optional whitespace */ |
| 80 | "^[[:space:]]*" |
| 81 | /* followed by "entries" */ |
| 82 | "entries" |
| 83 | /* followed by optional whitespace */ |
| 84 | "[[:space:]]*$"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 85 | |
| 86 | static regex_t start_entries_expr; |
| 87 | |
| 88 | /* matches the line in a CMOS layout file indicating the start of the |
| 89 | * "enumerations" section |
| 90 | */ |
| 91 | static const char start_enums_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 92 | /* optional whitespace */ |
| 93 | "^[[:space:]]*" |
| 94 | /* followed by "enumerations" */ |
| 95 | "enumerations" |
| 96 | /* followed by optional whitespace */ |
| 97 | "[[:space:]]*$"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 98 | |
| 99 | static regex_t start_enums_expr; |
| 100 | |
| 101 | /* matches the line in a CMOS layout file indicating the start of the |
| 102 | * "checksums" section |
| 103 | */ |
| 104 | static const char start_checksums_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 105 | /* optional whitespace */ |
| 106 | "^[[:space:]]*" |
| 107 | /* followed by "checksums" */ |
| 108 | "checksums" |
| 109 | /* followed by optional whitespace */ |
| 110 | "[[:space:]]*$"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 111 | |
| 112 | static regex_t start_checksums_expr; |
| 113 | |
| 114 | /* matches a line in a CMOS layout file specifying a CMOS entry */ |
| 115 | static const char entries_line_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 116 | /* optional whitespace */ |
| 117 | "^[[:space:]]*" |
| 118 | /* followed by a chunk of nonwhitespace for start-bit field */ |
| 119 | "([^[:space:]]+)" |
| 120 | /* followed by one or more whitespace characters */ |
| 121 | "[[:space:]]+" |
| 122 | /* followed by a chunk of nonwhitespace for length field */ |
| 123 | "([^[:space:]]+)" |
| 124 | /* followed by one or more whitespace characters */ |
| 125 | "[[:space:]]+" |
| 126 | /* followed by a chunk of nonwhitespace for config field */ |
| 127 | "([^[:space:]]+)" |
| 128 | /* followed by one or more whitespace characters */ |
| 129 | "[[:space:]]+" |
| 130 | /* followed by a chunk of nonwhitespace for config-ID field */ |
| 131 | "([^[:space:]]+)" |
| 132 | /* followed by one or more whitespace characters */ |
| 133 | "[[:space:]]+" |
| 134 | /* followed by a chunk of nonwhitespace for name field */ |
| 135 | "([^[:space:]]+)" |
| 136 | /* followed by optional whitespace */ |
| 137 | "[[:space:]]*$"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 138 | |
| 139 | static regex_t entries_line_expr; |
| 140 | |
| 141 | /* matches a line in a CMOS layout file specifying a CMOS enumeration */ |
| 142 | static const char enums_line_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 143 | /* optional whitespace */ |
| 144 | "^[[:space:]]*" |
| 145 | /* followed by a chunk of nonwhitespace for ID field */ |
| 146 | "([^[:space:]]+)" |
| 147 | /* followed by one or more whitespace characters */ |
| 148 | "[[:space:]]+" |
| 149 | /* followed by a chunk of nonwhitespace for value field */ |
| 150 | "([^[:space:]]+)" |
| 151 | /* followed by one or more whitespace characters */ |
| 152 | "[[:space:]]+" |
| 153 | /* followed by a chunk of nonwhitespace for text field */ |
Patrick Georgi | c3fc4b9 | 2012-04-14 16:21:39 +0200 | [diff] [blame] | 154 | "([[:print:]]*[^[:space:]])" |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 155 | /* followed by optional whitespace */ |
| 156 | "[[:space:]]*$"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 157 | |
| 158 | static regex_t enums_line_expr; |
| 159 | |
| 160 | /* matches the line in a CMOS layout file specifying CMOS checksum |
| 161 | * information |
| 162 | */ |
| 163 | static const char checksum_line_regex[] = |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 164 | /* optional whitespace */ |
| 165 | "^[[:space:]]*" |
| 166 | /* followed by "checksum" */ |
| 167 | "checksum" |
| 168 | /* followed by one or more whitespace characters */ |
| 169 | "[[:space:]]+" |
| 170 | /* followed by a chunk of nonwhitespace for first bit of summed area */ |
| 171 | "([^[:space:]]+)" |
| 172 | /* followed by one or more whitespace characters */ |
| 173 | "[[:space:]]+" |
| 174 | /* followed by a chunk of nonwhitespace for last bit of summed area */ |
| 175 | "([^[:space:]]+)" |
| 176 | /* followed by one or more whitespace characters */ |
| 177 | "[[:space:]]+" |
| 178 | /* followed by a chunk of nonwhitespace for checksum location bit */ |
| 179 | "([^[:space:]]+)" |
| 180 | /* followed by optional whitespace */ |
| 181 | "[[:space:]]*$"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 182 | |
| 183 | static regex_t checksum_line_expr; |
| 184 | |
| 185 | static const int LINE_BUF_SIZE = 256; |
| 186 | |
| 187 | static int line_num; |
| 188 | |
| 189 | static const char *layout_filename = NULL; |
| 190 | |
| 191 | /**************************************************************************** |
| 192 | * set_layout_filename |
| 193 | * |
| 194 | * Set the name of the file we will obtain CMOS layout information from. |
| 195 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 196 | void set_layout_filename(const char filename[]) |
| 197 | { |
| 198 | layout_filename = filename; |
| 199 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 200 | |
| 201 | /**************************************************************************** |
| 202 | * get_layout_from_file |
| 203 | * |
| 204 | * Read CMOS layout information from the user-specified CMOS layout file. |
| 205 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 206 | void get_layout_from_file(void) |
| 207 | { |
| 208 | FILE *f; |
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 | assert(layout_filename != NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 211 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 212 | if ((f = fopen(layout_filename, "r")) == NULL) { |
| 213 | fprintf(stderr, |
| 214 | "%s: Can not open CMOS layout file %s for reading: " |
| 215 | "%s\n", prog_name, layout_filename, strerror(errno)); |
| 216 | exit(1); |
| 217 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 218 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 219 | process_layout_file(f); |
| 220 | fclose(f); |
| 221 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 222 | |
Vikram Narayanan | a8111cf | 2012-04-14 15:25:13 +0530 | [diff] [blame] | 223 | void write_cmos_layout_header(const char *header_filename) |
| 224 | { |
| 225 | FILE *fp; |
| 226 | const cmos_entry_t *cmos_entry; |
| 227 | cmos_checksum_layout_t layout; |
| 228 | |
| 229 | if ((fp = fopen(header_filename, "w+")) == NULL) { |
| 230 | fprintf(stderr, |
| 231 | "%s: Can't open file %s for writing: %s\n", |
| 232 | prog_name, header_filename, strerror(errno)); |
| 233 | exit(1); |
| 234 | } |
| 235 | |
| 236 | fprintf(fp, "/**\n * This is an autogenerated file. Do not EDIT.\n" |
| 237 | " * All changes made to this file will be lost.\n" |
| 238 | " * See mainboard's cmos.layout file.\n */\n" |
| 239 | "\n#ifndef __OPTION_TABLE_H\n" |
| 240 | "#define __OPTION_TABLE_H\n\n"); |
| 241 | |
| 242 | for (cmos_entry = first_cmos_entry(); cmos_entry != NULL; |
| 243 | cmos_entry = next_cmos_entry(cmos_entry)) { |
| 244 | |
| 245 | if (!is_ident((char *)cmos_entry->name)) { |
| 246 | fprintf(stderr, |
| 247 | "Error - Name %s is an invalid identifier\n", |
| 248 | cmos_entry->name); |
| 249 | fclose(fp); |
| 250 | exit(1); |
| 251 | } |
| 252 | |
| 253 | fprintf(fp, "#define CMOS_VSTART_%s\t%d\n", |
| 254 | cmos_entry->name, cmos_entry->bit); |
| 255 | fprintf(fp, "#define CMOS_VLEN_%s\t%d\n", |
| 256 | cmos_entry->name, cmos_entry->length); |
| 257 | } |
| 258 | |
| 259 | layout.summed_area_start = cmos_checksum_start; |
| 260 | layout.summed_area_end = cmos_checksum_end; |
| 261 | layout.checksum_at = cmos_checksum_index; |
| 262 | checksum_layout_to_bits(&layout); |
| 263 | |
| 264 | fprintf(fp, "\n#define LB_CKS_RANGE_START %d\n", |
| 265 | layout.summed_area_start / 8); |
| 266 | fprintf(fp, "#define LB_CKS_RANGE_END %d\n", |
| 267 | layout.summed_area_end / 8); |
| 268 | fprintf(fp, "#define LB_CKS_LOC %d\n", |
| 269 | layout.checksum_at / 8); |
| 270 | fprintf(fp, "\n#endif /* __OPTION_TABLE_H */\n"); |
| 271 | |
| 272 | fclose(fp); |
| 273 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 274 | /**************************************************************************** |
| 275 | * write_cmos_layout |
| 276 | * |
| 277 | * Write CMOS layout information to file 'f'. The output is written in the |
| 278 | * format that CMOS layout files adhere to. |
| 279 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 280 | void write_cmos_layout(FILE * f) |
| 281 | { |
| 282 | const cmos_entry_t *cmos_entry; |
| 283 | const cmos_enum_t *cmos_enum; |
| 284 | cmos_checksum_layout_t layout; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 285 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 286 | fprintf(f, "entries\n"); |
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 | for (cmos_entry = first_cmos_entry(); |
| 289 | cmos_entry != NULL; cmos_entry = next_cmos_entry(cmos_entry)) |
| 290 | fprintf(f, "%u %u %c %u %s\n", cmos_entry->bit, |
| 291 | cmos_entry->length, |
| 292 | cmos_entry_char_value(cmos_entry->config), |
| 293 | cmos_entry->config_id, cmos_entry->name); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 294 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 295 | fprintf(f, "\nenumerations\n"); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 296 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 297 | for (cmos_enum = first_cmos_enum(); |
| 298 | cmos_enum != NULL; cmos_enum = next_cmos_enum(cmos_enum)) |
| 299 | fprintf(f, "%u %llu %s\n", cmos_enum->config_id, |
| 300 | cmos_enum->value, cmos_enum->text); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 301 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 302 | layout.summed_area_start = cmos_checksum_start; |
| 303 | layout.summed_area_end = cmos_checksum_end; |
| 304 | layout.checksum_at = cmos_checksum_index; |
| 305 | checksum_layout_to_bits(&layout); |
| 306 | fprintf(f, "\nchecksums\nchecksum %u %u %u\n", layout.summed_area_start, |
| 307 | layout.summed_area_end, layout.checksum_at); |
| 308 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 309 | |
| 310 | /**************************************************************************** |
| 311 | * process_layout_file |
| 312 | * |
| 313 | * Read CMOS layout information from file 'f' and add it to our internal |
| 314 | * repository. |
| 315 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 316 | static void process_layout_file(FILE * f) |
| 317 | { |
Patrick Georgi | bf64985 | 2011-01-28 07:40:08 +0000 | [diff] [blame] | 318 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, blank_or_comment_regex, &blank_or_comment_expr); |
| 319 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, start_entries_regex, &start_entries_expr); |
| 320 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, entries_line_regex, &entries_line_expr); |
| 321 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, start_enums_regex, &start_enums_expr); |
| 322 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, enums_line_regex, &enums_line_expr); |
| 323 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, start_checksums_regex, &start_checksums_expr); |
| 324 | compile_reg_expr(REG_EXTENDED | REG_NEWLINE, checksum_line_regex, &checksum_line_expr); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 325 | line_num = 1; |
| 326 | skip_past_start(f); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 327 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 328 | /* Skip past all entries. We will process these later when we |
| 329 | * make a second pass through the file. |
| 330 | */ |
| 331 | while (!process_entry(f, 1)) ; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 332 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 333 | /* Process all enums, adding them to our internal repository as |
| 334 | * we go. */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 335 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 336 | if (process_enum(f, 0)) { |
| 337 | fprintf(stderr, "%s: Error: CMOS layout file contains no " |
| 338 | "enumerations.\n", prog_name); |
| 339 | exit(1); |
| 340 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 341 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 342 | while (!process_enum(f, 0)) ; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 343 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 344 | /* Go back to start of file. */ |
| 345 | line_num = 1; |
| 346 | fseek(f, 0, SEEK_SET); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 347 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 348 | skip_past_start(f); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 349 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 350 | /* Process all entries, adding them to the repository as we go. |
| 351 | * We must add the entries after the enums, even though they |
| 352 | * appear in the layout file before the enums. This is because |
| 353 | * the entries are sanity checked against the enums as they are |
| 354 | * added. |
| 355 | */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 356 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 357 | if (process_entry(f, 0)) { |
| 358 | fprintf(stderr, |
| 359 | "%s: Error: CMOS layout file contains no entries.\n", |
| 360 | prog_name); |
| 361 | exit(1); |
| 362 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 363 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 364 | while (!process_entry(f, 0)) ; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 365 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 366 | /* Skip past all enums. They have already been processed. */ |
| 367 | while (!process_enum(f, 1)) ; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 368 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 369 | /* Process CMOS checksum info. */ |
| 370 | process_checksum_info(f); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 371 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 372 | /* See if there are any lines left to process. If so, verify |
| 373 | * that they are all either blank lines or comments. |
| 374 | */ |
| 375 | skip_remaining_lines(f); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 376 | |
Patrick Georgi | bf64985 | 2011-01-28 07:40:08 +0000 | [diff] [blame] | 377 | regfree(&blank_or_comment_expr); |
| 378 | regfree(&start_entries_expr); |
| 379 | regfree(&entries_line_expr); |
| 380 | regfree(&start_enums_expr); |
| 381 | regfree(&enums_line_expr); |
| 382 | regfree(&start_checksums_expr); |
| 383 | regfree(&checksum_line_expr); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 384 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 385 | |
| 386 | /**************************************************************************** |
| 387 | * skip_past_start |
| 388 | * |
| 389 | * Skip past the line that marks the start of the "entries" section. |
| 390 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 391 | static void skip_past_start(FILE * f) |
| 392 | { |
| 393 | char line[LINE_BUF_SIZE]; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 394 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 395 | for (;; line_num++) { |
| 396 | if (get_layout_file_line(f, line, LINE_BUF_SIZE)) { |
| 397 | fprintf(stderr, |
| 398 | "%s: \"entries\" line not found in CMOS layout file.\n", |
| 399 | prog_name); |
| 400 | exit(1); |
| 401 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 402 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 403 | if (!regexec(&blank_or_comment_expr, line, 0, NULL, 0)) |
| 404 | continue; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 405 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 406 | if (!regexec(&start_entries_expr, line, 0, NULL, 0)) |
| 407 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 408 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 409 | fprintf(stderr, |
| 410 | "%s: Syntax error on line %d of CMOS layout file. " |
| 411 | "\"entries\" line expected.\n", prog_name, line_num); |
| 412 | exit(1); |
| 413 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 414 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 415 | line_num++; |
| 416 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 417 | |
| 418 | /**************************************************************************** |
| 419 | * process_entry |
| 420 | * |
| 421 | * Get an entry from "entries" section of file and add it to our repository |
| 422 | * of layout information. Return 0 if an entry was found and processed. |
| 423 | * Return 1 if there are no more entries. |
| 424 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 425 | static int process_entry(FILE * f, int skip_add) |
| 426 | { |
| 427 | static const size_t N_MATCHES = 6; |
| 428 | char line[LINE_BUF_SIZE]; |
| 429 | regmatch_t match[N_MATCHES]; |
| 430 | cmos_entry_t cmos_entry; |
| 431 | int result; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 432 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 433 | result = 1; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 434 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 435 | for (;; line_num++) { |
| 436 | if (get_layout_file_line(f, line, LINE_BUF_SIZE)) { |
| 437 | fprintf(stderr, |
| 438 | "%s: Unexpected end of CMOS layout file reached while " |
| 439 | "reading \"entries\" section.\n", prog_name); |
| 440 | exit(1); |
| 441 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 442 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 443 | if (!regexec(&blank_or_comment_expr, line, 0, NULL, 0)) |
| 444 | continue; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 445 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 446 | if (regexec(&entries_line_expr, line, N_MATCHES, match, 0)) { |
| 447 | if (regexec(&start_enums_expr, line, 0, NULL, 0)) { |
| 448 | fprintf(stderr, |
| 449 | "%s: Syntax error on line %d of CMOS layout " |
| 450 | "file.\n", prog_name, line_num); |
| 451 | exit(1); |
| 452 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 453 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 454 | break; /* start of enumerations reached: no more entries */ |
| 455 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 456 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 457 | result = 0; /* next layout entry found */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 458 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 459 | if (skip_add) |
| 460 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 461 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 462 | line[match[1].rm_eo] = '\0'; |
| 463 | line[match[2].rm_eo] = '\0'; |
| 464 | line[match[3].rm_eo] = '\0'; |
| 465 | line[match[4].rm_eo] = '\0'; |
| 466 | line[match[5].rm_eo] = '\0'; |
| 467 | create_entry(&cmos_entry, &line[match[1].rm_so], |
| 468 | &line[match[2].rm_so], &line[match[3].rm_so], |
| 469 | &line[match[4].rm_so], &line[match[5].rm_so]); |
| 470 | try_add_layout_file_entry(&cmos_entry); |
| 471 | break; |
| 472 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 473 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 474 | line_num++; |
| 475 | return result; |
| 476 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 477 | |
| 478 | /**************************************************************************** |
| 479 | * process_enum |
| 480 | * |
| 481 | * Get an enuneration from "enumerations" section of file and add it to our |
| 482 | * repository of layout information. Return 0 if an enumeration was found |
| 483 | * and processed. Return 1 if there are no more enumerations. |
| 484 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 485 | static int process_enum(FILE * f, int skip_add) |
| 486 | { |
| 487 | static const size_t N_MATCHES = 4; |
| 488 | char line[LINE_BUF_SIZE]; |
| 489 | regmatch_t match[N_MATCHES]; |
| 490 | cmos_enum_t cmos_enum; |
| 491 | int result; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 492 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 493 | result = 1; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 494 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 495 | for (;; line_num++) { |
| 496 | if (get_layout_file_line(f, line, LINE_BUF_SIZE)) { |
| 497 | fprintf(stderr, |
| 498 | "%s: Unexpected end of CMOS layout file reached while " |
| 499 | "reading \"enumerations\" section.\n", |
| 500 | prog_name); |
| 501 | exit(1); |
| 502 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 503 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 504 | if (!regexec(&blank_or_comment_expr, line, 0, NULL, 0)) |
| 505 | continue; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 506 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 507 | if (regexec(&enums_line_expr, line, N_MATCHES, match, 0)) { |
| 508 | if (regexec(&start_checksums_expr, line, 0, NULL, 0)) { |
| 509 | fprintf(stderr, |
| 510 | "%s: Syntax error on line %d of CMOS layout " |
| 511 | "file.\n", prog_name, line_num); |
| 512 | exit(1); |
| 513 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 514 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 515 | break; /* start of checksums reached: no more enumerations */ |
| 516 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 517 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 518 | result = 0; /* next layout enumeration found */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 519 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 520 | if (skip_add) |
| 521 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 522 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 523 | line[match[1].rm_eo] = '\0'; |
| 524 | line[match[2].rm_eo] = '\0'; |
| 525 | line[match[3].rm_eo] = '\0'; |
| 526 | create_enum(&cmos_enum, &line[match[1].rm_so], |
| 527 | &line[match[2].rm_so], &line[match[3].rm_so]); |
| 528 | try_add_cmos_enum(&cmos_enum); |
| 529 | break; |
| 530 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 531 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 532 | line_num++; |
| 533 | return result; |
| 534 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 535 | |
| 536 | /**************************************************************************** |
| 537 | * process_checksum_info |
| 538 | * |
| 539 | * Get line conatining CMOS checksum information. |
| 540 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 541 | static void process_checksum_info(FILE * f) |
| 542 | { |
| 543 | static const size_t N_MATCHES = 4; |
| 544 | char line[LINE_BUF_SIZE]; |
| 545 | regmatch_t match[N_MATCHES]; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 546 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 547 | for (;; line_num++) { |
| 548 | if (get_layout_file_line(f, line, LINE_BUF_SIZE)) { |
| 549 | fprintf(stderr, |
| 550 | "%s: Unexpected end of CMOS layout file reached while " |
| 551 | "reading \"checksums\" section.\n", prog_name); |
| 552 | exit(1); |
| 553 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 554 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 555 | if (!regexec(&blank_or_comment_expr, line, 0, NULL, 0)) |
| 556 | continue; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 557 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 558 | if (regexec(&checksum_line_expr, line, N_MATCHES, match, 0)) { |
| 559 | fprintf(stderr, |
| 560 | "%s: Syntax error on line %d of CMOS layout " |
| 561 | "file. \"checksum\" line expected.\n", |
| 562 | prog_name, line_num); |
| 563 | exit(1); |
| 564 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 565 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 566 | /* checksum line found */ |
| 567 | line[match[1].rm_eo] = '\0'; |
| 568 | line[match[2].rm_eo] = '\0'; |
| 569 | line[match[3].rm_eo] = '\0'; |
| 570 | set_checksum_info(&line[match[1].rm_so], &line[match[2].rm_so], |
| 571 | &line[match[3].rm_so]); |
| 572 | break; |
| 573 | } |
| 574 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 575 | |
| 576 | /**************************************************************************** |
| 577 | * skip_remaining_lines |
| 578 | * |
| 579 | * Get any remaining lines of unprocessed input. Complain if we find a line |
| 580 | * that contains anything other than comments and whitespace. |
| 581 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 582 | static void skip_remaining_lines(FILE * f) |
| 583 | { |
| 584 | char line[LINE_BUF_SIZE]; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 585 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 586 | for (line_num++; |
| 587 | get_layout_file_line(f, line, LINE_BUF_SIZE) == OK; line_num++) { |
| 588 | if (regexec(&blank_or_comment_expr, line, 0, NULL, 0)) { |
| 589 | fprintf(stderr, |
| 590 | "%s: Syntax error on line %d of CMOS layout file: " |
| 591 | "Only comments and/or whitespace allowed after " |
| 592 | "\"checksum\" line.\n", prog_name, line_num); |
| 593 | exit(1); |
| 594 | } |
| 595 | } |
| 596 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 597 | |
| 598 | /**************************************************************************** |
| 599 | * create_entry |
| 600 | * |
| 601 | * Create a CMOS entry structure representing the given information. Perform |
| 602 | * sanity checking on input parameters. |
| 603 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 604 | static void create_entry(cmos_entry_t * cmos_entry, |
| 605 | const char start_bit_str[], const char length_str[], |
| 606 | const char config_str[], const char config_id_str[], |
| 607 | const char name_str[]) |
| 608 | { |
| 609 | cmos_entry->bit = string_to_unsigned(start_bit_str, "start-bit"); |
| 610 | cmos_entry->length = string_to_unsigned(length_str, "length"); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 611 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 612 | if (config_str[1] != '\0') |
| 613 | goto bad_config_str; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 614 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 615 | switch (config_str[0]) { |
| 616 | case 'e': |
| 617 | cmos_entry->config = CMOS_ENTRY_ENUM; |
| 618 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 619 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 620 | case 'h': |
| 621 | cmos_entry->config = CMOS_ENTRY_HEX; |
| 622 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 623 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 624 | case 's': |
| 625 | cmos_entry->config = CMOS_ENTRY_STRING; |
| 626 | break; |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 627 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 628 | case 'r': |
| 629 | cmos_entry->config = CMOS_ENTRY_RESERVED; |
| 630 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 631 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 632 | default: |
| 633 | goto bad_config_str; |
| 634 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 635 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 636 | cmos_entry->config_id = string_to_unsigned(config_id_str, "config-ID"); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 637 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 638 | if (strlen(name_str) >= CMOS_MAX_NAME_LENGTH) { |
| 639 | fprintf(stderr, |
| 640 | "%s: Error on line %d of CMOS layout file: name too " |
| 641 | "long (max length is %d).\n", prog_name, line_num, |
| 642 | CMOS_MAX_NAME_LENGTH - 1); |
| 643 | exit(1); |
| 644 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 645 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 646 | strcpy(cmos_entry->name, name_str); |
| 647 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 648 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 649 | bad_config_str: |
| 650 | fprintf(stderr, |
| 651 | "%s: Error on line %d of CMOS layout file: 'e', 'h', or " |
| 652 | "'r' expected for config value.\n", prog_name, line_num); |
| 653 | exit(1); |
| 654 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 655 | |
| 656 | /**************************************************************************** |
| 657 | * try_add_layout_file_entry |
| 658 | * |
| 659 | * Attempt to add the given CMOS entry to our internal repository. Exit with |
| 660 | * an error message on failure. |
| 661 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 662 | static void try_add_layout_file_entry(const cmos_entry_t * cmos_entry) |
| 663 | { |
| 664 | const cmos_entry_t *conflict; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 665 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 666 | switch (add_cmos_entry(cmos_entry, &conflict)) { |
| 667 | case OK: |
| 668 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 669 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 670 | case CMOS_AREA_OUT_OF_RANGE: |
| 671 | fprintf(stderr, |
| 672 | "%s: Error on line %d of CMOS layout file. Area " |
| 673 | "covered by entry %s is out of range.\n", prog_name, |
| 674 | line_num, cmos_entry->name); |
| 675 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 676 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 677 | case CMOS_AREA_TOO_WIDE: |
| 678 | fprintf(stderr, |
| 679 | "%s: Error on line %d of CMOS layout file. Area " |
| 680 | "covered by entry %s is too wide.\n", prog_name, |
| 681 | line_num, cmos_entry->name); |
| 682 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 683 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 684 | case LAYOUT_ENTRY_OVERLAP: |
| 685 | fprintf(stderr, |
| 686 | "%s: Error on line %d of CMOS layout file. Layouts " |
| 687 | "overlap for entries %s and %s.\n", prog_name, line_num, |
| 688 | cmos_entry->name, conflict->name); |
| 689 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 690 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 691 | case LAYOUT_ENTRY_BAD_LENGTH: |
| 692 | /* Silently ignore entries with zero length. Although this should |
| 693 | * never happen in practice, we should handle the case in a |
| 694 | * reasonable manner just to be safe. |
| 695 | */ |
| 696 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 697 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 698 | default: |
| 699 | BUG(); |
| 700 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 701 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 702 | exit(1); |
| 703 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 704 | |
| 705 | /**************************************************************************** |
| 706 | * create_enum |
| 707 | * |
| 708 | * Create a CMOS enumeration structure representing the given information. |
| 709 | * Perform sanity checking on input parameters. |
| 710 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 711 | static void create_enum(cmos_enum_t * cmos_enum, const char id_str[], |
| 712 | const char value_str[], const char text_str[]) |
| 713 | { |
| 714 | cmos_enum->config_id = string_to_unsigned(id_str, "ID"); |
| 715 | cmos_enum->value = string_to_unsigned_long(value_str, "value"); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 716 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 717 | if (strlen(text_str) >= CMOS_MAX_TEXT_LENGTH) { |
| 718 | fprintf(stderr, |
| 719 | "%s: Error on line %d of CMOS layout file: text too " |
| 720 | "long (max length is %d).\n", prog_name, line_num, |
| 721 | CMOS_MAX_TEXT_LENGTH - 1); |
| 722 | exit(1); |
| 723 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 724 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 725 | strcpy(cmos_enum->text, text_str); |
| 726 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 727 | |
| 728 | /**************************************************************************** |
| 729 | * try_add_cmos_enum |
| 730 | * |
| 731 | * Attempt to add the given CMOS enum to our internal repository. Exit with |
| 732 | * an error message on failure. |
| 733 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 734 | static void try_add_cmos_enum(const cmos_enum_t * cmos_enum) |
| 735 | { |
| 736 | switch (add_cmos_enum(cmos_enum)) { |
| 737 | case OK: |
| 738 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 739 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 740 | case LAYOUT_DUPLICATE_ENUM: |
| 741 | fprintf(stderr, "%s: Error on line %d of CMOS layout file: " |
| 742 | "Enumeration found with duplicate ID/value combination.\n", |
| 743 | prog_name, line_num); |
| 744 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 745 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 746 | default: |
| 747 | BUG(); |
| 748 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 749 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 750 | exit(1); |
| 751 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 752 | |
| 753 | /**************************************************************************** |
| 754 | * set_checksum_info |
| 755 | * |
| 756 | * Set CMOS checksum information according to input parameters and perform |
| 757 | * sanity checking on input parameters. |
| 758 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 759 | static void set_checksum_info(const char start_str[], const char end_str[], |
| 760 | const char index_str[]) |
| 761 | { |
| 762 | cmos_checksum_layout_t layout; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 763 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 764 | /* These are bit positions that we want to convert to byte positions. */ |
| 765 | layout.summed_area_start = |
| 766 | string_to_unsigned(start_str, "CMOS checksummed area start"); |
| 767 | layout.summed_area_end = |
| 768 | string_to_unsigned(end_str, "CMOS checksummed area end"); |
| 769 | layout.checksum_at = |
| 770 | string_to_unsigned(index_str, "CMOS checksum location"); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 771 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 772 | switch (checksum_layout_to_bytes(&layout)) { |
| 773 | case OK: |
| 774 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 775 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 776 | case LAYOUT_SUMMED_AREA_START_NOT_ALIGNED: |
| 777 | fprintf(stderr, |
| 778 | "%s: Error on line %d of CMOS layout file. CMOS " |
| 779 | "checksummed area start is not byte-aligned.\n", |
| 780 | prog_name, line_num); |
| 781 | goto fail; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 782 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 783 | case LAYOUT_SUMMED_AREA_END_NOT_ALIGNED: |
| 784 | fprintf(stderr, |
| 785 | "%s: Error on line %d of CMOS layout file. CMOS " |
| 786 | "checksummed area end is not byte-aligned.\n", |
| 787 | prog_name, line_num); |
| 788 | goto fail; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 789 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 790 | case LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED: |
| 791 | fprintf(stderr, |
| 792 | "%s: Error on line %d of CMOS layout file. CMOS " |
| 793 | "checksum location is not byte-aligned.\n", prog_name, |
| 794 | line_num); |
| 795 | goto fail; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 796 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 797 | case LAYOUT_INVALID_SUMMED_AREA: |
| 798 | fprintf(stderr, |
| 799 | "%s: Error on line %d of CMOS layout file. CMOS " |
| 800 | "checksummed area end must be greater than CMOS checksummed " |
| 801 | "area start.\n", prog_name, line_num); |
| 802 | goto fail; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 803 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 804 | case LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA: |
| 805 | fprintf(stderr, |
| 806 | "%s: Error on line %d of CMOS layout file. CMOS " |
| 807 | "checksum overlaps checksummed area.\n", prog_name, |
| 808 | line_num); |
| 809 | goto fail; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 810 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 811 | case LAYOUT_SUMMED_AREA_OUT_OF_RANGE: |
| 812 | fprintf(stderr, |
| 813 | "%s: Error on line %d of CMOS layout file. CMOS " |
| 814 | "checksummed area out of range.\n", prog_name, |
| 815 | line_num); |
| 816 | goto fail; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 817 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 818 | case LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE: |
| 819 | fprintf(stderr, |
| 820 | "%s: Error on line %d of CMOS layout file. CMOS " |
| 821 | "checksum location out of range.\n", prog_name, |
| 822 | line_num); |
| 823 | goto fail; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 824 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 825 | default: |
| 826 | BUG(); |
| 827 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 828 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 829 | cmos_checksum_start = layout.summed_area_start; |
| 830 | cmos_checksum_end = layout.summed_area_end; |
| 831 | cmos_checksum_index = layout.checksum_at; |
| 832 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 833 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 834 | fail: |
| 835 | exit(1); |
| 836 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 837 | |
| 838 | /**************************************************************************** |
| 839 | * cmos_entry_char_value |
| 840 | * |
| 841 | * Return the character representation of 'config'. |
| 842 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 843 | static char cmos_entry_char_value(cmos_entry_config_t config) |
| 844 | { |
| 845 | switch (config) { |
| 846 | case CMOS_ENTRY_ENUM: |
| 847 | return 'e'; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 848 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 849 | case CMOS_ENTRY_HEX: |
| 850 | return 'h'; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 851 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 852 | case CMOS_ENTRY_RESERVED: |
| 853 | return 'r'; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 854 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 855 | case CMOS_ENTRY_STRING: |
| 856 | return 's'; |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 857 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 858 | default: |
| 859 | BUG(); |
| 860 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 861 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 862 | return 0; /* not reached */ |
| 863 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 864 | |
| 865 | /**************************************************************************** |
| 866 | * get_layout_file_line |
| 867 | * |
| 868 | * Get a line of input from file 'f'. Store result in 'line' which is an |
| 869 | * array of 'line_buf_size' bytes. Return OK on success or an error code on |
| 870 | * failure. |
| 871 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 872 | static int get_layout_file_line(FILE * f, char line[], int line_buf_size) |
| 873 | { |
| 874 | switch (get_line_from_file(f, line, line_buf_size)) { |
| 875 | case OK: |
| 876 | return OK; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 877 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 878 | case LINE_EOF: |
| 879 | return LINE_EOF; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 880 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 881 | case LINE_TOO_LONG: |
| 882 | fprintf(stderr, |
| 883 | "%s: Error on line %d of CMOS layout file: Maximum " |
| 884 | "line length exceeded. Max is %d characters.\n", |
| 885 | prog_name, line_num, line_buf_size - 2); |
| 886 | break; |
| 887 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 888 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 889 | exit(1); |
| 890 | return 1; /* keep compiler happy */ |
| 891 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 892 | |
| 893 | /**************************************************************************** |
| 894 | * string_to_unsigned |
| 895 | * |
| 896 | * Convert the string 'str' to an unsigned and return the result. |
| 897 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 898 | static unsigned string_to_unsigned(const char str[], const char str_name[]) |
| 899 | { |
| 900 | unsigned long n; |
| 901 | unsigned z; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 902 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 903 | n = do_string_to_unsigned_long(str, str_name, ""); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 904 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 905 | if ((z = (unsigned)n) != n) { |
| 906 | /* This could happen on an architecture in which |
| 907 | * sizeof(unsigned) < sizeof(unsigned long). |
| 908 | */ |
| 909 | fprintf(stderr, |
| 910 | "%s: Error on line %d of CMOS layout file: %s value is " |
| 911 | "out of range.\n", prog_name, line_num, str_name); |
| 912 | exit(1); |
| 913 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 914 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 915 | return z; |
| 916 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 917 | |
| 918 | /**************************************************************************** |
| 919 | * string_to_unsigned_long |
| 920 | * |
| 921 | * Convert the string 'str' to an unsigned long and return the result. |
| 922 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 923 | static unsigned long string_to_unsigned_long(const char str[], |
| 924 | const char str_name[]) |
| 925 | { |
| 926 | return do_string_to_unsigned_long(str, str_name, " long"); |
| 927 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 928 | |
| 929 | /**************************************************************************** |
| 930 | * do_string_to_unsigned_long |
| 931 | * |
| 932 | * Convert the string 'str' to an unsigned long and return the result. Exit |
| 933 | * with an appropriate error message on failure. |
| 934 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 935 | static unsigned long do_string_to_unsigned_long(const char str[], |
| 936 | const char str_name[], |
| 937 | const char blurb[]) |
| 938 | { |
| 939 | unsigned long n; |
| 940 | char *p; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 941 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 942 | n = strtoul(str, &p, 0); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 943 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 944 | if (*p != '\0') { |
| 945 | fprintf(stderr, |
| 946 | "%s: Error on line %d of CMOS layout file: %s is not a " |
| 947 | "valid unsigned%s integer.\n", prog_name, line_num, |
| 948 | str_name, blurb); |
| 949 | exit(1); |
| 950 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 951 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 952 | return n; |
| 953 | } |