@@ -154,6 +154,9 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
154
154
#define IMAGES_ITER (x )
155
155
#endif
156
156
157
+ #define SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN 1
158
+ #define BOOT_DIRECT_UPLOAD_SECONDARY_SLOT_ID_REMAINDER 0
159
+
157
160
static char in_buf [MCUBOOT_SERIAL_MAX_RECEIVE_SIZE + 1 ];
158
161
static char dec_buf [MCUBOOT_SERIAL_MAX_RECEIVE_SIZE + 1 ];
159
162
const struct boot_uart_funcs * boot_uf ;
@@ -165,9 +168,14 @@ static char bs_obuf[BOOT_SERIAL_OUT_MAX];
165
168
static void boot_serial_output (void );
166
169
167
170
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
171
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
172
+ static int boot_serial_get_hash (const struct image_header * hdr ,
173
+ const struct flash_area * fap , uint8_t * hash , uint32_t start_off );
174
+ #else
168
175
static int boot_serial_get_hash (const struct image_header * hdr ,
169
176
const struct flash_area * fap , uint8_t * hash );
170
177
#endif
178
+ #endif
171
179
172
180
static zcbor_state_t cbor_state [2 ];
173
181
@@ -288,6 +296,7 @@ bs_list(char *buf, int len)
288
296
289
297
for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
290
298
FIH_DECLARE (fih_rc , FIH_FAILURE );
299
+ int rc ;
291
300
uint8_t tmpbuf [64 ];
292
301
293
302
#ifdef MCUBOOT_SERIAL_IMG_GRP_IMAGE_STATE
@@ -297,16 +306,41 @@ bs_list(char *buf, int len)
297
306
bool permanent = false;
298
307
#endif
299
308
309
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
310
+ uint32_t start_off = 0 ;
311
+ #endif
312
+
300
313
area_id = flash_area_id_from_multi_image_slot (image_index , slot );
301
314
if (flash_area_open (area_id , & fap )) {
302
315
continue ;
303
316
}
304
317
305
- int rc = BOOT_HOOK_CALL (boot_read_image_header_hook ,
306
- BOOT_HOOK_REGULAR , image_index , slot , & hdr );
318
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
319
+ if (slot == BOOT_SECONDARY_SLOT && swap_status != BOOT_SWAP_TYPE_REVERT ) {
320
+ uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ;
321
+ struct flash_sector sector_data ;
322
+
323
+ rc = flash_area_sectors (fap , & num_sectors , & sector_data );
324
+
325
+ if ((rc != 0 && rc != - ENOMEM ) ||
326
+ num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ) {
327
+ flash_area_close (fap );
328
+ continue ;
329
+ }
330
+
331
+ start_off = sector_data .fs_size ;
332
+ }
333
+ #endif
334
+
335
+ rc = BOOT_HOOK_CALL (boot_read_image_header_hook ,
336
+ BOOT_HOOK_REGULAR , image_index , slot , & hdr );
307
337
if (rc == BOOT_HOOK_REGULAR )
308
338
{
339
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
340
+ flash_area_read (fap , start_off , & hdr , sizeof (hdr ));
341
+ #else
309
342
flash_area_read (fap , 0 , & hdr , sizeof (hdr ));
343
+ #endif
310
344
}
311
345
312
346
if (hdr .ih_magic == IMAGE_MAGIC )
@@ -319,8 +353,13 @@ bs_list(char *buf, int len)
319
353
#if defined(MCUBOOT_ENC_IMAGES )
320
354
#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT )
321
355
if (IS_ENCRYPTED (& hdr ) && MUST_DECRYPT (fap , image_index , & hdr )) {
356
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
357
+ FIH_CALL (boot_image_validate_encrypted , fih_rc , fap ,
358
+ & hdr , tmpbuf , sizeof (tmpbuf ), start_off );
359
+ #else
322
360
FIH_CALL (boot_image_validate_encrypted , fih_rc , fap ,
323
361
& hdr , tmpbuf , sizeof (tmpbuf ));
362
+ #endif
324
363
} else {
325
364
#endif
326
365
if (IS_ENCRYPTED (& hdr )) {
@@ -333,8 +372,13 @@ bs_list(char *buf, int len)
333
372
}
334
373
#endif
335
374
375
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
376
+ FIH_CALL (bootutil_img_validate , fih_rc , NULL , & hdr ,
377
+ fap , tmpbuf , sizeof (tmpbuf ), NULL , 0 , NULL , start_off );
378
+ #else
336
379
FIH_CALL (bootutil_img_validate , fih_rc , NULL , & hdr ,
337
380
fap , tmpbuf , sizeof (tmpbuf ), NULL , 0 , NULL );
381
+ #endif
338
382
#if defined(MCUBOOT_ENC_IMAGES ) && !defined(MCUBOOT_SINGLE_APPLICATION_SLOT )
339
383
}
340
384
#endif
@@ -348,7 +392,11 @@ bs_list(char *buf, int len)
348
392
349
393
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
350
394
/* Retrieve hash of image for identification */
395
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
396
+ rc = boot_serial_get_hash (& hdr , fap , hash , start_off );
397
+ #else
351
398
rc = boot_serial_get_hash (& hdr , fap , hash );
399
+ #endif
352
400
#endif
353
401
354
402
flash_area_close (fap );
@@ -494,17 +542,39 @@ bs_set(char *buf, int len)
494
542
const struct flash_area * fap ;
495
543
uint8_t tmpbuf [64 ];
496
544
545
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
546
+ uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ;
547
+ struct flash_sector sector_data ;
548
+ uint32_t start_off = 0 ;
549
+ #endif
550
+
497
551
area_id = flash_area_id_from_multi_image_slot (image_index , 1 );
498
552
if (flash_area_open (area_id , & fap )) {
499
553
BOOT_LOG_ERR ("Failed to open flash area ID %d" , area_id );
500
554
continue ;
501
555
}
502
556
557
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
558
+ rc = flash_area_sectors (fap , & num_sectors , & sector_data );
559
+
560
+ if ((rc != 0 && rc != - ENOMEM ) ||
561
+ num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ) {
562
+ flash_area_close (fap );
563
+ continue ;
564
+ }
565
+
566
+ start_off = sector_data .fs_size ;
567
+ #endif
568
+
503
569
rc = BOOT_HOOK_CALL (boot_read_image_header_hook ,
504
570
BOOT_HOOK_REGULAR , image_index , 1 , & hdr );
505
571
if (rc == BOOT_HOOK_REGULAR )
506
572
{
573
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
574
+ flash_area_read (fap , start_off , & hdr , sizeof (hdr ));
575
+ #else
507
576
flash_area_read (fap , 0 , & hdr , sizeof (hdr ));
577
+ #endif
508
578
}
509
579
510
580
if (hdr .ih_magic == IMAGE_MAGIC )
@@ -518,12 +588,22 @@ bs_set(char *buf, int len)
518
588
{
519
589
#ifdef MCUBOOT_ENC_IMAGES
520
590
if (IS_ENCRYPTED (& hdr )) {
591
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
592
+ FIH_CALL (boot_image_validate_encrypted , fih_rc , fap ,
593
+ & hdr , tmpbuf , sizeof (tmpbuf ), start_off );
594
+ #else
521
595
FIH_CALL (boot_image_validate_encrypted , fih_rc , fap ,
522
596
& hdr , tmpbuf , sizeof (tmpbuf ));
597
+ #endif
523
598
} else {
524
599
#endif
600
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
601
+ FIH_CALL (bootutil_img_validate , fih_rc , NULL , & hdr ,
602
+ fap , tmpbuf , sizeof (tmpbuf ), NULL , 0 , NULL , start_off );
603
+ #else
525
604
FIH_CALL (bootutil_img_validate , fih_rc , NULL , & hdr ,
526
605
fap , tmpbuf , sizeof (tmpbuf ), NULL , 0 , NULL );
606
+ #endif
527
607
#ifdef MCUBOOT_ENC_IMAGES
528
608
}
529
609
#endif
@@ -534,8 +614,14 @@ bs_set(char *buf, int len)
534
614
}
535
615
}
536
616
617
+ #ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
537
618
/* Retrieve hash of image for identification */
619
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
620
+ rc = boot_serial_get_hash (& hdr , fap , hash , start_off );
621
+ #else
538
622
rc = boot_serial_get_hash (& hdr , fap , hash );
623
+ #endif
624
+ #endif
539
625
flash_area_close (fap );
540
626
541
627
if (rc == 0 && memcmp (hash , img_hash .value , sizeof (hash )) == 0 ) {
@@ -811,6 +897,9 @@ bs_upload(char *buf, int len)
811
897
*/
812
898
static struct flash_sector status_sector ;
813
899
#endif
900
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
901
+ static uint32_t start_off = 0 ;
902
+ #endif
814
903
815
904
zcbor_state_t zsd [4 ];
816
905
zcbor_new_state (zsd , sizeof (zsd ) / sizeof (zcbor_state_t ), (uint8_t * )buf , len , 1 , NULL , 0 );
@@ -874,6 +963,11 @@ bs_upload(char *buf, int len)
874
963
*/
875
964
const size_t area_size = flash_area_get_size (fap );
876
965
966
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
967
+ uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ;
968
+ struct flash_sector sector_data ;
969
+ #endif
970
+
877
971
curr_off = 0 ;
878
972
#ifdef MCUBOOT_ERASE_PROGRESSIVELY
879
973
/* Get trailer sector information; this is done early because inability to get
@@ -915,6 +1009,35 @@ bs_upload(char *buf, int len)
915
1009
#endif
916
1010
917
1011
img_size = img_size_tmp ;
1012
+
1013
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
1014
+ #ifdef MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD
1015
+ if (img_num > 0 &&
1016
+ (img_num % BOOT_NUM_SLOTS ) == BOOT_DIRECT_UPLOAD_SECONDARY_SLOT_ID_REMAINDER ) {
1017
+ rc = flash_area_sectors (fap , & num_sectors , & sector_data );
1018
+
1019
+ if ((rc != 0 && rc != - ENOMEM ) ||
1020
+ num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ) {
1021
+ rc = MGMT_ERR_ENOENT ;
1022
+ goto out ;
1023
+ }
1024
+
1025
+ start_off = sector_data .fs_size ;
1026
+ } else {
1027
+ start_off = 0 ;
1028
+ }
1029
+ #else
1030
+ rc = flash_area_sectors (fap , & num_sectors , & sector_data );
1031
+
1032
+ if ((rc != 0 && rc != - ENOMEM ) ||
1033
+ num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN ) {
1034
+ rc = MGMT_ERR_ENOENT ;
1035
+ goto out ;
1036
+ }
1037
+
1038
+ start_off = sector_data .fs_size ;
1039
+ #endif
1040
+ #endif
918
1041
} else if (img_chunk_off != curr_off ) {
919
1042
/* If received chunk offset does not match expected one jump, pretend
920
1043
* success and jump to out; out will respond to client with success
@@ -931,8 +1054,13 @@ bs_upload(char *buf, int len)
931
1054
/* Progressive erase will erase enough flash, aligned to sector size,
932
1055
* as needed for the current chunk to be written.
933
1056
*/
1057
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
1058
+ not_yet_erased = erase_range (fap , not_yet_erased ,
1059
+ curr_off + img_chunk_len - 1 + start_off );
1060
+ #else
934
1061
not_yet_erased = erase_range (fap , not_yet_erased ,
935
1062
curr_off + img_chunk_len - 1 );
1063
+ #endif
936
1064
937
1065
if (not_yet_erased < 0 ) {
938
1066
rc = MGMT_ERR_EINVAL ;
@@ -969,7 +1097,11 @@ bs_upload(char *buf, int len)
969
1097
memset (wbs_aligned , flash_area_erased_val (fap ), sizeof (wbs_aligned ));
970
1098
memcpy (wbs_aligned , img_chunk , write_size );
971
1099
1100
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
1101
+ rc = flash_area_write (fap , curr_off + start_off , wbs_aligned , write_size );
1102
+ #else
972
1103
rc = flash_area_write (fap , curr_off , wbs_aligned , write_size );
1104
+ #endif
973
1105
974
1106
if (rc != 0 ) {
975
1107
goto out ;
@@ -980,10 +1112,18 @@ bs_upload(char *buf, int len)
980
1112
img_chunk_len -= write_size ;
981
1113
}
982
1114
} else {
1115
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
1116
+ rc = flash_area_write (fap , curr_off + start_off , img_chunk , img_chunk_len );
1117
+ #else
983
1118
rc = flash_area_write (fap , curr_off , img_chunk , img_chunk_len );
1119
+ #endif
984
1120
}
1121
+ #else
1122
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
1123
+ rc = flash_area_write (fap , curr_off + start_off , img_chunk , img_chunk_len );
985
1124
#else
986
1125
rc = flash_area_write (fap , curr_off , img_chunk , img_chunk_len );
1126
+ #endif
987
1127
#endif
988
1128
989
1129
if (rc == 0 && rem_bytes ) {
@@ -996,8 +1136,13 @@ bs_upload(char *buf, int len)
996
1136
memset (wbs_aligned , flash_area_erased_val (fap ), sizeof (wbs_aligned ));
997
1137
memcpy (wbs_aligned , img_chunk + img_chunk_len , rem_bytes );
998
1138
1139
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
1140
+ rc = flash_area_write (fap , curr_off + img_chunk_len + start_off , wbs_aligned ,
1141
+ flash_area_align (fap ));
1142
+ #else
999
1143
rc = flash_area_write (fap , curr_off + img_chunk_len , wbs_aligned ,
1000
1144
flash_area_align (fap ));
1145
+ #endif
1001
1146
}
1002
1147
1003
1148
if (rc == 0 ) {
@@ -1452,8 +1597,13 @@ boot_serial_check_start(const struct boot_uart_funcs *f, int timeout_in_ms)
1452
1597
1453
1598
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
1454
1599
/* Function to find the hash of an image, returns 0 on success. */
1600
+ #ifdef MCUBOOT_SWAP_USING_OFFSET
1601
+ static int boot_serial_get_hash (const struct image_header * hdr ,
1602
+ const struct flash_area * fap , uint8_t * hash , uint32_t start_off )
1603
+ #else
1455
1604
static int boot_serial_get_hash (const struct image_header * hdr ,
1456
1605
const struct flash_area * fap , uint8_t * hash )
1606
+ #endif
1457
1607
{
1458
1608
struct image_tlv_iter it ;
1459
1609
uint32_t offset ;
@@ -1464,6 +1614,10 @@ static int boot_serial_get_hash(const struct image_header *hdr,
1464
1614
/* Manifest data is concatenated to the end of the image.
1465
1615
* It is encoded in TLV format.
1466
1616
*/
1617
+ #if defined(MCUBOOT_SWAP_USING_OFFSET )
1618
+ it .start_off = start_off ;
1619
+ #endif
1620
+
1467
1621
rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_ANY , false);
1468
1622
if (rc ) {
1469
1623
return -1 ;
0 commit comments