7
7
#include <zephyr/logging/log.h>
8
8
LOG_MODULE_REGISTER (updatehub , CONFIG_UPDATEHUB_LOG_LEVEL );
9
9
10
- #include <zephyr/kernel.h>
11
-
12
10
#include <zephyr/logging/log_ctrl.h>
13
11
#include <zephyr/net/socket.h>
14
12
#include <zephyr/net/net_mgmt.h>
15
13
#include <zephyr/net/net_ip.h>
16
14
#include <zephyr/net/udp.h>
17
15
#include <zephyr/net/coap.h>
18
16
#include <zephyr/net/dns_resolve.h>
19
- #include <zephyr/drivers/flash.h>
20
17
#include <zephyr/sys/reboot.h>
21
18
#include <zephyr/data/json.h>
22
- #include <zephyr/dfu/mcuboot.h>
23
- #include <zephyr/storage/flash_map.h>
24
19
25
20
#include "include/updatehub.h"
26
21
#include "updatehub_priv.h"
27
22
#include "updatehub_firmware.h"
28
23
#include "updatehub_device.h"
29
24
#include "updatehub_timer.h"
30
25
#include "updatehub_integrity.h"
26
+ #include "updatehub_storage.h"
31
27
32
28
#if defined(CONFIG_UPDATEHUB_DTLS )
33
29
#define CA_CERTIFICATE_TAG 1
@@ -64,7 +60,7 @@ LOG_MODULE_REGISTER(updatehub, CONFIG_UPDATEHUB_LOG_LEVEL);
64
60
static struct updatehub_context {
65
61
struct coap_block_context block ;
66
62
struct k_sem semaphore ;
67
- struct flash_img_context flash_ctx ;
63
+ struct updatehub_storage_context storage_ctx ;
68
64
struct updatehub_crypto_context crypto_ctx ;
69
65
enum updatehub_response code_status ;
70
66
uint8_t hash [SHA256_BIN_DIGEST_SIZE ];
@@ -416,9 +412,6 @@ static int install_update_cb_check_blk_num(const struct coap_packet *resp)
416
412
static void install_update_cb (void )
417
413
{
418
414
struct coap_packet response_packet ;
419
- #ifdef _STORAGE_SHA256_VERIFICATION
420
- struct flash_img_check fic ;
421
- #endif
422
415
uint8_t * data = k_malloc (MAX_DOWNLOAD_DATA );
423
416
const uint8_t * payload_start ;
424
417
uint16_t payload_len ;
@@ -469,14 +462,8 @@ static void install_update_cb(void)
469
462
}
470
463
#endif
471
464
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 )) {
480
467
LOG_ERR ("Error to write on the flash" );
481
468
ctx .code_status = UPDATEHUB_INSTALL_ERROR ;
482
469
goto cleanup ;
@@ -513,11 +500,9 @@ static void install_update_cb(void)
513
500
#endif
514
501
515
502
#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 )) {
521
506
LOG_ERR ("Firmware - flash validation has failed" );
522
507
ctx .code_status = UPDATEHUB_INSTALL_ERROR ;
523
508
goto cleanup ;
@@ -533,12 +518,6 @@ static void install_update_cb(void)
533
518
534
519
static enum updatehub_response install_update (void )
535
520
{
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
-
542
521
#ifdef _DOWNLOAD_SHA256_VERIFICATION
543
522
if (updatehub_integrity_init (& ctx .crypto_ctx )) {
544
523
LOG_ERR ("Could not start sha256sum" );
@@ -560,7 +539,8 @@ static enum updatehub_response install_update(void)
560
539
goto cleanup ;
561
540
}
562
541
563
- if (flash_img_init (& ctx .flash_ctx )) {
542
+ if (updatehub_storage_init (& ctx .storage_ctx ,
543
+ UPDATEHUB_SLOT_PARTITION_1 )) {
564
544
LOG_ERR ("Unable init flash" );
565
545
ctx .code_status = UPDATEHUB_FLASH_INIT_ERROR ;
566
546
goto cleanup ;
@@ -632,7 +612,7 @@ static int report(enum updatehub_state state)
632
612
goto error ;
633
613
}
634
614
635
- if (!updatehub_get_firmware_version (FIXED_PARTITION_ID ( slot0_partition ) ,
615
+ if (!updatehub_get_firmware_version (UPDATEHUB_SLOT_PARTITION_0 ,
636
616
firmware_version ,
637
617
FIRMWARE_IMG_VER_STRLEN_MAX )) {
638
618
goto error ;
@@ -765,7 +745,7 @@ static void probe_cb(char *metadata, size_t metadata_size)
765
745
766
746
int updatehub_confirm (void )
767
747
{
768
- return boot_write_img_confirmed ( );
748
+ return updatehub_storage_mark_partition_as_confirmed ( UPDATEHUB_SLOT_PARTITION_0 );
769
749
}
770
750
771
751
int updatehub_reboot (void )
@@ -795,13 +775,13 @@ enum updatehub_response updatehub_probe(void)
795
775
goto error ;
796
776
}
797
777
798
- if (!boot_is_img_confirmed ( )) {
778
+ if (!updatehub_storage_is_partition_good ( & ctx . storage_ctx )) {
799
779
LOG_ERR ("The current image is not confirmed" );
800
780
ctx .code_status = UPDATEHUB_UNCONFIRMED_IMAGE ;
801
781
goto error ;
802
782
}
803
783
804
- if (!updatehub_get_firmware_version (FIXED_PARTITION_ID ( slot0_partition ) ,
784
+ if (!updatehub_get_firmware_version (UPDATEHUB_SLOT_PARTITION_0 ,
805
785
firmware_version ,
806
786
FIRMWARE_IMG_VER_STRLEN_MAX )) {
807
787
ctx .code_status = UPDATEHUB_METADATA_ERROR ;
@@ -960,7 +940,8 @@ enum updatehub_response updatehub_update(void)
960
940
goto error ;
961
941
}
962
942
963
- if (boot_request_upgrade (BOOT_UPGRADE_TEST )) {
943
+ if (updatehub_storage_mark_partition_to_upgrade (& ctx .storage_ctx ,
944
+ UPDATEHUB_SLOT_PARTITION_1 )) {
964
945
LOG_ERR ("Could not reporting downloaded state" );
965
946
ctx .code_status = UPDATEHUB_INSTALL_ERROR ;
966
947
goto error ;
0 commit comments