Skip to content

Commit 7d35ce1

Browse files
committed
usb: split parts of usb_desc.h into more headers
Keep the main usb_desc.h smaller, with the goal if copy/pasting it once per platform (BitBox02 and BitBox02 Plus) to add new product strings for BitBox02 Plus.
1 parent 733cabf commit 7d35ce1

File tree

9 files changed

+248
-140
lines changed

9 files changed

+248
-140
lines changed

src/hww.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <hardfault.h>
1818
#include <keystore.h>
1919
#include <memory/memory.h>
20+
#include <version.h>
2021

2122
#include <platform_config.h>
2223
#include <rust/rust.h>

src/u2f.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <usb/u2f/u2f_keys.h>
3333
#include <usb/usb_packet.h>
3434
#include <usb/usb_processing.h>
35+
#include <version.h>
3536
#include <wally_crypto.h>
3637

3738
#ifndef TESTING

src/usb/class/hid/u2f/hid_u2f.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
#include "hid_u2f.h"
1616
#include "usb/usb_processing.h"
17-
#include "usb_desc.h"
17+
#include "usb_size.h"
18+
#include "usb_u2f_desc.h"
1819
#include <queue.h>
1920
#include <string.h>
2021
#include <u2f/u2f_packet.h>

src/usb/class/usb_desc.h

Lines changed: 4 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2019 Shift Cryptosecurity AG
2+
// Copyright 2025 Shift Crypto AG
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
45
// you may not use this file except in compliance with the License.
@@ -15,57 +16,16 @@
1516
#ifndef _USB_DESC_H_
1617
#define _USB_DESC_H_
1718

19+
#include "usb_desc_common.h"
20+
#include "usb_size.h"
1821
#include <bootloader/bootloader_version.h>
1922
#include <version.h>
2023
#ifndef TESTING
2124
#include "usb_protocol.h"
2225
#include "usb_protocol_hid.h"
26+
#include <usb_u2f_desc.h>
2327
#endif
2428

25-
#define USB_DESC_LE16(a) ((uint8_t)(a)), ((uint8_t)((a) >> 8))
26-
27-
#define USB_DESC_IDVENDER 0x03eb
28-
#define USB_DESC_IDPRODUCT 0x2403
29-
#define USB_DESC_HWW_EP_IN (1 | USB_EP_DIR_IN)
30-
#define USB_DESC_HWW_EP_OUT (2 | USB_EP_DIR_OUT)
31-
#if APP_U2F == 1
32-
#define USB_DESC_U2F_EP_IN (3 | USB_EP_DIR_IN)
33-
#define USB_DESC_U2F_EP_OUT (4 | USB_EP_DIR_OUT)
34-
#define USB_DESC_IFACE_NUM_U2F 1
35-
#define USB_DESC_NUM_IFACES 2
36-
#else
37-
#define USB_DESC_NUM_IFACES 1
38-
#endif
39-
#define USB_DESC_IFACE_NUM_HWW 0
40-
#define USB_DESC_IFACE_LEN 32
41-
#define USB_DESC_CONFIG_LEN 9
42-
#define USB_DESC_WTOTALLEN (USB_DESC_CONFIG_LEN + USB_DESC_IFACE_LEN * USB_DESC_NUM_IFACES)
43-
#define USB_DESC_BMAXPKSZ0 0x40
44-
#define USB_DESC_BCDUSB 0x200 // 0x0200 => USB 2.0 version; 0x0210 => USB 2.1 version
45-
#define USB_DESC_BCDDEVICE 0x100
46-
#define USB_DESC_BNUMCONFIG 0x1
47-
#define USB_DESC_BCONFIGVAL 0x1
48-
#define USB_DESC_BMATTRI 0x80 // Bus power supply, no support for remote wakeup
49-
#define USB_DESC_BMAXPOWER 0x32
50-
#define USB_DESC_HID_EP_SIZE 0x40
51-
#define USB_REPORT_SIZE USB_DESC_HID_EP_SIZE
52-
#define USB_HID_REPORT_IN_SIZE USB_REPORT_SIZE
53-
#define USB_HID_REPORT_OUT_SIZE USB_REPORT_SIZE
54-
55-
#define USB_DESC_LANGID 0x0409 // English - United States
56-
#define USB_DESC_LANGID_DESC \
57-
4, /* bLength */ \
58-
0x03, /* bDescriptorType */ \
59-
USB_DESC_LE16(USB_DESC_LANGID), /* wLANGID[0] */
60-
61-
#define USB_DESC_IMANUFACT 1
62-
#define USB_DESC_IMANUFACT_STR_DESC \
63-
26, /* bLength */ \
64-
0x03, /* bDescriptorType */ \
65-
'b', 0, 'i', 0, 't', 0, 'b', 0, 'o', 0, 'x', 0, '.', 0, 's', 0, 'w', 0, 'i', 0, 's', 0, \
66-
's', 0,
67-
68-
#define USB_DESC_IPRODUCT 2
6929
#if defined(BOOTLOADER)
7030
#if PRODUCT_BITBOX_BTCONLY == 1
7131
#define USB_DESC_IPRODUCT_STR_DESC \
@@ -98,31 +58,6 @@
9858
'B', 0, 'i', 0, 't', 0, 'B', 0, 'o', 0, 'x', 0, '0', 0, '2', 0,
9959
#endif
10060

