Skip to content

Commit 4d3fa3b

Browse files
committed
usb: add product strings for bb02-plus, make dynamic
For the time being we intend to have one firmware per edition targetting both BB02 and BB02-Plus. For this reason we add a memory flag to store which platform it is (BB02 vs BB02-Plus), and make the USB HID descriptor product string dynamic dependent on that. Since our descriptor bytes are built from macros, this commit does a quick solution by copy pasting the descriptor and changing only the product string. In the future we can consider building the descriptor bytes in normal code which would make setting the product string easier.
1 parent 7d35ce1 commit 4d3fa3b

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

src/memory/memory_shared.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,17 @@ uint8_t memory_get_securechip_type(void)
6565
return MEMORY_SECURECHIP_TYPE_ATECC;
6666
}
6767
}
68+
69+
uint8_t memory_get_platform(void)
70+
{
71+
chunk_shared_t chunk = {0};
72+
memory_read_shared_bootdata(&chunk);
73+
uint8_t platform = chunk.fields.platform;
74+
util_zero(&chunk, sizeof(chunk));
75+
switch (platform) {
76+
case MEMORY_PLATFORM_BITBOX02_PLUS:
77+
return platform;
78+
default:
79+
return MEMORY_PLATFORM_BITBOX02;
80+
}
81+
}

src/memory/memory_shared.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ typedef union {
8080
uint8_t io_protection_key_split[32];
8181
uint8_t authorization_key_split[32];
8282
uint8_t encryption_key_split[32];
83+
uint8_t platform;
84+
uint8_t reserved[3]; // align to 4 bytes
8385
} fields;
8486
uint8_t bytes[FLASH_SHARED_DATA_LEN];
8587
} chunk_shared_t;
@@ -106,4 +108,8 @@ USE_RESULT uint8_t memory_get_screen_type(void);
106108
#define MEMORY_SECURECHIP_TYPE_OPTIGA 0x01
107109
USE_RESULT uint8_t memory_get_securechip_type(void);
108110

111+
#define MEMORY_PLATFORM_BITBOX02 0xFF
112+
#define MEMORY_PLATFORM_BITBOX02_PLUS 0x01
113+
USE_RESULT uint8_t memory_get_platform(void);
114+
109115
#endif

