From 2010213338bd27c8bb58db900f98f4a5ac4978d6 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 16 Oct 2017 18:03:04 +0200 Subject: [PATCH] bootloader: first UI steps --- Makefile | 3 +++ embed/bootloader/main.c | 20 ++++++++++++++------ embed/bootloader/messages.c | 17 ++++++++++++----- embed/bootloader/messages.h | 9 +-------- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 8bba324f7c..90069bb84b 100644 --- a/Makefile +++ b/Makefile @@ -178,3 +178,6 @@ combine: ## combine boardloader + bootloader + firmware into one combined image $(BOOTLOADER_START) $(BOOTLOADER_BUILD_DIR)/bootloader.bin \ $(FIRMWARE_START) $(FIRMWARE_BUILD_DIR)/firmware.bin \ > $(FIRMWARE_BUILD_DIR)/combined.bin \ + +upload: ## upload firmware using trezorctl + trezorctl firmware_update -f $(FIRMWARE_BUILD_DIR)/firmware.bin diff --git a/embed/bootloader/main.c b/embed/bootloader/main.c index c178f9d910..ab47036e1f 100644 --- a/embed/bootloader/main.c +++ b/embed/bootloader/main.c @@ -66,10 +66,6 @@ static const uint8_t * const BOOTLOADER_KEYS[] = { #endif }; -void check_and_jump(void) -{ -} - int usb_init_all(void) { static const usb_dev_info_t dev_info = { .vendor_id = 0x1209, @@ -124,6 +120,8 @@ void bootloader_loop(void) ensure(0 == usb_init_all(), NULL); display_clear(); + display_text_center(120, 30, "TREZOR Bootloader", -1, FONT_BOLD, COLOR_WHITE, COLOR_BLACK); + display_fade(0, BACKLIGHT_NORMAL, 1000); uint8_t buf[USB_PACKET_SIZE]; @@ -147,10 +145,19 @@ void bootloader_loop(void) process_msg_Ping(USB_IFACE_NUM, msg_size, buf); break; case 6: // FirmwareErase + display_text_center(120, 230, "Updating firmware", -1, FONT_BOLD, COLOR_WHITE, COLOR_BLACK); process_msg_FirmwareErase(USB_IFACE_NUM, msg_size, buf); break; case 7: // FirmwareUpload - process_msg_FirmwareUpload(USB_IFACE_NUM, msg_size, buf); + r = process_msg_FirmwareUpload(USB_IFACE_NUM, msg_size, buf); + if (r < 0) { // error + display_clear(); + display_text_center(120, 230, "Error", -1, FONT_BOLD, COLOR_WHITE, COLOR_BLACK); + } else + if (r == 0) { // last chunk received + display_clear(); + display_text_center(120, 230, "Done", -1, FONT_BOLD, COLOR_WHITE, COLOR_BLACK); + } break; default: process_msg_unknown(USB_IFACE_NUM, msg_size, buf); @@ -201,7 +208,8 @@ int main(void) touched |= touch_read(); } - if (touched != 0) { + // start the bootloader if user touched the screen or no firmware installed + if (touched != 0 || !vendor_parse_header((const uint8_t *)FIRMWARE_START, NULL)) { bootloader_loop(); shutdown(); } diff --git a/embed/bootloader/messages.c b/embed/bootloader/messages.c index 3d50ca66ff..f4c0139ede 100644 --- a/embed/bootloader/messages.c +++ b/embed/bootloader/messages.c @@ -1,5 +1,6 @@ #include +#include #include #include #include "messages.pb.h" @@ -7,11 +8,17 @@ #include "common.h" #include "display.h" #include "flash.h" +#include "image.h" #include "usb.h" #include "version.h" #include "messages.h" +#define FIRMWARE_CHUNK_SIZE (128 * 1024) + +#define MSG_HEADER1_LEN 9 +#define MSG_HEADER2_LEN 1 + bool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size) { if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') { @@ -204,8 +211,8 @@ void process_msg_Initialize(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) MSG_SEND_ASSIGN_VALUE(minor_version, VERSION_MINOR); MSG_SEND_ASSIGN_VALUE(patch_version, VERSION_PATCH); MSG_SEND_ASSIGN_VALUE(bootloader_mode, true); - // TODO: properly detect firmware - MSG_SEND_ASSIGN_VALUE(firmware_present, false); + bool firmware_present = vendor_parse_header((const uint8_t *)FIRMWARE_START, NULL); + MSG_SEND_ASSIGN_VALUE(firmware_present, firmware_present); MSG_SEND(Features); } @@ -286,14 +293,14 @@ static bool _read_payload(pb_istream_t *stream, const pb_field_t *field, void ** return true; } -void process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) +int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) { if (!flash_unlock()) { MSG_SEND_INIT(Failure); MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError); MSG_SEND_ASSIGN_STRING(message, "Could not unlock flash"); MSG_SEND(Failure); - return; + return -1; } MSG_RECV_INIT(FirmwareUpload); @@ -318,10 +325,10 @@ void process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *b MSG_SEND_ASSIGN_VALUE(length, chunk_requested); MSG_SEND(FirmwareRequest); } else { - display_clear(); MSG_SEND_INIT(Success); MSG_SEND(Success); } + return (int)firmware_remaining; } void process_msg_unknown(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) diff --git a/embed/bootloader/messages.h b/embed/bootloader/messages.h index 935136504e..78a1f144cb 100644 --- a/embed/bootloader/messages.h +++ b/embed/bootloader/messages.h @@ -4,22 +4,15 @@ #include #include -#include - -#define MSG_HEADER1_LEN 9 -#define MSG_HEADER2_LEN 1 - #define USB_PACKET_SIZE 64 #define USB_IFACE_NUM 0 -#define FIRMWARE_CHUNK_SIZE (128*1024) - bool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size); void process_msg_Initialize(uint8_t iface_num, uint32_t msg_size, uint8_t *buf); void process_msg_Ping(uint8_t iface_num, uint32_t msg_size, uint8_t *buf); void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *buf); -void process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *buf); +int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *buf); void process_msg_unknown(uint8_t iface_num, uint32_t msg_size, uint8_t *buf); #endif