1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +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 "usb_desc.h"
#include "usb_erase.h"
#include "usb_send.h"
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_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) {
uint32_t offset = (chunk_idx == 0) ? FLASH_FWHEADER_LEN : 0;
uint32_t chunk_pos = flash_pos % FW_CHUNK_SIZE;
@ -89,17 +101,14 @@ static void check_and_write_chunk(void) {
return;
}
flash_wait_for_last_operation();
flash_clear_status_flags();
flash_unlock();
flash_enter();
for (uint32_t i = offset / sizeof(uint32_t); i < chunk_pos / sizeof(uint32_t);
i++) {
flash_program_word(
FLASH_FWHEADER_START + chunk_idx * FW_CHUNK_SIZE + i * sizeof(uint32_t),
FW_CHUNK[i]);
}
flash_wait_for_last_operation();
flash_lock();
flash_exit();
// all done
if (flash_len == flash_pos) {
@ -395,9 +404,8 @@ static void rx_callback(usbd_device *dev, uint8_t ep) {
return;
}
}
flash_wait_for_last_operation();
flash_clear_status_flags();
flash_unlock();
flash_enter();
// write firmware header only when hash was confirmed
if (hash_check_ok) {
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_wait_for_last_operation();
flash_lock();
flash_exit();
flash_state = STATE_END;
if (hash_check_ok) {

View File

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