1 /*
2  * Copyright (c) 2017 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/storage/flash_map.h>
9 #include <bootutil/bootutil_public.h>
10 #include <zephyr/dfu/mcuboot.h>
11 
12 #define SLOT0_PARTITION		slot0_partition
13 #define SLOT1_PARTITION		slot1_partition
14 
15 #define SLOT0_PARTITION_ID	FIXED_PARTITION_ID(SLOT0_PARTITION)
16 #define SLOT1_PARTITION_ID	FIXED_PARTITION_ID(SLOT1_PARTITION)
17 
18 #define BOOT_MAGIC_VAL_W0 0xf395c277
19 #define BOOT_MAGIC_VAL_W1 0x7fefd260
20 #define BOOT_MAGIC_VAL_W2 0x0f505235
21 #define BOOT_MAGIC_VAL_W3 0x8079b62c
22 #define BOOT_MAGIC_VALUES {BOOT_MAGIC_VAL_W0, BOOT_MAGIC_VAL_W1,\
23 			   BOOT_MAGIC_VAL_W2, BOOT_MAGIC_VAL_W3 }
24 
ZTEST(mcuboot_interface,test_bank_erase)25 ZTEST(mcuboot_interface, test_bank_erase)
26 {
27 	const struct flash_area *fa;
28 	uint32_t temp;
29 	uint32_t temp2 = 0x5a5a5a5a;
30 	off_t offs;
31 	int ret;
32 
33 	ret = flash_area_open(SLOT1_PARTITION_ID, &fa);
34 	if (ret) {
35 		printf("Flash driver was not found!\n");
36 		return;
37 	}
38 
39 	for (offs = 0; offs < fa->fa_size; offs += sizeof(temp)) {
40 		ret = flash_area_read(fa, offs, &temp, sizeof(temp));
41 		zassert_true(ret == 0, "Reading from flash");
42 		if (temp == 0xFFFFFFFF) {
43 			ret = flash_area_write(fa, offs, &temp2, sizeof(temp));
44 			zassert_true(ret == 0, "Writing to flash");
45 		}
46 	}
47 
48 	zassert(boot_erase_img_bank(SLOT1_PARTITION_ID) == 0,
49 		"pass", "fail");
50 
51 	for (offs = 0; offs < fa->fa_size; offs += sizeof(temp)) {
52 		ret = flash_area_read(fa, offs, &temp, sizeof(temp));
53 		zassert_true(ret == 0, "Reading from flash");
54 		zassert(temp == 0xFFFFFFFF, "pass", "fail");
55 	}
56 }
57 
ZTEST(mcuboot_interface,test_request_upgrade)58 ZTEST(mcuboot_interface, test_request_upgrade)
59 {
60 	const struct flash_area *fa;
61 	const uint32_t expectation[6] = {
62 		0xffffffff,
63 		0xffffffff,
64 		BOOT_MAGIC_VAL_W0,
65 		BOOT_MAGIC_VAL_W1,
66 		BOOT_MAGIC_VAL_W2,
67 		BOOT_MAGIC_VAL_W3
68 	};
69 	uint32_t readout[ARRAY_SIZE(expectation)];
70 	int ret;
71 
72 	ret = flash_area_open(SLOT1_PARTITION_ID, &fa);
73 	if (ret) {
74 		printf("Flash driver was not found!\n");
75 		return;
76 	}
77 
78 	zassert(boot_request_upgrade(false) == 0, "pass", "fail");
79 
80 	ret = flash_area_read(fa, fa->fa_size - sizeof(expectation),
81 			      &readout, sizeof(readout));
82 	zassert_true(ret == 0, "Read from flash");
83 
84 	zassert(memcmp(expectation, readout, sizeof(expectation)) == 0,
85 		"pass", "fail");
86 
87 	zassert(boot_erase_img_bank(SLOT1_PARTITION_ID) == 0,
88 				    "pass", "fail");
89 
90 	zassert(boot_request_upgrade(true) == 0, "pass", "fail");
91 
92 	ret = flash_area_read(fa, fa->fa_size - sizeof(expectation),
93 			      &readout, sizeof(readout));
94 	zassert_true(ret == 0, "Read from flash");
95 
96 	zassert(memcmp(&expectation[2], &readout[2], sizeof(expectation) -
97 		       2 * sizeof(expectation[0])) == 0, "pass", "fail");
98 
99 	zassert_equal(1, readout[0] & 0xff, "confirmation error");
100 }
101 
ZTEST(mcuboot_interface,test_write_confirm)102 ZTEST(mcuboot_interface, test_write_confirm)
103 {
104 	const uint32_t img_magic[4] = BOOT_MAGIC_VALUES;
105 	uint32_t readout[ARRAY_SIZE(img_magic)];
106 	uint8_t flag[BOOT_MAX_ALIGN];
107 	const struct flash_area *fa;
108 	int ret;
109 
110 	flag[0] = 0x01;
111 	memset(&flag[1], 0xff, sizeof(flag) - 1);
112 
113 	ret = flash_area_open(SLOT0_PARTITION_ID, &fa);
114 	if (ret) {
115 		printf("Flash driver was not found!\n");
116 		return;
117 	}
118 
119 	zassert(boot_erase_img_bank(SLOT0_PARTITION_ID) == 0,
120 		"pass", "fail");
121 
122 	ret = flash_area_read(fa, fa->fa_size - sizeof(img_magic),
123 			      &readout, sizeof(img_magic));
124 	zassert_true(ret == 0, "Read from flash");
125 
126 	if (memcmp(img_magic, readout, sizeof(img_magic)) != 0) {
127 		ret = flash_area_write(fa, fa->fa_size - 16,
128 				       img_magic, 16);
129 		zassert_true(ret == 0, "Write to flash");
130 	}
131 
132 	/* set copy-done flag */
133 	ret = flash_area_write(fa, fa->fa_size - 32, &flag, sizeof(flag));
134 	zassert_true(ret == 0, "Write to flash");
135 
136 	ret = boot_write_img_confirmed();
137 	zassert(ret == 0, "pass", "fail (%d)", ret);
138 
139 	ret = flash_area_read(fa, fa->fa_size - 24, readout,
140 			      sizeof(readout[0]));
141 	zassert_true(ret == 0, "Read from flash");
142 
143 	zassert_equal(1, readout[0] & 0xff, "confirmation error");
144 }
145 
146 ZTEST_SUITE(mcuboot_interface, NULL, NULL, NULL, NULL, NULL);
147