Debian Codegraph @

tests/chip.c

raw
1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2021 Google LLC
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This file contains tests for operations on flash chip.
16 *
17 * Two flash chip test variants are used:
18 *
19 * 1) Mock chip state backed by `g_chip_state`.
20 * Example of test: erase_chip_test_success.
21 *
22 * 2) Mock chip operations backed by `dummyflasher` emulation.
23 * Dummyflasher controls chip state and emulates read/write/erase.
24 * `g_chip_state` is NOT used for this type of tests.
25 * Example of test: erase_chip_with_dummyflasher_test_success.
26 */
27
28 #include <include/test.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "tests.h"
34 #include "chipdrivers.h"
35 #include "flash.h"
36 #include "io_mock.h"
37 #include "libflashrom.h"
38 #include "programmer.h"
39
40 #define MOCK_CHIP_SIZE (8*MiB)
41 #define MOCK_CHIP_CONTENT 0xCC /* 0x00 is a zeroed heap and 0xFF is an erased chip. */
42
43 static struct {
44 uint8_t buf[MOCK_CHIP_SIZE]; /* buffer of total size of chip, to emulate a chip */
45 } g_chip_state = {
46 .buf = { 0 },
47 };
48
49 struct progress_user_data {
50 /* % of progress last reported for each operation, to be asserted in the progress callback. */
51 size_t last_seen[FLASHROM_PROGRESS_NR];
52 };
53
54 static int read_chip(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
55 {
56 printf("Read chip called with start=0x%x, len=0x%x\n", start, len);
57
58 assert_in_range(start + len, 0, MOCK_CHIP_SIZE);
59
60 memcpy(buf, &g_chip_state.buf[start], len);
61 return 0;
62 }
63
64 static int write_chip(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
65 {
66 printf("Write chip called with start=0x%x, len=0x%x\n", start, len);
67
68 assert_in_range(start + len, 0, MOCK_CHIP_SIZE);
69
70 memcpy(&g_chip_state.buf[start], buf, len);
71 return 0;
72 }
73
74 static int block_erase_chip(struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen)
75 {
76 printf("Block erase called with blockaddr=0x%x, blocklen=0x%x\n", blockaddr, blocklen);
77
78 assert_in_range(blockaddr + blocklen, 0, MOCK_CHIP_SIZE);
79
80 memset(&g_chip_state.buf[blockaddr], 0xff, blocklen);
81 return 0;
82 }
83
84 static void progress_callback(enum flashrom_progress_stage stage, size_t current, size_t total, void* user_data) {
85 struct progress_user_data *progress_user_data = user_data;
86
87 if (current == 0) {
88 printf("Progress started for stage %d, initial callback call\n", stage);
89 } else {
90 /* Progress cannot go backwards. */
91 assert_true(current >= progress_user_data->last_seen[stage]);
92 }
93
94 if (current >= total - 1)
95 printf("Progress almost complete for stage %d, current %zu, total %zu\n", stage, current, total);
96
97 progress_user_data->last_seen[stage] = current;
98 }
99
100 static void setup_chip(struct flashrom_flashctx *flashctx, struct flashrom_layout **layout,
101 struct flashchip *chip, const char *programmer_param, const struct io_mock *io)
102 {
103 io_mock_register(io);
104
105 flashctx->chip = chip;
106
107 memset(g_chip_state.buf, MOCK_CHIP_CONTENT, sizeof(g_chip_state.buf));
108
109 printf("Creating layout with one included region... ");
110 assert_int_equal(0, flashrom_layout_new(layout));
111 /* One region which covers total size of chip. */
112 assert_int_equal(0, flashrom_layout_add_region(*layout, 0, chip->total_size * KiB - 1, "region"));
113 assert_int_equal(0, flashrom_layout_include_region(*layout, "region"));
114
115 flashrom_layout_set(flashctx, *layout);
116 printf("done\n");
117
118 /*
119 * We need some programmer (any), and dummy is a very good one,
120 * because it doesn't need any mocking. So no extra complexity
121 * from a programmer side, and test can focus on working with the chip.
122 */
123 printf("Dummyflasher initialising with param=\"%s\"... ", programmer_param);
124 assert_int_equal(0, programmer_init(&programmer_dummy, programmer_param));
125 /* Assignment below normally happens while probing, but this test is not probing. */
126 flashctx->mst = &registered_masters[0];
127 printf("done\n");
128 }
129
130 static void teardown(struct flashrom_layout **layout)
131 {
132 printf("Dummyflasher shutdown... ");
133 assert_int_equal(0, programmer_shutdown());
134 printf("done\n");
135
136 printf("Releasing layout... ");
137 flashrom_layout_release(*layout);
138 printf("done\n");
139
140 io_mock_register(NULL);
141 }
142
143 static const struct flashchip chip_8MiB = {
144 .vendor = "aklm",
145 .total_size = MOCK_CHIP_SIZE / KiB,
146 .tested = TEST_OK_PREW,
147 .read = TEST_READ_INJECTOR,
148 .write = TEST_WRITE_INJECTOR,
149 .page_size = 256,
150 .block_erasers =
151 {{
152 /* All blocks within total size of the chip. */
153 .eraseblocks = { {2 * MiB, 4} },
154 .block_erase = TEST_ERASE_INJECTOR_1,
155 }},
156 };
157
158 /* Chip expected to be processed with dummyflasher, so using real op functions. */
159 static const struct flashchip chip_no_erase = {
160 .vendor = "aklm&dummyflasher",
161 .total_size = 16 * 1024,
162 .tested = TEST_OK_PREW,
163 .read = SPI_CHIP_READ,
164 .write = SPI_CHIP_WRITE256,
165 .page_size = 256,
166 .feature_bits = FEATURE_NO_ERASE | FEATURE_ERASED_ZERO,
167 .block_erasers =
168 {
169 {
170 .eraseblocks = { {16 * 1024 * 1024, 1} },
171 /* Special erase fn for chips without erase capability. */
172 .block_erase = SPI_BLOCK_ERASE_EMULATION,
173 }
174 },
175 };
176
177 /* Setup the struct for W25Q128.V, all values come from flashchips.c */
178 static const struct flashchip chip_W25Q128_V = {
179 .vendor = "aklm&dummyflasher",
180 .total_size = 16 * 1024,
181 .tested = TEST_OK_PREW,
182 .read = SPI_CHIP_READ,
183 .write = SPI_CHIP_WRITE256,
184 .page_size = 256,
185 .block_erasers =
186 {
187 {
188 .eraseblocks = { {4 * 1024, 4096} },
189 .block_erase = SPI_BLOCK_ERASE_20,
190 }, {
191 .eraseblocks = { {32 * 1024, 512} },
192 .block_erase = SPI_BLOCK_ERASE_52,
193 }, {
194 .eraseblocks = { {64 * 1024, 256} },
195 .block_erase = SPI_BLOCK_ERASE_D8,
196 }, {
197 .eraseblocks = { {16 * 1024 * 1024, 1} },
198 .block_erase = SPI_BLOCK_ERASE_60,
199 }, {
200 .eraseblocks = { {16 * 1024 * 1024, 1} },
201 .block_erase = SPI_BLOCK_ERASE_C7,
202 }
203 },
204 };
205
206 void erase_chip_test_success(void **state)
207 {
208 (void) state; /* unused */
209
210 static struct io_mock_fallback_open_state data = {
211 .noc = 0,
212 .paths = { NULL },
213 };
214 const struct io_mock chip_io = {
215 .fallback_open_state = &data,
216 };
217
218 g_test_write_injector = write_chip;
219 g_test_read_injector = read_chip;
220 g_test_erase_injector[0] = block_erase_chip;
221 struct flashrom_flashctx flashctx = { 0 };
222 struct flashrom_layout *layout;
223 struct flashchip mock_chip = chip_8MiB;
224 const char *param = ""; /* Default values for all params. */
225
226 setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
227
228 printf("Erase chip operation started.\n");
229 assert_int_equal(0, flashrom_flash_erase(&flashctx));
230 printf("Erase chip operation done.\n");
231
232 teardown(&layout);
233 }
234
235 void erase_chip_with_progress(void **state)
236 {
237 (void) state; /* unused */
238
239 static struct io_mock_fallback_open_state data = {
240 .noc = 0,
241 .paths = { NULL },
242 };
243 const struct io_mock chip_io = {
244 .fallback_open_state = &data,
245 };
246
247 g_test_write_injector = write_chip;
248 g_test_read_injector = read_chip;
249 g_test_erase_injector[0] = block_erase_chip;
250 struct flashrom_flashctx flashctx = { 0 };
251 struct flashrom_layout *layout;
252 struct flashchip mock_chip = chip_8MiB;
253 const char *param = ""; /* Default values for all params. */
254
255 setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
256
257 struct progress_user_data progress_user_data = {0};
258 flashrom_set_progress_callback_v2(&flashctx, progress_callback, &progress_user_data);
259
260 printf("Erase chip operation started.\n");
261 assert_int_equal(0, flashrom_flash_erase(&flashctx));
262 printf("Erase chip operation done.\n");
263
264 teardown(&layout);
265 }
266
267 void erase_chip_with_dummyflasher_test_success(void **state)
268 {
269 (void) state; /* unused */
270
271 static struct io_mock_fallback_open_state data = {
272 .noc = 0,
273 .paths = { NULL },
274 };
275 const struct io_mock chip_io = {
276 .fallback_open_state = &data,
277 };
278
279 struct flashrom_flashctx flashctx = { 0 };
280 struct flashrom_layout *layout;
281 struct flashchip mock_chip = chip_W25Q128_V;
282 /*
283 * Dummyflasher is capable to emulate W25Q128.V, so we ask it to do this.
284 * Nothing to mock, dummy is taking care of this already.
285 */
286 const char *param_dup = "bus=spi,emulate=W25Q128FV";
287
288 setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
289
290 printf("Erase chip operation started.\n");
291 assert_int_equal(0, flashrom_flash_erase(&flashctx));
292 printf("Erase chip operation done.\n");
293
294 teardown(&layout);
295 }
296
297 void read_chip_test_success(void **state)
298 {
299 (void) state; /* unused */
300
301 static struct io_mock_fallback_open_state data = {
302 .noc = 0,
303 .paths = { NULL },
304 };
305 const struct io_mock chip_io = {
306 .fallback_open_state = &data,
307 };
308
309 g_test_write_injector = write_chip;
310 g_test_read_injector = read_chip;
311 g_test_erase_injector[0] = block_erase_chip;
312 struct flashrom_flashctx flashctx = { 0 };
313 struct flashrom_layout *layout;
314 struct flashchip mock_chip = chip_8MiB;
315 const char *param = ""; /* Default values for all params. */
316
317 setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
318
319 const char *const filename = "read_chip.test";
320 unsigned long size = mock_chip.total_size * 1024;
321 unsigned char *buf = calloc(size, sizeof(unsigned char));
322 assert_non_null(buf);
323
324 printf("Read chip operation started.\n");
325 assert_int_equal(0, flashrom_image_read(&flashctx, buf, size));
326 assert_int_equal(0, write_buf_to_file(buf, size, filename));
327 printf("Read chip operation done.\n");
328
329 teardown(&layout);
330
331 free(buf);
332 }
333
334 void read_chip_with_progress(void **state)
335 {
336 (void) state; /* unused */
337
338 static struct io_mock_fallback_open_state data = {
339 .noc = 0,
340 .paths = { NULL },
341 };
342 const struct io_mock chip_io = {
343 .fallback_open_state = &data,
344 };
345
346 g_test_write_injector = write_chip;
347 g_test_read_injector = read_chip;
348 g_test_erase_injector[0] = block_erase_chip;
349 struct flashrom_flashctx flashctx = { 0 };
350 struct flashrom_layout *layout;
351 struct flashchip mock_chip = chip_8MiB;
352 const char *param = ""; /* Default values for all params. */
353
354 setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
355
356 struct progress_user_data progress_user_data = {0};
357 flashrom_set_progress_callback_v2(&flashctx, progress_callback, &progress_user_data);
358
359 const char *const filename = "read_chip.test";
360 unsigned long size = mock_chip.total_size * 1024;
361 unsigned char *buf = calloc(size, sizeof(unsigned char));
362 assert_non_null(buf);
363
364 printf("Read chip operation started.\n");
365 assert_int_equal(0, flashrom_image_read(&flashctx, buf, size));
366 assert_int_equal(0, write_buf_to_file(buf, size, filename));
367 printf("Read chip operation done.\n");
368
369 teardown(&layout);
370
371 free(buf);
372 }
373
374 void read_chip_with_dummyflasher_test_success(void **state)
375 {
376 (void) state; /* unused */
377
378 static struct io_mock_fallback_open_state data = {
379 .noc = 0,
380 .paths = { NULL },
381 };
382 const struct io_mock chip_io = {
383 .fallback_open_state = &data,
384 };
385
386 struct flashrom_flashctx flashctx = { 0 };
387 struct flashrom_layout *layout;
388 struct flashchip mock_chip = chip_W25Q128_V;
389 /*
390 * Dummyflasher is capable to emulate W25Q128.V, so we ask it to do this.
391 * Nothing to mock, dummy is taking care of this already.
392 */
393 const char *param_dup = "bus=spi,emulate=W25Q128FV";
394
395 setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
396
397 const char *const filename = "read_chip.test";
398 unsigned long size = mock_chip.total_size * 1024;
399 unsigned char *buf = calloc(size, sizeof(unsigned char));
400 assert_non_null(buf);
401
402 printf("Read chip operation started.\n");
403 assert_int_equal(0, flashrom_image_read(&flashctx, buf, size));
404 assert_int_equal(0, write_buf_to_file(buf, size, filename));
405 printf("Read chip operation done.\n");
406
407 teardown(&layout);
408
409 free(buf);
410 }
411
412 void write_chip_test_success(void **state)
413 {
414 (void) state; /* unused */
415
416 static struct io_mock_fallback_open_state data = {
417 .noc = 0,
418 .paths = { NULL },
419 };
420 const struct io_mock chip_io = {
421 .fallback_open_state = &data,
422 };
423
424 g_test_write_injector = write_chip;
425 g_test_read_injector = read_chip;
426 g_test_erase_injector[0] = block_erase_chip;
427 struct flashrom_flashctx flashctx = { 0 };
428 struct flashrom_layout *layout;
429 struct flashchip mock_chip = chip_8MiB;
430 const char *param = ""; /* Default values for all params. */
431
432 setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
433
434 /*
435 * Providing filename "-" means content is taken from standard input.
436 * This doesn't change much because all file operations are mocked.
437 * However filename "-" makes a difference for
438 * flashrom.c#read_buf_from_file and allows to avoid mocking
439 * image_stat.st_size.
440 *
441 * Now this does mean test covers successful path only, but this test
442 * is designed to cover only successful write operation anyway.
443 *
444 * To cover error path of image_stat.st_size != flash size, filename
445 * needs to be provided and image_stat.st_size needs to be mocked.
446 */
447 const char *const filename = "-";
448 unsigned long size = mock_chip.total_size * 1024;
449 uint8_t *const newcontents = malloc(size);
450 assert_non_null(newcontents);
451
452 printf("Write chip operation started.\n");
453 assert_int_equal(0, read_buf_from_file(newcontents, size, filename));
454 assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, size, NULL));
455 printf("Write chip operation done.\n");
456
457 teardown(&layout);
458
459 free(newcontents);
460 }
461
462 void write_chip_with_progress(void **state)
463 {
464 (void) state; /* unused */
465
466 static struct io_mock_fallback_open_state data = {
467 .noc = 0,
468 .paths = { NULL },
469 };
470 const struct io_mock chip_io = {
471 .fallback_open_state = &data,
472 };
473
474 g_test_write_injector = write_chip;
475 g_test_read_injector = read_chip;
476 g_test_erase_injector[0] = block_erase_chip;
477 struct flashrom_flashctx flashctx = { 0 };
478 struct flashrom_layout *layout;
479 struct flashchip mock_chip = chip_8MiB;
480 const char *param = ""; /* Default values for all params. */
481
482 setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
483
484 struct progress_user_data progress_user_data = {0};
485 flashrom_set_progress_callback_v2(&flashctx, progress_callback, &progress_user_data);
486
487 const char *const filename = "-";
488 unsigned long size = mock_chip.total_size * 1024;
489 uint8_t *const newcontents = malloc(size);
490 assert_non_null(newcontents);
491
492 printf("Write chip operation started.\n");
493 assert_int_equal(0, read_buf_from_file(newcontents, size, filename));
494 assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, size, NULL));
495 printf("Write chip operation done.\n");
496
497 teardown(&layout);
498
499 free(newcontents);
500 }
501
502 void write_chip_with_dummyflasher_test_success(void **state)
503 {
504 (void) state; /* unused */
505
506 static struct io_mock_fallback_open_state data = {
507 .noc = 0,
508 .paths = { NULL },
509 };
510 const struct io_mock chip_io = {
511 .fallback_open_state = &data,
512 };
513
514 struct flashrom_flashctx flashctx = { 0 };
515 struct flashrom_layout *layout;
516 struct flashchip mock_chip = chip_W25Q128_V;
517 /*
518 * Dummyflasher is capable to emulate W25Q128.V, so we ask it to do this.
519 * Nothing to mock, dummy is taking care of this already.
520 */
521 const char *param_dup = "bus=spi,emulate=W25Q128FV";
522
523 setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
524
525 /* See comment in write_chip_test_success */
526 const char *const filename = "-";
527 unsigned long size = mock_chip.total_size * 1024;
528 uint8_t *const newcontents = malloc(size);
529 assert_non_null(newcontents);
530
531 printf("Write chip operation started.\n");
532 assert_int_equal(0, read_buf_from_file(newcontents, size, filename));
533 assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, size, NULL));
534 printf("Write chip operation done.\n");
535
536 teardown(&layout);
537
538 free(newcontents);
539 }
540
541 void write_chip_feature_no_erase(void **state)
542 {
543 (void) state; /* unused */
544
545 static struct io_mock_fallback_open_state data = {
546 .noc = 0,
547 .paths = { NULL },
548 };
549 const struct io_mock chip_io = {
550 .fallback_open_state = &data,
551 };
552
553 struct flashrom_flashctx flashctx = { 0 };
554 struct flashrom_layout *layout;
555
556 /*
557 * Tricking the dummyflasher by asking to emulate W25Q128FV but giving to it
558 * mock chip with FEATURE_NO_ERASE.
559 * As long as chip size is the same, this is fine.
560 */
561 struct flashchip mock_chip = chip_no_erase;
562 const char *param_dup = "bus=spi,emulate=W25Q128FV";
563
564 setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
565
566 /* See comment in write_chip_test_success */
567 const char *const filename = "-";
568 unsigned long size = mock_chip.total_size * 1024;
569 uint8_t *const newcontents = malloc(size);
570 assert_non_null(newcontents);
571
572 printf("Write chip operation started.\n");
573 assert_int_equal(0, read_buf_from_file(newcontents, size, filename));
574 assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, size, NULL));
575 assert_int_equal(0, flashrom_image_verify(&flashctx, newcontents, size));
576 printf("Write chip operation done.\n");
577
578 teardown(&layout);
579
580 free(newcontents);
581 }
582
583 void write_chip_feature_no_erase_with_progress(void **state)
584 {
585 (void) state; /* unused */
586
587 static struct io_mock_fallback_open_state data = {
588 .noc = 0,
589 .paths = { NULL },
590 };
591 const struct io_mock chip_io = {
592 .fallback_open_state = &data,
593 };
594
595 struct flashrom_flashctx flashctx = { 0 };
596 struct flashrom_layout *layout;
597
598 /*
599 * Tricking the dummyflasher by asking to emulate W25Q128FV but giving to it
600 * mock chip with FEATURE_NO_ERASE.
601 * As long as chip size is the same, this is fine.
602 */
603 struct flashchip mock_chip = chip_no_erase;
604 const char *param_dup = "bus=spi,emulate=W25Q128FV";
605
606 setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
607
608 /* See comment in write_chip_test_success */
609 const char *const filename = "-";
610 unsigned long size = mock_chip.total_size * 1024;
611 uint8_t *const newcontents = malloc(size);
612 assert_non_null(newcontents);
613
614 struct progress_user_data progress_user_data = {0};
615 flashrom_set_progress_callback_v2(&flashctx, progress_callback, &progress_user_data);
616
617 printf("Write chip operation started.\n");
618 assert_int_equal(0, read_buf_from_file(newcontents, size, filename));
619 assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, size, NULL));
620 assert_int_equal(0, flashrom_image_verify(&flashctx, newcontents, size));
621 printf("Write chip operation done.\n");
622
623 teardown(&layout);
624
625 free(newcontents);
626 }
627
628
629 void write_nonaligned_region_with_dummyflasher_test_success(void **state)
630 {
631 (void) state; /* unused */
632
633 static struct io_mock_fallback_open_state data = {
634 .noc = 0,
635 .paths = { NULL },
636 };
637 const struct io_mock chip_io = {
638 .fallback_open_state = &data,
639 };
640
641 struct flashrom_flashctx flashctx = { 0 };
642 struct flashrom_layout *layout;
643 struct flashchip mock_chip = chip_W25Q128_V;
644 const uint32_t mock_chip_size = mock_chip.total_size * KiB;
645 /*
646 * Dummyflasher is capable to emulate W25Q128.V, so we ask it to do this.
647 * Nothing to mock, dummy is taking care of this already.
648 */
649 const char *param_dup = "bus=spi,emulate=W25Q128FV";
650
651 /* FIXME: MOCK_CHIP_CONTENT is buggy within setup_chip, it should also
652 * not be either 0x00 or 0xFF as those are specific values related to
653 * either a erased chip or zero'ed heap thus ambigous.
654 */
655 #define MOCK_CHIP_SUBREGION_CONTENTS 0xCC
656 /**
657 * Step 0 - Prepare newcontents as contiguous sample data bytes as follows:
658 * {MOCK_CHIP_SUBREGION_CONTENTS, [..]}.
659 */
660 uint8_t *newcontents = calloc(1, mock_chip_size);
661 assert_non_null(newcontents);
662 memset(newcontents, MOCK_CHIP_SUBREGION_CONTENTS, mock_chip_size);
663
664 setup_chip(&flashctx, &layout, &mock_chip, param_dup, &chip_io);
665 /* Expect to verify only the non-aligned write operation within the region. */
666 flashrom_flag_set(&flashctx, FLASHROM_FLAG_VERIFY_AFTER_WRITE, true);
667 flashrom_flag_set(&flashctx, FLASHROM_FLAG_VERIFY_WHOLE_CHIP, false);
668
669 /**
670 * Prepare mock chip content and release setup_chip() layout for our
671 * custom ones.
672 */
673 assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, mock_chip_size, NULL));
674 flashrom_layout_release(layout);
675
676 /**
677 * Create region smaller than erase granularity of chip.
678 */
679 printf("Creating custom region layout... ");
680 assert_int_equal(0, flashrom_layout_new(&layout));
681 printf("Adding and including region0... ");
682 assert_int_equal(0, flashrom_layout_add_region(layout, 0, (1 * KiB), "region0"));
683 assert_int_equal(0, flashrom_layout_include_region(layout, "region0"));
684 flashrom_layout_set(&flashctx, layout);
685 printf("Subregion layout configuration done.\n");
686
687 /**
688 * Step 1 - Modify newcontents as non-contiguous sample data bytes as follows:
689 * 0xAA 0xAA {MOCK_CHIP_SUBREGION_CONTENTS}, [..]}.
690 */
691 printf("Subregion chip write op..\n");
692 memset(newcontents, 0xAA, 2);
693 assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, mock_chip_size, NULL));
694 printf("Subregion chip write op done.\n");
695
696 /**
697 * FIXME: A 'NULL' layout should indicate a default layout however this
698 * causes a crash for a unknown reason. For now prepare a new default
699 * layout of the entire chip. flashrom_layout_set(&flashctx, NULL); // use default layout.
700 */
701 flashrom_layout_release(layout);
702 assert_int_equal(0, flashrom_layout_new(&layout));
703 assert_int_equal(0, flashrom_layout_add_region(layout, 0, mock_chip_size - 1, "entire"));
704 assert_int_equal(0, flashrom_layout_include_region(layout, "entire"));
705 flashrom_layout_set(&flashctx, layout);
706
707 /**
708 * Expect a verification pass that the previous content within the region, however
709 * outside the region write length, is untouched.
710 */
711 printf("Entire chip verify op..\n");
712 assert_int_equal(0, flashrom_image_verify(&flashctx, newcontents, mock_chip_size));
713 printf("Entire chip verify op done.\n");
714
715 teardown(&layout);
716 free(newcontents);
717 }
718
719 static size_t verify_chip_fread(void *state, void *buf, size_t size, size_t len, FILE *fp)
720 {
721 /*
722 * Verify operation compares contents of the file vs contents on the chip.
723 * To emulate successful verification we emulate file contents to be the
724 * same as what is on the chip.
725 */
726 memset(buf, MOCK_CHIP_CONTENT, len);
727 return len;
728 }
729
730 void verify_chip_test_success(void **state)
731 {
732 (void) state; /* unused */
733
734 static struct io_mock_fallback_open_state data = {
735 .noc = 0,
736 .paths = { NULL },
737 };
738 const struct io_mock verify_chip_io = {
739 .iom_fread = verify_chip_fread,
740 .fallback_open_state = &data,
741 };
742
743 g_test_write_injector = write_chip;
744 g_test_read_injector = read_chip;
745 g_test_erase_injector[0] = block_erase_chip;
746 struct flashrom_flashctx flashctx = { 0 };
747 struct flashrom_layout *layout;
748 struct flashchip mock_chip = chip_8MiB;
749 const char *param = ""; /* Default values for all params. */
750
751 setup_chip(&flashctx, &layout, &mock_chip, param, &verify_chip_io);
752
753 /* See comment in write_chip_test_success */
754 const char *const filename = "-";
755 unsigned long size = mock_chip.total_size * 1024;
756 uint8_t *const newcontents = malloc(size);
757 assert_non_null(newcontents);
758
759 printf("Verify chip operation started.\n");
760 assert_int_equal(0, read_buf_from_file(newcontents, size, filename));
761 assert_int_equal(0, flashrom_image_verify(&flashctx, newcontents, size));
762 printf("Verify chip operation done.\n");
763
764 teardown(&layout);
765
766 free(newcontents);
767 }
768
769 void verify_chip_with_dummyflasher_test_success(void **state)
770 {
771 (void) state; /* unused */
772
773 static struct io_mock_fallback_open_state data = {
774 .noc = 0,
775 .paths = { NULL },
776 };
777 const struct io_mock verify_chip_io = {
778 .iom_fread = verify_chip_fread,
779 .fallback_open_state = &data,
780 };
781
782 struct flashrom_flashctx flashctx = { 0 };
783 struct flashrom_layout *layout;
784 struct flashchip mock_chip = chip_W25Q128_V;
785 /*
786 * Dummyflasher is capable to emulate W25Q128.V, so we ask it to do this.
787 * Nothing to mock, dummy is taking care of this already.
788 */
789 const char *param_dup = "bus=spi,emulate=W25Q128FV";
790
791 setup_chip(&flashctx, &layout, &mock_chip, param_dup, &verify_chip_io);
792
793 /* See comment in write_chip_test_success */
794 const char *const filename = "-";
795 unsigned long size = mock_chip.total_size * 1024;
796 uint8_t *const newcontents = malloc(size);
797 assert_non_null(newcontents);
798
799 /*
800 * Dummyflasher controls chip state and fully emulates reads and writes,
801 * so to set up initial chip state we need to write on chip. Write
802 * operation takes content from file and writes on chip. File content is
803 * emulated in verify_chip_fread mock.
804 */
805
806 printf("Write chip operation started.\n");
807 assert_int_equal(0, read_buf_from_file(newcontents, size, filename));
808 assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, size, NULL));
809 printf("Write chip operation done.\n");
810
811 printf("Verify chip operation started.\n");
812 assert_int_equal(0, flashrom_image_verify(&flashctx, newcontents, size));
813 printf("Verify chip operation done.\n");
814
815 teardown(&layout);
816
817 free(newcontents);
818 }