/*
* This file is part of the flashrom project.
*
* Copyright (C) 2000 Silicon Integrated System Corporation
* Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
* Copyright (C) 2005-2008 coresystems GmbH
* Copyright (C) 2008,2009,2010 Carl-Daniel Hailfinger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <getopt.h>
#include "flash.h"
#include "flashchips.h"
#include "fmap.h"
#include "programmer.h"
#include "libflashrom.h"
static void cli_classic_usage(const char *name)
{
printf("Usage: %s [-h|-R|-L|"
#if CONFIG_PRINT_WIKI == 1
"-z|"
#endif
"\n\t-p <programmername>[:<parameters>] [-c <chipname>]\n"
"\t\t(--flash-name|--flash-size|\n"
"\t\t [-E|-x|(-r|-w|-v) <file>]\n"
"\t\t [(-l <layoutfile>|--ifd| --fmap|--fmap-file <file>) [-i <region>[:<file>]]...]\n"
"\t\t [-n] [-N] [-f])]\n"
"\t[-V[V[V]]] [-o <logfile>]\n\n", name);
printf(" -h | --help print this help text\n"
" -R | --version print version (release)\n"
" -r | --read <file> read flash and save to <file>\n"
" -w | --write (<file>|-) write <file> or the content provided\n"
" on the standard input to flash\n"
" -v | --verify (<file>|-) verify flash against <file>\n"
" or the content provided on the standard input\n"
" -E | --erase erase flash memory\n"
" -V | --verbose more verbose output\n"
" -c | --chip <chipname> probe only for specified flash chip\n"
" -f | --force force specific operations (see man page)\n"
" -n | --noverify don't auto-verify\n"
" -N | --noverify-all verify included regions only (cf. -i)\n"
" -x | --extract extract regions to files\n"
" -l | --layout <layoutfile> read ROM layout from <layoutfile>\n"
" --wp-disable disable write protection\n"
" --wp-enable enable write protection\n"
" --wp-list list supported write protection ranges\n"
" --wp-status show write protection status\n"
" --wp-range=<start>,<len> set write protection range (use --wp-range=0,0\n"
" to unprotect the entire flash)\n"
" --wp-region <region> set write protection region\n"
" --flash-name read out the detected flash name\n"
" --flash-size read out the detected flash size\n"
" --fmap read ROM layout from fmap embedded in ROM\n"
" --fmap-file <fmapfile> read ROM layout from fmap in <fmapfile>\n"
" --ifd read layout from an Intel Firmware Descriptor\n"
" -i | --include <region>[:<file>] only read/write image <region> from layout\n"
" (optionally with data from <file>)\n"
" --image <region>[:<file>] deprecated, please use --include\n"
" -o | --output <logfile> log output to <logfile>\n"
" --flash-contents <ref-file> assume flash contents to be <ref-file>\n"
" -L | --list-supported print supported devices\n"
#if CONFIG_PRINT_WIKI == 1
" -z | --list-supported-wiki print supported devices in wiki syntax\n"
#endif
" --progress show progress percentage on the standard output\n"
" -p | --programmer <name>[:<param>] specify the programmer device. One of\n");
list_programmers_linebreak(4, 80, 0);
printf(".\n\nYou can specify one of -h, -R, -L, "
#if CONFIG_PRINT_WIKI == 1
"-z, "
#endif
"-E, -r, -w, -v or no operation.\n"
"If no operation is specified, flashrom will only probe for flash chips.\n");
}
static void cli_classic_abort_usage(const char *msg)
{
if (msg)
fprintf(stderr, "%s", msg);
printf("Please run \"flashrom --help\" for usage info.\n");
exit(1);
}
static void cli_classic_validate_singleop(int *operation_specified)
{
if (++(*operation_specified) > 1) {
cli_classic_abort_usage("More than one operation specified. Aborting.\n");
}
}
static int check_filename(char *filename, const char *type)
{
if (!filename || (filename[0] == '\0')) {
fprintf(stderr, "Error: No %s file specified.\n", type);
return 1;
}
/* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
if (filename[0] == '-' && filename[1] != '\0')
fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
return 0;
}
/* Ensure a file is open by means of fstat */
static bool check_file(FILE *file)
{
struct stat statbuf;
if (fstat(fileno(file), &statbuf) < 0)
return false;
return true;
}
static int parse_wp_range(unsigned int *start, unsigned int *len)
{
char *endptr = NULL, *token = NULL;
if (!optarg) {
msg_gerr("Error: No wp-range values provided\n");
return -1;
}
token = strtok(optarg, ",");
if (!token) {
msg_gerr("Error: Invalid wp-range argument format\n");
return -1;
}
*start = strtoul(token, &endptr, 0);
token = strtok(NULL, ",");
if (!token) {
msg_gerr("Error: Invalid wp-range argument format\n");
return -1;
}
*len = strtoul(token, &endptr, 0);
return 0;
}
static int print_wp_range(struct flashrom_flashctx *flash, size_t start, size_t len)
{
/* Start address and length */
msg_ginfo("start=0x%08zx length=0x%08zx ", start, len);
/* Easily readable description like 'none' or 'lower 1/8' */
size_t chip_len = flashrom_flash_getsize(flash);
if (len == 0) {
msg_ginfo("(none)");
} else if (len == chip_len) {
msg_ginfo("(all)");
} else {
const char *location = "";
if (start == 0)
location = "lower ";
if (start == chip_len - len)
location = "upper ";
/* Remove common factors of 2 to simplify */
/* the (range_len/chip_len) fraction. */
while ((chip_len % 2) == 0 && (len % 2) == 0) {
chip_len /= 2;
len /= 2;
}
msg_ginfo("(%s%zu/%zu)", location, len, chip_len);
}
return 0;
}
static const char *get_wp_error_str(int err)
{
switch (err) {
case FLASHROM_WP_ERR_CHIP_UNSUPPORTED:
return "WP operations are not implemented for this chip";
case FLASHROM_WP_ERR_READ_FAILED:
return "failed to read the current WP configuration";
case FLASHROM_WP_ERR_WRITE_FAILED:
return "failed to write the new WP configuration";
case FLASHROM_WP_ERR_VERIFY_FAILED:
return "unexpected WP configuration read back from chip";
case FLASHROM_WP_ERR_MODE_UNSUPPORTED:
return "the requested protection mode is not supported";
case FLASHROM_WP_ERR_RANGE_UNSUPPORTED:
return "the requested protection range is not supported";
case FLASHROM_WP_ERR_RANGE_LIST_UNAVAILABLE:
return "could not determine what protection ranges are available";
case FLASHROM_WP_ERR_UNSUPPORTED_STATE:
return "can't operate on current WP configuration of the chip";
}
return "unknown WP error";
}
static int wp_cli(
struct flashctx *flash,
bool enable_wp,
bool disable_wp,
bool print_wp_status,
bool print_wp_ranges,
bool set_wp_range,
uint32_t wp_start,
uint32_t wp_len)
{
if (print_wp_ranges) {
struct flashrom_wp_ranges *list;
enum flashrom_wp_result ret = flashrom_wp_get_available_ranges(&list, flash);
if (ret != FLASHROM_WP_OK) {