1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2000 Silicon Integrated System Corporation
5  * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
6  * Copyright (C) 2005-2008 coresystems GmbH
7  * Copyright (C) 2008,2009,2010 Carl-Daniel Hailfinger
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <getopt.h>
26 #include "flash.h"
27 #include "flashchips.h"
28 #include "fmap.h"
29 #include "programmer.h"
30 #include "libflashrom.h"
31 
cli_classic_usage(const char * name)32 static void cli_classic_usage(const char *name)
33 {
34 	printf("Usage: %s [-h|-R|-L|"
35 #if CONFIG_PRINT_WIKI == 1
36 	       "-z|"
37 #endif
38 	       "\n\t-p <programmername>[:<parameters>] [-c <chipname>]\n"
39 	       "\t\t(--flash-name|--flash-size|\n"
40 	       "\t\t [-E|(-r|-w|-v) <file>]\n"
41 	       "\t\t [(-l <layoutfile>|--ifd| --fmap|--fmap-file <file>) [-i <imagename>]...]\n"
42 	       "\t\t [-n] [-N] [-f])]\n"
43 	       "\t[-V[V[V]]] [-o <logfile>]\n\n", name);
44 
45 	printf(" -h | --help                        print this help text\n"
46 	       " -R | --version                     print version (release)\n"
47 	       " -r | --read <file>                 read flash and save to <file>\n"
48 	       " -w | --write <file>                write <file> to flash\n"
49 	       " -v | --verify <file>               verify flash against <file>\n"
50 	       " -E | --erase                       erase flash memory\n"
51 	       " -V | --verbose                     more verbose output\n"
52 	       " -c | --chip <chipname>             probe only for specified flash chip\n"
53 	       " -f | --force                       force specific operations (see man page)\n"
54 	       " -n | --noverify                    don't auto-verify\n"
55 	       " -N | --noverify-all                verify included regions only (cf. -i)\n"
56 	       " -l | --layout <layoutfile>         read ROM layout from <layoutfile>\n"
57 	       "      --flash-name                  read out the detected flash name\n"
58 	       "      --flash-size                  read out the detected flash size\n"
59 	       "      --fmap                        read ROM layout from fmap embedded in ROM\n"
60 	       "      --fmap-file <fmapfile>        read ROM layout from fmap in <fmapfile>\n"
61 	       "      --ifd                         read layout from an Intel Firmware Descriptor\n"
62 	       " -i | --image <name>                only flash image <name> from flash layout\n"
63 	       " -o | --output <logfile>            log output to <logfile>\n"
64 	       "      --flash-contents <ref-file>   assume flash contents to be <ref-file>\n"
65 	       " -L | --list-supported              print supported devices\n"
66 #if CONFIG_PRINT_WIKI == 1
67 	       " -z | --list-supported-wiki         print supported devices in wiki syntax\n"
68 #endif
69 	       " -p | --programmer <name>[:<param>] specify the programmer device. One of\n");
70 	list_programmers_linebreak(4, 80, 0);
71 	printf(".\n\nYou can specify one of -h, -R, -L, "
72 #if CONFIG_PRINT_WIKI == 1
73 	         "-z, "
74 #endif
75 	         "-E, -r, -w, -v or no operation.\n"
76 	       "If no operation is specified, flashrom will only probe for flash chips.\n");
77 }
78 
cli_classic_abort_usage(const char * msg)79 static void cli_classic_abort_usage(const char *msg)
80 {
81 	if (msg)
82 		fprintf(stderr, "%s", msg);
83 	printf("Please run \"flashrom --help\" for usage info.\n");
84 	exit(1);
85 }
86 
cli_classic_validate_singleop(int * operation_specified)87 static void cli_classic_validate_singleop(int *operation_specified)
88 {
89 	if (++(*operation_specified) > 1) {
90 		cli_classic_abort_usage("More than one operation specified. Aborting.\n");
91 	}
92 }
93 
check_filename(char * filename,const char * type)94 static int check_filename(char *filename, const char *type)
95 {
96 	if (!filename || (filename[0] == '\0')) {
97 		fprintf(stderr, "Error: No %s file specified.\n", type);
98 		return 1;
99 	}
100 	/* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
101 	if (filename[0] == '-')
102 		fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
103 	return 0;
104 }
105 
main(int argc,char * argv[])106 int main(int argc, char *argv[])
107 {
108 	const struct flashchip *chip = NULL;
109 	/* Probe for up to eight flash chips. */
110 	struct flashctx flashes[8] = {{0}};
111 	struct flashctx *fill_flash;
112 	const char *name;
113 	int namelen, opt, i, j;
114 	int startchip = -1, chipcount = 0, option_index = 0, force = 0, ifd = 0, fmap = 0;
115 #if CONFIG_PRINT_WIKI == 1
116 	int list_supported_wiki = 0;
117 #endif
118 	int flash_name = 0, flash_size = 0;
119 	int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
120 	int dont_verify_it = 0, dont_verify_all = 0, list_supported = 0, operation_specified = 0;
121 	struct flashrom_layout *layout = NULL;
122 	enum programmer prog = PROGRAMMER_INVALID;
123 	enum {
124 		OPTION_IFD = 0x0100,
125 		OPTION_FMAP,
126 		OPTION_FMAP_FILE,
127 		OPTION_FLASH_CONTENTS,
128 		OPTION_FLASH_NAME,
129 		OPTION_FLASH_SIZE,
130 	};
131 	int ret = 0;
132 
133 	static const char optstring[] = "r:Rw:v:nNVEfc:l:i:p:Lzho:";
134 	static const struct option long_options[] = {
135 		{"read",		1, NULL, 'r'},
136 		{"write",		1, NULL, 'w'},
137 		{"erase",		0, NULL, 'E'},
138 		{"verify",		1, NULL, 'v'},
139 		{"noverify",		0, NULL, 'n'},
140 		{"noverify-all",	0, NULL, 'N'},
141 		{"chip",		1, NULL, 'c'},
142 		{"verbose",		0, NULL, 'V'},
143 		{"force",		0, NULL, 'f'},
144 		{"layout",		1, NULL, 'l'},
145 		{"ifd",			0, NULL, OPTION_IFD},
146 		{"fmap",		0, NULL, OPTION_FMAP},
147 		{"fmap-file",		1, NULL, OPTION_FMAP_FILE},
148 		{"image",		1, NULL, 'i'},
149 		{"flash-contents",	1, NULL, OPTION_FLASH_CONTENTS},
150 		{"flash-name",		0, NULL, OPTION_FLASH_NAME},
151 		{"flash-size",		0, NULL, OPTION_FLASH_SIZE},
152 		{"get-size",		0, NULL, OPTION_FLASH_SIZE}, // (deprecated): back compatibility.
153 		{"list-supported",	0, NULL, 'L'},
154 		{"list-supported-wiki",	0, NULL, 'z'},
155 		{"programmer",		1, NULL, 'p'},
156 		{"help",		0, NULL, 'h'},
157 		{"version",		0, NULL, 'R'},
158 		{"output",		1, NULL, 'o'},
159 		{NULL,			0, NULL, 0},
160 	};
161 
162 	char *filename = NULL;
163 	char *referencefile = NULL;
164 	char *layoutfile = NULL;
165 	char *fmapfile = NULL;
166 #ifndef STANDALONE
167 	char *logfile = NULL;
168 #endif /* !STANDALONE */
169 	char *tempstr = NULL;
170 	char *pparam = NULL;
171 	struct layout_include_args *include_args = NULL;
172 
173 	flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb);
174 
175 	print_version();
176 	print_banner();
177 
178 	if (selfcheck())
179 		exit(1);
180 
181 	setbuf(stdout, NULL);
182 	/* FIXME: Delay all operation_specified checks until after command
183 	 * line parsing to allow --help overriding everything else.
184 	 */
185 	while ((opt = getopt_long(argc, argv, optstring,
186 				  long_options, &option_index)) != EOF) {
187 		switch (opt) {
188 		case 'r':
189 			cli_classic_validate_singleop(&operation_specified);
190 			filename = strdup(optarg);
191 			read_it = 1;
192 			break;
193 		case 'w':
194 			cli_classic_validate_singleop(&operation_specified);
195 			filename = strdup(optarg);
196 			write_it = 1;
197 			break;
198 		case 'v':
199 			//FIXME: gracefully handle superfluous -v
200 			cli_classic_validate_singleop(&operation_specified);
201 			if (dont_verify_it) {
202 				cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
203 			}
204 			filename = strdup(optarg);
205 			verify_it = 1;
206 			break;
207 		case 'n':
208 			if (verify_it) {
209 				cli_classic_abort_usage("--verify and --noverify are mutually exclusive. Aborting.\n");
210 			}
211 			dont_verify_it = 1;
212 			break;
213 		case 'N':
214 			dont_verify_all = 1;
215 			break;
216 		case 'c':
217 			chip_to_probe = strdup(optarg);
218 			break;
219 		case 'V':
220 			verbose_screen++;
221 			if (verbose_screen > FLASHROM_MSG_DEBUG2)
222 				verbose_logfile = verbose_screen;
223 			break;
224 		case 'E':
225 			cli_classic_validate_singleop(&operation_specified);
226 			erase_it = 1;
227 			break;
228 		case 'f':
229 			force = 1;
230 			break;
231 		case 'l':
232 			if (layoutfile)
233 				cli_classic_abort_usage("Error: --layout specified more than once. Aborting.\n");
234 			if (ifd)
235 				cli_classic_abort_usage("Error: --layout and --ifd both specified. Aborting.\n");
236 			if (fmap)
237 				cli_classic_abort_usage("Error: --layout and --fmap-file both specified. Aborting.\n");
238 			layoutfile = strdup(optarg);
239 			break;
240 		case OPTION_IFD:
241 			if (layoutfile)
242 				cli_classic_abort_usage("Error: --layout and --ifd both specified. Aborting.\n");
243 			if (fmap)
244 				cli_classic_abort_usage("Error: --fmap-file and --ifd both specified. Aborting.\n");
245 			ifd = 1;
246 			break;
247 		case OPTION_FMAP_FILE:
248 			if (fmap)
249 				cli_classic_abort_usage("Error: --fmap or --fmap-file specified "
250 					"more than once. Aborting.\n");
251 			if (ifd)
252 				cli_classic_abort_usage("Error: --fmap-file and --ifd both specified. Aborting.\n");
253 			if (layoutfile)
254 				cli_classic_abort_usage("Error: --fmap-file and --layout both specified. Aborting.\n");
255 			fmapfile = strdup(optarg);
256 			fmap = 1;
257 			break;
258 		case OPTION_FMAP:
259 			if (fmap)
260 				cli_classic_abort_usage("Error: --fmap or --fmap-file specified "
261 					"more than once. Aborting.\n");
262 			if (ifd)
263 				cli_classic_abort_usage("Error: --fmap and --ifd both specified. Aborting.\n");
264 			if (layoutfile)
265 				cli_classic_abort_usage("Error: --layout and --fmap both specified. Aborting.\n");
266 			fmap = 1;
267 			break;
268 		case 'i':
269 			tempstr = strdup(optarg);
270 			if (register_include_arg(&include_args, tempstr)) {
271 				free(tempstr);
272 				cli_classic_abort_usage(NULL);
273 			}
274 			break;
275 		case OPTION_FLASH_CONTENTS:
276 			referencefile = strdup(optarg);
277 			break;
278 		case OPTION_FLASH_NAME:
279 			cli_classic_validate_singleop(&operation_specified);
280 			flash_name = 1;
281 			break;
282 		case OPTION_FLASH_SIZE:
283 			cli_classic_validate_singleop(&operation_specified);
284 			flash_size = 1;
285 			break;
286 		case 'L':
287 			cli_classic_validate_singleop(&operation_specified);
288 			list_supported = 1;
289 			break;
290 		case 'z':
291 #if CONFIG_PRINT_WIKI == 1
292 			cli_classic_validate_singleop(&operation_specified);
293 			list_supported_wiki = 1;
294 #else
295 			cli_classic_abort_usage("Error: Wiki output was not"
296 					"compiled in. Aborting.\n");
297 #endif
298 			break;
299 		case 'p':
300 			if (prog != PROGRAMMER_INVALID) {
301 				cli_classic_abort_usage("Error: --programmer specified "
302 					"more than once. You can separate "
303 					"multiple\nparameters for a programmer "
304 					"with \",\". Please see the man page "
305 					"for details.\n");
306 			}
307 			for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
308 				name = programmer_table[prog].name;
309 				namelen = strlen(name);
310 				if (strncmp(optarg, name, namelen) == 0) {
311 					switch (optarg[namelen]) {
312 					case ':':
313 						pparam = strdup(optarg + namelen + 1);
314 						if (!strlen(pparam)) {
315 							free(pparam);
316 							pparam = NULL;
317 						}
318 						break;
319 					case '\0':
320 						break;
321 					default:
322 						/* The continue refers to the
323 						 * for loop. It is here to be
324 						 * able to differentiate between
325 						 * foo and foobar.
326 						 */
327 						continue;
328 					}
329 					break;
330 				}
331 			}
332 			if (prog == PROGRAMMER_INVALID) {
333 				fprintf(stderr, "Error: Unknown programmer \"%s\". Valid choices are:\n",
334 					optarg);
335 				list_programmers_linebreak(0, 80, 0);
336 				msg_ginfo(".\n");
337 				cli_classic_abort_usage(NULL);
338 			}
339 			break;
340 		case 'R':
341 			/* print_version() is always called during startup. */
342 			cli_classic_validate_singleop(&operation_specified);
343 			exit(0);
344 			break;
345 		case 'h':
346 			cli_classic_validate_singleop(&operation_specified);
347 			cli_classic_usage(argv[0]);
348 			exit(0);
349 			break;
350 		case 'o':
351 #ifdef STANDALONE
352 			cli_classic_abort_usage("Log file not supported in standalone mode. Aborting.\n");
353 #else /* STANDALONE */
354 			if (logfile) {
355 				fprintf(stderr, "Warning: -o/--output specified multiple times.\n");
356 				free(logfile);
357 			}
358 
359 			logfile = strdup(optarg);
360 			if (logfile[0] == '\0') {
361 				cli_classic_abort_usage("No log filename specified.\n");
362 			}
363 #endif /* STANDALONE */
364 			break;
365 		default:
366 			cli_classic_abort_usage(NULL);
367 			break;
368 		}
369 	}
370 
371 	if (optind < argc)
372 		cli_classic_abort_usage("Error: Extra parameter found.\n");
373 	if ((read_it | write_it | verify_it) && check_filename(filename, "image"))
374 		cli_classic_abort_usage(NULL);
375 	if (layoutfile && check_filename(layoutfile, "layout"))
376 		cli_classic_abort_usage(NULL);
377 	if (fmapfile && check_filename(fmapfile, "fmap"))
378 		cli_classic_abort_usage(NULL);
379 	if (referencefile && check_filename(referencefile, "reference"))
380 		cli_classic_abort_usage(NULL);
381 
382 #ifndef STANDALONE
383 	if (logfile && check_filename(logfile, "log"))
384 		cli_classic_abort_usage(NULL);
385 	if (logfile && open_logfile(logfile))
386 		cli_classic_abort_usage(NULL);
387 #endif /* !STANDALONE */
388 
389 #if CONFIG_PRINT_WIKI == 1
390 	if (list_supported_wiki) {
391 		print_supported_wiki();
392 		goto out;
393 	}
394 #endif
395 
396 	if (list_supported) {
397 		if (print_supported())
398 			ret = 1;
399 		goto out;
400 	}
401 
402 #ifndef STANDALONE
403 	start_logging();
404 #endif /* !STANDALONE */
405 
406 	print_buildinfo();
407 	msg_gdbg("Command line (%i args):", argc - 1);
408 	for (i = 0; i < argc; i++) {
409 		msg_gdbg(" %s", argv[i]);
410 	}
411 	msg_gdbg("\n");
412 
413 	if (layoutfile && read_romlayout(layoutfile)) {
414 		ret = 1;
415 		goto out;
416 	}
417 
418 	if (!ifd && !fmap && process_include_args(get_global_layout(), include_args)) {
419 		ret = 1;
420 		goto out;
421 	}
422 	/* Does a chip with the requested name exist in the flashchips array? */
423 	if (chip_to_probe) {
424 		for (chip = flashchips; chip && chip->name; chip++)
425 			if (!strcmp(chip->name, chip_to_probe))
426 				break;
427 		if (!chip || !chip->name) {
428 			msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe);
429 			msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n");
430 			ret = 1;
431 			goto out;
432 		}
433 		/* Keep chip around for later usage in case a forced read is requested. */
434 	}
435 
436 	if (prog == PROGRAMMER_INVALID) {
437 		if (CONFIG_DEFAULT_PROGRAMMER != PROGRAMMER_INVALID) {
438 			prog = CONFIG_DEFAULT_PROGRAMMER;
439 			/* We need to strdup here because we free(pparam) unconditionally later. */
440 			pparam = strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS);
441 			msg_pinfo("Using default programmer \"%s\" with arguments \"%s\".\n",
442 				  programmer_table[CONFIG_DEFAULT_PROGRAMMER].name, pparam);
443 		} else {
444 			msg_perr("Please select a programmer with the --programmer parameter.\n"
445 #if CONFIG_INTERNAL == 1
446 				 "To choose the mainboard of this computer use 'internal'. "
447 #endif
448 				 "Valid choices are:\n");
449 			list_programmers_linebreak(0, 80, 0);
450 			msg_ginfo(".\n");
451 			ret = 1;
452 			goto out;
453 		}
454 	}
455 
456 	/* FIXME: Delay calibration should happen in programmer code. */
457 	myusec_calibrate_delay();
458 
459 	if (programmer_init(prog, pparam)) {
460 		msg_perr("Error: Programmer initialization failed.\n");
461 		ret = 1;
462 		goto out_shutdown;
463 	}
464 	tempstr = flashbuses_to_text(get_buses_supported());
465 	msg_pdbg("The following protocols are supported: %s.\n", tempstr);
466 	free(tempstr);
467 
468 	for (j = 0; j < registered_master_count; j++) {
469 		startchip = 0;
470 		while (chipcount < (int)ARRAY_SIZE(flashes)) {
471 			startchip = probe_flash(&registered_masters[j], startchip, &flashes[chipcount], 0);
472 			if (startchip == -1)
473 				break;
474 			chipcount++;
475 			startchip++;
476 		}
477 	}
478 
479 	if (chipcount > 1) {
480 		msg_cinfo("Multiple flash chip definitions match the detected chip(s): \"%s\"",
481 			  flashes[0].chip->name);
482 		for (i = 1; i < chipcount; i++)
483 			msg_cinfo(", \"%s\"", flashes[i].chip->name);
484 		msg_cinfo("\nPlease specify which chip definition to use with the -c <chipname> option.\n");
485 		ret = 1;
486 		goto out_shutdown;
487 	} else if (!chipcount) {
488 		msg_cinfo("No EEPROM/flash device found.\n");
489 		if (!force || !chip_to_probe) {
490 			msg_cinfo("Note: flashrom can never write if the flash chip isn't found "
491 				  "automatically.\n");
492 		}
493 		if (force && read_it && chip_to_probe) {
494 			struct registered_master *mst;
495 			int compatible_masters = 0;
496 			msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
497 			/* This loop just counts compatible controllers. */
498 			for (j = 0; j < registered_master_count; j++) {
499 				mst = &registered_masters[j];
500 				/* chip is still set from the chip_to_probe earlier in this function. */
501 				if (mst->buses_supported & chip->bustype)
502 					compatible_masters++;
503 			}
504 			if (!compatible_masters) {
505 				msg_cinfo("No compatible controller found for the requested flash chip.\n");
506 				ret = 1;
507 				goto out_shutdown;
508 			}
509 			if (compatible_masters > 1)
510 				msg_cinfo("More than one compatible controller found for the requested flash "
511 					  "chip, using the first one.\n");
512 			for (j = 0; j < registered_master_count; j++) {
513 				mst = &registered_masters[j];
514 				startchip = probe_flash(mst, 0, &flashes[0], 1);
515 				if (startchip != -1)
516 					break;
517 			}
518 			if (startchip == -1) {
519 				// FIXME: This should never happen! Ask for a bug report?
520 				msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe);
521 				ret = 1;
522 				goto out_shutdown;
523 			}
524 			if (map_flash(&flashes[0]) != 0) {
525 				free(flashes[0].chip);
526 				ret = 1;
527 				goto out_shutdown;
528 			}
529 			msg_cinfo("Please note that forced reads most likely contain garbage.\n");
530 			ret = read_flash_to_file(&flashes[0], filename);
531 			unmap_flash(&flashes[0]);
532 			free(flashes[0].chip);
533 			goto out_shutdown;
534 		}
535 		ret = 1;
536 		goto out_shutdown;
537 	} else if (!chip_to_probe) {
538 		/* repeat for convenience when looking at foreign logs */
539 		tempstr = flashbuses_to_text(flashes[0].chip->bustype);
540 		msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
541 			 flashes[0].chip->vendor, flashes[0].chip->name, flashes[0].chip->total_size, tempstr);
542 		free(tempstr);
543 	}
544 
545 	fill_flash = &flashes[0];
546 
547 	print_chip_support_status(fill_flash->chip);
548 
549 	unsigned int limitexceeded = count_max_decode_exceedings(fill_flash);
550 	if (limitexceeded > 0 && !force) {
551 		enum chipbustype commonbuses = fill_flash->mst->buses_supported & fill_flash->chip->bustype;
552 
553 		/* Sometimes chip and programmer have more than one bus in common,
554 		 * and the limit is not exceeded on all buses. Tell the user. */
555 		if ((bitcount(commonbuses) > limitexceeded)) {
556 			msg_pdbg("There is at least one interface available which could support the size of\n"
557 				 "the selected flash chip.\n");
558 		}
559 		msg_cerr("This flash chip is too big for this programmer (--verbose/-V gives details).\n"
560 			 "Use --force/-f to override at your own risk.\n");
561 		ret = 1;
562 		goto out_shutdown;
563 	}
564 
565 	if (!(read_it | write_it | verify_it | erase_it | flash_name | flash_size)) {
566 		msg_ginfo("No operations were specified.\n");
567 		goto out_shutdown;
568 	}
569 
570 	if (flash_name) {
571 		if (fill_flash->chip->vendor && fill_flash->chip->name) {
572 			printf("vendor=\"%s\" name=\"%s\"\n",
573 				fill_flash->chip->vendor,
574 				fill_flash->chip->name);
575 		} else {
576 			ret = -1;
577 		}
578 		goto out_shutdown;
579 	}
580 
581 	if (flash_size) {
582 		printf("%d\n", fill_flash->chip->total_size * 1024);
583 		goto out_shutdown;
584 	}
585 
586 	if (layoutfile) {
587 		layout = get_global_layout();
588 	} else if (ifd && (flashrom_layout_read_from_ifd(&layout, fill_flash, NULL, 0) ||
589 			   process_include_args(layout, include_args))) {
590 		ret = 1;
591 		goto out_shutdown;
592 	} else if (fmap && fmapfile) {
593 		struct stat s;
594 		if (stat(fmapfile, &s) != 0) {
595 			msg_gerr("Failed to stat fmapfile \"%s\"\n", fmapfile);
596 			ret = 1;
597 			goto out_shutdown;
598 		}
599 
600 		size_t fmapfile_size = s.st_size;
601 		uint8_t *fmapfile_buffer = malloc(fmapfile_size);
602 		if (!fmapfile_buffer) {
603 			ret = 1;
604 			goto out_shutdown;
605 		}
606 
607 		if (read_buf_from_file(fmapfile_buffer, fmapfile_size, fmapfile)) {
608 			ret = 1;
609 			free(fmapfile_buffer);
610 			goto out_shutdown;
611 		}
612 
613 		if (flashrom_layout_read_fmap_from_buffer(&layout, fill_flash, fmapfile_buffer, fmapfile_size) ||
614 		    process_include_args(layout, include_args)) {
615 			ret = 1;
616 			free(fmapfile_buffer);
617 			goto out_shutdown;
618 		}
619 		free(fmapfile_buffer);
620 	} else if (fmap && (flashrom_layout_read_fmap_from_rom(&layout, fill_flash, 0,
621 		       fill_flash->chip->total_size * 1024) || process_include_args(layout, include_args))) {
622 		ret = 1;
623 		goto out_shutdown;
624 	}
625 
626 	flashrom_layout_set(fill_flash, layout);
627 	flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE, !!force);
628 #if CONFIG_INTERNAL == 1
629 	flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE_BOARDMISMATCH, !!force_boardmismatch);
630 #endif
631 	flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_AFTER_WRITE, !dont_verify_it);
632 	flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_WHOLE_CHIP, !dont_verify_all);
633 
634 	/* FIXME: We should issue an unconditional chip reset here. This can be
635 	 * done once we have a .reset function in struct flashchip.
636 	 * Give the chip time to settle.
637 	 */
638 	programmer_delay(100000);
639 	if (read_it)
640 		ret = do_read(fill_flash, filename);
641 	else if (erase_it)
642 		ret = do_erase(fill_flash);
643 	else if (write_it)
644 		ret = do_write(fill_flash, filename, referencefile);
645 	else if (verify_it)
646 		ret = do_verify(fill_flash, filename);
647 
648 	flashrom_layout_release(layout);
649 
650 out_shutdown:
651 	programmer_shutdown();
652 out:
653 	for (i = 0; i < chipcount; i++)
654 		free(flashes[i].chip);
655 
656 	layout_cleanup(&include_args);
657 	free(filename);
658 	free(fmapfile);
659 	free(referencefile);
660 	free(layoutfile);
661 	free(pparam);
662 	/* clean up global variables */
663 	free((char *)chip_to_probe); /* Silence! Freeing is not modifying contents. */
664 	chip_to_probe = NULL;
665 #ifndef STANDALONE
666 	free(logfile);
667 	ret |= close_logfile();
668 #endif /* !STANDALONE */
669 	return ret;
670 }
671