src/usb/class/usb_desc_bitbox02plus.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2025 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _USB_DESC_BITBOX02_PLUS_H_
16+
#define _USB_DESC_BITBOX02_PLUS_H_
17+
18+
#include "usb_desc_common.h"
19+
#include "usb_size.h"
20+
#include <bootloader/bootloader_version.h>
21+
#include <version.h>
22+
#ifndef TESTING
23+
#include "usb_protocol.h"
24+
#include "usb_protocol_hid.h"
25+
#include <usb_u2f_desc.h>
26+
#endif
27+
28+
#if defined(BOOTLOADER)
29+
#if PRODUCT_BITBOX_BTCONLY == 1
30+
#define USB_DESC_BB02PLUS_IPRODUCT_STR_DESC \
31+
34, /* bLength */ \
32+
0x03, /* bDescriptorType */ \
33+
'b', 0, 'b', 0, '0', 0, '2', 0, 'p', 0, '-', 0, 'b', 0, 'l', 0, '-', 0, 'b', 0, 't', 0, \
34+
'c', 0, 'o', 0, 'n', 0, 'l', 0, 'y', 0,
35+
#else
36+
#define USB_DESC_BB02PLUS_IPRODUCT_STR_DESC \
37+
30, /* bLength */ \
38+
0x03, /* bDescriptorType */ \
39+
'b', 0, 'b', 0, '0', 0, '2', 0, 'p', 0, '-', 0, 'b', 0, 'l', 0, '-', 0, 'm', 0, 'u', 0, \
40+
'l', 0, 't', 0, 'i', 0,
41+
#endif
42+
#elif FACTORYSETUP == 1
43+
#define USB_DESC_BB02PLUS_IPRODUCT_STR_DESC \
44+
28, /* bLength */ \
45+
0x03, /* bDescriptorType */ \
46+
'b', 0, 'b', 0, '0', 0, '2', 0, 'p', 0, '-', 0, 'f', 0, 'a', 0, 'c', 0, 't', 0, 'o', 0, \
47+
'r', 0, 'y', 0,
48+
#elif PRODUCT_BITBOX_BTCONLY == 1
49+
#define USB_DESC_BB02PLUS_IPRODUCT_STR_DESC \
50+
28, /* bLength */ \
51+
0x03, /* bDescriptorType */ \
52+
'b', 0, 'b', 0, '0', 0, '2', 0, 'p', 0, '-', 0, 'b', 0, 't', 0, 'c', 0, 'o', 0, 'n', 0, \
53+
'l', 0, 'y', 0,
54+
#else
55+
#define USB_DESC_BB02PLUS_IPRODUCT_STR_DESC \
56+
24, /* bLength */ \
57+
0x03, /* bDescriptorType */ \
58+
'b', 0, 'b', 0, '0', 0, '2', 0, 'p', 0, '-', 0, 'm', 0, 'u', 0, 'l', 0, 't', 0, 'i', 0,
59+
#endif
60+
61+
#define USB_STR_DESC_BB02PLUS \
62+
USB_DESC_LANGID_DESC \
63+
USB_DESC_IMANUFACT_STR_DESC \
64+
USB_DESC_BB02PLUS_IPRODUCT_STR_DESC \
65+
USB_DESC_ISERIALNUM_STR_DESC
66+
67+
#define USB_DESC_BB02PLUS_HWW_REPORT_LEN 34
68+
#define USB_DESC_BB02PLUS_HWW_REPORT \
69+
0x06, 0xff, 0xff, /* USAGE_PAGE (Vendor Defined) */ \
70+
0x09, 0x01, /* USAGE (HID Generic Device) */ \
71+
0xa1, 0x01, /* COLLECTION (Application) */ /* In Report */ \
72+
0x09, 0x20, /* USAGE (Input Report Data) */ \
73+
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
74+
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */ \
75+
0x75, 0x08, /* REPORT_SIZE (8) */ \
76+
0x95, 0x40, /* REPORT_COUNT (64) */ \
77+
0x81, 0x02, /* INPUT (Data,Var,Abs) */ /* Out Report */ \
78+
0x09, 0x21, /* USAGE (Output Report Data) */ \
79+
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
80+
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */ \
81+
0x75, 0x08, /* REPORT_SIZE (8) */ \
82+
0x95, 0x40, /* REPORT_COUNT (64) */ \
83+
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ \
84+
0xc0 /* END_COLLECTION */
85+
86+
// ** If add an interface, adjust USB_DESC_BB02PLUS_WTOTALLEN **
87+
// TODO: USB_DESC_BB02PLUS_D_MAX_EP_N doesn't exist, but there is CONF_USB_D_NUM_EP_SP
88+
// (= supported endpoints) - is that the one that needs to change?
89+
// ** If add more endpoints, adjust USB_DESC_BB02PLUS_D_MAX_EP_N **
90+
#if APP_U2F == 0
91+
#define USB_DESC_BB02PLUS_FS \
92+
USB_DEV_DESC, USB_DESC_CONFIG, USB_DESC_IFACE_HWW, USB_STR_DESC_BB02PLUS
93+
#else
94+
#define USB_DESC_BB02PLUS_FS \
95+
USB_DEV_DESC, USB_DESC_CONFIG, USB_DESC_IFACE_HWW, USB_DESC_IFACE_U2F, USB_STR_DESC_BB02PLUS
96+
#endif
97+
98+
#endif

src/usb/usb.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#ifndef TESTING
1717
#include "hid_hww.h"
1818
#include "usb_desc.h"
19+
#include "usb_desc_bitbox02plus.h"
1920
#include "usb_size.h"
2021
#include "usbdc.h"
22+
#include <memory/memory_shared.h>
2123
#if APP_U2F == 1
2224
#include "u2f.h"
2325
#include <usb/class/hid/u2f/hid_u2f.h>
@@ -40,6 +42,11 @@ static uint8_t _descriptor_bytes[] = {
4042
USB_DESC_FS}; // Device descriptors and Configuration descriptors list.
4143
static struct usbd_descriptors _descriptor[] = {
4244
{_descriptor_bytes, _descriptor_bytes + sizeof(_descriptor_bytes)}};
45+
static uint8_t _descriptor_bytes_bitbox02plus[] = {
46+
USB_DESC_BB02PLUS_FS}; // Device descriptors and Configuration descriptors list.
47+
static struct usbd_descriptors _descriptor_bitbox02plus[] = {
48+
{_descriptor_bytes_bitbox02plus,
49+
_descriptor_bytes_bitbox02plus + sizeof(_descriptor_bytes_bitbox02plus)}};
4350
static void (*_on_hww_init)(void) = NULL;
4451
static void _hww_endpoint_available(void);
4552
#if APP_U2F == 1
@@ -110,7 +117,14 @@ int32_t usb_start(void (*on_hww_init)(void))
110117
return ret;
111118
}
112119
#endif
113-
usbdc_start(_descriptor);
120+
switch (memory_get_platform()) {
121+
case MEMORY_PLATFORM_BITBOX02_PLUS:
122+
usbdc_start(_descriptor_bitbox02plus);
123+
break;
124+
default:
125+
usbdc_start(_descriptor);
126+
break;
127+
}
114128
usbdc_attach();
115129
#else
116130
(void)on_hww_init;

0 commit comments

Comments
 (0)