From 9bbda46e1d61470fabc196cfdcd8009e8d54e900 Mon Sep 17 00:00:00 2001 From: Adrian Warecki Date: Tue, 14 Nov 2023 15:02:37 +0100 Subject: [PATCH] rimage: Introduce ALIGN_UP macro Created ALIGN_UP macro to simplify calculations. Signed-off-by: Adrian Warecki --- tools/rimage/src/include/rimage/misc_utils.h | 1 + tools/rimage/src/manifest.c | 10 ++-------- tools/rimage/src/module.c | 6 +++--- tools/rimage/src/plat_auth.c | 9 ++++----- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/tools/rimage/src/include/rimage/misc_utils.h b/tools/rimage/src/include/rimage/misc_utils.h index e3432d57a391..c5bfc91c863a 100644 --- a/tools/rimage/src/include/rimage/misc_utils.h +++ b/tools/rimage/src/include/rimage/misc_utils.h @@ -9,6 +9,7 @@ #include #define DIV_ROUND_UP(val, div) (((val) + (div) - 1) / (div)) +#define ALIGN_UP(val, align) (((val) + (align) - 1) & ~((align) - 1)) /** * Reverses the order of bytes in the array diff --git a/tools/rimage/src/manifest.c b/tools/rimage/src/manifest.c index eb9fc322535f..3433de88b3e2 100644 --- a/tools/rimage/src/manifest.c +++ b/tools/rimage/src/manifest.c @@ -431,10 +431,7 @@ static int man_module_create(struct image *image, struct manifest_module *module } /* round module end upto nearest page */ - if (image->image_end % MAN_PAGE_SIZE) { - image->image_end = (image->image_end / MAN_PAGE_SIZE) + 1; - image->image_end *= MAN_PAGE_SIZE; - } + image->image_end = ALIGN_UP(image->image_end, MAN_PAGE_SIZE); out: fprintf(stdout, " Total pages text %d data %d bss %d module file limit: 0x%x\n\n", @@ -498,10 +495,7 @@ static int man_module_create_reloc(struct image *image, struct manifest_module * image->image_end = module->foffset + module->file.elf.file_size; /* round module end up to nearest page */ - if (image->image_end % MAN_PAGE_SIZE) { - image->image_end = (image->image_end / MAN_PAGE_SIZE) + 1; - image->image_end *= MAN_PAGE_SIZE; - } + image->image_end = ALIGN_UP(image->image_end, MAN_PAGE_SIZE); fprintf(stdout, " Total pages text %d data %d bss %d module file limit: 0x%x\n\n", man_module->segment[SOF_MAN_SEGMENT_TEXT].flags.r.length, diff --git a/tools/rimage/src/module.c b/tools/rimage/src/module.c index b150e246ca8c..f81d2d3a5bef 100644 --- a/tools/rimage/src/module.c +++ b/tools/rimage/src/module.c @@ -14,7 +14,7 @@ #include #include #include - +#include int module_read_section(const struct module *module, const struct module_section *section, void *buffer, const size_t size) @@ -252,10 +252,10 @@ static void sections_info_add(struct module_sections_info *info, struct module_s */ static void sections_info_finalize(struct module_sections_info *info) { - info->file_size = info->end - info->start; + size_t size = (info->end > info->start) ? info->end - info->start : 0; /* file sizes round up to nearest page */ - info->file_size = (info->file_size + MAN_PAGE_SIZE - 1) & ~(MAN_PAGE_SIZE - 1); + info->file_size = ALIGN_UP(size, MAN_PAGE_SIZE); } /** diff --git a/tools/rimage/src/plat_auth.c b/tools/rimage/src/plat_auth.c index 321d1712d0e4..5d8d888bdf05 100644 --- a/tools/rimage/src/plat_auth.c +++ b/tools/rimage/src/plat_auth.c @@ -8,6 +8,7 @@ #include #include #include +#include void ri_adsp_meta_data_create_v1_8(struct image *image, int meta_start_offset, int meta_end_offset) @@ -52,7 +53,7 @@ void ri_plat_ext_data_create(struct image *image) fprintf(stdout, " auth: completing authentication manifest\n"); part->length = meta->comp_desc[0].limit_offset - MAN_DESC_OFFSET_V1_8; - part->length += MAN_PAGE_SIZE - (part->length % MAN_PAGE_SIZE); + part->length = ALIGN_UP(part->length, MAN_PAGE_SIZE); /* do this here atm */ desc->header.preload_page_count = part->length / MAN_PAGE_SIZE; @@ -69,10 +70,9 @@ void ri_plat_ext_data_create_v2_5(struct image *image) fprintf(stdout, " auth: completing authentication manifest\n"); size = meta->comp_desc[0].limit_offset - MAN_DESC_OFFSET_V1_8; - size += MAN_PAGE_SIZE - (size % MAN_PAGE_SIZE); /* do this here atm */ - desc->header.preload_page_count = size / MAN_PAGE_SIZE; + desc->header.preload_page_count = DIV_ROUND_UP(size, MAN_PAGE_SIZE); ext->size = image->image_end; } @@ -87,8 +87,7 @@ void ri_plat_ext_data_create_ace_v1_5(struct image *image) fprintf(stdout, " auth: completing authentication manifest\n"); size = meta->comp_desc[0].limit_offset - MAN_DESC_OFFSET_V1_8; - size += MAN_PAGE_SIZE - (size % MAN_PAGE_SIZE); - desc->header.preload_page_count = size / MAN_PAGE_SIZE; + desc->header.preload_page_count = DIV_ROUND_UP(size, MAN_PAGE_SIZE); ext->size = image->image_end; }