Module Name:    src
Committed By:   tkusumi
Date:           Fri Dec 27 11:06:23 UTC 2019

Modified Files:
        src/usr.sbin/fstyp: Makefile fstyp.c fstyp.h
Added Files:
        src/usr.sbin/fstyp: hfsplus.c

Log Message:
fstyp: Add HFS+ support

taken-from: FreeBSD (freebsd/freebsd@b4d7ad9f787e74e712423def67de8bd76f71943a)


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/usr.sbin/fstyp/Makefile
cvs rdiff -u -r1.3 -r1.4 src/usr.sbin/fstyp/fstyp.c
cvs rdiff -u -r1.2 -r1.3 src/usr.sbin/fstyp/fstyp.h
cvs rdiff -u -r0 -r1.1 src/usr.sbin/fstyp/hfsplus.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/fstyp/Makefile
diff -u src/usr.sbin/fstyp/Makefile:1.6 src/usr.sbin/fstyp/Makefile:1.7
--- src/usr.sbin/fstyp/Makefile:1.6	Mon Nov 18 14:53:34 2019
+++ src/usr.sbin/fstyp/Makefile	Fri Dec 27 11:06:23 2019
@@ -1,9 +1,9 @@
-#	$NetBSD: Makefile,v 1.6 2019/11/18 14:53:34 tkusumi Exp $
+#	$NetBSD: Makefile,v 1.7 2019/12/27 11:06:23 tkusumi Exp $
 
 .include <bsd.own.mk>
 
 PROG=	fstyp
-SRCS=	cd9660.c exfat.c ext2fs.c fstyp.c msdosfs.c ntfs.c ufs.c
+SRCS=	cd9660.c exfat.c ext2fs.c fstyp.c hfsplus.c msdosfs.c ntfs.c ufs.c
 
 .if (${MKZFS} != "no")
 SRCS+=	zfs.c

