Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 5 | * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com> |
| 6 | * Copyright (C) 2005-2008 coresystems GmbH |
| 7 | * Copyright (C) 2008,2009,2010 Carl-Daniel Hailfinger |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <getopt.h> |
| 30 | #include "flash.h" |
| 31 | #include "flashchips.h" |
| 32 | |
| 33 | void cli_classic_usage(const char *name) |
| 34 | { |
| 35 | const char *pname; |
| 36 | int pnamelen; |
| 37 | int remaining = 0; |
| 38 | enum programmer p; |
| 39 | |
| 40 | printf("Usage: %s [-VfLzhR] [-E|-r file|-w file|-v file] [-c chipname]\n" |
| 41 | " [-m [vendor:]part] [-l file] [-i image] [-p programmer]\n\n", name); |
| 42 | |
| 43 | printf("Please note that the command line interface for flashrom will " |
| 44 | "change before\nflashrom 1.0. Do not use flashrom in scripts " |
| 45 | "or other automated tools without\nchecking that your flashrom" |
| 46 | " version won't interpret options in a different way.\n\n"); |
| 47 | |
| 48 | printf |
| 49 | (" -r | --read: read flash and save into file\n" |
| 50 | " -w | --write: write file into flash\n" |
| 51 | " -v | --verify: verify flash against file\n" |
| 52 | " -n | --noverify: don't verify flash against file\n" |
| 53 | " -E | --erase: erase flash device\n" |
| 54 | " -V | --verbose: more verbose output\n" |
| 55 | " -c | --chip <chipname>: probe only for specified flash chip\n" |
| 56 | #if INTERNAL_SUPPORT == 1 |
| 57 | " -m | --mainboard <[vendor:]part>: override mainboard settings\n" |
| 58 | #endif |
| 59 | " -f | --force: force write without checking image\n" |
| 60 | " -l | --layout <file.layout>: read ROM layout from file\n" |
| 61 | " -i | --image <name>: only flash image name from flash layout\n" |
| 62 | " -L | --list-supported: print supported devices\n" |
| 63 | #if PRINT_WIKI_SUPPORT == 1 |
| 64 | " -z | --list-supported-wiki: print supported devices in wiki syntax\n" |
| 65 | #endif |
| 66 | " -p | --programmer <name>: specify the programmer device"); |
| 67 | |
| 68 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
| 69 | pname = programmer_table[p].name; |
| 70 | pnamelen = strlen(pname); |
| 71 | if (remaining - pnamelen - 2 < 0) { |
| 72 | printf("\n "); |
| 73 | remaining = 43; |
| 74 | } else { |
| 75 | printf(" "); |
| 76 | remaining--; |
| 77 | } |
| 78 | if (p == 0) { |
| 79 | printf("("); |
| 80 | remaining--; |
| 81 | } |
| 82 | printf("%s", pname); |
| 83 | remaining -= pnamelen; |
| 84 | if (p < PROGRAMMER_INVALID - 1) { |
| 85 | printf(","); |
| 86 | remaining--; |
| 87 | } else { |
| 88 | printf(")\n"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | printf( |
| 93 | " -h | --help: print this help text\n" |
| 94 | " -R | --version: print the version (release)\n" |
| 95 | "\nYou can specify one of -E, -r, -w, -v or no operation. If no operation is\n" |
| 96 | "specified, then all that happens is that flash info is dumped.\n\n"); |
| 97 | exit(1); |
| 98 | } |
| 99 | |
| 100 | int cli_classic(int argc, char *argv[]) |
| 101 | { |
| 102 | unsigned long size; |
| 103 | /* Probe for up to three flash chips. */ |
| 104 | struct flashchip *flash, *flashes[3]; |
| 105 | const char *name; |
| 106 | int namelen; |
| 107 | int opt; |
| 108 | int option_index = 0; |
| 109 | int force = 0; |
| 110 | int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0; |
| 111 | int dont_verify_it = 0, list_supported = 0; |
| 112 | #if PRINT_WIKI_SUPPORT == 1 |
| 113 | int list_supported_wiki = 0; |
| 114 | #endif |
| 115 | int operation_specified = 0; |
| 116 | int i; |
| 117 | |
| 118 | #if PRINT_WIKI_SUPPORT == 1 |
| 119 | const char *optstring = "rRwvnVEfc:m:l:i:p:Lzh"; |
| 120 | #else |
| 121 | const char *optstring = "rRwvnVEfc:m:l:i:p:Lh"; |
| 122 | #endif |
| 123 | static struct option long_options[] = { |
| 124 | {"read", 0, 0, 'r'}, |
| 125 | {"write", 0, 0, 'w'}, |
| 126 | {"erase", 0, 0, 'E'}, |
| 127 | {"verify", 0, 0, 'v'}, |
| 128 | {"noverify", 0, 0, 'n'}, |
| 129 | {"chip", 1, 0, 'c'}, |
| 130 | {"mainboard", 1, 0, 'm'}, |
| 131 | {"verbose", 0, 0, 'V'}, |
| 132 | {"force", 0, 0, 'f'}, |
| 133 | {"layout", 1, 0, 'l'}, |
| 134 | {"image", 1, 0, 'i'}, |
| 135 | {"list-supported", 0, 0, 'L'}, |
| 136 | #if PRINT_WIKI_SUPPORT == 1 |
| 137 | {"list-supported-wiki", 0, 0, 'z'}, |
| 138 | #endif |
| 139 | {"programmer", 1, 0, 'p'}, |
| 140 | {"help", 0, 0, 'h'}, |
| 141 | {"version", 0, 0, 'R'}, |
| 142 | {0, 0, 0, 0} |
| 143 | }; |
| 144 | |
| 145 | char *filename = NULL; |
| 146 | |
| 147 | char *tempstr = NULL; |
| 148 | |
| 149 | print_version(); |
| 150 | |
| 151 | if (argc > 1) { |
| 152 | /* Yes, print them. */ |
| 153 | int i; |
| 154 | printf_debug("The arguments are:\n"); |
| 155 | for (i = 1; i < argc; ++i) |
| 156 | printf_debug("%s\n", argv[i]); |
| 157 | } |
| 158 | |
| 159 | if (selfcheck()) |
| 160 | exit(1); |
| 161 | |
| 162 | setbuf(stdout, NULL); |
| 163 | while ((opt = getopt_long(argc, argv, optstring, |
| 164 | long_options, &option_index)) != EOF) { |
| 165 | switch (opt) { |
| 166 | case 'r': |
| 167 | if (++operation_specified > 1) { |
| 168 | fprintf(stderr, "More than one operation " |
| 169 | "specified. Aborting.\n"); |
| 170 | exit(1); |
| 171 | } |
| 172 | read_it = 1; |
| 173 | break; |
| 174 | case 'w': |
| 175 | if (++operation_specified > 1) { |
| 176 | fprintf(stderr, "More than one operation " |
| 177 | "specified. Aborting.\n"); |
| 178 | exit(1); |
| 179 | } |
| 180 | write_it = 1; |
| 181 | break; |
| 182 | case 'v': |
| 183 | //FIXME: gracefully handle superfluous -v |
| 184 | if (++operation_specified > 1) { |
| 185 | fprintf(stderr, "More than one operation " |
| 186 | "specified. Aborting.\n"); |
| 187 | exit(1); |
| 188 | } |
| 189 | if (dont_verify_it) { |
| 190 | fprintf(stderr, "--verify and --noverify are" |
| 191 | "mutually exclusive. Aborting.\n"); |
| 192 | exit(1); |
| 193 | } |
| 194 | verify_it = 1; |
| 195 | break; |
| 196 | case 'n': |
| 197 | if (verify_it) { |
| 198 | fprintf(stderr, "--verify and --noverify are" |
| 199 | "mutually exclusive. Aborting.\n"); |
| 200 | exit(1); |
| 201 | } |
| 202 | dont_verify_it = 1; |
| 203 | break; |
| 204 | case 'c': |
| 205 | chip_to_probe = strdup(optarg); |
| 206 | break; |
| 207 | case 'V': |
| 208 | verbose = 1; |
| 209 | break; |
| 210 | case 'E': |
| 211 | if (++operation_specified > 1) { |
| 212 | fprintf(stderr, "More than one operation " |
| 213 | "specified. Aborting.\n"); |
| 214 | exit(1); |
| 215 | } |
| 216 | erase_it = 1; |
| 217 | break; |
| 218 | #if INTERNAL_SUPPORT == 1 |
| 219 | case 'm': |
| 220 | tempstr = strdup(optarg); |
| 221 | lb_vendor_dev_from_string(tempstr); |
| 222 | break; |
| 223 | #endif |
| 224 | case 'f': |
| 225 | force = 1; |
| 226 | break; |
| 227 | case 'l': |
| 228 | tempstr = strdup(optarg); |
| 229 | if (read_romlayout(tempstr)) |
| 230 | exit(1); |
| 231 | break; |
| 232 | case 'i': |
| 233 | tempstr = strdup(optarg); |
| 234 | find_romentry(tempstr); |
| 235 | break; |
| 236 | case 'L': |
| 237 | list_supported = 1; |
| 238 | break; |
| 239 | #if PRINT_WIKI_SUPPORT == 1 |
| 240 | case 'z': |
| 241 | list_supported_wiki = 1; |
| 242 | break; |
| 243 | #endif |
| 244 | case 'p': |
| 245 | for (programmer = 0; programmer < PROGRAMMER_INVALID; programmer++) { |
| 246 | name = programmer_table[programmer].name; |
| 247 | namelen = strlen(name); |
| 248 | if (strncmp(optarg, name, namelen) == 0) { |
| 249 | switch (optarg[namelen]) { |
| 250 | case ':': |
| 251 | programmer_param = strdup(optarg + namelen + 1); |
| 252 | break; |
| 253 | case '\0': |
| 254 | break; |
| 255 | default: |
| 256 | /* The continue refers to the |
| 257 | * for loop. It is here to be |
| 258 | * able to differentiate between |
| 259 | * foo and foobar. |
| 260 | */ |
| 261 | continue; |
| 262 | } |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | if (programmer == PROGRAMMER_INVALID) { |
| 267 | printf("Error: Unknown programmer %s.\n", optarg); |
| 268 | exit(1); |
| 269 | } |
| 270 | break; |
| 271 | case 'R': |
| 272 | /* print_version() is always called during startup. */ |
| 273 | exit(0); |
| 274 | break; |
| 275 | case 'h': |
| 276 | default: |
| 277 | cli_classic_usage(argv[0]); |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (list_supported) { |
| 283 | print_supported(); |
| 284 | exit(0); |
| 285 | } |
| 286 | |
| 287 | #if PRINT_WIKI_SUPPORT == 1 |
| 288 | if (list_supported_wiki) { |
| 289 | print_supported_wiki(); |
| 290 | exit(0); |
| 291 | } |
| 292 | #endif |
| 293 | |
| 294 | if (read_it && write_it) { |
| 295 | printf("Error: -r and -w are mutually exclusive.\n"); |
| 296 | cli_classic_usage(argv[0]); |
| 297 | } |
| 298 | |
| 299 | if (optind < argc) |
| 300 | filename = argv[optind++]; |
| 301 | |
| 302 | if (optind < argc) { |
| 303 | printf("Error: Extra parameter found.\n"); |
| 304 | cli_classic_usage(argv[0]); |
| 305 | } |
| 306 | |
| 307 | if (programmer_init()) { |
| 308 | fprintf(stderr, "Error: Programmer initialization failed.\n"); |
| 309 | exit(1); |
| 310 | } |
| 311 | |
| 312 | // FIXME: Delay calibration should happen in programmer code. |
| 313 | myusec_calibrate_delay(); |
| 314 | |
| 315 | for (i = 0; i < ARRAY_SIZE(flashes); i++) { |
| 316 | flashes[i] = |
| 317 | probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0); |
| 318 | if (!flashes[i]) |
| 319 | for (i++; i < ARRAY_SIZE(flashes); i++) |
| 320 | flashes[i] = NULL; |
| 321 | } |
| 322 | |
| 323 | if (flashes[1]) { |
| 324 | printf("Multiple flash chips were detected:"); |
| 325 | for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++) |
| 326 | printf(" %s", flashes[i]->name); |
| 327 | printf("\nPlease specify which chip to use with the -c <chipname> option.\n"); |
| 328 | programmer_shutdown(); |
| 329 | exit(1); |
| 330 | } else if (!flashes[0]) { |
| 331 | printf("No EEPROM/flash device found.\n"); |
| 332 | if (!force || !chip_to_probe) { |
| 333 | printf("If you know which flash chip you have, and if this version of flashrom\n"); |
| 334 | printf("supports a similar flash chip, you can try to force read your chip. Run:\n"); |
| 335 | printf("flashrom -f -r -c similar_supported_flash_chip filename\n"); |
| 336 | printf("\n"); |
| 337 | printf("Note: flashrom can never write when the flash chip isn't found automatically.\n"); |
| 338 | } |
| 339 | if (force && read_it && chip_to_probe) { |
| 340 | printf("Force read (-f -r -c) requested, forcing chip probe success:\n"); |
| 341 | flashes[0] = probe_flash(flashchips, 1); |
| 342 | if (!flashes[0]) { |
| 343 | printf("flashrom does not support a flash chip named '%s'.\n", chip_to_probe); |
| 344 | printf("Run flashrom -L to view the hardware supported in this flashrom version.\n"); |
| 345 | exit(1); |
| 346 | } |
| 347 | printf("Please note that forced reads most likely contain garbage.\n"); |
| 348 | return read_flash(flashes[0], filename); |
| 349 | } |
| 350 | // FIXME: flash writes stay enabled! |
| 351 | programmer_shutdown(); |
| 352 | exit(1); |
| 353 | } |
| 354 | |
| 355 | flash = flashes[0]; |
| 356 | |
| 357 | check_chip_supported(flash); |
| 358 | |
| 359 | size = flash->total_size * 1024; |
| 360 | if (check_max_decode((buses_supported & flash->bustype), size) && |
| 361 | (!force)) { |
| 362 | fprintf(stderr, "Chip is too big for this programmer " |
| 363 | "(-V gives details). Use --force to override.\n"); |
| 364 | programmer_shutdown(); |
| 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | if (!(read_it | write_it | verify_it | erase_it)) { |
| 369 | printf("No operations were specified.\n"); |
| 370 | // FIXME: flash writes stay enabled! |
| 371 | programmer_shutdown(); |
| 372 | exit(1); |
| 373 | } |
| 374 | |
| 375 | if (!filename && !erase_it) { |
| 376 | printf("Error: No filename specified.\n"); |
| 377 | // FIXME: flash writes stay enabled! |
| 378 | programmer_shutdown(); |
| 379 | exit(1); |
| 380 | } |
| 381 | |
| 382 | /* Always verify write operations unless -n is used. */ |
| 383 | if (write_it && !dont_verify_it) |
| 384 | verify_it = 1; |
| 385 | |
| 386 | return doit(flash, force, filename, read_it, write_it, erase_it, verify_it); |
| 387 | } |