]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - tools/imagetool.h
imagetool: make the image_save_datafile() available to all image types
[karo-tx-uboot.git] / tools / imagetool.h
1 /*
2  * (C) Copyright 2013
3  *
4  * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #ifndef _IMAGETOOL_H_
10 #define _IMAGETOOL_H_
11
12 #include "os_support.h"
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include <u-boot/sha1.h>
22 #include "fdt_host.h"
23
24 #define ARRAY_SIZE(x)           (sizeof(x) / sizeof((x)[0]))
25
26 #define IH_ARCH_DEFAULT         IH_ARCH_INVALID
27
28 /*
29  * This structure defines all such variables those are initialized by
30  * mkimage and dumpimage main core and need to be referred by image
31  * type specific functions
32  */
33 struct image_tool_params {
34         int dflag;
35         int eflag;
36         int fflag;
37         int iflag;
38         int lflag;
39         int pflag;
40         int vflag;
41         int xflag;
42         int skipcpy;
43         int os;
44         int arch;
45         int type;
46         int comp;
47         char *dtc;
48         unsigned int addr;
49         unsigned int ep;
50         char *imagename;
51         char *imagename2;
52         char *datafile;
53         char *imagefile;
54         char *cmdname;
55         const char *outfile;    /* Output filename */
56         const char *keydir;     /* Directory holding private keys */
57         const char *keydest;    /* Destination .dtb for public key */
58         const char *comment;    /* Comment to add to signature node */
59         int require_keys;       /* 1 to mark signing keys as 'required' */
60 };
61
62 /*
63  * image type specific variables and callback functions
64  */
65 struct image_type_params {
66         /* name is an identification tag string for added support */
67         char *name;
68         /*
69          * header size is local to the specific image type to be supported,
70          * mkimage core treats this as number of bytes
71          */
72         uint32_t header_size;
73         /* Image type header pointer */
74         void *hdr;
75         /*
76          * There are several arguments that are passed on the command line
77          * and are registered as flags in image_tool_params structure.
78          * This callback function can be used to check the passed arguments
79          * are in-lined with the image type to be supported
80          *
81          * Returns 1 if parameter check is successful
82          */
83         int (*check_params) (struct image_tool_params *);
84         /*
85          * This function is used by list command (i.e. mkimage -l <filename>)
86          * image type verification code must be put here
87          *
88          * Returns 0 if image header verification is successful
89          * otherwise, returns respective negative error codes
90          */
91         int (*verify_header) (unsigned char *, int, struct image_tool_params *);
92         /* Prints image information abstracting from image header */
93         void (*print_header) (const void *);
94         /*
95          * The header or image contents need to be set as per image type to
96          * be generated using this callback function.
97          * further output file post processing (for ex. checksum calculation,
98          * padding bytes etc..) can also be done in this callback function.
99          */
100         void (*set_header) (void *, struct stat *, int,
101                                         struct image_tool_params *);
102         /*
103          * This function is used by the command to retrieve a data file from
104          * the image (i.e. dumpimage -i <image> -p <position> <data_file>).
105          * Thus the code to extract a file from an image must be put here.
106          *
107          * Returns 0 if the file was successfully retrieved from the image,
108          * or a negative value on error.
109          */
110         int (*extract_datafile) (void *, struct image_tool_params *);
111         /*
112          * Some image generation support for ex (default image type) supports
113          * more than one type_ids, this callback function is used to check
114          * whether input (-T <image_type>) is supported by registered image
115          * generation/list low level code
116          */
117         int (*check_image_type) (uint8_t);
118         /* This callback function will be executed if fflag is defined */
119         int (*fflag_handle) (struct image_tool_params *);
120         /*
121          * This callback function will be executed for variable size record
122          * It is expected to build this header in memory and return its length
123          * and a pointer to it by using image_type_params.header_size and
124          * image_type_params.hdr. The return value shall indicate if an
125          * additional padding should be used when copying the data image
126          * by returning the padding length.
127          */
128         int (*vrec_header) (struct image_tool_params *,
129                 struct image_type_params *);
130         /* pointer to the next registered entry in linked list */
131         struct image_type_params *next;
132 };
133
134 /*
135  * Tool registration function.
136  */
137 typedef void (*imagetool_register_t)(struct image_type_params *);
138
139 /*
140  * Initializes all image types with the given registration callback
141  * function.
142  * An image tool uses this function to initialize all image types.
143  */
144 void register_image_tool(imagetool_register_t image_register);
145
146 /*
147  * Register a image type within a tool.
148  * An image type uses this function to register itself within
149  * all tools.
150  */
151 void register_image_type(struct image_type_params *tparams);
152
153 /**
154  * imagetool_get_type() - find the image type params for a given image type
155  *
156  * It scans all registers image type supports
157  * checks the input type for each supported image type
158  *
159  * if successful,
160  *     returns respective image_type_params pointer if success
161  * if input type_id is not supported by any of image_type_support
162  *     returns NULL
163  */
164 struct image_type_params *imagetool_get_type(
165         int type,
166         struct image_type_params *tparams);
167
168 /*
169  * imagetool_verify_print_header() - verifies the image header
170  *
171  * Scan registered image types and verify the image_header for each
172  * supported image type. If verification is successful, this prints
173  * the respective header.
174  *
175  * @return 0 on success, negative if input image format does not match with
176  * any of supported image types
177  */
178 int imagetool_verify_print_header(
179         void *ptr,
180         struct stat *sbuf,
181         struct image_type_params *tparams,
182         struct image_tool_params *params);
183
184 /**
185  * imagetool_save_datafile - store data into a file
186  * @file_name: name of the destination file
187  * @file_data: data to be written
188  * @file_len: the amount of data to store
189  *
190  * imagetool_save_datafile() store file_len bytes of data pointed by file_data
191  * into the file name by file_name.
192  *
193  * returns:
194  *     zero in case of success or a negative value if fail.
195  */
196 int imagetool_save_datafile(
197         const char *file_name,
198         ulong file_data,
199         ulong file_len);
200
201 /*
202  * There is a c file associated with supported image type low level code
203  * for ex. default_image.c, fit_image.c
204  * init_xxx_type() is the only function referred by image tool core to avoid
205  * a single lined header file, you can define them here
206  *
207  * Supported image types init functions
208  */
209 void init_default_image_type(void);
210 void init_atmel_image_type(void);
211 void init_pbl_image_type(void);
212 void init_ais_image_type(void);
213 void init_kwb_image_type(void);
214 void init_imx_image_type(void);
215 void init_mxs_image_type(void);
216 void init_fit_image_type(void);
217 void init_ubl_image_type(void);
218 void init_omap_image_type(void);
219 void init_socfpga_image_type(void);
220 void init_gpimage_type(void);
221
222 void pbl_load_uboot(int fd, struct image_tool_params *mparams);
223
224 #endif /* _IMAGETOOL_H_ */