blob: 5ad6d7413fec1f2561da24896ce1b80d0a80f050 [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
74 return retval;
75}
Guilherme Maciel Ferreira067d1562015-01-15 02:48:06 -020076
Pali Rohár11f29d42022-02-13 01:09:46 +010077static int imagetool_verify_print_header_by_type(
Jordan Handd32aa3c2019-03-05 14:47:56 -080078 void *ptr,
79 struct stat *sbuf,
80 struct image_type_params *tparams,
81 struct image_tool_params *params)
82{
83 int retval;
84
85 retval = tparams->verify_header((unsigned char *)ptr, sbuf->st_size,
86 params);
87
88 if (retval == 0) {
89 /*
90 * Print the image information if verify is successful
91 */
92 if (tparams->print_header) {
93 if (!params->quiet)
94 tparams->print_header(ptr);
95 } else {
96 fprintf(stderr,
97 "%s: print_header undefined for %s\n",
98 params->cmdname, tparams->name);
99 }
100 } else {
101 fprintf(stderr,
102 "%s: verify_header failed for %s with exit code %d\n",
103 params->cmdname, tparams->name, retval);
104 }
105
106 return retval;
107}
108
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -0200109int imagetool_save_subimage(
Guilherme Maciel Ferreira067d1562015-01-15 02:48:06 -0200110 const char *file_name,
111 ulong file_data,
112 ulong file_len)
113{
114 int dfd;
115
116 dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
117 S_IRUSR | S_IWUSR);
118 if (dfd < 0) {
119 fprintf(stderr, "Can't open \"%s\": %s\n",
120 file_name, strerror(errno));
121 return -1;
122 }
123
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));
127 close(dfd);
128 return -1;
129 }
130
131 close(dfd);
132
133 return 0;
134}
Simon Glass3837ce62016-02-22 22:55:49 -0700135
136int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
137{
138 struct stat sbuf;
139 int fd;
140
141 fd = open(fname, O_RDONLY | O_BINARY);
142 if (fd < 0) {
143 fprintf(stderr, "%s: Can't open %s: %s\n",
144 params->cmdname, fname, strerror(errno));
145 return -1;
146 }
147
148 if (fstat(fd, &sbuf) < 0) {
149 fprintf(stderr, "%s: Can't stat %s: %s\n",
150 params->cmdname, fname, strerror(errno));
Simon Glassb12a81c2016-03-16 07:45:37 -0600151 close(fd);
Simon Glass3837ce62016-02-22 22:55:49 -0700152 return -1;
153 }
154 close(fd);
155
156 return sbuf.st_size;
157}
Vagrant Cascadian58470842016-06-16 12:28:40 -0700158
159time_t imagetool_get_source_date(
Alex Kiernan87925df2018-06-20 20:10:51 +0000160 const char *cmdname,
Vagrant Cascadian58470842016-06-16 12:28:40 -0700161 time_t fallback)
162{
163 char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
164
165 if (source_date_epoch == NULL)
166 return fallback;
167
168 time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
169
170 if (gmtime(&time) == NULL) {
171 fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",
Alex Kiernan87925df2018-06-20 20:10:51 +0000172 cmdname);
Vagrant Cascadian58470842016-06-16 12:28:40 -0700173 time = 0;
174 }
175
176 return time;
177}