Skip to content

Commit 9d91a87

Browse files
committed
[nrf noup] Enable hash calculation direclty 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]>
1 parent d550335 commit 9d91a87

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

boot/bootutil/src/image_validate.c

Lines changed: 15 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,15 @@ 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+
133+
/* No chunk loading, storage is mapped to address space and can
134+
* be directly given to hashing function.
135+
*/
136+
bootutil_sha_update(&sha_ctx, (void *)flash_area_get_off(fap), size);
137+
138+
#else /* MCUBOOT_HASH_STORAGE_DIRECTLY */
139+
129140
#ifdef MCUBOOT_RAM_LOAD
130141
bootutil_sha_update(&sha_ctx,
131142
(void*)(IMAGE_RAM_BASE + hdr->ih_load_addr),
@@ -170,6 +181,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
170181
bootutil_sha_update(&sha_ctx, tmp_buf, blk_sz);
171182
}
172183
#endif /* MCUBOOT_RAM_LOAD */
184+
#endif /* MCUBOOT_HASH_STORAGE_DIRECTLY */
173185
bootutil_sha_finish(&sha_ctx, hash_result);
174186
bootutil_sha_drop(&sha_ctx);
175187

boot/zephyr/Kconfig

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

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

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,18 @@
137137
#endif
138138

139139
#ifdef CONFIG_BOOT_DECOMPRESSION
140+
140141
#define MCUBOOT_DECOMPRESS_IMAGES
141142
#endif
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+
#ifdef MCUBOOT_ENC_IMAGES
148+
#error "Direct hash check is currently not supported when encrypted images are enabled"
149+
#endif
150+
#define MCUBOOT_HASH_STORAGE_DIRECTLY
151+
#endif
142152

143153
#ifdef CONFIG_BOOT_BOOTSTRAP
144154
#define MCUBOOT_BOOTSTRAP 1

0 commit comments

Comments
 (0)