@@ -124,6 +124,7 @@ struct boot_status {
124124 * (`MCUBOOT_ENC_IMAGES`).
125125 */
126126
127+ extern uint8_t current_image ;
127128extern const uint32_t boot_img_magic [4 ];
128129
129130struct boot_swap_state {
@@ -202,14 +203,15 @@ struct boot_loader_state {
202203 const struct flash_area * area ;
203204 boot_sector_t * sectors ;
204205 size_t num_sectors ;
205- } imgs [BOOT_NUM_SLOTS ];
206+ } imgs [BOOT_IMAGE_NUMBER ][ BOOT_NUM_SLOTS ];
206207
207208 struct {
208209 const struct flash_area * area ;
209210 boot_sector_t * sectors ;
210211 size_t num_sectors ;
211212 } scratch ;
212213
214+ uint8_t swap_type [BOOT_IMAGE_NUMBER ];
213215 uint8_t write_sz ;
214216};
215217
@@ -245,20 +247,22 @@ int boot_read_enc_key(uint8_t slot, uint8_t *enckey);
245247 */
246248
247249/* These are macros so they can be used as lvalues. */
248- #define BOOT_IMG_AREA (state , slot ) ((state)->imgs[(slot)].area)
250+ #define BOOT_IMG (state , slot ) ((state)->imgs[current_image][(slot)])
251+ #define BOOT_IMG_AREA (state , slot ) (BOOT_IMG(state, slot).area)
249252#define BOOT_SCRATCH_AREA (state ) ((state)->scratch.area)
250253#define BOOT_WRITE_SZ (state ) ((state)->write_sz)
254+ #define BOOT_SWAP_TYPE (state ) ((state)->swap_type[current_image])
251255
252256static inline struct image_header *
253257boot_img_hdr (struct boot_loader_state * state , size_t slot )
254258{
255- return & state -> imgs [ slot ] .hdr ;
259+ return & BOOT_IMG ( state , slot ) .hdr ;
256260}
257261
258262static inline size_t
259263boot_img_num_sectors (struct boot_loader_state * state , size_t slot )
260264{
261- return state -> imgs [ slot ] .num_sectors ;
265+ return BOOT_IMG ( state , slot ) .num_sectors ;
262266}
263267
264268static inline size_t
@@ -273,7 +277,7 @@ boot_scratch_num_sectors(struct boot_loader_state *state)
273277static inline uint32_t
274278boot_img_slot_off (struct boot_loader_state * state , size_t slot )
275279{
276- return state -> imgs [ slot ] .area -> fa_off ;
280+ return BOOT_IMG ( state , slot ) .area -> fa_off ;
277281}
278282
279283static inline size_t boot_scratch_area_size (struct boot_loader_state * state )
@@ -287,7 +291,7 @@ static inline size_t
287291boot_img_sector_size (struct boot_loader_state * state ,
288292 size_t slot , size_t sector )
289293{
290- return state -> imgs [ slot ] .sectors [sector ].fa_size ;
294+ return BOOT_IMG ( state , slot ) .sectors [sector ].fa_size ;
291295}
292296
293297/*
@@ -298,8 +302,8 @@ static inline uint32_t
298302boot_img_sector_off (struct boot_loader_state * state , size_t slot ,
299303 size_t sector )
300304{
301- return state -> imgs [ slot ] .sectors [sector ].fa_off -
302- state -> imgs [ slot ] .sectors [0 ].fa_off ;
305+ return BOOT_IMG ( state , slot ) .sectors [sector ].fa_off -
306+ BOOT_IMG ( state , slot ) .sectors [0 ].fa_off ;
303307}
304308
305309static inline int
@@ -310,13 +314,13 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
310314
311315 if (flash_area == FLASH_AREA_IMAGE_PRIMARY ) {
312316 rc = flash_area_to_sectors (flash_area , & num_sectors ,
313- state -> imgs [ BOOT_PRIMARY_SLOT ] .sectors );
314- state -> imgs [ BOOT_PRIMARY_SLOT ] .num_sectors = (size_t )num_sectors ;
317+ BOOT_IMG ( state , BOOT_PRIMARY_SLOT ) .sectors );
318+ BOOT_IMG ( state , BOOT_PRIMARY_SLOT ) .num_sectors = (size_t )num_sectors ;
315319
316320 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY ) {
317321 rc = flash_area_to_sectors (flash_area , & num_sectors ,
318- state -> imgs [ BOOT_SECONDARY_SLOT ] .sectors );
319- state -> imgs [ BOOT_SECONDARY_SLOT ] .num_sectors = (size_t )num_sectors ;
322+ BOOT_IMG ( state , BOOT_SECONDARY_SLOT ) .sectors );
323+ BOOT_IMG ( state , BOOT_SECONDARY_SLOT ) .num_sectors = (size_t )num_sectors ;
320324
321325 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH ) {
322326 rc = flash_area_to_sectors (flash_area , & num_sectors ,
@@ -335,15 +339,15 @@ static inline size_t
335339boot_img_sector_size (struct boot_loader_state * state ,
336340 size_t slot , size_t sector )
337341{
338- return state -> imgs [ slot ] .sectors [sector ].fs_size ;
342+ return BOOT_IMG ( state , slot ) .sectors [sector ].fs_size ;
339343}
340344
341345static inline uint32_t
342346boot_img_sector_off (struct boot_loader_state * state , size_t slot ,
343347 size_t sector )
344348{
345- return state -> imgs [ slot ] .sectors [sector ].fs_off -
346- state -> imgs [ slot ] .sectors [0 ].fs_off ;
349+ return BOOT_IMG ( state , slot ) .sectors [sector ].fs_off -
350+ BOOT_IMG ( state , slot ) .sectors [0 ].fs_off ;
347351}
348352
349353static inline int
@@ -357,11 +361,11 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
357361 num_sectors = BOOT_MAX_IMG_SECTORS ;
358362
359363 if (flash_area == FLASH_AREA_IMAGE_PRIMARY ) {
360- out_sectors = state -> imgs [ BOOT_PRIMARY_SLOT ] .sectors ;
361- out_num_sectors = & state -> imgs [ BOOT_PRIMARY_SLOT ] .num_sectors ;
364+ out_sectors = BOOT_IMG ( state , BOOT_PRIMARY_SLOT ) .sectors ;
365+ out_num_sectors = & BOOT_IMG ( state , BOOT_PRIMARY_SLOT ) .num_sectors ;
362366 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY ) {
363- out_sectors = state -> imgs [ BOOT_SECONDARY_SLOT ] .sectors ;
364- out_num_sectors = & state -> imgs [ BOOT_SECONDARY_SLOT ] .num_sectors ;
367+ out_sectors = BOOT_IMG ( state , BOOT_SECONDARY_SLOT ) .sectors ;
368+ out_num_sectors = & BOOT_IMG ( state , BOOT_SECONDARY_SLOT ) .num_sectors ;
365369 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH ) {
366370 out_sectors = state -> scratch .sectors ;
367371 out_num_sectors = & state -> scratch .num_sectors ;
0 commit comments