/
Justfile
429 lines (346 loc) · 14.9 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# SPDX-FileCopyrightText: © 2021 Foundation Devices, Inc. <hello@foundationdevices.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
cosign_filepath := "default"
cosign_keypath := join(env_var('HOME'), "bin/keys/user.pem")
builddir := join(justfile_directory(), "build-Passport")
contribdir := join(justfile_directory(), "boards/Passport/contrib")
openocd_cfg := join(contribdir, "openocd.cfg")
compile_commands := join(builddir, "compile_commands.json")
# Number of jobs to use when building
jobs := if os() == "macos" {
`sysctl -n hw.logicalcpu`
} else if os() == "linux" {
`nproc`
} else {
"1"
}
# Install dependencies.
deps:
pip3 install bear
# Initialize development environment
init: deps init-openocd
init-openocd:
#!/usr/bin/env python3
if sys.platform == 'linux':
if not os.path.exists('/etc/udev/rules.d/60-openocd.rules'):
print('Trying to install `60-openocd.rules` to `/etc/udev/rules.d/`')
# Probably on /usr/local/share as the openocd package is not installed (because the rules don't exist).
subprocess.call(['pkexec',
'cp',
'/usr/local/share/openocd/contrib/60-openocd.rules',
'/etc/udev/rules.d/60-openocd.rules'])
print('Success')
else:
print('OpenOCD rules already installed')
user = os.getenv('USER')
for group in GROUPS:
print('Adding user `{}` to group: {}'.format(user, group))
subprocess.call(['pkexec', 'usermod', '-aG', group, user])
print('Success')
# Lint only the python code of the project
lint-py:
pycodestyle --exclude trezor-firmware,unused_modules,graphics.py,translations --statistics boards/Passport
# Lint only the C code of the project
lint-c:
@echo "TODO"
# 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
# Generate compile_commands.json database.
compile-commands screen="mono" ext="":
@mkdir -p build-Passport
@bear --output {{compile_commands}} --append -- just build {{screen}} {{ext}}
#
# Firmware Commands
#
# Build the firmware
build screen="mono" dev="" +ext='':
@echo {{ if screen == "mono" { "Building with SCREEN_MODE=MONO" } else if screen == "color" { "Building with SCREEN_MODE=COLOR" } else { error("Unsupported screen type. Use 'mono' or 'color'") } }}
make -j{{jobs}} \
{{ if dev == "dev" { "DEV_BUILD=1" } else { "" } }} \
BOARD=Passport \
SCREEN_MODE={{ uppercase(screen) }} \
FROZEN_MANIFEST={{ if dev == "dev" { 'boards/Passport/manifest_dev.py' } else { 'boards/Passport/manifest.py' } }} \
{{ext}}
# Sign current firmware build with the user.pem key and set specified version
sign version="1.0.0" screen="mono" dev="" ext="": (build screen dev ext)
#!/usr/bin/env bash
set -e
SCREEN_EXT={{screen}}
SCREEN_EXT=`echo ${SCREEN_EXT^^}`
COSIGN_FILEPATH={{cosign_filepath}}
if [ $COSIGN_FILEPATH == "default" ]
then
COSIGN_FILEPATH="build-Passport/firmware-$SCREEN_EXT.bin"
fi
echo -e "\nAdding user signature...\n"
cosign -t {{lowercase(screen)}} -f $COSIGN_FILEPATH -k {{cosign_keypath}} -v {{version}}
# Remove .bin and append the signed filename string
if [ {{screen}} == "color" ]
then
SIGNED_FILE="build-Passport/v{{version}}-beta-passport.bin"
else
SIGNED_FILE="build-Passport/v{{version}}-beta-founders-passport.bin"
fi
# Show the signature data
cosign -t {{lowercase(screen)}} -f $SIGNED_FILE -p
echo -e "\nSigning Complete!"
# Build and sign all beta versions
sign-beta version="1.0.0" beta-num="1":
#!/usr/bin/env bash
rm -rf "{{version}}-beta-{{beta-num}}"
mkdir -p "{{version}}-beta-{{beta-num}}"
screens=("color" "mono")
screen_names=("" "-founders")
paths=("beta-passport" "beta-founders-passport")
builds=("" "dev")
build_names=("" "-dev")
i=0
for screen in "${screens[@]}"; do
j=0
for build in "${builds[@]}"; do
just clean
just sign {{version}} $screen $build
mv "build-Passport/v{{version}}-${paths[$i]}.bin" "build-Passport/v{{version}}-beta-{{beta-num}}${screen_names[$i]}${build_names[$j]}-passport.bin"
just hash "build-Passport/v{{version}}-beta-{{beta-num}}${screen_names[$i]}${build_names[$j]}-passport.bin" "${screen}" "{{version}}-beta-{{beta-num}}/v{{version}}-beta-{{beta-num}}${screen_names[$i]}${build_names[$j]}-notes.md"
cp build-Passport/v{{version}}-beta-{{beta-num}}${screen_names[$i]}${build_names[$j]}* "{{version}}-beta-{{beta-num}}/"
j=$((j + 1))
done
i=$((i + 1))
done
# Build, sign and flash the firmware with the specified version
flash version="1.0.0" screen="mono" dev="" ext="": (sign version screen dev ext) && (reset)
#!/usr/bin/env bash
set -euo pipefail
# Get the address depending on the selected screen mode.
BL_FW_HDR_BASE=$(make bl_fw_hdr_base SCREEN_MODE={{uppercase(screen)}} V=0)
echo "Firmware address for '{{screen}}': $BL_FW_HDR_BASE"
if [ {{screen}} == "color" ]
then
SIGNED_FILENAME="v{{version}}-beta-passport.bin"
else
SIGNED_FILENAME="v{{version}}-beta-founders-passport.bin"
fi
SIZE=$(ls -l build-Passport/$SIGNED_FILENAME | cut -d " " -f5)
# 1792k firmware size limit
LIMIT=1835008
echo "limit: $LIMIT"
echo "size: $SIZE"
if [ $SIZE -gt $LIMIT ]
then
echo "ERROR: Firmware too large"
exit 1
fi
just run-ocd-command "flash write_image erase build-Passport/$SIGNED_FILENAME $BL_FW_HDR_BASE"
# Install a recent Foundation-signed build
flash-foundation screen="mono" filename="v2.0.3-passport.bin": && (reset)
#!/usr/bin/env bash
set -euo pipefail
# Get the address depending on the selected screen mode.
BL_FW_HDR_BASE=$(make bl_fw_hdr_base SCREEN_MODE={{uppercase(screen)}} V=0)
echo "Firmware address for '{{screen}}': $BL_FW_HDR_BASE"
just run-ocd-command "flash write_image erase ../../releases/{{filename}} $BL_FW_HDR_BASE"
# Clean the firmware build
clean:
make BOARD=Passport clean
#
# Misc. Commands
#
# Launch OCD, run a command and then exit
@run-ocd-command command:
openocd -f {{openocd_cfg}} -c "init; reset halt; {{command}}" -c "exit"
# Launch OCD command, without halting.
@run-ocd-command-no-halt command:
openocd -f {{openocd_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
# Build all images.
images: (convert-images "boards/Passport/images" "true") (convert-images "boards/Passport/bootloader/images" "false")
# LVGL Image Conversion Tool
convert-images image_path process_mono='false':
#!/usr/bin/python3
import os
from os import listdir
image_path = '{{image_path}}'
process_mono = '{{process_mono}}'
conv_path = '../../../../../../tools/lv_img_conv/lv_img_conv.js'
f = open(image_path + '/images.h', 'w')
# NOTE: We split the string up below so that REUSE doesn't think this is a direct copyright string
f.write('// SP' + 'DX-FileCopyrightText: © 2022 Foundation Devices, Inc. <hello@foundationdevices.com>\n')
f.write('// SP' + 'DX-License-Identifier: GPL-3.0-or-later\n')
f.write('//\n')
f.write('// Autogenerated by ports/stm32/Justfile images command -- DO NOT EDIT MANUALLY!\n')
f.write('//\n\n')
def handle_images(folder):
from shutil import copyfile
full_folder = image_path + '/{}'.format(folder)
image_filenames = [f for f in listdir(full_folder) if f.endswith('.png')]
image_filenames.sort()
for filename in image_filenames:
base = os.path.splitext(filename)[0] # Trim extension
parts = base.split('__')
if len(parts) != 2:
# File name not formatted to our requirements
print('WARNING: Unexpected PNG filename: {}'.format(filename))
continue
name = parts[0]
format = parts[1]
f.write('extern const lv_img_dsc_t {};\n'.format(name))
# Only convert the file if the png is newer than the c file
source_time = os.path.getmtime(os.path.join(full_folder, filename))
dest_filename = '{}.c'.format(name)
dest_path = os.path.join(full_folder, dest_filename)
dest_time = None
if os.path.exists(dest_path):
dest_time = os.path.getmtime(dest_path)
if dest_time is None or source_time > dest_time:
print('Converting {}.png to C'.format(base, name))
# Converter doesn't have a way to specify python name, so we make a temporary copy instead
tmp_filename = '{}.png'.format(name)
copyfile(full_folder + '/' + filename, full_folder + '/' + tmp_filename)
# Convert the file -- parts[1] has the format string needed by the conversion
cmd = 'cd {}/{} && lv_img_conv {} -f -c {}'.format(image_path, folder, tmp_filename, format)
print(cmd)
stream = os.popen(cmd)
output = stream.read()
print(output)
# Remove the temporary PNG file
os.remove(full_folder + '/' + tmp_filename)
# Concatenate the copyright info
cmd = 'cd {}/{} && cat ../copyright_template.c {}.c >> {}.c.tmp && mv {}.c.tmp {}.c'.format(image_path, folder, name, name, name, name)
stream = os.popen(cmd)
stream.read()
else:
pass
# print('Skipping conversion of {}.png as {}.c is newer'.format(base, name))
f.write('#ifdef SCREEN_MODE_COLOR\n')
handle_images('color')
f.write('#endif\n\n')
if process_mono == "true":
f.write('#ifdef SCREEN_MODE_MONO\n')
handle_images('mono')
f.write('#endif\n\n')
f.close()
# Reset the Passport
reset:
just run-ocd-command "reset"
# Get the user and group ID for use below
user := `id -u`
group := `id -g`
# Read the "ROM Secrets" from Passport and save them to a file
save-secrets screen="mono" filename="boards/Passport/bootloader/secrets":
#!/usr/bin/env bash
set -euo pipefail
# Get the secrets address depending on the selected screen mode.
BL_NVROM_BASE=$(make bl_nvrom_base SCREEN_MODE={{uppercase(screen)}} V=0)
echo "Bootloader secrets address for '{{screen}}': $BL_NVROM_BASE"
just run-ocd-command "dump_image {{filename}} $BL_NVROM_BASE 256"
secrets screen="mono":
#!/usr/bin/env bash
set -euo pipefail
# Get the secrets address depending on the selected screen mode.
BL_NVROM_BASE=$(make bl_nvrom_base SCREEN_MODE={{uppercase(screen)}} V=0)
echo "Bootloader secrets address for '{{screen}}': $BL_NVROM_BASE"
# The last bit below redirects stderr to stdout, which the backticks capture into the variable `secrets`
secrets=`just run-ocd-command "mdb $BL_NVROM_BASE 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 screen="mono" output_file="":
#!/usr/bin/env bash
if [ "{{screen}}" != "mono" ] && [ "{{screen}}" != "color" ]; then
echo "Screen must be either 'mono' or 'color', the default value is 'mono'."
exit 1
fi
set -e
filename=`basename {{filepath}}`
directory=`dirname {{filepath}}`
release_name=${filename::-13}
# SHA256
sha=`shasum -b -a 256 {{filepath}} | sed -rn 's/^(.*) .*$/\1/p'`
echo "$sha" > ${directory}/${release_name}-sha256
# MD5
md5=`md5sum {{filepath}} | sed -rn 's/^(.*) .*$/\1/p' | xargs`
echo "$md5" > ${directory}/${release_name}-md5
# Build Hash
build_hash=$(cosign -t {{screen}} -p -f {{filepath}} | sed -rn 's/^FW Build Hash: (.*)$/\1/p')
echo "$build_hash" > ${directory}/${release_name}-build-hash
output="## RELEASE HASHES\n\n"
if [ -n "{{output_file}}" ]; then
output+="### $filename:\n\n"
fi
output+="SHA256: \`$sha\`\nMD5: \`$md5\`\n\nYou can check these hashes with the following commands on most operating systems:\n\n"
output+="SHA256: \`shasum -b -a 256 $filename\`\nMD5: \`md5 $filename\` or \`mdsum $filename\` or \`md5sum $filename\`\n\n"
output+="### DEVELOPERS ONLY\n\nBuild Hash: \`$build_hash\`"
if [ -z "{{output_file}}" ]; then
echo -e "$output"
echo -e "$output" > ${directory}/${release_name}-hashes.md
else
echo -e "$output" > "{{output_file}}"
fi
# 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-screenshot from_file to_file:
#!/usr/bin/python3
from PIL import Image, ImageOps
raw_bits = open('{{from_file}}', 'rb').read()
WIDTH = 240
HEIGHT = 320
# Convert RGB565 to RGB (8 bits per color)
rgb = bytearray(WIDTH*HEIGHT*3)
for y in range(0, HEIGHT):
for x in range(0, WIDTH):
offset = (y*WIDTH*2) + (x*2)
b1 = raw_bits[offset]
b2 = raw_bits[offset+1]
c = b1 << 8 | b2
r = (c & 0xF800) >> 11
g = (c & 0x07E0) >> 5
b = (c & 0x001F)
# Set into RGB buffer
dest_offset = (y*WIDTH*3) + (x*3)
rgb[dest_offset] = r << 3
rgb[dest_offset + 1] = g << 2
rgb[dest_offset + 2] = b << 3
img = Image.frombytes('RGB', (WIDTH, HEIGHT), bytes(rgb))
img.save('{{to_file}}')
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 $TMP_FILE 0x$FRAMEBUFFER_ADDR 153600"
just convert-screenshot $TMP_FILE {{filename}}
rm -f $TMP_FILE $ADDR_FILE
# Copy out of Ubuntu VM to host OS (replace existing file with the same name if it exists
cp source:
cp -f {{source}} /media/psf/Home/Downloads/
cpr source dest:
cp -f {{source}} /media/psf/Home/Downloads/{{dest}}