2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-12-01 19:43:10 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2013
|
|
|
|
*
|
|
|
|
* Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "imagetool.h"
|
|
|
|
|
2015-01-15 04:48:05 +00:00
|
|
|
#include <image.h>
|
|
|
|
|
2015-01-15 04:48:07 +00:00
|
|
|
struct image_type_params *imagetool_get_type(int type)
|
2015-01-15 04:48:05 +00:00
|
|
|
{
|
2015-02-08 23:06:10 +00:00
|
|
|
struct image_type_params **curr;
|
|
|
|
INIT_SECTION(image_type);
|
|
|
|
|
|
|
|
struct image_type_params **start = __start_image_type;
|
|
|
|
struct image_type_params **end = __stop_image_type;
|
2015-01-15 04:48:05 +00:00
|
|
|
|
2015-01-15 04:48:07 +00:00
|
|
|
for (curr = start; curr != end; curr++) {
|
2015-02-08 23:06:10 +00:00
|
|
|
if ((*curr)->check_image_type) {
|
|
|
|
if (!(*curr)->check_image_type(type))
|
|
|
|
return *curr;
|
2015-01-15 04:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-02-13 00:09:46 +00:00
|
|
|
static int imagetool_verify_print_header_by_type(
|
|
|
|
void *ptr,
|
|
|
|
struct stat *sbuf,
|
|
|
|
struct image_type_params *tparams,
|
|
|
|
struct image_tool_params *params);
|
|
|
|
|
2015-01-15 04:48:05 +00:00
|
|
|
int imagetool_verify_print_header(
|
|
|
|
void *ptr,
|
|
|
|
struct stat *sbuf,
|
|
|
|
struct image_type_params *tparams,
|
|
|
|
struct image_tool_params *params)
|
|
|
|
{
|
|
|
|
int retval = -1;
|
2015-02-08 23:06:10 +00:00
|
|
|
struct image_type_params **curr;
|
|
|
|
INIT_SECTION(image_type);
|
2015-01-15 04:48:05 +00:00
|
|
|
|
2015-02-08 23:06:10 +00:00
|
|
|
struct image_type_params **start = __start_image_type;
|
|
|
|
struct image_type_params **end = __stop_image_type;
|
2015-01-15 04:48:07 +00:00
|
|
|
|
2022-02-13 00:09:46 +00:00
|
|
|
if (tparams)
|
|
|
|
return imagetool_verify_print_header_by_type(ptr, sbuf, tparams, params);
|
|
|
|
|
2015-01-15 04:48:07 +00:00
|
|
|
for (curr = start; curr != end; curr++) {
|
2023-01-29 16:45:55 +00:00
|
|
|
/*
|
|
|
|
* Basically every data file can be guessed / verified as gpimage,
|
|
|
|
* so skip autodetection of data file as gpimage as it does not work.
|
|
|
|
*/
|
|
|
|
if ((*curr)->check_image_type && (*curr)->check_image_type(IH_TYPE_GPIMAGE) == 0)
|
|
|
|
continue;
|
2015-02-08 23:06:10 +00:00
|
|
|
if ((*curr)->verify_header) {
|
|
|
|
retval = (*curr)->verify_header((unsigned char *)ptr,
|
2015-01-15 04:48:05 +00:00
|
|
|
sbuf->st_size, params);
|
|
|
|
|
|
|
|
if (retval == 0) {
|
|
|
|
/*
|
2019-03-05 22:47:56 +00:00
|
|
|
* Print the image information if verify is
|
2015-01-15 04:48:05 +00:00
|
|
|
* successful
|
|
|
|
*/
|
2015-02-08 23:06:10 +00:00
|
|
|
if ((*curr)->print_header) {
|
2016-05-01 19:55:38 +00:00
|
|
|
if (!params->quiet)
|
2023-03-29 19:25:54 +00:00
|
|
|
(*curr)->print_header(ptr, params);
|
2015-01-15 04:48:05 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: print_header undefined for %s\n",
|
2015-02-08 23:06:10 +00:00
|
|
|
params->cmdname, (*curr)->name);
|
2015-01-15 04:48:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 16:45:54 +00:00
|
|
|
if (retval != 0) {
|
|
|
|
fprintf(stderr, "%s: cannot detect image type\n",
|
|
|
|
params->cmdname);
|
|
|
|
}
|
|
|
|
|
2015-01-15 04:48:05 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2015-01-15 04:48:06 +00:00
|
|
|
|
2022-02-13 00:09:46 +00:00
|
|
|
static int imagetool_verify_print_header_by_type(
|
2019-03-05 22:47:56 +00:00
|
|
|
void *ptr,
|
|
|
|
struct stat *sbuf,
|
|
|
|
struct image_type_params *tparams,
|
|
|
|
struct image_tool_params *params)
|
|
|
|
{
|
2022-04-20 21:58:39 +00:00
|
|
|
int retval = -1;
|
|
|
|
|
|
|
|
if (tparams->verify_header) {
|
|
|
|
retval = tparams->verify_header((unsigned char *)ptr,
|
|
|
|
sbuf->st_size, params);
|
|
|
|
|
|
|
|
if (retval == 0) {
|
|
|
|
/*
|
|
|
|
* Print the image information if verify is successful
|
|
|
|
*/
|
|
|
|
if (tparams->print_header) {
|
|
|
|
if (!params->quiet)
|
2023-03-29 19:25:54 +00:00
|
|
|
tparams->print_header(ptr, params);
|
2022-04-20 21:58:39 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: print_header undefined for %s\n",
|
|
|
|
params->cmdname, tparams->name);
|
|
|
|
}
|
2019-03-05 22:47:56 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
2022-04-20 21:58:39 +00:00
|
|
|
"%s: verify_header failed for %s with exit code %d\n",
|
|
|
|
params->cmdname, tparams->name, retval);
|
2019-03-05 22:47:56 +00:00
|
|
|
}
|
2022-04-20 21:58:39 +00:00
|
|
|
|
2019-03-05 22:47:56 +00:00
|
|
|
} else {
|
2023-01-29 16:45:53 +00:00
|
|
|
fprintf(stderr, "%s: verify_header undefined for %s\n",
|
2022-04-20 21:58:39 +00:00
|
|
|
params->cmdname, tparams->name);
|
2019-03-05 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-01-15 04:54:41 +00:00
|
|
|
int imagetool_save_subimage(
|
2015-01-15 04:48:06 +00:00
|
|
|
const char *file_name,
|
|
|
|
ulong file_data,
|
|
|
|
ulong file_len)
|
|
|
|
{
|
|
|
|
int dfd;
|
|
|
|
|
|
|
|
dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
|
|
|
|
S_IRUSR | S_IWUSR);
|
|
|
|
if (dfd < 0) {
|
|
|
|
fprintf(stderr, "Can't open \"%s\": %s\n",
|
|
|
|
file_name, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
|
|
|
|
fprintf(stderr, "Write error on \"%s\": %s\n",
|
|
|
|
file_name, strerror(errno));
|
|
|
|
close(dfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(dfd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-23 05:55:49 +00:00
|
|
|
|
|
|
|
int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
|
|
|
|
{
|
|
|
|
struct stat sbuf;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(fname, O_RDONLY | O_BINARY);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "%s: Can't open %s: %s\n",
|
|
|
|
params->cmdname, fname, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &sbuf) < 0) {
|
|
|
|
fprintf(stderr, "%s: Can't stat %s: %s\n",
|
|
|
|
params->cmdname, fname, strerror(errno));
|
2016-03-16 13:45:37 +00:00
|
|
|
close(fd);
|
2016-02-23 05:55:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return sbuf.st_size;
|
|
|
|
}
|
2016-06-16 19:28:40 +00:00
|
|
|
|
|
|
|
time_t imagetool_get_source_date(
|
2018-06-20 20:10:51 +00:00
|
|
|
const char *cmdname,
|
2016-06-16 19:28:40 +00:00
|
|
|
time_t fallback)
|
|
|
|
{
|
|
|
|
char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
|
|
|
|
|
|
|
|
if (source_date_epoch == NULL)
|
|
|
|
return fallback;
|
|
|
|
|
|
|
|
time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
|
|
|
|
|
|
|
|
if (gmtime(&time) == NULL) {
|
|
|
|
fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",
|
2018-06-20 20:10:51 +00:00
|
|
|
cmdname);
|
2016-06-16 19:28:40 +00:00
|
|
|
time = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|