@@ -235,20 +235,20 @@ impl ImagesBuilder {
235
235
let ( primaries, upgrades) = if img_manipulation == ImageManipulation :: CorruptHigherVersionImage && !higher_version_corrupted {
236
236
higher_version_corrupted = true ;
237
237
let prim = install_image ( & mut flash, & slots[ 0 ] ,
238
- maximal ( 42784 ) , & ram, & * dep, ImageManipulation :: None , Some ( 0 ) ) ;
238
+ maximal ( 42784 ) , & ram, & * dep, ImageManipulation :: None , Some ( 0 ) , false ) ;
239
239
let upgr = match deps. depends [ image_num] {
240
240
DepType :: NoUpgrade => install_no_image ( ) ,
241
241
_ => install_image ( & mut flash, & slots[ 1 ] ,
242
- maximal ( 46928 ) , & ram, & * dep, ImageManipulation :: BadSignature , Some ( 0 ) )
242
+ maximal ( 46928 ) , & ram, & * dep, ImageManipulation :: BadSignature , Some ( 0 ) , true )
243
243
} ;
244
244
( prim, upgr)
245
245
} else {
246
246
let prim = install_image ( & mut flash, & slots[ 0 ] ,
247
- maximal ( 42784 ) , & ram, & * dep, img_manipulation, Some ( 0 ) ) ;
247
+ maximal ( 42784 ) , & ram, & * dep, img_manipulation, Some ( 0 ) , false ) ;
248
248
let upgr = match deps. depends [ image_num] {
249
249
DepType :: NoUpgrade => install_no_image ( ) ,
250
250
_ => install_image ( & mut flash, & slots[ 1 ] ,
251
- maximal ( 46928 ) , & ram, & * dep, img_manipulation, Some ( 0 ) )
251
+ maximal ( 46928 ) , & ram, & * dep, img_manipulation, Some ( 0 ) , true )
252
252
} ;
253
253
( prim, upgr)
254
254
} ;
@@ -299,9 +299,9 @@ impl ImagesBuilder {
299
299
let images = self . slots . into_iter ( ) . enumerate ( ) . map ( |( image_num, slots) | {
300
300
let dep = BoringDep :: new ( image_num, & NO_DEPS ) ;
301
301
let primaries = install_image ( & mut bad_flash, & slots[ 0 ] ,
302
- maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , Some ( 0 ) ) ;
302
+ maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , Some ( 0 ) , false ) ;
303
303
let upgrades = install_image ( & mut bad_flash, & slots[ 1 ] ,
304
- maximal ( 41928 ) , & ram, & dep, ImageManipulation :: BadSignature , Some ( 0 ) ) ;
304
+ maximal ( 41928 ) , & ram, & dep, ImageManipulation :: BadSignature , Some ( 0 ) , true ) ;
305
305
OneImage {
306
306
slots,
307
307
primaries,
@@ -322,9 +322,9 @@ impl ImagesBuilder {
322
322
let images = self . slots . into_iter ( ) . enumerate ( ) . map ( |( image_num, slots) | {
323
323
let dep = BoringDep :: new ( image_num, & NO_DEPS ) ;
324
324
let primaries = install_image ( & mut bad_flash, & slots[ 0 ] ,
325
- maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , Some ( 0 ) ) ;
325
+ maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , Some ( 0 ) , false ) ;
326
326
let upgrades = install_image ( & mut bad_flash, & slots[ 1 ] ,
327
- ImageSize :: Oversized , & ram, & dep, ImageManipulation :: None , Some ( 0 ) ) ;
327
+ ImageSize :: Oversized , & ram, & dep, ImageManipulation :: None , Some ( 0 ) , true ) ;
328
328
OneImage {
329
329
slots,
330
330
primaries,
@@ -345,7 +345,7 @@ impl ImagesBuilder {
345
345
let images = self . slots . into_iter ( ) . enumerate ( ) . map ( |( image_num, slots) | {
346
346
let dep = BoringDep :: new ( image_num, & NO_DEPS ) ;
347
347
let primaries = install_image ( & mut flash, & slots[ 0 ] ,
348
- maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , Some ( 0 ) ) ;
348
+ maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , Some ( 0 ) , false ) ;
349
349
let upgrades = install_no_image ( ) ;
350
350
OneImage {
351
351
slots,
@@ -368,7 +368,7 @@ impl ImagesBuilder {
368
368
let dep = BoringDep :: new ( image_num, & NO_DEPS ) ;
369
369
let primaries = install_no_image ( ) ;
370
370
let upgrades = install_image ( & mut flash, & slots[ 1 ] ,
371
- maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , Some ( 0 ) ) ;
371
+ maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , Some ( 0 ) , true ) ;
372
372
OneImage {
373
373
slots,
374
374
primaries,
@@ -390,7 +390,7 @@ impl ImagesBuilder {
390
390
let dep = BoringDep :: new ( image_num, & NO_DEPS ) ;
391
391
let primaries = install_no_image ( ) ;
392
392
let upgrades = install_image ( & mut flash, & slots[ 1 ] ,
393
- ImageSize :: Oversized , & ram, & dep, ImageManipulation :: None , Some ( 0 ) ) ;
393
+ ImageSize :: Oversized , & ram, & dep, ImageManipulation :: None , Some ( 0 ) , true ) ;
394
394
OneImage {
395
395
slots,
396
396
primaries,
@@ -412,9 +412,9 @@ impl ImagesBuilder {
412
412
let images = self . slots . into_iter ( ) . enumerate ( ) . map ( |( image_num, slots) | {
413
413
let dep = BoringDep :: new ( image_num, & NO_DEPS ) ;
414
414
let primaries = install_image ( & mut flash, & slots[ 0 ] ,
415
- maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , security_cnt) ;
415
+ maximal ( 32784 ) , & ram, & dep, ImageManipulation :: None , security_cnt, false ) ;
416
416
let upgrades = install_image ( & mut flash, & slots[ 1 ] ,
417
- maximal ( 41928 ) , & ram, & dep, ImageManipulation :: None , security_cnt. map ( |v| v + 1 ) ) ;
417
+ maximal ( 41928 ) , & ram, & dep, ImageManipulation :: None , security_cnt. map ( |v| v + 1 ) , true ) ;
418
418
OneImage {
419
419
slots,
420
420
primaries,
@@ -451,7 +451,7 @@ impl ImagesBuilder {
451
451
452
452
let mut flash = SimMultiFlash :: new ( ) ;
453
453
flash. insert ( dev_id, dev) ;
454
- ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingMove ] )
454
+ ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingMove , Caps :: SwapUsingOffset ] )
455
455
}
456
456
DeviceName :: K64f => {
457
457
// NXP style flash. Small sectors, one small sector for scratch.
@@ -482,7 +482,7 @@ impl ImagesBuilder {
482
482
483
483
let mut flash = SimMultiFlash :: new ( ) ;
484
484
flash. insert ( dev_id, dev) ;
485
- ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingMove ] )
485
+ ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingMove , Caps :: SwapUsingOffset ] )
486
486
}
487
487
DeviceName :: Nrf52840 => {
488
488
// Simulating the flash on the nrf52840 with partitions set up so that the scratch size
@@ -511,7 +511,20 @@ impl ImagesBuilder {
511
511
512
512
let mut flash = SimMultiFlash :: new ( ) ;
513
513
flash. insert ( dev_id, dev) ;
514
- ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingScratch , Caps :: OverwriteUpgrade ] )
514
+ ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingScratch , Caps :: OverwriteUpgrade , Caps :: SwapUsingOffset ] )
515
+ }
516
+ DeviceName :: Nrf52840UnequalSlotsLargerSlot1 => {
517
+ let dev = SimFlash :: new ( vec ! [ 4096 ; 128 ] , align as usize , erased_val) ;
518
+
519
+ let dev_id = 0 ;
520
+ let mut areadesc = AreaDesc :: new ( ) ;
521
+ areadesc. add_flash_sectors ( dev_id, & dev) ;
522
+ areadesc. add_image ( 0x008000 , 0x03b000 , FlashId :: Image0 , dev_id) ;
523
+ areadesc. add_image ( 0x043000 , 0x03c000 , FlashId :: Image1 , dev_id) ;
524
+
525
+ let mut flash = SimMultiFlash :: new ( ) ;
526
+ flash. insert ( dev_id, dev) ;
527
+ ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingScratch , Caps :: OverwriteUpgrade , Caps :: SwapUsingMove , Caps :: RamLoad , Caps :: DirectXip ] )
515
528
}
516
529
DeviceName :: Nrf52840SpiFlash => {
517
530
// Simulate nrf52840 with external SPI flash. The external SPI flash
@@ -530,7 +543,7 @@ impl ImagesBuilder {
530
543
let mut flash = SimMultiFlash :: new ( ) ;
531
544
flash. insert ( 0 , dev0) ;
532
545
flash. insert ( 1 , dev1) ;
533
- ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingMove ] )
546
+ ( flash, Rc :: new ( areadesc) , & [ Caps :: SwapUsingMove , Caps :: SwapUsingOffset ] )
534
547
}
535
548
DeviceName :: K64fMulti => {
536
549
// NXP style flash, but larger, to support multiple images.
@@ -653,7 +666,7 @@ impl Images {
653
666
}
654
667
655
668
fn is_swap_upgrade ( & self ) -> bool {
656
- Caps :: SwapUsingScratch . present ( ) || Caps :: SwapUsingMove . present ( )
669
+ Caps :: SwapUsingScratch . present ( ) || Caps :: SwapUsingMove . present ( ) || Caps :: SwapUsingOffset . present ( )
657
670
}
658
671
659
672
pub fn run_basic_revert ( & self ) -> bool {
@@ -1738,7 +1751,7 @@ fn image_largest_trailer(dev: &dyn Flash) -> usize {
1738
1751
// This computation is incorrect, and we need to figure out the correct size.
1739
1752
// c::boot_status_sz(dev.align() as u32) as usize
1740
1753
16 + 4 * dev. align ( )
1741
- } else if Caps :: SwapUsingMove . present ( ) {
1754
+ } else if Caps :: SwapUsingOffset . present ( ) || Caps :: SwapUsingMove . present ( ) {
1742
1755
let sector_size = dev. sector_iter ( ) . next ( ) . unwrap ( ) . size as u32 ;
1743
1756
align_up ( c:: boot_trailer_sz ( dev. align ( ) as u32 ) , sector_size) as usize
1744
1757
} else if Caps :: SwapUsingScratch . present ( ) {
@@ -1754,13 +1767,19 @@ fn image_largest_trailer(dev: &dyn Flash) -> usize {
1754
1767
/// fields used by the given code. Returns a copy of the image that was written.
1755
1768
fn install_image ( flash : & mut SimMultiFlash , slot : & SlotInfo , len : ImageSize ,
1756
1769
ram : & RamData ,
1757
- deps : & dyn Depender , img_manipulation : ImageManipulation , security_counter : Option < u32 > ) -> ImageData {
1758
- let offset = slot. base_off ;
1770
+ deps : & dyn Depender , img_manipulation : ImageManipulation , security_counter : Option < u32 > , secondary_slot : bool ) -> ImageData {
1771
+ let mut offset = slot. base_off ;
1759
1772
let slot_len = slot. len ;
1760
1773
let dev_id = slot. dev_id ;
1761
1774
let dev = flash. get_mut ( & dev_id) . unwrap ( ) ;
1762
1775
1763
1776
let mut tlv: Box < dyn ManifestGen > = Box :: new ( make_tlv ( ) ) ;
1777
+
1778
+ if Caps :: SwapUsingOffset . present ( ) && secondary_slot {
1779
+ let sector_size = dev. sector_iter ( ) . next ( ) . unwrap ( ) . size as usize ;
1780
+ offset += sector_size;
1781
+ }
1782
+
1764
1783
if img_manipulation == ImageManipulation :: IgnoreRamLoadFlag {
1765
1784
tlv. set_ignore_ram_load_flag ( ) ;
1766
1785
}
@@ -1798,11 +1817,19 @@ fn install_image(flash: &mut SimMultiFlash, slot: &SlotInfo, len: ImageSize,
1798
1817
ImageSize :: Oversized => {
1799
1818
let trailer = image_largest_trailer ( dev) ;
1800
1819
let tlv_len = tlv. estimate_size ( ) ;
1820
+ let mut sector_offset = 0 ;
1821
+
1822
+ if Caps :: SwapUsingOffset . present ( ) && secondary_slot {
1823
+ // This accounts for when both slots have the same size, it will not work where
1824
+ // the second slot is one sector larger than the primary
1825
+ sector_offset = dev. sector_iter ( ) . next ( ) . unwrap ( ) . size as usize ;
1826
+ }
1827
+
1801
1828
info ! ( "slot: 0x{:x}, HDR: 0x{:x}, trailer: 0x{:x}" ,
1802
1829
slot_len, HDR_SIZE , trailer) ;
1803
1830
// the overflow size is rougly estimated to work for all
1804
1831
// configurations. It might be precise if tlv_len will be maked precise.
1805
- slot_len - HDR_SIZE - trailer - tlv_len + dev. align ( ) * 4
1832
+ slot_len - HDR_SIZE - trailer - tlv_len - sector_offset + dev. align ( ) * 4
1806
1833
}
1807
1834
1808
1835
} ;
@@ -2046,17 +2073,37 @@ fn verify_image(flash: &SimMultiFlash, slot: &SlotInfo, images: &ImageData) -> b
2046
2073
let dev = flash. get ( & dev_id) . unwrap ( ) ;
2047
2074
dev. read ( offset, & mut copy) . unwrap ( ) ;
2048
2075
2049
- if buf != & copy[ ..] {
2050
- for i in 0 .. buf. len ( ) {
2051
- if buf[ i] != copy[ i] {
2052
- info ! ( "First failure for slot{} at {:#x} ({:#x} within) {:#x}!={:#x}" ,
2053
- slot. index, offset + i, i, buf[ i] , copy[ i] ) ;
2054
- break ;
2076
+ if Caps :: SwapUsingOffset . present ( ) && ( slot. index % 2 ) == 1 {
2077
+ let sector_size = dev. sector_iter ( ) . next ( ) . unwrap ( ) . size as usize ;
2078
+ let mut copy_offset = vec ! [ 0u8 ; buf. len( ) ] ;
2079
+ let offset_offset = slot. base_off + sector_size;
2080
+ dev. read ( offset_offset, & mut copy_offset) . unwrap ( ) ;
2081
+
2082
+ if buf != & copy[ ..] && buf != & copy_offset[ ..] {
2083
+ for i in 0 .. buf. len ( ) {
2084
+ if buf[ i] != copy[ i] && buf[ i] != copy_offset[ i] {
2085
+ info ! ( "First failure for slot{} at {:#x} ({:#x} within) {:#x}!=({:#x} or {:#x})" ,
2086
+ slot. index, offset + i, i, buf[ i] , copy[ i] , copy_offset[ i] ) ;
2087
+ break ;
2088
+ }
2055
2089
}
2090
+ false
2091
+ } else {
2092
+ true
2056
2093
}
2057
- false
2058
2094
} else {
2059
- true
2095
+ if buf != & copy[ ..] {
2096
+ for i in 0 .. buf. len ( ) {
2097
+ if buf[ i] != copy[ i] {
2098
+ info ! ( "First failure for slot{} at {:#x} ({:#x} within) {:#x}!={:#x}" ,
2099
+ slot. index, offset + i, i, buf[ i] , copy[ i] ) ;
2100
+ break ;
2101
+ }
2102
+ }
2103
+ false
2104
+ } else {
2105
+ true
2106
+ }
2060
2107
}
2061
2108
}
2062
2109
@@ -2294,6 +2341,7 @@ trait AsRaw : Sized {
2294
2341
/// Returns an ImageSize representing the best size to test, possibly just with the given size.
2295
2342
fn maximal ( size : usize ) -> ImageSize {
2296
2343
if Caps :: OverwriteUpgrade . present ( ) ||
2344
+ Caps :: SwapUsingOffset . present ( ) ||
2297
2345
Caps :: SwapUsingMove . present ( )
2298
2346
{
2299
2347
ImageSize :: Given ( size)
0 commit comments