diff options
author | 2016-01-25 23:43:20 +0000 | |
---|---|---|
committer | 2016-01-25 23:43:20 +0000 | |
commit | f7e12bbc164b488226e5adeffd8ebe9080487ad5 (patch) | |
tree | 97d4c708d7bf581ff6e4362af0b381d1c8357dcf | |
parent | Add read/write_block0 and read/write_dpme functions, not yet used. (diff) | |
download | wireguard-openbsd-f7e12bbc164b488226e5adeffd8ebe9080487ad5.tar.xz wireguard-openbsd-f7e12bbc164b488226e5adeffd8ebe9080487ad5.zip |
Use new read/write_block0 and read/write_dpme which handle any
endian or alignment issues forcefully. Removes need for convert_*
functions so unhook convert.c from build. read/write_block become
static functions inside file_media.c.
Tweak struct block0 to stop trying to handle alignment issues by clever
declaration. Rely on the new functions to accurately translate between
on-disk and in-memory layouts.
Enables pdisk to work on amd64 and likely other architectures if
it's ever desirable. Does bring back DEV_BSIZE to pdisk.c since the
in-memory structure will *not* be 512 bytes on other archs.
-rw-r--r-- | distrib/special/pdisk/Makefile | 4 | ||||
-rw-r--r-- | sbin/pdisk/Makefile | 4 | ||||
-rw-r--r-- | sbin/pdisk/dpme.h | 31 | ||||
-rw-r--r-- | sbin/pdisk/dump.c | 30 | ||||
-rw-r--r-- | sbin/pdisk/file_media.c | 21 | ||||
-rw-r--r-- | sbin/pdisk/file_media.h | 5 | ||||
-rw-r--r-- | sbin/pdisk/partition_map.c | 34 | ||||
-rw-r--r-- | sbin/pdisk/pdisk.c | 17 |
8 files changed, 53 insertions, 93 deletions
diff --git a/distrib/special/pdisk/Makefile b/distrib/special/pdisk/Makefile index 11bcb0eb098..daa42c4574b 100644 --- a/distrib/special/pdisk/Makefile +++ b/distrib/special/pdisk/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.8 2016/01/18 03:10:16 deraadt Exp $ +# $Id: Makefile,v 1.9 2016/01/25 23:43:20 krw Exp $ .if ${MACHINE} == "macppc" PROG= pdisk @@ -6,7 +6,7 @@ LDADD= -lutil DPADD= ${LIBUTIL} CFLAGS+=-Wall -SRCS= convert.c dump.c \ +SRCS= dump.c \ file_media.c io.c partition_map.c \ pdisk.c validate.c diff --git a/sbin/pdisk/Makefile b/sbin/pdisk/Makefile index bdef3eb32d9..5d0783710c1 100644 --- a/sbin/pdisk/Makefile +++ b/sbin/pdisk/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.21 2016/01/17 14:13:57 jasper Exp $ +# $Id: Makefile,v 1.22 2016/01/25 23:43:20 krw Exp $ .if ${MACHINE} == "macppc" PROG= pdisk @@ -6,7 +6,7 @@ LDADD= -lutil DPADD= ${LIBUTIL} CFLAGS+=-Wall -SRCS= convert.c dump.c \ +SRCS= dump.c \ file_media.c io.c partition_map.c \ pdisk.c validate.c diff --git a/sbin/pdisk/dpme.h b/sbin/pdisk/dpme.h index 98e77761339..e1e946a715e 100644 --- a/sbin/pdisk/dpme.h +++ b/sbin/pdisk/dpme.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dpme.h,v 1.20 2016/01/25 21:51:23 krw Exp $ */ +/* $OpenBSD: dpme.h,v 1.21 2016/01/25 23:43:20 krw Exp $ */ /* * dpme.h - Disk Partition Map Entry (dpme) @@ -47,33 +47,26 @@ #define DPISTRLEN 32 -struct block0 { - uint16_t sbSig; /* unique value for SCSI block 0 */ - uint16_t sbBlkSize; /* block size of device */ - uint32_t sbBlkCount; /* number of blocks on device */ - uint16_t sbDevType; /* device type */ - uint16_t sbDevId; /* device id */ - uint32_t sbData; /* not used */ - uint16_t sbDrvrCount; /* driver descriptor count */ - uint16_t sbMap[247]; /* descriptor map */ -}; - -/* - * Where &sbMap[0] is actually an array struct ddmap[sbDrvrCount] - * kludge to get around alignment junk - */ struct ddmap { uint32_t ddBlock; /* 1st driver's starting block (in sbBlkSize blocks!) */ uint16_t ddSize; /* size of 1st driver (512-byte blks) */ uint16_t ddType; /* system type (1 for Mac+) */ }; +struct block0 { + uint16_t sbSig; /* unique value for SCSI block 0 */ + uint16_t sbBlkSize; /* block size of device */ + uint32_t sbBlkCount; /* number of blocks on device */ + uint16_t sbDevType; /* device type */ + uint16_t sbDevId; /* device id */ + uint32_t sbData; /* not used */ + uint16_t sbDrvrCount; /* driver descriptor count */ + struct ddmap sbDDMap[8]; /* driver descriptor map*/ + uint8_t sbReserved[430]; +}; /* * Each partition map entry (blocks 1 through n) has this format - * - * Since dpme is assumed to be the same size as the physical sector size, - * support is limited to 512-byte sector devices! */ struct dpme { uint16_t dpme_signature; /* "PM" */ diff --git a/sbin/pdisk/dump.c b/sbin/pdisk/dump.c index 58aa072768a..1b5ee4d4585 100644 --- a/sbin/pdisk/dump.c +++ b/sbin/pdisk/dump.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dump.c,v 1.52 2016/01/25 21:51:23 krw Exp $ */ +/* $OpenBSD: dump.c,v 1.53 2016/01/25 23:43:20 krw Exp $ */ /* * dump.c - dumping partition maps @@ -61,11 +61,10 @@ dump_block_zero(struct partition_map_header *map) printf("\nDevice block size=%u, Number of Blocks=%u (%1.1f%c)\n", p->sbBlkSize, p->sbBlkCount, value, prefix); - printf("DeviceType=0x%x, DeviceId=0x%x\n", - p->sbDevType, p->sbDevId); + printf("DeviceType=0x%x, DeviceId=0x%x\n", p->sbDevType, p->sbDevId); if (p->sbDrvrCount > 0) { printf("Drivers-\n"); - m = (struct ddmap *)p->sbMap; + m = p->sbDDMap; for (i = 0; i < p->sbDrvrCount; i++) { printf("%d: %3u @ %u, ", i + 1, m[i].ddSize, m[i].ddBlock); @@ -202,7 +201,7 @@ show_data_structures(struct partition_map_header *map) } else { printf("%u driver%s-\n", zp->sbDrvrCount, (zp->sbDrvrCount > 1) ? "s" : ""); - m = (struct ddmap *)zp->sbMap; + m = zp->sbDDMap; for (i = 0; i < zp->sbDrvrCount; i++) { printf("%u: @ %u for %u, type=0x%x\n", i + 1, m[i].ddBlock, m[i].ddSize, m[i].ddType); @@ -243,8 +242,8 @@ show_data_structures(struct partition_map_header *map) printf("%2ld: ", entry->disk_address); printf("%7u ", p->dpme_boot_block); printf("%7u ", p->dpme_boot_bytes); - printf("%8x ", (uint32_t) p->dpme_load_addr); - printf("%8x ", (uint32_t) p->dpme_goto_addr); + printf("%8x ", p->dpme_load_addr); + printf("%8x ", p->dpme_goto_addr); printf("%8x ", p->dpme_checksum); printf("%.32s", p->dpme_processor_id); printf("\n"); @@ -308,14 +307,11 @@ full_dump_partition_entry(struct partition_map_header *map, int ix) printf(" start address: 0x%08x\n", p->dpme_goto_addr); printf(" checksum: 0x%08x\n", p->dpme_checksum); printf(" processor: '%.32s'\n", p->dpme_processor_id); - printf("dpme_reserved_1 -"); + printf("dpme_reserved_3 -"); dump_block(p->dpme_reserved_1, sizeof(p->dpme_reserved_1)); - printf("dpme_reserved_2 -"); dump_block(p->dpme_reserved_2, sizeof(p->dpme_reserved_2)); - printf("dpme_reserved_3 -"); dump_block(p->dpme_reserved_3, sizeof(p->dpme_reserved_3)); - printf("dpme_reserved_4 -"); - dump_block(p->dpme_reserved_3, sizeof(p->dpme_reserved_4)); + dump_block(p->dpme_reserved_4, sizeof(p->dpme_reserved_4)); } @@ -373,6 +369,7 @@ full_dump_block_zero(struct partition_map_header *map) return; } zp = map->block0; + m = zp->sbDDMap; printf(" signature: 0x%x\n", zp->sbSig); printf(" size of a block: %u\n", zp->sbBlkSize); @@ -381,18 +378,15 @@ full_dump_block_zero(struct partition_map_header *map) printf(" device id: 0x%x\n", zp->sbDevId); printf(" data: 0x%x\n", zp->sbData); printf(" driver count: %u\n", zp->sbDrvrCount); - m = (struct ddmap *)zp->sbMap; - for (i = 0; &m[i].ddType < &zp->sbMap[247]; i++) { - if (m[i].ddBlock == 0 && m[i].ddSize == 0 && m[i].ddType == 0) { + for (i = 0; i < 8; i++) { + if (m[i].ddBlock == 0 && m[i].ddSize == 0 && m[i].ddType == 0) break; - } printf(" driver %3u block: %u\n", i + 1, m[i].ddBlock); printf(" size in blocks: %u\n", m[i].ddSize); printf(" driver type: 0x%x\n", m[i].ddType); } printf("remainder of block -"); - dump_block((unsigned char *)&m[i].ddBlock, (&zp->sbMap[247] - - ((unsigned short *)&m[i].ddBlock)) * 2); + dump_block(zp->sbReserved, sizeof(zp->sbReserved)); } int diff --git a/sbin/pdisk/file_media.c b/sbin/pdisk/file_media.c index b2cc34ba20a..97e37ddda80 100644 --- a/sbin/pdisk/file_media.c +++ b/sbin/pdisk/file_media.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file_media.c,v 1.40 2016/01/25 22:12:22 krw Exp $ */ +/* $OpenBSD: file_media.c,v 1.41 2016/01/25 23:43:20 krw Exp $ */ /* * file_media.c - @@ -78,7 +78,10 @@ struct dpme_ondisk { uint8_t dpme_reserved_4[376]; }; -int +static int read_block(int, uint64_t, void *); +static int write_block(int, uint64_t, void *); + +static int read_block(int fd, uint64_t sector, void *address) { ssize_t off; @@ -97,7 +100,7 @@ read_block(int fd, uint64_t sector, void *address) return (0); } -int +static int write_block(int fd, uint64_t sector, void *address) { ssize_t off; @@ -114,10 +117,8 @@ int read_block0(int fd, struct block0 *block0) { struct block0_ondisk *block0_ondisk; -#if 0 struct ddmap_ondisk ddmap_ondisk; int i; -#endif block0_ondisk = malloc(sizeof(struct block0_ondisk)); if (block0_ondisk == NULL) @@ -148,7 +149,6 @@ read_block0(int fd, struct block0 *block0) sizeof(block0->sbDrvrCount)); block0->sbDrvrCount = betoh16(block0->sbDrvrCount); -#if 0 for (i = 0; i < 8; i++) { memcpy(&ddmap_ondisk, block0->sbDDMap+i*sizeof(struct ddmap_ondisk), @@ -163,7 +163,6 @@ read_block0(int fd, struct block0 *block0) sizeof(block0->sbDDMap[i].ddType)); block0->sbDDMap[i].ddType = betoh32(block0->sbDDMap[i].ddType); } -#endif free(block0_ondisk); return 1; @@ -173,10 +172,8 @@ int write_block0(int fd, struct block0 *block0) { struct block0_ondisk *block0_ondisk; -#if 0 struct ddmap_ondisk ddmap_ondisk; -#endif - int rslt; + int i, rslt; uint32_t tmp32; uint16_t tmp16; @@ -197,7 +194,7 @@ write_block0(int fd, struct block0 *block0) memcpy(block0_ondisk->sbDevType, &tmp16, sizeof(block0_ondisk->sbDevType)); tmp16 = htobe16(block0->sbDevId); - memcpy(block0_ondisk->sbDevId, &tmp32, + memcpy(block0_ondisk->sbDevId, &tmp16, sizeof(block0_ondisk->sbDevId)); tmp32 = htobe32(block0->sbData); memcpy(block0_ondisk->sbData, &tmp32, @@ -206,7 +203,6 @@ write_block0(int fd, struct block0 *block0) memcpy(block0_ondisk->sbDrvrCount, &tmp16, sizeof(block0_ondisk->sbDrvrCount)); -#if 0 for (i = 0; i < 8; i++) { tmp32 = htobe32(block0->sbDDMap[i].ddBlock); memcpy(ddmap_ondisk.ddBlock, &tmp32, @@ -220,7 +216,6 @@ write_block0(int fd, struct block0 *block0) memcpy(block0->sbDDMap+i*sizeof(struct ddmap_ondisk), &ddmap_ondisk, sizeof(ddmap_ondisk)); } -#endif rslt = write_block(fd, 0, block0_ondisk); free(block0_ondisk); diff --git a/sbin/pdisk/file_media.h b/sbin/pdisk/file_media.h index e3865b6bf92..bd997fc66f6 100644 --- a/sbin/pdisk/file_media.h +++ b/sbin/pdisk/file_media.h @@ -1,4 +1,4 @@ -/* $OpenBSD: file_media.h,v 1.17 2016/01/25 22:12:22 krw Exp $ */ +/* $OpenBSD: file_media.h,v 1.18 2016/01/25 23:43:20 krw Exp $ */ /* * file_media.h - @@ -30,9 +30,6 @@ #ifndef __file_media__ #define __file_media__ -int read_block(int, uint64_t, void *); -int write_block(int, uint64_t, void *); - int read_block0(int, struct block0 *); int write_block0(int, struct block0 *); diff --git a/sbin/pdisk/partition_map.c b/sbin/pdisk/partition_map.c index e43a64087ad..dd75ec753e8 100644 --- a/sbin/pdisk/partition_map.c +++ b/sbin/pdisk/partition_map.c @@ -1,4 +1,4 @@ -/* $OpenBSD: partition_map.c,v 1.63 2016/01/24 15:23:33 krw Exp $ */ +/* $OpenBSD: partition_map.c,v 1.64 2016/01/25 23:43:20 krw Exp $ */ /* * partition_map.c - partition map routines @@ -37,7 +37,6 @@ #include "dpme.h" #include "partition_map.h" #include "io.h" -#include "convert.h" #include "file_media.h" #define APPLE_HFS_FLAGS_VALUE 0x4000037f @@ -103,9 +102,7 @@ open_partition_map(int fd, char *name, uint64_t mediasz, uint32_t sectorsz) free(map); return NULL; } - if (read_block(map->fd, 0, map->block0) == 0 || - convert_block0(map->block0, 1) || - coerce_block0(map)) { + if (read_block0(map->fd, map->block0) == 0) { warnx("Can't read block 0 from '%s'", name); free_partition_map(map); return NULL; @@ -161,22 +158,20 @@ read_partition_map(struct partition_map_header *map) warn("can't allocate memory for disk buffers"); return -1; } - if (read_block(map->fd, 1, dpme) == 0) { + if (read_dpme(map->fd, 1, dpme) == 0) { warnx("Can't read block 1 from '%s'", map->name); free(dpme); return -1; - } else if (convert_dpme(dpme, 1) || - dpme->dpme_signature != DPME_SIGNATURE) { + } else if (dpme->dpme_signature != DPME_SIGNATURE) { old_logical = map->logical_block; map->logical_block = 512; while (map->logical_block <= map->physical_block) { - if (read_block(map->fd, 1, dpme) == 0) { + if (read_dpme(map->fd, 1, dpme) == 0) { warnx("Can't read block 1 from '%s'", map->name); free(dpme); return -1; - } else if (convert_dpme(dpme, 1) == 0 - && dpme->dpme_signature == DPME_SIGNATURE) { + } else if (dpme->dpme_signature == DPME_SIGNATURE) { d = map->media_size; map->media_size = (d * old_logical) / map->logical_block; @@ -208,12 +203,11 @@ read_partition_map(struct partition_map_header *map) warn("can't allocate memory for disk buffers"); return -1; } - if (read_block(map->fd, ix, dpme) == 0) { + if (read_dpme(map->fd, ix, dpme) == 0) { warnx("Can't read block %u from '%s'", ix, map->name); free(dpme); return -1; - } else if (convert_dpme(dpme, 1) || - (dpme->dpme_signature != DPME_SIGNATURE) || + } else if ((dpme->dpme_signature != DPME_SIGNATURE) || (dpme->dpme_map_entries != limit)) { warnx("Bad dpme in block %u from '%s'", ix, map->name); free(dpme); @@ -230,19 +224,15 @@ write_partition_map(struct partition_map_header *map) struct partition_map *entry; int result; - convert_block0(map->block0, 0); - result = write_block(map->fd, 0, map->block0); + result = write_block0(map->fd, map->block0); if (result == 0) warn("Unable to write block zero"); - convert_block0(map->block0, 1); for (entry = map->disk_order; entry != NULL; entry = entry->next_on_disk) { - convert_dpme(entry->dpme, 0); - result = write_block(map->fd, entry->disk_address, entry->dpme); + result = write_dpme(map->fd, entry->disk_address, entry->dpme); if (result == 0) warn("Unable to write block %ld", entry->disk_address); - convert_dpme(entry->dpme, 1); } } @@ -623,7 +613,7 @@ contains_driver(struct partition_map *entry) f = p->sbBlkSize / map->logical_block; } if (p->sbDrvrCount > 0) { - m = (struct ddmap *)p->sbMap; + m = p->sbDDMap; for (i = 0; i < p->sbDrvrCount; i++) { start = m[i].ddBlock; if (entry->dpme->dpme_pblock_start <= f * start && @@ -982,7 +972,7 @@ remove_driver(struct partition_map *entry) f = p->sbBlkSize / map->logical_block; } if (p->sbDrvrCount > 0) { - m = (struct ddmap *)p->sbMap; + m = p->sbDDMap; for (i = 0; i < p->sbDrvrCount; i++) { start = m[i].ddBlock; diff --git a/sbin/pdisk/pdisk.c b/sbin/pdisk/pdisk.c index ef188e60812..c3b68293c34 100644 --- a/sbin/pdisk/pdisk.c +++ b/sbin/pdisk/pdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pdisk.c,v 1.68 2016/01/25 03:26:54 jsg Exp $ */ +/* $OpenBSD: pdisk.c,v 1.69 2016/01/25 23:43:20 krw Exp $ */ /* * pdisk - an editor for Apple format partition tables @@ -29,7 +29,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <sys/types.h> +#include <sys/param.h> /* DEV_BSIZE */ #include <sys/dkio.h> #include <sys/disklabel.h> #include <sys/ioctl.h> @@ -106,17 +106,8 @@ main(int argc, char **argv) *argv); if (ioctl(fd, DIOCGPDINFO, &dl) == -1) err(1, "can't get disklabel for %s", *argv); - - if (sizeof(struct block0) != dl.d_secsize) { - errx(1, "Size of block zero structure (%zu) is not equal " - "to disk sector size (%d)\n", sizeof(struct block0), - dl.d_secsize); - } - if (sizeof(struct dpme) != dl.d_secsize) { - errx(1, "Size of partition map entry (%zu) is not equal " - "to disk sector size (%d)\n", sizeof(struct dpme), - dl.d_secsize); - } + if (dl.d_secsize != DEV_BSIZE) + errx(1, "disk sector size (%d) != 512\n", dl.d_secsize); map = open_partition_map(fd, *argv, DL_GETDSIZE(&dl), dl.d_secsize); if (map != NULL) { |