Skip to content

Commit

Permalink
boot/bootutil: Split RAM load code to its own file
Browse files Browse the repository at this point in the history
RAM loading code is currently under bootutil/loader.c, and it's not
accessible for different loaders, such as the single loaders. Future
patches will make use of the RAM loading code outside the
bootutil/loader.c context, and this patch prepares for that by making it
standalone on boot/bootutil/src/ram_load.c

Signed-off-by: Ederson de Souza <[email protected]>
Signed-off-by: Tom Burdick <[email protected]>
  • Loading branch information
edersondisouza authored and nvlsianpu committed Oct 25, 2024
1 parent 33de65c commit 9f1e573
Show file tree
Hide file tree
Showing 11 changed files with 546 additions and 471 deletions.
6 changes: 6 additions & 0 deletions boot/bootutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ target_sources(bootutil
src/swap_scratch.c
src/tlv.c
)
if(CONFIG_BOOT_RAM_LOAD)
target_sources(bootutil
PRIVATE
src/ram_load.c
)
endif()
3 changes: 3 additions & 0 deletions boot/bootutil/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pkg.ign_files.BOOTUTIL_SINGLE_APPLICATION_SLOT:
- "loader.c"
- "swap_scratch.c"

pkg.ign_files:
- "ram_load.c"

pkg.deps.BOOTUTIL_USE_MBED_TLS:
- "@apache-mynewt-core/crypto/mbedtls"

Expand Down
65 changes: 65 additions & 0 deletions boot/bootutil/src/bootutil_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,68 @@ uint32_t bootutil_max_image_size(const struct flash_area *fap)
return boot_swap_info_off(fap);
#endif
}

/*
* Compute the total size of the given image. Includes the size of
* the TLVs.
*/
#if !defined(MCUBOOT_DIRECT_XIP) && \
(!defined(MCUBOOT_OVERWRITE_ONLY) || \
defined(MCUBOOT_OVERWRITE_ONLY_FAST))
int
boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
{
const struct flash_area *fap;
struct image_tlv_info info;
uint32_t off;
uint32_t protect_tlv_size;
int area_id;
int rc;

#if (BOOT_IMAGE_NUMBER == 1)
(void)state;
#endif

area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
rc = flash_area_open(area_id, &fap);
if (rc != 0) {
rc = BOOT_EFLASH;
goto done;
}

off = BOOT_TLV_OFF(boot_img_hdr(state, slot));

if (flash_area_read(fap, off, &info, sizeof(info))) {
rc = BOOT_EFLASH;
goto done;
}

protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
if (protect_tlv_size != info.it_tlv_tot) {
rc = BOOT_EBADIMAGE;
goto done;
}

if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
rc = BOOT_EFLASH;
goto done;
}
} else if (protect_tlv_size != 0) {
rc = BOOT_EBADIMAGE;
goto done;
}

if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
rc = BOOT_EBADIMAGE;
goto done;
}

*size = off + protect_tlv_size + info.it_tlv_tot;
rc = 0;

done:
flash_area_close(fap);
return rc;
}
#endif /* !MCUBOOT_OVERWRITE_ONLY */
11 changes: 11 additions & 0 deletions boot/bootutil/src/bootutil_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct flash_area;

#define BOOT_TMPBUF_SZ 256

#define NO_ACTIVE_SLOT UINT32_MAX

/** Number of image slots in flash; currently limited to two. */
#define BOOT_NUM_SLOTS 2

Expand Down Expand Up @@ -467,15 +469,24 @@ struct bootsim_ram_info *bootsim_get_ram_info(void);
#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
(memcpy((output),(void*)(IMAGE_RAM_BASE + (hdr)->ih_load_addr + (start)), \
(size)), 0)

int boot_load_image_to_sram(struct boot_loader_state *state);
int boot_remove_image_from_sram(struct boot_loader_state *state);
int boot_remove_image_from_flash(struct boot_loader_state *state,
uint32_t slot);
#else
#define IMAGE_RAM_BASE ((uintptr_t)0)

#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
(flash_area_read((fap), (start), (output), (size)))

#endif /* MCUBOOT_RAM_LOAD */

uint32_t bootutil_max_image_size(const struct flash_area *fap);

int boot_read_image_size(struct boot_loader_state *state, int slot,
uint32_t *size);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 9f1e573

Please sign in to comment.