4. Dump the flash¶
Flash to RAM, RAM to file¶
U-Boot cannot send a file to you, but it can print memory as hex with md.b. So the
classic way to dump a flash chip over a console is:
sf probeandsf read <ram> <offset> <length>: copy the flash into RAM;md.b <ram> <length>: print that RAM as hex, chunk by chunk, and turn the hex back into bytes on your side.
You learned in lesson 3 that this board's firmware is the first 0x8000 bytes of the
flash (sf read 0x82000000 0x0 0x8000). UBoot.dump() does step 2 for you, with progress,
retries and a file it appends to as it goes.
from hwntools import UBoot
from hwntools.learn.lab import FLASH_SIZE, RAM_ADDR, device
ub = UBoot(device())
ub.interrupt()
print(ub.run("sf probe").strip())
print(ub.run(f"sf read {RAM_ADDR:x} 0 {FLASH_SIZE:x}").strip()) # flash -> RAM
data = ub.dump(RAM_ADDR, FLASH_SIZE, out="flash.bin") # RAM -> file, over md.b
print(len(data), data[:24])
[x] Interrupting U-Boot autoboot
[+] Interrupting U-Boot autoboot: got prompt b'=> '
SF: Detected w25q128 with page size 256 Bytes, erase size 4 KiB, total 16 MiB
device 0 offset 0x0, size 0x8000
SF: 32768 bytes @ 0x0 Read: OK
[x] Dumping 0x8000 bytes from 0x82000000
[+] Dumping 0x8000 bytes from 0x82000000: 32768 bytes
32768 b'RX-AP100 boot stub (lab)'
You now have flash.bin. Keep it: the next two lessons use it.
Simulator limit
The built-in FakeUBoot has no sf command; hwntools.learn.lab adds a minimal one. The image is
only 32 KiB so the lesson runs in a second. A real 16 MiB flash over a 115200-baud
console takes hours with md.b (each byte travels as about four characters), which is
why the next part matters, and why lesson 7 shows faster ways.
When the wire comes loose¶
Long dumps break: a jumper wire slips, the adapter resets, the laptop sleeps. dump()
writes every chunk to the file as soon as it arrives, and when the file already holds part
of the dump it continues from the end of the file. lab.device(unplug_after_md=3)
simulates a cable that falls off after three md.b commands.
from pathlib import Path
from hwntools import UBoot
from hwntools.learn.lab import FLASH_SIZE, RAM_ADDR, device
def connect(**kwargs):
ub = UBoot(device(**kwargs))
ub.interrupt()
ub.run("sf probe")
ub.run(f"sf read {RAM_ADDR:x} 0 {FLASH_SIZE:x}")
return ub
# First attempt: the jumper wire falls off after three md.b commands.
ub = connect(unplug_after_md=3)
try:
ub.dump(RAM_ADDR, FLASH_SIZE, out="flash2.bin")
except EOFError as exc:
print("connection lost:", exc)
# Second attempt: same call, same file. It continues where it stopped.
ub = connect()
data = ub.dump(RAM_ADDR, FLASH_SIZE, out="flash2.bin")
print(data == Path("flash.bin").read_bytes())
[x] Interrupting U-Boot autoboot
[+] Interrupting U-Boot autoboot: got prompt b'=> '
[x] Dumping 0x8000 bytes from 0x82000000
[!] md.b at 0x82003000 failed (md.b output has 0 bytes, expected 4096), retry 1/3
[-] Dumping 0x8000 bytes from 0x82000000: Failed
connection lost: device closed
[x] Interrupting U-Boot autoboot
[+] Interrupting U-Boot autoboot: got prompt b'=> '
[*] Resuming dump at offset 0x3000 (12288 bytes on disk)
[x] Dumping 0x8000 bytes from 0x82000000
[+] Dumping 0x8000 bytes from 0x82000000: 32768 bytes
True
What just happened¶
- Three chunks of
0x1000bytes (the defaultchunk) reached the file before the console went silent. The fourthmd.bcame back empty,dump()retried, and then the closed console raisedEOFError. - The second run used the same call with the same file name. hwntools saw 12288 bytes on disk and asked only for the rest. The final file matches the clean dump from before.
- Pass
resume=Falseto start over, and remember that resuming only makes sense if the RAM still holds the same data: after a power cycle, redo thesf readfirst, asconnect()does here.
Try it¶
Try it
Real serial lines drop characters. device(corrupt_md_every=3) damages every third
md.b output. Dump the image from such a device with chunk=0x2000 and check that the
result is still identical to flash.bin.
Solution
from pathlib import Path
from hwntools import UBoot
from hwntools.learn.lab import FLASH_SIZE, RAM_ADDR, device
ub = UBoot(device(corrupt_md_every=3)) # every 3rd md.b loses a character
ub.interrupt()
ub.run("sf probe")
ub.run(f"sf read {RAM_ADDR:x} 0 {FLASH_SIZE:x}")
data = ub.dump(RAM_ADDR, FLASH_SIZE, chunk=0x2000)
print(data == Path("flash.bin").read_bytes())
[x] Interrupting U-Boot autoboot
[+] Interrupting U-Boot autoboot: got prompt b'=> '
[x] Dumping 0x8000 bytes from 0x82000000
[!] md.b at 0x82004000 failed (md.b address jump: expected 0x82004000, got 0x82004010), retry 1/3
[+] Dumping 0x8000 bytes from 0x82000000: 32768 bytes
True
The parser checks every md.b line's address and byte count. A damaged line fails the
whole chunk, which is read again (up to retries=3 times), so bad bytes never reach
the file.