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