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