Skip to content

Commit

Permalink
cmake: Split app sources into separate libraries.
Browse files Browse the repository at this point in the history
As the files related to APP_BTC don't depend on any PRODUCT* macro,
but only on the APP_* macros, move those to a separate CMake unit
(app_btc-multi, app_btc-btc), to avoid multiple unnecessary
compilations.
  • Loading branch information
conte91 committed Dec 17, 2019
1 parent ccd538b commit 9a7f817
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 77 deletions.
64 changes: 45 additions & 19 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ set(DBB-FIRMWARE-COMMON-SOURCES
${CMAKE_SOURCE_DIR}/src/random.c
${CMAKE_SOURCE_DIR}/src/attestation.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/memory.c
Expand Down Expand Up @@ -112,7 +111,6 @@ set(DBB-FIRMWARE-UI-SOURCES
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
Expand Down Expand Up @@ -142,16 +140,6 @@ set(DRIVER-SOURCES
)
set(DRIVER-SOURCES ${DRIVER-SOURCES} PARENT_SCOPE)

set(APP-BTC-SOURCES
${CMAKE_SOURCE_DIR}/src/commander/commander_btc.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_bip143.c
)
set(APP-BTC-SOURCES ${APP-BTC-SOURCES} PARENT_SCOPE)

# Modules that provide HW support for the BitBox02,
# and don't depend on any PRODUCT_* macro definition.
set(PLATFORM-BITBOX02-SOURCES
Expand Down Expand Up @@ -179,6 +167,7 @@ set(BITBOXBASE-ONLY-SOURCES
# Modules specific to the BitBox02 platform,
# that depend on the PRODUCT_* macros.
set(BITBOX02-ONLY-SOURCES
${CMAKE_SOURCE_DIR}/src/usb/usb_frame.c
${CMAKE_SOURCE_DIR}/src/usb/usb.c
${CMAKE_SOURCE_DIR}/src/usb/usb_frame.c
${CMAKE_SOURCE_DIR}/src/sd_mmc/sd_mmc_start.c
Expand All @@ -197,7 +186,6 @@ set(BITBOXBASE-FIRMWARE-SOURCES ${BITBOXBASE-FIRMWARE-SOURCES} PARENT_SCOPE)
set(BITBOX02-FIRMWARE-SOURCES
${DBB-FIRMWARE-COMMON-SOURCES}
${BITBOX02-ONLY-SOURCES}
${APP-BTC-SOURCES}
${CMAKE_SOURCE_DIR}/src/firmware_main_loop.c
)
set(BITBOX02-FIRMWARE-SOURCES ${BITBOX02-FIRMWARE-SOURCES} PARENT_SCOPE)
Expand Down Expand Up @@ -252,13 +240,47 @@ 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(APP-BTC-SOURCES
${CMAKE_SOURCE_DIR}/src/commander/commander_btc.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_bip143.c
)
set(APP-BTC-SOURCES ${APP-BTC-SOURCES} PARENT_SCOPE)

get_property(WALLY_INCLUDES TARGET wallycore PROPERTY INTERFACE_INCLUDE_DIRECTORIES)

add_library(util ${CMAKE_SOURCE_DIR}/src/util.c)
target_include_directories(util PRIVATE ${INCLUDES})
target_include_directories(util SYSTEM PUBLIC ${SYSTEMINCLUDES})
target_include_directories(util SYSTEM PUBLIC ${NANOPB_INCLUDE_DIRS})
target_include_directories(util SYSTEM PUBLIC ${CMAKE_BINARY_DIR}/src)

add_library(app_btc-btc ${APP-BTC-SOURCES})
target_compile_definitions(app_btc-btc PUBLIC "APP_BTC=1" "APP_LTC=0")
target_include_directories(app_btc-btc PRIVATE ${INCLUDES})
target_include_directories(app_btc-btc SYSTEM PUBLIC ${SYSTEMINCLUDES})
target_include_directories(app_btc-btc SYSTEM PUBLIC ${NANOPB_INCLUDE_DIRS})
target_include_directories(app_btc-btc SYSTEM PUBLIC ${CMAKE_BINARY_DIR}/src)
target_link_libraries(app_btc-btc noiseprotocol wallycore secp256k1 util)

add_library(app_btc-multi ${APP-BTC-SOURCES})
target_compile_definitions(app_btc-multi PUBLIC "APP_BTC=1" "APP_LTC=1")
target_compile_definitions(app_btc-multi PRIVATE "NO_PLATFORM_CONFIGURED")
target_include_directories(app_btc-multi PRIVATE ${INCLUDES})
target_include_directories(app_btc-multi SYSTEM PUBLIC ${SYSTEMINCLUDES})
target_include_directories(app_btc-multi SYSTEM PUBLIC ${NANOPB_INCLUDE_DIRS})
target_include_directories(app_btc-multi SYSTEM PUBLIC ${CMAKE_BINARY_DIR}/src)
target_link_libraries(app_btc-multi noiseprotocol wallycore secp256k1 util)

set(FIRMWARE-SOURCES
${DBB-FIRMWARE-UI-SOURCES}
${FIRMWARE-DRIVER-SOURCES}
Expand Down Expand Up @@ -394,7 +416,7 @@ if(CMAKE_CROSSCOMPILING)
set(HEAP_SIZE ${HEAP_SIZE} PARENT_SCOPE)

add_library(bitbox02-platform ${PLATFORM-BITBOX02-SOURCES})
target_link_libraries(bitbox02-platform asf4-drivers-min)
target_link_libraries(bitbox02-platform asf4-drivers-min util)
target_include_directories(bitbox02-platform PRIVATE ${INCLUDES})
target_include_directories(bitbox02-platform SYSTEM PUBLIC ${SYSTEMINCLUDES})
target_include_directories(bitbox02-platform SYSTEM PUBLIC ${NANOPB_INCLUDE_DIRS})
Expand All @@ -408,7 +430,7 @@ if(CMAKE_CROSSCOMPILING)
#endif()

add_library(bitboxbase-platform ${PLATFORM-BITBOXBASE-SOURCES})
target_link_libraries(bitboxbase-platform asf4-drivers)
target_link_libraries(bitboxbase-platform asf4-drivers util)
target_include_directories(bitboxbase-platform PRIVATE ${INCLUDES})
target_include_directories(bitboxbase-platform SYSTEM PUBLIC ${SYSTEMINCLUDES})
target_include_directories(bitboxbase-platform SYSTEM PUBLIC ${NANOPB_INCLUDE_DIRS})
Expand Down Expand Up @@ -454,6 +476,7 @@ if(CMAKE_CROSSCOMPILING)
target_compile_options(${elf} PRIVATE --specs=nano.specs)
target_link_libraries(${elf} PRIVATE --specs=nano.specs)
string(FIND ${bootloader} semihosting SEMIHOSTING_FOUND)
target_compile_definitions(${elf} PRIVATE "APP_U2F=0" "APP_BTC=0" "APP_ETH=0" "APP_LTC=0")
if(SEMIHOSTING_FOUND EQUAL -1)
target_compile_options(${elf} PRIVATE --specs=nosys.specs)
target_link_libraries(${elf} PRIVATE --specs=nosys.specs)
Expand Down Expand Up @@ -563,21 +586,24 @@ if(CMAKE_CROSSCOMPILING)
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_compile_definitions(firmware.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_ETH=1" "APP_U2F=1")
target_sources(firmware.elf PRIVATE ${FIRMWARE-U2F-SOURCES})
target_link_libraries(firmware.elf PRIVATE bitbox02-platform)
target_link_libraries(firmware.elf PRIVATE app_btc-multi)

target_sources(firmware-semihosting.elf PRIVATE firmware.c)
target_sources(firmware-semihosting.elf PRIVATE ${FIRMWARE-U2F-SOURCES})
target_link_libraries(firmware-semihosting.elf PRIVATE bitbox02-platform)
target_link_libraries(firmware-semihosting.elf PRIVATE app_btc-multi)

# Select an implementation of the system calls that can communicate with the debugger
target_compile_definitions(firmware-semihosting.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_BTC=1" "APP_LTC=1" "APP_ETH=1" "APP_U2F=1")
target_compile_definitions(firmware-semihosting.elf PRIVATE PRODUCT_BITBOX_MULTI "APP_ETH=1" "APP_U2F=1")
target_compile_definitions(firmware-semihosting.elf PRIVATE SEMIHOSTING)

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_compile_definitions(firmware-btc.elf PRIVATE PRODUCT_BITBOX_BTCONLY "APP_ETH=0" "APP_U2F=0")
target_link_libraries(firmware-btc.elf PRIVATE bitbox02-platform)
target_link_libraries(firmware-btc.elf PRIVATE app_btc-btc)

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")
Expand Down
15 changes: 4 additions & 11 deletions src/apps/btc/btc_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static void _sha256(const uint8_t* bytes, size_t bytes_len, uint8_t* out)
sha256_finish(&ctx, out);
}

static void _reset(void)
void btc_sign_reset(void)
{
_state = STATE_INIT;
_coin_params = NULL;
Expand All @@ -103,7 +103,7 @@ static void _reset(void)

static app_btc_sign_error_t _error(app_btc_sign_error_t err)
{
_reset();
btc_sign_reset();
return err;
}

Expand Down Expand Up @@ -134,7 +134,7 @@ app_btc_sign_error_t app_btc_sign_init(
// legacy not supported
return _error(APP_BTC_SIGN_ERR_INVALID_INPUT);
}
_reset();
btc_sign_reset();
_coin_params = coin_params;
_script_type = request->script_type;
_bip44_account = request->bip44_account;
Expand Down Expand Up @@ -292,7 +292,7 @@ static app_btc_sign_error_t _sign_input_pass2(
next_out->index = _index;
} else {
// Done with inputs pass2 -> done completely.
_reset();
btc_sign_reset();
next_out->type = BTCSignNextResponse_Type_DONE;
}
return APP_BTC_SIGN_OK;
Expand Down Expand Up @@ -472,10 +472,3 @@ app_btc_sign_error_t app_btc_sign_output(
}
return APP_BTC_SIGN_OK;
}

#ifdef TESTING
void tst_app_btc_reset(void)
{
_reset();
}
#endif
4 changes: 1 addition & 3 deletions src/apps/btc/btc_sign.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ app_btc_sign_input(const BTCSignInputRequest* request, BTCSignNextResponse* next
USE_RESULT app_btc_sign_error_t
app_btc_sign_output(const BTCSignOutputRequest* request, BTCSignNextResponse* next_out);

#ifdef TESTING
void tst_app_btc_reset(void);
#endif
void btc_sign_reset(void);

#endif
1 change: 0 additions & 1 deletion src/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <stddef.h>
#include <stdint.h>

#include <platform_config.h>
#include <ui/ugui/ugui.h>

// TODO: allow updating
Expand Down
1 change: 1 addition & 0 deletions src/touch/gestures.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <stdbool.h>

#include <platform/platform_config.h>
#include <screen.h>
#include <ui/component.h>

Expand Down
2 changes: 1 addition & 1 deletion src/usb/class/hid/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

#include "hid.h"
#include "usb_desc.h"
//#include "usb_desc.h"
#if !defined(TESTING)
#include "usb_protocol.h"
#endif
Expand Down
32 changes: 2 additions & 30 deletions src/usb/class/usb_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
#define USB_DESC_IDPRODUCT 0x2403
#define USB_DESC_HWW_EP_IN (1 | USB_EP_DIR_IN)
#define USB_DESC_HWW_EP_OUT (2 | USB_EP_DIR_OUT)
#define USB_DESC_IFACE_NUM_HWW 0
#define USB_DESC_IFACE_LEN 32
#define USB_DESC_CONFIG_LEN 9
#define USB_DESC_WTOTALLEN (USB_DESC_CONFIG_LEN + USB_DESC_IFACE_LEN * USB_DESC_NUM_IFACES)
#define USB_DESC_U2F_EP_IN (3 | USB_EP_DIR_IN)
#define USB_DESC_U2F_EP_OUT (4 | USB_EP_DIR_OUT)
#define USB_DESC_BMAXPKSZ0 0x40
#define USB_DESC_BCDUSB 0x200 // 0x0200 => USB 2.0 version; 0x0210 => USB 2.1 version
#define USB_DESC_BCDDEVICE 0x100
Expand Down Expand Up @@ -130,30 +128,4 @@
USB_DESC_LE16(USB_DESC_HID_EP_SIZE), /* ep_out.wMaxPacketSize */ \
4 /* ep_out.bInterval */

#define USB_DESC_CONFIG \
USB_DESC_CONFIG_LEN, /* bLength */ \
0x02, /* bDescriptorType: CONFIGURATION */ \
USB_DESC_LE16(USB_DESC_WTOTALLEN), /* wTotalLength */ \
USB_DESC_NUM_IFACES, /* bNumInterfaces */ \
USB_DESC_BCONFIGVAL, /* bConfigurationValue */ \
0x00, /* iConfiguration */ \
USB_DESC_BMATTRI, /* bmAttributes */ \
USB_DESC_BMAXPOWER /* bMaxPower */

#define USB_DEV_DESC \
18, /* bLength */ \
0x01, /* bDescriptorType: DEVICE */ \
USB_DESC_LE16(USB_DESC_BCDUSB), /* bcdUSB */ \
USB_CLASS_NO, /* bDeviceClass */ \
USB_SUBCLASS_NO, /* bDeviceSubClass */ \
USB_PROTOCOL_NO, /* bDeviceProtocol */ \
USB_DESC_BMAXPKSZ0, /* bMaxPacketSize0 */ \
USB_DESC_LE16(USB_DESC_IDVENDER), /* idVendor */ \
USB_DESC_LE16(USB_DESC_IDPRODUCT), /* idProduct */ \
USB_DESC_LE16(USB_DESC_BCDDEVICE), /* bcdDevice */ \
USB_DESC_IMANUFACT, /* iManufacturer */ \
USB_DESC_IPRODUCT, /* iProduct */ \
USB_DESC_ISERIALNUM, /* iSerialNumber */ \
USB_DESC_BNUMCONFIG /* bNumConfigurations */

#endif
77 changes: 76 additions & 1 deletion src/usb/usb_desc_bytes.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef __USB_DESC_BYTES_H
#define __USB_DESC_BYTES_H

#include <version.h>

#if APP_U2F == 1
Expand Down Expand Up @@ -103,6 +106,76 @@
USB_DESC_IPRODUCT_STR_DESC \
USB_DESC_ISERIALNUM_STR_DESC

#if APP_U2F == 1
#define USB_DESC_IFACE_NUM_U2F 1
#define USB_DESC_NUM_IFACES 2
#else
#define USB_DESC_NUM_IFACES 1
#endif

#define USB_DESC_IFACE_NUM_HWW 0
#define USB_DESC_IFACE_LEN 32
#define USB_DESC_CONFIG_LEN 9
#define USB_DESC_WTOTALLEN (USB_DESC_CONFIG_LEN + USB_DESC_IFACE_LEN * USB_DESC_NUM_IFACES)

#define USB_DESC_CONFIG \
USB_DESC_CONFIG_LEN, /* bLength */ \
0x02, /* bDescriptorType: CONFIGURATION */ \
USB_DESC_LE16(USB_DESC_WTOTALLEN), /* wTotalLength */ \
USB_DESC_NUM_IFACES, /* bNumInterfaces */ \
USB_DESC_BCONFIGVAL, /* bConfigurationValue */ \
0x00, /* iConfiguration */ \
USB_DESC_BMATTRI, /* bmAttributes */ \
USB_DESC_BMAXPOWER /* bMaxPower */

#if APP_U2F == 1
#define USB_DESC_IFACE_U2F \
9, /* iface.bLength */ \
0x04, /* iface.bDescriptorType: INTERFACE */ \
USB_DESC_IFACE_NUM_U2F, /* iface.bInterfaceNumber */ \
0x00, /* iface.bAlternateSetting */ \
0x02, /* iface.bNumEndpoints */ \
HID_CLASS, /* iface.bInterfaceClass */ \
USB_SUBCLASS_NO, /* iface.bInterfaceSubClass */ \
USB_PROTOCOL_NO, /* iface.bInterfaceProtocol */ \
0x00, /* iface.iInterface */ \
9, /* hid.bLength */ \
USB_DT_HID, /* hid.bDescriptorType: HID */ \
USB_DESC_LE16(USB_HID_BDC_V1_11), /* hid.bcdHID */ \
0x00, /* hid.bCountryCode */ \
0x01, /* hid.bNumDescriptors */ \
0x22, /* hid.bRDescriptorType */ \
USB_DESC_LE16(USB_DESC_U2F_REPORT_LEN), /* hid.wDescriptorLength */ \
7, /* ep_in.bLength */ \
0x05, /* ep_in.bDescriptorType: ENDPOINT */ \
USB_DESC_U2F_EP_IN, /* ep_in.bEndpointAddress */ \
0x03, /* ep_in.bmAttributes */ \
USB_DESC_LE16(USB_DESC_HID_EP_SIZE), /* ep_in.wMaxPacketSize */ \
4, /* ep_in.bInterval */ \
7, /* ep_out.bLength */ \
0x05, /* ep_out.bDescriptorType: ENDPOINT */ \
USB_DESC_U2F_EP_OUT, /* ep_out.bEndpointAddress */ \
0x03, /* ep_out.bmAttributes */ \
USB_DESC_LE16(USB_DESC_HID_EP_SIZE), /* ep_out.wMaxPacketSize */ \
4 /* ep_out.bInterval */
#endif

#define USB_DEV_DESC \
18, /* bLength */ \
0x01, /* bDescriptorType: DEVICE */ \
USB_DESC_LE16(USB_DESC_BCDUSB), /* bcdUSB */ \
USB_CLASS_NO, /* bDeviceClass */ \
USB_SUBCLASS_NO, /* bDeviceSubClass */ \
USB_PROTOCOL_NO, /* bDeviceProtocol */ \
USB_DESC_BMAXPKSZ0, /* bMaxPacketSize0 */ \
USB_DESC_LE16(USB_DESC_IDVENDER), /* idVendor */ \
USB_DESC_LE16(USB_DESC_IDPRODUCT), /* idProduct */ \
USB_DESC_LE16(USB_DESC_BCDDEVICE), /* bcdDevice */ \
USB_DESC_IMANUFACT, /* iManufacturer */ \
USB_DESC_IPRODUCT, /* iProduct */ \
USB_DESC_ISERIALNUM, /* iSerialNumber */ \
USB_DESC_BNUMCONFIG /* bNumConfigurations */

// ** If add an interface, adjust USB_DESC_WTOTALLEN **
// TODO: USB_DESC_D_MAX_EP_N doesn't exist, but there is CONF_USB_D_NUM_EP_SP
// (= supported endpoints) - is that the one that needs to change?
Expand All @@ -112,4 +185,6 @@
#else
#define USB_DESC_FS \
USB_DEV_DESC, USB_DESC_CONFIG, USB_DESC_IFACE_HWW, USB_DESC_IFACE_U2F, USB_STR_DESC
#endif
#endif // APP_U2F

#endif // __USB_DESC_BYTES_H
2 changes: 1 addition & 1 deletion src/usb/usb_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <stdint.h>

#include "queue.h"
#include <usb/class/usb_desc.h>
#include "usb_desc.h"

#define FRAME_TYPE_MASK 0x80 // Frame type mask
#define FRAME_TYPE_INIT 0x80 // Initial frame identifier
Expand Down
1 change: 1 addition & 0 deletions src/workflow/reboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "reboot.h"
#include "confirm.h"
#include <memory/memory.h>
#include <platform/platform_config.h>
#include <screen.h>
#ifndef TESTING
#include <driver_init.h>
Expand Down
Loading

0 comments on commit 9a7f817

Please sign in to comment.