diff options
author | Pali Rohár <pali@kernel.org> | 2023-03-29 21:25:55 +0200 |
---|---|---|
committer | Stefan Roese <sr@denx.de> | 2023-04-13 11:34:47 +0200 |
commit | 62d81d68d2446739374d6d2fa13ab32c8a4923ec (patch) | |
tree | 600a12a67ec4557b31c0f5756660b5de4510f254 /tools | |
parent | 2972d7d62f8f177bf2186c7a38bdae53dff99a7a (diff) | |
download | u-boot-62d81d68d2446739374d6d2fa13ab32c8a4923ec.zip u-boot-62d81d68d2446739374d6d2fa13ab32c8a4923ec.tar.gz u-boot-62d81d68d2446739374d6d2fa13ab32c8a4923ec.tar.bz2 |
tools: kwbimage: Simplify align code
Replace repeated code patterns by generic code.
Signed-off-by: Pali Rohár <pali@kernel.org>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/kwbimage.c | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/tools/kwbimage.c b/tools/kwbimage.c index 8e573d9..360fedd 100644 --- a/tools/kwbimage.c +++ b/tools/kwbimage.c @@ -2118,8 +2118,6 @@ static int kwbimage_verify_header(unsigned char *ptr, int image_size, return 0; } -static int kwbimage_align_size(int bootfrom, int alloc_len, struct stat s); - static int kwbimage_generate(struct image_tool_params *params, struct image_type_params *tparams) { @@ -2130,6 +2128,7 @@ static int kwbimage_generate(struct image_tool_params *params, int version; void *hdr; int ret; + int align, size; fcfg = fopen(params->imagename, "r"); if (!fcfg) { @@ -2212,39 +2211,40 @@ static int kwbimage_generate(struct image_tool_params *params, tparams->hdr = hdr; /* + * Final SATA and SDIO images must be aligned to 512 bytes. + * Final SPI and NAND images must be aligned to 256 bytes. + * Final UART image must be aligned to 128 bytes. + */ + if (bootfrom == IBR_HDR_SATA_ID || bootfrom == IBR_HDR_SDIO_ID) + align = 512; + else if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID) + align = 256; + else if (bootfrom == IBR_HDR_UART_ID) + align = 128; + else + align = 4; + + /* + * The resulting image needs to be 4-byte aligned. At least + * the Marvell hdrparser tool complains if its unaligned. + * After the image data is stored 4-byte checksum. + */ + size = 4 + (align - (alloc_len + s.st_size + 4) % align) % align; + + /* * This function should return aligned size of the datafile. * When skipcpy is set (datafile is skipped) then return value of this * function is ignored, so we have to put required kwbimage aligning * into the preallocated header size. */ if (params->skipcpy) { - tparams->header_size += kwbimage_align_size(bootfrom, alloc_len, s); + tparams->header_size += size; return 0; } else { - return kwbimage_align_size(bootfrom, alloc_len, s); + return size; } } -static int kwbimage_align_size(int bootfrom, int alloc_len, struct stat s) -{ - /* - * The resulting image needs to be 4-byte aligned. At least - * the Marvell hdrparser tool complains if its unaligned. - * After the image data is stored 4-byte checksum. - * Final UART image must be aligned to 128 bytes. - * Final SPI and NAND images must be aligned to 256 bytes. - * Final SATA and SDIO images must be aligned to 512 bytes. - */ - if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID) - return 4 + (256 - (alloc_len + s.st_size + 4) % 256) % 256; - else if (bootfrom == IBR_HDR_SATA_ID || bootfrom == IBR_HDR_SDIO_ID) - return 4 + (512 - (alloc_len + s.st_size + 4) % 512) % 512; - else if (bootfrom == IBR_HDR_UART_ID) - return 4 + (128 - (alloc_len + s.st_size + 4) % 128) % 128; - else - return 4 + (4 - s.st_size % 4) % 4; -} - static int kwbimage_generate_config(void *ptr, struct image_tool_params *params) { struct main_hdr_v0 *mhdr0 = (struct main_hdr_v0 *)ptr; |