@@ -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
122122static 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+
124129static zcbor_state_t cbor_state [2 ];
125130
126131void 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
0 commit comments