1 // SPDX-License-Identifier: GPL-2.0+
5 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
12 struct image_type_params *imagetool_get_type(int type)
14 struct image_type_params **curr;
15 INIT_SECTION(image_type);
17 struct image_type_params **start = __start_image_type;
18 struct image_type_params **end = __stop_image_type;
20 for (curr = start; curr != end; curr++) {
21 if ((*curr)->check_image_type) {
22 if (!(*curr)->check_image_type(type))
29 static int imagetool_verify_print_header_by_type(
32 struct image_type_params *tparams,
33 struct image_tool_params *params);
35 int imagetool_verify_print_header(
38 struct image_type_params *tparams,
39 struct image_tool_params *params)
42 struct image_type_params **curr;
43 INIT_SECTION(image_type);
45 struct image_type_params **start = __start_image_type;
46 struct image_type_params **end = __stop_image_type;
49 return imagetool_verify_print_header_by_type(ptr, sbuf, tparams, params);
51 for (curr = start; curr != end; curr++) {
53 * Basically every data file can be guessed / verified as gpimage,
54 * so skip autodetection of data file as gpimage as it does not work.
56 if ((*curr)->check_image_type && (*curr)->check_image_type(IH_TYPE_GPIMAGE) == 0)
58 if ((*curr)->verify_header) {
59 retval = (*curr)->verify_header((unsigned char *)ptr,
60 sbuf->st_size, params);
64 * Print the image information if verify is
67 if ((*curr)->print_header) {
69 (*curr)->print_header(ptr);
72 "%s: print_header undefined for %s\n",
73 params->cmdname, (*curr)->name);
81 fprintf(stderr, "%s: cannot detect image type\n",
88 static int imagetool_verify_print_header_by_type(
91 struct image_type_params *tparams,
92 struct image_tool_params *params)
96 if (tparams->verify_header) {
97 retval = tparams->verify_header((unsigned char *)ptr,
98 sbuf->st_size, params);
102 * Print the image information if verify is successful
104 if (tparams->print_header) {
106 tparams->print_header(ptr);
109 "%s: print_header undefined for %s\n",
110 params->cmdname, tparams->name);
114 "%s: verify_header failed for %s with exit code %d\n",
115 params->cmdname, tparams->name, retval);
119 fprintf(stderr, "%s: verify_header undefined for %s\n",
120 params->cmdname, tparams->name);
126 int imagetool_save_subimage(
127 const char *file_name,
133 dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
136 fprintf(stderr, "Can't open \"%s\": %s\n",
137 file_name, strerror(errno));
141 if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
142 fprintf(stderr, "Write error on \"%s\": %s\n",
143 file_name, strerror(errno));
153 int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
158 fd = open(fname, O_RDONLY | O_BINARY);
160 fprintf(stderr, "%s: Can't open %s: %s\n",
161 params->cmdname, fname, strerror(errno));
165 if (fstat(fd, &sbuf) < 0) {
166 fprintf(stderr, "%s: Can't stat %s: %s\n",
167 params->cmdname, fname, strerror(errno));
176 time_t imagetool_get_source_date(
180 char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
182 if (source_date_epoch == NULL)
185 time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
187 if (gmtime(&time) == NULL) {
188 fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",