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++) {
52 if ((*curr)->verify_header) {
53 retval = (*curr)->verify_header((unsigned char *)ptr,
54 sbuf->st_size, params);
58 * Print the image information if verify is
61 if ((*curr)->print_header) {
63 (*curr)->print_header(ptr);
66 "%s: print_header undefined for %s\n",
67 params->cmdname, (*curr)->name);
77 static int imagetool_verify_print_header_by_type(
80 struct image_type_params *tparams,
81 struct image_tool_params *params)
85 retval = tparams->verify_header((unsigned char *)ptr, sbuf->st_size,
90 * Print the image information if verify is successful
92 if (tparams->print_header) {
94 tparams->print_header(ptr);
97 "%s: print_header undefined for %s\n",
98 params->cmdname, tparams->name);
102 "%s: verify_header failed for %s with exit code %d\n",
103 params->cmdname, tparams->name, retval);
109 int imagetool_save_subimage(
110 const char *file_name,
116 dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
119 fprintf(stderr, "Can't open \"%s\": %s\n",
120 file_name, strerror(errno));
124 if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
125 fprintf(stderr, "Write error on \"%s\": %s\n",
126 file_name, strerror(errno));
136 int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
141 fd = open(fname, O_RDONLY | O_BINARY);
143 fprintf(stderr, "%s: Can't open %s: %s\n",
144 params->cmdname, fname, strerror(errno));
148 if (fstat(fd, &sbuf) < 0) {
149 fprintf(stderr, "%s: Can't stat %s: %s\n",
150 params->cmdname, fname, strerror(errno));
159 time_t imagetool_get_source_date(
163 char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
165 if (source_date_epoch == NULL)
168 time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
170 if (gmtime(&time) == NULL) {
171 fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",