tools: kwboot: Calculate real used space in kwbimage header when calling kwboot_img_grow_hdr()

Size of the header stored in kwbimage may be larger than real used size in
the kwbimage header. If there is unused space in kwbimage header then use
it for growing it. So update code to calculate used space of kwbimage
header.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>
Reviewed-by: Stefan Roese <sr@denx.de>
This commit is contained in:
Pali Rohár 2021-10-25 15:13:02 +02:00 committed by Stefan Roese
parent e511cc3b1a
commit d656f5a0ee

View file

@ -1318,11 +1318,20 @@ kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
{
uint32_t hdrsz, datasz, srcaddr;
struct main_hdr_v1 *hdr = img;
struct opt_hdr_v1 *ohdr;
uint8_t *data;
srcaddr = le32_to_cpu(hdr->srcaddr);
/* calculate real used space in kwbimage header */
if (kwbimage_version(img) == 0) {
hdrsz = kwbheader_size(img);
} else {
hdrsz = sizeof(*hdr);
for_each_opt_hdr_v1 (ohdr, hdr)
hdrsz += opt_hdr_v1_size(ohdr);
}
data = (uint8_t *)img + srcaddr;
datasz = *size - srcaddr;
@ -1339,10 +1348,12 @@ kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
if (kwbimage_version(img) == 1) {
hdrsz += grow;
if (hdrsz > kwbheader_size(img)) {
hdr->headersz_msb = hdrsz >> 16;
hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
}
}
}
static void *
kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)