blob: 64bb38a23356a5e54c1301231610f32bbdbaecb5 [file] [log] [blame]
Stefan Tauner9b32de92014-08-08 23:52:33 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2009 Carl-Daniel Hailfinger
6 * Copyright (C) 2011-2014 Stefan Tauner
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Stefan Tauner9b32de92014-08-08 23:52:33 +000017 */
18
19#include <stdlib.h>
20#include <string.h>
Nico Huber34e783a2023-02-11 00:30:27 +010021
Stefan Tauner9b32de92014-08-08 23:52:33 +000022#include "flash.h"
Nico Huber34e783a2023-02-11 00:30:27 +010023#include "cli.h"
24
Nico Huberd91822a2023-02-11 00:43:54 +010025int cli_check_filename(const char *const filename, const char *const type)
26{
27 if (!filename || (filename[0] == '\0')) {
28 fprintf(stderr, "Error: No %s file specified.\n", type);
29 return 1;
30 }
31 /* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
32 if (filename[0] == '-' && filename[1] != '\0')
33 fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
34 return 0;
35}
36
Nico Huber34e783a2023-02-11 00:30:27 +010037int cli_parse_flash_args(struct flash_args *const args, const int opt, const char *const optarg)
38{
39 switch (opt) {
40 case OPTION_PROGRAMMER:
41 if (args->prog_name) {
42 fprintf(stderr,
43 "Error: --programmer specified more than once. You can separate multiple\n"
44 "arguments for a programmer with ','. Please see the man page for details.\n");
45 return 1;
46 }
47 const char *const colon = strchr(optarg, ':');
48 if (colon) {
49 args->prog_name = strndup(optarg, colon - optarg);
50 args->prog_args = strdup(colon + 1);
51 } else {
52 args->prog_name = strdup(optarg);
53 }
54 if (!args->prog_name || (colon && !args->prog_args)) {
55 fprintf(stderr, "Out of memory!\n");
56 return 2;
57 }
58 break;
59 case OPTION_CHIP:
60 if (args->chip) {
61 fprintf(stderr, "Error: --chip specified more than once.\n");
62 return 1;
63 }
64 args->chip = strdup(optarg);
65 if (!args->chip) {
66 fprintf(stderr, "Out of memory!\n");
67 return 2;
68 }
69 break;
70 }
71
72 return 0;
73}
Stefan Tauner9b32de92014-08-08 23:52:33 +000074
Nico Huberd91822a2023-02-11 00:43:54 +010075int cli_parse_layout_args(struct layout_args *const args, const int opt, const char *const optarg)
76{
77 if (args->layoutfile || args->ifd || args->fmap || args->fmapfile) {
78 fprintf(stderr, "Error: Only one layout source may be specified.\n");
79 return 1;
80 }
81
82 switch (opt) {
83 case OPTION_LAYOUT:
84 if (cli_check_filename(optarg, "layout"))
85 return 1;
86
87 args->layoutfile = strdup(optarg);
88 if (!args->layoutfile) {
89 fprintf(stderr, "Out of memory!\n");
90 return 2;
91 }
92 break;
93 case OPTION_IFD:
94 args->ifd = true;
95 break;
96 case OPTION_FMAP:
97 args->fmap = true;
98 break;
99 case OPTION_FMAP_FILE:
100 if (cli_check_filename(optarg, "fmap"))
101 return 1;
102
103 args->fmapfile = strdup(optarg);
104 if (!args->fmapfile) {
105 fprintf(stderr, "Out of memory!\n");
106 return 2;
107 }
108 break;
109 }
110
111 return 0;
112}
113
Stefan Tauner9b32de92014-08-08 23:52:33 +0000114void print_chip_support_status(const struct flashchip *chip)
115{
116 if (chip->feature_bits & FEATURE_OTP) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200117 msg_cdbg("This chip may contain one-time programmable memory. flashprog cannot read\n"
Stefan Tauner9b32de92014-08-08 23:52:33 +0000118 "and may never be able to write it, hence it may not be able to completely\n"
119 "clone the contents of this chip (see man page for details).\n");
120 }
121
122 if ((chip->tested.erase == NA) && (chip->tested.write == NA)) {
123 msg_cdbg("This chip's main memory can not be erased/written by design.\n");
124 }
125
126 if ((chip->tested.probe == BAD) || (chip->tested.probe == NT) ||
127 (chip->tested.read == BAD) || (chip->tested.read == NT) ||
128 (chip->tested.erase == BAD) || (chip->tested.erase == NT) ||
Nico Huberbb4f3b02022-12-30 14:28:06 +0100129 (chip->tested.write == BAD) || (chip->tested.write == NT)) {
Stefan Tauner9b32de92014-08-08 23:52:33 +0000130 msg_cinfo("===\n");
131 if ((chip->tested.probe == BAD) ||
132 (chip->tested.read == BAD) ||
133 (chip->tested.erase == BAD) ||
Nico Huberbb4f3b02022-12-30 14:28:06 +0100134 (chip->tested.write == BAD)) {
Stefan Tauner9b32de92014-08-08 23:52:33 +0000135 msg_cinfo("This flash part has status NOT WORKING for operations:");
136 if (chip->tested.probe == BAD)
137 msg_cinfo(" PROBE");
138 if (chip->tested.read == BAD)
139 msg_cinfo(" READ");
140 if (chip->tested.erase == BAD)
141 msg_cinfo(" ERASE");
142 if (chip->tested.write == BAD)
143 msg_cinfo(" WRITE");
144 msg_cinfo("\n");
145 }
146 if ((chip->tested.probe == NT) ||
147 (chip->tested.read == NT) ||
148 (chip->tested.erase == NT) ||
Nico Huberbb4f3b02022-12-30 14:28:06 +0100149 (chip->tested.write == NT)) {
Stefan Tauner9b32de92014-08-08 23:52:33 +0000150 msg_cinfo("This flash part has status UNTESTED for operations:");
151 if (chip->tested.probe == NT)
152 msg_cinfo(" PROBE");
153 if (chip->tested.read == NT)
154 msg_cinfo(" READ");
155 if (chip->tested.erase == NT)
156 msg_cinfo(" ERASE");
157 if (chip->tested.write == NT)
158 msg_cinfo(" WRITE");
159 msg_cinfo("\n");
160 }
161 msg_cinfo("The test status of this chip may have been updated in the latest development\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200162 "version of flashprog. If you are running the latest development version,\n"
163 "please email a report to flashprog@flashprog.org if any of the above\n"
Nico Huberac90af62022-12-18 00:22:47 +0000164 "operations work correctly for you with this flash chip. Please include the\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200165 "flashprog log file for all operations you tested (see the man page for details),\n"
Nico Huberac90af62022-12-18 00:22:47 +0000166 "and mention which mainboard or programmer you tested in the subject line.\n"
Stefan Tauner9b32de92014-08-08 23:52:33 +0000167 "Thanks for your help!\n");
168 }
169}