Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fiptool: Add --pad command line option #952

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions tools/fiptool/fiptool.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define OPT_TOC_ENTRY 0
#define OPT_PLAT_TOC_FLAGS 1
#define OPT_ALIGN 2
#define OPT_PAD 3

static int info_cmd(int argc, char *argv[]);
static void info_usage(void);
Expand Down Expand Up @@ -488,7 +489,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 padding)
{
FILE *fp;
image_desc_t *desc;
Expand Down Expand Up @@ -523,7 +525,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 + padding + 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 @@ -630,6 +632,19 @@ static unsigned long get_image_align(char *arg)
return align;
}

static unsigned long get_image_padding(char *arg)
{
char *endptr;
unsigned long padding;

errno = 0;
padding = strtoul(arg, &endptr, 0);
if (*endptr != '\0' || errno != 0)
log_errx("Invalid padding: %s", arg);

return padding;
}

static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
{
char *p;
Expand All @@ -650,7 +665,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, padding = 0;

if (argc < 2)
create_usage();
Expand All @@ -659,6 +674,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, "pad", required_argument,
OPT_PAD);
opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
opts = add_opt(opts, &nr_opts, NULL, 0, 0);

Expand All @@ -683,6 +700,9 @@ static int create_cmd(int argc, char *argv[])
case OPT_ALIGN:
align = get_image_align(optarg);
break;
case OPT_PAD:
padding = get_image_padding(optarg);
break;
case 'b': {
char name[_UUID_STR_LEN + 1];
char filename[PATH_MAX] = { 0 };
Expand Down Expand Up @@ -718,7 +738,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, padding);
return 0;
}

Expand All @@ -729,7 +749,8 @@ static void create_usage(void)
printf("fiptool create [opts] FIP_FILENAME\n");
printf("\n");
printf("Options:\n");
printf(" --align <value>\t\tEach image is aligned to <value> (default: 1).\n");
printf(" --align <bytes>\t\tEach image is aligned to <bytes> (default: 1).\n");
printf(" --pad <bytes>\t\t\tEach image is padded by <bytes> (default: 0).\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: Could say "... padded at the end by...". Same for other cases below.

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 @@ -747,14 +768,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, padding = 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, "pad", required_argument,
OPT_PAD);
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 @@ -805,6 +828,9 @@ static int update_cmd(int argc, char *argv[])
case OPT_ALIGN:
align = get_image_align(optarg);
break;
case OPT_PAD:
padding = get_image_padding(optarg);
break;
case 'o':
snprintf(outfile, sizeof(outfile), "%s", optarg);
break;
Expand All @@ -831,7 +857,7 @@ static int update_cmd(int argc, char *argv[])

update_fip();

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

Expand All @@ -842,7 +868,8 @@ static void update_usage(void)
printf("fiptool update [opts] FIP_FILENAME\n");
printf("\n");
printf("Options:\n");
printf(" --align <value>\t\tEach image is aligned to <value> (default: 1).\n");
printf(" --align <bytes>\t\tEach image is aligned to <bytes> (default: 1).\n");
printf(" --pad <bytes>\t\t\tEach image is padded by <bytes> (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 @@ -997,14 +1024,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, padding = 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, "pad", required_argument,
OPT_PAD);
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 @@ -1028,6 +1057,9 @@ static int remove_cmd(int argc, char *argv[])
case OPT_ALIGN:
align = get_image_align(optarg);
break;
case OPT_PAD:
padding = get_image_padding(optarg);
break;
case 'b': {
char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
uuid_t uuid = { 0 };
Expand Down Expand Up @@ -1090,7 +1122,7 @@ static int remove_cmd(int argc, char *argv[])
}
}

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

Expand All @@ -1101,7 +1133,8 @@ static void remove_usage(void)
printf("fiptool remove [opts] FIP_FILENAME\n");
printf("\n");
printf("Options:\n");
printf(" --align <value>\tEach image is aligned to <value> (default: 1).\n");
printf(" --align <bytes>\tEach image is aligned to <bytes> (default: 1).\n");
printf(" --pad <bytes>\t\tEach image is padded by <bytes> (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