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