firmware/stm32/mk4-bootloader
2022-11-14 15:10:02 -05:00
..
assets Correct fontfile 2022-01-27 10:49:37 -05:00
micro-ecc split from mk3 2021-06-17 10:36:09 -04:00
releases bootrom v3.1.5 2022-09-15 10:36:19 -04:00
.gitignore working psram 2021-05-12 10:02:58 -04:00
ae_config.h Add ReqRandom to two slots 2022-03-14 09:29:06 -04:00
ae.c Add ReqRandom to two slots 2022-03-14 09:29:06 -04:00
ae.h Add ReqRandom to two slots 2022-03-14 09:29:06 -04:00
aes.c working 2021-07-02 14:14:17 -04:00
aes.h working, fast AES 2021-06-25 10:33:52 -04:00
basics.h latest mk4 changes 2021-09-07 08:21:11 -04:00
clocks.c Better RTC setting 2022-01-31 11:39:25 -05:00
clocks.h anti-glitch measures, new ram layout 2021-05-26 09:53:54 -04:00
console.c working dual SE setup 2021-06-23 11:56:57 -04:00
console.h working AES, but fat 2021-06-25 09:25:40 -04:00
constant_time.c bugfix 2022-01-17 11:02:12 -05:00
constant_time.h bugfix 2022-01-17 11:02:12 -05:00
delay.c psram restore, soon sdcard recovery 2021-06-04 08:51:40 -04:00
delay.h Remove delay_us 2021-05-14 11:01:00 -04:00
dispatch.c Add SE version number reporting 2022-03-14 11:29:16 -04:00
dispatch.h Cleanups 2021-06-14 09:08:54 -04:00
faster_sha256.c latest mk4 changes 2021-09-07 08:21:11 -04:00
faster_sha256.h read hard secret 2021-06-30 11:39:23 -04:00
firewall.c Hash PIN w/ SE2 secrets 2022-03-01 11:51:14 -05:00
firmware-keys.h backport v4 fix, files 2022-11-14 15:10:02 -05:00
flash-unlock.txt bugfixes 2022-02-25 11:34:30 -05:00
gogo.gdb first commit 2021-05-11 14:03:13 -04:00
gpio.c Cleanups 2021-06-14 09:08:54 -04:00
gpio.h first commit 2021-05-11 14:03:13 -04:00
hal_glue.c Removed submod 2022-01-14 14:24:33 -05:00
keylayout.py Add ReqRandom to two slots 2022-03-14 09:29:06 -04:00
link-script.ld new flash layout 2022-02-01 12:42:52 -05:00
main.c bugfixes 2022-02-25 11:34:30 -05:00
main.h working dual SE setup 2021-06-23 11:56:57 -04:00
Makefile Hash PIN w/ SE2 secrets 2022-03-01 11:51:14 -05:00
mathcheck.py first commit 2021-05-11 14:03:13 -04:00
misc.h psram done 2021-05-13 09:49:15 -04:00
mk-sigheader.py runs 2021-05-21 13:55:34 -04:00
oled.c anti-glitch measures, new ram layout 2021-05-26 09:53:54 -04:00
oled.h first commit 2021-05-11 14:03:13 -04:00
pins.c Support 12-word duress wallets 2022-04-15 11:46:17 -04:00
pins.h Hash PIN w/ SE2 secrets 2022-03-01 11:51:14 -05:00
psram.c cap 2022-02-03 10:25:15 -05:00
psram.h psram restore, soon sdcard recovery 2021-06-04 08:51:40 -04:00
README.md psram restore, soon sdcard recovery 2021-06-04 08:51:40 -04:00
rng.c Bugfix and performance 2022-03-11 12:09:29 -05:00
rng.h anti-glitch measures, new ram layout 2021-05-26 09:53:54 -04:00
sdcard.c Cleanup 2022-02-23 08:59:54 -05:00
sdcard.h Cleanups 2021-06-14 09:08:54 -04:00
se2.c v3.1.5 changes, fix for bug141 2022-09-15 10:30:52 -04:00
se2.h Seed RNG with RNG from both SE's 2022-03-11 13:42:49 -05:00
secel_config.py Add ReqRandom to two slots 2022-03-14 09:29:06 -04:00
secel_debug.py first commit 2021-05-11 14:03:13 -04:00
secrets.h add notice 2022-03-07 14:34:53 -05:00
sigheader.h runs 2021-05-21 13:55:34 -04:00
sigheader.py runs 2021-05-21 13:55:34 -04:00
startup.S honest comment 2022-01-31 11:49:00 -05:00
stm32l4xx_hal_conf.h working AES, but fat 2021-06-25 09:25:40 -04:00
stm32l4xx_hal_firewall.c runs 2021-05-21 13:55:34 -04:00
stm32l4xx_hal_gpio.c first commit 2021-05-11 14:03:13 -04:00
stm32l4xx_hal_hash.c working 2021-07-02 14:14:17 -04:00
stm32l4xx_hal_i2c.c working dual SE setup 2021-06-23 11:56:57 -04:00
stm32l4xx_hal_ospi.c working dual SE setup 2021-06-23 11:56:57 -04:00
stm32l4xx_hal_rcc_ex.c working psram 2021-05-12 10:02:58 -04:00
stm32l4xx_hal_rcc.c working psram 2021-05-12 10:02:58 -04:00
stm32l4xx_hal_sd.c working dual SE setup 2021-06-23 11:56:57 -04:00
stm32l4xx_hal_spi.c Comments 2022-01-31 11:49:39 -05:00
stm32l4xx_ll_sdmmc.c psram restore, soon sdcard recovery 2021-06-04 08:51:40 -04:00
storage.c Hash PIN w/ SE2 secrets 2022-03-01 11:51:14 -05:00
storage.h new flash layout 2022-02-01 12:42:52 -05:00
verify.c Bugfix: MCU keys change at runtime, must be excluded from checksum 2022-03-22 08:41:24 -04:00
verify.h sdcard recovery mode 2021-06-04 11:28:12 -04:00
version.c first commit 2021-05-11 14:03:13 -04:00
version.h v3.1.5 changes, fix for bug141 2022-09-15 10:30:52 -04:00

