From 09ef8c96cfb4f18c9c3262d13caf12afc7d542c8 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Tue, 1 Jun 2004 15:09:13 +0000 Subject: [PATCH] allow to read big endian on little endian machine --- tools/Makefile | 6 ++-- tools/emile-first-info.c | 25 ++++++++------- tools/emile-first-tune.c | 15 +++++---- tools/emile-set-cmdline.c | 3 +- tools/emile.h | 67 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 tools/emile.h diff --git a/tools/Makefile b/tools/Makefile index cfe96fe..0f7c90d 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -10,11 +10,11 @@ CFLAGS = -Wall all: $(PROGRAMS) -emile-set-cmdline.o: emile-set-cmdline.c emile-first.h +emile-set-cmdline.o: emile-set-cmdline.c emile-first.h emile.h -emile-first-info.o: emile-first-info.c emile-first.h +emile-first-info.o: emile-first-info.c emile-first.h emile.h -emile-first-tune.o: emile-first-tune.c emile-first.h +emile-first-tune.o: emile-first-tune.c emile-first.h emile.h clean: rm -f *.o $(PROGRAMS) diff --git a/tools/emile-first-info.c b/tools/emile-first-info.c index 8081173..5cfe292 100644 --- a/tools/emile-first-info.c +++ b/tools/emile-first-info.c @@ -11,6 +11,7 @@ #include #include +#include "emile.h" #include "emile-first.h" static void usage(int argc, char** argv) @@ -50,11 +51,11 @@ int first_info(char* image) printf("Boot Block Header:\n\n"); printf("Boot blocks signature: 0x%x\n", - firstBlock.boot_block_header.ID); + read_short(&firstBlock.boot_block_header.ID)); printf("Entry point to bootcode: 0x%lX\n", - firstBlock.boot_block_header.Entry); + read_long(&firstBlock.boot_block_header.Entry)); printf("Boot blocks version number: %x\n", - firstBlock.boot_block_header.Version); + read_short(&firstBlock.boot_block_header.Version)); printf("System filename: "); pprint(firstBlock.boot_block_header.SysName); putchar('\n'); @@ -77,15 +78,15 @@ int first_info(char* image) pprint(firstBlock.boot_block_header.ScrapName); putchar('\n'); printf("Number of FCBs to allocate: %d\n", - firstBlock.boot_block_header.CntFCBs); + read_short(&firstBlock.boot_block_header.CntFCBs)); printf("Number of event queue elements: %d\n", - firstBlock.boot_block_header.CntEvts); + read_short(&firstBlock.boot_block_header.CntEvts)); printf("System heap size on 128K Mac: 0x%lx\n", - firstBlock.boot_block_header.Heap128K); + read_long(&firstBlock.boot_block_header.Heap128K)); printf("System heap size on 256K Mac: 0x%lx\n", - firstBlock.boot_block_header.Heap256K); + read_long(&firstBlock.boot_block_header.Heap256K)); printf("System heap size on all machines: 0x%lx\n", - firstBlock.boot_block_header.SysHeapSize); + read_long(&firstBlock.boot_block_header.SysHeapSize)); if ( strncmp( firstBlock.boot_block_header.SysName+1, "Mac Bootloader", 14) == 0 ) @@ -93,13 +94,13 @@ int first_info(char* image) printf("\nEMILE boot block identified\n\n"); printf("Drive number: %d\n", - firstBlock.second_param_block.ioVRefNum); + read_short(&firstBlock.second_param_block.ioVRefNum)); printf("File reference number: %d\n", - firstBlock.second_param_block.ioRefNum); + read_short(&firstBlock.second_param_block.ioRefNum)); printf("Second level size: %ld\n", - firstBlock.second_param_block.ioReqCount); + read_long(&firstBlock.second_param_block.ioReqCount)); printf("Second level offset: %ld\n", - firstBlock.second_param_block.ioPosOffset); + read_long(&firstBlock.second_param_block.ioPosOffset)); } close(fd); diff --git a/tools/emile-first-tune.c b/tools/emile-first-tune.c index f5e3b9f..ffa22cf 100644 --- a/tools/emile-first-tune.c +++ b/tools/emile-first-tune.c @@ -12,6 +12,7 @@ #include #include +#include "emile.h" #include "emile-first.h" #define TUNE_DRIVE 0x0001 @@ -58,31 +59,31 @@ int first_tune( char* image, unsigned short tune_mask, int drive_num, if (tune_mask & TUNE_DRIVE) { printf("Set drive number to %d\n", drive_num); - firstBlock.second_param_block.ioVRefNum = drive_num; + write_short(&firstBlock.second_param_block.ioVRefNum, drive_num); } if (tune_mask & TUNE_OFFSET) { printf("Set second level offset to %d\n", second_offset); - firstBlock.second_param_block.ioPosOffset = second_offset; + write_long(&firstBlock.second_param_block.ioPosOffset, second_offset); } if (tune_mask & TUNE_SIZE) { printf("Set second level size to %d\n", second_size); - firstBlock.second_param_block.ioReqCount = second_size; + write_long(&firstBlock.second_param_block.ioReqCount, second_size); } if (tune_mask == 0) { printf("Drive number: %d\n", - firstBlock.second_param_block.ioVRefNum); + read_short(&firstBlock.second_param_block.ioVRefNum)); printf("File reference number: %d\n", - firstBlock.second_param_block.ioRefNum); + read_short(&firstBlock.second_param_block.ioRefNum)); printf("Second level size: %ld\n", - firstBlock.second_param_block.ioReqCount); + read_long(&firstBlock.second_param_block.ioReqCount)); printf("Second level offset: %ld\n", - firstBlock.second_param_block.ioPosOffset); + read_long(&firstBlock.second_param_block.ioPosOffset)); } else { diff --git a/tools/emile-set-cmdline.c b/tools/emile-set-cmdline.c index 5f95277..989eaa4 100644 --- a/tools/emile-set-cmdline.c +++ b/tools/emile-set-cmdline.c @@ -12,6 +12,7 @@ #include #include +#include "emile.h" #include "emile-first.h" #include "emile-second.h" @@ -68,7 +69,7 @@ int set_cmdline(int readonly, char* image, char* cmdline) return 7; } - if (EMILE_001_SIGNATURE != header.signature) + if (EMILE_001_SIGNATURE != read_long(&header.signature)) { fprintf(stderr, "Bad Header signature\n"); return 8; diff --git a/tools/emile.h b/tools/emile.h new file mode 100644 index 0000000..55e5353 --- /dev/null +++ b/tools/emile.h @@ -0,0 +1,67 @@ +/* + * + * (c) 2004 Laurent Vivier + * + */ + +#include + +#if __BYTE_ORDER == __BIG_ENDIAN + +/* nothing to do, because m68k is big endian too */ + +static inline unsigned short read_short(unsigned short* addr) +{ + return *addr; +} + +static inline void write_short(unsigned short* addr, unsigned short value) +{ + *addr = value; +} + +static inline unsigned long read_long(unsigned long* addr) +{ + return *addr; +} + +static inline void write_long(unsigned long* addr, unsigned long value) +{ + *addr = value; +} + +#else /* __BYTE_ORDER == __LITTLE_ENDIAN */ + +/* little endian (or unknown), read byte by byte to get it in good order */ + +static inline unsigned short read_short(unsigned short* addr) +{ + unsigned char* baddr = (unsigned char*)addr; + + return ((unsigned short)(*baddr) << 8) | (unsigned short)*(baddr+1); +} + +static inline void write_short(unsigned short* addr, unsigned short value) +{ + unsigned char* baddr = (unsigned char*)addr; + + *baddr = (unsigned char)(value>>8); + *(baddr+1) = (unsigned char)value; +} + +static inline unsigned long read_long(unsigned long* addr) +{ + unsigned short* saddr = (unsigned short*)addr; + + return ((unsigned long)read_short(saddr) << 16) | + (unsigned long)read_short(saddr+1);; +} + +static inline void write_long(unsigned long* addr, unsigned long value) +{ + unsigned short* saddr = (unsigned short*)addr; + + write_short(saddr, (unsigned short)(value>>16)); + write_short(saddr+1, (unsigned short)value); +} +#endif