CycloneBOOT 2.6.6
Secure Bootloader for ARM Cortex-M MCU
Loading...
Searching...
No Matches
mailbox.h
Go to the documentation of this file.
1
31#ifndef _MAILBOX_H
32#define _MAILBOX_H
33
34// Dependencies
35#include <stddef.h>
36#include "boot_config.h"
37#include "core/cboot_error.h"
38#include "image/image_format.h"
39#include "debug.h"
40#include "os_port.h"
41
42// Bootloader mailbox version
43#define BOOT_MBX_VERSION (uint32_t) 0x00010001
44
45// Bootloader mailbox signture
46#define BOOT_MBX_SIGNATURE 0x1b241671
47
48// Bootloader mailbox flags
49enum
50{
51 UPDATE_REQUESTED = 1 << 0,
52 UPDATE_PERFORMED = 1 << 1,
53 UPDATE_CONFIRMED = 1 << 2,
54 FALLBACK_REQUESTED = 1 << 3,
55 FALLBACK_PERFORMED = 1 << 4,
56 FALLBACK_AVAILABLE = 1 << 5,
57 REPAIR_PERFORMED = 1 << 6
58};
59
60// Bootloader mailbox NVM flags
61#define BOOT_MBX_NVM_MAGIC 0xBEEF
62#define BOOT_MBX_NVM_FLAG_UPDATE_REQUESTED 0xF00D
63#define BOOT_MBX_NVM_FLAG_UPDATE_PERFORMED 0xC0DE
64#define BOOT_MBX_NVM_FLAG_UPDATE_CONFIRMED 0xFEED
65#define BOOT_MBX_NVM_FLAG_FALLBACK_REQUESTED 0xB105
66#define BOOT_MBX_NVM_FLAG_FALLBACK_PERFORMED 0xDEAD
67#define BOOT_MBX_NVM_FLAG_FALLBACK_AVAILABLE 0x0BAC
68#define BOOT_MBX_NVM_FLAG_REPAIR_PERFORMED 0xB007
69
70// Bootloader mailbox maximum PSK size
71#define BOOT_MBX_PSK_MAX_SIZE 32
72
73// Portable packed-struct attributes (GCC/Clang/ARM-style attribute vs MSVC pragma).
74#if defined(_MSC_VER)
75 #define BOOT_MBX_PACK_BEGIN __pragma(pack(push, 1))
76 #define BOOT_MBX_PACK_END __pragma(pack(pop))
77 #define BOOT_MBX_PACK_ATTR
78#elif defined(__GNUC__) || defined(__clang__) || defined(__ARMCC_VERSION) || \
79 defined(__CC_ARM) || defined(__IAR_SYSTEMS_ICC__) || defined(__TASKING__)
80 #define BOOT_MBX_PACK_BEGIN
81 #define BOOT_MBX_PACK_END
82 #define BOOT_MBX_PACK_ATTR __attribute__((packed))
83#else
84 #define BOOT_MBX_PACK_BEGIN
85 #define BOOT_MBX_PACK_END
86 #define BOOT_MBX_PACK_ATTR
87#endif
88
92BOOT_MBX_PACK_BEGIN
93typedef struct
94{
95 uint32_t version;
96 uint32_t signature;
97 uint32_t pskSize;
98 uint8_t psk[BOOT_MBX_PSK_MAX_SIZE];
99 uint32_t flags;
100 uint32_t boot_counter;
101 ImageVersion fsbVersion;
102 ImageVersion ssbVersion;
103 uint8_t reserved[64];
104} BOOT_MBX_PACK_ATTR BootMailBox;
105BOOT_MBX_PACK_END
106
107// Portable compile-time assert (C11 _Static_assert / C++11 static_assert / a
108// negative-array-size fallback for older or non-GCC C compilers).
109#if defined(__cplusplus) && (__cplusplus >= 201103L)
110 #define BOOT_MBX_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
111#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
112 #define BOOT_MBX_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
113#else
114 #define BOOT_MBX_STATIC_ASSERT_CAT_(a, b) a ## b
115 #define BOOT_MBX_STATIC_ASSERT_CAT(a, b) BOOT_MBX_STATIC_ASSERT_CAT_(a, b)
116 #define BOOT_MBX_STATIC_ASSERT(cond, msg) \
117 typedef char BOOT_MBX_STATIC_ASSERT_CAT(boot_mbx_static_assert_, __LINE__)[(cond) ? 1 : -1]
118#endif
119
120// Use the same rules as image_format.h, to add backward compatible fields to the structure
121BOOT_MBX_STATIC_ASSERT(sizeof(BootMailBox) == 132, "BootMailBox RAM layout changed (must stay 132 bytes)");
122BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, version) == 0, "BootMailBox.version offset changed");
123BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, signature) == 4, "BootMailBox.signature offset changed");
124BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, pskSize) == 8, "BootMailBox.pskSize offset changed");
125BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, psk) == 12, "BootMailBox.psk offset changed");
126BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, flags) == 44, "BootMailBox.flags offset changed");
127BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, boot_counter) == 48, "BootMailBox.boot_counter offset changed");
128BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, fsbVersion) == 52, "BootMailBox.fsbVersion offset changed");
129BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, ssbVersion) == 60, "BootMailBox.ssbVersion offset changed");
130BOOT_MBX_STATIC_ASSERT(offsetof(BootMailBox, reserved) == 68, "BootMailBox.reserved offset changed");
131
132// C++ guard
133#ifdef __cplusplus
134extern "C"
135{
136#endif
137
138// CycloneBOOT IAP Mailbox functions
140
141bool_t mailBoxIsUpdateRequested(void);
142void mailBoxSetUpdateRequested(bool_t state);
143
144bool_t mailBoxIsUpdatePerformed(void);
145void mailBoxSetUpdatePerformed(bool_t state);
146
147bool_t mailBoxIsUpdateConfirmed(void);
148void mailBoxSetUpdateConfirmed(bool_t state);
149
150bool_t mailBoxIsFallbackRequested(void);
151bool_t mailBoxIsFallbackPerformed(void);
152void mailBoxSetFallbackRequested(bool_t state);
153void mailBoxSetFallbackPerformed(bool_t state);
154
155bool_t mailBoxIsFallbackAvailable(void);
156void mailBoxSetFallbackAvailable(bool_t state);
157
158bool_t mailBoxIsRepairPerformed(void);
159void mailBoxSetRepairPerformed(bool_t state);
160
161void mailBoxSetFirstStageVersion(const ImageVersion *version);
162const ImageVersion *mailBoxGetFirstStageVersion(void);
163void mailBoxSetSecondStageVersion(const ImageVersion *version);
164const ImageVersion *mailBoxGetSecondStageVersion(void);
165
167uint32_t mailBoxGetBootCounter(void);
168void mailBoxClearBootCounter(void);
169
170void mailBoxSetPsk(const uint8_t *psk, size_t pskSize);
171void mailBoxGetPsk(uint8_t *psk, size_t *pskSize);
172void mailBoxClearPsk(void);
173
174// Define weakly defined function to place boot flags in NVM
175#if ((defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) || defined(__GNUC__) || \
176 defined(__CC_ARM) || defined(__IAR_SYSTEMS_ICC__) || defined(__TASKING__) || \
177 defined(__CWCC__) || defined(__TI_ARM__))
178cboot_error_t mbx_init_nvm(void);
179cboot_error_t mbx_set_nvm_flag(uint32_t flag, bool_t value);
180bool_t mbx_get_nvm_flag(uint32_t flag);
181#elif ((defined(_MSC_VER) || defined(_WIN32) || defined(WIN32) || defined(WIN64) || \
182 defined(__unix__)))
183extern cboot_error_t mbx_init_nvm(void);
184extern cboot_error_t mbx_set_nvm_flag(uint32_t flag, bool_t value);
185extern bool_t mbx_get_nvm_flag(uint32_t flag);
186#endif
187
188// C++ guard
189#ifdef __cplusplus
190}
191#endif
192#endif
Error codes description.
cboot_error_t
Error codes.
Definition cboot_error.h:57
CycloneBOOT on-flash image format.
bool_t mailBoxIsUpdatePerformed(void)
Checks whether UPDATE_PERFORMED message is present in the mailbox.
Definition mailbox.c:194
bool_t mailBoxIsUpdateConfirmed(void)
Checks whether UPDATE_CONFIRMED message is present in the mailbox.
Definition mailbox.c:210
void mailBoxSetFallbackAvailable(bool_t state)
Posts FALLBACK_AVAILABLE message to the mailbox.
Definition mailbox.c:268
void mailBoxSetFallbackPerformed(bool_t state)
Posts FALLBACK_PERFORMED message to the mailbox.
Definition mailbox.c:250
bool_t mailBoxIsUpdateRequested(void)
Checks whether UPDATE_REQUESTED message is present in the mailbox.
Definition mailbox.c:178
void mailBoxSetFallbackRequested(bool_t state)
Posts FALLBACK_REQUESTED message to the mailbox.
Definition mailbox.c:234
void mailBoxSetFirstStageVersion(const ImageVersion *version)
Publish the first-stage version for later stages (HDP/HDPL-safe).
Definition mailbox.c:292
void mailBoxClearBootCounter(void)
Clears the current value of the boot counter in the mailbox.
Definition mailbox.c:343
void mailBoxSetPsk(const uint8_t *psk, size_t pskSize)
Set Pre-Shared Key value of the mailbox.
Definition mailbox.c:352
bool_t mailBoxIsFallbackPerformed(void)
Checks whether FALLBACK_PERFORMED message is present in the mailbox.
Definition mailbox.c:242
void mailBoxSetSecondStageVersion(const ImageVersion *version)
Publish the second-stage version for the application to read.
Definition mailbox.c:310
bool_t mailBoxIsRepairPerformed(void)
Tells whether the bootloader already reinstalled a candidate to repair the application.
Definition mailbox.c:276
const ImageVersion * mailBoxGetFirstStageVersion(void)
Get the first-stage version published in the mailbox.
Definition mailbox.c:302
void mailBoxSetRepairPerformed(bool_t state)
Posts REPAIR_PERFORMED message to the mailbox.
Definition mailbox.c:284
cboot_error_t mailBoxInit(void)
Initializes the shared bootloader mailbox if signature is not valid. The NVM flag storage is initiali...
Definition mailbox.c:151
void mailBoxSetUpdatePerformed(bool_t state)
Posts UPDATE_PERFORMED message to the mailbox.
Definition mailbox.c:202
void mailBoxClearPsk(void)
Clear the Pre-Shared Key from the mailbox.
Definition mailbox.c:389
void mailBoxGetPsk(uint8_t *psk, size_t *pskSize)
Returns a copy of Pre-Shared Key value of the mailbox.
Definition mailbox.c:373
bool_t mailBoxIsFallbackAvailable(void)
Checks whether a fall back to a previous firmware is possible.
Definition mailbox.c:260
void mailBoxSetUpdateRequested(bool_t state)
Posts UPDATE_REQUESTED message to the mailbox.
Definition mailbox.c:186
bool_t mailBoxIsFallbackRequested(void)
Checks whether FALLBACK_REQUESTED message is present in the mailbox.
Definition mailbox.c:226
void mailBoxIncrementBootCounter(void)
Increments the boot counter in the mailbox.
Definition mailbox.c:329
uint32_t mailBoxGetBootCounter(void)
Returns the current value of the boot counter in the mailbox.
Definition mailbox.c:336
const ImageVersion * mailBoxGetSecondStageVersion(void)
Get the second-stage version published in the mailbox.
Definition mailbox.c:320
void mailBoxSetUpdateConfirmed(bool_t state)
Posts UPDATE_REQUESTED message to the mailbox.
Definition mailbox.c:218
Bootloader shared RAM mailbox structure.
Definition mailbox.h:94
uint32_t version
Bootloader Mailbox version.
Definition mailbox.h:95
ImageVersion fsbVersion
First-stage version, published forward by the 1st stage.
Definition mailbox.h:101
uint32_t signature
Bootloader Mailbox signature.
Definition mailbox.h:96
ImageVersion ssbVersion
Second-stage version, published forward by the 2nd stage.
Definition mailbox.h:102
uint32_t boot_counter
Bootloader Mailbox boot counter.
Definition mailbox.h:100
uint32_t pskSize
Bootloader Mailbox PSK size.
Definition mailbox.h:97
uint32_t flags
Bootloader Mailbox event flags.
Definition mailbox.h:99