Tag: embedded

  • 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 4: finding the UART console on a router I own

    Hardware hacking, part 4: finding the UART console on a router I own

    Part 4 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.

    The first thing I check on a router I own is whether it has a serial console, and whether that console hands a root shell to anyone who clips a cable on. Plenty of consumer gear does. That takes physical access, so it isn’t a remote bug. Still, routers get stolen, resold and left in shared closets, and a free root prompt turns a lost box into an open one.

    Everything here is for hardware I own. If a router is ISP supplied or rented, I check my terms first. I only open routers that run from a low-voltage external DC adapter, and the board stays unplugged while I open it and do the continuity checks. If a device has mains-voltage circuitry inside, or I can’t tell what powers it, I don’t open it, and I never connect anything to a board that has mains on it. A cheap transformerless adapter is not isolated, so I use the one that shipped with the router. After unplugging I wait a minute, then touch an earthed metal object (or wear a wrist strap) before handling the board.

    Finding the pads

    Flowchart I drew: unpowered board, find pads, measure idle voltage, listen with the logic analyzer, find baud rate, then wire the cable per part 3, with warnings never to connect the 5 V wire or a mains-powered board

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

    A router UART is usually four pads or an unpopulated header near the SoC: ground, TX, RX and a supply pin, often 3.3V. Some are labelled and many aren’t. With the board unpowered I use the multimeter’s continuity mode. Ground beeps to the metal shield cans and to the ground of the power jack. The pad that beeps to the 3.3V regulator output is the supply pin, and I never connect that one to anything.

    Then I power the board from its own adapter and measure the other pads against ground, with insulated probe tips and a hand that stays still, because a slipped probe shorting two fine pads is how boards die. TX usually idles high and flickers during boot. RX usually idles high and doesn’t move, though a floating pad can read anything. I measure at idle after boot, since a meter averages a toggling line into a meaningless number.

    About 3.3V is what I expect. If a pad reads 1.8V, 2.5V or anything well above 3.6V, I stop. The divider from part 3 is wrong for that board and I need a proper level shifter.

    Finding the baud rate

    A UART has no clock, so the receiver has to know the bit time in advance. Consoles almost always use 8N1: eight data bits, no parity, one stop bit. The line idles high and a frame starts with a low start bit, which is how the sigrok UART decoder documentation puts it. So the shortest pulse on the line is one bit, and one over its width is the baud rate. 115200 baud is 8.68 µs per bit, 57600 is 17.36 µs and 9600 is 104.17 µs.

    With the board unpowered, analyzer ground goes to board ground first. Then one channel goes on the router’s TX pad and I power the board. I only listen, though I still touch only pads I have measured, because some SoC pins double as boot straps. I haven’t found a datasheet for the clone analyzer’s input limits, so I only clip it onto a line I have measured at 3.3V or lower.

    Sample rate matters. At 1 MHz a 115200 baud bit is 8.7 samples wide, which is fine. At 921600 baud it is 1.09 samples and you can’t measure anything. When hunting for an unknown rate I use the highest rate the analyzer gives me for the channels I enable, power cycle the router and capture the boot.

    In PulseView I put two cursors across the narrowest pulse. The same estimate in code is a few lines, and I snap the result to the nearest standard rate because one sample of error moves the raw number by several percent. The capture below is synthetic: I generated a 57600 baud signal with Python so I could show the decoding without publishing a real device’s boot log.

    Terminal output from a synthetic 57600 baud capture generated with Python: a pulse-width baud estimate, sigrok-cli decoding garbage at the wrong rate 115200, and readable text at 57600

    The screenshot is real output from sigrok-cli 0.7.2 and my small Python script, run on the synthetic file in a throwaway container.

    The first command measures the pulse: 17 samples at 1 MHz, 58824 raw, snapped to 57600. The second decodes the same file with a wrong guess of 115200 and gives bytes like 0x80 and 0x98 instead of text. At 57600 the text appears. The pulse method needs a bit that sits alone between two opposite levels, which boot messages almost always have. If the estimate is ambiguous I try the usual rates in turn, starting with 115200, the most common and U-Boot’s CONFIG_BAUDRATE default (boards override it). A capture of a real boot log is sensitive, since it can hold Wi-Fi keys, tokens and serial numbers.

    Talking to it

    Once I can read the boot text I know the rate. I still haven’t connected a transmitter, and that’s deliberate: the second wire goes in only after I can see the whole boot log.

    The wiring is part 3: cable ground to board ground first, board TX straight to the cable’s RX, and the cable’s TX through the 1 kΩ and 2 kΩ divider to the board’s RX. The cable’s red 5V wire stays insulated and unconnected. The board’s only power source is its own adapter. I plug the cable into USB last, and I never power cycle the board with the cable still plugged in, because the cable’s TX idling high into an unpowered board can feed it through the pin’s protection diodes. To finish I unplug USB, power the board off, remove the signal wires and take ground off last. Then I open the port:

    picocom -b 57600 /dev/ttyUSB0

    Use the rate you measured in place of 57600. picocom defaults to 8N1 with no flow control, and Ctrl-A then Ctrl-X exits. On Debian-family systems the account needs to be in the dialout group, and without it you get a permission error, not garbage. Garbage usually means the wrong rate, but I also check ground, voltage level and framing. Boot text with no response to typing can be a wiring fault, but it can also be the vendor’s doing: input disabled in the bootloader, or no login process on the port. I go back to the meter before I guess.

    What I check on my own boards

    Does the bootloader stop at a prompt if I press a key during the countdown? Does the kernel boot into a shell with no login? Does the boot log print secrets? Does root have a password? Any yes goes in my notes for that device. If the problem is the design and not just my unit, I tell the vendor. I don’t change a bootloader environment on a router I depend on until I have a verified flash backup. That is part 5.

    Closing the console

    The U-Boot documentation lists the knobs. The bootdelay variable set to -2 boots with no delay and doesn’t check for a keypress. Set to -1 it disables autoboot and leaves you at a prompt, the worst choice for hardening. CONFIG_AUTOBOOT_KEYED requires a specific stop string, so a stray character doesn’t halt the boot, though a string compiled into the image is not a password. silent_linux boots the kernel with console= empty, which hides output and removes no shell. These are environment settings, and the environment is often writable from the prompt or from flash, so they are only as strong as access to those. A build-time CONFIG_BOOTDELAY is stronger.

    On the OS side the fix is boring: no auto-login root shell on the serial port, a real login prompt, and a root password that isn’t shared across a product line. Leaving the header unpopulated costs a vendor nothing and stops casual attempts. It won’t stop someone who desolders the flash chip, and that’s what the next parts are about.

    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 3: using my 5V FTDI cable on 3.3V UARTs without killing the board

    Hardware hacking, part 3: using my 5V FTDI cable on 3.3V UARTs without killing the board

    Part 3 of 7 in the Hardware hacking series. Part 1: updating the firmware on a Pinecil V2. Part 2: the bench I work on.

    My USB serial cable has an FTDI FT232RL in it and is sold as a 5V cable. Most router and IoT consoles run their UART at 3.3V, a few at 1.8V. Feed a 5V transmit line into a 3.3V receiver and you are past what the chip is rated for. I would rather spend a few cents on resistors than find out whether a board survives.

    This is for boards I own, powered from a low-voltage supply. I have not opened the cable. The seller says 5V and I expect the data lines are 5V too, but cables that look alike can differ, so the meter has the last word.

    Two directions, two problems

    The diagram above is one I drew: the wiring order, the divider and the one wire that stays disconnected.

    A UART link crosses over: cable TX to target RX, target TX to cable RX.

    The cable receiving is the easy direction. The FT232R datasheet (version 2.07, Table 5.3, VCCIO = 5.0V) gives an input switching threshold of 1.0V minimum, 1.2V typical and 1.5V maximum. It says “switching threshold”, not VIH, so I don’t call it that either. Anything a 3.3V device drives above 1.5V reads as high, and the absolute maximum input is VCC + 0.5V, where VCC is the chip’s own supply (USB, about 5V). No shifting needed.

    The cable transmitting is the problem. Same table: output high is 3.2V minimum, 4.1V typical, 4.9V maximum at 2 mA. The ESP32 datasheet (version 5.3, Table 5-3) shows what a 3.3V part expects: input high tops out at VDD + 0.3V, so 3.6V. It’s an example, but one I can point to a number for. Only the cable’s TX wire has to come down.

    A two-resistor divider

    The cheapest fix is 1 kΩ in series from the cable’s TX, then 2 kΩ from the target’s RX pin to ground, so the pin sees two thirds of what the cable drives. This is the check I ran:

    R1, R2 = 1000.0, 2000.0
    cases = {"Voh min": 3.2, "Voh typ": 4.1, "Voh max": 4.9,
             "5.0 V ideal": 5.0, "VCCIO max 5.25 V": 5.25}
    for name, v in cases.items():
        i = v / (R1 + R2)
        print(f"{name:17s} Vout={v*R2/(R1+R2):5.3f} V  I={i*1000:4.2f} mA")

    The screenshot is the real output of that script. The 3 kΩ load draws 1.07 to 1.75 mA, below the 2 mA the datasheet uses as its test condition for Voh, so those figures are a fair basis. The pair dissipates about 9 mW at 5.25V, nothing for a 1/8 W resistor.

    At the target’s pin that is 2.13V worst case, 2.73V typical and 3.27V at the maximum. A receiver that needs 2.0V for a high passes, though not by much. The SN74LVC1G125 datasheet, for one, specifies 2V at a 3 to 3.6V supply. With 1% resistors the worst case is 2.12V, with 5% resistors 2.06V, and that assumes nothing else loads the pin. A pull-down on the target’s RX eats the margin, and so does a USB port that sags below 5V. A receiver that wants 0.75 × VDD, like the ESP32, needs 2.475V. It passes at 2.73V typical and fails at 2.13V. So the divider suits the usual LVTTL-style console receivers and is marginal for strict CMOS inputs. I would rather say that than call it safe.

    The image is page 18 of FTDI’s datasheet, with a red box I added around the Voh row of Table 5.3.

    I don’t use 2.2 kΩ and 3.3 kΩ even though they give a neat 3.00V unloaded: at the minimum Voh they give 1.92V, which fails even a 2.0V input. The high end matters too. At the maximum VCCIO of 5.25V (Table 5.2), my 1 kΩ and 2 kΩ pair with 5% resistors can put 3.61V on the pin, just over that 3.6V ceiling, and lower rails make it worse. With 1% resistors the worst case is 3.52V, so I buy 1% parts. Speed isn’t a problem: with 20 to 50 pF of pin and wire capacitance the time constant is 13 to 33 ns, against 8.68 µs per bit at 115200 baud.

    When I would skip the divider

    For a margin I can defend, a buffer is better. The SN74LVC1G125 is a single non-inverting buffer. Powered from 3.3V, its inputs accept up to 5.5V (recommended range, section 5.3) and 2V counts as high. Cable TX goes into pin 2, pin 4 goes to the target’s RX, and pin 1 (OE, active low) is tied to ground. It works in one direction, which is all this wire needs. The supply comes from the target’s 3.3V rail once I have identified it with a meter, or from a separate 3.3V source with common grounds, and the datasheet recommends a 0.1 µF bypass capacitor across it.

    The TXS0108E is the bidirectional option. Its A port runs from 1.4 to 3.6V and its B port from 1.65 to 5.5V with VCCA never above VCCB, so the target goes on A, the cable on B, and OE goes high to the target’s 3.3V rail. The only job I would give the cable’s red wire is powering VCCB, never the target. It’s more chip than a console needs. A 3.3V cable or adapter removes the problem entirely.

    A 1.8V target is a different case, and I look for it before I wire anything. Its output cannot exceed 1.8V, only 0.3V above the cable’s 1.5V maximum threshold, so the cable may not read it reliably, and the divider would hand a 1.8V receiver about 3.3V. That target needs a proper shifter or a 1.8V adapter.

    What I do at the bench

    I never connect the cable’s red 5V wire to a target. It gets insulated and parked. I haven’t verified the wire colours on my cable, so I read its pinout card.

    Before connecting anything I measure. With the target powered from its own supply and nothing else attached, I put a multimeter between its TX pad and ground, at idle, since a toggling line gives a misleading average. If I don’t see about 3.3V I stop. That covers 1.8V, 2.5V, a floating reading, and anything well above 3.6V. I use insulated probe tips and hold the board still, because a slipped probe shorting two fine pads is a real risk. Then I plug the cable into USB with nothing on its header and measure TXD against its ground wire. A listing that says 5V may only describe the power wire.

    Wiring goes in this order, with the cable unplugged from USB and the target off: cable ground to target ground, target TX straight to cable RXD, then cable TXD through the 1 kΩ to the target’s RX with the 2 kΩ from that point to ground. Then I power the target from its own supply, never from the cable as well, and plug the cable in last. At idle the target’s RX pin should read about 3.3V with an ideal 5V cable. Under 2V means something is loading the line, and anything above the target’s VDD + 0.3V means I unplug at once. To disconnect I unplug USB first, power the target down, then remove the wires. The order matters because a cable plugged into a dead board puts about 3.3V on its RX pin, and some of that can leak through the chip’s protection structures into a board that should be off.

    The last rule has no numbers. I never connect to a device that is plugged into mains. Smart plugs, bulbs and some routers use non-isolated supplies, where the board’s ground can sit at mains potential, and a serial cable then ties that to your PC. A USB isolator reduces the risk without making it safe. If I don’t know what powers a board, I don’t connect. If the target runs from a battery, I disconnect the battery before soldering, and I never short one or charge a swollen cell. I touch an earthed metal object before handling boards, hold them by the edges, and unplug before soldering or moving wires.

    Part 4 uses this adapter to find the console on my own router, with the logic analyzer to work out the baud rate.

    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.