Coldcard Bootloader - Mk3 and Mk4

We have a bootloader. It does the usual code signature checking, but also offers some security features used during runtime. Part of this is keeping some bytes secret in the long term. It can never be field upgraded, and yet plays an important part in that process.

Firewalled Code/Data

This code is linked separately from other executables, and resides in its own reserved area at the start of flash memory. That area is protected from readback using chip features: "Proprietary Code Read-Out Protection (PCROP)" aka. firewall.

A very limited amount of security-sensitive code resides here. It protects your currency, but only indirectly. It's more about making your key storage per-system unique.

Notes

  • the most helpful file here is bootloader.lss which is generated in build process

  • using OpenOCD is prefered for lower level code like this (not GDB)

  • stm32l4x.cpu arm disassemble 0x000008 10 thumb is very helpful

  • you can power cycle the board (to enter/exit DFU) and OpenOCD keeps working

  • for consistent reading of state, do this:

    • power cycle
    • "reset"
    • "halt"
  • wipe chip with:

    • stm32l4x mass_erase 0 in openocd monitor to bulk-erase whole chip
  • To clear flash with write protect on... FLASH regs at 0x40022000 base FLASH->CR = 0x40022014 FLASH->WRP1AR = 0x4002202c

    this works on Mk4: with less weird errors/warnings.

    have DFU active. doesn't work from running

    halt

    expect 0x40000000, if it's 0xc0000000, can't work; reboot w/ DFU pressed, to fix

    mdw 0x40022014

    ignore warning about "power cycle" from this:

    stm32l4x unlock 0

    expect 0 from this:

    mdw 0x40022014

    disable all write-protect (bank 1, A region)

    mww 0x4002202c 0xff00ffff mww 0x40022030 0xff00ffff

    commit change

    mww 0x40022014 0x20000

    read back in OB (expect ff00ffff NOT ff0fff00)

    mdw 0x1FFF7818

    launch changes? (causes weird reset)

    mww 0x40022014 0x8000000

  • "stm32l4x.cpu mdb" is nice hexdump, much better than regular mdb

  • If you're having trouble getting the debugger to started / link up right, try in DFU mode.

  • You must always wipe flash when you change the 508/608 because no code to erase the pairing secret and can't rewrite flash.

    halt stm32l4x unlock 0 stm32l4x mass_erase 0

Reading 'pairing secret'

This is a useful command, but only works on non-production units:

Mk1-3:

dfu-util -d 0483:df11 -a 0 -s 0x08007800:256 -U pairing.bin

Mk4:

dfu-util -d 0483:df11 -a 0 -s 0x0801c000:8192 -U pairing.bin
  • but that file is misleading, because all the unused mcu key slots are un-programmed-cells (ones)
  • will hit assert on new key attempt if you just write that back
  • trim and write only actual non-ones content

Wiping Secrets

Mk4: flash erase_address 0x801c000 0x4000

Resources

Todo List

  • measure OLED reset and CS pulse lengths, and SPI clk during boot w/ internal RC oscilator
  • HAL code for SPI should be removed and replaced with a few one-liners
  • GPIO code may be removed as well?

Bootloader upgrade 3.0.? to 3.0.2

  • capture pairing data:

    dfu-util -d 0483:df11 -a 0 -s 0x0801e000:8192 -U pairing.bin

  • do the above unlock write-protect process, but don't erase any flash

    • would only be required if "bag" operation done
  • then upload old pairing data

    dfu-util -d 0483:df11 -a 0 -s 0x0801c000:8192 -D pairing.bin

  • unit will boot w/ no seed

  • for cleanest result, wipe last flash page:

    flash erase_address 0x0801e000 0x2000

Bootloader upgrade 3.0.? to 3.1.?

  • clear main PIN before install, or else you won't be able to get back in!

Re-do Bag Number

  • cannot writes ones, and then change flash cells; have to remain unprogrammed

    dfu-util -d 0483:df11 -a 0 -s 0x0801c000:8192 -U pairing.bin

  • want 0..0x50 then 0x70

    dd if=pairing.bin of=p1.bin bs=80 count=1 dd if=pairing.bin of=p2.bin bs=112 skip=1

  • two burns: => DFU does not work, each run erases other part of page => same with "program" cmd in openocd => these work tho:

    flash erase_address 0x801c000 0x2000 flash write_image mk4-bootloader/p1.bin 0x0801c000 flash write_image mk4-bootloader/p2.bin 0x0801c070