Skip to content

Commit 34aeb33

Browse files
committed
add bootloader lock support
1 parent 213f886 commit 34aeb33

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ To print out the serial console from the STM32, run tests/debug_console.py
6262

6363
Hardware
6464
------
65+
6566
Check out the hardware [guide](https://github.com/commaai/panda/blob/master/docs/guide.pdf)
6667

6768
Licensing

board/bootstub.c

+21
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,28 @@
1313

1414
#include "obj/cert.h"
1515

16+
void lock_bootloader() {
17+
if (FLASH->OPTCR & FLASH_OPTCR_nWRP_0) {
18+
FLASH->OPTKEYR = 0x08192A3B;
19+
FLASH->OPTKEYR = 0x4C5D6E7F;
20+
21+
// write protect the bootloader
22+
FLASH->OPTCR &= ~FLASH_OPTCR_nWRP_0;
23+
24+
// OPT program
25+
FLASH->OPTCR |= FLASH_OPTCR_OPTSTRT;
26+
while (FLASH->SR & FLASH_SR_BSY);
27+
28+
// relock it
29+
FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
30+
31+
// reset
32+
NVIC_SystemReset();
33+
}
34+
}
35+
1636
void __initialize_hardware_early() {
37+
lock_bootloader();
1738
early();
1839
}
1940

board/main.c

+2
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,8 @@ int main() {
897897
#endif
898898
__enable_irq();
899899

900+
puts("OPTCR: "); puth(FLASH->OPTCR); puts("\n");
901+
900902
// LED should keep on blinking all the time
901903
uint64_t cnt;
902904
for (cnt=0;;cnt++) {

board/tools/bl_unlock.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
import usb1
3+
import struct
4+
from hexdump import hexdump
5+
6+
def dostat():
7+
while 1:
8+
dat = dev.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6)
9+
hexdump(dat)
10+
if dat[1] == "\x00":
11+
break
12+
13+
context = usb1.USBContext()
14+
dev = context.openByVendorIDAndProductID(0x0483, 0xdf11)
15+
dev.claimInterface(0)
16+
print dev
17+
18+
DFU_DNLOAD = 1
19+
DFU_UPLOAD = 2
20+
DFU_GETSTATUS = 3
21+
DFU_CLRSTATUS = 4
22+
DFU_ABORT = 6
23+
24+
# Clear status
25+
stat = dev.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6)
26+
hexdump(stat)
27+
if stat[4] == "\x0a":
28+
dev.controlRead(0x21, DFU_CLRSTATUS, 0, 0, 0)
29+
elif stat[4] == "\x09":
30+
dev.controlWrite(0x21, DFU_ABORT, 0, 0, "")
31+
dostat()
32+
hexdump(dev.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
33+
34+
# Read Unprotect
35+
#dev.controlWrite(0x21, DFU_DNLOAD, 0, 0, "\x92")
36+
#hexdump(dev.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
37+
38+
# Set Address Pointer
39+
dev.controlWrite(0x21, DFU_DNLOAD, 0, 0, "\x21" + struct.pack("I", 0x1fffc000))
40+
dostat()
41+
42+
# Abort
43+
dev.controlWrite(0x21, DFU_ABORT, 0, 0, "")
44+
dostat()
45+
46+
# Dump
47+
val = dev.controlRead(0xA1, DFU_UPLOAD, 2, 0, 0x10)
48+
hexdump(val)
49+
50+
# Abort
51+
dev.controlWrite(0x21, DFU_ABORT, 0, 0, "")
52+
dostat()
53+
54+
# Set Address Pointer
55+
dev.controlWrite(0x21, DFU_DNLOAD, 0, 0, "\x21" + struct.pack("I", 0x1fffc000))
56+
dostat()
57+
58+
#val = val[0:8] + "\xfe\x7f\x01\x80"*2
59+
val = val[0:8] + "\xff\x7f\x00\x80"*2
60+
61+
# Program
62+
dev.controlWrite(0x21, DFU_DNLOAD, 2, 0, val)
63+
dostat()
64+

0 commit comments

Comments
 (0)