Skip to content

Commit

Permalink
mgmt: hawkbit: use flash_area from flash_ctx
Browse files Browse the repository at this point in the history
don't assume, that the slot to upload is the second.

Signed-off-by: Fin Maaß <[email protected]>
  • Loading branch information
maass-hamburg authored and kartben committed Dec 23, 2024
1 parent 97cdd97 commit 6dce233
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions subsys/mgmt/hawkbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ zephyr_library_sources_ifdef(CONFIG_HAWKBIT_AUTOHANDLER hawkbit_autohandler.c)
zephyr_library_sources_ifdef(CONFIG_HAWKBIT hawkbit_device.c)
zephyr_library_sources_ifdef(CONFIG_HAWKBIT hawkbit_firmware.c)
zephyr_library_sources_ifdef(CONFIG_HAWKBIT_SHELL shell.c)

zephyr_library_link_libraries(MCUBOOT_BOOTUTIL)
12 changes: 10 additions & 2 deletions subsys/mgmt/hawkbit/hawkbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <zephyr/storage/flash_map.h>
#include <zephyr/sys/reboot.h>

#include <bootutil/bootutil_public.h>

#include "hawkbit_device.h"
#include "hawkbit_firmware.h"
#include "hawkbit_priv.h"
Expand Down Expand Up @@ -1439,6 +1441,7 @@ static void s_download(void *o)
int ret = 0;
struct s_object *s = (struct s_object *)o;
struct flash_img_check fic = {0};
const struct flash_area *flash_area_ptr;
char *url_buffer;

ret = hawkbit_parse_deployment(&s->hb_context, &s->hb_context.results.dep, &url_buffer);
Expand All @@ -1451,6 +1454,11 @@ static void s_download(void *o)

flash_img_init(&s->hb_context.flash_ctx);

/* The flash_area pointer has to be copied before the download starts
* because the flash_area will be set to NULL after the download has finished.
*/
flash_area_ptr = s->hb_context.flash_ctx.flash_area;

if (!send_request(&s->hb_context, HAWKBIT_DOWNLOAD, url_buffer, NULL)) {
LOG_ERR("Send request failed (%s)", "HAWKBIT_DOWNLOAD");
smf_set_state(SMF_CTX(s), &hawkbit_states[S_HAWKBIT_TERMINATE]);
Expand All @@ -1468,15 +1476,15 @@ static void s_download(void *o)
/* Verify the hash of the stored firmware */
fic.match = s->hb_context.dl.file_hash;
fic.clen = s->hb_context.dl.downloaded_size;
if (flash_img_check(&s->hb_context.flash_ctx, &fic, FIXED_PARTITION_ID(SLOT1_LABEL))) {
if (flash_img_check(&s->hb_context.flash_ctx, &fic, flash_area_ptr->fa_id)) {
LOG_ERR("Failed to validate stored firmware");
s->hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR;
smf_set_state(SMF_CTX(s), &hawkbit_states[S_HAWKBIT_TERMINATE]);
return;
}

/* Request mcuboot to upgrade */
if (boot_request_upgrade(BOOT_UPGRADE_TEST)) {
if (boot_set_next(flash_area_ptr, false, false)) {
LOG_ERR("Failed to mark the image in slot 1 as pending");
s->hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR;
smf_set_state(SMF_CTX(s), &hawkbit_states[S_HAWKBIT_TERMINATE]);
Expand Down

0 comments on commit 6dce233

Please sign in to comment.