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