/*
* A simple program to manipulate flat files
*
- * Copyright (C) 2001 SnapGear Inc, davidm@snapgear.com
+ * Copyright (C) 2001-2003 SnapGear Inc, davidm@snapgear.com
* Copyright (C) 2001 Lineo, davidm@lineo.com
+ *
+ * This is Free Software, under the GNU Public Licence v2 or greater.
+ *
*/
/****************************************************************************/
#include <stdio.h> /* Userland pieces of the ANSI C standard I/O package */
#include <unistd.h> /* Userland prototypes of the Unix std system calls */
+#include <time.h>
+#include <stdlib.h> /* exit() */
+#include <string.h> /* strcat(), strcpy() */
/* macros for conversion between host and (internet) network byte order */
+#ifndef WIN32
#include <netinet/in.h> /* Consts and structs defined by the internet system */
+#define BINARY_FILE_OPTS
+#else
+#include <winsock2.h>
+#define BINARY_FILE_OPTS "b"
+#endif
/* from uClinux-x.x.x/include/linux */
#include "flat.h" /* Binary flat header description */
+#if defined(__MINGW32__)
+#include <getopt.h>
+
+#define mkstemp(p) mktemp(p)
+
+#endif
+
/****************************************************************************/
char *program_name;
static char cmd[1024];
-static int print = 0, compress = 0, ramload = 0, stacksize = 0;
+static int print = 0, compress = 0, ramload = 0, stacksize = 0, ktrace = 0;
static int short_format = 0;
/****************************************************************************/
{
int old_flags, old_stack, new_flags, new_stack;
FILE *ifp, *ofp;
+ int ofp_is_pipe = 0;
struct flat_hdr old_hdr, new_hdr;
char tfile[256];
char tfile2[256];
*tfile = *tfile2 = '\0';
- if ((ifp = fopen(ifile, "r")) == NULL) {
+ if ((ifp = fopen(ifile, "r" BINARY_FILE_OPTS)) == NULL) {
fprintf(stderr, "Cannot open %s\n", ifile);
return;
}
else if (ramload < 0)
new_flags &= ~FLAT_FLAG_RAM;
+ if (ktrace > 0)
+ new_flags |= FLAT_FLAG_KTRACE;
+ else if (ktrace < 0)
+ new_flags &= ~FLAT_FLAG_KTRACE;
+
if (stacksize)
new_stack = stacksize;
if (print == 1) {
+ time_t t;
+
printf("%s\n", ifile);
printf(" Magic: %4.4s\n", old_hdr.magic);
printf(" Rev: %d\n", ntohl(old_hdr.rev));
+ t = (time_t) htonl(old_hdr.build_date);
+ printf(" Build Date: %s", t?ctime(&t):"not specified\n");
printf(" Entry: 0x%x\n", ntohl(old_hdr.entry));
printf(" Data Start: 0x%x\n", ntohl(old_hdr.data_start));
printf(" Data End: 0x%x\n", ntohl(old_hdr.data_end));
printf("Gzip-Compressed ");
if (old_flags & FLAT_FLAG_GZDATA)
printf("Gzip-Data-Compressed ");
+ if (old_flags & FLAT_FLAG_KTRACE)
+ printf("Kernel-Traced-Load ");
printf(")\n");
}
} else if (print > 1) {
first = 0;
}
*tfile = '\0';
+ strcat(tfile, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
strcat(tfile, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
strcat(tfile, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
strcat(tfile, (old_flags & FLAT_FLAG_GZIP) ? "z" :
strcpy(tfile, "/tmp/flatXXXXXX");
mkstemp(tfile);
- if ((ofp = fopen(tfile, "w")) == NULL) {
+ if ((ofp = fopen(tfile, "w" BINARY_FILE_OPTS)) == NULL) {
fprintf(stderr, "Failed to open %s for writing\n", tfile);
unlink(tfile);
unlink(tfile2);
mkstemp(tfile2);
if (old_flags & FLAT_FLAG_GZDATA) {
- tfp = fopen(tfile2, "w");
+ tfp = fopen(tfile2, "w" BINARY_FILE_OPTS);
if (!tfp) {
fprintf(stderr, "Failed to open %s for writing\n", tfile2);
exit(1);
}
sprintf(cmd, "gunzip >> %s", tfile2);
- tfp = popen(cmd, "w");
+ tfp = popen(cmd, "w" BINARY_FILE_OPTS);
+ if(!tfp) {
+ perror("popen");
+ exit(1);
+ }
transferr(ifp, tfp, -1);
- fclose(tfp);
+ pclose(tfp);
fclose(ifp);
- ifp = fopen(tfile2, "r");
+ ifp = fopen(tfile2, "r" BINARY_FILE_OPTS);
if (!ifp) {
fprintf(stderr, "Failed to open %s for reading\n", tfile2);
unlink(tfile);
printf("zflat %s --> %s\n", ifile, ofile);
fclose(ofp);
sprintf(cmd, "gzip -9 -f >> %s", tfile);
- ofp = popen(cmd, "w");
+ ofp = popen(cmd, "w" BINARY_FILE_OPTS);
+ ofp_is_pipe = 1;
} else if (new_flags & FLAT_FLAG_GZDATA) {
printf("zflat-data %s --> %s\n", ifile, ofile);
transferr(ifp, ofp, ntohl(new_hdr.data_start) -
sizeof(struct flat_hdr));
fclose(ofp);
sprintf(cmd, "gzip -9 -f >> %s", tfile);
- ofp = popen(cmd, "w");
+ ofp = popen(cmd, "w" BINARY_FILE_OPTS);
+ ofp_is_pipe = 1;
}
if (!ofp) { /* can only happen if using gzip/gunzip */
}
fclose(ifp);
- fclose(ofp);
+ if (ofp_is_pipe)
+ pclose(ofp);
+ else
+ fclose(ofp);
/* cheat a little here to preserve file permissions */
sprintf(cmd, "cp %s %s", tfile, ofile);
fprintf(stderr, " -Z : un-compressed flat file\n");
fprintf(stderr, " -r : ram load\n");
fprintf(stderr, " -R : do not RAM load\n");
+ fprintf(stderr, " -k : kernel traced load (for debug)\n");
+ fprintf(stderr, " -K : normal non-kernel traced load\n");
fprintf(stderr, " -s size : stack size\n");
fprintf(stderr, " -o file : output-file\n"
" (default is to modify input file)\n");
program_name = argv[0];
- while ((c = getopt(argc, argv, "pdzZrRs:o:")) != EOF) {
+ while ((c = getopt(argc, argv, "pdzZrRkKs:o:")) != EOF) {
switch (c) {
case 'p': print = 1; break;
case 'z': compress = 1; break;
case 'Z': compress = -1; break;
case 'r': ramload = 1; break;
case 'R': ramload = -1; break;
+ case 'k': ktrace = 1; break;
+ case 'K': ktrace = -1; break;
case 's': stacksize = atoi(optarg); break;
case 'o': ofile = optarg; break;
default: