How the Z 6II emulator came to be

Arne Baeyens wanted to run Doom on his Nikon Z 6II. Two weeks earlier he had shared progress on running the game on the camera, and introduced qemu-expeed, an emulator he built to help the process. Now he wanted to explain how that went, and what it said about using LLMs for reverse engineering.

Baeyens is not a professional camera hacker. By education he is a mechanical engineer, and by day he works on robotics software with filtering and control algorithms—a far higher abstraction level than this project. In fact, this was the first time he read assembly. General software skills—Linux and Docker—helped, as did his electrical engineering background, which included a chip design course.

Like most software engineers, he uses LLMs at work. He noticed they were decent at getting things to "work," though he put quotation marks on that word. That made him wonder whether LLMs could help insight into his Nikon camera.

From firmware mystery to readable internals

Poking at the Nikon firmware purely by himself, like previous projects, would only have led him into the same wall as everyone before. So he opted to use agentic LLMs and adapt the working strategy.

In classical reverse engineering, the physical device is closely observed, trying to get in through exposed UART and JTAG headers. That approach is not compatible with LLMs, which he did not trust to leave alone probing, and neither did he want himself probing on instruction. What he did know: these agentic LLMs work better the faster they are confronted with the results of their actions. Therefore he chose to put emphasis on building a good emulator, code that makes his computer behave like the Z 6II's hardware. Most importantly, this gave the agent a clear target to work towards—the further the program counter gets, the better.

This emulator-only approach has its limits. While some hardware components were easy to identify—like the two Cortex-A7 cores and the ARM926 coprocessor—others are basically invisible, like the many hardware accelerators, peripherals, and flash areas not part of the firmware update package. Getting access to the camera's internals was therefore key.

His initial plan was to screw it open and dump bus traffic, though after some coaxing and trial-and-error, the agent got something far better to work: the read-RAM-over-PTP protocol, documented years ago in older Nikons, was still present on the more recent Z 6II, albeit changed slightly. This provided an extraordinary view on the camera's internals—little need to attach a bus dumper if one can just read the communication buffers. This revealed the communication protocol with the microcontroller (an STM32F412) which interfaces the main Expeed with the buttons and dials as well as the battery. A fun fact: in one of the first emulator boots, the emulated camera complained hard about not being powered by an authentic Nikon battery.

Next to dumping bus traffic, this memory read primitive also made it straightforward to read the camera's internal state because the Cortex-A7, being ARM, has MMIO, so next to RAM he could also read the peripheral registers. Comparing those reads with the same reads in the emulated machine allowed him to pinpoint where the two diverged and especially what correct emulation looks like.

This approach worked pretty well and got the camera booted, but got stuck on the more complex tasks.

Public pieces that unlocked the platform

What helped there was the observation that, counter-intuitively, several things of this camera's software are public, it is just that Nikon does not advertise this. The first one was the OS this camera runs, SMP T-Kernel 1.00.01, a Japanese RTOS. While its code is closed source, TRON Forum helpfully published a very detailed spec which made it straightforward to name all the firmware functions handling OS tasks.

With the already gathered facts in hand, and in combination with some general public knowledge such as that Nikon in the past partnered with Fujitsu and that Nikon's Expeed is of Socionext Milbeaut lineage, he started searching for datasheets and related code. This turned up the Socionext M10V/SC2000, a SoC similar to the Expeed 6 (PCIe Gen2, USB3 device), though with some differences such as having four instead of two Cortex-A7 cores and (as far as he is aware) the Expeed 6 omitting the HEVC codec.

Searching more on Socionext, GoPro caught his eye. As Mark Kirschenbaum of Hypoxic shared in his excellent reverse-engineering blog post, GoPro's GP1 (their custom SoC that debuted in their HERO6) relates closely to the SC2000. Most importantly, GoPro runs Linux on one of the cores, and following the Linux GPL2 license, this meant they had to distribute their kernel source modifications. While usually not a lot of code, because the kernel takes care of interacting with the hardware, this was exactly what he needed. The GP2 (HERO10) release (he did not find a GP1 release) was helpful because it carries material related to both the M20V and the older M10V, and with that provided the register map of the inter-processor mailbox (IPCU, key for emulating the dual-core processor) as well as the SPI, I2C, HDMAC and EXIU peripherals.

Further searches on Linux releases of other manufacturers revealed that the Ricoh GR III and Pentax K-3 III run the SC2000, and their kernel provided the GPIO controller and confirmed the SPI block. Ricoh's release includes Socionext's Stubby bootloader, containing code for the M8M clock controller, which after adding some registers happens to fit the Expeed 6. Helpfully, Socionext spent considerable effort in mainlining their kit, which gave the SD driver and XDMAC. One of their patches did not make it into mainline, but nevertheless provided him the Expeed 6's counter block. Finally, the PCIe host bridge glue came from Socionext's SynQuacer server processor.

With this approach already having yielded fruitful results, he cast the net even wider, getting the Linux releases of the newer Nikon, Fuji, Panasonic and Sony cameras. Apart from a few remaining bits, this mostly yielded the knowledge that the Expeed 7 may have as siblings the Socionext M20V/SC2006 (Panasonic GH6 and S5II) and perhaps also the Fuji X-Processor 5 (Fuji X-H2). A fun fact: the Z 6II shares its USB block, a Fujitsu "F_USB30DR", with the Sony A7 III.

Disclaimer: he did not have access to any of Nikon's internal documentation; as such, he pieced this together from public sources, the firmware itself and a large number of educated guesses. Some of it may be wrong. Corrections are welcome at corrections@arnebaeyens.com.

