blob: a4e39b24bca79bdd24adabdafd54d9b6be2f1a37 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -07002/*
3 * (C) Copyright 2013
4 *
5 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -07006 */
7
8#include "imagetool.h"
9
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020010#include <image.h>
11
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -020012struct image_type_params *imagetool_get_type(int type)
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020013{
Andreas Bießmannd631d702015-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 Ferreira0b338032015-01-15 02:48:05 -020019
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -020020 for (curr = start; curr != end; curr++) {
Andreas Bießmannd631d702015-02-09 00:06:10 +010021 if ((*curr)->check_image_type) {
22 if (!(*curr)->check_image_type(type))
23 return *curr;
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020024 }
25 }
26 return NULL;
27}
28
29int imagetool_verify_print_header(
30 void *ptr,
31 struct stat *sbuf,
32 struct image_type_params *tparams,
33 struct image_tool_params *params)
34{
35 int retval = -1;
Andreas Bießmannd631d702015-02-09 00:06:10 +010036 struct image_type_params **curr;
37 INIT_SECTION(image_type);
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020038
Andreas Bießmannd631d702015-02-09 00:06:10 +010039 struct image_type_params **start = __start_image_type;
40 struct image_type_params **end = __stop_image_type;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -020041
42 for (curr = start; curr != end; curr++) {
Andreas Bießmannd631d702015-02-09 00:06:10 +010043 if ((*curr)->verify_header) {
44 retval = (*curr)->verify_header((unsigned char *)ptr,
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020045 sbuf->st_size, params);
46
47 if (retval == 0) {
48 /*
49 * Print the image information if verify is
50 * successful
51 */
Andreas Bießmannd631d702015-02-09 00:06:10 +010052 if ((*curr)->print_header) {
Simon Glassda1805f2016-05-01 13:55:38 -060053 if (!params->quiet)
54 (*curr)->print_header(ptr);
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020055 } else {
56 fprintf(stderr,
57 "%s: print_header undefined for %s\n",
Andreas Bießmannd631d702015-02-09 00:06:10 +010058 params->cmdname, (*curr)->name);
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020059 }
60 break;
61 }
62 }
63 }
64
65 return retval;
66}
Guilherme Maciel Ferreirafc3c9672015-01-15 02:48:06 -020067
Guilherme Maciel Ferreiraf3f4a0a2015-01-15 02:54:41 -020068int imagetool_save_subimage(
Guilherme Maciel Ferreirafc3c9672015-01-15 02:48:06 -020069 const char *file_name,
70 ulong file_data,
71 ulong file_len)
72{
73 int dfd;
74
75 dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
76 S_IRUSR | S_IWUSR);
77 if (dfd < 0) {
78 fprintf(stderr, "Can't open \"%s\": %s\n",
79 file_name, strerror(errno));
80 return -1;
81 }
82
83 if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
84 fprintf(stderr, "Write error on \"%s\": %s\n",
85 file_name, strerror(errno));
86 close(dfd);
87 return -1;
88 }
89
90 close(dfd);
91
92 return 0;
93}
Simon Glass1c570232016-02-22 22:55:49 -070094
95int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
96{
97 struct stat sbuf;
98 int fd;
99
100 fd = open(fname, O_RDONLY | O_BINARY);
101 if (fd < 0) {
102 fprintf(stderr, "%s: Can't open %s: %s\n",
103 params->cmdname, fname, strerror(errno));
104 return -1;
105 }
106
107 if (fstat(fd, &sbuf) < 0) {
108 fprintf(stderr, "%s: Can't stat %s: %s\n",
109 params->cmdname, fname, strerror(errno));
Simon Glassa1c090b2016-03-16 07:45:37 -0600110 close(fd);
Simon Glass1c570232016-02-22 22:55:49 -0700111 return -1;
112 }
113 close(fd);
114
115 return sbuf.st_size;
116}
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -0700117
118time_t imagetool_get_source_date(
119 struct image_tool_params *params,
120 time_t fallback)
121{
122 char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
123
124 if (source_date_epoch == NULL)
125 return fallback;
126
127 time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
128
129 if (gmtime(&time) == NULL) {
130 fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",
131 params->cmdname);
132 time = 0;
133 }
134
135 return time;
136}