From owner-svn-src-head@freebsd.org Mon Dec 23 05:43:19 2019 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id A63281E642A; Mon, 23 Dec 2019 05:43:19 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47h7Yg3h18z4GXw; Mon, 23 Dec 2019 05:43:19 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7634DB425; Mon, 23 Dec 2019 05:43:19 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id xBN5hJHp003931; Mon, 23 Dec 2019 05:43:19 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id xBN5hIQa003927; Mon, 23 Dec 2019 05:43:18 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201912230543.xBN5hIQa003927@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Mon, 23 Dec 2019 05:43:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r356034 - head/usr.sbin/fstyp X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: head/usr.sbin/fstyp X-SVN-Commit-Revision: 356034 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Dec 2019 05:43:19 -0000 Author: cem Date: Mon Dec 23 05:43:18 2019 New Revision: 356034 URL: https://svnweb.freebsd.org/changeset/base/356034 Log: fstyp(8): Detect APFS containers APFS NXSBs are more like slices (or zvols?) than individual filesystem, but go ahead and detect them nicely as well. Added: head/usr.sbin/fstyp/apfs.c (contents, props changed) Modified: head/usr.sbin/fstyp/Makefile head/usr.sbin/fstyp/fstyp.c head/usr.sbin/fstyp/fstyp.h Modified: head/usr.sbin/fstyp/Makefile ============================================================================== --- head/usr.sbin/fstyp/Makefile Mon Dec 23 05:43:01 2019 (r356033) +++ head/usr.sbin/fstyp/Makefile Mon Dec 23 05:43:18 2019 (r356034) @@ -3,7 +3,7 @@ .include PROG= fstyp -SRCS= cd9660.c exfat.c ext2fs.c fstyp.c geli.c hfsplus.c msdosfs.c ntfs.c ufs.c +SRCS= apfs.c cd9660.c exfat.c ext2fs.c fstyp.c geli.c hfsplus.c msdosfs.c ntfs.c ufs.c .if ${MK_ZFS} != "no" SRCS += zfs.c Added: head/usr.sbin/fstyp/apfs.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.sbin/fstyp/apfs.c Mon Dec 23 05:43:18 2019 (r356034) @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2019 Conrad Meyer . All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "fstyp.h" + +/* + * This really detects the container format, which might be best supported by + * geom_part or a special GEOM class. + * + * https://developer.apple.com/support/downloads/Apple-File-System-Reference.pdf + */ + +#define NX_CKSUM_SZ 8 + +typedef uint64_t nx_oid_t; + +typedef uint64_t nx_xid_t; + +struct nx_obj { + uint8_t o_cksum[NX_CKSUM_SZ]; /* Fletcher 64 */ + nx_oid_t o_oid; + nx_xid_t o_xid; + uint32_t o_type; + uint32_t o_subtype; +}; + +/* nx_obj::o_oid */ +#define OID_NX_SUPERBLOCK 1 + +/* nx_obj::o_type: */ +#define OBJECT_TYPE_MASK 0x0000ffff +#define OBJECT_TYPE_NX_SUPERBLOCK 0x00000001 +#define OBJECT_TYPE_FLAGS_MASK 0xffff0000 +#define OBJ_STORAGETYPE_MASK 0xc0000000 +#define OBJECT_TYPE_FLAGS_DEFINED_MASK 0xf8000000 +#define OBJ_STORAGE_VIRTUAL 0x00000000 +#define OBJ_STORAGE_EPHEMERAL 0x80000000 +#define OBJ_STORAGE_PHYSICAL 0x40000000 +#define OBJ_NOHEADER 0x20000000 +#define OBJ_ENCRYPTED 0x10000000 +#define OBJ_NONPERSISTENT 0x08000000 + +struct nx_superblock { + struct nx_obj nx_o; + char nx_magic[4]; + /* ... other stuff that doesn't matter */ +}; + +int +fstyp_apfs(FILE *fp, char *label, size_t size) +{ + struct nx_superblock *csb; + int retval; + + retval = 1; + csb = read_buf(fp, 0, sizeof(*csb)); + if (csb == NULL) + goto fail; + + /* Ideally, checksum the SB here. */ + if (strncmp(csb->nx_magic, "NXSB", 4) != 0 || + csb->nx_o.o_oid != OID_NX_SUPERBLOCK || + (csb->nx_o.o_type & OBJECT_TYPE_MASK) != OBJECT_TYPE_NX_SUPERBLOCK) + goto fail; + + retval = 0; + + /* No label support yet. */ + +fail: + free(csb); + return (retval); +} Modified: head/usr.sbin/fstyp/fstyp.c ============================================================================== --- head/usr.sbin/fstyp/fstyp.c Mon Dec 23 05:43:01 2019 (r356033) +++ head/usr.sbin/fstyp/fstyp.c Mon Dec 23 05:43:18 2019 (r356034) @@ -62,6 +62,7 @@ static struct { bool unmountable; char *precache_encoding; } fstypes[] = { + { "apfs", &fstyp_apfs, true, NULL }, { "cd9660", &fstyp_cd9660, false, NULL }, { "exfat", &fstyp_exfat, false, EXFAT_ENC }, { "ext2fs", &fstyp_ext2fs, false, NULL }, Modified: head/usr.sbin/fstyp/fstyp.h ============================================================================== --- head/usr.sbin/fstyp/fstyp.h Mon Dec 23 05:43:01 2019 (r356033) +++ head/usr.sbin/fstyp/fstyp.h Mon Dec 23 05:43:18 2019 (r356034) @@ -50,6 +50,7 @@ void *read_buf(FILE *fp, off_t off, size_t len); char *checked_strdup(const char *s); void rtrim(char *label, size_t size); +int fstyp_apfs(FILE *fp, char *label, size_t size); int fstyp_cd9660(FILE *fp, char *label, size_t size); int fstyp_exfat(FILE *fp, char *label, size_t size); int fstyp_ext2fs(FILE *fp, char *label, size_t size);