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