OpenOCD

Open On-Chip Debugger fournissant le débogage JTAG/SWD, la programmation flash in-system, et le boundary-scan pour le hacking matériel d'équipements embarqués et IoT. Open On-Chip Debugger providing JTAG/SWD debugging, in-system flash programming, and boundary-scan for embedded and IoT device hardware hacking.

↗ https://openocd.org

Overview

OpenOCD (Open On-Chip Debugger) bridges a JTAG/SWD hardware debug adapter (FTDI-based probes, ST-Link, J-Link, Bus Pirate, etc.) to standard debugging tools like GDB. For hardware/IoT security work it’s used to halt a target MCU, dump firmware straight from flash, patch and reflash code, and set hardware breakpoints — essential when a device has no other way to extract or inspect its firmware.

Common Usage

Start OpenOCD with an interface + target config (adapt to your probe/chip)

openocd -f interface/ftdi/um232h.cfg -f target/stm32f4x.cfg

Generic FTDI-based probe against a generic ARM Cortex-M target

openocd -f interface/jlink.cfg -f target/stm32f1x.cfg

Specify adapter speed explicitly

openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "adapter speed 4000"

Dumping Firmware via GDB

Terminal 1: start OpenOCD, exposes a GDB server on :3333

openocd -f interface/stlink.cfg -f target/stm32f4x.cfg

Terminal 2: connect with GDB and dump flash to a file

gdb-multiarch -ex "target remote localhost:3333" \
  -ex "monitor halt" \
  -ex "dump binary memory firmware.bin 0x08000000 0x08100000" \
  -ex "monitor resume"

Direct Telnet/TCL Interface

# OpenOCD also exposes a telnet console on :4444 for interactive commands
telnet localhost 4444
> halt
> flash read_bank 0 dump.bin
> reset run

Tips

  • Always confirm the exact flash base address/size for the target chip’s datasheet before dumping — wrong offsets produce garbage or crash the target.
  • Read protection (RDP/lock bits) on many MCUs (e.g. STM32 RDP level 1/2) can block flash reads via JTAG/SWD entirely — check protection status before assuming a dump will work.
  • interface/ and target/ config files ship with OpenOCD for most common probes and chip families — check /usr/share/openocd/scripts/ before writing a custom one.
Help / Man page
Usage: openocd [options]

  -f, --file <file>       use configuration file <file>
  -c, --command <cmd>     run <cmd> after config files are parsed
  -d, --debug [<n>]        set debug level
  -l, --log_output <file>  redirect log output to <file>
  -s, --search <dir>       add <dir> to search path for config/script files
  -v, --version            show program version
  --help                   show this help

Default ports:
  4444    telnet command interface
  3333    GDB server
  6666    Tcl RPC interface

Vue d’ensemble

OpenOCD (Open On-Chip Debugger) fait le pont entre un adaptateur de débogage matériel JTAG/SWD (sondes FTDI, ST-Link, J-Link, Bus Pirate, etc.) et des outils de débogage standard comme GDB. Pour le travail de sécurité hardware/IoT, il sert à mettre en pause un MCU cible, dumper le firmware directement depuis la flash, patcher et reflasher du code, et poser des points d’arrêt matériels : essentiel quand un équipement n’a aucun autre moyen d’extraire ou d’inspecter son firmware.

Utilisation courante

# Démarrer OpenOCD avec une config interface + target (adapter à votre sonde/puce)
openocd -f interface/ftdi/um232h.cfg -f target/stm32f4x.cfg

# Sonde générique FTDI contre une cible générique ARM Cortex-M
openocd -f interface/jlink.cfg -f target/stm32f1x.cfg

# Spécifier explicitement la vitesse de l'adaptateur
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "adapter speed 4000"

Dumper le firmware via GDB

# Terminal 1 : démarrer OpenOCD, expose un serveur GDB sur :3333
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg

# Terminal 2 : se connecter avec GDB et dumper la flash dans un fichier
gdb-multiarch -ex "target remote localhost:3333" \
  -ex "monitor halt" \
  -ex "dump binary memory firmware.bin 0x08000000 0x08100000" \
  -ex "monitor resume"

Interface Telnet/TCL directe

# OpenOCD expose aussi une console telnet sur :4444 pour les commandes interactives
telnet localhost 4444
> halt
> flash read_bank 0 dump.bin
> reset run

Conseils

  • Toujours vérifier l’adresse de base/taille exacte de la flash pour la puce cible dans la datasheet avant de dumper : de mauvais offsets produisent des données invalides ou plantent la cible.
  • La protection en lecture (bits RDP/lock) sur de nombreux MCU (par exemple STM32 RDP niveau 1/2) peut bloquer entièrement les lectures flash via JTAG/SWD : vérifier le statut de protection avant de supposer qu’un dump fonctionnera.
  • Les fichiers de config interface/ et target/ sont fournis avec OpenOCD pour la plupart des sondes et familles de puces courantes : vérifier /usr/share/openocd/scripts/ avant d’en écrire un personnalisé.
Aide / Page de manuel
Usage: openocd [options]

  -f, --file <file>       use configuration file <file>
  -c, --command <cmd>     run <cmd> after config files are parsed
  -d, --debug [<n>]        set debug level
  -l, --log_output <file>  redirect log output to <file>
  -s, --search <dir>       add <dir> to search path for config/script files
  -v, --version            show program version
  --help                   show this help

Default ports:
  4444    telnet command interface
  3333    GDB server
  6666    Tcl RPC interface