1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

flash/sdcard: introduce wur, simplify code

This commit is contained in:
Pavol Rusnak 2017-12-16 18:42:06 +01:00
parent 5e93cca0a9
commit 47f5cba439
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
9 changed files with 26 additions and 97 deletions

View File

@ -114,10 +114,7 @@ static secbool copy_sdcard(void)
}
display_printf(" done\n\n");
if (sectrue != flash_unlock()) {
display_printf("could not unlock flash\n");
return secfalse;
}
ensure(flash_unlock(), NULL);
// copy bootloader from SD card to Flash
display_printf("copying new bootloader from SD card\n\n");
@ -128,12 +125,7 @@ static secbool copy_sdcard(void)
for (int i = 0; i < (IMAGE_HEADER_SIZE + codelen) / SDCARD_BLOCK_SIZE; i++) {
ensure(sdcard_read_blocks(buf, i, 1), NULL);
for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
if (sectrue != flash_write_word(BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j])) {
display_printf("copy failed\n");
sdcard_power_off();
ensure(flash_lock(), NULL);
return secfalse;
}
ensure(flash_write_word(BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]), NULL);
}
}

View File

@ -411,25 +411,12 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
return -4;
}
if (sectrue != 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 -5;
}
ensure(flash_unlock(), NULL);
// TODO: fix writing to non-continous area
const uint32_t * const src = (const uint32_t * const)chunk_buffer;
for (int i = 0; i < chunk_size / sizeof(uint32_t); i++) {
if (sectrue != flash_write_word(FIRMWARE_START + firmware_block * IMAGE_CHUNK_SIZE + i * sizeof(uint32_t), src[i])) {
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
MSG_SEND_ASSIGN_STRING(message, "Could not write data");
MSG_SEND(Failure);
ensure(flash_lock(), NULL);
return -6;
}
ensure(flash_write_word(FIRMWARE_START + firmware_block * IMAGE_CHUNK_SIZE + i * sizeof(uint32_t), src[i]), NULL);
}
ensure(flash_lock(), NULL);

View File

@ -35,32 +35,23 @@ static secbool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, co
if (sector >= NORCOW_SECTOR_COUNT) {
return secfalse;
}
if (sectrue != flash_unlock()) {
return secfalse;
}
ensure(flash_unlock(), NULL);
// write prefix
if (sectrue != flash_write_word_rel(norcow_sectors[sector], offset, prefix)) {
flash_lock();
return secfalse;
}
ensure(flash_write_word_rel(norcow_sectors[sector], offset, prefix), NULL);
if (len > 0) {
offset += sizeof(uint32_t);
// write data
for (uint16_t i = 0; i < len; i++, offset++) {
if (sectrue != flash_write_byte_rel(norcow_sectors[sector], offset, data[i])) {
flash_lock();
return secfalse;
}
ensure(flash_write_byte_rel(norcow_sectors[sector], offset, data[i]), NULL);
}
// pad with zeroes
for (; offset % 4; offset++) {
if (sectrue != flash_write_byte_rel(norcow_sectors[sector], offset, 0x00)) {
flash_lock();
return secfalse;
ensure(flash_write_byte_rel(norcow_sectors[sector], offset, 0x00), NULL);
}
}
}
flash_lock();
ensure(flash_lock(), NULL);
return sectrue;
}
@ -295,10 +286,8 @@ secbool norcow_update(uint16_t key, uint16_t offset, uint32_t value)
return secfalse;
}
uint32_t sector_offset = (const uint8_t*) ptr - (const uint8_t *)norcow_ptr(norcow_active_sector, 0, NORCOW_SECTOR_SIZE) + offset;
if (sectrue != flash_unlock()) {
return secfalse;
}
secbool result = flash_write_word_rel(norcow_sectors[norcow_active_sector], sector_offset, value);
flash_lock();
return result;
ensure(flash_unlock(), NULL);
ensure(flash_write_word_rel(norcow_sectors[norcow_active_sector], sector_offset, value), NULL);
ensure(flash_lock(), NULL);
return sectrue;
}

View File

