2019-12-06 08:56:38 +00:00
# Trezor One Bootloader and Firmware
2019-12-10 13:32:22 +00:00
## Building with Docker
2019-12-06 08:56:38 +00:00
2019-12-10 13:32:22 +00:00
Ensure that you have Docker installed. You can follow [Docker's installation instructions ](https://docs.docker.com/engine/installation/ ).
2019-12-06 08:56:38 +00:00
2019-12-10 13:32:22 +00:00
Clone this repository, then use `build-docker.sh` to build all images:
```sh
2019-12-15 08:49:15 +00:00
git clone https://github.com/trezor/trezor-firmware.git
2019-12-10 13:32:22 +00:00
cd trezor-firmware
./build-docker.sh
```
2019-12-06 08:56:38 +00:00
2021-03-27 08:23:45 +00:00
When the build is done, you will find the current firmware in `build/legacy/firmware/firmware.bin` .
2019-12-10 13:32:22 +00:00
### Running with sudo
It is possible to run `build-docker.sh` if either your Docker is configured in rootless mode,
or if your user is a member of the `docker` group; see [Docker documentation ](https://docs.docker.com/install/linux/linux-postinstall/ )
for details.
If you don't satisfy the above conditions, and run `sudo ./build-docker.sh` , you might receive a `Permission denied`
error. To work around it, make sure that the directory hierarchy in `build/` directory
is world-writable - e.g., by running `chmod -R a+w build/` .
## Building older versions
For firmware versions **1.8.1** and newer, you can checkout the respective tag locally.
To build firmware 1.8.2, for example, run `git checkout legacy/v1.8.2` and then use
the instructions below.
Note that the unified Docker build was added after version 1.8.3, so it is not available
for older versions.
For firmwares older than 1.8.1, please clone the archived [trezor-mcu ](https://github.com/trezor/trezor-mcu ) repository and follow the instructions in its README.
## Local development build
2020-09-25 14:21:23 +00:00
Make sure you have Python 3.6 or later and [Poetry ](https://python-poetry.org/ )
2019-12-10 13:32:22 +00:00
installed.
If you want to build device firmware, also make sure that you have the [GNU ARM Embedded toolchain ](https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads ) installed.
See [Dockerfile ](../../ci/Dockerfile#L72-L76 ) for up-to-date version of the toolchain.
The build process is configured via environment variables:
* `EMULATOR=1` specifies that an emulator should be built, instead of the device firmware.
* `DEBUG_LINK=1` specifies that DebugLink should be available in the built image.
2021-12-12 14:45:10 +00:00
* `PRODUCTION=0` disables memory protection. This is necessary for installing unofficial firmware.
2019-12-10 13:32:22 +00:00
* `DEBUG_LOG=1` enables debug messages to be printed on device screen.
* `BITCOIN_ONLY=1` specifies Bitcoin-only version of the firmware.
To run the build process, execute the following commands:
2019-12-06 08:56:38 +00:00
```sh
2019-12-10 13:32:22 +00:00
# enter the legacy subdirectory
cd legacy
2020-09-25 14:21:23 +00:00
# set up poetry
poetry install
2019-12-10 13:32:22 +00:00
# set up environment variables. For example, to build emulator with debuglink:
export EMULATOR=1 DEBUG_LINK=1
# clear build artifacts
2020-09-25 14:21:23 +00:00
poetry run ./script/setup
2019-12-10 13:32:22 +00:00
# run build process
2020-09-25 14:21:23 +00:00
poetry run ./script/cibuild
2019-12-06 08:56:38 +00:00
```
2019-12-10 13:32:22 +00:00
A built device firmware will be located in `legacy/firmware/trezor.bin` . A built emulator will be
located in `legacy/firmware/trezor.elf` .
2019-12-06 08:56:38 +00:00
2019-12-10 13:32:22 +00:00
### Common errors
* **"Exception: bootloader has to be smaller than 32736 bytes"**: if you didn't modify the bootloader
source code, simply make sure you always run `./script/setup` before runnning `./script/cibuild`
* **"error adding symbols: File in wrong format"**: This happens when building emulator after building
the firmware, or vice-versa. Execute the following command to fix the problem:
2019-12-06 08:56:38 +00:00
```sh
2019-12-10 13:32:22 +00:00
find -L vendor -name "*.o" -delete
2019-12-06 08:56:38 +00:00
```
2019-12-10 13:32:22 +00:00
You can launch the emulator using `./firmware/trezor.elf` . To use `trezorctl` with the emulator, use
`trezorctl -p udp` (for example, `trezorctl -p udp get_features` ).
2019-12-06 08:56:38 +00:00
You can use `TREZOR_OLED_SCALE` environment variable to make emulator screen bigger.
## How to get fingerprint of firmware signed and distributed by SatoshiLabs?
2021-04-16 14:18:48 +00:00
1. Pick version of firmware binary listed on https://data.trezor.io/firmware/1/releases.json
2. Download it: `wget -O trezor.signed.bin https://data.trezor.io/firmware/1/trezor-1.9.4.bin`
2019-12-10 13:32:22 +00:00
3. Use `trezorctl` dry-run mode to get the firmware fingerprint:
```sh
trezorctl firmware-update -n -f trezor.signed.bin
```
2019-12-06 08:56:38 +00:00
2019-12-10 13:32:22 +00:00
Step 3 should produce the same fingerprint like your local build (for the same version tag).
2019-12-06 08:56:38 +00:00
## How to install custom built firmware?
**WARNING: This will erase the recovery seed stored on the device! You should never do this on Trezor that contains coins!**
2021-12-12 14:45:10 +00:00
Build with `PRODUCTION=0` or you will get a hard fault on your device.
2020-02-18 14:28:44 +00:00
2019-12-10 13:32:22 +00:00
Switch your device to bootloader mode, then execute:
```sh
2021-03-27 08:23:45 +00:00
trezorctl firmware-update -f build/legacy/firmware/firmware.bin
2019-12-10 13:32:22 +00:00
```
2020-08-21 12:58:13 +00:00
2021-12-12 14:45:10 +00:00
## Combining bootloader and firmware with various `PRODUCTION` settings, signed/unsigned
2020-08-21 12:58:13 +00:00
Not all combinations of bootloader and firmware will work. This depends on
2021-12-12 14:45:10 +00:00
3 variables: PRODUCTION of bootloader, PRODUCTION of firmware, whether firmware is signed
2020-08-21 12:58:13 +00:00
This table shows the result for bootloader 1.8.0+ and 1.9.1+:
2021-12-12 14:45:10 +00:00
| Bootloader PRODUCTION | Firmware PRODUCTION | Is firmware officially signed? | Result |
2020-08-21 12:58:13 +00:00
| ------------------------- | ----------------------- | ------------------------------ | ------------------------------------------------------------------------------------------ |
| 1 | 1 | yes | works, official configuration |
| 1 | 1 | no | hardfault in header.S when setting VTOR and stack |
2021-05-10 15:22:05 +00:00
| 0 | 1 | no | works, but don't forget to comment out `check_and_replace_bootloader` , otherwise it'll get overwritten |
2020-08-21 12:58:13 +00:00
| 0 | 0 | no | hard fault because header.S doesn't set VTOR and stack right |
| 1 | 0 | no | works |
2021-12-12 14:45:10 +00:00
The other three possibilities with signed firmware and `PRODUCTION!=0` for bootloader/firmware don't exist.
2020-08-21 12:58:13 +00:00