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 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 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" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 32 | #include "programmer.h" |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 33 | |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 34 | #if CONFIG_INTERNAL == 1 |
| 35 | static enum programmer default_programmer = PROGRAMMER_INTERNAL; |
| 36 | #elif CONFIG_DUMMY == 1 |
| 37 | static enum programmer default_programmer = PROGRAMMER_DUMMY; |
| 38 | #else |
| 39 | /* If neither internal nor dummy are selected, we must pick a sensible default. |
| 40 | * Since there is no reason to prefer a particular external programmer, we fail |
| 41 | * if more than one of them is selected. If only one is selected, it is clear |
| 42 | * that the user wants that one to become the default. |
| 43 | */ |
| 44 | #if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG+CONFIG_RAYER_SPI+CONFIG_NICINTEL+CONFIG_NICINTEL_SPI+CONFIG_OGP_SPI+CONFIG_SATAMV > 1 |
| 45 | #error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one. |
| 46 | #endif |
| 47 | static enum programmer default_programmer = |
| 48 | #if CONFIG_NIC3COM == 1 |
| 49 | PROGRAMMER_NIC3COM |
| 50 | #endif |
| 51 | #if CONFIG_NICREALTEK == 1 |
| 52 | PROGRAMMER_NICREALTEK |
| 53 | #endif |
| 54 | #if CONFIG_NICNATSEMI == 1 |
| 55 | PROGRAMMER_NICNATSEMI |
| 56 | #endif |
| 57 | #if CONFIG_GFXNVIDIA == 1 |
| 58 | PROGRAMMER_GFXNVIDIA |
| 59 | #endif |
| 60 | #if CONFIG_DRKAISER == 1 |
| 61 | PROGRAMMER_DRKAISER |
| 62 | #endif |
| 63 | #if CONFIG_SATASII == 1 |
| 64 | PROGRAMMER_SATASII |
| 65 | #endif |
| 66 | #if CONFIG_ATAHPT == 1 |
| 67 | PROGRAMMER_ATAHPT |
| 68 | #endif |
| 69 | #if CONFIG_FT2232_SPI == 1 |
| 70 | PROGRAMMER_FT2232_SPI |
| 71 | #endif |
| 72 | #if CONFIG_SERPROG == 1 |
| 73 | PROGRAMMER_SERPROG |
| 74 | #endif |
| 75 | #if CONFIG_BUSPIRATE_SPI == 1 |
| 76 | PROGRAMMER_BUSPIRATE_SPI |
| 77 | #endif |
| 78 | #if CONFIG_DEDIPROG == 1 |
| 79 | PROGRAMMER_DEDIPROG |
| 80 | #endif |
| 81 | #if CONFIG_RAYER_SPI == 1 |
| 82 | PROGRAMMER_RAYER_SPI |
| 83 | #endif |
| 84 | #if CONFIG_NICINTEL == 1 |
| 85 | PROGRAMMER_NICINTEL |
| 86 | #endif |
| 87 | #if CONFIG_NICINTEL_SPI == 1 |
| 88 | PROGRAMMER_NICINTEL_SPI |
| 89 | #endif |
| 90 | #if CONFIG_OGP_SPI == 1 |
| 91 | PROGRAMMER_OGP_SPI |
| 92 | #endif |
| 93 | #if CONFIG_SATAMV == 1 |
| 94 | PROGRAMMER_SATAMV |
| 95 | #endif |
| 96 | #if CONFIG_LINUX_SPI == 1 |
| 97 | PROGRAMMER_LINUX_SPI |
| 98 | #endif |
| 99 | ; |
| 100 | #endif |
| 101 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 102 | static void cli_classic_usage(const char *name) |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 103 | { |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 104 | printf("Usage: flashrom [-n] [-V] [-f] [-h|-R|-L|" |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 105 | #if CONFIG_PRINT_WIKI == 1 |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 106 | "-z|" |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 107 | #endif |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 108 | "-E|-r <file>|-w <file>|-v <file>]\n" |
Carl-Daniel Hailfinger | 2d927fb | 2012-01-04 00:48:27 +0000 | [diff] [blame^] | 109 | " [-c <chipname>] [-l <file>]\n" |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 110 | " [-i <image>] [-p <programmername>[:<parameters>]]\n\n"); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 111 | |
| 112 | printf("Please note that the command line interface for flashrom has " |
| 113 | "changed between\n" |
| 114 | "0.9.1 and 0.9.2 and will change again before flashrom 1.0.\n" |
| 115 | "Do not use flashrom in scripts or other automated tools " |
| 116 | "without checking\n" |
| 117 | "that your flashrom version won't interpret options in a " |
| 118 | "different way.\n\n"); |
| 119 | |
| 120 | printf(" -h | --help print this help text\n" |
| 121 | " -R | --version print version (release)\n" |
| 122 | " -r | --read <file> read flash and save to " |
| 123 | "<file>\n" |
| 124 | " -w | --write <file> write <file> to flash\n" |
| 125 | " -v | --verify <file> verify flash against " |
| 126 | "<file>\n" |
| 127 | " -E | --erase erase flash device\n" |
| 128 | " -V | --verbose more verbose output\n" |
| 129 | " -c | --chip <chipname> probe only for specified " |
| 130 | "flash chip\n" |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 131 | " -f | --force force specific operations " |
| 132 | "(see man page)\n" |
| 133 | " -n | --noverify don't auto-verify\n" |
| 134 | " -l | --layout <file> read ROM layout from " |
| 135 | "<file>\n" |
| 136 | " -i | --image <name> only flash image <name> " |
| 137 | "from flash layout\n" |
| 138 | " -L | --list-supported print supported devices\n" |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 139 | #if CONFIG_PRINT_WIKI == 1 |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 140 | " -z | --list-supported-wiki print supported devices " |
| 141 | "in wiki syntax\n" |
| 142 | #endif |
| 143 | " -p | --programmer <name>[:<param>] specify the programmer " |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 144 | "device\n"); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 145 | |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 146 | list_programmers_linebreak(37, 80, 1); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 147 | printf("\nYou can specify one of -h, -R, -L, " |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 148 | #if CONFIG_PRINT_WIKI == 1 |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 149 | "-z, " |
| 150 | #endif |
| 151 | "-E, -r, -w, -v or no operation.\n" |
| 152 | "If no operation is specified, flashrom will only probe for " |
| 153 | "flash chips.\n\n"); |
| 154 | } |
| 155 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 156 | static void cli_classic_abort_usage(void) |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 157 | { |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 158 | printf("Please run \"flashrom --help\" for usage info.\n"); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 159 | exit(1); |
| 160 | } |
| 161 | |
Uwe Hermann | 394ee78 | 2011-08-20 14:14:22 +0000 | [diff] [blame] | 162 | int main(int argc, char *argv[]) |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 163 | { |
| 164 | unsigned long size; |
| 165 | /* Probe for up to three flash chips. */ |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 166 | const struct flashchip *flash; |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 167 | struct flashctx flashes[3]; |
| 168 | struct flashctx *fill_flash; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 169 | const char *name; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 170 | int namelen, opt, i, j; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 171 | int startchip = 0, chipcount = 0, option_index = 0, force = 0; |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 172 | #if CONFIG_PRINT_WIKI == 1 |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 173 | int list_supported_wiki = 0; |
| 174 | #endif |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 175 | int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0; |
| 176 | int dont_verify_it = 0, list_supported = 0, operation_specified = 0; |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 177 | enum programmer prog = PROGRAMMER_INVALID; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 178 | int ret = 0; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 179 | |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 180 | static const char optstring[] = "r:Rw:v:nVEfc:m:l:i:p:Lzh"; |
| 181 | static const struct option long_options[] = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 182 | {"read", 1, NULL, 'r'}, |
| 183 | {"write", 1, NULL, 'w'}, |
| 184 | {"erase", 0, NULL, 'E'}, |
| 185 | {"verify", 1, NULL, 'v'}, |
| 186 | {"noverify", 0, NULL, 'n'}, |
| 187 | {"chip", 1, NULL, 'c'}, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 188 | {"verbose", 0, NULL, 'V'}, |
| 189 | {"force", 0, NULL, 'f'}, |
| 190 | {"layout", 1, NULL, 'l'}, |
| 191 | {"image", 1, NULL, 'i'}, |
| 192 | {"list-supported", 0, NULL, 'L'}, |
| 193 | {"list-supported-wiki", 0, NULL, 'z'}, |
| 194 | {"programmer", 1, NULL, 'p'}, |
| 195 | {"help", 0, NULL, 'h'}, |
| 196 | {"version", 0, NULL, 'R'}, |
| 197 | {NULL, 0, NULL, 0}, |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | char *filename = NULL; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 201 | char *tempstr = NULL; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 202 | char *pparam = NULL; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 203 | |
| 204 | print_version(); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 205 | print_banner(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 206 | |
| 207 | if (selfcheck()) |
| 208 | exit(1); |
| 209 | |
| 210 | setbuf(stdout, NULL); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 211 | /* FIXME: Delay all operation_specified checks until after command |
| 212 | * line parsing to allow --help overriding everything else. |
| 213 | */ |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 214 | while ((opt = getopt_long(argc, argv, optstring, |
| 215 | long_options, &option_index)) != EOF) { |
| 216 | switch (opt) { |
| 217 | case 'r': |
| 218 | if (++operation_specified > 1) { |
| 219 | fprintf(stderr, "More than one operation " |
| 220 | "specified. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 221 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 222 | } |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 223 | filename = strdup(optarg); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 224 | read_it = 1; |
| 225 | break; |
| 226 | case 'w': |
| 227 | if (++operation_specified > 1) { |
| 228 | fprintf(stderr, "More than one operation " |
| 229 | "specified. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 230 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 231 | } |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 232 | filename = strdup(optarg); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 233 | write_it = 1; |
| 234 | break; |
| 235 | case 'v': |
| 236 | //FIXME: gracefully handle superfluous -v |
| 237 | if (++operation_specified > 1) { |
| 238 | fprintf(stderr, "More than one operation " |
| 239 | "specified. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 240 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 241 | } |
| 242 | if (dont_verify_it) { |
| 243 | fprintf(stderr, "--verify and --noverify are" |
| 244 | "mutually exclusive. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 245 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 246 | } |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 247 | filename = strdup(optarg); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 248 | verify_it = 1; |
| 249 | break; |
| 250 | case 'n': |
| 251 | if (verify_it) { |
| 252 | fprintf(stderr, "--verify and --noverify are" |
| 253 | "mutually exclusive. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 254 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 255 | } |
| 256 | dont_verify_it = 1; |
| 257 | break; |
| 258 | case 'c': |
| 259 | chip_to_probe = strdup(optarg); |
| 260 | break; |
| 261 | case 'V': |
Sean Nelson | 51e97d7 | 2010-01-07 20:09:33 +0000 | [diff] [blame] | 262 | verbose++; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 263 | break; |
| 264 | case 'E': |
| 265 | if (++operation_specified > 1) { |
| 266 | fprintf(stderr, "More than one operation " |
| 267 | "specified. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 268 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 269 | } |
| 270 | erase_it = 1; |
| 271 | break; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 272 | case 'f': |
| 273 | force = 1; |
| 274 | break; |
| 275 | case 'l': |
| 276 | tempstr = strdup(optarg); |
| 277 | if (read_romlayout(tempstr)) |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 278 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 279 | break; |
| 280 | case 'i': |
| 281 | tempstr = strdup(optarg); |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 282 | if (register_include_arg(tempstr)) |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 283 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 284 | break; |
| 285 | case 'L': |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 286 | if (++operation_specified > 1) { |
| 287 | fprintf(stderr, "More than one operation " |
| 288 | "specified. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 289 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 290 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 291 | list_supported = 1; |
| 292 | break; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 293 | case 'z': |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 294 | #if CONFIG_PRINT_WIKI == 1 |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 295 | if (++operation_specified > 1) { |
| 296 | fprintf(stderr, "More than one operation " |
| 297 | "specified. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 298 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 299 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 300 | list_supported_wiki = 1; |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 301 | #else |
| 302 | fprintf(stderr, "Error: Wiki output was not compiled " |
| 303 | "in. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 304 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 305 | #endif |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 306 | break; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 307 | case 'p': |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 308 | if (prog != PROGRAMMER_INVALID) { |
| 309 | fprintf(stderr, "Error: --programmer specified " |
| 310 | "more than once. You can separate " |
| 311 | "multiple\nparameters for a programmer " |
| 312 | "with \",\". Please see the man page " |
| 313 | "for details.\n"); |
| 314 | cli_classic_abort_usage(); |
| 315 | } |
| 316 | for (prog = 0; prog < PROGRAMMER_INVALID; prog++) { |
| 317 | name = programmer_table[prog].name; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 318 | namelen = strlen(name); |
| 319 | if (strncmp(optarg, name, namelen) == 0) { |
| 320 | switch (optarg[namelen]) { |
| 321 | case ':': |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 322 | pparam = strdup(optarg + namelen + 1); |
| 323 | if (!strlen(pparam)) { |
| 324 | free(pparam); |
| 325 | pparam = NULL; |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 326 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 327 | break; |
| 328 | case '\0': |
| 329 | break; |
| 330 | default: |
| 331 | /* The continue refers to the |
| 332 | * for loop. It is here to be |
| 333 | * able to differentiate between |
| 334 | * foo and foobar. |
| 335 | */ |
| 336 | continue; |
| 337 | } |
| 338 | break; |
| 339 | } |
| 340 | } |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 341 | if (prog == PROGRAMMER_INVALID) { |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 342 | fprintf(stderr, "Error: Unknown programmer " |
| 343 | "%s.\n", optarg); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 344 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 345 | } |
| 346 | break; |
| 347 | case 'R': |
| 348 | /* print_version() is always called during startup. */ |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 349 | if (++operation_specified > 1) { |
| 350 | fprintf(stderr, "More than one operation " |
| 351 | "specified. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 352 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 353 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 354 | exit(0); |
| 355 | break; |
| 356 | case 'h': |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 357 | if (++operation_specified > 1) { |
| 358 | fprintf(stderr, "More than one operation " |
| 359 | "specified. Aborting.\n"); |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 360 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 361 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 362 | cli_classic_usage(argv[0]); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 363 | exit(0); |
| 364 | break; |
| 365 | default: |
Uwe Hermann | 2db77a0 | 2010-06-04 17:07:39 +0000 | [diff] [blame] | 366 | cli_classic_abort_usage(); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 371 | if (optind < argc) { |
| 372 | fprintf(stderr, "Error: Extra parameter found.\n"); |
| 373 | cli_classic_abort_usage(); |
| 374 | } |
| 375 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 376 | if (process_include_args()) |
| 377 | cli_classic_abort_usage(); |
| 378 | |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 379 | /* FIXME: Print the actions flashrom will take. */ |
| 380 | |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 381 | if (list_supported) { |
| 382 | print_supported(); |
| 383 | exit(0); |
| 384 | } |
| 385 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 386 | #if CONFIG_PRINT_WIKI == 1 |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 387 | if (list_supported_wiki) { |
| 388 | print_supported_wiki(); |
| 389 | exit(0); |
| 390 | } |
| 391 | #endif |
| 392 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 393 | /* Does a chip with the requested name exist in the flashchips array? */ |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 394 | if (chip_to_probe) { |
| 395 | for (flash = flashchips; flash && flash->name; flash++) |
| 396 | if (!strcmp(flash->name, chip_to_probe)) |
| 397 | break; |
| 398 | if (!flash || !flash->name) { |
| 399 | fprintf(stderr, "Error: Unknown chip '%s' specified.\n", |
| 400 | chip_to_probe); |
| 401 | printf("Run flashrom -L to view the hardware supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 402 | "in this flashrom version.\n"); |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 403 | exit(1); |
| 404 | } |
| 405 | /* Clean up after the check. */ |
| 406 | flash = NULL; |
| 407 | } |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 408 | |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 409 | if (prog == PROGRAMMER_INVALID) |
| 410 | prog = default_programmer; |
| 411 | |
Carl-Daniel Hailfinger | 4988420 | 2010-05-22 07:10:46 +0000 | [diff] [blame] | 412 | /* FIXME: Delay calibration should happen in programmer code. */ |
| 413 | myusec_calibrate_delay(); |
| 414 | |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 415 | if (programmer_init(prog, pparam)) { |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 416 | fprintf(stderr, "Error: Programmer initialization failed.\n"); |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 417 | ret = 1; |
| 418 | goto out_shutdown; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 419 | } |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 420 | tempstr = flashbuses_to_text(get_buses_supported()); |
| 421 | msg_pdbg("The following protocols are supported: %s.\n", |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 422 | tempstr); |
| 423 | free(tempstr); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 424 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 425 | for (j = 0; j < registered_programmer_count; j++) { |
| 426 | startchip = 0; |
Michael Karcher | 222bf10 | 2011-12-22 23:27:03 +0000 | [diff] [blame] | 427 | while (chipcount < ARRAY_SIZE(flashes)) { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 428 | startchip = probe_flash(®istered_programmers[j], |
Michael Karcher | 222bf10 | 2011-12-22 23:27:03 +0000 | [diff] [blame] | 429 | startchip, |
| 430 | &flashes[chipcount], 0); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 431 | if (startchip == -1) |
| 432 | break; |
| 433 | chipcount++; |
| 434 | startchip++; |
| 435 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 438 | if (chipcount > 1) { |
Stefan Tauner | 716e098 | 2011-07-25 20:38:52 +0000 | [diff] [blame] | 439 | printf("Multiple flash chips were detected: \"%s\"", |
| 440 | flashes[0].name); |
| 441 | for (i = 1; i < chipcount; i++) |
| 442 | printf(", \"%s\"", flashes[i].name); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 443 | printf("\nPlease specify which chip to use with the " |
| 444 | "-c <chipname> option.\n"); |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 445 | ret = 1; |
| 446 | goto out_shutdown; |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 447 | } else if (!chipcount) { |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 448 | printf("No EEPROM/flash device found.\n"); |
| 449 | if (!force || !chip_to_probe) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 450 | printf("Note: flashrom can never write if the flash " |
| 451 | "chip isn't found automatically.\n"); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 452 | } |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 453 | #if 0 // FIXME: What happens for a forced chip read if multiple compatible programmers are registered? |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 454 | if (force && read_it && chip_to_probe) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 455 | printf("Force read (-f -r -c) requested, pretending " |
| 456 | "the chip is there:\n"); |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 457 | startchip = probe_flash(0, &flashes[0], 1); |
| 458 | if (startchip == -1) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 459 | printf("Probing for flash chip '%s' failed.\n", |
| 460 | chip_to_probe); |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 461 | ret = 1; |
| 462 | goto out_shutdown; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 463 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 464 | printf("Please note that forced reads most likely " |
| 465 | "contain garbage.\n"); |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 466 | return read_flash_to_file(&flashes[0], filename); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 467 | } |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 468 | #endif |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 469 | ret = 1; |
| 470 | goto out_shutdown; |
Stefan Tauner | 1d94763 | 2011-09-11 22:08:58 +0000 | [diff] [blame] | 471 | } else if (!chip_to_probe) { |
| 472 | /* repeat for convenience when looking at foreign logs */ |
| 473 | tempstr = flashbuses_to_text(flashes[0].bustype); |
| 474 | msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n", |
| 475 | flashes[0].vendor, flashes[0].name, |
| 476 | flashes[0].total_size, tempstr); |
| 477 | free(tempstr); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 480 | fill_flash = &flashes[0]; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 481 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 482 | check_chip_supported(fill_flash); |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 483 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 484 | size = fill_flash->total_size * 1024; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 485 | if (check_max_decode(fill_flash->pgm->buses_supported & fill_flash->bustype, size) && |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 486 | (!force)) { |
| 487 | fprintf(stderr, "Chip is too big for this programmer " |
| 488 | "(-V gives details). Use --force to override.\n"); |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 489 | ret = 1; |
| 490 | goto out_shutdown; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | if (!(read_it | write_it | verify_it | erase_it)) { |
| 494 | printf("No operations were specified.\n"); |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 495 | goto out_shutdown; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | if (!filename && !erase_it) { |
| 499 | printf("Error: No filename specified.\n"); |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 500 | ret = 1; |
| 501 | goto out_shutdown; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* Always verify write operations unless -n is used. */ |
| 505 | if (write_it && !dont_verify_it) |
| 506 | verify_it = 1; |
| 507 | |
Carl-Daniel Hailfinger | 9ad4255 | 2010-09-15 10:20:16 +0000 | [diff] [blame] | 508 | /* FIXME: We should issue an unconditional chip reset here. This can be |
| 509 | * done once we have a .reset function in struct flashchip. |
| 510 | * Give the chip time to settle. |
| 511 | */ |
| 512 | programmer_delay(100000); |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 513 | return doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it); |
Carl-Daniel Hailfinger | d566014 | 2011-07-15 23:47:45 +0000 | [diff] [blame] | 514 | |
| 515 | out_shutdown: |
| 516 | programmer_shutdown(); |
| 517 | return ret; |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 518 | } |