xref: /rk3399_rockchip-uboot/tools/imagetool.h (revision a93648d197df48fa46dd55f925ff70468bd81c71)
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 
23 /* define __KERNEL__ in order to get the definitions
24  * required by the linker list. This is probably not
25  * the best way to do this */
26 #ifndef __KERNEL__
27 #define __KERNEL__
28 #include <linker_lists.h>
29 #undef __KERNEL__
30 #endif /* __KERNEL__ */
31 
32 #include "fdt_host.h"
33 
34 #define ARRAY_SIZE(x)		(sizeof(x) / sizeof((x)[0]))
35 
36 #define IH_ARCH_DEFAULT		IH_ARCH_INVALID
37 
38 /*
39  * This structure defines all such variables those are initialized by
40  * mkimage and dumpimage main core and need to be referred by image
41  * type specific functions
42  */
43 struct image_tool_params {
44 	int dflag;
45 	int eflag;
46 	int fflag;
47 	int iflag;
48 	int lflag;
49 	int pflag;
50 	int vflag;
51 	int xflag;
52 	int skipcpy;
53 	int os;
54 	int arch;
55 	int type;
56 	int comp;
57 	char *dtc;
58 	unsigned int addr;
59 	unsigned int ep;
60 	char *imagename;
61 	char *imagename2;
62 	char *datafile;
63 	char *imagefile;
64 	char *cmdname;
65 	const char *outfile;	/* Output filename */
66 	const char *keydir;	/* Directory holding private keys */
67 	const char *keydest;	/* Destination .dtb for public key */
68 	const char *comment;	/* Comment to add to signature node */
69 	int require_keys;	/* 1 to mark signing keys as 'required' */
70 };
71 
72 /*
73  * image type specific variables and callback functions
74  */
75 struct image_type_params {
76 	/* name is an identification tag string for added support */
77 	char *name;
78 	/*
79 	 * header size is local to the specific image type to be supported,
80 	 * mkimage core treats this as number of bytes
81 	 */
82 	uint32_t header_size;
83 	/* Image type header pointer */
84 	void *hdr;
85 	/*
86 	 * There are several arguments that are passed on the command line
87 	 * and are registered as flags in image_tool_params structure.
88 	 * This callback function can be used to check the passed arguments
89 	 * are in-lined with the image type to be supported
90 	 *
91 	 * Returns 1 if parameter check is successful
92 	 */
93 	int (*check_params) (struct image_tool_params *);
94 	/*
95 	 * This function is used by list command (i.e. mkimage -l <filename>)
96 	 * image type verification code must be put here
97 	 *
98 	 * Returns 0 if image header verification is successful
99 	 * otherwise, returns respective negative error codes
100 	 */
101 	int (*verify_header) (unsigned char *, int, struct image_tool_params *);
102 	/* Prints image information abstracting from image header */
103 	void (*print_header) (const void *);
104 	/*
105 	 * The header or image contents need to be set as per image type to
106 	 * be generated using this callback function.
107 	 * further output file post processing (for ex. checksum calculation,
108 	 * padding bytes etc..) can also be done in this callback function.
109 	 */
110 	void (*set_header) (void *, struct stat *, int,
111 					struct image_tool_params *);
112 	/*
113 	 * This function is used by the command to retrieve a data file from
114 	 * the image (i.e. dumpimage -i <image> -p <position> <data_file>).
115 	 * Thus the code to extract a file from an image must be put here.
116 	 *
117 	 * Returns 0 if the file was successfully retrieved from the image,
118 	 * or a negative value on error.
119 	 */
120 	int (*extract_datafile) (void *, struct image_tool_params *);
121 	/*
122 	 * Some image generation support for ex (default image type) supports
123 	 * more than one type_ids, this callback function is used to check
124 	 * whether input (-T <image_type>) is supported by registered image
125 	 * generation/list low level code
126 	 */
127 	int (*check_image_type) (uint8_t);
128 	/* This callback function will be executed if fflag is defined */
129 	int (*fflag_handle) (struct image_tool_params *);
130 	/*
131 	 * This callback function will be executed for variable size record
132 	 * It is expected to build this header in memory and return its length
133 	 * and a pointer to it by using image_type_params.header_size and
134 	 * image_type_params.hdr. The return value shall indicate if an
135 	 * additional padding should be used when copying the data image
136 	 * by returning the padding length.
137 	 */
138 	int (*vrec_header) (struct image_tool_params *,
139 		struct image_type_params *);
140 };
141 
142 /**
143  * imagetool_get_type() - find the image type params for a given image type
144  *
145  * It scans all registers image type supports
146  * checks the input type for each supported image type
147  *
148  * if successful,
149  *     returns respective image_type_params pointer if success
150  * if input type_id is not supported by any of image_type_support
151  *     returns NULL
152  */
153 struct image_type_params *imagetool_get_type(int type);
154 
155 /*
156  * imagetool_verify_print_header() - verifies the image header
157  *
158  * Scan registered image types and verify the image_header for each
159  * supported image type. If verification is successful, this prints
160  * the respective header.
161  *
162  * @return 0 on success, negative if input image format does not match with
163  * any of supported image types
164  */
165 int imagetool_verify_print_header(
166 	void *ptr,
167 	struct stat *sbuf,
168 	struct image_type_params *tparams,
169 	struct image_tool_params *params);
170 
171 /**
172  * imagetool_save_datafile - store data into a file
173  * @file_name: name of the destination file
174  * @file_data: data to be written
175  * @file_len: the amount of data to store
176  *
177  * imagetool_save_datafile() store file_len bytes of data pointed by file_data
178  * into the file name by file_name.
179  *
180  * returns:
181  *     zero in case of success or a negative value if fail.
182  */
183 int imagetool_save_datafile(
184 	const char *file_name,
185 	ulong file_data,
186 	ulong file_len);
187 
188 /*
189  * There is a c file associated with supported image type low level code
190  * for ex. default_image.c, fit_image.c
191  */
192 
193 
194 void pbl_load_uboot(int fd, struct image_tool_params *mparams);
195 
196 #define U_BOOT_IMAGE_TYPE( \
197 		_id, \
198 		_name, \
199 		_header_size, \
200 		_header, \
201 		_check_params, \
202 		_verify_header, \
203 		_print_header, \
204 		_set_header, \
205 		_extract_datafile, \
206 		_check_image_type, \
207 		_fflag_handle, \
208 		_vrec_header \
209 	) \
210 	ll_entry_declare(struct image_type_params, _id, image_type) = { \
211 		.name = _name, \
212 		.header_size = _header_size, \
213 		.hdr = _header, \
214 		.check_params = _check_params, \
215 		.verify_header = _verify_header, \
216 		.print_header = _print_header, \
217 		.set_header = _set_header, \
218 		.extract_datafile = _extract_datafile, \
219 		.check_image_type = _check_image_type, \
220 		.fflag_handle = _fflag_handle, \
221 		.vrec_header = _vrec_header \
222 	}
223 
224 #endif /* _IMAGETOOL_H_ */
225