CycloneBOOT 2.6.6
Secure Bootloader for ARM Cortex-M MCU
Loading...
Searching...
No Matches
boot.h
Go to the documentation of this file.
1
31#ifndef _FIRST_STAGE_BOOT_H
32#define _FIRST_STAGE_BOOT_H
33
34#include "core/mcu.h"
35#include "core/crc32.h"
36#include "core/flash.h"
37#include "core/cboot_error.h"
38#include "image/image.h"
40#include "debug.h"
41
42#ifndef BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT
43#define BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT DISABLED
44#endif
45
46#if (BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT != ENABLED) && \
47 (BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT != DISABLED)
48#error "BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT must be ENABLED or DISABLED"
49#endif
50
51#ifndef BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT
52#define BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT DISABLED
53#endif
54
55#if (BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT != ENABLED) && \
56 (BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT != DISABLED)
57#error "BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT must be ENABLED or DISABLED"
58#endif
59
60#ifndef BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_ECDSA
61#define BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_ECDSA DISABLED
62#endif
63#ifndef BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_RSA
64#define BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_RSA DISABLED
65#endif
66
67// A candidate embeds EITHER a hash digest OR a signature in the runtime field, never
68// both, so the two checks are mutually exclusive.
69#if (BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT == ENABLED) && \
70 (BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT == ENABLED)
71#error "Enable only one of BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT or _RUNTIME_SIGNATURE_SUPPORT"
72#endif
73
74// Runtime integrity needs a hash algorithm, for example SHA256_HASH_ALGO
75#if (BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT == ENABLED) && \
76 !defined(BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_ALGO)
77#error "Define BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_ALGO when BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT is ENABLED"
78#endif
79
80// When signature support is on, exactly one scheme must be selected.
81#if (BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT == ENABLED) && \
82 ((BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_ECDSA == ENABLED) == \
83 (BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_RSA == ENABLED))
84#error "Enable exactly one of BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_ECDSA or _RSA"
85#endif
86
87// The first stage refuses a second-stage candidate whose firmware version is not
88// newer than the resident one, mirroring the second stage's own anti-rollback.
89#ifndef BOOT_ANTI_ROLLBACK_SUPPORT
90#define BOOT_ANTI_ROLLBACK_SUPPORT DISABLED
91#elif ((BOOT_ANTI_ROLLBACK_SUPPORT != ENABLED) && (BOOT_ANTI_ROLLBACK_SUPPORT != DISABLED))
92#error BOOT_ANTI_ROLLBACK_SUPPORT parameter is not valid
93#endif
94
95// A candidate staged in external memory is held encrypted, so the first stage
96// must decrypt it while installing.
97#ifndef BOOT_EXT_MEM_ENCRYPTION_SUPPORT
98#define BOOT_EXT_MEM_ENCRYPTION_SUPPORT DISABLED
99#elif ((BOOT_EXT_MEM_ENCRYPTION_SUPPORT != ENABLED) && \
100 (BOOT_EXT_MEM_ENCRYPTION_SUPPORT != DISABLED))
101#error BOOT_EXT_MEM_ENCRYPTION_SUPPORT parameter is not valid
102#endif
103
104// Check verified boot settings
105
106// Refuse a build that would hand over to an unverified second stage
107#if (BOOT_FIRST_STAGE_RUNTIME_INTEGRITY_SUPPORT == DISABLED) && \
108 (BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT == DISABLED) && \
109 !defined(BOOT_FIRST_STAGE_ALLOW_UNVERIFIED_SECOND_STAGE)
110#error "The first stage would accept any second stage passing a CRC32 check. Enable \
111 BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT (or _INTEGRITY_), or define \
112 BOOT_FIRST_STAGE_ALLOW_UNVERIFIED_SECOND_STAGE to accept this."
113#endif
114
118typedef struct {
119 Memory memories[NB_MEMORIES]; //Memories and their slots (driver carried per memory)
120 uint32_t self_address; //Address of the first-stage bootloader's own slot
121 uint32_t self_size; //Size of the first-stage bootloader's own slot
122 uint32_t offset; //Jump offset applied when handing off to the second stage
123#if (BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT == ENABLED)
124 const char_t *signKey; //PEM public key used to verify the 2nd-stage candidate signature
125 size_t signKeyLen; //Length of the PEM public key
126#endif
127#if (BOOT_EXT_MEM_ENCRYPTION_SUPPORT == ENABLED)
128 const uint8_t *cipherKey; //Key decrypting a candidate staged in external memory
129 size_t cipherKeyLen; //Length of the cipher key
130#endif
132
136typedef struct {
137 Memory memories[NB_MEMORIES]; //Working copy of the memories/slots
138 uint32_t self_address; //Address of the first-stage bootloader's own slot
139 uint32_t self_size; //Size of the first-stage bootloader's own slot
140 uint32_t offset; //Jump offset applied when handing off to the second stage
141 Slot *updateSlot; //Selected newer second-stage candidate slot (NULL if none)
142#if (BOOT_FIRST_STAGE_RUNTIME_SIGNATURE_SUPPORT == ENABLED)
143 const char_t *signKey; //PEM public key used to verify the 2nd-stage candidate signature
144 size_t signKeyLen; //Length of the PEM public key
145#endif
146#if (BOOT_EXT_MEM_ENCRYPTION_SUPPORT == ENABLED)
147 const uint8_t *cipherKey; //Key decrypting a candidate staged in external memory
148 size_t cipherKeyLen; //Length of the cipher key
149#endif
151
155cboot_error_t getSecondStageBlIndex(BootContext *context, uint32_t *index);
156cboot_error_t checkForSecondStageBlUpdate(BootContext *context, uint32_t currentIndex,
157 bool_t residentOk, bool_t *updateFound);
159
160// Resolve the active second-stage (BOOT) slot in the primary memory.
162
163void jumpToSecondStageBl(BootContext *context);
164
165#if ((defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) || defined(__GNUC__) || \
166 defined(__CC_ARM) || defined(__IAR_SYSTEMS_ICC__) || defined(__TASKING__) || \
167 defined(__CWCC__) || defined(__TI_ARM__))
168void bootHandleError(void);
169void bootSecondStageUpdatedHook(void);
170void bootJumpingToSecondStageHook(void);
171void bootRecoveryHook(void);
172#elif ((defined(_MSC_VER) || defined(_WIN32) || defined(WIN32) || defined(WIN64) || \
173 defined(__unix__)))
174extern void bootHandleError(void);
175extern void bootSecondStageUpdatedHook(void);
176extern void bootJumpingToSecondStageHook(void);
177extern void bootRecoveryHook(void);
178#endif
179
180#endif
__weak_func cboot_error_t bootRecoveryHook(RecoveryReason reason, RecoveryAction *action)
Recovery action callback. It is declared weak and may be replaced in the user code space....
Definition boot_recovery.c:101
Error codes description.
cboot_error_t
Error codes.
Definition cboot_error.h:57
CycloneBOOT CRC32 cryptographic integrity tag.
cboot_error_t updateSecondStageBl(BootContext *context)
Install the 2nd stage bootloader update.
Definition boot.c:606
void jumpToSecondStageBl(BootContext *context)
Branching to the 2nd stage bootloader.
Definition boot.c:410
cboot_error_t bootFsm(BootContext *context)
Bootloader Task routine.
Definition boot.c:233
cboot_error_t checkForSecondStageBlUpdate(BootContext *context, uint32_t currentIndex, bool_t residentOk, bool_t *updateFound)
Iterates over the available download slots.
Definition boot.c:434
cboot_error_t bootInit(BootContext *context, BootSettings *settings)
Initialize the first-stage bootloader context from user settings.
Definition boot.c:131
void bootGetDefaultSettings(BootSettings *settings)
Initialize first-stage bootloader settings with default values.
Definition boot.c:55
cboot_error_t getSecondStageBlIndex(BootContext *context, uint32_t *index)
Get the image index of the second stage bootloader.
Definition boot.c:364
Slot * bootGetActiveSlot(BootContext *context)
Resolve the active second-stage (BOOT) slot in the primary memory.
Definition boot.c:174
CycloneBOOT Flash layer.
CycloneBOOT Image management.
CycloneBOOT MCU layer.
CycloneBOOT Memory Layer Abstraction.
First-stage bootloader context.
Definition boot.h:136
First-stage bootloader user settings.
Definition boot.h:118
Memory Definition.
Definition memory.h:225
Slot Type definition.
Definition memory.h:181