1 /*
2  * Copyright (c) 2020-2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/drivers/flash.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/storage/flash_map.h>
12 
13 #if defined(CONFIG_NORDIC_QSPI_NOR)
14 #define TEST_AREA_DEV_NODE	DT_INST(0, nordic_qspi_nor)
15 #elif defined(CONFIG_SPI_NOR)
16 #define TEST_AREA_DEV_NODE	DT_INST(0, jedec_spi_nor)
17 #else
18 #define TEST_AREA	storage_partition
19 #endif
20 
21 /* TEST_AREA is only defined for configurations that realy on
22  * fixed-partition nodes.
23  */
24 #ifdef TEST_AREA
25 #define TEST_AREA_OFFSET	FIXED_PARTITION_OFFSET(TEST_AREA)
26 #define TEST_AREA_SIZE		FIXED_PARTITION_SIZE(TEST_AREA)
27 #define TEST_AREA_MAX		(TEST_AREA_OFFSET + TEST_AREA_SIZE)
28 #define TEST_AREA_DEVICE	FIXED_PARTITION_DEVICE(TEST_AREA)
29 
30 #elif defined(TEST_AREA_DEV_NODE)
31 
32 #define TEST_AREA_DEVICE	DEVICE_DT_GET(TEST_AREA_DEV_NODE)
33 #define TEST_AREA_OFFSET	0xff000
34 
35 #if DT_NODE_HAS_PROP(TEST_AREA_DEV_NODE, size_in_bytes)
36 #define TEST_AREA_MAX DT_PROP(TEST_AREA_DEV_NODE, size_in_bytes)
37 #else
38 #define TEST_AREA_MAX (DT_PROP(TEST_AREA_DEV_NODE, size) / 8)
39 #endif
40 
41 #else
42 #error "Unsupported configuraiton"
43 #endif
44 
45 #define EXPECTED_SIZE	512
46 
47 #if !defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE) &&		\
48 	!defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
49 #error There is no flash device enabled or it is missing Kconfig options
50 #endif
51 
52 static const struct device *const flash_dev = TEST_AREA_DEVICE;
53 static struct flash_pages_info page_info;
54 static uint8_t __aligned(4) expected[EXPECTED_SIZE];
55 static uint8_t erase_value;
56 static bool ebw_required;
57 
flash_driver_setup(void)58 static void *flash_driver_setup(void)
59 {
60 	int rc;
61 
62 	TC_PRINT("Test will run on device %s\n", flash_dev->name);
63 	zassert_true(device_is_ready(flash_dev));
64 
65 	/* Check for erase is only needed when there is mix of devices */
66 	if (IS_ENABLED(CONFIG_FLASH_HAS_EXPLICIT_ERASE)) {
67 		const struct flash_parameters *fparams = flash_get_parameters(flash_dev);
68 
69 		erase_value = fparams->erase_value;
70 		ebw_required = flash_params_get_erase_cap(fparams) & FLASH_ERASE_C_EXPLICIT;
71 		/* For tests purposes use page (in nrf_qspi_nor page = 64 kB) */
72 		flash_get_page_info_by_offs(flash_dev, TEST_AREA_OFFSET,
73 					    &page_info);
74 	} else {
75 		TC_PRINT("No devices with erase requirement present\n");
76 		erase_value = 0x55;
77 		page_info.start_offset = TEST_AREA_OFFSET;
78 		page_info.size = TEST_AREA_MAX - TEST_AREA_OFFSET;
79 	}
80 
81 
82 	/* Check if test region is not empty */
83 	uint8_t buf[EXPECTED_SIZE];
84 
85 	rc = flash_read(flash_dev, TEST_AREA_OFFSET,
86 			buf, EXPECTED_SIZE);
87 	zassert_equal(rc, 0, "Cannot read flash");
88 
89 	/* Fill test buffer with random data */
90 	for (int i = 0, val = 0; i < EXPECTED_SIZE; i++, val++) {
91 		/* Skip erase value */
92 		if (val == erase_value) {
93 			val++;
94 		}
95 		expected[i] = val;
96 	}
97 
98 	/* Check if tested region fits in flash */
99 	zassert_true((TEST_AREA_OFFSET + EXPECTED_SIZE) <= TEST_AREA_MAX,
100 		     "Test area exceeds flash size");
101 
102 	/* Check if flash is cleared */
103 	if (IS_ENABLED(CONFIG_FLASH_HAS_EXPLICIT_ERASE) && ebw_required) {
104 		bool is_buf_clear = true;
105 
106 		for (off_t i = 0; i < EXPECTED_SIZE; i++) {
107 			if (buf[i] != erase_value) {
108 				is_buf_clear = false;
109 				break;
110 			}
111 		}
112 
113 		if (!is_buf_clear) {
114 			/* Erase a nb of pages aligned to the EXPECTED_SIZE */
115 			rc = flash_erase(flash_dev, page_info.start_offset,
116 					(page_info.size *
117 					((EXPECTED_SIZE + page_info.size - 1)
118 					/ page_info.size)));
119 
120 			zassert_equal(rc, 0, "Flash memory not properly erased");
121 		}
122 	}
123 
124 	return NULL;
125 }
126 
ZTEST(flash_driver,test_read_unaligned_address)127 ZTEST(flash_driver, test_read_unaligned_address)
128 {
129 	int rc;
130 	uint8_t buf[EXPECTED_SIZE];
131 	const uint8_t canary = erase_value;
132 	uint32_t start;
133 
134 	if (IS_ENABLED(CONFIG_FLASH_HAS_EXPLICIT_ERASE) && ebw_required) {
135 		start = page_info.start_offset;
136 		/* Erase a nb of pages aligned to the EXPECTED_SIZE */
137 		rc = flash_erase(flash_dev, page_info.start_offset,
138 				(page_info.size *
139 				((EXPECTED_SIZE + page_info.size - 1)
140 				/ page_info.size)));
141 
142 		zassert_equal(rc, 0, "Flash memory not properly erased");
143 	} else {
144 		start = TEST_AREA_OFFSET;
145 	}
146 
147 	rc = flash_write(flash_dev,
148 			 start,
149 			 expected, EXPECTED_SIZE);
150 	zassert_equal(rc, 0, "Cannot write to flash");
151 
152 	/* read buffer length*/
153 	for (off_t len = 0; len < 25; len++) {
154 		/* address offset */
155 		for (off_t ad_o = 0; ad_o < 4; ad_o++) {
156 			/* buffer offset; leave space for buffer guard */
157 			for (off_t buf_o = 1; buf_o < 5; buf_o++) {
158 				/* buffer overflow protection */
159 				buf[buf_o - 1] = canary;
160 				buf[buf_o + len] = canary;
161 				memset(buf + buf_o, 0, len);
162 				rc = flash_read(flash_dev,
163 						start + ad_o,
164 						buf + buf_o, len);
165 				zassert_equal(rc, 0, "Cannot read flash");
166 				zassert_equal(memcmp(buf + buf_o,
167 						expected + ad_o,
168 						len),
169 					0, "Flash read failed at len=%d, "
170 					"ad_o=%d, buf_o=%d", len, ad_o, buf_o);
171 				/* check buffer guards */
172 				zassert_equal(buf[buf_o - 1], canary,
173 					"Buffer underflow at len=%d, "
174 					"ad_o=%d, buf_o=%d", len, ad_o, buf_o);
175 				zassert_equal(buf[buf_o + len], canary,
176 					"Buffer overflow at len=%d, "
177 					"ad_o=%d, buf_o=%d", len, ad_o, buf_o);
178 			}
179 		}
180 	}
181 }
182 
ZTEST(flash_driver,test_flash_fill)183 ZTEST(flash_driver, test_flash_fill)
184 {
185 	uint8_t buf[EXPECTED_SIZE];
186 	int rc;
187 	off_t i;
188 
189 	if (IS_ENABLED(CONFIG_FLASH_HAS_EXPLICIT_ERASE) && ebw_required) {
190 		/* Erase a nb of pages aligned to the EXPECTED_SIZE */
191 		rc = flash_erase(flash_dev, page_info.start_offset,
192 				(page_info.size *
193 				((EXPECTED_SIZE + page_info.size - 1)
194 				/ page_info.size)));
195 
196 		zassert_equal(rc, 0, "Flash memory not properly erased");
197 	} else {
198 		rc = flash_fill(flash_dev, 0x55, page_info.start_offset,
199 				(page_info.size *
200 				((EXPECTED_SIZE + page_info.size - 1)
201 				/ page_info.size)));
202 		zassert_equal(rc, 0, "Leveling memory with fill failed\n");
203 	}
204 
205 	/* Fill the device with 0xaa */
206 	rc = flash_fill(flash_dev, 0xaa, page_info.start_offset,
207 			(page_info.size *
208 			((EXPECTED_SIZE + page_info.size - 1)
209 			/ page_info.size)));
210 	zassert_equal(rc, 0, "Fill failed\n");
211 
212 	rc = flash_read(flash_dev, TEST_AREA_OFFSET,
213 			buf, EXPECTED_SIZE);
214 	zassert_equal(rc, 0, "Cannot read flash");
215 
216 	for (i = 0; i < EXPECTED_SIZE; i++) {
217 		if (buf[i] != 0xaa) {
218 			break;
219 		}
220 	}
221 	zassert_equal(i, EXPECTED_SIZE, "Expected device to be filled wth 0xaa");
222 }
223 
ZTEST(flash_driver,test_flash_flatten)224 ZTEST(flash_driver, test_flash_flatten)
225 {
226 	uint8_t buf[EXPECTED_SIZE];
227 	int rc;
228 	off_t i;
229 
230 	rc = flash_flatten(flash_dev, page_info.start_offset,
231 			   (page_info.size *
232 			   ((EXPECTED_SIZE + page_info.size - 1)
233 			   / page_info.size)));
234 
235 	zassert_equal(rc, 0, "Flash not leveled not properly erased");
236 
237 	/* Fill the device with 0xaa */
238 	rc = flash_fill(flash_dev, 0xaa, page_info.start_offset,
239 			(page_info.size *
240 			((EXPECTED_SIZE + page_info.size - 1)
241 			/ page_info.size)));
242 	zassert_equal(rc, 0, "Fill failed\n");
243 
244 	rc = flash_read(flash_dev, TEST_AREA_OFFSET,
245 			buf, EXPECTED_SIZE);
246 	zassert_equal(rc, 0, "Cannot read flash");
247 
248 	for (i = 0; i < EXPECTED_SIZE; i++) {
249 		if (buf[i] != 0xaa) {
250 			break;
251 		}
252 	}
253 	zassert_equal(i, EXPECTED_SIZE, "Expected device to be filled wth 0xaa");
254 }
255 
256 
257 ZTEST_SUITE(flash_driver, NULL, flash_driver_setup, NULL, NULL, NULL);
258