Commit d0157343 authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

sim: add bootstrap test

Add caps for bootstrap option and implement simple bootstrap test
for the simulator.

Signed-off-by: default avatarFabio Utzig <fabio.utzig@nordicsemi.no>
parent aac99a14
Loading
Loading
Loading
Loading
+1 −0
Changes for boot/bootutil/include/bootutil/caps.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ uint32_t bootutil_get_caps(void);
#define BOOTUTIL_CAP_SWAP_USING_MOVE        (1<<11)
#define BOOTUTIL_CAP_DOWNGRADE_PREVENTION   (1<<12)
#define BOOTUTIL_CAP_ENC_X25519             (1<<13)
#define BOOTUTIL_CAP_BOOTSTRAP              (1<<14)

/*
 * Query the number of images this bootloader is configured for.  This
+3 −0
Changes for boot/bootutil/src/caps.c: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -65,6 +65,9 @@ uint32_t bootutil_get_caps(void)
#if defined(MCUBOOT_DOWNGRADE_PREVENTION)
    res |= BOOTUTIL_CAP_DOWNGRADE_PREVENTION;
#endif
#if defined(MCUBOOT_BOOTSTRAP)
    res |= BOOTUTIL_CAP_BOOTSTRAP;
#endif

    return res;
}
+1 −0
Changes for sim/src/caps.rs: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ pub enum Caps {
    SwapUsingMove        = (1 << 11),
    DowngradePrevention  = (1 << 12),
    EncX25519            = (1 << 13),
    Bootstrap            = (1 << 14),
}

impl Caps {
+52 −0
Changes for sim/src/image.rs: 52 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -258,6 +258,25 @@ impl ImagesBuilder {
        }
    }

    pub fn make_bootstrap_image(self) -> Images {
        let mut flash = self.flash;
        let images = self.slots.into_iter().enumerate().map(|(image_num, slots)| {
            let dep = BoringDep::new(image_num, &NO_DEPS);
            let primaries = install_no_image();
            let upgrades = install_image(&mut flash, &slots[1], 32784, &dep, false);
            OneImage {
                slots: slots,
                primaries: primaries,
                upgrades: upgrades,
            }}).collect();
        Images {
            flash: flash,
            areadesc: self.areadesc,
            images: images,
            total_count: None,
        }
    }

    /// Build the Flash and area descriptor for a given device.
    pub fn make_device(device: DeviceName, align: usize, erased_val: u8) -> (SimMultiFlash, AreaDesc, &'static [Caps]) {
        match device {
@@ -399,6 +418,39 @@ impl Images {
        }
    }

    pub fn run_bootstrap(&self) -> bool {
        let mut flash = self.flash.clone();
        let mut fails = 0;

        if Caps::Bootstrap.present() {
            info!("Try bootstraping image in the primary");

            let (result, _) = c::boot_go(&mut flash, &self.areadesc, None, false);
            if result != 0 {
                warn!("Failed first boot");
                fails += 1;
            }

            if !self.verify_images(&flash, 0, 1) {
                warn!("Image in the first slot was not bootstrapped");
                fails += 1;
            }

            if !self.verify_trailers(&flash, 0, BOOT_MAGIC_GOOD,
                                     BOOT_FLAG_SET, BOOT_FLAG_SET) {
                warn!("Mismatched trailer for the primary slot");
                fails += 1;
            }
        }

        if fails > 0 {
            error!("Expected trailer on secondary slot to be erased");
        }

        fails > 0
    }


    /// Test a simple upgrade, with dependencies given, and verify that the
    /// image does as is described in the test.
    pub fn run_check_deps(&self, deps: &DepTest) -> bool {
+1 −0
Changes for sim/tests/core.rs: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ macro_rules! sim_test {

sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade());
sim_test!(secondary_trailer_leftover, make_erased_secondary_image(), run_secondary_leftover_trailer());
sim_test!(bootstrap, make_bootstrap_image(), run_bootstrap());
sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage());
sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert());
sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails());