-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin2svf.c
371 lines (309 loc) · 6.76 KB
/
bin2svf.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* bin2svf.c - HiSilicon D02 BIOS binary to SVF conversion tool
*/
#include <arpa/inet.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/* Flags for generate_svf() */
#define MODE_ERASE 0x1
#define MODE_WRITE 0x2
#define MODE_VERIFY 0x4
/*
* A pattern of bits sent in a SDR command to program or verify a page of the
* SPI flash.
*/
struct page_pattern {
uint8_t data[256];
uint32_t addr_and_op;
};
/*
* Type of operation the page pattern is intended for.
*/
#define PAGE_OP_PROGRAM_TDI 0x40
#define PAGE_OP_VERIFY_TDI 0xc0
#define PAGE_OP_VERIFY_TDO 0x00
#define PAGE_OP_VERIFY_MASK 0x00
enum chip_size {
SIZE_4MB,
SIZE_8MB,
SIZE_16MB,
};
/*
* Chip size only affects the time we wait when erasing the chip
* (for the supported sizes at least).
*/
static const int chip_size = SIZE_16MB;
static int erase_time(enum chip_size size)
{
switch (size) {
case SIZE_4MB:
return 80000;
case SIZE_8MB:
return 160000;
case SIZE_16MB:
return 250000;
default:
printf("Unsupported chip size\n");
exit(1);
}
}
static void usage()
{
printf("Usage: bin2svf [INFILE] >OUTFILE\n\n");
printf("Converts a Hisilicon D02 BIOS binary to SVF format.\n");
printf("If INFILE is not supplied, reads from standard input.\n");
}
/* Reverse bits (MSB becomes LSB) */
static uint8_t reverse_bits_8(uint8_t v)
{
uint8_t res = 0;
int i;
for (i = 0; i < 8; i++)
res |= ((v >> i) & 1) << (7 - i);
return res;
}
static uint32_t reverse_bits_32(uint32_t v)
{
uint32_t res = 0;
int i;
for (i = 0; i < 32; i++)
res |= ((v >> i) & 1) << (31 - i);
return res;
}
static void hexdump(uint8_t *buf, size_t len, bool uppercase)
{
size_t i;
for (i = 0; i < len; i++)
printf("%02x", buf[i]);
}
/*
* Fill a bit pattern to be used in a SDR command to program or verify a page
* @pattern: the bit pattern to send for writing, for comparing against,
* or as a mask
* @buf: the data to send
* @len: the data length in bytes
* @page_addr: the start address of the page in the flash memory (must be a
* multiple of 256 less than 16 MiB)
* @op: the operation this page will be used with
*/
static int create_page_pattern(struct page_pattern *pattern, uint8_t *buf,
size_t len, uint32_t page_addr, uint8_t op)
{
size_t i;
uint32_t addr;
if (!buf)
len = 0;
for (i = 0; i < len; i++)
pattern->data[255-i] = reverse_bits_8(buf[i]);
for (i = len; i < 256; i++)
pattern->data[255-i] = 0xff;
if (op == PAGE_OP_VERIFY_TDO || op == PAGE_OP_VERIFY_MASK)
addr = 0;
else
addr = htonl(reverse_bits_32(page_addr));
pattern->addr_and_op = addr | htonl(op);
return 0;
}
static void trst(int on)
{
printf("TRST %s;\n\n", (on ? "ON" : "OFF"));
}
static void set_frequency()
{
printf("FREQUENCY 5.00e+006 HZ;\n\n");
}
static void write_enable()
{
printf("! Write enable\n");
printf("SDR 8 TDI(60);\n\n");
}
static void write_disable()
{
printf("! Write disable\n");
printf("SDR 8 TDI(20);\n\n");
}
static void wait(int ms)
{
printf("RUNTEST IDLE %G SEC ENDSTATE IDLE;\n\n", (float)ms/1000);
}
static void clear_software_protect()
{
printf("! Clear software protect\n");
printf("SDR 16 TDI(0080);\n\n");
}
static void check_no_software_protect()
{
printf("! Check no software protect\n");
printf("SDR 16 TDI(ffa0) TDO(c6ff) MASK(3900);\n\n");
}
static void check_status()
{
printf("! Check status\n");
printf("SDR 16 TDI(ffa0) TDO(0000) MASK(8000);\n\n");
}
static void bulk_erase()
{
printf("! Bulk erase\n");
printf("SDR 8 TDI(e3);\n\n");
}
static int send_page_program(uint8_t *buf, size_t len, uint32_t addr)
{
struct page_pattern pattern;
int rc;
rc = create_page_pattern(&pattern, buf, len, addr,
PAGE_OP_PROGRAM_TDI);
if (rc < 0)
return rc;
write_enable();
printf("! Program page: 0x%08x\n", addr);
printf("SDR 2080 TDI (");
hexdump((uint8_t *)&pattern, sizeof(pattern), false);
printf(");\n\n");
write_disable();
wait(2);
return 0;
}
static int send_page_verify(uint8_t *buf, size_t len, uint32_t addr)
{
struct page_pattern pattern;
int rc;
rc = create_page_pattern(&pattern, NULL, 0, addr,
PAGE_OP_VERIFY_TDI);
if (rc < 0)
return rc;
printf("! Verify page: 0x%08x\n", addr);
printf("SDR 2080 TDI (");
hexdump((uint8_t *)&pattern, sizeof(pattern), true);
printf(")\n");
rc = create_page_pattern(&pattern, buf, len, addr,
PAGE_OP_VERIFY_TDO);
if (rc < 0)
return rc;
printf("TDO (");
hexdump((uint8_t *)&pattern, sizeof(pattern), false);
printf(")\n");
rc = create_page_pattern(&pattern, NULL, 0, addr,
PAGE_OP_VERIFY_MASK);
if (rc < 0)
return rc;
printf("MASK (");
hexdump((uint8_t *)&pattern, sizeof(pattern), true);
printf(");\n");
return 0;
}
static int generate_svf(uint8_t *buf, size_t len, uint32_t addr, uint32_t mode)
{
int i;
int rc;
size_t blen;
int all_ones;
int written;
trst(0);
set_frequency();
write_enable();
clear_software_protect();
write_disable();
wait(100);
check_no_software_protect();
if (mode & MODE_ERASE) {
write_enable();
bulk_erase();
write_disable();
wait(erase_time(chip_size));
check_status();
}
do {
blen = len < 256 ? len : 256;
all_ones = 1;
written = 0;
for (int i = 0; i < blen; i++) {
if (buf[i] != 0xFF) {
all_ones = 0;
break;
}
}
if (mode & MODE_WRITE) {
/*
* No need to write a page full of ones if the
* flash was cleared previously.
*/
if (!(mode & MODE_ERASE) || !all_ones) {
rc = send_page_program(buf, blen, addr);
if (rc < 0)
goto out;
written = 1;
}
}
/* Verify if asked to. Always verify after write. */
if ((mode & MODE_VERIFY) || ((mode & MODE_WRITE) && written)) {
rc = send_page_verify(buf, blen, addr);
if (rc < 0)
goto out;
}
len -= blen;
buf += blen;
addr += blen;
} while (len);
trst(1);
out:
return rc;
}
int main(int argc, char *argv[])
{
int fd = 0;
uint8_t *buf;
uint8_t *ptr;
size_t n;
size_t left = 32 * 1024 * 1024; /* Yeah... */
if (argc == 2) {
if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
usage();
return 0;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open");
return 1;
}
}
buf = malloc(left);
if (!buf) {
printf("bin2svf: out of memory\n");
return 1;
}
ptr = buf;
do {
n = read(fd, ptr, left);
if (!n) {
if (errno == EINTR)
continue;
/* EOF */
break;
}
left -= n;
ptr += n;
if (!left) {
char c;
do {
n = read(fd, &c, 1);
} while (!n && errno == EINTR);
if (n) {
printf("%s: input file to big\n", argv[0]);
return 1;
}
break;
}
} while (left);
if (ptr != buf)
generate_svf(buf, ptr - buf, 0x800000,
MODE_ERASE | MODE_WRITE);
return 0;
}