[kpartx] Add support for mac partition table
authorBastian Blank <waldi@debian.org>
Thu, 9 Feb 2006 17:22:18 +0000 (17:22 +0000)
committerBastian Blank <waldi@debian.org>
Thu, 9 Feb 2006 17:22:18 +0000 (17:22 +0000)
Signed-off-by: Bastian Blank <waldi@debian.org>
kpartx/Makefile
kpartx/byteorder.h
kpartx/kpartx.c
kpartx/kpartx.h
kpartx/mac.c [new file with mode: 0644]
kpartx/mac.h [new file with mode: 0644]

index 1b68ca6..bf6e6c1 100644 (file)
@@ -10,12 +10,12 @@ CFLAGS += -I. -D_LARGEFILE64_SOURCE
 
 ifeq ($(strip $(BUILD)),klibc)
        OBJS = bsd.o dos.o kpartx.o solaris.o unixware.o gpt.o crc32.o \
-              lopart.o xstrncpy.o devmapper.o dasd.o \
+              lopart.o xstrncpy.o devmapper.o dasd.o mac.o \
               $(MULTIPATHLIB)-$(BUILD).a $(libdm)
 else
        LDFLAGS = -ldevmapper
        OBJS = bsd.o dos.o kpartx.o solaris.o unixware.o dasd.o \
-              gpt.o crc32.o lopart.o xstrncpy.o devmapper.o
+              gpt.o mac.o crc32.o lopart.o xstrncpy.o devmapper.o
 endif
 
 EXEC = kpartx
index 6d2588b..21962d6 100644 (file)
@@ -9,9 +9,15 @@
 #endif
 
 #if BYTE_ORDER == LITTLE_ENDIAN
+#  define le16_to_cpu(x) (x)
+#  define be16_to_cpu(x) bswap_16(x)
 #  define le32_to_cpu(x) (x)
+#  define be32_to_cpu(x) bswap_32(x)
 #elif BYTE_ORDER == BIG_ENDIAN
+#  define le16_to_cpu(x) bswap_16(x)
+#  define be16_to_cpu(x) (x)
 #  define le32_to_cpu(x) bswap_32(x)
+#  define be32_to_cpu(x) (x)
 #else
 #  error unsupported
 #endif
index c1c2fae..2198302 100644 (file)
@@ -78,6 +78,7 @@ initpts(void)
        addpts("solaris", read_solaris_pt);
        addpts("unixware", read_unixware_pt);
        addpts("dasd", read_dasd_pt);
+       addpts("mac", read_mac_pt);
 }
 
 static char short_opts[] = "ladgvnp:t:";
index 9819268..6a715de 100644 (file)
@@ -32,6 +32,7 @@ extern ptreader read_solaris_pt;
 extern ptreader read_unixware_pt;
 extern ptreader read_gpt_pt;
 extern ptreader read_dasd_pt;
+extern ptreader read_mac_pt;
 
 char *getblock(int fd, unsigned int secnr);
 
diff --git a/kpartx/mac.c b/kpartx/mac.c
new file mode 100644 (file)
index 0000000..5432e67
--- /dev/null
@@ -0,0 +1,47 @@
+#include "kpartx.h"
+#include "byteorder.h"
+#include <stdio.h>
+#include <string.h>
+#include "mac.h"
+
+int
+read_mac_pt(int fd, struct slice all, struct slice *sp, int ns) {
+       struct mac_driver_desc *md;
+        struct mac_partition *part;
+       unsigned secsize;
+       char *data;
+       int blk, blocks_in_map;
+        int n = 0;
+
+       md = (struct mac_driver_desc *) getblock(fd, 0);
+       if (md == NULL)
+               return -1;
+
+       if (be16_to_cpu(md->signature) != MAC_DRIVER_MAGIC)
+               return -1;
+
+       secsize = be16_to_cpu(md->block_size);
+       data = getblock(fd, secsize/512);
+       if (!data)
+               return -1;
+       part = (struct mac_partition *) (data + secsize%512);
+
+       if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
+               return -1;
+
+       blocks_in_map = be32_to_cpu(part->map_count);
+       for (blk = 1; blk <= blocks_in_map && blk <= ns; ++blk, ++n) {
+               int pos = blk * secsize;
+               data = getblock(fd, pos/512);
+               if (!data)
+                       return -1;
+
+               part = (struct mac_partition *) (data + pos%512);
+               if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
+                       break;
+
+               sp[n].start = be32_to_cpu(part->start_block) * (secsize/512);
+               sp[n].size = be32_to_cpu(part->block_count) * (secsize/512);
+       }
+       return n;
+}
diff --git a/kpartx/mac.h b/kpartx/mac.h
new file mode 100644 (file)
index 0000000..3c712ba
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef MAC_H
+#define MAC_H
+
+#include <stdint.h>
+
+#define MAC_PARTITION_MAGIC     0x504d
+
+/* type field value for A/UX or other Unix partitions */
+#define APPLE_AUX_TYPE  "Apple_UNIX_SVR2"
+
+struct mac_partition {
+        uint16_t  signature;      /* expected to be MAC_PARTITION_MAGIC */
+        uint16_t  res1;
+        uint32_t  map_count;      /* # blocks in partition map */
+        uint32_t  start_block;    /* absolute starting block # of partition */
+        uint32_t  block_count;    /* number of blocks in partition */
+        /* there is more stuff after this that we don't need */
+};
+
+#define MAC_DRIVER_MAGIC        0x4552
+
+/* Driver descriptor structure, in block 0 */
+struct mac_driver_desc {
+        uint16_t  signature;      /* expected to be MAC_DRIVER_MAGIC */
+        uint16_t  block_size;
+        uint32_t  block_count;
+    /* ... more stuff */
+};
+
+#endif