1 /*
2  * Copyright (c) 2018-2023 O.S.Systems
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_DECLARE(updatehub, CONFIG_UPDATEHUB_LOG_LEVEL);
9 
10 #include <zephyr/dfu/mcuboot.h>
11 #include <zephyr/storage/flash_map.h>
12 #include <zephyr/sys/printk.h>
13 
14 #include "updatehub_firmware.h"
15 
updatehub_get_firmware_version(const uint32_t partition_id,char * version,int version_len)16 bool updatehub_get_firmware_version(const uint32_t partition_id,
17 				    char *version, int version_len)
18 {
19 	struct mcuboot_img_header header;
20 
21 	if (boot_read_bank_header(partition_id, &header,
22 				  sizeof(struct mcuboot_img_header)) != 0) {
23 		LOG_DBG("Error when executing boot_read_bank_header function");
24 		return false;
25 	}
26 
27 	if (header.mcuboot_version != 1) {
28 		LOG_DBG("MCUboot header version not supported!");
29 		return false;
30 	}
31 
32 	snprintk(version, version_len, "%d.%d.%d",
33 		 header.h.v1.sem_ver.major,
34 		 header.h.v1.sem_ver.minor,
35 		 header.h.v1.sem_ver.revision);
36 
37 	return true;
38 }
39