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 int imagetool_verify_print_header(
32 struct image_type_params *tparams,
33 struct image_tool_params *params)
36 struct image_type_params **curr;
37 INIT_SECTION(image_type);
39 struct image_type_params **start = __start_image_type;
40 struct image_type_params **end = __stop_image_type;
42 for (curr = start; curr != end; curr++) {
43 if ((*curr)->verify_header) {
44 retval = (*curr)->verify_header((unsigned char *)ptr,
45 sbuf->st_size, params);
49 * Print the image information if verify is
52 if ((*curr)->print_header) {
54 (*curr)->print_header(ptr);
57 "%s: print_header undefined for %s\n",
58 params->cmdname, (*curr)->name);
68 int imagetool_verify_print_header_by_type(
71 struct image_type_params *tparams,
72 struct image_tool_params *params)
76 retval = tparams->verify_header((unsigned char *)ptr, sbuf->st_size,
81 * Print the image information if verify is successful
83 if (tparams->print_header) {
85 tparams->print_header(ptr);
88 "%s: print_header undefined for %s\n",
89 params->cmdname, tparams->name);
93 "%s: verify_header failed for %s with exit code %d\n",
94 params->cmdname, tparams->name, retval);
100 int imagetool_save_subimage(
101 const char *file_name,
107 dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
110 fprintf(stderr, "Can't open \"%s\": %s\n",
111 file_name, strerror(errno));
115 if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
116 fprintf(stderr, "Write error on \"%s\": %s\n",
117 file_name, strerror(errno));
127 int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
132 fd = open(fname, O_RDONLY | O_BINARY);
134 fprintf(stderr, "%s: Can't open %s: %s\n",
135 params->cmdname, fname, strerror(errno));
139 if (fstat(fd, &sbuf) < 0) {
140 fprintf(stderr, "%s: Can't stat %s: %s\n",
141 params->cmdname, fname, strerror(errno));
150 time_t imagetool_get_source_date(
154 char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
156 if (source_date_epoch == NULL)
159 time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
161 if (gmtime(&time) == NULL) {
162 fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",