1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

flash: add unix/flash.c

This commit is contained in:
Jan Pochyla 2017-10-17 15:11:27 +02:00
parent 4cabe40220
commit 543e8c5007
5 changed files with 186 additions and 13 deletions

View File

@ -215,6 +215,7 @@ SOURCE_UNIX = [
'vendor/micropython/ports/unix/alloc.c',
'embed/unix/common.c',
'embed/unix/touch.c',
'embed/unix/flash.c',
]
SOURCE_EMIT_NATIVE = ['vendor/micropython/py/emitnative.c']

View File

@ -5,13 +5,7 @@
* see LICENSE file for details
*/
#if defined TREZOR_STM32
#include "flash.h"
#elif defined TREZOR_UNIX
#include "unix-flash-mock.h"
#else
#error Unsupported TREZOR port. Only STM32 and UNIX ports are supported.
#endif
#include "../../trezorhal/flash.h"
/// class FlashOTP:
/// '''

View File

@ -91,12 +91,12 @@ secbool flash_write_word(uint32_t address, uint32_t data)
return sectrue * (HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data));
}
bool flash_write_byte_rel(uint32_t sector, uint32_t offset, uint8_t data)
bool flash_write_byte_rel(uint8_t sector, uint32_t offset, uint8_t data)
{
return HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, SECTOR_TABLE[sector] + offset, data);
}
bool flash_write_word_rel(uint32_t sector, uint32_t offset, uint32_t data)
bool flash_write_word_rel(uint8_t sector, uint32_t offset, uint32_t data)
{
if (offset % 4 != 0) {
return false;
@ -104,7 +104,7 @@ bool flash_write_word_rel(uint32_t sector, uint32_t offset, uint32_t data)
return HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, SECTOR_TABLE[sector] + offset, data);
}
bool flash_read_word_rel(uint32_t sector, uint32_t offset, uint32_t *data)
bool flash_read_word_rel(uint8_t sector, uint32_t offset, uint32_t *data)
{
if (offset % 4 != 0) {
return false;

View File

@ -51,9 +51,9 @@ secbool flash_lock(void);
secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len));
secbool flash_write_byte(uint32_t address, uint8_t data);
secbool flash_write_word(uint32_t address, uint32_t data);
secbool flash_write_byte_rel(uint32_t sector, uint32_t offset, uint8_t data);
secbool flash_write_word_rel(uint32_t sector, uint32_t offset, uint32_t data);
secbool flash_read_word_rel(uint32_t sector, uint32_t offset, uint32_t *data);
secbool flash_write_byte_rel(uint8_t sector, uint32_t offset, uint8_t data);
secbool flash_write_word_rel(uint8_t sector, uint32_t offset, uint32_t data);
secbool 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

178
embed/unix/flash.c Normal file
View File

