From b9c0b3969b03943eb6d23383208411a08fd17c17 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 18 Nov 2020 11:21:57 +0100 Subject: [PATCH] legacy+core: unify error codes in bootloaders (#1347) --- core/embed/bootloader/messages.c | 4 ++-- core/embed/bootloader_ci/messages.c | 4 ++-- legacy/bootloader/usb.c | 32 +++++++++++++++++------------ legacy/bootloader/usb_send.h | 12 +++++------ 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/core/embed/bootloader/messages.c b/core/embed/bootloader/messages.c index 06675c5a0..9acf5d899 100644 --- a/core/embed/bootloader/messages.c +++ b/core/embed/bootloader/messages.c @@ -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; diff --git a/core/embed/bootloader_ci/messages.c b/core/embed/bootloader_ci/messages.c index eddb6d66d..d1051cdfd 100644 --- a/core/embed/bootloader_ci/messages.c +++ b/core/embed/bootloader_ci/messages.c @@ -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; diff --git a/legacy/bootloader/usb.c b/legacy/bootloader/usb.c index 837ed87b5..e410d062b 100644 --- a/legacy/bootloader/usb.c +++ b/legacy/bootloader/usb.c @@ -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); + if (msg_id == 0x0007) { // FirmwareUpload message (id 7) + if (buf[9] != 0x0a) { // invalid contents + 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); + if (buf[0] != '?') { // invalid contents + 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; diff --git a/legacy/bootloader/usb_send.h b/legacy/bootloader/usb_send.h index a41554fcc..954008211 100644 --- a/legacy/bootloader/usb_send.h +++ b/legacy/bootloader/usb_send.h @@ -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) { } }