Automating Hardware Diagnostics: Resurrecting Motherboards with Python and a Raspberry Pi

By Mesue Collins Asibong • Embedded Systems & Python Engineering • 9 min read
View Open-Source Repository (`py_programmer.py`)
Automating Hardware Diagnostics: Resurrecting Motherboards with Python and a Raspberry Pi

When a laptop or desktop motherboard has stable power rails (3.3V and 5V always-on rails present) yet refuses to complete Power-On Self-Test (POST), a corrupted SPI NOR flash BIOS/UEFI image is one of the most frequent culprits. Traditionally, flashing a bricked BIOS chip requires either desoldering the SOIC-8 chip for a dedicated hardware programmer or compiling heavy C toolchains like flashrom.

Based on my talk at PyCon Africa 2026, this article walks through the architecture of py_programmer.py—a lightweight, standalone Text User Interface (TUI) BIOS programmer written in pure Python. Using only spidev and Python's built-in curses library, it communicates directly with the Raspberry Pi's hardware SPI registers via /dev/spidev0.0 using raw JEDEC opcodes, running effortlessly even on legacy boards like the Raspberry Pi 1.

Pure-Python SPI BIOS Flasher Architecture (`py_programmer.py`)

flowchart LR classDef tui fill:#3b82f6,stroke:#fff,stroke-width:2px,color:#fff classDef spi fill:#f59e0b,stroke:#000,stroke-width:2px,color:#000 classDef hw fill:#10b981,stroke:#fff,stroke-width:2px,color:#fff classDef web fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff Remote(["GitHub or Google Drive ROM"]):::web -->|urllib Direct Stream| TUI["Curses TUI Controller"]:::tui TUI -->|JEDEC Opcodes 0x9F 0x20 0x02| SPIDev["spidev0.0 Kernel Driver"]:::spi SPIDev -->|GPIO SPI0 Bus + 3.3V Pull-Ups| Chip["SOIC-8 BIOS Flash Chip"]:::hw Chip -->|Read-Back Verification 0x03| TUI

Step 1: Hardware Pin Mapping & Macronix/Winbond Signal Stabilization

To interface with the motherboard's SOIC-8 SPI flash chip using a test clip or breakout socket, we wire the chip directly to the Raspberry Pi's hardware SPI0 header. A critical hardware nuance we discovered on the repair bench—especially with Macronix (`0xC2`) and Winbond (`0xEF`) chips—is that Pin 3 (`/WP` Write Protect) and Pin 7 (`/HOLD` Reset) must not be left floating; they must be pulled high by bridging them directly to the 3.3V VCC rail to prevent mid-transfer bus lockups:

SOIC-8 Chip Pin   Function                  Raspberry Pi Physical GPIO Pin
--------------------------------------------------------------------------
Pin 1 (/CS)       Chip Select               Pin 24 (SPI0_CE0_N)
Pin 2 (MISO)      Master In Slave Out       Pin 21 (SPI0_MISO)
Pin 3 (/WP)       Write Protect             Bridge to Pin 8 (3.3V VCC)
Pin 4 (GND)       Ground                    Pin 25 (GND)
Pin 5 (MOSI)      Master Out Slave In       Pin 19 (SPI0_MOSI)
Pin 6 (SCLK)      Serial Clock              Pin 23 (SPI0_SCLK)
Pin 7 (/HOLD)     Hold / Reset              Bridge to Pin 8 (3.3V VCC)
Pin 8 (VCC)       3.3V Power Supply         Pin 17 (3.3V Power)

Step 2: Dynamic JEDEC Silicon Profiling (`0x9F`)

Instead of maintaining a massive static lookup table of flash chip part numbers, SPIFlashProgrammer sends the raw JEDEC Read ID opcode (0x9F) over SPI Mode 0. The chip responds with 3 bytes: Manufacturer ID (0xC2 for Macronix, 0xEF for Winbond) and a 2-byte Device ID where the capacity byte (dev_id2) encodes the exact silicon size as a power of two (1 << dev_id2):

# Low-Level SPI Flash JEDEC Opcodes
CMD_READ_ID      = 0x9F
CMD_READ_DATA    = 0x03
CMD_WRITE_ENABLE = 0x06
CMD_SECTOR_ERASE = 0x20
CMD_PAGE_PROGRAM = 0x02
CMD_READ_STATUS  = 0x05

