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 if (tparams->verify_header) {
86 retval = tparams->verify_header((unsigned char *)ptr,
87 sbuf->st_size, params);
91 * Print the image information if verify is successful
93 if (tparams->print_header) {
95 tparams->print_header(ptr);
98 "%s: print_header undefined for %s\n",
99 params->cmdname, tparams->name);
103 "%s: verify_header failed for %s with exit code %d\n",
104 params->cmdname, tparams->name, retval);
108 fprintf(stderr, "%s: print_header undefined for %s\n",
109 params->cmdname, tparams->name);
115 int imagetool_save_subimage(
116 const char *file_name,
122 dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
125 fprintf(stderr, "Can't open \"%s\": %s\n",
126 file_name, strerror(errno));
130 if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
131 fprintf(stderr, "Write error on \"%s\": %s\n",
132 file_name, strerror(errno));
142 int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
147 fd = open(fname, O_RDONLY | O_BINARY);
149 fprintf(stderr, "%s: Can't open %s: %s\n",
150 params->cmdname, fname, strerror(errno));
154 if (fstat(fd, &sbuf) < 0) {
155 fprintf(stderr, "%s: Can't stat %s: %s\n",
156 params->cmdname, fname, strerror(errno));
165 time_t imagetool_get_source_date(
169 char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
171 if (source_date_epoch == NULL)
174 time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
176 if (gmtime(&time) == NULL) {
177 fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",