Skip to content

Commit 49bbbab

Browse files
thedjnKde-nordic
authored andcommitted
boot: zephyr: Add watchdog setup/timeout configurability options
Adds Kconfig options for configuring and setting up the watchdog in MCUboot which follow the Zephyr watchdog driver requirements, these options can be changed to restore the previous (invalid) MCUboot watchdog functionality of setting up a watchdog without installing any timeouts or can, optionally, be wholly replaced out of tree by overriding the newly introduced weak functions. Signed-off-by: Jamie McCrae <[email protected]>
1 parent c25d250 commit 49bbbab

File tree

2 files changed

+86
-20
lines changed

2 files changed

+86
-20
lines changed

boot/zephyr/Kconfig

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,21 +1104,6 @@ config MCUBOOT_UUID_CID
11041104
Provide an image class identification scheme to prevent processing
11051105
images for a different CPU or device produced by the same vendor.
11061106

1107-
config BOOT_WATCHDOG_FEED
1108-
bool "Feed the watchdog while doing swap"
1109-
default y if WATCHDOG
1110-
default y if SOC_FAMILY_NORDIC_NRF
1111-
imply BOOT_WATCHDOG_FEED_NRFX_WDT if SOC_FAMILY_NORDIC_NRF
1112-
help
1113-
Enables implementation of MCUBOOT_WATCHDOG_FEED() macro which is
1114-
used to feed watchdog while doing time consuming operations.
1115-
1116-
config BOOT_WATCHDOG_FEED_NRFX_WDT
1117-
bool "Feed the watchdog using NRFX WDT directly"
1118-
depends on SOC_FAMILY_NORDIC_NRF
1119-
# for nRF nrfx based implementation is available
1120-
imply NRFX_WDT
1121-
11221107
config BOOT_IMAGE_ACCESS_HOOKS
11231108
bool "Hooks for overriding MCUboot's native routines"
11241109
help
@@ -1263,6 +1248,60 @@ config MCUBOOT_STORAGE_MINIMAL_SCRAMBLE
12631248
Depending on type of device this may be done by erase of minimal
12641249
number of pages or overwrite of part of image.
12651250

1251+
menu "Watchdog configuration"
1252+
1253+
config BOOT_WATCHDOG_SETUP_AT_BOOT
1254+
bool "Setup watchdog on boot"
1255+
depends on WATCHDOG
1256+
default y
1257+
help
1258+
Will set the watchdog up at boot, if this option is enabled and
1259+
CONFIG_BOOT_WATCHDOG_INSTALL_TIMEOUT_AT_BOOT is disabled then this is non-compliant
1260+
with the Zephyr watchdog driver interface as no timeouts will be installed but is
1261+
left as an option as MCUboot has seemingly done this for 6 years prior and it might be
1262+
used in configurations where the watchdog is enabled by other images or directly in
1263+
hardware.
1264+
1265+
Note that the in-built watchdog functionality in MCUboot can be replaced with custom
1266+
logic by overriding the weak symbol functions `mcuboot_watchdog_setup` and
1267+
`mcuboot_watchdog_feed`.
1268+
1269+
config BOOT_WATCHDOG_INSTALL_TIMEOUT_AT_BOOT
1270+
bool "Install watchdog timeout on boot"
1271+
depends on BOOT_WATCHDOG_SETUP_AT_BOOT
1272+
default y
1273+
help
1274+
Will set the watchdog up at boot and install a timeout, note that this watchdog will
1275+
then need to be continuously fed from the application once it is booted. The Zephyr
1276+
watchdog driver might need special configuration or re-init in the application to
1277+
allow the application to feed it.
1278+
1279+
config BOOT_WATCHDOG_TIMEOUT_MS
1280+
int "Watchdog timeout (ms)"
1281+
depends on BOOT_WATCHDOG_INSTALL_TIMEOUT_AT_BOOT
1282+
default 300000
1283+
help
1284+
Will setup a timeout for this duration when MCUboot starts. This defaults to a 5 minute
1285+
timeout, which is sufficient for most devices to be able to swap an image an boot the
1286+
new application which can initialise itself and feed the watchdog before it times out.
1287+
1288+
config BOOT_WATCHDOG_FEED
1289+
bool "Feed the watchdog while doing swap"
1290+
default y if WATCHDOG
1291+
default y if SOC_FAMILY_NORDIC_NRF
1292+
imply BOOT_WATCHDOG_FEED_NRFX_WDT if SOC_FAMILY_NORDIC_NRF
1293+
help
1294+
Enables implementation of MCUBOOT_WATCHDOG_FEED() macro which is
1295+
used to feed watchdog while doing time consuming operations.
1296+
1297+
config BOOT_WATCHDOG_FEED_NRFX_WDT
1298+
bool "Feed the watchdog using NRFX WDT directly"
1299+
depends on SOC_FAMILY_NORDIC_NRF
1300+
# for nRF nrfx based implementation is available
1301+
imply NRFX_WDT
1302+
1303+
endmenu # "Watchdog configuration"
1304+
12661305
menu "Defaults"
12671306
# Items in this menu should not be manually set. These options are for modules/sysbuild to
12681307
# set as defaults to allow MCUboot's default configuration to be set, but still allow it

