Remove current_image global and macro updates
Currently to determine which image is being operated on, there is a global
variable called current_image which is used by most macros and APIs to
correctly access the flash areas required by the bootloader. This moves
this variable to the already existing state struct and refactors all
macros and APIs to receive the current image by parameters. To maintain
compatibility some of the macros were not updated and use image 0 when
called.
The definitions of FLASH_AREA_IMAGE_PRIMARY and FLASH_AREA_IMAGE_SECONDARY
for Mynewt compatibility were moved out of bootutil sources to a Mynewt
specific include file.
Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/bootutil/src/image_validate.c b/boot/bootutil/src/image_validate.c
index b4defb2..256d125 100644
--- a/boot/bootutil/src/image_validate.c
+++ b/boot/bootutil/src/image_validate.c
@@ -51,9 +51,10 @@
* Compute SHA256 over the image.
*/
static int
-bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
- uint8_t *tmp_buf, uint32_t tmp_buf_sz,
- uint8_t *hash_result, uint8_t *seed, int seed_len)
+bootutil_img_hash(int image_index, struct image_header *hdr,
+ const struct flash_area *fap, uint8_t *tmp_buf,
+ uint32_t tmp_buf_sz, uint8_t *hash_result, uint8_t *seed,
+ int seed_len)
{
bootutil_sha256_context sha256_ctx;
uint32_t blk_sz;
@@ -65,6 +66,10 @@
uint32_t blk_off;
#endif
+#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
+ (void)image_index;
+#endif
+
bootutil_sha256_init(&sha256_ctx);
/* in some cases (split image) the hash is seeded with data from
@@ -75,9 +80,8 @@
#ifdef MCUBOOT_ENC_IMAGES
/* Encrypted images only exist in the secondary slot */
- if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY &&
- IS_ENCRYPTED(hdr) &&
- !boot_enc_valid(fap)) {
+ if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
+ IS_ENCRYPTED(hdr) && !boot_enc_valid(image_index, fap)) {
return -1;
}
#endif
@@ -115,11 +119,11 @@
return rc;
}
#ifdef MCUBOOT_ENC_IMAGES
- if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY &&
- IS_ENCRYPTED(hdr) &&
- off >= hdr_size) {
+ if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
+ IS_ENCRYPTED(hdr) && off >= hdr_size) {
blk_off = (off - hdr_size) & 0xf;
- boot_encrypt(fap, off - hdr_size, blk_sz, blk_off, tmp_buf);
+ boot_encrypt(image_index, fap, off - hdr_size, blk_sz, blk_off,
+ tmp_buf);
}
#endif
bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
@@ -197,9 +201,10 @@
* Return non-zero if image could not be validated/does not validate.
*/
int
-bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
- uint8_t *tmp_buf, uint32_t tmp_buf_sz,
- uint8_t *seed, int seed_len, uint8_t *out_hash)
+bootutil_img_validate(int image_index, struct image_header *hdr,
+ const struct flash_area *fap, uint8_t *tmp_buf,
+ uint32_t tmp_buf_sz, uint8_t *seed, int seed_len,
+ uint8_t *out_hash)
{
uint32_t off;
uint32_t end;
@@ -214,7 +219,8 @@
uint8_t hash[32];
int rc;
- rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len);
+ rc = bootutil_img_hash(image_index, hdr, fap, tmp_buf, tmp_buf_sz, hash,
+ seed, seed_len);
if (rc) {
return rc;
}