Skip to content

Commit 1fe1f35

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

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

sys/include/riotboot/flashwrite.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int riotboot_flashwrite_init_raw(riotboot_flashwrite_t *state, int target_slot,
150150
* @brief Initialize firmware update (riotboot version)
151151
*
152152
* This function initializes a firmware update, skipping riotboot's magic
153-
* number ("RIOT") by calling @ref riotboot_flashwrite_init_raw() with an
153+
* number by calling @ref riotboot_flashwrite_init_raw() with an
154154
* offset of RIOTBOOT_FLASHWRITE_SKIPLEN (4). This ensures that riotboot will
155155
* ignore the slot until the magic number has been restored, e.g., through @ref
156156
* riotboot_flashwrite_finish().
@@ -163,7 +163,7 @@ int riotboot_flashwrite_init_raw(riotboot_flashwrite_t *state, int target_slot,
163163
static inline int riotboot_flashwrite_init(riotboot_flashwrite_t *state,
164164
int target_slot)
165165
{
166-
/* initialize state, but skip "RIOT" */
166+
/* initialize state, but skip magic number */
167167
return riotboot_flashwrite_init_raw(state, target_slot,
168168
RIOTBOOT_FLASHWRITE_SKIPLEN);
169169
}

sys/include/riotboot/hdr.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* The header contains
1919
*
20-
* - "RIOT" as magic number
20+
* - the magic number
2121
* - the application version
2222
* - the address where the RIOT firmware is found
2323
* - the checksum of the three previous fields
@@ -49,11 +49,13 @@ extern "C" {
4949
*/
5050
#define RIOTBOOT_MAGIC_V2 0x54304952 /* "RI0T" */
5151

52+
#ifndef DOXYGEN
5253
#if defined(MODULE_RIOTBOOT_HDR_V2)
5354
# define RIOTBOOT_MAGIC RIOTBOOT_MAGIC_V2
5455
#else
5556
# define RIOTBOOT_MAGIC RIOTBOOT_MAGIC_V1
5657
#endif
58+
#endif
5759

5860
/**
5961
* @brief Bit shift position of tentative boot counter
@@ -84,6 +86,9 @@ static_assert(CONFIG_RIOTBOOT_MAX_ATTEMPTS > 0,
8486
static_assert(CONFIG_RIOTBOOT_MAX_ATTEMPTS <= 4,
8587
"CONFIG_RIOTBOOT_MAX_ATTEMPTS must be <= 4");
8688

89+
/**
90+
* @brief Image state enumeration
91+
*/
8792
typedef enum riotboot_hdr_img_state {
8893
RIOTBOOT_HDR_IMG_STATE_INSTALLED = 0x0f, /**< Image is installed (1111) */
8994
RIOTBOOT_HDR_IMG_STATE_ACTIVATED = 0x0e, /**< Image is activated (1110) */
@@ -95,7 +100,7 @@ typedef enum riotboot_hdr_img_state {
95100
* @brief Structure to store image header - All members are little endian
96101
*/
97102
struct riotboot_hdr_v1 {
98-
uint32_t magic_number; /**< Header magic number (always "RIOT") */
103+
uint32_t magic_number; /**< Header magic number */
99104
uint32_t version; /**< Integer representing the partition version */
100105
uint32_t start_addr; /**< Address after the allocated space for the header */
101106
uint32_t chksum; /**< Checksum of riotboot_hdr */
@@ -105,7 +110,7 @@ struct riotboot_hdr_v1 {
105110
* @brief Structure to store image header v2 - All members are little endian
106111
*/
107112
struct riotboot_hdr_v2 {
108-
uint32_t magic_number; /**< Header magic number (always "RIOT") */
113+
uint32_t magic_number; /**< Header magic number */
109114
uint32_t version; /**< Integer representing the partition version */
110115
uint32_t start_addr; /**< Address after the allocated space for the header */
111116
uint32_t chksum; /**< Checksum of riotboot_hdr */

sys/riotboot/flashwrite_verify_sha256.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @}
2121
*/
2222

23+
#include "riotboot/hdr.h"
2324
#include <stdint.h>
2425
#include <string.h>
2526

@@ -49,7 +50,8 @@ int riotboot_flashwrite_verify_sha256(const uint8_t *sha256_digest,
4950

5051
/* add RIOTBOOT_MAGIC since it isn't written into flash until
5152
* riotboot_flashwrite_finish()" */
52-
sha256_update(&sha256, "RIOT", 4);
53+
uint32_t magic = RIOTBOOT_MAGIC;
54+
sha256_update(&sha256, &magic, 4);
5355

5456
/* account for injected RIOTBOOT_MAGIC by skipping RIOTBOOT_MAGIC_LEN */
5557
sha256_update(&sha256, img_start + 4, img_len - 4);

sys/suit/storage/flashwrite.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*
1919
* @}
2020
*/
21+
#include "riotboot/hdr.h"
2122
#include <string.h>
2223

2324
#include "architecture.h"
@@ -121,17 +122,16 @@ static int _flashwrite_read(suit_storage_t *storage, uint8_t *buf,
121122
{
122123
suit_storage_flashwrite_t *fw = _get_fw(storage);
123124

124-
static const char _prefix[] = "RIOT";
125-
static const size_t _prefix_len = sizeof(_prefix) - 1;
125+
const uint32_t magic = RIOTBOOT_MAGIC;
126126
int target_slot = riotboot_slot_other();
127127
size_t slot_size = riotboot_slot_size(target_slot);
128128

129-
/* Insert the "RIOT" magic number */
130-
if (offset < (_prefix_len)) {
131-
size_t prefix_to_copy = _prefix_len - offset;
132-
memcpy(buf, _prefix + offset, prefix_to_copy);
129+
/* Insert the magic number */
130+
if (offset < (sizeof(magic))) {
131+
size_t prefix_to_copy = sizeof(magic) - offset;
132+
memcpy(buf, &magic + offset, prefix_to_copy);
133133
len -= prefix_to_copy;
134-
offset = _prefix_len;
134+
offset = sizeof(magic);
135135
buf += prefix_to_copy;
136136

137137
}
@@ -142,7 +142,7 @@ static int _flashwrite_read(suit_storage_t *storage, uint8_t *buf,
142142
* contains the magic number already copied above. */
143143
if (offset < RIOTBOOT_FLASHPAGE_BUFFER_SIZE) {
144144
const size_t chunk_remaining =
145-
RIOTBOOT_FLASHPAGE_BUFFER_SIZE - _prefix_len;
145+
RIOTBOOT_FLASHPAGE_BUFFER_SIZE - sizeof(magic);
146146
/* How much of the first page must be copied */
147147
size_t firstpage_to_copy = len > chunk_remaining ?
148148
(chunk_remaining) : len;

0 commit comments

Comments
 (0)