xref: /freebsd/usr.sbin/fstyp/apfs.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1e41d6276SConrad Meyer /*
2e41d6276SConrad Meyer  * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>.  All rights reserved.
3e41d6276SConrad Meyer  *
4e41d6276SConrad Meyer  * Redistribution and use in source and binary forms, with or without
5e41d6276SConrad Meyer  * modification, are permitted provided that the following conditions
6e41d6276SConrad Meyer  * are met:
7e41d6276SConrad Meyer  * 1. Redistributions of source code must retain the above copyright
8e41d6276SConrad Meyer  *    notice, this list of conditions and the following disclaimer.
9e41d6276SConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
10e41d6276SConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
11e41d6276SConrad Meyer  *    documentation and/or other materials provided with the distribution.
12e41d6276SConrad Meyer  *
13e41d6276SConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14e41d6276SConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15e41d6276SConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16e41d6276SConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17e41d6276SConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18e41d6276SConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19e41d6276SConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20e41d6276SConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21e41d6276SConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22e41d6276SConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23e41d6276SConrad Meyer  * SUCH DAMAGE.
24e41d6276SConrad Meyer  */
25e41d6276SConrad Meyer 
26e41d6276SConrad Meyer #include <sys/cdefs.h>
27e41d6276SConrad Meyer #include <assert.h>
28e41d6276SConrad Meyer #include <err.h>
29e41d6276SConrad Meyer #include <errno.h>
30e41d6276SConrad Meyer #include <stdbool.h>
31e41d6276SConrad Meyer #include <stdint.h>
32e41d6276SConrad Meyer #include <stdio.h>
33e41d6276SConrad Meyer #include <stdlib.h>
34e41d6276SConrad Meyer #include <string.h>
35e41d6276SConrad Meyer 
36e41d6276SConrad Meyer #include "fstyp.h"
37e41d6276SConrad Meyer 
38e41d6276SConrad Meyer /*
39e41d6276SConrad Meyer  * This really detects the container format, which might be best supported by
40e41d6276SConrad Meyer  * geom_part or a special GEOM class.
41e41d6276SConrad Meyer  *
42e41d6276SConrad Meyer  * https://developer.apple.com/support/downloads/Apple-File-System-Reference.pdf
43e41d6276SConrad Meyer  */
44e41d6276SConrad Meyer 
45e41d6276SConrad Meyer #define	NX_CKSUM_SZ		8
46e41d6276SConrad Meyer 
47e41d6276SConrad Meyer typedef uint64_t nx_oid_t;
48e41d6276SConrad Meyer 
49e41d6276SConrad Meyer typedef uint64_t nx_xid_t;
50e41d6276SConrad Meyer 
51e41d6276SConrad Meyer struct nx_obj {
52e41d6276SConrad Meyer 	uint8_t		o_cksum[NX_CKSUM_SZ];	/* Fletcher 64 */
53e41d6276SConrad Meyer 	nx_oid_t	o_oid;
54e41d6276SConrad Meyer 	nx_xid_t	o_xid;
55e41d6276SConrad Meyer 	uint32_t	o_type;
56e41d6276SConrad Meyer 	uint32_t	o_subtype;
57e41d6276SConrad Meyer };
58e41d6276SConrad Meyer 
59e41d6276SConrad Meyer /* nx_obj::o_oid */
60e41d6276SConrad Meyer #define	OID_NX_SUPERBLOCK	1
61e41d6276SConrad Meyer 
62e41d6276SConrad Meyer /* nx_obj::o_type: */
63e41d6276SConrad Meyer #define	OBJECT_TYPE_MASK		0x0000ffff
64e41d6276SConrad Meyer #define	OBJECT_TYPE_NX_SUPERBLOCK	0x00000001
65e41d6276SConrad Meyer #define	OBJECT_TYPE_FLAGS_MASK		0xffff0000
66e41d6276SConrad Meyer #define	OBJ_STORAGETYPE_MASK		0xc0000000
67e41d6276SConrad Meyer #define	OBJECT_TYPE_FLAGS_DEFINED_MASK	0xf8000000
68e41d6276SConrad Meyer #define	OBJ_STORAGE_VIRTUAL		0x00000000
69e41d6276SConrad Meyer #define	OBJ_STORAGE_EPHEMERAL		0x80000000
70e41d6276SConrad Meyer #define	OBJ_STORAGE_PHYSICAL		0x40000000
71e41d6276SConrad Meyer #define	OBJ_NOHEADER			0x20000000
72e41d6276SConrad Meyer #define	OBJ_ENCRYPTED			0x10000000
73e41d6276SConrad Meyer #define	OBJ_NONPERSISTENT		0x08000000
74e41d6276SConrad Meyer 
75e41d6276SConrad Meyer struct nx_superblock {
76e41d6276SConrad Meyer 	struct nx_obj	nx_o;
77e41d6276SConrad Meyer 	char		nx_magic[4];
78e41d6276SConrad Meyer 	/* ... other stuff that doesn't matter */
79e41d6276SConrad Meyer };
80e41d6276SConrad Meyer 
81e41d6276SConrad Meyer int
fstyp_apfs(FILE * fp,char * label,size_t size)82e41d6276SConrad Meyer fstyp_apfs(FILE *fp, char *label, size_t size)
83e41d6276SConrad Meyer {
84e41d6276SConrad Meyer 	struct nx_superblock *csb;
85e41d6276SConrad Meyer 	int retval;
86e41d6276SConrad Meyer 
87e41d6276SConrad Meyer 	retval = 1;
88e41d6276SConrad Meyer 	csb = read_buf(fp, 0, sizeof(*csb));
89e41d6276SConrad Meyer 	if (csb == NULL)
90e41d6276SConrad Meyer 		goto fail;
91e41d6276SConrad Meyer 
92e41d6276SConrad Meyer 	/* Ideally, checksum the SB here. */
93e41d6276SConrad Meyer 	if (strncmp(csb->nx_magic, "NXSB", 4) != 0 ||
94e41d6276SConrad Meyer 	    csb->nx_o.o_oid != OID_NX_SUPERBLOCK ||
95e41d6276SConrad Meyer 	    (csb->nx_o.o_type & OBJECT_TYPE_MASK) != OBJECT_TYPE_NX_SUPERBLOCK)
96e41d6276SConrad Meyer 		goto fail;
97e41d6276SConrad Meyer 
98e41d6276SConrad Meyer 	retval = 0;
99e41d6276SConrad Meyer 
100e41d6276SConrad Meyer 	/* No label support yet. */
101*da0c0e01SPiotr Pawel Stefaniak 	(void)size;
102*da0c0e01SPiotr Pawel Stefaniak 	(void)label;
103e41d6276SConrad Meyer 
104e41d6276SConrad Meyer fail:
105e41d6276SConrad Meyer 	free(csb);
106e41d6276SConrad Meyer 	return (retval);
107e41d6276SConrad Meyer }
108