From ed6aa2eca8a443e763d4fa6a528bcd40996bc18c Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Tue, 19 Apr 2011 07:39:53 -0700 Subject: [PATCH] add Intel's bin2obj tool - this is part of http://marcbug.scc-dc.com/svn/repository/trunk/linuxkernel/bin2obj --- tools/Makefile | 9 ++- tools/bin2obj.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 tools/bin2obj.c diff --git a/tools/Makefile b/tools/Makefile index e0375ae0..fb77c026 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -41,16 +41,19 @@ scc_bootinfo.asm: bootinfo.sh scc_bootinfo.bin: scc_bootinfo.asm $(NASM) $(NASMFLAGS) -o $@ $< -SCC: scc_bootinfo.bin scc_setup.bin reset_vector.bin initrd.img +bin2obj: bin2obj.c + $(CC) $(CFLAGS) -o $@ $< + +SCC: scc_bootinfo.bin scc_setup.bin reset_vector.bin initrd.img bin2obj cp ../metalsvm.elf . $(CROSS_OBJCOPY) -j .mboot -j .text -j .data -j .rodata -j .bss -O binary metalsvm.elf metalsvm.bin chmod a-x *.bin . ./prepare.sh - /home/lankes/tools/bin2obj -m load.map -o metalsvm.obj + ./bin2obj -m load.map -o metalsvm.obj sccMerge -noimage -m 8 -n 12 -force ./metalsvm.mt clean: - $(RM) -rf *.o *~ make_initrd initrd.img *.bin *.obj *.hex *.elf obj + $(RM) -rf *.o *~ bin2obj make_initrd initrd.img *.bin *.obj *.hex *.elf obj depend: $(CC) -MM $(CFLAGS) *.c > Makefile.dep diff --git a/tools/bin2obj.c b/tools/bin2obj.c new file mode 100644 index 00000000..45c07caa --- /dev/null +++ b/tools/bin2obj.c @@ -0,0 +1,147 @@ +#include +#include +#include + + +const char BIN2OBJIDSTRING[] = "$Id: bin2obj.c 8016 2007-11-01 14:24:42Z tlehnig $"; + + +long long convertToHex(char *fn, unsigned long origin, FILE *outfile) { + FILE *datafile; + unsigned char data1, data2, data3, data4; + int res = 0; + long long count = 0; + + datafile = fopen(fn, "r"); + if (!datafile) { + printf("Datafile >%s< could not be opened, not writing data for this file\n", fn); + return -1; + } + + printf("Converting file >%s< to .32.obj format at origin 0x%08lx (0x%08lx) ... ", + fn, origin >> 2, origin); + fprintf(outfile, "/origin %08lx\n", origin >> 2); + + do { + data1 = 0; + data2 = 0; + data3 = 0; + data4 = 0; + + res = fscanf(datafile, "%c%c%c%c", &data1, &data2, &data3, &data4); + if (res > 0) { + count += res; + + fprintf(outfile, "%02x%02x%02x%02x", data4, data3, data2, data1); + + if ((count % 16) == 0) + fprintf(outfile, "\n"); + else + fprintf(outfile, " "); + } + + } while (res > 0); + + if ((count % 16) != 0) fprintf(outfile, "\n"); + + printf("done with %lli Bytes.\n", count); + + fclose(datafile); + + return count; + +} + + +void print_help() { + printf("Usage: bin2obj [FLAGS] [OPTIONS]\n"); + printf("\nFLAGS: -h, -v\n"); + printf("-h Print this help\n"); + printf("-v Print Version ID\n"); + printf("\nOPTIONS: -m, -o\n"); + printf("-m Defines mapfile to use for bin2obj\n"); + printf("-o Defines output file to use for bin2obj\n"); + printf("\nbin2obj converts the binary files defined in the mapfile to a hex based textfile\n"); + printf("used by MCEMU\n"); +} + + +int main(int argc, char **argv) { + + FILE *mapfile = NULL, *outfile = NULL; + unsigned long origin; + char datafn[255]; + char outfn[255] = "output.obj"; + char mapfn[255] = "load.map"; + int res = 0; + unsigned long long count = 0; + long long thiscount = 0; + int retval = 0; + + int c, doOptLoop = 1; + + while (doOptLoop) { + c = getopt(argc, argv, "m:o:hv"); + + if (c == -1) { + doOptLoop = 0; + break; + } + + switch (c) { + case 'h': + print_help(); + return 0; + break; + case 'v': + printf("%s %s\n", argv[0], BIN2OBJIDSTRING); + return 0; + break; + case 'm': + printf("Mapfile: >%s<\n", optarg); + strncpy(mapfn, optarg, 255); + break; + case 'o': + printf("Outfile: >%s<\n", optarg); + strncpy(outfn, optarg, 255); + break; + default: + print_help(); + return 0; + } + } + + + mapfile = fopen(mapfn, "r"); + if (!mapfile) { + printf("Mapfile >%s< not found, exiting.\n", mapfn); + return -1; + } + + outfile = fopen(outfn, "w"); + if (!outfile) { + printf("Outputfile >%s< could not be created, exiting\n", outfn); + return -1; + } + + // res = fscanf(mapfile, "%lx %s\n", &origin, datafn); + + while ((res = fscanf(mapfile, "%lx %s\n", &origin, datafn)) == 2) { + //printf("ReadMapFile origin: 0x%08lx, filename: >%s<\n", origin, datafn); + + thiscount = convertToHex(datafn, origin, outfile); + if (thiscount < 0) { + retval = -1; + } + else count += thiscount; + + } + + fprintf(outfile, "/eof\n"); + fclose(mapfile); + fclose (outfile); + + printf("Total conversion: %lli Bytes\n", count); + + return retval; +}