blob: f3ae123b27dab209d26160c37b8fd0a9c3afaaa4 [file] [log] [blame]
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001/*
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.
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000018 */
19
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +100020#include <errno.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000021#include <stdio.h>
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000022#include <fcntl.h>
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000023#include <sys/stat.h>
24#include <string.h>
Jack Rosenthal5f1524b2021-04-09 10:03:14 -060025#include <stdbool.h>
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000026#include <stdlib.h>
27#include <getopt.h>
28#include "flash.h"
29#include "flashchips.h"
Arthur Heymansc82900b2018-01-10 12:48:16 +010030#include "fmap.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000031#include "programmer.h"
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +100032#include "writeprotect.h"
Nico Huber18781102012-12-10 13:34:12 +000033#include "libflashrom.h"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000034
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000035static void cli_classic_usage(const char *name)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000036{
Stefan Taunerb226cb12012-11-24 18:59:39 +000037 printf("Usage: %s [-h|-R|-L|"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000038#if CONFIG_PRINT_WIKI == 1
Stefan Taunerb226cb12012-11-24 18:59:39 +000039 "-z|"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000040#endif
Edward O'Callaghan16ec45c2019-10-04 20:24:53 +100041 "\n\t-p <programmername>[:<parameters>] [-c <chipname>]\n"
42 "\t\t(--flash-name|--flash-size|\n"
Daniel Campelloce983bc2021-04-16 15:26:42 -060043 "\t\t [-E|-x|(-r|-w|-v) <file>]\n"
Daniel Campello45d50a12021-04-13 10:47:25 -060044 "\t\t [(-l <layoutfile>|--ifd| --fmap|--fmap-file <file>) [-i <region>[:<file>]]...]\n"
Edward O'Callaghan16ec45c2019-10-04 20:24:53 +100045 "\t\t [-n] [-N] [-f])]\n"
46 "\t[-V[V[V]]] [-o <logfile>]\n\n", name);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000047
Stefan Taunerb226cb12012-11-24 18:59:39 +000048 printf(" -h | --help print this help text\n"
49 " -R | --version print version (release)\n"
50 " -r | --read <file> read flash and save to <file>\n"
Daniel Campellod4063bf2021-04-15 10:36:04 -060051 " -w | --write <file|-> write <file> or the content provided\n"
52 " on the standard input to flash\n"
53 " -v | --verify <file|-> verify flash against <file>\n"
54 " or the content provided on the standard input\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000055 " -E | --erase erase flash memory\n"
56 " -V | --verbose more verbose output\n"
57 " -c | --chip <chipname> probe only for specified flash chip\n"
58 " -f | --force force specific operations (see man page)\n"
59 " -n | --noverify don't auto-verify\n"
Nico Huber99d15952016-05-02 16:54:24 +020060 " -N | --noverify-all verify included regions only (cf. -i)\n"
Daniel Campelloce983bc2021-04-16 15:26:42 -060061 " -x | --extract extract regions to files\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000062 " -l | --layout <layoutfile> read ROM layout from <layoutfile>\n"
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +100063 " --wp-disable disable write protection\n"
64 " --wp-enable enable write protection\n"
65 " --wp-list list write protect range\n"
66 " --wp-status show write protect status\n"
67 " --wp-range=<start>,<len> set write protect range\n"
Daniel Campello7367f772021-04-19 18:43:09 -060068 " --wp-region <region> set write protect region\n"
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +100069 " --flash-name read out the detected flash name\n"
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +100070 " --flash-size read out the detected flash size\n"
Arthur Heymansc82900b2018-01-10 12:48:16 +010071 " --fmap read ROM layout from fmap embedded in ROM\n"
72 " --fmap-file <fmapfile> read ROM layout from fmap in <fmapfile>\n"
Nico Huber305f4172013-06-14 11:55:26 +020073 " --ifd read layout from an Intel Firmware Descriptor\n"
Daniel Campello45d50a12021-04-13 10:47:25 -060074 " -i | --image <region>[:<file>] only read/write image <region> from layout\n"
75 " (optionally with data from <file>)\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000076 " -o | --output <logfile> log output to <logfile>\n"
Paul Kocialkowskif701f342018-01-15 01:10:36 +030077 " --flash-contents <ref-file> assume flash contents to be <ref-file>\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000078 " -L | --list-supported print supported devices\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000079#if CONFIG_PRINT_WIKI == 1
Stefan Taunerb226cb12012-11-24 18:59:39 +000080 " -z | --list-supported-wiki print supported devices in wiki syntax\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000081#endif
Stefan Taunerb226cb12012-11-24 18:59:39 +000082 " -p | --programmer <name>[:<param>] specify the programmer device. One of\n");
83 list_programmers_linebreak(4, 80, 0);
84 printf(".\n\nYou can specify one of -h, -R, -L, "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000085#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000086 "-z, "
87#endif
88 "-E, -r, -w, -v or no operation.\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000089 "If no operation is specified, flashrom will only probe for flash chips.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000090}
91
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +100092static void cli_classic_abort_usage(const char *msg)
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000093{
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +100094 if (msg)
95 fprintf(stderr, "%s", msg);
Uwe Hermann2db77a02010-06-04 17:07:39 +000096 printf("Please run \"flashrom --help\" for usage info.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000097 exit(1);
98}
99
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000100static void cli_classic_validate_singleop(int *operation_specified)
101{
102 if (++(*operation_specified) > 1) {
103 cli_classic_abort_usage("More than one operation specified. Aborting.\n");
104 }
105}
106
Jacob Garber4a84ec22019-07-25 19:12:31 -0600107static int check_filename(char *filename, const char *type)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000108{
109 if (!filename || (filename[0] == '\0')) {
110 fprintf(stderr, "Error: No %s file specified.\n", type);
111 return 1;
112 }
113 /* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
Daniel Campellod4063bf2021-04-15 10:36:04 -0600114 if (filename[0] == '-' && filename[1] != '\0')
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000115 fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
116 return 0;
117}
118
Jack Rosenthal5f1524b2021-04-09 10:03:14 -0600119/* Ensure a file is open by means of fstat */
120static bool check_file(FILE *file)
121{
122#ifndef STANDALONE
123 struct stat statbuf;
124
125 if (fstat(fileno(file), &statbuf) < 0)
126 return false;
127#endif /* !STANDALONE */
128 return true;
129}
130
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000131static int parse_wp_range(unsigned int *start, unsigned int *len)
132{
133 char *endptr = NULL, *token = NULL;
134
135 if (!optarg) {
136 msg_gerr("Error: No wp-range values provided\n");
137 return -1;
138 }
139
140 token = strtok(optarg, ",");
141 if (!token) {
142 msg_gerr("Error: Invalid wp-range argument format\n");
143 return -1;
144 }
145 *start = strtoul(token, &endptr, 0);
146
147 token = strtok(NULL, ",");
148 if (!token) {
149 msg_gerr("Error: Invalid wp-range argument format\n");
150 return -1;
151 }
152 *len = strtoul(token, &endptr, 0);
153
154 return 0;
155}
156
Uwe Hermann394ee782011-08-20 14:14:22 +0000157int main(int argc, char *argv[])
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000158{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000159 const struct flashchip *chip = NULL;
Jernej Škrabece814a9b2014-12-12 00:32:03 +0000160 /* Probe for up to eight flash chips. */
161 struct flashctx flashes[8] = {{0}};
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000162 struct flashctx *fill_flash;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000163 const char *name;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000164 int namelen, opt, i, j;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100165 int startchip = -1, chipcount = 0, option_index = 0, force = 0, ifd = 0, fmap = 0;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000166#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000167 int list_supported_wiki = 0;
168#endif
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000169 int flash_name = 0, flash_size = 0;
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000170 int set_wp_enable = 0, set_wp_disable = 0, wp_status = 0;
171 int set_wp_range = 0, set_wp_region = 0, wp_list = 0;
Daniel Campelloce983bc2021-04-16 15:26:42 -0600172 int read_it = 0, extract_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
Nico Huber99d15952016-05-02 16:54:24 +0200173 int dont_verify_it = 0, dont_verify_all = 0, list_supported = 0, operation_specified = 0;
Nico Huber305f4172013-06-14 11:55:26 +0200174 struct flashrom_layout *layout = NULL;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000175 enum programmer prog = PROGRAMMER_INVALID;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300176 enum {
177 OPTION_IFD = 0x0100,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100178 OPTION_FMAP,
179 OPTION_FMAP_FILE,
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300180 OPTION_FLASH_CONTENTS,
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000181 OPTION_FLASH_NAME,
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000182 OPTION_FLASH_SIZE,
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000183 OPTION_WP_STATUS,
184 OPTION_WP_SET_RANGE,
185 OPTION_WP_SET_REGION,
186 OPTION_WP_ENABLE,
187 OPTION_WP_DISABLE,
188 OPTION_WP_LIST,
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300189 };
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000190 int ret = 0;
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000191 unsigned int wp_start = 0, wp_len = 0;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000192
Daniel Campelloce983bc2021-04-16 15:26:42 -0600193 static const char optstring[] = "r:Rw:v:nNVEfc:l:i:p:Lzho:x";
Mathias Krausea60faab2011-01-17 07:50:42 +0000194 static const struct option long_options[] = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000195 {"read", 1, NULL, 'r'},
196 {"write", 1, NULL, 'w'},
197 {"erase", 0, NULL, 'E'},
198 {"verify", 1, NULL, 'v'},
199 {"noverify", 0, NULL, 'n'},
Nico Huber99d15952016-05-02 16:54:24 +0200200 {"noverify-all", 0, NULL, 'N'},
Daniel Campelloce983bc2021-04-16 15:26:42 -0600201 {"extract", 0, NULL, 'x'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000202 {"chip", 1, NULL, 'c'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000203 {"verbose", 0, NULL, 'V'},
204 {"force", 0, NULL, 'f'},
205 {"layout", 1, NULL, 'l'},
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300206 {"ifd", 0, NULL, OPTION_IFD},
Arthur Heymansc82900b2018-01-10 12:48:16 +0100207 {"fmap", 0, NULL, OPTION_FMAP},
208 {"fmap-file", 1, NULL, OPTION_FMAP_FILE},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000209 {"image", 1, NULL, 'i'},
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300210 {"flash-contents", 1, NULL, OPTION_FLASH_CONTENTS},
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000211 {"flash-name", 0, NULL, OPTION_FLASH_NAME},
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000212 {"flash-size", 0, NULL, OPTION_FLASH_SIZE},
213 {"get-size", 0, NULL, OPTION_FLASH_SIZE}, // (deprecated): back compatibility.
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000214 {"wp-status", 0, 0, OPTION_WP_STATUS},
215 {"wp-range", required_argument, NULL, OPTION_WP_SET_RANGE},
216 {"wp-region", 1, 0, OPTION_WP_SET_REGION},
217 {"wp-enable", optional_argument, 0, OPTION_WP_ENABLE},
218 {"wp-disable", 0, 0, OPTION_WP_DISABLE},
219 {"wp-list", 0, 0, OPTION_WP_LIST},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000220 {"list-supported", 0, NULL, 'L'},
221 {"list-supported-wiki", 0, NULL, 'z'},
222 {"programmer", 1, NULL, 'p'},
223 {"help", 0, NULL, 'h'},
224 {"version", 0, NULL, 'R'},
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000225 {"output", 1, NULL, 'o'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000226 {NULL, 0, NULL, 0},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000227 };
228
229 char *filename = NULL;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300230 char *referencefile = NULL;
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000231 char *layoutfile = NULL;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100232 char *fmapfile = NULL;
Stefan Taunerb8911d62012-12-26 07:55:00 +0000233#ifndef STANDALONE
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000234 char *logfile = NULL;
Stefan Taunerb8911d62012-12-26 07:55:00 +0000235#endif /* !STANDALONE */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000236 char *tempstr = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000237 char *pparam = NULL;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100238 struct layout_include_args *include_args = NULL;
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000239 char *wp_mode_opt = NULL;
Daniel Campello7367f772021-04-19 18:43:09 -0600240 char *wp_region = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000241
Jack Rosenthal5f1524b2021-04-09 10:03:14 -0600242 /*
243 * Safety-guard against a user who has (mistakenly) closed
244 * stdout or stderr before exec'ing flashrom. We disable
245 * logging in this case to prevent writing log data to a flash
246 * chip when a flash device gets opened with fd 1 or 2.
247 */
248 if (check_file(stdout) && check_file(stderr)) {
249 flashrom_set_log_callback(
250 (flashrom_log_callback *)&flashrom_print_cb);
251 }
Nico Huber18781102012-12-10 13:34:12 +0000252
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000253 print_version();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000254 print_banner();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000255
256 if (selfcheck())
257 exit(1);
258
259 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000260 /* FIXME: Delay all operation_specified checks until after command
261 * line parsing to allow --help overriding everything else.
262 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000263 while ((opt = getopt_long(argc, argv, optstring,
264 long_options, &option_index)) != EOF) {
265 switch (opt) {
266 case 'r':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000267 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000268 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000269 read_it = 1;
270 break;
271 case 'w':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000272 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000273 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000274 write_it = 1;
275 break;
276 case 'v':
277 //FIXME: gracefully handle superfluous -v
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000278 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000279 if (dont_verify_it) {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000280 cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000281 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000282 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000283 verify_it = 1;
284 break;
285 case 'n':
286 if (verify_it) {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000287 cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000288 }
289 dont_verify_it = 1;
290 break;
Nico Huber99d15952016-05-02 16:54:24 +0200291 case 'N':
292 dont_verify_all = 1;
293 break;
Daniel Campelloce983bc2021-04-16 15:26:42 -0600294 case 'x':
295 cli_classic_validate_singleop(&operation_specified);
296 extract_it = 1;
297 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000298 case 'c':
299 chip_to_probe = strdup(optarg);
300 break;
301 case 'V':
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000302 verbose_screen++;
Nico Huberd152fb92017-06-19 12:57:10 +0200303 if (verbose_screen > FLASHROM_MSG_DEBUG2)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000304 verbose_logfile = verbose_screen;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000305 break;
306 case 'E':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000307 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000308 erase_it = 1;
309 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000310 case 'f':
311 force = 1;
312 break;
313 case 'l':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000314 if (layoutfile)
315 cli_classic_abort_usage("Error: --layout specified more than once. Aborting.\n");
316 if (ifd)
317 cli_classic_abort_usage("Error: --layout and --ifd both specified. Aborting.\n");
318 if (fmap)
319 cli_classic_abort_usage("Error: --layout and --fmap-file both specified. Aborting.\n");
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000320 layoutfile = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000321 break;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300322 case OPTION_IFD:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000323 if (layoutfile)
324 cli_classic_abort_usage("Error: --layout and --ifd both specified. Aborting.\n");
325 if (fmap)
326 cli_classic_abort_usage("Error: --fmap-file and --ifd both specified. Aborting.\n");
Nico Huber305f4172013-06-14 11:55:26 +0200327 ifd = 1;
328 break;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100329 case OPTION_FMAP_FILE:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000330 if (fmap)
331 cli_classic_abort_usage("Error: --fmap or --fmap-file specified "
Arthur Heymansc82900b2018-01-10 12:48:16 +0100332 "more than once. Aborting.\n");
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000333 if (ifd)
334 cli_classic_abort_usage("Error: --fmap-file and --ifd both specified. Aborting.\n");
335 if (layoutfile)
336 cli_classic_abort_usage("Error: --fmap-file and --layout both specified. Aborting.\n");
Arthur Heymansc82900b2018-01-10 12:48:16 +0100337 fmapfile = strdup(optarg);
338 fmap = 1;
339 break;
340 case OPTION_FMAP:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000341 if (fmap)
342 cli_classic_abort_usage("Error: --fmap or --fmap-file specified "
Arthur Heymansc82900b2018-01-10 12:48:16 +0100343 "more than once. Aborting.\n");
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000344 if (ifd)
345 cli_classic_abort_usage("Error: --fmap and --ifd both specified. Aborting.\n");
346 if (layoutfile)
347 cli_classic_abort_usage("Error: --layout and --fmap both specified. Aborting.\n");
Arthur Heymansc82900b2018-01-10 12:48:16 +0100348 fmap = 1;
349 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000350 case 'i':
Daniel Campello45d50a12021-04-13 10:47:25 -0600351 if (register_include_arg(&include_args, optarg))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000352 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000353 break;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300354 case OPTION_FLASH_CONTENTS:
Angel Ponse0fac2e2020-03-31 15:02:02 +0200355 if (referencefile)
356 cli_classic_abort_usage("Error: --flash-contents specified more than once."
357 "Aborting.\n");
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300358 referencefile = strdup(optarg);
359 break;
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000360 case OPTION_FLASH_NAME:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000361 cli_classic_validate_singleop(&operation_specified);
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000362 flash_name = 1;
363 break;
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000364 case OPTION_FLASH_SIZE:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000365 cli_classic_validate_singleop(&operation_specified);
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000366 flash_size = 1;
367 break;
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000368 case OPTION_WP_STATUS:
369 wp_status = 1;
370 break;
371 case OPTION_WP_LIST:
372 wp_list = 1;
373 break;
374 case OPTION_WP_SET_RANGE:
375 if (parse_wp_range(&wp_start, &wp_len) < 0)
376 cli_classic_abort_usage("Incorrect wp-range arguments provided.\n");
377
378 set_wp_range = 1;
379 break;
380 case OPTION_WP_ENABLE:
381 set_wp_enable = 1;
382 if (optarg)
383 wp_mode_opt = strdup(optarg);
384 break;
385 case OPTION_WP_DISABLE:
386 set_wp_disable = 1;
387 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000388 case 'L':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000389 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000390 list_supported = 1;
391 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000392 case 'z':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000393#if CONFIG_PRINT_WIKI == 1
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000394 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000395 list_supported_wiki = 1;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000396#else
Idwer Volleringa7855502020-12-08 00:24:39 +0100397 cli_classic_abort_usage("Error: Wiki output was not "
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000398 "compiled in. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000399#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000400 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000401 case 'p':
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000402 if (prog != PROGRAMMER_INVALID) {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000403 cli_classic_abort_usage("Error: --programmer specified "
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000404 "more than once. You can separate "
405 "multiple\nparameters for a programmer "
406 "with \",\". Please see the man page "
407 "for details.\n");
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000408 }
Thomas Heijligene14b2ec2021-05-19 14:12:18 +0200409 for (prog = 0; prog < programmer_table_size; prog++) {
Thomas Heijligendee884e2021-03-31 19:09:44 +0200410 name = programmer_table[prog]->name;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000411 namelen = strlen(name);
412 if (strncmp(optarg, name, namelen) == 0) {
413 switch (optarg[namelen]) {
414 case ':':
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000415 pparam = strdup(optarg + namelen + 1);
416 if (!strlen(pparam)) {
417 free(pparam);
418 pparam = NULL;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000419 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000420 break;
421 case '\0':
422 break;
423 default:
424 /* The continue refers to the
425 * for loop. It is here to be
426 * able to differentiate between
427 * foo and foobar.
428 */
429 continue;
430 }
431 break;
432 }
433 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000434 if (prog == PROGRAMMER_INVALID) {
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000435 fprintf(stderr, "Error: Unknown programmer \"%s\". Valid choices are:\n",
436 optarg);
437 list_programmers_linebreak(0, 80, 0);
Stefan Taunerb226cb12012-11-24 18:59:39 +0000438 msg_ginfo(".\n");
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000439 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000440 }
441 break;
442 case 'R':
443 /* print_version() is always called during startup. */
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000444 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000445 exit(0);
446 break;
447 case 'h':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000448 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000449 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000450 exit(0);
451 break;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000452 case 'o':
453#ifdef STANDALONE
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000454 cli_classic_abort_usage("Log file not supported in standalone mode. Aborting.\n");
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000455#else /* STANDALONE */
Elyes HAOUASef78de42019-07-18 15:08:10 +0200456 if (logfile) {
457 fprintf(stderr, "Warning: -o/--output specified multiple times.\n");
458 free(logfile);
459 }
460
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000461 logfile = strdup(optarg);
462 if (logfile[0] == '\0') {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000463 cli_classic_abort_usage("No log filename specified.\n");
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000464 }
465#endif /* STANDALONE */
466 break;
Daniel Campello7367f772021-04-19 18:43:09 -0600467 case OPTION_WP_SET_REGION:
468 set_wp_region = 1;
469 wp_region = strdup(optarg);
470 break;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000471 default:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000472 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000473 break;
474 }
475 }
476
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000477 if (optind < argc)
478 cli_classic_abort_usage("Error: Extra parameter found.\n");
479 if ((read_it | write_it | verify_it) && check_filename(filename, "image"))
480 cli_classic_abort_usage(NULL);
481 if (layoutfile && check_filename(layoutfile, "layout"))
482 cli_classic_abort_usage(NULL);
483 if (fmapfile && check_filename(fmapfile, "fmap"))
484 cli_classic_abort_usage(NULL);
485 if (referencefile && check_filename(referencefile, "reference"))
486 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000487
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000488#ifndef STANDALONE
489 if (logfile && check_filename(logfile, "log"))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000490 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000491 if (logfile && open_logfile(logfile))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000492 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000493#endif /* !STANDALONE */
494
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000495#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000496 if (list_supported_wiki) {
497 print_supported_wiki();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000498 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000499 }
500#endif
501
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000502 if (list_supported) {
Niklas Söderlundede2fa42012-10-23 13:06:46 +0000503 if (print_supported())
504 ret = 1;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000505 goto out;
506 }
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000507
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000508#ifndef STANDALONE
509 start_logging();
510#endif /* !STANDALONE */
511
512 print_buildinfo();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000513 msg_gdbg("Command line (%i args):", argc - 1);
514 for (i = 0; i < argc; i++) {
515 msg_gdbg(" %s", argv[i]);
516 }
517 msg_gdbg("\n");
518
519 if (layoutfile && read_romlayout(layoutfile)) {
520 ret = 1;
521 goto out;
522 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100523
Arthur Heymansb04fef92019-02-05 17:35:05 +0100524 if (!ifd && !fmap && process_include_args(get_global_layout(), include_args)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000525 ret = 1;
526 goto out;
527 }
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000528 /* Does a chip with the requested name exist in the flashchips array? */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000529 if (chip_to_probe) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000530 for (chip = flashchips; chip && chip->name; chip++)
531 if (!strcmp(chip->name, chip_to_probe))
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000532 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000533 if (!chip || !chip->name) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000534 msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe);
535 msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n");
536 ret = 1;
537 goto out;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000538 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000539 /* Keep chip around for later usage in case a forced read is requested. */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000540 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000541
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000542 if (prog == PROGRAMMER_INVALID) {
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000543 if (CONFIG_DEFAULT_PROGRAMMER != PROGRAMMER_INVALID) {
544 prog = CONFIG_DEFAULT_PROGRAMMER;
Stefan Tauner265fcac2014-06-02 00:12:23 +0000545 /* We need to strdup here because we free(pparam) unconditionally later. */
546 pparam = strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS);
547 msg_pinfo("Using default programmer \"%s\" with arguments \"%s\".\n",
Thomas Heijligendee884e2021-03-31 19:09:44 +0200548 programmer_table[CONFIG_DEFAULT_PROGRAMMER]->name, pparam);
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000549 } else {
550 msg_perr("Please select a programmer with the --programmer parameter.\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +0000551#if CONFIG_INTERNAL == 1
552 "To choose the mainboard of this computer use 'internal'. "
553#endif
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000554 "Valid choices are:\n");
555 list_programmers_linebreak(0, 80, 0);
Stefan Taunerb226cb12012-11-24 18:59:39 +0000556 msg_ginfo(".\n");
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000557 ret = 1;
558 goto out;
559 }
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000560 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000561
Carl-Daniel Hailfinger49884202010-05-22 07:10:46 +0000562 /* FIXME: Delay calibration should happen in programmer code. */
563 myusec_calibrate_delay();
564
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000565 if (programmer_init(prog, pparam)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000566 msg_perr("Error: Programmer initialization failed.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000567 ret = 1;
568 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000569 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000570 tempstr = flashbuses_to_text(get_buses_supported());
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000571 msg_pdbg("The following protocols are supported: %s.\n", tempstr);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000572 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000573
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000574 for (j = 0; j < registered_master_count; j++) {
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000575 startchip = 0;
Nico Huber519be662018-12-23 20:03:35 +0100576 while (chipcount < (int)ARRAY_SIZE(flashes)) {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000577 startchip = probe_flash(&registered_masters[j], startchip, &flashes[chipcount], 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000578 if (startchip == -1)
579 break;
580 chipcount++;
581 startchip++;
582 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000583 }
584
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000585 if (chipcount > 1) {
Stefan Tauner0554ca52013-07-25 22:54:25 +0000586 msg_cinfo("Multiple flash chip definitions match the detected chip(s): \"%s\"",
587 flashes[0].chip->name);
Stefan Tauner716e0982011-07-25 20:38:52 +0000588 for (i = 1; i < chipcount; i++)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000589 msg_cinfo(", \"%s\"", flashes[i].chip->name);
Stefan Tauner0554ca52013-07-25 22:54:25 +0000590 msg_cinfo("\nPlease specify which chip definition to use with the -c <chipname> option.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000591 ret = 1;
592 goto out_shutdown;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000593 } else if (!chipcount) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000594 msg_cinfo("No EEPROM/flash device found.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000595 if (!force || !chip_to_probe) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000596 msg_cinfo("Note: flashrom can never write if the flash chip isn't found "
597 "automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000598 }
599 if (force && read_it && chip_to_probe) {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000600 struct registered_master *mst;
601 int compatible_masters = 0;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000602 msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000603 /* This loop just counts compatible controllers. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000604 for (j = 0; j < registered_master_count; j++) {
605 mst = &registered_masters[j];
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000606 /* chip is still set from the chip_to_probe earlier in this function. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000607 if (mst->buses_supported & chip->bustype)
608 compatible_masters++;
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000609 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000610 if (!compatible_masters) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000611 msg_cinfo("No compatible controller found for the requested flash chip.\n");
612 ret = 1;
613 goto out_shutdown;
614 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000615 if (compatible_masters > 1)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000616 msg_cinfo("More than one compatible controller found for the requested flash "
617 "chip, using the first one.\n");
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000618 for (j = 0; j < registered_master_count; j++) {
619 mst = &registered_masters[j];
620 startchip = probe_flash(mst, 0, &flashes[0], 1);
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000621 if (startchip != -1)
622 break;
623 }
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000624 if (startchip == -1) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000625 // FIXME: This should never happen! Ask for a bug report?
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000626 msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000627 ret = 1;
628 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000629 }
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000630 if (map_flash(&flashes[0]) != 0) {
631 free(flashes[0].chip);
632 ret = 1;
633 goto out_shutdown;
634 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000635 msg_cinfo("Please note that forced reads most likely contain garbage.\n");
636 ret = read_flash_to_file(&flashes[0], filename);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000637 unmap_flash(&flashes[0]);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000638 free(flashes[0].chip);
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000639 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000640 }
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000641 ret = 1;
642 goto out_shutdown;
Stefan Tauner1d947632011-09-11 22:08:58 +0000643 } else if (!chip_to_probe) {
644 /* repeat for convenience when looking at foreign logs */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000645 tempstr = flashbuses_to_text(flashes[0].chip->bustype);
Stefan Tauner1d947632011-09-11 22:08:58 +0000646 msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000647 flashes[0].chip->vendor, flashes[0].chip->name, flashes[0].chip->total_size, tempstr);
Stefan Tauner1d947632011-09-11 22:08:58 +0000648 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000649 }
650
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000651 fill_flash = &flashes[0];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000652
Stefan Tauner9b32de92014-08-08 23:52:33 +0000653 print_chip_support_status(fill_flash->chip);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000654
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000655 unsigned int limitexceeded = count_max_decode_exceedings(fill_flash);
656 if (limitexceeded > 0 && !force) {
657 enum chipbustype commonbuses = fill_flash->mst->buses_supported & fill_flash->chip->bustype;
658
659 /* Sometimes chip and programmer have more than one bus in common,
660 * and the limit is not exceeded on all buses. Tell the user. */
661 if ((bitcount(commonbuses) > limitexceeded)) {
662 msg_pdbg("There is at least one interface available which could support the size of\n"
663 "the selected flash chip.\n");
664 }
665 msg_cerr("This flash chip is too big for this programmer (--verbose/-V gives details).\n"
666 "Use --force/-f to override at your own risk.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000667 ret = 1;
668 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000669 }
670
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000671 if (!(read_it | write_it | verify_it | erase_it | flash_name | flash_size
672 | set_wp_range | set_wp_region | set_wp_enable |
Daniel Campelloce983bc2021-04-16 15:26:42 -0600673 set_wp_disable | wp_status | wp_list | extract_it)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000674 msg_ginfo("No operations were specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000675 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000676 }
677
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000678 if (set_wp_enable && set_wp_disable) {
679 msg_ginfo("Error: --wp-enable and --wp-disable are mutually exclusive\n");
680 ret = 1;
681 goto out_shutdown;
682 }
683 if (set_wp_range && set_wp_region) {
684 msg_gerr("Error: Cannot use both --wp-range and --wp-region simultaneously.\n");
685 ret = 1;
686 goto out_shutdown;
687 }
688
Daniel Campello65099122021-04-19 18:37:56 -0600689 struct wp *wp = fill_flash->chip->wp;
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000690 if (set_wp_range || set_wp_region) {
Daniel Campello65099122021-04-19 18:37:56 -0600691 if (!wp || !wp->set_range) {
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000692 msg_gerr("Error: write protect is not supported on this flash chip.\n");
693 ret = 1;
694 goto out_shutdown;
695 }
696 }
697
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000698 if (flash_name) {
699 if (fill_flash->chip->vendor && fill_flash->chip->name) {
700 printf("vendor=\"%s\" name=\"%s\"\n",
701 fill_flash->chip->vendor,
702 fill_flash->chip->name);
703 } else {
704 ret = -1;
705 }
706 goto out_shutdown;
707 }
708
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000709 if (flash_size) {
710 printf("%d\n", fill_flash->chip->total_size * 1024);
711 goto out_shutdown;
712 }
713
Nico Huber305f4172013-06-14 11:55:26 +0200714 if (layoutfile) {
715 layout = get_global_layout();
716 } else if (ifd && (flashrom_layout_read_from_ifd(&layout, fill_flash, NULL, 0) ||
Arthur Heymansb04fef92019-02-05 17:35:05 +0100717 process_include_args(layout, include_args))) {
Nico Huber305f4172013-06-14 11:55:26 +0200718 ret = 1;
719 goto out_shutdown;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100720 } else if (fmap && fmapfile) {
721 struct stat s;
722 if (stat(fmapfile, &s) != 0) {
723 msg_gerr("Failed to stat fmapfile \"%s\"\n", fmapfile);
724 ret = 1;
725 goto out_shutdown;
726 }
727
728 size_t fmapfile_size = s.st_size;
729 uint8_t *fmapfile_buffer = malloc(fmapfile_size);
730 if (!fmapfile_buffer) {
731 ret = 1;
732 goto out_shutdown;
733 }
734
735 if (read_buf_from_file(fmapfile_buffer, fmapfile_size, fmapfile)) {
736 ret = 1;
737 free(fmapfile_buffer);
738 goto out_shutdown;
739 }
740
741 if (flashrom_layout_read_fmap_from_buffer(&layout, fill_flash, fmapfile_buffer, fmapfile_size) ||
Arthur Heymansb04fef92019-02-05 17:35:05 +0100742 process_include_args(layout, include_args)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100743 ret = 1;
744 free(fmapfile_buffer);
745 goto out_shutdown;
746 }
747 free(fmapfile_buffer);
748 } else if (fmap && (flashrom_layout_read_fmap_from_rom(&layout, fill_flash, 0,
Arthur Heymansb04fef92019-02-05 17:35:05 +0100749 fill_flash->chip->total_size * 1024) || process_include_args(layout, include_args))) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100750 ret = 1;
751 goto out_shutdown;
Nico Huber305f4172013-06-14 11:55:26 +0200752 }
Nico Huber305f4172013-06-14 11:55:26 +0200753 flashrom_layout_set(fill_flash, layout);
Daniel Campello65099122021-04-19 18:37:56 -0600754
755 if (wp_status) {
756 if (wp && wp->wp_status) {
757 ret |= wp->wp_status(fill_flash);
758 } else {
759 msg_gerr("Error: write protect is not supported on this flash chip.\n");
760 ret = 1;
761 }
762 goto out_release;
763 }
764
765 /* Note: set_wp_disable should be done before setting the range */
766 if (set_wp_disable) {
767 if (wp && wp->disable) {
768 ret |= wp->disable(fill_flash);
769 } else {
770 msg_gerr("Error: write protect is not supported on this flash chip.\n");
771 ret = 1;
772 goto out_release;
773 }
774 }
775
776 /* Note: set_wp_range must happen before set_wp_enable */
777 if (set_wp_range) {
778 ret |= wp->set_range(fill_flash, wp_start, wp_len);
779 }
780
Daniel Campello7367f772021-04-19 18:43:09 -0600781 if (set_wp_region && wp_region) {
782 if (get_region_range(layout, wp_region, &wp_start, &wp_len)) {
783 ret = 1;
784 goto out_release;
785 }
786 ret |= wp->set_range(fill_flash, wp_start, wp_len);
787 free(wp_region);
788 }
789
Daniel Campello65099122021-04-19 18:37:56 -0600790 if (!ret && set_wp_enable) {
791 enum wp_mode wp_mode;
792
793 if (wp_mode_opt)
794 wp_mode = get_wp_mode(wp_mode_opt);
795 else
796 wp_mode = WP_MODE_HARDWARE; /* default */
797
798 if (wp_mode == WP_MODE_UNKNOWN) {
799 msg_gerr("Error: Invalid WP mode: \"%s\"\n", wp_mode_opt);
800 ret = 1;
801 goto out_release;
802 }
803
804 if (wp && wp->enable) {
805 ret |= wp->enable(fill_flash, wp_mode);
806 } else {
807 msg_gerr("Error: write protect is not supported on this flash chip.\n");
808 ret = 1;
809 goto out_release;
810 }
811 }
812
813 if (wp_list) {
814 msg_ginfo("Valid write protection ranges:\n");
815 if (wp && wp->list_ranges) {
816 ret |= wp->list_ranges(fill_flash);
817 } else {
818 msg_gerr("Error: write protect is not supported on this flash chip.\n");
819 ret = 1;
820 }
821 goto out_release;
822 }
823
Nico Huber899e4ec2016-04-29 18:39:01 +0200824 flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE, !!force);
Urja Rannikko7258cf52017-06-17 11:31:57 +0300825#if CONFIG_INTERNAL == 1
Nico Huber899e4ec2016-04-29 18:39:01 +0200826 flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE_BOARDMISMATCH, !!force_boardmismatch);
Urja Rannikko7258cf52017-06-17 11:31:57 +0300827#endif
Nico Huber899e4ec2016-04-29 18:39:01 +0200828 flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_AFTER_WRITE, !dont_verify_it);
Nico Huber99d15952016-05-02 16:54:24 +0200829 flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_WHOLE_CHIP, !dont_verify_all);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000830
Carl-Daniel Hailfinger9ad42552010-09-15 10:20:16 +0000831 /* FIXME: We should issue an unconditional chip reset here. This can be
832 * done once we have a .reset function in struct flashchip.
833 * Give the chip time to settle.
834 */
835 programmer_delay(100000);
Nico Huber899e4ec2016-04-29 18:39:01 +0200836 if (read_it)
837 ret = do_read(fill_flash, filename);
Daniel Campelloce983bc2021-04-16 15:26:42 -0600838 else if (extract_it)
839 ret = do_extract(fill_flash);
Nico Huber899e4ec2016-04-29 18:39:01 +0200840 else if (erase_it)
841 ret = do_erase(fill_flash);
842 else if (write_it)
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300843 ret = do_write(fill_flash, filename, referencefile);
Nico Huber899e4ec2016-04-29 18:39:01 +0200844 else if (verify_it)
845 ret = do_verify(fill_flash, filename);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000846
Daniel Campello65099122021-04-19 18:37:56 -0600847out_release:
Nico Huber305f4172013-06-14 11:55:26 +0200848 flashrom_layout_release(layout);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000849out_shutdown:
850 programmer_shutdown();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000851out:
Stefan Taunerb8911d62012-12-26 07:55:00 +0000852 for (i = 0; i < chipcount; i++)
853 free(flashes[i].chip);
854
Arthur Heymansb04fef92019-02-05 17:35:05 +0100855 layout_cleanup(&include_args);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000856 free(filename);
Richard Hughes6eca7612018-12-19 15:40:27 +0000857 free(fmapfile);
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300858 free(referencefile);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000859 free(layoutfile);
860 free(pparam);
861 /* clean up global variables */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000862 free((char *)chip_to_probe); /* Silence! Freeing is not modifying contents. */
Stefan Taunerb8911d62012-12-26 07:55:00 +0000863 chip_to_probe = NULL;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000864#ifndef STANDALONE
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000865 free(logfile);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000866 ret |= close_logfile();
867#endif /* !STANDALONE */
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000868 return ret;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000869}