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(); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 165 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 166 | params.cmdname = *argv; |
| 167 | params.addr = params.ep = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 168 | |
| 169 | while (--argc > 0 && **++argv == '-') { |
| 170 | while (*++*argv) { |
| 171 | switch (**argv) { |
| 172 | case 'l': |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 173 | params.lflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 174 | break; |
| 175 | case 'A': |
| 176 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 177 | (params.arch = |
| 178 | genimg_get_arch_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 179 | usage (); |
| 180 | goto NXTARG; |
| 181 | case 'C': |
| 182 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 183 | (params.comp = |
| 184 | genimg_get_comp_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 185 | usage (); |
| 186 | goto NXTARG; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 187 | case 'D': |
| 188 | if (--argc <= 0) |
| 189 | usage (); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 190 | params.dtc = *++argv; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 191 | goto NXTARG; |
| 192 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 193 | case 'O': |
| 194 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 195 | (params.os = |
| 196 | genimg_get_os_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 197 | usage (); |
| 198 | goto NXTARG; |
| 199 | case 'T': |
| 200 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 201 | (params.type = |
| 202 | genimg_get_type_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 203 | usage (); |
| 204 | goto NXTARG; |
| 205 | |
| 206 | case 'a': |
| 207 | if (--argc <= 0) |
| 208 | usage (); |
Peter Tyser | a2513e2 | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 209 | params.addr = strtoul (*++argv, &ptr, 16); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 210 | if (*ptr) { |
| 211 | fprintf (stderr, |
| 212 | "%s: invalid load address %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 213 | params.cmdname, *argv); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 214 | exit (EXIT_FAILURE); |
| 215 | } |
| 216 | goto NXTARG; |
| 217 | case 'd': |
| 218 | if (--argc <= 0) |
| 219 | usage (); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 220 | params.datafile = *++argv; |
| 221 | params.dflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 222 | goto NXTARG; |
| 223 | case 'e': |
| 224 | if (--argc <= 0) |
| 225 | usage (); |
Peter Tyser | a2513e2 | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 226 | params.ep = strtoul (*++argv, &ptr, 16); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 227 | if (*ptr) { |
| 228 | fprintf (stderr, |
| 229 | "%s: invalid entry point %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 230 | params.cmdname, *argv); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 231 | exit (EXIT_FAILURE); |
| 232 | } |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 233 | params.eflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 234 | goto NXTARG; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 235 | case 'f': |
| 236 | if (--argc <= 0) |
| 237 | usage (); |
Peter Tyser | 1a99de2 | 2009-11-24 16:42:08 -0600 | [diff] [blame] | 238 | /* |
| 239 | * The flattened image tree (FIT) format |
| 240 | * requires a flattened device tree image type |
| 241 | */ |
| 242 | params.type = IH_TYPE_FLATDT; |
Peter Tyser | fbc1c8f | 2009-12-06 01:33:24 -0600 | [diff] [blame] | 243 | params.datafile = *++argv; |
| 244 | params.fflag = 1; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 245 | goto NXTARG; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 246 | case 'n': |
| 247 | if (--argc <= 0) |
| 248 | usage (); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 249 | params.imagename = *++argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 250 | goto NXTARG; |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 251 | case 's': |
| 252 | params.skipcpy = 1; |
| 253 | break; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 254 | case 'v': |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 255 | params.vflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 256 | break; |
Wolfgang Denk | 976b38c | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 257 | case 'V': |
| 258 | printf("mkimage version %s\n", PLAIN_VERSION); |
| 259 | exit(EXIT_SUCCESS); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 260 | case 'x': |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 261 | params.xflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 262 | break; |
| 263 | default: |
| 264 | usage (); |
| 265 | } |
| 266 | } |
| 267 | NXTARG: ; |
| 268 | } |
| 269 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 270 | if (argc != 1) |
| 271 | usage (); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 272 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 273 | /* set tparams as per input type_id */ |
| 274 | tparams = mkimage_get_type(params.type); |
| 275 | if (tparams == NULL) { |
| 276 | fprintf (stderr, "%s: unsupported type %s\n", |
| 277 | params.cmdname, genimg_get_type_name(params.type)); |
| 278 | exit (EXIT_FAILURE); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * check the passed arguments parameters meets the requirements |
| 283 | * as per image type to be generated/listed |
| 284 | */ |
| 285 | if (tparams->check_params) |
| 286 | if (tparams->check_params (¶ms)) |
| 287 | usage (); |
| 288 | |
| 289 | if (!params.eflag) { |
| 290 | params.ep = params.addr; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 291 | /* If XIP, entry point must be after the U-Boot header */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 292 | if (params.xflag) |
| 293 | params.ep += tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 296 | params.imagefile = *argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 297 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 298 | if (params.fflag){ |
| 299 | if (tparams->fflag_handle) |
| 300 | /* |
| 301 | * in some cases, some additional processing needs |
| 302 | * to be done if fflag is defined |
| 303 | * |
| 304 | * For ex. fit_handle_file for Fit file support |
| 305 | */ |
| 306 | retval = tparams->fflag_handle(¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 307 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 308 | if (retval != EXIT_SUCCESS) |
| 309 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 312 | if (params.lflag || params.fflag) { |
| 313 | ifd = open (params.imagefile, O_RDONLY|O_BINARY); |
| 314 | } else { |
| 315 | ifd = open (params.imagefile, |
| 316 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); |
| 317 | } |
| 318 | |
| 319 | if (ifd < 0) { |
| 320 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 321 | params.cmdname, params.imagefile, |
| 322 | strerror(errno)); |
| 323 | exit (EXIT_FAILURE); |
| 324 | } |
| 325 | |
| 326 | if (params.lflag || params.fflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 327 | /* |
| 328 | * list header information of existing image |
| 329 | */ |
| 330 | if (fstat(ifd, &sbuf) < 0) { |
| 331 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 332 | params.cmdname, params.imagefile, |
| 333 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 334 | exit (EXIT_FAILURE); |
| 335 | } |
| 336 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 337 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 338 | fprintf (stderr, |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 339 | "%s: Bad size: \"%s\" is not valid image\n", |
| 340 | params.cmdname, params.imagefile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 341 | exit (EXIT_FAILURE); |
| 342 | } |
| 343 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 344 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); |
| 345 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 346 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 347 | params.cmdname, params.imagefile, |
| 348 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 349 | exit (EXIT_FAILURE); |
| 350 | } |
| 351 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 352 | /* |
| 353 | * scan through mkimage registry for all supported image types |
| 354 | * and verify the input image file header for match |
| 355 | * Print the image information for matched image type |
| 356 | * Returns the error code if not matched |
| 357 | */ |
| 358 | retval = mkimage_verify_print_header (ptr, &sbuf); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 359 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 360 | (void) munmap((void *)ptr, sbuf.st_size); |
| 361 | (void) close (ifd); |
| 362 | |
Prafulla Wadaskar | f7644c0 | 2009-08-10 18:49:37 +0530 | [diff] [blame] | 363 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 367 | * In case there an header with a variable |
| 368 | * length will be added, the corresponding |
| 369 | * function is called. This is responsible to |
| 370 | * allocate memory for the header itself. |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 371 | */ |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 372 | if (tparams->vrec_header) |
| 373 | tparams->vrec_header(¶ms, tparams); |
| 374 | else |
| 375 | memset(tparams->hdr, 0, tparams->header_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 376 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 377 | if (write(ifd, tparams->hdr, tparams->header_size) |
| 378 | != tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 379 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 380 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 381 | exit (EXIT_FAILURE); |
| 382 | } |
| 383 | |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 384 | if (!params.skipcpy && |
| 385 | (params.type == IH_TYPE_MULTI || |
| 386 | params.type == IH_TYPE_SCRIPT)) { |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 387 | char *file = params.datafile; |
Wolfgang Denk | 3bb6680 | 2006-01-11 13:03:54 +0100 | [diff] [blame] | 388 | uint32_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 389 | |
| 390 | for (;;) { |
| 391 | char *sep = NULL; |
| 392 | |
| 393 | if (file) { |
| 394 | if ((sep = strchr(file, ':')) != NULL) { |
| 395 | *sep = '\0'; |
| 396 | } |
| 397 | |
| 398 | if (stat (file, &sbuf) < 0) { |
| 399 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 400 | params.cmdname, file, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 401 | exit (EXIT_FAILURE); |
| 402 | } |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 403 | size = cpu_to_uimage (sbuf.st_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 404 | } else { |
| 405 | size = 0; |
| 406 | } |
| 407 | |
| 408 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
| 409 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 410 | params.cmdname, params.imagefile, |
| 411 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 412 | exit (EXIT_FAILURE); |
| 413 | } |
| 414 | |
| 415 | if (!file) { |
| 416 | break; |
| 417 | } |
| 418 | |
| 419 | if (sep) { |
| 420 | *sep = ':'; |
| 421 | file = sep + 1; |
| 422 | } else { |
| 423 | file = NULL; |
| 424 | } |
| 425 | } |
| 426 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 427 | file = params.datafile; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 428 | |
| 429 | for (;;) { |
| 430 | char *sep = strchr(file, ':'); |
| 431 | if (sep) { |
| 432 | *sep = '\0'; |
| 433 | copy_file (ifd, file, 1); |
| 434 | *sep++ = ':'; |
| 435 | file = sep; |
| 436 | } else { |
| 437 | copy_file (ifd, file, 0); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | } else { |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 442 | copy_file (ifd, params.datafile, 0); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 446 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 447 | !defined(__sun__) && \ |
| 448 | !defined(__FreeBSD__) && \ |
| 449 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 450 | (void) fdatasync (ifd); |
| 451 | #else |
| 452 | (void) fsync (ifd); |
| 453 | #endif |
| 454 | |
| 455 | if (fstat(ifd, &sbuf) < 0) { |
| 456 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 457 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 458 | exit (EXIT_FAILURE); |
| 459 | } |
| 460 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 461 | ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); |
| 462 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 463 | fprintf (stderr, "%s: Can't map %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 464 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 465 | exit (EXIT_FAILURE); |
| 466 | } |
| 467 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 468 | /* Setup the image header as per input image type*/ |
| 469 | if (tparams->set_header) |
| 470 | tparams->set_header (ptr, &sbuf, ifd, ¶ms); |
| 471 | else { |
| 472 | fprintf (stderr, "%s: Can't set header for %s: %s\n", |
| 473 | params.cmdname, tparams->name, strerror(errno)); |
| 474 | exit (EXIT_FAILURE); |
| 475 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 476 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 477 | /* Print the image information by processing image header */ |
| 478 | if (tparams->print_header) |
| 479 | tparams->print_header (ptr); |
| 480 | else { |
| 481 | fprintf (stderr, "%s: Can't print header for %s: %s\n", |
| 482 | params.cmdname, tparams->name, strerror(errno)); |
| 483 | exit (EXIT_FAILURE); |
| 484 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 485 | |
| 486 | (void) munmap((void *)ptr, sbuf.st_size); |
| 487 | |
| 488 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 489 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 490 | !defined(__sun__) && \ |
| 491 | !defined(__FreeBSD__) && \ |
| 492 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 493 | (void) fdatasync (ifd); |
| 494 | #else |
| 495 | (void) fsync (ifd); |
| 496 | #endif |
| 497 | |
| 498 | if (close(ifd)) { |
| 499 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 500 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 501 | exit (EXIT_FAILURE); |
| 502 | } |
| 503 | |
| 504 | exit (EXIT_SUCCESS); |
| 505 | } |
| 506 | |
| 507 | static void |
| 508 | copy_file (int ifd, const char *datafile, int pad) |
| 509 | { |
| 510 | int dfd; |
| 511 | struct stat sbuf; |
| 512 | unsigned char *ptr; |
| 513 | int tail; |
| 514 | int zero = 0; |
| 515 | int offset = 0; |
| 516 | int size; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 517 | struct image_type_params *tparams = mkimage_get_type (params.type); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 518 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 519 | if (params.vflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 520 | fprintf (stderr, "Adding Image %s\n", datafile); |
| 521 | } |
| 522 | |
wdenk | ef1464c | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 523 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 524 | fprintf (stderr, "%s: Can't open %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 525 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 526 | exit (EXIT_FAILURE); |
| 527 | } |
| 528 | |
| 529 | if (fstat(dfd, &sbuf) < 0) { |
| 530 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 531 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 532 | exit (EXIT_FAILURE); |
| 533 | } |
| 534 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 535 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 536 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 537 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 538 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 539 | exit (EXIT_FAILURE); |
| 540 | } |
| 541 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 542 | if (params.xflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 543 | unsigned char *p = NULL; |
| 544 | /* |
| 545 | * XIP: do not append the image_header_t at the |
| 546 | * beginning of the file, but consume the space |
| 547 | * reserved for it. |
| 548 | */ |
| 549 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 550 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 551 | fprintf (stderr, |
| 552 | "%s: Bad size: \"%s\" is too small for XIP\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 553 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 554 | exit (EXIT_FAILURE); |
| 555 | } |
| 556 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 557 | for (p = ptr; p < ptr + tparams->header_size; p++) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 558 | if ( *p != 0xff ) { |
| 559 | fprintf (stderr, |
| 560 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 561 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 562 | exit (EXIT_FAILURE); |
| 563 | } |
| 564 | } |
| 565 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 566 | offset = tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | size = sbuf.st_size - offset; |
| 570 | if (write(ifd, ptr + offset, size) != size) { |
| 571 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 572 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 573 | exit (EXIT_FAILURE); |
| 574 | } |
| 575 | |
| 576 | if (pad && ((tail = size % 4) != 0)) { |
| 577 | |
| 578 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { |
| 579 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 580 | params.cmdname, params.imagefile, |
| 581 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 582 | exit (EXIT_FAILURE); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | (void) munmap((void *)ptr, sbuf.st_size); |
| 587 | (void) close (dfd); |
| 588 | } |
| 589 | |
| 590 | void |
| 591 | usage () |
| 592 | { |
| 593 | fprintf (stderr, "Usage: %s -l image\n" |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 594 | " -l ==> list image header information\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 595 | params.cmdname); |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 596 | fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp " |
| 597 | "-a addr -e ep -n name -d data_file[:data_file...] image\n" |
| 598 | " -A ==> set architecture to 'arch'\n" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 599 | " -O ==> set operating system to 'os'\n" |
| 600 | " -T ==> set image type to 'type'\n" |
| 601 | " -C ==> set compression type 'comp'\n" |
| 602 | " -a ==> set load address to 'addr' (hex)\n" |
| 603 | " -e ==> set entry point to 'ep' (hex)\n" |
| 604 | " -n ==> set image name to 'name'\n" |
| 605 | " -d ==> use image data from 'datafile'\n" |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 606 | " -x ==> set XIP (execute in place)\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 607 | params.cmdname); |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 608 | 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] | 609 | params.cmdname); |
Wolfgang Denk | 976b38c | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 610 | fprintf (stderr, " %s -V ==> print version information and exit\n", |
| 611 | params.cmdname); |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 612 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 613 | exit (EXIT_FAILURE); |
| 614 | } |