Files
thebearsandClaude Opus 5.5 07da135257 Add the vendor firmware: stock flash dump, update file, both unpacked
vendor/ holds the camera's 16 MB NOR dump (backup-ssc377d-nor16m.bin), the
vendor update V3.6.1.3, and their unpacked trees, which the RE tools and
notes read. verify.py now defaults to vendor/flash_dump_ssc377d, and the
docs point at vendor/ instead of folders beside the checkout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EfVNki7giKruPunvux6dDA
2026-10-29 14:23:37 -04:00

19 KiB
Raw Permalink Blame History

OpenIPC on the AnjoyVision MC-L18D

A port of OpenIPC to the AnjoyVision MC-L18D:

  • SoC: SigmaStar SSC377D (Infinity6C), a copy of SigmaStar's SSC027A-S01A reference board;
  • sensor: GalaxyCore GC5603;
  • flash: 16 MB SPI NOR;
  • optional extras on connector D7: a zoom/focus lens board and a Pelco-D pan/tilt head.

This repository holds the reverse-engineering notes and tools, the built image, and the changes to OpenIPC's repositories. Those repositories are submodules pinned to their upstream commits, and the changes to each one are a patch in patches/.

This README walks through how the port was done, in order. Each step links to the document that covers it in depth.


Status

Works on the camera:

  • boots OpenIPC from the 16 MB layout;
  • GC5603 video at 2960x1666 through majestic (RTSP, web UI, snapshots);
  • pan, tilt, zoom, focus and iris from the shell (pelco-ptz) and over ONVIF on port 85 (ContinuousMove, Stop, presets);
  • one-shot autofocus (mcl18d-af): 12–27 s, ending at 99.4–99.6% of the sharpest snapshot seen;
  • night-mode switching: majestic follows the light board's GPIO 23 and drives the IR-cut and IR lamp pins.

Open:

  • rare colour flares (a frame fills with noise below a random row), cause unknown;
  • the light board's IR LEDs never lit during testing;
  • the head only takes presets, so ONVIF AbsoluteMove and RelativeMove are not offered;
  • the lens board's own autofocus can't run, because majestic doesn't export the ISP's AF statistics on this chip;
  • SD card, audio and GPIO 10/61 not yet checked on hardware.

Layout

Path What it is
firmware/ submodule: OpenIPC/firmware at 7d213e9. Its patch adds the board, the sensor driver, the board package and the ONVIF fixes. This is the only tree you need to build.
sensors/ submodule: OpenIPC/sensors. Its patch adds sigmastar/infinity6c/sensor_gc5603_mipi.c
linux-infinity6c/ submodule: OpenIPC/linux, branch sigmastar-infinity6c. Its patch adds the MC-L18D device tree. About 1.3 GB
onvif_simple_server/ submodule: roleoroleo/onvif_simple_server. Its patch fixes PTZ bugs
ipctool/, majestic-af/ submodules, reference only, no changes
patches/ <submodule>.patch for each submodule with changes, and export.sh, which regenerates them
images/ the built firmware: openipc.ssc377d-nor-lite.tgz, uImage.ssc377d, rootfs.squashfs.ssc377d
vendor/ the stock firmware: the camera's 16 MB flash dump, the vendor's update file, and both unpacked
af-board-re/ lens-board protocol tools: ARM emulator, PLT and literal-pool resolvers, verify.py
gc5603-difftest/ differential test running the vendor's and the rebuilt sensor driver side by side
builder/Dockerfile the Ubuntu 24.04 build container
LEARNING-GUIDE.md the whole port: sources for every fact, every change, tests, mistakes
MCL18D-PORT.md short port notes and pin map
TECH-SPEC.md specification: interfaces, configs, protocols, algorithms, verification matrix
AF-BOARD-PROTOCOL.md the lens-board serial protocol, byte by byte
CODE-CHANGES.md every changed file, in full

The vendor firmware is in vendor/. The RE steps start from these files:

Path What it is
vendor/backup-ssc377d-nor16m.bin full 16 MB NOR dump of this camera before OpenIPC: boot, kernel, rootfs (app V3.4.5.2) and settings. The only way back to stock
vendor/r1885__firmware_MCL18D_V0-A-H5_V3.6.1.3_20260917092033.bin the vendor's update file: same kernel and sensor driver, newer app
vendor/flash_dump_ssc377d/ the dump unpacked: boot.bin, kernel, rootfs/, and the settings partition in data/
vendor/firmware_MCL18D_V3.6.1.3/ the update file unpacked, plus the RE outputs board.dts (the vendor device tree) and gpio_map.md

