2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-12-01 19:43:11 +00:00
|
|
|
/*
|
|
|
|
* Based on mkimage.c.
|
|
|
|
*
|
|
|
|
* Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dumpimage.h"
|
|
|
|
#include <image.h>
|
|
|
|
#include <version.h>
|
|
|
|
|
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
/* parameters initialized by core will be used by the image type code */
|
2022-02-13 00:09:46 +00:00
|
|
|
static struct image_tool_params params;
|
2013-12-01 19:43:11 +00:00
|
|
|
|
|
|
|
/*
|
2015-01-15 04:54:41 +00:00
|
|
|
* dumpimage_extract_subimage -
|
2013-12-01 19:43:11 +00:00
|
|
|
*
|
|
|
|
* It scans all registered image types,
|
|
|
|
* verifies image_header for each supported image type
|
|
|
|
* if verification is successful, it extracts the desired file,
|
|
|
|
* indexed by pflag, from the image
|
|
|
|
*
|
|
|
|
* returns negative if input image format does not match with any of
|
|
|
|
* supported image types
|
|
|
|
*/
|
2015-01-15 04:54:41 +00:00
|
|
|
static int dumpimage_extract_subimage(struct image_type_params *tparams,
|
2015-01-15 04:54:40 +00:00
|
|
|
void *ptr, struct stat *sbuf)
|
2013-12-01 19:43:11 +00:00
|
|
|
{
|
|
|
|
int retval = -1;
|
2015-01-15 04:54:40 +00:00
|
|
|
|
|
|
|
if (tparams->verify_header) {
|
|
|
|
retval = tparams->verify_header((unsigned char *)ptr,
|
|
|
|
sbuf->st_size, ¶ms);
|
2019-09-17 21:09:33 +00:00
|
|
|
if (retval != 0) {
|
|
|
|
fprintf(stderr, "%s: failed to verify header of %s\n",
|
|
|
|
params.cmdname, tparams->name);
|
2015-01-15 04:54:40 +00:00
|
|
|
return -1;
|
2019-09-17 21:09:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 04:54:40 +00:00
|
|
|
/*
|
|
|
|
* Extract the file from the image
|
|
|
|
* if verify is successful
|
|
|
|
*/
|
2015-01-15 04:54:41 +00:00
|
|
|
if (tparams->extract_subimage) {
|
|
|
|
retval = tparams->extract_subimage(ptr, ¶ms);
|
2019-09-17 21:09:33 +00:00
|
|
|
if (retval != 0) {
|
|
|
|
fprintf(stderr, "%s: extract_subimage failed for %s\n",
|
|
|
|
params.cmdname, tparams->name);
|
|
|
|
return -3;
|
|
|
|
}
|
2015-01-15 04:54:40 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
2015-01-15 04:54:41 +00:00
|
|
|
"%s: extract_subimage undefined for %s\n",
|
2015-01-15 04:54:40 +00:00
|
|
|
params.cmdname, tparams->name);
|
|
|
|
return -2;
|
2013-12-01 19:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
int ifd = -1;
|
|
|
|
struct stat sbuf;
|
|
|
|
char *ptr;
|
2019-01-26 02:31:52 +00:00
|
|
|
int retval = EXIT_SUCCESS;
|
2013-12-01 19:43:11 +00:00
|
|
|
struct image_type_params *tparams = NULL;
|
|
|
|
|
|
|
|
params.cmdname = *argv;
|
|
|
|
|
2019-01-26 02:31:53 +00:00
|
|
|
while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
|
2013-12-01 19:43:11 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'l':
|
|
|
|
params.lflag = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
params.outfile = optarg;
|
2019-01-26 02:31:51 +00:00
|
|
|
params.iflag = 1;
|
2013-12-01 19:43:11 +00:00
|
|
|
break;
|
2015-01-15 04:54:40 +00:00
|
|
|
case 'T':
|
|
|
|
params.type = genimg_get_type_id(optarg);
|
|
|
|
if (params.type < 0) {
|
2019-01-26 02:31:50 +00:00
|
|
|
fprintf(stderr, "%s: Invalid type\n",
|
|
|
|
params.cmdname);
|
2019-01-26 02:31:53 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2015-01-15 04:54:40 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-12-01 19:43:11 +00:00
|
|
|
case 'p':
|
|
|
|
params.pflag = strtoul(optarg, &ptr, 10);
|
|
|
|
if (*ptr) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: invalid file position %s\n",
|
|
|
|
params.cmdname, *argv);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
printf("dumpimage version %s\n", PLAIN_VERSION);
|
|
|
|
exit(EXIT_SUCCESS);
|
2019-01-26 02:31:53 +00:00
|
|
|
case 'h':
|
2013-12-01 19:43:11 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2015-01-15 04:54:40 +00:00
|
|
|
break;
|
2013-12-01 19:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 00:09:46 +00:00
|
|
|
if (argc < 2 || (params.iflag && params.lflag))
|
2019-01-26 02:31:53 +00:00
|
|
|
usage();
|
|
|
|
|
2019-01-26 02:31:50 +00:00
|
|
|
if (optind >= argc) {
|
|
|
|
fprintf(stderr, "%s: image file missing\n", params.cmdname);
|
2019-01-26 02:31:53 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2019-01-26 02:31:50 +00:00
|
|
|
}
|
2013-12-01 19:43:11 +00:00
|
|
|
|
2019-01-26 02:31:51 +00:00
|
|
|
params.imagefile = argv[optind];
|
|
|
|
|
2013-12-01 19:43:11 +00:00
|
|
|
/* set tparams as per input type_id */
|
2015-01-15 04:48:07 +00:00
|
|
|
tparams = imagetool_get_type(params.type);
|
2022-02-13 00:09:46 +00:00
|
|
|
if (!params.lflag && tparams == NULL) {
|
2015-01-15 04:54:40 +00:00
|
|
|
fprintf(stderr, "%s: unsupported type: %s\n",
|
2013-12-01 19:43:11 +00:00
|
|
|
params.cmdname, genimg_get_type_name(params.type));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check the passed arguments parameters meets the requirements
|
|
|
|
* as per image type to be generated/listed
|
|
|
|
*/
|
2022-02-13 00:09:46 +00:00
|
|
|
if (tparams && tparams->check_params) {
|
2019-01-26 02:31:50 +00:00
|
|
|
if (tparams->check_params(¶ms)) {
|
|
|
|
fprintf(stderr, "%s: Parameter check failed\n",
|
|
|
|
params.cmdname);
|
2019-01-26 02:31:53 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2019-01-26 02:31:50 +00:00
|
|
|
}
|
2013-12-01 19:43:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 02:31:51 +00:00
|
|
|
if (!params.lflag && !params.outfile) {
|
|
|
|
fprintf(stderr, "%s: No output file provided\n",
|
|
|
|
params.cmdname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-12-01 19:43:11 +00:00
|
|
|
|
|
|
|
ifd = open(params.imagefile, O_RDONLY|O_BINARY);
|
|
|
|
if (ifd < 0) {
|
2019-01-26 02:31:52 +00:00
|
|
|
fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname,
|
|
|
|
params.imagefile, strerror(errno));
|
2013-12-01 19:43:11 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-01-26 02:31:52 +00:00
|
|
|
if (fstat(ifd, &sbuf) < 0) {
|
|
|
|
fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname,
|
|
|
|
params.imagefile, strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-12-01 19:43:11 +00:00
|
|
|
|
2022-02-13 00:09:46 +00:00
|
|
|
if (tparams && (uint32_t)sbuf.st_size < tparams->header_size) {
|
2019-01-26 02:31:52 +00:00
|
|
|
fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n",
|
|
|
|
params.cmdname, params.imagefile);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-12-01 19:43:11 +00:00
|
|
|
|
2019-01-26 02:31:52 +00:00
|
|
|
ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
|
|
|
|
if (ptr == MAP_FAILED) {
|
|
|
|
fprintf(stderr, "%s: Can't read \"%s\": %s\n", params.cmdname,
|
|
|
|
params.imagefile, strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-12-01 19:43:11 +00:00
|
|
|
|
2019-01-26 02:31:52 +00:00
|
|
|
/*
|
|
|
|
* Both calls bellow scan through dumpimage registry for all
|
|
|
|
* supported image types and verify the input image file
|
|
|
|
* header for match
|
|
|
|
*/
|
|
|
|
if (params.iflag) {
|
2013-12-01 19:43:11 +00:00
|
|
|
/*
|
2019-01-26 02:31:52 +00:00
|
|
|
* Extract the data files from within the matched
|
|
|
|
* image type. Returns the error code if not matched
|
2013-12-01 19:43:11 +00:00
|
|
|
*/
|
2019-01-26 02:31:52 +00:00
|
|
|
retval = dumpimage_extract_subimage(tparams, ptr, &sbuf);
|
2019-09-17 21:09:33 +00:00
|
|
|
if (retval)
|
|
|
|
fprintf(stderr, "%s: Can't extract subimage from %s\n",
|
|
|
|
params.cmdname, params.imagefile);
|
2019-01-26 02:31:52 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Print the image information for matched image type
|
|
|
|
* Returns the error code if not matched
|
|
|
|
*/
|
|
|
|
retval = imagetool_verify_print_header(ptr, &sbuf, tparams,
|
|
|
|
¶ms);
|
2013-12-01 19:43:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 02:31:52 +00:00
|
|
|
(void)munmap((void *)ptr, sbuf.st_size);
|
2013-12-01 19:43:11 +00:00
|
|
|
(void)close(ifd);
|
|
|
|
|
2019-01-26 02:31:52 +00:00
|
|
|
return retval;
|
2013-12-01 19:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
2022-02-13 00:09:46 +00:00
|
|
|
fprintf(stderr, "Usage: %s [-T type] -l image\n"
|
|
|
|
" -l ==> list image header information\n"
|
|
|
|
" -T ==> parse image file as 'type'\n",
|
2013-12-01 19:43:11 +00:00
|
|
|
params.cmdname);
|
|
|
|
fprintf(stderr,
|
2019-01-26 02:31:54 +00:00
|
|
|
" %s [-T type] [-p position] [-o outfile] image\n"
|
|
|
|
" -T ==> declare image type as 'type'\n"
|
|
|
|
" -p ==> 'position' (starting at 0) of the component to extract from image\n"
|
|
|
|
" -o ==> extract component to file 'outfile'\n",
|
2013-12-01 19:43:11 +00:00
|
|
|
params.cmdname);
|
2019-01-26 02:31:53 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
" %s -h ==> print usage information and exit\n",
|
|
|
|
params.cmdname);
|
2013-12-01 19:43:11 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
" %s -V ==> print version information and exit\n",
|
|
|
|
params.cmdname);
|
|
|
|
|
2019-01-26 02:31:53 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2013-12-01 19:43:11 +00:00
|
|
|
}
|