浏览文件
types.go
126 行 · 3.4 KB历史下载| 1 | // SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // Package apfs provides minimal APFS disk image manipulation. |
| 5 | // It reads on-disk structures at known byte offsets per the Apple APFS |
| 6 | // Reference and modifies inode UID/GID fields directly, bypassing |
| 7 | // the kernel VFS. |
| 8 | package apfs |
| 9 | |
| 10 | // Magic numbers. |
| 11 | const ( |
| 12 | nxMagic = 0x4253584E // "NXSB" |
| 13 | apfsMagic = 0x42535041 // "APSB" |
| 14 | ) |
| 15 | |
| 16 | // Object types (lower 16 bits of o_type). |
| 17 | const ( |
| 18 | objectTypeNXSuperblock = 0x01 |
| 19 | objectTypeCheckpointMap = 0x0C |
| 20 | objectTypeOmap = 0x0B |
| 21 | objectTypeBTreeNode = 0x02 |
| 22 | objectTypeFS = 0x0D |
| 23 | objectTypeFSTree = 0x0E |
| 24 | objectTypeBlockRefTree = 0x0F |
| 25 | objectTypeOmapSnapshot = 0x10 |
| 26 | objectTypeNXReaperFS = 0x12 |
| 27 | objectTypeNXReaperListKey = 0x13 |
| 28 | ) |
| 29 | |
| 30 | // Object storage classes (upper 16 bits of o_type, shifted). |
| 31 | const ( |
| 32 | objPhysical = 0x00000000 |
| 33 | objEphemeral = 0x80000000 |
| 34 | objVirtual = 0x00000000 |
| 35 | objTypeMask = 0x0000FFFF |
| 36 | objFlagsMask = 0xFFFF0000 |
| 37 | ) |
| 38 | |
| 39 | // B-tree node flags. |
| 40 | const ( |
| 41 | btnodeRoot = 0x0001 |
| 42 | btnodeLeaf = 0x0002 |
| 43 | btnodeFixedKVSize = 0x0004 |
| 44 | ) |
| 45 | |
| 46 | // Filesystem object types (upper 4 bits of j_key_t.obj_id_and_type). |
| 47 | const ( |
| 48 | apfsTypeInode = 3 |
| 49 | apfsTypeDirRec = 9 |
| 50 | ) |
| 51 | |
| 52 | // Filesystem key masks. |
| 53 | const ( |
| 54 | objIDMask = 0x0FFFFFFFFFFFFFFF |
| 55 | objTypeShift = 60 |
| 56 | ) |
| 57 | |
| 58 | // Well-known inode numbers. |
| 59 | const ( |
| 60 | rootDirInodeNum = 2 |
| 61 | ) |
| 62 | |
| 63 | // noownersPlaceholderID is the on-disk UID and GID that APFS stores |
| 64 | // for files on volumes mounted with the noowners option. |
| 65 | const noownersPlaceholderID = 99 |
| 66 | |
| 67 | // VolRoleData is the APFS volume role for "Data" volumes. |
| 68 | const VolRoleData = 0x0040 // APFS_VOL_ROLE_DATA (shifted representation: (1 << 2) << 4) |
| 69 | |
| 70 | // Container superblock (nx_superblock_t) field offsets from block start. |
| 71 | const ( |
| 72 | nxMagicOff = 32 // uint32 |
| 73 | nxBlockSizeOff = 36 // uint32 |
| 74 | nxXPDescBlocksOff = 104 // uint32 (mask 0x7FFFFFFF for count) |
| 75 | nxXPDescBaseOff = 112 // uint64 (physical block address) |
| 76 | nxXPDescIndexOff = 136 // uint32 |
| 77 | nxXPDescLenOff = 140 // uint32 |
| 78 | nxOmapOIDOff = 160 // uint64 (physical) |
| 79 | nxFSOIDOff = 184 // uint64[100] (virtual OIDs) |
| 80 | nxMaxFileSystems = 100 |
| 81 | nxXPDescBlocksMask = 0x7FFFFFFF |
| 82 | ) |
| 83 | |
| 84 | // Object header (obj_phys_t) field offsets. |
| 85 | const ( |
| 86 | objChecksumOff = 0 // uint64 |
| 87 | objOIDOff = 8 // uint64 |
| 88 | objXIDOff = 16 // uint64 |
| 89 | objTypeOff = 24 // uint32 |
| 90 | objSubtypeOff = 28 // uint32 |
| 91 | objHeaderSize = 32 |
| 92 | ) |
| 93 | |
| 94 | // Volume superblock (apfs_superblock_t) field offsets from block start. |
| 95 | const ( |
| 96 | apfsMagicOff = 32 // uint32 |
| 97 | apfsOmapOIDOff = 128 // uint64 (physical) |
| 98 | apfsRootTreeOIDOff = 136 // uint64 (virtual) |
| 99 | apfsVolNameOff = 704 // 256 bytes UTF-8 |
| 100 | apfsRoleOff = 964 // uint16 |
| 101 | ) |
| 102 | |
| 103 | // Object map (omap_phys_t) field offsets from block start. |
| 104 | const ( |
| 105 | omapTreeOIDOff = 48 // uint64 (physical) |
| 106 | ) |
| 107 | |
| 108 | // B-tree node (btree_node_phys_t) field offsets from block start. |
| 109 | const ( |
| 110 | btnFlagsOff = 32 // uint16 |
| 111 | btnLevelOff = 34 // uint16 |
| 112 | btnNKeysOff = 36 // uint32 |
| 113 | btnTableSpaceOff = 40 // nloc_t: off uint16 + len uint16 |
| 114 | btnDataOff = 56 // start of btn_data[] |
| 115 | ) |
| 116 | |
| 117 | // btree_info_t size (sits at end of root node block). |
| 118 | const btreeInfoSize = 40 |
| 119 | |
| 120 | // j_inode_val_t field offsets (from start of value data). |
| 121 | const ( |
| 122 | inodeParentIDOff = 0 // uint64 |
| 123 | inodePrivateIDOff = 8 // uint64 |
| 124 | inodeOwnerOff = 72 // uint32 |
| 125 | inodeGroupOff = 76 // uint32 |
| 126 | ) |

