diff --git a/Makefile b/Makefile index 1c5b275..10e4fdb 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS=-std=gnu99 +CFLAGS=-std=gnu99 -Wall all: hfdisk hfdisk: hfdisk.o dump.o partition_map.o convert.o io.o errors.o bitfield.o diff --git a/dpme.h b/dpme.h index 8ff858c..b3f7ab4 100644 --- a/dpme.h +++ b/dpme.h @@ -69,7 +69,7 @@ struct Block0 { uint32_t sbData; /* not used */ uint16_t sbDrvrCount; /* driver descriptor count */ uint16_t sbMap[247]; /* descriptor map */ -}; +} __attribute__((packed)); typedef struct Block0 Block0; // Where &sbMap[0] is actually an array DDMap[sbDrvrCount] @@ -78,7 +78,7 @@ struct DDMap { uint32_t ddBlock; /* 1st driver's starting block */ uint16_t ddSize; /* size of 1st driver (512-byte blks) */ uint16_t ddType; /* system type (1 for Mac+) */ -}; +} __attribute__((packed)); typedef struct DDMap DDMap; @@ -116,7 +116,7 @@ struct dpme { char dpme_process_id[16] ; uint32_t dpme_boot_args[32] ; uint32_t dpme_reserved_3[62] ; -}; +} __attribute__((packed)); typedef struct dpme DPME; #define dpme_automount_set(p, v) bitfield_set(&p->dpme_flags, 30, 1, v) /* MSch */ @@ -150,13 +150,13 @@ struct abm /* altblk map info stored in bzb */ uint32_t abm_size; /* size of map in bytes */ uint32_t abm_ents; /* number of used entries */ uint32_t abm_start; /* start of altblk map */ -}; +} __attribute__((packed)); typedef struct abm ABM; // BZB (Block Zero Block, but I can't remember the etymology) // Where &dpme_boot_args[0] is actually the address of a struct bzb // kludge to get around alignment junk -struct bzb /* block zero block format */ +struct bzb /* block zero block format */ { uint32_t bzb_magic; /* magic number */ uint8_t bzb_cluster; /* Autorecovery cluster grouping */ @@ -177,7 +177,7 @@ struct bzb /* block zero block format */ ABM bzb_abm; /* altblk map info */ uint32_t bzb_fill2[7]; /* for expansion of ABM (ha!ha!) */ uint8_t bzb_mount_point[64]; /* default mount point name */ -}; +} __attribute__((packed)); typedef struct bzb BZB; #define bzb_root_set(p, v) bitfield_set(&p->bzb_flags, 31, 1, v) diff --git a/dump.c b/dump.c index 77c2b49..7e9560f 100644 --- a/dump.c +++ b/dump.c @@ -61,16 +61,16 @@ typedef struct names { // Global Constants // NAMES plist[] = { - "Drvr", "Apple_Driver", - "Dr43", "Apple_Driver43", - "Free", "Apple_Free", - " HFS", "Apple_HFS", - " MFS", "Apple_MFS", - "PDOS", "Apple_PRODOS", - "junk", "Apple_Scratch", - "unix", "Apple_UNIX_SVR2", - " map", "Apple_partition_map", - 0, 0 + {"Drvr", "Apple_Driver"}, + {"Dr43", "Apple_Driver43"}, + {"Free", "Apple_Free"}, + {" HFS", "Apple_HFS"}, + {" MFS", "Apple_MFS"}, + {"PDOS", "Apple_PRODOS"}, + {"junk", "Apple_Scratch"}, + {"unix", "Apple_UNIX_SVR2"}, + {" map", "Apple_partition_map"}, + {0, 0} }; const char * kStringEmpty = ""; @@ -122,7 +122,6 @@ dump_block_zero(partition_map_header *map) } printf("\nBlock size=%u, Number of Blocks=%u\n", p->sbBlkSize, p->sbBlkCount); -#ifndef __mc68000__ printf("DeviceType=0x%x, DeviceId=0x%x\n", p->sbDevType, p->sbDevId); if (p->sbDrvrCount > 0) { @@ -134,7 +133,6 @@ dump_block_zero(partition_map_header *map) } } printf("\n"); -#endif } @@ -150,23 +148,16 @@ dump_partition_map(partition_map_header *map, int disk_order) bad_input("No partition map exists"); return; } -#ifdef __mc68000__ - printf("Disk %s\n", map->name); -#else printf("%s\n", map->name); -#endif j = number_of_digits(map->media_size); if (j < 7) { j = 7; } -#ifdef __mc68000__ - printf("%*s type name " - "%*s %-*s ( size ) system\n", strlen(map->name)+1, "#", j, "length", j, "base"); -#else + // convert size_t to int for "*" format specifier + int mapNameWidth = strlen(map->name)+1; printf("%*s type name " - "%*s %-*s ( size ) system\n", strlen(map->name)+1, "#", j, "length", j, "base"); -#endif + "%*s %-*s ( size ) system\n", mapNameWidth, "#", j, "length", j, "base"); /* Grok devfs names. (courtesy Colin Walters)*/ @@ -196,22 +187,15 @@ dump_partition_map(partition_map_header *map, int disk_order) void dump_partition_entry(partition_map *entry, int digits, char *dev) { - partition_map_header *map; int j; DPME *p; - BZB *bp; char *s; -#ifdef __mc68000__ - int aflag = 1; -#else int aflag = 0; -#endif int pflag = 1; uint32_t size; double bytes; - map = entry->the_map; p = entry->data; if (aflag) { s = "????"; @@ -221,13 +205,9 @@ dump_partition_entry(partition_map *entry, int digits, char *dev) break; } } -#ifdef __mc68000__ - printf("%s%-2d %.4s %-12.12s ", dev, entry->disk_address, s, p->dpme_name); -#else - printf("%s%-4d %.4s %-18.32s ", dev, entry->disk_address, s, p->dpme_name); -#endif + printf("%s%-4ld %.4s %-18.32s ", dev, entry->disk_address, s, p->dpme_name); } else { - printf("%s%-4d %20.32s %-18.32s ", dev, + printf("%s%-4ld %20.32s %-18.32s ", dev, entry->disk_address, p->dpme_type, p->dpme_name); } @@ -312,7 +292,6 @@ list_all_disks() int i; int fd; DPME * data; - long t; data = (DPME *) malloc(PBLOCK_SIZE); if (data == NULL) { @@ -322,13 +301,9 @@ list_all_disks() for (i = 0; i < 7; i++) { sprintf(name, "/dev/sd%c", 'a'+i); if ((fd = open_device(name, O_RDONLY)) < 0) { -#ifdef __linux__ if (errno == EACCES) { error(errno, "can't open file '%s'", name); } -#else - error(errno, "can't open file '%s'", name); -#endif continue; } if (read_block(fd, 1, (char *)data, 1) == 0) { @@ -339,7 +314,6 @@ list_all_disks() dump(name); } -#ifdef __linux__ for (i = 0; i < 4; i++) { sprintf(name, "/dev/hd%c", 'a'+i); if ((fd = open_device(name, O_RDONLY)) < 0) { @@ -356,7 +330,6 @@ list_all_disks() dump(name); } -#endif free(data); } @@ -423,7 +396,7 @@ uint32_t dpme_reserved_3[62] ; "flags (logical)\n"); for (entry = map->disk_order; entry != NULL; entry = entry->next_on_disk) { p = entry->data; - printf("%2d: %20.32s ", + printf("%2ld: %20.32s ", entry->disk_address, p->dpme_type); printf("%7u @ %-7u ", p->dpme_pblocks, p->dpme_pblock_start); printf("%c%c%c%c%c%c%c%c%c%c ", @@ -447,7 +420,7 @@ uint32_t dpme_reserved_3[62] ; "goto_address checksum processor\n"); for (entry = map->disk_order; entry != NULL; entry = entry->next_on_disk) { p = entry->data; - printf("%2d: ", entry->disk_address); + printf("%2ld: ", entry->disk_address); printf("%7u ", p->dpme_boot_block); printf("%7u ", p->dpme_boot_bytes); printf("%8x ", p->dpme_load_addr); @@ -464,7 +437,7 @@ xx: cccc RU *dd s... */ for (entry = map->disk_order; entry != NULL; entry = entry->next_on_disk) { p = entry->data; - printf("%2d: ", entry->disk_address); + printf("%2ld: ", entry->disk_address); bp = (BZB *) (p->dpme_bzb); j = -1; diff --git a/errors.c b/errors.c index af4d9b8..184a957 100644 --- a/errors.c +++ b/errors.c @@ -31,7 +31,7 @@ #include #include "errors.h" -#include "pdisk.h" +#include "hfdisk.h" // @@ -66,15 +66,11 @@ char *program_name; void init_program_name(char **argv) { -#ifdef __linux__ if ((program_name = strrchr(argv[0], '/')) != (char *)NULL) { program_name++; } else { program_name = argv[0]; } -#else - program_name = "pdisk"; -#endif } @@ -112,15 +108,7 @@ fatal(int value, char *fmt, ...) vfprintf(stderr, fmt, ap); va_end(ap); -#ifdef __linux__ - if (value > 0 && value < sys_nerr) { - fprintf(stderr, " (%s)\n", sys_errlist[value]); - } else { - fprintf(stderr, "\n"); - } -#else - fprintf(stderr, "\n"); -#endif + fprintf(stderr, " (%s)\n", strerror(value)); exit(value); } @@ -141,13 +129,5 @@ error(int value, char *fmt, ...) vfprintf(stderr, fmt, ap); va_end(ap); -#ifdef __linux__ - if (value > 0 && value < sys_nerr) { - fprintf(stderr, " (%s)\n", sys_errlist[value]); - } else { - fprintf(stderr, "\n"); - } -#else - fprintf(stderr, "\n"); -#endif + fprintf(stderr, " (%s)\n", strerror(value)); } diff --git a/hfdisk.c b/hfdisk.c index cb030a1..9b0aba0 100644 --- a/hfdisk.c +++ b/hfdisk.c @@ -331,7 +331,6 @@ do_create_partition(partition_map_header *map, int get_type) { long base; long length; - long mult; char *name; char *type_name; @@ -493,7 +492,6 @@ do_delete_partition(partition_map_header *map) void do_reorder(partition_map_header *map) { - partition_map * cur; long old_index; long index; diff --git a/io.c b/io.c index 01158a9..6297030 100644 --- a/io.c +++ b/io.c @@ -28,19 +28,13 @@ #include #include #include -#ifndef __linux__ -#include -#else -#ifdef __GLIBC__ #include -#endif -#endif #include #include #include #include -#include "pdisk.h" +#include "hfdisk.h" #include "io.h" #include "errors.h" @@ -51,14 +45,6 @@ #define BAD_DIGIT 17 /* must be greater than any base */ #define STRING_CHUNK 16 #define UNGET_MAX_COUNT 10 -#ifndef __linux__ -#define SCSI_FD 8 -#define loff_t long -#define llseek lseek -#else -#define llseek lseek64 -#endif - // // Types @@ -83,11 +69,6 @@ int unget_count; // long get_number(int first_char); char* get_string(int eos); -#ifndef __linux__ -int DoTestUnitReady(UInt8 targetID); -int DoRead(UInt8 targetID, UInt32 block, UInt16 count, char* addr); -int DoWrite(UInt8 targetID, UInt32 block, UInt16 count, char* addr); -#endif // @@ -147,7 +128,7 @@ get_okay(char *prompt, int default_value) int c; flush_to_newline(0); - printf(prompt); + printf("%s", prompt); for (;;) { c = getch(); @@ -165,7 +146,7 @@ get_okay(char *prompt, int default_value) return 0; } else { flush_to_newline(0); - printf(prompt); + printf("%s", prompt); } } return -1; @@ -178,7 +159,7 @@ get_command(char *prompt, int promptBeforeGet, int *command) int c; if (promptBeforeGet) { - printf(prompt); + printf("%s", prompt); } for (;;) { c = getch(); @@ -188,7 +169,7 @@ get_command(char *prompt, int promptBeforeGet, int *command) } else if (c == ' ' || c == '\t') { // skip blanks and tabs } else if (c == '\n') { - printf(prompt); + printf("%s", prompt); } else { *command = c; return 1; @@ -213,7 +194,7 @@ get_number_argument(char *prompt, long *number, long default_value) // skip blanks and tabs } else if (c == '\n') { if (default_value < 0) { - printf(prompt); + printf("%s", prompt); } else { ungetch(c); *number = default_value; @@ -291,7 +272,7 @@ get_string_argument(char *prompt, char **string, int reprompt) // skip blanks and tabs } else if (c == '\n') { if (reprompt) { - printf(prompt); + printf("%s", prompt); } else { ungetch(c); *string = NULL; @@ -430,19 +411,12 @@ bad_input(char *fmt, ...) int read_block(int fd, unsigned long num, char *buf, int quiet) { - loff_t x; + off_t x; long t; -#ifndef __linux__ - if (fd <= SCSI_FD) { - //printf("Read block %d of scsi device %d\n", num, fd); - return DoRead(fd, num, 1, buf); - } else { -#else { -#endif x = ((long long) num * PBLOCK_SIZE); /* cast to ll to work around compiler bug */ - if ((x = lseek64(fd, x, 0)) < 0) { + if ((x = lseek(fd, x, 0)) < 0) { if (quiet == 0) { error(errno, "Can't seek on file"); } @@ -462,21 +436,14 @@ read_block(int fd, unsigned long num, char *buf, int quiet) int write_block(int fd, unsigned long num, char *buf) { - loff_t x; + off_t x; long t; if (rflag) { - printf("Can't write block %u to file", num); + printf("Can't write block %lu to file", num); return 0; } -#ifndef __linux__ - if (fd <= SCSI_FD) { - //printf("Write block %d of scsi device %d\n", num, fd); - return DoWrite(fd, num, 1, buf); - } else { -#else { -#endif x = num * PBLOCK_SIZE; if ((x = lseek(fd, x, 0)) < 0) { error(errno, "Can't seek on file"); @@ -494,42 +461,12 @@ write_block(int fd, unsigned long num, char *buf) int close_device(int fildes) { -#ifndef __linux__ - if (fildes <= SCSI_FD) { - //printf("Close of scsi device %d\n", fildes); - return 1; - } else { -#else - { -#endif return close(fildes); - } } int open_device(const char *path, int oflag) { -#ifndef __linux__ - int id; - int fd; - DeviceIdent scsiDevice; - - if (strncmp("/dev/sd", path, 7) == 0 - && path[7] >= 'a' && path[7] <= 'g' - && path[8] == 0) { - id = path[7] - 'a'; - //printf("Open scsi device %d\n", id); - - if (DoTestUnitReady(id) > 0) { - return id; - } else { - return -1; - } - } else { -#else - { -#endif return open(path, oflag); - } } diff --git a/partition_map.c b/partition_map.c index 5ad3de7..d191df9 100644 --- a/partition_map.c +++ b/partition_map.c @@ -34,26 +34,18 @@ #include #include #include + +#ifdef __linux__ #include // For IOCTLs +#endif #include "partition_map.h" -#include "pdisk.h" +#include "hfdisk.h" #include "convert.h" #include "io.h" #include "errors.h" -// -// Defines -// -// #define TEST_COMPUTE - - -// -// Types -// - - // // Global Constants // @@ -103,10 +95,7 @@ open_partition_map(char *name, int *valid_file) int fd; partition_map_header * map; int writeable; - unsigned long length; -#ifdef __linux__ struct stat info; -#endif fd = open_device(name, (rflag)?O_RDONLY:O_RDWR); if (fd < 0) { @@ -139,16 +128,12 @@ open_partition_map(char *name, int *valid_file) map->maximum_in_map = -1; map->media_size = compute_device_size(fd); -#ifdef __linux__ if (fstat(fd, &info) < 0) { error(errno, "can't stat file '%s'", name); map->regular_file = 0; } else { map->regular_file = S_ISREG(info.st_mode); } -#else - map->regular_file = 0; -#endif map->misc = (Block0 *) malloc(PBLOCK_SIZE); if (map->misc == NULL) { @@ -287,11 +272,11 @@ write_partition_map(partition_map_header *map) } printf("The partition map has been saved successfully!\n\n"); -#ifdef __linux__ if (map->regular_file) { close_device(map->fd); } else { - // printf("Calling ioctl() to re-read partition table.\n"); +#ifdef BLKFLSBUF + printf("Calling ioctl() to re-read partition table.\n"); if ((i = ioctl(fd, BLKFLSBUF)) != 0) { perror("ioctl(BLKFLSBUF)"); sync(); @@ -326,10 +311,12 @@ write_partition_map(partition_map_header *map) printf("Reboot your system to ensure the " "partition table is updated.\n"); } - } #else - close_device(map->fd); + printf("Reboot your system to ensure the " + "partition table is updated.\n"); + close_device(map->fd); #endif + } map->fd = open_device(map->name, (map->writeable)?O_RDWR:O_RDONLY); if (map->fd < 0) { fatal(errno, "can't re-open file '%s' for %sing", map->name, @@ -400,13 +387,9 @@ create_partition_map(char *name) { int fd; partition_map_header * map; - unsigned long length; DPME *data; - int ok; unsigned long number; -#ifdef __linux__ struct stat info; -#endif fd = open_device(name, (rflag)?O_RDONLY:O_RDWR); if (fd < 0) { @@ -431,25 +414,21 @@ create_partition_map(char *name) map->maximum_in_map = -1; number = compute_device_size(fd); - printf("size of 'device' is %u blocks: ", number); + printf("size of 'device' is %lu blocks: ", number); flush_to_newline(0); get_number_argument("what should be the size? ", (long *)&number, number); if (number < 4) { number = 4; } - printf("new size of 'device' is %u blocks\n", number); + printf("new size of 'device' is %lu blocks\n", number); map->media_size = number; -#ifdef __linux__ if (fstat(fd, &info) < 0) { error(errno, "can't stat file '%s'", name); map->regular_file = 0; } else { map->regular_file = S_ISREG(info.st_mode); } -#else - map->regular_file = 0; -#endif map->misc = (Block0 *) malloc(PBLOCK_SIZE); if (map->misc == NULL) { @@ -660,52 +639,10 @@ renumber_disk_addresses(partition_map_header *map) long compute_device_size(int fd) { -#ifdef TEST_COMPUTE - unsigned long length; - struct hd_geometry geometry; - struct stat info; - loff_t pos; -#endif char* data; unsigned long l, r, x; int valid; -#ifdef TEST_COMPUTE - printf("\n"); - if (fstat(fd, &info) < 0) { - printf("stat of device failed\n"); - } else { - printf("stat: mode = 0%o, type=%s\n", info.st_mode, - (S_ISREG(info.st_mode)? "Regular": - (S_ISBLK(info.st_mode)?"Block":"Other"))); - printf("size = %d, blocks = %d\n", - info.st_size, info.st_size/PBLOCK_SIZE); - } - - if (ioctl(fd, BLKGETSIZE, &length) < 0) { - printf("get device size failed\n"); - } else { - printf("BLKGETSIZE:size in blocks = %u\n", length); - } - - if (ioctl(fd, HDIO_GETGEO, &geometry) < 0) { - printf("get device geometry failed\n"); - } else { - printf("HDIO_GETGEO: heads=%d, sectors=%d, cylinders=%d, start=%d, total=%d\n", - geometry.heads, geometry.sectors, - geometry.cylinders, geometry.start, - geometry.heads*geometry.sectors*geometry.cylinders); - } - - if ((pos = lseek64(fd, 0, SEEK_END)) < 0) { - printf("llseek to end of device failed\n"); - } else if ((pos = lseek64(fd, 0, SEEK_CUR)) < 0) { - printf("llseek to end of device failed on second try\n"); - } else { - printf("llseek: pos = %d, blocks=%d\n", pos, pos/PBLOCK_SIZE); - } -#endif - data = (char *) malloc(PBLOCK_SIZE); if (data == NULL) { error(errno, "can't allocate memory for try buffer"); diff --git a/version.h b/version.h index 8a3ac25..e42ca5e 100644 --- a/version.h +++ b/version.h @@ -45,8 +45,8 @@ * version will be be represented by a larger number. */ -#define VERSION "1.0-rc1" -#define RELEASE_DATE "11 September 2013" +#define VERSION "1.0-rc3" +#define RELEASE_DATE "28 May 2014" #define kVersionMajor 0x01 /* ie. N has two BCD digits */ #define kVersionMinor 0x0 /* ie. M has a single BCD digit */