Debian Codegraph @

tests/erase_func_algo.c

raw
1 /*
2 * This file is part of the flashrom project.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <include/test.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "tests.h"
20 #include "chipdrivers.h"
21 #include "flash.h"
22 #include "io_mock.h"
23 #include "libflashrom.h"
24 #include "programmer.h"
25
26 #define LOG_ERASE_FUNC printf("Eraser called with blockaddr=0x%x, blocklen=0x%x, erase_func=%d\n", blockaddr, blocklen, erase_func - TEST_ERASE_INJECTOR_1 + 1)
27 #define LOG_READ_WRITE_FUNC printf("%s called with start=0x%x, len=0x%x\n", __func__, start, len)
28
29 #define ERASE_VALUE 0xff
30 #define MOCK_CHIP_SIZE 16
31 #define MIN_BUF_SIZE 1024 /* Minimum buffer size flashrom operates for chip operations. */
32 #define MIN_REAL_CHIP_SIZE 1024 /* Minimum chip size that can be defined for real chip in flashchips */
33
34 struct test_region {
35 const size_t start;
36 const size_t end;
37 const char *name;
38 };
39
40 struct erase_invoke {
41 unsigned int blockaddr;
42 unsigned int blocklen;
43 enum block_erase_func erase_func;
44 };
45
46 struct test_case {
47 struct flashchip *chip; /* Chip definition. */
48 struct test_region regions[MOCK_CHIP_SIZE]; /* Layout regions. */
49 uint8_t initial_buf[MOCK_CHIP_SIZE]; /* Initial state of chip memory. */
50 uint8_t erased_buf[MOCK_CHIP_SIZE]; /* Expected content after erase. */
51 uint8_t written_buf[MOCK_CHIP_SIZE]; /* Expected content after write. */
52 uint8_t written_protected_buf[MOCK_CHIP_SIZE]; /* Expected content after write with protected region. */
53 struct erase_invoke eraseblocks_expected[MOCK_CHIP_SIZE]; /* Expected order of eraseblocks invocations. */
54 unsigned int eraseblocks_expected_ind; /* Expected number of eraseblocks invocations. */
55 struct erase_invoke write_eraseblocks_expected[MOCK_CHIP_SIZE]; /* Expected order of eraseblocks invocations. */
56 unsigned int write_eraseblocks_expected_ind; /* Expected number of eraseblocks invocations. */
57 char erase_test_name[40]; /* Test case display name for testing erase operation. */
58 char write_test_name[40]; /* Test case display name for testing write operation. */
59 };
60
61 struct all_state {
62 uint8_t buf[MIN_REAL_CHIP_SIZE]; /* Buffer emulating the memory of the mock chip. */
63 bool was_modified[MIN_REAL_CHIP_SIZE]; /* Which bytes were modified, 0x1 if byte was modified. */
64 bool was_verified[MIN_REAL_CHIP_SIZE]; /* Which bytes were verified, 0x1 if byte was verified. */
65 struct erase_invoke eraseblocks_actual[MOCK_CHIP_SIZE]; /* The actual order of eraseblocks invocations. */
66 unsigned int eraseblocks_actual_ind; /* Actual number of eraseblocks invocations. */
67 const struct test_case* current_test_case; /* Currently executed test case. */
68 } g_state;
69
70 static int read_chip(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
71 {
72 if (start < MOCK_CHIP_SIZE)
73 LOG_READ_WRITE_FUNC;
74
75 assert_in_range(start + len, 0, MIN_REAL_CHIP_SIZE);
76
77 memcpy(buf, &g_state.buf[start], len);
78
79 /* If these bytes were modified before => current read op is verify op, track it */
80 bool bytes_modified = false;
81 for (unsigned int i = start; i < start + len; i++)
82 if (g_state.was_modified[i]) {
83 bytes_modified = true;
84 break;
85 }
86 if (bytes_modified)
87 memset(&g_state.was_verified[start], true, len);
88
89 return 0;
90 }
91
92 static int write_chip(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
93 {
94 if (start < MOCK_CHIP_SIZE)
95 LOG_READ_WRITE_FUNC;
96
97 assert_in_range(start + len, 0, MIN_REAL_CHIP_SIZE);
98
99 memcpy(&g_state.buf[start], buf, len);
100
101 /* Track the bytes were written */
102 memset(&g_state.was_modified[start], true, len);
103 /* Clear the records of previous verification, if there were any */
104 memset(&g_state.was_verified[start], false, len);
105
106 return 0;
107 }
108
109 static int block_erase_chip_tagged(struct flashctx *flash, enum block_erase_func erase_func, unsigned int blockaddr, unsigned int blocklen)
110 {
111 if (blockaddr < MOCK_CHIP_SIZE) {
112 LOG_ERASE_FUNC;
113
114 /* Register eraseblock invocation. */
115 g_state.eraseblocks_actual[g_state.eraseblocks_actual_ind] = (struct erase_invoke){
116 .blocklen = blocklen,
117 .blockaddr = blockaddr,
118 .erase_func = erase_func,
119 };
120 g_state.eraseblocks_actual_ind++;
121 }
122
123 assert_in_range(blockaddr + blocklen, 0, MIN_REAL_CHIP_SIZE);
124
125 memset(&g_state.buf[blockaddr], ERASE_VALUE, blocklen);
126
127 /* Track the bytes were erased */
128 memset(&g_state.was_modified[blockaddr], true, blocklen);
129 /* Clear the records of previous verification, if there were any */
130 memset(&g_state.was_verified[blockaddr], false, blocklen);
131
132 return 0;
133 }
134
135 static struct flashchip chip_1_2_4_8_16 = {
136 .vendor = "aklm",
137 /*
138 * total_size is supposed to be in Kilobytes and this number is multiplied
139 * by 1024 everywhere in flashrom code. With this, we can't have the chip of
140 * size MOCK_CHIP_SIZE anyway, because MOCK_CHIP_SIZE is much smaller than
141 * 1024.
142 *
143 * So setting 1 here as this is the smallest possible value of unsigned int.
144 * Why aim for the smallest value? Because various places in flashrom code
145 * are allocating buffers of size total_size * 1024, however in these unit
146 * tests only MOCK_CHIP_SIZE bytes are tracked/logged/asserted.
147 */
148 .total_size = 1,
149 .tested = TEST_OK_PREW,
150 .gran = WRITE_GRAN_1BYTE,
151 .read = TEST_READ_INJECTOR,
152 .write = TEST_WRITE_INJECTOR,
153 .block_erasers =
154 {
155 {
156 .eraseblocks = { {1, MIN_REAL_CHIP_SIZE} },
157 .block_erase = TEST_ERASE_INJECTOR_1,
158 }, {
159 .eraseblocks = { {2, MIN_REAL_CHIP_SIZE / 2} },
160 .block_erase = TEST_ERASE_INJECTOR_2,
161 }, {
162 .eraseblocks = { {4, MIN_REAL_CHIP_SIZE / 4} },
163 .block_erase = TEST_ERASE_INJECTOR_3,
164 }, {
165 .eraseblocks = { {8, MIN_REAL_CHIP_SIZE / 8} },
166 .block_erase = TEST_ERASE_INJECTOR_4,
167 }, {
168 .eraseblocks = { {16, MIN_REAL_CHIP_SIZE / 16} },
169 .block_erase = TEST_ERASE_INJECTOR_5,
170 }
171 },
172 };
173
174 static struct flashchip chip_1_8_16 = {
175 .vendor = "aklm",
176 /* See comment on previous chip. */
177 .total_size = 1,
178 .tested = TEST_OK_PREW,
179 .gran = WRITE_GRAN_1BYTE,
180 .read = TEST_READ_INJECTOR,
181 .write = TEST_WRITE_INJECTOR,
182 .block_erasers =
183 {
184 {
185 .eraseblocks = { {1, MIN_REAL_CHIP_SIZE} },
186 .block_erase = TEST_ERASE_INJECTOR_1,
187 }, {
188 .eraseblocks = { {8, MIN_REAL_CHIP_SIZE / 8} },
189 .block_erase = TEST_ERASE_INJECTOR_4,
190 }, {
191 .eraseblocks = { {16, MIN_REAL_CHIP_SIZE / 16} },
192 .block_erase = TEST_ERASE_INJECTOR_5,
193 }
194 },
195 };
196
197 static struct flashchip chip_8_16 = {
198 .vendor = "aklm",
199 /* See comment on previous chip. */
200 .total_size = 1,
201 .tested = TEST_OK_PREW,
202 .gran = WRITE_GRAN_1BYTE,
203 .read = TEST_READ_INJECTOR,
204 .write = TEST_WRITE_INJECTOR,
205 .block_erasers =
206 {
207 {
208 .eraseblocks = { {8, MIN_REAL_CHIP_SIZE / 8} },
209 .block_erase = TEST_ERASE_INJECTOR_4,
210 }, {
211 .eraseblocks = { {16, MIN_REAL_CHIP_SIZE / 16} },
212 .block_erase = TEST_ERASE_INJECTOR_5,
213 }
214 },
215 };
216
217 static struct flashchip chip_1_4_16 = {
218 .vendor = "aklm",
219 /* See comment on previous chip. */
220 .total_size = 1,
221 .tested = TEST_OK_PREW,
222 .gran = WRITE_GRAN_1BYTE,
223 .read = TEST_READ_INJECTOR,
224 .write = TEST_WRITE_INJECTOR,
225 .block_erasers =
226 {
227 {
228 .eraseblocks = { {1, MIN_REAL_CHIP_SIZE} },
229 .block_erase = TEST_ERASE_INJECTOR_1,
230 }, {
231 .eraseblocks = { {4, MIN_REAL_CHIP_SIZE / 4} },
232 .block_erase = TEST_ERASE_INJECTOR_3,
233 }, {
234 .eraseblocks = { {16, MIN_REAL_CHIP_SIZE / 16} },
235 .block_erase = TEST_ERASE_INJECTOR_5,
236 }
237 },
238 };
239
240 #define BLOCK_ERASE_FUNC(n) \
241 static int block_erase_chip_ ## n (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen) { \
242 return block_erase_chip_tagged(flash, TEST_ERASE_INJECTOR_ ## n, blockaddr, blocklen); \
243 }
244 BLOCK_ERASE_FUNC(1)
245 BLOCK_ERASE_FUNC(2)
246 BLOCK_ERASE_FUNC(3)
247 BLOCK_ERASE_FUNC(4)
248 BLOCK_ERASE_FUNC(5)
249
250 /*
251 * Returns the offset how far we need to verify mock chip memory.
252 * Which is minimum out of MOCK_CHIP_SIZE and the end of the logical layout.
253 */
254 static chipoff_t setup_chip(struct flashrom_flashctx *flashctx, struct flashrom_layout **layout,
255 const char *programmer_param, struct test_case *current_test_case)
256 {
257 chipoff_t verify_end_boundary = MOCK_CHIP_SIZE - 1;
258
259 g_test_write_injector = write_chip;
260 g_test_read_injector = read_chip;
261 /* Each erasefunc corresponds to an operation that erases a block of
262 * the chip with a particular size in bytes. */
263 memcpy(g_test_erase_injector,
264 (erasefunc_t *const[]){
265 block_erase_chip_1, // 1 byte
266 block_erase_chip_2, // 2 bytes
267 block_erase_chip_3, // 4 bytes
268 block_erase_chip_4, // 8 bytes
269 block_erase_chip_5, // 16 bytes
270 },
271 sizeof(g_test_erase_injector)
272 );
273
274 /* First MOCK_CHIP_SIZE bytes have a meaning and set with given values for this test case. */
275 memcpy(g_state.buf, current_test_case->initial_buf, MOCK_CHIP_SIZE);
276 /* The rest of mock chip memory does not matter. */
277 memset(g_state.buf + MOCK_CHIP_SIZE, ERASE_VALUE, MIN_REAL_CHIP_SIZE - MOCK_CHIP_SIZE);
278
279 /* Clear eraseblock invocation records. */
280 memset(g_state.eraseblocks_actual, 0, MOCK_CHIP_SIZE * sizeof(struct erase_invoke));
281 g_state.eraseblocks_actual_ind = 0;
282
283 /* Clear the tracking of each byte modified. */
284 memset(g_state.was_modified, false, MIN_REAL_CHIP_SIZE);
285 /* Clear the tracking of each byte verified. */
286 memset(g_state.was_verified, false, MIN_REAL_CHIP_SIZE);
287
288 /* Set the flag to verify after writing on chip */
289 flashrom_flag_set(flashctx, FLASHROM_FLAG_VERIFY_AFTER_WRITE, true);
290
291 flashctx->chip = current_test_case->chip;
292
293 printf("Creating layout ... ");
294 assert_int_equal(0, flashrom_layout_new(layout));
295
296 /* Adding regions from test case. */
297 int i = 0;
298 while (current_test_case->regions[i].name != NULL) {
299 assert_int_equal(0, flashrom_layout_add_region(*layout,
300 current_test_case->regions[i].start,
301 current_test_case->regions[i].end,
302 current_test_case->regions[i].name));
303 assert_int_equal(0, flashrom_layout_include_region(*layout, current_test_case->regions[i].name));
304
305 if (current_test_case->regions[i].end < MOCK_CHIP_SIZE - 1)
306 verify_end_boundary = current_test_case->regions[i].end;
307 else
308 verify_end_boundary = MOCK_CHIP_SIZE - 1;
309
310 i++;
311 }
312
313 flashrom_layout_set(flashctx, *layout);
314 printf("done\n");
315
316 /*
317 * We need some programmer (any), and dummy is a very good one,
318 * because it doesn't need any mocking. So no extra complexity
319 * from a programmer side, and test can focus on working with the chip.
320 */
321 printf("Dummyflasher initialising with param=\"%s\"... ", programmer_param);
322 assert_int_equal(0, programmer_init(&programmer_dummy, programmer_param));
323 /* Assignment below normally happens while probing, but this test is not probing. */
324 flashctx->mst = &registered_masters[0];
325 printf("done\n");
326
327 return verify_end_boundary;
328 }
329
330 static void teardown_chip(struct flashrom_layout **layout)
331 {
332 printf("Dummyflasher shutdown... ");
333 assert_int_equal(0, programmer_shutdown());
334 printf("done\n");
335
336 printf("Releasing layout... ");
337 flashrom_layout_release(*layout);
338 printf("done\n");
339 }
340
341 /*
342 * Setup all test cases.
343 *
344 * First half of test cases is set up for a chip with erasers: 1, 2, 4, 8, 16 bytes.
345 * Second half repeates the same test cases for a chip with erasers: 1, 8, 16 bytes.
346 * Tests from #16 onwards use the chip with erasers: 8, 16 bytes, to test unaligned layout regions.
347 */
348 static struct test_case test_cases[] = {
349 {
350 /*
351 * Test case #0
352 *
353 * Initial vs written: all 16 bytes are different.
354 * One layout region for the whole chip.
355 * Chip with eraseblocks 1, 2, 4, 8, 16.
356 */
357 .chip = &chip_1_2_4_8_16,
358 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
359 .initial_buf = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
360 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
361 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
362 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
363 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
364 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
365 .written_buf = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
366 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f},
367 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
368 .eraseblocks_expected_ind = 1,
369 .write_eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
370 .write_eraseblocks_expected_ind = 1,
371 .erase_test_name = "Erase test case #0",
372 .write_test_name = "Write test case #0",
373 }, {
374 /*
375 * Test case #1
376 *
377 * Initial vs written: 9 bytes the same, 7 bytes different.
378 * Two layout regions each one 8 bytes, which is 1/2 size of chip.
379 * Chip with eraseblocks 1, 2, 4, 8, 16.
380 */
381 .chip = &chip_1_2_4_8_16,
382 .regions = {{0, MOCK_CHIP_SIZE/2 - 1, "part1"}, {MOCK_CHIP_SIZE/2, MIN_REAL_CHIP_SIZE - 1, "part2"}},
383 .initial_buf = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
384 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xf1},
385 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
386 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
387 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
388 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
389 .written_buf = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
390 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
391 .eraseblocks_expected = {{0x8, 0x8, TEST_ERASE_INJECTOR_4}, {0x0, 0x8, TEST_ERASE_INJECTOR_4}},
392 .eraseblocks_expected_ind = 2,
393 .write_eraseblocks_expected = {{0x9, 0x1, TEST_ERASE_INJECTOR_1},
394 {0xa, 0x2, TEST_ERASE_INJECTOR_2},
395 {0xc, 0x4, TEST_ERASE_INJECTOR_3}},
396 .write_eraseblocks_expected_ind = 3,
397 .erase_test_name = "Erase test case #1",
398 .write_test_name = "Write test case #1",
399 }, {
400 /*
401 * Test case #2
402 *
403 * Initial vs written: 6 bytes the same, 4 bytes different, 4 bytes the same, 2 bytes different.
404 * Two layout regions 11 and 5 bytes each.
405 * Chip with eraseblocks 1, 2, 4, 8, 16.
406 */
407 .chip = &chip_1_2_4_8_16,
408 .regions = {{0, 10, "odd1"}, {11, 15, "odd2"}, {MOCK_CHIP_SIZE, MIN_REAL_CHIP_SIZE - 1, "longtail"}},
409 .initial_buf = {0xff, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff,
410 0x0, 0xff, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff},
411 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
412 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
413 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
414 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
415 .written_buf = {0xff, 0xff, 0x0, 0xff, 0x0, 0xff, 0x20, 0x2f,
416 0x20, 0x2f, 0x0, 0xff, 0xff, 0xff, 0x2f, 0x2f},
417 .eraseblocks_expected = {
418 {0xb, 0x1, TEST_ERASE_INJECTOR_1},
419 {0xc, 0x4, TEST_ERASE_INJECTOR_3},
420 {0xa, 0x1, TEST_ERASE_INJECTOR_1},
421 {0x8, 0x2, TEST_ERASE_INJECTOR_2},
422 {0x0, 0x8, TEST_ERASE_INJECTOR_4}
423 },
424 .eraseblocks_expected_ind = 5,
425 .write_eraseblocks_expected = {
426 {0x6, 0x1, TEST_ERASE_INJECTOR_1},
427 {0x8, 0x1, TEST_ERASE_INJECTOR_1}
428 },
429 .write_eraseblocks_expected_ind = 2,
430 .erase_test_name = "Erase test case #2",
431 .write_test_name = "Write test case #2",
432 }, {
433 /*
434 * Test case #3
435 *
436 * Initial vs written: 4 bytes the same, 4 bytes different, 8 bytes the same.
437 * One layout region covering the whole chip.
438 * Chip with eraseblocks 1, 2, 4, 8, 16.
439 */
440 .chip = &chip_1_2_4_8_16,
441 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
442 .initial_buf = {0xff, 0xff, 0xff, 0xff, 0x11, 0x22, 0x33, 0x44,
443 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
444 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
445 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
446 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
447 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
448 .written_buf = {0xff, 0xff, 0xff, 0xff, 0x1, 0x2, 0x3, 0x4,
449 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
450 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
451 .eraseblocks_expected_ind = 1,
452 .write_eraseblocks_expected = {{0x4, 0x4, TEST_ERASE_INJECTOR_3}},
453 .write_eraseblocks_expected_ind = 1,
454 .erase_test_name = "Erase test case #3",
455 .write_test_name = "Write test case #3",
456 }, {
457 /*
458 * Test case #4
459 *
460 * Initial vs written: 4 bytes different, 4 bytes the same, 8 bytes different.
461 * One layout region covering the whole chip.
462 * Chip with eraseblocks 1, 2, 4, 8, 16.
463 */
464 .chip = &chip_1_2_4_8_16,
465 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
466 .initial_buf = {0x1, 0x2, 0x3, 0x4, 0xff, 0xff, 0xff, 0xff,
467 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18},
468 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
469 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
470 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
471 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
472 .written_buf = {0x11, 0x22, 0x33, 0x44, 0xff, 0xff, 0xff, 0xff,
473 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8},
474 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
475 .eraseblocks_expected_ind = 1,
476 .write_eraseblocks_expected = {{0x0, 0x4, TEST_ERASE_INJECTOR_3}, {0x8, 0x8, TEST_ERASE_INJECTOR_4}},
477 .write_eraseblocks_expected_ind = 2,
478 .erase_test_name = "Erase test case #4",
479 .write_test_name = "Write test case #4",
480 }, {
481 /*
482 * Test case #5
483 *
484 * Initial vs written: 7 bytes different, 1 bytes the same, 8 bytes different.
485 * One layout region covering the whole chip.
486 * Chip with eraseblocks 1, 2, 4, 8, 16.
487 */
488 .chip = &chip_1_2_4_8_16,
489 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
490 .initial_buf = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0xff,
491 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18},
492 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
493 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
494 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
495 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
496 .written_buf = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xff,
497 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8},
498 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
499 .eraseblocks_expected_ind = 1,
500 .write_eraseblocks_expected = {{0x6, 0x1, TEST_ERASE_INJECTOR_1},
501 {0x4, 0x2, TEST_ERASE_INJECTOR_2},
502 {0x0, 0x4, TEST_ERASE_INJECTOR_3},
503 {0x8, 0x8, TEST_ERASE_INJECTOR_4}},
504 .write_eraseblocks_expected_ind = 4,
505 .erase_test_name = "Erase test case #5",
506 .write_test_name = "Write test case #5",
507 }, {
508 /*
509 * Test case #6
510 *
511 * Initial vs written: 7 bytes the same, 1 bytes different, 8 bytes the same.
512 * One layout region covering the whole chip.
513 * Chip with eraseblocks 1, 2, 4, 8, 16.
514 */
515 .chip = &chip_1_2_4_8_16,
516 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
517 .initial_buf = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1d,
518 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8},
519 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
520 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
521 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
522 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
523 .written_buf = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0xdd,
524 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8},
525 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
526 .eraseblocks_expected_ind = 1,
527 .write_eraseblocks_expected = {{0x7, 0x1, TEST_ERASE_INJECTOR_1}},
528 .write_eraseblocks_expected_ind = 1,
529 .erase_test_name = "Erase test case #6",
530 .write_test_name = "Write test case #6",
531 }, {
532 /*
533 * Test case #7
534 *
535 * Initial vs written: all 16 bytes are different.
536 * Layout with irregular regions unaligned with eraseblocks.
537 * Chip with eraseblocks 1, 2, 4, 8, 16.
538 */
539 .chip = &chip_1_2_4_8_16,
540 .regions = {{0, 2, "reg3"}, {3, 7, "reg5"}, {8, 14, "reg7"}, {15, MIN_REAL_CHIP_SIZE - 1, "reg1"}},
541 .initial_buf = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
542 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
543 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
544 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
545 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
546 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
547 .written_buf = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
548 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f},
549 .eraseblocks_expected = {
550 {0xf, 0x1, TEST_ERASE_INJECTOR_1},
551 {0xe, 0x1, TEST_ERASE_INJECTOR_1},
552 {0xc, 0x2, TEST_ERASE_INJECTOR_2},
553 {0x8, 0x4, TEST_ERASE_INJECTOR_3},
554 {0x3, 0x1, TEST_ERASE_INJECTOR_1},
555 {0x4, 0x4, TEST_ERASE_INJECTOR_3},
556 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
557 {0x0, 0x2, TEST_ERASE_INJECTOR_2}
558 },
559 .eraseblocks_expected_ind = 8,
560 .write_eraseblocks_expected = {
561 {0xf, 0x1, TEST_ERASE_INJECTOR_1},
562 {0xe, 0x1, TEST_ERASE_INJECTOR_1},
563 {0xc, 0x2, TEST_ERASE_INJECTOR_2},
564 {0x8, 0x4, TEST_ERASE_INJECTOR_3},
565 {0x3, 0x1, TEST_ERASE_INJECTOR_1},
566 {0x4, 0x4, TEST_ERASE_INJECTOR_3},
567 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
568 {0x0, 0x2, TEST_ERASE_INJECTOR_2}
569 },
570 .write_eraseblocks_expected_ind = 8,
571 .erase_test_name = "Erase test case #7",
572 .write_test_name = "Write test case #7",
573 }, {
574 /*
575 * Test case #8
576 *
577 * Initial vs written: all 16 bytes are different.
578 * One layout region for the whole chip.
579 * Chip with eraseblocks 1, 8, 16.
580 */
581 .chip = &chip_1_8_16,
582 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
583 .initial_buf = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
584 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
585 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
586 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
587 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
588 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
589 .written_buf = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
590 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f},
591 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
592 .eraseblocks_expected_ind = 1,
593 .write_eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
594 .write_eraseblocks_expected_ind = 1,
595 .erase_test_name = "Erase test case #8",
596 .write_test_name = "Write test case #8",
597 }, {
598 /*
599 * Test case #9
600 *
601 * Initial vs written: 9 bytes the same, 7 bytes different.
602 * Two layout regions each one 8 bytes, which is 1/2 size of chip.
603 * Chip with eraseblocks 1, 8, 16.
604 */
605 .chip = &chip_1_8_16,
606 .regions = {{0, MOCK_CHIP_SIZE/2 - 1, "part1"}, {MOCK_CHIP_SIZE/2, MOCK_CHIP_SIZE - 1, "part2"},
607 {MOCK_CHIP_SIZE, MIN_REAL_CHIP_SIZE - 1, "longtail"}},
608 .initial_buf = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
609 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xf1},
610 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
611 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
612 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
613 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
614 .written_buf = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
615 0xf8, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
616 .eraseblocks_expected = {{0x8, 0x8, TEST_ERASE_INJECTOR_4}, {0x0, 0x8, TEST_ERASE_INJECTOR_4}},
617 .eraseblocks_expected_ind = 2,
618 .write_eraseblocks_expected = {{0x9, 0x1, TEST_ERASE_INJECTOR_1},
619 {0xa, 0x1, TEST_ERASE_INJECTOR_1},
620 {0xb, 0x1, TEST_ERASE_INJECTOR_1},
621 {0xc, 0x1, TEST_ERASE_INJECTOR_1},
622 {0xd, 0x1, TEST_ERASE_INJECTOR_1},
623 {0xe, 0x1, TEST_ERASE_INJECTOR_1},
624 {0xf, 0x1, TEST_ERASE_INJECTOR_1}},
625 .write_eraseblocks_expected_ind = 7,
626 .erase_test_name = "Erase test case #9",
627 .write_test_name = "Write test case #9",
628 }, {
629 /*
630 * Test case #10
631 *
632 * Initial vs written: 6 bytes the same, 4 bytes different, 4 bytes the same, 2 bytes different.
633 * Two layout regions 11 and 5 bytes each.
634 * Chip with eraseblocks 1, 8, 16.
635 */
636 .chip = &chip_1_8_16,
637 .regions = {{0, 10, "odd1"}, {11, 15, "odd2"}, {MOCK_CHIP_SIZE, MIN_REAL_CHIP_SIZE - 1, "longtail"}},
638 .initial_buf = {0xff, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff,
639 0x0, 0xff, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff},
640 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
641 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
642 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
643 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
644 .written_buf = {0xff, 0xff, 0x0, 0xff, 0x0, 0xff, 0x20, 0x2f,
645 0x20, 0x2f, 0x0, 0xff, 0xff, 0xff, 0x2f, 0x2f},
646 .eraseblocks_expected = {
647 {0xb, 0x1, TEST_ERASE_INJECTOR_1},
648 {0xc, 0x1, TEST_ERASE_INJECTOR_1},
649 {0xd, 0x1, TEST_ERASE_INJECTOR_1},
650 {0xe, 0x1, TEST_ERASE_INJECTOR_1},
651 {0xf, 0x1, TEST_ERASE_INJECTOR_1},
652 {0x8, 0x1, TEST_ERASE_INJECTOR_1},
653 {0x9, 0x1, TEST_ERASE_INJECTOR_1},
654 {0xa, 0x1, TEST_ERASE_INJECTOR_1},
655 {0x0, 0x8, TEST_ERASE_INJECTOR_4}
656 },
657 .eraseblocks_expected_ind = 9,
658 .write_eraseblocks_expected = {
659 {0x6, 0x1, TEST_ERASE_INJECTOR_1},
660 {0x8, 0x1, TEST_ERASE_INJECTOR_1}
661 },
662 .write_eraseblocks_expected_ind = 2,
663 .erase_test_name = "Erase test case #10",
664 .write_test_name = "Write test case #10",
665 }, {
666 /*
667 * Test case #11
668 *
669 * Initial vs written: 4 bytes the same, 4 bytes different, 8 bytes the same.
670 * One layout region covering the whole chip.
671 * Chip with eraseblocks 1, 8, 16.
672 */
673 .chip = &chip_1_8_16,
674 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
675 .initial_buf = {0xff, 0xff, 0xff, 0xff, 0x11, 0x22, 0x33, 0x44,
676 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
677 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
678 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
679 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
680 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
681 .written_buf = {0xff, 0xff, 0xff, 0xff, 0x1, 0x2, 0x3, 0x4,
682 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
683 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
684 .eraseblocks_expected_ind = 1,
685 .write_eraseblocks_expected = {{0x4, 0x1, TEST_ERASE_INJECTOR_1},
686 {0x5, 0x1, TEST_ERASE_INJECTOR_1},
687 {0x6, 0x1, TEST_ERASE_INJECTOR_1},
688 {0x7, 0x1, TEST_ERASE_INJECTOR_1}},
689 .write_eraseblocks_expected_ind = 4,
690 .erase_test_name = "Erase test case #11",
691 .write_test_name = "Write test case #11",
692 }, {
693 /*
694 * Test case #12
695 *
696 * Initial vs written: 4 bytes different, 4 bytes the same, 8 bytes different.
697 * One layout region covering the whole chip.
698 * Chip with eraseblocks 1, 8, 16.
699 */
700 .chip = &chip_1_8_16,
701 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
702 .initial_buf = {0x1, 0x2, 0x3, 0x4, 0xff, 0xff, 0xff, 0xff,
703 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18},
704 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
705 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
706 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
707 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
708 .written_buf = {0x11, 0x22, 0x33, 0x44, 0xff, 0xff, 0xff, 0xff,
709 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8},
710 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
711 .eraseblocks_expected_ind = 1,
712 .write_eraseblocks_expected = {{0x0, 0x1, TEST_ERASE_INJECTOR_1},
713 {0x1, 0x1, TEST_ERASE_INJECTOR_1},
714 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
715 {0x3, 0x1, TEST_ERASE_INJECTOR_1},
716 {0x8, 0x8, TEST_ERASE_INJECTOR_4}},
717 .write_eraseblocks_expected_ind = 5,
718 .erase_test_name = "Erase test case #12",
719 .write_test_name = "Write test case #12",
720 }, {
721 /*
722 * Test case #13
723 *
724 * Initial vs written: 7 bytes different, 1 bytes the same, 8 bytes different.
725 * One layout region covering the whole chip.
726 * Chip with eraseblocks 1, 8, 16.
727 */
728 .chip = &chip_1_8_16,
729 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
730 .initial_buf = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0xff,
731 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18},
732 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
733 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
734 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
735 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
736 .written_buf = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xff,
737 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8},
738 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
739 .eraseblocks_expected_ind = 1,
740 .write_eraseblocks_expected = {{0x0, 0x1, TEST_ERASE_INJECTOR_1},
741 {0x1, 0x1, TEST_ERASE_INJECTOR_1},
742 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
743 {0x3, 0x1, TEST_ERASE_INJECTOR_1},
744 {0x4, 0x1, TEST_ERASE_INJECTOR_1},
745 {0x5, 0x1, TEST_ERASE_INJECTOR_1},
746 {0x6, 0x1, TEST_ERASE_INJECTOR_1},
747 {0x8, 0x8, TEST_ERASE_INJECTOR_4}},
748 .write_eraseblocks_expected_ind = 8,
749 .erase_test_name = "Erase test case #13",
750 .write_test_name = "Write test case #13",
751 }, {
752 /*
753 * Test case #14
754 *
755 * Initial vs written: 7 bytes the same, 1 bytes different, 8 bytes the same.
756 * One layout region covering the whole chip.
757 * Chip with eraseblocks 1, 8, 16.
758 */
759 .chip = &chip_1_8_16,
760 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
761 .initial_buf = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1d,
762 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8},
763 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
764 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
765 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
766 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
767 .written_buf = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0xdd,
768 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8},
769 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
770 .eraseblocks_expected_ind = 1,
771 .write_eraseblocks_expected = {{0x7, 0x1, TEST_ERASE_INJECTOR_1}},
772 .write_eraseblocks_expected_ind = 1,
773 .erase_test_name = "Erase test case #14",
774 .write_test_name = "Write test case #14",
775 }, {
776 /*
777 * Test case #15
778 *
779 * Initial vs written: all 16 bytes are different.
780 * Layout with irregular regions unaligned with eraseblocks.
781 * Chip with eraseblocks 1, 8, 16.
782 */
783 .chip = &chip_1_8_16,
784 .regions = {{0, 2, "reg3"}, {3, 7, "reg5"}, {8, 14, "reg7"},
785 {15, MIN_REAL_CHIP_SIZE - 1, "reg1"}},
786 .initial_buf = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
787 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
788 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
789 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
790 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
791 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
792 .written_buf = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
793 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f},
794 .eraseblocks_expected = {
795 {0xf, 0x1, TEST_ERASE_INJECTOR_1},
796 {0x8, 0x1, TEST_ERASE_INJECTOR_1},
797 {0x9, 0x1, TEST_ERASE_INJECTOR_1},
798 {0xa, 0x1, TEST_ERASE_INJECTOR_1},
799 {0xb, 0x1, TEST_ERASE_INJECTOR_1},
800 {0xc, 0x1, TEST_ERASE_INJECTOR_1},
801 {0xd, 0x1, TEST_ERASE_INJECTOR_1},
802 {0xe, 0x1, TEST_ERASE_INJECTOR_1},
803 {0x3, 0x1, TEST_ERASE_INJECTOR_1},
804 {0x4, 0x1, TEST_ERASE_INJECTOR_1},
805 {0x5, 0x1, TEST_ERASE_INJECTOR_1},
806 {0x6, 0x1, TEST_ERASE_INJECTOR_1},
807 {0x7, 0x1, TEST_ERASE_INJECTOR_1},
808 {0x0, 0x1, TEST_ERASE_INJECTOR_1},
809 {0x1, 0x1, TEST_ERASE_INJECTOR_1},
810 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
811 },
812 .eraseblocks_expected_ind = 16,
813 .write_eraseblocks_expected = {
814 {0xf, 0x1, TEST_ERASE_INJECTOR_1},
815 {0x8, 0x1, TEST_ERASE_INJECTOR_1},
816 {0x9, 0x1, TEST_ERASE_INJECTOR_1},
817 {0xa, 0x1, TEST_ERASE_INJECTOR_1},
818 {0xb, 0x1, TEST_ERASE_INJECTOR_1},
819 {0xc, 0x1, TEST_ERASE_INJECTOR_1},
820 {0xd, 0x1, TEST_ERASE_INJECTOR_1},
821 {0xe, 0x1, TEST_ERASE_INJECTOR_1},
822 {0x3, 0x1, TEST_ERASE_INJECTOR_1},
823 {0x4, 0x1, TEST_ERASE_INJECTOR_1},
824 {0x5, 0x1, TEST_ERASE_INJECTOR_1},
825 {0x6, 0x1, TEST_ERASE_INJECTOR_1},
826 {0x7, 0x1, TEST_ERASE_INJECTOR_1},
827 {0x0, 0x1, TEST_ERASE_INJECTOR_1},
828 {0x1, 0x1, TEST_ERASE_INJECTOR_1},
829 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
830 },
831 .write_eraseblocks_expected_ind = 16,
832 .erase_test_name = "Erase test case #15",
833 .write_test_name = "Write test case #15",
834 }, {
835 /*
836 * Test case #16
837 *
838 * Initial vs written: all 16 bytes are different.
839 * Layout with unaligned regions 2+4+9+1b which are smaller than the smallest eraseblock.
840 * Chip with eraseblocks 8, 16.
841 */
842 .chip = &chip_8_16,
843 .regions = {{0, 1, "reg2"}, {2, 5, "reg4"}, {6, 14, "reg9"},
844 {15, MIN_REAL_CHIP_SIZE - 1, "reg1"}},
845 .initial_buf = {0x4, 0x4, 0x5, 0x5, 0x5, 0x5, 0x6, 0x6,
846 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x7},
847 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
848 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
849 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
850 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
851 .written_buf = {0x14, 0x14, 0x15, 0x15, 0x15, 0x15, 0x16, 0x16,
852 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x17},
853 .eraseblocks_expected = {
854 {0x8, 0x8, TEST_ERASE_INJECTOR_4},
855 {0x0, 0x10, TEST_ERASE_INJECTOR_5},
856 {0x0, 0x8, TEST_ERASE_INJECTOR_4},
857 {0x0, 0x8, TEST_ERASE_INJECTOR_4},
858 },
859 .eraseblocks_expected_ind = 4,
860 .write_eraseblocks_expected = {
861 {0x8, 0x8, TEST_ERASE_INJECTOR_4},
862 {0x0, 0x10, TEST_ERASE_INJECTOR_5},
863 {0x0, 0x8, TEST_ERASE_INJECTOR_4},
864 {0x0, 0x8, TEST_ERASE_INJECTOR_4},
865 },
866 .write_eraseblocks_expected_ind = 4,
867 .erase_test_name = "Erase test case #16",
868 .write_test_name = "Write test case #16",
869 }, {
870 /*
871 * Test case #17
872 *
873 * Initial vs written: all 16 bytes are different.
874 * Layout with unaligned region 3+13b which are smaller than the smallest eraseblock.
875 * Chip with eraseblocks 8, 16.
876 */
877 .chip = &chip_8_16,
878 .regions = {{0, 2, "reg3"}, {3, MIN_REAL_CHIP_SIZE - 1, "tail"}},
879 .initial_buf = {0x4, 0x4, 0x4, 0x6, 0x6, 0x6, 0x6, 0x6,
880 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6},
881 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
882 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
883 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
884 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
885 .written_buf = {0x14, 0x14, 0x14, 0x16, 0x16, 0x16, 0x16, 0x16,
886 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16},
887 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}, {0x0, 0x8, TEST_ERASE_INJECTOR_4}},
888 .eraseblocks_expected_ind = 2,
889 .write_eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}, {0x0, 0x8, TEST_ERASE_INJECTOR_4}},
890 .write_eraseblocks_expected_ind = 2,
891 .erase_test_name = "Erase test case #17",
892 .write_test_name = "Write test case #17",
893 }, {
894 /*
895 * Test case #18
896 *
897 * Initial vs written: all 16 bytes are different.
898 * Layout with unaligned region 9+7b.
899 * Chip with eraseblocks 8, 16.
900 */
901 .chip = &chip_8_16,
902 .regions = {{0, 8, "reg9"}, {9, MIN_REAL_CHIP_SIZE - 1, "tail"}},
903 .initial_buf = {0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4,
904 0x4, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6},
905 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
906 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
907 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
908 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
909 .written_buf = {0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
910 0x14, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16},
911 .eraseblocks_expected = {{0x8, 0x8, TEST_ERASE_INJECTOR_4}, {0x0, 0x10, TEST_ERASE_INJECTOR_5}},
912 .eraseblocks_expected_ind = 2,
913 .write_eraseblocks_expected = {{0x8, 0x8, TEST_ERASE_INJECTOR_4}, {0x0, 0x10, TEST_ERASE_INJECTOR_5}},
914 .write_eraseblocks_expected_ind = 2,
915 .erase_test_name = "Erase test case #18",
916 .write_test_name = "Write test case #18",
917 }, {
918 /*
919 * Test case #19
920 *
921 * Initial vs written: 3 bytes of the logical layout are different, rest is the same.
922 * Layout with unaligned region 3 bytes. Layout does not cover the whole chip memory.
923 * Chip memory outside of logical layout is skipped by both erase and write ops.
924 * Chip with eraseblocks 8, 16.
925 */
926 .chip = &chip_8_16,
927 .regions = {{0, 2, "reg3"}},
928 .initial_buf = {0x4, 0x4, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0,
929 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
930 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, 0x0,
931 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
932 .written_buf = {0x14, 0x14, 0x14, 0x0, 0x0, 0x0, 0x0, 0x0,
933 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
934 .eraseblocks_expected = {{0x0, 0x8, TEST_ERASE_INJECTOR_4}},
935 .eraseblocks_expected_ind = 1,
936 .write_eraseblocks_expected = {{0x0, 0x8, TEST_ERASE_INJECTOR_4}},
937 .write_eraseblocks_expected_ind = 1,
938 .erase_test_name = "Erase test case #19",
939 .write_test_name = "Write test case #19",
940 }, {
941 /*
942 * Test case #20
943 *
944 * Initial vs written: 3s+1d+3s+1d+3s+1d+3d+1s
945 * Layout with one region covering the whole chip
946 * Chip with eraseblocks 1, 4, 16.
947 */
948 .chip = &chip_1_4_16,
949 .regions = {{0, MOCK_CHIP_SIZE - 1, "mock chip"}, {MOCK_CHIP_SIZE, MIN_REAL_CHIP_SIZE - 1, "longtail"}},
950 .initial_buf = {0x0, 0x1, 0x2, 0xf, 0x4, 0x5, 0x6, 0xf,
951 0x8, 0x9, 0xa, 0xf, 0xf, 0xf, 0xf, 0x1f},
952 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
953 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
954 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
955 ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE},
956 .written_buf = {0x01, 0x11, 0x22, 0xf, 0x44, 0x55, 0x66, 0xf,
957 0x88, 0x99, 0xaa, 0xf, 0xf, 0xf, 0xf, 0xf1},
958 .eraseblocks_expected = {{0x0, 0x10, TEST_ERASE_INJECTOR_5}},
959 .eraseblocks_expected_ind = 1,
960 .write_eraseblocks_expected = {{0x0, 0x1, TEST_ERASE_INJECTOR_1},
961 {0x1, 0x1, TEST_ERASE_INJECTOR_1},
962 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
963 {0x4, 0x1, TEST_ERASE_INJECTOR_1},
964 {0x5, 0x1, TEST_ERASE_INJECTOR_1},
965 {0x6, 0x1, TEST_ERASE_INJECTOR_1},
966 {0x8, 0x1, TEST_ERASE_INJECTOR_1},
967 {0x9, 0x1, TEST_ERASE_INJECTOR_1},
968 {0xa, 0x1, TEST_ERASE_INJECTOR_1},
969 {0xf, 0x1, TEST_ERASE_INJECTOR_1}},
970 .write_eraseblocks_expected_ind = 10,
971 .erase_test_name = "Erase test case #20",
972 .write_test_name = "Write test case #20",
973 },
974 };
975
976
977 #define START_PROTECTED_REGION 6
978 #define END_PROTECTED_REGION 13
979
980 /*
981 * Setup all test cases with protected region.
982 * Protected region is the same for all test cases, between bytes START_PROTECTED_REGION and up to END_PROTECTED_REGION.
983 */
984 static struct test_case test_cases_protected_region[] = {
985 {
986 /*
987 * Test case #0
988 *
989 * Initial vs written: all 16 bytes are different.
990 * One layout region for the whole chip.
991 * Chip with eraseblocks 1, 2, 4, 8, 16.
992 */
993 .chip = &chip_1_2_4_8_16,
994 .regions = {{0, MIN_REAL_CHIP_SIZE - 1, "whole chip"}},
995 .initial_buf = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
996 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
997 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
998 ERASE_VALUE, ERASE_VALUE, 0x6, 0x7,
999 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, ERASE_VALUE, ERASE_VALUE},
1000 .written_buf = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
1001 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff},
1002 .written_protected_buf = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0x6, 0x7,
1003 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xfe, 0xff},
1004 .eraseblocks_expected = {{0x4, 0x2, TEST_ERASE_INJECTOR_2}, {0x0, 0x4, TEST_ERASE_INJECTOR_3},
1005 {0xe, 0x2, TEST_ERASE_INJECTOR_2}},
1006 .eraseblocks_expected_ind = 3,
1007 .erase_test_name = "Erase protected region test case #0",
1008 .write_test_name = "Write protected region test case #0",
1009 }, {
1010 /*
1011 * Test case #1
1012 *
1013 * Initial vs written: all 16 bytes are different.
1014 * Two layout regions each one 8 bytes, which is 1/2 size of chip.
1015 * Chip with eraseblocks 1, 2, 4, 8, 16.
1016 */
1017 .chip = &chip_1_2_4_8_16,
1018 .regions = {{0, MOCK_CHIP_SIZE/2 - 1, "part1"}, {MOCK_CHIP_SIZE/2, MIN_REAL_CHIP_SIZE - 1, "part2"}},
1019 .initial_buf = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
1020 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff},
1021 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
1022 ERASE_VALUE, ERASE_VALUE, 0xf6, 0xf7,
1023 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, ERASE_VALUE, ERASE_VALUE},
1024 .written_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1025 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf},
1026 .written_protected_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xf6, 0xf7,
1027 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xae, 0xaf},
1028 .eraseblocks_expected = {{0xe, 0x2, TEST_ERASE_INJECTOR_2}, {0x4, 0x2, TEST_ERASE_INJECTOR_2},
1029 {0x0, 0x4, TEST_ERASE_INJECTOR_3}},
1030 .eraseblocks_expected_ind = 3,
1031 .erase_test_name = "Erase protected region test case #1",
1032 .write_test_name = "Write protected region test case #1",
1033 }, {
1034 /*
1035 * Test case #2
1036 *
1037 * Initial vs written: all 16 bytes are different.
1038 * Three layout regions 8+4+4b
1039 * Chip with eraseblocks 1, 2, 4, 8, 16.
1040 */
1041 .chip = &chip_1_2_4_8_16,
1042 .regions = {{0, 7, "odd1"}, {8, 11, "odd2"}, {12, 15, "odd3"},
1043 {MOCK_CHIP_SIZE, MIN_REAL_CHIP_SIZE - 1, "longtail"}},
1044 .initial_buf = {0xff, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff,
1045 0x0, 0xff, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff},
1046 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
1047 ERASE_VALUE, ERASE_VALUE, 0x0, 0xff,
1048 0x0, 0xff, 0x0, 0xff, 0xff, 0xff, ERASE_VALUE, ERASE_VALUE},
1049 .written_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1050 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf},
1051 .written_protected_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x0, 0xff,
1052 0x0, 0xff, 0x0, 0xff, 0xff, 0xff, 0xae, 0xaf},
1053 .eraseblocks_expected = {{0xe, 0x2, TEST_ERASE_INJECTOR_2}, {0x4, 0x2, TEST_ERASE_INJECTOR_2},
1054 {0x0, 0x4, TEST_ERASE_INJECTOR_3}},
1055 .eraseblocks_expected_ind = 3,
1056 .erase_test_name = "Erase protected region test case #2",
1057 .write_test_name = "Write protected region test case #2",
1058 }, {
1059 /*
1060 * Test case #3
1061 *
1062 * Initial vs written: all 16 bytes are different.
1063 * Layout with unaligned regions 2+4+9+1b which require use of the 1-byte erase block.
1064 * Chip with eraseblocks 1, 2, 4, 8, 16.
1065 */
1066 .chip = &chip_1_2_4_8_16,
1067 .regions = {{0, 1, "reg2"}, {2, 5, "reg4"}, {6, 14, "reg9"},
1068 {15, MIN_REAL_CHIP_SIZE - 1, "reg1"}},
1069 .initial_buf = {0x4, 0x4, 0x5, 0x5, 0x5, 0x5, 0x6, 0x6,
1070 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x7},
1071 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
1072 ERASE_VALUE, ERASE_VALUE, 0x6, 0x6,
1073 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, ERASE_VALUE, ERASE_VALUE},
1074 .written_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1075 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf},
1076 .written_protected_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x6, 0x6,
1077 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0xae, 0xaf},
1078 .eraseblocks_expected = {{0xf, 0x1, TEST_ERASE_INJECTOR_1}, {0xe, 0x1, TEST_ERASE_INJECTOR_1},
1079 {0x2, 0x2, TEST_ERASE_INJECTOR_2}, {0x4, 0x2, TEST_ERASE_INJECTOR_2},
1080 {0x0, 0x2, TEST_ERASE_INJECTOR_2}},
1081 .eraseblocks_expected_ind = 5,
1082 .erase_test_name = "Erase protected region test case #3",
1083 .write_test_name = "Write protected region test case #3",
1084 }, {
1085 /*
1086 * Test case #4
1087 *
1088 * Initial vs written: all 16 bytes are different.
1089 * Layout with unaligned region 3+13b which require use of the 1-byte erase block.
1090 * Chip with eraseblocks 1, 8, 16.
1091 */
1092 .chip = &chip_1_8_16,
1093 .regions = {{0, 2, "reg3"}, {3, MIN_REAL_CHIP_SIZE - 1, "tail"}},
1094 .initial_buf = {0x4, 0x4, 0x4, 0x6, 0x6, 0x6, 0x6, 0x6,
1095 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6},
1096 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
1097 ERASE_VALUE, ERASE_VALUE, 0x6, 0x6,
1098 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, ERASE_VALUE, ERASE_VALUE},
1099 .written_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1100 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf},
1101 .written_protected_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x6, 0x6,
1102 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0xae, 0xaf},
1103 .eraseblocks_expected = {
1104 {0x3, 0x1, TEST_ERASE_INJECTOR_1},
1105 {0x4, 0x1, TEST_ERASE_INJECTOR_1},
1106 {0x5, 0x1, TEST_ERASE_INJECTOR_1},
1107 {0xe, 0x1, TEST_ERASE_INJECTOR_1},
1108 {0xf, 0x1, TEST_ERASE_INJECTOR_1},
1109 {0x0, 0x1, TEST_ERASE_INJECTOR_1},
1110 {0x1, 0x1, TEST_ERASE_INJECTOR_1},
1111 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
1112 },
1113 .eraseblocks_expected_ind = 8,
1114 .erase_test_name = "Erase protected region test case #4",
1115 .write_test_name = "Write protected region test case #4",
1116 }, {
1117 /*
1118 * Test case #5
1119 *
1120 * Initial vs written: all 16 bytes are different.
1121 * Layout with unaligned region 9+7b.
1122 * Chip with eraseblocks 1, 8, 16.
1123 */
1124 .chip = &chip_1_8_16,
1125 .regions = {{0, 8, "reg9"}, {9, MIN_REAL_CHIP_SIZE - 1, "tail"}},
1126 .initial_buf = {0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4,
1127 0x4, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6, 0x6},
1128 .erased_buf = {ERASE_VALUE, ERASE_VALUE, ERASE_VALUE, ERASE_VALUE,
1129 ERASE_VALUE, ERASE_VALUE, 0x4, 0x4,
1130 0x4, 0x6, 0x6, 0x6, 0x6, 0x6, ERASE_VALUE, ERASE_VALUE},
1131 .written_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1132 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf},
1133 .written_protected_buf = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x4, 0x4,
1134 0x4, 0x6, 0x6, 0x6, 0x6, 0x6, 0xae, 0xaf},
1135 .eraseblocks_expected = {
1136 {0xe, 0x1, TEST_ERASE_INJECTOR_1},
1137 {0xf, 0x1, TEST_ERASE_INJECTOR_1},
1138 {0x0, 0x1, TEST_ERASE_INJECTOR_1},
1139 {0x1, 0x1, TEST_ERASE_INJECTOR_1},
1140 {0x2, 0x1, TEST_ERASE_INJECTOR_1},
1141 {0x3, 0x1, TEST_ERASE_INJECTOR_1},
1142 {0x4, 0x1, TEST_ERASE_INJECTOR_1},
1143 {0x5, 0x1, TEST_ERASE_INJECTOR_1},
1144 },
1145 .eraseblocks_expected_ind = 8,
1146 .erase_test_name = "Erase protected region test case #5",
1147 .write_test_name = "Write protected region test case #5",
1148 },
1149 };
1150
1151 static int setup(void **state) {
1152 struct test_case *current_test_case = *state;
1153 g_state.current_test_case = current_test_case;
1154 return 0;
1155 }
1156
1157 static int teardown(void **state) {
1158 return 0;
1159 }
1160
1161 /*
1162 * Creates the array of tests for each test case in test_cases[].
1163 * Each test_case produces two tests: one for erase and one for write operation.
1164 * The caller needs to free the allocated memory.
1165 */
1166 struct CMUnitTest *get_erase_func_algo_tests(size_t *num_tests) {
1167 const size_t test_cases_num = ARRAY_SIZE(test_cases);
1168
1169 // Twice the number of test cases, because each test case is run twice: for erase and write.
1170 struct CMUnitTest *all_cases = calloc(test_cases_num * 2, sizeof(struct CMUnitTest));
1171 *num_tests = test_cases_num * 2;
1172
1173 for (size_t i = 0; i < test_cases_num; i++) {
1174 all_cases[i] = (struct CMUnitTest) {
1175 .name = test_cases[i].erase_test_name,
1176 .setup_func = setup,
1177 .teardown_func = teardown,
1178 .initial_state = &test_cases[i],
1179 .test_func = erase_function_algo_test_success,
1180 };
1181 all_cases[i + test_cases_num] = (struct CMUnitTest) {
1182 .name = test_cases[i].write_test_name,
1183 .setup_func = setup,
1184 .teardown_func = teardown,
1185 .initial_state = &test_cases[i],
1186 .test_func = write_function_algo_test_success,
1187 };
1188 }
1189
1190 return all_cases;
1191 }
1192
1193 static void test_erase_fails_for_unwritable_region(void **);
1194 static void test_write_with_sacrifice_ratio50(void **);
1195 static void erase_unwritable_regions_skipflag_on_test_success(void **);
1196 static void write_unwritable_regions_skipflag_on_test_success(void **);
1197
1198 /*
1199 * Creates the array of tests for each test case in test_cases_protected_region[].
1200 * The caller needs to free the allocated memory.
1201 */
1202 struct CMUnitTest *get_erase_protected_region_algo_tests(size_t *num_tests) {
1203 const size_t num_parameterized = ARRAY_SIZE(test_cases_protected_region);
1204 const size_t num_unparameterized = 2;
1205 // Twice the number of parameterized test cases, because each test case is run twice:
1206 // for erase and write.
1207 const size_t num_cases = num_parameterized * 2 + num_unparameterized;
1208
1209 struct CMUnitTest *all_cases = calloc(num_cases, sizeof(struct CMUnitTest));
1210 *num_tests = num_cases;
1211
1212 for (size_t i = 0; i < num_parameterized; i++) {
1213 all_cases[i] = (struct CMUnitTest) {
1214 .name = test_cases_protected_region[i].erase_test_name,
1215 .setup_func = setup,
1216 .teardown_func = teardown,
1217 .initial_state = &test_cases_protected_region[i],
1218 .test_func = erase_unwritable_regions_skipflag_on_test_success,
1219 };
1220 all_cases[i + num_parameterized] = (struct CMUnitTest) {
1221 .name = test_cases_protected_region[i].write_test_name,
1222 .setup_func = setup,
1223 .teardown_func = teardown,
1224 .initial_state = &test_cases_protected_region[i],
1225 .test_func = write_unwritable_regions_skipflag_on_test_success,
1226 };
1227 }
1228
1229 memcpy(
1230 &all_cases[num_parameterized * 2],
1231 (const struct CMUnitTest[]){
1232 (const struct CMUnitTest) {
1233 .name = "erase failure for unskipped unwritable regions",
1234 .test_func = test_erase_fails_for_unwritable_region,
1235 },
1236 (const struct CMUnitTest) {
1237 .name = "write with sacrifice ratio 50",
1238 .test_func = test_write_with_sacrifice_ratio50,
1239 },
1240 },
1241 sizeof(*all_cases) * num_unparameterized
1242 );
1243
1244 return all_cases;
1245 }
1246
1247 /*
1248 * This function is invoked for every test case in test_cases[],
1249 * current test case is passed as an argument.
1250 */
1251 void erase_function_algo_test_success(void **state)
1252 {
1253 struct test_case* current_test_case = *state;
1254
1255 int all_erase_tests_result = 0;
1256 struct flashrom_flashctx flashctx = { 0 };
1257 const char *param = ""; /* Default values for all params. */
1258
1259 struct flashrom_layout *layout;
1260
1261 const chipoff_t verify_end_boundary = setup_chip(&flashctx, &layout, param, current_test_case);
1262
1263 printf("%s started.\n", current_test_case->erase_test_name);
1264 int ret = flashrom_flash_erase(&flashctx);
1265 printf("%s returned %d.\n", current_test_case->erase_test_name, ret);
1266
1267 int chip_erased = !memcmp(g_state.buf, current_test_case->erased_buf, MOCK_CHIP_SIZE);
1268
1269 int eraseblocks_in_order = !memcmp(g_state.eraseblocks_actual,
1270 current_test_case->eraseblocks_expected,
1271 current_test_case->eraseblocks_expected_ind * sizeof(struct erase_invoke));
1272
1273 int eraseblocks_invocations = (g_state.eraseblocks_actual_ind ==
1274 current_test_case->eraseblocks_expected_ind);
1275
1276 int chip_verified = 1;
1277 for (unsigned int i = 0; i <= verify_end_boundary; i++)
1278 if (g_state.was_modified[i] && !g_state.was_verified[i]) {
1279 chip_verified = 0; /* byte was modified, but not verified after */
1280 printf("Error: byte 0x%x, modified: %d, verified: %d\n", i, g_state.was_modified[i], g_state.was_verified[i]);
1281 }
1282
1283 if (chip_erased)
1284 printf("Erased chip memory state for %s is CORRECT\n",
1285 current_test_case->erase_test_name);
1286 else
1287 printf("Erased chip memory state for %s is WRONG\n",
1288 current_test_case->erase_test_name);
1289
1290 if (eraseblocks_in_order)
1291 printf("Eraseblocks order of invocation for %s is CORRECT\n",
1292 current_test_case->erase_test_name);
1293 else
1294 printf("Eraseblocks order of invocation for %s is WRONG\n",
1295 current_test_case->erase_test_name);
1296
1297 if (eraseblocks_invocations)
1298 printf("Eraseblocks number of invocations for %s is CORRECT\n",
1299 current_test_case->erase_test_name);
1300 else
1301 printf("Eraseblocks number of invocations for %s is WRONG, expected %d actual %d\n",
1302 current_test_case->erase_test_name,
1303 current_test_case->eraseblocks_expected_ind,
1304 g_state.eraseblocks_actual_ind);
1305
1306 if (chip_verified)
1307 printf("Erased chip memory state for %s was verified successfully\n",
1308 current_test_case->erase_test_name);
1309 else
1310 printf("Erased chip memory state for %s was NOT verified completely\n",
1311 current_test_case->erase_test_name);
1312
1313 all_erase_tests_result |= ret;
1314 all_erase_tests_result |= !chip_erased;
1315 all_erase_tests_result |= !eraseblocks_in_order;
1316 all_erase_tests_result |= !eraseblocks_invocations;
1317 all_erase_tests_result |= !chip_verified;
1318
1319 teardown_chip(&layout);
1320
1321 assert_int_equal(0, all_erase_tests_result);
1322 }
1323
1324 /*
1325 * This function is invoked for every test case in test_cases[],
1326 * current test case is passed as an argument.
1327 */
1328 void write_function_algo_test_success(void **state)
1329 {
1330 struct test_case* current_test_case = *state;
1331
1332 int all_write_test_result = 0;
1333 struct flashrom_flashctx flashctx = { 0 };
1334 uint8_t newcontents[MIN_BUF_SIZE];
1335 const char *param = ""; /* Default values for all params. */
1336
1337 struct flashrom_layout *layout;
1338
1339 const chipoff_t verify_end_boundary = setup_chip(&flashctx, &layout, param, current_test_case);
1340 memcpy(&newcontents, current_test_case->written_buf, MOCK_CHIP_SIZE);
1341
1342 printf("%s started.\n", current_test_case->write_test_name);
1343 int ret = flashrom_image_write(&flashctx, &newcontents, MIN_BUF_SIZE, NULL);
1344 printf("%s returned %d.\n", current_test_case->write_test_name, ret);
1345
1346 int chip_written = !memcmp(g_state.buf, current_test_case->written_buf, MOCK_CHIP_SIZE);
1347
1348 int eraseblocks_in_order = !memcmp(g_state.eraseblocks_actual,
1349 current_test_case->write_eraseblocks_expected,
1350 current_test_case->write_eraseblocks_expected_ind * sizeof(struct erase_invoke));
1351
1352 int eraseblocks_invocations = (g_state.eraseblocks_actual_ind ==
1353 current_test_case->write_eraseblocks_expected_ind);
1354
1355 int chip_verified = 1;
1356 for (unsigned int i = 0; i <= verify_end_boundary; i++)
1357 if (g_state.was_modified[i] && !g_state.was_verified[i]) {
1358 chip_verified = 0; /* the byte was modified, but not verified after */
1359 printf("Error: byte 0x%x, modified: %d, verified: %d\n", i, g_state.was_modified[i], g_state.was_verified[i]);
1360 }
1361
1362 if (chip_written)
1363 printf("Written chip memory state for %s is CORRECT\n",
1364 current_test_case->write_test_name);
1365 else
1366 printf("Written chip memory state for %s is WRONG\n",
1367 current_test_case->write_test_name);
1368
1369 if (eraseblocks_in_order)
1370 printf("Eraseblocks order of invocation for %s is CORRECT\n",
1371 current_test_case->write_test_name);
1372 else
1373 printf("Eraseblocks order of invocation for %s is WRONG\n",
1374 current_test_case->write_test_name);
1375
1376 if (eraseblocks_invocations)
1377 printf("Eraseblocks number of invocations for %s is CORRECT\n",
1378 current_test_case->write_test_name);
1379 else
1380 printf("Eraseblocks number of invocations for %s is WRONG, expected %d actual %d\n",
1381 current_test_case->write_test_name,
1382 current_test_case->write_eraseblocks_expected_ind,
1383 g_state.eraseblocks_actual_ind);
1384
1385 if (chip_verified)
1386 printf("Written chip memory state for %s was verified successfully\n",
1387 current_test_case->write_test_name);
1388 else
1389 printf("Written chip memory state for %s was NOT verified completely\n",
1390 current_test_case->write_test_name);
1391
1392 all_write_test_result |= ret;
1393 all_write_test_result |= !chip_written;
1394 all_write_test_result |= !eraseblocks_in_order;
1395 all_write_test_result |= !eraseblocks_invocations;
1396 all_write_test_result |= !chip_verified;
1397
1398 teardown_chip(&layout);
1399
1400 assert_int_equal(0, all_write_test_result);
1401 }
1402
1403 static void get_protected_region(const struct flashctx *flash, unsigned int addr, struct flash_region *region)
1404 {
1405 if (addr < 20)
1406 printf("Inside test get_protected_region for addr=0x%x\n", addr);
1407
1408 if (addr < START_PROTECTED_REGION) {
1409 region->name = strdup("not protected");
1410 region->start = 0;
1411 region->end = START_PROTECTED_REGION - 1;
1412 region->read_prot = false;
1413 region->write_prot = false;
1414 } else if (addr <= END_PROTECTED_REGION) {
1415 region->name = strdup("protected");
1416 region->start = START_PROTECTED_REGION;
1417 region->end = END_PROTECTED_REGION;
1418 region->read_prot = false;
1419 region->write_prot = true;
1420 } else {
1421 region->name = strdup("tail");
1422 region->start = END_PROTECTED_REGION + 1;
1423 region->end = flashrom_flash_getsize(flash) - 1;
1424 region->read_prot = false;
1425 region->write_prot = false;
1426 }
1427 }
1428
1429 static int block_erase_chip_with_protected_region(struct flashctx *flash, enum block_erase_func erase_func, unsigned int blockaddr, unsigned int blocklen)
1430 {
1431 if (blockaddr + blocklen <= MOCK_CHIP_SIZE) {
1432 LOG_ERASE_FUNC;
1433
1434 /* Register eraseblock invocation. */
1435 g_state.eraseblocks_actual[g_state.eraseblocks_actual_ind] = (struct erase_invoke){
1436 .erase_func = erase_func,
1437 .blocklen = blocklen,
1438 .blockaddr = blockaddr,
1439 };
1440 g_state.eraseblocks_actual_ind++;
1441 }
1442
1443 assert_in_range(blockaddr + blocklen, 0, MIN_REAL_CHIP_SIZE);
1444
1445 // Check we are not trying to erase protected region. This should not happen,
1446 // because the logic should handle protected regions and never invoke erasefn
1447 // for them. If this happens, means there is a bug in erasure logic, and test fails.
1448 //
1449 // Note: returning 1 instead of assert, so that the flow goes back to erasure code
1450 // to clean up the memory after failed erase. Memory leaks are also tested by unit tests.
1451 const unsigned int erase_op_size = 1 << (erase_func - TEST_ERASE_INJECTOR_1);
1452 if (blocklen < erase_op_size) {
1453 printf("Error: block length %d is smaller than erase_func length %d\n", blocklen, erase_op_size);
1454 return 1;
1455 }
1456
1457 if ((blockaddr >= START_PROTECTED_REGION && blockaddr <= END_PROTECTED_REGION)
1458 || (blockaddr + blocklen - 1 >= START_PROTECTED_REGION
1459 && blockaddr + blocklen - 1 <= END_PROTECTED_REGION)
1460 || (blockaddr < START_PROTECTED_REGION
1461 && blockaddr + blocklen + 1 > END_PROTECTED_REGION)) {
1462 printf("Error: block with start=%d, len=%d overlaps protected region %d-%d\n",
1463 blockaddr, blocklen, START_PROTECTED_REGION, END_PROTECTED_REGION);
1464 return 1;
1465 }
1466
1467 memset(&g_state.buf[blockaddr], ERASE_VALUE, blocklen);
1468
1469 /* Track the bytes were erased */
1470 memset(&g_state.was_modified[blockaddr], true, blocklen);
1471 /* Clear the records of previous verification, if there were any */
1472 memset(&g_state.was_verified[blockaddr], false, blocklen);
1473
1474 return 0;
1475 }
1476
1477 #define BLOCK_ERASE_PROTECTED_FUNC(i) static int block_erase_chip_with_protected_region_ ## i \
1478 (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen) { \
1479 return block_erase_chip_with_protected_region(flash, TEST_ERASE_INJECTOR_ ## i, blockaddr, blocklen); \
1480 }
1481 BLOCK_ERASE_PROTECTED_FUNC(1)
1482 BLOCK_ERASE_PROTECTED_FUNC(2)
1483 BLOCK_ERASE_PROTECTED_FUNC(3)
1484 BLOCK_ERASE_PROTECTED_FUNC(4)
1485 BLOCK_ERASE_PROTECTED_FUNC(5)
1486
1487 /*
1488 * Runs the test cases that use protected flash regions (regions returned from
1489 * get_flash_region() where write_prot is true) when the runtime flag to avoid
1490 * writing to those regions is enabled.
1491 *
1492 * These tests verify that no protected region is erased, and that the erase
1493 * commands used match the expected erase size (ensuring for example that a
1494 * command erasing 16 bytes is not used when only 8 should be erased).
1495 */
1496 static void erase_unwritable_regions_skipflag_on_test_success(void **state)
1497 {
1498 struct test_case* current_test_case = *state;
1499
1500 int all_erase_tests_result = 0;
1501 struct flashrom_flashctx flashctx = { 0 };
1502 const char *param = ""; /* Default values for all params. */
1503
1504 struct flashrom_layout *layout;
1505
1506 const chipoff_t verify_end_boundary = setup_chip(&flashctx, &layout, param, current_test_case);
1507
1508 // This test needs special block erase to emulate protected regions.
1509 memcpy(g_test_erase_injector,
1510 (erasefunc_t *const[]){
1511 block_erase_chip_with_protected_region_1,
1512 block_erase_chip_with_protected_region_2,
1513 block_erase_chip_with_protected_region_3,
1514 block_erase_chip_with_protected_region_4,
1515 block_erase_chip_with_protected_region_5,
1516 },
1517 sizeof(g_test_erase_injector)
1518 );
1519
1520 flashrom_flag_set(&flashctx, FLASHROM_FLAG_SKIP_UNWRITABLE_REGIONS, true);
1521
1522 // We use dummyflasher programmer in tests, but for this test we need to
1523 // replace dummyflasher's default get_region fn with test one.
1524 // The rest of master struct is fine for this test.
1525 // Note dummyflasher registers multiple masters by default, so replace
1526 // get_region for each of them.
1527 flashctx.mst->spi.get_region = &get_protected_region;
1528 flashctx.mst->opaque.get_region = &get_protected_region;
1529
1530 printf("%s started.\n", current_test_case->erase_test_name);
1531 int ret = flashrom_flash_erase(&flashctx);
1532 printf("%s returned %d.\n", current_test_case->erase_test_name, ret);
1533
1534 int chip_erased = !memcmp(g_state.buf, current_test_case->erased_buf, MOCK_CHIP_SIZE);
1535
1536 int eraseblocks_in_order = !memcmp(g_state.eraseblocks_actual,
1537 current_test_case->eraseblocks_expected,
1538 current_test_case->eraseblocks_expected_ind * sizeof(struct erase_invoke));
1539
1540 int eraseblocks_invocations = (g_state.eraseblocks_actual_ind ==
1541 current_test_case->eraseblocks_expected_ind);
1542
1543 int chip_verified = 1;
1544 for (unsigned int i = 0; i <= verify_end_boundary; i++)
1545 if (g_state.was_modified[i] && !g_state.was_verified[i]) {
1546 chip_verified = 0; /* byte was modified, but not verified after */
1547 printf("Error: byte 0x%x, modified: %d, verified: %d\n", i, g_state.was_modified[i], g_state.was_verified[i]);
1548 }
1549
1550 if (chip_erased)
1551 printf("Erased chip memory state for %s is CORRECT\n",
1552 current_test_case->erase_test_name);
1553 else
1554 printf("Erased chip memory state for %s is WRONG\n",
1555 current_test_case->erase_test_name);
1556
1557 if (eraseblocks_in_order)
1558 printf("Eraseblocks order of invocation for %s is CORRECT\n",
1559 current_test_case->erase_test_name);
1560 else
1561 printf("Eraseblocks order of invocation for %s is WRONG\n",
1562 current_test_case->erase_test_name);
1563
1564 if (eraseblocks_invocations)
1565 printf("Eraseblocks number of invocations for %s is CORRECT\n",
1566 current_test_case->erase_test_name);
1567 else
1568 printf("Eraseblocks number of invocations for %s is WRONG, expected %d actual %d\n",
1569 current_test_case->erase_test_name,
1570 current_test_case->eraseblocks_expected_ind,
1571 g_state.eraseblocks_actual_ind);
1572
1573 if (chip_verified)
1574 printf("Erased chip memory state for %s was verified successfully\n",
1575 current_test_case->erase_test_name);
1576 else
1577 printf("Erased chip memory state for %s was NOT verified completely\n",
1578 current_test_case->erase_test_name);
1579
1580 all_erase_tests_result |= ret;
1581 all_erase_tests_result |= !chip_erased;
1582 all_erase_tests_result |= !eraseblocks_in_order;
1583 all_erase_tests_result |= !eraseblocks_invocations;
1584 all_erase_tests_result |= !chip_verified;
1585
1586 teardown_chip(&layout);
1587
1588 assert_int_equal(0, all_erase_tests_result);
1589 }
1590
1591 /*
1592 * Runs the test cases that use protected flash regions (regions returned from
1593 * get_flash_region() where write_prot is true) when the runtime flag to avoid
1594 * writing to those regions is enabled.
1595 *
1596 * These tests verify that no protected region is written, i.e. protected region
1597 * memory state stays untouched.
1598 */
1599 static void write_unwritable_regions_skipflag_on_test_success(void **state) {
1600 struct test_case* current_test_case = *state;
1601
1602 int all_write_tests_result = 0;
1603 struct flashrom_flashctx flashctx = { 0 };
1604 uint8_t newcontents[MIN_BUF_SIZE];
1605 uint8_t newcontents_protected[MIN_BUF_SIZE];
1606 const char *param = ""; /* Default values for all params. */
1607
1608 struct flashrom_layout *layout;
1609
1610 const chipoff_t verify_end_boundary = setup_chip(&flashctx, &layout, param, current_test_case);
1611 memcpy(&newcontents, current_test_case->written_buf, MOCK_CHIP_SIZE);
1612
1613 // This test needs special block erase to emulate protected regions.
1614 memcpy(g_test_erase_injector,
1615 (erasefunc_t *const[]){
1616 block_erase_chip_with_protected_region_1,
1617 block_erase_chip_with_protected_region_2,
1618 block_erase_chip_with_protected_region_3,
1619 block_erase_chip_with_protected_region_4,
1620 block_erase_chip_with_protected_region_5,
1621 },
1622 sizeof(g_test_erase_injector)
1623 );
1624
1625 flashrom_flag_set(&flashctx, FLASHROM_FLAG_SKIP_UNWRITABLE_REGIONS, true);
1626 flashrom_flag_set(&flashctx, FLASHROM_FLAG_SKIP_UNREADABLE_REGIONS, true);
1627 flashrom_flag_set(&flashctx, FLASHROM_FLAG_VERIFY_WHOLE_CHIP, false);
1628 /* We need to manually trigger verify op after write, because of protected regions */
1629 flashrom_flag_set(&flashctx, FLASHROM_FLAG_VERIFY_AFTER_WRITE, false);
1630
1631 // We use dummyflasher programmer in tests, but for this test we need to
1632 // replace dummyflasher's default get_region fn with test one.
1633 // The rest of master struct is fine for this test.
1634 // Note dummyflasher registers multiple masters by default, so replace
1635 // get_region for each of them.
1636 flashctx.mst->spi.get_region = &get_protected_region;
1637 flashctx.mst->opaque.get_region = &get_protected_region;
1638
1639 printf("%s started.\n", current_test_case->write_test_name);
1640 int ret = flashrom_image_write(&flashctx, &newcontents, MIN_BUF_SIZE, NULL);
1641 printf("%s returned %d.\n", current_test_case->write_test_name, ret);
1642
1643 /* Expected end result leaves protected region untouched */
1644 memcpy(&newcontents_protected, current_test_case->written_protected_buf, MOCK_CHIP_SIZE);
1645 /* Outside of MOCK_CHIP_SIZE newcontents are not initialised in test cases, so just copy */
1646 memcpy(&newcontents_protected[MOCK_CHIP_SIZE],
1647 &newcontents[MOCK_CHIP_SIZE],
1648 MIN_BUF_SIZE - MOCK_CHIP_SIZE);
1649 printf("%s verification started.\n", current_test_case->write_test_name);
1650 ret = flashrom_image_verify(&flashctx, &newcontents_protected, MIN_BUF_SIZE);
1651 printf("%s verification returned %d.\n", current_test_case->write_test_name, ret);
1652
1653 int chip_written = !memcmp(g_state.buf, current_test_case->written_protected_buf, MOCK_CHIP_SIZE);
1654
1655 int chip_verified = 1;
1656 for (unsigned int i = 0; i <= verify_end_boundary; i++)
1657 if (g_state.was_modified[i] && !g_state.was_verified[i]) {
1658 chip_verified = 0; /* byte was modified, but not verified after */
1659 printf("Error: byte 0x%x, modified: %d, verified: %d\n", i, g_state.was_modified[i], g_state.was_verified[i]);
1660 }
1661
1662 if (chip_written)
1663 printf("Written chip memory state for %s is CORRECT\n",
1664 current_test_case->write_test_name);
1665 else
1666 printf("Written chip memory state for %s is WRONG\n",
1667 current_test_case->write_test_name);
1668
1669 if (chip_verified)
1670 printf("Written chip memory state for %s was verified successfully\n",
1671 current_test_case->write_test_name);
1672 else
1673 printf("Written chip memory state for %s was NOT verified completely\n",
1674 current_test_case->write_test_name);
1675
1676 all_write_tests_result |= ret;
1677 all_write_tests_result |= !chip_written;
1678 all_write_tests_result |= !chip_verified;
1679
1680 teardown_chip(&layout);
1681
1682 assert_int_equal(0, all_write_tests_result);
1683 }
1684
1685 static void test_erase_fails_for_unwritable_region(void **state) {
1686 struct flashrom_flashctx flashctx = {
1687 .chip = &chip_1_2_4_8_16,
1688 };
1689 assert_int_equal(0, programmer_init(&programmer_dummy, ""));
1690 flashctx.mst = &registered_masters[0];
1691
1692 flashctx.mst->spi.get_region = &get_protected_region;
1693 flashctx.mst->opaque.get_region = &get_protected_region;
1694 flashrom_flag_set(&flashctx, FLASHROM_FLAG_SKIP_UNWRITABLE_REGIONS, false);
1695
1696 /* Ask to erase one byte at the end of the unprotected region and one byte
1697 * at the beginning of the protected one. If the check for unwritable regions
1698 * wrongly treats the upper bound as exclusive, it will incorrectly try
1699 * to erase inside the protected region. */
1700 struct flashrom_layout *layout;
1701 flashrom_layout_new(&layout);
1702 flashrom_layout_add_region(layout, 7, 8, "protected");
1703 flashrom_layout_include_region(layout, "protected");
1704 flashrom_layout_set(&flashctx, layout);
1705
1706 int ret = flashrom_flash_erase(&flashctx);
1707
1708 assert_int_equal(0, programmer_shutdown());
1709 flashrom_layout_release(layout);
1710
1711 assert_int_not_equal(ret, 0);
1712 }
1713
1714 static void test_write_with_sacrifice_ratio50(void **state) {
1715 struct test_case* current_test_case = &test_cases[20];
1716
1717 int all_write_test_result = 0;
1718 struct flashrom_flashctx flashctx = {
1719 /* If eraseblocks of smaller size fill in more than a half of the area,
1720 * erase one larger size block instead. */
1721 .sacrifice_ratio = 50,
1722 };
1723 /* Custom expectations because sacrifice ratio is modified (not default). */
1724 struct erase_invoke eraseblocks_expected = {0x0, 0x10, TEST_ERASE_INJECTOR_5};
1725 unsigned int eraseblocks_expected_ind = 1;
1726
1727 uint8_t newcontents[MIN_BUF_SIZE];
1728 const char *param = ""; /* Default values for all params. */
1729
1730 struct flashrom_layout *layout;
1731
1732 const chipoff_t verify_end_boundary = setup_chip(&flashctx, &layout, param, current_test_case);
1733 memcpy(&newcontents, current_test_case->written_buf, MOCK_CHIP_SIZE);
1734
1735 printf("%s started.\n", __func__);
1736 int ret = flashrom_image_write(&flashctx, &newcontents, MIN_BUF_SIZE, NULL);
1737 printf("%s returned %d.\n", __func__, ret);
1738
1739 int chip_written = !memcmp(g_state.buf, current_test_case->written_buf, MOCK_CHIP_SIZE);
1740
1741 int eraseblocks_in_order = !memcmp(g_state.eraseblocks_actual, &eraseblocks_expected,
1742 eraseblocks_expected_ind * sizeof(struct erase_invoke));
1743
1744 int eraseblocks_invocations = (g_state.eraseblocks_actual_ind == eraseblocks_expected_ind);
1745
1746 int chip_verified = 1;
1747 for (unsigned int i = 0; i <= verify_end_boundary; i++)
1748 if (g_state.was_modified[i] && !g_state.was_verified[i]) {
1749 chip_verified = 0; /* the byte was modified, but not verified after */
1750 printf("Error: byte 0x%x, modified: %d, verified: %d\n", i, g_state.was_modified[i], g_state.was_verified[i]);
1751 }
1752
1753 if (chip_written)
1754 printf("Written chip memory state for %s is CORRECT\n", __func__);
1755 else
1756 printf("Written chip memory state for %s is WRONG\n", __func__);
1757
1758 if (eraseblocks_in_order)
1759 printf("Eraseblocks order of invocation for %s is CORRECT\n", __func__);
1760 else
1761 printf("Eraseblocks order of invocation for %s is WRONG\n", __func__);
1762
1763 if (eraseblocks_invocations)
1764 printf("Eraseblocks number of invocations for %s is CORRECT\n", __func__);
1765 else
1766 printf("Eraseblocks number of invocations for %s is WRONG, expected %d actual %d\n",
1767 __func__,
1768 eraseblocks_expected_ind,
1769 g_state.eraseblocks_actual_ind);
1770
1771 if (chip_verified)
1772 printf("Written chip memory state for %s was verified successfully\n", __func__);
1773 else
1774 printf("Written chip memory state for %s was NOT verified completely\n", __func__);
1775
1776 all_write_test_result |= ret;
1777 all_write_test_result |= !chip_written;
1778 all_write_test_result |= !eraseblocks_in_order;
1779 all_write_test_result |= !eraseblocks_invocations;
1780 all_write_test_result |= !chip_verified;
1781
1782 teardown_chip(&layout);
1783
1784 assert_int_equal(0, all_write_test_result);
1785 }