]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/fstyp/apfs.c
nvi: import version 2.2.1
[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 #include <assert.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "fstyp.h"
37
38 /*
39  * This really detects the container format, which might be best supported by
40  * geom_part or a special GEOM class.
41  *
42  * https://developer.apple.com/support/downloads/Apple-File-System-Reference.pdf
43  */
44
45 #define NX_CKSUM_SZ             8
46
47 typedef uint64_t nx_oid_t;
48
49 typedef uint64_t nx_xid_t;
50
51 struct nx_obj {
52         uint8_t         o_cksum[NX_CKSUM_SZ];   /* Fletcher 64 */
53         nx_oid_t        o_oid;
54         nx_xid_t        o_xid;
55         uint32_t        o_type;
56         uint32_t        o_subtype;
57 };
58
59 /* nx_obj::o_oid */
60 #define OID_NX_SUPERBLOCK       1
61
62 /* nx_obj::o_type: */
63 #define OBJECT_TYPE_MASK                0x0000ffff
64 #define OBJECT_TYPE_NX_SUPERBLOCK       0x00000001
65 #define OBJECT_TYPE_FLAGS_MASK          0xffff0000
66 #define OBJ_STORAGETYPE_MASK            0xc0000000
67 #define OBJECT_TYPE_FLAGS_DEFINED_MASK  0xf8000000
68 #define OBJ_STORAGE_VIRTUAL             0x00000000
69 #define OBJ_STORAGE_EPHEMERAL           0x80000000
70 #define OBJ_STORAGE_PHYSICAL            0x40000000
71 #define OBJ_NOHEADER                    0x20000000
72 #define OBJ_ENCRYPTED                   0x10000000
73 #define OBJ_NONPERSISTENT               0x08000000
74
75 struct nx_superblock {
76         struct nx_obj   nx_o;
77         char            nx_magic[4];
78         /* ... other stuff that doesn't matter */
79 };
80
81 int
82 fstyp_apfs(FILE *fp, char *label, size_t size)
83 {
84         struct nx_superblock *csb;
85         int retval;
86
87         retval = 1;
88         csb = read_buf(fp, 0, sizeof(*csb));
89         if (csb == NULL)
90                 goto fail;
91
92         /* Ideally, checksum the SB here. */
93         if (strncmp(csb->nx_magic, "NXSB", 4) != 0 ||
94             csb->nx_o.o_oid != OID_NX_SUPERBLOCK ||
95             (csb->nx_o.o_type & OBJECT_TYPE_MASK) != OBJECT_TYPE_NX_SUPERBLOCK)
96                 goto fail;
97
98         retval = 0;
99
100         /* No label support yet. */
101         (void)size;
102         (void)label;
103
104 fail:
105         free(csb);
106         return (retval);
107 }