Skip to content

Commit cedde21

Browse files
committed
Use 1 byte for signature type instead of magic key
1 parent 9b277e8 commit cedde21

File tree

5 files changed

+67
-51
lines changed

5 files changed

+67
-51
lines changed

Bootloader/Inc/binary_update.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
typedef struct bootInfo {
4444
uint32_t jump_address; //!< Address for BL to jump
4545
bool skip_bl_loop; //!< Flag to skip BL loop
46-
detectedBinary_E previus_binary; //!< Previous detected binary
46+
signatureType_E previus_binary; //!< Previous detected binary
4747
} bootInfo_S;
4848
#pragma pack(pop)
4949

50-
bool BinaryUpdate_handleDetectedBinary(detectedBinary_E detected_binary);
50+
bool BinaryUpdate_handleDetectedBinary(signatureType_E detected_binary);
5151
void BinaryUpdate_handleBootInfo(void);
5252
uint32_t BinaryUpdate_getJumpAddress(void);
5353
void BinaryUpdate_resetJumpAddress(void);

Bootloader/Inc/signature.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,22 @@
4040
#define SIGNATURE_SIZE 64 //!< Signature size is 64 bytes
4141

4242
typedef struct signature {
43-
uint64_t magic_key; //!< First 8 bytes for the magic key
44-
uint64_t unused[7]; //!< The rest of the 56 bytes are reserved for future use
43+
uint64_t magic_key; //!< First 8 bytes for the magic key
44+
uint64_t type : 8; //!< Type of binary file
45+
uint64_t encrypted : 1; //!< Indicate if file is encrypted (file only, not communication)
46+
uint64_t reserved_1 : 55; //!< The rest of bits are reserved for future use
47+
uint64_t reserved_2[6]; //!< The rest of the 48 bytes are reserved for future use
4548
} signature_S;
4649

4750
//! Enumeration for different signatures
48-
typedef enum detectedBinary_ENUM {
49-
detectedBinary_FIRMWARE_FLASH, //!< New firmware for FLASH
50-
detectedBinary_FIRMWARE_RAM, //!< Firmware for RAM
51-
detectedBinary_BOOTLOADER_FLASH, //!< New bootloader for FLASH
52-
detectedBinary_BOOTLOADER_RAM, //!< Bootloader for RAM
53-
detectedBinary_UNKNOWN, //!< Not existing or unknown signature
54-
} detectedBinary_E;
51+
typedef enum signatureType_ENUM {
52+
signatureType_FIRMWARE_FLASH = 0x00, //!< New firmware for FLASH
53+
signatureType_FIRMWARE_RAM = 0x01, //!< Firmware for RAM
54+
signatureType_BOOTLOADER_FLASH = 0x02, //!< New bootloader for FLASH
55+
signatureType_BOOTLOADER_RAM = 0x03, //!< Bootloader for RAM
56+
signatureType_UNKNOWN = 0xFF, //!< Not existing or unknown signature
57+
} signatureType_E __attribute__ ((__packed__));
5558

56-
detectedBinary_E Signature_verification(const signature_S* signature);
59+
signatureType_E Signature_verification(const signature_S* signature);
5760

5861
#endif /* BOOTLOADER_INC_SIGNATURE_H_ */

Bootloader/Src/binary_update.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,32 @@
4242
__attribute__ ((section(".restart_info")))
4343
bootInfo_S boot_info; //!< Instruction on where to jump after the restart
4444
static uint64_t s_address; //!< Address from where to erase flash and write binary
45-
static detectedBinary_E s_detected_binary; //!< Detected binary
45+
static signatureType_E s_detected_binary; //!< Detected binary
4646

4747
static bool BinaryUpdate_writeToFlash(uint8_t* write_buffer, const uint32_t data_length);
4848

