Skip to content

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 super logical partitions and physical boot partitions first; see Storage Layout & super Orchestration.
  • Prerequisite (Read-Only Modem & Calibration Partitions): Where present (persist, modem_*, nvram, nvdata, nvcfg, protect1, protect2), verify they mount strictly ro,norecovery prior to userland start.
  • Recommendation (OLED Burn-In Protection): On OLED panels, ensure BRI=0 and 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-up

Modern consumer phones rarely expose UART test pads without hardware modification. Stage 0 establishes reliable early observability and instant rollback before attempting full userland boot.

  1. Archive Stock Boot Partitions: Back up active-slot boot, init_boot, vendor_boot, dtbo, and vbmeta images offline outside Git.
  2. Instrument init_boot with UniOHOS/core/early-bringup:
    • misc Persistent Ring Buffer: Write early /dev/kmsg and 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 ACM Console: Bring up usb-udc-guard early in PID1 to expose /dev/ttyACM0 over USB alongside hdc, 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

  1. Directory umask Trap: Explicitly run chmod 0755 /dev (and /dev/unix/socket). If early mkdir("/dev") inherits 0700, non-root OpenHarmony services fail to connect to Unix domain sockets silently.
  2. Watchdog Module Ordering Trap: When loading the vendor .ko dependency closure in PID1, treat EEXIST (module already loaded) as non-fatal and ensure hardware watchdog drivers finish loading promptly; otherwise the SoC watchdog forces a fixed-uptime hardware reset.
  3. Android vs OpenHarmony Mount Conflict: Do not leave AOSP first_stage_init CHECKCALL(mount(...)) logic running against already-mounted /dev, /proc, or /sys — EBUSY causes 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
  1. Lock Down GKI 6.x Kernel & Vendor .ko Compatibility (UniOHOS/gki-sdk):
    • Modern Linux 6.6 / 6.18 GKI kernels enforce EXTENDED_MODVERSIONS symbol CRCs, KCFI, and strict ARM64 module relocations (exact .gnu.linkonce.this_module size and zero GOT 311/312 relocations).
    • Run offline verification gates on your host before every kernel or out-of-tree .ko flash (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
  2. Bring Up the OpenHarmony Two-Stage Init Chain:
    • First Stage (init_early / custom stage1-init): Mounts /proc, /sys, /dev, parses super LP metadata to instantiate dm-linear block devices, loads essential early .ko closures in topological dependency order (gki-module-inventory), mounts the OpenHarmony rootfs onto /usr (the standard OpenHarmony switch_root staging point), and processes fstab.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 launches samgr, hilogd, hdf_devmgr, and hdcd.
    • Audit Init Triggers: Inspect stock init.cfg and fstab entries so no OEM factory-reset or recovery trigger formats or unmounts system or userdata.

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
  1. Display Hardware Abstraction (composer_vdi & buffer_vdi):
    • Implement composer_vdi over standard Linux DRM/KMS Atomic Modesetting (/dev/dri/card0): enumerate connectors, CRTCs, and DPU hardware planes, map compositor layers onto planes, and wire vsync + present_fence synchronization.
    • Implement buffer_vdi over Linux DMA-BUF Heaps (/dev/dma_heap/system or SoC-specific heaps), ensuring stride alignment, pixel format translation (RGBA8888, RGBX8888, NV12, P010), and proper descriptor permissions for cross-process buffer sharing.
  2. 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 llvmpipe during 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.
  3. Connect Input (evdev) & Display Geometry:
    • Verify /dev/input/event* nodes report accurate ABS_MT_POSITION_{X,Y} ranges (or laptop keyboard/touchpad events), configure physical DPI, and wire multimodalinput.
  4. Iterate Fast with Single-File Hotbind:
    • Do not repack multi-gigabyte super images for every single-library rebuild. Use hotbind_test_harness.sh to test one VDI shared object or daemon via MS_BIND, then merge verified binaries into super.

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
  1. 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 /vendor strictly MS_RDONLY and calibration/modem partitions ro,norecovery.
    • Bridge hardware services over typed SOCK_SEQPACKET Unix sockets with SO_PEERCRED caller verification and SCM_RIGHTS zero-copy DMA-BUF FD passing:
      • Video Codec (VPU): Zero-copy DMA-BUF hardware 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.
    • Because each subsystem sits behind an explicit IPC/VDI boundary, any service can be replaced independently by a native open-source driver once available.
  2. In-Process TPIDR_EL0 Dual-TLS GPU Bridge (UniOHOS/core/hybris-bridge):
    • ARM64 proprietary GPU libraries inline mrs xN, tpidr_el0 assuming Bionic TCB offsets, which crashes if called directly from Musl threads.
    • hybris-bridge uses static ARM64 bti c trampolines (.text + .data.rel.ro, zero TEXTREL, PAC/BTI-safe) to swap TPIDR_EL0 to a paired Bionic TCB on entry and restore Musl TPIDR_EL0 on return, automatically falling back to Stage 2 open-source Mesa when vendor GPU libraries are absent.
  3. 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:
      bash
      uniohos-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} via profiles/<vendor>/<codename>/ (or an optional device_overlay_repo) and a distinct dtbo_target.

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 Ready
  1. SELinux Enforcing=1 Transition:
    • Maintain neverallow == 0 across all incremental .cil policy deltas.
    • Collect and resolve AVC denials during Stage 3 (Permissive=0 with full audit visibility), verify /data security contexts, then switch to Day-0 Enforcing=1 (or atomic switch at BOOT_DONE via enforcing-boot-guard).
  2. Low-Power Standby & Storage Optimization:
    • Replace polling loops in custom daemons with event-driven epoll / netlink / uevent listeners and freeze idle background containers (cgroup.freeze), targeting < 1% CPU combined daemon load in deep sleep (mem or s2idle).
    • Align hot read-only libraries listed in tier0-preload.txt on 64KB boundaries (PTE_CONT) via UniOHOS/builder/fs-tools and verify repeated cold boots plus multi-subsystem stability regression (bench-suite).

Stage 4 Exit Criterion: getenforce == Enforcing, neverallow == 0, deep sleep power budget verified, and repeated cold-boot regressions green.

Independent Open-Source Community (CC-BY-4.0). Not affiliated with Huawei, HiSilicon, OpenAtom Foundation, Xiaomi, Qualcomm, MediaTek, or Arm.