The dump carries this unit's MAC address and settings. In vendor/firmware_MCL18D_V3.6.1.3/, two files did not come from the vendor: passwd_sys.orig.bak and rootfs.new.squashfs (guide §18).


Getting the sources

git clone ssh://gitea@winslowthehedgehog.com/thebears/mcfl18d_openipc_re.git
cd mcfl18d_openipc_re

# Enough to build: the firmware patch carries the other trees' changes as Buildroot patches
git submodule update --init firmware
git -C firmware apply ../patches/firmware.patch

# Everything (linux-infinity6c is a large download)
git submodule update --init
for p in patches/*.patch; do git -C "$(basename "$p" .patch)" apply "../$p"; done

The changes stay uncommitted in each submodule, so git -C firmware status shows them. After changing a submodule, run patches/export.sh to rewrite its patch, then commit the patch here.


How it was done

1. Learn how OpenIPC builds an image

You need this picture before any of the changes make sense (guide §2):

  • OpenIPC firmware is a Buildroot external tree. make BOARD=x picks br-ext-chip-<vendor>/configs/x_defconfig.
  • The kernel comes from OpenIPC/linux as a tarball. On SigmaStar the device tree is chosen by the kernel option CONFIG_SS_DTB_NAME, so a new board needs a new .dts and a kernel config that names it.
  • Sensor drivers are separate modules built from OpenIPC/sensors. At boot load_sigmastar loads the one named in the U-Boot variable sensor.
  • majestic, the closed-source streamer, needs an IQ (tuning) file per sensor and reads /etc/majestic.yaml.
  • Per-board settings go in sanctioned hooks, customizer.sh and muxes.sh, never in shared files.

2. Take the vendor firmware apart

(guide §3)

Both inputs, and what they unpack to, are in vendor/.

The update file. binwalk and xxd show a 1556-byte vendor header (ANJOY888), a uImage kernel and an xz SquashFS. The uImage header says LZMA but the payload is xz: trust the magic bytes, not the header.

dd if=update.bin of=kernel.uImage bs=1 skip=1556 count=$((1899576+64))
dd if=kernel.uImage of=kernel.xz bs=64 skip=1
xz -dc kernel.xz > kernel.img
dd if=update.bin of=rootfs.squashfs bs=1 skip=1901196 count=8897236
unsquashfs -d rootfs rootfs.squashfs

To get the device tree, search kernel.img for the DTB magic D0 0D FE ED, keep the candidate whose totalsize field makes sense, carve it out and decompile it with dtc -I dtb -O dts.

The flash dump. The U-Boot environment's mtdparts gives the partitions: BOOT 192 KB, kernel 2 MB, rootfs 0xCD0000, DATA 1 MB (jffs2, unpacked with jefferson). The kernel and sensor driver match the update file. DATA holds the live settings (config.xml): lens board on, Pelco-D on COM1. Keep the dump. It is the only way back to stock.

Where the vendor's logic lives: libtools.so (board tables), media_server (video, ISP, lamps, AF data), comm_server (serial: PTZ, lens board, upgrade), gc5603_MIPI_1125.ko (sensor), gc4023_day.bin (the IQ file this board really runs). The tools were strings, objdump, radare2, unicorn, pyelftools, dtc and jefferson.

3. Map the hardware

(guide §4, pin map in MCL18D-PORT.md)

The app stores no pin numbers as strings. Find gpio_init_by_array, follow its caller to the array for this board (device type 0x681), and decode each entry's type code from the getter functions that read it (the reset-button getter looks for type 3, and so on). Name each pin by what its consumer does with it, not by where it sits in the table. The IR-cut pins and the light sensor come from the vendor kernel's /dev/irled driver, and their direction from disassembling media_server's day/night functions.

4. Kernel: the device tree

(guide §5, patch: patches/linux-infinity6c.patch)

  1. Compile OpenIPC's own infinity6c-ssc027a-s01a.dts.
  2. Decompile it and the vendor's DTB with dtc -s (sorted), and diff node by node.
  3. Decode the padmux entries into the kernel's macro names.

The differences: the white lamp, IR lamp and Ethernet LED pads, the 2-wire FUART to connector D7, and slower SD card settings. The new DTS #includes the reference DTS and overrides only padmux and sdmmc, so upstream fixes still flow in. The kernel build's own checks pass ("The configuration of GPIO & PADMUX is correct!").

5. Rebuild the GC5603 sensor driver

(guide §6, patch: patches/sensors.patch)

The vendor's .ko can't be loaded on OpenIPC. Its ms_cus_sensor struct lacks OpenIPC's SPI fields and two frame-sync fields, so every callback pointer sits 24 bytes off. That was measured with offsetof on OpenIPC's side and from the vendor's load offsets (ldr r6, [r0, #0x40]) on the other.

So the driver was rewritten:

  1. Disassemble the .ko with relocations, so calls and table references resolve to names.
  2. Generate the register tables (init, gain, exposure, VTS, mirror/flip) from its read-only data. Never hand-copy them.
  3. Recover the constants from the code: BG Bayer, 10-bit, 2-lane MIPI, 27 MHz MCLK, I2C 0x62, VTS 1750 at 30 fps.
  4. Keep the vendor's temperature-dependent black-level logic.
  5. Write it in OpenIPC's sensor-driver style (sensor_gc5603_mipi.c, about 950 lines).

Verify without the camera (gc5603-difftest/): emu_ko.py loads a kernel module into unicorn, applies its relocations, stubs the kernel and attaches a fake sensor that records every I2C transaction. difftest.py drives the vendor's module and the new one through init, gain and exposure sweeps, FPS, orientation and temperature levels, and compares every register write: 76,011 checks, 0 mismatches.

Prove the test can fail. Plant bugs and check they are caught. The first version missed two planted bugs in the temperature code because it never reached it. The fixed test catches all four (82 to 928 mismatches each).

There is no GC5603 IQ file in the dump. The vendor's media_server loads gc4023_day.bin for this board, so that file ships as /etc/sensors/gc5603.bin.

6. Register the board in OpenIPC

(guide §7, patch: patches/firmware.patch)

  • ssc377d_mcl18d_defconfig: ssc377d_lite plus the board kernel config and DTS patch, 16 MB flash, SNS_MODEL=gc5603, the board package and onvif-simple-server.
  • infinity6c-ssc027a-mcl18d.config: the reference kernel config with CONFIG_SS_DTB_NAME="infinity6c-ssc027a-s01a-mcl18d".
  • load_sigmastar: a new gc5603) case that loads sensor_gc5603_mipi.ko.
  • The DTS and the sensor driver go in as Buildroot patches generated from the other two submodules.
  • ci-matrix.py lists the board and package as not built by CI, so --self-test passes.

7. Write the board package

(guide §8)

firmware/general/package/anjvision-mcl18d/:

  • customizer.sh (first boot): majestic's pin map (IR-cut 11/80, light sensor 23 with lightMonitor true, IR lamp on GPIO 9, white lamp PWM0, speaker 12), streams 2560x1440 + 640x360, majestic's ONVIF off, and a random ONVIF password.
  • muxes.sh (every boot): GPIO 10 and 61 high, as the vendor does.
  • S94mcl18d: starts mcl18d-io, pelco-ptz keepalive and ONVIF discovery.
  • mcl18d-io: reset button (hold 5 s for a factory reset) and alarm input.
  • pelco-ptz (C): Pelco-D for the head and lens board.
  • mcl18d-af: one-shot autofocus.

8. PTZ over ONVIF

(guide §9, patch: patches/onvif_simple_server.patch)

OpenIPC's PTZ plugin (majestic-af) can't load here. Comparing majestic's strings across chip families shows the infinity6c build has no "motorized lens" settings, and majestic is closed source. So ONVIF goes through onvif-simple-server on port 85, whose PTZ hooks run pelco-ptz.

pelco-ptz keeps frame building (pelco.c, pure functions) apart from I/O (main.c), so tests pin every byte. It combines pan and tilt calls that arrive within 300 ms into one diagonal frame, stores presets, and serialises access with flock. Testing it against the real ONVIF server found six upstream bugs, among them an AbsoluteMove that drove the lens fully wide every time, an uninitialised buffer passed to system(), and memory corruption in SetPreset. They are fixed in firmware/general/package/onvif-simple-server/03-ptz-absolute-relative-zoom-fixes.patch.

9. Decode the lens board protocol

(guide §10, spec: AF-BOARD-PROTOCOL.md, tools: af-board-re/)

  • Static reading of comm_server and media_server. radare2 labels this binary's library calls one PLT entry off, so plt.py rebuilds the map from each stub's relocation. od.py resolves movw/movt pairs and literal pools.
  • Emulation (emu.py): run the vendor's own HandleYuntai PTZ dispatcher and AF senders in unicorn with libc stubbed, and record exactly what they write() to the UART.
  • Follow the flags. FC 01 frames at 115200 baud are in the binary, but only for other customers' builds. With af.flag present, as on this camera, the link is /dev/ttyS2 at 57600 8N1, plain Pelco-D at address 1, plus an 18-byte AF statistics frame (CAfV2) every video frame.
  • verify.py re-derives every frame in the spec from the binaries: 40/40 checks pass.
python3 af-board-re/verify.py      # reads vendor/flash_dump_ssc377d; needs unicorn

10. Build

(guide §12)

Buildroot 2024.02's host tar doesn't build on Arch, so the build runs in the Ubuntu 24.04 container from builder/. Build outside any synced folder.

docker build -t openipc-builder:24.04 builder
F=$PWD/firmware; O=~/.cache/openipc-mcl18d
docker run --rm --user $(id -u):$(id -g) -e HOME=$O/home -e BR2_DL_DIR=$O/dl -e PWD=$F \
  -v $F:$F -v $O:$O -w $F openipc-builder:24.04 make BOARD=ssc377d_mcl18d TARGET=$O/output
  • PWD must be passed in, because the Makefile reads it and docker doesn't set it. Without it the build "finishes" instantly. Read the log, not the exit code.
  • The container needs bsdextrautils for hexdump. Without it the kernel's padmux check fails with a misleading dtc "Unable to parse input tree".
  • Result: uImage 1991 / 2048 KB, rootfs 5264 / 8192 KB.

11. Test on the host

(guide §13)

Test Result
gc5603-difftest/ against the vendor driver 76,011 checks, 0 mismatches; 4/4 planted bugs caught
make test in anjvision-mcl18d/src (every Pelco-D frame byte for byte) pass
make e2e (pelco-ptz against a simulated head on a pseudo-terminal) pass
tests/onvif_e2e.py (ONVIF SOAP → patched server → pelco-ptz → simulated head) 23/23; the unpatched server fails 7
make af BUSYBOX=… (mcl18d-af, comment-stripped as shipped, under the tree's own busybox ash, against a simulated lens) 14/14
af-board-re/verify.py 40/40
ci-matrix.py --self-test, shell parse and comment-strip tests pass

12. Flash and bring up on the camera

(guide §20)

Bench: a 3.3 V USB serial adapter on UART0 at 115200, a TFTP server for the images, and a switchable plug to power-cycle the camera.

Getting into U-Boot: the vendor's U-Boot ignores Ctrl-C. It stops on Enter, and only in a very short window, so send Enter continuously from power-on.

Flashing: load each image to RAM over TFTP, check its crc32 against the PC's, write everything except the boot area first, write the boot area last in one command line, then read back and compare. OpenIPC's U-Boot picks the rootfs size from the squashfs already in flash, so writing the larger rootfs is what moves the camera to the 16 MB layout.

First claim: root's password is set at the first login, which also accepts the majestic EULA. Only the camera's owner may do that.

What the hardware then showed, and what changed because of it:

  • Resets and memory corruption were heat. The boot ROM said HW Reset every time, which ruled out software. majestic's /metrics read 76 C. With the heat sink refitted: 63 C, no more resets.
  • Nothing on D7 moved, though the bytes left the UART. Replaying the rest of the vendor's traffic found it: the lens board, and the head behind it, ignore every command unless AF frames keep arriving. Five frames wake it, one frame every 100 ms keeps it awake. The vendor never noticed because it streams them from boot. Hence pelco-ptz keepalive.
  • The head ignores Pelco-D set-position and never answers queries, but presets work. So presets are its only absolute positions.
  • Autofocus had to be redesigned. The motor is slow (focus up to 19 s of travel away) and reports no position, and majestic refuses snapshot crops on this chip. mcl18d-af now judges sharpness by the size of a full-frame JPEG, sweeps with the motor running until focus has been passed, then approaches it from one side so gear backlash can't leave it short.
  • Night mode needed lightMonitor true before majestic would read the light sensor, and backlightPin 9, because this majestic brings up no PWM lamp on infinity6c.
  • With no position feedback, the picture was the instrument: mean pixel difference for "did it move", phase correlation for pan/tilt, a scale search for zoom, Laplacian variance for focus.

Lessons

The guide lists 21 mistakes and how each was caught (§17). The ones that matter most:

  • A byte pattern in a binary means the code exists, not that your configuration uses it. Follow the flags.
  • Prove a test can fail before trusting it.
  • Reproduce everything the vendor puts on the wire, not just the part you think matters.
  • Diff device trees after normalising them (dtc -s), never as source.
  • Find tables through the functions that consume them.
  • Emulate branchy vendor code instead of reading it.
  • Test shell scripts under the shell they ship with, after the build has transformed them.
  • Measure the actuator before tuning a search for it.
  • On SigmaStar, a devmem read of the wrong register bank resets the SoC. Probe one bank before dumping many.

Upstreaming

This is a local port. Upstream, the driver would go to OpenIPC/sensors, the device tree to OpenIPC/linux and the board files to OpenIPC/builder. Two parts would not be accepted as they are: the sensor's register tables come from the vendor module, and gc5603.bin is the vendor's IQ file. The onvif-simple-server fixes can go upstream as a pull request (guide §15).