Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 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> |
Jeroen Hofstee | 2b9912e | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 21 | #include <u-boot/sha1.h> |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 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 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 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; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 47 | int iflag; |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 48 | int lflag; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 49 | int pflag; |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 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; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 65 | const char *outfile; /* Output filename */ |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 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 | /* |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 113 | * This function is used by the command to retrieve a component |
| 114 | * (sub-image) from the image (i.e. dumpimage -i <image> -p <position> |
| 115 | * <sub-image-name>). |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 116 | * Thus the code to extract a file from an image must be put here. |
| 117 | * |
| 118 | * Returns 0 if the file was successfully retrieved from the image, |
| 119 | * or a negative value on error. |
| 120 | */ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 121 | int (*extract_subimage)(void *, struct image_tool_params *); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 122 | /* |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 123 | * Some image generation support for ex (default image type) supports |
| 124 | * more than one type_ids, this callback function is used to check |
| 125 | * whether input (-T <image_type>) is supported by registered image |
| 126 | * generation/list low level code |
| 127 | */ |
| 128 | int (*check_image_type) (uint8_t); |
| 129 | /* This callback function will be executed if fflag is defined */ |
| 130 | int (*fflag_handle) (struct image_tool_params *); |
| 131 | /* |
| 132 | * This callback function will be executed for variable size record |
| 133 | * It is expected to build this header in memory and return its length |
| 134 | * and a pointer to it by using image_type_params.header_size and |
| 135 | * image_type_params.hdr. The return value shall indicate if an |
| 136 | * additional padding should be used when copying the data image |
| 137 | * by returning the padding length. |
| 138 | */ |
| 139 | int (*vrec_header) (struct image_tool_params *, |
| 140 | struct image_type_params *); |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 143 | /** |
| 144 | * imagetool_get_type() - find the image type params for a given image type |
| 145 | * |
| 146 | * It scans all registers image type supports |
| 147 | * checks the input type for each supported image type |
| 148 | * |
| 149 | * if successful, |
| 150 | * returns respective image_type_params pointer if success |
| 151 | * if input type_id is not supported by any of image_type_support |
| 152 | * returns NULL |
| 153 | */ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 154 | struct image_type_params *imagetool_get_type(int type); |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * imagetool_verify_print_header() - verifies the image header |
| 158 | * |
| 159 | * Scan registered image types and verify the image_header for each |
| 160 | * supported image type. If verification is successful, this prints |
| 161 | * the respective header. |
| 162 | * |
| 163 | * @return 0 on success, negative if input image format does not match with |
| 164 | * any of supported image types |
| 165 | */ |
| 166 | int imagetool_verify_print_header( |
| 167 | void *ptr, |
| 168 | struct stat *sbuf, |
| 169 | struct image_type_params *tparams, |
| 170 | struct image_tool_params *params); |
| 171 | |
Guilherme Maciel Ferreira | 067d156 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 172 | /** |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 173 | * imagetool_save_subimage - store data into a file |
Guilherme Maciel Ferreira | 067d156 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 174 | * @file_name: name of the destination file |
| 175 | * @file_data: data to be written |
| 176 | * @file_len: the amount of data to store |
| 177 | * |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 178 | * imagetool_save_subimage() store file_len bytes of data pointed by file_data |
Guilherme Maciel Ferreira | 067d156 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 179 | * into the file name by file_name. |
| 180 | * |
| 181 | * returns: |
| 182 | * zero in case of success or a negative value if fail. |
| 183 | */ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 184 | int imagetool_save_subimage( |
Guilherme Maciel Ferreira | 067d156 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 185 | const char *file_name, |
| 186 | ulong file_data, |
| 187 | ulong file_len); |
| 188 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 189 | /* |
| 190 | * There is a c file associated with supported image type low level code |
| 191 | * for ex. default_image.c, fit_image.c |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 192 | */ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 193 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 194 | |
| 195 | void pbl_load_uboot(int fd, struct image_tool_params *mparams); |
| 196 | |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 197 | #define U_BOOT_IMAGE_TYPE( \ |
| 198 | _id, \ |
| 199 | _name, \ |
| 200 | _header_size, \ |
| 201 | _header, \ |
| 202 | _check_params, \ |
| 203 | _verify_header, \ |
| 204 | _print_header, \ |
| 205 | _set_header, \ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 206 | _extract_subimage, \ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 207 | _check_image_type, \ |
| 208 | _fflag_handle, \ |
| 209 | _vrec_header \ |
| 210 | ) \ |
| 211 | ll_entry_declare(struct image_type_params, _id, image_type) = { \ |
| 212 | .name = _name, \ |
| 213 | .header_size = _header_size, \ |
| 214 | .hdr = _header, \ |
| 215 | .check_params = _check_params, \ |
| 216 | .verify_header = _verify_header, \ |
| 217 | .print_header = _print_header, \ |
| 218 | .set_header = _set_header, \ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 219 | .extract_subimage = _extract_subimage, \ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 220 | .check_image_type = _check_image_type, \ |
| 221 | .fflag_handle = _fflag_handle, \ |
| 222 | .vrec_header = _vrec_header \ |
| 223 | } |
| 224 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 225 | #endif /* _IMAGETOOL_H_ */ |