1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-25 17:09:44 +00:00

legacy+core: unify error codes in bootloaders (#1347)

This commit is contained in:
Pavol Rusnak 2020-11-18 11:21:57 +01:00 committed by GitHub
parent e871a1c4ae
commit b9c0b3969b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 23 deletions

View File

@ -352,7 +352,7 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size,
} else {
// invalid firmware size
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_DataError);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
MSG_SEND_ASSIGN_STRING(message, "Wrong firmware size");
MSG_SEND(Failure);
}
@ -468,7 +468,7 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size,
if (sectrue != r || chunk_size != (chunk_requested + read_offset)) {
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_DataError);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
MSG_SEND_ASSIGN_STRING(message, "Invalid chunk size");
MSG_SEND(Failure);
return -1;

View File

@ -352,7 +352,7 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size,
} else {
// invalid firmware size
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_DataError);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
MSG_SEND_ASSIGN_STRING(message, "Wrong firmware size");
MSG_SEND(Failure);
}
@ -468,7 +468,7 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size,
if (sectrue != r || chunk_size != (chunk_requested + read_offset)) {
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_DataError);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
MSG_SEND_ASSIGN_STRING(message, "Invalid chunk size");
MSG_SEND(Failure);
return -1;

View File

@ -207,10 +207,14 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
} else {
flash_state = STATE_END;
show_unplug("Device wipe", "aborted.");
send_msg_failure(dev);
send_msg_failure(dev, 4); // Failure_ActionCancelled
}
return;
}
if (msg_id != 0x0006) {
send_msg_failure(dev, 1); // Failure_UnexpectedMessage
return;
}
}
if (flash_state == STATE_OPEN) {
@ -240,19 +244,20 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
send_msg_success(dev);
flash_state = STATE_FLASHSTART;
} else {
send_msg_failure(dev);
send_msg_failure(dev, 4); // Failure_ActionCancelled
flash_state = STATE_END;
show_unplug("Firmware installation", "aborted.");
}
return;
}
send_msg_failure(dev, 1); // Failure_UnexpectedMessage
return;
}
if (flash_state == STATE_FLASHSTART) {
if (msg_id == 0x0007) { // FirmwareUpload message (id 7)
if (buf[9] != 0x0a) { // invalid contents
send_msg_failure(dev);
send_msg_failure(dev, 9); // Failure_ProcessError
flash_state = STATE_END;
show_halt("Error installing", "firmware.");
return;
@ -260,27 +265,27 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
// read payload length
const uint8_t *p = buf + 10;
if (readprotobufint(&p, &flash_len) != sectrue) { // integer too large
send_msg_failure(dev);
send_msg_failure(dev, 9); // Failure_ProcessError
flash_state = STATE_END;
show_halt("Firmware is", "too big.");
return;
}
if (flash_len <= FLASH_FWHEADER_LEN) { // firmware is too small
send_msg_failure(dev);
send_msg_failure(dev, 9); // Failure_ProcessError
flash_state = STATE_END;
show_halt("Firmware is", "too small.");
return;
}
if (flash_len >
FLASH_FWHEADER_LEN + FLASH_APP_LEN) { // firmware is too big
send_msg_failure(dev);
send_msg_failure(dev, 9); // Failure_ProcessError
flash_state = STATE_END;
show_halt("Firmware is", "too big.");
return;
}
// check firmware magic
if (memcmp(p, &FIRMWARE_MAGIC_NEW, 4) != 0) {
send_msg_failure(dev);
send_msg_failure(dev, 9); // Failure_ProcessError
flash_state = STATE_END;
show_halt("Wrong firmware", "header.");
return;
@ -304,12 +309,13 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
}
return;
}
send_msg_failure(dev, 1); // Failure_UnexpectedMessage
return;
}
if (flash_state == STATE_FLASHING) {
if (buf[0] != '?') { // invalid contents
send_msg_failure(dev);
send_msg_failure(dev, 9); // Failure_ProcessError
flash_state = STATE_END;
show_halt("Error installing", "firmware.");
return;
@ -394,7 +400,7 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
"\x75\x12\x5e\x5c\xa2\xcd\x52\x7f\x35\x82\xec\x87\xff\xd9\x40"
"\x76\xbc",
32) != 0) {
send_msg_failure(dev);
send_msg_failure(dev, 9); // Failure_ProcessError
show_halt("Error installing", "firmware.");
return;
}
@ -432,7 +438,7 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
layoutDialog(&bmp_icon_warning, NULL, NULL, NULL, "Firmware installation",
"aborted.", NULL, "You need to repeat", "the procedure with",
"the correct firmware.");
send_msg_failure(dev);
send_msg_failure(dev, 9); // Failure_ProcessError
shutdown();
}
return;

View File

@ -14,11 +14,10 @@ static void send_msg_success(usbd_device *dev) {
}
}
static void send_msg_failure(usbd_device *dev) {
static void send_msg_failure(usbd_device *dev, uint8_t code) {
uint8_t response[64];
memzero(response, sizeof(response));
// response: Failure message (id 3), payload len 2
// - code = 99 (Failure_FirmwareError)
memcpy(response,
// header
"?##"
@ -26,10 +25,11 @@ static void send_msg_failure(usbd_device *dev) {
"\x00\x03"
// msg_size
"\x00\x00\x00\x02"
// data
"\x08"
"\x63",
11);
// code field id
"\x08",
10);
// assign code value
response[10] = code;
while (usbd_ep_write_packet(dev, ENDPOINT_ADDRESS_IN, response, 64) != 64) {
}
}