Skip to content

Commit

Permalink
fiptool: Add --align-reserve command line option
Browse files Browse the repository at this point in the history
Allow each image to reserve some space before alignment.

Use case is --align 0x4000 --align-reserve 64 to make sure sufficient
room is available to prepend (or append) vendor-specific headers.

Helps resolve afaerber/meson-tools#3.

Signed-off-by: Andreas Färber <[email protected]>
  • Loading branch information
afaerber committed Apr 21, 2017
1 parent c67488a commit df0995e
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions tools/fiptool/fiptool.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#define OPT_TOC_ENTRY 0
#define OPT_PLAT_TOC_FLAGS 1
#define OPT_ALIGN 2
#define OPT_ALIGN_RESERVE 3

static int info_cmd(int argc, char *argv[]);
static void info_usage(void);
Expand Down Expand Up @@ -511,7 +512,8 @@ static void info_usage(void)
exit(1);
}

static int pack_images(const char *filename, uint64_t toc_flags, unsigned long align)
static int pack_images(const char *filename, uint64_t toc_flags,
unsigned long align, unsigned long align_reserve)
{
FILE *fp;
image_desc_t *desc;
Expand Down Expand Up @@ -546,7 +548,7 @@ static int pack_images(const char *filename, uint64_t toc_flags, unsigned long a
if (image == NULL)
continue;
payload_size += image->toc_e.size;
entry_offset = (entry_offset + align - 1) & ~(align - 1);
entry_offset = (entry_offset + align_reserve + align - 1) & ~(align - 1);
image->toc_e.offset_address = entry_offset;
*toc_entry++ = image->toc_e;
entry_offset += image->toc_e.size;
Expand Down Expand Up @@ -656,6 +658,22 @@ static unsigned long get_image_align(char *arg)
return align;
}

static unsigned long get_image_align_reservation(char *arg)
{
char *endptr;
unsigned long align_reserve;

errno = 0;
if (strlen(arg) > 2 && strncmp(arg, "0x", 2) == 0)
align_reserve = strtoul(arg + 2, &endptr, 16);
else
align_reserve = strtoul(arg, &endptr, 10);
if (*endptr != '\0' || errno != 0)
log_errx("Invalid alignment: %s", arg);

return align_reserve;
}

static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
{
char *p;
Expand All @@ -676,7 +694,7 @@ static int create_cmd(int argc, char *argv[])
struct option *opts = NULL;
size_t nr_opts = 0;
unsigned long long toc_flags = 0;
unsigned long align = 1;
unsigned long align = 1, align_reserve = 0;

if (argc < 2)
create_usage();
Expand All @@ -685,6 +703,8 @@ static int create_cmd(int argc, char *argv[])
opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
OPT_PLAT_TOC_FLAGS);
opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
opts = add_opt(opts, &nr_opts, "align-reserve", required_argument,
OPT_ALIGN_RESERVE);
opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
opts = add_opt(opts, &nr_opts, NULL, 0, 0);

Expand All @@ -709,6 +729,9 @@ static int create_cmd(int argc, char *argv[])
case OPT_ALIGN:
align = get_image_align(optarg);
break;
case OPT_ALIGN_RESERVE:
align_reserve = get_image_align_reservation(optarg);
break;
case 'b': {
char name[_UUID_STR_LEN + 1];
char filename[PATH_MAX] = { 0 };
Expand Down Expand Up @@ -744,7 +767,7 @@ static int create_cmd(int argc, char *argv[])

update_fip();

pack_images(argv[0], toc_flags, align);
pack_images(argv[0], toc_flags, align, align_reserve);
return 0;
}

Expand All @@ -756,6 +779,7 @@ static void create_usage(void)
printf("\n");
printf("Options:\n");
printf(" --align <value>\t\tEach image is aligned to <value> (default: 1).\n");
printf(" --align-reserve <value>\t\tEach image is padded by <value> (default: 0).\n");
printf(" --blob uuid=...,file=...\tAdd an image with the given UUID pointed to by file.\n");
printf(" --plat-toc-flags <value>\t16-bit platform specific flag field occupying bits 32-47 in 64-bit ToC header.\n");
printf("\n");
Expand All @@ -773,14 +797,16 @@ static int update_cmd(int argc, char *argv[])
char outfile[PATH_MAX] = { 0 };
fip_toc_header_t toc_header = { 0 };
unsigned long long toc_flags = 0;
unsigned long align = 1;
unsigned long align = 1, align_reserve = 0;
int pflag = 0;

if (argc < 2)
update_usage();

opts = fill_common_opts(opts, &nr_opts, required_argument);
opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
opts = add_opt(opts, &nr_opts, "align-reserve", required_argument,
OPT_ALIGN_RESERVE);
opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
Expand Down Expand Up @@ -831,6 +857,9 @@ static int update_cmd(int argc, char *argv[])
case OPT_ALIGN:
align = get_image_align(optarg);
break;
case OPT_ALIGN_RESERVE:
align_reserve = get_image_align_reservation(optarg);
break;
case 'o':
snprintf(outfile, sizeof(outfile), "%s", optarg);
break;
Expand All @@ -857,7 +886,7 @@ static int update_cmd(int argc, char *argv[])

update_fip();

pack_images(outfile, toc_flags, align);
pack_images(outfile, toc_flags, align, align_reserve);
return 0;
}

Expand All @@ -869,6 +898,7 @@ static void update_usage(void)
printf("\n");
printf("Options:\n");
printf(" --align <value>\t\tEach image is aligned to <value> (default: 1).\n");
printf(" --align-reserve <value>\t\tEach image is padded by <value> (default: 0).\n");
printf(" --blob uuid=...,file=...\tAdd or update an image with the given UUID pointed to by file.\n");
printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
printf(" --plat-toc-flags <value>\t16-bit platform specific flag field occupying bits 32-47 in 64-bit ToC header.\n");
Expand Down Expand Up @@ -1023,14 +1053,16 @@ static int remove_cmd(int argc, char *argv[])
char outfile[PATH_MAX] = { 0 };
fip_toc_header_t toc_header;
image_desc_t *desc;
unsigned long align = 1;
unsigned long align = 1, align_reserve = 0;
int fflag = 0;

if (argc < 2)
remove_usage();

opts = fill_common_opts(opts, &nr_opts, no_argument);
opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
opts = add_opt(opts, &nr_opts, "align-reserve", required_argument,
OPT_ALIGN_RESERVE);
opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
Expand All @@ -1054,6 +1086,9 @@ static int remove_cmd(int argc, char *argv[])
case OPT_ALIGN:
align = get_image_align(optarg);
break;
case OPT_ALIGN_RESERVE:
align_reserve = get_image_align_reservation(optarg);
break;
case 'b': {
char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
uuid_t uuid = { 0 };
Expand Down Expand Up @@ -1116,7 +1151,7 @@ static int remove_cmd(int argc, char *argv[])
}
}

pack_images(outfile, toc_header.flags, align);
pack_images(outfile, toc_header.flags, align, align_reserve);
return 0;
}

Expand All @@ -1128,6 +1163,7 @@ static void remove_usage(void)
printf("\n");
printf("Options:\n");
printf(" --align <value>\tEach image is aligned to <value> (default: 1).\n");
printf(" --align-reserve <value>\t\tEach image is padded by <value> (default: 0).\n");
printf(" --blob uuid=...\tRemove an image with the given UUID.\n");
printf(" --force\t\tIf the output FIP file already exists, use --force to overwrite it.\n");
printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
Expand Down

0 comments on commit df0995e

Please sign in to comment.