Commit 5f7ec2b4 authored by David Brown's avatar David Brown Committed by David Brown
Browse files

sim: Move `Images` methods to be methods

Now that most of the relevant data is assembled into a single `Images`
struct, make the test operations on it into methods on this type.

This change looks larger than it really is because of the indentation
change of moving top-level functions into methods.  There is also a lot
of change of `image` to `self`.

Signed-off-by: default avatarDavid Brown <david.brown@linaro.org>
parent 3f687dc5
Loading
Loading
Loading
Loading
+61 −62
Changes for sim/src/lib.rs: 61 added lines, 62 removed lines.
Original line number Diff line number Diff line
@@ -242,7 +242,7 @@ impl RunStatus {
            align: align,
        };

        failed |= run_signfail_upgrade(&bad_slot1_image);
        failed |= bad_slot1_image.run_signfail_upgrade();

        let mut flash = run.flash.clone();
        let primary = install_image(&mut flash, run.slots[0].base_off, 32784, false);
@@ -257,12 +257,12 @@ impl RunStatus {
            align: align,
        };

        failed |= run_norevert_newimage(&images);
        failed |= images.run_norevert_newimage();

        mark_upgrade(&mut images.flash, &images.slot1);

        // upgrades without fails, counts number of flash operations
        let total_count = match run_basic_upgrade(&images) {
        let total_count = match images.run_basic_upgrade() {
            Ok(v)  => v,
            Err(_) => {
                self.failures += 1;
@@ -270,11 +270,11 @@ impl RunStatus {
            },
        };

        failed |= run_basic_revert(&images);
        failed |= run_revert_with_fails(&images, total_count);
        failed |= run_perm_with_fails(&images, total_count);
        failed |= run_perm_with_random_fails(&images, total_count, 5);
        failed |= run_norevert(&images);
        failed |= images.run_basic_revert();
        failed |= images.run_revert_with_fails(total_count);
        failed |= images.run_perm_with_fails(total_count);
        failed |= images.run_perm_with_random_fails(total_count, 5);
        failed |= images.run_norevert();

        //show_flash(&flash);

@@ -340,15 +340,16 @@ pub fn make_device(device: DeviceName, align: u8) -> (SimFlash, AreaDesc) {
    }
}

impl Images {
    /// A simple upgrade without forced failures.
    ///
    /// Returns the number of flash operations which can later be used to
    /// inject failures at chosen steps.
fn run_basic_upgrade(images: &Images) -> Result<i32, ()> {
    let (fl, total_count) = try_upgrade(&images.flash, &images, None);
    fn run_basic_upgrade(&self) -> Result<i32, ()> {
        let (fl, total_count) = try_upgrade(&self.flash, &self, None);
        info!("Total flash operation count={}", total_count);

    if !verify_image(&fl, images.slot0.base_off, &images.upgrade) {
        if !verify_image(&fl, self.slot0.base_off, &self.upgrade) {
            warn!("Image mismatch after first boot");
            Err(())
        } else {
@@ -357,21 +358,20 @@ fn run_basic_upgrade(images: &Images) -> Result<i32, ()> {
    }

    #[cfg(feature = "overwrite-only")]
#[allow(unused_variables)]
fn run_basic_revert(images: &Images) -> bool {
    fn run_basic_revert(&self) -> bool {
        false
    }

    #[cfg(not(feature = "overwrite-only"))]
fn run_basic_revert(images: &Images) -> bool {
    fn run_basic_revert(&self) -> bool {
        let mut fails = 0;

        // FIXME: this test would also pass if no swap is ever performed???
        if Caps::SwapUpgrade.present() {
            for count in 2 .. 5 {
                info!("Try revert: {}", count);
            let fl = try_revert(&images.flash, &images.areadesc, count, images.align);
            if !verify_image(&fl, images.slot0.base_off, &images.primary) {
                let fl = try_revert(&self.flash, &self.areadesc, count, self.align);
                if !verify_image(&fl, self.slot0.base_off, &self.primary) {
                    error!("Revert failure on count {}", count);
                    fails += 1;
                }
@@ -381,33 +381,33 @@ fn run_basic_revert(images: &Images) -> bool {
        fails > 0
    }

fn run_perm_with_fails(images: &Images, total_flash_ops: i32) -> bool {
    fn run_perm_with_fails(&self, total_flash_ops: i32) -> bool {
        let mut fails = 0;

        // Let's try an image halfway through.
        for i in 1 .. total_flash_ops {
            info!("Try interruption at {}", i);
        let (fl, count) = try_upgrade(&images.flash, &images, Some(i));
            let (fl, count) = try_upgrade(&self.flash, &self, Some(i));
            info!("Second boot, count={}", count);
        if !verify_image(&fl, images.slot0.base_off, &images.upgrade) {
            if !verify_image(&fl, self.slot0.base_off, &self.upgrade) {
                warn!("FAIL at step {} of {}", i, total_flash_ops);
                fails += 1;
            }

        if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
            if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
                               COPY_DONE) {
                warn!("Mismatched trailer for Slot 0");
                fails += 1;
            }

        if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET,
            if !verify_trailer(&fl, self.slot1.trailer_off, MAGIC_UNSET, UNSET,
                               UNSET) {
                warn!("Mismatched trailer for Slot 1");
                fails += 1;
            }

            if Caps::SwapUpgrade.present() {
            if !verify_image(&fl, images.slot1.base_off, &images.primary) {
                if !verify_image(&fl, self.slot1.base_off, &self.primary) {
                    warn!("Slot 1 FAIL at step {} of {}", i, total_flash_ops);
                    fails += 1;
                }
@@ -422,16 +422,16 @@ fn run_perm_with_fails(images: &Images, total_flash_ops: i32) -> bool {
        fails > 0
    }

fn run_perm_with_random_fails(images: &Images, total_flash_ops: i32,
    fn run_perm_with_random_fails(&self, total_flash_ops: i32,
                                  total_fails: usize) -> bool {
        let mut fails = 0;
    let (fl, total_counts) = try_random_fails(&images.flash, &images,
        let (fl, total_counts) = try_random_fails(&self.flash, &self,
                                                  total_flash_ops, total_fails);
        info!("Random interruptions at reset points={:?}", total_counts);

    let slot0_ok = verify_image(&fl, images.slot0.base_off, &images.upgrade);
        let slot0_ok = verify_image(&fl, self.slot0.base_off, &self.upgrade);
        let slot1_ok = if Caps::SwapUpgrade.present() {
        verify_image(&fl, images.slot1.base_off, &images.primary)
            verify_image(&fl, self.slot1.base_off, &self.primary)
        } else {
            true
        };
@@ -441,12 +441,12 @@ fn run_perm_with_random_fails(images: &Images, total_flash_ops: i32,
                   if slot1_ok { "ok" } else { "fail" });
            fails += 1;
        }
    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
        if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
                           COPY_DONE) {
            error!("Mismatched trailer for Slot 0");
            fails += 1;
        }
    if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET,
        if !verify_trailer(&fl, self.slot1.trailer_off, MAGIC_UNSET, UNSET,
                           UNSET) {
            error!("Mismatched trailer for Slot 1");
            fails += 1;
@@ -461,19 +461,18 @@ fn run_perm_with_random_fails(images: &Images, total_flash_ops: i32,

    #[cfg(feature = "overwrite-only")]
    #[allow(unused_variables)]
fn run_revert_with_fails(areadesc: &AreaDesc, images: &Images,
                         total_count: i32) -> bool {
    fn run_revert_with_fails(&self, total_count: i32) -> bool {
        false
    }

    #[cfg(not(feature = "overwrite-only"))]
fn run_revert_with_fails(images: &Images, total_count: i32) -> bool {
    fn run_revert_with_fails(&self, total_count: i32) -> bool {
        let mut fails = 0;

        if Caps::SwapUpgrade.present() {
            for i in 1 .. (total_count - 1) {
                info!("Try interruption at {}", i);
            if try_revert_with_fail_at(&images.flash, &images, i) {
                if try_revert_with_fail_at(&self.flash, &self, i) {
                    error!("Revert failed at interruption {}", i);
                    fails += 1;
                }
@@ -484,20 +483,19 @@ fn run_revert_with_fails(images: &Images, total_count: i32) -> bool {
    }

    #[cfg(feature = "overwrite-only")]
#[allow(unused_variables)]
fn run_norevert(images: &Images) -> bool {
    fn run_norevert(&self) -> bool {
        false
    }

    #[cfg(not(feature = "overwrite-only"))]
fn run_norevert(images: &Images) -> bool {
    let mut fl = images.flash.clone();
    fn run_norevert(&self) -> bool {
        let mut fl = self.flash.clone();
        let mut fails = 0;

        info!("Try norevert");

        // First do a normal upgrade...
    if c::boot_go(&mut fl, &images.areadesc, None, images.align) != 0 {
        if c::boot_go(&mut fl, &self.areadesc, None, self.align) != 0 {
            warn!("Failed first boot");
            fails += 1;
        }
@@ -505,41 +503,41 @@ fn run_norevert(images: &Images) -> bool {
        //FIXME: copy_done is written by boot_go, is it ok if no copy
        //       was ever done?

    if !verify_image(&fl, images.slot0.base_off, &images.upgrade) {
        if !verify_image(&fl, self.slot0.base_off, &self.upgrade) {
            warn!("Slot 0 image verification FAIL");
            fails += 1;
        }
    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET,
        if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, UNSET,
                           COPY_DONE) {
            warn!("Mismatched trailer for Slot 0");
            fails += 1;
        }
    if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET,
        if !verify_trailer(&fl, self.slot1.trailer_off, MAGIC_UNSET, UNSET,
                           UNSET) {
            warn!("Mismatched trailer for Slot 1");
            fails += 1;
        }

        // Marks image in slot0 as permanent, no revert should happen...
    mark_permanent_upgrade(&mut fl, &images.slot0, images.align);
        mark_permanent_upgrade(&mut fl, &self.slot0, self.align);

    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
        if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
                           COPY_DONE) {
            warn!("Mismatched trailer for Slot 0");
            fails += 1;
        }

    if c::boot_go(&mut fl, &images.areadesc, None, images.align) != 0 {
        if c::boot_go(&mut fl, &self.areadesc, None, self.align) != 0 {
            warn!("Failed second boot");
            fails += 1;
        }

    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
        if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
                           COPY_DONE) {
            warn!("Mismatched trailer for Slot 0");
            fails += 1;
        }
    if !verify_image(&fl, images.slot0.base_off, &images.upgrade) {
        if !verify_image(&fl, self.slot0.base_off, &self.upgrade) {
            warn!("Failed image verification");
            fails += 1;
        }
@@ -553,37 +551,37 @@ fn run_norevert(images: &Images) -> bool {

    // Tests a new image written to slot0 that already has magic and image_ok set
    // while there is no image on slot1, so no revert should ever happen...
fn run_norevert_newimage(images: &Images) -> bool {
    let mut fl = images.flash.clone();
    fn run_norevert_newimage(&self) -> bool {
        let mut fl = self.flash.clone();
        let mut fails = 0;

        info!("Try non-revert on imgtool generated image");

    mark_upgrade(&mut fl, &images.slot0);
        mark_upgrade(&mut fl, &self.slot0);

        // This simulates writing an image created by imgtool to Slot 0
    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET, UNSET) {
        if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, UNSET, UNSET) {
            warn!("Mismatched trailer for Slot 0");
            fails += 1;
        }

        // Run the bootloader...
    if c::boot_go(&mut fl, &images.areadesc, None, images.align) != 0 {
        if c::boot_go(&mut fl, &self.areadesc, None, self.align) != 0 {
            warn!("Failed first boot");
            fails += 1;
        }

        // State should not have changed
    if !verify_image(&fl, images.slot0.base_off, &images.primary) {
        if !verify_image(&fl, self.slot0.base_off, &self.primary) {
            warn!("Failed image verification");
            fails += 1;
        }
    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET,
        if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, UNSET,
                           UNSET) {
            warn!("Mismatched trailer for Slot 0");
            fails += 1;
        }
    if !verify_trailer(&fl, images.slot1.trailer_off, MAGIC_UNSET, UNSET,
        if !verify_trailer(&fl, self.slot1.trailer_off, MAGIC_UNSET, UNSET,
                           UNSET) {
            warn!("Mismatched trailer for Slot 1");
            fails += 1;
@@ -598,34 +596,34 @@ fn run_norevert_newimage(images: &Images) -> bool {

    // Tests a new image written to slot0 that already has magic and image_ok set
    // while there is no image on slot1, so no revert should ever happen...
fn run_signfail_upgrade(images: &Images) -> bool {
    let mut fl = images.flash.clone();
    fn run_signfail_upgrade(&self) -> bool {
        let mut fl = self.flash.clone();
        let mut fails = 0;

        info!("Try upgrade image with bad signature");

    mark_upgrade(&mut fl, &images.slot0);
    mark_permanent_upgrade(&mut fl, &images.slot0, images.align);
    mark_upgrade(&mut fl, &images.slot1);
        mark_upgrade(&mut fl, &self.slot0);
        mark_permanent_upgrade(&mut fl, &self.slot0, self.align);
        mark_upgrade(&mut fl, &self.slot1);

    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
        if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
                           UNSET) {
            warn!("Mismatched trailer for Slot 0");
            fails += 1;
        }

        // Run the bootloader...
    if c::boot_go(&mut fl, &images.areadesc, None, images.align) != 0 {
        if c::boot_go(&mut fl, &self.areadesc, None, self.align) != 0 {
            warn!("Failed first boot");
            fails += 1;
        }

        // State should not have changed
    if !verify_image(&fl, images.slot0.base_off, &images.primary) {
        if !verify_image(&fl, self.slot0.base_off, &self.primary) {
            warn!("Failed image verification");
            fails += 1;
        }
    if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
        if !verify_trailer(&fl, self.slot0.trailer_off, MAGIC_VALID, IMAGE_OK,
                           UNSET) {
            warn!("Mismatched trailer for Slot 0");
            fails += 1;
@@ -637,6 +635,7 @@ fn run_signfail_upgrade(images: &Images) -> bool {

        fails > 0
    }
}

/// Test a boot, optionally stopping after 'n' flash options.  Returns a count
/// of the number of flash operations done total.