101-
#define USB_DESC_ISERIALNUM 3
102-
103-
#if defined(BOOTLOADER_DEVDEVICE) && defined(BOOTLOADER_VERSION_HAS_METADATA)
104-
#define BOOTLOADER_VERSION_APPEND_LEN 4
105-
#define BOOTLOADER_VERSION_APPEND_W16 '.', 0, 'd', 0, 'e', 0, 'v', 0,
106-
#elif defined(BOOTLOADER_DEVDEVICE)
107-
#define BOOTLOADER_VERSION_APPEND_LEN 4
108-
#define BOOTLOADER_VERSION_APPEND_W16 '+', 0, 'd', 0, 'e', 0, 'v', 0,
109-
#else
110-
#define BOOTLOADER_VERSION_APPEND_LEN 0
111-
#define BOOTLOADER_VERSION_APPEND_W16
112-
#endif
113-
114-
#ifdef BOOTLOADER
115-
#define USB_DESC_ISERIALNUM_STR_DESC \
116-
(2 + BOOTLOADER_VERSION_LEN * 2 + BOOTLOADER_VERSION_APPEND_LEN * 2), /* bLength */ \
117-
0x03, /* bDescriptorType */ \
118-
BOOTLOADER_VERSION_W16 BOOTLOADER_VERSION_APPEND_W16
119-
#else
120-
#define USB_DESC_ISERIALNUM_STR_DESC \
121-
(2 + DIGITAL_BITBOX_VERSION_LEN * 2), /* bLength */ \
122-
0x03, /* bDescriptorType */ \
123-
DIGITAL_BITBOX_VERSION_W16
124-
#endif
125-
12661
#define USB_STR_DESC \
12762
USB_DESC_LANGID_DESC \
12863
USB_DESC_IMANUFACT_STR_DESC \
@@ -148,27 +83,6 @@
14883
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ \
14984
0xc0 /* END_COLLECTION */
15085

151-
#ifndef BOOTLOADER
152-
#define USB_DESC_U2F_REPORT_LEN 34
153-
#define USB_DESC_U2F_REPORT \
154-
0x06, 0xd0, 0xf1, /* USAGE_PAGE (Reserved 0xFIDO) */ \
155-
0x09, 0x01, /* USAGE (HID Generic Device) */ \
156-
0xa1, 0x01, /* COLLECTION (Application) */ /* In Report */ \
157-
0x09, 0x20, /* USAGE (Input Report Data) */ \
158-
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
159-
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */ \
160-
0x75, 0x08, /* REPORT_SIZE (8) */ \
161-
0x95, 0x40, /* REPORT_COUNT (64) */ \
162-
0x81, 0x02, /* INPUT (Data,Var,Abs) */ /* Out Report */ \
163-
0x09, 0x21, /* USAGE (Output Report Data) */ \
164-
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
165-
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */ \
166-
0x75, 0x08, /* REPORT_SIZE (8) */ \
167-
0x95, 0x40, /* REPORT_COUNT (64) */ \
168-
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ \
169-
0xc0 /* END_COLLECTION */
170-
#endif
171-
17286
#define USB_DESC_IFACE_HWW \
17387
9, /* iface.bLength */ \
17488
0x04, /* iface.bDescriptorType: INTERFACE */ \
@@ -199,38 +113,6 @@
199113
USB_DESC_LE16(USB_DESC_HID_EP_SIZE), /* ep_out.wMaxPacketSize */ \
200114
4 /* ep_out.bInterval */
201115

