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