-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.c
453 lines (358 loc) · 13 KB
/
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
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#if defined(ARCH_LINUX)
# define DEVICE_INDEX 0
# define FLASH_ADDRESS 0x0000
# define FLASH_SIZE 0x800
# define CHUNK_SIZE 0x100
static uint8_t flash_buf[FLASH_SIZE];
#elif defined(FAMILY_SPC5)
# define DEVICE_INDEX 1
# define FLASH_ADDRESS SPC5_DFLASH_ADDRESS
# define FLASH_SIZE 0x8000
# define CHUNK_SIZE 0x1000
#else
# error "Unsupported target."
#endif
static struct eeprom_soft_block_t blocks[2] = {
{
.address = FLASH_ADDRESS,
.size = FLASH_SIZE / 2
},
{
.address = FLASH_ADDRESS + FLASH_SIZE / 2,
.size = FLASH_SIZE / 2
}
};
static struct flash_driver_t flash;
static struct eeprom_soft_driver_t eeprom_soft;
static int test_init(void)
{
struct flash_device_t *dev_p;
BTASSERT(eeprom_soft_module_init() == 0);
BTASSERT(eeprom_soft_module_init() == 0);
dev_p = &flash_device[DEVICE_INDEX];
BTASSERT(flash_init(&flash, dev_p) == 0);
BTASSERT(flash_erase(&flash, blocks[0].address, blocks[0].size) == 0);
BTASSERT(flash_erase(&flash, blocks[1].address, blocks[1].size) == 0);
BTASSERT(eeprom_soft_init(&eeprom_soft,
&flash,
&blocks[0],
membersof(blocks),
CHUNK_SIZE) == 0);
return (0);
}
static int test_format_mount(void)
{
BTASSERT(eeprom_soft_mount(&eeprom_soft) == -1);
BTASSERT(eeprom_soft_format(&eeprom_soft) == 0);
BTASSERT(eeprom_soft_mount(&eeprom_soft) == 0);
return (0);
}
static int test_read_write_sizes(void)
{
int i;
uint8_t buf[55];
/* Read various sizes at various alignments from a blank
EEPROM. */
memset(&buf[0], 0, sizeof(buf));
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[0], 0, 1) == 1);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[1], 1, 2) == 2);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[3], 3, 3) == 3);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[6], 6, 4) == 4);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[10], 10, 5) == 5);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[15], 15, 6) == 6);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[21], 21, 7) == 7);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[28], 28, 8) == 8);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[36], 36, 9) == 9);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[45], 45, 10) == 10);
for (i = 0; i < membersof(buf); i++) {
BTASSERT(buf[i] == 0xff, "[%d]: %02x", i, buf[i]);
}
/* Write various sizes at various alignments. */
for (i = 0; i < membersof(buf); i++) {
buf[i] = i;
}
BTASSERT(eeprom_soft_write(&eeprom_soft, 0, &buf[0], 1) == 1);
BTASSERT(eeprom_soft_write(&eeprom_soft, 1, &buf[1], 2) == 2);
BTASSERT(eeprom_soft_write(&eeprom_soft, 3, &buf[3], 3) == 3);
BTASSERT(eeprom_soft_write(&eeprom_soft, 6, &buf[6], 4) == 4);
BTASSERT(eeprom_soft_write(&eeprom_soft, 10, &buf[10], 5) == 5);
BTASSERT(eeprom_soft_write(&eeprom_soft, 15, &buf[15], 6) == 6);
BTASSERT(eeprom_soft_write(&eeprom_soft, 21, &buf[21], 7) == 7);
BTASSERT(eeprom_soft_write(&eeprom_soft, 28, &buf[28], 8) == 8);
BTASSERT(eeprom_soft_write(&eeprom_soft, 36, &buf[36], 9) == 9);
BTASSERT(eeprom_soft_write(&eeprom_soft, 45, &buf[45], 10) == 10);
/* Read and verify the written data. */
memset(&buf[0], -1, sizeof(buf));
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[0], 0, 1) == 1);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[1], 1, 2) == 2);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[3], 3, 3) == 3);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[6], 6, 4) == 4);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[10], 10, 5) == 5);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[15], 15, 6) == 6);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[21], 21, 7) == 7);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[28], 28, 8) == 8);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[36], 36, 9) == 9);
BTASSERT(eeprom_soft_read(&eeprom_soft, &buf[45], 45, 10) == 10);
for (i = 0; i < membersof(buf); i++) {
BTASSERT(buf[i] == i, "[%d]: %02x", i, buf[i]);
}
return (0);
}
static int test_read_write_low_high(void)
{
uint8_t byte;
/* Lowest address. */
byte = 1;
BTASSERT(eeprom_soft_write(&eeprom_soft,
0,
&byte,
sizeof(byte)) == sizeof(byte));
byte = 0;
BTASSERT(eeprom_soft_read(&eeprom_soft,
&byte,
0,
sizeof(byte)) == sizeof(byte));
BTASSERT(byte == 1);
/* Highest address. */
byte = 2;
BTASSERT(eeprom_soft_write(&eeprom_soft,
CHUNK_SIZE - 9,
&byte,
sizeof(byte)) == sizeof(byte));
byte = 0;
BTASSERT(eeprom_soft_read(&eeprom_soft,
&byte,
CHUNK_SIZE - 9,
sizeof(byte)) == sizeof(byte));
return (0);
}
static int test_read_write_bad_address(void)
{
uint8_t buf[2];
memset(&buf[0], 0, sizeof(buf));
/* Start address outside the EEPROM. */
BTASSERT(eeprom_soft_read(&eeprom_soft,
&buf[0],
CHUNK_SIZE - 8,
sizeof(buf)) == -EINVAL);
BTASSERT(eeprom_soft_write(&eeprom_soft,
CHUNK_SIZE - 8,
&buf[0],
sizeof(buf)) == -EINVAL);
/* End address outside the EEPROM. */
BTASSERT(eeprom_soft_read(&eeprom_soft,
&buf[0],
CHUNK_SIZE - 9,
sizeof(buf)) == -EINVAL);
BTASSERT(eeprom_soft_write(&eeprom_soft,
CHUNK_SIZE - 9,
&buf[0],
sizeof(buf)) == -EINVAL);
return (0);
}
static int test_mount_corrupt_data(void)
{
uint8_t byte;
BTASSERT(eeprom_soft_format(&eeprom_soft) == 0);
byte = 0x01;
BTASSERT(flash_write(&flash,
FLASH_ADDRESS + 8,
&byte,
sizeof(byte)) == sizeof(byte));
BTASSERT(eeprom_soft_mount(&eeprom_soft) == -1);
return (0);
}
#if defined(ARCH_LINUX)
static int test_mount_corrupt_header_valid(void)
{
BTASSERT(eeprom_soft_format(&eeprom_soft) == 0);
/* Overwrite parts of the valid flag. */
flash_buf[7] = 1;
BTASSERT(eeprom_soft_mount(&eeprom_soft) == -1);
return (0);
}
static int test_mount_corrupt_header_crc(void)
{
BTASSERT(eeprom_soft_format(&eeprom_soft) == 0);
/* Overwrite parts of the crc. */
flash_buf[0] = 1;
BTASSERT(eeprom_soft_mount(&eeprom_soft) == -1);
return (0);
}
static int test_mount_flash_read_fails(void)
{
ssize_t res;
BTASSERT(eeprom_soft_format(&eeprom_soft) == 0);
res = -1;
harness_mock_write("flash_read(): return (res)", &res, sizeof(res));
BTASSERT(eeprom_soft_mount(&eeprom_soft) == -1);
return (0);
}
static int test_mount_after_write(void)
{
uint8_t byte;
BTASSERT(eeprom_soft_format(&eeprom_soft) == 0);
BTASSERT(eeprom_soft_mount(&eeprom_soft) == 0);
byte = 1;
BTASSERT(eeprom_soft_write(&eeprom_soft,
0,
&byte,
sizeof(byte)) == sizeof(byte));
BTASSERT(eeprom_soft_mount(&eeprom_soft) == 0);
return (0);
}
static int test_write_flash_read_fails_blank_chunk(void)
{
uint8_t byte;
ssize_t res;
BTASSERT(eeprom_soft_format(&eeprom_soft) == 0);
BTASSERT(eeprom_soft_mount(&eeprom_soft) == 0);
/* Flash read fails checking blank chunk. */
res = -1;
harness_mock_write("flash_read(): return (res)", &res, sizeof(res));
byte = 1;
BTASSERT(eeprom_soft_write(&eeprom_soft,
0,
&byte,
sizeof(byte)) == sizeof(byte));
return (0);
}
static int test_write_blank_chunk_not_blank(void)
{
uint8_t byte;
BTASSERT(eeprom_soft_format(&eeprom_soft) == 0);
BTASSERT(eeprom_soft_mount(&eeprom_soft) == 0);
/* Overwrite a byte in the first blank chunk. */
flash_buf[CHUNK_SIZE] = 0;
byte = 1;
BTASSERT(eeprom_soft_write(&eeprom_soft,
0,
&byte,
sizeof(byte)) == sizeof(byte));
return (0);
}
static int test_format_flash_erase_fails(void)
{
int res;
/* Flash erase fails. */
res = -1;
harness_mock_write("flash_erase(): return (res)", &res, sizeof(res));
BTASSERT(eeprom_soft_format(&eeprom_soft) == -1);
return (0);
}
int __wrap_flash_module_init(void)
{
return (0);
}
int __wrap_flash_init(struct flash_driver_t *self_p,
struct flash_device_t *dev_p)
{
BTASSERT(self_p != NULL);
BTASSERT(dev_p != NULL);
memset(&flash_buf[0], 0, sizeof(flash_buf));
return (0);
}
ssize_t __wrap_flash_read(struct flash_driver_t *self_p,
void *dst_p,
uintptr_t src,
size_t size)
{
BTASSERT(self_p != NULL);
BTASSERTI(src, <, FLASH_SIZE);
BTASSERTI(size, <=, CHUNK_SIZE);
ssize_t res;
int res2;
res2 = harness_mock_try_read("flash_read(): return (res)",
&res,
sizeof(res));
if (res2 == -ENOENT) {
memcpy(dst_p, &flash_buf[src], size);
res = size;
}
return (res);
}
ssize_t __wrap_flash_write(struct flash_driver_t *self_p,
uintptr_t dst,
const void *src_p,
size_t size)
{
BTASSERT(self_p != NULL);
BTASSERT(dst < FLASH_SIZE);
BTASSERT(size <= CHUNK_SIZE);
memcpy(&flash_buf[dst], src_p, size);
return (size);
}
int __wrap_flash_erase(struct flash_driver_t *self_p,
uintptr_t addr,
size_t size)
{
BTASSERT(self_p != NULL);
BTASSERT(addr < FLASH_SIZE);
BTASSERT(size <= FLASH_SIZE);
int res;
int res2;
res2 = harness_mock_try_read("flash_erase(): return (res)",
&res,
sizeof(res));
if (res2 == -ENOENT) {
memset(&flash_buf[addr], 0xff, size);
res = 0;
}
return (res);
}
#endif
int main()
{
struct harness_testcase_t testcases[] = {
{ test_init, "test_init" },
{ test_format_mount, "test_format_mount" },
{ test_read_write_sizes, "test_read_write_sizes" },
{ test_read_write_low_high, "test_read_write_low_high" },
{ test_read_write_bad_address, "test_read_write_bad_address" },
{ test_mount_corrupt_data, "test_mount_corrupt_data" },
#if defined(ARCH_LINUX)
{ test_mount_corrupt_header_valid, "test_mount_corrupt_header_valid" },
{ test_mount_corrupt_header_crc, "test_mount_corrupt_header_crc" },
{ test_mount_flash_read_fails, "test_mount_flash_read_fails" },
{ test_mount_after_write, "test_mount_after_write" },
{
test_write_flash_read_fails_blank_chunk,
"test_write_flash_read_fails_blank_chunk"
},
{ test_write_blank_chunk_not_blank, "test_write_blank_chunk_not_blank" },
{ test_format_flash_erase_fails, "test_format_flash_erase_fails" },
#endif
{ NULL, NULL }
};
sys_start();
harness_run(testcases);
return (0);
}