From 8f898c578ac72abb65eef50e82382c2ef66a549e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 12 Mar 2009 13:00:27 +0100 Subject: [PATCH] lib: pttype: add BSD subpartitions support Signed-off-by: Karel Zak --- lib/pttype.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/pttype.c b/lib/pttype.c index 429b4755..23070b9a 100644 --- a/lib/pttype.c +++ b/lib/pttype.c @@ -13,6 +13,9 @@ #include "blkdev.h" +/* we need to read two sectors, beacuse BSD label offset is 512 */ +#define PTTYPE_BUFSIZ (2 * DEFAULT_SECTOR_SIZE) /* 1024 */ + /* * SGI */ @@ -212,16 +215,35 @@ mac_parttable(char *base) ntohs(maclabel(base)->magic) == MAC_OLD_PARTITION_MAGIC); } +/* + * BSD subpartitions listed in a disklabel, under a dos-like partition. + */ +#define BSD_DISKMAGIC 0x82564557UL /* The disk magic number */ +#define BSD_DISKMAGIC_SWAPED 0x57455682UL +struct bsd_disklabel { + uint32_t magic; /* the magic number */ + /* ... */ +}; + +static int +bsd_parttable(char *base) +{ + struct bsd_disklabel *l = (struct bsd_disklabel *) + (base + (DEFAULT_SECTOR_SIZE * 1)); + + return (l->magic == BSD_DISKMAGIC || l->magic == BSD_DISKMAGIC_SWAPED); +} + const char * get_pt_type(const char *device) { int fd; char *type = NULL; - char buf[DEFAULT_SECTOR_SIZE]; + char buf[PTTYPE_BUFSIZ]; if ((fd = open(device, O_RDONLY)) < 0) ; - else if (read(fd, buf, DEFAULT_SECTOR_SIZE) != DEFAULT_SECTOR_SIZE) + else if (read(fd, buf, PTTYPE_BUFSIZ) != PTTYPE_BUFSIZ) ; else { if (sgi_parttable(buf)) @@ -234,6 +256,8 @@ get_pt_type(const char *device) type = "DOS"; else if (mac_parttable(buf)) type = "Mac"; + else if (bsd_parttable(buf)) + type = "BSD"; } if (fd >= 0) -- 2.39.5