blob: 44d9380995b9a28e5b99c85cc3a66d8735f12468 [file] [log] [blame]
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -07001/*
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>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020021#include <u-boot/sha1.h>
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070022#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 */
33struct image_tool_params {
34 int dflag;
35 int eflag;
36 int fflag;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070037 int iflag;
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070038 int lflag;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070039 int pflag;
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070040 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;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070055 const char *outfile; /* Output filename */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070056 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 */
65struct 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 /*
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700103 * 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 /*
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700112 * 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 */
137typedef 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 */
144void 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 */
151void register_image_type(struct image_type_params *tparams);
152
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -0200153/**
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 */
164struct 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 */
178int imagetool_verify_print_header(
179 void *ptr,
180 struct stat *sbuf,
181 struct image_type_params *tparams,
182 struct image_tool_params *params);
183
Guilherme Maciel Ferreirafc3c9672015-01-15 02:48:06 -0200184/**
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 */
196int imagetool_save_datafile(
197 const char *file_name,
198 ulong file_data,
199 ulong file_len);
200
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700201/*
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 */
209void init_default_image_type(void);
Andreas Bießmann7c75d3e2014-05-19 14:23:39 +0200210void init_atmel_image_type(void);
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700211void init_pbl_image_type(void);
212void init_ais_image_type(void);
213void init_kwb_image_type(void);
214void init_imx_image_type(void);
215void init_mxs_image_type(void);
216void init_fit_image_type(void);
217void init_ubl_image_type(void);
218void init_omap_image_type(void);
Charles Manning685e87f2014-03-06 15:40:50 +1300219void init_socfpga_image_type(void);
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400220void init_gpimage_type(void);
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700221
222void pbl_load_uboot(int fd, struct image_tool_params *mparams);
223
224#endif /* _IMAGETOOL_H_ */