From 2ad7ccdb2de8c1ccf3ba94a2eb05b3a679f0cad5 Mon Sep 17 00:00:00 2001 From: Christopher Collins Date: Thu, 25 Jan 2018 18:31:11 -0800 Subject: [PATCH] subsys: dfu: boot: mcuboot: Query current img ver Allow a running image to query its own version number. Signed-off-by: Christopher Collins --- include/dfu/mcuboot.h | 13 ++++++++++- subsys/dfu/boot/mcuboot.c | 48 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/include/dfu/mcuboot.h b/include/dfu/mcuboot.h index 27df47a83fb..5b3f8e6d408 100644 --- a/include/dfu/mcuboot.h +++ b/include/dfu/mcuboot.h @@ -28,6 +28,15 @@ /** Swap failed because image to be run is not valid */ #define BOOT_SWAP_TYPE_FAIL 5 +#define BOOT_IMG_VER_STRLEN_MAX 25 /* 255.255.65535.4294967295\0 */ + +struct image_version { + u8_t iv_major; + u8_t iv_minor; + u16_t iv_revision; + u32_t iv_build_num; +}; + /** * @brief MCUboot image header representation for image version * @@ -137,6 +146,8 @@ bool boot_is_img_confirmed(void); */ int boot_write_img_confirmed(void); +int boot_current_image_version(struct image_version *out_ver); + /** * @brief Determines the action, if any, that mcuboot will take on the next * reboot. @@ -165,4 +176,4 @@ int boot_request_upgrade(int permanent); */ int boot_erase_img_bank(u32_t bank_offset); -#endif /* __MCUBOOT_H__ */ +#endif /* __MCUBOOT_H__ */ diff --git a/subsys/dfu/boot/mcuboot.c b/subsys/dfu/boot/mcuboot.c index 73249e8db85..978b9ffc24e 100644 --- a/subsys/dfu/boot/mcuboot.c +++ b/subsys/dfu/boot/mcuboot.c @@ -76,6 +76,7 @@ struct mcuboot_v1_raw_header { #define FLASH_STATE_OFFSET (FLASH_AREA_IMAGE_SCRATCH_OFFSET +\ FLASH_AREA_IMAGE_SCRATCH_SIZE) +#define VERSION_OFFSET(bank_offs) (bank_offs + 20) #define COPY_DONE_OFFS(bank_offs) (bank_offs + FLASH_BANK_SIZE -\ BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2) @@ -91,7 +92,7 @@ static const u32_t boot_img_magic[4] = { }; struct boot_swap_table { - /** * For each field, a value of 0 means "any". */ + /** For each field, a value of 0 means "any". */ u8_t magic_slot0; u8_t magic_slot1; u8_t image_ok_slot0; @@ -252,6 +253,14 @@ static int boot_copy_done_read(u32_t bank_offs) return boot_flag_read(BOOT_FLAG_COPY_DONE, bank_offs); } +static int boot_version_read(u32_t bank_offs, struct image_version *out_ver) +{ + u32_t offs; + + offs = VERSION_OFFSET(bank_offs); + return flash_read(flash_dev, offs, out_ver, sizeof(*out_ver)); +} + static int boot_magic_write(u32_t bank_offs) { u32_t offs; @@ -344,6 +353,38 @@ int boot_read_bank_header(u32_t bank_offset, return 0; } +static int boot_magic_code_check(const u32_t *magic) +{ + int i; + + if (memcmp(magic, boot_img_magic, sizeof(boot_img_magic)) == 0) { + return BOOT_MAGIC_GOOD; + } + + for (i = 0; i < ARRAY_SIZE(boot_img_magic); i++) { + if (magic[i] != 0xffffffff) { + return BOOT_MAGIC_BAD; + } + } + + return BOOT_MAGIC_UNSET; +} + +static int boot_magic_state_read(u32_t bank_offs) +{ + u32_t magic[4]; + u32_t offs; + int rc; + + offs = MAGIC_OFFS(bank_offs); + rc = flash_read(flash_dev, offs, magic, sizeof(magic)); + if (rc != 0) { + return rc; + } + + return boot_magic_code_check(magic); +} + static int boot_read_swap_state(u32_t bank_offs, struct boot_swap_state *state) { int rc; @@ -371,6 +412,11 @@ static int boot_read_swap_state(u32_t bank_offs, struct boot_swap_state *state) return 0; } +int boot_current_image_version(struct image_version *out_ver) +{ + return boot_version_read(FLASH_BANK0_OFFSET, out_ver); +} + int boot_swap_type(void) { const struct boot_swap_table *table;