1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 09:28:13 +00:00

bootloader: firmware update working

This commit is contained in:
Pavol Rusnak 2017-06-20 17:32:21 +02:00
parent 95bc321113
commit 0290a13427
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 67 additions and 19 deletions

View File

@ -51,27 +51,18 @@ bool check_sdcard(void)
} }
} }
static void progress_callback(void) {
display_printf(".");
}
bool copy_sdcard(void) bool copy_sdcard(void)
{ {
display_printf("erasing flash "); display_printf("erasing flash ");
// erase flash (except boardloader) // erase flash (except boardloader)
HAL_FLASH_Unlock(); if (0 != flash_erase_sectors(FLASH_SECTOR_BOARDLOADER_END + 1, FLASH_SECTOR_FIRMWARE_END, progress_callback)) {
FLASH_EraseInitTypeDef EraseInitStruct; display_printf(" failed\n");
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | return false;
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.NbSectors = 1;
uint32_t SectorError = 0;
for (int i = 2; i < 12; i++) {
EraseInitStruct.Sector = i;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
HAL_FLASH_Lock();
display_printf(" failed\n");
return false;
}
display_printf(".");
} }
display_printf(" done\n"); display_printf(" done\n");
@ -87,10 +78,10 @@ bool copy_sdcard(void)
if (!image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) { if (!image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
display_printf("invalid header\n"); display_printf("invalid header\n");
sdcard_power_off(); sdcard_power_off();
HAL_FLASH_Lock();
return false; return false;
} }
HAL_FLASH_Unlock();
for (int i = 0; i < (HEADER_SIZE + hdr.codelen) / SDCARD_BLOCK_SIZE; i++) { for (int i = 0; i < (HEADER_SIZE + hdr.codelen) / SDCARD_BLOCK_SIZE; i++) {
sdcard_read_blocks((uint8_t *)buf, i, 1); sdcard_read_blocks((uint8_t *)buf, i, 1);
for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) { for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {

View File

@ -1,9 +1,13 @@
#include STM32_HAL_H
#include <string.h> #include <string.h>
#include <pb_decode.h> #include <pb_decode.h>
#include <pb_encode.h> #include <pb_encode.h>
#include "messages.pb.h" #include "messages.pb.h"
#include "common.h"
#include "flash.h"
#include "usb.h" #include "usb.h"
#include "version.h" #include "version.h"
@ -229,6 +233,9 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
firmware_size = msg_recv.has_length ? msg_recv.length : 0; firmware_size = msg_recv.has_length ? msg_recv.length : 0;
if (firmware_size > 0 && firmware_size % 4 == 0) { if (firmware_size > 0 && firmware_size % 4 == 0) {
// erase flash
flash_erase_sectors(FLASH_SECTOR_FIRMWARE_START, FLASH_SECTOR_FIRMWARE_END, NULL);
// request new firmware
chunk_requested = (firmware_size > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_size; chunk_requested = (firmware_size > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_size;
MSG_SEND_INIT(FirmwareRequest); MSG_SEND_INIT(FirmwareRequest);
MSG_SEND_ASSIGN_VALUE(offset, 0); MSG_SEND_ASSIGN_VALUE(offset, 0);
@ -247,21 +254,33 @@ static uint32_t chunk_size = 0;
static bool _read_payload(pb_istream_t *stream, const pb_field_t *field, void **arg) static bool _read_payload(pb_istream_t *stream, const pb_field_t *field, void **arg)
{ {
#define BUFSIZE 1024 #define BUFSIZE 1024
pb_byte_t buf[BUFSIZE]; uint32_t buf[BUFSIZE / sizeof(uint32_t)];
uint32_t chunk_written = 0;
chunk_size = stream->bytes_left; chunk_size = stream->bytes_left;
while (stream->bytes_left) { while (stream->bytes_left) {
if (!pb_read(stream, buf, (stream->bytes_left > BUFSIZE) ? BUFSIZE : stream->bytes_left)) { memset(buf, 0xFF, sizeof(buf));
// read data
if (!pb_read(stream, (pb_byte_t *)buf, (stream->bytes_left > BUFSIZE) ? BUFSIZE : stream->bytes_left)) {
return false; return false;
} }
// write data
for (int i = 0; i < BUFSIZE / sizeof(uint32_t); i++) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FIRMWARE_START + firmware_flashed + chunk_written + i * sizeof(uint32_t), buf[i]) != HAL_OK) {
return false;
}
}
chunk_written += BUFSIZE;
} }
return true; return true;
} }
void process_msg_FirmwareUpload(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)
{ {
HAL_FLASH_Unlock();
MSG_RECV_INIT(FirmwareUpload); MSG_RECV_INIT(FirmwareUpload);
MSG_RECV_CALLBACK(payload, _read_payload); MSG_RECV_CALLBACK(payload, _read_payload);
MSG_RECV(FirmwareUpload); MSG_RECV(FirmwareUpload);
HAL_FLASH_Lock();
if (chunk_size != chunk_requested) { if (chunk_size != chunk_requested) {
MSG_SEND_INIT(Failure); MSG_SEND_INIT(Failure);

View File

@ -38,3 +38,27 @@ void flash_set_option_bytes(void)
HAL_FLASHEx_OBProgram(&opts); HAL_FLASHEx_OBProgram(&opts);
} }
} }
int flash_erase_sectors(int start, int end, void (*progress)(void))
{
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef EraseInitStruct;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.NbSectors = 1;
uint32_t SectorError = 0;
for (int i = start; i <= end; i++) {
EraseInitStruct.Sector = i;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
HAL_FLASH_Lock();
return 0;
}
if (progress) {
progress();
}
}
HAL_FLASH_Lock();
return 1;
}

View File

@ -5,4 +5,18 @@ int flash_init(void);
void flash_set_option_bytes(void); void flash_set_option_bytes(void);
#define FLASH_SECTOR_BOARDLOADER_START 0
#define FLASH_SECTOR_BOARDLOADER_END 1
#define FLASH_SECTOR_STORAGE_START 2
#define FLASH_SECTOR_STORAGE_END 3
#define FLASH_SECTOR_BOOTLOADER_START 4
#define FLASH_SECTOR_BOOTLOADER_END 4
#define FLASH_SECTOR_FIRMWARE_START 5
#define FLASH_SECTOR_FIRMWARE_END 11
int flash_erase_sectors(int start, int end, void (*progress)(void));
#endif #endif