@ -45,7 +45,8 @@ STATIC mp_obj_t mod_trezorio_SDCard_power(mp_obj_t self, mp_obj_t state) {
if (mp_obj_is_true(state)) {
return mp_obj_new_bool(sdcard_power_on());
} else {
return mp_obj_new_bool(sdcard_power_off());
sdcard_power_off();
return mp_const_true;
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_SDCard_power_obj, mod_trezorio_SDCard_power);

View File

@ -5,19 +5,15 @@
static int vcp_iface_num = -1;
int mp_hal_stdin_rx_chr(void) {
ensure(sectrue * (vcp_iface_num >= 0), "vcp stdio is not configured");
uint8_t c = 0;
usb_vcp_read_blocking(vcp_iface_num, &c, 1, -1);
ensure(usb_vcp_read_blocking(vcp_iface_num, &c, 1, -1), NULL);
return c;
}
void mp_hal_stdout_tx_strn(const char *str, size_t len) {
if (vcp_iface_num >= 0) {
usb_vcp_write_blocking(vcp_iface_num, (const uint8_t *)str, len, 0);
} else {
// no-op
ensure(usb_vcp_write_blocking(vcp_iface_num, (const uint8_t *)str, len, 0), NULL);
}
}

View File

@ -45,9 +45,7 @@ static void flash_from_sdcard(uint32_t target, uint32_t source, uint32_t length)
"sdcard_read_blocks");
for (uint32_t j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
ensure(
flash_write_word(target + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]),
"flash_write_word");
ensure(flash_write_word(target + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]), NULL);
}
}
}
@ -78,9 +76,7 @@ int main(void)
display_printf("\n");
display_printf("erased\n");
ensure(
flash_unlock(),
"flash_unlock");
ensure(flash_unlock(), NULL);
sdcard_power_on();
@ -92,7 +88,7 @@ int main(void)
display_printf("done\n");
sdcard_power_off();
flash_lock();
ensure(flash_lock(), NULL);
return 0;
}

View File

@ -74,9 +74,7 @@ const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size)
secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len))
{
if (sectrue != flash_unlock()) {
return secfalse;
}
ensure(flash_unlock(), NULL);
FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
@ -130,15 +128,6 @@ secbool flash_write_word_rel(uint8_t sector, uint32_t offset, uint32_t data)
return sectrue * (HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_SECTOR_TABLE[sector] + offset, data));
}
secbool flash_read_word_rel(uint8_t sector, uint32_t offset, uint32_t *data)
{
if (offset % 4 != 0) {
return secfalse;
}
*data = *((const uint32_t *) (FLASH_SECTOR_TABLE[sector] + offset));
return sectrue;
}
#define FLASH_OTP_LOCK_BASE 0x1FFF7A00U
secbool flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data, uint8_t datalen)
@ -157,15 +146,10 @@ secbool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint
if (block >= FLASH_OTP_NUM_BLOCKS || offset + datalen > FLASH_OTP_BLOCK_SIZE) {
return secfalse;
}
if (sectrue != flash_unlock()) {
return secfalse;
}
ensure(flash_unlock(), NULL);
secbool ret = secfalse;
for (uint8_t i = 0; i < datalen; i++) {
ret = flash_write_byte(FLASH_OTP_BASE + block * FLASH_OTP_BLOCK_SIZE + offset + i, data[i]);
if (ret != sectrue) {
break;
}
ensure(flash_write_byte(FLASH_OTP_BASE + block * FLASH_OTP_BLOCK_SIZE + offset + i, data[i]), NULL);
}
ensure(flash_lock(), NULL);
return ret;
@ -176,9 +160,7 @@ secbool flash_otp_lock(uint8_t block)
if (block >= FLASH_OTP_NUM_BLOCKS) {
return secfalse;
}
if (sectrue != flash_unlock()) {
return secfalse;
}
ensure(flash_unlock(), NULL);
HAL_StatusTypeDef ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, FLASH_OTP_LOCK_BASE + block, 0x00);
ensure(flash_lock(), NULL);
return sectrue * (ret == HAL_OK);

View File

@ -56,7 +56,6 @@ secbool __wur flash_write_byte(uint32_t address, uint8_t data);
secbool __wur flash_write_word(uint32_t address, uint32_t data);
secbool __wur flash_write_byte_rel(uint8_t sector, uint32_t offset, uint8_t data);
secbool __wur flash_write_word_rel(uint8_t sector, uint32_t offset, uint32_t data);
secbool __wur flash_read_word_rel(uint8_t sector, uint32_t offset, uint32_t *data);
#define FLASH_OTP_NUM_BLOCKS 16
#define FLASH_OTP_BLOCK_SIZE 32

View File

@ -161,19 +161,6 @@ secbool flash_write_word_rel(uint8_t sector, uint32_t offset, uint32_t data)
return sectrue;
}
secbool flash_read_word_rel(uint8_t sector, uint32_t offset, uint32_t *data)
{
if (offset % 4) { // we read only at 4-byte boundary
return secfalse;
}
const uint32_t *flash = (const uint32_t *)flash_get_address(sector, offset, sizeof(data));
if (!flash) {
return secfalse;
}
data[0] = flash[0];
return sectrue;
}
secbool flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data, uint8_t datalen)
{
return secfalse;