Skip to content

Commit 0700002

Browse files
authored
Merge pull request #1486 from NickeZ/nickez/factory-setup-ble-used-device
factory-setup: Check if BLE chip is booted
2 parents 19331c1 + df73f5a commit 0700002

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

bitbox-da14531-firmware.bin

-16 Bytes
Binary file not shown.

src/da14531/da14531_protocol.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ struct da14531_protocol {
8282

8383
static struct da14531_protocol _protocol;
8484

85+
static uint8_t* ble_fw = NULL;
86+
static size_t ble_fw_size = 0;
87+
static uint8_t ble_fw_checksum = 0;
88+
8589
#if 0
8690
static const char* _firmware_loader_state_str(enum firmware_loader_state state)
8791
{
@@ -104,14 +108,7 @@ static const char* _firmware_loader_state_str(enum firmware_loader_state state)
104108

105109
static void _firmware_loader_init(struct firmware_loader* self)
106110
{
107-
// If we are in factory setup we expect to load the ble firmware, so we start in IDLE.
108-
// In production bootloader and firmware we expect the da14531 to already be booted. We will still
109-
// load the firmware if it happens to not be loaded.
110-
#if FACTORYSETUP == 1
111-
self->state = FIRMWARE_LOADER_STATE_IDLE;
112-
#else
113111
self->state = FIRMWARE_LOADER_STATE_DONE;
114-
#endif
115112
}
116113

117114
#define SOH 0x01
@@ -125,10 +122,6 @@ static void _firmware_loader_poll(
125122
uint16_t* buf_in_len,
126123
struct ringbuffer* out_queue)
127124
{
128-
static uint8_t* ble_fw = NULL;
129-
static size_t ble_fw_size = 0;
130-
static uint8_t ble_fw_checksum = 0;
131-
132125
// if (*buf_in_len > 0) {
133126
// util_log(
134127
// "%s, got bytes %s",
@@ -394,7 +387,7 @@ struct da14531_protocol_frame* da14531_protocol_poll(
394387
const uint8_t** hww_data,
395388
struct ringbuffer* out_queue)
396389
{
397-
if (*hww_data) {
390+
if (hww_data && *hww_data) {
398391
uint8_t tmp[128];
399392
int len = da14531_protocol_format(
400393
&tmp[0], sizeof(tmp), DA14531_PROTOCOL_PACKET_TYPE_BLE_DATA, *hww_data, 64);
@@ -464,11 +457,19 @@ void da14531_protocol_init(void)
464457
// Only attempt swd reset in factory setup or debug builds. In production swd is turned off and
465458
// this is therefore useless.
466459
#if FACTORYSETUP == 1 || !defined(NDEBUG)
460+
// Load the firmware from external flash to RAM so that we are ready to flash.
461+
if (ble_fw == NULL) {
462+
if (!memory_spi_get_active_ble_firmware(&ble_fw, &ble_fw_size, &ble_fw_checksum)) {
463+
util_log("da14531: no valid firmware");
464+
}
465+
}
467466
// Reset the device if possible, if we cannot reset it over SWD, it must already be running
468467
if (!_swd_reset_da14531()) {
469468
// This may fail if the BLE chip has been started with a production firmware that has
470469
// disabled the debug interface
471470
util_log("da14531: Failed to reset over SWD");
471+
free(ble_fw);
472+
ble_fw = NULL;
472473
} else {
473474
// If we successfully reset the chip, we also would like to load it with firmware
474475
_protocol.loader.state = FIRMWARE_LOADER_STATE_IDLE;

src/factorysetup.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "common_main.h"
16+
#include "da14531/da14531.h"
1617
#include "da14531/da14531_binary.h"
1718
#include "da14531/da14531_protocol.h"
1819
#include "driver_init.h"
@@ -45,8 +46,8 @@
4546

4647
// We commit to the BLE firmware hash here to avoid accidentally installing an unexpected firmware.
4748
static const uint8_t _allowed_ble_fw_hash[32] =
48-
"\x87\xfc\xda\x87\x69\xcd\xa6\x16\x66\xa0\x99\xbc\x23\x8b\xf7\xd8\x36\xc5\x4e\x3a\xfd\x7f\xaa"
49-
"\x41\x7b\xc8\xa6\x09\x80\x9e\x1c\xb1";
49+
"\xa6\xe8\xda\x32\xe5\x2c\x9b\xdf\xca\xb2\xb8\xbd\x9c\x3f\x5c\xb2\xb8\xa4\xe4\x14\x29\x49\x5e"
50+
"\x98\x63\xcc\xb0\xd4\x96\xfa\xd5\xe6";
5051

5152
// 65 bytes uncompressed secp256k1 root attestation pubkey.
5253
#define ROOT_PUBKEY_SIZE 65
@@ -244,18 +245,18 @@ typedef enum {
244245
} error_code_t;
245246

246247
typedef enum {
247-
BLE_OK,
248-
BLE_ERR_FW_TOO_LARGE,
249-
BLE_ERR_FLASH_FW,
250-
BLE_ERR_GET_METADATA,
251-
BLE_ERR_SET_METADATA,
252-
BLE_ERR_READ_FW,
253-
BLE_ERR_FW_SIZE_MISMATCH,
254-
BLE_ERR_FW_CHECKSUM_MISMATCH,
255-
BLE_ERR_FW_MISMATCH,
256-
BLE_ERR_SPI_ERASE,
257-
BLE_ERR_FW_NOT_ALLOWED,
258-
BLE_ERR_NOT_BOOTED,
248+
BLE_OK = 0,
249+
BLE_ERR_FW_TOO_LARGE = 1,
250+
BLE_ERR_FLASH_FW = 2,
251+
BLE_ERR_GET_METADATA = 3,
252+
BLE_ERR_SET_METADATA = 4,
253+
BLE_ERR_READ_FW = 5,
254+
BLE_ERR_FW_SIZE_MISMATCH = 6,
255+
BLE_ERR_FW_CHECKSUM_MISMATCH = 7,
256+
BLE_ERR_FW_MISMATCH = 8,
257+
BLE_ERR_SPI_ERASE = 9,
258+
BLE_ERR_FW_NOT_ALLOWED = 10,
259+
BLE_ERR_NOT_BOOTED = 11,
259260
} ble_error_code_t;
260261

261262
static ble_error_code_t _ble_result;
@@ -563,6 +564,10 @@ static ble_error_code_t _setup_ble(void)
563564
uint8_t uart_write_buf[1024];
564565
struct ringbuffer uart_write_queue;
565566
ringbuffer_init(&uart_write_queue, uart_write_buf, sizeof(uart_write_buf));
567+
// If the BLE chip already was successfully booted, for example by running the factory-setup
568+
// once already and not power cycled, we need to ask for something to get a uart frame back.
569+
// Therefore we schedule a "get connection state".
570+
da14531_get_connection_state(&uart_write_queue);
566571
int32_t timeout = 1000000;
567572
while (timeout-- > 0) {
568573
uart_poll(uart_read_buf, sizeof(uart_read_buf), &uart_read_buf_len, &uart_write_queue);
@@ -573,6 +578,7 @@ static ble_error_code_t _setup_ble(void)
573578
return BLE_OK;
574579
}
575580
}
581+
screen_print_debug("Failed to check BLE chip status", 0);
576582
return BLE_ERR_NOT_BOOTED;
577583
}
578584

src/rust/bitbox02-rust/src/hww/api/bluetooth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use alloc::vec::Vec;
2727

2828
use bitbox02::{memory, spi_mem};
2929

30-
const ALLOWED_HASH: &[u8; 32] = b"\x87\xfc\xda\x87\x69\xcd\xa6\x16\x66\xa0\x99\xbc\x23\x8b\xf7\xd8\x36\xc5\x4e\x3a\xfd\x7f\xaa\x41\x7b\xc8\xa6\x09\x80\x9e\x1c\xb1";
30+
const ALLOWED_HASH: &[u8; 32] = b"\xa6\xe8\xda\x32\xe5\x2c\x9b\xdf\xca\xb2\xb8\xbd\x9c\x3f\x5c\xb2\xb8\xa4\xe4\x14\x29\x49\x5e\x98\x63\xcc\xb0\xd4\x96\xfa\xd5\xe6";
3131

3232
// We want to write FW to the memory chip in erase-size chunks, so that we don't repeatedly need to
3333
// read-erase-write the same sector.

0 commit comments

Comments
 (0)