1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/drivers/flash.h>
9 #include <zephyr/device.h>
10 
11 /* Warning: The test has been written for testing boards with single
12  * instance of Flash Simulator device only.
13  */
14 
15 /* configuration derived from DT */
16 #ifdef CONFIG_ARCH_POSIX
17 #define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_0)
18 #else
19 #define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_sim_0)
20 #endif /* CONFIG_ARCH_POSIX */
21 #define FLASH_SIMULATOR_BASE_OFFSET DT_REG_ADDR(SOC_NV_FLASH_NODE)
22 #define FLASH_SIMULATOR_ERASE_UNIT DT_PROP(SOC_NV_FLASH_NODE, erase_block_size)
23 #define FLASH_SIMULATOR_PROG_UNIT DT_PROP(SOC_NV_FLASH_NODE, write_block_size)
24 #define FLASH_SIMULATOR_FLASH_SIZE DT_REG_SIZE(SOC_NV_FLASH_NODE)
25 
26 #define FLASH_SIMULATOR_ERASE_VALUE \
27 		DT_PROP(DT_PARENT(SOC_NV_FLASH_NODE), erase_value)
28 
29 /* Offset between pages */
30 #define TEST_SIM_FLASH_SIZE FLASH_SIMULATOR_FLASH_SIZE
31 
32 #define TEST_SIM_FLASH_END (TEST_SIM_FLASH_SIZE +\
33 			   FLASH_SIMULATOR_BASE_OFFSET)
34 
35 #define PATTERN8TO32BIT(pat) \
36 		(((((((0xff & pat) << 8) | (0xff & pat)) << 8) | \
37 		   (0xff & pat)) << 8) | (0xff & pat))
38 
39 #if (defined(CONFIG_ARCH_POSIX) || defined(CONFIG_BOARD_QEMU_X86))
40 static const struct device *const flash_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
41 #else
42 static const struct device *const flash_dev = DEVICE_DT_GET(DT_NODELABEL(sim_flash_controller));
43 #endif
44 static uint8_t test_read_buf[TEST_SIM_FLASH_SIZE];
45 
46 static uint32_t p32_inc;
47 
pattern32_ini(uint32_t val)48 void pattern32_ini(uint32_t val)
49 {
50 	p32_inc = val;
51 }
52 
pattern32_inc(void)53 static uint32_t pattern32_inc(void)
54 {
55 	return p32_inc++;
56 }
57 
pattern32_flat(void)58 static uint32_t pattern32_flat(void)
59 {
60 	return p32_inc;
61 }
62 
test_check_pattern32(off_t start,uint32_t (* pattern_gen)(void),size_t size)63 static void test_check_pattern32(off_t start, uint32_t (*pattern_gen)(void),
64 				 size_t size)
65 {
66 	off_t off;
67 	uint32_t val32, r_val32;
68 	int rc;
69 
70 	for (off = 0; off < size; off += 4) {
71 		rc = flash_read(flash_dev, start + off, &r_val32,
72 				sizeof(r_val32));
73 		zassert_equal(0, rc, "flash_write should succeed");
74 		val32 = pattern_gen();
75 		zassert_equal(val32, r_val32,
76 			     "flash word at offset 0x%x has value 0x%08x, " \
77 			     "expected 0x%08x",
78 			     start + off, r_val32, val32);
79 	}
80 }
81 
82 /* ret < 0 is errno; ret == 1 is bad value in flash */
test_check_erase(const struct device * dev,off_t offset,size_t size)83 static int test_check_erase(const struct device *dev, off_t offset, size_t size)
84 {
85 	uint8_t buf[FLASH_SIMULATOR_PROG_UNIT];
86 	int rc;
87 	int i = 0;
88 
89 	BUILD_ASSERT(sizeof(buf) >= FLASH_SIMULATOR_PROG_UNIT);
90 
91 	i = 0;
92 	while (i < size) {
93 		size_t chunk = MIN(size - i, sizeof(buf));
94 		/* The memset is done to set buf to something else than
95 		 * FLASH_SIMULATOR_ERASE_VALUE, as we are trying to chek
96 		 * whether that is what is now in the memory.
97 		 */
98 		memset(buf, ~FLASH_SIMULATOR_ERASE_VALUE, sizeof(buf));
99 		rc = flash_read(flash_dev, offset + i,
100 				buf, chunk);
101 		if (rc < 0) {
102 			TC_PRINT("Unexpected flash_read fail @ %ld",
103 				 (long)(offset + i));
104 			return rc;
105 		}
106 		do {
107 			if ((uint8_t)buf[i & (sizeof(buf) - 1)] !=
108 			    (uint8_t)FLASH_SIMULATOR_ERASE_VALUE) {
109 				TC_PRINT("Flash not erased at %ld\n",
110 					 (long)(offset + i));
111 				return 1;
112 			}
113 			++i;
114 			--chunk;
115 		} while (chunk);
116 	}
117 	return 0;
118 }
119 
120 /* Get access to the device and erase it ready for testing*/
test_init(void)121 static void test_init(void)
122 {
123 	zassert_true(device_is_ready(flash_dev),
124 		     "Simulated flash device not ready");
125 }
126 
ZTEST(flash_sim_api,test_read)127 ZTEST(flash_sim_api, test_read)
128 {
129 	off_t i;
130 	int rc;
131 
132 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
133 			 FLASH_SIMULATOR_FLASH_SIZE);
134 	zassert_equal(0, rc, "flash_erase should succeed");
135 
136 
137 	rc = flash_read(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
138 			test_read_buf, sizeof(test_read_buf));
139 	zassert_equal(0, rc, "flash_read should succeed");
140 
141 	for (i = 0; i < sizeof(test_read_buf); i++) {
142 		zassert_equal(FLASH_SIMULATOR_ERASE_VALUE,
143 			     test_read_buf[i],
144 			     "sim flash byte at offset 0x%x has value 0x%08x",
145 			     i, test_read_buf[i]);
146 	}
147 }
148 
test_write_read(void)149 static void test_write_read(void)
150 {
151 	off_t off;
152 	uint32_t val32 = 0, r_val32;
153 	int rc;
154 
155 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
156 			 FLASH_SIMULATOR_FLASH_SIZE);
157 	zassert_equal(0, rc, "flash_erase should succeed");
158 
159 
160 	for (off = 0; off < TEST_SIM_FLASH_SIZE; off += 4) {
161 		rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET +
162 					    off,
163 				 &val32, sizeof(val32));
164 		zassert_equal(0, rc,
165 			      "flash_write (%d) should succeed at off 0x%x", rc,
166 			       FLASH_SIMULATOR_BASE_OFFSET + off);
167 		val32++;
168 	}
169 
170 	val32 = 0;
171 
172 	for (off = 0; off < TEST_SIM_FLASH_SIZE; off += 4) {
173 		rc = flash_read(flash_dev, FLASH_SIMULATOR_BASE_OFFSET +
174 					   off,
175 				&r_val32, sizeof(r_val32));
176 		zassert_equal(0, rc, "flash_write should succeed");
177 		zassert_equal(val32, r_val32,
178 			"flash byte at offset 0x%x has value 0x%08x, expected" \
179 			" 0x%08x",
180 			off, r_val32, val32);
181 		val32++;
182 	}
183 }
184 
test_erase(void)185 static void test_erase(void)
186 {
187 	int rc;
188 
189 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET +
190 			 FLASH_SIMULATOR_ERASE_UNIT,
191 			 FLASH_SIMULATOR_ERASE_UNIT);
192 	zassert_equal(0, rc, "flash_erase should succeed");
193 
194 	TC_PRINT("Incremental pattern expected\n");
195 	pattern32_ini(0);
196 	test_check_pattern32(FLASH_SIMULATOR_BASE_OFFSET, pattern32_inc,
197 			     FLASH_SIMULATOR_ERASE_UNIT);
198 
199 	TC_PRINT("Erased area expected\n");
200 	pattern32_ini(PATTERN8TO32BIT(FLASH_SIMULATOR_ERASE_VALUE));
201 	test_check_pattern32(FLASH_SIMULATOR_BASE_OFFSET +
202 			     FLASH_SIMULATOR_ERASE_UNIT, pattern32_flat,
203 			     FLASH_SIMULATOR_ERASE_UNIT);
204 
205 	TC_PRINT("Incremental pattern expected\n");
206 	pattern32_ini(FLASH_SIMULATOR_ERASE_UNIT*2 /
207 		      FLASH_SIMULATOR_PROG_UNIT);
208 	test_check_pattern32(FLASH_SIMULATOR_BASE_OFFSET +
209 			     FLASH_SIMULATOR_ERASE_UNIT*2, pattern32_inc,
210 			     FLASH_SIMULATOR_ERASE_UNIT*2);
211 }
212 
ZTEST(flash_sim_api,test_write_read_erase)213 ZTEST(flash_sim_api, test_write_read_erase)
214 {
215 	test_write_read();
216 	test_erase();
217 }
218 
219 
ZTEST(flash_sim_api,test_out_of_bounds)220 ZTEST(flash_sim_api, test_out_of_bounds)
221 {
222 	int rc;
223 	uint8_t data[8] = {0};
224 
225 	rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET - 4,
226 				 data, 4);
227 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
228 
229 	rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET - 4,
230 				 data, 8);
231 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
232 
233 	rc = flash_write(flash_dev, TEST_SIM_FLASH_END,
234 				 data, 4);
235 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
236 
237 	rc = flash_write(flash_dev, TEST_SIM_FLASH_END - 4,
238 				 data, 8);
239 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
240 
241 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET -
242 			 FLASH_SIMULATOR_ERASE_UNIT,
243 			 FLASH_SIMULATOR_ERASE_UNIT);
244 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
245 
246 	rc = flash_erase(flash_dev, TEST_SIM_FLASH_END,
247 			 FLASH_SIMULATOR_ERASE_UNIT);
248 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
249 
250 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET -
251 			 FLASH_SIMULATOR_ERASE_UNIT*2,
252 			 FLASH_SIMULATOR_ERASE_UNIT*2);
253 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
254 
255 	rc = flash_erase(flash_dev, TEST_SIM_FLASH_END -
256 			 FLASH_SIMULATOR_ERASE_UNIT,
257 			 FLASH_SIMULATOR_ERASE_UNIT*2);
258 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
259 
260 	rc = flash_read(flash_dev, FLASH_SIMULATOR_BASE_OFFSET - 4,
261 				 data, 4);
262 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
263 
264 	rc = flash_read(flash_dev, FLASH_SIMULATOR_BASE_OFFSET - 4,
265 				 data, 8);
266 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
267 
268 	rc = flash_read(flash_dev, TEST_SIM_FLASH_END,
269 				 data, 4);
270 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
271 
272 	rc = flash_read(flash_dev, TEST_SIM_FLASH_END - 4, data, 8);
273 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
274 }
275 
ZTEST(flash_sim_api,test_align)276 ZTEST(flash_sim_api, test_align)
277 {
278 	int rc;
279 	uint8_t data[4] = {0};
280 
281 	rc = flash_read(flash_dev, FLASH_SIMULATOR_BASE_OFFSET + 1,
282 				 data, 4);
283 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
284 
285 	rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET + 1,
286 				 data, 4);
287 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
288 
289 	rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
290 				 data, 3);
291 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
292 
293 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET + 1,
294 			 FLASH_SIMULATOR_ERASE_UNIT);
295 
296 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
297 
298 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
299 			 FLASH_SIMULATOR_ERASE_UNIT + 1);
300 
301 	zassert_equal(-EINVAL, rc, "Unexpected error code (%d)", rc);
302 }
303 
304 #if defined(CONFIG_FLASH_SIMULATOR_DOUBLE_WRITES) &&	\
305 	defined(CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE)
ZTEST(flash_sim_api,test_double_write)306 ZTEST(flash_sim_api, test_double_write)
307 {
308 	int rc;
309 	/* Test checks behaviour of write when attempting to double write
310 	 * selected offset. Simulator, prior to write, checks if selected
311 	 * memory contains erased values and returns -EIO if not; data has
312 	 * to be initialized to value that will not be equal to erase
313 	 * value of flash, for this test.
314 	 */
315 	uint32_t data = ~(PATTERN8TO32BIT(FLASH_SIMULATOR_ERASE_VALUE));
316 
317 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
318 			 FLASH_SIMULATOR_ERASE_UNIT);
319 	zassert_equal(0, rc, "flash_erase should succeed");
320 
321 	rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
322 				 &data, sizeof(data));
323 	zassert_equal(0, rc, "flash_write should succeed");
324 
325 	rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
326 				 &data, sizeof(data));
327 	zassert_equal(-EIO, rc, "Unexpected error code (%d)", rc);
328 }
329 #endif
330 
331 #if !defined(CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE)
ZTEST(flash_sim_api,test_ramlike)332 ZTEST(flash_sim_api, test_ramlike)
333 {
334 	/* Within code below there is assumption that the src size is
335 	 * equal or greater than the FLASH_SIMULATOR_PROG_UNIT
336 	 * (write-block-size) of device.
337 	 */
338 	const uint8_t src[] = "Hello world! This is test string";
339 	uint8_t buf[FLASH_SIMULATOR_PROG_UNIT];
340 	/* Round up to next write-block-size */
341 	int max = (sizeof(src) + FLASH_SIMULATOR_PROG_UNIT - 1) &
342 		  ~(FLASH_SIMULATOR_PROG_UNIT - 1);
343 	int rc;
344 	int i = 0;
345 	int is = 0;	/* The index within src */
346 
347 	BUILD_ASSERT(sizeof(src) >= FLASH_SIMULATOR_PROG_UNIT);
348 
349 	/* Scrub memory with something constant */
350 	memset(buf, FLASH_SIMULATOR_ERASE_VALUE, sizeof(buf));
351 	while (i < max) {
352 		rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET + i,
353 				 buf, sizeof(buf));
354 		zassert_equal(0, rc, "flash_write should succeed");
355 		i += sizeof(buf);
356 	}
357 
358 	/* Check the scrubbing */
359 	zassert_equal(0, test_check_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET, max),
360 		      "Area not erased");
361 
362 	/* Now write something new */
363 	i = 0;
364 	while (i < max) {
365 		do {
366 			buf[i & (sizeof(buf) - 1)] = src[is];
367 			++i;
368 			++is;
369 
370 			/* Continue writing from the beginning of the src */
371 			if (is >= sizeof(src)) {
372 				is = 0;
373 			}
374 		} while (i & (sizeof(buf) - 1));
375 		rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET	+ i - sizeof(buf),
376 				 buf, sizeof(buf));
377 		zassert_equal(0, rc, "flash_write should succeed");
378 	}
379 
380 	/* Check the write */
381 	i = 0;
382 	is = 0;
383 	while (i < max) {
384 		rc = flash_read(flash_dev, FLASH_SIMULATOR_BASE_OFFSET + i,
385 				buf, sizeof(buf));
386 		zassert_equal(0, rc, "flash_read should succeed");
387 		do {
388 			zassert_equal((uint8_t)src[is], (uint8_t)buf[i & (sizeof(buf) - 1)],
389 				      "Expected src and buf to match at index %d\n", i);
390 			++i;
391 			++is;
392 			/* Src has wrapped around */
393 			if (is >= sizeof(src)) {
394 				is = 0;
395 			}
396 		} while (i & (sizeof(buf) - 1));
397 		zassert_equal(0, rc, "Unexpected value read");
398 	}
399 
400 	/* Because we are checking random access writes, we are now going to
401 	 * write binary not of the same data we have just written. If this would be
402 	 * program-erase type memory, where you can only change from erase value
403 	 * to opposite, such write would render incorrect data in memory,
404 	 * but this is random access device so we should get exactly what we write.
405 	 */
406 	i = 0;
407 	is = 0;
408 	while (i < max) {
409 		do {
410 			buf[i & (sizeof(buf) - 1)] = ~src[is];
411 			++i;
412 			++is;
413 
414 			/* Continue writing from the beginning of the src */
415 			if (is >= sizeof(src)) {
416 				is = 0;
417 			}
418 		} while (i & (sizeof(buf) - 1));
419 		rc = flash_write(flash_dev, FLASH_SIMULATOR_BASE_OFFSET	+ i - sizeof(buf),
420 				 buf, sizeof(buf));
421 		zassert_equal(0, rc, "flash_write should succeed");
422 	}
423 
424 	/* Check the write */
425 	i = 0;
426 	is = 0;
427 	while (i < max) {
428 		rc = flash_read(flash_dev, FLASH_SIMULATOR_BASE_OFFSET + i,
429 				buf, sizeof(buf));
430 		zassert_equal(0, rc, "flash_read should succeed");
431 		do {
432 			zassert_equal((uint8_t)~src[is], (uint8_t)buf[i & (sizeof(buf) - 1)],
433 				      "Expected src and buf to match at index %d\n", i);
434 			++i;
435 			++is;
436 			/* Src has wrapped around */
437 			if (is >= sizeof(src)) {
438 				is = 0;
439 			}
440 		} while (i & (sizeof(buf) - 1));
441 		zassert_equal(0, rc, "Unexpected value read");
442 	}
443 }
444 #endif
445 
ZTEST(flash_sim_api,test_get_erase_value)446 ZTEST(flash_sim_api, test_get_erase_value)
447 {
448 	const struct flash_parameters *fp = flash_get_parameters(flash_dev);
449 
450 	zassert_equal(fp->erase_value, FLASH_SIMULATOR_ERASE_VALUE,
451 		      "Expected erase value %x",
452 		      FLASH_SIMULATOR_ERASE_VALUE);
453 }
454 
ZTEST(flash_sim_api,test_flash_fill)455 ZTEST(flash_sim_api, test_flash_fill)
456 {
457 	off_t i;
458 	int rc;
459 	uint8_t buf[FLASH_SIMULATOR_PROG_UNIT];
460 #if defined(CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE)
461 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
462 			 FLASH_SIMULATOR_FLASH_SIZE);
463 	zassert_equal(0, rc, "flash_erase should succeed");
464 #else
465 	rc = flash_fill(flash_dev, FLASH_SIMULATOR_ERASE_VALUE,
466 			FLASH_SIMULATOR_BASE_OFFSET,
467 			FLASH_SIMULATOR_FLASH_SIZE);
468 	zassert_equal(0, rc, "flash_fill should succeed");
469 #endif
470 	size_t size = FLASH_SIMULATOR_FLASH_SIZE;
471 
472 	zassert_equal(0, test_check_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
473 					  FLASH_SIMULATOR_FLASH_SIZE),
474 		      "Area not erased");
475 
476 	rc = flash_fill(flash_dev, 0x55,
477 			FLASH_SIMULATOR_BASE_OFFSET,
478 			FLASH_SIMULATOR_FLASH_SIZE);
479 	zassert_equal(0, rc, "flash_fill should succeed");
480 
481 	/* Check if fill did work on both type of devices */
482 	i = 0;
483 	size = FLASH_SIMULATOR_FLASH_SIZE;
484 	while (i < FLASH_SIMULATOR_FLASH_SIZE) {
485 		size_t chunk = MIN(size, FLASH_SIMULATOR_PROG_UNIT);
486 
487 		memset(buf, FLASH_SIMULATOR_ERASE_VALUE, sizeof(buf));
488 		rc = flash_read(flash_dev, FLASH_SIMULATOR_BASE_OFFSET + i,
489 				buf, chunk);
490 		zassert_equal(0, rc, "flash_read should succeed at offset %d", i);
491 		do {
492 			zassert_equal((uint8_t)buf[i & (sizeof(buf) - 1)], 0x55,
493 				      "Unexpected value at offset %d\n", i);
494 			++i;
495 			--size;
496 			--chunk;
497 		} while (chunk);
498 	}
499 }
500 
ZTEST(flash_sim_api,test_flash_flatten)501 ZTEST(flash_sim_api, test_flash_flatten)
502 {
503 	int rc;
504 #if defined(CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE)
505 	rc = flash_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
506 			 FLASH_SIMULATOR_FLASH_SIZE);
507 	zassert_equal(0, rc, "flash_erase should succeed");
508 #else
509 	rc = flash_fill(flash_dev, FLASH_SIMULATOR_ERASE_VALUE,
510 			FLASH_SIMULATOR_BASE_OFFSET,
511 			FLASH_SIMULATOR_FLASH_SIZE);
512 	zassert_equal(0, rc, "flash_fill should succeed");
513 #endif
514 	/* Now fill the device with anything */
515 	rc = flash_fill(flash_dev, 0xaa,
516 			FLASH_SIMULATOR_BASE_OFFSET,
517 			FLASH_SIMULATOR_FLASH_SIZE);
518 	zassert_equal(0, rc, "flash_fill should succeed");
519 
520 	/* And erase */
521 	rc = flash_flatten(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
522 			   FLASH_SIMULATOR_FLASH_SIZE);
523 	zassert_equal(0, rc, "flash_flatten should succeed");
524 
525 #if !defined(CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE)
526 	/* In case of RAM like devices, the erase may not be
527 	 * provided, in such case flash_flatten calls flash_fill
528 	 * with erase_value.
529 	 */
530 	struct device other;
531 	struct flash_driver_api api;
532 
533 	memcpy(&other, flash_dev, sizeof(other));
534 	other.api = &api;
535 	memcpy(&api, flash_dev->api, sizeof(api));
536 	api.erase = NULL;
537 
538 	/* Now fill the device with anything */
539 	rc = flash_fill(flash_dev, 0xaa,
540 			FLASH_SIMULATOR_BASE_OFFSET,
541 			FLASH_SIMULATOR_FLASH_SIZE);
542 	zassert_equal(0, rc, "flash_fill should succeed");
543 
544 	/* The erase is not implemented */
545 	rc = flash_erase(&other, FLASH_SIMULATOR_BASE_OFFSET,
546 			 FLASH_SIMULATOR_FLASH_SIZE);
547 	zassert_equal(-ENOSYS, rc, "Expected not implemented");
548 
549 	rc = flash_flatten(&other, FLASH_SIMULATOR_BASE_OFFSET,
550 				  FLASH_SIMULATOR_FLASH_SIZE);
551 	zassert_equal(0, rc, "flash_flatten should succeed");
552 
553 	test_check_erase(flash_dev, FLASH_SIMULATOR_BASE_OFFSET,
554 			 FLASH_SIMULATOR_FLASH_SIZE);
555 
556 #endif
557 }
558 
559 #include <zephyr/drivers/flash/flash_simulator.h>
560 
ZTEST(flash_sim_api,test_get_mock)561 ZTEST(flash_sim_api, test_get_mock)
562 {
563 #ifdef CONFIG_ARCH_POSIX
564 	ztest_test_skip();
565 #else
566 	size_t mock_size;
567 	void *mock_ptr;
568 
569 	mock_ptr = flash_simulator_get_memory(flash_dev, &mock_size);
570 
571 	zassert_true(mock_ptr != NULL,
572 		     "Expected mock_flash address, got NULL.");
573 	zassert_equal(mock_size, FLASH_SIMULATOR_FLASH_SIZE,
574 		     "Expected mock_flash size %d, got %d",
575 		      FLASH_SIMULATOR_FLASH_SIZE, mock_size);
576 #endif
577 }
578 
flash_sim_setup(void)579 void *flash_sim_setup(void)
580 {
581 	test_init();
582 
583 	return NULL;
584 }
585 
586 ZTEST_SUITE(flash_sim_api, NULL, flash_sim_setup, NULL, NULL, NULL);
587