-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
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]
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;
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 */
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 */
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)
// 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 = "";
}
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) {
}
}
printf("\n");
-#endif
}
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)*/
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 = "????";
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);
}
int i;
int fd;
DPME * data;
- long t;
data = (DPME *) malloc(PBLOCK_SIZE);
if (data == NULL) {
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) {
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) {
dump(name);
}
-#endif
free(data);
}
"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 ",
"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);
*/
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;
#include <stdarg.h>
#include "errors.h"
-#include "pdisk.h"
+#include "hfdisk.h"
//
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
}
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);
}
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));
}
{
long base;
long length;
- long mult;
char *name;
char *type_name;
void
do_reorder(partition_map_header *map)
{
- partition_map * cur;
long old_index;
long index;
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
-#ifndef __linux__
-#include <SCSI.h>
-#else
-#ifdef __GLIBC__
#include <sys/types.h>
-#endif
-#endif
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
-#include "pdisk.h"
+#include "hfdisk.h"
#include "io.h"
#include "errors.h"
#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
//
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
//
int c;
flush_to_newline(0);
- printf(prompt);
+ printf("%s", prompt);
for (;;) {
c = getch();
return 0;
} else {
flush_to_newline(0);
- printf(prompt);
+ printf("%s", prompt);
}
}
return -1;
int c;
if (promptBeforeGet) {
- printf(prompt);
+ printf("%s", prompt);
}
for (;;) {
c = getch();
} else if (c == ' ' || c == '\t') {
// skip blanks and tabs
} else if (c == '\n') {
- printf(prompt);
+ printf("%s", prompt);
} else {
*command = c;
return 1;
// skip blanks and tabs
} else if (c == '\n') {
if (default_value < 0) {
- printf(prompt);
+ printf("%s", prompt);
} else {
ungetch(c);
*number = default_value;
// skip blanks and tabs
} else if (c == '\n') {
if (reprompt) {
- printf(prompt);
+ printf("%s", prompt);
} else {
ungetch(c);
*string = NULL;
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");
}
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");
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);
- }
}
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
+
+#ifdef __linux__
#include <linux/fs.h> // 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
//
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) {
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) {
}
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();
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,
{
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) {
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) {
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");
* 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 */