Reverse engineeringguide
Dumping a locked nRF52840's flash over USB with a patched app
I recovered a full flash image of a locked nRF52840 mouse and both chips of its 8K dongle (CH32V305 + nRF52820) without any debug hardware. The technique: patch the device’s own application with a bounded read-memory command, install it through the normal update path, and read every byte over the USB cable. A community developer had already done this on the dongle; the mouse added the protocol, an automated two-pass compare, and a layout derived from the image’s own records. The mouse’s debug port was locked (APPROTECT enabled, per the nRF52840 product spec), so SWD reads were never an option.
TL;DR:
- Patch the stock firmware with a read-memory command restricted to flash, FICR and UICR, with a byte cap and strict bounds checks, using unused code space where it exists.
- Flash the patched image through the vendor updater as if it were any other firmware update. The updater itself is unmodified.
- Read the whole chip in two passes, compare them, and abort on any mismatch.
- Flash the original image back. This needs an exact stock copy of the app, because an update erases it first.
The mouse dump produced a 1 MB flash image, a 4 KB FICR, and a 4 KB UICR over two byte-identical passes in about 17.5 minutes, with 0 retries.
When this works, and when it does not
Section titled “When this works, and when it does not”| Condition | Why it matters |
|---|---|
| The update path accepts patched images | The whole technique rides on the normal firmware update. If the updater rejects a modified package, there is no path to install the dumper |
| An exact stock app image exists to restore | The update erases the old app before writing the new one. Without the original image, the device comes back with different firmware than it had |
| The read command is strictly bounded | The handler must reject addresses outside flash, FICR, and UICR, enforce a byte cap, and abort on any error so a failed block cannot shift later bytes |
| Unused code space exists or a function can be proven inert | Overwriting live code means the device cannot run the dumper. On the nRF52840 mouse, an image extension region held 2 KB of padding. On the CH32V305, a stock function was sacrificed |
| The debug port is not the only blocker | On the nRF52840 mouse, APPROTECT blocked SWD reads. If the updater had also refused modified packages, the USB route would be closed too |
| The device has no known app image | The first-generation 8K dongle (NXP LPC55-class USB MCU per its regulatory filing photos) has no public firmware image. Without one, flashing a dumper means the original firmware is lost, so the dump route as described here does not apply. The NXP boot ROM’s USB ISP read is the next route to investigate (untested) |
The read command
Section titled “The read command”The handler runs inside the main loop through the firmware’s existing feature-call framework. The host queues a read: a 32-bit address, a length from 1 to 12 bytes, and a feature id that the framework dispatches. The handler checks the address against three allowed ranges, reads the bytes with plain ldrb instructions, and returns a status byte plus the data. Status codes: 0 = ok, 1 = bad argument, 2 = address or length outside the allowed ranges.
| Range | Start | End (exclusive) | Size | Notes |
|---|---|---|---|---|
| Code flash | 0x00000000 | 0x00100000 | 1 MB | The nRF52840’s full flash |
| FICR | 0x10000000 | 0x10001000 | 4 KB | Factory information configuration registers. The first dump read only 1 KB and missed non-erased blocks at 0x400, 0xC00 and 0xF00 (by the product spec’s FICR map these hold calibration and TRNG data; not checked field by field). The limit was widened to 4 KB and the region re-read |
| UICR | 0x10001000 | 0x10002000 | 4 KB | User information configuration registers. The APPROTECT word is at offset 0x208 (0x10001208); its low byte was 0x00, which per the nRF52840 product spec enables access-port protection and disables SWD reads |
The host side runs two complete passes, compares them in memory, and stops on the first mismatch rather than writing mismatched data to disk. Each 12-byte read costs two USB round trips (queue the call, poll for the result) of about 3 ms each, measured, so one pass over 1 MB takes about 8.7 minutes.
A second read command removes the round trips. One call starts a bulk read; the mouse then sends one numbered 12-byte block per main-loop pass, through the same send path and under the same checks as its other unsolicited reports. The host places each block by its number and re-reads any it missed with the per-call command, so a dropped report costs one round trip and can never shift later bytes. Measured on the mouse: 1 MB in 88 seconds per pass instead of 524, with 5 of 87,377 blocks re-read individually and both passes identical. That command was the first one written in C rather than hand assembly.
Mistakes made on the way
Section titled “Mistakes made on the way”Three bugs in the host code and one in the handler showed up before the first clean dump.
The first dump tool never sent the result poll. It queued a read, waited a fixed period for a reply that only arrives on request, and treated status 0x01 (pending) as done. It sat for 20 minutes with almost no CPU time and nothing written before I stopped it. The fix: after queuing a call, poll the result register until the status byte changes from pending to done, then read the data.
The first handler accepted 13 bytes into a 13-byte buffer. The result format is one status byte followed by up to 12 data bytes, so a 13-byte request wrote one byte past the buffer into an adjacent word. That word held a queue-consumed flag that was read before the handler runs, so the overflow could not fake a queued call, but it was still out of bounds. The fix changed one instruction: cmp r5, #12 instead of cmp r5, #13, and decompile review confirmed the new cap.
A failed block would have shifted later bytes. If a block failed all its retries and the host moved on to the next block, the data from the next read would land at the wrong offset and every later byte would be misplaced by exactly the failed block’s length. The fix: a block that fails all retries stops the run.
FICR limited to 1 KB missed real data. The FICR is a 4 KB page; non-erased blocks above 0x10000400 were skipped. The limit was widened to 4 KB after the first dump, and the region was re-read.
Region layout from the image’s own records
Section titled “Region layout from the image’s own records”The dumper reads raw bytes. A separate tool derives the layout from the image’s own contents rather than from a fixed memory map:
| Region | Range | Size | Source |
|---|---|---|---|
| MBR | 0x00000000-0x00001000 | 4 KB | Fixed location; the Master Boot Record is the first 4 KB of nRF52 flash |
| SoftDevice | 0x00001000-0x0001C000 | 108 KB | Info struct at 0x3000: magic 0x51B1E5DB, end address, FWID 0x0102, version word 7002000 (decimal). FWID 0x0102 with an end of 0x1C000 is S113 v7.2.0 by Nordic’s release notes |
| Application | 0x0001C000-0x00037000 | 108 KB | SoftDevice end to last non-erased page |
| Data pages | 0x000E6000-0x000F2000 | 28 KB in a 48 KB span | Seven non-erased pages in five runs; two runs are byte-identical |
| Bootloader | 0x000F8000-0x000FD000 | 20 KB | MBR word 0xFF8 = 0x000F8000 (UICR NRFFW[0] agrees) |
| MBR parameter page | 0x000FE000-0x000FF000 | 4 KB | MBR word 0xFFC; erased |
The version word 7002000 decodes as 7.2.0 under Nordic’s MMMmmmbbb format (major, minor, build). Every boundary except the MBR came from the image rather than from a datasheet. Nordic’s nrf_mbr.h says the MBR words at 0xFF8 and 0xFFC take priority over the UICR copies, so the tool reads those first.
As a check on the whole dump, Nordic’s released s113_nrf52_7.2.0_softdevice.hex programs 110,508 bytes; all of them equal the dump. The hex leaves 0xB00-0xFFF empty, and in the dump that gap holds only the two MBR address words. A dump that matches a vendor’s published image byte for byte, over the full range the vendor programs, is a stronger check than two identical passes, which only show that the reads were stable.
The same technique on the dongle
Section titled “The same technique on the dongle”A community developer had already dumped both chips of the 8K dongle (CH32V305 + nRF52820) with the same approach: dump-only builds that replace stock code with a bounded read handler, accepting read requests over USB and returning data through the outgoing report path. On the CH32V305 the read handler replaces a stock main-loop function, and the call to that function is nopped out, so the build is for dumping only. On the nRF52820, a stock timer-tick routine was overwritten.
Both chips were read twice, with identical SHA-256 hashes per region per pass. The identity pairing record in the CH32’s last flash pages held the model id the dongle shipped with, and the nRF52820’s settings page held the dongle LED colour bytes.
The community developer’s dumps arrived without tool source or build scripts, and the dumper builds themselves are not preserved (they replace live stock code and were never meant to stay installed).
Our own dumper for the dongle’s CH32V305 hooks the default case of the firmware’s command dispatcher, the branch every unknown command takes. A new read command answers through the stock reply path; every other command continues into the stock default case unchanged. The full 128 KB of code flash plus the 4 KB information page took 34 seconds, two passes identical; the dongle then got its stock firmware back. Compared with the other dongle’s dump, the bootloaders are byte-identical and the 4-byte model record is the only firmware-level difference.
Getting there took two failures:
| Build | What went wrong | Recovery |
|---|---|---|
| First (written by an agent on a cheaper model) | The handler ended with a return instruction. Inside the dispatcher the return-address register still held the address of a helper call made earlier, so the return jumped back into the dispatcher, which read the same command again and looped forever. The dongle still enumerated but answered nothing, including the command that enters update mode | Unplug and replug: the loop lived only in RAM, and the update command has its own dispatcher case, so a fresh boot accepted the stock firmware |
| Second | The handler read the address from the reply buffer, whose byte 2 the dispatcher had already overwritten with a status code, so every read started one byte late. Harmless, because every read stayed in range, and caught by reading the bootloader’s first instruction before trusting a full dump | Read the untouched request copy instead |
The emulator test for the second build passed before it went on the device, because it placed the request where the handler expected it instead of where the dispatcher leaves it. Rewritten to rebuild the dispatcher’s real state, it fails on the second build and passes on the fix.
The bootloader explains why the first failure could not be fixed over USB without a power cycle: it enters update mode only when a flag page holds a fixed byte pattern, and only the running application writes that pattern, in response to the update command.
Why this was parked on SWD
Section titled “Why this was parked on SWD”Every ingredient was in place before the dumps arrived: the key that encrypts update packages, a working patch and flash path, and a whole-image gate that catches structural defects. Work was parked on SWD because of two assumptions that the dumps disproved:
-
A “full backup before any modified flash” rule made a dumper patch look circular. The rule says do not modify flash without a backup, and the dumper modifies flash to get the backup. But the precondition is only that an original image exists to restore, not that the current flash stays untouched during the dump. The update path itself erases the app, so the dumper does nothing the updater would not do on any firmware change.
-
Negatives measured on stock firmware were carried over to patched firmware. Stock firmware has no flash-read command and its boot mode is write-only, but a patched app can expose any operation the hardware supports. Those limits apply to the unpatched firmware only.
Before parking work on a hardware route, ask whether a patched app can do the job through the normal update path.
Running one mouse model on another model’s dongle
Section titled “Running one mouse model on another model’s dongle”The 8K dongle has the same hardware across mouse families: one CH32V305 for USB and flash storage, one nRF52820 for the radio. But plugging one family’s mouse into another family’s dongle does not work out of the box.
The CH32 stores a pairing identity record (vendor id, model id, and a checksum) in its last flash pages. When the host sends a CID-only pair command, the mouse pairs successfully and the dongle reports paired state, but no cursor movement follows. The identity record stays unchanged. A CID+MID pair command with the mouse’s actual model id produces the same result: paired, no data.
The next attempt forced the model id in the CH32’s identity loader with a one-instruction patch, so the dongle’s handshake reported the target model id. Pairing succeeded but still carried no data. So the radio firmware also differs per mouse family; what exactly differs on the radio side is not yet known. Flashing the target family’s firmware on both chips (nRF and CH32) with the same one-instruction identity patch produced a working connection. The mouse moved the cursor.
Later tests settled it: the radio firmware is the whole difference. With the target family’s radio image, the mouse paired and worked whatever the identity record said, including a model id no mouse uses; the record only changes what the dongle reports to configuration apps. The static code trace predicted that a CID+MID pair would rewrite the identity record (the CH32 has a handler that calls SetCidMid when the nRF reports a new model id), but the hardware contradicted that: the record stayed on the original model id. Whether the nRF does not send the model id during a CID+MID pair, or sends it as zero with a guard that rejects it, is an open question.
Both chips were dumped before any flash change, so the original state is recoverable.
Gotchas and lessons learned
Section titled “Gotchas and lessons learned”- If the vendor updater accepts a patched package, the device will run a dumper.
- A hook inside a function must never return on its own. Jump back into the stock code on every path.
- Read a known address first (a bootloader’s first instruction is ideal) before trusting a full dump.
- An emulator test has to rebuild the state the real caller leaves behind, or it will pass on the bug. Only an exact original image is needed to restore.
- Bound the read command at both ends: address ranges, byte counts, and error handling. A 13-byte read into a 13-byte buffer is an overflow even if the stray byte lands harmlessly.
- FICR is a full 4 KB page, and this one had data past the first 1 KB. Read the whole page.
- Two byte-identical passes with 0 retries rule out intermittent read and USB errors; a single pass only shows that every read returned bytes.
- Derive layout from the image, not from the datasheet. The SoftDevice info struct, the app end, and the bootloader address are all inside the flash image itself. A datasheet maps the platform, not this firmware build.
- Before parking on an external hardware route, ask whether the normal update path can carry a dumper. The stock firmware’s limits describe the stock firmware. A patched app can expose anything the silicon supports.
Related docs
Section titled “Related docs”- Patching an nRF52840 gaming mouse’s firmware - the patching process, the gate that checks each change, and the features that run on this mouse
- CH32V305 WCH-LinkE read-only debug triage - the debug-probe route for the dongle’s CH32V305, which the USB dump made unnecessary
- tikus: a browser configurator and firmware updater for vendor mice - how the browser app installs patched firmware, and the bug that made every early flash send the wrong bytes