Blame view

tools/imagetool.c 3.39 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
2
3
4
5
  /*
   * (C) Copyright 2013
   *
   * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
6
7
8
   */
  
  #include "imagetool.h"
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
9
  #include <image.h>
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
10
  struct image_type_params *imagetool_get_type(int type)
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
11
  {
1fddd7b63   Andreas Bießmann   tools/imagetool: ...
12
13
14
15
16
  	struct image_type_params **curr;
  	INIT_SECTION(image_type);
  
  	struct image_type_params **start = __start_image_type;
  	struct image_type_params **end = __stop_image_type;
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
17

a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
18
  	for (curr = start; curr != end; curr++) {
1fddd7b63   Andreas Bießmann   tools/imagetool: ...
19
20
21
  		if ((*curr)->check_image_type) {
  			if (!(*curr)->check_image_type(type))
  				return *curr;
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
22
23
24
25
26
27
28
29
30
31
32
33
  		}
  	}
  	return NULL;
  }
  
  int imagetool_verify_print_header(
  	void *ptr,
  	struct stat *sbuf,
  	struct image_type_params *tparams,
  	struct image_tool_params *params)
  {
  	int retval = -1;
1fddd7b63   Andreas Bießmann   tools/imagetool: ...
34
35
  	struct image_type_params **curr;
  	INIT_SECTION(image_type);
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
36

1fddd7b63   Andreas Bießmann   tools/imagetool: ...
37
38
  	struct image_type_params **start = __start_image_type;
  	struct image_type_params **end = __stop_image_type;
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
39
40
  
  	for (curr = start; curr != end; curr++) {
1fddd7b63   Andreas Bießmann   tools/imagetool: ...
41
42
  		if ((*curr)->verify_header) {
  			retval = (*curr)->verify_header((unsigned char *)ptr,
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
43
44
45
46
  						     sbuf->st_size, params);
  
  			if (retval == 0) {
  				/*
d32aa3cae   Jordan Hand   fdt: Fix FIT head...
47
  				 * Print the image information if verify is
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
48
49
  				 * successful
  				 */
1fddd7b63   Andreas Bießmann   tools/imagetool: ...
50
  				if ((*curr)->print_header) {
bd6e14209   Simon Glass   mkimage: Add a qu...
51
52
  					if (!params->quiet)
  						(*curr)->print_header(ptr);
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
53
54
55
56
  				} else {
  					fprintf(stderr,
  						"%s: print_header undefined for %s
  ",
1fddd7b63   Andreas Bießmann   tools/imagetool: ...
57
  						params->cmdname, (*curr)->name);
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
58
59
60
61
62
63
64
65
  				}
  				break;
  			}
  		}
  	}
  
  	return retval;
  }
067d15607   Guilherme Maciel Ferreira   imagetool: make t...
66

d32aa3cae   Jordan Hand   fdt: Fix FIT head...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  int imagetool_verify_print_header_by_type(
  	void *ptr,
  	struct stat *sbuf,
  	struct image_type_params *tparams,
  	struct image_tool_params *params)
  {
  	int retval;
  
  	retval = tparams->verify_header((unsigned char *)ptr, sbuf->st_size,
  			params);
  
  	if (retval == 0) {
  		/*
  		 * Print the image information if verify is successful
  		 */
  		if (tparams->print_header) {
  			if (!params->quiet)
  				tparams->print_header(ptr);
  		} else {
  			fprintf(stderr,
  				"%s: print_header undefined for %s
  ",
  				params->cmdname, tparams->name);
  		}
  	} else {
  		fprintf(stderr,
  			"%s: verify_header failed for %s with exit code %d
  ",
  			params->cmdname, tparams->name, retval);
  	}
  
  	return retval;
  }
67f946cd1   Guilherme Maciel Ferreira   dumpimage: replac...
100
  int imagetool_save_subimage(
067d15607   Guilherme Maciel Ferreira   imagetool: make t...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  	const char *file_name,
  	ulong file_data,
  	ulong file_len)
  {
  	int dfd;
  
  	dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
  		   S_IRUSR | S_IWUSR);
  	if (dfd < 0) {
  		fprintf(stderr, "Can't open \"%s\": %s
  ",
  			file_name, strerror(errno));
  		return -1;
  	}
  
  	if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
  		fprintf(stderr, "Write error on \"%s\": %s
  ",
  			file_name, strerror(errno));
  		close(dfd);
  		return -1;
  	}
  
  	close(dfd);
  
  	return 0;
  }
3837ce65b   Simon Glass   tools: Add a func...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  
  int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
  {
  	struct stat sbuf;
  	int fd;
  
  	fd = open(fname, O_RDONLY | O_BINARY);
  	if (fd < 0) {
  		fprintf(stderr, "%s: Can't open %s: %s
  ",
  			params->cmdname, fname, strerror(errno));
  		return -1;
  	}
  
  	if (fstat(fd, &sbuf) < 0) {
  		fprintf(stderr, "%s: Can't stat %s: %s
  ",
  			params->cmdname, fname, strerror(errno));
b12a81c4c   Simon Glass   mkimage: Close th...
146
  		close(fd);
3837ce65b   Simon Glass   tools: Add a func...
147
148
149
150
151
152
  		return -1;
  	}
  	close(fd);
  
  	return sbuf.st_size;
  }
5847084f6   Vagrant Cascadian   Respect SOURCE_DA...
153
154
  
  time_t imagetool_get_source_date(
87925df2b   Alex Kiernan   mkimage: Refactor...
155
  	 const char *cmdname,
5847084f6   Vagrant Cascadian   Respect SOURCE_DA...
156
157
158
159
160
161
162
163
164
165
166
167
  	 time_t fallback)
  {
  	char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  
  	if (source_date_epoch == NULL)
  		return fallback;
  
  	time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
  
  	if (gmtime(&time) == NULL) {
  		fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid
  ",
87925df2b   Alex Kiernan   mkimage: Refactor...
168
  			cmdname);
5847084f6   Vagrant Cascadian   Respect SOURCE_DA...
169
170
171
172
173
  		time = 0;
  	}
  
  	return time;
  }