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 #include <zephyr/sys/reboot.h>
11 #include <zephyr/toolchain.h>
12
13 #define MAGIC_WORD 0xABDE2134
14
15 #define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_sim_0)
16 #define FLASH_SIMULATOR_FLASH_SIZE DT_REG_SIZE(SOC_NV_FLASH_NODE)
17
18 static const struct device *const flash_dev = DEVICE_DT_GET(DT_NODELABEL(sim_flash_controller));
19 static uint32_t boot_count __noinit;
20
ZTEST(flash_sim_reboot,test_preserve_over_reboot)21 ZTEST(flash_sim_reboot, test_preserve_over_reboot)
22 {
23 uint32_t word = MAGIC_WORD;
24 int rc;
25
26 if (boot_count == 0) {
27 printk("First boot, erasing flash\n");
28 rc = flash_erase(flash_dev, 0, FLASH_SIMULATOR_FLASH_SIZE);
29 zassert_equal(0, rc, "Failed to erase flash");
30 printk("Writing magic word to offset 0\n");
31 rc = flash_write(flash_dev, 0, &word, sizeof(word));
32 zassert_equal(0, rc, "Failed to write flash");
33 printk("Rebooting device...\n");
34 boot_count += 1;
35 sys_reboot(SYS_REBOOT_WARM);
36 zassert_unreachable("Failed to reboot");
37 } else if (boot_count == 1) {
38 printk("Second boot, reading magic word\n");
39 rc = flash_read(flash_dev, 0, &word, sizeof(word));
40 zassert_equal(0, rc, "Failed to read flash");
41 zassert_equal(MAGIC_WORD, word, "Magic word not preserved");
42 } else {
43 zassert_unreachable("Unexpected boot_count value %d", boot_count);
44 }
45 }
46
flash_sim_setup(void)47 void *flash_sim_setup(void)
48 {
49 zassert_true(device_is_ready(flash_dev), "Simulated flash device not ready");
50
51 return NULL;
52 }
53
54 ZTEST_SUITE(flash_sim_reboot, NULL, flash_sim_setup, NULL, NULL, NULL);
55