2018-06-25 16:09:52 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# script/build: Build the TREZOR firmware in a clean working tree.
|
|
|
|
#
|
|
|
|
|
2018-09-19 17:44:35 +00:00
|
|
|
# this needs to be there, otherwise python click installer vomits an error
|
|
|
|
export LC_ALL=C.UTF-8
|
|
|
|
export LANG=C.UTF-8
|
|
|
|
|
2018-06-25 16:09:52 +00:00
|
|
|
set -eu
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
cd "$(dirname "$0")/../.."
|
|
|
|
|
|
|
|
readonly MCU_ROOT=legacy
|
2018-06-25 16:09:52 +00:00
|
|
|
|
|
|
|
readonly ARTIFACT_EXTENSIONS=(bin elf)
|
|
|
|
readonly BUILD_DIR="$(readlink -f build)"
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
readonly BOOTLOADER_DIR="$BUILD_DIR/legacy/bootloader"
|
|
|
|
readonly BOOTLOADER_FILENAME="$MCU_ROOT/bootloader/bootloader.bin"
|
2018-06-25 16:09:52 +00:00
|
|
|
readonly BOOTLOADER_PATH="$BOOTLOADER_DIR/$BOOTLOADER_FILENAME"
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
readonly FIRMWARE_DIR="$BUILD_DIR/legacy/firmware"
|
|
|
|
readonly FIRMWARE_FILENAME="$MCU_ROOT/firmware/trezor.bin"
|
2018-06-25 16:09:52 +00:00
|
|
|
readonly FIRMWARE_PATH="$FIRMWARE_DIR/$FIRMWARE_FILENAME"
|
|
|
|
|
2018-10-11 12:58:22 +00:00
|
|
|
readonly EMULATOR_DIR="$FIRMWARE_DIR"
|
2019-04-18 12:52:24 +00:00
|
|
|
readonly EMULATOR_FILENAME="$MCU_ROOT/firmware/trezor-emulator.elf"
|
|
|
|
readonly EMULATOR_PATH="$EMULATOR_DIR/$MCU_ROOT/firmware/trezor.elf"
|
2018-10-11 12:58:22 +00:00
|
|
|
|
2018-06-25 16:09:52 +00:00
|
|
|
worktree_setup() {
|
|
|
|
local path="$1"
|
|
|
|
local commit="$2"
|
|
|
|
|
|
|
|
rm -rf "$path"
|
|
|
|
git clone -n --reference=. . "$path" --recurse-submodules
|
|
|
|
|
|
|
|
# Use `git rev-parse` so that we can use any reference from the working repository.
|
|
|
|
git -C "$path" checkout "$(git rev-parse "$commit")"
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
( cd "$path/$MCU_ROOT" && script/setup )
|
2018-06-25 16:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
worktree_build() {
|
|
|
|
local path="$1"
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
if [ ! -e "$path/$MCU_ROOT/Pipfile" ]; then
|
|
|
|
echo "Can't handle pre-monorepo tags properly. You will have to check out manually"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
pushd $path/$MCU_ROOT
|
2019-04-25 10:59:44 +00:00
|
|
|
pipenv install
|
2019-04-18 12:52:24 +00:00
|
|
|
pipenv run script/cibuild
|
|
|
|
popd
|
2018-06-25 16:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
worktree_copy() {
|
|
|
|
local path="$1"
|
|
|
|
local filename="$2"
|
|
|
|
local pattern="$3"
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
local describe="$(git -C "$path" describe --tags --match "legacy/$pattern")"
|
|
|
|
describe="${describe##legacy/}"
|
2018-06-25 16:09:52 +00:00
|
|
|
|
|
|
|
local src="$path/$filename"
|
|
|
|
|
|
|
|
local basename="$(basename "$filename")"
|
|
|
|
local dest="$BUILD_DIR/${basename%.*}-$describe.${basename##*.}"
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
if [ "$EMULATOR" = 1 ]; then
|
|
|
|
install -Dm0644 "${src%.*}.elf" "${dest%.*}.elf"
|
|
|
|
else
|
|
|
|
for extension in "${ARTIFACT_EXTENSIONS[@]}"; do
|
|
|
|
install -Dm0644 \
|
|
|
|
"${src%.*}.$extension" \
|
|
|
|
"${dest%.*}.$extension"
|
|
|
|
done
|
|
|
|
fi
|
2018-06-25 16:09:52 +00:00
|
|
|
|
|
|
|
printf "%s" "$dest"
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
local bootloader_commit="$1"
|
|
|
|
local firmware_commit="$2"
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
if [ "$bootloader_commit" != "HEAD" ]; then
|
|
|
|
bootloader_commit="legacy/$bootloader_commit"
|
|
|
|
fi
|
|
|
|
if [ "$firmware_commit" != "HEAD" ]; then
|
|
|
|
firmware_commit="legacy/$firmware_commit"
|
|
|
|
fi
|
|
|
|
|
2018-10-11 12:58:22 +00:00
|
|
|
worktree_setup "$FIRMWARE_DIR" "$firmware_commit"
|
2018-07-16 13:08:55 +00:00
|
|
|
|
2018-10-11 12:58:22 +00:00
|
|
|
if [ "$EMULATOR" != 1 ]; then
|
|
|
|
worktree_setup "$BOOTLOADER_DIR" "$bootloader_commit"
|
|
|
|
worktree_build "$BOOTLOADER_DIR"
|
2018-06-25 16:09:52 +00:00
|
|
|
|
2018-10-11 12:58:22 +00:00
|
|
|
cp "$BOOTLOADER_PATH" "$FIRMWARE_DIR/$BOOTLOADER_FILENAME"
|
|
|
|
fi
|
2018-06-25 16:09:52 +00:00
|
|
|
|
|
|
|
worktree_build "$FIRMWARE_DIR"
|
|
|
|
|
2018-10-11 12:58:22 +00:00
|
|
|
if [ "$EMULATOR" = 1 ]; then
|
|
|
|
cp "$EMULATOR_PATH" "$EMULATOR_DIR/$EMULATOR_FILENAME"
|
|
|
|
local firmware_path="$(worktree_copy \
|
|
|
|
"$EMULATOR_DIR" \
|
|
|
|
"$EMULATOR_FILENAME" \
|
|
|
|
"v*")"
|
|
|
|
chmod +x "$firmware_path"
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
local firmware_path="$(worktree_copy \
|
|
|
|
"$FIRMWARE_DIR" \
|
|
|
|
"$FIRMWARE_FILENAME" \
|
|
|
|
"v*")"
|
|
|
|
|
|
|
|
local bootloader_path="$(worktree_copy \
|
|
|
|
"$BOOTLOADER_DIR" \
|
|
|
|
"$BOOTLOADER_FILENAME" \
|
|
|
|
"bl*")"
|
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
printf "\n\n"; $PYTHON $MCU_ROOT/script/fingerprint \
|
2018-10-11 12:58:22 +00:00
|
|
|
"$bootloader_path" \
|
|
|
|
--max-size 32768 \
|
|
|
|
--double
|
|
|
|
fi
|
2018-06-25 16:09:52 +00:00
|
|
|
|
2019-04-18 12:52:24 +00:00
|
|
|
printf "\n\n"; $PYTHON $MCU_ROOT/script/fingerprint \
|
2018-06-27 16:12:18 +00:00
|
|
|
"$firmware_path" \
|
|
|
|
--offset 256 \
|
2018-12-19 17:04:23 +00:00
|
|
|
--max-size 983296 # 256 + 64*1024 + 3*128*1024 + 4*128*1024
|
2018-06-25 16:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|