Skip to content

Commit ff6b526

Browse files
nandojvecarlescufi
authored andcommitted
mgmt: updatehub: Add storage abstraction
This add storage abstraction to allow switch between different flash APIs. This remove the erase command at updatehub core and move it to storage init phase. Signed-off-by: Gerson Fernando Budke <[email protected]>
1 parent eb39f1f commit ff6b526

File tree

5 files changed

+141
-38
lines changed

5 files changed

+141
-38
lines changed

subsys/mgmt/updatehub/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zephyr_library_sources_ifdef(CONFIG_UPDATEHUB updatehub_device.c)
1111
zephyr_library_sources_ifdef(CONFIG_UPDATEHUB updatehub_firmware.c)
1212
zephyr_library_sources_ifdef(CONFIG_UPDATEHUB updatehub_timer.c)
1313
zephyr_library_sources_ifdef(CONFIG_UPDATEHUB updatehub_integrity.c)
14+
zephyr_library_sources_ifdef(CONFIG_UPDATEHUB updatehub_storage.c)
1415
zephyr_library_sources_ifdef(CONFIG_UPDATEHUB_SHELL shell.c)
1516

1617
zephyr_include_directories(

subsys/mgmt/updatehub/shell.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
*/
66

77
#include <zephyr/shell/shell.h>
8-
#include <zephyr/drivers/flash.h>
9-
#include <zephyr/dfu/mcuboot.h>
10-
#include <zephyr/dfu/flash_img.h>
8+
119
#include "include/updatehub.h"
1210
#include "updatehub_firmware.h"
1311
#include "updatehub_device.h"
12+
#include "updatehub_storage.h"
1413

1514
#if defined(CONFIG_UPDATEHUB_CE)
1615
#define UPDATEHUB_SERVER CONFIG_UPDATEHUB_SERVER
@@ -59,7 +58,7 @@ static int cmd_info(const struct shell *shell, size_t argc, char **argv)
5958
char *firmware_version = k_malloc(FIRMWARE_IMG_VER_STRLEN_MAX);
6059

6160
updatehub_get_device_identity(device_id, DEVICE_ID_HEX_MAX_SIZE);
62-
updatehub_get_firmware_version(FIXED_PARTITION_ID(slot0_partition),
61+
updatehub_get_firmware_version(UPDATEHUB_SLOT_PARTITION_0,
6362
firmware_version,
6463
FIRMWARE_IMG_VER_STRLEN_MAX);
6564

subsys/mgmt/updatehub/updatehub.c

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,23 @@
77
#include <zephyr/logging/log.h>
88
LOG_MODULE_REGISTER(updatehub, CONFIG_UPDATEHUB_LOG_LEVEL);
99

10-
#include <zephyr/kernel.h>
11-
1210
#include <zephyr/logging/log_ctrl.h>
1311
#include <zephyr/net/socket.h>
1412
#include <zephyr/net/net_mgmt.h>
1513
#include <zephyr/net/net_ip.h>
1614
#include <zephyr/net/udp.h>
1715
#include <zephyr/net/coap.h>
1816
#include <zephyr/net/dns_resolve.h>
19-
#include <zephyr/drivers/flash.h>
2017
#include <zephyr/sys/reboot.h>
2118
#include <zephyr/data/json.h>
22-
#include <zephyr/dfu/mcuboot.h>
23-
#include <zephyr/storage/flash_map.h>
2419

2520
#include "include/updatehub.h"
2621
#include "updatehub_priv.h"
2722
#include "updatehub_firmware.h"
2823
#include "updatehub_device.h"
2924
#include "updatehub_timer.h"
3025
#include "updatehub_integrity.h"
26+
#include "updatehub_storage.h"
3127

3228
#if defined(CONFIG_UPDATEHUB_DTLS)
3329
#define CA_CERTIFICATE_TAG 1
@@ -64,7 +60,7 @@ LOG_MODULE_REGISTER(updatehub, CONFIG_UPDATEHUB_LOG_LEVEL);
6460
static struct updatehub_context {
6561
struct coap_block_context block;
6662
struct k_sem semaphore;
67-
struct flash_img_context flash_ctx;
63+
struct updatehub_storage_context storage_ctx;
6864
struct updatehub_crypto_context crypto_ctx;
6965
enum updatehub_response code_status;
7066
uint8_t hash[SHA256_BIN_DIGEST_SIZE];
@@ -416,9 +412,6 @@ static int install_update_cb_check_blk_num(const struct coap_packet *resp)
416412
static void install_update_cb(void)
417413
{
418414
struct coap_packet response_packet;
419-
#ifdef _STORAGE_SHA256_VERIFICATION
420-
struct flash_img_check fic;
421-
#endif
422415
uint8_t *data = k_malloc(MAX_DOWNLOAD_DATA);
423416
const uint8_t *payload_start;
424417
uint16_t payload_len;
@@ -469,14 +462,8 @@ static void install_update_cb(void)
469462
}
470463
#endif
471464

472-
LOG_DBG("Flash: Address: 0x%08x, Size: %d, Flush: %s",
473-
ctx.flash_ctx.stream.bytes_written, payload_len,
474-
(ctx.downloaded_size == ctx.block.total_size ?
475-
"True" : "False"));
476-
477-
if (flash_img_buffered_write(&ctx.flash_ctx,
478-
payload_start, payload_len,
479-
ctx.downloaded_size == ctx.block.total_size) < 0) {
465+
if (updatehub_storage_write(&ctx.storage_ctx, payload_start, payload_len,
466+
ctx.downloaded_size == ctx.block.total_size)) {
480467
LOG_ERR("Error to write on the flash");
481468
ctx.code_status = UPDATEHUB_INSTALL_ERROR;
482469
goto cleanup;
@@ -513,11 +500,9 @@ static void install_update_cb(void)
513500
#endif
514501

515502
#ifdef _STORAGE_SHA256_VERIFICATION
516-
fic.match = ctx.hash;
517-
fic.clen = ctx.downloaded_size;
518-
519-
if (flash_img_check(&ctx.flash_ctx, &fic,
520-
FIXED_PARTITION_ID(slot1_partition))) {
503+
if (updatehub_storage_check(&ctx.storage_ctx,
504+
UPDATEHUB_SLOT_PARTITION_1,
505+
ctx.hash, ctx.downloaded_size)) {
521506
LOG_ERR("Firmware - flash validation has failed");
522507
ctx.code_status = UPDATEHUB_INSTALL_ERROR;
523508
goto cleanup;
@@ -533,12 +518,6 @@ static void install_update_cb(void)
533518

534519
static enum updatehub_response install_update(void)
535520
{
536-
if (boot_erase_img_bank(FIXED_PARTITION_ID(slot1_partition)) != 0) {
537-
LOG_ERR("Failed to init flash and erase second slot");
538-
ctx.code_status = UPDATEHUB_FLASH_INIT_ERROR;
539-
goto error;
540-
}
541-
542521
#ifdef _DOWNLOAD_SHA256_VERIFICATION
543522
if (updatehub_integrity_init(&ctx.crypto_ctx)) {
544523
LOG_ERR("Could not start sha256sum");
@@ -560,7 +539,8 @@ static enum updatehub_response install_update(void)
560539
goto cleanup;
561540
}
562541

563-
if (flash_img_init(&ctx.flash_ctx)) {
542+
if (updatehub_storage_init(&ctx.storage_ctx,
543+
UPDATEHUB_SLOT_PARTITION_1)) {
564544
LOG_ERR("Unable init flash");
565545
ctx.code_status = UPDATEHUB_FLASH_INIT_ERROR;
566546
goto cleanup;
@@ -632,7 +612,7 @@ static int report(enum updatehub_state state)
632612
goto error;
633613
}
634614

635-
if (!updatehub_get_firmware_version(FIXED_PARTITION_ID(slot0_partition),
615+
if (!updatehub_get_firmware_version(UPDATEHUB_SLOT_PARTITION_0,
636616
firmware_version,
637617
FIRMWARE_IMG_VER_STRLEN_MAX)) {
638618
goto error;
@@ -765,7 +745,7 @@ static void probe_cb(char *metadata, size_t metadata_size)
765745

766746
int updatehub_confirm(void)
767747
{
768-
return boot_write_img_confirmed();
748+
return updatehub_storage_mark_partition_as_confirmed(UPDATEHUB_SLOT_PARTITION_0);
769749
}
770750

771751
int updatehub_reboot(void)
@@ -795,13 +775,13 @@ enum updatehub_response updatehub_probe(void)
795775
goto error;
796776
}
797777

798-
if (!boot_is_img_confirmed()) {
778+
if (!updatehub_storage_is_partition_good(&ctx.storage_ctx)) {
799779
LOG_ERR("The current image is not confirmed");
800780
ctx.code_status = UPDATEHUB_UNCONFIRMED_IMAGE;
801781
goto error;
802782
}
803783

804-
if (!updatehub_get_firmware_version(FIXED_PARTITION_ID(slot0_partition),
784+
if (!updatehub_get_firmware_version(UPDATEHUB_SLOT_PARTITION_0,
805785
firmware_version,
806786
FIRMWARE_IMG_VER_STRLEN_MAX)) {
807787
ctx.code_status = UPDATEHUB_METADATA_ERROR;
@@ -960,7 +940,8 @@ enum updatehub_response updatehub_update(void)
960940
goto error;
961941
}
962942

963-
if (boot_request_upgrade(BOOT_UPGRADE_TEST)) {
943+
if (updatehub_storage_mark_partition_to_upgrade(&ctx.storage_ctx,
944+
UPDATEHUB_SLOT_PARTITION_1)) {
964945
LOG_ERR("Could not reporting downloaded state");
965946
ctx.code_status = UPDATEHUB_INSTALL_ERROR;
966947
goto error;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 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+
12+
#include "updatehub_storage.h"
13+
14+
int updatehub_storage_is_partition_good(struct updatehub_storage_context *ctx)
15+
{
16+
bool ret;
17+
18+
if (ctx == NULL) {
19+
return -EINVAL;
20+
}
21+
22+
ret = boot_is_img_confirmed() == 0;
23+
24+
return ret ? 0 : -EIO;
25+
}
26+
27+
int updatehub_storage_init(struct updatehub_storage_context *ctx,
28+
const uint32_t partition_id)
29+
{
30+
if (ctx == NULL) {
31+
return -EINVAL;
32+
}
33+
34+
if (boot_erase_img_bank(partition_id)) {
35+
return -EIO;
36+
}
37+
38+
return flash_img_init(&ctx->flash_ctx);
39+
}
40+
41+
int updatehub_storage_write(struct updatehub_storage_context *ctx,
42+
const uint8_t *data, const size_t size,
43+
const bool flush)
44+
{
45+
if (ctx == NULL || (size > 0 && data == NULL)) {
46+
return -EINVAL;
47+
}
48+
49+
LOG_DBG("Flash: Address: 0x%08x, Size: %d, Flush: %s",
50+
ctx->flash_ctx.stream.bytes_written, size,
51+
flush ? "True" : "False");
52+
53+
return flash_img_buffered_write(&ctx->flash_ctx, data, size, flush);
54+
}
55+
56+
int updatehub_storage_check(struct updatehub_storage_context *ctx,
57+
const uint32_t partition_id,
58+
const uint8_t *hash, const size_t size)
59+
{
60+
if (ctx == NULL || hash == NULL || size == 0) {
61+
return -EINVAL;
62+
}
63+
64+
const struct flash_img_check fic = { .match = hash, .clen = size };
65+
66+
return flash_img_check(&ctx->flash_ctx, &fic, partition_id);
67+
}
68+
69+
int updatehub_storage_mark_partition_to_upgrade(struct updatehub_storage_context *ctx,
70+
const uint32_t partition_id)
71+
{
72+
if (ctx == NULL) {
73+
return -EINVAL;
74+
}
75+
76+
return boot_request_upgrade_multi(partition_id, BOOT_UPGRADE_TEST);
77+
}
78+
79+
int updatehub_storage_mark_partition_as_confirmed(const uint32_t partition_id)
80+
{
81+
return boot_write_img_confirmed();
82+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2023 O.S.Systems
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __UPDATEHUB_STORAGE_H__
8+
#define __UPDATEHUB_STORAGE_H__
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
#include <zephyr/dfu/flash_img.h>
15+
#include <zephyr/storage/flash_map.h>
16+
#define UPDATEHUB_SLOT_PARTITION_0 FIXED_PARTITION_ID(slot0_partition)
17+
#define UPDATEHUB_SLOT_PARTITION_1 FIXED_PARTITION_ID(slot1_partition)
18+
19+
struct updatehub_storage_context {
20+
struct flash_img_context flash_ctx;
21+
};
22+
23+
int updatehub_storage_is_partition_good(struct updatehub_storage_context *ctx);
24+
int updatehub_storage_init(struct updatehub_storage_context *ctx,
25+
const uint32_t partition_id);
26+
int updatehub_storage_write(struct updatehub_storage_context *ctx,
27+
const uint8_t *data, const size_t size,
28+
const bool flush);
29+
int updatehub_storage_check(struct updatehub_storage_context *ctx,
30+
const uint32_t partition_id,
31+
const uint8_t *hash, const size_t size);
32+
int updatehub_storage_mark_partition_to_upgrade(struct updatehub_storage_context *ctx,
33+
const uint32_t partition_id);
34+
int updatehub_storage_mark_partition_as_confirmed(const uint32_t partition_id);
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
40+
#endif /* __UPDATEHUB_STORAGE_H__ */

0 commit comments

Comments
 (0)