Commit 919ad05e authored by danh-arm's avatar danh-arm Committed by GitHub
Browse files

Merge pull request #773 from dp-arm/dp/fiptool-enhancements

Various fiptool enhancements and bug fixes
parents ad64ab28 a22f6285
...@@ -82,7 +82,7 @@ static size_t nr_images; ...@@ -82,7 +82,7 @@ static size_t nr_images;
static uuid_t uuid_null = { 0 }; static uuid_t uuid_null = { 0 };
static int verbose; static int verbose;
static void vlog(int prio, char *msg, va_list ap) static void vlog(int prio, const char *msg, va_list ap)
{ {
char *prefix[] = { "DEBUG", "WARN", "ERROR" }; char *prefix[] = { "DEBUG", "WARN", "ERROR" };
...@@ -91,7 +91,7 @@ static void vlog(int prio, char *msg, va_list ap) ...@@ -91,7 +91,7 @@ static void vlog(int prio, char *msg, va_list ap)
fputc('\n', stderr); fputc('\n', stderr);
} }
static void log_dbgx(char *msg, ...) static void log_dbgx(const char *msg, ...)
{ {
va_list ap; va_list ap;
...@@ -100,7 +100,7 @@ static void log_dbgx(char *msg, ...) ...@@ -100,7 +100,7 @@ static void log_dbgx(char *msg, ...)
va_end(ap); va_end(ap);
} }
static void log_warnx(char *msg, ...) static void log_warnx(const char *msg, ...)
{ {
va_list ap; va_list ap;
...@@ -109,7 +109,7 @@ static void log_warnx(char *msg, ...) ...@@ -109,7 +109,7 @@ static void log_warnx(char *msg, ...)
va_end(ap); va_end(ap);
} }
static void log_err(char *msg, ...) static void log_err(const char *msg, ...)
{ {
char buf[512]; char buf[512];
va_list ap; va_list ap;
...@@ -121,7 +121,7 @@ static void log_err(char *msg, ...) ...@@ -121,7 +121,7 @@ static void log_err(char *msg, ...)
exit(1); exit(1);
} }
static void log_errx(char *msg, ...) static void log_errx(const char *msg, ...)
{ {
va_list ap; va_list ap;
...@@ -131,6 +131,26 @@ static void log_errx(char *msg, ...) ...@@ -131,6 +131,26 @@ static void log_errx(char *msg, ...)
exit(1); exit(1);
} }
static char *xstrdup(const char *s, const char *msg)
{
char *d;
d = strdup(s);
if (d == NULL)
log_errx("strdup: ", msg);
return d;
}
static void *xmalloc(size_t size, const char *msg)
{
void *d;
d = malloc(size);
if (d == NULL)
log_errx("malloc: ", msg);
return d;
}
static void add_image(image_t *image) static void add_image(image_t *image)
{ {
if (nr_images + 1 > MAX_IMAGES) if (nr_images + 1 > MAX_IMAGES)
...@@ -210,7 +230,7 @@ static image_t *lookup_image_from_uuid(uuid_t *uuid) ...@@ -210,7 +230,7 @@ static image_t *lookup_image_from_uuid(uuid_t *uuid)
return NULL; return NULL;
} }
static int parse_fip(char *filename, fip_toc_header_t *toc_header_out) static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
{ {
struct stat st; struct stat st;
FILE *fp; FILE *fp;
...@@ -227,10 +247,7 @@ static int parse_fip(char *filename, fip_toc_header_t *toc_header_out) ...@@ -227,10 +247,7 @@ static int parse_fip(char *filename, fip_toc_header_t *toc_header_out)
if (fstat(fileno(fp), &st) == -1) if (fstat(fileno(fp), &st) == -1)
log_err("fstat %s", filename); log_err("fstat %s", filename);
buf = malloc(st.st_size); buf = xmalloc(st.st_size, "failed to load file into memory");
if (buf == NULL)
log_err("malloc");
if (fread(buf, 1, st.st_size, fp) != st.st_size) if (fread(buf, 1, st.st_size, fp) != st.st_size)
log_errx("Failed to read %s", filename); log_errx("Failed to read %s", filename);
bufend = buf + st.st_size; bufend = buf + st.st_size;
...@@ -261,16 +278,11 @@ static int parse_fip(char *filename, fip_toc_header_t *toc_header_out) ...@@ -261,16 +278,11 @@ static int parse_fip(char *filename, fip_toc_header_t *toc_header_out)
* Build a new image out of the ToC entry and add it to the * Build a new image out of the ToC entry and add it to the
* table of images. * table of images.
*/ */
image = malloc(sizeof(*image)); image = xmalloc(sizeof(*image),
if (image == NULL) "failed to allocate memory for image");
log_err("malloc");
memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t)); memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
image->buffer = xmalloc(toc_entry->size,
image->buffer = malloc(toc_entry->size); "failed to allocate image buffer, is FIP file corrupted?");
if (image->buffer == NULL)
log_err("malloc");
/* Overflow checks before memory copy. */ /* Overflow checks before memory copy. */
if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address) if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
log_errx("FIP %s is corrupted", filename); log_errx("FIP %s is corrupted", filename);
...@@ -293,7 +305,7 @@ static int parse_fip(char *filename, fip_toc_header_t *toc_header_out) ...@@ -293,7 +305,7 @@ static int parse_fip(char *filename, fip_toc_header_t *toc_header_out)
return 0; return 0;
} }
static image_t *read_image_from_file(uuid_t *uuid, char *filename) static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
{ {
struct stat st; struct stat st;
image_t *image; image_t *image;
...@@ -308,15 +320,9 @@ static image_t *read_image_from_file(uuid_t *uuid, char *filename) ...@@ -308,15 +320,9 @@ static image_t *read_image_from_file(uuid_t *uuid, char *filename)
if (fstat(fileno(fp), &st) == -1) if (fstat(fileno(fp), &st) == -1)
log_errx("fstat %s", filename); log_errx("fstat %s", filename);
image = malloc(sizeof(*image)); image = xmalloc(sizeof(*image), "failed to allocate memory for image");
if (image == NULL)
log_err("malloc");
memcpy(&image->uuid, uuid, sizeof(uuid_t)); memcpy(&image->uuid, uuid, sizeof(uuid_t));
image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
image->buffer = malloc(st.st_size);
if (image->buffer == NULL)
log_err("malloc");
if (fread(image->buffer, 1, st.st_size, fp) != st.st_size) if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
log_errx("Failed to read %s", filename); log_errx("Failed to read %s", filename);
image->size = st.st_size; image->size = st.st_size;
...@@ -325,7 +331,7 @@ static image_t *read_image_from_file(uuid_t *uuid, char *filename) ...@@ -325,7 +331,7 @@ static image_t *read_image_from_file(uuid_t *uuid, char *filename)
return image; return image;
} }
static int write_image_to_file(image_t *image, char *filename) static int write_image_to_file(const image_t *image, const char *filename)
{ {
FILE *fp; FILE *fp;
...@@ -570,9 +576,9 @@ static int create_cmd(int argc, char *argv[]) ...@@ -570,9 +576,9 @@ static int create_cmd(int argc, char *argv[])
add_opt(opts, ++i, NULL, 0, 0); add_opt(opts, ++i, NULL, 0, 0);
while (1) { while (1) {
int c, opt_index; int c, opt_index = 0;
c = getopt_long(argc, argv, "o:", opts, &opt_index); c = getopt_long(argc, argv, "", opts, &opt_index);
if (c == -1) if (c == -1)
break; break;
...@@ -582,9 +588,8 @@ static int create_cmd(int argc, char *argv[]) ...@@ -582,9 +588,8 @@ static int create_cmd(int argc, char *argv[])
toc_entry = &toc_entries[opt_index]; toc_entry = &toc_entries[opt_index];
toc_entry->action = DO_PACK; toc_entry->action = DO_PACK;
toc_entry->action_arg = strdup(optarg); toc_entry->action_arg = xstrdup(optarg,
if (toc_entry->action_arg == NULL) "failed to allocate memory for argument");
log_err("strdup");
break; break;
} }
case OPT_PLAT_TOC_FLAGS: case OPT_PLAT_TOC_FLAGS:
...@@ -641,7 +646,7 @@ static int update_cmd(int argc, char *argv[]) ...@@ -641,7 +646,7 @@ static int update_cmd(int argc, char *argv[])
add_opt(opts, ++i, NULL, 0, 0); add_opt(opts, ++i, NULL, 0, 0);
while (1) { while (1) {
int c, opt_index; int c, opt_index = 0;
c = getopt_long(argc, argv, "o:", opts, &opt_index); c = getopt_long(argc, argv, "o:", opts, &opt_index);
if (c == -1) if (c == -1)
...@@ -653,9 +658,8 @@ static int update_cmd(int argc, char *argv[]) ...@@ -653,9 +658,8 @@ static int update_cmd(int argc, char *argv[])
toc_entry = &toc_entries[opt_index]; toc_entry = &toc_entries[opt_index];
toc_entry->action = DO_PACK; toc_entry->action = DO_PACK;
toc_entry->action_arg = strdup(optarg); toc_entry->action_arg = xstrdup(optarg,
if (toc_entry->action_arg == NULL) "failed to allocate memory for argument");
log_err("strdup");
break; break;
} }
case OPT_PLAT_TOC_FLAGS: { case OPT_PLAT_TOC_FLAGS: {
...@@ -728,7 +732,7 @@ static int unpack_cmd(int argc, char *argv[]) ...@@ -728,7 +732,7 @@ static int unpack_cmd(int argc, char *argv[])
add_opt(opts, ++i, NULL, 0, 0); add_opt(opts, ++i, NULL, 0, 0);
while (1) { while (1) {
int c, opt_index; int c, opt_index = 0;
c = getopt_long(argc, argv, "fo:", opts, &opt_index); c = getopt_long(argc, argv, "fo:", opts, &opt_index);
if (c == -1) if (c == -1)
...@@ -739,9 +743,8 @@ static int unpack_cmd(int argc, char *argv[]) ...@@ -739,9 +743,8 @@ static int unpack_cmd(int argc, char *argv[])
unpack_all = 0; unpack_all = 0;
toc_entry = &toc_entries[opt_index]; toc_entry = &toc_entries[opt_index];
toc_entry->action = DO_UNPACK; toc_entry->action = DO_UNPACK;
toc_entry->action_arg = strdup(optarg); toc_entry->action_arg = xstrdup(optarg,
if (toc_entry->action_arg == NULL) "failed to allocate memory for argument");
log_err("strdup");
break; break;
case 'f': case 'f':
fflag = 1; fflag = 1;
...@@ -845,7 +848,7 @@ static int remove_cmd(int argc, char *argv[]) ...@@ -845,7 +848,7 @@ static int remove_cmd(int argc, char *argv[])
add_opt(opts, ++i, NULL, 0, 0); add_opt(opts, ++i, NULL, 0, 0);
while (1) { while (1) {
int c, opt_index; int c, opt_index = 0;
c = getopt_long(argc, argv, "fo:", opts, &opt_index); c = getopt_long(argc, argv, "fo:", opts, &opt_index);
if (c == -1) if (c == -1)
...@@ -977,17 +980,36 @@ int main(int argc, char *argv[]) ...@@ -977,17 +980,36 @@ int main(int argc, char *argv[])
{ {
int i, ret = 0; int i, ret = 0;
if (argc < 2) while (1) {
usage(); int c, opt_index = 0;
argc--, argv++; static struct option opts[] = {
{ "verbose", no_argument, NULL, 'v' },
{ NULL, no_argument, NULL, 0 }
};
/*
* Set POSIX mode so getopt stops at the first non-option
* which is the subcommand.
*/
c = getopt_long(argc, argv, "+v", opts, &opt_index);
if (c == -1)
break;
if (strcmp(argv[0], "-v") == 0 || switch (c) {
strcmp(argv[0], "--verbose") == 0) { case 'v':
verbose = 1; verbose = 1;
argc--, argv++; break;
if (argc < 1) default:
usage(); usage();
} }
}
argc -= optind;
argv += optind;
/* Reset optind for subsequent getopt processing. */
optind = 0;
if (argc == 0)
usage();
for (i = 0; i < NELEM(cmds); i++) { for (i = 0; i < NELEM(cmds); i++) {
if (strcmp(cmds[i].name, argv[0]) == 0) { if (strcmp(cmds[i].name, argv[0]) == 0) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment