CycloneBOOT 2.6.6
Secure Bootloader for ARM Cortex-M MCU
Loading...
Searching...
No Matches
image.h
Go to the documentation of this file.
1
31#ifndef _IMAGE_H
32#define _IMAGE_H
33
34// Dependencies
35#include "boot_config.h"
36#include "compiler_port.h"
37#include "core/cboot_error.h"
38#include "image_format.h"
39#include "core/crypto.h"
40#include "memory/memory.h"
41#include "core/verify.h"
42
43/*
44 * CycloneBOOT Open is licensed under GPL version 2. In particular:
45 *
46 * - If you link your program to CycloneBOOT Open, the result is a derivative
47 * work that can only be distributed under the same GPL license terms.
48 *
49 * - If additions or changes to CycloneBOOT Open are made, the result is a
50 * derivative work that can only be distributed under the same license terms.
51 *
52 * - The GPL license requires that you make the source code available to
53 * whoever you make the binary available to.
54 *
55 * - If you sell or distribute a hardware product that runs CycloneBOOT Open,
56 * the GPL license requires you to provide public and full access to all
57 * source code on a nondiscriminatory basis.
58 *
59 * If you fully understand and accept the terms of the GPL license, then edit
60 * the os_port_config.h header and add the following directive:
61 *
62 * #define GPL_LICENSE_TERMS_ACCEPTED
63 */
64
65#ifndef GPL_LICENSE_TERMS_ACCEPTED
66 #error Before compiling CycloneBOOT Open, you must accept the terms of the GPL license
67#endif
68
69// Image input encryption support
70#ifndef IMAGE_INPUT_ENCRYPTED
71#define IMAGE_INPUT_ENCRYPTED DISABLED
72#elif (IMAGE_INPUT_ENCRYPTED != ENABLED && IMAGE_INPUT_ENCRYPTED != DISABLED)
73#error IMAGE_INPUT_ENCRYPTED parameter is not valid!
74#endif
75
76// Image output encryption support (for encrypted external memory if needed)
77#ifndef IMAGE_OUTPUT_ENCRYPTED
78#define IMAGE_OUTPUT_ENCRYPTED DISABLED
79#elif (IMAGE_OUTPUT_ENCRYPTED != ENABLED && IMAGE_OUTPUT_ENCRYPTED != DISABLED)
80#error IMAGE_OUTPUT_ENCRYPTED parameter is not valid!
81#endif
82
83// Acceptable input or output image encryption support
84#if (((IMAGE_OUTPUT_ENCRYPTED == ENABLED) || (IMAGE_INPUT_ENCRYPTED == ENABLED)) && \
85 (CIPHER_SUPPORT == DISABLED))
86#error CIPHER_SUPPORT MUST be ENABLED if IMAGE_INPUT_ENCRYPTED or IMAGE_OUTPUT_ENCRYPTED is enabled!
87#endif
88
89// Add image encryption related dependencies
90#if (CIPHER_SUPPORT == ENABLED) && \
91 ((IMAGE_INPUT_ENCRYPTED == ENABLED) || (IMAGE_OUTPUT_ENCRYPTED == ENABLED))
92#include "security/cipher.h"
93#endif
94
95// Maximum image check data size
96#define IMAGE_MAX_CHECK_DATA_SIZE 512
97
102typedef enum
103{
104 IMAGE_STATE_IDLE,
105 IMAGE_STATE_RECV_APP_HEADER,
106 IMAGE_STATE_RECV_APP_DATA,
107 IMAGE_STATE_RECV_APP_CHECK,
108 IMAGE_STATE_VALIDATE_APP,
109 IMAGE_STATE_APP_REBOOT,
110 IMAGE_STATE_WRITE_APP_INIT,
111 IMAGE_STATE_WRITE_APP_HEADER,
112 IMAGE_STATE_WRITE_APP_DATA,
113 IMAGE_STATE_WRITE_APP_CHECK,
114 IMAGE_STATE_WRITE_APP_END
115} ImageState;
116
121typedef enum
122{
123 IMAGE_VERSION_EQUAL,
124 IMAGE_VERSION_SUPERIOR,
125 IMAGE_VERSION_INFERIOR
127
128// Image Header Major version
129#define IMAGE_HEADER_VERSION_MAJOR 1
130// Image Header Minor version
131#define IMAGE_HEADER_VERSION_MINOR 3
132// Image Header Revision number
133#define IMAGE_HEADER_VERSION_PATCH 0
134
135
136// Image Header version (always computed from the above)
137#define IMAGE_HEADER_VERSION \
138 (uint32_t)(((IMAGE_HEADER_VERSION_MAJOR & 0xFF) << 16) | \
139 ((IMAGE_HEADER_VERSION_MINOR & 0xFF) << 8) | (IMAGE_HEADER_VERSION_PATCH & 0xFF))
140
141// Helper macros to extract version components
142#define IMAGE_HEADER_VERSION_GET_MAJOR(version) (((version) >> 16) & 0xFF)
143#define IMAGE_HEADER_VERSION_GET_MINOR(version) (((version) >> 8) & 0xFF)
144#define IMAGE_HEADER_VERSION_GET_PATCH(version) ((version) & 0xFF)
145
146// ImageVersion + ImageHeader layout
147
152typedef struct
153{
154 uint8_t buffer[128];
155 uint8_t *bufferPos;
156 size_t bufferLen;
157
159
160 uint32_t newImageIdx;
162
163 uint32_t firmwareAddr;
165 uint32_t pos;
166 size_t written;
167
168 ImageState state;
169
170#if ((CIPHER_SUPPORT == ENABLED) && \
171 ((IMAGE_INPUT_ENCRYPTED == ENABLED) || (IMAGE_OUTPUT_ENCRYPTED == ENABLED)))
173 bool_t ivRetrieved;
174 uint32_t magicNumberCrc;
175 bool_t magicNumberCrcRetrieved;
176#endif
177
179
180 uint8_t checkData[IMAGE_MAX_CHECK_DATA_SIZE];
181 uint8_t *checkDataPos;
184
185} Image;
186
191typedef bool_t (*ImageAntiRollbackCallback)(ImageVersion currentAppVersion,
192 ImageVersion updateAppVersion);
193
209
210// C++ guard
211#ifdef __cplusplus
212extern "C"
213{
214#endif
215
216// CycloneBOOT Image related functions
217bool_t imageSafeAdd32(uint32_t a, uint32_t b, uint32_t *sum);
218bool_t imageFitsSlot(const ImageHeader *header, size_t slotSize);
219bool_t imageIsBootImage(const ImageHeader *header);
220cboot_error_t imageCheckHeader(ImageHeader *header);
221cboot_error_t imageGetHeader(uint8_t *buffer, size_t bufferLen, ImageHeader **header);
222cboot_error_t imageComputeHeaderCrc(ImageHeader *header);
224
225// C++ guard
226#ifdef __cplusplus
227}
228#endif
229#endif
Error codes description.
cboot_error_t
Error codes.
Definition cboot_error.h:57
CycloneBOOT Encryption & Decryption handling.
cboot_error_t imageGetHeader(uint8_t *buffer, size_t bufferLen, ImageHeader **header)
Get image header from data buffer.
Definition image.c:173
bool_t(* ImageAntiRollbackCallback)(ImageVersion currentAppVersion, ImageVersion updateAppVersion)
Image Anti-Rollback callback definition.
Definition image.h:191
bool_t imageSafeAdd32(uint32_t a, uint32_t b, uint32_t *sum)
Overflow-safe addition of two 32-bit image geometry values.
Definition image.c:72
ImageState
Image states.
Definition image.h:103
cboot_error_t imageCheckIntegrity(Image *image)
Verifies the complete image integrity.
Definition image.c:232
cboot_error_t imageCheckHeader(ImageHeader *header)
Check Internal Image header validity.
Definition image.c:130
bool_t imageIsBootImage(const ImageHeader *header)
Check whether an image holds a bootloader.
Definition image.c:112
ImageVersionComparisonFlag
Image version comparison flags definition.
Definition image.h:122
bool_t imageFitsSlot(const ImageHeader *header, size_t slotSize)
Check that an image (header, data and CRC32 tag) fits in a slot.
Definition image.c:92
cboot_error_t imageComputeHeaderCrc(ImageHeader *header)
Compute image header crc and store it in the image header crc field. The given input header will also...
Definition image.c:208
CycloneBOOT on-flash image format.
CycloneBOOT Memory Layer Abstraction.
Cipher engine structure definition.
Definition cipher.h:64
Image Process context definition.
Definition image.h:199
ImageVersion currentAppVersion
Current Application version.
Definition image.h:203
ImageVersion currentBootVersion
Current Bootloader version.
Definition image.h:204
Memory * memories
Memories list.
Definition image.h:207
Image inputImage
Input Image context.
Definition image.h:200
Image outputImage
Output Image context.
Definition image.h:201
ImageAntiRollbackCallback imgAntiRollbackCallback
Anti-Rollback callback.
Definition image.h:205
Image context definition.
Definition image.h:153
VerifyContext verifyContext
Image verification context.
Definition image.h:178
Slot * activeSlot
Pointer to the slot to write the image in.
Definition image.h:158
uint32_t newBootImageIdx
Image index number used for a bootloader image.
Definition image.h:161
CipherEngine cipherEngine
Image cipher engine.
Definition image.h:172
uint8_t * checkDataPos
Position in image check data buffer.
Definition image.h:181
size_t checkDataSize
Image check data buffer size.
Definition image.h:183
size_t firmwareLength
Image data firmware length.
Definition image.h:164
uint8_t * bufferPos
Position in image processing buffer.
Definition image.h:155
uint32_t firmwareAddr
Image firmware data write address.
Definition image.h:163
uint32_t newImageIdx
Image index number.
Definition image.h:160
size_t checkDataLen
Current number of byte in image check data buffer.
Definition image.h:182
size_t written
Current written firmware data byte number.
Definition image.h:166
size_t bufferLen
Number of byte in image processing buffer.
Definition image.h:156
uint32_t pos
Image current firmware data write position.
Definition image.h:165
Memory Definition.
Definition memory.h:225
Slot Type definition.
Definition memory.h:181
Verification context.
Definition verify.h:181
CycloneBOOT Image data verification module.