#include <sys/stat.h>
#include <endian.h> /* for __BYTE_ORDER */
+#define FALSE 0
+#define TRUE 1
+
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
# define HOST_TO_LE16(x) (x)
# define HOST_TO_LE32(x) (x)
+# define HOST_TO_BE16(x) bswap_16(x)
+# define HOST_TO_BE32(x) bswap_32(x)
#else
# define HOST_TO_LE16(x) bswap_16(x)
# define HOST_TO_LE32(x) bswap_32(x)
+# define HOST_TO_BE16(x) (x)
+# define HOST_TO_BE32(x) (x)
#endif
struct header
};
static char *progname;
+static int force_be = FALSE;
static void usage(int status)
{
" -i <file> read input from file <file>\n"
" -o <file> write output to file <file>\n"
" -f <flash> set flash address to <flash>\n"
- " -S <start> set start address to <start>\n");
+ " -S <start> set start address to <start>\n"
+ " -b big-endianness mode\n");
exit(status);
}
int i;
unsigned short ret = 0;
- for (i = 0; i < buf->size / 2; i++)
- ret -= ((unsigned short *) buf->start)[i];
+ for (i = 0; i < buf->size / 2; i++) {
+ if (force_be == FALSE)
+ ret -= ((unsigned short *) buf->start)[i];
+ else
+ ret -= HOST_TO_BE16(((unsigned short *) buf->start)[i]);
+ }
return ret;
}
header.flash = header.size = header.start = 0;
progname = basename(argv[0]);
- while((c = getopt(argc, argv, "i:o:m:s:f:S:h")) != -1) {
+ while((c = getopt(argc, argv, "i:o:m:s:f:S:h:b")) != -1) {
switch (c) {
case 'i':
ifinfo.name = optarg;
usage(EXIT_FAILURE);
}
break;
+ case 'b':
+ force_be = TRUE;
+ break;
default:
usage(EXIT_FAILURE);
break;
if (fwread(&ifinfo, &ibuf))
usage(EXIT_FAILURE);
- header.flash = HOST_TO_LE32(header.flash);
- header.size = HOST_TO_LE32(obuf.size - sizeof(struct header));
- header.start = HOST_TO_LE32(header.start);
+ if (force_be == FALSE) {
+ header.flash = HOST_TO_LE32(header.flash);
+ header.size = HOST_TO_LE32(obuf.size - sizeof(struct header));
+ header.start = HOST_TO_LE32(header.start);
+ } else {
+ header.flash = HOST_TO_BE32(header.flash);
+ header.size = HOST_TO_BE32(obuf.size - sizeof(struct header));
+ header.start = HOST_TO_BE32(header.start);
+ }
+
memcpy (obuf.start, &header, sizeof(struct header));
- csum = HOST_TO_LE16(fwcsum(&ibuf));
+ if (force_be == FALSE)
+ csum = HOST_TO_LE16(fwcsum(&ibuf));
+ else
+ csum = HOST_TO_BE16(fwcsum(&ibuf));
+
memcpy(obuf.start + obuf.size - sizeof(unsigned short),
&csum, sizeof(unsigned short));