blob: d712bdef5843d409eb7769c4f887eaa722a965fa [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>
Nico Huber47412a92026-03-10 23:48:25 +010027
28#define flashprog_chip flashchip /* For now, we use direct pointers */
29#include "libflashprog.h" /* to the internal struct flashchip. */
30
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000031#include "flash.h"
32#include "flashchips.h"
Arthur Heymansc82900b2018-01-10 12:48:16 +010033#include "fmap.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000034#include "programmer.h"
Nico Huber34e783a2023-02-11 00:30:27 +010035#include "cli.h"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000036
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000037static void cli_classic_usage(const char *name)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000038{
Stefan Taunerb226cb12012-11-24 18:59:39 +000039 printf("Usage: %s [-h|-R|-L|"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000040#if CONFIG_PRINT_WIKI == 1
Stefan Taunerb226cb12012-11-24 18:59:39 +000041 "-z|"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000042#endif
Edward O'Callaghan16ec45c2019-10-04 20:24:53 +100043 "\n\t-p <programmername>[:<parameters>] [-c <chipname>]\n"
44 "\t\t(--flash-name|--flash-size|\n"
45 "\t\t [-E|(-r|-w|-v) <file>]\n"
46 "\t\t [(-l <layoutfile>|--ifd| --fmap|--fmap-file <file>) [-i <imagename>]...]\n"
47 "\t\t [-n] [-N] [-f])]\n"
48 "\t[-V[V[V]]] [-o <logfile>]\n\n", name);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000049
Stefan Taunerb226cb12012-11-24 18:59:39 +000050 printf(" -h | --help print this help text\n"
51 " -R | --version print version (release)\n"
52 " -r | --read <file> read flash and save to <file>\n"
Daniel Campellod12b6bc2022-03-14 11:43:16 -060053 " -w | --write (<file>|-) write <file> or the content provided\n"
Daniel Campello8eaef7d2021-04-15 10:36:04 -060054 " on the standard input to flash\n"
Daniel Campellod12b6bc2022-03-14 11:43:16 -060055 " -v | --verify (<file>|-) verify flash against <file>\n"
Daniel Campello8eaef7d2021-04-15 10:36:04 -060056 " or the content provided on the standard input\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000057 " -E | --erase erase flash memory\n"
58 " -V | --verbose more verbose output\n"
59 " -c | --chip <chipname> probe only for specified flash chip\n"
60 " -f | --force force specific operations (see man page)\n"
61 " -n | --noverify don't auto-verify\n"
Nico Huber99d15952016-05-02 16:54:24 +020062 " -N | --noverify-all verify included regions only (cf. -i)\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000063 " -l | --layout <layoutfile> read ROM layout from <layoutfile>\n"
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +100064 " --flash-name read out the detected flash name\n"
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +100065 " --flash-size read out the detected flash size\n"
Arthur Heymansc82900b2018-01-10 12:48:16 +010066 " --fmap read ROM layout from fmap embedded in ROM\n"
67 " --fmap-file <fmapfile> read ROM layout from fmap in <fmapfile>\n"
Nico Huber305f4172013-06-14 11:55:26 +020068 " --ifd read layout from an Intel Firmware Descriptor\n"
Anastasia Klimchuka7cb7e92022-11-25 18:10:43 +110069 " -i | --include <region> only read/write image <region> from layout\n"
70 " --image <region> deprecated, please use --include\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000071 " -o | --output <logfile> log output to <logfile>\n"
Paul Kocialkowskif701f342018-01-15 01:10:36 +030072 " --flash-contents <ref-file> assume flash contents to be <ref-file>\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000073 " -L | --list-supported print supported devices\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000074#if CONFIG_PRINT_WIKI == 1
Stefan Taunerb226cb12012-11-24 18:59:39 +000075 " -z | --list-supported-wiki print supported devices in wiki syntax\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000076#endif
Richard Hughes842d6782021-01-15 09:48:12 +000077 " --progress show progress percentage on the standard output\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000078 " -p | --programmer <name>[:<param>] specify the programmer device. One of\n");
79 list_programmers_linebreak(4, 80, 0);
80 printf(".\n\nYou can specify one of -h, -R, -L, "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000081#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000082 "-z, "
83#endif
84 "-E, -r, -w, -v or no operation.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +020085 "If no operation is specified, flashprog will only probe for flash chips.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000086}
87
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +100088static void cli_classic_abort_usage(const char *msg)
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000089{
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +100090 if (msg)
91 fprintf(stderr, "%s", msg);
Nico Huberc3b02dc2023-08-12 01:13:45 +020092 printf("Please run \"flashprog --help\" for usage info.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000093 exit(1);
94}
95
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +100096static void cli_classic_validate_singleop(int *operation_specified)
97{
98 if (++(*operation_specified) > 1) {
99 cli_classic_abort_usage("More than one operation specified. Aborting.\n");
100 }
101}
102
Nico Huber47412a92026-03-10 23:48:25 +0100103static int force_chip(struct flashprog_flashctx **flash,
104 const struct flashprog_programmer *const prog,
105 const char *const chip_name)
106{
107 const struct flashprog_chip *chip;
108 struct master_common *bus = NULL;
109 int bus_idx;
110
111 chip = flashprog_chip_by_name(chip_name);
112 if (!chip)
113 return 1;
114
115 for (bus_idx = 0; bus_idx < registered_master_count; ++bus_idx) {
116 const enum flashprog_bus_type bustype = flashprog_chip_buses(chip);
117 if (!(registered_masters[bus_idx].buses_supported & bustype))
118 continue;
119
120 if (!bus)
121 bus = &registered_masters[bus_idx].common;
122 else
123 msg_cwarn("More than one compatible controller found for the\n"
124 "requested flash chip, using the first one.\n");
125 }
126 if (!bus) {
127 msg_cerr("No compatible controller found for the requested flash chip.\n");
128 return 1;
129 }
130
131 return flashprog_flash_prepare_context(flash, prog, bus, chip);
132}
133
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100134static int do_read(struct flashctx *const flash, const char *const filename)
135{
136 int ret;
137
Nico Huberc3b02dc2023-08-12 01:13:45 +0200138 unsigned long size = flashprog_flash_getsize(flash);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100139 unsigned char *buf = calloc(size, sizeof(unsigned char));
140 if (!buf) {
141 msg_gerr("Memory allocation failed!\n");
142 return 1;
143 }
144
Nico Huberc3b02dc2023-08-12 01:13:45 +0200145 ret = flashprog_image_read(flash, buf, size);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100146 if (ret > 0)
147 goto free_out;
148
149 ret = write_buf_to_file(buf, size, filename);
150
151free_out:
152 free(buf);
153 return ret;
154}
155
156static int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
157{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200158 const size_t flash_size = flashprog_flash_getsize(flash);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100159 int ret = 1;
160
161 uint8_t *const newcontents = malloc(flash_size);
162 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
163
164 if (!newcontents || (referencefile && !refcontents)) {
165 msg_gerr("Out of memory!\n");
166 goto _free_ret;
167 }
168
169 if (read_buf_from_file(newcontents, flash_size, filename))
170 goto _free_ret;
171
172 if (referencefile) {
173 if (read_buf_from_file(refcontents, flash_size, referencefile))
174 goto _free_ret;
175 }
176
Nico Huberc3b02dc2023-08-12 01:13:45 +0200177 ret = flashprog_image_write(flash, newcontents, flash_size, refcontents);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100178
179_free_ret:
180 free(refcontents);
181 free(newcontents);
182 return ret;
183}
184
185static int do_verify(struct flashctx *const flash, const char *const filename)
186{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200187 const size_t flash_size = flashprog_flash_getsize(flash);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100188 int ret = 1;
189
190 uint8_t *const newcontents = malloc(flash_size);
191 if (!newcontents) {
192 msg_gerr("Out of memory!\n");
193 goto _free_ret;
194 }
195
196 if (read_buf_from_file(newcontents, flash_size, filename))
197 goto _free_ret;
198
Nico Huberc3b02dc2023-08-12 01:13:45 +0200199 ret = flashprog_image_verify(flash, newcontents, flash_size);
Edward O'Callaghan98cfa692021-12-13 12:46:12 +1100200
201_free_ret:
202 free(newcontents);
203 return ret;
204}
205
Nico Huber89569d62023-01-12 23:31:40 +0100206/* Returns true if the flash chip cannot be completely accessed due to size/address limits of the programmer. */
Nico Huberbe42cf22026-02-21 17:40:44 +0100207static bool max_decode_exceeded(const struct flashctx *const flash, const bool force)
Edward O'Callaghanaba36582022-09-05 11:09:28 +1000208{
Nico Huber006d08d2026-03-10 22:33:22 +0100209 if (flashprog_flash_getsize(flash) <= flash->mst.common->max_rom_decode)
Nico Huber89569d62023-01-12 23:31:40 +0100210 return false;
Edward O'Callaghanaba36582022-09-05 11:09:28 +1000211
Nico Huber89569d62023-01-12 23:31:40 +0100212 msg_pdbg("Chip size %u kB is bigger than supported size %zu kB of\n"
Nico Huberbe42cf22026-02-21 17:40:44 +0100213 "chipset/board/programmer for memory-mapped interface.\n",
Nico Huber45290d02026-03-14 20:15:55 +0100214 flash->chip.total_size, flash->mst.common->max_rom_decode / KiB);
Nico Huberbe42cf22026-02-21 17:40:44 +0100215
216 if (!force) {
217 msg_cerr("This flash chip is too big for this programmer (--verbose/-V gives details).\n"
218 "Use --force/-f to override at your own risk. Any write may erase the whole\n"
219 "chip and leave the unwritten area in erased state! You have been warned.\n");
220 } else {
221 msg_cwarn("This flash chip is too big for this programmer. Any write may erase the whole\n"
222 "chip and leave the unwritten area in erased state!\n");
223 }
Nico Huber89569d62023-01-12 23:31:40 +0100224 return true;
Edward O'Callaghanaba36582022-09-05 11:09:28 +1000225}
226
Nico Hubera7050432023-02-11 18:01:26 +0100227int flashprog_classic_main(int argc, char *argv[])
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000228{
Nico Huber47412a92026-03-10 23:48:25 +0100229 const struct flashprog_chip *chip;
230 struct flashctx *fill_flash = NULL;
231 int opt, i;
232 int option_index = 0;
Felix Singer2a768e42022-05-22 02:04:18 +0200233 int operation_specified = 0;
Nico Huberd91822a2023-02-11 00:43:54 +0100234 bool force = false;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000235#if CONFIG_PRINT_WIKI == 1
Felix Singer2a768e42022-05-22 02:04:18 +0200236 bool list_supported_wiki = false;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000237#endif
Felix Singer2a768e42022-05-22 02:04:18 +0200238 bool flash_name = false, flash_size = false;
239 bool read_it = false, write_it = false, erase_it = false, verify_it = false;
240 bool dont_verify_it = false, dont_verify_all = false;
241 bool list_supported = false;
Richard Hughes842d6782021-01-15 09:48:12 +0000242 bool show_progress = false;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200243 struct flashprog_layout *layout = NULL;
Nico Hubere68b08b2023-02-11 00:00:54 +0100244 struct flashprog_programmer *prog = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000245 int ret = 0;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000246
Nico Huber99d15952016-05-02 16:54:24 +0200247 static const char optstring[] = "r:Rw:v:nNVEfc:l:i:p:Lzho:";
Mathias Krausea60faab2011-01-17 07:50:42 +0000248 static const struct option long_options[] = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000249 {"read", 1, NULL, 'r'},
250 {"write", 1, NULL, 'w'},
251 {"erase", 0, NULL, 'E'},
252 {"verify", 1, NULL, 'v'},
253 {"noverify", 0, NULL, 'n'},
Nico Huber99d15952016-05-02 16:54:24 +0200254 {"noverify-all", 0, NULL, 'N'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000255 {"chip", 1, NULL, 'c'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000256 {"verbose", 0, NULL, 'V'},
257 {"force", 0, NULL, 'f'},
258 {"layout", 1, NULL, 'l'},
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300259 {"ifd", 0, NULL, OPTION_IFD},
Arthur Heymansc82900b2018-01-10 12:48:16 +0100260 {"fmap", 0, NULL, OPTION_FMAP},
261 {"fmap-file", 1, NULL, OPTION_FMAP_FILE},
Anastasia Klimchuka7cb7e92022-11-25 18:10:43 +1100262 {"image", 1, NULL, 'i'}, // (deprecated): back compatibility.
263 {"include", 1, NULL, 'i'},
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300264 {"flash-contents", 1, NULL, OPTION_FLASH_CONTENTS},
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000265 {"flash-name", 0, NULL, OPTION_FLASH_NAME},
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000266 {"flash-size", 0, NULL, OPTION_FLASH_SIZE},
267 {"get-size", 0, NULL, OPTION_FLASH_SIZE}, // (deprecated): back compatibility.
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000268 {"list-supported", 0, NULL, 'L'},
269 {"list-supported-wiki", 0, NULL, 'z'},
270 {"programmer", 1, NULL, 'p'},
271 {"help", 0, NULL, 'h'},
272 {"version", 0, NULL, 'R'},
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000273 {"output", 1, NULL, 'o'},
Richard Hughes842d6782021-01-15 09:48:12 +0000274 {"progress", 0, NULL, OPTION_PROGRESS},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000275 {NULL, 0, NULL, 0},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000276 };
277
278 char *filename = NULL;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300279 char *referencefile = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000280 char *tempstr = NULL;
Nico Huberdf6ce9f2023-02-11 16:16:04 +0100281 struct log_args log_args = { FLASHPROG_MSG_INFO, FLASHPROG_MSG_DEBUG2, NULL };
Nico Huber34e783a2023-02-11 00:30:27 +0100282 struct flash_args flash_args = { 0 };
Nico Huberd91822a2023-02-11 00:43:54 +0100283 struct layout_args layout_args = { 0 };
Arthur Heymansb04fef92019-02-05 17:35:05 +0100284 struct layout_include_args *include_args = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000285
Nico Huberd39c7d62023-02-11 00:53:08 +0100286 if (cli_init())
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000287 exit(1);
288
289 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000290 /* FIXME: Delay all operation_specified checks until after command
291 * line parsing to allow --help overriding everything else.
292 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000293 while ((opt = getopt_long(argc, argv, optstring,
294 long_options, &option_index)) != EOF) {
295 switch (opt) {
296 case 'r':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000297 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000298 filename = strdup(optarg);
Felix Singer2a768e42022-05-22 02:04:18 +0200299 read_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000300 break;
301 case 'w':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000302 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000303 filename = strdup(optarg);
Felix Singer2a768e42022-05-22 02:04:18 +0200304 write_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000305 break;
306 case 'v':
307 //FIXME: gracefully handle superfluous -v
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000308 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000309 if (dont_verify_it) {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000310 cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000311 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000312 filename = strdup(optarg);
Felix Singer2a768e42022-05-22 02:04:18 +0200313 verify_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000314 break;
315 case 'n':
316 if (verify_it) {
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000317 cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000318 }
Felix Singer2a768e42022-05-22 02:04:18 +0200319 dont_verify_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000320 break;
Nico Huber99d15952016-05-02 16:54:24 +0200321 case 'N':
Felix Singer2a768e42022-05-22 02:04:18 +0200322 dont_verify_all = true;
Nico Huber99d15952016-05-02 16:54:24 +0200323 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000324 case 'c':
Nico Huber34e783a2023-02-11 00:30:27 +0100325 case 'p':
326 ret = cli_parse_flash_args(&flash_args, opt, optarg);
327 if (ret == 1)
328 cli_classic_abort_usage(NULL);
329 else if (ret)
330 exit(1);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000331 break;
332 case 'V':
Nico Huberdf6ce9f2023-02-11 16:16:04 +0100333 case 'o':
334 ret = cli_parse_log_args(&log_args, opt, optarg);
335 if (ret == 1)
336 cli_classic_abort_usage(NULL);
337 else if (ret)
338 exit(1);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000339 break;
340 case 'E':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000341 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200342 erase_it = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000343 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000344 case 'f':
Felix Singer2a768e42022-05-22 02:04:18 +0200345 force = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000346 break;
Nico Huberd91822a2023-02-11 00:43:54 +0100347 case OPTION_LAYOUT:
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300348 case OPTION_IFD:
Arthur Heymansc82900b2018-01-10 12:48:16 +0100349 case OPTION_FMAP:
Nico Huberd91822a2023-02-11 00:43:54 +0100350 case OPTION_FMAP_FILE:
351 ret = cli_parse_layout_args(&layout_args, opt, optarg);
352 if (ret == 1)
353 cli_classic_abort_usage(NULL);
354 else if (ret)
355 exit(1);
Arthur Heymansc82900b2018-01-10 12:48:16 +0100356 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000357 case 'i':
358 tempstr = strdup(optarg);
Arthur Heymansb04fef92019-02-05 17:35:05 +0100359 if (register_include_arg(&include_args, tempstr)) {
Stefan Taunerb8911d62012-12-26 07:55:00 +0000360 free(tempstr);
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000361 cli_classic_abort_usage(NULL);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000362 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000363 break;
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300364 case OPTION_FLASH_CONTENTS:
Angel Ponsa60f6412020-03-31 15:02:02 +0200365 if (referencefile)
366 cli_classic_abort_usage("Error: --flash-contents specified more than once."
367 "Aborting.\n");
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300368 referencefile = strdup(optarg);
369 break;
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000370 case OPTION_FLASH_NAME:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000371 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200372 flash_name = true;
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000373 break;
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000374 case OPTION_FLASH_SIZE:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000375 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200376 flash_size = true;
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000377 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000378 case 'L':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000379 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200380 list_supported = true;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000381 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000382 case 'z':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000383#if CONFIG_PRINT_WIKI == 1
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000384 cli_classic_validate_singleop(&operation_specified);
Felix Singer2a768e42022-05-22 02:04:18 +0200385 list_supported_wiki = true;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000386#else
Idwer Vollering40407b62020-12-08 00:24:39 +0100387 cli_classic_abort_usage("Error: Wiki output was not "
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000388 "compiled in. Aborting.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000389#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000390 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000391 case 'R':
392 /* print_version() is always called during startup. */
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000393 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000394 exit(0);
395 break;
396 case 'h':
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000397 cli_classic_validate_singleop(&operation_specified);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000398 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000399 exit(0);
400 break;
Richard Hughes842d6782021-01-15 09:48:12 +0000401 case OPTION_PROGRESS:
402 show_progress = true;
403 break;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000404 default:
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000405 cli_classic_abort_usage(NULL);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000406 break;
407 }
408 }
409
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000410 if (optind < argc)
411 cli_classic_abort_usage("Error: Extra parameter found.\n");
Nico Huberd91822a2023-02-11 00:43:54 +0100412 if ((read_it | write_it | verify_it) && cli_check_filename(filename, "image"))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000413 cli_classic_abort_usage(NULL);
Nico Huberd91822a2023-02-11 00:43:54 +0100414 if (referencefile && cli_check_filename(referencefile, "reference"))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000415 cli_classic_abort_usage(NULL);
Nico Huberdf6ce9f2023-02-11 16:16:04 +0100416 if (log_args.logfile && open_logfile(log_args.logfile))
Edward O'Callaghan8b60fc72019-09-26 11:17:20 +1000417 cli_classic_abort_usage(NULL);
Nico Huberdf6ce9f2023-02-11 16:16:04 +0100418 verbose_screen = log_args.screen_level;
419 verbose_logfile = log_args.logfile_level;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000420
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000421#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000422 if (list_supported_wiki) {
423 print_supported_wiki();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000424 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000425 }
426#endif
427
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000428 if (list_supported) {
Niklas Söderlundede2fa42012-10-23 13:06:46 +0000429 if (print_supported())
430 ret = 1;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000431 goto out;
432 }
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000433
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000434 start_logging();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000435
436 print_buildinfo();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000437 msg_gdbg("Command line (%i args):", argc - 1);
438 for (i = 0; i < argc; i++) {
439 msg_gdbg(" %s", argv[i]);
440 }
441 msg_gdbg("\n");
442
Nico Huber34e783a2023-02-11 00:30:27 +0100443 if (flash_args.prog_name == NULL) {
Nico Hubere68b08b2023-02-11 00:00:54 +0100444 const char *const default_programmer = CONFIG_DEFAULT_PROGRAMMER_NAME;
Thomas Heijligen84e9c912021-06-01 16:22:14 +0200445
Nico Hubere68b08b2023-02-11 00:00:54 +0100446 if (default_programmer[0]) {
447 /* We need to strdup here because we free() unconditionally later. */
Nico Huber34e783a2023-02-11 00:30:27 +0100448 flash_args.prog_name = strdup(default_programmer);
449 flash_args.prog_args = strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS);
450 if (!flash_args.prog_name || !flash_args.prog_args) {
Nico Hubere68b08b2023-02-11 00:00:54 +0100451 fprintf(stderr, "Out of memory!\n");
452 ret = 1;
453 goto out;
454 }
Stefan Tauner265fcac2014-06-02 00:12:23 +0000455 msg_pinfo("Using default programmer \"%s\" with arguments \"%s\".\n",
Nico Huber34e783a2023-02-11 00:30:27 +0100456 flash_args.prog_name, flash_args.prog_args);
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000457 } else {
458 msg_perr("Please select a programmer with the --programmer parameter.\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +0000459#if CONFIG_INTERNAL == 1
460 "To choose the mainboard of this computer use 'internal'. "
461#endif
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000462 "Valid choices are:\n");
463 list_programmers_linebreak(0, 80, 0);
Stefan Taunerb226cb12012-11-24 18:59:39 +0000464 msg_ginfo(".\n");
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000465 ret = 1;
466 goto out;
467 }
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000468 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000469
Nico Huber34e783a2023-02-11 00:30:27 +0100470 if (flashprog_programmer_init(&prog, flash_args.prog_name, flash_args.prog_args)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000471 msg_perr("Error: Programmer initialization failed.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000472 ret = 1;
Nico Huber2b66ad92023-01-11 20:15:15 +0100473 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000474 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000475 tempstr = flashbuses_to_text(get_buses_supported());
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000476 msg_pdbg("The following protocols are supported: %s.\n", tempstr);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000477 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000478
Nico Huber47412a92026-03-10 23:48:25 +0100479 switch (flashprog_flash_probe(&fill_flash, prog, flash_args.chip)) {
480 case 0:
481 break;
482 case 1:
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000483 ret = 1;
484 goto out_shutdown;
Nico Huber47412a92026-03-10 23:48:25 +0100485 case 3:
486 msg_cinfo("Please specify which chip definition to use with the -c <chipname> option.\n");
487 ret = 1;
488 goto out_shutdown;
489 case 4:
490 msg_gerr("Flash context preparation failed! (--verbose/-V may provide details).\n");
491 ret = 1;
492 goto out_shutdown;
493 case 5:
494 msg_gerr("Run flashprog -L to view the hardware supported in this flashprog version.\n");
495 ret = 1;
496 goto out_shutdown;
497 default:
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000498 msg_cinfo("No EEPROM/flash device found.\n");
Nico Huber47412a92026-03-10 23:48:25 +0100499 msg_cinfo("Note: flashprog can never write if the flash chip isn't found automatically.\n");
Nico Huber34e783a2023-02-11 00:30:27 +0100500 if (force && read_it && flash_args.chip) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000501 msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
Nico Huber47412a92026-03-10 23:48:25 +0100502 if (force_chip(&fill_flash, prog, flash_args.chip) == 0) {
503 msg_cinfo("Please note that forced reads most likely contain garbage.\n");
504 break;
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000505 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000506 }
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000507 ret = 1;
508 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000509 }
Nico Huber45290d02026-03-14 20:15:55 +0100510 chip = &fill_flash->chip;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000511
Richard Hughes842d6782021-01-15 09:48:12 +0000512 if (show_progress)
513 flashprog_set_progress_callback(fill_flash, &flashprog_progress_cb, NULL);
514
Nico Huber47412a92026-03-10 23:48:25 +0100515 print_chip_support_status(chip);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000516
Nico Huberbe42cf22026-02-21 17:40:44 +0100517 if (max_decode_exceeded(fill_flash, force)) {
518 if (!force || flashprog_limit_chip(fill_flash)) {
519 ret = 1;
520 goto out_shutdown;
521 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000522 }
523
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000524 if (!(read_it | write_it | verify_it | erase_it | flash_name | flash_size)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000525 msg_ginfo("No operations were specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000526 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000527 }
528
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000529 if (flash_name) {
Nico Huber47412a92026-03-10 23:48:25 +0100530 const char *const vendor = flashprog_chip_vendor(chip);
531 const char *const name = flashprog_chip_name(chip);
532 if (vendor && name) {
533 printf("vendor=\"%s\" name=\"%s\"\n", vendor, name);
Edward O'Callaghan0cd11d82019-09-23 22:46:12 +1000534 } else {
535 ret = -1;
536 }
537 goto out_shutdown;
538 }
539
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000540 if (flash_size) {
Nico Huber47412a92026-03-10 23:48:25 +0100541 printf("%zu\n", flashprog_chip_size(chip));
Edward O'Callaghan7d6b5262019-09-23 22:53:14 +1000542 goto out_shutdown;
543 }
544
Nico Huber0da839b2023-02-11 01:40:07 +0100545 ret = cli_process_layout_args(&layout, fill_flash, &layout_args);
546 if (ret)
Nico Huber305f4172013-06-14 11:55:26 +0200547 goto out_shutdown;
Nico Huber0da839b2023-02-11 01:40:07 +0100548 ret = process_include_args(layout, include_args);
549 if (ret)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100550 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000551
Nico Hubercfd607d2018-12-22 00:25:58 +0100552 if (layout && layout_num_regions_included(layout) == 0)
553 msg_gwarn("Warning: Layout specified but no region included!\n");
554
Nico Huberc3b02dc2023-08-12 01:13:45 +0200555 flashprog_layout_set(fill_flash, layout);
556 flashprog_flag_set(fill_flash, FLASHPROG_FLAG_FORCE, force);
Urja Rannikko7258cf52017-06-17 11:31:57 +0300557#if CONFIG_INTERNAL == 1
Nico Huberc3b02dc2023-08-12 01:13:45 +0200558 flashprog_flag_set(fill_flash, FLASHPROG_FLAG_FORCE_BOARDMISMATCH, force_boardmismatch);
Urja Rannikko7258cf52017-06-17 11:31:57 +0300559#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +0200560 flashprog_flag_set(fill_flash, FLASHPROG_FLAG_VERIFY_AFTER_WRITE, !dont_verify_it);
561 flashprog_flag_set(fill_flash, FLASHPROG_FLAG_VERIFY_WHOLE_CHIP, !dont_verify_all);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000562
Carl-Daniel Hailfinger9ad42552010-09-15 10:20:16 +0000563 /* FIXME: We should issue an unconditional chip reset here. This can be
564 * done once we have a .reset function in struct flashchip.
565 * Give the chip time to settle.
566 */
567 programmer_delay(100000);
Nico Huber899e4ec2016-04-29 18:39:01 +0200568 if (read_it)
569 ret = do_read(fill_flash, filename);
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +1100570 else if (erase_it) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200571 ret = flashprog_flash_erase(fill_flash);
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +1100572 /*
573 * FIXME: Do we really want the scary warning if erase failed?
574 * After all, after erase the chip is either blank or partially
575 * blank or it has the old contents. A blank chip won't boot,
576 * so if the user wanted erase and reboots afterwards, the user
577 * knows very well that booting won't work.
578 */
579 if (ret)
580 emergency_help_message();
581 }
Nico Huber899e4ec2016-04-29 18:39:01 +0200582 else if (write_it)
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300583 ret = do_write(fill_flash, filename, referencefile);
Nico Huber899e4ec2016-04-29 18:39:01 +0200584 else if (verify_it)
585 ret = do_verify(fill_flash, filename);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000586
587out_shutdown:
Nico Huber47412a92026-03-10 23:48:25 +0100588 flashprog_layout_release(layout);
589 flashprog_flash_release(fill_flash);
Nico Hubere68b08b2023-02-11 00:00:54 +0100590 flashprog_programmer_shutdown(prog);
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000591out:
Nico Huber345f65a2021-05-14 01:11:08 +0200592 cleanup_include_args(&include_args);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000593 free(filename);
Paul Kocialkowskif701f342018-01-15 01:10:36 +0300594 free(referencefile);
Nico Huberd91822a2023-02-11 00:43:54 +0100595 free(layout_args.fmapfile);
596 free(layout_args.layoutfile);
Nico Huber34e783a2023-02-11 00:30:27 +0100597 free(flash_args.prog_args);
598 free(flash_args.prog_name);
599 free(flash_args.chip);
Nico Huberdf6ce9f2023-02-11 16:16:04 +0100600 free(log_args.logfile);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000601 ret |= close_logfile();
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000602 return ret;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000603}