Blame view

tools/imagetool.h 10.2 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
9
10
11
12
13
   */
  
  #ifndef _IMAGETOOL_H_
  #define _IMAGETOOL_H_
  
  #include "os_support.h"
  #include <errno.h>
  #include <fcntl.h>
d2bf1152c   Masahiro Yamada   tools: include ne...
14
  #include <stdbool.h>
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
15
16
17
18
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <sys/stat.h>
26e355d13   Jörg Krause   Fix musl build
19
  #include <sys/types.h>
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
20
21
  #include <time.h>
  #include <unistd.h>
2b9912e6a   Jeroen Hofstee   includes: move op...
22
  #include <u-boot/sha1.h>
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
23

f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
24
25
26
27
28
  #include "fdt_host.h"
  
  #define ARRAY_SIZE(x)		(sizeof(x) / sizeof((x)[0]))
  
  #define IH_ARCH_DEFAULT		IH_ARCH_INVALID
fb4cce0f9   Simon Glass   mkimage: Support ...
29
30
31
32
33
34
  /* Information about a file that needs to be placed into the FIT */
  struct content_info {
  	struct content_info *next;
  	int type;		/* File type (IH_TYPE_...) */
  	const char *fname;
  };
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
35
36
37
38
39
40
41
42
43
  /*
   * This structure defines all such variables those are initialized by
   * mkimage and dumpimage main core and need to be referred by image
   * type specific functions
   */
  struct image_tool_params {
  	int dflag;
  	int eflag;
  	int fflag;
a804b5ce2   Guilherme Maciel Ferreira   Add dumpimage, a ...
44
  	int iflag;
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
45
  	int lflag;
a804b5ce2   Guilherme Maciel Ferreira   Add dumpimage, a ...
46
  	int pflag;
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  	int vflag;
  	int xflag;
  	int skipcpy;
  	int os;
  	int arch;
  	int type;
  	int comp;
  	char *dtc;
  	unsigned int addr;
  	unsigned int ep;
  	char *imagename;
  	char *imagename2;
  	char *datafile;
  	char *imagefile;
  	char *cmdname;
a804b5ce2   Guilherme Maciel Ferreira   Add dumpimage, a ...
62
  	const char *outfile;	/* Output filename */
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
63
64
65
66
  	const char *keydir;	/* Directory holding private keys */
  	const char *keydest;	/* Destination .dtb for public key */
  	const char *comment;	/* Comment to add to signature node */
  	int require_keys;	/* 1 to mark signing keys as 'required' */
92a655c32   Simon Glass   mkimage: Set up a...
67
  	int file_size;		/* Total size of output file */
1b99e5bbb   Simon Glass   mkimage: Allow th...
68
  	int orig_file_size;	/* Original size for file before padding */
8e35bb07e   Simon Glass   mkimage: Support ...
69
  	bool auto_its;		/* Automatically create the .its file */
d505a09c1   Simon Glass   mkimage: Allow a ...
70
  	int fit_image_type;	/* Image type to put into the FIT */
0f7c6cdc8   Tomeu Vizoso   mkimage: Allow in...
71
  	char *fit_ramdisk;	/* Ramdisk file to include */
fb4cce0f9   Simon Glass   mkimage: Support ...
72
73
  	struct content_info *content_head;	/* List of files to include */
  	struct content_info *content_tail;
722ebc8f8   Simon Glass   mkimage: Support ...
74
  	bool external_data;	/* Store data outside the FIT */
bd6e14209   Simon Glass   mkimage: Add a qu...
75
  	bool quiet;		/* Don't output text in normal operation */
f8f9107d9   Teddy Reed   mkimage: fit: spl...
76
  	unsigned int external_offset;	/* Add padding to external data */
f1ca1fdeb   George McCollister   mkimage: Add supp...
77
  	const char *engine_id;	/* Engine to use for signing */
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  };
  
  /*
   * image type specific variables and callback functions
   */
  struct image_type_params {
  	/* name is an identification tag string for added support */
  	char *name;
  	/*
  	 * header size is local to the specific image type to be supported,
  	 * mkimage core treats this as number of bytes
  	 */
  	uint32_t header_size;
  	/* Image type header pointer */
  	void *hdr;
  	/*
  	 * There are several arguments that are passed on the command line
  	 * and are registered as flags in image_tool_params structure.
  	 * This callback function can be used to check the passed arguments
  	 * are in-lined with the image type to be supported
  	 *
  	 * Returns 1 if parameter check is successful
  	 */
  	int (*check_params) (struct image_tool_params *);
  	/*
  	 * This function is used by list command (i.e. mkimage -l <filename>)
  	 * image type verification code must be put here
  	 *
  	 * Returns 0 if image header verification is successful
  	 * otherwise, returns respective negative error codes
  	 */
  	int (*verify_header) (unsigned char *, int, struct image_tool_params *);
  	/* Prints image information abstracting from image header */
  	void (*print_header) (const void *);
  	/*
  	 * The header or image contents need to be set as per image type to
  	 * be generated using this callback function.
  	 * further output file post processing (for ex. checksum calculation,
  	 * padding bytes etc..) can also be done in this callback function.
  	 */
  	void (*set_header) (void *, struct stat *, int,
  					struct image_tool_params *);
  	/*
67f946cd1   Guilherme Maciel Ferreira   dumpimage: replac...
121
122
123
  	 * This function is used by the command to retrieve a component
  	 * (sub-image) from the image (i.e. dumpimage -i <image> -p <position>
  	 * <sub-image-name>).
a804b5ce2   Guilherme Maciel Ferreira   Add dumpimage, a ...
124
125
126
127
128
  	 * Thus the code to extract a file from an image must be put here.
  	 *
  	 * Returns 0 if the file was successfully retrieved from the image,
  	 * or a negative value on error.
  	 */
67f946cd1   Guilherme Maciel Ferreira   dumpimage: replac...
129
  	int (*extract_subimage)(void *, struct image_tool_params *);
a804b5ce2   Guilherme Maciel Ferreira   Add dumpimage, a ...
130
  	/*
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	 * Some image generation support for ex (default image type) supports
  	 * more than one type_ids, this callback function is used to check
  	 * whether input (-T <image_type>) is supported by registered image
  	 * generation/list low level code
  	 */
  	int (*check_image_type) (uint8_t);
  	/* This callback function will be executed if fflag is defined */
  	int (*fflag_handle) (struct image_tool_params *);
  	/*
  	 * This callback function will be executed for variable size record
  	 * It is expected to build this header in memory and return its length
  	 * and a pointer to it by using image_type_params.header_size and
  	 * image_type_params.hdr. The return value shall indicate if an
  	 * additional padding should be used when copying the data image
  	 * by returning the padding length.
  	 */
  	int (*vrec_header) (struct image_tool_params *,
  		struct image_type_params *);
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
149
  };
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
150
151
152
153
154
155
156
157
158
159
160
  /**
   * imagetool_get_type() - find the image type params for a given image type
   *
   * It scans all registers image type supports
   * checks the input type for each supported image type
   *
   * if successful,
   *     returns respective image_type_params pointer if success
   * if input type_id is not supported by any of image_type_support
   *     returns NULL
   */
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
161
  struct image_type_params *imagetool_get_type(int type);
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  
  /*
   * imagetool_verify_print_header() - verifies the image header
   *
   * Scan registered image types and verify the image_header for each
   * supported image type. If verification is successful, this prints
   * the respective header.
   *
   * @return 0 on success, negative if input image format does not match with
   * any of supported image types
   */
  int imagetool_verify_print_header(
  	void *ptr,
  	struct stat *sbuf,
d32aa3cae   Jordan Hand   fdt: Fix FIT head...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  	struct image_type_params *tparams,
  	struct image_tool_params *params);
  
  /*
   * imagetool_verify_print_header_by_type() - verifies the image header
   *
   * Verify the image_header for the image type given by tparams.
   * If verification is successful, this prints the respective header.
   * @ptr: pointer the the image header
   * @sbuf: stat information about the file pointed to by ptr
   * @tparams: image type parameters
   * @params: mkimage parameters
   *
   * @return 0 on success, negative if input image format does not match with
   * the given image type
   */
  int imagetool_verify_print_header_by_type(
  	void *ptr,
  	struct stat *sbuf,
0ca6691c2   Guilherme Maciel Ferreira   imagetool: move c...
195
196
  	struct image_type_params *tparams,
  	struct image_tool_params *params);
