xref: /netbsd-src/usr.sbin/fstyp/hfsplus.c (revision ce58ffd338fadaf7de9a4ee9bc8f24033111977c)
1*ce58ffd3Schristos /*	$NetBSD: hfsplus.c,v 1.3 2021/09/17 21:06:35 christos Exp $	*/
2c952179fStkusumi /*
3c952179fStkusumi  * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>.  All rights reserved.
4c952179fStkusumi  *
5c952179fStkusumi  * Redistribution and use in source and binary forms, with or without
6c952179fStkusumi  * modification, are permitted provided that the following conditions
7c952179fStkusumi  * are met:
8c952179fStkusumi  * 1. Redistributions of source code must retain the above copyright
9c952179fStkusumi  *    notice, this list of conditions and the following disclaimer.
10c952179fStkusumi  * 2. Redistributions in binary form must reproduce the above copyright
11c952179fStkusumi  *    notice, this list of conditions and the following disclaimer in the
12c952179fStkusumi  *    documentation and/or other materials provided with the distribution.
13c952179fStkusumi  *
14c952179fStkusumi  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15c952179fStkusumi  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c952179fStkusumi  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c952179fStkusumi  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18c952179fStkusumi  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c952179fStkusumi  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c952179fStkusumi  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c952179fStkusumi  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c952179fStkusumi  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c952179fStkusumi  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c952179fStkusumi  * SUCH DAMAGE.
25c952179fStkusumi  */
26c952179fStkusumi #include <sys/cdefs.h>
27*ce58ffd3Schristos __RCSID("$NetBSD: hfsplus.c,v 1.3 2021/09/17 21:06:35 christos Exp $");
28c952179fStkusumi 
29c952179fStkusumi #include <assert.h>
30c952179fStkusumi #include <err.h>
31c952179fStkusumi #include <errno.h>
32c952179fStkusumi #include <stdbool.h>
33c952179fStkusumi #include <stdint.h>
34c952179fStkusumi #include <stdio.h>
35c952179fStkusumi #include <stdlib.h>
36c952179fStkusumi #include <string.h>
37c952179fStkusumi 
38c952179fStkusumi #include "fstyp.h"
39c952179fStkusumi 
40c952179fStkusumi /*
41c952179fStkusumi  * https://developer.apple.com/library/archive/technotes/tn/tn1150.html
42c952179fStkusumi  */
43c952179fStkusumi 
44c952179fStkusumi #define	VOL_HDR_OFF	1024
45c952179fStkusumi 
46c952179fStkusumi typedef uint32_t hfsp_cat_nodeid;
47c952179fStkusumi 
48c952179fStkusumi typedef struct hfsp_ext_desc {
49c952179fStkusumi 	uint32_t	ex_startBlock;
50c952179fStkusumi 	uint32_t	ex_blockCount;
51c952179fStkusumi } hfsp_ext_desc;
52c952179fStkusumi 
53c952179fStkusumi typedef struct hfsp_fork_data {
54c952179fStkusumi 	uint64_t	fd_logicalSz;
55c952179fStkusumi 	uint32_t	fd_clumpSz;
56c952179fStkusumi 	uint32_t	fd_totalBlocks;
57c952179fStkusumi 	hfsp_ext_desc	fd_extents[8];
58c952179fStkusumi } hfsp_fork_data;
59c952179fStkusumi 
60c952179fStkusumi struct hfsp_vol_hdr {
61c952179fStkusumi 	char		hp_signature[2];
62c952179fStkusumi 	uint16_t	hp_version;
63c952179fStkusumi 	uint32_t	hp_attributes;
64c952179fStkusumi 	uint32_t	hp_lastMounted;
65c952179fStkusumi 	uint32_t	hp_journalInfoBlock;
66c952179fStkusumi 
67c952179fStkusumi 	/* Creation / etc dates. */
68c952179fStkusumi 	uint32_t	hp_create;
69c952179fStkusumi 	uint32_t	hp_modify;
70c952179fStkusumi 	uint32_t	hp_backup;
71c952179fStkusumi 	uint32_t	hp_checked;
72c952179fStkusumi 
73c952179fStkusumi 	/* Stats */
74c952179fStkusumi 	uint32_t	hp_files;
75c952179fStkusumi 	uint32_t	hp_folders;
76c952179fStkusumi 
77c952179fStkusumi 	/* Parameters */
78c952179fStkusumi 	uint32_t	hp_blockSize;
79c952179fStkusumi 	uint32_t	hp_totalBlocks;
80c952179fStkusumi 	uint32_t	hp_freeBlocks;
81c952179fStkusumi 
82c952179fStkusumi 	uint32_t	hp_nextAlloc;
83c952179fStkusumi 	uint32_t	hp_rsrcClumpSz;
84c952179fStkusumi 	uint32_t	hp_dataClumpSz;
85c952179fStkusumi 
86c952179fStkusumi 	hfsp_cat_nodeid	hp_nextCatID;
87c952179fStkusumi 
88c952179fStkusumi 	uint32_t	hp_writeCount;
89c952179fStkusumi 	uint64_t	hp_encodingsBM;
90c952179fStkusumi 
91c952179fStkusumi 	uint32_t	hp_finderInfo[8];
92c952179fStkusumi 
93c952179fStkusumi 	hfsp_fork_data	hp_allocationFile;
94c952179fStkusumi 	hfsp_fork_data	hp_extentsFile;
95c952179fStkusumi 	hfsp_fork_data	hp_catalogFile;
96c952179fStkusumi 	hfsp_fork_data	hp_attributesFile;
97c952179fStkusumi 	hfsp_fork_data	hp_startupFile;
98c952179fStkusumi };
99c952179fStkusumi _Static_assert(sizeof(struct hfsp_vol_hdr) == 512, "");
100c952179fStkusumi 
101c952179fStkusumi int
fstyp_hfsp(FILE * fp,char * label,size_t size)102c952179fStkusumi fstyp_hfsp(FILE *fp, char *label, size_t size)
103c952179fStkusumi {
104c952179fStkusumi 	struct hfsp_vol_hdr *hdr;
105c952179fStkusumi 	int retval;
106c952179fStkusumi 
107c952179fStkusumi 	retval = 1;
108c952179fStkusumi 	hdr = read_buf(fp, VOL_HDR_OFF, sizeof(*hdr));
109c952179fStkusumi 	if (hdr == NULL)
110c952179fStkusumi 		goto fail;
111c952179fStkusumi 
112c952179fStkusumi 	if ((strncmp(hdr->hp_signature, "H+", 2) != 0 || hdr->hp_version != 4)
113c952179fStkusumi 	    &&
114c952179fStkusumi 	    (strncmp(hdr->hp_signature, "HX", 2) != 0 || hdr->hp_version != 5))
115c952179fStkusumi 		goto fail;
116c952179fStkusumi 
117c952179fStkusumi 	/* This is an HFS+ volume. */
118c952179fStkusumi 	retval = 0;
119c952179fStkusumi 
120c952179fStkusumi 	/* No label support yet. */
121c952179fStkusumi 
122c952179fStkusumi fail:
123c952179fStkusumi 	free(hdr);
124c952179fStkusumi 	return (retval);
125c952179fStkusumi }
126