Skip to content

Commit

Permalink
Add "packed" attribute to structures loaded from disk.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcmaster-au committed May 28, 2014
1 parent 6c85bfa commit b40203e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 228 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions dpme.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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;


Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand All @@ -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)
Expand Down
63 changes: 18 additions & 45 deletions dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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) {
Expand All @@ -134,7 +133,6 @@ dump_block_zero(partition_map_header *map)
}
}
printf("\n");
#endif
}


Expand All @@ -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)*/

Expand Down Expand Up @@ -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 = "????";
Expand All @@ -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);
}

Expand Down Expand Up @@ -312,7 +292,6 @@ list_all_disks()
int i;
int fd;
DPME * data;
long t;

data = (DPME *) malloc(PBLOCK_SIZE);
if (data == NULL) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -356,7 +330,6 @@ list_all_disks()

dump(name);
}
#endif
free(data);
}

Expand Down Expand Up @@ -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 ",
Expand All @@ -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);
Expand All @@ -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;
Expand Down
26 changes: 3 additions & 23 deletions errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <stdarg.h>

#include "errors.h"
#include "pdisk.h"
#include "hfdisk.h"


//
Expand Down Expand Up @@ -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
}


Expand Down Expand Up @@ -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);
}
Expand All @@ -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));
}
2 changes: 0 additions & 2 deletions hfdisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
Loading

0 comments on commit b40203e

Please sign in to comment.