Errors while compiling older versions (<1.8.0)

Hello everyone,

I am trying to reproduce results of the following scientific paper : Side-Channel assessment of Open Source Hardware Wallets by Manuel San Pedro, Victor Servant, and Charles Guillemet, which describes side channel attacks applied on the Trezor One crypto-wallet. In order to do so I need to compile older versions of the firmware, and implement them in a Trezor One (or on a target using the same MCU). In order to do so, I followed the guide found on the trezor-mcu github repository and used pipenv command lines. However while compiling version 1.8.0 (or older), I get some errors :

  • Bootloader size keeps going up even after I re-run the setup script.
  • The dynamic linker fails with following output :
../vendor/QR-Code-generator/c/qrcodegen.o: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
make: *** [../Makefile.include:191: trezor.elf] Error 1

Has anyone encountered this problem before ? If so I really would appreciate any help.
Thank you.

probably indicates you didn’t erase previous build results.

in the trezor-mcu repo, you should generally use ./script/setup to clean the build environment, and ./script/cibuild to perform the build.

Thank you @matejcik for the prompt reply.

I did actually, in fact before I run the script/cibuild to build the binary, I always run the setup script once again. And still the size keeps going up. However I should probably mention that I modified some dependencies because I had some errors due to the use of older versions, and some other small errors, in particular :

  • I added the following line to the Pipfile : libusb1 = “*”

  • There was a typing error (i presume) in the cibuild script file :

    • Line 13 was : make -C vendor/linopencm3 lib/stm32/f2 so I deleted the blank space and replaced with a ‘/’
  • after running the setup script, running the cibuild script return the following error :

    • In file included from ../../../include/libopencm3/cm3/nvic.h:133, from ../../../include/libopencm3/cm3/vector.h:41, from ../../cm3/vector.c:22: ../../../include/libopencm3/dispatch/nvic.h:10:17: fatal error: libopencm3/stm32/f2/nvic.h: No such file or directory 10 | # include <libopencm3/stm32/f2/nvic.h> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. make: *** [../../Makefile.include:41: vector.o] Error 1 make: Leaving directory '/home/mouad/Documents/Trezor/mcu/trezor-mcu/vendor/libopencm3/lib/stm32/f2'

    In order to remediate this, I go to the vendor/linopencm3 folder and execute the following command line : pipenv run make before re-running the script/cibuild (I suspect this is why the bootloader size exceeds the maximum)

Another thing to note is that this only occurs when i set the environment variable EMULATOR to 0. I don’t know if setting the EMULATOR to 1 will generate a binary that can be implemented in the hardware.
I am sorry I wasn’t clear enough on the post itself.

I’m ~70 % sure there are no typos in the scripts. You replaced a command that does one thing with a command that does a different thing.

What problem were you trying to solve by this?

I was trying to solve the following error which occurs when trying to run the cibuild script :

BUILD lib/stm32/f2
/bin/sh: 2: Syntax error: Unterminated quoted string
make: *** [Makefile:69: lib/stm32/f2] Error 2
make: Leaving directory ‘/home/mouad/Documents/Trezor/mcu/trezor-mcu/vendor/libopencm3’

After you told me you are pretty sure that there are no typos in the script files, I checked the makefile on vendor/libopencm3 folder and found out that it was a backslash in line 28 that caused this error :

SRCLIBDIR:= $(subst $(space),\$(space),$(realpath lib))

I replaced it with a ‘/’ and it solved the problem, thank you (I will never doubt your script files again :sweat_smile:)

However, this doesn’t solve the problem of bootloader size exceeding the maximum size allowed, I still get this error when trying to perform the build :

Traceback (most recent call last):
File “/home/mouad/Documents/Trezor/mcu/trezor-mcu/bootloader/./”, line 11, in
raise Exception(‘bootloader has to be smaller than %d bytes (current size is %d)’ % (MAXSIZE, fs))
Exception: bootloader has to be smaller than 32736 bytes (current size is 34592)
make: *** [Makefile:21: align] Error 1
make: Leaving directory ‘/home/mouad/Documents/Trezor/mcu/trezor-mcu/bootloader’

you’re running into the strangest errors. but i confirm that the subst thing is broken for me too. funny, I am absolutely certain that this used to work as written – and it even makes sense, it’s just replacing with \ for shell, so that, e.g., if the full path had spaces in it, they would be properly escaped. Maybe some sort of change in make??

…anyway, your fix is as good as any, doing SRCLIBDIR:= $(realpath lib) would presumably be the cleanest

as for the bootloader error, that might be a problem because IIRC it’s something like, gcc 8.x produced significantly smaller code. so you can try to get yourself an old toolchain. or hack out the part that builds the bootloader – there is a prebuilt one in firmware/bootloader.dat

I couldn’t find the prebuilt bootloader.dat in the trezor-mcu repo. But I am going to try to downgrade my gcc version in order to redure size of output files. Thank you for the tip.

I have another unrelated question if it’s no trouble: in fact I want to change the target MCU to an STM32F4, is it possible to do so ? If it’s the case, what changes should I make (other than the necessary flags) ?
I have seen 4 linker script files (memory_app_xxx.ld) I suppose I’ll have to change those too.