Tag: firmware

  • Hardware hacking, part 7: JTAG and SWD basics, and how vendors lock the door

    Hardware hacking, part 7: JTAG and SWD basics, and how vendors lock the door

    Part 7 of 7 in the Hardware hacking series. Part 1: updating the firmware on a Pinecil V2. Part 2: the bench I work on. Part 3: using my 5V FTDI cable on 3.3V UARTs. Part 4: finding the UART console on a router I own. Part 5: reading SPI flash without wrecking it. Part 6: decoding I2C and SPI with sigrok.

    UART gives you a console, SPI flash gives you the firmware image, and JTAG or SWD gives you the CPU itself. Halt it, read registers, step through code, read and write memory, reflash, all on a part that has not been locked. That’s why the debug port is the most powerful interface on a board, and why vendors work hard to make it go away in production.

    This is the one part of the series where I own less than the topic needs. I have no dedicated debug probe, so this part is about understanding the interface and how defenders lock it, not a walkthrough of attacking a device. Everything here applies only to hardware you own or are authorised to test. Ownership does not switch off copyright or anti-circumvention law, so check yours before you bypass read protection on anything.

    What the two interfaces are

    Diagram I drew: STM32 read-out protection levels 0 (0xAA), 1 (0x55) and 2 (0xCC) with the allowed transitions between them

    The diagram above summarises the protection levels described below; it is a diagram I drew, not a screenshot.

    JTAG is the old one. It uses four signals, TCK (clock), TMS (mode select), TDI (data in) and TDO (data out), plus an optional active-low reset for the test logic, usually written nTRST. Devices sit in a chain, and OpenOCD’s documentation notes that JTAG also supports boundary scan testing.

    SWD is the ARM-specific alternative. The OpenOCD manual describes it as using fewer signal wires than JTAG: SWDIO and SWCLK, plus ground and usually a reset line. It has no boundary scan. Some processors support both.

    The pin counts matter for defenders because they decide how easy it is to find the port on an unmarked board. Here is the number of ways to assign the data signals if you have a row of n candidate pads and no silkscreen. These are my own permutation counts, not a vendor figure, and they ignore the ground, reset and reference-voltage pads on the same header:

    pads   JTAG (TCK,TMS,TDI,TDO)   SWD (SWDIO,SWCLK)
      6            360                    30
      8           1680                    56
     10           5040                    90
     12          11880                   132
     16          43680                   240

    Few people try them by hand, and pin-finding tools exist for exactly this. Those tools drive the pads, and an unknown pad can be a supply rail, so set the tool to the target’s measured logic level first and never use one on a mains-connected board. I treat unpopulated pads as obscurity, not as a control.

    What the software side looks like

    OpenOCD is the open source tool that sits between a debug adapter and GDB. The adapter list in my capture comes from the real 0.12.0 package on Ubuntu 24.04. One entry is ft232r, which drives an FT232R in bitbang mode, so in principle my cable could act as a crude JTAG adapter. I have not tried it and will not. The seller describes the cable as 5V logic (I confirm by measuring TXD at idle, as in part 3), and most debug ports are 3.3V or lower.

    Real terminal output from OpenOCD 0.12.0: the list of 28 built-in debug adapters, then a run with a CMSIS-DAP interface and an STM32F4 target that fails with unable to find a matching CMSIS-DAP device because no probe is attached

    Real terminal output from OpenOCD 0.12.0 in a throwaway container: the list of built-in debug adapters, then a run with no probe attached.

    The second command, run with no probe attached, shows a failed first attempt: OpenOCD opens its TCL and telnet ports, then stops with “unable to find a matching CMSIS-DAP device”. Both outputs are real, from a throwaway container, and prove nothing about any chip.

    How vendors lock it

    Most microcontroller vendors have some version of the same idea: a setting in non-volatile memory that tells the chip to refuse debug access, or to refuse it unless the flash is erased first.

    STMicroelectronics calls it read-out protection. In the HAL header for the STM32F4 family, level 0 is 0xAA (no protection), level 1 is 0x55 (read protection of the memory) and level 2 is 0xCC (full chip protection). The same header carries the warning that once level 2 is set it is no longer possible to go back to level 1 or 0. OpenOCD’s documentation for the STM32L0/L1 driver says mass erase is the only way to unlock a protected flash, unless the level is 2, which cannot be unlocked at all. On those parts, dropping from level 1 to level 0 wipes the flash, so the attacker gets the chip back but not the firmware. That’s the design intent. The next section shows it didn’t always hold.

    Espressif does it with eFuses. The ESP-IDF documentation says that when secure boot is enabled, JTAG debugging is disabled via eFuse, and the bootloader sets that on first boot at the same time it enables secure boot. Flash encryption disables it by default too. The eFuse names vary by chip (DIS_PAD_JTAG and DIS_USB_JTAG on some, HARD_DIS_JTAG on the ESP32-S2). eFuses are one-time programmable, so the hard-disable ones are a one-way door too. Run your first flash-encryption experiments on a board you can afford to lose.

    Locks that fail

    A protection bit is only as good as the silicon that enforces it. Two cases I can point to with sources.

    Obermaier and Tatschner at Fraunhofer AISEC presented “Shedding too much Light on a Microcontroller’s Firmware Protection” at USENIX WOOT 2017. They looked at the STM32F0 and found three weaknesses: a common configuration that gave low protection and could be exploited with a cold-boot approach, a design weakness that let the protection level be downgraded, and a hardware race condition in the debug interface that let them extract read-protected firmware. Each attack needed only low-priced equipment, according to the abstract.

    CVE-2020-27211 is the nRF52840 case, where Nordic’s flash read-out protection is called APPROTECT. NVD describes it as being bypassable by injecting a fault during the boot phase, with a CVSS 3.1 base score of 5.7 and a physical attack vector. The score is modest for a consumer gadget. For a device holding a fleet-wide secret in flash it is not, which is the point I would make to a product team.

    My takeaway for anyone shipping hardware: assume someone with the device in hand eventually gets in, and make sure what they find is worth nothing beyond that one unit. Per-device keys, no shared credentials in firmware, no signing key near the board. The lock buys time. It doesn’t keep a secret.

    What I check on my own devices

    On a product I am responsible for, “debug port open, same key on every unit” goes in the risk register ahead of most software findings.

    If you do connect a debug adapter to a board you own, these rules come from the earlier parts and I follow every one of them:

    • Power the target from its own supply, and only one source at a time. Never connect an adapter’s power output to a target that has its own supply, least of all the red 5V wire of a cable like mine. The reference-voltage pin, where the adapter has one, is a sense input and the only power-related pin to wire.
    • With the board powered and nothing connected, measure its supply or the header’s reference pad against ground to learn the logic level. Then power down, connect ground first and the signals second, power up, and only then start OpenOCD. Disconnect in the opposite order.
    • Power the board down, and disconnect any battery, before moving a lead or soldering a header. Let the iron tip cool before you put it away.
    • Do not probe or connect anything to a board that is plugged into mains, or whose supply is mains-connected and non-isolated. A low-voltage output does not make it safe, and bulk capacitors stay charged after you unplug.
    • Touch a grounded object first or use a wrist strap, and handle boards by the edges. Do not wear a wrist strap near mains-connected circuits.
    • Read the protection settings before you change them, and back up the firmware before you lock anything, because afterwards you cannot. Level 1 on an STM32 can only be removed by a mass erase, level 2 cannot be removed, and ESP32 eFuse burns cannot be undone and can brick the board.

    That closes the series. Measure first, read before you write, and back up whatever you can’t replace.

  • Hardware hacking, part 5: reading SPI flash without wrecking it or fooling yourself

    Hardware hacking, part 5: reading SPI flash without wrecking it or fooling yourself

    Part 5 of 7 in the Hardware hacking series. Part 1: updating the firmware on a Pinecil V2. Part 2: the bench I work on. Part 3: using my 5V FTDI cable on 3.3V UARTs. Part 4: finding the UART console on a router I own.

    The flash chip is where a device keeps its firmware, its configuration and often the secrets that were supposed to stay secret. On most external SPI flash, anyone who can read the chip gets whatever is stored in the clear, no exploit needed. I want to know what comes out of my own devices, and I want to read it without killing the board.

    Everything here is for hardware I own, on boards that run from a low-voltage adapter or a battery. I never open or probe anything with mains-voltage circuitry inside, and never connect anything to a board that is plugged into the wall. If I can’t tell what powers it, I leave it alone. Before handling a board I touch an earthed metal object or wear a wrist strap, because flash chips are ESD sensitive.

    Read the datasheet for the exact chip first

    Flowchart I drew: board off and ESD care, read the datasheet supply, in-circuit read with the probe frame, JEDEC ID check, desolder to the socket if the ID is wrong, read twice and compare SHA-256, keep two copies; red warnings: never two power sources, never mains

    I drew the flowchart above. It’s the order I work in, and the two red warnings apply at every step.

    I’ll use the Winbond W25Q128JV (datasheet Revision F, March 2018). In the 8-pin SOIC package the pins are 1 /CS, 2 DO (IO1), 3 /WP (IO2), 4 GND, 5 DI (IO0), 6 CLK, 7 /HOLD (IO3, labelled “Hold or Reset” in the datasheet) and 8 VCC. Pin 1 sits next to the dot. Every other part gets its own datasheet check.

    The supply range is 2.7V to 3.6V (section 9.2), with the lower part allowed only at lower maximum clock speeds. The absolute maximum for VCC is 4.6V, and no pin may go above VCC plus 0.4V (section 9.1). That is why a 5V signal has no business near these pins. If the marking says a different family member, a lower-voltage one for instance, I read that datasheet before powering it.

    The cheapest sanity check is the JEDEC ID. Instruction 9Fh returns a manufacturer byte (EFh for Winbond) and two device bytes: 40h 18h for the W25Q128JV-IQ/JQ, 70h 18h for the -IM/JM variants. A sensible ID means contact, supply and wiring are probably fine for those few bytes, which says nothing yet about a full read. All zeros or all ones means I stop and fix the contact.

    In-circuit reads and why they fail

    When I can’t or don’t want to desolder, I use the probe frame, four spring pens that press on the chip’s legs. It’s quick and leaves the board as it was. It also fails often.

    The chip is also not alone on the bus. The SoC is usually wired to the same pins. If it’s unpowered, current can leak through its protection diodes from whatever the programmer drives. If it’s powered, it may read the flash while I do. Either way the result is a garbled ID, or a read that looks fine and is subtly wrong.

    The programmer’s supply may also feed the board’s whole 3.3V rail, and the board can power itself backwards through the chip’s lines. A faint LED glow on a board that should be off is the symptom, and on glow, heat or a smell I disconnect and go to the socket.

    One rule I never bend: exactly one power source at a time. The board’s adapter is unplugged and any battery is out, and a lithium cell never stays in a board I’m clipping onto. I wait until a meter shows the rails near 0V, since capacitors hold charge. Before clipping on I check pin 1 against the clip’s marked wire, and I measure the programmer’s output with a meter, because a cheap programmer can put 5V on its data lines, which is fatal to a 3.3V chip.

    Read twice, then compare

    I read the chip at least twice and hash both files. The terminal below shows the routine with flashrom. There is no real chip in it. I used flashrom’s dummy programmer, which emulates a W25Q128FV (an older family member than the JV above) from a 16 MiB file of random data I generated, so it is real software on a synthetic target, not a hardware capture.

    Terminal output from flashrom using its dummy programmer to emulate a W25Q128FV: two reads with identical SHA-256 hashes, then a third read after one byte of the synthetic image was changed, with a different hash and cmp reporting the differing byte

    The screenshot is real flashrom output on the emulated chip. I trimmed flashrom’s banner and its “untested operations” notice, and nothing else.

    The first two reads give the same SHA-256. For the third I overwrote one byte of the emulated chip to mimic a bad contact, and the hash changes completely while cmp names the byte. A mismatch tells you something. A match doesn’t prove much on its own.

    sha256sum read1.bin read2.bin
    cmp read1.bin read2.bin && echo "reads match"
    ls -l read1.bin

    A bad contact can fail identically twice, so I also check that the size equals the capacity (128 Mbit is 16,777,216 bytes) and that the headers and strings look sane. A slow clock helps. At 1 MHz a full read is at least 134 seconds of bit time, at 8 MHz about 17. My programmer is the XGecu T48 with its Windows-only Xgpro software. I didn’t run it for this post, so there’s no screenshot, and I’m not claiming flashrom supports it.

    Desoldering and the socket

    If the in-circuit read won’t settle, I move the chip to the T48’s socket adapter. That removes everything else from the bus and is riskier for the board. Hot air or an iron can lift pads or cook the chip if I rush. The tool and board stay hot after switch-off, so I park the iron, let things cool, use a fan for the flux fumes and wear eye protection. Hot air blows small parts away, and any lithium cell comes out before I heat anything. I photograph the pin 1 marking first. In the software I select the exact part number and compare the supply voltage it shows against the datasheet. A wrong selection can put 3.3V on a 1.8V part. Bare chips go in anti-static packaging.

    Back up before any write

    A read changes nothing. A write or erase can brick the device, and the dump is the only way back. So two matching reads, hashes written down, files copied elsewhere, and only then do I think about writing. Per-device data is the trap. Many routers keep their MAC address and radio calibration in flash, and a stock image written over it loses that for good.

    The chip’s write protection is weaker than people assume. Array protection comes from the BP, TB and CMP bits in the status register. SRP, SRL and the /WP pin only decide whether those bits can be changed, and /WP stops acting as a protect pin when quad mode turns it into IO2. At the factory default (SRL=0, SRP=0) the status register can be rewritten after a Write Enable, so anyone on the bus can undo the protection. SRP=1 with /WP held low locks it in hardware. SRL=1 locks it until the next power cycle. A separate one-time-programmable mode locks it permanently. None of it stops a read. Write protection is about integrity, not secrecy.

    What a defender does about it

    I assume anyone with ten minutes and physical access can dump the external flash chip on a board I deploy. The design question is what the dump gives them.

    A shared password, a fleet-wide key or a plaintext Wi-Fi passphrase means one stolen device compromises all of them. Secrets should be per-device, and where the hardware allows, held in a secure element or SoC fuses instead of the external chip. Firmware should be signed and checked by a boot ROM the attacker can’t rewrite, so a modified dump doesn’t boot. Anything confidential on flash should be encrypted with a key that doesn’t sit next to it. Otherwise the chip is a USB stick.

    Part 6 uses the logic analyzer to decode SPI and I2C on a powered board. It’s a different setup with its own warnings: ground first, and only measured low-voltage lines.

    Other parts of the Hardware hacking series: 1, updating the firmware on a Pinecil V2; 2, the bench I work on; 3, using my 5V FTDI cable on 3.3V UARTs; 4, finding the UART console on a router I own; 5, reading SPI flash without wrecking it; 6, decoding I2C and SPI with sigrok; 7, JTAG and SWD basics.

  • Hardware hacking, part 1: updating the firmware on a Pinecil V2

    Hardware hacking, part 1: updating the firmware on a Pinecil V2

    Part 1 of 7 in the Hardware hacking series.

    I do most of my soldering and rework with a Pine64 Pinecil V2, and it is the first tool in this series because it is the one I touch every time I open a device. This series is about the bench gear I actually own: the Pinecil, an FTDI serial cable, a cheap logic analyzer, an XGecu T48 programmer and a probe frame for chips I don’t want to desolder. Everything here is done on my own hardware, from the defender’s side. If you understand how a device looks from the inside, you know what an attacker with ten minutes and a screwdriver can learn about it.

    Firmware first, then. A soldering iron that talks USB-C power delivery and Bluetooth is a small computer, and it deserves updates like any other. The V2 runs IronOS, the open source firmware by Ralim, on a Bouffalo BL706 chip.

    Which release and which file

    As I write this (October 2026) the latest stable IronOS release is v2.23, published on 31 August 2025. Its changelog has a few items I care about: Bluetooth LE is now off by default on the V2, the default USB-PD negotiation uses a more conservative profile, and there are fixes for PD supplies that caused reboots with some laptops. Bluetooth support for the V2 first arrived in v2.21, so if you are on v2.21 or v2.22 this update switches it off by default, and anything older has no Bluetooth to switch off.

    The release page lists assets for every supported iron. For the V2 you want Pinecilv2.zip. The similarly named Pinecil.zip is for the original V1, which uses a different chip and a different file format.

    The Assets list of the IronOS v2.23 release on GitHub, with Pinecilv2.zip boxed in red and Pinecil.zip boxed in orange. Red annotation labels were added to the screenshot.

    That screenshot is the real GitHub page, with the two boxes and labels added by me. GitHub also shows a SHA-256 digest for each asset. After downloading I compared it with the local file:

    $ sha256sum Pinecilv2.zip
    fe75a2e3227ba53b7fdd8b0d8a362fc2ff0af264ce442ba76e26f21a3abb7503  Pinecilv2.zip

    That matches the full SHA-256 digest GitHub’s API reports for the asset (the release page truncates it, so compare all 64 characters, not just the start). It only proves the file I got is the file GitHub is serving, which is still worth ten seconds. Inside the zip there is one .bin per language. I use Pinecilv2_EN.bin, 194,508 bytes in this release. Ignore the .dfu and .hex files for a normal update. Pine64’s wiki is explicit that flashing a .dfu as firmware leaves the iron unable to boot.

    Putting the iron in flash mode

    This is the one step you can’t do from software. First a safety note: let the tip cool completely and park the iron in its stand before you unplug it or start fiddling with cables. If it boots normally instead of staying black, it is a live iron on a USB supply, so unplug it straight away rather than pressing buttons. Now unplug everything, hold the minus button, and only then plug the USB-C cable into the iron. Pine64’s wiki says to keep holding for around 10 to 15 seconds after plugging in. If the screen stays black, you are in flash mode. If it boots normally, unplug and try again, because the order matters.

    Screenshot of the Flash Mode section of the Pine64 wiki page on Pinecil firmware, including the warning not to use the DC barrel jack while updating.

    The warning at the top of that section matters. Pine64’s wiki says that using the DC barrel jack during a firmware update may destroy both your computer and the Pinecil, and that the iron is designed to use only one power port at a time. So for the whole update only the USB-C cable goes in: no barrel jack, no second supply, nothing on the other rear port.

    I have not photographed the iron’s own screen and buttons for this post yet, so the black-screen state and the on-device menus are described here rather than shown. In flash mode the iron does not appear as a USB drive. According to the wiki it shows up on Linux as a ttyACM serial device, and Windows lists it as a COM port.

    Flashing with blisp

    blisp is Pine64’s command line flasher for Bouffalo chips, and it is what I would reach for first. Version 0.0.5 (August 2025) is the current release and there are prebuilt binaries for Linux (x86_64, aarch64, armv7, riscv64), macOS and Windows. Unzip it, put it next to the .bin and run:

    sudo ./blisp write -c bl70x --reset Pinecilv2_EN.bin

    On Windows it is .\blisp.exe write -c bl70x --reset .\Pinecilv2_EN.bin from an administrator terminal, and the leading .\ matters. I can’t plug the iron into the machine I used for the screenshots, so I ran the real tool with no iron attached, in a throwaway Ubuntu 24.04 container:

    Terminal output in a dark theme showing blisp v0.0.5, the sha256sum of Pinecilv2.zip, the size of Pinecilv2_EN.bin, and the blisp write command ending in Device not found with exit status 3.

    “Device not found” with exit status 3 is what you get when the iron isn’t in flash mode, or when the cable is charge-only. If you see it with the iron attached, check the troubleshooting list below. I haven’t seen the success output because there was no iron on that machine. With --reset, blisp restarts the chip after the write.

    PineFlash, the GUI option

    Pine64’s wiki lists PineFlash as the GUI route. It is a community project by Spagett1, not a Pine64 product, and its latest release is 0.5.5 from April 2024. I ran the Linux AppImage in the same kind of container, under a virtual display:

    The PineFlash 0.5.5 window with the status Soldering Iron Disconnected, Pinecil V2 selected, and an empty version dropdown.

    The layout is simple: pick the iron, pick a version, pick a language, press Update. The Connection Guide section repeats the hold-minus instruction with a diagram. The problem showed up in the log panel:

    PineFlash log panel showing the line Invalid json downloaded, could not fetch versions, highlighted with a red box and an annotation label.

    On my run the version list never filled in, and the only entry in the dropdown was “Custom”. My network could reach GitHub fine, so it is probably not a plain connectivity problem, although I haven’t ruled out something like API rate limiting. I suspect the app is tripping on the newer release layout (v2.23 ships a metadata.zip asset), but I haven’t confirmed that. In practice, with PineFlash 0.5.5 you may need to download the .bin yourself and use the custom file option, at which point blisp is less work. I haven’t tested a real flash with PineFlash, so I won’t claim it works end to end.

    Checking the version and getting back out

    After the flash, unplug the iron and plug it in normally. Pine64’s wiki says to hold the minus button for a couple of seconds on the home screen to see the firmware version, and it should read v2.23. I would rather show you my own screen than paraphrase a wiki, so a photo of it will go in here later.

    Pine64’s wiki says the Pinecil is very hard to brick with a firmware update, and that re-flashing usually fixes a bad one: enter flash mode again and write a known good file. The flash-mode bootloader sits in the chip’s ROM, which is why that route is always available. To roll back, download the older release from the same GitHub page, take its Pinecilv2_EN.bin and run the same command. If something goes wrong, work through these in order:

    • Hold the minus button before the cable goes in, and keep holding for 10 seconds or more.
    • Try another cable. Some USB-A to USB-C cables carry power only.
    • Try another port, or another computer. Some laptop USB-C ports negotiate badly, and a plain USB-A port often works.
    • On Windows, name the port with -p COM3 (or whatever Device Manager shows). On macOS, allow the new device when prompted, then replug in flash mode.
    • Make sure you used the .bin, not the .dfu.

    Next is the rest of the bench: the tools I use to open up devices, and why each one earns its place.

    Other parts of the Hardware hacking series: 1, updating the firmware on a Pinecil V2; 2, the bench I work on; 3, using my 5V FTDI cable on 3.3V UARTs; 4, finding the UART console on a router I own; 5, reading SPI flash without wrecking it; 6, decoding I2C and SPI with sigrok; 7, JTAG and SWD basics.