Skip to content

Commit 9bbda46

Browse files
committed
rimage: Introduce ALIGN_UP macro
Created ALIGN_UP macro to simplify calculations. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent 19006e6 commit 9bbda46

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed

tools/rimage/src/include/rimage/misc_utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdint.h>
1010

1111
#define DIV_ROUND_UP(val, div) (((val) + (div) - 1) / (div))
12+
#define ALIGN_UP(val, align) (((val) + (align) - 1) & ~((align) - 1))
1213

1314
/**
1415
* Reverses the order of bytes in the array

tools/rimage/src/manifest.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,7 @@ static int man_module_create(struct image *image, struct manifest_module *module
431431
}
432432

433433
/* round module end upto nearest page */
434-
if (image->image_end % MAN_PAGE_SIZE) {
435-
image->image_end = (image->image_end / MAN_PAGE_SIZE) + 1;
436-
image->image_end *= MAN_PAGE_SIZE;
437-
}
434+
image->image_end = ALIGN_UP(image->image_end, MAN_PAGE_SIZE);
438435

439436
out:
440437
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 *
498495
image->image_end = module->foffset + module->file.elf.file_size;
499496

500497
/* round module end up to nearest page */
501-
if (image->image_end % MAN_PAGE_SIZE) {
502-
image->image_end = (image->image_end / MAN_PAGE_SIZE) + 1;
503-
image->image_end *= MAN_PAGE_SIZE;
504-
}
498+
image->image_end = ALIGN_UP(image->image_end, MAN_PAGE_SIZE);
505499

506500
fprintf(stdout, " Total pages text %d data %d bss %d module file limit: 0x%x\n\n",
507501
man_module->segment[SOF_MAN_SEGMENT_TEXT].flags.r.length,

tools/rimage/src/module.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <rimage/elf_file.h>
1515
#include <rimage/file_utils.h>
1616
#include <rimage/rimage.h>
17-
17+
#include <rimage/misc_utils.h>
1818

1919
int module_read_section(const struct module *module, const struct module_section *section,
2020
void *buffer, const size_t size)
@@ -252,10 +252,10 @@ static void sections_info_add(struct module_sections_info *info, struct module_s
252252
*/
253253
static void sections_info_finalize(struct module_sections_info *info)
254254
{
255-
info->file_size = info->end - info->start;
255+
size_t size = (info->end > info->start) ? info->end - info->start : 0;
256256

257257
/* file sizes round up to nearest page */
258-
info->file_size = (info->file_size + MAN_PAGE_SIZE - 1) & ~(MAN_PAGE_SIZE - 1);
258+
info->file_size = ALIGN_UP(size, MAN_PAGE_SIZE);
259259
}
260260

261261
/**

tools/rimage/src/plat_auth.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <rimage/rimage.h>
99
#include <rimage/manifest.h>
1010
#include <rimage/plat_auth.h>
11+
#include <rimage/misc_utils.h>
1112

1213
void ri_adsp_meta_data_create_v1_8(struct image *image, int meta_start_offset,
1314
int meta_end_offset)
@@ -52,7 +53,7 @@ void ri_plat_ext_data_create(struct image *image)
5253
fprintf(stdout, " auth: completing authentication manifest\n");
5354

5455
part->length = meta->comp_desc[0].limit_offset - MAN_DESC_OFFSET_V1_8;
55-
part->length += MAN_PAGE_SIZE - (part->length % MAN_PAGE_SIZE);
56+
part->length = ALIGN_UP(part->length, MAN_PAGE_SIZE);
5657

5758
/* do this here atm */
5859
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)
6970
fprintf(stdout, " auth: completing authentication manifest\n");
7071

7172
size = meta->comp_desc[0].limit_offset - MAN_DESC_OFFSET_V1_8;
72-
size += MAN_PAGE_SIZE - (size % MAN_PAGE_SIZE);
7373

7474
/* do this here atm */
75-
desc->header.preload_page_count = size / MAN_PAGE_SIZE;
75+
desc->header.preload_page_count = DIV_ROUND_UP(size, MAN_PAGE_SIZE);
7676
ext->size = image->image_end;
7777
}
7878

@@ -87,8 +87,7 @@ void ri_plat_ext_data_create_ace_v1_5(struct image *image)
8787
fprintf(stdout, " auth: completing authentication manifest\n");
8888

8989
size = meta->comp_desc[0].limit_offset - MAN_DESC_OFFSET_V1_8;
90-
size += MAN_PAGE_SIZE - (size % MAN_PAGE_SIZE);
9190

92-
desc->header.preload_page_count = size / MAN_PAGE_SIZE;
91+
desc->header.preload_page_count = DIV_ROUND_UP(size, MAN_PAGE_SIZE);
9392
ext->size = image->image_end;
9493
}

0 commit comments

Comments
 (0)