Skip to content

Commit b4e5a3b

Browse files
de-nordicrlubos
authored andcommitted
[nrf noup] bootutil: Enable hash calculation directly on storage
The commit add support for passing storage device address space to hash calculation functions, which allows to use hardware accelerated hash calculation on storage. This feature only works when image encryption is not enabled and all slots are defined within internal storage of device. The feature is enabled using Kconfig option CONFIG_BOOT_IMG_HASH_DIRECTLY_ON_STORAGE Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit 258b369)
1 parent 10211d4 commit b4e5a3b

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

boot/bootutil/src/image_validate.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
7777
uint8_t *seed, int seed_len)
7878
{
7979
bootutil_sha_context sha_ctx;
80-
uint32_t blk_sz;
8180
uint32_t size;
8281
uint16_t hdr_size;
83-
uint32_t off;
84-
int rc;
8582
uint32_t blk_off;
8683
uint32_t tlv_off;
84+
#if !defined(MCUBOOT_HASH_STORAGE_DIRECTLY)
85+
int rc;
86+
uint32_t off;
87+
uint32_t blk_sz;
88+
#endif
8789

8890
#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \
8991
defined(MCUBOOT_RAM_LOAD)
@@ -126,6 +128,12 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
126128
/* If protected TLVs are present they are also hashed. */
127129
size += hdr->ih_protect_tlv_size;
128130

131+
#ifdef MCUBOOT_HASH_STORAGE_DIRECTLY
132+
/* No chunk loading, storage is mapped to address space and can
133+
* be directly given to hashing function.
134+
*/
135+
bootutil_sha_update(&sha_ctx, (void *)flash_area_get_off(fap), size);
136+
#else /* MCUBOOT_HASH_STORAGE_DIRECTLY */
129137
#ifdef MCUBOOT_RAM_LOAD
130138
bootutil_sha_update(&sha_ctx,
131139
(void*)(IMAGE_RAM_BASE + hdr->ih_load_addr),
@@ -170,6 +178,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
170178
bootutil_sha_update(&sha_ctx, tmp_buf, blk_sz);
171179
}
172180
#endif /* MCUBOOT_RAM_LOAD */
181+
#endif /* MCUBOOT_HASH_STORAGE_DIRECTLY */
173182
bootutil_sha_finish(&sha_ctx, hash_result);
174183
bootutil_sha_drop(&sha_ctx);
175184

boot/zephyr/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ config BOOT_IMG_HASH_ALG_SHA512_ALLOW
146146
help
147147
Hidden option set by configurations that allow SHA512
148148

149+
config BOOT_IMG_HASH_DIRECTLY_ON_STORAGE
150+
bool "Hash calculation functions access storage through address space"
151+
depends on !BOOT_ENCRYPT_IMAGE
152+
help
153+
When possible to map storage device, at least for read operations,
154+
to address space or RAM area, enabling this option allows hash
155+
calculation functions to directly access the storage through that address
156+
space or using its own DMA. This reduces flash read overhead done
157+
by the MCUboot.
158+
Notes:
159+
- not supported when encrypted images are in use, because calculating
160+
SHA requires image to be decrypted first, which is done to RAM.
161+
- currently only supported on internal storage of devices; this
162+
option will not work with devices that use external storage for
163+
either of image slots.
164+
149165
choice BOOT_IMG_HASH_ALG
150166
prompt "Selected image hash algorithm"
151167
default BOOT_IMG_HASH_ALG_SHA256 if BOOT_IMG_HASH_ALG_SHA256_ALLOW

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@
140140
#define MCUBOOT_DECOMPRESS_IMAGES
141141
#endif
142142

143+
/* Invoke hashing functions directly on storage. This requires for device
144+
* to be able to map storage to address space or RAM.
145+
*/
146+
#ifdef CONFIG_BOOT_IMG_HASH_DIRECTLY_ON_STORAGE
147+
#define MCUBOOT_HASH_STORAGE_DIRECTLY
148+
#endif
149+
143150
#ifdef CONFIG_BOOT_BOOTSTRAP
144151
#define MCUBOOT_BOOTSTRAP 1
145152
#endif

0 commit comments

Comments
 (0)