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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 11 | #include "mkimage.h" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 12 | #include <image.h> |
Wolfgang Denk | 976b38c | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 13 | #include <version.h> |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 14 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 15 | static void copy_file(int, const char *, int); |
| 16 | static void usage(void); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 17 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 18 | /* parameters initialized by core will be used by the image type code */ |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 19 | struct image_tool_params params = { |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 20 | .os = IH_OS_LINUX, |
| 21 | .arch = IH_ARCH_PPC, |
| 22 | .type = IH_TYPE_KERNEL, |
| 23 | .comp = IH_COMP_GZIP, |
| 24 | .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS, |
Wolfgang Denk | 04387d2 | 2010-03-27 23:37:46 +0100 | [diff] [blame] | 25 | .imagename = "", |
Shaohui Xie | 5d898a0 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 26 | .imagename2 = "", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 27 | }; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 28 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 29 | int |
| 30 | main (int argc, char **argv) |
| 31 | { |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 32 | int ifd = -1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 33 | struct stat sbuf; |
Peter Tyser | a2513e2 | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 34 | char *ptr; |
Prafulla Wadaskar | 449609f | 2009-08-16 05:28:19 +0530 | [diff] [blame] | 35 | int retval = 0; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 36 | struct image_type_params *tparams = NULL; |
Stefano Babic | 9bac0bb | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 37 | int pad_len = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 38 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 39 | params.cmdname = *argv; |
| 40 | params.addr = params.ep = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 41 | |
| 42 | while (--argc > 0 && **++argv == '-') { |
| 43 | while (*++*argv) { |
| 44 | switch (**argv) { |
| 45 | case 'l': |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 46 | params.lflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 47 | break; |
| 48 | case 'A': |
| 49 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 50 | (params.arch = |
| 51 | genimg_get_arch_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 52 | usage (); |
| 53 | goto NXTARG; |
Simon Glass | 4f61042 | 2013-06-13 15:10:06 -0700 | [diff] [blame] | 54 | case 'c': |
| 55 | if (--argc <= 0) |
| 56 | usage(); |
| 57 | params.comment = *++argv; |
| 58 | goto NXTARG; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 59 | case 'C': |
| 60 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 61 | (params.comp = |
| 62 | genimg_get_comp_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 63 | usage (); |
| 64 | goto NXTARG; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 65 | case 'D': |
| 66 | if (--argc <= 0) |
| 67 | usage (); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 68 | params.dtc = *++argv; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 69 | goto NXTARG; |
| 70 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 71 | case 'O': |
| 72 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 73 | (params.os = |
| 74 | genimg_get_os_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 75 | usage (); |
| 76 | goto NXTARG; |
| 77 | case 'T': |
| 78 | if ((--argc <= 0) || |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 79 | (params.type = |
| 80 | genimg_get_type_id (*++argv)) < 0) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 81 | usage (); |
| 82 | goto NXTARG; |
| 83 | |
| 84 | case 'a': |
| 85 | if (--argc <= 0) |
| 86 | usage (); |
Peter Tyser | a2513e2 | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 87 | params.addr = strtoul (*++argv, &ptr, 16); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 88 | if (*ptr) { |
| 89 | fprintf (stderr, |
| 90 | "%s: invalid load address %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 91 | params.cmdname, *argv); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 92 | exit (EXIT_FAILURE); |
| 93 | } |
| 94 | goto NXTARG; |
| 95 | case 'd': |
| 96 | if (--argc <= 0) |
| 97 | usage (); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 98 | params.datafile = *++argv; |
| 99 | params.dflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 100 | goto NXTARG; |
| 101 | case 'e': |
| 102 | if (--argc <= 0) |
| 103 | usage (); |
Peter Tyser | a2513e2 | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 104 | params.ep = strtoul (*++argv, &ptr, 16); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 105 | if (*ptr) { |
| 106 | fprintf (stderr, |
| 107 | "%s: invalid entry point %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 108 | params.cmdname, *argv); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 109 | exit (EXIT_FAILURE); |
| 110 | } |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 111 | params.eflag = 1; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 112 | goto NXTARG; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 113 | case 'f': |
| 114 | if (--argc <= 0) |
| 115 | usage (); |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 116 | params.datafile = *++argv; |
| 117 | /* no break */ |
| 118 | case 'F': |
Peter Tyser | 1a99de2 | 2009-11-24 16:42:08 -0600 | [diff] [blame] | 119 | /* |
| 120 | * The flattened image tree (FIT) format |
| 121 | * requires a flattened device tree image type |
| 122 | */ |
| 123 | params.type = IH_TYPE_FLATDT; |
Peter Tyser | fbc1c8f | 2009-12-06 01:33:24 -0600 | [diff] [blame] | 124 | params.fflag = 1; |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 125 | goto NXTARG; |
Simon Glass | 80e4df8 | 2013-06-13 15:10:03 -0700 | [diff] [blame] | 126 | case 'k': |
| 127 | if (--argc <= 0) |
| 128 | usage(); |
| 129 | params.keydir = *++argv; |
| 130 | goto NXTARG; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 131 | case 'K': |
| 132 | if (--argc <= 0) |
| 133 | usage(); |
| 134 | params.keydest = *++argv; |
| 135 | goto NXTARG; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 136 | case 'n': |
| 137 | if (--argc <= 0) |
| 138 | usage (); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 139 | params.imagename = *++argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 140 | goto NXTARG; |
Simon Glass | 399c744 | 2013-06-13 15:10:07 -0700 | [diff] [blame] | 141 | case 'r': |
| 142 | params.require_keys = 1; |
| 143 | break; |
Shaohui Xie | 5d898a0 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 144 | case 'R': |
| 145 | if (--argc <= 0) |
| 146 | usage(); |
| 147 | /* |
| 148 | * This entry is for the second configuration |
| 149 | * file, if only one is not enough. |
| 150 | */ |
| 151 | params.imagename2 = *++argv; |
| 152 | goto NXTARG; |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 153 | case 's': |
| 154 | params.skipcpy = 1; |
| 155 | break; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 156 | case 'v': |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 157 | params.vflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 158 | break; |
Wolfgang Denk | 976b38c | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 159 | case 'V': |
| 160 | printf("mkimage version %s\n", PLAIN_VERSION); |
| 161 | exit(EXIT_SUCCESS); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 162 | case 'x': |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 163 | params.xflag++; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 164 | break; |
| 165 | default: |
| 166 | usage (); |
| 167 | } |
| 168 | } |
| 169 | NXTARG: ; |
| 170 | } |
| 171 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 172 | if (argc != 1) |
| 173 | usage (); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 174 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 175 | /* set tparams as per input type_id */ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 176 | tparams = imagetool_get_type(params.type); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 177 | if (tparams == NULL) { |
| 178 | fprintf (stderr, "%s: unsupported type %s\n", |
| 179 | params.cmdname, genimg_get_type_name(params.type)); |
| 180 | exit (EXIT_FAILURE); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * check the passed arguments parameters meets the requirements |
| 185 | * as per image type to be generated/listed |
| 186 | */ |
| 187 | if (tparams->check_params) |
| 188 | if (tparams->check_params (¶ms)) |
| 189 | usage (); |
| 190 | |
| 191 | if (!params.eflag) { |
| 192 | params.ep = params.addr; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 193 | /* If XIP, entry point must be after the U-Boot header */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 194 | if (params.xflag) |
| 195 | params.ep += tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 198 | params.imagefile = *argv; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 199 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 200 | if (params.fflag){ |
| 201 | if (tparams->fflag_handle) |
| 202 | /* |
| 203 | * in some cases, some additional processing needs |
| 204 | * to be done if fflag is defined |
| 205 | * |
| 206 | * For ex. fit_handle_file for Fit file support |
| 207 | */ |
| 208 | retval = tparams->fflag_handle(¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 209 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 210 | if (retval != EXIT_SUCCESS) |
| 211 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Peter Tyser | c81296c | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 214 | if (params.lflag || params.fflag) { |
| 215 | ifd = open (params.imagefile, O_RDONLY|O_BINARY); |
| 216 | } else { |
| 217 | ifd = open (params.imagefile, |
| 218 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); |
| 219 | } |
| 220 | |
| 221 | if (ifd < 0) { |
| 222 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 223 | params.cmdname, params.imagefile, |
| 224 | strerror(errno)); |
| 225 | exit (EXIT_FAILURE); |
| 226 | } |
| 227 | |
| 228 | if (params.lflag || params.fflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 229 | /* |
| 230 | * list header information of existing image |
| 231 | */ |
| 232 | if (fstat(ifd, &sbuf) < 0) { |
| 233 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 234 | params.cmdname, params.imagefile, |
| 235 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 236 | exit (EXIT_FAILURE); |
| 237 | } |
| 238 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 239 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 240 | fprintf (stderr, |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 241 | "%s: Bad size: \"%s\" is not valid image\n", |
| 242 | params.cmdname, params.imagefile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 243 | exit (EXIT_FAILURE); |
| 244 | } |
| 245 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 246 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); |
| 247 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 248 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 249 | params.cmdname, params.imagefile, |
| 250 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 251 | exit (EXIT_FAILURE); |
| 252 | } |
| 253 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 254 | /* |
| 255 | * scan through mkimage registry for all supported image types |
| 256 | * and verify the input image file header for match |
| 257 | * Print the image information for matched image type |
| 258 | * Returns the error code if not matched |
| 259 | */ |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 260 | retval = imagetool_verify_print_header(ptr, &sbuf, |
| 261 | tparams, ¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 262 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 263 | (void) munmap((void *)ptr, sbuf.st_size); |
| 264 | (void) close (ifd); |
| 265 | |
Prafulla Wadaskar | f7644c0 | 2009-08-10 18:49:37 +0530 | [diff] [blame] | 266 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 270 | * In case there an header with a variable |
| 271 | * length will be added, the corresponding |
| 272 | * function is called. This is responsible to |
| 273 | * allocate memory for the header itself. |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 274 | */ |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 275 | if (tparams->vrec_header) |
Stefano Babic | 9bac0bb | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 276 | pad_len = tparams->vrec_header(¶ms, tparams); |
Stefano Babic | f066210 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 277 | else |
| 278 | memset(tparams->hdr, 0, tparams->header_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 279 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 280 | if (write(ifd, tparams->hdr, tparams->header_size) |
| 281 | != tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 282 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 283 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 284 | exit (EXIT_FAILURE); |
| 285 | } |
| 286 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 287 | if (!params.skipcpy) { |
| 288 | if (params.type == IH_TYPE_MULTI || |
| 289 | params.type == IH_TYPE_SCRIPT) { |
| 290 | char *file = params.datafile; |
| 291 | uint32_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 292 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 293 | for (;;) { |
| 294 | char *sep = NULL; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 295 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 296 | if (file) { |
| 297 | if ((sep = strchr(file, ':')) != NULL) { |
| 298 | *sep = '\0'; |
| 299 | } |
| 300 | |
| 301 | if (stat (file, &sbuf) < 0) { |
| 302 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 303 | params.cmdname, file, strerror(errno)); |
| 304 | exit (EXIT_FAILURE); |
| 305 | } |
| 306 | size = cpu_to_uimage (sbuf.st_size); |
| 307 | } else { |
| 308 | size = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 311 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
| 312 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 313 | params.cmdname, params.imagefile, |
| 314 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 315 | exit (EXIT_FAILURE); |
| 316 | } |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 317 | |
| 318 | if (!file) { |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | if (sep) { |
| 323 | *sep = ':'; |
| 324 | file = sep + 1; |
| 325 | } else { |
| 326 | file = NULL; |
| 327 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 330 | file = params.datafile; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 331 | |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 332 | for (;;) { |
| 333 | char *sep = strchr(file, ':'); |
| 334 | if (sep) { |
| 335 | *sep = '\0'; |
| 336 | copy_file (ifd, file, 1); |
| 337 | *sep++ = ':'; |
| 338 | file = sep; |
| 339 | } else { |
| 340 | copy_file (ifd, file, 0); |
| 341 | break; |
| 342 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 343 | } |
Shaohui Xie | 5d898a0 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 344 | } else if (params.type == IH_TYPE_PBLIMAGE) { |
| 345 | /* PBL has special Image format, implements its' own */ |
| 346 | pbl_load_uboot(ifd, ¶ms); |
Christian Riesch | d1be8f9 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 347 | } else { |
Stefano Babic | 9bac0bb | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 348 | copy_file(ifd, params.datafile, pad_len); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 349 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 353 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 354 | !defined(__sun__) && \ |
| 355 | !defined(__FreeBSD__) && \ |
Luka Perkov | 31cbe80 | 2014-08-09 18:17:47 +0200 | [diff] [blame] | 356 | !defined(__OpenBSD__) && \ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 357 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 358 | (void) fdatasync (ifd); |
| 359 | #else |
| 360 | (void) fsync (ifd); |
| 361 | #endif |
| 362 | |
| 363 | if (fstat(ifd, &sbuf) < 0) { |
| 364 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 365 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 366 | exit (EXIT_FAILURE); |
| 367 | } |
| 368 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 369 | ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); |
| 370 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 371 | fprintf (stderr, "%s: Can't map %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 372 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 373 | exit (EXIT_FAILURE); |
| 374 | } |
| 375 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 376 | /* Setup the image header as per input image type*/ |
| 377 | if (tparams->set_header) |
| 378 | tparams->set_header (ptr, &sbuf, ifd, ¶ms); |
| 379 | else { |
| 380 | fprintf (stderr, "%s: Can't set header for %s: %s\n", |
| 381 | params.cmdname, tparams->name, strerror(errno)); |
| 382 | exit (EXIT_FAILURE); |
| 383 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 384 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 385 | /* Print the image information by processing image header */ |
| 386 | if (tparams->print_header) |
| 387 | tparams->print_header (ptr); |
| 388 | else { |
| 389 | fprintf (stderr, "%s: Can't print header for %s: %s\n", |
| 390 | params.cmdname, tparams->name, strerror(errno)); |
| 391 | exit (EXIT_FAILURE); |
| 392 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 393 | |
| 394 | (void) munmap((void *)ptr, sbuf.st_size); |
| 395 | |
| 396 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 397 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 398 | !defined(__sun__) && \ |
| 399 | !defined(__FreeBSD__) && \ |
Luka Perkov | 31cbe80 | 2014-08-09 18:17:47 +0200 | [diff] [blame] | 400 | !defined(__OpenBSD__) && \ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 401 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 402 | (void) fdatasync (ifd); |
| 403 | #else |
| 404 | (void) fsync (ifd); |
| 405 | #endif |
| 406 | |
| 407 | if (close(ifd)) { |
| 408 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 409 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 410 | exit (EXIT_FAILURE); |
| 411 | } |
| 412 | |
| 413 | exit (EXIT_SUCCESS); |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | copy_file (int ifd, const char *datafile, int pad) |
| 418 | { |
| 419 | int dfd; |
| 420 | struct stat sbuf; |
| 421 | unsigned char *ptr; |
| 422 | int tail; |
| 423 | int zero = 0; |
Stefano Babic | 9bac0bb | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 424 | uint8_t zeros[4096]; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 425 | int offset = 0; |
| 426 | int size; |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 427 | struct image_type_params *tparams = imagetool_get_type(params.type); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 428 | |
Stefano Babic | 9bac0bb | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 429 | if (pad >= sizeof(zeros)) { |
| 430 | fprintf(stderr, "%s: Can't pad to %d\n", |
| 431 | params.cmdname, pad); |
| 432 | exit(EXIT_FAILURE); |
| 433 | } |
| 434 | |
| 435 | memset(zeros, 0, sizeof(zeros)); |
| 436 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 437 | if (params.vflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 438 | fprintf (stderr, "Adding Image %s\n", datafile); |
| 439 | } |
| 440 | |
wdenk | ef1464c | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 441 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 442 | fprintf (stderr, "%s: Can't open %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 443 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 444 | exit (EXIT_FAILURE); |
| 445 | } |
| 446 | |
| 447 | if (fstat(dfd, &sbuf) < 0) { |
| 448 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 449 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 450 | exit (EXIT_FAILURE); |
| 451 | } |
| 452 | |
Mike Frysinger | fa956fd | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 453 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 454 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 455 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 456 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 457 | exit (EXIT_FAILURE); |
| 458 | } |
| 459 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 460 | if (params.xflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 461 | unsigned char *p = NULL; |
| 462 | /* |
| 463 | * XIP: do not append the image_header_t at the |
| 464 | * beginning of the file, but consume the space |
| 465 | * reserved for it. |
| 466 | */ |
| 467 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 468 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 469 | fprintf (stderr, |
| 470 | "%s: Bad size: \"%s\" is too small for XIP\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 471 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 472 | exit (EXIT_FAILURE); |
| 473 | } |
| 474 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 475 | for (p = ptr; p < ptr + tparams->header_size; p++) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 476 | if ( *p != 0xff ) { |
| 477 | fprintf (stderr, |
| 478 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 479 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 480 | exit (EXIT_FAILURE); |
| 481 | } |
| 482 | } |
| 483 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 484 | offset = tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | size = sbuf.st_size - offset; |
| 488 | if (write(ifd, ptr + offset, size) != size) { |
| 489 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 490 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 491 | exit (EXIT_FAILURE); |
| 492 | } |
| 493 | |
Stefano Babic | 9bac0bb | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 494 | tail = size % 4; |
| 495 | if ((pad == 1) && (tail != 0)) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 496 | |
| 497 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { |
| 498 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 499 | params.cmdname, params.imagefile, |
| 500 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 501 | exit (EXIT_FAILURE); |
| 502 | } |
Stefano Babic | 9bac0bb | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 503 | } else if (pad > 1) { |
| 504 | if (write(ifd, (char *)&zeros, pad) != pad) { |
| 505 | fprintf(stderr, "%s: Write error on %s: %s\n", |
| 506 | params.cmdname, params.imagefile, |
| 507 | strerror(errno)); |
| 508 | exit(EXIT_FAILURE); |
| 509 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | (void) munmap((void *)ptr, sbuf.st_size); |
| 513 | (void) close (dfd); |
| 514 | } |
| 515 | |
Guilherme Maciel Ferreira | f1cc458 | 2013-12-01 12:43:09 -0700 | [diff] [blame] | 516 | static void usage(void) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 517 | { |
| 518 | fprintf (stderr, "Usage: %s -l image\n" |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 519 | " -l ==> list image header information\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 520 | params.cmdname); |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 521 | fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp " |
| 522 | "-a addr -e ep -n name -d data_file[:data_file...] image\n" |
| 523 | " -A ==> set architecture to 'arch'\n" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 524 | " -O ==> set operating system to 'os'\n" |
| 525 | " -T ==> set image type to 'type'\n" |
| 526 | " -C ==> set compression type 'comp'\n" |
| 527 | " -a ==> set load address to 'addr' (hex)\n" |
| 528 | " -e ==> set entry point to 'ep' (hex)\n" |
| 529 | " -n ==> set image name to 'name'\n" |
| 530 | " -d ==> use image data from 'datafile'\n" |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 531 | " -x ==> set XIP (execute in place)\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 532 | params.cmdname); |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 533 | fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 534 | params.cmdname); |
Simon Glass | 80e4df8 | 2013-06-13 15:10:03 -0700 | [diff] [blame] | 535 | fprintf(stderr, " -D => set options for device tree compiler\n" |
| 536 | " -f => input filename for FIT source\n"); |
| 537 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | 399c744 | 2013-06-13 15:10:07 -0700 | [diff] [blame] | 538 | fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n" |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 539 | " -k => set directory containing private keys\n" |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 540 | " -K => write public keys to this .dtb file\n" |
Simon Glass | 4f61042 | 2013-06-13 15:10:06 -0700 | [diff] [blame] | 541 | " -c => add comment in signature node\n" |
Simon Glass | 399c744 | 2013-06-13 15:10:07 -0700 | [diff] [blame] | 542 | " -F => re-sign existing FIT image\n" |
| 543 | " -r => mark keys used as 'required' in dtb\n"); |
Simon Glass | 80e4df8 | 2013-06-13 15:10:03 -0700 | [diff] [blame] | 544 | #else |
| 545 | fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n"); |
| 546 | #endif |
Wolfgang Denk | 976b38c | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 547 | fprintf (stderr, " %s -V ==> print version information and exit\n", |
| 548 | params.cmdname); |
Bartlomiej Sieka | 9d25438 | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 549 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 550 | exit (EXIT_FAILURE); |
| 551 | } |