-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash.c
502 lines (457 loc) · 9.92 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include "flash.h"
#define assume(expr) { \
if (!(expr)) { \
printf("%s:%d: failed assumption `%s'\n", __FILE__, __LINE__, \
#expr); \
abort(); \
} \
}
#define FLASH_READ 0
#define FLASH_WRITE 1
#define FLASH_ERASE 2
typedef struct FlashHeader {
char magic[8]; // "FLASH"
u_int wearLimit;
u_int blocks;
u_int stateOffset; // Offset of sector state array, in bytes.
u_int wearOffset; // Offset of wear level array, in bytes.
u_int blockOffset; // Offset of data blocks, in bytes.
} FlashHeader;
typedef struct FlashInfo {
char *file;
Flash_Flags flags;
int fd;
FlashHeader hdr;
long long readOps;
long long readSectors;
long long writeOps;
long long writeSectors;
long long eraseOps;
long long eraseBlocks;
} FlashInfo;
#define FLASH_STATE_EMPTY 0
#define FLASH_STATE_FULL 1
int
Flash_Create(
char *file,
u_int wearLimit,
u_int blocks)
{
int rc;
FlashHeader hdr;
int fd = -1;
u_int sectors;
int amount;
off_t len;
fd = open(file, O_CREAT|O_RDWR|O_TRUNC, 0666);
if (fd < 0) {
rc = 1;
goto done;
}
strcpy(hdr.magic, "FLASH");
if (wearLimit > 100000) {
errno = EINVAL;
rc = 1;
goto done;
}
hdr.wearLimit = wearLimit;
if (blocks > 1000000) {
errno = EINVAL;
rc = 1;
goto done;
}
hdr.blocks = blocks;
sectors = blocks * 16;
hdr.stateOffset = sizeof(FlashHeader);
hdr.wearOffset = hdr.stateOffset + sectors;
hdr.blockOffset = hdr.wearOffset + (blocks * sizeof(u_int));
amount = write(fd, &hdr, sizeof(hdr));
if (amount != sizeof(hdr)) {
rc = 1;
if (errno == 0) {
errno = EIO;
}
goto done;
}
len = hdr.blockOffset + (hdr.blocks * FLASH_BLOCK_SIZE);
rc = ftruncate(fd, len);
if (rc) {
goto done;
}
rc = 0;
done:
if (fd >= 0) {
close(fd);
}
return rc;
}
int
Flash_GetWear(
Flash flashHandle,
u_int block,
u_int *wear)
{
FlashInfo *flash = (FlashInfo *) flashHandle;
u_int buffer;
off_t offset;
off_t result;
int rc;
int amount;
if (flash == NULL) {
rc = 1;
errno = EINVAL;
goto done;
}
offset = flash->hdr.wearOffset + (block * sizeof(u_int));
result = lseek(flash->fd, offset, SEEK_SET);
if (result != offset) {
rc = 1;
goto done;
}
amount = read(flash->fd, &buffer, sizeof(buffer));
if (amount != sizeof(buffer)) {
rc = 1;
if (errno == 0) {
errno = EIO;
}
goto done;
}
rc = 0;
*wear = buffer;
done:
return rc;
}
int
FlashSetWear(
FlashInfo *flash,
int block,
u_int wear)
{
off_t offset;
off_t result;
int rc;
int amount;
offset = flash->hdr.wearOffset + (block * sizeof(u_int));
result = lseek(flash->fd, offset, SEEK_SET);
if (result != offset) {
rc = 1;
goto done;
}
amount = write(flash->fd, &wear, sizeof(wear));
if (amount != sizeof(wear)) {
rc = 1;
if (errno == 0) {
errno = EIO;
}
goto done;
}
rc = 0;
done:
return rc;
}
int
FlashGetState(
FlashInfo *flash,
int sector,
u_char *state)
{
u_char buffer;
off_t offset;
off_t result;
int rc;
int amount;
offset = flash->hdr.stateOffset + (sector * sizeof(u_char));
result = lseek(flash->fd, offset, SEEK_SET);
if (result != offset) {
rc = 1;
goto done;
}
amount = read(flash->fd, &buffer, sizeof(buffer));
if (amount != sizeof(buffer)) {
rc = 1;
if (errno == 0) {
errno = EIO;
}
goto done;
}
rc = 0;
*state = buffer;
done:
return rc;
}
int
FlashSetState(
FlashInfo *flash,
int sector,
u_char state)
{
off_t offset;
off_t result;
int rc;
int amount;
offset = flash->hdr.stateOffset + (sector * sizeof(u_char));
result = lseek(flash->fd, offset, SEEK_SET);
if (result != offset) {
rc = 1;
goto done;
}
amount = write(flash->fd, &state, sizeof(state));
if (amount != sizeof(state)) {
rc = 1;
if (errno == 0) {
errno = EIO;
}
goto done;
}
rc = 0;
done:
return rc;
}
Flash
Flash_Open(
char *file,
Flash_Flags flags,
u_int *blocks)
{
FlashInfo *flash;
int rc;
int amount;
flash = (FlashInfo *) malloc(sizeof(FlashInfo));
memset(flash, 0, sizeof(*flash));
assume(flash != NULL);
flash->file = file;
flash->flags = flags;
flash->fd = open(file, O_RDWR);
if (flash->fd < 0) {
rc = 1;
goto done;
}
amount = read(flash->fd, &flash->hdr, sizeof(flash->hdr));
if (amount != sizeof(flash->hdr)) {
rc = 1;
if (errno == 0) {
errno = EIO;
}
goto done;
}
if (strcmp(flash->hdr.magic, "FLASH")) {
rc = 1;
errno = EIO;
goto done;
}
*blocks = flash->hdr.blocks;
rc = 0;
done:
if (rc) {
free((void *) flash);
flash = NULL;
}
return flash;
}
static int
FlashIO(
FlashInfo *flash,
int type,
u_int offset,
u_int count,
void *buffer)
{
off_t seekOffset;
off_t resultOffset;
int rc;
ssize_t amount;
if (flash == NULL) {
rc = 1;
errno = EINVAL;
goto done;
}
if ((offset + count) > flash->hdr.blocks * FLASH_SECTORS_PER_BLOCK) {
rc = 1;
errno = EINVAL;
goto done;
}
seekOffset = flash->hdr.blockOffset + (offset * FLASH_SECTOR_SIZE);
resultOffset = lseek(flash->fd, seekOffset, SEEK_SET);
if (resultOffset != seekOffset) {
rc = 1;
goto done;
}
switch (type) {
case FLASH_READ:
amount = read(flash->fd, buffer, count * FLASH_SECTOR_SIZE);
break;
case FLASH_WRITE:
amount = write(flash->fd, buffer, count * FLASH_SECTOR_SIZE);
break;
default:
fprintf(stderr, "Internal error in FlashIO\n");
rc = 1;
goto done;
}
if (amount != (count * FLASH_SECTOR_SIZE)) {
rc = 1;
if (errno == 0) {
errno = EIO;
}
goto done;
}
if ((flash->flags & FLASH_ASYNC) == 0) {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 10000000;
rc = nanosleep(&req, NULL);
if (rc) {
rc = 1;
goto done;
}
}
rc = 0;
done:
return rc;
}
int
Flash_Read(
Flash flashHandle,
u_int sector,
u_int count,
void *buffer)
{
FlashInfo *flash = (FlashInfo *) flashHandle;
int rc;
if (flash == NULL) {
rc = 1;
errno = EINVAL;
goto done;
}
rc = FlashIO(flash, FLASH_READ, sector, count, buffer);
if (rc == 0) {
flash->readOps++;
flash->readSectors += count;
}
done:
return rc;
}
int
Flash_Write(
Flash flashHandle,
u_int sector,
u_int count,
void *buffer)
{
FlashInfo *flash = (FlashInfo *) flashHandle;
int rc;
int i;
u_char state;
if (flash == NULL) {
rc = 1;
errno = EINVAL;
goto done;
}
for (i = sector; i < sector + count; i++) {
rc = FlashGetState(flash, i, &state);
if (rc) {
goto done;
}
if (state == FLASH_STATE_FULL) {
rc = 1;
errno = EIO;
goto done;
}
}
rc = FlashIO(flash, FLASH_WRITE, sector, count, buffer);
if (rc) {
goto done;
}
for (i = sector; i < sector + count; i++) {
rc = FlashSetState(flash, i, FLASH_STATE_FULL);
if (rc) {
goto done;
}
}
rc = 0;
flash->writeOps++;
flash->writeSectors += count;
done:
return rc;
}
int
Flash_Erase(
Flash flashHandle,
u_int block,
u_int count)
{
FlashInfo *flash = (FlashInfo *) flashHandle;
int rc;
int i,j;
u_int wear;
u_int sector;
if (flash == NULL) {
rc = 1;
errno = EINVAL;
goto done;
}
for (i = block; i < block + count; i++) {
rc = Flash_GetWear(flash, i, &wear);
if (rc) {
goto done;
}
if (wear >= flash->hdr.wearLimit) {
rc = 1;
errno = EIO;
goto done;
}
wear++;
rc = FlashSetWear(flash, i, wear);
if (rc) {
goto done;
}
for (j = 0; j < FLASH_SECTORS_PER_BLOCK; j++) {
sector = (i * FLASH_SECTORS_PER_BLOCK) + j;
rc = FlashSetState(flash, sector, FLASH_STATE_EMPTY);
if (rc) {
goto done;
}
}
}
rc = 0;
flash->eraseOps++;
flash->eraseBlocks += count;
done:
return rc;
}
int
Flash_Close(
Flash flashHandle)
{
FlashInfo *flash = (FlashInfo *) flashHandle;
int rc;
if (flash == NULL) {
rc = 1;
errno = EINVAL;
goto done;
}
rc = close(flash->fd);
if (rc) {
rc = 1;
goto done;
}
if ((flash->flags & FLASH_SILENT) == 0) {
fprintf(stderr, "Flash read ops: %lld\n", flash->readOps);
fprintf(stderr, "Flash read sectors: %lld\n", flash->readSectors);
fprintf(stderr, "Flash write ops: %lld\n", flash->writeOps);
fprintf(stderr, "Flash write sectors: %lld\n", flash->writeSectors);
}
free((void *) flash);
rc = 0;
done:
return rc;
}