Skip to content

Commit dbcdc7e

Browse files
committed
fixup! sys/riotboot: riotboot head v2
1 parent 1fe1f35 commit dbcdc7e

File tree

9 files changed

+84
-28
lines changed

9 files changed

+84
-28
lines changed

bootloaders/riotboot/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void kernel_init(void)
4949
continue;
5050
}
5151
state = riotboot_hdr_get_img_state(riot_hdr);
52-
if (state == RIOTBOOT_HDR_IMG_STATE_INSTALLED ||
52+
if (state == RIOTBOOT_HDR_IMG_STATE_DEACTIVATED ||
5353
state == RIOTBOOT_HDR_IMG_STATE_DISMISSED) {
5454
/* skip image which previously failed to boot or is not yet activated */
5555
continue;

dist/tools/riotboot_gen_hdr/genhdr.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ static size_t populate_hdr_v2(riotboot_hdr_t *hdr, uint32_t ver, uint32_t addr)
5050
hdr->v2.version = ver;
5151
hdr->v2.start_addr = addr;
5252
hdr->v2.flags = 0xffffffff;
53-
hdr->v2.flags &= ~RIOTBOOT_HDR_IMAGE_STATE_MASK;
54-
hdr->v2.flags |= (RIOTBOOT_HDR_IMG_STATE_ACTIVATED << RIOTBOOT_HDR_IMAGE_STATE_SHIFT);
5553
/* calculate header checksum */
5654
hdr->v2.chksum = riotboot_hdr_checksum(hdr);
5755
return sizeof(hdr->v2);

examples/advanced/suit_update/main.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "suit/transport/coap.h"
2727
#ifdef MODULE_SUIT_STORAGE_FLASHWRITE
28+
#include "periph/flashpage.h"
2829
#include "riotboot/slot.h"
2930
#endif
3031

@@ -69,19 +70,47 @@ static int cmd_print_riotboot_hdr(int argc, char **argv)
6970
(void)argc;
7071
(void)argv;
7172

72-
int current_slot = riotboot_slot_current();
73-
if (current_slot != -1) {
73+
if (riotboot_slot_current() == -1) {
74+
printf("[FAILED] You're not running riotboot\n");
75+
return -1;
76+
}
77+
if (argc == 1) {
78+
printf("Usage: %s [<slot> [activate|deactivate|confirm|dismiss]]\n", argv[0]);
79+
return 0;
80+
}
81+
int slot = atoi(argv[1]);
82+
if (argc == 2) {
7483
/* Sometimes, udhcp output messes up the following printfs. That
75-
* confuses the test script. As a workaround, just disable interrupts
76-
* for a while.
77-
*/
84+
* confuses the test script. As a workaround, just disable interrupts
85+
* for a while.
86+
*/
7887
unsigned state = irq_disable();
79-
riotboot_slot_print_hdr(current_slot);
88+
riotboot_slot_print_hdr(slot);
8089
irq_restore(state);
90+
return 0;
8191
}
82-
else {
83-
printf("[FAILED] You're not running riotboot\n");
92+
riotboot_hdr_t hdr = *riotboot_slot_get_hdr(slot);
93+
if (riotboot_hdr_is_v2(&hdr)) {
94+
if (!strcmp(argv[2], "activate")) {
95+
riotboot_hdr_set_img_state(&hdr, RIOTBOOT_HDR_IMG_STATE_ACTIVATED);
96+
}
97+
else if (!strcmp(argv[2], "deactivate")) {
98+
riotboot_hdr_set_img_state(&hdr, RIOTBOOT_HDR_IMG_STATE_DEACTIVATED);
99+
}
100+
else if (!strcmp(argv[2], "confirm")) {
101+
riotboot_hdr_set_img_state(&hdr, RIOTBOOT_HDR_IMG_STATE_CONFIRMED);
102+
}
103+
else if (!strcmp(argv[2], "dismiss")) {
104+
riotboot_hdr_set_img_state(&hdr, RIOTBOOT_HDR_IMG_STATE_DISMISSED);
105+
}
106+
else {
107+
printf("Unknown command %s\n", argv[2]);
108+
return -1;
109+
}
110+
flashpage_write(flashpage_addr(flashpage_page(riotboot_slot_get_hdr(slot))),
111+
&hdr.v2, sizeof(hdr.v2));
84112
}
113+
85114
return 0;
86115
}
87116

sys/include/riotboot/hdr.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ static_assert(CONFIG_RIOTBOOT_MAX_ATTEMPTS <= 4,
9090
* @brief Image state enumeration
9191
*/
9292
typedef enum riotboot_hdr_img_state {
93-
RIOTBOOT_HDR_IMG_STATE_INSTALLED = 0x0f, /**< Image is installed (1111) */
94-
RIOTBOOT_HDR_IMG_STATE_ACTIVATED = 0x0e, /**< Image is activated (1110) */
95-
RIOTBOOT_HDR_IMG_STATE_CONFIRMED = 0x0c, /**< Image is confirmed (1100) */
96-
RIOTBOOT_HDR_IMG_STATE_DISMISSED = 0x08, /**< Image is dismissed (1000) */
93+
RIOTBOOT_HDR_IMG_STATE_INSTALLED = 0x0f, /**< Image is installed (1111) */
94+
RIOTBOOT_HDR_IMG_STATE_DEACTIVATED = 0x0e, /**< Image is deactivated (1110) */
95+
RIOTBOOT_HDR_IMG_STATE_ACTIVATED = 0x0c, /**< Image is activated (1100) */
96+
RIOTBOOT_HDR_IMG_STATE_CONFIRMED = 0x08, /**< Image is confirmed (1000) */
97+
RIOTBOOT_HDR_IMG_STATE_DISMISSED = 0x00, /**< Image is dismissed (0000) */
9798
} riotboot_hdr_img_state_t;
9899

99100
/**

sys/include/riotboot/slot.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ void riotboot_slot_dump_addrs(void);
128128
*/
129129
void riotboot_slot_confirm(void);
130130

131+
/**
132+
* @brief Activate or deactivate the other slot
133+
*
134+
* This function sets the image state to ACTIVATED or DEACTIVATED in the header of the
135+
* non-running image slot.
136+
*
137+
* A deactivated slot can be activated but an activated slot cannot be deactivated.
138+
*
139+
* @param[in] active if true, set to ACTIVATED, else set to INACTIVE
140+
*/
141+
void riotboot_slot_activate_other(bool active);
142+
131143
/**
132144
* @brief Get the size of a slot
133145
*

sys/riotboot/hdr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ int riotboot_hdr_set_img_state(riotboot_hdr_t *riotboot_hdr,
186186
{
187187
if (riotboot_hdr_is_v2(riotboot_hdr)) {
188188
int current = riotboot_hdr_get_img_state(riotboot_hdr);
189-
if (current == RIOTBOOT_HDR_IMG_STATE_INSTALLED &&
189+
if (current == RIOTBOOT_HDR_IMG_STATE_DEACTIVATED &&
190190
state != RIOTBOOT_HDR_IMG_STATE_ACTIVATED) {
191191
return -1;
192192
}

sys/riotboot/slot.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,20 @@ void riotboot_slot_confirm(void)
121121
}
122122
}
123123
}
124+
125+
void riotboot_slot_activate_other(bool active)
126+
{
127+
int slot = riotboot_slot_other();
128+
riotboot_hdr_t riot_hdr = *riotboot_slot_get_hdr(slot);
129+
if (riotboot_hdr_is_v2(&riot_hdr)) {
130+
riotboot_hdr_set_img_state(&riot_hdr,
131+
active ? RIOTBOOT_HDR_IMG_STATE_ACTIVATED
132+
: RIOTBOOT_HDR_IMG_STATE_DEACTIVATED);
133+
if (memcmp(&riot_hdr, riotboot_slot_get_hdr(slot), sizeof(riot_hdr.v2))) {
134+
#ifdef MODULE_PERIPH_FLASHPAGE
135+
flashpage_write(flashpage_addr(flashpage_page(riotboot_slot_get_hdr(slot))),
136+
&riot_hdr.v2, sizeof(riot_hdr.v2));
137+
#endif
138+
}
139+
}
140+
}

sys/suit/storage/flashwrite.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,6 @@ static int _flashwrite_install(suit_storage_t *storage,
106106
if (fin < 0) {
107107
return SUIT_ERR_STORAGE;
108108
}
109-
#if IS_USED(MODULE_RIOTBOOT_HDR_AUTO_ACTIVATE)
110-
riotboot_hdr_t hdr = *riotboot_slot_get_hdr(fw->writer.target_slot);
111-
if (riotboot_hdr_is_v2(&hdr)) {
112-
riotboot_hdr_set_img_state(&hdr, RIOTBOOT_HDR_IMG_STATE_ACTIVATED);
113-
flashpage_write(flashpage_addr(flashpage_page(riotboot_slot_get_hdr(fw->writer.target_slot))),
114-
&hdr.v2, sizeof(hdr.v2));
115-
}
116-
#endif
117109
return fin;
118110
}
119111

sys/suit/transport/worker.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,21 @@ int suit_handle_manifest_buf(const uint8_t *buffer, size_t size)
142142

143143
if ((res = riotboot_hdr_validate(hdr)) < 0) {
144144
LOG_INFO("suit_worker: target slot header invalid\n");
145-
res = -EINVAL;
145+
return -EINVAL;
146146
}
147-
else if (riotboot_hdr_get_flags(hdr) != UINT32_MAX) {
147+
if (riotboot_hdr_get_flags(hdr) != UINT32_MAX) {
148148
LOG_INFO("suit_worker: target slot header flags invalid\n");
149-
res = -EINVAL;
149+
return -EINVAL;
150+
}
151+
if (IS_USED(MODULE_RIOTBOOT_HDR_AUTO_ACTIVATE)) {
152+
LOG_INFO("suit_worker: activating target slot\n");
153+
riotboot_slot_activate_other(true);
154+
}
155+
else {
156+
LOG_INFO("suit_worker: deactivating target slot\n");
157+
riotboot_slot_activate_other(false);
150158
}
151159
#endif
152-
153160
return res;
154161
}
155162

0 commit comments

Comments
 (0)