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