blob: 158110b9a8e1d1be0b39a2c5cfd07ed5bcfa8279 [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;
Thomas Heijligenbf0396a2021-06-01 14:51:13 +0200175 static const struct programmer_entry *prog = NULL;
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':
Thomas Heijligenbf0396a2021-06-01 14:51:13 +0200402 if (prog != NULL) {
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 Heijligenbf0396a2021-06-01 14:51:13 +0200409 size_t p;
410 for (p = 0; p < programmer_table_size; p++) {
411 name = programmer_table[p]->name;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000412 namelen = strlen(name);
413 if (strncmp(optarg, name, namelen) == 0) {
414 switch (optarg[namelen]) {
415 case ':':
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000416 pparam = strdup(optarg + namelen + 1);
417 if (!strlen(pparam)) {
418 free(pparam);
419 pparam = NULL;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000420 }
Thomas Heijligenbf0396a2021-06-01 14:51:13 +0200421 prog = programmer_table[p];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000422 break;
423 case '\0':
Thomas Heijligenbf0396a2021-06-01 14:51:13 +0200424 prog = programmer_table[p];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000425 break;
426 default:
427 /* The continue refers to the
428 * for loop. It is here to be
429 * able to differentiate between
430 * foo and foobar.
431 */
432 continue;
433 }
434 break;
435 }
436 }
Thomas Heijligenbf0396a2021-06-01 14:51:13 +0200437 if (prog == NULL) {
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000438 fprintf(stderr, "Error: Unknown programmer \"%s\". Valid choices are:\n",
439 optarg);
440 list_programmers_linebreak(0, 80, 0);
Stefan Taunerb226cb12012-11-24 18:59:39 +0000441 msg_ginfo(".\n");
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000442 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000443 }
444 break;
445 case 'R':
446 /* print_version() is always called during startup. */
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000447 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000448 exit(0);
449 break;
450 case 'h':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000451 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000452 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000453 exit(0);
454 break;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000455 case 'o':
456#ifdef STANDALONE
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000457 cli_classic_abort_usage("Log file not supported in standalone mode. Aborting.\n");
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000458#else /* STANDALONE */
Elyes HAOUASef78de42019-07-18 15:08:10 +0200459 if (logfile) {
460 fprintf(stderr, "Warning: -o/--output specified multiple times.\n");
461 free(logfile);
462 }
463
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000464 logfile = strdup(optarg);
465 if (logfile[0] == '\0') {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000466 cli_classic_abort_usage("No log filename specified.\n");
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000467 }
468#endif /* STANDALONE */
469 break;
Daniel Campello7367f772021-04-19 18:43:09 -0600470 case OPTION_WP_SET_REGION:
471 set_wp_region = 1;
472 wp_region = strdup(optarg);
473 break;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000474 default:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000475 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000476 break;
477 }
478 }
479
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000480 if (optind < argc)
481 cli_classic_abort_usage("Error: Extra parameter found.\n");
482 if ((read_it | write_it | verify_it) && check_filename(filename, "image"))
483 cli_classic_abort_usage(NULL);
484 if (layoutfile && check_filename(layoutfile, "layout"))
485 cli_classic_abort_usage(NULL);
486 if (fmapfile && check_filename(fmapfile, "fmap"))
487 cli_classic_abort_usage(NULL);
488 if (referencefile && check_filename(referencefile, "reference"))
489 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000490
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000491#ifndef STANDALONE
492 if (logfile && check_filename(logfile, "log"))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000493 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000494 if (logfile && open_logfile(logfile))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000495 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000496#endif /* !STANDALONE */
497
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000498#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000499 if (list_supported_wiki) {
500 print_supported_wiki();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000501 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000502 }
503#endif
504
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000505 if (list_supported) {
Niklas Söderlundede2fa42012-10-23 13:06:46 +0000506 if (print_supported())
507 ret = 1;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000508 goto out;
509 }
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000510
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000511#ifndef STANDALONE
512 start_logging();
513#endif /* !STANDALONE */
514
515 print_buildinfo();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000516 msg_gdbg("Command line (%i args):", argc - 1);
517 for (i = 0; i < argc; i++) {
518 msg_gdbg(" %s", argv[i]);
519 }
520 msg_gdbg("\n");
521
522 if (layoutfile && read_romlayout(layoutfile)) {
523 ret = 1;
524 goto out;
525 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100526
Arthur Heymansb04fef92019-02-05 17:35:05 +0100527 if (!ifd && !fmap && process_include_args(get_global_layout(), include_args)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000528 ret = 1;
529 goto out;
530 }
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000531 /* Does a chip with the requested name exist in the flashchips array? */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000532 if (chip_to_probe) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000533 for (chip = flashchips; chip && chip->name; chip++)
534 if (!strcmp(chip->name, chip_to_probe))
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000535 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000536 if (!chip || !chip->name) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000537 msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe);
538 msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n");
539 ret = 1;
540 goto out;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000541 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000542 /* Keep chip around for later usage in case a forced read is requested. */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000543 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000544
Thomas Heijligenbf0396a2021-06-01 14:51:13 +0200545 if (prog == NULL) {
Thomas Heijligen732aafd2021-06-01 16:22:14 +0200546 const struct programmer_entry *const default_programmer = CONFIG_DEFAULT_PROGRAMMER_NAME;
547
548 if (default_programmer) {
549 prog = default_programmer;
Stefan Tauner265fcac2014-06-02 00:12:23 +0000550 /* We need to strdup here because we free(pparam) unconditionally later. */
551 pparam = strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS);
552 msg_pinfo("Using default programmer \"%s\" with arguments \"%s\".\n",
Thomas Heijligen732aafd2021-06-01 16:22:14 +0200553 default_programmer->name, pparam);
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000554 } else {
555 msg_perr("Please select a programmer with the --programmer parameter.\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +0000556#if CONFIG_INTERNAL == 1
557 "To choose the mainboard of this computer use 'internal'. "
558#endif
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000559 "Valid choices are:\n");
560 list_programmers_linebreak(0, 80, 0);
Stefan Taunerb226cb12012-11-24 18:59:39 +0000561 msg_ginfo(".\n");
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000562 ret = 1;
563 goto out;
564 }
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000565 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000566
Carl-Daniel Hailfinger49884202010-05-22 07:10:46 +0000567 /* FIXME: Delay calibration should happen in programmer code. */
568 myusec_calibrate_delay();
569
Thomas Heijligenbf0396a2021-06-01 14:51:13 +0200570 if (programmer_init(prog, pparam)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000571 msg_perr("Error: Programmer initialization failed.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000572 ret = 1;
573 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000574 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000575 tempstr = flashbuses_to_text(get_buses_supported());
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000576 msg_pdbg("The following protocols are supported: %s.\n", tempstr);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000577 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000578
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000579 for (j = 0; j < registered_master_count; j++) {
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000580 startchip = 0;
Nico Huber519be662018-12-23 20:03:35 +0100581 while (chipcount < (int)ARRAY_SIZE(flashes)) {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000582 startchip = probe_flash(&registered_masters[j], startchip, &flashes[chipcount], 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000583 if (startchip == -1)
584 break;
585 chipcount++;
586 startchip++;
587 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000588 }
589
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000590 if (chipcount > 1) {
Stefan Tauner0554ca52013-07-25 22:54:25 +0000591 msg_cinfo("Multiple flash chip definitions match the detected chip(s): \"%s\"",
592 flashes[0].chip->name);
Stefan Tauner716e0982011-07-25 20:38:52 +0000593 for (i = 1; i < chipcount; i++)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000594 msg_cinfo(", \"%s\"", flashes[i].chip->name);
Stefan Tauner0554ca52013-07-25 22:54:25 +0000595 msg_cinfo("\nPlease specify which chip definition to use with the -c <chipname> option.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000596 ret = 1;
597 goto out_shutdown;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000598 } else if (!chipcount) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000599 msg_cinfo("No EEPROM/flash device found.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000600 if (!force || !chip_to_probe) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000601 msg_cinfo("Note: flashrom can never write if the flash chip isn't found "
602 "automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000603 }
604 if (force && read_it && chip_to_probe) {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000605 struct registered_master *mst;
606 int compatible_masters = 0;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000607 msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000608 /* This loop just counts compatible controllers. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000609 for (j = 0; j < registered_master_count; j++) {
610 mst = &registered_masters[j];
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000611 /* chip is still set from the chip_to_probe earlier in this function. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000612 if (mst->buses_supported & chip->bustype)
613 compatible_masters++;
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000614 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000615 if (!compatible_masters) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000616 msg_cinfo("No compatible controller found for the requested flash chip.\n");
617 ret = 1;
618 goto out_shutdown;
619 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000620 if (compatible_masters > 1)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000621 msg_cinfo("More than one compatible controller found for the requested flash "
622 "chip, using the first one.\n");
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000623 for (j = 0; j < registered_master_count; j++) {
624 mst = &registered_masters[j];
625 startchip = probe_flash(mst, 0, &flashes[0], 1);
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000626 if (startchip != -1)
627 break;
628 }
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000629 if (startchip == -1) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000630 // FIXME: This should never happen! Ask for a bug report?
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000631 msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000632 ret = 1;
633 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000634 }
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000635 if (map_flash(&flashes[0]) != 0) {
636 free(flashes[0].chip);
637 ret = 1;
638 goto out_shutdown;
639 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000640 msg_cinfo("Please note that forced reads most likely contain garbage.\n");
641 ret = read_flash_to_file(&flashes[0], filename);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000642 unmap_flash(&flashes[0]);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000643 free(flashes[0].chip);
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000644 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000645 }
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000646 ret = 1;
647 goto out_shutdown;
Stefan Tauner1d947632011-09-11 22:08:58 +0000648 } else if (!chip_to_probe) {
649 /* repeat for convenience when looking at foreign logs */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000650 tempstr = flashbuses_to_text(flashes[0].chip->bustype);
Stefan Tauner1d947632011-09-11 22:08:58 +0000651 msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000652 flashes[0].chip->vendor, flashes[0].chip->name, flashes[0].chip->total_size, tempstr);
Stefan Tauner1d947632011-09-11 22:08:58 +0000653 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000654 }
655
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000656 fill_flash = &flashes[0];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000657
Stefan Tauner9b32de92014-08-08 23:52:33 +0000658 print_chip_support_status(fill_flash->chip);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000659
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000660 unsigned int limitexceeded = count_max_decode_exceedings(fill_flash);
661 if (limitexceeded > 0 && !force) {
662 enum chipbustype commonbuses = fill_flash->mst->buses_supported & fill_flash->chip->bustype;
663
664 /* Sometimes chip and programmer have more than one bus in common,
665 * and the limit is not exceeded on all buses. Tell the user. */
666 if ((bitcount(commonbuses) > limitexceeded)) {
667 msg_pdbg("There is at least one interface available which could support the size of\n"
668 "the selected flash chip.\n");
669 }
670 msg_cerr("This flash chip is too big for this programmer (--verbose/-V gives details).\n"
671 "Use --force/-f to override at your own risk.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000672 ret = 1;
673 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000674 }
675
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000676 if (!(read_it | write_it | verify_it | erase_it | flash_name | flash_size
677 | set_wp_range | set_wp_region | set_wp_enable |
Daniel Campelloce983bc2021-04-16 15:26:42 -0600678 set_wp_disable | wp_status | wp_list | extract_it)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000679 msg_ginfo("No operations were specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000680 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000681 }
682
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000683 if (set_wp_enable && set_wp_disable) {
684 msg_ginfo("Error: --wp-enable and --wp-disable are mutually exclusive\n");
685 ret = 1;
686 goto out_shutdown;
687 }
688 if (set_wp_range && set_wp_region) {
689 msg_gerr("Error: Cannot use both --wp-range and --wp-region simultaneously.\n");
690 ret = 1;
691 goto out_shutdown;
692 }
693
Daniel Campello65099122021-04-19 18:37:56 -0600694 struct wp *wp = fill_flash->chip->wp;
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000695 if (set_wp_range || set_wp_region) {
Daniel Campello65099122021-04-19 18:37:56 -0600696 if (!wp || !wp->set_range) {
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +1000697 msg_gerr("Error: write protect is not supported on this flash chip.\n");
698 ret = 1;
699 goto out_shutdown;
700 }
701 }
702
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000703 if (flash_name) {
704 if (fill_flash->chip->vendor && fill_flash->chip->name) {
705 printf("vendor=\"%s\" name=\"%s\"\n",
706 fill_flash->chip->vendor,
707 fill_flash->chip->name);
708 } else {
709 ret = -1;
710 }
711 goto out_shutdown;
712 }
713
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000714 if (flash_size) {
715 printf("%d\n", fill_flash->chip->total_size * 1024);
716 goto out_shutdown;
717 }
718
Nico Huber305f4172013-06-14 11:55:26 +0200719 if (layoutfile) {
720 layout = get_global_layout();
721 } else if (ifd && (flashrom_layout_read_from_ifd(&layout, fill_flash, NULL, 0) ||
Arthur Heymansb04fef92019-02-05 17:35:05 +0100722 process_include_args(layout, include_args))) {
Nico Huber305f4172013-06-14 11:55:26 +0200723 ret = 1;
724 goto out_shutdown;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100725 } else if (fmap && fmapfile) {
726 struct stat s;
727 if (stat(fmapfile, &s) != 0) {
728 msg_gerr("Failed to stat fmapfile \"%s\"\n", fmapfile);
729 ret = 1;
730 goto out_shutdown;
731 }
732
733 size_t fmapfile_size = s.st_size;
734 uint8_t *fmapfile_buffer = malloc(fmapfile_size);
735 if (!fmapfile_buffer) {
736 ret = 1;
737 goto out_shutdown;
738 }
739
740 if (read_buf_from_file(fmapfile_buffer, fmapfile_size, fmapfile)) {
741 ret = 1;
742 free(fmapfile_buffer);
743 goto out_shutdown;
744 }
745
746 if (flashrom_layout_read_fmap_from_buffer(&layout, fill_flash, fmapfile_buffer, fmapfile_size) ||
Arthur Heymansb04fef92019-02-05 17:35:05 +0100747 process_include_args(layout, include_args)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100748 ret = 1;
749 free(fmapfile_buffer);
750 goto out_shutdown;
751 }
752 free(fmapfile_buffer);
753 } else if (fmap && (flashrom_layout_read_fmap_from_rom(&layout, fill_flash, 0,
Arthur Heymansb04fef92019-02-05 17:35:05 +0100754 fill_flash->chip->total_size * 1024) || process_include_args(layout, include_args))) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100755 ret = 1;
756 goto out_shutdown;
Nico Huber305f4172013-06-14 11:55:26 +0200757 }
Nico Huber305f4172013-06-14 11:55:26 +0200758 flashrom_layout_set(fill_flash, layout);
Daniel Campello65099122021-04-19 18:37:56 -0600759
760 if (wp_status) {
761 if (wp && wp->wp_status) {
762 ret |= wp->wp_status(fill_flash);
763 } else {
764 msg_gerr("Error: write protect is not supported on this flash chip.\n");
765 ret = 1;
766 }
767 goto out_release;
768 }
769
770 /* Note: set_wp_disable should be done before setting the range */
771 if (set_wp_disable) {
772 if (wp && wp->disable) {
773 ret |= wp->disable(fill_flash);
774 } else {
775 msg_gerr("Error: write protect is not supported on this flash chip.\n");
776 ret = 1;
777 goto out_release;
778 }
779 }
780
781 /* Note: set_wp_range must happen before set_wp_enable */
782 if (set_wp_range) {
783 ret |= wp->set_range(fill_flash, wp_start, wp_len);
784 }
785
Daniel Campello7367f772021-04-19 18:43:09 -0600786 if (set_wp_region && wp_region) {
787 if (get_region_range(layout, wp_region, &wp_start, &wp_len)) {
788 ret = 1;
789 goto out_release;
790 }
791 ret |= wp->set_range(fill_flash, wp_start, wp_len);
792 free(wp_region);
793 }
794
Daniel Campello65099122021-04-19 18:37:56 -0600795 if (!ret && set_wp_enable) {
796 enum wp_mode wp_mode;
797
798 if (wp_mode_opt)
799 wp_mode = get_wp_mode(wp_mode_opt);
800 else
801 wp_mode = WP_MODE_HARDWARE; /* default */
802
803 if (wp_mode == WP_MODE_UNKNOWN) {
804 msg_gerr("Error: Invalid WP mode: \"%s\"\n", wp_mode_opt);
805 ret = 1;
806 goto out_release;
807 }
808
809 if (wp && wp->enable) {
810 ret |= wp->enable(fill_flash, wp_mode);
811 } else {
812 msg_gerr("Error: write protect is not supported on this flash chip.\n");
813 ret = 1;
814 goto out_release;
815 }
816 }
817
818 if (wp_list) {
819 msg_ginfo("Valid write protection ranges:\n");
820 if (wp && wp->list_ranges) {
821 ret |= wp->list_ranges(fill_flash);
822 } else {
823 msg_gerr("Error: write protect is not supported on this flash chip.\n");
824 ret = 1;
825 }
826 goto out_release;
827 }
828
Nico Huber899e4ec2016-04-29 18:39:01 +0200829 flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE, !!force);
Urja Rannikko7258cf52017-06-17 11:31:57 +0300830#if CONFIG_INTERNAL == 1
Nico Huber899e4ec2016-04-29 18:39:01 +0200831 flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE_BOARDMISMATCH, !!force_boardmismatch);
Urja Rannikko7258cf52017-06-17 11:31:57 +0300832#endif
Nico Huber899e4ec2016-04-29 18:39:01 +0200833 flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_AFTER_WRITE, !dont_verify_it);
Nico Huber99d15952016-05-02 16:54:24 +0200834 flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_WHOLE_CHIP, !dont_verify_all);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000835
Carl-Daniel Hailfinger9ad42552010-09-15 10:20:16 +0000836 /* FIXME: We should issue an unconditional chip reset here. This can be
837 * done once we have a .reset function in struct flashchip.
838 * Give the chip time to settle.
839 */
840 programmer_delay(100000);
Nico Huber899e4ec2016-04-29 18:39:01 +0200841 if (read_it)
842 ret = do_read(fill_flash, filename);
Daniel Campelloce983bc2021-04-16 15:26:42 -0600843 else if (extract_it)
844 ret = do_extract(fill_flash);
Nico Huber899e4ec2016-04-29 18:39:01 +0200845 else if (erase_it)
846 ret = do_erase(fill_flash);
847 else if (write_it)
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300848 ret = do_write(fill_flash, filename, referencefile);
Nico Huber899e4ec2016-04-29 18:39:01 +0200849 else if (verify_it)
850 ret = do_verify(fill_flash, filename);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000851
Daniel Campello65099122021-04-19 18:37:56 -0600852out_release:
Nico Huber305f4172013-06-14 11:55:26 +0200853 flashrom_layout_release(layout);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000854out_shutdown:
855 programmer_shutdown();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000856out:
Stefan Taunerb8911d62012-12-26 07:55:00 +0000857 for (i = 0; i < chipcount; i++)
858 free(flashes[i].chip);
859
Arthur Heymansb04fef92019-02-05 17:35:05 +0100860 layout_cleanup(&include_args);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000861 free(filename);
Richard Hughes6eca7612018-12-19 15:40:27 +0000862 free(fmapfile);
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300863 free(referencefile);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000864 free(layoutfile);
865 free(pparam);
866 /* clean up global variables */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000867 free((char *)chip_to_probe); /* Silence! Freeing is not modifying contents. */
Stefan Taunerb8911d62012-12-26 07:55:00 +0000868 chip_to_probe = NULL;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000869#ifndef STANDALONE
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000870 free(logfile);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000871 ret |= close_logfile();
872#endif /* !STANDALONE */
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000873 return ret;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000874}