@ -0,0 +1,178 @@
/*
* Copyright (c) Jan Pochyla, SatoshiLabs
*
* Licensed under TREZOR License
* see LICENSE file for details
*/
#include <string.h>
#include <stdio.h>
#include "../trezorhal/flash.h"
#define SECTOR_COUNT 24
static const uint32_t sector_table[SECTOR_COUNT + 1] = {
[ 0] = 0x08000000, // - 0x08003FFF | 16 KiB
[ 1] = 0x08004000, // - 0x08007FFF | 16 KiB
[ 2] = 0x08008000, // - 0x0800BFFF | 16 KiB
[ 3] = 0x0800C000, // - 0x0800FFFF | 16 KiB
[ 4] = 0x08010000, // - 0x0801FFFF | 64 KiB
[ 5] = 0x08020000, // - 0x0803FFFF | 128 KiB
[ 6] = 0x08040000, // - 0x0805FFFF | 128 KiB
[ 7] = 0x08060000, // - 0x0807FFFF | 128 KiB
[ 8] = 0x08080000, // - 0x0809FFFF | 128 KiB
[ 9] = 0x080A0000, // - 0x080BFFFF | 128 KiB
[10] = 0x080C0000, // - 0x080DFFFF | 128 KiB
[11] = 0x080E0000, // - 0x080FFFFF | 128 KiB
[12] = 0x08100000, // - 0x08103FFF | 16 KiB
[13] = 0x08104000, // - 0x08107FFF | 16 KiB
[14] = 0x08108000, // - 0x0810BFFF | 16 KiB
[15] = 0x0810C000, // - 0x0810FFFF | 16 KiB
[16] = 0x08110000, // - 0x0811FFFF | 64 KiB
[17] = 0x08120000, // - 0x0813FFFF | 128 KiB
[18] = 0x08140000, // - 0x0815FFFF | 128 KiB
[19] = 0x08160000, // - 0x0817FFFF | 128 KiB
[20] = 0x08180000, // - 0x0819FFFF | 128 KiB
[21] = 0x081A0000, // - 0x081BFFFF | 128 KiB
[22] = 0x081C0000, // - 0x081DFFFF | 128 KiB
[23] = 0x081E0000, // - 0x081FFFFF | 128 KiB
[24] = 0x08200000, // last element - not a valid sector
};
#define FLASH_FILE "tmp"
static uint8_t flash_buffer[0x200000];
static void flash_sync(void)
{
FILE *f = fopen(FLASH_FILE, "wb");
if (f) {
fwrite(flash_buffer, sizeof(flash_buffer), 1, f);
fclose(f);
}
}
static void flash_read(void)
{
FILE *f = fopen(FLASH_FILE, "rb");
if (f) {
fread(flash_buffer, sizeof(flash_buffer), 1, f);
fclose(f);
}
}
static void *sector_ptr(uint8_t sector, uint32_t offset, uint32_t size)
{
if (sector >= SECTOR_COUNT) {
return NULL;
}
uint32_t sector_size = sector_table[sector + 1] - sector_table[sector];
if (offset + size > sector_size) {
return NULL;
}
uint32_t sector_offset = sector_table[sector] - sector_table[0];
return flash_buffer + sector_offset + offset;
}
int flash_init(void)
{
flash_read();
return 0;
}
void flash_set_option_bytes(void)
{
}
bool flash_unlock(void)
{
return true;
}
bool flash_lock(void)
{
return true;
}
bool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len))
{
if (progress) {
progress(0, len);
}
for (int i = 0; i < len; i++) {
uint8_t sector = sectors[i];
uint32_t offset = sector_table[sector] - sector_table[0];
uint32_t size = sector_table[sector + 1] - sector_table[sector];
memset(flash_buffer + offset, 0xFF, size);
if (progress) {
progress(i + 1, len);
}
flash_sync();
}
return true;
}
bool flash_write_byte_rel(uint8_t sector, uint32_t offset, uint8_t data)
{
uint8_t *flash = sector_ptr(sector, offset, sizeof(data));
if (!flash) {
return false;
}
if ((flash[0] & data) != data) {
return false; // we cannot change zeros to ones
}
flash[0] = data;
flash_sync();
return true;
}
bool flash_write_word_rel(uint8_t sector, uint32_t offset, uint32_t data)
{
if (offset % 4) { // we write only at 4-byte boundary
return false;
}
uint32_t *flash = sector_ptr(sector, offset, sizeof(data));
if (!flash) {
return false;
}
if ((flash[0] & data) != data) {
return false; // we cannot change zeros to ones
}
flash[0] = data;
flash_sync();
return true;
}
bool flash_read_word_rel(uint8_t sector, uint32_t offset, uint32_t *data)
{
if (offset % 4) { // we read only at 4-byte boundary
return false;
}
uint32_t *flash = sector_ptr(sector, offset, sizeof(data));
if (!flash) {
return false;
}
data[0] = flash[0];
return true;
}
bool flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data, uint8_t datalen)
{
return false;
}
bool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint8_t datalen)
{
return false;
}
bool flash_otp_lock(uint8_t block)
{
return false;
}
bool flash_otp_is_locked(uint8_t block)
{
return false;
}