Device Porting Guide (Stage 0 to Stage 4)
This guide defines the end-to-end engineering workflow for porting OpenHarmony / HarmonyOS onto modern ARM64 A/B Dynamic-Partition (super) devices (as well as non-A/B laptops and SBCs), aligned with DEVICE-GUIDELINES.md.
Prerequisites & Recommendations
- Prerequisite (Partition Storage Plan): Plan your
superlogical partitions and physical boot partitions first; see Storage Layout &superOrchestration. - Prerequisite (Read-Only Modem & Calibration Partitions): Where present (
persist,modem_*,nvram,nvdata,nvcfg,protect1,protect2), verify they mount strictlyro,norecoveryprior to userland start. - Recommendation (OLED Burn-In Protection): On OLED panels, ensure
BRI=0and panel power-off during unattended screen-off test runs. - Recommendation (Thermal & Charge Protection): During long automated runs, enable thermal current limiting or
80%charge bypass where supported by hardware.
Stage 0: Early Bring-up (Solderless Logging & First-Stage PID1)
Stage 0: Early Bring-upModern consumer phones rarely expose UART test pads without hardware modification. Stage 0 establishes reliable early observability and instant rollback before attempting full userland boot.
- Archive Stock Boot Partitions: Back up active-slot
boot,init_boot,vendor_boot,dtbo, andvbmetaimages offline outside Git. - Instrument
init_bootwithUniOHOS/core/early-bringup:miscPersistent Ring Buffer: Write early/dev/kmsgand PID1 stage checkpoints into a reserved offset (misc+0x1000). Even after a hard reboot or kernel panic, booting back into stock OS or fastboot lets you read back the exact last checkpoint.- Early ConfigFS
USB ACMConsole: Bring upusb-udc-guardearly in PID1 to expose/dev/ttyACM0over USB alongsidehdc, providing live interactive logging without soldering UART wires. - PC / Laptop Targets: On laptops and SBCs with Ethernet or exposed UART (such as Huawei Qingyun L420x), configure Ethernet serial transport or UART console as the Stage 0 channel.
Three Classic Custom-PID1 Pitfalls
- Directory
umaskTrap: Explicitly runchmod 0755 /dev(and/dev/unix/socket). If earlymkdir("/dev")inherits0700, non-root OpenHarmony services fail to connect to Unix domain sockets silently. - Watchdog Module Ordering Trap: When loading the vendor
.kodependency closure in PID1, treatEEXIST(module already loaded) as non-fatal and ensure hardware watchdog drivers finish loading promptly; otherwise the SoC watchdog forces a fixed-uptime hardware reset. - Android vs OpenHarmony Mount Conflict: Do not leave AOSP
first_stage_initCHECKCALL(mount(...))logic running against already-mounted/dev,/proc, or/sys—EBUSYcauses an immediate fatal abort.
Stage 0 Exit Criterion: Repeatable early kernel + PID1 log capture verified alongside confirmed rollback to stock OS.
Stage 1: Minimal Boot (GKI 6.x ABI Alignment & Two-Stage switch_root)
Stage 1: Minimal Boot- Lock Down GKI 6.x Kernel & Vendor
.koCompatibility (UniOHOS/gki-sdk):- Modern Linux 6.6 / 6.18 GKI kernels enforce
EXTENDED_MODVERSIONSsymbol CRCs,KCFI, and strict ARM64 module relocations (exact.gnu.linkonce.this_modulesize and zero GOT311/312relocations). - Run offline verification gates on your host before every kernel or out-of-tree
.koflash (see full flag documentation in GKI 6.x Verification SDK):bash# Verify zero symbol CRC drift across vendor_boot / vendor_dlkm .ko trees gki-abi-gate --our-symvers out/vmlinux.symvers --moddir vendor_boot=work/vendor_boot_mods --moddir vendor_dlkm=work/vendor_dlkm_mods # Verify zero ARM64 GOT (311/312) relocations and exact .this_module size gki-kcfi-ko-gate --dir out/kmod --expect-this-module-size 0x680 # Compare kernel BTF struct layouts and function prototypes against stock Image gki-btf-diff --a work/Image.stock --b out/vmlinux
- Modern Linux 6.6 / 6.18 GKI kernels enforce
- Bring Up the OpenHarmony Two-Stage Init Chain:
- First Stage (
init_early/ customstage1-init): Mounts/proc,/sys,/dev, parsessuperLP metadata to instantiatedm-linearblock devices, loads essential early.koclosures in topological dependency order (gki-module-inventory), mounts the OpenHarmony rootfs onto/usr(the standard OpenHarmonyswitch_rootstaging point), and processesfstab.required. - Root Switch & Second Stage: Executes
switch_root /usr /bin/init --second-stage, transitions into OpenHarmony Musl userland, mounts/system,/vendor,/sys_prod,/chip_prod, and/data, and launchessamgr,hilogd,hdf_devmgr, andhdcd. - Audit Init Triggers: Inspect stock
init.cfgandfstabentries so no OEM factory-reset or recovery trigger formats or unmountssystemoruserdata.
- First Stage (
Stage 1 Exit Criterion: Stable USB hdc shell (or Ethernet hdc) login, readable hilog, GKI ABI gates clean, and all critical modem/calibration partitions verified ro,norecovery.
Stage 2: Graphical Bring-up (DRM/KMS VDI, Input & Open-Source Mesa)
Stage 2: Graphical Bring-up- Display Hardware Abstraction (
composer_vdi&buffer_vdi):- Implement
composer_vdiover standard Linux DRM/KMS Atomic Modesetting (/dev/dri/card0): enumerate connectors, CRTCs, and DPU hardware planes, map compositor layers onto planes, and wirevsync+present_fencesynchronization. - Implement
buffer_vdiover LinuxDMA-BUFHeaps (/dev/dma_heap/systemor SoC-specific heaps), ensuring stride alignment, pixel format translation (RGBA8888,RGBX8888,NV12,P010), and proper descriptor permissions for cross-process buffer sharing.
- Implement
- Start with Open-Source Mesa Graphics:
- Decouple display bring-up from proprietary GPU driver quirks by launching the compositor on open-source Mesa Vulkan/GLES (or software
llvmpipeduring early laptop/PC bring-up):- Arm Mali / Immortalis (Valhall / 5th Gen): Mesa
PanVK/Panfrost. - Qualcomm Adreno: Mesa
Turnip(Vulkan) +Zink. - x86_64 / PC Platforms: Mesa
RADV/ANV/Iris/llvmpipe.
- Arm Mali / Immortalis (Valhall / 5th Gen): Mesa
- Decouple display bring-up from proprietary GPU driver quirks by launching the compositor on open-source Mesa Vulkan/GLES (or software
- Connect Input (
evdev) & Display Geometry:- Verify
/dev/input/event*nodes report accurateABS_MT_POSITION_{X,Y}ranges (or laptop keyboard/touchpad events), configure physical DPI, and wiremultimodalinput.
- Verify
- Iterate Fast with Single-File Hotbind:
- Do not repack multi-gigabyte
superimages for every single-library rebuild. Usehotbind_test_harness.shto test one VDI shared object or daemon viaMS_BIND, then merge verified binaries intosuper.
- Do not repack multi-gigabyte
Stage 2 Exit Criterion: Native DRM/KMS display output, responsive touch or keyboard/touchpad input, basic networking, and Mesa/software rendering online.
Stage 3: Full Hardware Adaptation (Isolated Sidecar, Dual-TLS GPU & Manifests)
Stage 3: Hardware Adaptation- Isolated
Bionic Sidecar(UniOHOS/core/sidecar-runtime):- Never mix Android Bionic shared libraries into OpenHarmony's Musl rootfs. Launch an isolated mount and linker namespace mounting
/vendorstrictlyMS_RDONLYand calibration/modem partitionsro,norecovery. - Bridge hardware services over typed
SOCK_SEQPACKETUnix sockets withSO_PEERCREDcaller verification andSCM_RIGHTSzero-copyDMA-BUFFD passing:- Video Codec (VPU): Zero-copy
DMA-BUFhardware decode/encode (AVC/HEVC/VP9/AV1) into OpenHarmony Codec HDI. - Camera ISP & NPU: Multi-camera ISP daemon, frame/metadata transport, and NPU RPC bridge.
- Audio & Cellular RIL: Speaker/earpiece/BT/USB-DAC audio bridge and
HRil<-> modem RIL adapter.
- Video Codec (VPU): Zero-copy
- Because each subsystem sits behind an explicit IPC/VDI boundary, any service can be replaced independently by a native open-source driver once available.
- Never mix Android Bionic shared libraries into OpenHarmony's Musl rootfs. Launch an isolated mount and linker namespace mounting
- In-Process
TPIDR_EL0Dual-TLS GPU Bridge (UniOHOS/core/hybris-bridge):- ARM64 proprietary GPU libraries inline
mrs xN, tpidr_el0assuming Bionic TCB offsets, which crashes if called directly from Musl threads. hybris-bridgeuses static ARM64bti ctrampolines (.text+.data.rel.ro, zeroTEXTREL, PAC/BTI-safe) to swapTPIDR_EL0to a paired Bionic TCB on entry and restore MuslTPIDR_EL0on return, automatically falling back to Stage 2 open-source Mesa when vendor GPU libraries are absent.
- ARM64 proprietary GPU libraries inline
- Register the Device Manifest & Validate Offline:
- Author
devices/<vendor>/<codename>/(device.yaml,capabilities.json,tier0-preload.txt, bilingual READMEs) following the Device Manifest Schema and run:bashuniohos-builder validate-device devices/<vendor>/<codename>/device.yaml uniohos-builder byof-plan --device devices/<vendor>/<codename>/device.yaml --dry-run - Sibling devices sharing an existing SoC reuse
UniOHOS-Devices/<vendor>-<soc>-{bsp,kernel}viaprofiles/<vendor>/<codename>/(or an optionaldevice_overlay_repo) and a distinctdtbo_target.
- Author
Stage 3 Exit Criterion: Core hardware subsystems operational, device.yaml passing schema validation, and Git history strictly free of proprietary binaries.
Stage 4: Daily-Driver Ready (SELinux Enforcing, Standby & Stability)
Stage 4: Daily-Driver ReadySELinux Enforcing=1Transition:- Maintain
neverallow == 0across all incremental.cilpolicy deltas. - Collect and resolve AVC denials during Stage 3 (
Permissive=0with full audit visibility), verify/datasecurity contexts, then switch to Day-0Enforcing=1(or atomic switch atBOOT_DONEviaenforcing-boot-guard).
- Maintain
- Low-Power Standby & Storage Optimization:
- Replace polling loops in custom daemons with event-driven
epoll/netlink/ueventlisteners and freeze idle background containers (cgroup.freeze), targeting< 1% CPUcombined daemon load in deep sleep (memors2idle). - Align hot read-only libraries listed in
tier0-preload.txton64KBboundaries (PTE_CONT) viaUniOHOS/builder/fs-toolsand verify repeated cold boots plus multi-subsystem stability regression (bench-suite).
- Replace polling loops in custom daemons with event-driven
Stage 4 Exit Criterion: getenforce == Enforcing, neverallow == 0, deep sleep power budget verified, and repeated cold-boot regressions green.