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