wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 1 | /* |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 2 | * (C) Copyright 2008 Semihalf |
| 3 | * |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 4 | * (C) Copyright 2000-2009 |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 5 | * DENX Software Engineering |
| 6 | * Wolfgang Denk, wd@denx.de |
wdenk | db82c8e | 2004-02-26 23:01:04 +0000 | [diff] [blame] | 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 11 | #include "mkimage.h" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 12 | #include <image.h> |
Wolfgang Denk | e97ab72 | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 13 | #include <version.h> |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 14 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 15 | static void copy_file(int, const char *, int); |
| 16 | static void usage(void); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 17 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 18 | /* image_type_params link list to maintain registered image type supports */ |
| 19 | struct image_type_params *mkimage_tparams = NULL; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 20 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 21 | /* parameters initialized by core will be used by the image type code */ |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 22 | struct image_tool_params params = { |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 23 | .os = IH_OS_LINUX, |
| 24 | .arch = IH_ARCH_PPC, |
| 25 | .type = IH_TYPE_KERNEL, |
| 26 | .comp = IH_COMP_GZIP, |
| 27 | .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS, |
Wolfgang Denk | 23416b8 | 2010-03-27 23:37:46 +0100 | [diff] [blame] | 28 | .imagename = "", |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 29 | .imagename2 = "", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 30 | }; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 31 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 32 | /* |
| 33 | * mkimage_register - |
| 34 | * |
| 35 | * It is used to register respective image generation/list support to the |
| 36 | * mkimage core |
| 37 | * |
| 38 | * the input struct image_type_params is checked and appended to the link |
| 39 | * list, if the input structure is already registered, error |
| 40 | */ |
| 41 | void mkimage_register (struct image_type_params *tparams) |
| 42 | { |
| 43 | struct image_type_params **tp; |
| 44 | |
| 45 | if (!tparams) { |
| 46 | fprintf (stderr, "%s: %s: Null input\n", |
| 47 | params.cmdname, __FUNCTION__); |
| 48 | exit (EXIT_FAILURE); |
| 49 | } |
| 50 | |
| 51 | /* scan the linked list, check for registry and point the last one */ |
| 52 | for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) { |
| 53 | if (!strcmp((*tp)->name, tparams->name)) { |
| 54 | fprintf (stderr, "%s: %s already registered\n", |
| 55 | params.cmdname, tparams->name); |
| 56 | return; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /* add input struct entry at the end of link list */ |
| 61 | *tp = tparams; |
| 62 | /* mark input entry as last entry in the link list */ |
| 63 | tparams->next = NULL; |
| 64 | |
| 65 | debug ("Registered %s\n", tparams->name); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * mkimage_get_type - |
| 70 | * |
| 71 | * It scans all registers image type supports |
| 72 | * checks the input type_id for each supported image type |
| 73 | * |
| 74 | * if successful, |
| 75 | * returns respective image_type_params pointer if success |
| 76 | * if input type_id is not supported by any of image_type_support |
| 77 | * returns NULL |
| 78 | */ |
| 79 | struct image_type_params *mkimage_get_type(int type) |
| 80 | { |
| 81 | struct image_type_params *curr; |
| 82 | |
| 83 | for (curr = mkimage_tparams; curr != NULL; curr = curr->next) { |
| 84 | if (curr->check_image_type) { |
| 85 | if (!curr->check_image_type (type)) |
| 86 | return curr; |
| 87 | } |
| 88 | } |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * mkimage_verify_print_header - |
| 94 | * |
| 95 | * It scans mkimage_tparams link list, |
| 96 | * verifies image_header for each supported image type |
| 97 | * if verification is successful, prints respective header |
| 98 | * |
| 99 | * returns negative if input image format does not match with any of |
| 100 | * supported image types |
| 101 | */ |
| 102 | int mkimage_verify_print_header (void *ptr, struct stat *sbuf) |
| 103 | { |
| 104 | int retval = -1; |
| 105 | struct image_type_params *curr; |
| 106 | |
| 107 | for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) { |
| 108 | if (curr->verify_header) { |
| 109 | retval = curr->verify_header ( |
| 110 | (unsigned char *)ptr, sbuf->st_size, |
| 111 | ¶ms); |
| 112 | |
| 113 | if (retval == 0) { |
| 114 | /* |
| 115 | * Print the image information |
| 116 | * if verify is successful |
| 117 | */ |
| 118 | if (curr->print_header) |
| 119 | curr->print_header (ptr); |
| 120 | else { |
| 121 | fprintf (stderr, |
| 122 | "%s: print_header undefined for %s\n", |
| 123 | params.cmdname, curr->name); |
| 124 | } |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return retval; |
| 130 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 131 | |
| 132 | int |
| 133 | main (int argc, char **argv) |
| 134 | { |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 135 | int ifd = -1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 136 | struct stat sbuf; |
Peter Tyser | 632d9ca | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 137 | char *ptr; |
Prafulla Wadaskar | 4d4d67d | 2009-08-16 05:28:19 +0530 | [diff] [blame] | 138 | int retval = 0; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 139 | struct image_type_params *tparams = NULL; |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 140 | int pad_len = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 141 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 142 | /* Init all image generation/list support */ |
| 143 | register_image_tool(mkimage_register); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 144 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 145 | params.cmdname = *argv; |
| 146 | params.addr = params.ep = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 147 | |
| 148 | while (--argc > 0 && **++argv == '-') { |
| 149 | while (*++*argv) { |
| 150 | switch (**argv) { |
| 151 | case 'l': |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 152 | params.lflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 153 | break; |
| 154 | case 'A': |
| 155 | if ((--argc <= 0) || |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 156 | (params.arch = |
| 157 | genimg_get_arch_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 158 | usage (); |
| 159 | goto NXTARG; |
Simon Glass | bf27d3d | 2013-06-13 15:10:06 -0700 | [diff] [blame] | 160 | case 'c': |
| 161 | if (--argc <= 0) |
| 162 | usage(); |
| 163 | params.comment = *++argv; |
| 164 | goto NXTARG; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 165 | case 'C': |
| 166 | if ((--argc <= 0) || |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 167 | (params.comp = |
| 168 | genimg_get_comp_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 169 | usage (); |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 170 | goto NXTARG; |
| 171 | case 'D': |
| 172 | if (--argc <= 0) |
| 173 | usage (); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 174 | params.dtc = *++argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 175 | goto NXTARG; |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 176 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 177 | case 'O': |
| 178 | if ((--argc <= 0) || |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 179 | (params.os = |
| 180 | genimg_get_os_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 181 | usage (); |
| 182 | goto NXTARG; |
| 183 | case 'T': |
| 184 | if ((--argc <= 0) || |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 185 | (params.type = |
| 186 | genimg_get_type_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 187 | usage (); |
| 188 | goto NXTARG; |
| 189 | |
| 190 | case 'a': |
| 191 | if (--argc <= 0) |
| 192 | usage (); |
Peter Tyser | 632d9ca | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 193 | params.addr = strtoul (*++argv, &ptr, 16); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 194 | if (*ptr) { |
| 195 | fprintf (stderr, |
| 196 | "%s: invalid load address %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 197 | params.cmdname, *argv); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 198 | exit (EXIT_FAILURE); |
| 199 | } |
| 200 | goto NXTARG; |
| 201 | case 'd': |
| 202 | if (--argc <= 0) |
| 203 | usage (); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 204 | params.datafile = *++argv; |
| 205 | params.dflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 206 | goto NXTARG; |
| 207 | case 'e': |
| 208 | if (--argc <= 0) |
| 209 | usage (); |
Peter Tyser | 632d9ca | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 210 | params.ep = strtoul (*++argv, &ptr, 16); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 211 | if (*ptr) { |
| 212 | fprintf (stderr, |
| 213 | "%s: invalid entry point %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 214 | params.cmdname, *argv); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 215 | exit (EXIT_FAILURE); |
| 216 | } |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 217 | params.eflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 218 | goto NXTARG; |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 219 | case 'f': |
| 220 | if (--argc <= 0) |
| 221 | usage (); |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 222 | params.datafile = *++argv; |
| 223 | /* no break */ |
| 224 | case 'F': |
Peter Tyser | 94734e6 | 2009-11-24 16:42:08 -0600 | [diff] [blame] | 225 | /* |
| 226 | * The flattened image tree (FIT) format |
| 227 | * requires a flattened device tree image type |
| 228 | */ |
| 229 | params.type = IH_TYPE_FLATDT; |
Peter Tyser | 54cd539 | 2009-12-06 01:33:24 -0600 | [diff] [blame] | 230 | params.fflag = 1; |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 231 | goto NXTARG; |
Simon Glass | b148974 | 2013-06-13 15:10:03 -0700 | [diff] [blame] | 232 | case 'k': |
| 233 | if (--argc <= 0) |
| 234 | usage(); |
| 235 | params.keydir = *++argv; |
| 236 | goto NXTARG; |
Simon Glass | b4d8b09 | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 237 | case 'K': |
| 238 | if (--argc <= 0) |
| 239 | usage(); |
| 240 | params.keydest = *++argv; |
| 241 | goto NXTARG; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 242 | case 'n': |
| 243 | if (--argc <= 0) |
| 244 | usage (); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 245 | params.imagename = *++argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 246 | goto NXTARG; |
Simon Glass | 817278a | 2013-06-13 15:10:07 -0700 | [diff] [blame] | 247 | case 'r': |
| 248 | params.require_keys = 1; |
| 249 | break; |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 250 | case 'R': |
| 251 | if (--argc <= 0) |
| 252 | usage(); |
| 253 | /* |
| 254 | * This entry is for the second configuration |
| 255 | * file, if only one is not enough. |
| 256 | */ |
| 257 | params.imagename2 = *++argv; |
| 258 | goto NXTARG; |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 259 | case 's': |
| 260 | params.skipcpy = 1; |
| 261 | break; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 262 | case 'v': |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 263 | params.vflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 264 | break; |
Wolfgang Denk | e97ab72 | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 265 | case 'V': |
| 266 | printf("mkimage version %s\n", PLAIN_VERSION); |
| 267 | exit(EXIT_SUCCESS); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 268 | case 'x': |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 269 | params.xflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 270 | break; |
| 271 | default: |
| 272 | usage (); |
| 273 | } |
| 274 | } |
| 275 | NXTARG: ; |
| 276 | } |
| 277 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 278 | if (argc != 1) |
| 279 | usage (); |
| 280 | |
| 281 | /* set tparams as per input type_id */ |
| 282 | tparams = mkimage_get_type(params.type); |
| 283 | if (tparams == NULL) { |
| 284 | fprintf (stderr, "%s: unsupported type %s\n", |
| 285 | params.cmdname, genimg_get_type_name(params.type)); |
| 286 | exit (EXIT_FAILURE); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * check the passed arguments parameters meets the requirements |
| 291 | * as per image type to be generated/listed |
| 292 | */ |
| 293 | if (tparams->check_params) |
| 294 | if (tparams->check_params (¶ms)) |
| 295 | usage (); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 296 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 297 | if (!params.eflag) { |
| 298 | params.ep = params.addr; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 299 | /* If XIP, entry point must be after the U-Boot header */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 300 | if (params.xflag) |
| 301 | params.ep += tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 304 | params.imagefile = *argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 305 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 306 | if (params.fflag){ |
| 307 | if (tparams->fflag_handle) |
| 308 | /* |
| 309 | * in some cases, some additional processing needs |
| 310 | * to be done if fflag is defined |
| 311 | * |
| 312 | * For ex. fit_handle_file for Fit file support |
| 313 | */ |
| 314 | retval = tparams->fflag_handle(¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 315 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 316 | if (retval != EXIT_SUCCESS) |
| 317 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 320 | if (params.lflag || params.fflag) { |
| 321 | ifd = open (params.imagefile, O_RDONLY|O_BINARY); |
| 322 | } else { |
| 323 | ifd = open (params.imagefile, |
| 324 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); |
| 325 | } |
| 326 | |
| 327 | if (ifd < 0) { |
| 328 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 329 | params.cmdname, params.imagefile, |
| 330 | strerror(errno)); |
| 331 | exit (EXIT_FAILURE); |
| 332 | } |
| 333 | |
| 334 | if (params.lflag || params.fflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 335 | /* |
| 336 | * list header information of existing image |
| 337 | */ |
| 338 | if (fstat(ifd, &sbuf) < 0) { |
| 339 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 340 | params.cmdname, params.imagefile, |
| 341 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 342 | exit (EXIT_FAILURE); |
| 343 | } |
| 344 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 345 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 346 | fprintf (stderr, |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 347 | "%s: Bad size: \"%s\" is not valid image\n", |
| 348 | params.cmdname, params.imagefile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 349 | exit (EXIT_FAILURE); |
| 350 | } |
| 351 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 352 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); |
| 353 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 354 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 355 | params.cmdname, params.imagefile, |
| 356 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 357 | exit (EXIT_FAILURE); |
| 358 | } |
| 359 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 360 | /* |
| 361 | * scan through mkimage registry for all supported image types |
| 362 | * and verify the input image file header for match |
| 363 | * Print the image information for matched image type |
| 364 | * Returns the error code if not matched |
| 365 | */ |
| 366 | retval = mkimage_verify_print_header (ptr, &sbuf); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 367 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 368 | (void) munmap((void *)ptr, sbuf.st_size); |
| 369 | (void) close (ifd); |
| 370 | |
Prafulla Wadaskar | 139cb08 | 2009-08-10 18:49:37 +0530 | [diff] [blame] | 371 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 375 | * In case there an header with a variable |
| 376 | * length will be added, the corresponding |
| 377 | * function is called. This is responsible to |
| 378 | * allocate memory for the header itself. |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 379 | */ |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 380 | if (tparams->vrec_header) |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 381 | pad_len = tparams->vrec_header(¶ms, tparams); |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 382 | else |
| 383 | memset(tparams->hdr, 0, tparams->header_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 384 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 385 | if (write(ifd, tparams->hdr, tparams->header_size) |
| 386 | != tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 387 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 388 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 389 | exit (EXIT_FAILURE); |
| 390 | } |
| 391 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 392 | if (!params.skipcpy) { |
| 393 | if (params.type == IH_TYPE_MULTI || |
| 394 | params.type == IH_TYPE_SCRIPT) { |
| 395 | char *file = params.datafile; |
| 396 | uint32_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 397 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 398 | for (;;) { |
| 399 | char *sep = NULL; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 400 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 401 | if (file) { |
| 402 | if ((sep = strchr(file, ':')) != NULL) { |
| 403 | *sep = '\0'; |
| 404 | } |
| 405 | |
| 406 | if (stat (file, &sbuf) < 0) { |
| 407 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 408 | params.cmdname, file, strerror(errno)); |
| 409 | exit (EXIT_FAILURE); |
| 410 | } |
| 411 | size = cpu_to_uimage (sbuf.st_size); |
| 412 | } else { |
| 413 | size = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 416 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
| 417 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 418 | params.cmdname, params.imagefile, |
| 419 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 420 | exit (EXIT_FAILURE); |
| 421 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 422 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 423 | if (!file) { |
| 424 | break; |
| 425 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 426 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 427 | if (sep) { |
| 428 | *sep = ':'; |
| 429 | file = sep + 1; |
| 430 | } else { |
| 431 | file = NULL; |
| 432 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 433 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 434 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 435 | file = params.datafile; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 436 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 437 | for (;;) { |
| 438 | char *sep = strchr(file, ':'); |
| 439 | if (sep) { |
| 440 | *sep = '\0'; |
| 441 | copy_file (ifd, file, 1); |
| 442 | *sep++ = ':'; |
| 443 | file = sep; |
| 444 | } else { |
| 445 | copy_file (ifd, file, 0); |
| 446 | break; |
| 447 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 448 | } |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 449 | } else if (params.type == IH_TYPE_PBLIMAGE) { |
| 450 | /* PBL has special Image format, implements its' own */ |
| 451 | pbl_load_uboot(ifd, ¶ms); |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 452 | } else { |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 453 | copy_file(ifd, params.datafile, pad_len); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 454 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 458 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 459 | !defined(__sun__) && \ |
| 460 | !defined(__FreeBSD__) && \ |
| 461 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 462 | (void) fdatasync (ifd); |
| 463 | #else |
| 464 | (void) fsync (ifd); |
| 465 | #endif |
| 466 | |
| 467 | if (fstat(ifd, &sbuf) < 0) { |
| 468 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 469 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 470 | exit (EXIT_FAILURE); |
| 471 | } |
| 472 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 473 | ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); |
| 474 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 475 | fprintf (stderr, "%s: Can't map %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 476 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 477 | exit (EXIT_FAILURE); |
| 478 | } |
| 479 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 480 | /* Setup the image header as per input image type*/ |
| 481 | if (tparams->set_header) |
| 482 | tparams->set_header (ptr, &sbuf, ifd, ¶ms); |
| 483 | else { |
| 484 | fprintf (stderr, "%s: Can't set header for %s: %s\n", |
| 485 | params.cmdname, tparams->name, strerror(errno)); |
| 486 | exit (EXIT_FAILURE); |
| 487 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 488 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 489 | /* Print the image information by processing image header */ |
| 490 | if (tparams->print_header) |
| 491 | tparams->print_header (ptr); |
| 492 | else { |
| 493 | fprintf (stderr, "%s: Can't print header for %s: %s\n", |
| 494 | params.cmdname, tparams->name, strerror(errno)); |
| 495 | exit (EXIT_FAILURE); |
| 496 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 497 | |
| 498 | (void) munmap((void *)ptr, sbuf.st_size); |
| 499 | |
| 500 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 501 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 502 | !defined(__sun__) && \ |
| 503 | !defined(__FreeBSD__) && \ |
| 504 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 505 | (void) fdatasync (ifd); |
| 506 | #else |
| 507 | (void) fsync (ifd); |
| 508 | #endif |
| 509 | |
| 510 | if (close(ifd)) { |
| 511 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 512 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 513 | exit (EXIT_FAILURE); |
| 514 | } |
| 515 | |
| 516 | exit (EXIT_SUCCESS); |
| 517 | } |
| 518 | |
| 519 | static void |
| 520 | copy_file (int ifd, const char *datafile, int pad) |
| 521 | { |
| 522 | int dfd; |
| 523 | struct stat sbuf; |
| 524 | unsigned char *ptr; |
| 525 | int tail; |
| 526 | int zero = 0; |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 527 | uint8_t zeros[4096]; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 528 | int offset = 0; |
| 529 | int size; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 530 | struct image_type_params *tparams = mkimage_get_type (params.type); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 531 | |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 532 | if (pad >= sizeof(zeros)) { |
| 533 | fprintf(stderr, "%s: Can't pad to %d\n", |
| 534 | params.cmdname, pad); |
| 535 | exit(EXIT_FAILURE); |
| 536 | } |
| 537 | |
| 538 | memset(zeros, 0, sizeof(zeros)); |
| 539 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 540 | if (params.vflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 541 | fprintf (stderr, "Adding Image %s\n", datafile); |
| 542 | } |
| 543 | |
wdenk | 1ee550e | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 544 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 545 | fprintf (stderr, "%s: Can't open %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 546 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 547 | exit (EXIT_FAILURE); |
| 548 | } |
| 549 | |
| 550 | if (fstat(dfd, &sbuf) < 0) { |
| 551 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 552 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 553 | exit (EXIT_FAILURE); |
| 554 | } |
| 555 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 556 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 557 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 558 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 559 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 560 | exit (EXIT_FAILURE); |
| 561 | } |
| 562 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 563 | if (params.xflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 564 | unsigned char *p = NULL; |
| 565 | /* |
| 566 | * XIP: do not append the image_header_t at the |
| 567 | * beginning of the file, but consume the space |
| 568 | * reserved for it. |
| 569 | */ |
| 570 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 571 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 572 | fprintf (stderr, |
| 573 | "%s: Bad size: \"%s\" is too small for XIP\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 574 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 575 | exit (EXIT_FAILURE); |
| 576 | } |
| 577 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 578 | for (p = ptr; p < ptr + tparams->header_size; p++) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 579 | if ( *p != 0xff ) { |
| 580 | fprintf (stderr, |
| 581 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 582 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 583 | exit (EXIT_FAILURE); |
| 584 | } |
| 585 | } |
| 586 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 587 | offset = tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | size = sbuf.st_size - offset; |
| 591 | if (write(ifd, ptr + offset, size) != size) { |
| 592 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 593 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 594 | exit (EXIT_FAILURE); |
| 595 | } |
| 596 | |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 597 | tail = size % 4; |
| 598 | if ((pad == 1) && (tail != 0)) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 599 | |
| 600 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { |
| 601 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 602 | params.cmdname, params.imagefile, |
| 603 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 604 | exit (EXIT_FAILURE); |
| 605 | } |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 606 | } else if (pad > 1) { |
| 607 | if (write(ifd, (char *)&zeros, pad) != pad) { |
| 608 | fprintf(stderr, "%s: Write error on %s: %s\n", |
| 609 | params.cmdname, params.imagefile, |
| 610 | strerror(errno)); |
| 611 | exit(EXIT_FAILURE); |
| 612 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | (void) munmap((void *)ptr, sbuf.st_size); |
| 616 | (void) close (dfd); |
| 617 | } |
| 618 | |
Guilherme Maciel Ferreira | ff0fa61 | 2013-12-01 12:43:09 -0700 | [diff] [blame] | 619 | static void usage(void) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 620 | { |
| 621 | fprintf (stderr, "Usage: %s -l image\n" |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 622 | " -l ==> list image header information\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 623 | params.cmdname); |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 624 | fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp " |
| 625 | "-a addr -e ep -n name -d data_file[:data_file...] image\n" |
| 626 | " -A ==> set architecture to 'arch'\n" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 627 | " -O ==> set operating system to 'os'\n" |
| 628 | " -T ==> set image type to 'type'\n" |
| 629 | " -C ==> set compression type 'comp'\n" |
| 630 | " -a ==> set load address to 'addr' (hex)\n" |
| 631 | " -e ==> set entry point to 'ep' (hex)\n" |
| 632 | " -n ==> set image name to 'name'\n" |
| 633 | " -d ==> use image data from 'datafile'\n" |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 634 | " -x ==> set XIP (execute in place)\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 635 | params.cmdname); |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 636 | fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 637 | params.cmdname); |
Simon Glass | b148974 | 2013-06-13 15:10:03 -0700 | [diff] [blame] | 638 | fprintf(stderr, " -D => set options for device tree compiler\n" |
| 639 | " -f => input filename for FIT source\n"); |
| 640 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | 817278a | 2013-06-13 15:10:07 -0700 | [diff] [blame] | 641 | fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n" |
Simon Glass | b4d8b09 | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 642 | " -k => set directory containing private keys\n" |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 643 | " -K => write public keys to this .dtb file\n" |
Simon Glass | bf27d3d | 2013-06-13 15:10:06 -0700 | [diff] [blame] | 644 | " -c => add comment in signature node\n" |
Simon Glass | 817278a | 2013-06-13 15:10:07 -0700 | [diff] [blame] | 645 | " -F => re-sign existing FIT image\n" |
| 646 | " -r => mark keys used as 'required' in dtb\n"); |
Simon Glass | b148974 | 2013-06-13 15:10:03 -0700 | [diff] [blame] | 647 | #else |
| 648 | fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n"); |
| 649 | #endif |
Wolfgang Denk | e97ab72 | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 650 | fprintf (stderr, " %s -V ==> print version information and exit\n", |
| 651 | params.cmdname); |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 652 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 653 | exit (EXIT_FAILURE); |
| 654 | } |