202-
#if APP_U2F == 1
203-
#define USB_DESC_IFACE_U2F \
204-
9, /* iface.bLength */ \
205-
0x04, /* iface.bDescriptorType: INTERFACE */ \
206-
USB_DESC_IFACE_NUM_U2F, /* iface.bInterfaceNumber */ \
207-
0x00, /* iface.bAlternateSetting */ \
208-
0x02, /* iface.bNumEndpoints */ \
209-
HID_CLASS, /* iface.bInterfaceClass */ \
210-
USB_SUBCLASS_NO, /* iface.bInterfaceSubClass */ \
211-
USB_PROTOCOL_NO, /* iface.bInterfaceProtocol */ \
212-
0x00, /* iface.iInterface */ \
213-
9, /* hid.bLength */ \
214-
USB_DT_HID, /* hid.bDescriptorType: HID */ \
215-
USB_DESC_LE16(USB_HID_BDC_V1_11), /* hid.bcdHID */ \
216-
0x00, /* hid.bCountryCode */ \
217-
0x01, /* hid.bNumDescriptors */ \
218-
0x22, /* hid.bRDescriptorType */ \
219-
USB_DESC_LE16(USB_DESC_U2F_REPORT_LEN), /* hid.wDescriptorLength */ \
220-
7, /* ep_in.bLength */ \
221-
0x05, /* ep_in.bDescriptorType: ENDPOINT */ \
222-
USB_DESC_U2F_EP_IN, /* ep_in.bEndpointAddress */ \
223-
0x03, /* ep_in.bmAttributes */ \
224-
USB_DESC_LE16(USB_DESC_HID_EP_SIZE), /* ep_in.wMaxPacketSize */ \
225-
4, /* ep_in.bInterval */ \
226-
7, /* ep_out.bLength */ \
227-
0x05, /* ep_out.bDescriptorType: ENDPOINT */ \
228-
USB_DESC_U2F_EP_OUT, /* ep_out.bEndpointAddress */ \
229-
0x03, /* ep_out.bmAttributes */ \
230-
USB_DESC_LE16(USB_DESC_HID_EP_SIZE), /* ep_out.wMaxPacketSize */ \
231-
4 /* ep_out.bInterval */
232-
#endif
233-
234116
#define USB_DESC_CONFIG \
235117
USB_DESC_CONFIG_LEN, /* bLength */ \
236118
0x02, /* bDescriptorType: CONFIGURATION */ \
@@ -241,22 +123,6 @@
241123
USB_DESC_BMATTRI, /* bmAttributes */ \
242124
USB_DESC_BMAXPOWER /* bMaxPower */
243125

244-
#define USB_DEV_DESC \
245-
18, /* bLength */ \
246-
0x01, /* bDescriptorType: DEVICE */ \
247-
USB_DESC_LE16(USB_DESC_BCDUSB), /* bcdUSB */ \
248-
USB_CLASS_NO, /* bDeviceClass */ \
249-
USB_SUBCLASS_NO, /* bDeviceSubClass */ \
250-
USB_PROTOCOL_NO, /* bDeviceProtocol */ \
251-
USB_DESC_BMAXPKSZ0, /* bMaxPacketSize0 */ \
252-
USB_DESC_LE16(USB_DESC_IDVENDER), /* idVendor */ \
253-
USB_DESC_LE16(USB_DESC_IDPRODUCT), /* idProduct */ \
254-
USB_DESC_LE16(USB_DESC_BCDDEVICE), /* bcdDevice */ \
255-
USB_DESC_IMANUFACT, /* iManufacturer */ \
256-
USB_DESC_IPRODUCT, /* iProduct */ \
257-
USB_DESC_ISERIALNUM, /* iSerialNumber */ \
258-
USB_DESC_BNUMCONFIG /* bNumConfigurations */
259-
260126
// ** If add an interface, adjust USB_DESC_WTOTALLEN **
261127
// TODO: USB_DESC_D_MAX_EP_N doesn't exist, but there is CONF_USB_D_NUM_EP_SP
262128
// (= supported endpoints) - is that the one that needs to change?