067d15607   Guilherme Maciel Ferreira   imagetool: make t...
197
  /**
67f946cd1   Guilherme Maciel Ferreira   dumpimage: replac...
198
   * imagetool_save_subimage - store data into a file
067d15607   Guilherme Maciel Ferreira   imagetool: make t...
199
200
201
202
   * @file_name: name of the destination file
   * @file_data: data to be written
   * @file_len: the amount of data to store
   *
67f946cd1   Guilherme Maciel Ferreira   dumpimage: replac...
203
   * imagetool_save_subimage() store file_len bytes of data pointed by file_data
067d15607   Guilherme Maciel Ferreira   imagetool: make t...
204
205
206
207
208
   * into the file name by file_name.
   *
   * returns:
   *     zero in case of success or a negative value if fail.
   */
67f946cd1   Guilherme Maciel Ferreira   dumpimage: replac...
209
  int imagetool_save_subimage(
067d15607   Guilherme Maciel Ferreira   imagetool: make t...
210
211
212
  	const char *file_name,
  	ulong file_data,
  	ulong file_len);
3837ce65b   Simon Glass   tools: Add a func...
213
214
215
216
217
218
219
220
221
222
223
  /**
   * imagetool_get_filesize() - Utility function to obtain the size of a file
   *
   * This function prints a message if an error occurs, showing the error that
   * was obtained.
   *
   * @params:	mkimage parameters
   * @fname:	filename to check
   * @return size of file, or -ve value on error
   */
  int imagetool_get_filesize(struct image_tool_params *params, const char *fname);
