Skip to content

Commit

Permalink
subsystem: fota_download: correcting URI check
Browse files Browse the repository at this point in the history
- HASH checksums are used to validate changed URI

Signed-off-by: Veli-Tapani Peltoketo <veli-tapani.peltoketo@nordicsemi.no>
  • Loading branch information
VTPeltoketo committed Aug 4, 2023
1 parent 5f1a49a commit d3bcf52
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,12 @@ Libraries for networking
* The :kconfig:option:`CONFIG_DOWNLOAD_CLIENT_MAX_HOSTNAME_SIZE` Kconfig option's default value to ``255``.
* The :kconfig:option:`CONFIG_DOWNLOAD_CLIENT_MAX_FILENAME_SIZE` Kconfig option's default value to ``255``.

* :ref:`lib_fota_download` library:

* Updated:

* The library now verifies whether the download started with the same URI and resumes the interrupted download.

Libraries for NFC
-----------------

Expand Down
1 change: 1 addition & 0 deletions subsys/net/lib/fota_download/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ menuconfig FOTA_DOWNLOAD
bool "FOTA Download"
depends on DOWNLOAD_CLIENT
depends on DFU_TARGET
select SYS_HASH_FUNC32
imply FW_INFO

if (FOTA_DOWNLOAD)
Expand Down
14 changes: 13 additions & 1 deletion subsys/net/lib/fota_download/src/fota_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/hash_function.h>
#include <net/fota_download.h>
#include <net/download_client.h>
#include <pm_config.h>
Expand All @@ -29,6 +30,8 @@ LOG_MODULE_REGISTER(fota_download, CONFIG_FOTA_DOWNLOAD_LOG_LEVEL);
static fota_download_callback_t callback;
static const char *dl_host;
static const char *dl_file;
static uint32_t dl_host_hash;
static uint32_t dl_file_hash;
static struct download_client dlc;
static struct k_work_delayable dlc_with_offset_work;
static int socket_retries_left;
Expand Down Expand Up @@ -415,6 +418,8 @@ int fota_download_start_with_image_type(const char *host, const char *file,
const enum dfu_target_image_type expected_type)
{
static int sec_tag_list[1];
uint32_t host_hash = 0;
uint32_t file_hash = 0;
int err = -1;

struct download_client_cfg config = {
Expand All @@ -434,12 +439,19 @@ int fota_download_start_with_image_type(const char *host, const char *file,
atomic_clear_bit(&flags, FLAG_RESUME);
set_error_state(FOTA_DOWNLOAD_ERROR_CAUSE_NO_ERROR);

host_hash = sys_hash32(host, strlen(host));
file_hash = sys_hash32(file, strlen(file));
LOG_DBG("URI checksums %d,%d,%d,%d\r\n", host_hash, file_hash,
dl_host_hash, dl_file_hash);

/* Verify if the URI is same as last time, if not, prevent resuming. */
if ((!dl_host || strcmp(dl_host, host) != 0) || (!dl_file || strcmp(dl_file, file) != 0)) {
if (dl_host_hash != host_hash || dl_file_hash != file_hash) {
atomic_set_bit(&flags, FLAG_NEW_URI);
} else {
atomic_clear_bit(&flags, FLAG_NEW_URI);
}
dl_host_hash = host_hash;
dl_file_hash = file_hash;
dl_host = host;
dl_file = file;

Expand Down
1 change: 1 addition & 0 deletions tests/subsys/net/lib/fota_download/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ CONFIG_ZTEST_NEW_API=y
CONFIG_FLASH=y
CONFIG_FW_INFO=y
CONFIG_TFM_BUILD_NS=y # In order to mock tfm_platform_s0_active
CONFIG_SYS_HASH_FUNC32=y

0 comments on commit d3bcf52

Please sign in to comment.