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

@ -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;