wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 1 | /* |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 2 | * (C) Copyright 2008 Semihalf |
| 3 | * |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 4 | * (C) Copyright 2000-2009 |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 5 | * DENX Software Engineering |
| 6 | * Wolfgang Denk, wd@denx.de |
wdenk | db82c8e | 2004-02-26 23:01:04 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 24 | #include "mkimage.h" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 25 | #include <image.h> |
Wolfgang Denk | e97ab72 | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 26 | #include <version.h> |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 27 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 28 | static void copy_file(int, const char *, int); |
| 29 | static void usage(void); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 30 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 31 | /* image_type_params link list to maintain registered image type supports */ |
| 32 | struct image_type_params *mkimage_tparams = NULL; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 33 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 34 | /* parameters initialized by core will be used by the image type code */ |
| 35 | struct mkimage_params params = { |
| 36 | .os = IH_OS_LINUX, |
| 37 | .arch = IH_ARCH_PPC, |
| 38 | .type = IH_TYPE_KERNEL, |
| 39 | .comp = IH_COMP_GZIP, |
| 40 | .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS, |
Wolfgang Denk | 23416b8 | 2010-03-27 23:37:46 +0100 | [diff] [blame] | 41 | .imagename = "", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 42 | }; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 43 | |
Prafulla Wadaskar | fabf3cf | 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 | d7816fb | 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 | 632d9ca | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 149 | char *ptr; |
Prafulla Wadaskar | 4d4d67d | 2009-08-16 05:28:19 +0530 | [diff] [blame] | 150 | int retval = 0; |
Prafulla Wadaskar | fabf3cf | 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 | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 153 | /* Init Kirkwood Boot image generation/list support */ |
| 154 | init_kwb_image_type (); |
Stefano Babic | 7b07f09 | 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 | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 157 | /* Init FIT image generation/list support */ |
| 158 | init_fit_image_type (); |
| 159 | /* Init Default image generation/list support */ |
| 160 | init_default_image_type (); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 161 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 162 | params.cmdname = *argv; |
| 163 | params.addr = params.ep = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 164 | |
| 165 | while (--argc > 0 && **++argv == '-') { |
| 166 | while (*++*argv) { |
| 167 | switch (**argv) { |
| 168 | case 'l': |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 169 | params.lflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 170 | break; |
| 171 | case 'A': |
| 172 | if ((--argc <= 0) || |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 173 | (params.arch = |
| 174 | genimg_get_arch_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 175 | usage (); |
| 176 | goto NXTARG; |
| 177 | case 'C': |
| 178 | if ((--argc <= 0) || |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 179 | (params.comp = |
| 180 | genimg_get_comp_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 181 | usage (); |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 182 | goto NXTARG; |
| 183 | case 'D': |
| 184 | if (--argc <= 0) |
| 185 | usage (); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 186 | params.dtc = *++argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 187 | goto NXTARG; |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 188 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 189 | case 'O': |
| 190 | if ((--argc <= 0) || |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 191 | (params.os = |
| 192 | genimg_get_os_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 193 | usage (); |
| 194 | goto NXTARG; |
| 195 | case 'T': |
| 196 | if ((--argc <= 0) || |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 197 | (params.type = |
| 198 | genimg_get_type_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 199 | usage (); |
| 200 | goto NXTARG; |
| 201 | |
| 202 | case 'a': |
| 203 | if (--argc <= 0) |
| 204 | usage (); |
Peter Tyser | 632d9ca | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 205 | params.addr = strtoul (*++argv, &ptr, 16); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 206 | if (*ptr) { |
| 207 | fprintf (stderr, |
| 208 | "%s: invalid load address %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 209 | params.cmdname, *argv); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 210 | exit (EXIT_FAILURE); |
| 211 | } |
| 212 | goto NXTARG; |
| 213 | case 'd': |
| 214 | if (--argc <= 0) |
| 215 | usage (); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 216 | params.datafile = *++argv; |
| 217 | params.dflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 218 | goto NXTARG; |
| 219 | case 'e': |
| 220 | if (--argc <= 0) |
| 221 | usage (); |
Peter Tyser | 632d9ca | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 222 | params.ep = strtoul (*++argv, &ptr, 16); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 223 | if (*ptr) { |
| 224 | fprintf (stderr, |
| 225 | "%s: invalid entry point %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 226 | params.cmdname, *argv); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 227 | exit (EXIT_FAILURE); |
| 228 | } |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 229 | params.eflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 230 | goto NXTARG; |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 231 | case 'f': |
| 232 | if (--argc <= 0) |
| 233 | usage (); |
Peter Tyser | 94734e6 | 2009-11-24 16:42:08 -0600 | [diff] [blame] | 234 | /* |
| 235 | * The flattened image tree (FIT) format |
| 236 | * requires a flattened device tree image type |
| 237 | */ |
| 238 | params.type = IH_TYPE_FLATDT; |
Peter Tyser | 54cd539 | 2009-12-06 01:33:24 -0600 | [diff] [blame] | 239 | params.datafile = *++argv; |
| 240 | params.fflag = 1; |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 241 | goto NXTARG; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 242 | case 'n': |
| 243 | if (--argc <= 0) |
| 244 | usage (); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 245 | params.imagename = *++argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 246 | goto NXTARG; |
| 247 | case 'v': |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 248 | params.vflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 249 | break; |
Wolfgang Denk | e97ab72 | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 250 | case 'V': |
| 251 | printf("mkimage version %s\n", PLAIN_VERSION); |
| 252 | exit(EXIT_SUCCESS); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 253 | case 'x': |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 254 | params.xflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 255 | break; |
| 256 | default: |
| 257 | usage (); |
| 258 | } |
| 259 | } |
| 260 | NXTARG: ; |
| 261 | } |
| 262 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 263 | if (argc != 1) |
| 264 | usage (); |
| 265 | |
| 266 | /* set tparams as per input type_id */ |
| 267 | tparams = mkimage_get_type(params.type); |
| 268 | if (tparams == NULL) { |
| 269 | fprintf (stderr, "%s: unsupported type %s\n", |
| 270 | params.cmdname, genimg_get_type_name(params.type)); |
| 271 | exit (EXIT_FAILURE); |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * check the passed arguments parameters meets the requirements |
| 276 | * as per image type to be generated/listed |
| 277 | */ |
| 278 | if (tparams->check_params) |
| 279 | if (tparams->check_params (¶ms)) |
| 280 | usage (); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 281 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 282 | if (!params.eflag) { |
| 283 | params.ep = params.addr; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 284 | /* If XIP, entry point must be after the U-Boot header */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 285 | if (params.xflag) |
| 286 | params.ep += tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 289 | params.imagefile = *argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 290 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 291 | if (params.fflag){ |
| 292 | if (tparams->fflag_handle) |
| 293 | /* |
| 294 | * in some cases, some additional processing needs |
| 295 | * to be done if fflag is defined |
| 296 | * |
| 297 | * For ex. fit_handle_file for Fit file support |
| 298 | */ |
| 299 | retval = tparams->fflag_handle(¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 300 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 301 | if (retval != EXIT_SUCCESS) |
| 302 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 305 | if (params.lflag || params.fflag) { |
| 306 | ifd = open (params.imagefile, O_RDONLY|O_BINARY); |
| 307 | } else { |
| 308 | ifd = open (params.imagefile, |
| 309 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); |
| 310 | } |
| 311 | |
| 312 | if (ifd < 0) { |
| 313 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 314 | params.cmdname, params.imagefile, |
| 315 | strerror(errno)); |
| 316 | exit (EXIT_FAILURE); |
| 317 | } |
| 318 | |
| 319 | if (params.lflag || params.fflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 320 | /* |
| 321 | * list header information of existing image |
| 322 | */ |
| 323 | if (fstat(ifd, &sbuf) < 0) { |
| 324 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 325 | params.cmdname, params.imagefile, |
| 326 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 327 | exit (EXIT_FAILURE); |
| 328 | } |
| 329 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 330 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 331 | fprintf (stderr, |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 332 | "%s: Bad size: \"%s\" is not valid image\n", |
| 333 | params.cmdname, params.imagefile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 334 | exit (EXIT_FAILURE); |
| 335 | } |
| 336 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 337 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); |
| 338 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 339 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 340 | params.cmdname, params.imagefile, |
| 341 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 342 | exit (EXIT_FAILURE); |
| 343 | } |
| 344 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 345 | /* |
| 346 | * scan through mkimage registry for all supported image types |
| 347 | * and verify the input image file header for match |
| 348 | * Print the image information for matched image type |
| 349 | * Returns the error code if not matched |
| 350 | */ |
| 351 | retval = mkimage_verify_print_header (ptr, &sbuf); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 352 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 353 | (void) munmap((void *)ptr, sbuf.st_size); |
| 354 | (void) close (ifd); |
| 355 | |
Prafulla Wadaskar | 139cb08 | 2009-08-10 18:49:37 +0530 | [diff] [blame] | 356 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Must be -w then: |
| 361 | * |
| 362 | * write dummy header, to be fixed later |
| 363 | */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 364 | memset (tparams->hdr, 0, tparams->header_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 365 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 366 | if (write(ifd, tparams->hdr, tparams->header_size) |
| 367 | != tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 368 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 369 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 370 | exit (EXIT_FAILURE); |
| 371 | } |
| 372 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 373 | if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) { |
| 374 | char *file = params.datafile; |
Wolfgang Denk | 31f14c2 | 2006-01-11 13:03:54 +0100 | [diff] [blame] | 375 | uint32_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 376 | |
| 377 | for (;;) { |
| 378 | char *sep = NULL; |
| 379 | |
| 380 | if (file) { |
| 381 | if ((sep = strchr(file, ':')) != NULL) { |
| 382 | *sep = '\0'; |
| 383 | } |
| 384 | |
| 385 | if (stat (file, &sbuf) < 0) { |
| 386 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 387 | params.cmdname, file, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 388 | exit (EXIT_FAILURE); |
| 389 | } |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 390 | size = cpu_to_uimage (sbuf.st_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 391 | } else { |
| 392 | size = 0; |
| 393 | } |
| 394 | |
| 395 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
| 396 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 397 | params.cmdname, params.imagefile, |
| 398 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 399 | exit (EXIT_FAILURE); |
| 400 | } |
| 401 | |
| 402 | if (!file) { |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | if (sep) { |
| 407 | *sep = ':'; |
| 408 | file = sep + 1; |
| 409 | } else { |
| 410 | file = NULL; |
| 411 | } |
| 412 | } |
| 413 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 414 | file = params.datafile; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 415 | |
| 416 | for (;;) { |
| 417 | char *sep = strchr(file, ':'); |
| 418 | if (sep) { |
| 419 | *sep = '\0'; |
| 420 | copy_file (ifd, file, 1); |
| 421 | *sep++ = ':'; |
| 422 | file = sep; |
| 423 | } else { |
| 424 | copy_file (ifd, file, 0); |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | } else { |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 429 | copy_file (ifd, params.datafile, 0); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 433 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 434 | !defined(__sun__) && \ |
| 435 | !defined(__FreeBSD__) && \ |
| 436 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 437 | (void) fdatasync (ifd); |
| 438 | #else |
| 439 | (void) fsync (ifd); |
| 440 | #endif |
| 441 | |
| 442 | if (fstat(ifd, &sbuf) < 0) { |
| 443 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 444 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 445 | exit (EXIT_FAILURE); |
| 446 | } |
| 447 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 448 | ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); |
| 449 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 450 | fprintf (stderr, "%s: Can't map %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 451 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 452 | exit (EXIT_FAILURE); |
| 453 | } |
| 454 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 455 | /* Setup the image header as per input image type*/ |
| 456 | if (tparams->set_header) |
| 457 | tparams->set_header (ptr, &sbuf, ifd, ¶ms); |
| 458 | else { |
| 459 | fprintf (stderr, "%s: Can't set header for %s: %s\n", |
| 460 | params.cmdname, tparams->name, strerror(errno)); |
| 461 | exit (EXIT_FAILURE); |
| 462 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 463 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 464 | /* Print the image information by processing image header */ |
| 465 | if (tparams->print_header) |
| 466 | tparams->print_header (ptr); |
| 467 | else { |
| 468 | fprintf (stderr, "%s: Can't print header for %s: %s\n", |
| 469 | params.cmdname, tparams->name, strerror(errno)); |
| 470 | exit (EXIT_FAILURE); |
| 471 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 472 | |
| 473 | (void) munmap((void *)ptr, sbuf.st_size); |
| 474 | |
| 475 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | fabf3cf | 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 (close(ifd)) { |
| 486 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 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 | |
| 491 | exit (EXIT_SUCCESS); |
| 492 | } |
| 493 | |
| 494 | static void |
| 495 | copy_file (int ifd, const char *datafile, int pad) |
| 496 | { |
| 497 | int dfd; |
| 498 | struct stat sbuf; |
| 499 | unsigned char *ptr; |
| 500 | int tail; |
| 501 | int zero = 0; |
| 502 | int offset = 0; |
| 503 | int size; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 504 | struct image_type_params *tparams = mkimage_get_type (params.type); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 505 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 506 | if (params.vflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 507 | fprintf (stderr, "Adding Image %s\n", datafile); |
| 508 | } |
| 509 | |
wdenk | 1ee550e | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 510 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 511 | fprintf (stderr, "%s: Can't open %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 512 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 513 | exit (EXIT_FAILURE); |
| 514 | } |
| 515 | |
| 516 | if (fstat(dfd, &sbuf) < 0) { |
| 517 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 518 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 519 | exit (EXIT_FAILURE); |
| 520 | } |
| 521 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 522 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 523 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 524 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 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 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 529 | if (params.xflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 530 | unsigned char *p = NULL; |
| 531 | /* |
| 532 | * XIP: do not append the image_header_t at the |
| 533 | * beginning of the file, but consume the space |
| 534 | * reserved for it. |
| 535 | */ |
| 536 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 537 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 538 | fprintf (stderr, |
| 539 | "%s: Bad size: \"%s\" is too small for XIP\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 540 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 541 | exit (EXIT_FAILURE); |
| 542 | } |
| 543 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 544 | for (p = ptr; p < ptr + tparams->header_size; p++) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 545 | if ( *p != 0xff ) { |
| 546 | fprintf (stderr, |
| 547 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 548 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 549 | exit (EXIT_FAILURE); |
| 550 | } |
| 551 | } |
| 552 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 553 | offset = tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | size = sbuf.st_size - offset; |
| 557 | if (write(ifd, ptr + offset, size) != size) { |
| 558 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 559 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 560 | exit (EXIT_FAILURE); |
| 561 | } |
| 562 | |
| 563 | if (pad && ((tail = size % 4) != 0)) { |
| 564 | |
| 565 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { |
| 566 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 567 | params.cmdname, params.imagefile, |
| 568 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 569 | exit (EXIT_FAILURE); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | (void) munmap((void *)ptr, sbuf.st_size); |
| 574 | (void) close (dfd); |
| 575 | } |
| 576 | |
| 577 | void |
| 578 | usage () |
| 579 | { |
| 580 | fprintf (stderr, "Usage: %s -l image\n" |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 581 | " -l ==> list image header information\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 582 | params.cmdname); |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 583 | fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp " |
| 584 | "-a addr -e ep -n name -d data_file[:data_file...] image\n" |
| 585 | " -A ==> set architecture to 'arch'\n" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 586 | " -O ==> set operating system to 'os'\n" |
| 587 | " -T ==> set image type to 'type'\n" |
| 588 | " -C ==> set compression type 'comp'\n" |
| 589 | " -a ==> set load address to 'addr' (hex)\n" |
| 590 | " -e ==> set entry point to 'ep' (hex)\n" |
| 591 | " -n ==> set image name to 'name'\n" |
| 592 | " -d ==> use image data from 'datafile'\n" |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 593 | " -x ==> set XIP (execute in place)\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 594 | params.cmdname); |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 595 | fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 596 | params.cmdname); |
Wolfgang Denk | e97ab72 | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 597 | fprintf (stderr, " %s -V ==> print version information and exit\n", |
| 598 | params.cmdname); |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 599 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 600 | exit (EXIT_FAILURE); |
| 601 | } |