forked from mikeryan/ems-flasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.c
336 lines (277 loc) · 9.55 KB
/
flash.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
/*
* High-level flash I/O commands, notably those described in update.h
*
* This module can be initialized by flash_init() with progress_cb and
* checkint_cb callbacks. Both callbacks are optional.
*
* When writing a ROM to flash memory, one piece of the header is written last
* (containing a part of the logo), making the header invalid and thus the ROM
* hidden until the very last block is transferred and all operations succeeded.
*
* The move operation delete the ROM from its source location only when it has
* been copied completely.
*
* Global Variables
*
* flash_lastofs: higher address written on flash. This can be used to determine
* the last formated erase-block.
*
* Progression status
*
* progress_cb is called for every 4 KB of transfered bytes as required by
* the current implementation of progress.c.
* The total number of bytes transferred is computed as follow:
* writef, read, write: "size" bytes
* move: 2*"size" bytes
* erase: 0 bytes
*
* Signals handling
*
* checkint_cb is called regularly during a transfer and must return non-zero
* if an interrupt signal has been caught. A function returns EFLASH_EINTR
* when interrupted.
* The caller is responsible for blocking or redirecting signals.
* write() is atomic, it doesn't check for interrupts. This keeps us from
* recovering a partialy written ROM.
*/
#include "ems.h"
#include "flash.h"
#include "progress.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <err.h>
#define NBSLOTS 3
static unsigned char slot[NBSLOTS][ERASEBLOCKSIZE/2];
static void (*flash_progress_cb)(int, ems_size_t);
static int (*flash_checkint_cb)(void);
#define CHECKINT (flash_checkint_cb?flash_checkint_cb():0)
#define PROGRESS(type, size) \
do { \
if (flash_progress_cb) flash_progress_cb(type, size); \
} while (0)
#define xwarn(fmt, ...) \
snprintf(flash_lasterrorstr, sizeof(flash_lasterrorstr), fmt ": %s", \
__VA_ARGS__, strerror(errno))
#define xwarnx(...)\
snprintf(flash_lasterrorstr, sizeof(flash_lasterrorstr), __VA_ARGS__)
void
flash_init(void (*progress_cb)(int, ems_size_t), int (*checkint_cb)(void)) {
flash_lastofs = -1;
flash_progress_cb = progress_cb;
flash_checkint_cb = checkint_cb;
}
void
flash_setprogresscb(void (*progress_cb)(int, ems_size_t)) {
flash_progress_cb = progress_cb;
}
int
flash_writef(ems_size_t offset, ems_size_t size, char *path) {
unsigned char blockbuf[WRITEBLOCKSIZE*2], blockbuf100[WRITEBLOCKSIZE*2];
ems_size_t blockofs, progress;
FILE *f;
int i, r;
if ((f = fopen(path, "rb")) == NULL) {
xwarn("can't open %s", path);
return FLASH_EFILE;
}
progress = 0;
for (blockofs = 0; blockofs < size; blockofs += WRITEBLOCKSIZE*2) {
if (fread(blockbuf, 1, WRITEBLOCKSIZE*2, f) < WRITEBLOCKSIZE*2) {
if (ferror(f)) {
xwarn("error reading %s", path);
fclose(f);
return FLASH_EFILE;
} else break;
}
if (blockofs == 0x100) {
memcpy(blockbuf100, blockbuf, WRITEBLOCKSIZE*2);
continue;
}
if (CHECKINT) {
xwarnx("operation interrupted");
return FLASH_EINTR;
}
for (i = 0; i < 2; i++) {
flash_lastofs = offset + blockofs + i*WRITEBLOCKSIZE;
r = ems_write(TO_ROM, flash_lastofs, blockbuf + i*WRITEBLOCKSIZE,
WRITEBLOCKSIZE);
if (r != WRITEBLOCKSIZE) {
xwarnx("write error flashing %s", path);
fclose(f);
return FLASH_EUSB;
}
}
if ((offset + blockofs)%ERASEBLOCKSIZE == 0)
PROGRESS(PROGRESS_ERASE, 0);
if ((progress += WRITEBLOCKSIZE*2)%READBLOCKSIZE == 0)
PROGRESS(PROGRESS_WRITEF, READBLOCKSIZE);
}
for (i = 0; i < 2; i++) {
r = ems_write(TO_ROM,
offset + 0x100 + i*WRITEBLOCKSIZE,
blockbuf100 + i*WRITEBLOCKSIZE,
WRITEBLOCKSIZE);
if (r != WRITEBLOCKSIZE) {
xwarnx("write error flashing %s", path);
fclose(f);
return FLASH_EUSB;
}
}
PROGRESS(PROGRESS_WRITEF, READBLOCKSIZE);
if (fclose(f) == EOF) {
xwarn("can't close %s", path);
return FLASH_EFILE;
}
return 0;
}
int
flash_move(ems_size_t offset, ems_size_t size, ems_size_t origoffset) {
unsigned char blockbuf[(READBLOCKSIZE < WRITEBLOCKSIZE)?WRITEBLOCKSIZE:READBLOCKSIZE];
unsigned char blockbuf100[WRITEBLOCKSIZE*2];
ems_size_t remain, src, dest, blockofs, progress;
int r, flipflop;
src = origoffset;
dest = offset;
progress = 0;
flipflop = 0;
for (remain = size; remain > 0; remain -= READBLOCKSIZE) {
if (CHECKINT) {
xwarnx("operation interrupted");
return FLASH_EINTR;
}
r = ems_read(FROM_ROM, src, blockbuf, READBLOCKSIZE);
if (r != READBLOCKSIZE) {
xwarnx("read error updating flash memory");
return FLASH_EUSB;
}
PROGRESS(PROGRESS_READ, READBLOCKSIZE);
for (blockofs = 0; blockofs < READBLOCKSIZE; blockofs += WRITEBLOCKSIZE) {
if (src == origoffset && blockofs == 0x100) {
memcpy(blockbuf100, blockbuf+0x100, WRITEBLOCKSIZE*2);
blockofs += WRITEBLOCKSIZE;
continue;
}
if ((flipflop = 1-flipflop) && CHECKINT) {
xwarnx("operation interrupted");
return FLASH_EINTR;
}
flash_lastofs = dest+blockofs;
r = ems_write(TO_ROM, flash_lastofs, blockbuf+blockofs,
WRITEBLOCKSIZE);
if (r != WRITEBLOCKSIZE) {
xwarnx("write error updating flash memory");
return FLASH_EUSB;
}
if ((dest + blockofs)%ERASEBLOCKSIZE == WRITEBLOCKSIZE)
PROGRESS(PROGRESS_ERASE, 0);
if ((progress += WRITEBLOCKSIZE)%READBLOCKSIZE == 0)
PROGRESS(PROGRESS_WRITE, READBLOCKSIZE);
}
src += READBLOCKSIZE;
dest += READBLOCKSIZE;
}
for (int i = 0; i < 2; i++) {
r = ems_write(TO_ROM,
offset+0x100+i*WRITEBLOCKSIZE,
blockbuf100+i*WRITEBLOCKSIZE,
WRITEBLOCKSIZE);
if (r != WRITEBLOCKSIZE) {
xwarnx("write error updating flash memory");
return FLASH_EUSB;
}
}
PROGRESS(PROGRESS_WRITE, READBLOCKSIZE);
return flash_delete(origoffset, 2);
}
int
flash_read(int slotn, ems_size_t size, ems_size_t offset) {
ems_size_t remain;
unsigned char *block;
int r;
block = slot[slotn];
for (remain = size; remain > 0; remain -= READBLOCKSIZE) {
if (CHECKINT) {
xwarnx("operation interrupted");
return FLASH_EINTR;
}
r = ems_read(FROM_ROM, offset, block, READBLOCKSIZE);
if (r != READBLOCKSIZE) {
xwarnx("read error updating flash memory");
return FLASH_EUSB;
}
block += READBLOCKSIZE;
offset += READBLOCKSIZE;
PROGRESS(PROGRESS_READ, READBLOCKSIZE);
}
return 0;
}
/* doesn't test for signals */
int
flash_write(ems_size_t offset, ems_size_t size, int slotn) {
ems_size_t blockofs, progress;
unsigned char *buf;
int r;
buf = slot[slotn];
progress = 0;
for (blockofs = 0; blockofs < size; blockofs += WRITEBLOCKSIZE) {
if (blockofs == 0x100)
continue;
flash_lastofs = offset + blockofs;
r = ems_write(TO_ROM, flash_lastofs, buf + blockofs, WRITEBLOCKSIZE);
if (r != WRITEBLOCKSIZE) {
xwarnx("write error updating flash memory");
return FLASH_EUSB;
}
if ((offset + blockofs) % ERASEBLOCKSIZE == WRITEBLOCKSIZE)
PROGRESS(PROGRESS_ERASE, 0);
if ((progress += WRITEBLOCKSIZE) % READBLOCKSIZE == 0)
PROGRESS(PROGRESS_WRITE, READBLOCKSIZE);
}
r = ems_write(TO_ROM, offset + 0x100, buf + 0x100, WRITEBLOCKSIZE);
if (r != WRITEBLOCKSIZE) {
xwarnx("write error updating flash memory");
return FLASH_EUSB;
}
PROGRESS(PROGRESS_WRITE, READBLOCKSIZE);
return 0;
}
int
flash_erase(ems_size_t offset) {
unsigned char blankbuf[32];
int i, r;
if (CHECKINT) {
xwarnx("operation interrupted");
return FLASH_EINTR;
}
for (i = 0; i < 2; i++) {
memset(blankbuf, 0xff, 32);
r = ems_write(TO_ROM, flash_lastofs = offset + i*32, blankbuf, 32);
if (r != 32) {
xwarnx("write error updating flash memory");
return FLASH_EUSB;
}
}
PROGRESS(PROGRESS_ERASE, 0);
return 0;
}
int
flash_delete(ems_size_t offset, int blocks) {
unsigned char zerobuf[32];
int r;
memset(zerobuf, 0, 32);
while (blocks--) {
if ((blocks+1)%2 == 0 && CHECKINT) {
xwarnx("operation interrupted");
return FLASH_EINTR;
}
r = ems_write(TO_ROM, offset + 0x130 - blocks*32, zerobuf, 32);
if (r != 32) {
xwarnx("flash write error (address=%"PRIuEMSSIZE")",
offset + 0x130 - blocks*32);
return FLASH_EUSB;
}
}
return 0;
}