Skip to content

Commit b58d435

Browse files
nordicjmcarlescufi
authored andcommitted
mgmt: mcumgr: img_mgmt: Prevent re-upload of duplicate image data
This adds a hash check when the CONFIG_IMG_ENABLE_IMAGE_CHECK Kconfig option is enabled that will check the underlying image hash to see if it is the same as the one provided by the mcumgr client, and if so, will prevent erasure/uploading the same image data. Signed-off-by: Jamie McCrae <[email protected]>
1 parent a65fe8b commit b58d435

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

subsys/mgmt/mcumgr/lib/cmd/img_mgmt/src/img_mgmt.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include "img_mgmt_priv.h"
2424
#include "img_mgmt/img_mgmt_config.h"
2525

26+
#ifdef CONFIG_IMG_ENABLE_IMAGE_CHECK
27+
#include <zephyr/dfu/flash_img.h>
28+
#endif
29+
2630
static img_mgmt_upload_fn img_mgmt_upload_cb;
2731

2832
const struct img_mgmt_dfu_callbacks_t *img_mgmt_dfu_callbacks_fn;
@@ -416,6 +420,11 @@ img_mgmt_upload(struct mgmt_ctxt *ctxt)
416420
/*
417421
* New upload.
418422
*/
423+
#ifdef CONFIG_IMG_ENABLE_IMAGE_CHECK
424+
struct flash_img_context ctx;
425+
struct flash_img_check fic;
426+
#endif
427+
419428
g_img_mgmt_state.off = 0;
420429

421430
img_mgmt_dfu_started();
@@ -431,6 +440,23 @@ img_mgmt_upload(struct mgmt_ctxt *ctxt)
431440
memset(&g_img_mgmt_state.data_sha[req.data_sha.len], 0,
432441
IMG_MGMT_DATA_SHA_LEN - req.data_sha.len);
433442

443+
#ifdef CONFIG_IMG_ENABLE_IMAGE_CHECK
444+
/* Check if the existing image hash matches the hash of the underlying data */
445+
fic.match = g_img_mgmt_state.data_sha;
446+
fic.clen = g_img_mgmt_state.size;
447+
448+
if (flash_img_check(&ctx, &fic, g_img_mgmt_state.area_id) == 0) {
449+
/* Underlying data already matches, no need to upload any more, set offset
450+
* to image size so client knows upload has finished.
451+
*/
452+
g_img_mgmt_state.off = g_img_mgmt_state.size;
453+
img_mgmt_dfu_pending();
454+
cmd_status_arg.status = IMG_MGMT_ID_UPLOAD_STATUS_COMPLETE;
455+
g_img_mgmt_state.area_id = -1;
456+
goto end;
457+
}
458+
#endif
459+
434460
#ifndef CONFIG_IMG_ERASE_PROGRESSIVELY
435461
/* erase the entire req.size all at once */
436462
if (action.erase) {

0 commit comments

Comments
 (0)