1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*
* This file is part of the flashrom project.
*
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
* Copyright (C) 2009 Carl-Daniel Hailfinger
* Copyright (C) 2011-2014 Stefan Tauner
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/stat.h>
#include "flash.h"
#include "cli.h"
int cli_check_filename(const char *const filename, const char *const 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 cli_check_file(FILE *file)
{
struct stat statbuf;
if (fstat(fileno(file), &statbuf) < 0)
return false;
return true;
}
int cli_init(void)
{
/*
* Safety-guard against a user who has (mistakenly) closed
* stdout or stderr before exec'ing flashprog. We disable
* logging in this case to prevent writing log data to a flash
* chip when a flash device gets opened with fd 1 or 2.
*/
if (cli_check_file(stdout) && cli_check_file(stderr)) {
flashprog_set_log_callback((flashprog_log_callback *)&flashprog_print_cb);
}
print_version();
print_banner();
return flashprog_init(/* perform_selfcheck => */1);
}
int cli_parse_log_args(struct log_args *const args, const int opt, const char *const opt_arg)
{
switch (opt) {
case OPTION_VERBOSE:
args->screen_level++;
if (args->screen_level > args->logfile_level)
args->logfile_level = args->screen_level;
break;
case OPTION_LOGFILE:
if (cli_check_filename(opt_arg, "log"))
return 1;
if (args->logfile) {
fprintf(stderr, "Warning: -o/--output specified multiple times.\n");
free(args->logfile);
}
args->logfile = strdup(opt_arg);
if (!args->logfile) {
fprintf(stderr, "Out of memory!\n");
return 2;
}
break;
}
return 0;
}
int cli_parse_flash_args(struct flash_args *const args, const int opt, const char *const opt_arg)
{
switch (opt) {
case OPTION_PROGRAMMER:
if (args->prog_name) {
fprintf(stderr,
"Error: --programmer specified more than once. You can separate multiple\n"
"arguments for a programmer with ','. Please see the man page for details.\n");
return 1;
}
const char *const colon = strchr(opt_arg, ':');
if (colon) {
args->prog_name = strndup(opt_arg, colon - opt_arg);
args->prog_args = strdup(colon + 1);
} else {
args->prog_name = strdup(opt_arg);
}
if (!args->prog_name || (colon && !args->prog_args)) {
fprintf(stderr, "Out of memory!\n");
return 2;
}
break;
case OPTION_CHIP:
if (args->chip) {
fprintf(stderr, "Error: --chip specified more than once.\n");
return 1;
}
args->chip = strdup(opt_arg);
if (!args->chip) {
fprintf(stderr, "Out of memory!\n");
return 2;
}
break;
}
return 0;
}
int cli_parse_layout_args(struct layout_args *const args, const int opt, const char *const opt_arg)
{
if (args->layoutfile || args->ifd || args->fmap || args->fmapfile) {
fprintf(stderr, "Error: Only one layout source may be specified.\n");
return 1;
}
switch (opt) {
case OPTION_LAYOUT:
if (cli_check_filename(opt_arg, "layout"))
return 1;
args->layoutfile = strdup(opt_arg);
if (!args->layoutfile) {
fprintf(stderr, "Out of memory!\n");
return 2;
}
break;
case OPTION_IFD:
args->ifd = true;
break;
case OPTION_FMAP:
args->fmap = true;
break;
case OPTION_FMAP_FILE:
if (cli_check_filename(opt_arg, "fmap"))
return 1;
args->fmapfile = strdup(opt_arg);
if (!args->fmapfile) {
fprintf(stderr, "Out of memory!\n");
return 2;
}
break;
}
return 0;
}
int cli_process_layout_args(struct flashprog_layout **const layout,
struct flashprog_flashctx *const flash,
const struct layout_args *const args)
{
*layout = NULL;
if (args->layoutfile) {
if (layout_from_file(layout, args->layoutfile))
return 1;
} else if (args->ifd) {
if (flashprog_layout_read_from_ifd(layout, flash, NULL, 0))
return 1;
} else if (args->fmap) {
if (flashprog_layout_read_fmap_from_rom(layout, flash, 0, flashprog_flash_getsize(flash)))
return 1;
} else if (args->fmapfile) {
struct stat s;
if (stat(args->fmapfile, &s) != 0) {
msg_gerr("Failed to stat fmapfile \"%s\"\n", args->fmapfile);
return 1;
}
size_t fmapfile_size = s.st_size;
uint8_t *fmapfile_buffer = malloc(fmapfile_size);
if (!fmapfile_buffer) {
fprintf(stderr, "Out of memory!\n");
return 1;
}
if (read_buf_from_file(fmapfile_buffer, fmapfile_size, args->fmapfile)) {
free(fmapfile_buffer);
return 1;
}
if (flashprog_layout_read_fmap_from_buffer(layout, flash, fmapfile_buffer, fmapfile_size)) {
free(fmapfile_buffer);
return 1;
}
free(fmapfile_buffer);
}
return 0;
}
/* Note: Changes global `optind` from <getopt.h>. */
int getopt_command(const int argc, char *const argv[], const struct opt_command *const opts)
{
if (optind >= argc || argv[optind][0] == '-')
return -1;
unsigned int i;
for (i = 0; opts[i].name; ++i) {
if (!strcmp(argv[optind], opts[i].name)) {
++optind;
return opts[i].val;
}
}
return -1;
}
void print_generic_options(const bool layout_options)
{
fprintf(stderr, "\n"
"Where generic <options> are\n"
" -p | --programmer <name>[:<params>] specify the programmer device. One of\n");
list_programmers_linebreak(12, 80, 0);
fprintf(stderr, "\n"
" -c | --chip <chipname> probe only for specified flash chip\n"
" -V | --verbose more verbose output\n"
" -o | --output <logfile> log output to <logfile>\n"
" -h | --help print help text\n");
if (!layout_options)
return;
fprintf(stderr, "\n"
"and layout <options> are\n"
" -l | --layout <layoutfile> read ROM layout from <layoutfile>\n"
" --fmap-file <fmapfile> read ROM layout from fmap in <fmapfile>\n"
" --fmap read ROM layout from fmap embedded in ROM\n"
" --ifd read layout from an Intel Flash Descriptor\n");
}
void print_chip_support_status(const struct flashchip *chip)
{
if (chip->feature_bits & FEATURE_OTP) {
msg_cdbg("This chip may contain one-time programmable memory. flashprog cannot read\n"
"and may never be able to write it, hence it may not be able to completely\n"
"clone the contents of this chip (see man page for details).\n");
}
if ((chip->tested.erase == NA) && (chip->tested.write == NA)) {
msg_cdbg("This chip's main memory can not be erased/written by design.\n");
}
if ((chip->tested.probe == BAD) || (chip->tested.probe == NT) ||
(chip->tested.read == BAD) || (chip->tested.read == NT) ||
(chip->tested.erase == BAD) || (chip->tested.erase == NT) ||
(chip->tested.write == BAD) || (chip->tested.write == NT)) {
msg_cinfo("===\n");
if ((chip->tested.probe == BAD) ||
(chip->tested.read == BAD) ||
(chip->tested.erase == BAD) ||
(chip->tested.write == BAD)) {
msg_cinfo("This flash part has status NOT WORKING for operations:");
if (chip->tested.probe == BAD)
msg_cinfo(" PROBE");
if (chip->tested.read == BAD)
msg_cinfo(" READ");
if (chip->tested.erase == BAD)
msg_cinfo(" ERASE");
if (chip->tested.write == BAD)
msg_cinfo(" WRITE");
msg_cinfo("\n");
}
if ((chip->tested.probe == NT) ||
(chip->tested.read == NT) ||
(chip->tested.erase == NT) ||
(chip->tested.write == NT)) {
msg_cinfo("This flash part has status UNTESTED for operations:");
if (chip->tested.probe == NT)
msg_cinfo(" PROBE");
if (chip->tested.read == NT)
msg_cinfo(" READ");
if (chip->tested.erase == NT)
msg_cinfo(" ERASE");
if (chip->tested.write == NT)
msg_cinfo(" WRITE");
msg_cinfo("\n");
}
msg_cinfo("The test status of this chip may have been updated in the latest development\n"
"version of flashprog. If you are running the latest development version,\n"
"please email a report to flashprog@flashprog.org if any of the above\n"
"operations work correctly for you with this flash chip. Please include the\n"
"flashprog log file for all operations you tested (see the man page for details),\n"
"and mention which mainboard or programmer you tested in the subject line.\n"
"Thanks for your help!\n");
}
}
|