Combined with many RAM reads and a lot of trial and error, this made for a camera with basic functionality working. A nice side effect of using known peripherals is that all of the otherwise fiddly items—like PCIe for example—got operational pretty quickly, and with functionality that generalizes beyond the exact sequence the firmware drives it.

The code chaos and what actually works

The Doom port thereafter only took an afternoon, as he already knew all the T-Kernel function addresses and had figured out how to execute code with a handful of well-placed RAM writes over PTP/USB. So, the project is a great success, the proof of the new age of AI? Let's have a closer look.

We shall start by running cloc (Count Lines Of Code) on all committed files. This rolls in at a whopping 344859 lines spread over 723 files. For the non-software people reading this: usual write speed of a productive programmer is on the order of 100 lines/day, equating those 345 kloc to over 13 years of full-time development.

Now, to be fair, one third of that is due to him storing the firmware annotations in JSON in a line-hungry format. He also let the agent keep a log of all investigations, and this markdown file has grown to 60 kloc. Remaining we have 46 kloc of documentation, 64 kloc of Python tooling, 50 kloc of C code (which is mostly the emulator), and finally another few kloc of various type (git diffs, bash, Dockerfile etc.

In classical (non-regulated) software development, having documentation expressed in dozens of kloc is basically unheard of. But before we pride ourselves in documenting our project so outstandingly well, let's take five minutes to read some excerpts from these files.

A first one, from sensor.md, which ought to document the sensor and the Expeed's front end:

Measured: the whole keyed property space is unset at boot […]
And §3.7 says what those four bytes are, which turns the measurement into an explanation. They read 1f ba 1f 10 — the magic that the region load-failure path stamps. This array is reserved region 0x08 (128 KB at 0xc20d2954), and its loader runs at boot and fails: measured in a block census, 0x103c31aa → 0x103c2bb6 → the storage helpers 0x102a9352/0x102a9308 all execute, and then so do the failure arms 0x103c31c8 (clear) and 0x103c31d0 (stamp the magic). The image template at 0x118b6bf0 is itself 12 non-zero bytes in 128 KB, so there is no factory data to fall back on either.

The keyed property area refers to a flash storage region next to the firmware which Nikon in the factory programs with various calibration and other settings. He dumped that flash region over PTP/USB and made it part of the emulated flash so the camera reads in the correct settings. The doc snippet above originates from before that (so it is not applicable anymore) and, translated from ClaudeSplain(TM) to laymen's, it reads "the firmware doesn't run properly when it's fed garbage." Wow what a surprise!

Even worse is that the same applies for all other uses of the config area, which includes about every camera functionality, so it is not even specific to the sensor. Let's take another few lines from that file:

Since each Expeed has its own address space (FINDINGS §6 item 10), 0x20060000 on the master and 0x20060000 on the slave are different hardware: two instances of the block, of which the slave's has considerably more in use. That fits the documented split — the master orchestrates capture while the slave does the pixel work (FINDINGS §10.5) — and it means an emulated sensor has to be installed per cluster, not once. The register file added to expeed6_sif.c is installed inside expeed6_install_soc(), so it already is.

This is Claude blowing things up beyond proportion:

  • "different hardware": yes, they're two different chips (the Z 6II runs two Expeed 6 processors), so of course it's not the same instance. What the LLM is probably referring to is that the first time it put two Expeeds together, it didn't configure QEMU correctly resulting in them operating in a shared address space, which of course didn't work. Since then, it feels the need to mention this "learning" everywhere.
  • "an emulated sensor has to be installed per cluster": uh please no, this camera has only one sensor! The intended meaning may be "emulated sensor frontend", as the emulator (luckily) has only one sensor. The core issue here is that the next agent will take this wording for fact, perhaps even as a desired state and change the code and documentation toward that. Also, it tends to propagate existing, in this case sloppy, wording, resulting in that after some time, none of the words have the intention as you and I understand them. (And, for that matter, neither does the LLM, with the difference that it never actually understood them in the first place.)

Granted, the documentation also contains better parts. And somewhere, that 46 kloc does contain genuinely useful information. But finding it requires one to search by wading through seas of word salad. (For reference, before publishing qemu-expeed he stripped out all this "documentation", and the docs in there are all handwritten.)

Unfortunately, this documentation is just the tip of the iceberg. The code is just as terrible. Of all the code, he went through one part, the MIPU, which is the block that converts a raw Bayer image into a YCC one ready for JPEG compression, and to do so applies the white balance, tone curves etc. It's one of the easiest blocks to emulate because it's observable: the image that comes out should match the JPEG the camera produces, end of story. Implementing a basic version of this should be a piece of cake, so he handed it off to Claude. How difficult can it be, converting a Bayer RGB image into a YCC one? Very difficult, apparently. Claude decided to implement auto exposure and auto white balance in this block. And then compared output pixel values for known inputs and outputs, and concluded they didn't match the camera. After some coaching, the agent found the registers of the white balance gains, tone curves and other key processing steps, and with those implemented, the outputs matched pretty well. In doing so, Claude kept all its superfluous logic, treating it as "stand ins" and "fallbacks." Because yes, if the firmware programs a gain of zero, then the MIPU must use the fallback gain! It did not occur to Claude that x times 0 simply equals 0...

My first efforts to let Claude clean up this code were fruitless. After some time, it explained the reason: it had added tests that assumed that particular auto white balance it dreamed up, and of course those failed when the agent used the registers the firmware wrote. But by rule tests must pass, so it didn't want to delete those fallbacks. After some stern imperative...