-
Notifications
You must be signed in to change notification settings - Fork 5
/
bioswrite-main.c
281 lines (243 loc) · 5.35 KB
/
bioswrite-main.c
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
/** \file
* SPI flash command line tool.
*
* Much simpler than flashrom, but far less flexible.
* The flash ROM needs to be in an unlocked state before this can
* be used. Doing so is left as an exercise to the user.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include <getopt.h>
#include "bioswrite.h"
static int force = 0;
static int verbose = 0;
static const struct option long_options[] = {
{ "force", 0, NULL, 'f' },
{ "verbose", 0, NULL, 'v' },
{ "read", 1, NULL, 'r' },
{ "write", 1, NULL, 'w' },
{ "offset", 1, NULL, 'O' },
{ "length", 1, NULL, 'n' },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
static const char usage[] =
"Usage: sudo bioswrite [options]\n"
"\n"
"-h | -? | --help This help\n"
"-v | --verbose Increase verbosity\n"
"-r | --read file Read the ROM range and dump to file\n"
"-w | --write file Read the file and write to the ROM range\n"
"-O | --offset N Flash offset to start writing at, otherwise 0\n"
"-n | --length N Length in bytes to read/write (default whole ROM)\n"
"-f | --force Write all flash pages, not just the changed ones\n"
"\n"
"WARNING: This tool can permanently brick your machine!\n"
"Use with caution, especially if you do not have an ISP to fix the\n"
"SPI flash ROM chip through hardware.\n"
"\n";
static int
read_from_spi(
spiflash_t * const sp,
const char * const filename,
unsigned offset,
unsigned length
)
{
const unsigned flash_size = spiflash_size(sp);
if (length == 0)
length = flash_size - offset;
if (offset + length > flash_size)
{
fprintf(stderr, "offset %08x + length %08x > flash_size %08x\n",
offset,
length,
flash_size
);
return EXIT_FAILURE;
}
uint8_t * const buf = calloc(1, length);
if (!buf)
{
perror("malloc");
return EXIT_FAILURE;
}
if (verbose)
printf("spiflash: reading from %08x: 0x%x bytes\n", offset, length);
if (spiflash_read(sp, offset, buf, length) < 0)
{
fprintf(stderr, "spiflash_read(%08x,%08x) failed?\n",
offset,
length
);
return EXIT_FAILURE;
}
FILE * file;
if (strcmp(filename, "-") == 0)
{
file = stdout;
} else {
file = fopen(filename, "w");
if (!file)
{
perror(filename);
return EXIT_FAILURE;
}
}
fwrite(buf, 1, length, file);
fclose(file);
return EXIT_SUCCESS;
}
static int
write_to_spi(
spiflash_t * const sp,
const char * const filename,
unsigned offset,
unsigned length
)
{
// if a filename was given, read it in
FILE * file;
if (strcmp(filename, "-") == 0)
{
file = stdin;
} else {
file = fopen(filename, "r");
if (!file)
{
perror(filename);
return EXIT_FAILURE;
}
}
const unsigned flash_size = spiflash_size(sp);
uint8_t * const buf = calloc(1, flash_size+1);
const unsigned read_len = fread(buf, 1, flash_size+1, file);
if (length == 0)
{
// they didn't tell us how much, use this value
length = read_len;
} else {
// should we pad with 0xff if too short?
if (read_len != length)
{
fprintf(stderr, "Read %x bytes, expected %x\n", read_len, length);
return EXIT_FAILURE;
}
}
if (offset + length > flash_size)
{
fprintf(stderr, "offset %08x + length %08x > flash size %08x\n",
offset,
length,
flash_size
);
return EXIT_FAILURE;
}
if (spiflash_write_enable(sp) < 0)
{
fprintf(stderr, "spiflash: unable to enable writes\n");
return EXIT_FAILURE;
}
if (verbose)
printf("spiflash: writing to %08x: 0x%x bytes\n", offset, length);
if (spiflash_program_buffer(sp, offset, buf, length) < 0)
{
fprintf(stderr, "program write failed!\n");
return EXIT_FAILURE;
}
if (verbose)
printf("success!\n");
return EXIT_SUCCESS;
}
int
main(
int argc,
char ** argv
)
{
const char * const prog_name = argv[0];
int opt;
int do_read = 0;
int do_write = 0;
unsigned offset = 0;
unsigned length = 0;
const char * filename = NULL;
spiflash_t * sp = calloc(1, sizeof(*sp));
if (!sp)
return EXIT_FAILURE;
while ((opt = getopt_long(argc, argv, "h?fvO:n:r:w:", long_options, NULL)) != -1)
{
switch(opt)
{
case 'O':
offset = strtoul(optarg, NULL, 0);
break;
case 'n':
length = strtoul(optarg, NULL, 0);
break;
case 'v':
verbose++;
break;
case 'f':
force++;
break;
case 'r':
do_read = 1;
filename = optarg;
break;
case 'w':
do_write = 1;
filename = optarg;
break;
case '?': case 'h':
printf("%s", usage);
return EXIT_SUCCESS;
default:
fprintf(stderr, "%s", usage);
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (argc != 0)
{
fprintf(stderr, "%s: Excess arguments?\n", prog_name);
return EXIT_FAILURE;
}
sp->verbose = verbose;
if (spiflash_init(sp) < 0)
{
fprintf(stderr, "spiflash_init failed\n");
return -1;
}
if (verbose)
{
printf("lpc: %p\n", sp->lpc_base);
printf("spibar: %p\n", sp->spibar);
}
const unsigned flash_size = spiflash_size(sp);
if (verbose)
printf("flash size: 0x%08x\n", flash_size);
if (offset > flash_size)
{
fprintf(stderr, "offset %08x > flash size %08x\n",
offset,
flash_size
);
return EXIT_FAILURE;
}
if (do_read && do_write)
{
fprintf(stderr, "Only one of read or write may be used\n");
return EXIT_FAILURE;
}
if (do_read)
return read_from_spi(sp, filename, offset, length);
if (do_write)
return write_to_spi(sp, filename, offset, length);
return EXIT_SUCCESS;
}