#!/usr/bin/env python3 """HFS+ wrapper images (fs/hfsplus/wrapper.c read_wrapper + read_mdb). wrap_loop : sector 2 carries an HFS wrapper MDB (signature 'BD') whose embedded-volume extent says ablk_start = 0, embed_start = 0, so hfsplus_read_wrapper recomputes part_start to the same value and jumps back to `reread` with identical input. wrap_once : same MDB but ablk_start = 8 and embed_start = 1 with ablk_size 512, so part_start advances to sector 9, where a real HFS+ volume header (made by mkfs.hfsplus) sits. Control: one re-read, then the volume mounts. """ import os, struct, subprocess, sys SEC = 512 def mdb(ablk_size, ablk_start, embed_start, embed_count): b = bytearray(SEC) struct.pack_into('>H', b, 0x00, 0x4244) # HFSP_WRAP_MAGIC struct.pack_into('>H', b, 0x0A, 0x8000 | 0x0200) # SLOCK | SPARED struct.pack_into('>I', b, 0x14, ablk_size) # drAlBlkSiz struct.pack_into('>H', b, 0x1C, ablk_start) # drAlBlSt struct.pack_into('>H', b, 0x7C, 0x482B) # embedded signature H+ struct.pack_into('>HH', b, 0x7E, embed_start, embed_count) # embedded extent return b def main(): kind, out = sys.argv[1], sys.argv[2] if os.path.exists(out): os.unlink(out) open(out, 'wb').truncate(64 << 20) if kind == 'wrap_step': # Same MDB, but the extent advances part_start by 9 sectors each time, so each re-read looks at a new # sector; nothing there is a wrapper, so the loop ends after one extra read. Control for wrap_loop. with open(out, 'r+b') as f: f.seek(2 * SEC) f.write(mdb(512, 8, 1, 1024)) print('wrap_step: MDB at sector 2 -> part_start advances to 9 each pass') elif kind == 'wrap_once': # a real HFS+ volume starting at sector 9: build one, then shift it by 9 sectors tmp = out + '.inner' open(tmp, 'wb').truncate(32 << 20) subprocess.check_call(['mkfs.hfsplus', '-v', 'T', tmp], stdout=subprocess.DEVNULL) inner = open(tmp, 'rb').read() os.unlink(tmp) with open(out, 'r+b') as f: f.seek(9 * SEC) f.write(inner) f.seek(2 * SEC) f.write(mdb(512, 8, 1, (32 << 20) // SEC)) print('wrap_once: MDB at sector 2 -> part_start 0+8+1*1 = 9, HFS+ volume header at sector 9+2') else: with open(out, 'r+b') as f: f.seek(2 * SEC) f.write(mdb(512, 0, 0, 1024)) print('wrap_loop: MDB at sector 2, ablk_start=0 embed_start=0 -> part_start unchanged') if __name__ == '__main__': main()