1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-16 17:42:02 +00:00

legacy/bootloader: refactor flash_enter/flash_exit

This commit is contained in:
Pavol Rusnak 2019-11-09 18:20:32 +00:00
parent 92c64122a1
commit f86ac65ad1
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 24 additions and 26 deletions

View File

@ -42,7 +42,6 @@
#include "winusb.h" #include "winusb.h"
#include "usb_desc.h" #include "usb_desc.h"
#include "usb_erase.h"
#include "usb_send.h" #include "usb_send.h"
enum { enum {
@ -61,6 +60,19 @@ static char flash_state = STATE_READY;
static uint32_t FW_HEADER[FLASH_FWHEADER_LEN / sizeof(uint32_t)]; static uint32_t FW_HEADER[FLASH_FWHEADER_LEN / sizeof(uint32_t)];
static uint32_t FW_CHUNK[FW_CHUNK_SIZE / sizeof(uint32_t)]; static uint32_t FW_CHUNK[FW_CHUNK_SIZE / sizeof(uint32_t)];
static void flash_enter(void) {
flash_wait_for_last_operation();
flash_clear_status_flags();
flash_unlock();
}
static void flash_exit(void) {
flash_wait_for_last_operation();
flash_lock();
}
#include "usb_erase.h"
static void check_and_write_chunk(void) { static void check_and_write_chunk(void) {
uint32_t offset = (chunk_idx == 0) ? FLASH_FWHEADER_LEN : 0; uint32_t offset = (chunk_idx == 0) ? FLASH_FWHEADER_LEN : 0;
uint32_t chunk_pos = flash_pos % FW_CHUNK_SIZE; uint32_t chunk_pos = flash_pos % FW_CHUNK_SIZE;
@ -89,17 +101,14 @@ static void check_and_write_chunk(void) {
return; return;
} }
flash_wait_for_last_operation(); flash_enter();
flash_clear_status_flags();
flash_unlock();
for (uint32_t i = offset / sizeof(uint32_t); i < chunk_pos / sizeof(uint32_t); for (uint32_t i = offset / sizeof(uint32_t); i < chunk_pos / sizeof(uint32_t);
i++) { i++) {
flash_program_word( flash_program_word(
FLASH_FWHEADER_START + chunk_idx * FW_CHUNK_SIZE + i * sizeof(uint32_t), FLASH_FWHEADER_START + chunk_idx * FW_CHUNK_SIZE + i * sizeof(uint32_t),
FW_CHUNK[i]); FW_CHUNK[i]);
} }
flash_wait_for_last_operation(); flash_exit();
flash_lock();
// all done // all done
if (flash_len == flash_pos) { if (flash_len == flash_pos) {
@ -395,9 +404,8 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
return; return;
} }
} }
flash_wait_for_last_operation();
flash_clear_status_flags(); flash_enter();
flash_unlock();
// write firmware header only when hash was confirmed // write firmware header only when hash was confirmed
if (hash_check_ok) { if (hash_check_ok) {
for (size_t i = 0; i < FLASH_FWHEADER_LEN / sizeof(uint32_t); i++) { for (size_t i = 0; i < FLASH_FWHEADER_LEN / sizeof(uint32_t); i++) {
@ -409,8 +417,7 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
flash_program_word(FLASH_FWHEADER_START + i * sizeof(uint32_t), 0); flash_program_word(FLASH_FWHEADER_START + i * sizeof(uint32_t), 0);
} }
} }
flash_wait_for_last_operation(); flash_exit();
flash_lock();
flash_state = STATE_END; flash_state = STATE_END;
if (hash_check_ok) { if (hash_check_ok) {

View File

@ -1,7 +1,5 @@
static void erase_storage_code_progress(void) { static void erase_storage_code_progress(void) {
flash_wait_for_last_operation(); flash_enter();
flash_clear_status_flags();
flash_unlock();
// erase storage area // erase storage area
for (int i = FLASH_STORAGE_SECTOR_FIRST; i <= FLASH_STORAGE_SECTOR_LAST; for (int i = FLASH_STORAGE_SECTOR_FIRST; i <= FLASH_STORAGE_SECTOR_LAST;
i++) { i++) {
@ -17,14 +15,11 @@ static void erase_storage_code_progress(void) {
(FLASH_CODE_SECTOR_LAST - FLASH_STORAGE_SECTOR_FIRST)); (FLASH_CODE_SECTOR_LAST - FLASH_STORAGE_SECTOR_FIRST));
flash_erase_sector(i, FLASH_CR_PROGRAM_X32); flash_erase_sector(i, FLASH_CR_PROGRAM_X32);
} }
flash_wait_for_last_operation(); flash_exit();
flash_lock();
} }
static void erase_code_progress(void) { static void erase_code_progress(void) {
flash_wait_for_last_operation(); flash_enter();
flash_clear_status_flags();
flash_unlock();
for (int i = FLASH_CODE_SECTOR_FIRST; i <= FLASH_CODE_SECTOR_LAST; i++) { for (int i = FLASH_CODE_SECTOR_FIRST; i <= FLASH_CODE_SECTOR_LAST; i++) {
layoutProgress("PREPARING ... Please wait", layoutProgress("PREPARING ... Please wait",
1000 * (i - FLASH_CODE_SECTOR_FIRST) / 1000 * (i - FLASH_CODE_SECTOR_FIRST) /
@ -32,18 +27,14 @@ static void erase_code_progress(void) {
flash_erase_sector(i, FLASH_CR_PROGRAM_X32); flash_erase_sector(i, FLASH_CR_PROGRAM_X32);
} }
layoutProgress("INSTALLING ... Please wait", 0); layoutProgress("INSTALLING ... Please wait", 0);
flash_wait_for_last_operation(); flash_exit();
flash_lock();
} }
static void erase_storage(void) { static void erase_storage(void) {
flash_wait_for_last_operation(); flash_enter();
flash_clear_status_flags();
flash_unlock();
for (int i = FLASH_STORAGE_SECTOR_FIRST; i <= FLASH_STORAGE_SECTOR_LAST; for (int i = FLASH_STORAGE_SECTOR_FIRST; i <= FLASH_STORAGE_SECTOR_LAST;
i++) { i++) {
flash_erase_sector(i, FLASH_CR_PROGRAM_X32); flash_erase_sector(i, FLASH_CR_PROGRAM_X32);
} }
flash_wait_for_last_operation(); flash_exit();
flash_lock();
} }