blob: e1021f44f5add76ca462f5668a565e1918ad14b8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -07002/*
3 * (C) Copyright 2013
4 *
5 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -07006 */
7
8#include "imagetool.h"
9
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020010#include <image.h>
11
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -020012struct image_type_params *imagetool_get_type(int type)
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020013{
Andreas Bießmann1fddd7b2015-02-09 00:06:10 +010014 struct image_type_params **curr;
15 INIT_SECTION(image_type);
16
17 struct image_type_params **start = __start_image_type;
18 struct image_type_params **end = __stop_image_type;
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020019
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -020020 for (curr = start; curr != end; curr++) {
Andreas Bießmann1fddd7b2015-02-09 00:06:10 +010021 if ((*curr)->check_image_type) {
22 if (!(*curr)->check_image_type(type))
23 return *curr;
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020024 }
25 }
26 return NULL;
27}
28
Pali Rohár11f29d42022-02-13 01:09:46 +010029static int imagetool_verify_print_header_by_type(
30 void *ptr,
31 struct stat *sbuf,
32 struct image_type_params *tparams,
33 struct image_tool_params *params);
34
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020035int imagetool_verify_print_header(
36 void *ptr,
37 struct stat *sbuf,
38 struct image_type_params *tparams,
39 struct image_tool_params *params)
40{
41 int retval = -1;
Andreas Bießmann1fddd7b2015-02-09 00:06:10 +010042 struct image_type_params **curr;
43 INIT_SECTION(image_type);
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020044
Andreas Bießmann1fddd7b2015-02-09 00:06:10 +010045 struct image_type_params **start = __start_image_type;
46 struct image_type_params **end = __stop_image_type;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -020047
Pali Rohár11f29d42022-02-13 01:09:46 +010048 if (tparams)
49 return imagetool_verify_print_header_by_type(ptr, sbuf, tparams, params);
50
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -020051 for (curr = start; curr != end; curr++) {
Andreas Bießmann1fddd7b2015-02-09 00:06:10 +010052 if ((*curr)->verify_header) {
53 retval = (*curr)->verify_header((unsigned char *)ptr,
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020054 sbuf->st_size, params);
55
56 if (retval == 0) {
57 /*
Jordan Handd32aa3c2019-03-05 14:47:56 -080058 * Print the image information if verify is
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020059 * successful
60 */
Andreas Bießmann1fddd7b2015-02-09 00:06:10 +010061 if ((*curr)->print_header) {
Simon Glassbd6e1422016-05-01 13:55:38 -060062 if (!params->quiet)
63 (*curr)->print_header(ptr);
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020064 } else {
65 fprintf(stderr,
66 "%s: print_header undefined for %s\n",
Andreas Bießmann1fddd7b2015-02-09 00:06:10 +010067 params->cmdname, (*curr)->name);
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020068 }
69 break;
70 }
71 }
72 }
73
Pali Rohár6c58aa12023-01-29 17:45:54 +010074 if (retval != 0) {
75 fprintf(stderr, "%s: cannot detect image type\n",
76 params->cmdname);
77 }
78
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020079 return retval;
80}
Guilherme Maciel Ferreira067d1562015-01-15 02:48:06 -020081
Pali Rohár11f29d42022-02-13 01:09:46 +010082static int imagetool_verify_print_header_by_type(
Jordan Handd32aa3c2019-03-05 14:47:56 -080083 void *ptr,
84 struct stat *sbuf,
85 struct image_type_params *tparams,
86 struct image_tool_params *params)
87{
Nicolas Heemeryck30705cd2022-04-20 23:58:39 +020088 int retval = -1;
Jordan Handd32aa3c2019-03-05 14:47:56 -080089
Nicolas Heemeryck30705cd2022-04-20 23:58:39 +020090 if (tparams->verify_header) {
91 retval = tparams->verify_header((unsigned char *)ptr,
92 sbuf->st_size, params);
Jordan Handd32aa3c2019-03-05 14:47:56 -080093
Nicolas Heemeryck30705cd2022-04-20 23:58:39 +020094 if (retval == 0) {
95 /*
96 * Print the image information if verify is successful
97 */
98 if (tparams->print_header) {
99 if (!params->quiet)
100 tparams->print_header(ptr);
101 } else {
102 fprintf(stderr,
103 "%s: print_header undefined for %s\n",
104 params->cmdname, tparams->name);
105 }
Jordan Handd32aa3c2019-03-05 14:47:56 -0800106 } else {
107 fprintf(stderr,
Nicolas Heemeryck30705cd2022-04-20 23:58:39 +0200108 "%s: verify_header failed for %s with exit code %d\n",
109 params->cmdname, tparams->name, retval);
Jordan Handd32aa3c2019-03-05 14:47:56 -0800110 }
Nicolas Heemeryck30705cd2022-04-20 23:58:39 +0200111
Jordan Handd32aa3c2019-03-05 14:47:56 -0800112 } else {
Pali Rohár27e46a92023-01-29 17:45:53 +0100113 fprintf(stderr, "%s: verify_header undefined for %s\n",
Nicolas Heemeryck30705cd2022-04-20 23:58:39 +0200114 params->cmdname, tparams->name);
Jordan Handd32aa3c2019-03-05 14:47:56 -0800115 }
116
117 return retval;
118}
119
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -0200120int imagetool_save_subimage(
Guilherme Maciel Ferreira067d1562015-01-15 02:48:06 -0200121 const char *file_name,
122 ulong file_data,
123 ulong file_len)
124{
125 int dfd;
126
127 dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
128 S_IRUSR | S_IWUSR);
129 if (dfd < 0) {
130 fprintf(stderr, "Can't open \"%s\": %s\n",
131 file_name, strerror(errno));
132 return -1;
133 }
134
135 if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
136 fprintf(stderr, "Write error on \"%s\": %s\n",
137 file_name, strerror(errno));
138 close(dfd);
139 return -1;
140 }
141
142 close(dfd);
143
144 return 0;
145}
Simon Glass3837ce62016-02-22 22:55:49 -0700146
147int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
148{
149 struct stat sbuf;
150 int fd;
151
152 fd = open(fname, O_RDONLY | O_BINARY);
153 if (fd < 0) {
154 fprintf(stderr, "%s: Can't open %s: %s\n",
155 params->cmdname, fname, strerror(errno));
156 return -1;
157 }
158
159 if (fstat(fd, &sbuf) < 0) {
160 fprintf(stderr, "%s: Can't stat %s: %s\n",
161 params->cmdname, fname, strerror(errno));
Simon Glassb12a81c2016-03-16 07:45:37 -0600162 close(fd);
Simon Glass3837ce62016-02-22 22:55:49 -0700163 return -1;
164 }
165 close(fd);
166
167 return sbuf.st_size;
168}
Vagrant Cascadian58470842016-06-16 12:28:40 -0700169
170time_t imagetool_get_source_date(
Alex Kiernan87925df2018-06-20 20:10:51 +0000171 const char *cmdname,
Vagrant Cascadian58470842016-06-16 12:28:40 -0700172 time_t fallback)
173{
174 char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
175
176 if (source_date_epoch == NULL)
177 return fallback;
178
179 time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
180
181 if (gmtime(&time) == NULL) {
182 fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",
Alex Kiernan87925df2018-06-20 20:10:51 +0000183 cmdname);
Vagrant Cascadian58470842016-06-16 12:28:40 -0700184 time = 0;
185 }
186
187 return time;
188}