1 /* SPDX-License-Identifier: GPL-2.0+ */
5 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
11 #include "os_support.h"
19 #include <sys/types.h>
22 #include <u-boot/sha1.h>
28 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
30 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
31 #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
33 #define IH_ARCH_DEFAULT IH_ARCH_INVALID
35 /* Information about a file that needs to be placed into the FIT */
37 struct content_info *next;
38 int type; /* File type (IH_TYPE_...) */
43 * This structure defines all such variables those are initialized by
44 * mkimage and dumpimage main core and need to be referred by image
45 * type specific functions
47 struct image_tool_params {
69 const char *outfile; /* Output filename */
70 const char *keydir; /* Directory holding private keys */
71 const char *keydest; /* Destination .dtb for public key */
72 const char *keyfile; /* Filename of private or public key */
73 const char *comment; /* Comment to add to signature node */
74 const char *algo_name; /* Algorithm name to use hashing/signing */
75 int require_keys; /* 1 to mark signing keys as 'required' */
76 int file_size; /* Total size of output file */
77 int orig_file_size; /* Original size for file before padding */
78 bool auto_its; /* Automatically create the .its file */
79 int fit_image_type; /* Image type to put into the FIT */
80 char *fit_ramdisk; /* Ramdisk file to include */
81 struct content_info *content_head; /* List of files to include */
82 struct content_info *content_tail;
83 bool external_data; /* Store data outside the FIT */
84 bool quiet; /* Don't output text in normal operation */
85 unsigned int external_offset; /* Add padding to external data */
86 int bl_len; /* Block length in byte for external data */
87 const char *engine_id; /* Engine to use for signing */
88 bool reset_timestamp; /* Reset the timestamp on an existing image */
89 struct image_summary summary; /* results of signing process */
93 * image type specific variables and callback functions
95 struct image_type_params {
96 /* name is an identification tag string for added support */
99 * header size is local to the specific image type to be supported,
100 * mkimage core treats this as number of bytes
102 uint32_t header_size;
103 /* Image type header pointer */
106 * There are several arguments that are passed on the command line
107 * and are registered as flags in image_tool_params structure.
108 * This callback function can be used to check the passed arguments
109 * are in-lined with the image type to be supported
111 * Returns 1 if parameter check is successful
113 int (*check_params) (struct image_tool_params *);
115 * This function is used by list command (i.e. mkimage -l <filename>)
116 * image type verification code must be put here
118 * Returns 0 if image header verification is successful
119 * otherwise, returns respective negative error codes
121 int (*verify_header) (unsigned char *, int, struct image_tool_params *);
122 /* Prints image information abstracting from image header */
123 void (*print_header) (const void *);
125 * The header or image contents need to be set as per image type to
126 * be generated using this callback function.
127 * further output file post processing (for ex. checksum calculation,
128 * padding bytes etc..) can also be done in this callback function.
130 void (*set_header) (void *, struct stat *, int,
131 struct image_tool_params *);
133 * This function is used by the command to retrieve a component
134 * (sub-image) from the image (i.e. dumpimage -p <position>
135 * -o <component-outfile> <image>). Thus the code to extract a file
136 * from an image must be put here.
138 * Returns 0 if the file was successfully retrieved from the image,
139 * or a negative value on error.
141 int (*extract_subimage)(void *, struct image_tool_params *);
143 * Some image generation support for ex (default image type) supports
144 * more than one type_ids, this callback function is used to check
145 * whether input (-T <image_type>) is supported by registered image
146 * generation/list low level code
148 int (*check_image_type) (uint8_t);
149 /* This callback function will be executed if fflag is defined */
150 int (*fflag_handle) (struct image_tool_params *);
152 * This callback function will be executed for variable size record
153 * It is expected to build this header in memory and return its length
154 * and a pointer to it by using image_type_params.header_size and
155 * image_type_params.hdr. The return value shall indicate if an
156 * additional padding should be used when copying the data image
157 * by returning the padding length.
159 int (*vrec_header) (struct image_tool_params *,
160 struct image_type_params *);
164 * imagetool_get_type() - find the image type params for a given image type
166 * It scans all registers image type supports
167 * checks the input type for each supported image type
170 * returns respective image_type_params pointer if success
171 * if input type_id is not supported by any of image_type_support
174 struct image_type_params *imagetool_get_type(int type);
177 * imagetool_verify_print_header() - verifies the image header
179 * Scan registered image types and verify the image_header for each
180 * supported image type. If verification is successful, this prints
181 * the respective header.
183 * Return: 0 on success, negative if input image format does not match with
184 * any of supported image types
186 int imagetool_verify_print_header(
189 struct image_type_params *tparams,
190 struct image_tool_params *params);
193 * imagetool_verify_print_header_by_type() - verifies the image header
195 * Verify the image_header for the image type given by tparams.
196 * If verification is successful, this prints the respective header.
197 * @ptr: pointer the the image header
198 * @sbuf: stat information about the file pointed to by ptr
199 * @tparams: image type parameters
200 * @params: mkimage parameters
202 * Return: 0 on success, negative if input image format does not match with
203 * the given image type
205 int imagetool_verify_print_header_by_type(
208 struct image_type_params *tparams,
209 struct image_tool_params *params);
212 * imagetool_save_subimage - store data into a file
213 * @file_name: name of the destination file
214 * @file_data: data to be written
215 * @file_len: the amount of data to store
217 * imagetool_save_subimage() store file_len bytes of data pointed by file_data
218 * into the file name by file_name.
221 * zero in case of success or a negative value if fail.
223 int imagetool_save_subimage(
224 const char *file_name,
229 * imagetool_get_filesize() - Utility function to obtain the size of a file
231 * This function prints a message if an error occurs, showing the error that
234 * @params: mkimage parameters
235 * @fname: filename to check
236 * Return: size of file, or -ve value on error
238 int imagetool_get_filesize(struct image_tool_params *params, const char *fname);
241 * imagetool_get_source_date() - Get timestamp for build output.
243 * Gets a timestamp for embedding it in a build output. If set
244 * SOURCE_DATE_EPOCH is used. Else the given fallback value is returned. Prints
245 * an error message if SOURCE_DATE_EPOCH contains an invalid value and returns
248 * @cmdname: command name
249 * @fallback: timestamp to use if SOURCE_DATE_EPOCH isn't set
250 * Return: timestamp based on SOURCE_DATE_EPOCH
252 time_t imagetool_get_source_date(
257 * There is a c file associated with supported image type low level code
258 * for ex. default_image.c, fit_image.c
262 void pbl_load_uboot(int fd, struct image_tool_params *mparams);
263 int zynqmpbif_copy_image(int fd, struct image_tool_params *mparams);
264 int imx8image_copy_image(int fd, struct image_tool_params *mparams);
265 int imx8mimage_copy_image(int fd, struct image_tool_params *mparams);
266 int rockchip_copy_image(int fd, struct image_tool_params *mparams);
268 #define ___cat(a, b) a ## b
269 #define __cat(a, b) ___cat(a, b)
271 /* we need some special handling for this host tool running eventually on
272 * Darwin. The Mach-O section handling is a bit different than ELF section
273 * handling. The differnces in detail are:
274 * a) we have segments which have sections
275 * b) we need a API call to get the respective section symbols */
276 #if defined(__MACH__)
277 #include <mach-o/getsect.h>
278 #include <mach-o/dyld.h>
280 #define INIT_SECTION(name) do { \
281 unsigned long name ## _len; \
282 char *__cat(pstart_, name) = getsectdata("__DATA", \
283 #name, &__cat(name, _len)); \
284 __cat(pstart_, name) += _dyld_get_image_vmaddr_slide(0);\
285 char *__cat(pstop_, name) = __cat(pstart_, name) + \
287 __cat(__start_, name) = (void *)__cat(pstart_, name); \
288 __cat(__stop_, name) = (void *)__cat(pstop_, name); \
290 #define SECTION(name) __attribute__((section("__DATA, " #name)))
292 struct image_type_params **__start_image_type, **__stop_image_type;
294 #define INIT_SECTION(name) /* no-op for ELF */
295 #define SECTION(name) __attribute__((section(#name)))
297 /* We construct a table of pointers in an ELF section (pointers generally
298 * go unpadded by gcc). ld creates boundary syms for us. */
299 extern struct image_type_params *__start_image_type[], *__stop_image_type[];
300 #endif /* __MACH__ */
303 # if __GNUC__ == 3 && __GNUC_MINOR__ < 3
304 # define __used __attribute__((__unused__))
306 # define __used __attribute__((__used__))
310 #define U_BOOT_IMAGE_TYPE( \
324 static struct image_type_params __cat(image_type_, _id) = \
327 .header_size = _header_size, \
329 .check_params = _check_params, \
330 .verify_header = _verify_header, \
331 .print_header = _print_header, \
332 .set_header = _set_header, \
333 .extract_subimage = _extract_subimage, \
334 .check_image_type = _check_image_type, \
335 .fflag_handle = _fflag_handle, \
336 .vrec_header = _vrec_header \
338 static struct image_type_params *SECTION(image_type) __used \
339 __cat(image_type_ptr_, _id) = &__cat(image_type_, _id)
341 #endif /* _IMAGETOOL_H_ */