Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178330606
D62424.1796506700.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
D62424.1796506700.diff
View Options
Index: usr.sbin/fstyp/apfs.c
===================================================================
--- usr.sbin/fstyp/apfs.c
+++ usr.sbin/fstyp/apfs.c
@@ -79,11 +79,23 @@
#define NX_MAGIC 0x4253584e /* "NXSB" */
#define NX_MINIMUM_BLOCK_SIZE 4096
#define NX_MAXIMUM_BLOCK_SIZE 65536
+#define NX_XP_DESC_IS_BTREE 0x80000000U
+#define APFS_MAX_XP_DESC_BLOCKS 1024
struct nx_superblock {
struct obj_phys nx_o;
uint32_t nx_magic;
uint32_t nx_block_size;
+ uint64_t nx_block_count;
+ uint64_t nx_features;
+ uint64_t nx_readonly_compatible_features;
+ uint64_t nx_incompatible_features;
+ uint8_t nx_uuid[16];
+ oid_t nx_next_oid;
+ xid_t nx_next_xid;
+ uint32_t nx_xp_desc_blocks;
+ uint32_t nx_xp_data_blocks;
+ paddr_t nx_xp_desc_base;
/* ... remaining fields follow */
} __packed;
@@ -91,6 +103,10 @@
"nx_magic offset");
_Static_assert(offsetof(struct nx_superblock, nx_block_size) == 36,
"nx_block_size offset");
+_Static_assert(offsetof(struct nx_superblock, nx_xp_desc_blocks) == 104,
+ "nx_xp_desc_blocks offset");
+_Static_assert(offsetof(struct nx_superblock, nx_xp_desc_base) == 112,
+ "nx_xp_desc_base offset");
/*
* Compute the APFS Fletcher-64 checksum over everything after o_cksum.
@@ -120,6 +136,16 @@
return (le64dec(obj->o_cksum) == apfs_fletcher64(obj, len));
}
+static void *
+apfs_read_block(FILE *fp, paddr_t paddr, uint32_t block_size)
+{
+
+ if (paddr < 0 || (uint64_t)paddr > ((uint64_t)INT64_MAX / block_size))
+ return (NULL);
+ return (read_buf(fp, (off_t)((uint64_t)paddr * block_size),
+ block_size));
+}
+
static bool
apfs_is_nxsb(const struct nx_superblock *nx)
{
@@ -130,14 +156,57 @@
OBJECT_TYPE_NX_SUPERBLOCK);
}
+/*
+ * Walk the checkpoint descriptor area and return the container superblock
+ * with the highest o_xid that passes Fletcher-64 validation. Falls back to
+ * the block-0 copy (csb) if the descriptor area is a B-tree or has no newer
+ * valid superblock.
+ */
+static struct nx_superblock *
+apfs_latest_nxsb(FILE *fp, struct nx_superblock *csb, uint32_t block_size)
+{
+ struct nx_superblock *best, *cand;
+ paddr_t desc_base;
+ uint64_t max_blk;
+ uint32_t desc_blocks, i;
+
+ best = csb;
+ desc_blocks = le32toh(csb->nx_xp_desc_blocks);
+ if ((desc_blocks & NX_XP_DESC_IS_BTREE) != 0)
+ return (best);
+ desc_base = (paddr_t)le64toh((uint64_t)csb->nx_xp_desc_base);
+ max_blk = (uint64_t)INT64_MAX / block_size;
+ if (desc_base < 0 || desc_blocks > APFS_MAX_XP_DESC_BLOCKS ||
+ (uint64_t)desc_base > max_blk ||
+ desc_blocks > max_blk - (uint64_t)desc_base)
+ return (best);
+
+ for (i = 0; i < desc_blocks; i++) {
+ cand = apfs_read_block(fp, desc_base + (paddr_t)i, block_size);
+ if (cand == NULL)
+ break;
+ if (apfs_is_nxsb(cand) &&
+ apfs_obj_cksum_valid(&cand->nx_o, block_size) &&
+ le64toh(cand->nx_o.o_xid) > le64toh(best->nx_o.o_xid)) {
+ if (best != csb)
+ free(best);
+ best = cand;
+ } else {
+ free(cand);
+ }
+ }
+ return (best);
+}
+
int
fstyp_apfs(FILE *fp, char *label, size_t size)
{
- struct nx_superblock *csb;
+ struct nx_superblock *csb, *nx;
uint32_t block_size;
int retval;
retval = 1;
+ nx = NULL;
csb = read_buf(fp, 0, NX_MINIMUM_BLOCK_SIZE);
if (csb == NULL)
goto fail;
@@ -153,7 +222,7 @@
if (block_size != NX_MINIMUM_BLOCK_SIZE) {
free(csb);
- csb = read_buf(fp, 0, block_size);
+ csb = apfs_read_block(fp, 0, block_size);
if (csb == NULL)
goto fail;
}
@@ -161,6 +230,10 @@
if (!apfs_obj_cksum_valid(&csb->nx_o, block_size))
goto fail;
+ nx = apfs_latest_nxsb(fp, csb, block_size);
+ if (nx == csb)
+ csb = NULL;
+
retval = 0;
/* No label support yet. */
@@ -169,5 +242,6 @@
fail:
free(csb);
+ free(nx);
return (retval);
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Dec 5, 9:38 PM (23 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42241644
Default Alt Text
D62424.1796506700.diff (3 KB)
Attached To
Mode
D62424: fstyp: locate the latest APFS container superblock in checkpoint descriptor area
Attached
Detach File
Event Timeline