Loading boot/bootutil/include/bootutil/bootutil.h +5 −1 Changes for boot/bootutil/include/bootutil/bootutil.h: 5 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -38,7 +38,11 @@ extern "C" { /** Swap back to alternate slot. A confirm changes this state to NONE. */ #define BOOT_SWAP_TYPE_REVERT 4 #define BOOT_SWAP_TYPE_FAIL 0xff /** Swap failed because image to be run is not valid */ #define BOOT_SWAP_TYPE_FAIL 5 /** Swapping encountered an unrecoverable error */ #define BOOT_SWAP_TYPE_PANIC 0xff #define MAX_FLASH_ALIGN 8 extern const uint32_t BOOT_MAX_ALIGN; Loading boot/bootutil/src/bootutil_misc.c +53 −99 Changes for boot/bootutil/src/bootutil_misc.c: 53 added lines, 99 removed lines. Original line number Diff line number Diff line Loading @@ -48,97 +48,49 @@ const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN; struct boot_swap_table { /** * For each field, a value of 0 means "any". */ uint8_t bsw_magic_slot0; uint8_t bsw_magic_slot1; uint8_t bsw_image_ok_slot0; uint8_t bsw_image_ok_slot1; uint8_t magic_slot0; uint8_t magic_slot1; uint8_t image_ok_slot0; uint8_t image_ok_slot1; uint8_t copy_done_slot0; uint8_t bsw_swap_type; uint8_t swap_type; }; /** * This set of tables maps image trailer contents to swap operation type. * When searching for a match, these tables must be iterated sequentially. * * NOTE: the table order is very important. The settings in Slot 1 always * are priority to Slot 0 and should be located earlier in the table. * * The table lists only states where there is action needs to be taken by * the bootloader, as in starting/finishing a swap operation. */ static const struct boot_swap_table boot_swap_tables[] = { { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Unset | Unset | * image-ok | Any | Any | * ---------+------------+------------' * swap: none | * -----------------------------------' */ .bsw_magic_slot0 = BOOT_MAGIC_UNSET, .bsw_magic_slot1 = BOOT_MAGIC_UNSET, .bsw_image_ok_slot0 = 0, .bsw_image_ok_slot1 = 0, .bsw_swap_type = BOOT_SWAP_TYPE_NONE, }, { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Any | Good | * image-ok | Any | Unset | * ---------+------------+------------` * swap: test | * -----------------------------------' */ .bsw_magic_slot0 = 0, .bsw_magic_slot1 = BOOT_MAGIC_GOOD, .bsw_image_ok_slot0 = 0, .bsw_image_ok_slot1 = 0xff, .bsw_swap_type = BOOT_SWAP_TYPE_TEST, }, { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Any | Good | * image-ok | Any | 0x01 | * ---------+------------+------------` * swap: permanent | * -----------------------------------' */ .bsw_magic_slot0 = 0, .bsw_magic_slot1 = BOOT_MAGIC_GOOD, .bsw_image_ok_slot0 = 0, .bsw_image_ok_slot1 = 0x01, .bsw_swap_type = BOOT_SWAP_TYPE_PERM, .magic_slot0 = 0, .magic_slot1 = BOOT_MAGIC_GOOD, .image_ok_slot0 = 0, .image_ok_slot1 = 0xff, .copy_done_slot0 = 0, .swap_type = BOOT_SWAP_TYPE_TEST, }, { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Good | Unset | * image-ok | 0xff | Any | * ---------+------------+------------' * swap: revert (test image running) | * -----------------------------------' */ .bsw_magic_slot0 = BOOT_MAGIC_GOOD, .bsw_magic_slot1 = BOOT_MAGIC_UNSET, .bsw_image_ok_slot0 = 0xff, .bsw_image_ok_slot1 = 0, .bsw_swap_type = BOOT_SWAP_TYPE_REVERT, .magic_slot0 = 0, .magic_slot1 = BOOT_MAGIC_GOOD, .image_ok_slot0 = 0, .image_ok_slot1 = 0x01, .copy_done_slot0 = 0, .swap_type = BOOT_SWAP_TYPE_PERM, }, { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Good | Unset | * image-ok | 0x01 | Any | * ---------+------------+------------' * swap: none (confirmed test image) | * -----------------------------------' */ .bsw_magic_slot0 = BOOT_MAGIC_GOOD, .bsw_magic_slot1 = BOOT_MAGIC_UNSET, .bsw_image_ok_slot0 = 0x01, .bsw_image_ok_slot1 = 0, .bsw_swap_type = BOOT_SWAP_TYPE_NONE, .magic_slot0 = BOOT_MAGIC_GOOD, .magic_slot1 = BOOT_MAGIC_UNSET, .image_ok_slot0 = 0xff, .image_ok_slot1 = 0, .copy_done_slot0 = 0x01, .swap_type = BOOT_SWAP_TYPE_REVERT, }, }; Loading Loading @@ -357,40 +309,42 @@ int boot_swap_type(void) { const struct boot_swap_table *table; struct boot_swap_state state_slot0; struct boot_swap_state state_slot1; struct boot_swap_state slot0; struct boot_swap_state slot1; int rc; int i; rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0); assert(rc == 0); rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0); if (rc) { return BOOT_SWAP_TYPE_PANIC; } rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1); assert(rc == 0); rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1); if (rc) { return BOOT_SWAP_TYPE_PANIC; } for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { table = boot_swap_tables + i; if ((table->bsw_magic_slot0 == 0 || table->bsw_magic_slot0 == state_slot0.magic) && (table->bsw_magic_slot1 == 0 || table->bsw_magic_slot1 == state_slot1.magic) && (table->bsw_image_ok_slot0 == 0 || table->bsw_image_ok_slot0 == state_slot0.image_ok) && (table->bsw_image_ok_slot1 == 0 || table->bsw_image_ok_slot1 == state_slot1.image_ok)) { if ((!table->magic_slot0 || table->magic_slot0 == slot0.magic ) && (!table->magic_slot1 || table->magic_slot1 == slot1.magic ) && (!table->image_ok_slot0 || table->image_ok_slot0 == slot0.image_ok ) && (!table->image_ok_slot1 || table->image_ok_slot1 == slot1.image_ok ) && (!table->copy_done_slot0 || table->copy_done_slot0 == slot0.copy_done)) { BOOT_LOG_INF("Swap type: %s", table->bsw_swap_type == BOOT_SWAP_TYPE_NONE ? "none" : table->bsw_swap_type == BOOT_SWAP_TYPE_TEST ? "test" : table->bsw_swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : table->bsw_swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : table->bsw_swap_type == BOOT_SWAP_TYPE_FAIL ? "fail" : table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : "BUG; can't happen"); return table->bsw_swap_type; assert(table->swap_type == BOOT_SWAP_TYPE_TEST || table->swap_type == BOOT_SWAP_TYPE_PERM || table->swap_type == BOOT_SWAP_TYPE_REVERT); return table->swap_type; } } assert(0); BOOT_LOG_INF("Swap type: none"); return BOOT_SWAP_TYPE_NONE; } Loading boot/bootutil/src/loader.c +64 −73 Changes for boot/bootutil/src/loader.c: 64 added lines, 73 removed lines. Original line number Diff line number Diff line Loading @@ -126,21 +126,6 @@ static const struct boot_status_table boot_status_tables[] = { #define BOOT_STATUS_TABLES_COUNT \ (sizeof boot_status_tables / sizeof boot_status_tables[0]) /** * This table indicates the next swap type that should be performed. The first * column contains the current swap type. The second column contains the swap * type that should be effected after the first completes. */ static const uint8_t boot_swap_trans_table[][2] = { /* From To */ { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE }, { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE }, { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT }, }; #define BOOT_SWAP_TRANS_TABLE_SIZE \ (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0]) #define BOOT_LOG_SWAP_STATE(area, state) \ BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \ (area), \ Loading Loading @@ -201,25 +186,24 @@ boot_status_source(void) /** * Calculates the type of swap that just completed. * * This is used when a swap is interrupted by an external event. After * finishing the swap operation determines what the initial request was. */ static int boot_previous_swap_type(void) { int post_swap_type; int i; post_swap_type = boot_swap_type(); for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){ if (boot_swap_trans_table[i][1] == post_swap_type) { return boot_swap_trans_table[i][0]; switch (post_swap_type) { case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM; case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST; case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC; } } /* XXX: Temporary assert. */ assert(0); return BOOT_SWAP_TYPE_REVERT; return BOOT_SWAP_TYPE_FAIL; } static int Loading Loading @@ -605,18 +589,16 @@ static int boot_validated_swap_type(void) { int swap_type; int rc; swap_type = boot_swap_type(); if (swap_type == BOOT_SWAP_TYPE_NONE) { /* Continue using slot 0. */ return BOOT_SWAP_TYPE_NONE; } switch (swap_type) { case BOOT_SWAP_TYPE_TEST: case BOOT_SWAP_TYPE_PERM: case BOOT_SWAP_TYPE_REVERT: /* Boot loader wants to switch to slot 1. Ensure image is valid. */ rc = boot_validate_slot(1); if (rc != 0) { return BOOT_SWAP_TYPE_FAIL; if (boot_validate_slot(1) != 0) { swap_type = BOOT_SWAP_TYPE_FAIL; } } return swap_type; Loading Loading @@ -1095,10 +1077,10 @@ boot_copy_image(struct boot_status *bs) #endif /** * Marks a test image in slot 0 as fully copied. * Marks the image in slot 0 as fully copied. */ static int boot_finalize_test_swap(void) boot_set_copy_done(void) { const struct flash_area *fap; int rc; Loading @@ -1109,23 +1091,24 @@ boot_finalize_test_swap(void) } rc = boot_write_copy_done(fap); if (rc != 0) { flash_area_close(fap); return rc; } return 0; } /** * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure * the status bytes from the image revert operation don't get processed on a * subsequent boot. * * NOTE: image_ok is tested before writing because if there's a valid permanent * image installed on slot0 and the new image to be upgrade to has a bad sig, * image_ok would be overwritten. */ static int boot_finalize_revert_swap(void) boot_set_image_ok(void) { const struct flash_area *fap; struct boot_swap_state state_slot0; struct boot_swap_state state; int rc; rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); Loading @@ -1133,33 +1116,19 @@ boot_finalize_revert_swap(void) return BOOT_EFLASH; } rc = boot_read_swap_state(fap, &state_slot0); if (rc != 0) { return BOOT_EFLASH; } if (state_slot0.magic == BOOT_MAGIC_UNSET) { rc = boot_write_magic(fap); if (rc != 0) { return rc; } } if (state_slot0.copy_done == BOOT_FLAG_UNSET) { rc = boot_write_copy_done(fap); rc = boot_read_swap_state(fap, &state); if (rc != 0) { return rc; } rc = BOOT_EFLASH; goto out; } if (state_slot0.image_ok == BOOT_FLAG_UNSET) { if (state.image_ok == BOOT_FLAG_UNSET) { rc = boot_write_image_ok(fap); if (rc != 0) { return rc; } } return 0; out: flash_area_close(fap); return rc; } /** Loading Loading @@ -1191,6 +1160,12 @@ boot_swap_if_needed(int *out_swap_type) rc = boot_copy_image(&bs); assert(rc == 0); /* NOTE: here we have finished a swap resume. The initial request * was either a TEST or PERM swap, which now after the completed * swap will be determined to be respectively REVERT (was TEST) * or NONE (was PERM). */ /* Extrapolate the type of the partial swap. We need this * information to know how to mark the swap complete in flash. */ Loading Loading @@ -1269,6 +1244,17 @@ boot_go(struct boot_rsp *rsp) if (rc != 0) { goto out; } /* * The following states need image_ok be explicitly set after the * swap was finished to avoid a new revert. */ if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) { rc = boot_set_image_ok(); if (rc != 0) { swap_type = BOOT_SWAP_TYPE_PANIC; } } } else { swap_type = BOOT_SWAP_TYPE_NONE; } Loading @@ -1278,17 +1264,15 @@ boot_go(struct boot_rsp *rsp) slot = 0; break; case BOOT_SWAP_TYPE_TEST: case BOOT_SWAP_TYPE_PERM: slot = 1; boot_finalize_test_swap(); reload_headers = true; break; case BOOT_SWAP_TYPE_TEST: /* fallthrough */ case BOOT_SWAP_TYPE_PERM: /* fallthrough */ case BOOT_SWAP_TYPE_REVERT: slot = 1; boot_finalize_revert_swap(); reload_headers = true; rc = boot_set_copy_done(); if (rc != 0) { swap_type = BOOT_SWAP_TYPE_PANIC; } break; case BOOT_SWAP_TYPE_FAIL: Loading @@ -1297,14 +1281,19 @@ boot_go(struct boot_rsp *rsp) * we just reverted back to slot 0. */ slot = 0; boot_finalize_revert_swap(); reload_headers = true; break; default: swap_type = BOOT_SWAP_TYPE_PANIC; } if (swap_type == BOOT_SWAP_TYPE_PANIC) { BOOT_LOG_ERR("panic!"); assert(0); slot = 0; break; /* Loop forever... */ while (1) {} } #ifdef MCUBOOT_VALIDATE_SLOT0 Loading @@ -1321,6 +1310,8 @@ boot_go(struct boot_rsp *rsp) rc = BOOT_EBADIMAGE; goto out; } #else (void)reload_headers; #endif /* Always boot from the primary slot. */ Loading sim/simflash/src/lib.rs +2 −2 Changes for sim/simflash/src/lib.rs: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -39,6 +39,7 @@ fn ebounds<T: AsRef<str>>(message: T) -> ErrorKind { ErrorKind::OutOfBounds(message.as_ref().to_owned()) } #[allow(dead_code)] fn ewrite<T: AsRef<str>>(message: T) -> ErrorKind { ErrorKind::Write(message.as_ref().to_owned()) } Loading Loading @@ -148,8 +149,7 @@ impl Flash for SimFlash { for (i, x) in &mut self.write_safe[offset .. offset + payload.len()].iter_mut().enumerate() { if !(*x) { bail!(ewrite(format!("Write to unerased location at 0x{:x}", offset + i))); panic!("Write to unerased location at 0x{:x}", offset + i); } *x = false; } Loading sim/src/main.rs +147 −15 Changes for sim/src/main.rs: 147 added lines, 15 removed lines. Original line number Diff line number Diff line Loading @@ -150,7 +150,7 @@ fn main() { error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures); process::exit(1); } else { warn!("{} Tests ran successfully", status.passes); error!("{} Tests ran successfully", status.passes); process::exit(0); } } Loading Loading @@ -245,17 +245,31 @@ impl RunStatus { trailer_off: scratch_base - offset_from_end, }; let images = Images { slot0: slot0, slot1: slot1, primary: install_image(&mut flash, slot0_base, 32784), upgrade: install_image(&mut flash, slot1_base, 41928), }; // Set an alignment, and position the magic value. c::set_sim_flash_align(align); let mut failed = false; // Set an alignment, and position the magic value. c::set_sim_flash_align(align); // Creates a badly signed image in slot1 to check that it is not // upgraded to let mut bad_flash = flash.clone(); let bad_slot1_image = Images { slot0: &slot0, slot1: &slot1, primary: install_image(&mut bad_flash, slot0_base, 32784, false), upgrade: install_image(&mut bad_flash, slot1_base, 41928, true), }; failed |= run_signfail_upgrade(&bad_flash, &areadesc, &bad_slot1_image); let images = Images { slot0: &slot0, slot1: &slot1, primary: install_image(&mut flash, slot0_base, 32784, false), upgrade: install_image(&mut flash, slot1_base, 41928, false), }; failed |= run_norevert_newimage(&flash, &areadesc, &images); mark_upgrade(&mut flash, &images.slot1); Loading Loading @@ -305,12 +319,13 @@ fn run_basic_upgrade(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) fn run_basic_revert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> 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(&flash, &areadesc, count); if !verify_image(&fl, images.slot0.base_off, &images.primary) { warn!("Revert failure on count {}", count); error!("Revert failure on count {}", count); fails += 1; } } Loading Loading @@ -353,8 +368,10 @@ fn run_perm_with_fails(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, } } info!("{} out of {} failed {:.2}%", fails, total_flash_ops, if fails > 0 { error!("{} out of {} failed {:.2}%", fails, total_flash_ops, fails as f32 * 100.0 / total_flash_ops as f32); } fails > 0 } Loading Loading @@ -390,6 +407,10 @@ fn run_perm_with_random_fails(flash: &SimFlash, areadesc: &AreaDesc, fails += 1; } if fails > 0 { error!("Error testing perm upgrade with {} fails", total_fails); } fails > 0 } Loading @@ -401,6 +422,7 @@ fn run_revert_with_fails(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, for i in 1 .. (total_count - 1) { info!("Try interruption at {}", i); if try_revert_with_fail_at(&flash, &areadesc, &images, i) { error!("Revert failed at interruption {}", i); fails += 1; } } Loading @@ -422,6 +444,9 @@ fn run_norevert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool fails += 1; } //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) { warn!("Slot 0 image verification FAIL"); fails += 1; Loading @@ -440,6 +465,12 @@ fn run_norevert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool // Marks image in slot0 as permanent, no revert should happen... mark_permanent_upgrade(&mut fl, &images.slot0); if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK, COPY_DONE) { warn!("Mismatched trailer for Slot 0"); fails += 1; } if c::boot_go(&mut fl, &areadesc) != 0 { warn!("Failed second boot"); fails += 1; Loading @@ -455,6 +486,101 @@ fn run_norevert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool fails += 1; } if fails > 0 { error!("Error running upgrade without revert"); } fails > 0 } // 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(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool { let mut fl = flash.clone(); let mut fails = 0; info!("Try non-revert on imgtool generated image"); c::set_flash_counter(0); mark_upgrade(&mut fl, &images.slot0); // This simulates writing an image created by imgtool to Slot 0 if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET, UNSET) { warn!("Mismatched trailer for Slot 0"); fails += 1; } // Run the bootloader... if c::boot_go(&mut fl, &areadesc) != 0 { warn!("Failed first boot"); fails += 1; } // State should not have changed if !verify_image(&fl, images.slot0.base_off, &images.primary) { warn!("Failed image verification"); fails += 1; } if !verify_trailer(&fl, images.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, UNSET) { warn!("Mismatched trailer for Slot 1"); fails += 1; } if fails > 0 { error!("Expected a non revert with new image"); } fails > 0 } // 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(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool { let mut fl = flash.clone(); let mut fails = 0; info!("Try upgrade image with bad signature"); c::set_flash_counter(0); mark_upgrade(&mut fl, &images.slot0); mark_permanent_upgrade(&mut fl, &images.slot0); mark_upgrade(&mut fl, &images.slot1); if !verify_trailer(&fl, images.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, &areadesc) != 0 { warn!("Failed first boot"); fails += 1; } // State should not have changed if !verify_image(&fl, images.slot0.base_off, &images.primary) { warn!("Failed image verification"); fails += 1; } if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK, UNSET) { warn!("Mismatched trailer for Slot 0"); fails += 1; } if fails > 0 { error!("Expected an upgrade failure when image has bad signature"); } fails > 0 } Loading Loading @@ -617,7 +743,8 @@ fn show_flash(flash: &Flash) { /// Install a "program" into the given image. This fakes the image header, or at least all of the /// fields used by the given code. Returns a copy of the image that was written. fn install_image(flash: &mut Flash, offset: usize, len: usize) -> Vec<u8> { fn install_image(flash: &mut Flash, offset: usize, len: usize, bad_sig: bool) -> Vec<u8> { let offset0 = offset; let mut tlv = make_tlv(); Loading Loading @@ -657,7 +784,12 @@ fn install_image(flash: &mut Flash, offset: usize, len: usize) -> Vec<u8> { tlv.add_bytes(&buf); // Get and append the TLV itself. if bad_sig { let good_sig = &mut tlv.make_tlv(); buf.append(&mut vec![0; good_sig.len()]); } else { buf.append(&mut tlv.make_tlv()); } // Pad the block to a flash alignment (8 bytes). while buf.len() % 8 != 0 { Loading Loading @@ -780,9 +912,9 @@ struct SlotInfo { trailer_off: usize, } struct Images { slot0: SlotInfo, slot1: SlotInfo, struct Images<'a> { slot0: &'a SlotInfo, slot1: &'a SlotInfo, primary: Vec<u8>, upgrade: Vec<u8>, } Loading Loading
boot/bootutil/include/bootutil/bootutil.h +5 −1 Changes for boot/bootutil/include/bootutil/bootutil.h: 5 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -38,7 +38,11 @@ extern "C" { /** Swap back to alternate slot. A confirm changes this state to NONE. */ #define BOOT_SWAP_TYPE_REVERT 4 #define BOOT_SWAP_TYPE_FAIL 0xff /** Swap failed because image to be run is not valid */ #define BOOT_SWAP_TYPE_FAIL 5 /** Swapping encountered an unrecoverable error */ #define BOOT_SWAP_TYPE_PANIC 0xff #define MAX_FLASH_ALIGN 8 extern const uint32_t BOOT_MAX_ALIGN; Loading
boot/bootutil/src/bootutil_misc.c +53 −99 Changes for boot/bootutil/src/bootutil_misc.c: 53 added lines, 99 removed lines. Original line number Diff line number Diff line Loading @@ -48,97 +48,49 @@ const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN; struct boot_swap_table { /** * For each field, a value of 0 means "any". */ uint8_t bsw_magic_slot0; uint8_t bsw_magic_slot1; uint8_t bsw_image_ok_slot0; uint8_t bsw_image_ok_slot1; uint8_t magic_slot0; uint8_t magic_slot1; uint8_t image_ok_slot0; uint8_t image_ok_slot1; uint8_t copy_done_slot0; uint8_t bsw_swap_type; uint8_t swap_type; }; /** * This set of tables maps image trailer contents to swap operation type. * When searching for a match, these tables must be iterated sequentially. * * NOTE: the table order is very important. The settings in Slot 1 always * are priority to Slot 0 and should be located earlier in the table. * * The table lists only states where there is action needs to be taken by * the bootloader, as in starting/finishing a swap operation. */ static const struct boot_swap_table boot_swap_tables[] = { { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Unset | Unset | * image-ok | Any | Any | * ---------+------------+------------' * swap: none | * -----------------------------------' */ .bsw_magic_slot0 = BOOT_MAGIC_UNSET, .bsw_magic_slot1 = BOOT_MAGIC_UNSET, .bsw_image_ok_slot0 = 0, .bsw_image_ok_slot1 = 0, .bsw_swap_type = BOOT_SWAP_TYPE_NONE, }, { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Any | Good | * image-ok | Any | Unset | * ---------+------------+------------` * swap: test | * -----------------------------------' */ .bsw_magic_slot0 = 0, .bsw_magic_slot1 = BOOT_MAGIC_GOOD, .bsw_image_ok_slot0 = 0, .bsw_image_ok_slot1 = 0xff, .bsw_swap_type = BOOT_SWAP_TYPE_TEST, }, { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Any | Good | * image-ok | Any | 0x01 | * ---------+------------+------------` * swap: permanent | * -----------------------------------' */ .bsw_magic_slot0 = 0, .bsw_magic_slot1 = BOOT_MAGIC_GOOD, .bsw_image_ok_slot0 = 0, .bsw_image_ok_slot1 = 0x01, .bsw_swap_type = BOOT_SWAP_TYPE_PERM, .magic_slot0 = 0, .magic_slot1 = BOOT_MAGIC_GOOD, .image_ok_slot0 = 0, .image_ok_slot1 = 0xff, .copy_done_slot0 = 0, .swap_type = BOOT_SWAP_TYPE_TEST, }, { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Good | Unset | * image-ok | 0xff | Any | * ---------+------------+------------' * swap: revert (test image running) | * -----------------------------------' */ .bsw_magic_slot0 = BOOT_MAGIC_GOOD, .bsw_magic_slot1 = BOOT_MAGIC_UNSET, .bsw_image_ok_slot0 = 0xff, .bsw_image_ok_slot1 = 0, .bsw_swap_type = BOOT_SWAP_TYPE_REVERT, .magic_slot0 = 0, .magic_slot1 = BOOT_MAGIC_GOOD, .image_ok_slot0 = 0, .image_ok_slot1 = 0x01, .copy_done_slot0 = 0, .swap_type = BOOT_SWAP_TYPE_PERM, }, { /* | slot-0 | slot-1 | *----------+------------+------------| * magic | Good | Unset | * image-ok | 0x01 | Any | * ---------+------------+------------' * swap: none (confirmed test image) | * -----------------------------------' */ .bsw_magic_slot0 = BOOT_MAGIC_GOOD, .bsw_magic_slot1 = BOOT_MAGIC_UNSET, .bsw_image_ok_slot0 = 0x01, .bsw_image_ok_slot1 = 0, .bsw_swap_type = BOOT_SWAP_TYPE_NONE, .magic_slot0 = BOOT_MAGIC_GOOD, .magic_slot1 = BOOT_MAGIC_UNSET, .image_ok_slot0 = 0xff, .image_ok_slot1 = 0, .copy_done_slot0 = 0x01, .swap_type = BOOT_SWAP_TYPE_REVERT, }, }; Loading Loading @@ -357,40 +309,42 @@ int boot_swap_type(void) { const struct boot_swap_table *table; struct boot_swap_state state_slot0; struct boot_swap_state state_slot1; struct boot_swap_state slot0; struct boot_swap_state slot1; int rc; int i; rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0); assert(rc == 0); rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0); if (rc) { return BOOT_SWAP_TYPE_PANIC; } rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1); assert(rc == 0); rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1); if (rc) { return BOOT_SWAP_TYPE_PANIC; } for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { table = boot_swap_tables + i; if ((table->bsw_magic_slot0 == 0 || table->bsw_magic_slot0 == state_slot0.magic) && (table->bsw_magic_slot1 == 0 || table->bsw_magic_slot1 == state_slot1.magic) && (table->bsw_image_ok_slot0 == 0 || table->bsw_image_ok_slot0 == state_slot0.image_ok) && (table->bsw_image_ok_slot1 == 0 || table->bsw_image_ok_slot1 == state_slot1.image_ok)) { if ((!table->magic_slot0 || table->magic_slot0 == slot0.magic ) && (!table->magic_slot1 || table->magic_slot1 == slot1.magic ) && (!table->image_ok_slot0 || table->image_ok_slot0 == slot0.image_ok ) && (!table->image_ok_slot1 || table->image_ok_slot1 == slot1.image_ok ) && (!table->copy_done_slot0 || table->copy_done_slot0 == slot0.copy_done)) { BOOT_LOG_INF("Swap type: %s", table->bsw_swap_type == BOOT_SWAP_TYPE_NONE ? "none" : table->bsw_swap_type == BOOT_SWAP_TYPE_TEST ? "test" : table->bsw_swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : table->bsw_swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : table->bsw_swap_type == BOOT_SWAP_TYPE_FAIL ? "fail" : table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : "BUG; can't happen"); return table->bsw_swap_type; assert(table->swap_type == BOOT_SWAP_TYPE_TEST || table->swap_type == BOOT_SWAP_TYPE_PERM || table->swap_type == BOOT_SWAP_TYPE_REVERT); return table->swap_type; } } assert(0); BOOT_LOG_INF("Swap type: none"); return BOOT_SWAP_TYPE_NONE; } Loading
boot/bootutil/src/loader.c +64 −73 Changes for boot/bootutil/src/loader.c: 64 added lines, 73 removed lines. Original line number Diff line number Diff line Loading @@ -126,21 +126,6 @@ static const struct boot_status_table boot_status_tables[] = { #define BOOT_STATUS_TABLES_COUNT \ (sizeof boot_status_tables / sizeof boot_status_tables[0]) /** * This table indicates the next swap type that should be performed. The first * column contains the current swap type. The second column contains the swap * type that should be effected after the first completes. */ static const uint8_t boot_swap_trans_table[][2] = { /* From To */ { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE }, { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE }, { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT }, }; #define BOOT_SWAP_TRANS_TABLE_SIZE \ (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0]) #define BOOT_LOG_SWAP_STATE(area, state) \ BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \ (area), \ Loading Loading @@ -201,25 +186,24 @@ boot_status_source(void) /** * Calculates the type of swap that just completed. * * This is used when a swap is interrupted by an external event. After * finishing the swap operation determines what the initial request was. */ static int boot_previous_swap_type(void) { int post_swap_type; int i; post_swap_type = boot_swap_type(); for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){ if (boot_swap_trans_table[i][1] == post_swap_type) { return boot_swap_trans_table[i][0]; switch (post_swap_type) { case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM; case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST; case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC; } } /* XXX: Temporary assert. */ assert(0); return BOOT_SWAP_TYPE_REVERT; return BOOT_SWAP_TYPE_FAIL; } static int Loading Loading @@ -605,18 +589,16 @@ static int boot_validated_swap_type(void) { int swap_type; int rc; swap_type = boot_swap_type(); if (swap_type == BOOT_SWAP_TYPE_NONE) { /* Continue using slot 0. */ return BOOT_SWAP_TYPE_NONE; } switch (swap_type) { case BOOT_SWAP_TYPE_TEST: case BOOT_SWAP_TYPE_PERM: case BOOT_SWAP_TYPE_REVERT: /* Boot loader wants to switch to slot 1. Ensure image is valid. */ rc = boot_validate_slot(1); if (rc != 0) { return BOOT_SWAP_TYPE_FAIL; if (boot_validate_slot(1) != 0) { swap_type = BOOT_SWAP_TYPE_FAIL; } } return swap_type; Loading Loading @@ -1095,10 +1077,10 @@ boot_copy_image(struct boot_status *bs) #endif /** * Marks a test image in slot 0 as fully copied. * Marks the image in slot 0 as fully copied. */ static int boot_finalize_test_swap(void) boot_set_copy_done(void) { const struct flash_area *fap; int rc; Loading @@ -1109,23 +1091,24 @@ boot_finalize_test_swap(void) } rc = boot_write_copy_done(fap); if (rc != 0) { flash_area_close(fap); return rc; } return 0; } /** * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure * the status bytes from the image revert operation don't get processed on a * subsequent boot. * * NOTE: image_ok is tested before writing because if there's a valid permanent * image installed on slot0 and the new image to be upgrade to has a bad sig, * image_ok would be overwritten. */ static int boot_finalize_revert_swap(void) boot_set_image_ok(void) { const struct flash_area *fap; struct boot_swap_state state_slot0; struct boot_swap_state state; int rc; rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); Loading @@ -1133,33 +1116,19 @@ boot_finalize_revert_swap(void) return BOOT_EFLASH; } rc = boot_read_swap_state(fap, &state_slot0); if (rc != 0) { return BOOT_EFLASH; } if (state_slot0.magic == BOOT_MAGIC_UNSET) { rc = boot_write_magic(fap); if (rc != 0) { return rc; } } if (state_slot0.copy_done == BOOT_FLAG_UNSET) { rc = boot_write_copy_done(fap); rc = boot_read_swap_state(fap, &state); if (rc != 0) { return rc; } rc = BOOT_EFLASH; goto out; } if (state_slot0.image_ok == BOOT_FLAG_UNSET) { if (state.image_ok == BOOT_FLAG_UNSET) { rc = boot_write_image_ok(fap); if (rc != 0) { return rc; } } return 0; out: flash_area_close(fap); return rc; } /** Loading Loading @@ -1191,6 +1160,12 @@ boot_swap_if_needed(int *out_swap_type) rc = boot_copy_image(&bs); assert(rc == 0); /* NOTE: here we have finished a swap resume. The initial request * was either a TEST or PERM swap, which now after the completed * swap will be determined to be respectively REVERT (was TEST) * or NONE (was PERM). */ /* Extrapolate the type of the partial swap. We need this * information to know how to mark the swap complete in flash. */ Loading Loading @@ -1269,6 +1244,17 @@ boot_go(struct boot_rsp *rsp) if (rc != 0) { goto out; } /* * The following states need image_ok be explicitly set after the * swap was finished to avoid a new revert. */ if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) { rc = boot_set_image_ok(); if (rc != 0) { swap_type = BOOT_SWAP_TYPE_PANIC; } } } else { swap_type = BOOT_SWAP_TYPE_NONE; } Loading @@ -1278,17 +1264,15 @@ boot_go(struct boot_rsp *rsp) slot = 0; break; case BOOT_SWAP_TYPE_TEST: case BOOT_SWAP_TYPE_PERM: slot = 1; boot_finalize_test_swap(); reload_headers = true; break; case BOOT_SWAP_TYPE_TEST: /* fallthrough */ case BOOT_SWAP_TYPE_PERM: /* fallthrough */ case BOOT_SWAP_TYPE_REVERT: slot = 1; boot_finalize_revert_swap(); reload_headers = true; rc = boot_set_copy_done(); if (rc != 0) { swap_type = BOOT_SWAP_TYPE_PANIC; } break; case BOOT_SWAP_TYPE_FAIL: Loading @@ -1297,14 +1281,19 @@ boot_go(struct boot_rsp *rsp) * we just reverted back to slot 0. */ slot = 0; boot_finalize_revert_swap(); reload_headers = true; break; default: swap_type = BOOT_SWAP_TYPE_PANIC; } if (swap_type == BOOT_SWAP_TYPE_PANIC) { BOOT_LOG_ERR("panic!"); assert(0); slot = 0; break; /* Loop forever... */ while (1) {} } #ifdef MCUBOOT_VALIDATE_SLOT0 Loading @@ -1321,6 +1310,8 @@ boot_go(struct boot_rsp *rsp) rc = BOOT_EBADIMAGE; goto out; } #else (void)reload_headers; #endif /* Always boot from the primary slot. */ Loading
sim/simflash/src/lib.rs +2 −2 Changes for sim/simflash/src/lib.rs: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -39,6 +39,7 @@ fn ebounds<T: AsRef<str>>(message: T) -> ErrorKind { ErrorKind::OutOfBounds(message.as_ref().to_owned()) } #[allow(dead_code)] fn ewrite<T: AsRef<str>>(message: T) -> ErrorKind { ErrorKind::Write(message.as_ref().to_owned()) } Loading Loading @@ -148,8 +149,7 @@ impl Flash for SimFlash { for (i, x) in &mut self.write_safe[offset .. offset + payload.len()].iter_mut().enumerate() { if !(*x) { bail!(ewrite(format!("Write to unerased location at 0x{:x}", offset + i))); panic!("Write to unerased location at 0x{:x}", offset + i); } *x = false; } Loading
sim/src/main.rs +147 −15 Changes for sim/src/main.rs: 147 added lines, 15 removed lines. Original line number Diff line number Diff line Loading @@ -150,7 +150,7 @@ fn main() { error!("{} Tests ran with {} failures", status.failures + status.passes, status.failures); process::exit(1); } else { warn!("{} Tests ran successfully", status.passes); error!("{} Tests ran successfully", status.passes); process::exit(0); } } Loading Loading @@ -245,17 +245,31 @@ impl RunStatus { trailer_off: scratch_base - offset_from_end, }; let images = Images { slot0: slot0, slot1: slot1, primary: install_image(&mut flash, slot0_base, 32784), upgrade: install_image(&mut flash, slot1_base, 41928), }; // Set an alignment, and position the magic value. c::set_sim_flash_align(align); let mut failed = false; // Set an alignment, and position the magic value. c::set_sim_flash_align(align); // Creates a badly signed image in slot1 to check that it is not // upgraded to let mut bad_flash = flash.clone(); let bad_slot1_image = Images { slot0: &slot0, slot1: &slot1, primary: install_image(&mut bad_flash, slot0_base, 32784, false), upgrade: install_image(&mut bad_flash, slot1_base, 41928, true), }; failed |= run_signfail_upgrade(&bad_flash, &areadesc, &bad_slot1_image); let images = Images { slot0: &slot0, slot1: &slot1, primary: install_image(&mut flash, slot0_base, 32784, false), upgrade: install_image(&mut flash, slot1_base, 41928, false), }; failed |= run_norevert_newimage(&flash, &areadesc, &images); mark_upgrade(&mut flash, &images.slot1); Loading Loading @@ -305,12 +319,13 @@ fn run_basic_upgrade(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) fn run_basic_revert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> 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(&flash, &areadesc, count); if !verify_image(&fl, images.slot0.base_off, &images.primary) { warn!("Revert failure on count {}", count); error!("Revert failure on count {}", count); fails += 1; } } Loading Loading @@ -353,8 +368,10 @@ fn run_perm_with_fails(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, } } info!("{} out of {} failed {:.2}%", fails, total_flash_ops, if fails > 0 { error!("{} out of {} failed {:.2}%", fails, total_flash_ops, fails as f32 * 100.0 / total_flash_ops as f32); } fails > 0 } Loading Loading @@ -390,6 +407,10 @@ fn run_perm_with_random_fails(flash: &SimFlash, areadesc: &AreaDesc, fails += 1; } if fails > 0 { error!("Error testing perm upgrade with {} fails", total_fails); } fails > 0 } Loading @@ -401,6 +422,7 @@ fn run_revert_with_fails(flash: &SimFlash, areadesc: &AreaDesc, images: &Images, for i in 1 .. (total_count - 1) { info!("Try interruption at {}", i); if try_revert_with_fail_at(&flash, &areadesc, &images, i) { error!("Revert failed at interruption {}", i); fails += 1; } } Loading @@ -422,6 +444,9 @@ fn run_norevert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool fails += 1; } //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) { warn!("Slot 0 image verification FAIL"); fails += 1; Loading @@ -440,6 +465,12 @@ fn run_norevert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool // Marks image in slot0 as permanent, no revert should happen... mark_permanent_upgrade(&mut fl, &images.slot0); if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK, COPY_DONE) { warn!("Mismatched trailer for Slot 0"); fails += 1; } if c::boot_go(&mut fl, &areadesc) != 0 { warn!("Failed second boot"); fails += 1; Loading @@ -455,6 +486,101 @@ fn run_norevert(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool fails += 1; } if fails > 0 { error!("Error running upgrade without revert"); } fails > 0 } // 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(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool { let mut fl = flash.clone(); let mut fails = 0; info!("Try non-revert on imgtool generated image"); c::set_flash_counter(0); mark_upgrade(&mut fl, &images.slot0); // This simulates writing an image created by imgtool to Slot 0 if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, UNSET, UNSET) { warn!("Mismatched trailer for Slot 0"); fails += 1; } // Run the bootloader... if c::boot_go(&mut fl, &areadesc) != 0 { warn!("Failed first boot"); fails += 1; } // State should not have changed if !verify_image(&fl, images.slot0.base_off, &images.primary) { warn!("Failed image verification"); fails += 1; } if !verify_trailer(&fl, images.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, UNSET) { warn!("Mismatched trailer for Slot 1"); fails += 1; } if fails > 0 { error!("Expected a non revert with new image"); } fails > 0 } // 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(flash: &SimFlash, areadesc: &AreaDesc, images: &Images) -> bool { let mut fl = flash.clone(); let mut fails = 0; info!("Try upgrade image with bad signature"); c::set_flash_counter(0); mark_upgrade(&mut fl, &images.slot0); mark_permanent_upgrade(&mut fl, &images.slot0); mark_upgrade(&mut fl, &images.slot1); if !verify_trailer(&fl, images.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, &areadesc) != 0 { warn!("Failed first boot"); fails += 1; } // State should not have changed if !verify_image(&fl, images.slot0.base_off, &images.primary) { warn!("Failed image verification"); fails += 1; } if !verify_trailer(&fl, images.slot0.trailer_off, MAGIC_VALID, IMAGE_OK, UNSET) { warn!("Mismatched trailer for Slot 0"); fails += 1; } if fails > 0 { error!("Expected an upgrade failure when image has bad signature"); } fails > 0 } Loading Loading @@ -617,7 +743,8 @@ fn show_flash(flash: &Flash) { /// Install a "program" into the given image. This fakes the image header, or at least all of the /// fields used by the given code. Returns a copy of the image that was written. fn install_image(flash: &mut Flash, offset: usize, len: usize) -> Vec<u8> { fn install_image(flash: &mut Flash, offset: usize, len: usize, bad_sig: bool) -> Vec<u8> { let offset0 = offset; let mut tlv = make_tlv(); Loading Loading @@ -657,7 +784,12 @@ fn install_image(flash: &mut Flash, offset: usize, len: usize) -> Vec<u8> { tlv.add_bytes(&buf); // Get and append the TLV itself. if bad_sig { let good_sig = &mut tlv.make_tlv(); buf.append(&mut vec![0; good_sig.len()]); } else { buf.append(&mut tlv.make_tlv()); } // Pad the block to a flash alignment (8 bytes). while buf.len() % 8 != 0 { Loading Loading @@ -780,9 +912,9 @@ struct SlotInfo { trailer_off: usize, } struct Images { slot0: SlotInfo, slot1: SlotInfo, struct Images<'a> { slot0: &'a SlotInfo, slot1: &'a SlotInfo, primary: Vec<u8>, upgrade: Vec<u8>, } Loading