CycloneBOOT 2.6.6
Secure Bootloader for ARM Cortex-M MCU
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1
30#ifndef _MEMORY_H
31#define _MEMORY_H
32
33// Dependencies
34#include "boot_config.h"
35#include "core/flash.h"
36#include "os_port.h"
37
38#include <stdint.h>
39#include <stdlib.h>
40
41#include "core/cboot_error.h"
42#include "error.h"
43
44/*
45 *
46 * Memory is the abstraction layer between hardware and the library.
47 * Inside a memory, there could be one or more slots. Depending on the
48 * type of the slot and the way it is implemented within a "memory", it
49 * shall expose different APIs and structs.
50 *
51 */
52
53// Number of memories used
54#ifndef NB_MEMORIES
55#define NB_MEMORIES 1
56#elif (NB_MEMORIES < 1)
57#error NB_MEMORIES parameter is not valid
58#endif
59
60// Maximum number of slots per memoriess
61#ifndef NB_MAX_MEMORY_SLOTS
62#define NB_MAX_MEMORY_SLOTS 2
63#elif (NB_MAX_MEMORY_SLOTS < 1)
64#error NB_MEMORIES parameter is not valid
65#endif
66
67// File system layer memory support
68#ifndef MEMORIES_FS_SUPPORT
69#define MEMORIES_FS_SUPPORT DISABLED
70#elif ((MEMORIES_FS_SUPPORT != DISABLED) && (MEMORIES_FS_SUPPORT != ENABLED))
71#error MEMORIES_FS_SUPPORT parameter is not valid
72#endif
73
74// External memory support
75#ifndef EXTERNAL_MEMORY_SUPPORT
76#define EXTERNAL_MEMORY_SUPPORT DISABLED
77#elif ((EXTERNAL_MEMORY_SUPPORT != DISABLED) && (EXTERNAL_MEMORY_SUPPORT != ENABLED))
78#error EXTERNAL_MEMORY_SUPPORT parameter is not valid
79#endif
80
81#if (MEMORIES_FS_SUPPORT == ENABLED)
82#include "core/fs.h"
83#endif
84
85// C++ guard
86#ifdef __cplusplus
87extern "C"
88{
89#endif
90
95typedef enum
96{
97 MEMORY_TYPE_FLASH, // FLASH
98#if (MEMORIES_FS_SUPPORT == ENABLED)
99 MEMORY_TYPE_FS, // File system type abstraction
100#endif
101 MEMORY_TYPE_EEPROM // EEPROM
102} MemoryType;
103
108typedef enum
109{
110 MEMORY_ROLE_PRIMARY, // Primary memory, most likely internal Flash
111 MEMORY_ROLE_SECONDARY // Secondary memory, most likely an external memory
112} MemoryRole;
113
118typedef enum
119{
120 MEMORY_STATUS_BUSY,
121 MEMORY_STATUS_ERROR,
122 MEMORY_STATUS_OK
124
129typedef enum
130{
131 MEMORY_WRITE_DEFAULT_FLAG,
132 MEMORY_WRITE_FORCE_FLAG,
133 MEMORY_WRITE_RESET_FLAG
135
140typedef enum
141{
142 SLOT_TYPE_DIRECT = 1, // FLASH
143#if (MEMORIES_FS_SUPPORT == ENABLED)
144 SLOT_TYPE_FILE, // File system type abstraction
145#endif
146} SlotType;
147
152typedef enum
153{
154 SLOT_STATUS_BUSY,
155 SLOT_STATUS_ERROR,
156 SLOT_STATUS_OK
157} SlotStatus;
158
163typedef enum
164{
165 SLOT_CONTENT_NONE = 0x00, // Each value has its own bit
166 SLOT_CONTENT_APP = 0x01, //
167 SLOT_CONTENT_UPDATE = 0x02, //
168 SLOT_CONTENT_BACKUP = 0x04, //
169 SLOT_CONTENT_BINARY = 0x08, //
170 SLOT_CONTENT_DATA = 0x10, //
171 SLOT_CONTENT_CONFIGURATION = 0x20, //
172 SLOT_CONTENT_BOOT = 0x40, //
173 SLOT_CONTENT_FACTORY = 0x80 // Standalone only for now; never an update target
175
180typedef struct
181{
182 SlotType type;
183 uint8_t cType;
184 const void *memParent;
185
186 __packed_union
187 {
188 __packed_struct
189 {
190 uint32_t addr;
191 size_t size;
192 } /*flash*/;
193#if (MEMORIES_FS_SUPPORT == ENABLED)
194 __packed_struct
195 {
196 const char *path;
197 const char *mode;
198 FsFileHandler *file;
199 } /*filesystem*/;
200#endif
201 } /*memory*/;
202
203} Slot;
204
209typedef struct
210{
211 uint32_t addr;
212 size_t size;
213 uint32_t bank1Addr;
214 uint32_t bank2Addr;
215 size_t bankSize;
216 size_t writeSize;
217 uint32_t flags;
218} MemoryInfo;
219
224typedef struct
225{
226 MemoryType memoryType;
227 void *memoryInfo;
228 Slot slots[NB_MAX_MEMORY_SLOTS];
229 uint8_t nbSlots;
230 const void *driver;
231 MemoryRole memoryRole;
232} Memory;
233
237cboot_error_t memoryInit(Memory *memories, size_t nbMemories);
238
242cboot_error_t memoryDeInit(Memory *memories, size_t nbMemories);
243
248
253
257cboot_error_t memoryWriteSlot(Slot *slot, uint32_t offset, uint8_t *buffer, size_t length,
258 size_t *written, uint8_t flag);
259
263cboot_error_t memoryReadSlot(Slot *slot, uint32_t offset, uint8_t *buffer, size_t length);
264
268cboot_error_t memoryEraseSlot(Slot *slot, uint32_t offset, size_t length);
269
273cboot_error_t memoryCopySlot(Slot *src, Slot *dst, size_t bytesNumber);
274
278void memoryCompareSlot(Slot *src, Slot *dst, uint8_t *result);
279
283cboot_error_t memoryCompareSlotContent(Slot *src, Slot *dst, uint8_t *result);
284
288cboot_error_t memoryCleanup(Memory *memories, size_t nbMemories);
289
290cboot_error_t memoryGetSlotByCType(Memory *memory, uint8_t slotCType, Slot **slot);
291cboot_error_t memoryGetMemoryByRole(Memory *memories, size_t nb_memories, MemoryRole role,
292 Memory **memory);
293
294void memoryInitWriteBuffer(void);
295void memoryResetWriteBuffer(void);
296
297// Include Extended Memory routines
298#include "memory_ex.h"
299
300// C++ guard
301#ifdef __cplusplus
302}
303#endif
304#endif //_MEMORY_H
Error codes description.
cboot_error_t
Error codes.
Definition cboot_error.h:57
CycloneBOOT Flash layer.
cboot_error_t memoryInit(Memory *memories, size_t nbMemories)
Memory initialization function.
Definition memory.c:63
SlotStatus
Slot status definition.
Definition memory.h:153
void memoryInitWriteBuffer(void)
Initialize memory write buffer.
Definition memory.c:916
cboot_error_t memoryCopySlot(Slot *src, Slot *dst, size_t bytesNumber)
Make a backup of the internal slot.
Definition memory.c:488
MemoryType
Memory Type definition.
Definition memory.h:96
cboot_error_t memoryGetSlotByCType(Memory *memory, uint8_t slotCType, Slot **slot)
Search a slot in memory that match the given slot content type.
Definition memory.c:855
cboot_error_t memoryDeInit(Memory *memories, size_t nbMemories)
Memory de-initialization function.
Definition memory.c:132
MemoryStatus
Memory status definition.
Definition memory.h:119
MemoryRole
Memory Role definition.
Definition memory.h:109
cboot_error_t memoryEraseSlot(Slot *slot, uint32_t offset, size_t length)
Erase Data from Memory function.
Definition memory.c:565
cboot_error_t memoryCleanup(Memory *memories, size_t nbMemories)
Memory cleanup function.
Definition memory.c:679
SlotContentType
Slot Type definition.
Definition memory.h:164
void memoryResetWriteBuffer(void)
Reset memory write buffer.
Definition memory.c:927
MemoryWriteFlag
Memory write flag definition.
Definition memory.h:130
cboot_error_t memoryCompareSlotContent(Slot *src, Slot *dst, uint8_t *result)
Compare two given slots content.
Definition memory.c:630
void memoryCompareSlot(Slot *src, Slot *dst, uint8_t *result)
Compare two given slots metadata.
Definition memory.c:610
cboot_error_t memoryGetSlotStatus(Slot *slot, SlotStatus *status)
Get Memory Status function.
Definition memory.c:252
cboot_error_t memoryGetInfo(Memory *memory, MemoryInfo *info)
Memory get driver information function.
Definition memory.c:181
cboot_error_t memoryReadSlot(Slot *slot, uint32_t offset, uint8_t *buffer, size_t length)
Read Data from Memory function.
Definition memory.c:441
cboot_error_t memoryWriteSlot(Slot *slot, uint32_t offset, uint8_t *buffer, size_t length, size_t *written, uint8_t flag)
Write Data into Memory function.
Definition memory.c:291
SlotType
Slot Type definition.
Definition memory.h:141
CycloneBOOT Memory Layer Abstraction (Extended)
Memory Information.
Definition memory.h:210
Memory Definition.
Definition memory.h:225
Slot Type definition.
Definition memory.h:181