subsys: dfu: boot: mcuboot: Query current img ver

Allow a running image to query its own version number.

Signed-off-by: Christopher Collins <ccollins@apache.org>
This commit is contained in:
Christopher Collins 2018-01-25 18:31:11 -08:00 committed by Carles Cufí
commit 2ad7ccdb2d
2 changed files with 59 additions and 2 deletions

View file

@ -28,6 +28,15 @@
/** Swap failed because image to be run is not valid */ /** Swap failed because image to be run is not valid */
#define BOOT_SWAP_TYPE_FAIL 5 #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 * @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_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 * @brief Determines the action, if any, that mcuboot will take on the next
* reboot. * reboot.
@ -165,4 +176,4 @@ int boot_request_upgrade(int permanent);
*/ */
int boot_erase_img_bank(u32_t bank_offset); int boot_erase_img_bank(u32_t bank_offset);
#endif /* __MCUBOOT_H__ */ #endif /* __MCUBOOT_H__ */

View file

@ -76,6 +76,7 @@ struct mcuboot_v1_raw_header {
#define FLASH_STATE_OFFSET (FLASH_AREA_IMAGE_SCRATCH_OFFSET +\ #define FLASH_STATE_OFFSET (FLASH_AREA_IMAGE_SCRATCH_OFFSET +\
FLASH_AREA_IMAGE_SCRATCH_SIZE) FLASH_AREA_IMAGE_SCRATCH_SIZE)
#define VERSION_OFFSET(bank_offs) (bank_offs + 20)
#define COPY_DONE_OFFS(bank_offs) (bank_offs + FLASH_BANK_SIZE -\ #define COPY_DONE_OFFS(bank_offs) (bank_offs + FLASH_BANK_SIZE -\
BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2) BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2)
@ -91,7 +92,7 @@ static const u32_t boot_img_magic[4] = {
}; };
struct boot_swap_table { 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_slot0;
u8_t magic_slot1; u8_t magic_slot1;
u8_t image_ok_slot0; 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); 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) static int boot_magic_write(u32_t bank_offs)
{ {
u32_t offs; u32_t offs;
@ -344,6 +353,38 @@ int boot_read_bank_header(u32_t bank_offset,
return 0; 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) static int boot_read_swap_state(u32_t bank_offs, struct boot_swap_state *state)
{ {
int rc; int rc;
@ -371,6 +412,11 @@ static int boot_read_swap_state(u32_t bank_offs, struct boot_swap_state *state)
return 0; 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) int boot_swap_type(void)
{ {
const struct boot_swap_table *table; const struct boot_swap_table *table;