forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a boot mode system which allows for redirecting the boot target of a device depending upon the state of a retained value. Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
- Loading branch information
1 parent
8f859a0
commit 7e11b63
Showing
4 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2023 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* @file | ||
* @brief Public API for boot mode interface | ||
*/ | ||
|
||
#ifndef ZEPHYR_INCLUDE_RETENTION_BOOTMODE_ | ||
#define ZEPHYR_INCLUDE_RETENTION_BOOTMODE_ | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/device.h> | ||
#include <zephyr/types.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief Boot mode interface | ||
* @defgroup boot_mode_interface Boot mode interface | ||
* @ingroup retention | ||
* @{ | ||
*/ | ||
|
||
enum BOOT_MODE_TYPES { | ||
/** Default (normal) boot, to user application */ | ||
BOOT_MODE_TYPE_NORMAL = 0x00, | ||
|
||
/** Bootloader boot mode (e.g. serial recovery for MCUboot) */ | ||
BOOT_MODE_TYPE_BOOTLOADER, | ||
}; | ||
|
||
/** | ||
* @brief Checks if the boot mode of the device is set to a specific value. | ||
* | ||
* @param boot_mode Expected boot mode to check. | ||
* | ||
* @retval 1 If successful and boot mode matches. | ||
* @retval 0 If boot mode does not match. | ||
* @retval -errno Error code code. | ||
*/ | ||
int bootmode_check(uint8_t boot_mode); | ||
|
||
/** | ||
* @brief Sets boot mode of device. | ||
* | ||
* @param boot_mode Boot mode value to set. | ||
* | ||
* @retval 0 If successful. | ||
* @retval -errno Error code code. | ||
*/ | ||
int bootmode_set(uint8_t boot_mode); | ||
|
||
/** | ||
* @brief Clear boot mode value (sets to 0) - which corresponds to | ||
* #BOOT_MODE_TYPE_NORMAL. | ||
* | ||
* @retval 0 If successful. | ||
* @retval -errno Error code code. | ||
*/ | ||
int bootmode_clear(void); | ||
|
||
/** | ||
* @} | ||
*/ | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* ZEPHYR_INCLUDE_RETENTION_BOOTMODE_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2023, Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/kernel.h> | ||
#include <zephyr/devicetree.h> | ||
#include <zephyr/retention/retention.h> | ||
#include <zephyr/logging/log.h> | ||
|
||
LOG_MODULE_REGISTER(bootmode, CONFIG_RETENTION_LOG_LEVEL); | ||
|
||
static const struct device *boot_mode_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_boot_mode)); | ||
|
||
int bootmode_check(uint8_t boot_mode) | ||
{ | ||
int rc; | ||
|
||
rc = retention_is_valid(boot_mode_dev); | ||
|
||
if (rc == 1) { | ||
uint8_t stored_mode; | ||
|
||
rc = retention_read(boot_mode_dev, 0, &stored_mode, sizeof(stored_mode)); | ||
|
||
/* Only check if modes match if there was no error, otherwise return the error */ | ||
if (rc == 0) { | ||
if (stored_mode == boot_mode) { | ||
rc = 1; | ||
} | ||
} | ||
} | ||
|
||
return rc; | ||
} | ||
|
||
int bootmode_set(uint8_t boot_mode) | ||
{ | ||
return retention_write(boot_mode_dev, 0, &boot_mode, sizeof(boot_mode)); | ||
} | ||
|
||
int bootmode_clear(void) | ||
{ | ||
return retention_clear(boot_mode_dev); | ||
} |