tools: mkimage: Show where signatures/keys are written
[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 <image.h>
25
26 #include "fdt_host.h"
27
28 #define ARRAY_SIZE(x)           (sizeof(x) / sizeof((x)[0]))
29
30 #define __ALIGN_MASK(x, mask)   (((x) + (mask)) & ~(mask))
31 #define ALIGN(x, a)             __ALIGN_MASK((x), (typeof(x))(a) - 1)
32
33 #define IH_ARCH_DEFAULT         IH_ARCH_INVALID
34
35 /* Information about a file that needs to be placed into the FIT */
36 struct content_info {
37         struct content_info *next;
38         int type;               /* File type (IH_TYPE_...) */
39         const char *fname;
40 };
41
42 /*
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
46  */
47 struct image_tool_params {
48         int dflag;
49         int eflag;
50         int fflag;
51         int iflag;
52         int lflag;
53         int pflag;
54         int vflag;
55         int xflag;
56         int skipcpy;
57         int os;
58         int arch;
59         int type;
60         int comp;
61         char *dtc;
62         unsigned int addr;
63         unsigned int ep;
64         char *imagename;
65         char *imagename2;
66         char *datafile;
67         char *imagefile;
68         char *cmdname;
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 */
90 };
91
92 /*
93  * image type specific variables and callback functions
94  */
95 struct image_type_params {
96         /* name is an identification tag string for added support */
97         char *name;
98         /*
99          * header size is local to the specific image type to be supported,
100          * mkimage core treats this as number of bytes
101          */
102         uint32_t header_size;
103         /* Image type header pointer */
104         void *hdr;
105         /*
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
110          *
111          * Returns 1 if parameter check is successful
112          */
113         int (*check_params) (struct image_tool_params *);
114         /*
115          * This function is used by list command (i.e. mkimage -l <filename>)
116          * image type verification code must be put here
117          *
118          * Returns 0 if image header verification is successful
119          * otherwise, returns respective negative error codes
120          */
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 *);
124         /*
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.
129          */
130         void (*set_header) (void *, struct stat *, int,
131                                         struct image_tool_params *);
132         /*
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.
137          *
138          * Returns 0 if the file was successfully retrieved from the image,
139          * or a negative value on error.
140          */
141         int (*extract_subimage)(void *, struct image_tool_params *);
142         /*
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
147          */
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 *);
151         /*
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.
158          */
159         int (*vrec_header) (struct image_tool_params *,
160                 struct image_type_params *);
161 };
162
163 /**
164  * imagetool_get_type() - find the image type params for a given image type
165  *
166  * It scans all registers image type supports
167  * checks the input type for each supported image type
168  *
169  * if successful,
170  *     returns respective image_type_params pointer if success
171  * if input type_id is not supported by any of image_type_support
172  *     returns NULL
173  */
174 struct image_type_params *imagetool_get_type(int type);
175
176 /*
177  * imagetool_verify_print_header() - verifies the image header
178  *
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.
182  *
183  * Return: 0 on success, negative if input image format does not match with
184  * any of supported image types
185  */
186 int imagetool_verify_print_header(
187         void *ptr,
188         struct stat *sbuf,
189         struct image_type_params *tparams,
190         struct image_tool_params *params);
191
192 /*
193  * imagetool_verify_print_header_by_type() - verifies the image header
194  *
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
201  *
202  * Return: 0 on success, negative if input image format does not match with
203  * the given image type
204  */
205 int imagetool_verify_print_header_by_type(
206         void *ptr,
207         struct stat *sbuf,
208         struct image_type_params *tparams,
209         struct image_tool_params *params);
210
211 /**
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
216  *
217  * imagetool_save_subimage() store file_len bytes of data pointed by file_data
218  * into the file name by file_name.
219  *
220  * returns:
221  *     zero in case of success or a negative value if fail.
222  */
223 int imagetool_save_subimage(
224         const char *file_name,
225         ulong file_data,
226         ulong file_len);
227
228 /**
229  * imagetool_get_filesize() - Utility function to obtain the size of a file
230  *
231  * This function prints a message if an error occurs, showing the error that
232  * was obtained.
233  *
234  * @params:     mkimage parameters
235  * @fname:      filename to check
236  * Return: size of file, or -ve value on error
237  */
238 int imagetool_get_filesize(struct image_tool_params *params, const char *fname);
239
240 /**
241  * imagetool_get_source_date() - Get timestamp for build output.
242  *
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
246  * 0.
247  *
248  * @cmdname:    command name
249  * @fallback:   timestamp to use if SOURCE_DATE_EPOCH isn't set
250  * Return: timestamp based on SOURCE_DATE_EPOCH
251  */
252 time_t imagetool_get_source_date(
253         const char *cmdname,
254         time_t fallback);
255
256 /*
257  * There is a c file associated with supported image type low level code
258  * for ex. default_image.c, fit_image.c
259  */
260
261
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);
267
268 #define ___cat(a, b) a ## b
269 #define __cat(a, b) ___cat(a, b)
270
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
279 #define INIT_SECTION(name)  do {                                        \
280                 unsigned long name ## _len;                             \
281                 char *__cat(pstart_, name) = getsectdata("__DATA",      \
282                         #name, &__cat(name, _len));                     \
283                 char *__cat(pstop_, name) = __cat(pstart_, name) +      \
284                         __cat(name, _len);                              \
285                 __cat(__start_, name) = (void *)__cat(pstart_, name);   \
286                 __cat(__stop_, name) = (void *)__cat(pstop_, name);     \
287         } while (0)
288 #define SECTION(name)   __attribute__((section("__DATA, " #name)))
289
290 struct image_type_params **__start_image_type, **__stop_image_type;
291 #else
292 #define INIT_SECTION(name) /* no-op for ELF */
293 #define SECTION(name)   __attribute__((section(#name)))
294
295 /* We construct a table of pointers in an ELF section (pointers generally
296  * go unpadded by gcc).  ld creates boundary syms for us. */
297 extern struct image_type_params *__start_image_type[], *__stop_image_type[];
298 #endif /* __MACH__ */
299
300 #if !defined(__used)
301 # if __GNUC__ == 3 && __GNUC_MINOR__ < 3
302 #  define __used                        __attribute__((__unused__))
303 # else
304 #  define __used                        __attribute__((__used__))
305 # endif
306 #endif
307
308 #define U_BOOT_IMAGE_TYPE( \
309                 _id, \
310                 _name, \
311                 _header_size, \
312                 _header, \
313                 _check_params, \
314                 _verify_header, \
315                 _print_header, \
316                 _set_header, \
317                 _extract_subimage, \
318                 _check_image_type, \
319                 _fflag_handle, \
320                 _vrec_header \
321         ) \
322         static struct image_type_params __cat(image_type_, _id) = \
323         { \
324                 .name = _name, \
325                 .header_size = _header_size, \
326                 .hdr = _header, \
327                 .check_params = _check_params, \
328                 .verify_header = _verify_header, \
329                 .print_header = _print_header, \
330                 .set_header = _set_header, \
331                 .extract_subimage = _extract_subimage, \
332                 .check_image_type = _check_image_type, \
333                 .fflag_handle = _fflag_handle, \
334                 .vrec_header = _vrec_header \
335         }; \
336         static struct image_type_params *SECTION(image_type) __used \
337                 __cat(image_type_ptr_, _id) = &__cat(image_type_, _id)
338
339 #endif /* _IMAGETOOL_H_ */