use crate::bytes::{get_arr, get_u16, get_u32, get_u64, put_arr, put_u16, put_u32, put_u64};
use super::{ObjectHeader, ParseError};
pub const VOLUME_MAGIC: [u8; 4] = *b"APSB";
pub const VOLUME_NAME_LEN: usize = 256;
pub const MODIFIED_BY_ENTRIES: usize = 8;
pub mod incompat {
pub const CASE_INSENSITIVE: u64 = 0x1;
pub const DATALESS_SNAPSHOTS: u64 = 0x2;
pub const ENCRYPTION_ROLLED: u64 = 0x4;
pub const NORMALIZATION_INSENSITIVE: u64 = 0x8;
pub const INCOMPLETE_RESTORE: u64 = 0x10;
pub const SEALED: u64 = 0x20;
}
pub mod volume_flags {
pub const UNENCRYPTED: u64 = 0x1;
pub const ONE_KEY: u64 = 0x8;
pub const DEFINED: u64 = 0x1FF;
}
#[must_use]
pub const fn role_name(role: u16) -> Option<&'static str> {
Some(match role {
0 => "none",
0x1 => "system",
0x2 => "user",
0x4 => "recovery",
0x8 => "vm",
0x10 => "preboot",
0x20 => "installer",
0x40 => "data",
0x80 => "baseband",
0xC0 => "update",
0x100 => "xart",
0x140 => "hardware",
0x180 => "backup",
0x1C0 => "sidecar",
0x240 => "enterprise",
0x2C0 => "prelogin",
_ => return None,
})
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ModifiedBy {
pub id: [u8; 32],
pub timestamp: u64,
pub last_xid: u64,
}
impl ModifiedBy {
pub const SIZE: usize = 48;
fn read(buf: &[u8], off: usize) -> Self {
Self {
id: get_arr(buf, off),
timestamp: get_u64(buf, off + 32),
last_xid: get_u64(buf, off + 40),
}
}
fn write(&self, buf: &mut [u8], off: usize) {
put_arr(buf, off, &self.id);
put_u64(buf, off + 32, self.timestamp);
put_u64(buf, off + 40, self.last_xid);
}
#[must_use]
pub fn name(&self) -> &[u8] {
let end = self.id.iter().position(|b| *b == 0).unwrap_or(32);
&self.id[..end]
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct VolumeSuperblock {
pub header: ObjectHeader,
pub index: u32,
pub features: u64,
pub readonly_compatible_features: u64,
pub incompatible_features: u64,
pub unmount_time: u64,
pub reserve_blocks: u64,
pub quota_blocks: u64,
pub allocated_blocks: u64,
pub meta_crypto: [u8; 20],
pub root_tree_type: u32,
pub extent_ref_tree_type: u32,
pub snapshot_tree_type: u32,
pub object_map_oid: u64,
pub root_tree_oid: u64,
pub extent_ref_tree_oid: u64,
pub snapshot_tree_oid: u64,
pub revert_to_xid: u64,
pub revert_to_superblock: u64,
pub next_object_id: u64,
pub file_count: u64,
pub directory_count: u64,
pub symlink_count: u64,
pub other_count: u64,
pub snapshot_count: u64,
pub total_blocks_allocated: u64,
pub total_blocks_freed: u64,
pub uuid: [u8; 16],
pub last_modified: u64,
pub flags: u64,
pub formatted_by: ModifiedBy,
pub modified_by: [ModifiedBy; MODIFIED_BY_ENTRIES],
pub name: [u8; VOLUME_NAME_LEN],
pub next_document_id: u32,
pub role: u16,
pub reserved: u16,
pub root_to_xid: u64,
pub encryption_rolling_oid: u64,
pub clone_info_epoch: u64,
pub clone_info_xid: u64,
pub snapshot_extension_oid: u64,
pub volume_group: [u8; 16],
pub integrity_meta_oid: u64,
pub file_extent_tree_oid: u64,
pub file_extent_tree_type: u32,
pub reserved_type: u32,
pub reserved_oid: u64,
}
impl VolumeSuperblock {
pub const SIZE: usize = 1056;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("volume superblock", Self::SIZE, buf)?;
let magic: [u8; 4] = get_arr(buf, 32);
if magic != VOLUME_MAGIC {
return Err(ParseError::BadMagic {
structure: "volume superblock",
found: magic,
});
}
let modified_by =
std::array::from_fn(|i| ModifiedBy::read(buf, 320 + ModifiedBy::SIZE * i));
Ok(Self {
header: ObjectHeader::read_from(buf)?,
index: get_u32(buf, 36),
features: get_u64(buf, 40),
readonly_compatible_features: get_u64(buf, 48),
incompatible_features: get_u64(buf, 56),
unmount_time: get_u64(buf, 64),
reserve_blocks: get_u64(buf, 72),
quota_blocks: get_u64(buf, 80),
allocated_blocks: get_u64(buf, 88),
meta_crypto: get_arr(buf, 96),
root_tree_type: get_u32(buf, 116),
extent_ref_tree_type: get_u32(buf, 120),
snapshot_tree_type: get_u32(buf, 124),
object_map_oid: get_u64(buf, 128),
root_tree_oid: get_u64(buf, 136),
extent_ref_tree_oid: get_u64(buf, 144),
snapshot_tree_oid: get_u64(buf, 152),
revert_to_xid: get_u64(buf, 160),
revert_to_superblock: get_u64(buf, 168),
next_object_id: get_u64(buf, 176),
file_count: get_u64(buf, 184),
directory_count: get_u64(buf, 192),
symlink_count: get_u64(buf, 200),
other_count: get_u64(buf, 208),
snapshot_count: get_u64(buf, 216),
total_blocks_allocated: get_u64(buf, 224),
total_blocks_freed: get_u64(buf, 232),
uuid: get_arr(buf, 240),
last_modified: get_u64(buf, 256),
flags: get_u64(buf, 264),
formatted_by: ModifiedBy::read(buf, 272),
modified_by,
name: get_arr(buf, 704),
next_document_id: get_u32(buf, 960),
role: get_u16(buf, 964),
reserved: get_u16(buf, 966),
root_to_xid: get_u64(buf, 968),
encryption_rolling_oid: get_u64(buf, 976),
clone_info_epoch: get_u64(buf, 984),
clone_info_xid: get_u64(buf, 992),
snapshot_extension_oid: get_u64(buf, 1000),
volume_group: get_arr(buf, 1008),
integrity_meta_oid: get_u64(buf, 1024),
file_extent_tree_oid: get_u64(buf, 1032),
file_extent_tree_type: get_u32(buf, 1040),
reserved_type: get_u32(buf, 1044),
reserved_oid: get_u64(buf, 1048),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a volume superblock needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
self.header.write_to(buf);
put_arr(buf, 32, &VOLUME_MAGIC);
put_u32(buf, 36, self.index);
put_u64(buf, 40, self.features);
put_u64(buf, 48, self.readonly_compatible_features);
put_u64(buf, 56, self.incompatible_features);
put_u64(buf, 64, self.unmount_time);
put_u64(buf, 72, self.reserve_blocks);
put_u64(buf, 80, self.quota_blocks);
put_u64(buf, 88, self.allocated_blocks);
put_arr(buf, 96, &self.meta_crypto);
put_u32(buf, 116, self.root_tree_type);
put_u32(buf, 120, self.extent_ref_tree_type);
put_u32(buf, 124, self.snapshot_tree_type);
put_u64(buf, 128, self.object_map_oid);
put_u64(buf, 136, self.root_tree_oid);
put_u64(buf, 144, self.extent_ref_tree_oid);
put_u64(buf, 152, self.snapshot_tree_oid);
put_u64(buf, 160, self.revert_to_xid);
put_u64(buf, 168, self.revert_to_superblock);
put_u64(buf, 176, self.next_object_id);
put_u64(buf, 184, self.file_count);
put_u64(buf, 192, self.directory_count);
put_u64(buf, 200, self.symlink_count);
put_u64(buf, 208, self.other_count);
put_u64(buf, 216, self.snapshot_count);
put_u64(buf, 224, self.total_blocks_allocated);
put_u64(buf, 232, self.total_blocks_freed);
put_arr(buf, 240, &self.uuid);
put_u64(buf, 256, self.last_modified);
put_u64(buf, 264, self.flags);
self.formatted_by.write(buf, 272);
for (i, entry) in self.modified_by.iter().enumerate() {
entry.write(buf, 320 + ModifiedBy::SIZE * i);
}
put_arr(buf, 704, &self.name);
put_u32(buf, 960, self.next_document_id);
put_u16(buf, 964, self.role);
put_u16(buf, 966, self.reserved);
put_u64(buf, 968, self.root_to_xid);
put_u64(buf, 976, self.encryption_rolling_oid);
put_u64(buf, 984, self.clone_info_epoch);
put_u64(buf, 992, self.clone_info_xid);
put_u64(buf, 1000, self.snapshot_extension_oid);
put_arr(buf, 1008, &self.volume_group);
put_u64(buf, 1024, self.integrity_meta_oid);
put_u64(buf, 1032, self.file_extent_tree_oid);
put_u32(buf, 1040, self.file_extent_tree_type);
put_u32(buf, 1044, self.reserved_type);
put_u64(buf, 1048, self.reserved_oid);
}
#[must_use]
pub fn name_bytes(&self) -> &[u8] {
let end = self
.name
.iter()
.position(|b| *b == 0)
.unwrap_or(VOLUME_NAME_LEN);
&self.name[..end]
}
#[must_use]
pub const fn is_unencrypted(&self) -> bool {
self.flags & volume_flags::UNENCRYPTED != 0
}
#[must_use]
pub const fn is_case_insensitive(&self) -> bool {
self.incompatible_features & incompat::CASE_INSENSITIVE != 0
}
}
#[cfg(test)]
mod tests {
use super::*;
pub(crate) fn sample() -> VolumeSuperblock {
let mut name = [0u8; VOLUME_NAME_LEN];
name[..4].copy_from_slice(b"Data");
let mut formatted_id = [0u8; 32];
formatted_id[..10].copy_from_slice(b"newfs_apfs");
VolumeSuperblock {
header: ObjectHeader {
checksum: 0,
oid: 1026,
xid: 2,
type_word: 0x0000_000D,
subtype: 0,
},
index: 0,
features: 0x2,
readonly_compatible_features: 0,
incompatible_features: incompat::CASE_INSENSITIVE,
unmount_time: 11,
reserve_blocks: 0,
quota_blocks: 0,
allocated_blocks: 300,
meta_crypto: [0; 20],
root_tree_type: 0x0000_0002,
extent_ref_tree_type: 0x4000_0002,
snapshot_tree_type: 0x4000_0002,
object_map_oid: 500,
root_tree_oid: 1028,
extent_ref_tree_oid: 501,
snapshot_tree_oid: 502,
revert_to_xid: 0,
revert_to_superblock: 0,
next_object_id: 22,
file_count: 3,
directory_count: 2,
symlink_count: 1,
other_count: 0,
snapshot_count: 0,
total_blocks_allocated: 310,
total_blocks_freed: 10,
uuid: [0x33; 16],
last_modified: 12,
flags: volume_flags::UNENCRYPTED,
formatted_by: ModifiedBy {
id: formatted_id,
timestamp: 13,
last_xid: 2,
},
modified_by: [ModifiedBy {
id: [0; 32],
timestamp: 0,
last_xid: 0,
}; MODIFIED_BY_ENTRIES],
name,
next_document_id: 3,
role: 0x40,
reserved: 0,
root_to_xid: 0,
encryption_rolling_oid: 0,
clone_info_epoch: 0,
clone_info_xid: 0,
snapshot_extension_oid: 0,
volume_group: [0; 16],
integrity_meta_oid: 0,
file_extent_tree_oid: 0,
file_extent_tree_type: 0,
reserved_type: 0,
reserved_oid: 0,
}
}
#[test]
fn a_superblock_round_trips() {
let sb = sample();
let mut buf = vec![0u8; 4096];
sb.write_to(&mut buf);
assert_eq!(&buf[32..36], b"APSB");
assert_eq!(VolumeSuperblock::read_from(&buf), Ok(sb.clone()));
assert_eq!(sb.name_bytes(), b"Data");
assert_eq!(sb.formatted_by.name(), b"newfs_apfs");
assert!(sb.is_unencrypted() && sb.is_case_insensitive());
}
#[test]
fn fields_sit_at_the_offsets_the_reference_gives() {
let mut buf = vec![0u8; 4096];
sample().write_to(&mut buf);
assert_eq!(get_u64(&buf, 128), 500, "the object map at 128");
assert_eq!(get_u64(&buf, 136), 1028, "the root tree at 136");
assert_eq!(get_u64(&buf, 264), 1, "the flags at 264");
assert_eq!(&buf[704..708], b"Data", "the name at 704");
assert_eq!(get_u16(&buf, 964), 0x40, "the role at 964");
}
#[test]
fn another_signature_is_not_a_volume() {
let mut buf = vec![0u8; 4096];
sample().write_to(&mut buf);
buf[35] = b'X';
assert!(matches!(
VolumeSuperblock::read_from(&buf),
Err(ParseError::BadMagic { .. })
));
}
#[test]
fn a_name_filling_its_field_has_no_terminator_to_stop_at() {
let mut sb = sample();
sb.name = [b'n'; VOLUME_NAME_LEN];
assert_eq!(sb.name_bytes().len(), VOLUME_NAME_LEN);
}
}