* (C) Copyright 2000-2004
* DENX Software Engineering
* Wolfgang Denk, wd@denx.de
- * All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
#include "mkimage.h"
#include <image.h>
+#include <u-boot/crc.h>
-extern int errno;
-
-#ifndef MAP_FAILED
-#define MAP_FAILED (-1)
-#endif
-
-extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
static void copy_file (int, const char *, int);
static void usage (void);
-static void image_verify_header (char *, int);
+static int image_verify_header (char *, int);
static void fit_handle_file (void);
char *datafile;
struct stat sbuf;
unsigned char *ptr;
char *name = "";
+ int retval = 0;
cmdname = *argv;
exit (EXIT_FAILURE);
}
- ptr = (unsigned char *)mmap(0, sbuf.st_size,
- PROT_READ, MAP_SHARED, ifd, 0);
- if ((caddr_t)ptr == (caddr_t)-1) {
+ ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
+ if (ptr == MAP_FAILED) {
fprintf (stderr, "%s: Can't read %s: %s\n",
cmdname, imagefile, strerror(errno));
exit (EXIT_FAILURE);
}
- if (fdt_check_header (ptr)) {
- /* old-style image */
- image_verify_header ((char *)ptr, sbuf.st_size);
- image_print_contents ((image_header_t *)ptr);
- } else {
+ if (!(retval = fdt_check_header (ptr))) {
/* FIT image */
fit_print_contents (ptr);
+ } else if (!(retval = image_verify_header ((char *)ptr,
+ sbuf.st_size))) {
+ /* old-style image */
+ image_print_contents ((image_header_t *)ptr);
}
(void) munmap((void *)ptr, sbuf.st_size);
(void) close (ifd);
- exit (EXIT_SUCCESS);
+ exit (retval);
} else if (fflag) {
/* Flattened Image Tree (FIT) format handling */
debug ("FIT format handling\n");
fit_handle_file ();
- exit (EXIT_SUCCESS);
+ exit (retval);
}
/*
exit (EXIT_FAILURE);
}
- ptr = (unsigned char *)mmap(0, sbuf.st_size,
- PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
- if (ptr == (unsigned char *)MAP_FAILED) {
+ ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
+ if (ptr == MAP_FAILED) {
fprintf (stderr, "%s: Can't map %s: %s\n",
cmdname, imagefile, strerror(errno));
exit (EXIT_FAILURE);
hdr = (image_header_t *)ptr;
checksum = crc32 (0,
- (const char *)(ptr + image_get_header_size ()),
+ (const unsigned char *)(ptr +
+ image_get_header_size ()),
sbuf.st_size - image_get_header_size ()
);
image_set_name (hdr, name);
- checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
+ checksum = crc32 (0, (const unsigned char *)hdr,
+ image_get_header_size ());
image_set_hcrc (hdr, checksum);
exit (EXIT_FAILURE);
}
- ptr = (unsigned char *)mmap(0, sbuf.st_size,
- PROT_READ, MAP_SHARED, dfd, 0);
- if (ptr == (unsigned char *)MAP_FAILED) {
+ ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
+ if (ptr == MAP_FAILED) {
fprintf (stderr, "%s: Can't read %s: %s\n",
cmdname, datafile, strerror(errno));
exit (EXIT_FAILURE);
exit (EXIT_FAILURE);
}
-static void
+static int
image_verify_header (char *ptr, int image_size)
{
int len;
- char *data;
+ const unsigned char *data;
uint32_t checksum;
image_header_t header;
image_header_t *hdr = &header;
*/
memcpy (hdr, ptr, sizeof(image_header_t));
- if (ntohl(hdr->ih_magic) != IH_MAGIC) {
+ if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
fprintf (stderr,
"%s: Bad Magic Number: \"%s\" is no valid image\n",
cmdname, imagefile);
- exit (EXIT_FAILURE);
+ return -FDT_ERR_BADMAGIC;
}
- data = (char *)hdr;
+ data = (const unsigned char *)hdr;
len = sizeof(image_header_t);
- checksum = ntohl(hdr->ih_hcrc);
- hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
+ checksum = be32_to_cpu(hdr->ih_hcrc);
+ hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
if (crc32 (0, data, len) != checksum) {
fprintf (stderr,
"%s: ERROR: \"%s\" has bad header checksum!\n",
cmdname, imagefile);
- exit (EXIT_FAILURE);
+ return -FDT_ERR_BADSTATE;
}
- data = ptr + sizeof(image_header_t);
+ data = (const unsigned char *)ptr + sizeof(image_header_t);
len = image_size - sizeof(image_header_t) ;
- if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
+ if (crc32 (0, data, len) != be32_to_cpu(hdr->ih_dcrc)) {
fprintf (stderr,
"%s: ERROR: \"%s\" has corrupted data!\n",
cmdname, imagefile);
- exit (EXIT_FAILURE);
+ return -FDT_ERR_BADSTRUCTURE;
}
+ return 0;
}
/**
exit (EXIT_FAILURE);
}
- ptr = (unsigned char *)mmap (0, sbuf.st_size,
- PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
- if ((caddr_t)ptr == (caddr_t)-1) {
+ ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
+ if (ptr == MAP_FAILED) {
fprintf (stderr, "%s: Can't read %s: %s\n",
cmdname, tmpfile, strerror(errno));
unlink (tmpfile);