Skip to content

Commit 827118f

Browse files
nordicjmde-nordic
authored andcommitted
boot: serial_recovery: Add image hash support
Adds support for outputting the image hash TLV in serial recovery mode, which is needed to comply with the img_mgmt MCUmgr group requirements. Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
1 parent f5e7753 commit 827118f

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

boot/boot_serial/src/boot_serial.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
8282
#define MCUBOOT_SERIAL_MAX_RECEIVE_SIZE 512
8383
#endif
8484

85-
#define BOOT_SERIAL_OUT_MAX (128 * BOOT_IMAGE_NUMBER)
85+
#define BOOT_SERIAL_OUT_MAX (160 * BOOT_IMAGE_NUMBER)
8686
#define BOOT_SERIAL_FRAME_MTU 124 /* 127 - pkt start (2 bytes) and stop (1 byte) */
8787

8888
#ifdef __ZEPHYR__
@@ -121,6 +121,11 @@ static char bs_obuf[BOOT_SERIAL_OUT_MAX];
121121

122122
static void boot_serial_output(void);
123123

124+
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
125+
static int boot_serial_get_hash(const struct image_header *hdr,
126+
const struct flash_area *fap, uint8_t *hash);
127+
#endif
128+
124129
static zcbor_state_t cbor_state[2];
125130

126131
void reset_cbor_state(void)
@@ -217,6 +222,9 @@ bs_list(char *buf, int len)
217222
uint32_t slot, area_id;
218223
const struct flash_area *fap;
219224
uint8_t image_index;
225+
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
226+
uint8_t hash[32];
227+
#endif
220228

221229
zcbor_map_start_encode(cbor_state, 1);
222230
zcbor_tstr_put_lit_cast(cbor_state, "images");
@@ -261,6 +269,11 @@ bs_list(char *buf, int len)
261269
}
262270
}
263271

272+
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
273+
/* Retrieve SHA256 hash of image for identification */
274+
rc = boot_serial_get_hash(&hdr, fap, hash);
275+
#endif
276+
264277
flash_area_close(fap);
265278

266279
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
@@ -276,9 +289,18 @@ bs_list(char *buf, int len)
276289

277290
zcbor_tstr_put_lit_cast(cbor_state, "slot");
278291
zcbor_uint32_put(cbor_state, slot);
292+
293+
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
294+
if (rc == 0) {
295+
zcbor_tstr_put_lit_cast(cbor_state, "hash");
296+
zcbor_bstr_encode_ptr(cbor_state, hash, sizeof(hash));
297+
}
298+
#endif
299+
279300
zcbor_tstr_put_lit_cast(cbor_state, "version");
280301

281302
bs_list_img_ver((char *)tmpbuf, sizeof(tmpbuf), &hdr.ih_ver);
303+
282304
zcbor_tstr_encode_ptr(cbor_state, tmpbuf, strlen((char *)tmpbuf));
283305
zcbor_map_end_encode(cbor_state, 20);
284306
}
@@ -985,3 +1007,50 @@ boot_serial_check_start(const struct boot_uart_funcs *f, int timeout_in_ms)
9851007
boot_serial_read_console(f,timeout_in_ms);
9861008
}
9871009
#endif
1010+
1011+
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
1012+
/* Function to find the hash of an image, returns 0 on success. */
1013+
static int boot_serial_get_hash(const struct image_header *hdr,
1014+
const struct flash_area *fap, uint8_t *hash)
1015+
{
1016+
struct image_tlv_iter it;
1017+
uint32_t offset;
1018+
uint16_t len;
1019+
uint16_t type;
1020+
int rc;
1021+
1022+
/* Manifest data is concatenated to the end of the image.
1023+
* It is encoded in TLV format.
1024+
*/
1025+
rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
1026+
if (rc) {
1027+
return -1;
1028+
}
1029+
1030+
/* Traverse through the TLV area to find the image hash TLV. */
1031+
while (true) {
1032+
rc = bootutil_tlv_iter_next(&it, &offset, &len, &type);
1033+
if (rc < 0) {
1034+
return -1;
1035+
} else if (rc > 0) {
1036+
break;
1037+
}
1038+
1039+
if (type == IMAGE_TLV_SHA256) {
1040+
/* Get the image's hash value from the manifest section. */
1041+
if (len != 32) {
1042+
return -1;
1043+
}
1044+
1045+
rc = flash_area_read(fap, offset, hash, len);
1046+
if (rc) {
1047+
return -1;
1048+
}
1049+
1050+
return 0;
1051+
}
1052+
}
1053+
1054+
return -1;
1055+
}
1056+
#endif

boot/zephyr/Kconfig.serial_recovery

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,11 @@ config BOOT_SERIAL_WAIT_FOR_DFU_TIMEOUT
168168
help
169169
timeout in ms for MCUboot to wait to allow for DFU to be invoked.
170170

171+
config BOOT_SERIAL_IMG_GRP_HASH
172+
bool "Image list hash support"
173+
default y
174+
help
175+
If y, image list responses will include the image hash (adds ~100
176+
bytes of flash).
177+
171178
endif # MCUBOOT_SERIAL

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@
205205
#define MCUBOOT_SERIAL_WAIT_FOR_DFU
206206
#endif
207207

208+
#ifdef CONFIG_BOOT_SERIAL_IMG_GRP_HASH
209+
#define MCUBOOT_SERIAL_IMG_GRP_HASH
210+
#endif
211+
208212
/*
209213
* The option enables code, currently in boot_serial, that attempts
210214
* to erase flash progressively, as update fragments are received,

0 commit comments

Comments
 (0)