boot/zephyr/watchdog.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,46 @@
1717

1818
BOOT_LOG_MODULE_DECLARE(mcuboot);
1919

20+
/*
21+
* Channel of watchdog that is setup and fed, if CONFIG_BOOT_WATCHDOG_INSTALL_TIMEOUT_AT_BOOT is
22+
* not set then this will remain at the default of 0 but the driver will not have been set up
23+
* which is non-compliant with the Zephyr watchdog driver interface but is left as some drivers
24+
* may have a hardware (or previously started watchdog) which the driver will automatically read
25+
* the configuration of upon driver init.
26+
*/
27+
#if defined(CONFIG_BOOT_WATCHDOG_INSTALL_TIMEOUT_AT_BOOT) && DT_NODE_HAS_STATUS(DT_ALIAS(watchdog0), okay)
28+
static int watchdog_channel = 0;
29+
#endif
30+
2031
__weak void mcuboot_watchdog_setup(void)
2132
{
22-
#if defined(CONFIG_WATCHDOG) && DT_NODE_HAS_STATUS(DT_ALIAS(watchdog0), okay)
33+
#if defined(CONFIG_BOOT_WATCHDOG_SETUP_AT_BOOT) && DT_NODE_HAS_STATUS(DT_ALIAS(watchdog0), okay)
2334
const struct device *watchdog_device = DEVICE_DT_GET(DT_ALIAS(watchdog0));
2435

2536
if (device_is_ready(watchdog_device)) {
2637
int rc;
38+
#if defined(CONFIG_BOOT_WATCHDOG_INSTALL_TIMEOUT_AT_BOOT)
39+
struct wdt_timeout_cfg wdt_config = {
40+
.flags = WDT_FLAG_RESET_SOC,
41+
.window.min = 0U,
42+
.window.max = CONFIG_BOOT_WATCHDOG_TIMEOUT_MS,
43+
};
2744

28-
rc = wdt_setup(watchdog_device, 0);
45+
rc = wdt_install_timeout(watchdog_device, &wdt_config);
2946

30-
if (rc != 0) {
31-
BOOT_LOG_ERR("Watchdog setup failed: %d", rc);
47+
if (rc >= 0) {
48+
watchdog_channel = rc;
49+
#endif
50+
rc = wdt_setup(watchdog_device, 0);
51+
52+
if (rc != 0) {
53+
BOOT_LOG_ERR("Watchdog setup failed: %d", rc);
54+
}
55+
#if defined(CONFIG_BOOT_WATCHDOG_INSTALL_TIMEOUT_AT_BOOT)
56+
} else {
57+
BOOT_LOG_ERR("Watchdog install timeout failed: %d", rc);
3258
}
59+
#endif
3360
}
3461
#endif
3562
}
@@ -66,7 +93,7 @@ __weak void mcuboot_watchdog_feed(void)
6693
const struct device *watchdog_device = DEVICE_DT_GET(DT_ALIAS(watchdog0));
6794

6895
if (device_is_ready(watchdog_device)) {
69-
wdt_feed(watchdog_device, 0);
96+
wdt_feed(watchdog_device, watchdog_channel);
7097
}
7198
#endif
7299
}

0 commit comments

Comments
 (0)