src/usb/class/usb_desc_common.h

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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_COMMON_H_
16+
#define _USB_DESC_COMMON_H_
17+
18+
#include <version.h>
19+
20+
#define USB_DESC_LE16(a) ((uint8_t)(a)), ((uint8_t)((a) >> 8))
21+
22+
#define USB_DESC_IDVENDER 0x03eb
23+
#define USB_DESC_IDPRODUCT 0x2403
24+
#define USB_DESC_HWW_EP_IN (1 | USB_EP_DIR_IN)
25+
#define USB_DESC_HWW_EP_OUT (2 | USB_EP_DIR_OUT)
26+
#if APP_U2F == 1
27+
#define USB_DESC_IFACE_NUM_U2F 1
28+
#define USB_DESC_NUM_IFACES 2
29+
#else
30+
#define USB_DESC_NUM_IFACES 1
31+
#endif
32+
#define USB_DESC_IFACE_NUM_HWW 0
33+
#define USB_DESC_IFACE_LEN 32
34+
#define USB_DESC_CONFIG_LEN 9
35+
#define USB_DESC_WTOTALLEN (USB_DESC_CONFIG_LEN + USB_DESC_IFACE_LEN * USB_DESC_NUM_IFACES)
36+
#define USB_DESC_BMAXPKSZ0 0x40
37+
#define USB_DESC_BCDUSB 0x200 // 0x0200 => USB 2.0 version; 0x0210 => USB 2.1 version
38+
#define USB_DESC_BCDDEVICE 0x100
39+
#define USB_DESC_BNUMCONFIG 0x1
40+
#define USB_DESC_BCONFIGVAL 0x1
41+
#define USB_DESC_BMATTRI 0x80 // Bus power supply, no support for remote wakeup
42+
#define USB_DESC_BMAXPOWER 0x32
43+
#define USB_DESC_HID_EP_SIZE USB_REPORT_SIZE
44+
45+
#define USB_DESC_LANGID 0x0409 // English - United States
46+
#define USB_DESC_LANGID_DESC \
47+
4, /* bLength */ \
48+
0x03, /* bDescriptorType */ \
49+
USB_DESC_LE16(USB_DESC_LANGID), /* wLANGID[0] */
50+
51+
#define USB_DESC_IMANUFACT 1
52+
#define USB_DESC_IMANUFACT_STR_DESC \
53+
26, /* bLength */ \
54+
0x03, /* bDescriptorType */ \
55+
'b', 0, 'i', 0, 't', 0, 'b', 0, 'o', 0, 'x', 0, '.', 0, 's', 0, 'w', 0, 'i', 0, 's', 0, \
56+
's', 0,
57+
58+
#define USB_DESC_ISERIALNUM 3
59+
60+
#ifdef BOOTLOADER
61+
#define USB_DESC_ISERIALNUM_STR_DESC \
62+
(2 + BOOTLOADER_VERSION_LEN * 2 + BOOTLOADER_VERSION_APPEND_LEN * 2), /* bLength */ \
63+
0x03, /* bDescriptorType */ \
64+
BOOTLOADER_VERSION_W16 BOOTLOADER_VERSION_APPEND_W16
65+
#else
66+
#define USB_DESC_ISERIALNUM_STR_DESC \
67+
(2 + DIGITAL_BITBOX_VERSION_LEN * 2), /* bLength */ \
68+
0x03, /* bDescriptorType */ \
69+
DIGITAL_BITBOX_VERSION_W16
70+
#endif
71+
72+
#if defined(BOOTLOADER_DEVDEVICE) && defined(BOOTLOADER_VERSION_HAS_METADATA)
73+
#define BOOTLOADER_VERSION_APPEND_LEN 4
74+
#define BOOTLOADER_VERSION_APPEND_W16 '.', 0, 'd', 0, 'e', 0, 'v', 0,
75+
#elif defined(BOOTLOADER_DEVDEVICE)
76+
#define BOOTLOADER_VERSION_APPEND_LEN 4
77+
#define BOOTLOADER_VERSION_APPEND_W16 '+', 0, 'd', 0, 'e', 0, 'v', 0,
78+
#else
79+
#define BOOTLOADER_VERSION_APPEND_LEN 0
80+
#define BOOTLOADER_VERSION_APPEND_W16
81+
#endif
82+
83+
#define USB_DESC_IFACE_HWW \
84+
9, /* iface.bLength */ \
85+
0x04, /* iface.bDescriptorType: INTERFACE */ \
86+
USB_DESC_IFACE_NUM_HWW, /* iface.bInterfaceNumber */ \
87+
0x00, /* iface.bAlternateSetting */ \
88+
0x02, /* iface.bNumEndpoints */ \
89+
HID_CLASS, /* iface.bInterfaceClass */ \
90+
USB_SUBCLASS_NO, /* iface.bInterfaceSubClass */ \
91+
USB_PROTOCOL_NO, /* iface.bInterfaceProtocol */ \
92+
0x00, /* iface.iInterface */ \
93+
9, /* hid.bLength */ \
94+
USB_DT_HID, /* hid.bDescriptorType: HID */ \
95+
USB_DESC_LE16(USB_HID_BDC_V1_11), /* hid.bcdHID */ \
96+
0x00, /* hid.bCountryCode */ \
97+
0x01, /* hid.bNumDescriptors */ \
98+
0x22, /* hid.bRDescriptorType */ \
99+
USB_DESC_LE16(USB_DESC_HWW_REPORT_LEN), /* hid.wDescriptorLength */ \
100+
7, /* ep_in.bLength */ \
101+
0x05, /* ep_in.bDescriptorType: ENDPOINT */ \
102+
USB_DESC_HWW_EP_IN, /* ep_in.bEndpointAddress */ \
103+
0x03, /* ep_in.bmAttributes */ \
104+
USB_DESC_LE16(USB_DESC_HID_EP_SIZE), /* ep_in.wMaxPacketSize */ \
105+
4, /* ep_in.bInterval */ \
106+
7, /* ep_out.bLength */ \
107+
0x05, /* ep_out.bDescriptorType: ENDPOINT */ \
108+
USB_DESC_HWW_EP_OUT, /* ep_out.bEndpointAddress */ \
109+
0x03, /* ep_out.bmAttributes */ \
110+
USB_DESC_LE16(USB_DESC_HID_EP_SIZE), /* ep_out.wMaxPacketSize */ \
111+
4 /* ep_out.bInterval */
112+
113+
#define USB_DESC_CONFIG \
114+
USB_DESC_CONFIG_LEN, /* bLength */ \
115+
0x02, /* bDescriptorType: CONFIGURATION */ \
116+
USB_DESC_LE16(USB_DESC_WTOTALLEN), /* wTotalLength */ \
117+
USB_DESC_NUM_IFACES, /* bNumInterfaces */ \
118+
USB_DESC_BCONFIGVAL, /* bConfigurationValue */ \
119+
0x00, /* iConfiguration */ \
120+
USB_DESC_BMATTRI, /* bmAttributes */ \
121+
USB_DESC_BMAXPOWER /* bMaxPower */
122+
123+
#define USB_DESC_IPRODUCT 2
124+
#define USB_DEV_DESC \
125+
18, /* bLength */ \
126+
0x01, /* bDescriptorType: DEVICE */ \
127+
USB_DESC_LE16(USB_DESC_BCDUSB), /* bcdUSB */ \
128+
USB_CLASS_NO, /* bDeviceClass */ \
129+
USB_SUBCLASS_NO, /* bDeviceSubClass */ \
130+
USB_PROTOCOL_NO, /* bDeviceProtocol */ \
131+
USB_DESC_BMAXPKSZ0, /* bMaxPacketSize0 */ \
132+
USB_DESC_LE16(USB_DESC_IDVENDER), /* idVendor */ \
133+
USB_DESC_LE16(USB_DESC_IDPRODUCT), /* idProduct */ \
134+
USB_DESC_LE16(USB_DESC_BCDDEVICE), /* bcdDevice */ \
135+
USB_DESC_IMANUFACT, /* iManufacturer */ \
136+
USB_DESC_IPRODUCT, /* iProduct */ \
137+
USB_DESC_ISERIALNUM, /* iSerialNumber */ \
138+
USB_DESC_BNUMCONFIG /* bNumConfigurations */
139+
140+
#endif

src/usb/class/usb_size.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2025 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_SIZE_H_
16+
#define _USB_SIZE_H_
17+
18+
#define USB_REPORT_SIZE 64
19+
#define USB_HID_REPORT_OUT_SIZE USB_REPORT_SIZE
20+
21+
#endif

0 commit comments

Comments
 (0)