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