5847084f6   Vagrant Cascadian   Respect SOURCE_DA...
224
225
226
227
228
229
230
231
  /**
   * imagetool_get_source_date() - Get timestamp for build output.
   *
   * Gets a timestamp for embedding it in a build output. If set
   * SOURCE_DATE_EPOCH is used. Else the given fallback value is returned. Prints
   * an error message if SOURCE_DATE_EPOCH contains an invalid value and returns
   * 0.
   *
87925df2b   Alex Kiernan   mkimage: Refactor...
232
   * @cmdname:	command name
5847084f6   Vagrant Cascadian   Respect SOURCE_DA...
233
234
235
236
   * @fallback:	timestamp to use if SOURCE_DATE_EPOCH isn't set
   * @return timestamp based on SOURCE_DATE_EPOCH
   */
  time_t imagetool_get_source_date(
87925df2b   Alex Kiernan   mkimage: Refactor...
237
  	const char *cmdname,
5847084f6   Vagrant Cascadian   Respect SOURCE_DA...
238
  	time_t fallback);
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
239
240
241
  /*
   * There is a c file associated with supported image type low level code
   * for ex. default_image.c, fit_image.c
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
242
   */
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
243

f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
244
245
  
  void pbl_load_uboot(int fd, struct image_tool_params *mparams);
6915dcf35   Alexander Graf   tools: zynqmpimag...
246
  int zynqmpbif_copy_image(int fd, struct image_tool_params *mparams);
a2b96ece5   Peng Fan   tools: add i.MX8/...
247
  int imx8image_copy_image(int fd, struct image_tool_params *mparams);
6609c2663   Peng Fan   tools: add i.MX8M...
248
  int imx8mimage_copy_image(int fd, struct image_tool_params *mparams);
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
249

1fddd7b63   Andreas Bießmann   tools/imagetool: ...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  #define ___cat(a, b) a ## b
  #define __cat(a, b) ___cat(a, b)
  
  /* we need some special handling for this host tool running eventually on
   * Darwin. The Mach-O section handling is a bit different than ELF section
   * handling. The differnces in detail are:
   *  a) we have segments which have sections
   *  b) we need a API call to get the respective section symbols */
  #if defined(__MACH__)
  #include <mach-o/getsect.h>
  
  #define INIT_SECTION(name)  do {					\
  		unsigned long name ## _len;				\
  		char *__cat(pstart_, name) = getsectdata("__TEXT",	\
  			#name, &__cat(name, _len));			\
  		char *__cat(pstop_, name) = __cat(pstart_, name) +	\
  			__cat(name, _len);				\
  		__cat(__start_, name) = (void *)__cat(pstart_, name);	\
  		__cat(__stop_, name) = (void *)__cat(pstop_, name);	\
  	} while (0)
  #define SECTION(name)   __attribute__((section("__TEXT, " #name)))
  
  struct image_type_params **__start_image_type, **__stop_image_type;
  #else
  #define INIT_SECTION(name) /* no-op for ELF */
  #define SECTION(name)   __attribute__((section(#name)))
  
  /* We construct a table of pointers in an ELF section (pointers generally
   * go unpadded by gcc).  ld creates boundary syms for us. */
  extern struct image_type_params *__start_image_type[], *__stop_image_type[];
  #endif /* __MACH__ */
  
  #if !defined(__used)
  # if __GNUC__ == 3 && __GNUC_MINOR__ < 3
  #  define __used			__attribute__((__unused__))
  # else
  #  define __used			__attribute__((__used__))
  # endif
  #endif
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
289
290
291
292
293
294
295
296
297
  #define U_BOOT_IMAGE_TYPE( \
  		_id, \
  		_name, \
  		_header_size, \
  		_header, \
  		_check_params, \
  		_verify_header, \
  		_print_header, \
  		_set_header, \
67f946cd1   Guilherme Maciel Ferreira   dumpimage: replac...
298
  		_extract_subimage, \
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
299
300
301
302
  		_check_image_type, \
  		_fflag_handle, \
  		_vrec_header \
  	) \
1fddd7b63   Andreas Bießmann   tools/imagetool: ...
303
304
  	static struct image_type_params __cat(image_type_, _id) = \
  	{ \
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
305
306
307
308
309
310
311
  		.name = _name, \
  		.header_size = _header_size, \
  		.hdr = _header, \
  		.check_params = _check_params, \
  		.verify_header = _verify_header, \
  		.print_header = _print_header, \
  		.set_header = _set_header, \
67f946cd1   Guilherme Maciel Ferreira   dumpimage: replac...
312
  		.extract_subimage = _extract_subimage, \
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
313
314
315
  		.check_image_type = _check_image_type, \
  		.fflag_handle = _fflag_handle, \
  		.vrec_header = _vrec_header \
1fddd7b63   Andreas Bießmann   tools/imagetool: ...
316
317
318
  	}; \
  	static struct image_type_params *SECTION(image_type) __used \
  		__cat(image_type_ptr_, _id) = &__cat(image_type_, _id)
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
319

f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
320
  #endif /* _IMAGETOOL_H_ */