forked from BitBoxSwiss/bitbox02-firmware
/
CMakeLists.txt
686 lines (622 loc) · 29.8 KB
/
CMakeLists.txt
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# Copyright (c) 2015-2016 Lucas Betschart, Douglas J. Bakkum
# Copyright 2019 Shift Cryptosecurity AG
# Copyright 2020 Shift Crypto AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set(DBB-FIRMWARE-SOURCES
${CMAKE_SOURCE_DIR}/src/firmware_main_loop.c
${CMAKE_SOURCE_DIR}/src/commander/commander.c
${CMAKE_SOURCE_DIR}/src/commander/commander_btc.c
${CMAKE_SOURCE_DIR}/src/commander/commander_states.c
${CMAKE_SOURCE_DIR}/src/commander/protobuf.c
${CMAKE_SOURCE_DIR}/src/keystore.c
${CMAKE_SOURCE_DIR}/src/random.c
${CMAKE_SOURCE_DIR}/src/hardfault.c
${CMAKE_SOURCE_DIR}/src/util.c
${CMAKE_SOURCE_DIR}/src/sd.c
${CMAKE_SOURCE_DIR}/src/hww.c
${CMAKE_SOURCE_DIR}/src/memory/bitbox02_smarteeprom.c
${CMAKE_SOURCE_DIR}/src/memory/memory.c
${CMAKE_SOURCE_DIR}/src/memory/mpu.c
${CMAKE_SOURCE_DIR}/src/memory/nvmctrl.c
${CMAKE_SOURCE_DIR}/src/memory/smarteeprom.c
${CMAKE_SOURCE_DIR}/src/salt.c
${CMAKE_SOURCE_DIR}/src/i2c_ecc.c
${CMAKE_SOURCE_DIR}/src/touch/gestures.c
${CMAKE_SOURCE_DIR}/src/backup/backup.c
${CMAKE_SOURCE_DIR}/src/backup/restore.c
${CMAKE_SOURCE_DIR}/src/backup/backup_common.c
${CMAKE_SOURCE_DIR}/src/reset.c
${CMAKE_SOURCE_DIR}/src/cipher/cipher.c
${CMAKE_SOURCE_DIR}/src/workflow/blocking.c
${CMAKE_SOURCE_DIR}/src/workflow/cancel.c
${CMAKE_SOURCE_DIR}/src/workflow/confirm.c
${CMAKE_SOURCE_DIR}/src/workflow/confirm_time.c
${CMAKE_SOURCE_DIR}/src/workflow/idle_workflow.c
${CMAKE_SOURCE_DIR}/src/workflow/password.c
${CMAKE_SOURCE_DIR}/src/workflow/reboot.c
${CMAKE_SOURCE_DIR}/src/workflow/restore.c
${CMAKE_SOURCE_DIR}/src/workflow/restore_from_mnemonic.c
${CMAKE_SOURCE_DIR}/src/workflow/orientation_screen.c
${CMAKE_SOURCE_DIR}/src/workflow/status.c
${CMAKE_SOURCE_DIR}/src/workflow/trinary_input.c
${CMAKE_SOURCE_DIR}/src/workflow/unlock_bip39.c
${CMAKE_SOURCE_DIR}/src/workflow/verify_pub.c
${CMAKE_SOURCE_DIR}/src/workflow/verify_recipient.c
${CMAKE_SOURCE_DIR}/src/workflow/verify_total.c
${CMAKE_SOURCE_DIR}/src/workflow/workflow.c
${CMAKE_SOURCE_DIR}/src/apps/common/bip32.c
${CMAKE_SOURCE_DIR}/src/apps/btc/btc.c
${CMAKE_SOURCE_DIR}/src/apps/btc/btc_common.c
${CMAKE_SOURCE_DIR}/src/apps/btc/btc_params.c
${CMAKE_SOURCE_DIR}/src/apps/btc/btc_sign.c
${CMAKE_SOURCE_DIR}/src/apps/btc/btc_sign_msg.c
${CMAKE_SOURCE_DIR}/src/apps/btc/btc_sign_validate.c
${CMAKE_SOURCE_DIR}/src/apps/btc/btc_bip143.c
${CMAKE_SOURCE_DIR}/src/apps/btc/confirm_locktime_rbf.c
${CMAKE_SOURCE_DIR}/src/apps/btc/confirm_multisig.c
${CMAKE_SOURCE_DIR}/src/queue.c
${CMAKE_SOURCE_DIR}/src/usb/usb_processing.c
)
set(DBB-FIRMWARE-SOURCES ${DBB-FIRMWARE-SOURCES} PARENT_SCOPE)
set(DBB-FIRMWARE-USB-SOURCES
${CMAKE_SOURCE_DIR}/src/usb/usb.c
${CMAKE_SOURCE_DIR}/src/usb/usb_frame.c
${CMAKE_SOURCE_DIR}/src/usb/usb_packet.c
${CMAKE_SOURCE_DIR}/src/u2f/u2f_packet.c
)
set(DBB-FIRMWARE-USB-SOURCES ${DBB-FIRMWARE-USB-SOURCES} PARENT_SCOPE)
set(DBB-FIRMWARE-USART-SOURCES
${CMAKE_SOURCE_DIR}/src/usart/usart_frame.c
)
set(DBB-FIRMWARE-USART-SOURCES ${DBB-FIRMWARE-USART-SOURCES} PARENT_SCOPE)
set(DBB-FIRMWARE-UI-SOURCES
${CMAKE_SOURCE_DIR}/src/screen.c
${CMAKE_SOURCE_DIR}/src/ui/graphics/graphics.c
${CMAKE_SOURCE_DIR}/src/ui/graphics/lock_animation.c
${CMAKE_SOURCE_DIR}/src/ui/ugui/ugui.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_9X9.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_11X10.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_11X12.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_13X14.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_15X16.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_17X18.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/monogram_5X9.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/password_9X9.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/password_11X12.c
${CMAKE_SOURCE_DIR}/src/ui/screen_saver.c
${CMAKE_SOURCE_DIR}/src/ui/screen_stack.c
${CMAKE_SOURCE_DIR}/src/ui/screen_process.c
${CMAKE_SOURCE_DIR}/src/ui/event_handler.c
${CMAKE_SOURCE_DIR}/src/ui/ui_util.c
${CMAKE_SOURCE_DIR}/src/ui/components/trinary_choice.c
${CMAKE_SOURCE_DIR}/src/ui/components/trinary_input_char.c
${CMAKE_SOURCE_DIR}/src/ui/components/trinary_input_string.c
${CMAKE_SOURCE_DIR}/src/ui/components/waiting.c
${CMAKE_SOURCE_DIR}/src/ui/components/screensaver.c
${CMAKE_SOURCE_DIR}/src/ui/components/entry_screen.c
${CMAKE_SOURCE_DIR}/src/ui/components/knight_rider.c
${CMAKE_SOURCE_DIR}/src/ui/components/right_arrow.c
${CMAKE_SOURCE_DIR}/src/ui/components/left_arrow.c
${CMAKE_SOURCE_DIR}/src/ui/components/icon_button.c
${CMAKE_SOURCE_DIR}/src/ui/components/confirm_gesture.c
${CMAKE_SOURCE_DIR}/src/ui/components/label.c
${CMAKE_SOURCE_DIR}/src/ui/components/confirm.c
${CMAKE_SOURCE_DIR}/src/ui/components/confirm_button.c
${CMAKE_SOURCE_DIR}/src/ui/components/keyboard_switch.c
${CMAKE_SOURCE_DIR}/src/ui/components/orientation_arrows.c
${CMAKE_SOURCE_DIR}/src/ui/components/info_centered.c
${CMAKE_SOURCE_DIR}/src/ui/components/menu.c
${CMAKE_SOURCE_DIR}/src/ui/components/status.c
${CMAKE_SOURCE_DIR}/src/ui/components/image.c
${CMAKE_SOURCE_DIR}/src/ui/components/button.c
${CMAKE_SOURCE_DIR}/src/ui/components/empty.c
${CMAKE_SOURCE_DIR}/src/ui/components/progress.c
${CMAKE_SOURCE_DIR}/src/ui/components/sdcard.c
${CMAKE_SOURCE_DIR}/src/ui/components/ui_images.c
${CMAKE_SOURCE_DIR}/src/ui/components/confirm_transaction.c
)
set(DBB-FIRMWARE-UI-SOURCES ${DBB-FIRMWARE-UI-SOURCES} PARENT_SCOPE)
set(DBB-BOOTLOADER-SOURCES
${CMAKE_SOURCE_DIR}/src/util.c
${CMAKE_SOURCE_DIR}/src/pukcc/curve_p256.c
${CMAKE_SOURCE_DIR}/src/pukcc/pukcc.c
${CMAKE_SOURCE_DIR}/src/bootloader/bootloader.c
${CMAKE_SOURCE_DIR}/src/bootloader/startup.c
${CMAKE_SOURCE_DIR}/src/bootloader/mpu_regions.c
${CMAKE_SOURCE_DIR}/src/memory/mpu.c
${CMAKE_SOURCE_DIR}/src/memory/nvmctrl.c
${CMAKE_SOURCE_DIR}/src/queue.c
${CMAKE_SOURCE_DIR}/src/usb/usb_processing.c
${CMAKE_SOURCE_DIR}/src/ui/ugui/ugui.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_9X9.c
${CMAKE_SOURCE_DIR}/src/ui/fonts/font_a_11X10.c
${CMAKE_SOURCE_DIR}/src/screen.c
${CMAKE_SOURCE_DIR}/src/hardfault.c
${CMAKE_SOURCE_DIR}/src/ui/components/ui_images.c
)
set(DBB-BOOTLOADER-SOURCES ${DBB-BOOTLOADER-SOURCES} PARENT_SCOPE)
set(DRIVER-SOURCES
${CMAKE_SOURCE_DIR}/src/platform/platform_init.c
${CMAKE_SOURCE_DIR}/src/platform/driver_init.c
${CMAKE_SOURCE_DIR}/src/qtouch/qtouch.c
${CMAKE_SOURCE_DIR}/src/ui/oled/oled.c
)
set(DRIVER-SOURCES ${DRIVER-SOURCES} PARENT_SCOPE)
set(PLATFORM-BITBOX02-SOURCES
${CMAKE_SOURCE_DIR}/src/sd_mmc/sd_mmc_start.c
${CMAKE_SOURCE_DIR}/src/usb/class/hid/hid.c
${CMAKE_SOURCE_DIR}/src/usb/class/hid/hww/hid_hww.c
${DBB-FIRMWARE-USB-SOURCES}
)
set(PLATFORM-BITBOX02-SOURCES ${PLATFORM-BITBOX02-SOURCES} PARENT_SCOPE)
set(PLATFORM-BITBOXBASE-SOURCES
${CMAKE_SOURCE_DIR}/src/platform/bitboxbase/leds.c
${CMAKE_SOURCE_DIR}/src/usart/usart.c
${CMAKE_SOURCE_DIR}/src/usart/usart_hww.c
${DBB-FIRMWARE-USART-SOURCES}
)
set(PLATFORM-BITBOXBASE-SOURCES ${PLATFORM-BITBOXBASE-SOURCES} PARENT_SCOPE)
set(BITBOXBASE-FIRMWARE-SOURCES
${CMAKE_SOURCE_DIR}/src/bitboxbase/bitboxbase_background.c
${CMAKE_SOURCE_DIR}/src/bitboxbase/bitboxbase_watchdog.c
${CMAKE_SOURCE_DIR}/src/bitboxbase/bitboxbase_screensaver.c
${CMAKE_SOURCE_DIR}/src/bitboxbase/bitboxbase_status.c
)
set(BITBOXBASE-FIRMWARE-SOURCES ${BITBOXBASE-FIRMWARE-SOURCES} PARENT_SCOPE)
set(CRYPTOAUTHLIB-SOURCES
${CMAKE_SOURCE_DIR}/src/securechip/securechip.c
)
set(CRYPTOAUTHLIB-SOURCES ${CRYPTOAUTHLIB-SOURCES} PARENT_SCOPE)
set(ETHEREUM-SOURCES
${CMAKE_SOURCE_DIR}/src/apps/eth/eth.c
${CMAKE_SOURCE_DIR}/src/apps/eth/eth_common.c
${CMAKE_SOURCE_DIR}/src/apps/eth/eth_params.c
${CMAKE_SOURCE_DIR}/src/apps/eth/eth_sighash.c
${CMAKE_SOURCE_DIR}/src/apps/eth/eth_sign.c
${CMAKE_SOURCE_DIR}/src/apps/eth/eth_sign_msg.c
${CMAKE_SOURCE_DIR}/src/apps/eth/eth_verify.c
${CMAKE_SOURCE_DIR}/src/commander/commander_eth.c
)
set(ETHEREUM-SOURCES ${ETHEREUM-SOURCES} PARENT_SCOPE)
set(FIRMWARE-DRIVER-SOURCES
${CMAKE_SOURCE_DIR}/src/usb/class/hid/u2f/hid_u2f.c
)
set(FIRMWARE-DRIVER-SOURCES ${FIRMWARE-DRIVER-SOURCES} PARENT_SCOPE)
set(FIRMWARE-U2F-SOURCES
${CMAKE_SOURCE_DIR}/src/u2f.c
${CMAKE_SOURCE_DIR}/src/u2f/u2f_app.c
)
set(FIRMWARE-U2F-SOURCES ${FIRMWARE-U2F-SOURCES} PARENT_SCOPE)
#-----------------------------------------------------------------------------
# Include directories
set(INCLUDES
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/src/platform
${CMAKE_SOURCE_DIR}/src/platform/bitboxbase
${CMAKE_SOURCE_DIR}/src/qtouch
${CMAKE_SOURCE_DIR}/src/usb/class
${CMAKE_SOURCE_DIR}/src/usb/class/hid
${CMAKE_SOURCE_DIR}/src/usb/class/hid/hww
${CMAKE_SOURCE_DIR}/src/usb/class/hid/u2f
)
set(INCLUDES ${INCLUDES} PARENT_SCOPE)
set(SYSTEMINCLUDES
${CMAKE_BINARY_DIR}/messages
)
set(SYSTEMINCLUDES ${SYSTEMINCLUDES} PARENT_SCOPE)
#-----------------------------------------------------------------------------
# Build embedded firmware
# TODO: Not needed when building bootlaoder..
# Since the nanopb is included in the source tree and not in the sysroot (/usr/local/arm-none-eabi) we have to disable searching in the sysroot.
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
find_package(Nanopb REQUIRED)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(FIRMWARE-SOURCES
${DBB-FIRMWARE-SOURCES}
${DBB-FIRMWARE-UI-SOURCES}
${FIRMWARE-DRIVER-SOURCES}
${DRIVER-SOURCES}
${DBB-FIRMWARE-PROTOBUF-SOURCES}
${CRYPTOAUTHLIB-SOURCES}
${CMAKE_SOURCE_DIR}/src/common_main.c
)
set(BOOTLOADER-SOURCES
${DBB-BOOTLOADER-SOURCES}
${DRIVER-SOURCES}
)
set_source_files_properties(
${DBB-FIRMWARE-PROTOBUF-SOURCES}
PROPERTIES GENERATED TRUE
)
# rust-bindgen to generate rust declarations from c-headers.
# The compiled binary to avoid recompiling bindgen on every clean build
find_program(BINDGEN bindgen)
# cbindgen is used to generate the c-headers from rust code.
find_program(CBINDGEN cbindgen)
# cargo is the rust build system and dependency manager
find_program(CARGO cargo)
if(CMAKE_BUILD_TYPE STREQUAL "DEBUG")
set(RUST_PROFILE "debug")
else()
set(RUST_PROFILE "release")
set(RUST_CARGO_FLAG "--release")
set(RUSTFLAGS --remap-path-prefix=${CMAKE_CURRENT_SOURCE_DIR}/rust=src)
endif()
if(CMAKE_CROSSCOMPILING)
set(RUST_TARGET_ARCH thumbv7em-none-eabi)
set(RUST_TARGET_ARCH_DIR ${RUST_TARGET_ARCH})
set(RUST_TARGET_ARCH_ARG --target ${RUST_TARGET_ARCH})
else()
set(RUST_TARGET_ARCH_DIR .)
endif()
set(RUST_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/rust/target)
set(LIBBITBOX02_RUST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/rust/bitbox02-rust-c)
set(LIBBITBOX02_RUST ${LIBBITBOX02_RUST} PARENT_SCOPE)
# Generate c-headers for the rust library
# We generate two headers so that one of them can avoid using types only available for the firmware.
# * bitbox02_rust.h should be includeable everywhere, even in the bootloader
# * commander_rust.h can only be included in targets that have access to protobuf types
add_custom_target(rust-cbindgen
COMMAND
${CBINDGEN} --quiet --config ${CMAKE_CURRENT_SOURCE_DIR}/rust/bitbox02-cbindgen.toml --output ${CMAKE_CURRENT_BINARY_DIR}/rust/rust.h ${LIBBITBOX02_RUST_SOURCE_DIR}
BYPRODUCTS
${CMAKE_CURRENT_BINARY_DIR}/rust/rust.h
)
if(CMAKE_CROSSCOMPILING)
get_property(SAMD51A_INCLUDES TARGET samd51a-ds PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(ASF4_MIN_INCLUDES TARGET asf4-drivers-min PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(ASF4_INCLUDES TARGET asf4-drivers PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(CMSIS_INCLUDES TARGET CMSIS PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
endif()
get_property(WALLY_INCLUDES TARGET wallycore PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(SECP256k1_INCLUDES TARGET secp256k1 PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
foreach(include ${INCLUDES} ${SYSTEMINCLUDES} ${SAMD51A_INCLUDES} ${ASF4_INCLUDES} ${ASF4_MIN_INCLUDES} ${CMSIS_INCLUDES} ${NANOPB_INCLUDE_DIRS} ${WALLY_INCLUDES} ${SECP256k1_INCLUDES})
list(APPEND RUST_INCLUDES -I${include})
endforeach()
if(CMAKE_CROSSCOMPILING)
set(RUST_BINDGEN_FLAGS -D__SAMD51J20A__ --target=thumbv7em-none-eabi -mcpu=cortex-m4 -mthumb -mfloat-abi=soft --sysroot=${CMAKE_SYSROOT})
else()
set(RUST_BINDGEN_FLAGS -DTESTING=1)
endif()
add_custom_target(rust-bindgen
# Generate rust bindings
COMMAND
${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/rust
COMMAND
${BINDGEN}
--output ${CMAKE_CURRENT_BINARY_DIR}/rust/bindings.rs
--use-core
--ctypes-prefix util::c_types
--rustified-enum backup_error_t
--whitelist-function backup_check
--whitelist-function backup_create
--whitelist-function memory_is_initialized
--whitelist-function memory_set_initialized
--whitelist-function memory_is_mnemonic_passphrase_enabled
--whitelist-function memory_get_attestation_pubkey_and_certificate
--whitelist-function memory_bootloader_hash
--whitelist-function memory_get_noise_static_private_key
--whitelist-function memory_check_noise_remote_static_pubkey
--whitelist-function memory_add_noise_remote_static_pubkey
--whitelist-function memory_get_device_name
--whitelist-function memory_set_device_name
--whitelist-function memory_set_mnemonic_passphrase_enabled
--whitelist-function memory_set_seed_birthdate
--whitelist-var MEMORY_DEVICE_NAME_MAX_LEN
--whitelist-function securechip_attestation_sign
--whitelist-function securechip_monotonic_increments_remaining
--whitelist-function keystore_is_locked
--whitelist-function keystore_unlock
--whitelist-function keystore_unlock_bip39
--whitelist-function keystore_create_and_store_seed
--whitelist-function keystore_get_bip39_mnemonic
--whitelist-function keystore_get_bip39_word
--whitelist-function lock_animation_start
--whitelist-function lock_animation_stop
--whitelist-function delay_us
--rustified-enum keystore_error_t
--whitelist-function util_format_datetime
--whitelist-function util_version_short
--whitelist-function delay_ms
--whitelist-function UG_PutString
--whitelist-function UG_FontSelect
--whitelist-function UG_ClearBuffer
--whitelist-function UG_SendBuffer
--whitelist-function screen_print_debug
--whitelist-function ui_screen_stack_push
--whitelist-function ui_screen_stack_pop
--whitelist-function ui_screen_stack_pop_all
--whitelist-function screen_process
--whitelist-function label_create
--whitelist-function confirm_create
--whitelist-function status_create
--whitelist-function sdcard_create
--whitelist-function menu_create
--whitelist-function bitboxbase_watchdog_reset
--whitelist-function bitboxbase_screensaver_reset
--whitelist-function leds_turn_small_led
--whitelist-function leds_turn_big_led
--whitelist-function wally_sha256
--whitelist-function random_32_bytes_mcu
--whitelist-type component_t
--whitelist-type confirm_params_t
--whitelist-type commander_error_t
--rustified-enum commander_error_t
--whitelist-function commander
--whitelist-function commander_states_can_call
--whitelist-function commander_states_clear_force_next
--whitelist-type BitBoxBaseRequest
--whitelist-var ".*_tag"
--whitelist-var MAX_LABEL_SIZE
--whitelist-var font_font_a_9X9
--whitelist-var font_font_a_11X10
--whitelist-var font_monogram_5X9
--whitelist-var font_password_11X12
--whitelist-var WALLY_OK
--whitelist-function trinary_input_string_create_password
--whitelist-function reset_reset
--whitelist-function sd_card_inserted
--whitelist-var BIP39_WORDLIST_LEN
${CMAKE_CURRENT_SOURCE_DIR}/rust/bitbox02-sys/wrapper.h --
-DPB_NO_PACKED_STRUCTS=1 -DPB_FIELD_16BIT=1 -fshort-enums ${RUST_BINDGEN_FLAGS} ${RUST_INCLUDES}
)
add_dependencies(rust-bindgen generate-protobufs)
# Test rust crates that contain business logic. Avoid testing crates that depend on hardware.
if(NOT CMAKE_CROSSCOMPILING)
add_custom_target(rust-test
COMMAND
${CMAKE_COMMAND} -E env
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
# enable nightly features in the stable release - needed to activate alloc_error_handler.
# see src/rust/bitbox02-rust-c/src/lib.rs.
# https://github.com/rust-lang/rust/issues/66740
RUSTC_BOOTSTRAP=1
${CARGO} test --all-features --target-dir ${RUST_BINARY_DIR} ${RUST_CARGO_FLAG} -- --nocapture
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rust/
)
add_dependencies(rust-test rust-bindgen)
add_custom_target(rust-clippy
COMMAND
# Force clippy to fully re-run. It is bad at figuring out when to run again and when to use caches.
${CARGO} clean --target-dir ${RUST_BINARY_DIR}
COMMAND
${CMAKE_COMMAND} -E env
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
# enable nightly features in the stable release - needed to activate alloc_error_handler.
# see src/rust/bitbox02-rust-c/src/lib.rs.
# https://github.com/rust-lang/rust/issues/66740
RUSTC_BOOTSTRAP=1
${CARGO} clippy
--all-features
--target-dir ${RUST_BINARY_DIR}
--release
-- # disabled linters:
-A clippy::large_enum_variant
-A clippy::identity_op
-A clippy::match_bool
-A clippy::new_without_default
-A clippy::single_match
-A clippy::iter_nth_zero
-A clippy::redundant_clone
-A clippy::type_complexity
-A clippy::collapsible-if
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rust/
)
add_dependencies(rust-clippy rust-bindgen)
endif()
# If a bootloader that locks the bootloader is flashed the bootloader area is permanently read-only.
set(BOOTLOADERS-BITBOX02
bootloader # Runs signed firmware
bootloader-development # Runs signed/unsigned firmware and has bootloader menu
bootloader-development-locked # Runs signed/unsigned firmware, has bootloader menu and bootloader is locked
bootloader-production # Runs signed firmware and bootloader is locked
bootloader-btc # Runs signed (btc-only keys) firmware
bootloader-btc-development # Runs signed/unsigned firmware and has bootloader menu
bootloader-btc-production # Runs signed (btc-only keys) firmware and bootloader is locked
)
set(BOOTLOADERS-BITBOXBASE
bootloader-bitboxbase # Runs signed (base keys) firmware
bootloader-bitboxbase-development # Runs signed/unsigned firmware and has bootloader menu
bootloader-bitboxbase-production # Runs signed (base keys) firmware and bootloader is locked
)
set(BOOTLOADERS
${BOOTLOADERS-BITBOX02}
${BOOTLOADERS-BITBOXBASE}
)
set(BOOTLOADERS ${BOOTLOADERS} PARENT_SCOPE)
set(FIRMWARES
firmware # Firmware MULTI
firmware-btc # Firmware BTC-ONLY
firmware-bitboxbase # Firmware BASE
factory-setup # Factory-setup
factory-setup-bitboxbase # Factory-setup
)
set(FIRMWARES ${FIRMWARES} PARENT_SCOPE)
if(CMAKE_CROSSCOMPILING)
set(RUST_LIBS ${FIRMWARES} ${BOOTLOADERS})
else()
set(RUST_LIBS c-unit-tests)
endif()
foreach(type ${RUST_LIBS})
set(lib ${RUST_BINARY_DIR}/${RUST_TARGET_ARCH_DIR}/${RUST_PROFILE}/libbitbox02_rust_c.a)
add_custom_target(${type}-rust
# Build rust library
COMMAND
${CMAKE_COMMAND} -E env
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
RUSTFLAGS=${RUSTFLAGS}
# enable nightly features in the stable release - needed to activate alloc_error_handler.
# see src/rust/bitbox02-rust-c/src/lib.rs.
# https://github.com/rust-lang/rust/issues/66740
RUSTC_BOOTSTRAP=1
${CARGO} build -v --features target-${type} --target-dir ${RUST_BINARY_DIR} ${RUST_CARGO_FLAG} ${RUST_TARGET_ARCH_ARG}
COMMAND
# Rust packages stdlib functions which we must remove/weaken on the arm target
# Currently the removed symbols only clash in device-tests for some reason. Without this command, compiling device tests would result in errors like:
# Linking C executable ../../bin/fw_test_usb_cmd_process.elf
# /usr/local/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld: /usr/local/bin/../lib/gcc/arm-none-eabi/8.2.1/thumb/v7e-m+fp/softfp/libgcc.a(_arm_addsubdf3.o): in function `__aeabi_dsub':
# (.text+0x8): multiple definition of `__aeabi_dsub'; ../../lib/libfirmware_rust_c.a(compiler_builtins-5829be534503bd8e.compiler_builtins.cthmhl66-cgu.175.rcgu.o):/cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.27/src/macros.rs:226: first defined here
if test "x${RUST_TARGET_ARCH}" = "xthumbv7em-none-eabi" \; then ${CMAKE_OBJCOPY} -W __aeabi_dsub -W __aeabi_dadd -W__aeabi_i2d -W __aeabi_f2d -W __aeabi_dmul ${lib} \; fi
COMMAND
${CMAKE_COMMAND} -E copy ${lib} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/lib${type}_rust_c.a
BYPRODUCTS
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/lib${type}_rust_c.a
WORKING_DIRECTORY ${LIBBITBOX02_RUST_SOURCE_DIR}
)
add_dependencies(${type}-rust rust-bindgen rust-cbindgen)
add_library(${type}_rust_c STATIC IMPORTED)
set_property(TARGET ${type}_rust_c PROPERTY IMPORTED_LOCATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/lib${type}_rust_c.a)
endforeach()
if(CMAKE_CROSSCOMPILING)
add_custom_target(rust-docs
COMMAND
${CMAKE_COMMAND} -E env
# enable nightly features in the stable release - needed to activate alloc_error_handler.
# see src/rust/bitbox02-rust-c/src/lib.rs.
# https://github.com/rust-lang/rust/issues/66740
RUSTC_BOOTSTRAP=1
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
SYSROOT=${CMAKE_SYSROOT}
INCLUDES="${RUST_INCLUDES}"
${CARGO} doc --document-private-items --target-dir ${CMAKE_BINARY_DIR}/docs-rust ${RUST_CARGO_FLAG} --target thumbv7em-none-eabi
COMMAND
${CMAKE_COMMAND} -E echo "See docs at file://${CMAKE_BINARY_DIR}/docs-rust/thumbv7em-none-eabi/doc/bitbox02_rust/index.html"
WORKING_DIRECTORY ${LIBBITBOX02_RUST_SOURCE_DIR}
)
add_dependencies(rust-docs rust-bindgen)
set(STACK_SIZE "0x10000" CACHE STRING "Specify stack size for bootloader/firmware")
set(STACK_SIZE ${STACK_SIZE} PARENT_SCOPE)
set(HEAP_SIZE "0x18000" CACHE STRING "Specify heap size for bootloader/firmware")
set(HEAP_SIZE ${HEAP_SIZE} PARENT_SCOPE)
foreach(bootloader ${BOOTLOADERS})
set(elf ${bootloader}.elf)
add_executable(${elf} ${BOOTLOADER-SOURCES})
target_link_libraries(${elf} PRIVATE c asf4-drivers-min samd51a-ds -Wl,-u,exception_table)
target_include_directories(${elf} PRIVATE ${INCLUDES})
target_include_directories(${elf} SYSTEM PRIVATE ${SYSTEMINCLUDES})
target_compile_definitions(${elf} PRIVATE BOOTLOADER "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
# needed to find version.h
target_include_directories(${elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
# needed to find bootloader_version.h
target_include_directories(${elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/bootloader)
target_link_libraries(${elf} PRIVATE "-Wl,-Map=\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${bootloader}.map\" -T\"${CMAKE_SOURCE_DIR}/bootloader.ld\"")
target_link_libraries(${elf} PRIVATE -Wl,--defsym=STACK_SIZE=${STACK_SIZE} -Wl,-defsym=HEAP_SIZE=${HEAP_SIZE})
target_link_libraries(${elf} PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T})
add_dependencies(${elf} ${bootloader}-rust)
target_link_libraries(${elf} PRIVATE ${bootloader}_rust_c)
# Select the smaller version of libc called nano.
target_compile_options(${elf} PRIVATE --specs=nano.specs)
target_link_libraries(${elf} PRIVATE --specs=nano.specs)
if(SEMIHOSTING)
# Select an implementation of the system calls that can communicate with the debugger
target_compile_options(${elf} PRIVATE --specs=rdimon.specs)
target_link_libraries(${elf} PRIVATE --specs=rdimon.specs)
target_compile_definitions(${elf} PRIVATE SEMIHOSTING)
else()
target_compile_options(${elf} PRIVATE --specs=nosys.specs)
target_link_libraries(${elf} PRIVATE --specs=nosys.specs)
endif()
endforeach(bootloader)
foreach(bootloader ${BOOTLOADERS-BITBOX02})
target_sources(${bootloader}.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES})
endforeach(bootloader)
foreach(bootloader ${BOOTLOADERS-BITBOXBASE})
target_sources(${bootloader}.elf PRIVATE ${PLATFORM-BITBOXBASE-SOURCES})
endforeach(bootloader)
target_compile_definitions(bootloader.elf PRIVATE PRODUCT_BITBOX_MULTI)
target_compile_definitions(bootloader-development.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_DEVDEVICE)
target_compile_definitions(bootloader-development-locked.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_DEVDEVICE BOOTLOADER_PRODUCTION)
set_property(TARGET bootloader-development-locked.elf PROPERTY EXCLUDE_FROM_ALL ON)
target_compile_definitions(bootloader-production.elf PRIVATE PRODUCT_BITBOX_MULTI BOOTLOADER_PRODUCTION)
set_property(TARGET bootloader-production.elf PROPERTY EXCLUDE_FROM_ALL ON)
target_compile_definitions(bootloader-btc.elf PRIVATE PRODUCT_BITBOX_BTCONLY)
target_compile_definitions(bootloader-btc-development.elf PRIVATE PRODUCT_BITBOX_BTCONLY BOOTLOADER_DEVDEVICE)
target_compile_definitions(bootloader-btc-production.elf PRIVATE PRODUCT_BITBOX_BTCONLY BOOTLOADER_PRODUCTION)
set_property(TARGET bootloader-btc-production.elf PROPERTY EXCLUDE_FROM_ALL ON)
target_compile_definitions(bootloader-bitboxbase.elf PRIVATE PRODUCT_BITBOX_BASE)
target_compile_definitions(bootloader-bitboxbase-development.elf PRIVATE BOOTLOADER_DEVDEVICE PRODUCT_BITBOX_BASE)
target_compile_definitions(bootloader-bitboxbase-production.elf PRIVATE BOOTLOADER_PRODUCTION PRODUCT_BITBOX_BASE)
set_property(TARGET bootloader-bitboxbase-production.elf PROPERTY EXCLUDE_FROM_ALL ON)
foreach(firmware ${FIRMWARES})
set(elf ${firmware}.elf)
add_executable(${elf} ${FIRMWARE-SOURCES})
# Must manually link against C so that malloc can find _sbrk
target_link_libraries(${elf}
PRIVATE
cryptoauthlib
fatfs
base32
bignum # TODO: only eth
sha3 # TODO: Only eth
ctaes
c
samd51a-ds
asf4-drivers-min
asf4-drivers
-Wl,-u,exception_table)
add_dependencies(${elf} generate-protobufs)
target_include_directories(${elf} PRIVATE ${INCLUDES})
target_include_directories(${elf} SYSTEM PRIVATE ${SYSTEMINCLUDES})
# Include and provide configuration for nanopb
target_include_directories(${elf} SYSTEM PRIVATE ${NANOPB_INCLUDE_DIRS})
target_compile_definitions(${elf} PRIVATE "PB_NO_PACKED_STRUCTS=1" "PB_FIELD_16BIT=1")
# needed to find version.h
target_include_directories(${elf} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${elf} PRIVATE "-Wl,-Map=\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${firmware}.map\" -T\"${CMAKE_SOURCE_DIR}/firmware.ld\"")
target_link_libraries(${elf} PRIVATE -Wl,--defsym=STACK_SIZE=${STACK_SIZE} -Wl,-defsym=HEAP_SIZE=${HEAP_SIZE})
add_dependencies(${elf} libwally-core)
target_link_libraries(${elf} PRIVATE wallycore secp256k1)
target_link_libraries(${elf} PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T})
# Select the smaller version of libc called nano.
target_compile_options(${elf} PRIVATE --specs=nano.specs)
target_link_libraries(${elf} PRIVATE --specs=nano.specs)
if(SEMIHOSTING)
# Select an implementation of the system calls that can communicate with the debugger
target_compile_options(${elf} PRIVATE --specs=rdimon.specs)
target_link_libraries(${elf} PRIVATE --specs=rdimon.specs)
target_compile_definitions(${elf} PRIVATE SEMIHOSTING)
else()
target_compile_options(${elf} PRIVATE --specs=nosys.specs)
target_link_libraries(${elf} PRIVATE --specs=nosys.specs)
endif()
add_dependencies(${elf} ${firmware}-rust)
target_link_libraries(${elf} PRIVATE ${firmware}_rust_c)
endforeach(firmware)
target_sources(firmware.elf PRIVATE firmware.c)
target_compile_definitions(firmware.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_BTC=1" "APP_LTC=1" "APP_ETH=1" "APP_U2F=1")
target_sources(firmware.elf PRIVATE ${FIRMWARE-U2F-SOURCES} ${ETHEREUM-SOURCES} ${PLATFORM-BITBOX02-SOURCES})
target_sources(firmware-btc.elf PRIVATE firmware.c)
target_compile_definitions(firmware-btc.elf PRIVATE PRODUCT_BITBOX_BTCONLY "APP_BTC=1" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_sources(firmware-btc.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES})
target_sources(firmware-bitboxbase.elf PRIVATE bitboxbase/bitboxbase.c)
target_compile_definitions(firmware-bitboxbase.elf PRIVATE PRODUCT_BITBOX_BASE "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_sources(firmware-bitboxbase.elf PRIVATE ${PLATFORM-BITBOXBASE-SOURCES} ${BITBOXBASE-FIRMWARE-SOURCES})
target_sources(factory-setup.elf PRIVATE factorysetup.c)
target_compile_definitions(factory-setup.elf PRIVATE PRODUCT_BITBOX02_FACTORYSETUP "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_sources(factory-setup.elf PRIVATE ${PLATFORM-BITBOX02-SOURCES})
target_sources(factory-setup-bitboxbase.elf PRIVATE factorysetup.c)
target_compile_definitions(factory-setup-bitboxbase.elf PRIVATE PRODUCT_BITBOXBASE_FACTORYSETUP "APP_BTC=0" "APP_LTC=0" "APP_ETH=0" "APP_U2F=0")
target_sources(factory-setup-bitboxbase.elf PRIVATE ${PLATFORM-BITBOXBASE-SOURCES})
foreach(name ${BOOTLOADERS} ${FIRMWARES})
add_custom_command(
TARGET ${name}.elf POST_BUILD
COMMAND ${CMAKE_SIZE} ${name}.elf
COMMAND ${CMAKE_OBJCOPY} -O binary ${name}.elf ${name}.bin
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
COMMENT "\nGenerating binary ${name}.bin"
)
# Fail on warnings for our builds
target_compile_options(${name}.elf PRIVATE "-Werror")
endforeach()
endif()