Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178328777
D62435.1796502910.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
D62435.1796502910.diff
View Options
Index: usr.sbin/fstyp/apfs.c
===================================================================
--- usr.sbin/fstyp/apfs.c
+++ usr.sbin/fstyp/apfs.c
@@ -38,8 +38,13 @@
#include "fstyp.h"
/*
- * This really detects the container format, which might be best supported by
- * geom_part or a special GEOM class.
+ * APFS container detection and first-volume label extraction.
+ *
+ * The container superblock at block 0 may be stale: the live one is the
+ * highest-xid, checksum-valid nx_superblock_t in the checkpoint descriptor
+ * area. From there the container object map (a physical, fixed-size-kv
+ * B-tree keyed by {ok_oid, ok_xid}) resolves nx_fs_oid[0] to the volume
+ * superblock, whose apfs_volname is reported as the label.
*
* https://developer.apple.com/support/downloads/Apple-File-System-Reference.pdf
*/
@@ -66,6 +71,10 @@
/* obj_phys::o_type */
#define OBJECT_TYPE_MASK 0x0000ffff
#define OBJECT_TYPE_NX_SUPERBLOCK 0x00000001
+#define OBJECT_TYPE_BTREE 0x00000002
+#define OBJECT_TYPE_BTREE_NODE 0x00000003
+#define OBJECT_TYPE_OMAP 0x0000000b
+#define OBJECT_TYPE_FS 0x0000000d
#define OBJECT_TYPE_FLAGS_MASK 0xffff0000
#define OBJ_STORAGETYPE_MASK 0xc0000000
#define OBJECT_TYPE_FLAGS_DEFINED_MASK 0xf8000000
@@ -79,8 +88,10 @@
#define NX_MAGIC 0x4253584e /* "NXSB" */
#define NX_MINIMUM_BLOCK_SIZE 4096
#define NX_MAXIMUM_BLOCK_SIZE 65536
+#define NX_MAX_FILE_SYSTEMS 100
#define NX_XP_DESC_IS_BTREE 0x80000000U
#define APFS_MAX_XP_DESC_BLOCKS 1024
+#define APFS_MAX_BTREE_DEPTH 16
struct nx_superblock {
struct obj_phys nx_o;
@@ -96,6 +107,19 @@
uint32_t nx_xp_desc_blocks;
uint32_t nx_xp_data_blocks;
paddr_t nx_xp_desc_base;
+ paddr_t nx_xp_data_base;
+ uint32_t nx_xp_desc_next;
+ uint32_t nx_xp_data_next;
+ uint32_t nx_xp_desc_index;
+ uint32_t nx_xp_desc_len;
+ uint32_t nx_xp_data_index;
+ uint32_t nx_xp_data_len;
+ oid_t nx_spaceman_oid;
+ oid_t nx_omap_oid;
+ oid_t nx_reaper_oid;
+ uint32_t nx_test_type;
+ uint32_t nx_max_file_systems;
+ oid_t nx_fs_oid[NX_MAX_FILE_SYSTEMS];
/* ... remaining fields follow */
} __packed;
@@ -107,6 +131,139 @@
"nx_xp_desc_blocks offset");
_Static_assert(offsetof(struct nx_superblock, nx_xp_desc_base) == 112,
"nx_xp_desc_base offset");
+_Static_assert(offsetof(struct nx_superblock, nx_omap_oid) == 160,
+ "nx_omap_oid offset");
+_Static_assert(offsetof(struct nx_superblock, nx_max_file_systems) == 180,
+ "nx_max_file_systems offset");
+_Static_assert(offsetof(struct nx_superblock, nx_fs_oid) == 184,
+ "nx_fs_oid offset");
+
+struct omap_phys {
+ struct obj_phys om_o;
+ uint32_t om_flags;
+ uint32_t om_snap_count;
+ uint32_t om_tree_type;
+ uint32_t om_snapshot_tree_type;
+ oid_t om_tree_oid;
+ oid_t om_snapshot_tree_oid;
+ xid_t om_most_recent_snap;
+ xid_t om_pending_revert_min;
+ xid_t om_pending_revert_max;
+} __packed;
+
+_Static_assert(offsetof(struct omap_phys, om_tree_oid) == 48,
+ "om_tree_oid offset");
+
+struct omap_key {
+ oid_t ok_oid;
+ xid_t ok_xid;
+} __packed;
+
+struct omap_val {
+ uint32_t ov_flags;
+ uint32_t ov_size;
+ paddr_t ov_paddr;
+} __packed;
+
+_Static_assert(sizeof(struct omap_key) == 16, "omap_key size");
+_Static_assert(sizeof(struct omap_val) == 16, "omap_val size");
+
+#define BTNODE_ROOT 0x0001
+#define BTNODE_LEAF 0x0002
+#define BTNODE_FIXED_KV_SIZE 0x0004
+
+struct nloc {
+ uint16_t off;
+ uint16_t len;
+} __packed;
+
+struct kvoff {
+ uint16_t k;
+ uint16_t v;
+} __packed;
+
+struct btree_node_phys {
+ struct obj_phys btn_o;
+ uint16_t btn_flags;
+ uint16_t btn_level;
+ uint32_t btn_nkeys;
+ struct nloc btn_table_space;
+ struct nloc btn_free_space;
+ struct nloc btn_key_free_list;
+ struct nloc btn_val_free_list;
+ uint8_t btn_data[];
+} __packed;
+
+struct btree_info_fixed {
+ uint32_t bt_flags;
+ uint32_t bt_node_size;
+ uint32_t bt_key_size;
+ uint32_t bt_val_size;
+} __packed;
+
+struct btree_info {
+ struct btree_info_fixed bt_fixed;
+ uint32_t bt_longest_key;
+ uint32_t bt_longest_val;
+ uint64_t bt_key_count;
+ uint64_t bt_node_count;
+} __packed;
+
+_Static_assert(offsetof(struct btree_node_phys, btn_data) == 56,
+ "btn_data offset");
+_Static_assert(sizeof(struct btree_info) == 40, "btree_info size");
+
+#define APFS_MAGIC 0x42535041 /* "APSB" */
+#define APFS_MODIFIED_NAMELEN 32
+#define APFS_MAX_HIST 8
+#define APFS_VOLNAME_LEN 256
+
+struct apfs_modified_by {
+ uint8_t id[APFS_MODIFIED_NAMELEN];
+ uint64_t timestamp;
+ xid_t last_xid;
+} __packed;
+
+struct apfs_superblock {
+ struct obj_phys apfs_o;
+ uint32_t apfs_magic;
+ uint32_t apfs_fs_index;
+ uint64_t apfs_features;
+ uint64_t apfs_readonly_compatible_features;
+ uint64_t apfs_incompatible_features;
+ uint64_t apfs_unmount_time;
+ uint64_t apfs_fs_reserve_block_count;
+ uint64_t apfs_fs_quota_block_count;
+ uint64_t apfs_fs_alloc_count;
+ uint8_t apfs_meta_crypto[20];
+ uint32_t apfs_root_tree_type;
+ uint32_t apfs_extentref_tree_type;
+ uint32_t apfs_snap_meta_tree_type;
+ oid_t apfs_omap_oid;
+ oid_t apfs_root_tree_oid;
+ oid_t apfs_extentref_tree_oid;
+ oid_t apfs_snap_meta_tree_oid;
+ xid_t apfs_revert_to_xid;
+ oid_t apfs_revert_to_sblock_oid;
+ uint64_t apfs_next_obj_id;
+ uint64_t apfs_num_files;
+ uint64_t apfs_num_directories;
+ uint64_t apfs_num_symlinks;
+ uint64_t apfs_num_other_fsobjects;
+ uint64_t apfs_num_snapshots;
+ uint64_t apfs_total_blocks_alloced;
+ uint64_t apfs_total_blocks_freed;
+ uint8_t apfs_vol_uuid[16];
+ uint64_t apfs_last_mod_time;
+ uint64_t apfs_fs_flags;
+ struct apfs_modified_by apfs_formatted_by;
+ struct apfs_modified_by apfs_modified_by[APFS_MAX_HIST];
+ uint8_t apfs_volname[APFS_VOLNAME_LEN];
+ /* ... remaining fields follow */
+} __packed;
+
+_Static_assert(offsetof(struct apfs_superblock, apfs_volname) == 704,
+ "apfs_volname offset");
/*
* Compute the APFS Fletcher-64 checksum over everything after o_cksum.
@@ -198,15 +355,121 @@
return (best);
}
+/*
+ * Look up (oid, xid <= want_xid) in a physical object-map B-tree rooted at
+ * block `root`. Internal nodes store each child's lowest key, so descent
+ * takes the rightmost entry whose key is <= the target at every level.
+ * Returns the mapped physical block, or 0 on failure.
+ */
+static paddr_t
+apfs_omap_lookup(FILE *fp, uint32_t block_size, paddr_t root, oid_t oid,
+ xid_t want_xid)
+{
+ struct btree_node_phys *node;
+ struct kvoff kv;
+ struct omap_key key;
+ struct omap_val val;
+ const uint8_t *raw;
+ paddr_t cur, res;
+ oid_t koid;
+ xid_t kxid;
+ uint32_t nkeys, i, pick;
+ uint16_t flags, toff, tlen, ko, vo;
+ size_t table_base, key_base, val_base, kp, vp;
+ int depth;
+
+ cur = root;
+ res = 0;
+ for (depth = 0; depth < APFS_MAX_BTREE_DEPTH; depth++) {
+ node = apfs_read_block(fp, cur, block_size);
+ if (node == NULL)
+ return (0);
+ raw = (const uint8_t *)node;
+ if (!apfs_obj_cksum_valid(&node->btn_o, block_size))
+ goto out;
+ flags = le16toh(node->btn_flags);
+ nkeys = le32toh(node->btn_nkeys);
+ toff = le16toh(node->btn_table_space.off);
+ tlen = le16toh(node->btn_table_space.len);
+ if ((flags & BTNODE_FIXED_KV_SIZE) == 0)
+ goto out;
+ table_base = offsetof(struct btree_node_phys, btn_data) +
+ (size_t)toff;
+ key_base = table_base + (size_t)tlen;
+ val_base = block_size -
+ ((flags & BTNODE_ROOT) != 0 ? sizeof(struct btree_info) : 0);
+ if (key_base > val_base ||
+ (size_t)nkeys > (size_t)tlen / sizeof(struct kvoff))
+ goto out;
+
+ /* Find rightmost key <= (oid, want_xid). */
+ pick = UINT32_MAX;
+ for (i = 0; i < nkeys; i++) {
+ memcpy(&kv, raw + table_base + i * sizeof(kv),
+ sizeof(kv));
+ ko = le16toh(kv.k);
+ kp = key_base + (size_t)ko;
+ if (kp > val_base || val_base - kp < sizeof(key))
+ goto out;
+ memcpy(&key, raw + kp, sizeof(key));
+ koid = le64toh(key.ok_oid);
+ kxid = le64toh(key.ok_xid);
+ if (koid < oid || (koid == oid && kxid <= want_xid))
+ pick = i;
+ else
+ break;
+ }
+ if (pick == UINT32_MAX)
+ goto out;
+ memcpy(&kv, raw + table_base + pick * sizeof(kv), sizeof(kv));
+ ko = le16toh(kv.k);
+ vo = le16toh(kv.v);
+ if ((flags & BTNODE_LEAF) != 0) {
+ kp = key_base + (size_t)ko;
+ if (kp > val_base || val_base - kp < sizeof(key))
+ goto out;
+ memcpy(&key, raw + kp, sizeof(key));
+ if (le64toh(key.ok_oid) != oid)
+ goto out;
+ if (vo < sizeof(val) || vo > val_base ||
+ val_base - vo < key_base)
+ goto out;
+ vp = val_base - vo;
+ memcpy(&val, raw + vp, sizeof(val));
+ res = (paddr_t)le64toh((uint64_t)val.ov_paddr);
+ goto out;
+ }
+ if (vo < sizeof(paddr_t) || vo > val_base ||
+ val_base - vo < key_base)
+ goto out;
+ vp = val_base - vo;
+ /* Physical B-tree: internal node value is child block paddr. */
+ cur = (paddr_t)le64dec(raw + vp);
+ free(node);
+ }
+ return (0);
+out:
+ free(node);
+ return (res);
+}
+
int
fstyp_apfs(FILE *fp, char *label, size_t size)
{
struct nx_superblock *csb, *nx;
- uint32_t block_size;
+ struct omap_phys *om;
+ struct apfs_superblock *vol;
+ xid_t xid;
+ oid_t omap_oid, fs_oid, tree_oid;
+ paddr_t vol_paddr;
+ uint32_t block_size, max_fs;
+ size_t vlen;
int retval;
retval = 1;
nx = NULL;
+ om = NULL;
+ vol = NULL;
csb = read_buf(fp, 0, NX_MINIMUM_BLOCK_SIZE);
if (csb == NULL)
goto fail;
@@ -234,14 +497,46 @@
if (nx == csb)
csb = NULL;
+ /* Valid APFS container. Everything below is label-only. */
retval = 0;
+ if (label == NULL || size == 0)
+ goto fail;
+
+ xid = le64toh(nx->nx_o.o_xid);
+ omap_oid = le64toh(nx->nx_omap_oid);
+ max_fs = le32toh(nx->nx_max_file_systems);
+ if (max_fs == 0 || omap_oid == 0)
+ goto fail;
+ fs_oid = le64toh(nx->nx_fs_oid[0]);
+ if (fs_oid == 0)
+ goto fail;
- /* No label support yet. */
- (void)size;
- (void)label;
+ om = apfs_read_block(fp, (paddr_t)omap_oid, block_size);
+ if (om == NULL || !apfs_obj_cksum_valid(&om->om_o, block_size))
+ goto fail;
+ tree_oid = le64toh(om->om_tree_oid);
+ vol_paddr = apfs_omap_lookup(fp, block_size, (paddr_t)tree_oid,
+ fs_oid, xid);
+ if (vol_paddr <= 0)
+ goto fail;
+
+ vol = apfs_read_block(fp, vol_paddr, block_size);
+ if (vol == NULL || !apfs_obj_cksum_valid(&vol->apfs_o, block_size) ||
+ le32toh(vol->apfs_magic) != APFS_MAGIC)
+ goto fail;
+
+ vlen = strnlen((const char *)vol->apfs_volname, APFS_VOLNAME_LEN);
+ if (vlen > 0) {
+ if (vlen >= size)
+ vlen = size - 1;
+ memcpy(label, vol->apfs_volname, vlen);
+ label[vlen] = '\0';
+ }
fail:
free(csb);
free(nx);
+ free(om);
+ free(vol);
return (retval);
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Dec 5, 8:35 PM (22 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42240539
Default Alt Text
D62435.1796502910.diff (10 KB)
Attached To
Mode
D62435: fstyp: report the APFS volume label
Attached
Detach File
Event Timeline