Index: src/usr.sbin/fstyp/fstyp.c
diff -u src/usr.sbin/fstyp/fstyp.c:1.3 src/usr.sbin/fstyp/fstyp.c:1.4
--- src/usr.sbin/fstyp/fstyp.c:1.3	Mon Nov 18 14:53:34 2019
+++ src/usr.sbin/fstyp/fstyp.c	Fri Dec 27 11:06:23 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: fstyp.c,v 1.3 2019/11/18 14:53:34 tkusumi Exp $	*/
+/*	$NetBSD: fstyp.c,v 1.4 2019/12/27 11:06:23 tkusumi Exp $	*/
 
 /*-
  * Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  *
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: fstyp.c,v 1.3 2019/11/18 14:53:34 tkusumi Exp $");
+__RCSID("$NetBSD: fstyp.c,v 1.4 2019/12/27 11:06:23 tkusumi Exp $");
 
 #include <sys/disklabel.h>
 #include <sys/dkio.h>
@@ -66,6 +66,7 @@ static struct {
 	{ "cd9660", &fstyp_cd9660, false },
 	{ "exfat", &fstyp_exfat, false },
 	{ "ext2fs", &fstyp_ext2fs, false },
+	{ "hfs+", &fstyp_hfsp, false },
 	{ "msdosfs", &fstyp_msdosfs, false },
 	{ "ntfs", &fstyp_ntfs, false },
 	{ "ufs", &fstyp_ufs, false },

Index: src/usr.sbin/fstyp/fstyp.h
diff -u src/usr.sbin/fstyp/fstyp.h:1.2 src/usr.sbin/fstyp/fstyp.h:1.3
--- src/usr.sbin/fstyp/fstyp.h:1.2	Mon Nov 18 14:53:34 2019
+++ src/usr.sbin/fstyp/fstyp.h	Fri Dec 27 11:06:23 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: fstyp.h,v 1.2 2019/11/18 14:53:34 tkusumi Exp $	*/
+/*	$NetBSD: fstyp.h,v 1.3 2019/12/27 11:06:23 tkusumi Exp $	*/
 
 /*-
  * Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -46,6 +46,7 @@ void	rtrim(char *, size_t);
 int	fstyp_cd9660(FILE *, char *, size_t);
 int	fstyp_exfat(FILE *fp, char *label, size_t size);
 int	fstyp_ext2fs(FILE *, char *, size_t);
+int	fstyp_hfsp(FILE *fp, char *label, size_t size);
 int	fstyp_msdosfs(FILE *, char *, size_t);
 int	fstyp_ntfs(FILE *, char *, size_t);
 int	fstyp_ufs(FILE *, char *, size_t);

Added files:

Index: src/usr.sbin/fstyp/hfsplus.c
diff -u /dev/null src/usr.sbin/fstyp/hfsplus.c:1.1
--- /dev/null	Fri Dec 27 11:06:23 2019
+++ src/usr.sbin/fstyp/hfsplus.c	Fri Dec 27 11:06:23 2019
@@ -0,0 +1,125 @@
+/*	$NetBSD: hfsplus.c,v 1.1 2019/12/27 11:06:23 tkusumi Exp $	*/
+/*
+ * Copyright (c) 2019 Conrad Meyer <c...@freebsd.org>.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: hfsplus.c,v 1.1 2019/12/27 11:06:23 tkusumi Exp $");
+
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "fstyp.h"
+
+/*
+ * https://developer.apple.com/library/archive/technotes/tn/tn1150.html
+ */
+
+#define	VOL_HDR_OFF	1024
+
+typedef uint32_t hfsp_cat_nodeid;
+
+typedef struct hfsp_ext_desc {
+	uint32_t	ex_startBlock;
+	uint32_t	ex_blockCount;
+} hfsp_ext_desc;
+
+typedef struct hfsp_fork_data {
+	uint64_t	fd_logicalSz;
+	uint32_t	fd_clumpSz;
+	uint32_t	fd_totalBlocks;
+	hfsp_ext_desc	fd_extents[8];
+} hfsp_fork_data;
+
+struct hfsp_vol_hdr {
+	char		hp_signature[2];
+	uint16_t	hp_version;
+	uint32_t	hp_attributes;
+	uint32_t	hp_lastMounted;
+	uint32_t	hp_journalInfoBlock;
+
+	/* Creation / etc dates. */
+	uint32_t	hp_create;
+	uint32_t	hp_modify;
+	uint32_t	hp_backup;
+	uint32_t	hp_checked;
+
+	/* Stats */
+	uint32_t	hp_files;
+	uint32_t	hp_folders;
+
+	/* Parameters */
+	uint32_t	hp_blockSize;
+	uint32_t	hp_totalBlocks;
+	uint32_t	hp_freeBlocks;
+
+	uint32_t	hp_nextAlloc;
+	uint32_t	hp_rsrcClumpSz;
+	uint32_t	hp_dataClumpSz;
+
+	hfsp_cat_nodeid	hp_nextCatID;
+
+	uint32_t	hp_writeCount;
+	uint64_t	hp_encodingsBM;
+
+	uint32_t	hp_finderInfo[8];
+
+	hfsp_fork_data	hp_allocationFile;
+	hfsp_fork_data	hp_extentsFile;
+	hfsp_fork_data	hp_catalogFile;
+	hfsp_fork_data	hp_attributesFile;
+	hfsp_fork_data	hp_startupFile;
+};
+_Static_assert(sizeof(struct hfsp_vol_hdr) == 512, "");
+
+int
+fstyp_hfsp(FILE *fp, char *label, size_t size)
+{
+	struct hfsp_vol_hdr *hdr;
+	int retval;
+
+	retval = 1;
+	hdr = read_buf(fp, VOL_HDR_OFF, sizeof(*hdr));
+	if (hdr == NULL)
+		goto fail;
+
+	if ((strncmp(hdr->hp_signature, "H+", 2) != 0 || hdr->hp_version != 4)
+	    &&
+	    (strncmp(hdr->hp_signature, "HX", 2) != 0 || hdr->hp_version != 5))
+		goto fail;
+
+	/* This is an HFS+ volume. */
+	retval = 0;
+
+	/* No label support yet. */
+
+fail:
+	free(hdr);
+	return (retval);
+}

Reply via email to