Skip to content

Commit 4a70a19

Browse files
committed
[nrf noup] dfu: boot: mcuboot_shell: Prevent erasing active areas
fixup! [nrf noup] tree-wide: support NCS Partition Manager (PM) definitions Adds in checks for partition manager to prevent users from erasing the MCUboot partition or the currently active partition Signed-off-by: Jamie McCrae <[email protected]>
1 parent 015b317 commit 4a70a19

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

subsys/dfu/boot/mcuboot_shell.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
#endif
2121
#endif
2222

23+
#if USE_PARTITION_MANAGER
24+
#include <flash_map_pm.h>
25+
26+
#ifdef CONFIG_NCS_IS_VARIANT_IMAGE
27+
#define ACTIVE_IMAGE_ID PM_MCUBOOT_SECONDARY_ID
28+
#else
29+
#define ACTIVE_IMAGE_ID PM_MCUBOOT_PRIMARY_ID
30+
#endif
31+
#endif
32+
2333
struct area_desc {
2434
const char *name;
2535
unsigned int id;
@@ -93,6 +103,35 @@ static int cmd_mcuboot_erase(const struct shell *sh, size_t argc,
93103
id = strtoul(argv[1], NULL, 0);
94104

95105
/* Check if this is the parent (MCUboot) or own slot and if so, deny the request */
106+
#if USE_PARTITION_MANAGER
107+
#ifdef PM_MCUBOOT_ID
108+
if (id == PM_MCUBOOT_ID || id == PM_MCUBOOT_PAD_ID) {
109+
shell_error(sh, "Cannot erase boot partition");
110+
return -EACCES;
111+
}
112+
#endif
113+
114+
#ifdef PM_APP_ID
115+
if (id == PM_APP_ID) {
116+
shell_error(sh, "Cannot erase this area");
117+
return -EACCES;
118+
}
119+
#endif
120+
121+
#ifdef PM_MCUBOOT_PRIMARY_APP_ID
122+
if (id == PM_MCUBOOT_PRIMARY_APP_ID) {
123+
shell_error(sh, "Cannot erase this area");
124+
return -EACCES;
125+
}
126+
#endif
127+
128+
#ifdef ACTIVE_IMAGE_ID
129+
if (id == ACTIVE_IMAGE_ID) {
130+
shell_error(sh, "Cannot erase active partitions");
131+
return -EACCES;
132+
}
133+
#endif
134+
#else
96135
#if FIXED_PARTITION_EXISTS(boot_partition)
97136
if (id == FIXED_PARTITION_ID(boot_partition)) {
98137
shell_error(sh, "Cannot erase boot partition");
@@ -105,6 +144,7 @@ static int cmd_mcuboot_erase(const struct shell *sh, size_t argc,
105144
shell_error(sh, "Cannot erase active partitions");
106145
return -EACCES;
107146
}
147+
#endif
108148
#endif
109149

110150
err = boot_erase_img_bank(id);

0 commit comments

Comments
 (0)