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