2 * (C) Copyright 2000-2004
3 * DENX Software Engineering
4 * Wolfgang Denk, wd@denx.de
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #define MAP_FAILED (-1)
32 extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
33 static void copy_file (int, const char *, int);
34 static void usage (void);
45 int opt_os = IH_OS_LINUX;
46 int opt_arch = IH_ARCH_PPC;
47 int opt_type = IH_TYPE_KERNEL;
48 int opt_comp = IH_COMP_GZIP;
50 image_header_t header;
51 image_header_t *hdr = &header;
54 main (int argc, char **argv)
68 while (--argc > 0 && **++argv == '-') {
76 (opt_arch = genimg_get_arch_id (*++argv)) < 0)
81 (opt_comp = genimg_get_comp_id (*++argv)) < 0)
86 (opt_os = genimg_get_os_id (*++argv)) < 0)
91 (opt_type = genimg_get_type_id (*++argv)) < 0)
98 addr = strtoul (*++argv, (char **)&ptr, 16);
101 "%s: invalid load address %s\n",
115 ep = strtoul (*++argv, (char **)&ptr, 16);
118 "%s: invalid entry point %s\n",
142 if ((argc != 1) || ((lflag ^ dflag) == 0))
147 /* If XIP, entry point must be after the U-Boot header */
149 ep += image_get_header_size ();
153 * If XIP, ensure the entry point is equal to the load address plus
154 * the size of the U-Boot header.
157 if (ep != addr + image_get_header_size ()) {
159 "%s: For XIP, the entry point must be the load addr + %lu\n",
161 (unsigned long)image_get_header_size ());
169 ifd = open(imagefile, O_RDONLY|O_BINARY);
171 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
175 fprintf (stderr, "%s: Can't open %s: %s\n",
176 cmdname, imagefile, strerror(errno));
184 * list header information of existing image
186 if (fstat(ifd, &sbuf) < 0) {
187 fprintf (stderr, "%s: Can't stat %s: %s\n",
188 cmdname, imagefile, strerror(errno));
192 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
194 "%s: Bad size: \"%s\" is no valid image\n",
199 ptr = (unsigned char *)mmap(0, sbuf.st_size,
200 PROT_READ, MAP_SHARED, ifd, 0);
201 if ((caddr_t)ptr == (caddr_t)-1) {
202 fprintf (stderr, "%s: Can't read %s: %s\n",
203 cmdname, imagefile, strerror(errno));
208 * image_check_hcrc() creates copy of header so that
209 * we can blank out the checksum field for checking -
210 * this can't be done on the PROT_READ mapped data.
212 hdr = (image_header_t *)ptr;
214 if (!image_check_magic (hdr)) {
216 "%s: Bad Magic Number: \"%s\" is no valid image\n",
221 if (!image_check_hcrc (hdr)) {
223 "%s: ERROR: \"%s\" has bad header checksum!\n",
228 data = (char *)image_get_data (hdr);
229 len = sbuf.st_size - image_get_header_size ();
231 if (crc32(0, data, len) != image_get_dcrc (hdr)) {
233 "%s: ERROR: \"%s\" has corrupted data!\n",
238 /* for multi-file images we need the data part, too */
239 image_print_contents_noindent ((image_header_t *)ptr);
241 (void) munmap((void *)ptr, sbuf.st_size);
250 * write dummy header, to be fixed later
252 memset (hdr, 0, image_get_header_size ());
254 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
255 fprintf (stderr, "%s: Write error on %s: %s\n",
256 cmdname, imagefile, strerror(errno));
260 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
261 char *file = datafile;
268 if ((sep = strchr(file, ':')) != NULL) {
272 if (stat (file, &sbuf) < 0) {
273 fprintf (stderr, "%s: Can't stat %s: %s\n",
274 cmdname, file, strerror(errno));
277 size = cpu_to_uimage (sbuf.st_size);
282 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
283 fprintf (stderr, "%s: Write error on %s: %s\n",
284 cmdname, imagefile, strerror(errno));
303 char *sep = strchr(file, ':');
306 copy_file (ifd, file, 1);
310 copy_file (ifd, file, 0);
315 copy_file (ifd, datafile, 0);
318 /* We're a bit of paranoid */
319 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
320 (void) fdatasync (ifd);
325 if (fstat(ifd, &sbuf) < 0) {
326 fprintf (stderr, "%s: Can't stat %s: %s\n",
327 cmdname, imagefile, strerror(errno));
331 ptr = (unsigned char *)mmap(0, sbuf.st_size,
332 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
333 if (ptr == (unsigned char *)MAP_FAILED) {
334 fprintf (stderr, "%s: Can't map %s: %s\n",
335 cmdname, imagefile, strerror(errno));
339 hdr = (image_header_t *)ptr;
342 (const char *)(ptr + image_get_header_size ()),
343 sbuf.st_size - image_get_header_size ()
346 /* Build new header */
347 image_set_magic (hdr, IH_MAGIC);
348 image_set_time (hdr, sbuf.st_mtime);
349 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
350 image_set_load (hdr, addr);
351 image_set_ep (hdr, ep);
352 image_set_dcrc (hdr, checksum);
353 image_set_os (hdr, opt_os);
354 image_set_arch (hdr, opt_arch);
355 image_set_type (hdr, opt_type);
356 image_set_comp (hdr, opt_comp);
358 image_set_name (hdr, name);
360 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
362 image_set_hcrc (hdr, checksum);
364 image_print_contents_noindent (hdr);
366 (void) munmap((void *)ptr, sbuf.st_size);
368 /* We're a bit of paranoid */
369 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
370 (void) fdatasync (ifd);
376 fprintf (stderr, "%s: Write error on %s: %s\n",
377 cmdname, imagefile, strerror(errno));
385 copy_file (int ifd, const char *datafile, int pad)
396 fprintf (stderr, "Adding Image %s\n", datafile);
399 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
400 fprintf (stderr, "%s: Can't open %s: %s\n",
401 cmdname, datafile, strerror(errno));
405 if (fstat(dfd, &sbuf) < 0) {
406 fprintf (stderr, "%s: Can't stat %s: %s\n",
407 cmdname, datafile, strerror(errno));
411 ptr = (unsigned char *)mmap(0, sbuf.st_size,
412 PROT_READ, MAP_SHARED, dfd, 0);
413 if (ptr == (unsigned char *)MAP_FAILED) {
414 fprintf (stderr, "%s: Can't read %s: %s\n",
415 cmdname, datafile, strerror(errno));
420 unsigned char *p = NULL;
422 * XIP: do not append the image_header_t at the
423 * beginning of the file, but consume the space
427 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
429 "%s: Bad size: \"%s\" is too small for XIP\n",
434 for (p = ptr; p < ptr + image_get_header_size (); p++) {
437 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
443 offset = image_get_header_size ();
446 size = sbuf.st_size - offset;
447 if (write(ifd, ptr + offset, size) != size) {
448 fprintf (stderr, "%s: Write error on %s: %s\n",
449 cmdname, imagefile, strerror(errno));
453 if (pad && ((tail = size % 4) != 0)) {
455 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
456 fprintf (stderr, "%s: Write error on %s: %s\n",
457 cmdname, imagefile, strerror(errno));
462 (void) munmap((void *)ptr, sbuf.st_size);
469 fprintf (stderr, "Usage: %s -l image\n"
470 " -l ==> list image header information\n"
471 " %s [-x] -A arch -O os -T type -C comp "
472 "-a addr -e ep -n name -d data_file[:data_file...] image\n",
474 fprintf (stderr, " -A ==> set architecture to 'arch'\n"
475 " -O ==> set operating system to 'os'\n"
476 " -T ==> set image type to 'type'\n"
477 " -C ==> set compression type 'comp'\n"
478 " -a ==> set load address to 'addr' (hex)\n"
479 " -e ==> set entry point to 'ep' (hex)\n"
480 " -n ==> set image name to 'name'\n"
481 " -d ==> use image data from 'datafile'\n"
482 " -x ==> set XIP (execute in place)\n"