add scripts to prepare firmware

This commit is contained in:
Michele Balistreri 2018-05-15 11:21:38 +03:00
parent cdbce138b7
commit 073a167516
5 changed files with 74 additions and 0 deletions

4
.gitignore vendored
View File

@ -1 +1,5 @@
/Debug/
/scripts/
!/scripts/*.py
!/scripts/*.sh
*.cfg

19
scripts/fw_load.sh Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
case "$1" in
"firmware")
address="0x8002000"
;;
"recovery")
address="0x8041000"
;;
"upgrade")
address="0x8080000"
;;
*)
echo "You must tell which part of memory you want to load. Possible values are firmware, recovery and upgrade"
exit 1
;;
esac
st-flash write fw.bin $address

19
scripts/fw_prepare.py Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env python
import struct
SIGNATURE_AREA_SIZE = 512 - 8
def main():
fw = open("fw.bin", "rb").read()
if fw[:4] == b'SHWF':
print("Firmware already prepared")
exit(1)
header = b'SHWF'
header += struct.pack('<I', len(fw))
header += b'\x00' * SIGNATURE_AREA_SIZE
open("fw.bin", "wb").write(header + fw)
if __name__ == '__main__':
main()

32
scripts/fw_sign.py Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env python
import ecdsa
import hashlib
import sys
SIG_LEN = 64
SIG_OFF = 8
HEADER_SIZE = 512
def main(index):
fw = open("fw.bin", "rb").read()
if index < 0 or index > 6:
print("Index must be between 0 and 6")
exit(1)
if fw[:4] != b'SHWF':
print("Firmware must be prepared with fw_prepare.py first!")
exit(1)
sk_pem = open("secret_key.pem", "r").read()
sk = ecdsa.SigningKey.from_pem(sk_pem)
plain_fw = fw[HEADER_SIZE:]
fw_sig = sk.sign_deterministic(plain_fw, hashfunc=hashlib.sha256)
fw = fw[:SIG_OFF + SIG_LEN * (index) ] + fw_sig + fw[SIG_OFF + SIG_LEN * (index + 1):]
open("fw.bin", "wb").write(fw)
if __name__ == '__main__':
if len(sys.argv) != 2:
print("You must insert the (zero-based) index of the signature")
exit(1)
main(int(sys.argv[1]))