1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

bootloader: show loader instead of text

This commit is contained in:
Pavol Rusnak 2017-06-20 19:53:24 +02:00
parent afd65833cb
commit be83b8b138
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
5 changed files with 38 additions and 27 deletions

View File

@ -51,7 +51,7 @@ bool check_sdcard(void)
}
}
static void progress_callback(void) {
static void progress_callback(uint16_t val) {
display_printf(".");
}

View File

@ -35,7 +35,8 @@ void display_vendor(const uint8_t *vimg, const char *vstr, uint32_t vstr_len, ui
}
uint32_t datalen = *(uint32_t *)(vimg + 8);
display_image(60, 32, w, h, vimg + 12, datalen);
display_text_center(120, 192, vstr, vstr_len, FONT_BOLD, COLOR_WHITE, COLOR_BLACK);
// FIXME: uncomment next line
// display_text_center(120, 192, vstr, vstr_len, FONT_BOLD, COLOR_WHITE, COLOR_BLACK);
char ver_str[32];
mini_snprintf(ver_str, sizeof(ver_str), "%d.%d.%d.%d",
(int)(fw_version & 0xFF),
@ -43,7 +44,8 @@ void display_vendor(const uint8_t *vimg, const char *vstr, uint32_t vstr_len, ui
(int)((fw_version >> 16) & 0xFF),
(int)((fw_version >> 24) & 0xFF)
);
display_text_center(120, 215, ver_str, -1, FONT_BOLD, COLOR_GRAY64, COLOR_BLACK);
// FIXME: uncomment next line
// display_text_center(120, 215, ver_str, -1, FONT_BOLD, COLOR_GRAY64, COLOR_BLACK);
display_refresh();
}
@ -155,6 +157,9 @@ void mainloop(void)
__fatal_error("usb_init_all", __FILE__, __LINE__, __FUNCTION__);
}
display_clear();
display_loader(0, 0, 0xFFFF, 0, 0, 0, 0);
uint8_t buf[USB_PACKET_SIZE];
for (;;) {
@ -171,23 +176,18 @@ void mainloop(void)
}
switch (msg_id) {
case 0: // Initialize
display_printf("received Initialize\n");
process_msg_Initialize(USB_IFACE_NUM, msg_size, buf);
break;
case 1: // Ping
display_printf("received Ping\n");
process_msg_Ping(USB_IFACE_NUM, msg_size, buf);
break;
case 6: // FirmwareErase
display_printf("received FirmwareErase\n");
process_msg_FirmwareErase(USB_IFACE_NUM, msg_size, buf);
break;
case 7: // FirmwareUpload
display_printf("received FirmwareUpload\n");
process_msg_FirmwareUpload(USB_IFACE_NUM, msg_size, buf);
break;
default:
display_printf("received unknown message\n");
process_msg_unknown(USB_IFACE_NUM, msg_size, buf);
break;
}

View File

@ -7,6 +7,7 @@
#include "messages.pb.h"
#include "common.h"
#include "display.h"
#include "flash.h"
#include "usb.h"
#include "version.h"
@ -15,12 +16,12 @@
bool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size)
{
if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') {
return false;
}
*msg_id = (buf[3] << 8) + buf[4];
*msg_size = (buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
return true;
if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') {
return false;
}
*msg_id = (buf[3] << 8) + buf[4];
*msg_size = (buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
return true;
}
typedef struct {
@ -220,23 +221,28 @@ void process_msg_Ping(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
MSG_SEND(Success);
}
static uint32_t firmware_size, firmware_flashed, chunk_requested;
static uint32_t firmware_remaining, firmware_flashed, chunk_requested;
static void progress_erase(uint16_t val)
{
display_loader(val, 0, 0xFFFF, 0, 0, 0, 0);
}
void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
{
firmware_size = 0;
firmware_remaining = 0;
firmware_flashed = 0;
chunk_requested = 0;
MSG_RECV_INIT(FirmwareErase);
MSG_RECV(FirmwareErase);
firmware_size = msg_recv.has_length ? msg_recv.length : 0;
if (firmware_size > 0 && firmware_size % 4 == 0) {
firmware_remaining = msg_recv.has_length ? msg_recv.length : 0;
if (firmware_remaining > 0 && firmware_remaining % 4 == 0) {
// erase flash
flash_erase_sectors(FLASH_SECTOR_FIRMWARE_START, FLASH_SECTOR_FIRMWARE_END, NULL);
flash_erase_sectors(FLASH_SECTOR_FIRMWARE_START, FLASH_SECTOR_FIRMWARE_END, progress_erase);
// request new firmware
chunk_requested = (firmware_size > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_size;
chunk_requested = (firmware_remaining > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_remaining;
MSG_SEND_INIT(FirmwareRequest);
MSG_SEND_ASSIGN_VALUE(offset, 0);
MSG_SEND_ASSIGN_VALUE(length, chunk_requested);
@ -253,11 +259,13 @@ static uint32_t chunk_size = 0;
static bool _read_payload(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
#define BUFSIZE 1024
#define BUFSIZE 4096
uint32_t buf[BUFSIZE / sizeof(uint32_t)];
uint32_t chunk_written = 0;
chunk_size = stream->bytes_left;
while (stream->bytes_left) {
// print loader
display_loader(1000 * (firmware_flashed + chunk_written) / (firmware_flashed + firmware_remaining), 0, 0xFFFF, 0, 0, 0, 0);
memset(buf, 0xFF, sizeof(buf));
// read data
if (!pb_read(stream, (pb_byte_t *)buf, (stream->bytes_left > BUFSIZE) ? BUFSIZE : stream->bytes_left)) {
@ -289,16 +297,17 @@ void process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *b
MSG_SEND(Failure);
}
firmware_size -= chunk_requested;
firmware_remaining -= chunk_requested;
firmware_flashed += chunk_requested;
if (firmware_size > 0) {
chunk_requested = (firmware_size > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_size;
if (firmware_remaining > 0) {
chunk_requested = (firmware_remaining > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_remaining;
MSG_SEND_INIT(FirmwareRequest);
MSG_SEND_ASSIGN_VALUE(offset, firmware_flashed);
MSG_SEND_ASSIGN_VALUE(length, chunk_requested);
MSG_SEND(FirmwareRequest);
} else {
display_clear();
MSG_SEND_INIT(Success);
MSG_SEND(Success);
}

View File

@ -39,7 +39,7 @@ void flash_set_option_bytes(void)
}
}
int flash_erase_sectors(int start, int end, void (*progress)(void))
int flash_erase_sectors(int start, int end, void (*progress)(uint16_t val))
{
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef EraseInitStruct;
@ -56,7 +56,7 @@ int flash_erase_sectors(int start, int end, void (*progress)(void))
return 0;
}
if (progress) {
progress();
progress(1000 * (i - start + 1) / (end - start + 1));
}
}
HAL_FLASH_Lock();

View File

@ -1,6 +1,8 @@
#ifndef __TREZORHAL_FLASH_H__
#define __TREZORHAL_FLASH_H__
#include <stdint.h>
int flash_init(void);
void flash_set_option_bytes(void);
@ -17,6 +19,6 @@ void flash_set_option_bytes(void);
#define FLASH_SECTOR_FIRMWARE_START 5
#define FLASH_SECTOR_FIRMWARE_END 11
int flash_erase_sectors(int start, int end, void (*progress)(void));
int flash_erase_sectors(int start, int end, void (*progress)(uint16_t val));
#endif