4949
bool
50-
BinaryUpdate_handleDetectedBinary(detectedBinary_E detected_binary) {
50+
BinaryUpdate_handleDetectedBinary(signatureType_E detected_binary) {
5151

5252
bool success = true;
5353
s_detected_binary = detected_binary;
5454

5555
switch (detected_binary) {
5656

57-
case detectedBinary_FIRMWARE_FLASH:
57+
case signatureType_FIRMWARE_FLASH:
5858
s_address = FLASH_FIRMWARE_ADDRESS;
5959
break;
6060

61-
case detectedBinary_BOOTLOADER_FLASH:
61+
case signatureType_BOOTLOADER_FLASH:
6262
s_address = FLASH_BOOTLOADER_ADDRESS;
6363
break;
6464

65-
case detectedBinary_FIRMWARE_RAM:
66-
case detectedBinary_BOOTLOADER_RAM:
65+
case signatureType_FIRMWARE_RAM:
66+
case signatureType_BOOTLOADER_RAM:
6767
s_address = RAM_FIRMWARE_ADDRESS;
6868
break;
6969

70-
case detectedBinary_UNKNOWN:
70+
case signatureType_UNKNOWN:
7171
//we support unsigned binary but handle it as firmware for flash
7272
success = false;
7373
s_address = FLASH_FIRMWARE_ADDRESS;
@@ -93,7 +93,7 @@ BinaryUpdate_handleBootInfo(void) {
9393
default:
9494
boot_info.jump_address = FLASH_FIRMWARE_ADDRESS;
9595
boot_info.skip_bl_loop = false;
96-
boot_info.previus_binary = detectedBinary_FIRMWARE_FLASH;
96+
boot_info.previus_binary = signatureType_FIRMWARE_FLASH;
9797
break;
9898
}
9999
}
@@ -105,7 +105,7 @@ BinaryUpdate_getJumpAddress(void) {
105105

106106
void
107107
BinaryUpdate_resetJumpAddress(void) {
108-
boot_info.jump_address = detectedBinary_FIRMWARE_FLASH;;
108+
boot_info.jump_address = signatureType_FIRMWARE_FLASH;;
109109
}
110110

111111
bool
@@ -124,21 +124,21 @@ BinaryUpdate_erase(uint32_t firmware_size) {
124124
bool success = true;
125125
switch (s_detected_binary) {
126126

127-
case detectedBinary_FIRMWARE_FLASH:
127+
case signatureType_FIRMWARE_FLASH:
128128
success = FlashAdapter_erase(firmware_size, s_address);
129129
break;
130-
case detectedBinary_BOOTLOADER_FLASH:
131-
if (detectedBinary_BOOTLOADER_RAM == boot_info.previus_binary) {
130+
case signatureType_BOOTLOADER_FLASH:
131+
if (signatureType_BOOTLOADER_RAM == boot_info.previus_binary) {
132132
//Only allowed to erase if RAM version is running
133133
success = FlashAdapter_erase(firmware_size, s_address);
134134
}
135135
break;
136136

137-
case detectedBinary_FIRMWARE_RAM:
138-
case detectedBinary_BOOTLOADER_RAM:
137+
case signatureType_FIRMWARE_RAM:
138+
case signatureType_BOOTLOADER_RAM:
139139
break;
140140

141-
case detectedBinary_UNKNOWN:
141+
case signatureType_UNKNOWN:
142142
success = FlashAdapter_erase(firmware_size, s_address);
143143
break;
144144

@@ -202,28 +202,28 @@ BinaryUpdate_finish(void) {
202202

203203
switch (s_detected_binary) {
204204

205-
case detectedBinary_FIRMWARE_FLASH:
205+
case signatureType_FIRMWARE_FLASH:
206206
boot_info.jump_address = FLASH_FIRMWARE_ADDRESS;
207207
boot_info.skip_bl_loop = false;
208208
success = FlashAdapter_finish();
209209
break;
210210

211-
case detectedBinary_FIRMWARE_RAM:
211+
case signatureType_FIRMWARE_RAM:
212212
boot_info.jump_address = RAM_FIRMWARE_ADDRESS;
213213
boot_info.skip_bl_loop = false;
214214
break;
215215

216-
case detectedBinary_BOOTLOADER_FLASH:
216+
case signatureType_BOOTLOADER_FLASH:
217217
boot_info.jump_address = FLASH_FIRMWARE_ADDRESS;
218218
boot_info.skip_bl_loop = false;
219219
break;
220220

221-
case detectedBinary_BOOTLOADER_RAM:
221+
case signatureType_BOOTLOADER_RAM:
222222
boot_info.jump_address = RAM_FIRMWARE_ADDRESS;
223223
boot_info.skip_bl_loop = true;
224224
break;
225225

226-
case detectedBinary_UNKNOWN:
226+
case signatureType_UNKNOWN:
227227
boot_info.jump_address = FLASH_FIRMWARE_ADDRESS;
228228
boot_info.skip_bl_loop = false;
229229
success = FlashAdapter_finish();

Bootloader/Src/communication.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Communication_handler(uint8_t* buf, uint32_t length) {
243243
case communicationState_CHECK_SIGNATURE: {
244244
signature_S signature;
245245
(void*)memcpy((void*)&signature, (void*)buf, SIGNATURE_SIZE);
246-
detectedBinary_E binary_detected = Signature_verification(&signature);
246+
signatureType_E binary_detected = Signature_verification(&signature);
247247
success = BinaryUpdate_handleDetectedBinary(binary_detected);
248248
s_update_state = communicationState_CMD_ACTION_SELECT;
249249

Bootloader/Src/signature.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,45 @@
3434
#include <string.h>
3535
#include "signature.h"
3636

37-
static const uint64_t fw_flash_signature_magic_key = 0xDEC0DE5528101987; //!< First 8 signature bytes of firmware for flash
38-
static const uint64_t fw_ram_signature_magic_key = 0xDEC0DE5523041858; //!< First 8 signature bytes of firmware for ram
39-
static const uint64_t bl_flash_signature_magic_key = 0xDEC0DE5510071856; //!< First 8 signature bytes of bootloader for flash
40-
static const uint64_t bl_ram_signature_magic_key = 0xDEC0DE5513061831; //!< First 8 signature bytes of bootloader for ram
37+
static const uint64_t signature_magic_key = 0xDEC0DE5528101987; //!< First 8 signature bytes of firmware for flash
4138

42-
__attribute__ ((section(".bl_flash_signature"))) signature_S bl_flash_signature = {.magic_key = bl_flash_signature_magic_key};
43-
__attribute__ ((section(".bl_ram_signature"))) signature_S bl_ram_signature = {.magic_key = bl_ram_signature_magic_key};
39+
__attribute__ ((section(".bl_flash_signature"))) signature_S bl_flash_signature = {
40+
.magic_key = signature_magic_key,
41+
.type = signatureType_BOOTLOADER_FLASH
42+
};
4443

45-
detectedBinary_E
46-
Signature_verification(const signature_S* signature) {
44+
__attribute__ ((section(".bl_ram_signature"))) signature_S bl_ram_signature = {
45+
.magic_key = signature_magic_key,
46+
.type = signatureType_BOOTLOADER_RAM
47+
};
4748

48-
detectedBinary_E detected_binary;
49+
signatureType_E
50+
Signature_verification(const signature_S* signature) {
4951

50-
if (0 == memcmp(&(signature->magic_key), &fw_flash_signature_magic_key, sizeof(fw_flash_signature_magic_key))) {
51-
detected_binary = detectedBinary_FIRMWARE_FLASH;
52+
signatureType_E detected_binary;
5253

53-
} else if (0 == memcmp(&(signature->magic_key), &fw_ram_signature_magic_key, sizeof(fw_ram_signature_magic_key))) {
54-
detected_binary = detectedBinary_FIRMWARE_RAM;
54+
if (0 == memcmp(&(signature->magic_key), &signature_magic_key, sizeof(signature_magic_key))) {
5555

56-
} else if (0 == memcmp(&(signature->magic_key), &bl_flash_signature_magic_key, sizeof(bl_flash_signature_magic_key))) {
57-
detected_binary = detectedBinary_BOOTLOADER_FLASH;
56+
switch (signature->type) {
57+
case signatureType_FIRMWARE_FLASH:
58+
detected_binary = signatureType_FIRMWARE_FLASH;
59+
break;
60+
case signatureType_FIRMWARE_RAM:
61+
detected_binary = signatureType_FIRMWARE_RAM;
62+
break;
63+
case signatureType_BOOTLOADER_FLASH:
64+
detected_binary = signatureType_BOOTLOADER_FLASH;
65+
break;
66+
case signatureType_BOOTLOADER_RAM:
67+
detected_binary = signatureType_BOOTLOADER_RAM;
68+
break;
69+
default:
70+
detected_binary = signatureType_UNKNOWN;
71+
break;
72+
}
5873

59-
} else if (0 == memcmp(&(signature->magic_key), &bl_ram_signature_magic_key, sizeof(bl_ram_signature_magic_key))) {
60-
detected_binary = detectedBinary_BOOTLOADER_RAM;
6174
} else {
62-
detected_binary = detectedBinary_UNKNOWN;
75+
detected_binary = signatureType_UNKNOWN;
6376
}
6477

6578
return detected_binary;

0 commit comments

Comments
 (0)