blob: 2621219c76cd4f2f1eee602b637aa96666a2749c [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
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000020#include <stdio.h>
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000021#include <fcntl.h>
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000022#include <sys/stat.h>
23#include <string.h>
Jack Rosenthal85777562021-04-09 10:03:14 -060024#include <stdbool.h>
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000025#include <stdlib.h>
26#include <getopt.h>
27#include "flash.h"
28#include "flashchips.h"
Arthur Heymansc82900b2018-01-10 12:48:16 +010029#include "fmap.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000030#include "programmer.h"
Nico Huberc3b02dc2023-08-12 01:13:45 +020031#include "libflashprog.h"
Nico Huber34e783a2023-02-11 00:30:27 +010032#include "cli.h"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000033
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000034static void cli_classic_usage(const char *name)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000035{
Stefan Taunerb226cb12012-11-24 18:59:39 +000036 printf("Usage: %s [-h|-R|-L|"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000037#if CONFIG_PRINT_WIKI == 1
Stefan Taunerb226cb12012-11-24 18:59:39 +000038 "-z|"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000039#endif
Edward O'Callaghan16ec45c2019-10-04 20:24:53 +100040 "\n\t-p <programmername>[:<parameters>] [-c <chipname>]\n"
41 "\t\t(--flash-name|--flash-size|\n"
42 "\t\t [-E|(-r|-w|-v) <file>]\n"
43 "\t\t [(-l <layoutfile>|--ifd| --fmap|--fmap-file <file>) [-i <imagename>]...]\n"
44 "\t\t [-n] [-N] [-f])]\n"
45 "\t[-V[V[V]]] [-o <logfile>]\n\n", name);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000046
Stefan Taunerb226cb12012-11-24 18:59:39 +000047 printf(" -h | --help print this help text\n"
48 " -R | --version print version (release)\n"
49 " -r | --read <file> read flash and save to <file>\n"
Daniel Campellod12b6bc2022-03-14 11:43:16 -060050 " -w | --write (<file>|-) write <file> or the content provided\n"
Daniel Campello8eaef7d2021-04-15 10:36:04 -060051 " on the standard input to flash\n"
Daniel Campellod12b6bc2022-03-14 11:43:16 -060052 " -v | --verify (<file>|-) verify flash against <file>\n"
Daniel Campello8eaef7d2021-04-15 10:36:04 -060053 " or the content provided on the standard input\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000054 " -E | --erase erase flash memory\n"
55 " -V | --verbose more verbose output\n"
56 " -c | --chip <chipname> probe only for specified flash chip\n"
57 " -f | --force force specific operations (see man page)\n"
58 " -n | --noverify don't auto-verify\n"
Nico Huber99d15952016-05-02 16:54:24 +020059 " -N | --noverify-all verify included regions only (cf. -i)\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000060 " -l | --layout <layoutfile> read ROM layout from <layoutfile>\n"
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +100061 " --flash-name read out the detected flash name\n"
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +100062 " --flash-size read out the detected flash size\n"
Arthur Heymansc82900b2018-01-10 12:48:16 +010063 " --fmap read ROM layout from fmap embedded in ROM\n"
64 " --fmap-file <fmapfile> read ROM layout from fmap in <fmapfile>\n"
Nico Huber305f4172013-06-14 11:55:26 +020065 " --ifd read layout from an Intel Firmware Descriptor\n"
Anastasia Klimchuka7cb7e92022-11-25 18:10:43 +110066 " -i | --include <region> only read/write image <region> from layout\n"
67 " --image <region> deprecated, please use --include\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000068 " -o | --output <logfile> log output to <logfile>\n"
Paul Kocialkowskif701f342018-01-15 01:10:36 +030069 " --flash-contents <ref-file> assume flash contents to be <ref-file>\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000070 " -L | --list-supported print supported devices\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000071#if CONFIG_PRINT_WIKI == 1
Stefan Taunerb226cb12012-11-24 18:59:39 +000072 " -z | --list-supported-wiki print supported devices in wiki syntax\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000073#endif
Richard Hughes842d6782021-01-15 09:48:12 +000074 " --progress show progress percentage on the standard output\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000075 " -p | --programmer <name>[:<param>] specify the programmer device. One of\n");
76 list_programmers_linebreak(4, 80, 0);
77 printf(".\n\nYou can specify one of -h, -R, -L, "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000078#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000079 "-z, "
80#endif
81 "-E, -r, -w, -v or no operation.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +020082 "If no operation is specified, flashprog will only probe for flash chips.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000083}
84
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +100085static void cli_classic_abort_usage(const char *msg)
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000086{
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +100087 if (msg)
88 fprintf(stderr, "%s", msg);
Nico Huberc3b02dc2023-08-12 01:13:45 +020089 printf("Please run \"flashprog --help\" for usage info.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000090 exit(1);
91}
92
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +100093static void cli_classic_validate_singleop(int *operation_specified)
94{
95 if (++(*operation_specified) > 1) {
96 cli_classic_abort_usage("More than one operation specified. Aborting.\n");
97 }
98}
99
Jacob Garber4a84ec22019-07-25 19:12:31 -0600100static int check_filename(char *filename, const char *type)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000101{
102 if (!filename || (filename[0] == '\0')) {
103 fprintf(stderr, "Error: No %s file specified.\n", type);
104 return 1;
105 }
106 /* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600107 if (filename[0] == '-' && filename[1] != '\0')
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000108 fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
109 return 0;
110}
111
Jack Rosenthal85777562021-04-09 10:03:14 -0600112/* Ensure a file is open by means of fstat */
113static bool check_file(FILE *file)
114{
Jack Rosenthal85777562021-04-09 10:03:14 -0600115 struct stat statbuf;
116
117 if (fstat(fileno(file), &statbuf) < 0)
118 return false;
Jack Rosenthal85777562021-04-09 10:03:14 -0600119 return true;
120}
121
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100122static int do_read(struct flashctx *const flash, const char *const filename)
123{
124 int ret;
125
Nico Huberc3b02dc2023-08-12 01:13:45 +0200126 unsigned long size = flashprog_flash_getsize(flash);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100127 unsigned char *buf = calloc(size, sizeof(unsigned char));
128 if (!buf) {
129 msg_gerr("Memory allocation failed!\n");
130 return 1;
131 }
132
Nico Huberc3b02dc2023-08-12 01:13:45 +0200133 ret = flashprog_image_read(flash, buf, size);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100134 if (ret > 0)
135 goto free_out;
136
137 ret = write_buf_to_file(buf, size, filename);
138
139free_out:
140 free(buf);
141 return ret;
142}
143
144static int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
145{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200146 const size_t flash_size = flashprog_flash_getsize(flash);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100147 int ret = 1;
148
149 uint8_t *const newcontents = malloc(flash_size);
150 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
151
152 if (!newcontents || (referencefile && !refcontents)) {
153 msg_gerr("Out of memory!\n");
154 goto _free_ret;
155 }
156
157 if (read_buf_from_file(newcontents, flash_size, filename))
158 goto _free_ret;
159
160 if (referencefile) {
161 if (read_buf_from_file(refcontents, flash_size, referencefile))
162 goto _free_ret;
163 }
164
Nico Huberc3b02dc2023-08-12 01:13:45 +0200165 ret = flashprog_image_write(flash, newcontents, flash_size, refcontents);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100166
167_free_ret:
168 free(refcontents);
169 free(newcontents);
170 return ret;
171}
172
173static int do_verify(struct flashctx *const flash, const char *const filename)
174{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200175 const size_t flash_size = flashprog_flash_getsize(flash);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100176 int ret = 1;
177
178 uint8_t *const newcontents = malloc(flash_size);
179 if (!newcontents) {
180 msg_gerr("Out of memory!\n");
181 goto _free_ret;
182 }
183
184 if (read_buf_from_file(newcontents, flash_size, filename))
185 goto _free_ret;
186
Nico Huberc3b02dc2023-08-12 01:13:45 +0200187 ret = flashprog_image_verify(flash, newcontents, flash_size);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100188
189_free_ret:
190 free(newcontents);
191 return ret;
192}
193
Nico Huber89569d62023-01-12 23:31:40 +0100194/* Returns true if the flash chip cannot be completely accessed due to size/address limits of the programmer. */
195static bool max_decode_exceeded(const struct registered_master *const mst, const struct flashctx *const flash)
Edward O'Callaghanaba36582022-09-05 11:09:28 +1000196{
Nico Huber89569d62023-01-12 23:31:40 +0100197 if (flashprog_flash_getsize(flash) <= mst->max_rom_decode)
198 return false;
Edward O'Callaghanaba36582022-09-05 11:09:28 +1000199
Nico Huber89569d62023-01-12 23:31:40 +0100200 msg_pdbg("Chip size %u kB is bigger than supported size %zu kB of\n"
201 "chipset/board/programmer for memory-mapped interface, probe/read/erase/write\n"
202 "may fail.\n", flash->chip->total_size, mst->max_rom_decode / KiB);
203 return true;
Edward O'Callaghanaba36582022-09-05 11:09:28 +1000204}
205
Uwe Hermann394ee782011-08-20 14:14:22 +0000206int main(int argc, char *argv[])
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000207{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000208 const struct flashchip *chip = NULL;
Jernej Å krabece814a9b2014-12-12 00:32:03 +0000209 /* Probe for up to eight flash chips. */
210 struct flashctx flashes[8] = {{0}};
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000211 struct flashctx *fill_flash;
Nico Hubere68b08b2023-02-11 00:00:54 +0100212 int opt, i, j;
Felix Singer2a768e42022-05-22 02:04:18 +0200213 int startchip = -1, chipcount = 0, option_index = 0;
214 int operation_specified = 0;
215 bool force = false, ifd = false, fmap = false;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000216#if CONFIG_PRINT_WIKI == 1
Felix Singer2a768e42022-05-22 02:04:18 +0200217 bool list_supported_wiki = false;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000218#endif
Felix Singer2a768e42022-05-22 02:04:18 +0200219 bool flash_name = false, flash_size = false;
220 bool read_it = false, write_it = false, erase_it = false, verify_it = false;
221 bool dont_verify_it = false, dont_verify_all = false;
222 bool list_supported = false;
Richard Hughes842d6782021-01-15 09:48:12 +0000223 bool show_progress = false;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200224 struct flashprog_layout *layout = NULL;
Nico Hubere68b08b2023-02-11 00:00:54 +0100225 struct flashprog_programmer *prog = NULL;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300226 enum {
227 OPTION_IFD = 0x0100,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100228 OPTION_FMAP,
229 OPTION_FMAP_FILE,
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300230 OPTION_FLASH_CONTENTS,
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000231 OPTION_FLASH_NAME,
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000232 OPTION_FLASH_SIZE,
Richard Hughes842d6782021-01-15 09:48:12 +0000233 OPTION_PROGRESS,
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300234 };
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000235 int ret = 0;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000236
Nico Huber99d15952016-05-02 16:54:24 +0200237 static const char optstring[] = "r:Rw:v:nNVEfc:l:i:p:Lzho:";
Mathias Krausea60faab2011-01-17 07:50:42 +0000238 static const struct option long_options[] = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000239 {"read", 1, NULL, 'r'},
240 {"write", 1, NULL, 'w'},
241 {"erase", 0, NULL, 'E'},
242 {"verify", 1, NULL, 'v'},
243 {"noverify", 0, NULL, 'n'},
Nico Huber99d15952016-05-02 16:54:24 +0200244 {"noverify-all", 0, NULL, 'N'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000245 {"chip", 1, NULL, 'c'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000246 {"verbose", 0, NULL, 'V'},
247 {"force", 0, NULL, 'f'},
248 {"layout", 1, NULL, 'l'},
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300249 {"ifd", 0, NULL, OPTION_IFD},
Arthur Heymansc82900b2018-01-10 12:48:16 +0100250 {"fmap", 0, NULL, OPTION_FMAP},
251 {"fmap-file", 1, NULL, OPTION_FMAP_FILE},
Anastasia Klimchuka7cb7e92022-11-25 18:10:43 +1100252 {"image", 1, NULL, 'i'}, // (deprecated): back compatibility.
253 {"include", 1, NULL, 'i'},
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300254 {"flash-contents", 1, NULL, OPTION_FLASH_CONTENTS},
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000255 {"flash-name", 0, NULL, OPTION_FLASH_NAME},
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000256 {"flash-size", 0, NULL, OPTION_FLASH_SIZE},
257 {"get-size", 0, NULL, OPTION_FLASH_SIZE}, // (deprecated): back compatibility.
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000258 {"list-supported", 0, NULL, 'L'},
259 {"list-supported-wiki", 0, NULL, 'z'},
260 {"programmer", 1, NULL, 'p'},
261 {"help", 0, NULL, 'h'},
262 {"version", 0, NULL, 'R'},
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000263 {"output", 1, NULL, 'o'},
Richard Hughes842d6782021-01-15 09:48:12 +0000264 {"progress", 0, NULL, OPTION_PROGRESS},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000265 {NULL, 0, NULL, 0},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000266 };
267
268 char *filename = NULL;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300269 char *referencefile = NULL;
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000270 char *layoutfile = NULL;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100271 char *fmapfile = NULL;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000272 char *logfile = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000273 char *tempstr = NULL;
Nico Huber34e783a2023-02-11 00:30:27 +0100274 struct flash_args flash_args = { 0 };
Arthur Heymansb04fef92019-02-05 17:35:05 +0100275 struct layout_include_args *include_args = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000276
Jack Rosenthal85777562021-04-09 10:03:14 -0600277 /*
278 * Safety-guard against a user who has (mistakenly) closed
Nico Huberc3b02dc2023-08-12 01:13:45 +0200279 * stdout or stderr before exec'ing flashprog. We disable
Jack Rosenthal85777562021-04-09 10:03:14 -0600280 * logging in this case to prevent writing log data to a flash
281 * chip when a flash device gets opened with fd 1 or 2.
282 */
283 if (check_file(stdout) && check_file(stderr)) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200284 flashprog_set_log_callback(
285 (flashprog_log_callback *)&flashprog_print_cb);
Jack Rosenthal85777562021-04-09 10:03:14 -0600286 }
Nico Huber18781102012-12-10 13:34:12 +0000287
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000288 print_version();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000289 print_banner();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000290
Edward O'Callaghand58496b2022-02-03 12:52:02 +1100291 /* FIXME: Delay calibration should happen in programmer code. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200292 if (flashprog_init(1))
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000293 exit(1);
294
295 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000296 /* FIXME: Delay all operation_specified checks until after command
297 * line parsing to allow --help overriding everything else.
298 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000299 while ((opt = getopt_long(argc, argv, optstring,
300 long_options, &option_index)) != EOF) {
301 switch (opt) {
302 case 'r':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000303 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000304 filename = strdup(optarg);
Felix Singer2a768e42022-05-22 02:04:18 +0200305 read_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000306 break;
307 case 'w':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000308 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000309 filename = strdup(optarg);
Felix Singer2a768e42022-05-22 02:04:18 +0200310 write_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000311 break;
312 case 'v':
313 //FIXME: gracefully handle superfluous -v
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000314 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000315 if (dont_verify_it) {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000316 cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000317 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000318 filename = strdup(optarg);
Felix Singer2a768e42022-05-22 02:04:18 +0200319 verify_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000320 break;
321 case 'n':
322 if (verify_it) {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000323 cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000324 }
Felix Singer2a768e42022-05-22 02:04:18 +0200325 dont_verify_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000326 break;
Nico Huber99d15952016-05-02 16:54:24 +0200327 case 'N':
Felix Singer2a768e42022-05-22 02:04:18 +0200328 dont_verify_all = true;
Nico Huber99d15952016-05-02 16:54:24 +0200329 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000330 case 'c':
Nico Huber34e783a2023-02-11 00:30:27 +0100331 case 'p':
332 ret = cli_parse_flash_args(&flash_args, opt, optarg);
333 if (ret == 1)
334 cli_classic_abort_usage(NULL);
335 else if (ret)
336 exit(1);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000337 break;
338 case 'V':
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000339 verbose_screen++;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200340 if (verbose_screen > FLASHPROG_MSG_DEBUG2)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000341 verbose_logfile = verbose_screen;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000342 break;
343 case 'E':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000344 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200345 erase_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000346 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000347 case 'f':
Felix Singer2a768e42022-05-22 02:04:18 +0200348 force = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000349 break;
350 case 'l':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000351 if (layoutfile)
352 cli_classic_abort_usage("Error: --layout specified more than once. Aborting.\n");
353 if (ifd)
354 cli_classic_abort_usage("Error: --layout and --ifd both specified. Aborting.\n");
355 if (fmap)
356 cli_classic_abort_usage("Error: --layout and --fmap-file both specified. Aborting.\n");
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000357 layoutfile = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000358 break;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300359 case OPTION_IFD:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000360 if (layoutfile)
361 cli_classic_abort_usage("Error: --layout and --ifd both specified. Aborting.\n");
362 if (fmap)
363 cli_classic_abort_usage("Error: --fmap-file and --ifd both specified. Aborting.\n");
Felix Singer2a768e42022-05-22 02:04:18 +0200364 ifd = true;
Nico Huber305f4172013-06-14 11:55:26 +0200365 break;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100366 case OPTION_FMAP_FILE:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000367 if (fmap)
368 cli_classic_abort_usage("Error: --fmap or --fmap-file specified "
Arthur Heymansc82900b2018-01-10 12:48:16 +0100369 "more than once. Aborting.\n");
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000370 if (ifd)
371 cli_classic_abort_usage("Error: --fmap-file and --ifd both specified. Aborting.\n");
372 if (layoutfile)
373 cli_classic_abort_usage("Error: --fmap-file and --layout both specified. Aborting.\n");
Arthur Heymansc82900b2018-01-10 12:48:16 +0100374 fmapfile = strdup(optarg);
Felix Singer2a768e42022-05-22 02:04:18 +0200375 fmap = true;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100376 break;
377 case OPTION_FMAP:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000378 if (fmap)
379 cli_classic_abort_usage("Error: --fmap or --fmap-file specified "
Arthur Heymansc82900b2018-01-10 12:48:16 +0100380 "more than once. Aborting.\n");
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000381 if (ifd)
382 cli_classic_abort_usage("Error: --fmap and --ifd both specified. Aborting.\n");
383 if (layoutfile)
384 cli_classic_abort_usage("Error: --layout and --fmap both specified. Aborting.\n");
Felix Singer2a768e42022-05-22 02:04:18 +0200385 fmap = true;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100386 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000387 case 'i':
388 tempstr = strdup(optarg);
Arthur Heymansb04fef92019-02-05 17:35:05 +0100389 if (register_include_arg(&include_args, tempstr)) {
Stefan Taunerb8911d62012-12-26 07:55:00 +0000390 free(tempstr);
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000391 cli_classic_abort_usage(NULL);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000392 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000393 break;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300394 case OPTION_FLASH_CONTENTS:
Angel Ponsa60f6412020-03-31 15:02:02 +0200395 if (referencefile)
396 cli_classic_abort_usage("Error: --flash-contents specified more than once."
397 "Aborting.\n");
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300398 referencefile = strdup(optarg);
399 break;
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000400 case OPTION_FLASH_NAME:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000401 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200402 flash_name = true;
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000403 break;
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000404 case OPTION_FLASH_SIZE:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000405 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200406 flash_size = true;
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000407 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000408 case 'L':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000409 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200410 list_supported = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000411 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000412 case 'z':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000413#if CONFIG_PRINT_WIKI == 1
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000414 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200415 list_supported_wiki = true;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000416#else
Idwer Vollering40407b62020-12-08 00:24:39 +0100417 cli_classic_abort_usage("Error: Wiki output was not "
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000418 "compiled in. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000419#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000420 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000421 case 'R':
422 /* print_version() is always called during startup. */
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000423 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000424 exit(0);
425 break;
426 case 'h':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000427 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000428 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000429 exit(0);
430 break;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000431 case 'o':
Elyes HAOUASef78de42019-07-18 15:08:10 +0200432 if (logfile) {
433 fprintf(stderr, "Warning: -o/--output specified multiple times.\n");
434 free(logfile);
435 }
436
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000437 logfile = strdup(optarg);
438 if (logfile[0] == '\0') {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000439 cli_classic_abort_usage("No log filename specified.\n");
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000440 }
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000441 break;
Richard Hughes842d6782021-01-15 09:48:12 +0000442 case OPTION_PROGRESS:
443 show_progress = true;
444 break;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000445 default:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000446 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000447 break;
448 }
449 }
450
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000451 if (optind < argc)
452 cli_classic_abort_usage("Error: Extra parameter found.\n");
453 if ((read_it | write_it | verify_it) && check_filename(filename, "image"))
454 cli_classic_abort_usage(NULL);
455 if (layoutfile && check_filename(layoutfile, "layout"))
456 cli_classic_abort_usage(NULL);
457 if (fmapfile && check_filename(fmapfile, "fmap"))
458 cli_classic_abort_usage(NULL);
459 if (referencefile && check_filename(referencefile, "reference"))
460 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000461 if (logfile && check_filename(logfile, "log"))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000462 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000463 if (logfile && open_logfile(logfile))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000464 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000465
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000466#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000467 if (list_supported_wiki) {
468 print_supported_wiki();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000469 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000470 }
471#endif
472
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000473 if (list_supported) {
Niklas Söderlundede2fa42012-10-23 13:06:46 +0000474 if (print_supported())
475 ret = 1;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000476 goto out;
477 }
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000478
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000479 start_logging();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000480
481 print_buildinfo();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000482 msg_gdbg("Command line (%i args):", argc - 1);
483 for (i = 0; i < argc; i++) {
484 msg_gdbg(" %s", argv[i]);
485 }
486 msg_gdbg("\n");
487
Nico Huberd9b57712021-05-14 01:07:28 +0200488 if (layoutfile && layout_from_file(&layout, layoutfile)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000489 ret = 1;
490 goto out;
491 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100492
Nico Huberd9b57712021-05-14 01:07:28 +0200493 if (!ifd && !fmap && process_include_args(layout, include_args)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000494 ret = 1;
495 goto out;
496 }
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000497 /* Does a chip with the requested name exist in the flashchips array? */
Nico Huber34e783a2023-02-11 00:30:27 +0100498 if (flash_args.chip) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000499 for (chip = flashchips; chip && chip->name; chip++)
Nico Huber34e783a2023-02-11 00:30:27 +0100500 if (!strcmp(chip->name, flash_args.chip))
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000501 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000502 if (!chip || !chip->name) {
Nico Huber34e783a2023-02-11 00:30:27 +0100503 msg_cerr("Error: Unknown chip '%s' specified.\n", flash_args.chip);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200504 msg_gerr("Run flashprog -L to view the hardware supported in this flashprog version.\n");
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000505 ret = 1;
506 goto out;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000507 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000508 /* Keep chip around for later usage in case a forced read is requested. */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000509 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000510
Nico Huber34e783a2023-02-11 00:30:27 +0100511 if (flash_args.prog_name == NULL) {
Nico Hubere68b08b2023-02-11 00:00:54 +0100512 const char *const default_programmer = CONFIG_DEFAULT_PROGRAMMER_NAME;
Thomas Heijligen84e9c912021-06-01 16:22:14 +0200513
Nico Hubere68b08b2023-02-11 00:00:54 +0100514 if (default_programmer[0]) {
515 /* We need to strdup here because we free() unconditionally later. */
Nico Huber34e783a2023-02-11 00:30:27 +0100516 flash_args.prog_name = strdup(default_programmer);
517 flash_args.prog_args = strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS);
518 if (!flash_args.prog_name || !flash_args.prog_args) {
Nico Hubere68b08b2023-02-11 00:00:54 +0100519 fprintf(stderr, "Out of memory!\n");
520 ret = 1;
521 goto out;
522 }
Stefan Tauner265fcac2014-06-02 00:12:23 +0000523 msg_pinfo("Using default programmer \"%s\" with arguments \"%s\".\n",
Nico Huber34e783a2023-02-11 00:30:27 +0100524 flash_args.prog_name, flash_args.prog_args);
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000525 } else {
526 msg_perr("Please select a programmer with the --programmer parameter.\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +0000527#if CONFIG_INTERNAL == 1
528 "To choose the mainboard of this computer use 'internal'. "
529#endif
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000530 "Valid choices are:\n");
531 list_programmers_linebreak(0, 80, 0);
Stefan Taunerb226cb12012-11-24 18:59:39 +0000532 msg_ginfo(".\n");
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000533 ret = 1;
534 goto out;
535 }
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000536 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000537
Nico Huber34e783a2023-02-11 00:30:27 +0100538 if (flashprog_programmer_init(&prog, flash_args.prog_name, flash_args.prog_args)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000539 msg_perr("Error: Programmer initialization failed.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000540 ret = 1;
Nico Huber2b66ad92023-01-11 20:15:15 +0100541 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000542 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000543 tempstr = flashbuses_to_text(get_buses_supported());
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000544 msg_pdbg("The following protocols are supported: %s.\n", tempstr);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000545 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000546
Nico Huber34e783a2023-02-11 00:30:27 +0100547 chip_to_probe = flash_args.chip;
Nico Huber89569d62023-01-12 23:31:40 +0100548 struct registered_master *matched_master = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000549 for (j = 0; j < registered_master_count; j++) {
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000550 startchip = 0;
Nico Huber519be662018-12-23 20:03:35 +0100551 while (chipcount < (int)ARRAY_SIZE(flashes)) {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000552 startchip = probe_flash(&registered_masters[j], startchip, &flashes[chipcount], 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000553 if (startchip == -1)
554 break;
Nico Huber89569d62023-01-12 23:31:40 +0100555 if (chipcount == 0)
556 matched_master = &registered_masters[j];
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000557 chipcount++;
558 startchip++;
559 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000560 }
561
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000562 if (chipcount > 1) {
Stefan Tauner0554ca52013-07-25 22:54:25 +0000563 msg_cinfo("Multiple flash chip definitions match the detected chip(s): \"%s\"",
564 flashes[0].chip->name);
Stefan Tauner716e0982011-07-25 20:38:52 +0000565 for (i = 1; i < chipcount; i++)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000566 msg_cinfo(", \"%s\"", flashes[i].chip->name);
Stefan Tauner0554ca52013-07-25 22:54:25 +0000567 msg_cinfo("\nPlease specify which chip definition to use with the -c <chipname> option.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000568 ret = 1;
569 goto out_shutdown;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000570 } else if (!chipcount) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000571 msg_cinfo("No EEPROM/flash device found.\n");
Nico Huber34e783a2023-02-11 00:30:27 +0100572 if (!force || !flash_args.chip) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200573 msg_cinfo("Note: flashprog can never write if the flash chip isn't found "
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000574 "automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000575 }
Nico Huber34e783a2023-02-11 00:30:27 +0100576 if (force && read_it && flash_args.chip) {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000577 struct registered_master *mst;
578 int compatible_masters = 0;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000579 msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000580 /* This loop just counts compatible controllers. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000581 for (j = 0; j < registered_master_count; j++) {
582 mst = &registered_masters[j];
Nico Huber34e783a2023-02-11 00:30:27 +0100583 /* chip is still set from the search earlier in this function. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000584 if (mst->buses_supported & chip->bustype)
585 compatible_masters++;
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000586 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000587 if (!compatible_masters) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000588 msg_cinfo("No compatible controller found for the requested flash chip.\n");
589 ret = 1;
590 goto out_shutdown;
591 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000592 if (compatible_masters > 1)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000593 msg_cinfo("More than one compatible controller found for the requested flash "
594 "chip, using the first one.\n");
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000595 for (j = 0; j < registered_master_count; j++) {
596 mst = &registered_masters[j];
597 startchip = probe_flash(mst, 0, &flashes[0], 1);
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000598 if (startchip != -1)
599 break;
600 }
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000601 if (startchip == -1) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000602 // FIXME: This should never happen! Ask for a bug report?
Nico Huber34e783a2023-02-11 00:30:27 +0100603 msg_cinfo("Probing for flash chip '%s' failed.\n", flash_args.chip);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000604 ret = 1;
605 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000606 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000607 msg_cinfo("Please note that forced reads most likely contain garbage.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200608 flashprog_flag_set(&flashes[0], FLASHPROG_FLAG_FORCE, force);
Edward O'Callaghan2d170412021-11-15 15:47:15 +1100609 ret = do_read(&flashes[0], filename);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000610 free(flashes[0].chip);
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000611 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000612 }
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000613 ret = 1;
614 goto out_shutdown;
Nico Huber34e783a2023-02-11 00:30:27 +0100615 } else if (!flash_args.chip) {
Stefan Tauner1d947632011-09-11 22:08:58 +0000616 /* repeat for convenience when looking at foreign logs */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000617 tempstr = flashbuses_to_text(flashes[0].chip->bustype);
Stefan Tauner1d947632011-09-11 22:08:58 +0000618 msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000619 flashes[0].chip->vendor, flashes[0].chip->name, flashes[0].chip->total_size, tempstr);
Stefan Tauner1d947632011-09-11 22:08:58 +0000620 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000621 }
622
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000623 fill_flash = &flashes[0];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000624
Richard Hughes842d6782021-01-15 09:48:12 +0000625 if (show_progress)
626 flashprog_set_progress_callback(fill_flash, &flashprog_progress_cb, NULL);
627
Stefan Tauner9b32de92014-08-08 23:52:33 +0000628 print_chip_support_status(fill_flash->chip);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000629
Nico Huber89569d62023-01-12 23:31:40 +0100630 if (max_decode_exceeded(matched_master, fill_flash) && !force) {
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000631 msg_cerr("This flash chip is too big for this programmer (--verbose/-V gives details).\n"
632 "Use --force/-f to override at your own risk.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000633 ret = 1;
634 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000635 }
636
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000637 if (!(read_it | write_it | verify_it | erase_it | flash_name | flash_size)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000638 msg_ginfo("No operations were specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000639 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000640 }
641
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000642 if (flash_name) {
643 if (fill_flash->chip->vendor && fill_flash->chip->name) {
644 printf("vendor=\"%s\" name=\"%s\"\n",
645 fill_flash->chip->vendor,
646 fill_flash->chip->name);
647 } else {
648 ret = -1;
649 }
650 goto out_shutdown;
651 }
652
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000653 if (flash_size) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200654 printf("%zu\n", flashprog_flash_getsize(fill_flash));
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000655 goto out_shutdown;
656 }
657
Nico Huberc3b02dc2023-08-12 01:13:45 +0200658 if (ifd && (flashprog_layout_read_from_ifd(&layout, fill_flash, NULL, 0) ||
Arthur Heymansb04fef92019-02-05 17:35:05 +0100659 process_include_args(layout, include_args))) {
Nico Huber305f4172013-06-14 11:55:26 +0200660 ret = 1;
661 goto out_shutdown;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100662 } else if (fmap && fmapfile) {
663 struct stat s;
664 if (stat(fmapfile, &s) != 0) {
665 msg_gerr("Failed to stat fmapfile \"%s\"\n", fmapfile);
666 ret = 1;
667 goto out_shutdown;
668 }
669
670 size_t fmapfile_size = s.st_size;
671 uint8_t *fmapfile_buffer = malloc(fmapfile_size);
672 if (!fmapfile_buffer) {
673 ret = 1;
674 goto out_shutdown;
675 }
676
677 if (read_buf_from_file(fmapfile_buffer, fmapfile_size, fmapfile)) {
678 ret = 1;
679 free(fmapfile_buffer);
680 goto out_shutdown;
681 }
682
Nico Huberc3b02dc2023-08-12 01:13:45 +0200683 if (flashprog_layout_read_fmap_from_buffer(&layout, fill_flash, fmapfile_buffer, fmapfile_size) ||
Arthur Heymansb04fef92019-02-05 17:35:05 +0100684 process_include_args(layout, include_args)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100685 ret = 1;
686 free(fmapfile_buffer);
687 goto out_shutdown;
688 }
689 free(fmapfile_buffer);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200690 } else if (fmap && (flashprog_layout_read_fmap_from_rom(&layout, fill_flash, 0,
691 flashprog_flash_getsize(fill_flash)) || process_include_args(layout, include_args))) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100692 ret = 1;
693 goto out_shutdown;
Nico Huber305f4172013-06-14 11:55:26 +0200694 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000695
Nico Huberc3b02dc2023-08-12 01:13:45 +0200696 flashprog_layout_set(fill_flash, layout);
697 flashprog_flag_set(fill_flash, FLASHPROG_FLAG_FORCE, force);
Urja Rannikko7258cf52017-06-17 11:31:57 +0300698#if CONFIG_INTERNAL == 1
Nico Huberc3b02dc2023-08-12 01:13:45 +0200699 flashprog_flag_set(fill_flash, FLASHPROG_FLAG_FORCE_BOARDMISMATCH, force_boardmismatch);
Urja Rannikko7258cf52017-06-17 11:31:57 +0300700#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +0200701 flashprog_flag_set(fill_flash, FLASHPROG_FLAG_VERIFY_AFTER_WRITE, !dont_verify_it);
702 flashprog_flag_set(fill_flash, FLASHPROG_FLAG_VERIFY_WHOLE_CHIP, !dont_verify_all);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000703
Carl-Daniel Hailfinger9ad42552010-09-15 10:20:16 +0000704 /* FIXME: We should issue an unconditional chip reset here. This can be
705 * done once we have a .reset function in struct flashchip.
706 * Give the chip time to settle.
707 */
708 programmer_delay(100000);
Nico Huber899e4ec2016-04-29 18:39:01 +0200709 if (read_it)
710 ret = do_read(fill_flash, filename);
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +1100711 else if (erase_it) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200712 ret = flashprog_flash_erase(fill_flash);
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +1100713 /*
714 * FIXME: Do we really want the scary warning if erase failed?
715 * After all, after erase the chip is either blank or partially
716 * blank or it has the old contents. A blank chip won't boot,
717 * so if the user wanted erase and reboots afterwards, the user
718 * knows very well that booting won't work.
719 */
720 if (ret)
721 emergency_help_message();
722 }
Nico Huber899e4ec2016-04-29 18:39:01 +0200723 else if (write_it)
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300724 ret = do_write(fill_flash, filename, referencefile);
Nico Huber899e4ec2016-04-29 18:39:01 +0200725 else if (verify_it)
726 ret = do_verify(fill_flash, filename);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000727
Nico Huberc3b02dc2023-08-12 01:13:45 +0200728 flashprog_layout_release(layout);
Nico Huber305f4172013-06-14 11:55:26 +0200729
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000730out_shutdown:
Nico Hubere68b08b2023-02-11 00:00:54 +0100731 flashprog_programmer_shutdown(prog);
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000732out:
Nico Huber5bd990c2019-06-16 19:46:46 +0200733 for (i = 0; i < chipcount; i++) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200734 flashprog_layout_release(flashes[i].default_layout);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000735 free(flashes[i].chip);
Nico Huber5bd990c2019-06-16 19:46:46 +0200736 }
Stefan Taunerb8911d62012-12-26 07:55:00 +0000737
Nico Huber345f65a2021-05-14 01:11:08 +0200738 cleanup_include_args(&include_args);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000739 free(filename);
Richard Hughes6eca7612018-12-19 15:40:27 +0000740 free(fmapfile);
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300741 free(referencefile);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000742 free(layoutfile);
Nico Huber34e783a2023-02-11 00:30:27 +0100743 free(flash_args.prog_args);
744 free(flash_args.prog_name);
745 free(flash_args.chip);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000746 /* clean up global variables */
Stefan Taunerb8911d62012-12-26 07:55:00 +0000747 chip_to_probe = NULL;
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000748 free(logfile);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000749 ret |= close_logfile();
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000750 return ret;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000751}