Merge branch '2022-01-24-assorted-fixes-and-updates'
[platform/kernel/u-boot.git] / tools / imagetool.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2013
4  *
5  * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
6  */
7
8 #ifndef _IMAGETOOL_H_
9 #define _IMAGETOOL_H_
10
11 #include "os_support.h"
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <u-boot/sha1.h>
23
24 #include "fdt_host.h"
25
26 #define ARRAY_SIZE(x)           (sizeof(x) / sizeof((x)[0]))
27
28 #define __ALIGN_MASK(x, mask)   (((x) + (mask)) & ~(mask))
29 #define ALIGN(x, a)             __ALIGN_MASK((x), (typeof(x))(a) - 1)
30
31 #define IH_ARCH_DEFAULT         IH_ARCH_INVALID
32
33 /* Information about a file that needs to be placed into the FIT */
34 struct content_info {
35         struct content_info *next;
36         int type;               /* File type (IH_TYPE_...) */
37         const char *fname;
38 };
39
40 /*
41  * This structure defines all such variables those are initialized by
42  * mkimage and dumpimage main core and need to be referred by image
43  * type specific functions
44  */
45 struct image_tool_params {
46         int dflag;
47         int eflag;
48         int fflag;
49         int iflag;
50         int lflag;
51         int pflag;
52         int vflag;
53         int xflag;
54         int skipcpy;
55         int os;
56         int arch;
57         int type;
58         int comp;
59         char *dtc;
60         unsigned int addr;
61         unsigned int ep;
62         char *imagename;
63         char *imagename2;
64         char *datafile;
65         char *imagefile;
66         char *cmdname;
67         const char *outfile;    /* Output filename */
68         const char *keydir;     /* Directory holding private keys */
69         const char *keydest;    /* Destination .dtb for public key */
70         const char *keyfile;    /* Filename of private or public key */
71         const char *comment;    /* Comment to add to signature node */
72         const char *algo_name;  /* Algorithm name to use hashing/signing */
73         int require_keys;       /* 1 to mark signing keys as 'required' */
74         int file_size;          /* Total size of output file */
75         int orig_file_size;     /* Original size for file before padding */
76         bool auto_its;          /* Automatically create the .its file */
77         int fit_image_type;     /* Image type to put into the FIT */
78         char *fit_ramdisk;      /* Ramdisk file to include */
79         struct content_info *content_head;      /* List of files to include */
80         struct content_info *content_tail;
81         bool external_data;     /* Store data outside the FIT */
82         bool quiet;             /* Don't output text in normal operation */
83         unsigned int external_offset;   /* Add padding to external data */
84         int bl_len;             /* Block length in byte for external data */
85         const char *engine_id;  /* Engine to use for signing */
86         bool reset_timestamp;   /* Reset the timestamp on an existing image */
87 };
88
89 /*
90  * image type specific variables and callback functions
91  */
92 struct image_type_params {
93         /* name is an identification tag string for added support */
94         char *name;
95         /*
96          * header size is local to the specific image type to be supported,
97          * mkimage core treats this as number of bytes
98          */
99         uint32_t header_size;
100         /* Image type header pointer */
101         void *hdr;
102         /*
103          * There are several arguments that are passed on the command line
104          * and are registered as flags in image_tool_params structure.
105          * This callback function can be used to check the passed arguments
106          * are in-lined with the image type to be supported
107          *
108          * Returns 1 if parameter check is successful
109          */
110         int (*check_params) (struct image_tool_params *);
111         /*
112          * This function is used by list command (i.e. mkimage -l <filename>)
113          * image type verification code must be put here
114          *
115          * Returns 0 if image header verification is successful
116          * otherwise, returns respective negative error codes
117          */
118         int (*verify_header) (unsigned char *, int, struct image_tool_params *);
119         /* Prints image information abstracting from image header */
120         void (*print_header) (const void *);
121         /*
122          * The header or image contents need to be set as per image type to
123          * be generated using this callback function.
124          * further output file post processing (for ex. checksum calculation,
125          * padding bytes etc..) can also be done in this callback function.
126          */
127         void (*set_header) (void *, struct stat *, int,
128                                         struct image_tool_params *);
129         /*
130          * This function is used by the command to retrieve a component
131          * (sub-image) from the image (i.e. dumpimage -p <position>
132          * -o <component-outfile> <image>). Thus the code to extract a file
133          * from an image must be put here.
134          *
135          * Returns 0 if the file was successfully retrieved from the image,
136          * or a negative value on error.
137          */
138         int (*extract_subimage)(void *, struct image_tool_params *);
139         /*
140          * Some image generation support for ex (default image type) supports
141          * more than one type_ids, this callback function is used to check
142          * whether input (-T <image_type>) is supported by registered image
143          * generation/list low level code
144          */
145         int (*check_image_type) (uint8_t);
146         /* This callback function will be executed if fflag is defined */
147         int (*fflag_handle) (struct image_tool_params *);
148         /*
149          * This callback function will be executed for variable size record
150          * It is expected to build this header in memory and return its length
151          * and a pointer to it by using image_type_params.header_size and
152          * image_type_params.hdr. The return value shall indicate if an
153          * additional padding should be used when copying the data image
154          * by returning the padding length.
155          */
156         int (*vrec_header) (struct image_tool_params *,
157                 struct image_type_params *);
158 };
159
160 /**
161  * imagetool_get_type() - find the image type params for a given image type
162  *
163  * It scans all registers image type supports
164  * checks the input type for each supported image type
165  *
166  * if successful,
167  *     returns respective image_type_params pointer if success
168  * if input type_id is not supported by any of image_type_support
169  *     returns NULL
170  */
171 struct image_type_params *imagetool_get_type(int type);
172
173 /*
174  * imagetool_verify_print_header() - verifies the image header
175  *
176  * Scan registered image types and verify the image_header for each
177  * supported image type. If verification is successful, this prints
178  * the respective header.
179  *
180  * Return: 0 on success, negative if input image format does not match with
181  * any of supported image types
182  */
183 int imagetool_verify_print_header(
184         void *ptr,
185         struct stat *sbuf,
186         struct image_type_params *tparams,
187         struct image_tool_params *params);
188
189 /*
190  * imagetool_verify_print_header_by_type() - verifies the image header
191  *
192  * Verify the image_header for the image type given by tparams.
193  * If verification is successful, this prints the respective header.
194  * @ptr: pointer the the image header
195  * @sbuf: stat information about the file pointed to by ptr
196  * @tparams: image type parameters
197  * @params: mkimage parameters
198  *
199  * Return: 0 on success, negative if input image format does not match with
200  * the given image type
201  */
202 int imagetool_verify_print_header_by_type(
203         void *ptr,
204         struct stat *sbuf,
205         struct image_type_params *tparams,
206         struct image_tool_params *params);
207
208 /**
209  * imagetool_save_subimage - store data into a file
210  * @file_name: name of the destination file
211  * @file_data: data to be written
212  * @file_len: the amount of data to store
213  *
214  * imagetool_save_subimage() store file_len bytes of data pointed by file_data
215  * into the file name by file_name.
216  *
217  * returns:
218  *     zero in case of success or a negative value if fail.
219  */
220 int imagetool_save_subimage(
221         const char *file_name,
222         ulong file_data,
223         ulong file_len);
224
225 /**
226  * imagetool_get_filesize() - Utility function to obtain the size of a file
227  *
228  * This function prints a message if an error occurs, showing the error that
229  * was obtained.
230  *
231  * @params:     mkimage parameters
232  * @fname:      filename to check
233  * Return: size of file, or -ve value on error
234  */
235 int imagetool_get_filesize(struct image_tool_params *params, const char *fname);
236
237 /**
238  * imagetool_get_source_date() - Get timestamp for build output.
239  *
240  * Gets a timestamp for embedding it in a build output. If set
241  * SOURCE_DATE_EPOCH is used. Else the given fallback value is returned. Prints
242  * an error message if SOURCE_DATE_EPOCH contains an invalid value and returns
243  * 0.
244  *
245  * @cmdname:    command name
246  * @fallback:   timestamp to use if SOURCE_DATE_EPOCH isn't set
247  * Return: timestamp based on SOURCE_DATE_EPOCH
248  */
249 time_t imagetool_get_source_date(
250         const char *cmdname,
251         time_t fallback);
252
253 /*
254  * There is a c file associated with supported image type low level code
255  * for ex. default_image.c, fit_image.c
256  */
257
258
259 void pbl_load_uboot(int fd, struct image_tool_params *mparams);
260 int zynqmpbif_copy_image(int fd, struct image_tool_params *mparams);
261 int imx8image_copy_image(int fd, struct image_tool_params *mparams);
262 int imx8mimage_copy_image(int fd, struct image_tool_params *mparams);
263 int rockchip_copy_image(int fd, struct image_tool_params *mparams);
264
265 #define ___cat(a, b) a ## b
266 #define __cat(a, b) ___cat(a, b)
267
268 /* we need some special handling for this host tool running eventually on
269  * Darwin. The Mach-O section handling is a bit different than ELF section
270  * handling. The differnces in detail are:
271  *  a) we have segments which have sections
272  *  b) we need a API call to get the respective section symbols */
273 #if defined(__MACH__)
274 #include <mach-o/getsect.h>
275
276 #define INIT_SECTION(name)  do {                                        \
277                 unsigned long name ## _len;                             \
278                 char *__cat(pstart_, name) = getsectdata("__DATA",      \
279                         #name, &__cat(name, _len));                     \
280                 char *__cat(pstop_, name) = __cat(pstart_, name) +      \
281                         __cat(name, _len);                              \
282                 __cat(__start_, name) = (void *)__cat(pstart_, name);   \
283                 __cat(__stop_, name) = (void *)__cat(pstop_, name);     \
284         } while (0)
285 #define SECTION(name)   __attribute__((section("__DATA, " #name)))
286
287 struct image_type_params **__start_image_type, **__stop_image_type;
288 #else
289 #define INIT_SECTION(name) /* no-op for ELF */
290 #define SECTION(name)   __attribute__((section(#name)))
291
292 /* We construct a table of pointers in an ELF section (pointers generally
293  * go unpadded by gcc).  ld creates boundary syms for us. */
294 extern struct image_type_params *__start_image_type[], *__stop_image_type[];
295 #endif /* __MACH__ */
296
297 #if !defined(__used)
298 # if __GNUC__ == 3 && __GNUC_MINOR__ < 3
299 #  define __used                        __attribute__((__unused__))
300 # else
301 #  define __used                        __attribute__((__used__))
302 # endif
303 #endif
304
305 #define U_BOOT_IMAGE_TYPE( \
306                 _id, \
307                 _name, \
308                 _header_size, \
309                 _header, \
310                 _check_params, \
311                 _verify_header, \
312                 _print_header, \
313                 _set_header, \
314                 _extract_subimage, \
315                 _check_image_type, \
316                 _fflag_handle, \
317                 _vrec_header \
318         ) \
319         static struct image_type_params __cat(image_type_, _id) = \
320         { \
321                 .name = _name, \
322                 .header_size = _header_size, \
323                 .hdr = _header, \
324                 .check_params = _check_params, \
325                 .verify_header = _verify_header, \
326                 .print_header = _print_header, \
327                 .set_header = _set_header, \
328                 .extract_subimage = _extract_subimage, \
329                 .check_image_type = _check_image_type, \
330                 .fflag_handle = _fflag_handle, \
331                 .vrec_header = _vrec_header \
332         }; \
333         static struct image_type_params *SECTION(image_type) __used \
334                 __cat(image_type_ptr_, _id) = &__cat(image_type_, _id)
335
336 #endif /* _IMAGETOOL_H_ */