1 /*
2  * Copyright (c) 2023, Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/devicetree.h>
9 #include <zephyr/retention/retention.h>
10 
11 static const struct device *boot_mode_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_boot_mode));
12 
bootmode_check(uint8_t boot_mode)13 int bootmode_check(uint8_t boot_mode)
14 {
15 	int rc;
16 
17 	rc = retention_is_valid(boot_mode_dev);
18 
19 	if (rc == 1 || rc == -ENOTSUP) {
20 		uint8_t stored_mode;
21 
22 		rc = retention_read(boot_mode_dev, 0, &stored_mode, sizeof(stored_mode));
23 
24 		/* Only check if modes match if there was no error, otherwise return the error */
25 		if (rc == 0) {
26 			if (stored_mode == boot_mode) {
27 				rc = 1;
28 			}
29 		}
30 	}
31 
32 	return rc;
33 }
34 
bootmode_set(uint8_t boot_mode)35 int bootmode_set(uint8_t boot_mode)
36 {
37 	return retention_write(boot_mode_dev, 0, &boot_mode, sizeof(boot_mode));
38 }
39 
bootmode_clear(void)40 int bootmode_clear(void)
41 {
42 	return retention_clear(boot_mode_dev);
43 }
44