wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 1 | /* |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 2 | * (C) Copyright 2008 Semihalf |
| 3 | * |
Prafulla Wadaskar | 89a4d6b | 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 | 0c852a2 | 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 | b97a2a0 | 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 | 976b38c | 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 | 89a4d6b | 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 | 89a4d6b | 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 | 89a4d6b | 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 | 04387d2 | 2010-03-27 23:37:46 +0100 | [diff] [blame] | 41 | .imagename = "", |
Shaohui Xie | 5d898a0 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 42 | .imagename2 = "", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 43 | }; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 44 | |
Prafulla Wadaskar | 89a4d6b | 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 | 9d25438 | 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 | a2513e2 | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 150 | char *ptr; |
Prafulla Wadaskar | 449609f | 2009-08-16 05:28:19 +0530 | [diff] [blame] | 151 | int retval = 0; |
Prafulla Wadaskar | 89a4d6b | 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 | 5d898a0 | 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 | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 156 | /* Init Kirkwood Boot image generation/list support */ |
| 157 | init_kwb_image_type (); |
Stefano Babic | 8edcde5 | 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 | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 160 | /* Init FIT image generation/list support */ |
| 161 | init_fit_image_type (); |
John Rigby | 3decb14 | 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 | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 164 | /* Init Default image generation/list support */ |
| 165 | init_default_image_type (); |
Heiko Schocher | 7816f2c | 2011-07-16 00:06:42 +0000 | [diff] [blame] | 166 | /* Init Davinci UBL support */ |
| 167 | init_ubl_image_type(); |
Stefano Babic | 4962e38 | 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 | 89a4d6b | 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 | 89a4d6b | 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 | 89a4d6b | 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 | 89a4d6b | 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 (); |
| 191 | goto NXTARG; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 192 | case 'D': |
| 193 | if (--argc <= 0) |
| 194 | usage (); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 195 | params.dtc = *++argv; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 196 | goto NXTARG; |
| 197 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 198 | case 'O': |
| 199 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 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 | 89a4d6b | 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 | a2513e2 | 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 | 89a4d6b | 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 | 89a4d6b | 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 | a2513e2 | 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 | 89a4d6b | 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 | 89a4d6b | 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 | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 240 | case 'f': |
| 241 | if (--argc <= 0) |
| 242 | usage (); |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame^] | 243 | params.datafile = *++argv; |
| 244 | /* no break */ |
| 245 | case 'F': |
Peter Tyser | 1a99de2 | 2009-11-24 16:42:08 -0600 | [diff] [blame] | 246 | /* |
| 247 | * The flattened image tree (FIT) format |
| 248 | * requires a flattened device tree image type |
| 249 | */ |
| 250 | params.type = IH_TYPE_FLATDT; |
Peter Tyser | fbc1c8f | 2009-12-06 01:33:24 -0600 | [diff] [blame] | 251 | params.fflag = 1; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 252 | goto NXTARG; |
Simon Glass | 80e4df8 | 2013-06-13 15:10:03 -0700 | [diff] [blame] | 253 | case 'k': |
| 254 | if (--argc <= 0) |
| 255 | usage(); |
| 256 | params.keydir = *++argv; |
| 257 | goto NXTARG; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 258 | case 'K': |
| 259 | if (--argc <= 0) |
| 260 | usage(); |
| 261 | params.keydest = *++argv; |
| 262 | goto NXTARG; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 263 | case 'n': |
| 264 | if (--argc <= 0) |
| 265 | usage (); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 266 | params.imagename = *++argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 267 | goto NXTARG; |
Shaohui Xie | 5d898a0 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 268 | case 'R': |
| 269 | if (--argc <= 0) |
| 270 | usage(); |
| 271 | /* |
| 272 | * This entry is for the second configuration |
| 273 | * file, if only one is not enough. |
| 274 | */ |
| 275 | params.imagename2 = *++argv; |
| 276 | goto NXTARG; |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 277 | case 's': |
| 278 | params.skipcpy = 1; |
| 279 | break; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 280 | case 'v': |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 281 | params.vflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 282 | break; |
Wolfgang Denk | 976b38c | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 283 | case 'V': |
| 284 | printf("mkimage version %s\n", PLAIN_VERSION); |
| 285 | exit(EXIT_SUCCESS); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 286 | case 'x': |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 287 | params.xflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 288 | break; |
| 289 | default: |
| 290 | usage (); |
| 291 | } |
| 292 | } |
| 293 | NXTARG: ; |
| 294 | } |
| 295 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 296 | if (argc != 1) |
| 297 | usage (); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 298 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 299 | /* set tparams as per input type_id */ |
| 300 | tparams = mkimage_get_type(params.type); |
| 301 | if (tparams == NULL) { |
| 302 | fprintf (stderr, "%s: unsupported type %s\n", |
| 303 | params.cmdname, genimg_get_type_name(params.type)); |
| 304 | exit (EXIT_FAILURE); |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * check the passed arguments parameters meets the requirements |
| 309 | * as per image type to be generated/listed |
| 310 | */ |
| 311 | if (tparams->check_params) |
| 312 | if (tparams->check_params (¶ms)) |
| 313 | usage (); |
| 314 | |
| 315 | if (!params.eflag) { |
| 316 | params.ep = params.addr; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 317 | /* If XIP, entry point must be after the U-Boot header */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 318 | if (params.xflag) |
| 319 | params.ep += tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 322 | params.imagefile = *argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 323 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 324 | if (params.fflag){ |
| 325 | if (tparams->fflag_handle) |
| 326 | /* |
| 327 | * in some cases, some additional processing needs |
| 328 | * to be done if fflag is defined |
| 329 | * |
| 330 | * For ex. fit_handle_file for Fit file support |
| 331 | */ |
| 332 | retval = tparams->fflag_handle(¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 333 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 334 | if (retval != EXIT_SUCCESS) |
| 335 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 338 | if (params.lflag || params.fflag) { |
| 339 | ifd = open (params.imagefile, O_RDONLY|O_BINARY); |
| 340 | } else { |
| 341 | ifd = open (params.imagefile, |
| 342 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); |
| 343 | } |
| 344 | |
| 345 | if (ifd < 0) { |
| 346 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 347 | params.cmdname, params.imagefile, |
| 348 | strerror(errno)); |
| 349 | exit (EXIT_FAILURE); |
| 350 | } |
| 351 | |
| 352 | if (params.lflag || params.fflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 353 | /* |
| 354 | * list header information of existing image |
| 355 | */ |
| 356 | if (fstat(ifd, &sbuf) < 0) { |
| 357 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 358 | params.cmdname, params.imagefile, |
| 359 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 360 | exit (EXIT_FAILURE); |
| 361 | } |
| 362 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 363 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 364 | fprintf (stderr, |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 365 | "%s: Bad size: \"%s\" is not valid image\n", |
| 366 | params.cmdname, params.imagefile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 367 | exit (EXIT_FAILURE); |
| 368 | } |
| 369 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 370 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); |
| 371 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 372 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 373 | params.cmdname, params.imagefile, |
| 374 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 375 | exit (EXIT_FAILURE); |
| 376 | } |
| 377 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 378 | /* |
| 379 | * scan through mkimage registry for all supported image types |
| 380 | * and verify the input image file header for match |
| 381 | * Print the image information for matched image type |
| 382 | * Returns the error code if not matched |
| 383 | */ |
| 384 | retval = mkimage_verify_print_header (ptr, &sbuf); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 385 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 386 | (void) munmap((void *)ptr, sbuf.st_size); |
| 387 | (void) close (ifd); |
| 388 | |
Prafulla Wadaskar | f7644c0 | 2009-08-10 18:49:37 +0530 | [diff] [blame] | 389 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 393 | * In case there an header with a variable |
| 394 | * length will be added, the corresponding |
| 395 | * function is called. This is responsible to |
| 396 | * allocate memory for the header itself. |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 397 | */ |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 398 | if (tparams->vrec_header) |
| 399 | tparams->vrec_header(¶ms, tparams); |
| 400 | else |
| 401 | memset(tparams->hdr, 0, tparams->header_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 402 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 403 | if (write(ifd, tparams->hdr, tparams->header_size) |
| 404 | != tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 405 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 406 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 407 | exit (EXIT_FAILURE); |
| 408 | } |
| 409 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 410 | if (!params.skipcpy) { |
| 411 | if (params.type == IH_TYPE_MULTI || |
| 412 | params.type == IH_TYPE_SCRIPT) { |
| 413 | char *file = params.datafile; |
| 414 | uint32_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 415 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 416 | for (;;) { |
| 417 | char *sep = NULL; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 418 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 419 | if (file) { |
| 420 | if ((sep = strchr(file, ':')) != NULL) { |
| 421 | *sep = '\0'; |
| 422 | } |
| 423 | |
| 424 | if (stat (file, &sbuf) < 0) { |
| 425 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 426 | params.cmdname, file, strerror(errno)); |
| 427 | exit (EXIT_FAILURE); |
| 428 | } |
| 429 | size = cpu_to_uimage (sbuf.st_size); |
| 430 | } else { |
| 431 | size = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 434 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
| 435 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 436 | params.cmdname, params.imagefile, |
| 437 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 438 | exit (EXIT_FAILURE); |
| 439 | } |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 440 | |
| 441 | if (!file) { |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | if (sep) { |
| 446 | *sep = ':'; |
| 447 | file = sep + 1; |
| 448 | } else { |
| 449 | file = NULL; |
| 450 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 453 | file = params.datafile; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 454 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 455 | for (;;) { |
| 456 | char *sep = strchr(file, ':'); |
| 457 | if (sep) { |
| 458 | *sep = '\0'; |
| 459 | copy_file (ifd, file, 1); |
| 460 | *sep++ = ':'; |
| 461 | file = sep; |
| 462 | } else { |
| 463 | copy_file (ifd, file, 0); |
| 464 | break; |
| 465 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 466 | } |
Shaohui Xie | 5d898a0 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 467 | } else if (params.type == IH_TYPE_PBLIMAGE) { |
| 468 | /* PBL has special Image format, implements its' own */ |
| 469 | pbl_load_uboot(ifd, ¶ms); |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 470 | } else { |
| 471 | copy_file (ifd, params.datafile, 0); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 472 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 476 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 477 | !defined(__sun__) && \ |
| 478 | !defined(__FreeBSD__) && \ |
| 479 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 480 | (void) fdatasync (ifd); |
| 481 | #else |
| 482 | (void) fsync (ifd); |
| 483 | #endif |
| 484 | |
| 485 | if (fstat(ifd, &sbuf) < 0) { |
| 486 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 487 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 488 | exit (EXIT_FAILURE); |
| 489 | } |
| 490 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 491 | ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); |
| 492 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 493 | fprintf (stderr, "%s: Can't map %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 494 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 495 | exit (EXIT_FAILURE); |
| 496 | } |
| 497 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 498 | /* Setup the image header as per input image type*/ |
| 499 | if (tparams->set_header) |
| 500 | tparams->set_header (ptr, &sbuf, ifd, ¶ms); |
| 501 | else { |
| 502 | fprintf (stderr, "%s: Can't set header for %s: %s\n", |
| 503 | params.cmdname, tparams->name, strerror(errno)); |
| 504 | exit (EXIT_FAILURE); |
| 505 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 506 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 507 | /* Print the image information by processing image header */ |
| 508 | if (tparams->print_header) |
| 509 | tparams->print_header (ptr); |
| 510 | else { |
| 511 | fprintf (stderr, "%s: Can't print header for %s: %s\n", |
| 512 | params.cmdname, tparams->name, strerror(errno)); |
| 513 | exit (EXIT_FAILURE); |
| 514 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 515 | |
| 516 | (void) munmap((void *)ptr, sbuf.st_size); |
| 517 | |
| 518 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 519 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 520 | !defined(__sun__) && \ |
| 521 | !defined(__FreeBSD__) && \ |
| 522 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 523 | (void) fdatasync (ifd); |
| 524 | #else |
| 525 | (void) fsync (ifd); |
| 526 | #endif |
| 527 | |
| 528 | if (close(ifd)) { |
| 529 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 530 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 531 | exit (EXIT_FAILURE); |
| 532 | } |
| 533 | |
| 534 | exit (EXIT_SUCCESS); |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | copy_file (int ifd, const char *datafile, int pad) |
| 539 | { |
| 540 | int dfd; |
| 541 | struct stat sbuf; |
| 542 | unsigned char *ptr; |
| 543 | int tail; |
| 544 | int zero = 0; |
| 545 | int offset = 0; |
| 546 | int size; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 547 | struct image_type_params *tparams = mkimage_get_type (params.type); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 548 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 549 | if (params.vflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 550 | fprintf (stderr, "Adding Image %s\n", datafile); |
| 551 | } |
| 552 | |
wdenk | ef1464c | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 553 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 554 | fprintf (stderr, "%s: Can't open %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 555 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 556 | exit (EXIT_FAILURE); |
| 557 | } |
| 558 | |
| 559 | if (fstat(dfd, &sbuf) < 0) { |
| 560 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 561 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 562 | exit (EXIT_FAILURE); |
| 563 | } |
| 564 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 565 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 566 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 567 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 568 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 569 | exit (EXIT_FAILURE); |
| 570 | } |
| 571 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 572 | if (params.xflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 573 | unsigned char *p = NULL; |
| 574 | /* |
| 575 | * XIP: do not append the image_header_t at the |
| 576 | * beginning of the file, but consume the space |
| 577 | * reserved for it. |
| 578 | */ |
| 579 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 580 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 581 | fprintf (stderr, |
| 582 | "%s: Bad size: \"%s\" is too small for XIP\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 583 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 584 | exit (EXIT_FAILURE); |
| 585 | } |
| 586 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 587 | for (p = ptr; p < ptr + tparams->header_size; p++) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 588 | if ( *p != 0xff ) { |
| 589 | fprintf (stderr, |
| 590 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 591 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 592 | exit (EXIT_FAILURE); |
| 593 | } |
| 594 | } |
| 595 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 596 | offset = tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | size = sbuf.st_size - offset; |
| 600 | if (write(ifd, ptr + offset, size) != size) { |
| 601 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 602 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 603 | exit (EXIT_FAILURE); |
| 604 | } |
| 605 | |
| 606 | if (pad && ((tail = size % 4) != 0)) { |
| 607 | |
| 608 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { |
| 609 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 610 | params.cmdname, params.imagefile, |
| 611 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 612 | exit (EXIT_FAILURE); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | (void) munmap((void *)ptr, sbuf.st_size); |
| 617 | (void) close (dfd); |
| 618 | } |
| 619 | |
| 620 | void |
| 621 | usage () |
| 622 | { |
| 623 | fprintf (stderr, "Usage: %s -l image\n" |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 624 | " -l ==> list image header information\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 625 | params.cmdname); |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 626 | fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp " |
| 627 | "-a addr -e ep -n name -d data_file[:data_file...] image\n" |
| 628 | " -A ==> set architecture to 'arch'\n" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 629 | " -O ==> set operating system to 'os'\n" |
| 630 | " -T ==> set image type to 'type'\n" |
| 631 | " -C ==> set compression type 'comp'\n" |
| 632 | " -a ==> set load address to 'addr' (hex)\n" |
| 633 | " -e ==> set entry point to 'ep' (hex)\n" |
| 634 | " -n ==> set image name to 'name'\n" |
| 635 | " -d ==> use image data from 'datafile'\n" |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 636 | " -x ==> set XIP (execute in place)\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 637 | params.cmdname); |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame^] | 638 | fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 639 | params.cmdname); |
Simon Glass | 80e4df8 | 2013-06-13 15:10:03 -0700 | [diff] [blame] | 640 | fprintf(stderr, " -D => set options for device tree compiler\n" |
| 641 | " -f => input filename for FIT source\n"); |
| 642 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 643 | fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb]\n" |
| 644 | " -k => set directory containing private keys\n" |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame^] | 645 | " -K => write public keys to this .dtb file\n" |
| 646 | " -F => re-sign existing FIT image\n"); |
Simon Glass | 80e4df8 | 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 | 976b38c | 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 | 9d25438 | 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 | } |