-
Notifications
You must be signed in to change notification settings - Fork 17
/
Justfile
180 lines (141 loc) · 5.48 KB
/
Justfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# SPDX-FileCopyrightText: 2021 Foundation Devices, Inc. <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
# Install dependencies.
deps:
@echo "Not sure we will need this if all deps are setup via Dockerfile"
# Initialize development environment
init: deps
git config core.hooksPath .githooks
# Lint only the python code of the project
lint-py:
pycodestyle --exclude trezor-firmware,graphics.py --statistics boards/Passport
# Lint only the C code of the project
lint-c:
@echo "TBD"
# Lint only the code of the project
lint-code: lint-py lint-c
# Lint the licensing
lint-license:
reuse lint
# Lint all of the project
lint: lint-code lint-license
#
# Firmware Commands
#
build:
make BOARD=Passport -j$(nproc)
# Sign current firmware build with the user.pem key and set specified version
sign version="1.0.0": build
@echo "\nAdding user signature...\n"
@cosign -f build-Passport/firmware.bin -k ~/bin/keys/user.pem -v {{version}} > /dev/null
@cosign -f build-Passport/firmware-key-user.bin -x
@echo "\nSigning Complete!"
# Build, sign and flash the firmware with the specified version
flash version="1.0.0": (sign version)
just run-ocd-command "flash write_image erase build-Passport/firmware-key-user.bin 0x8020000"
just reset
# Install a recent Foundation-signed build
flash-foundation version="1.0.0":
just run-ocd-command "flash write_image erase ../../releases/passport-fw-{{version}}.bin 0x8020000"
just reset
# Clean the firmware build
clean:
make BOARD=Passport clean
#
# Misc. Commands
#
# Launch OCD, run a command and then exit
run-ocd-command command:
sudo /usr/local/bin/openocd -f stlink.cfg -c "adapter speed 1000; transport select hla_swd" -f stm32h7x.cfg -c "init; reset halt; {{command}}" -c "exit"
run-ocd-command-no-halt command:
sudo /usr/local/bin/openocd -f stlink.cfg -c "adapter speed 1000; transport select hla_swd" -f stm32h7x.cfg -c "init; {{command}}" -c "exit"
# Build all Python graphics
graphics-py:
just -f boards/Passport/graphics/py/Justfile build
# Build all C graphics (firmware & bootloader)
graphics-c:
just -f boards/Passport/graphics/c/Justfile build
graphics: graphics-py graphics-c
# Reset the Passport
reset:
just run-ocd-command "reset"
# Get the username for use below
user := `whoami`
# Read the "ROM Secrets" from Passport and save them to a file
save-secrets filename="boards/Passport/bootloader/secrets":
just run-ocd-command "dump_image {{filename}} 0x0801FF00 256"
# Running OCD as sudo makes the output file be owned by root, so switch it back to the user
sudo chown {{user}}:{{user}} {{filename}}
secrets:
#!/usr/bin/env bash
# The last bit below redirects stderr to stdout, which the backticks capture into the variable `secrets`
secrets=`just run-ocd-command "mdb 0x0801FF00 256" 2>&1`
secrets=`echo "$secrets" | tail -n 8`
echo -e "Passport ROM Secrets:\n$secrets"
# Calculate all hashes and format it all for GitHub release notes
hash filepath:
#!/usr/bin/env bash
filename=`basename {{filepath}}`
# SHA256
sha=`shasum -b -a 256 {{filepath}} | sed -rn 's/^(.*) .*$/\1/p'`
echo -e "\n\`SHA256: $sha\`"
echo -e "\`(shasum -b -a 256 $filename)\`\n"
# MD5
md5=`mdsum {{filepath}} | sed -rn 's/^(.*) .*$/\1/p'`
echo "\`MD5: $md5\`"
echo -e "\`(md5 $filename or mdsum $filename)\`\n"
# Build Hash
build_hash=`cosign -f {{filepath}} -x | sed -rn 's/^FW Build Hash: (.*)$/\1/p'`
echo -e "\`Build Hash: $build_hash\`"
echo -e "\`(Developers Only)\`\n"
# Run all tests
test:
@echo "TBD"
# Format the project's .py files under boards/Passport/modules
fmt-py:
#!/usr/bin/env bash
pushd boards/Passport/modules
files_to_fmt=`find . -path ./trezor-firmware -prune -false -o -name '*.py'`
autopep8 --max-line-length=120 --in-place $files_to_fmt
popd
# Format the project's .c and .h files under boards/Passport/
fmt-c:
#!/usr/bin/env bash
pushd boards/Passport
files_to_fmt=`find . -path ./trezor-firmware -prune -false -o -name '*.[c|h]'`
clang-format-5.0 -i --style=file $files_to_fmt
popd
# Format the project's source code under boards/Passport
fmt: fmt-py fmt-c
# Convert a raw pixel map to a PNG
convert-screenshot from_file to_file:
#!/usr/bin/python3
from PIL import Image, ImageOps
raw_bits = open('{{from_file}}', 'rb').read()
WIDTH = 230
HEIGHT = 303
SCAN_WIDTH = 240
# Convert
img = Image.frombuffer('1', (SCAN_WIDTH, HEIGHT), raw_bits)
# Crop to actual width (framebuffer is 240 vs 230 for actual display)
img = img.crop((0, 0, WIDTH, HEIGHT))
# Invert since raw image is actually white on black - have to convert to grayscale first since invert() doesn't work
# for 1-bit per pixel black/white images.
img = ImageOps.grayscale(img)
img = ImageOps.invert(img)
# Apply a color shift to make it look nicer
img = ImageOps.colorize(img, (0,0,0,0), '#E0E0E0')
img.save('{{to_file}}')
# Capture a screenshot from Passport via OCD
screenshot filename:
#!/usr/bin/env bash
ADDR_FILE=screenshot-addr.tmp
TMP_FILE=screenshot.tmp
just run-ocd-command-no-halt "dump_image $ADDR_FILE 0x38006920 4"
N=`head -c 4 $ADDR_FILE | od -An --endian=little -t u4`
FRAMEBUFFER_ADDR=`printf '%x\n' $N`
echo FRAMEBUFFER_ADDR=$FRAMEBUFFER_ADDR
just run-ocd-command-no-halt "dump_image screenshot.tmp 0x$FRAMEBUFFER_ADDR 9090"
just convert-screenshot $TMP_FILE {{filename}}
rm -f $TMP_FILE $ADDR_FILE