class SPIFlashProgrammer:
    def __init__(self, bus=0, device=0, speed=512000):
        self.spi = spidev.SpiDev()
        self.spi.open(bus, device)
        self.spi.max_speed_hz = speed
        self.spi.mode = 0b00  # SPI Mode 0
        self.chip_name = "Unknown / Disconnected"
        self.chip_size_bytes = 0

    def read_jedec_id(self):
        """Reads Manufacturer ID and Device ID (Command 0x9F)"""
        response = self.spi.xfer2([CMD_READ_ID, 0x00, 0x00, 0x00])
        man_id, dev_id1, dev_id2 = response[1], response[2], response[3]

        if man_id in (0x00, 0xFF):
            self.chip_name = "Unknown / Disconnected"
            self.chip_size_bytes = 0
            return False

        man_str = "Macronix" if man_id == 0xC2 else ("Winbond" if man_id == 0xEF else f"Unknown (0x{man_id:02X})")

        if 0x10 <= dev_id2 <= 0x25:
            # Translate capacity code into exact byte count (e.g. 0x17 -> 8 MB, 0x18 -> 16 MB)
            self.chip_size_bytes = 1 << dev_id2
            size_mb = self.chip_size_bytes / (1024 * 1024)
            self.chip_name = f"{man_str} ID:0x{dev_id1:02X}{dev_id2:02X} ({size_mb:.1f} MB)"
            return True
        return False

Step 3: Sector Erase, Page Programming & WIP Status Polling

SPI NOR flash memory cannot overwrite bits from 0 back to 1 directly—cells must first be erased to 0xFF in 4KB sectors (CMD_SECTOR_ERASE = 0x20) and then programmed in 256-byte pages (CMD_PAGE_PROGRAM = 0x02). Before every erase or write operation, we issue Write Enable (0x06) and poll the Status Register (0x05) until Bit 0 (Write-In-Progress / WIP) clears:

    def wait_while_busy(self):
        """Polls status register until WIP (Write In Progress) bit 0 is cleared"""
        while True:
            status = self.spi.xfer2([CMD_READ_STATUS, 0x00])[1]
            if not (status & 0x01):
                break
            time.sleep(0.0005)

    def erase_chip(self, progress_callback=None):
        """Wipes the chip cell grid sequentially in 4KB sector steps"""
        sector_size = 4096
        for addr in range(0, self.chip_size_bytes, sector_size):
            self.spi.xfer2([CMD_WRITE_ENABLE])
            cmd = [CMD_SECTOR_ERASE, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF]
            self.spi.xfer2(cmd)
            self.wait_while_busy()
            if progress_callback:
                progress_callback(addr + sector_size, self.chip_size_bytes)
        return True

    def write_chip(self, filename, progress_callback=None):
        """Programs chip page by page in 256-byte bursts"""
        page_size = 256
        with open(filename, 'rb') as f:
            data = f.read()

        for addr in range(0, min(len(data), self.chip_size_bytes), page_size):
            chunk = list(data[addr:addr + page_size])
            if len(chunk) < page_size:
                chunk += [0xFF] * (page_size - len(chunk))

            self.spi.xfer2([CMD_WRITE_ENABLE])
            cmd = [CMD_PAGE_PROGRAM, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF] + chunk
            self.spi.xfer2(cmd)
            self.wait_while_busy()
            if progress_callback:
                progress_callback(addr + page_size, self.chip_size_bytes)
        return True

Step 4: Remote BIOS Streaming, Dynamic Padding & Read-Back Verification

On a busy repair bench, technicians often store known-good board dumps on Google Drive or GitHub repositories. py_programmer.py integrates a direct downloader inside the curses TUI that parses Google Drive share links (automatically resolving large-file confirmation tokens) or raw GitHub URLs right onto the Raspberry Pi.

Before writing a single byte to silicon, the safety interceptor verifies the payload size against chip_size_bytes. If a BIOS image is smaller than the physical chip capacity, it dynamically pads the remaining address space with 0xFF, flashes the matrix, and performs a full 2KB-chunked read-back pass (`CMD_READ_DATA = 0x03`) to verify that the silicon contents match the source binary byte-for-byte:

# Deploy and launch directly on any Raspberry Pi
cd ~
wget https://raw.githubusercontent.com/mesuecollins/py_programmer.py/main/py_programmer.py -O py_programmer.py
chmod +x ~/py_programmer.py
sudo python3 ~/py_programmer.py

Summary

By speaking raw JEDEC opcodes directly over /dev/spidev0.0 in pure Python, py_programmer.py turns any Raspberry Pi into a self-contained, zero-dependency BIOS recovery workstation—proving how high-level Python ergonomics and low-level silicon engineering can come together to resurrect dead hardware.