sodnpoo.com

jtag flashing and dumping with openocd 0.8.0

A few weeks ago, Paul Fertser (one of the the openocd devs) mailed me to say that he had seen my post on using openocd and a buspirate to flash and dump bcm6348 boards and had written a firmware recovery script to make the process much simpler. A flash/dump can now be achieved in a single command line; here's how to show the help text:

  openocd -f tools/firmware-recovery.tcl -c firmware_help
  

And the output:

lee@monkeybox ~/Downloads/openocd/openocd $ openocd -f tools/firmware-recovery.tcl -c firmware_help
Open On-Chip Debugger 0.8.0-rc1-dev-00439-ga719779-dirty (2014-04-06-12:11)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.sourceforge.net/doc/doxygen/bugs.html


Firmware recovery helpers
Use -c firmware_help to get help

adapter speed: 1000 kHz


Your OpenOCD command should look like this:
openocd -f interface/<jtag adapter>.cfg -f tools/firmware-recovery.tcl -c "<commands>*; shutdown"

Where:
<jtag adapter> is one of the supported devices, e.g. ftdi/jtagkey2
<commands> are firmware-recovery commands separated by semicolon

Supported commands:
firmware_help			get this help
list_boards			list known boards and exit
board <name>			select board you work with
list_partitions			list partitions of the currently selected board
dump_part <name> <filename>	save partition's contents to a file
erase_part <name>		erase the given partition
flash_part <name> <filename>	erase, flash and verify the given partition
ram_boot <filename>		load binary file to RAM and run it
adapter_khz <freq>		set JTAG clock frequency in kHz

For example, to clear nvram and reflash CFE on an RT-N16 using TUMPA, run:
openocd -f interface/ftdi/tumpa.cfg -f tools/firmware-recovery.tcl \
	-c "board asus-rt-n16; erase_part nvram; flash_part CFE cfe-n16.bin; shutdown"



shutdown command invoked
Error: Debug Adapter has to be specified, see "interface" command
in procedure 'init'  
  

The script supports a handful of commands, that are specified using '-c' and that can be chained together by separating them with semi-colons and wrapping the whole string in quotes ("command1; command2; etc"). A list of supported boards can be seen like this:

lee@monkeybox ~/Downloads/openocd/openocd $ openocd -f tools/firmware-recovery.tcl -c list_boards
Open On-Chip Debugger 0.8.0-rc1-dev-00439-ga719779-dirty (2014-04-06-12:11)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.sourceforge.net/doc/doxygen/bugs.html


Firmware recovery helpers
Use -c firmware_help to get help

adapter speed: 1000 kHz

List of the supported boards:

Board name		Description
-----------------------------------
asus-rt-n16		ASUS RT-N16
linksys-wrt54gl		Linksys WRT54GL v1.1
netgear-dg834v3		Netgear DG834G v3
bt-homehubv1		BT HomeHub v1



Error: Debug Adapter has to be specified, see "interface" command
in procedure 'init'  
  

Two commands are chained together ("board bt-homehubv1; list_partitions") to select the BT Homehub and list it's partitions (I've included the busblaster interface config - change as fit for your programmer):

lee@monkeybox ~/Downloads/openocd/openocd $ openocd -f interface/busblaster.cfg -f \ 
tools/firmware-recovery.tcl -c "board bt-homehubv1; list_partitions"
Open On-Chip Debugger 0.8.0-rc1-dev-00439-ga719779-dirty (2014-04-06-12:11)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.sourceforge.net/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'jtag'


Firmware recovery helpers
Use -c firmware_help to get help

adapter speed: 1000 kHz


The currently selected board is known to have these partitions:

Name            Start         Size          Description
-------------------------------------------------------
CFE             0xbe400000    0x00020000    Bootloader
firmware        0xbe420000    0x007D0000    Kernel+rootfs
fisdir          0xbebf0000    0x0000f000    FIS Directory
nvram           0xbebff000    0x00001000    Config space



Warn : Using DEPRECATED interface driver 'ft2232'
Info : Consider using the 'ftdi' interface driver, with configuration files in interface/ftdi/...
Error: unable to open ftdi device: device not found
in procedure 'init'  
  

And finally, dumping the CFE bootloader (already flashed with redboot on my HomeHubv1):

lee@monkeybox ~/Downloads/openocd/openocd $ time sudo openocd -f interface/busblaster.cfg -f \ 
tools/firmware-recovery.tcl -c "board bt-homehubv1;dump_part CFE redboot.fwrecovery.test.bin; shutdown"
Open On-Chip Debugger 0.8.0-rc1-dev-00439-ga719779-dirty (2014-04-06-12:11)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.sourceforge.net/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'jtag'


Firmware recovery helpers
Use -c firmware_help to get help

adapter speed: 1000 kHz

Warn : Using DEPRECATED interface driver 'ft2232'
Info : Consider using the 'ftdi' interface driver, with configuration files in interface/ftdi/...
Info : max TCK change to: 30000 kHz
Info : clock speed 1000 kHz
Info : JTAG tap: bcm6348.cpu tap/device found: 0x0634817f (mfg: 0x0bf, part: 0x6348, ver: 0x0)
target state: halted
target halted in MIPS32 mode due to debug-request, pc: 0x9e40a4b0
Info : JTAG tap: bcm6348.cpu tap/device found: 0x0634817f (mfg: 0x0bf, part: 0x6348, ver: 0x0)
Error: Error writing unexpected address 0xff202004
target state: halted
target halted in MIPS32 mode due to debug-request, pc: 0x9e40a4b0
dumped 131072 bytes in 668.114136s (0.192 KiB/s)
shutdown command invoked

real	11m9.933s
user	0m6.680s
sys	0m13.300s
  

Board config files are pretty straight forward, just defining how the target, the flash and how the flash has been carved up (usually can be extracted from the bootloader):

#
# BT HomeHub v1
# 

set partition_list {
    CFE       { Bootloader        0xbe400000 0x00020000 }
    firmware  { "Kernel+rootfs"   0xbe420000 0x007D0000 }
    fisdir    { "FIS Directory"   0xbebf0000 0x0000f000 }
    nvram     { "Config space"    0xbebff000 0x00001000 }
}

source [find target/bcm6348.cfg]

set _FLASHNAME $_CHIPNAME.norflash
flash bank $_FLASHNAME cfi 0xbe400000 0x00800000 2 2 $_TARGETNAME  
  

The recovery script will be included in the 0.8.0 release and hopefully my patches for the bcm6348 target and the Home Hub will be too :)