mirror of
https://github.com/UniXNU/UniXNU.git
synced 2026-11-18 18:11:42 +03:00
ab11a8cb68
Source-driver and source-storage builds, the arm64e PAC rewrite for the Rust bridge, the IOAccelerator base ABI of 24A446, the bridge with mimalloc and link-time optimisation, the plugin with the prime list, addon options for a prime list, a pipeline pack and cache export, wallpaper overlay and seed changes, and payload, kernel and userdata steps for the new profile.
138 lines
10 KiB
Python
Executable File
138 lines
10 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Generate a synthetic Apple-format DT for the single-CPU QEMU entry fixture.
|
|
|
|
This is neither sheng board data nor an iPad device identity. The arm-io
|
|
range describes QEMU PL011 solely to pass early platform discovery.
|
|
"""
|
|
from pathlib import Path
|
|
import argparse
|
|
import os
|
|
import struct
|
|
import zlib
|
|
import uuid
|
|
import platform
|
|
import subprocess
|
|
|
|
|
|
import sys
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from unixnu.devicetree import node, empty_nvram_proxy, read_fdt, registers
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--output', type=Path, default=Path(__file__).resolve().parents[2] / 'build/xnu-entry/qemu-test.adt')
|
|
parser.add_argument('--owned-memory-size', type=lambda value: int(value, 0), help='Request a real contiguous DarwinBoot allocation through chosen/darwinboot-memory-size; 256 MiB to 4 GiB in 2 MiB steps')
|
|
parser.add_argument('--dram-size', type=lambda value: int(value, 0), default=0x20000000)
|
|
parser.add_argument('--qemu-accel', choices=['tcg', 'hvf'], default='tcg', help='Offline CPU description; HVF uses the actual host counter frequency and a generic ARM CPU identity')
|
|
parser.add_argument('--qemu-vmm-present', action='store_true', help='Report the actual offline QEMU environment through the native XNU defaults ABI; never board data')
|
|
parser.add_argument('--armvirt', action='store_true', help='Describe actual QEMU virt/A710/GICv3 devices for the platform driver')
|
|
parser.add_argument('--platform-compatible', choices=['linux,dummy-virt', 'unmatched-test-platform', 'xiaomi,sheng'], default='linux,dummy-virt')
|
|
parser.add_argument('--smp', type=int, default=1, help='Number of QEMU CPUs; Affinity0 numbers them, PSCI conduit is hvc (virt without EL2)')
|
|
parser.add_argument('--armcpu', action='store_true', help='Add the real QEMU GICv3 distributor/redistributor DT resources')
|
|
parser.add_argument('--data-ramdisk', action='store_true', help='Provide a writable scratch RAMDisk sidecar while keeping actual disk root matching')
|
|
parser.add_argument('--ramdisk', action='store_true', help='Provide an owned memory-disk binding placeholder for the loader')
|
|
parser.add_argument('--nvram-proxy', action='store_true', help='Initialize real IODTNVRAM from an empty volatile CHRP bank; no persistence controller')
|
|
parser.add_argument('--conflicting-boot-manifest', action='store_true', help='Negative offline identity-collision input: install an untrusted A5 hash which owner publication must refuse to replace')
|
|
parser.add_argument('--crypto-hash-method', choices=['sha2-384', 'sha1'], help='Explicit Image4 hash algorithm; target J120 DT specifies sha2-384')
|
|
parser.add_argument('--osenvironment', choices=['ramdisk','recoveryos','main'], help='Explicit /chosen/osenvironment fixture; the kernel publishes it as hw.osenvironment')
|
|
parser.add_argument('--amfi-platform-only', action='store_true', help='Require platform code in the offline AMFI policy fixture')
|
|
parser.add_argument('--trust-cache', action='store_true', help='Provide a zeroed owned TrustCache sidecar binding')
|
|
parser.add_argument('--virtio-mmio-fdt', type=Path, help='Describe actual VirtIO resource windows from a QEMU-generated FDT')
|
|
parser.add_argument('--root-match-volume-uuid', type=uuid.UUID, help='Match an actual IOMedia UUID through the source root-matching mechanism')
|
|
args = parser.parse_args()
|
|
if args.owned_memory_size is not None and (not 0x10000000 <= args.owned_memory_size <= 0x100000000 or args.owned_memory_size % 0x200000):
|
|
parser.error('Owned memory must be 256 MiB to 4 GiB, aligned to 2 MiB')
|
|
timebase = 62500000
|
|
if args.qemu_accel == 'hvf':
|
|
if platform.system() != 'Darwin' or platform.machine() != 'arm64' or not args.armvirt or not args.qemu_vmm_present:
|
|
parser.error('HVF requires the actual macOS ARM64 QEMU fixture and explicit VMM declaration')
|
|
timebase = int(subprocess.check_output(['sysctl', '-n', 'hw.tbfrequency'], text=True))
|
|
if not 0 < timebase < 1 << 32:
|
|
parser.error('Host counter frequency is outside the DT u32 ABI')
|
|
if args.qemu_vmm_present and not args.armvirt:
|
|
parser.error('--qemu-vmm-present requires the actual QEMU platform fixture')
|
|
if args.armcpu and not args.armvirt:
|
|
parser.error('--armcpu requires --armvirt')
|
|
if not 1 <= args.smp <= 8 or (args.smp > 1 and not args.armcpu):
|
|
parser.error('--smp is 1..8 and needs --armcpu')
|
|
if args.root_match_volume_uuid and (not args.virtio_mmio_fdt or args.ramdisk):
|
|
parser.error('Direct volume matching requires a real offline disk and conflicts with a RAM root')
|
|
if args.data_ramdisk and (not args.root_match_volume_uuid or args.ramdisk):
|
|
parser.error('Scratch Data requires actual disk root matching and no RAM root')
|
|
root_matching = []
|
|
if args.root_match_volume_uuid:
|
|
xml = ('<dict><key>IOProviderClass</key><string>IOMedia</string>'
|
|
'<key>IOPropertyMatch</key><dict><key>UUID</key><string>' +
|
|
str(args.root_match_volume_uuid).upper() + '</string></dict></dict>\0')
|
|
root_matching = [('root-matching', xml.encode())]
|
|
virtio_nodes = []
|
|
if args.virtio_mmio_fdt:
|
|
if not args.armvirt:
|
|
parser.error('--virtio-mmio-fdt requires --armvirt')
|
|
root = read_fdt(args.virtio_mmio_fdt.read_bytes())
|
|
entries = []
|
|
for entry in root.walk():
|
|
if 'virtio,mmio' not in entry.strings('compatible'):
|
|
continue
|
|
if (entry.parent.scalar('#address-cells'), entry.parent.scalar('#size-cells')) != (2, 2):
|
|
raise SystemExit('Unexpected QEMU VirtIO parent address encoding')
|
|
regions = registers(entry)
|
|
if len(regions) != 1:
|
|
raise SystemExit('VirtIO requires one resource window')
|
|
address, size = regions[0]
|
|
if not 0x08000000 <= address < address + size <= 0x0c000000 or size < 0x108 or 'dma-coherent' not in entry.properties:
|
|
raise SystemExit('VirtIO resource is outside the established QEMU bus or not coherent')
|
|
entries.append((address, size))
|
|
if not entries or len(entries) != len(set(entries)):
|
|
raise SystemExit('Missing or repeated actual VirtIO resource windows')
|
|
for index, (address, size) in enumerate(sorted(entries)):
|
|
virtio_nodes.append(node('virtio-mmio-' + str(index), [('compatible', b'virtio,mmio\0'),
|
|
('reg', struct.pack('<QQ', address - 0x08000000, size)), ('dma-coherent', b'')]))
|
|
cpu = node('cpu0', [('state', b'running\0'), ('reg', struct.pack('<I', 0)),
|
|
*([('device_type', b'cpu\0'), ('compatible', b'arm,cortex-a710\0' if args.qemu_accel == 'tcg' else b'arm,armv8\0')] if args.armvirt else []),
|
|
('timebase-frequency', struct.pack('<I', timebase)),
|
|
*([('clock-frequency', struct.pack('<I', 1000000000))] if args.qemu_accel == 'tcg' else [])])
|
|
tree = node('device-tree', [('model', b'qemu-entry-fixture\0'),
|
|
*([('#address-cells', struct.pack('<I', 2)), ('#size-cells', struct.pack('<I', 2))] if virtio_nodes else []),
|
|
*([('compatible', args.platform_compatible.encode() + b'\0')] if args.armvirt else [])], [
|
|
node('chosen', [('random-seed', os.urandom(256)),
|
|
# This fixture's writable Data lives only in the owned
|
|
# RAMDisk. Publish its actual nonpersistent lifetime via
|
|
# XNU's standard hw.ephemeral_storage device-tree input.
|
|
*([('ephemeral-storage', struct.pack('<I', 1))] if args.data_ramdisk else []),
|
|
*([('osenvironment', args.osenvironment.encode() + b'\0')] if args.osenvironment else []),
|
|
*([('darwinboot-memory-size', struct.pack('<Q', args.owned_memory_size))] if args.owned_memory_size else []),
|
|
*root_matching,
|
|
*([('boot-manifest-hash', bytes([0xa5]) * 48)] if args.conflicting_boot_manifest else []),
|
|
('dram-base', struct.pack('<Q', 0x40000000)),
|
|
('dram-size', struct.pack('<Q', args.dram_size)),
|
|
*([('crypto-hash-method', args.crypto_hash_method.encode() + b'\0')] if args.crypto_hash_method else []),
|
|
*([('amfi-only-platform-code', struct.pack('<I', 1))] if args.amfi_platform_only else []),
|
|
*(empty_nvram_proxy() if args.nvram_proxy else [])], [node('memory-map',
|
|
[*([('RAMDisk', bytes(16))] if args.ramdisk or args.data_ramdisk else []),
|
|
*([('TrustCache', bytes(16))] if args.trust_cache else [])]),
|
|
*([node('manifest-properties')] if args.armvirt else [])]),
|
|
node('cpus', children=[cpu, *[node('cpu%d' % index, [('state', b'waiting\0'), ('reg', struct.pack('<I', index)),
|
|
('device_type', b'cpu\0'), ('compatible', b'arm,cortex-a710\0' if args.qemu_accel == 'tcg' else b'arm,armv8\0'),
|
|
('timebase-frequency', struct.pack('<I', timebase))]) for index in range(1, args.smp)]]),
|
|
*([node('psci', [('compatible', b'arm,psci-1.0\0'), ('method', b'hvc\0')])] if args.smp > 1 else []), node('defaults',
|
|
[('vmm-present', struct.pack('<I', 1))] if args.qemu_vmm_present else []),
|
|
*([node('options')] if args.nvram_proxy else []),
|
|
*([node('product', [('product-name', b'QEMU virt\0'),
|
|
('model', b'linux,dummy-virt\0')])] if args.armvirt else []),
|
|
node('arm-io', [('device_type', b'qemu-virt\0'),
|
|
*([('#address-cells', struct.pack('<I', 2)), ('#size-cells', struct.pack('<I', 2))] if virtio_nodes else []),
|
|
('ranges', struct.pack('<QQQ', 0, 0x08000000, 0x04000000) if args.armvirt
|
|
else struct.pack('<QQQ', 0, 0x09000000, 0x1000))],
|
|
([node('intc', [('compatible', b'arm,gic-v3\0'),
|
|
('reg', struct.pack('<4Q', 0, 0x10000, 0xa0000, 0xf60000))])]
|
|
if args.armcpu else []) +
|
|
([node('ufshc', [('compatible', b'qcom,sm8550-ufshc\0'),
|
|
('reg', struct.pack('<2Q', 0x03ff0000, 0x3000))]),
|
|
node('ufsphy', [('compatible', b'qcom,sm8550-qmp-ufs-phy\0'),
|
|
('reg', struct.pack('<2Q', 0x03fe0000, 0x2000))])]
|
|
if args.armvirt else []) + virtio_nodes)])
|
|
output = args.output
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_bytes(tree)
|
|
print(f'Synthetic QEMU DT: {len(tree)} bytes, timebase {timebase} Hz')
|