2018-02-26 13:06:10 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2018-02-26 13:06:10 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-06-19 21:59:13 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-10-16 16:03:04 +00:00
|
|
|
#include <pb.h>
|
2017-06-19 12:20:09 +00:00
|
|
|
#include <pb_decode.h>
|
2017-06-14 18:40:31 +00:00
|
|
|
#include <pb_encode.h>
|
|
|
|
#include "messages.pb.h"
|
2017-04-15 15:44:19 +00:00
|
|
|
|
2023-10-31 11:38:11 +00:00
|
|
|
#include "boot_args.h"
|
2017-06-20 15:32:21 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "flash.h"
|
2017-10-16 16:03:04 +00:00
|
|
|
#include "image.h"
|
2017-10-26 21:51:39 +00:00
|
|
|
#include "secbool.h"
|
2023-07-20 11:20:50 +00:00
|
|
|
#include "secret.h"
|
2023-04-14 15:18:03 +00:00
|
|
|
#include "unit_variant.h"
|
2017-04-12 16:31:23 +00:00
|
|
|
#include "usb.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
2017-12-17 00:09:45 +00:00
|
|
|
#include "bootui.h"
|
2017-04-12 16:31:23 +00:00
|
|
|
#include "messages.h"
|
2022-05-05 11:47:19 +00:00
|
|
|
#include "rust_ui.h"
|
2017-04-12 16:31:23 +00:00
|
|
|
|
2019-01-23 16:16:46 +00:00
|
|
|
#include "memzero.h"
|
2023-06-01 12:21:51 +00:00
|
|
|
#include "model.h"
|
2019-01-23 16:16:46 +00:00
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
#ifdef TREZOR_EMULATOR
|
|
|
|
#include "emulator.h"
|
|
|
|
#endif
|
|
|
|
|
2023-09-13 13:36:06 +00:00
|
|
|
#if USE_OPTIGA
|
|
|
|
#include "secret.h"
|
|
|
|
#endif
|
|
|
|
|
2017-10-16 16:03:04 +00:00
|
|
|
#define MSG_HEADER1_LEN 9
|
|
|
|
#define MSG_HEADER2_LEN 1
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
secbool msg_parse_header(const uint8_t *buf, uint16_t *msg_id,
|
|
|
|
uint32_t *msg_size) {
|
|
|
|
if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
*msg_id = (buf[3] << 8) + buf[4];
|
|
|
|
*msg_size = (buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
|
|
|
|
return sectrue;
|
2017-06-14 18:40:31 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 21:18:00 +00:00
|
|
|
typedef struct {
|
2019-03-29 15:26:02 +00:00
|
|
|
uint8_t iface_num;
|
|
|
|
uint8_t packet_index;
|
|
|
|
uint8_t packet_pos;
|
|
|
|
uint8_t buf[USB_PACKET_SIZE];
|
2017-06-14 21:18:00 +00:00
|
|
|
} usb_write_state;
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
/* we don't use secbool/sectrue/secfalse here as it is a nanopb api */
|
2019-03-29 15:26:02 +00:00
|
|
|
static bool _usb_write(pb_ostream_t *stream, const pb_byte_t *buf,
|
|
|
|
size_t count) {
|
|
|
|
usb_write_state *state = (usb_write_state *)(stream->state);
|
|
|
|
|
|
|
|
size_t written = 0;
|
|
|
|
// while we have data left
|
|
|
|
while (written < count) {
|
|
|
|
size_t remaining = count - written;
|
|
|
|
// if all remaining data fit into our packet
|
|
|
|
if (state->packet_pos + remaining <= USB_PACKET_SIZE) {
|
|
|
|
// append data from buf to state->buf
|
|
|
|
memcpy(state->buf + state->packet_pos, buf + written, remaining);
|
|
|
|
// advance position
|
|
|
|
state->packet_pos += remaining;
|
|
|
|
// and return
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// append data that fits
|
|
|
|
memcpy(state->buf + state->packet_pos, buf + written,
|
|
|
|
USB_PACKET_SIZE - state->packet_pos);
|
|
|
|
written += USB_PACKET_SIZE - state->packet_pos;
|
|
|
|
// send packet
|
|
|
|
int r = usb_webusb_write_blocking(state->iface_num, state->buf,
|
|
|
|
USB_PACKET_SIZE, USB_TIMEOUT);
|
|
|
|
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
|
|
|
|
// prepare new packet
|
|
|
|
state->packet_index++;
|
|
|
|
memzero(state->buf, USB_PACKET_SIZE);
|
|
|
|
state->buf[0] = '?';
|
|
|
|
state->packet_pos = MSG_HEADER2_LEN;
|
2017-06-14 18:40:31 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
2017-06-14 21:18:00 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return true;
|
2017-06-14 21:18:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static void _usb_write_flush(usb_write_state *state) {
|
|
|
|
// if packet is not filled up completely
|
|
|
|
if (state->packet_pos < USB_PACKET_SIZE) {
|
|
|
|
// pad it with zeroes
|
|
|
|
memzero(state->buf + state->packet_pos,
|
|
|
|
USB_PACKET_SIZE - state->packet_pos);
|
|
|
|
}
|
|
|
|
// send packet
|
|
|
|
int r = usb_webusb_write_blocking(state->iface_num, state->buf,
|
|
|
|
USB_PACKET_SIZE, USB_TIMEOUT);
|
|
|
|
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
|
2017-06-14 18:40:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static secbool _send_msg(uint8_t iface_num, uint16_t msg_id,
|
2019-12-23 15:33:34 +00:00
|
|
|
const pb_msgdesc_t *fields, const void *msg) {
|
2019-03-29 15:26:02 +00:00
|
|
|
// determine message size by serializing it into a dummy stream
|
|
|
|
pb_ostream_t sizestream = {.callback = NULL,
|
|
|
|
.state = NULL,
|
|
|
|
.max_size = SIZE_MAX,
|
|
|
|
.bytes_written = 0,
|
|
|
|
.errmsg = NULL};
|
|
|
|
if (false == pb_encode(&sizestream, fields, msg)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
const uint32_t msg_size = sizestream.bytes_written;
|
|
|
|
|
|
|
|
usb_write_state state = {
|
|
|
|
.iface_num = iface_num,
|
|
|
|
.packet_index = 0,
|
|
|
|
.packet_pos = MSG_HEADER1_LEN,
|
|
|
|
.buf =
|
|
|
|
{
|
|
|
|
'?',
|
|
|
|
'#',
|
|
|
|
'#',
|
|
|
|
(msg_id >> 8) & 0xFF,
|
|
|
|
msg_id & 0xFF,
|
|
|
|
(msg_size >> 24) & 0xFF,
|
|
|
|
(msg_size >> 16) & 0xFF,
|
|
|
|
(msg_size >> 8) & 0xFF,
|
|
|
|
msg_size & 0xFF,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
pb_ostream_t stream = {.callback = &_usb_write,
|
|
|
|
.state = &state,
|
|
|
|
.max_size = SIZE_MAX,
|
|
|
|
.bytes_written = 0,
|
|
|
|
.errmsg = NULL};
|
|
|
|
|
|
|
|
if (false == pb_encode(&stream, fields, msg)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
_usb_write_flush(&state);
|
|
|
|
|
|
|
|
return sectrue;
|
2017-06-14 18:40:31 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 12:20:09 +00:00
|
|
|
#define MSG_SEND_INIT(TYPE) TYPE msg_send = TYPE##_init_default
|
2021-02-09 15:10:23 +00:00
|
|
|
#define MSG_SEND_ASSIGN_REQUIRED_VALUE(FIELD, VALUE) \
|
|
|
|
{ msg_send.FIELD = VALUE; }
|
2019-03-29 15:26:02 +00:00
|
|
|
#define MSG_SEND_ASSIGN_VALUE(FIELD, VALUE) \
|
|
|
|
{ \
|
|
|
|
msg_send.has_##FIELD = true; \
|
|
|
|
msg_send.FIELD = VALUE; \
|
|
|
|
}
|
|
|
|
#define MSG_SEND_ASSIGN_STRING(FIELD, VALUE) \
|
|
|
|
{ \
|
|
|
|
msg_send.has_##FIELD = true; \
|
|
|
|
memzero(msg_send.FIELD, sizeof(msg_send.FIELD)); \
|
|
|
|
strncpy(msg_send.FIELD, VALUE, sizeof(msg_send.FIELD) - 1); \
|
|
|
|
}
|
|
|
|
#define MSG_SEND_ASSIGN_STRING_LEN(FIELD, VALUE, LEN) \
|
|
|
|
{ \
|
|
|
|
msg_send.has_##FIELD = true; \
|
|
|
|
memzero(msg_send.FIELD, sizeof(msg_send.FIELD)); \
|
|
|
|
strncpy(msg_send.FIELD, VALUE, MIN(LEN, sizeof(msg_send.FIELD) - 1)); \
|
|
|
|
}
|
|
|
|
#define MSG_SEND_ASSIGN_BYTES(FIELD, VALUE, LEN) \
|
|
|
|
{ \
|
|
|
|
msg_send.has_##FIELD = true; \
|
|
|
|
memzero(msg_send.FIELD.bytes, sizeof(msg_send.FIELD.bytes)); \
|
|
|
|
memcpy(msg_send.FIELD.bytes, VALUE, \
|
|
|
|
MIN(LEN, sizeof(msg_send.FIELD.bytes))); \
|
|
|
|
msg_send.FIELD.size = MIN(LEN, sizeof(msg_send.FIELD.bytes)); \
|
|
|
|
}
|
|
|
|
#define MSG_SEND(TYPE) \
|
|
|
|
_send_msg(iface_num, MessageType_MessageType_##TYPE, TYPE##_fields, &msg_send)
|
2017-06-19 12:20:09 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2019-03-29 15:26:02 +00:00
|
|
|
uint8_t iface_num;
|
|
|
|
uint8_t packet_index;
|
|
|
|
uint8_t packet_pos;
|
|
|
|
uint8_t *buf;
|
2017-06-19 12:20:09 +00:00
|
|
|
} usb_read_state;
|
2017-06-14 18:40:31 +00:00
|
|
|
|
2019-11-10 18:42:32 +00:00
|
|
|
static void _usb_webusb_read_retry(uint8_t iface_num, uint8_t *buf) {
|
|
|
|
for (int retry = 0;; retry++) {
|
|
|
|
int r =
|
|
|
|
usb_webusb_read_blocking(iface_num, buf, USB_PACKET_SIZE, USB_TIMEOUT);
|
|
|
|
if (r != USB_PACKET_SIZE) { // reading failed
|
|
|
|
if (r == 0 && retry < 10) {
|
|
|
|
// only timeout => let's try again
|
2023-03-21 08:43:35 +00:00
|
|
|
continue;
|
2019-11-10 18:42:32 +00:00
|
|
|
} else {
|
|
|
|
// error
|
2022-05-05 11:47:19 +00:00
|
|
|
error_shutdown("USB ERROR",
|
|
|
|
"Error reading from USB. Try different USB cable.");
|
2019-11-10 18:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return; // success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
/* we don't use secbool/sectrue/secfalse here as it is a nanopb api */
|
2019-03-29 15:26:02 +00:00
|
|
|
static bool _usb_read(pb_istream_t *stream, uint8_t *buf, size_t count) {
|
|
|
|
usb_read_state *state = (usb_read_state *)(stream->state);
|
|
|
|
|
|
|
|
size_t read = 0;
|
|
|
|
// while we have data left
|
|
|
|
while (read < count) {
|
|
|
|
size_t remaining = count - read;
|
|
|
|
// if all remaining data fit into our packet
|
|
|
|
if (state->packet_pos + remaining <= USB_PACKET_SIZE) {
|
|
|
|
// append data from buf to state->buf
|
|
|
|
memcpy(buf + read, state->buf + state->packet_pos, remaining);
|
|
|
|
// advance position
|
|
|
|
state->packet_pos += remaining;
|
|
|
|
// and return
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// append data that fits
|
|
|
|
memcpy(buf + read, state->buf + state->packet_pos,
|
|
|
|
USB_PACKET_SIZE - state->packet_pos);
|
|
|
|
read += USB_PACKET_SIZE - state->packet_pos;
|
2019-11-10 18:42:32 +00:00
|
|
|
// read next packet (with retry)
|
|
|
|
_usb_webusb_read_retry(state->iface_num, state->buf);
|
2019-03-29 15:26:02 +00:00
|
|
|
// prepare next packet
|
|
|
|
state->packet_index++;
|
|
|
|
state->packet_pos = MSG_HEADER2_LEN;
|
2017-06-16 13:40:27 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
2017-06-19 12:20:09 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return true;
|
2017-06-19 12:20:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static void _usb_read_flush(usb_read_state *state) { (void)state; }
|
2017-06-19 12:20:09 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static secbool _recv_msg(uint8_t iface_num, uint32_t msg_size, uint8_t *buf,
|
2019-12-23 15:33:34 +00:00
|
|
|
const pb_msgdesc_t *fields, void *msg) {
|
2019-03-29 15:26:02 +00:00
|
|
|
usb_read_state state = {.iface_num = iface_num,
|
|
|
|
.packet_index = 0,
|
|
|
|
.packet_pos = MSG_HEADER1_LEN,
|
|
|
|
.buf = buf};
|
|
|
|
|
|
|
|
pb_istream_t stream = {.callback = &_usb_read,
|
|
|
|
.state = &state,
|
|
|
|
.bytes_left = msg_size,
|
|
|
|
.errmsg = NULL};
|
2017-06-19 12:20:09 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (false == pb_decode_noinit(&stream, fields, msg)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2017-06-19 12:20:09 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
_usb_read_flush(&state);
|
|
|
|
|
|
|
|
return sectrue;
|
2017-06-16 13:40:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 21:05:21 +00:00
|
|
|
#define MSG_RECV_INIT(TYPE) TYPE msg_recv = TYPE##_init_default
|
2020-02-06 09:12:45 +00:00
|
|
|
#define MSG_RECV_CALLBACK(FIELD, CALLBACK, ARGUMENT) \
|
|
|
|
{ \
|
|
|
|
msg_recv.FIELD.funcs.decode = &CALLBACK; \
|
|
|
|
msg_recv.FIELD.arg = (void *)ARGUMENT; \
|
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
#define MSG_RECV(TYPE) \
|
|
|
|
_recv_msg(iface_num, msg_size, buf, TYPE##_fields, &msg_recv)
|
|
|
|
|
|
|
|
void send_user_abort(uint8_t iface_num, const char *msg) {
|
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ActionCancelled);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, msg);
|
|
|
|
MSG_SEND(Failure);
|
2018-01-24 16:00:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static void send_msg_features(uint8_t iface_num,
|
|
|
|
const vendor_header *const vhdr,
|
|
|
|
const image_header *const hdr) {
|
|
|
|
MSG_SEND_INIT(Features);
|
|
|
|
MSG_SEND_ASSIGN_STRING(vendor, "trezor.io");
|
2021-02-09 15:10:23 +00:00
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(major_version, VERSION_MAJOR);
|
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(minor_version, VERSION_MINOR);
|
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(patch_version, VERSION_PATCH);
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND_ASSIGN_VALUE(bootloader_mode, true);
|
2023-06-01 12:21:51 +00:00
|
|
|
MSG_SEND_ASSIGN_STRING(model, MODEL_NAME);
|
|
|
|
MSG_SEND_ASSIGN_STRING(internal_model, MODEL_INTERNAL_NAME);
|
2019-03-29 15:26:02 +00:00
|
|
|
if (vhdr && hdr) {
|
|
|
|
MSG_SEND_ASSIGN_VALUE(firmware_present, true);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(fw_major, (hdr->version & 0xFF));
|
|
|
|
MSG_SEND_ASSIGN_VALUE(fw_minor, ((hdr->version >> 8) & 0xFF));
|
|
|
|
MSG_SEND_ASSIGN_VALUE(fw_patch, ((hdr->version >> 16) & 0xFF));
|
|
|
|
MSG_SEND_ASSIGN_STRING_LEN(fw_vendor, vhdr->vstr, vhdr->vstr_len);
|
|
|
|
} else {
|
|
|
|
MSG_SEND_ASSIGN_VALUE(firmware_present, false);
|
|
|
|
}
|
2023-04-14 15:18:03 +00:00
|
|
|
if (unit_variant_present()) {
|
|
|
|
MSG_SEND_ASSIGN_VALUE(unit_color, unit_variant_get_color());
|
|
|
|
MSG_SEND_ASSIGN_VALUE(unit_btconly, unit_variant_get_btconly());
|
|
|
|
}
|
2023-09-13 13:36:06 +00:00
|
|
|
|
|
|
|
#if USE_OPTIGA
|
|
|
|
MSG_SEND_ASSIGN_VALUE(bootloader_locked,
|
|
|
|
(secret_bootloader_locked() == sectrue));
|
|
|
|
#endif
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND(Features);
|
2017-06-14 18:40:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void process_msg_Initialize(uint8_t iface_num, uint32_t msg_size, uint8_t *buf,
|
|
|
|
const vendor_header *const vhdr,
|
|
|
|
const image_header *const hdr) {
|
|
|
|
MSG_RECV_INIT(Initialize);
|
|
|
|
MSG_RECV(Initialize);
|
|
|
|
send_msg_features(iface_num, vhdr, hdr);
|
2018-02-06 16:06:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void process_msg_GetFeatures(uint8_t iface_num, uint32_t msg_size, uint8_t *buf,
|
|
|
|
const vendor_header *const vhdr,
|
|
|
|
const image_header *const hdr) {
|
|
|
|
MSG_RECV_INIT(GetFeatures);
|
|
|
|
MSG_RECV(GetFeatures);
|
|
|
|
send_msg_features(iface_num, vhdr, hdr);
|
2018-02-06 16:06:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void process_msg_Ping(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) {
|
|
|
|
MSG_RECV_INIT(Ping);
|
|
|
|
MSG_RECV(Ping);
|
2017-06-16 13:40:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND_INIT(Success);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, msg_recv.message);
|
|
|
|
MSG_SEND(Success);
|
2017-06-14 18:40:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:36:37 +00:00
|
|
|
static uint32_t firmware_remaining;
|
|
|
|
static uint32_t firmware_block;
|
|
|
|
static uint32_t chunk_requested;
|
|
|
|
static uint32_t erase_offset;
|
2017-06-20 17:53:24 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size,
|
|
|
|
uint8_t *buf) {
|
|
|
|
firmware_remaining = 0;
|
|
|
|
firmware_block = 0;
|
|
|
|
chunk_requested = 0;
|
2023-10-24 14:36:37 +00:00
|
|
|
erase_offset = 0;
|
2019-03-29 15:26:02 +00:00
|
|
|
|
|
|
|
MSG_RECV_INIT(FirmwareErase);
|
|
|
|
MSG_RECV(FirmwareErase);
|
|
|
|
|
|
|
|
firmware_remaining = msg_recv.has_length ? msg_recv.length : 0;
|
|
|
|
if ((firmware_remaining > 0) &&
|
|
|
|
((firmware_remaining % sizeof(uint32_t)) == 0) &&
|
2023-06-30 10:12:55 +00:00
|
|
|
(firmware_remaining <= FIRMWARE_IMAGE_MAXSIZE)) {
|
2019-03-29 15:26:02 +00:00
|
|
|
// request new firmware
|
2020-02-06 09:12:45 +00:00
|
|
|
chunk_requested = (firmware_remaining > IMAGE_INIT_CHUNK_SIZE)
|
|
|
|
? IMAGE_INIT_CHUNK_SIZE
|
2019-03-29 15:26:02 +00:00
|
|
|
: firmware_remaining;
|
|
|
|
MSG_SEND_INIT(FirmwareRequest);
|
2023-03-31 12:34:20 +00:00
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(offset, 0);
|
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(length, chunk_requested);
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND(FirmwareRequest);
|
|
|
|
} else {
|
|
|
|
// invalid firmware size
|
|
|
|
MSG_SEND_INIT(Failure);
|
2020-11-18 10:21:57 +00:00
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Wrong firmware size");
|
|
|
|
MSG_SEND(Failure);
|
|
|
|
}
|
2017-06-20 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t chunk_size = 0;
|
2022-05-31 07:31:32 +00:00
|
|
|
|
|
|
|
__attribute__((section(".buf"))) uint32_t chunk_buffer[IMAGE_CHUNK_SIZE / 4];
|
|
|
|
|
2023-10-31 09:34:54 +00:00
|
|
|
#define CHUNK_BUFFER_PTR ((const uint8_t *const)&chunk_buffer)
|
2017-06-20 10:43:29 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
/* we don't use secbool/sectrue/secfalse here as it is a nanopb api */
|
2019-03-29 15:26:02 +00:00
|
|
|
static bool _read_payload(pb_istream_t *stream, const pb_field_t *field,
|
|
|
|
void **arg) {
|
2017-10-27 02:20:03 +00:00
|
|
|
#define BUFSIZE 32768
|
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
size_t offset = (size_t)(*arg);
|
2020-02-06 09:12:45 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (stream->bytes_left > IMAGE_CHUNK_SIZE) {
|
|
|
|
chunk_size = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-06 09:12:45 +00:00
|
|
|
if (offset == 0) {
|
|
|
|
// clear chunk buffer
|
2022-05-31 07:31:32 +00:00
|
|
|
memset((uint8_t *)&chunk_buffer, 0xFF, IMAGE_CHUNK_SIZE);
|
2020-02-06 09:12:45 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
|
2020-02-06 09:12:45 +00:00
|
|
|
uint32_t chunk_written = offset;
|
|
|
|
chunk_size = offset + stream->bytes_left;
|
2017-10-27 02:20:03 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
while (stream->bytes_left) {
|
|
|
|
// update loader but skip first block
|
|
|
|
if (firmware_block > 0) {
|
|
|
|
ui_screen_install_progress_upload(
|
2023-10-24 14:36:37 +00:00
|
|
|
1000 * (firmware_block * IMAGE_CHUNK_SIZE + chunk_written) /
|
|
|
|
(firmware_block * IMAGE_CHUNK_SIZE + firmware_remaining));
|
2017-06-20 10:43:29 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
// read data
|
|
|
|
if (!pb_read(
|
2022-05-31 07:31:32 +00:00
|
|
|
stream, (pb_byte_t *)(CHUNK_BUFFER_PTR + chunk_written),
|
2019-03-29 15:26:02 +00:00
|
|
|
(stream->bytes_left > BUFSIZE) ? BUFSIZE : stream->bytes_left)) {
|
|
|
|
chunk_size = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
chunk_written += BUFSIZE;
|
|
|
|
}
|
2017-10-27 02:20:03 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return true;
|
2017-06-14 18:40:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static int version_compare(uint32_t vera, uint32_t verb) {
|
2023-10-27 13:02:20 +00:00
|
|
|
/* Explicit casts so that we control how compiler does the unsigned shift
|
|
|
|
* and correctly then promote uint8_t to int without possibility of
|
|
|
|
* having implementation-defined right shift on negative int
|
|
|
|
* in case compiler promoted the wrong unsinged int
|
|
|
|
*/
|
2019-03-29 15:26:02 +00:00
|
|
|
int a, b;
|
2023-10-27 13:02:20 +00:00
|
|
|
a = (uint8_t)vera & 0xFF;
|
|
|
|
b = (uint8_t)verb & 0xFF;
|
2019-03-29 15:26:02 +00:00
|
|
|
if (a != b) return a - b;
|
2023-10-27 13:02:20 +00:00
|
|
|
a = (uint8_t)(vera >> 8) & 0xFF;
|
|
|
|
b = (uint8_t)(verb >> 8) & 0xFF;
|
2019-03-29 15:26:02 +00:00
|
|
|
if (a != b) return a - b;
|
2023-10-27 13:02:20 +00:00
|
|
|
a = (uint8_t)(vera >> 16) & 0xFF;
|
|
|
|
b = (uint8_t)(verb >> 16) & 0xFF;
|
2019-03-29 15:26:02 +00:00
|
|
|
if (a != b) return a - b;
|
2023-10-27 13:02:20 +00:00
|
|
|
a = (uint8_t)(vera >> 24) & 0xFF;
|
|
|
|
b = (uint8_t)(verb >> 24) & 0xFF;
|
2019-03-29 15:26:02 +00:00
|
|
|
return a - b;
|
2017-12-14 20:58:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 13:12:49 +00:00
|
|
|
static void detect_installation(const vendor_header *current_vhdr,
|
|
|
|
const image_header *current_hdr,
|
2019-03-29 15:26:02 +00:00
|
|
|
const vendor_header *const new_vhdr,
|
|
|
|
const image_header *const new_hdr,
|
2023-10-20 12:58:32 +00:00
|
|
|
secbool *is_new, secbool *keep_seed,
|
|
|
|
secbool *is_newvendor, secbool *is_upgrade) {
|
2019-03-29 15:26:02 +00:00
|
|
|
*is_new = secfalse;
|
2023-10-20 12:58:32 +00:00
|
|
|
*keep_seed = secfalse;
|
2023-03-30 13:43:49 +00:00
|
|
|
*is_newvendor = secfalse;
|
2023-10-20 12:58:32 +00:00
|
|
|
*is_upgrade = secfalse;
|
2022-11-21 13:12:49 +00:00
|
|
|
if (sectrue != check_vendor_header_keys(current_vhdr)) {
|
2019-03-29 15:26:02 +00:00
|
|
|
*is_new = sectrue;
|
|
|
|
return;
|
|
|
|
}
|
2022-11-21 13:12:49 +00:00
|
|
|
if (sectrue != check_image_model(current_hdr)) {
|
|
|
|
*is_new = sectrue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sectrue != check_image_header_sig(current_hdr, current_vhdr->vsig_m,
|
|
|
|
current_vhdr->vsig_n,
|
|
|
|
current_vhdr->vpub)) {
|
2019-03-29 15:26:02 +00:00
|
|
|
*is_new = sectrue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t hash1[32], hash2[32];
|
2021-05-05 15:24:30 +00:00
|
|
|
vendor_header_hash(new_vhdr, hash1);
|
|
|
|
vendor_header_hash(current_vhdr, hash2);
|
2019-03-29 15:26:02 +00:00
|
|
|
if (0 != memcmp(hash1, hash2, 32)) {
|
2023-03-30 13:43:49 +00:00
|
|
|
*is_newvendor = sectrue;
|
2019-03-29 15:26:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (version_compare(new_hdr->version, current_hdr->fix_version) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-20 12:58:32 +00:00
|
|
|
if (version_compare(new_hdr->version, current_hdr->version) > 0) {
|
|
|
|
*is_upgrade = sectrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*keep_seed = sectrue;
|
2017-10-27 16:45:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 17:36:03 +00:00
|
|
|
static int firmware_upload_chunk_retry = FIRMWARE_UPLOAD_CHUNK_RETRY_COUNT;
|
2023-03-23 14:42:21 +00:00
|
|
|
static size_t headers_offset = 0;
|
|
|
|
static size_t read_offset = 0;
|
2018-06-11 17:36:03 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size,
|
|
|
|
uint8_t *buf) {
|
|
|
|
MSG_RECV_INIT(FirmwareUpload);
|
2020-02-06 09:12:45 +00:00
|
|
|
MSG_RECV_CALLBACK(payload, _read_payload, read_offset);
|
2021-03-04 10:59:38 +00:00
|
|
|
const secbool r = MSG_RECV(FirmwareUpload);
|
2017-10-27 02:49:59 +00:00
|
|
|
|
2020-02-06 09:12:45 +00:00
|
|
|
if (sectrue != r || chunk_size != (chunk_requested + read_offset)) {
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND_INIT(Failure);
|
2020-11-18 10:21:57 +00:00
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Invalid chunk size");
|
|
|
|
MSG_SEND(Failure);
|
2022-11-21 13:12:49 +00:00
|
|
|
return UPLOAD_ERR_INVALID_CHUNK_SIZE;
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static image_header hdr;
|
|
|
|
|
|
|
|
if (firmware_block == 0) {
|
2020-02-06 09:12:45 +00:00
|
|
|
if (headers_offset == 0) {
|
|
|
|
// first block and headers are not yet parsed
|
|
|
|
vendor_header vhdr;
|
2022-11-21 13:12:49 +00:00
|
|
|
|
|
|
|
if (sectrue != read_vendor_header(CHUNK_BUFFER_PTR, &vhdr)) {
|
2020-02-06 09:12:45 +00:00
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Invalid vendor header");
|
|
|
|
MSG_SEND(Failure);
|
2022-11-21 13:12:49 +00:00
|
|
|
return UPLOAD_ERR_INVALID_VENDOR_HEADER;
|
2020-02-06 09:12:45 +00:00
|
|
|
}
|
2022-11-21 13:12:49 +00:00
|
|
|
|
|
|
|
if (sectrue != check_vendor_header_keys(&vhdr)) {
|
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Invalid vendor header signature");
|
|
|
|
MSG_SEND(Failure);
|
|
|
|
return UPLOAD_ERR_INVALID_VENDOR_HEADER_SIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
const image_header *received_hdr =
|
|
|
|
read_image_header(CHUNK_BUFFER_PTR + vhdr.hdrlen,
|
|
|
|
FIRMWARE_IMAGE_MAGIC, FIRMWARE_IMAGE_MAXSIZE);
|
|
|
|
|
|
|
|
if (received_hdr !=
|
|
|
|
(const image_header *)(CHUNK_BUFFER_PTR + vhdr.hdrlen)) {
|
2020-02-06 09:12:45 +00:00
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Invalid firmware header");
|
|
|
|
MSG_SEND(Failure);
|
2022-11-21 13:12:49 +00:00
|
|
|
return UPLOAD_ERR_INVALID_IMAGE_HEADER;
|
2020-02-06 09:12:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 13:12:49 +00:00
|
|
|
if (sectrue != check_image_model(received_hdr)) {
|
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Wrong firmware model");
|
|
|
|
MSG_SEND(Failure);
|
|
|
|
return UPLOAD_ERR_INVALID_IMAGE_MODEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sectrue != check_image_header_sig(received_hdr, vhdr.vsig_m,
|
|
|
|
vhdr.vsig_n, vhdr.vpub)) {
|
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Invalid firmware signature");
|
|
|
|
MSG_SEND(Failure);
|
|
|
|
return UPLOAD_ERR_INVALID_IMAGE_HEADER_SIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&hdr, received_hdr, sizeof(hdr));
|
|
|
|
|
2020-02-06 09:12:45 +00:00
|
|
|
vendor_header current_vhdr;
|
2022-11-21 13:12:49 +00:00
|
|
|
|
2020-02-06 09:12:45 +00:00
|
|
|
secbool is_new = secfalse;
|
2022-11-21 13:12:49 +00:00
|
|
|
|
|
|
|
if (sectrue !=
|
|
|
|
read_vendor_header((const uint8_t *)FIRMWARE_START, ¤t_vhdr)) {
|
|
|
|
is_new = sectrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const image_header *current_hdr = NULL;
|
|
|
|
|
|
|
|
if (is_new == secfalse) {
|
|
|
|
current_hdr = read_image_header(
|
|
|
|
(const uint8_t *)FIRMWARE_START + current_vhdr.hdrlen,
|
|
|
|
FIRMWARE_IMAGE_MAGIC, FIRMWARE_IMAGE_MAXSIZE);
|
|
|
|
|
|
|
|
if (current_hdr !=
|
2023-01-25 16:59:05 +00:00
|
|
|
(const image_header *)(FIRMWARE_START + current_vhdr.hdrlen)) {
|
2022-11-21 13:12:49 +00:00
|
|
|
is_new = sectrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 13:51:32 +00:00
|
|
|
secbool should_keep_seed = secfalse;
|
|
|
|
secbool is_newvendor = secfalse;
|
2023-10-20 12:58:32 +00:00
|
|
|
secbool is_upgrade = secfalse;
|
2022-11-21 13:12:49 +00:00
|
|
|
if (is_new == secfalse) {
|
|
|
|
detect_installation(¤t_vhdr, current_hdr, &vhdr, &hdr, &is_new,
|
2023-10-20 12:58:32 +00:00
|
|
|
&should_keep_seed, &is_newvendor, &is_upgrade);
|
|
|
|
}
|
|
|
|
|
|
|
|
secbool is_ilu = secfalse; // interaction-less update
|
|
|
|
|
2023-10-31 11:38:11 +00:00
|
|
|
if (bootargs_get_command() == BOOT_COMMAND_INSTALL_UPGRADE) {
|
2023-12-15 22:50:33 +00:00
|
|
|
IMAGE_HASH_CTX ctx;
|
|
|
|
uint8_t hash[IMAGE_HASH_DIGEST_LENGTH];
|
|
|
|
IMAGE_HASH_INIT(&ctx);
|
|
|
|
IMAGE_HASH_UPDATE(&ctx, CHUNK_BUFFER_PTR,
|
|
|
|
vhdr.hdrlen + received_hdr->hdrlen);
|
|
|
|
IMAGE_HASH_FINAL(&ctx, hash);
|
2023-10-20 12:58:32 +00:00
|
|
|
|
|
|
|
// the firmware must be the same as confirmed by the user
|
2023-10-31 11:38:11 +00:00
|
|
|
if (memcmp(bootargs_get_args()->hash, hash, sizeof(hash)) != 0) {
|
2023-10-20 12:58:32 +00:00
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Firmware mismatch");
|
|
|
|
MSG_SEND(Failure);
|
|
|
|
return UPLOAD_ERR_FIRMWARE_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the firmware must be from the same vendor
|
|
|
|
// the firmware must be newer
|
|
|
|
if (is_upgrade != sectrue || is_newvendor != secfalse) {
|
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Not a firmware upgrade");
|
|
|
|
MSG_SEND(Failure);
|
|
|
|
return UPLOAD_ERR_NOT_FIRMWARE_UPGRADE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((vhdr.vtrust & VTRUST_ALL) != VTRUST_ALL) {
|
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Not a full-trust image");
|
|
|
|
MSG_SEND(Failure);
|
|
|
|
return UPLOAD_ERR_NOT_FULLTRUST_IMAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// upload the firmware without confirmation
|
|
|
|
is_ilu = sectrue;
|
2022-11-21 13:12:49 +00:00
|
|
|
}
|
2020-02-06 09:12:45 +00:00
|
|
|
|
2023-06-28 08:51:30 +00:00
|
|
|
#if defined USE_OPTIGA && !defined STM32U5
|
2023-07-20 11:20:50 +00:00
|
|
|
if (sectrue != secret_wiped() && ((vhdr.vtrust & VTRUST_SECRET) != 0)) {
|
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
2023-08-24 17:16:09 +00:00
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Install restricted");
|
2023-07-20 11:20:50 +00:00
|
|
|
MSG_SEND(Failure);
|
2023-08-24 17:16:09 +00:00
|
|
|
return UPLOAD_ERR_BOOTLOADER_LOCKED;
|
2023-07-20 11:20:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-05 11:47:19 +00:00
|
|
|
uint32_t response = INPUT_CANCEL;
|
2023-10-20 12:58:32 +00:00
|
|
|
if (sectrue == is_new || sectrue == is_ilu) {
|
|
|
|
// new installation or interaction less updated - auto confirm
|
2020-02-06 09:12:45 +00:00
|
|
|
response = INPUT_CONFIRM;
|
|
|
|
} else {
|
2023-03-30 13:43:49 +00:00
|
|
|
int version_cmp = version_compare(hdr.version, current_hdr->version);
|
|
|
|
response = ui_screen_install_confirm(&vhdr, &hdr, should_keep_seed,
|
|
|
|
is_newvendor, version_cmp);
|
2020-02-06 09:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (INPUT_CANCEL == response) {
|
|
|
|
send_user_abort(iface_num, "Firmware install cancelled");
|
2022-11-21 13:12:49 +00:00
|
|
|
return UPLOAD_ERR_USER_ABORT;
|
2020-02-06 09:12:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 11:47:19 +00:00
|
|
|
ui_screen_install_start();
|
|
|
|
|
2023-03-31 13:51:32 +00:00
|
|
|
// if firmware is not upgrade, erase storage
|
|
|
|
if (sectrue != should_keep_seed) {
|
2023-06-28 08:51:30 +00:00
|
|
|
#ifdef STM32U5
|
|
|
|
secret_bhk_regenerate();
|
|
|
|
#endif
|
2023-06-30 10:12:55 +00:00
|
|
|
ensure(flash_area_erase_bulk(STORAGE_AREAS, STORAGE_AREAS_COUNT, NULL),
|
|
|
|
NULL);
|
2023-03-31 13:51:32 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 09:12:45 +00:00
|
|
|
headers_offset = IMAGE_HEADER_SIZE + vhdr.hdrlen;
|
|
|
|
read_offset = IMAGE_INIT_CHUNK_SIZE;
|
|
|
|
|
|
|
|
// request the rest of the first chunk
|
|
|
|
MSG_SEND_INIT(FirmwareRequest);
|
2023-04-07 11:27:57 +00:00
|
|
|
uint32_t chunk_limit = (firmware_remaining > IMAGE_CHUNK_SIZE)
|
|
|
|
? IMAGE_CHUNK_SIZE
|
|
|
|
: firmware_remaining;
|
|
|
|
chunk_requested = chunk_limit - read_offset;
|
2023-03-31 12:34:20 +00:00
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(offset, read_offset);
|
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(length, chunk_requested);
|
2020-02-06 09:12:45 +00:00
|
|
|
MSG_SEND(FirmwareRequest);
|
2018-01-23 11:50:25 +00:00
|
|
|
|
2020-02-06 09:12:45 +00:00
|
|
|
firmware_remaining -= read_offset;
|
|
|
|
return (int)firmware_remaining;
|
2019-03-29 15:26:02 +00:00
|
|
|
} else {
|
2020-02-06 09:12:45 +00:00
|
|
|
// first block with the headers parsed -> the first chunk is now complete
|
|
|
|
read_offset = 0;
|
2017-06-20 10:43:29 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
2018-02-10 14:07:55 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
// should not happen, but double-check
|
2023-06-30 10:12:55 +00:00
|
|
|
if (flash_area_get_address(&FIRMWARE_AREA, firmware_block * IMAGE_CHUNK_SIZE,
|
|
|
|
0) == NULL) {
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Firmware too big");
|
|
|
|
MSG_SEND(Failure);
|
2022-11-21 13:12:49 +00:00
|
|
|
return UPLOAD_ERR_FIRMWARE_TOO_BIG;
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sectrue != check_single_hash(hdr.hashes + firmware_block * 32,
|
2022-05-31 07:31:32 +00:00
|
|
|
CHUNK_BUFFER_PTR + headers_offset,
|
2020-02-06 09:12:45 +00:00
|
|
|
chunk_size - headers_offset)) {
|
2019-03-29 15:26:02 +00:00
|
|
|
if (firmware_upload_chunk_retry > 0) {
|
|
|
|
--firmware_upload_chunk_retry;
|
|
|
|
MSG_SEND_INIT(FirmwareRequest);
|
2023-03-31 12:34:20 +00:00
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(offset, firmware_block * IMAGE_CHUNK_SIZE);
|
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(length, chunk_requested);
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND(FirmwareRequest);
|
|
|
|
return (int)firmware_remaining;
|
2018-02-10 14:07:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Invalid chunk hash");
|
|
|
|
MSG_SEND(Failure);
|
2022-11-21 13:12:49 +00:00
|
|
|
return UPLOAD_ERR_INVALID_CHUNK_HASH;
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:36:37 +00:00
|
|
|
// buffer with the received data
|
2023-12-26 13:33:11 +00:00
|
|
|
const uint32_t *src = (const uint32_t *)CHUNK_BUFFER_PTR;
|
2023-10-24 14:36:37 +00:00
|
|
|
// number of received bytes
|
|
|
|
uint32_t bytes_remaining = chunk_size;
|
2023-10-27 13:07:04 +00:00
|
|
|
// offset into the FIRMWARE_AREA part of the flash
|
2023-10-24 14:36:37 +00:00
|
|
|
uint32_t write_offset = firmware_block * IMAGE_CHUNK_SIZE;
|
|
|
|
|
2024-02-26 13:56:30 +00:00
|
|
|
ensure((chunk_size % FLASH_BURST_SIZE == 0) * sectrue, NULL);
|
2023-12-26 13:33:11 +00:00
|
|
|
|
2023-10-24 14:36:37 +00:00
|
|
|
while (bytes_remaining > 0) {
|
|
|
|
// erase flash before writing
|
2023-10-27 13:07:04 +00:00
|
|
|
uint32_t bytes_erased = 0;
|
|
|
|
|
2023-10-24 14:36:37 +00:00
|
|
|
if (write_offset >= erase_offset) {
|
|
|
|
// erase the next flash section
|
|
|
|
ensure(
|
|
|
|
flash_area_erase_partial(&FIRMWARE_AREA, erase_offset, &bytes_erased),
|
|
|
|
NULL);
|
|
|
|
erase_offset += bytes_erased;
|
|
|
|
} else {
|
|
|
|
// some erased space left from the previous round => use it
|
|
|
|
bytes_erased = erase_offset - write_offset;
|
|
|
|
}
|
2023-10-27 13:07:04 +00:00
|
|
|
|
2023-10-24 14:36:37 +00:00
|
|
|
// write the received data
|
|
|
|
uint32_t bytes_to_write = MIN(bytes_erased, bytes_remaining);
|
|
|
|
uint32_t write_end = write_offset + bytes_to_write;
|
|
|
|
|
|
|
|
ensure(flash_unlock_write(), NULL);
|
|
|
|
while (write_offset < write_end) {
|
2023-12-26 13:33:11 +00:00
|
|
|
// write a block to the flash
|
2024-02-26 13:56:30 +00:00
|
|
|
ensure(flash_area_write_burst(&FIRMWARE_AREA, write_offset, src), NULL);
|
|
|
|
write_offset += FLASH_BURST_SIZE;
|
|
|
|
src += FLASH_BURST_WORDS;
|
2023-10-24 14:36:37 +00:00
|
|
|
}
|
|
|
|
ensure(flash_lock_write(), NULL);
|
2023-06-30 10:12:55 +00:00
|
|
|
|
2023-10-24 14:36:37 +00:00
|
|
|
bytes_remaining -= bytes_to_write;
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:36:37 +00:00
|
|
|
firmware_remaining -= chunk_requested;
|
|
|
|
|
|
|
|
if (firmware_remaining == 0) {
|
|
|
|
// erase the rest (unused part) of the FIRMWARE_AREA
|
|
|
|
uint32_t bytes_erased = 0;
|
|
|
|
do {
|
|
|
|
ensure(
|
|
|
|
flash_area_erase_partial(&FIRMWARE_AREA, erase_offset, &bytes_erased),
|
|
|
|
NULL);
|
|
|
|
erase_offset += bytes_erased;
|
|
|
|
} while (bytes_erased > 0);
|
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
|
2020-02-06 09:12:45 +00:00
|
|
|
headers_offset = 0;
|
2019-03-29 15:26:02 +00:00
|
|
|
firmware_block++;
|
|
|
|
firmware_upload_chunk_retry = FIRMWARE_UPLOAD_CHUNK_RETRY_COUNT;
|
|
|
|
|
|
|
|
if (firmware_remaining > 0) {
|
|
|
|
chunk_requested = (firmware_remaining > IMAGE_CHUNK_SIZE)
|
|
|
|
? IMAGE_CHUNK_SIZE
|
|
|
|
: firmware_remaining;
|
|
|
|
MSG_SEND_INIT(FirmwareRequest);
|
2023-03-31 12:34:20 +00:00
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(offset, firmware_block * IMAGE_CHUNK_SIZE);
|
|
|
|
MSG_SEND_ASSIGN_REQUIRED_VALUE(length, chunk_requested);
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND(FirmwareRequest);
|
|
|
|
} else {
|
|
|
|
MSG_SEND_INIT(Success);
|
|
|
|
MSG_SEND(Success);
|
|
|
|
}
|
|
|
|
return (int)firmware_remaining;
|
|
|
|
}
|
2017-06-16 13:40:27 +00:00
|
|
|
|
2022-05-05 11:47:19 +00:00
|
|
|
secbool bootloader_WipeDevice(void) {
|
2023-06-30 10:12:55 +00:00
|
|
|
return flash_area_erase(&WIPE_AREA, ui_screen_wipe_progress);
|
2022-05-05 11:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) {
|
|
|
|
secbool wipe_result = bootloader_WipeDevice();
|
|
|
|
if (sectrue != wipe_result) {
|
2017-06-19 12:20:09 +00:00
|
|
|
MSG_SEND_INIT(Failure);
|
2019-03-29 15:26:02 +00:00
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Could not erase flash");
|
2017-06-14 18:40:31 +00:00
|
|
|
MSG_SEND(Failure);
|
2022-11-21 13:12:49 +00:00
|
|
|
return WIPE_ERR_CANNOT_ERASE;
|
2019-03-29 15:26:02 +00:00
|
|
|
} else {
|
|
|
|
MSG_SEND_INIT(Success);
|
|
|
|
MSG_SEND(Success);
|
2022-11-21 13:12:49 +00:00
|
|
|
return WIPE_OK;
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void process_msg_unknown(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) {
|
|
|
|
// consume remaining message
|
|
|
|
int remaining_chunks = 0;
|
|
|
|
|
|
|
|
if (msg_size > (USB_PACKET_SIZE - MSG_HEADER1_LEN)) {
|
|
|
|
// calculate how many blocks need to be read to drain the message (rounded
|
|
|
|
// up to not leave any behind)
|
|
|
|
remaining_chunks = (msg_size - (USB_PACKET_SIZE - MSG_HEADER1_LEN) +
|
|
|
|
((USB_PACKET_SIZE - MSG_HEADER2_LEN) - 1)) /
|
|
|
|
(USB_PACKET_SIZE - MSG_HEADER2_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < remaining_chunks; i++) {
|
2019-11-10 18:42:32 +00:00
|
|
|
// read next packet (with retry)
|
|
|
|
_usb_webusb_read_retry(iface_num, buf);
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MSG_SEND_INIT(Failure);
|
|
|
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_UnexpectedMessage);
|
|
|
|
MSG_SEND_ASSIGN_STRING(message, "Unexpected message");
|
|
|
|
MSG_SEND(Failure);
|
2017-04-15 15:44:19 +00:00
|
|
|
}
|
2023-07-20 11:20:50 +00:00
|
|
|
|
2023-06-28 08:51:30 +00:00
|
|
|
#if defined USE_OPTIGA && !defined STM32U5
|
2023-08-24 17:16:09 +00:00
|
|
|
void process_msg_UnlockBootloader(uint8_t iface_num, uint32_t msg_size,
|
|
|
|
uint8_t *buf) {
|
2023-07-20 11:20:50 +00:00
|
|
|
secret_erase();
|
|
|
|
MSG_SEND_INIT(Success);
|
|
|
|
MSG_SEND(Success);
|
|
|
|
}
|
|
|
|
#endif
|