commit
f88080b904
@ -1 +0,0 @@
|
||||
../../../vendor/norcow/norcow.c
|
@ -0,0 +1,262 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "norcow.h"
|
||||
|
||||
#include "../../trezorhal/flash.h"
|
||||
|
||||
#ifndef NORCOW_SECTORS
|
||||
#define NORCOW_SECTORS {4, 16}
|
||||
#endif
|
||||
|
||||
static uint8_t norcow_sectors[NORCOW_SECTOR_COUNT] = NORCOW_SECTORS;
|
||||
static uint8_t norcow_active_sector = 0;
|
||||
static uint32_t norcow_active_offset = 0;
|
||||
|
||||
/*
|
||||
* Erases sector
|
||||
*/
|
||||
static secbool norcow_erase(uint8_t sector)
|
||||
{
|
||||
if (sector >= NORCOW_SECTOR_COUNT) {
|
||||
return secfalse;
|
||||
}
|
||||
return flash_erase_sectors(&norcow_sectors[sector], 1, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns pointer to sector, starting with offset
|
||||
* Fails when there is not enough space for data of given size
|
||||
*/
|
||||
static const void *norcow_ptr(uint8_t sector, uint32_t offset, uint32_t size)
|
||||
{
|
||||
if (sector >= NORCOW_SECTOR_COUNT) {
|
||||
return NULL;
|
||||
}
|
||||
return flash_get_address(norcow_sectors[sector], offset, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Writes data to given sector, starting from offset
|
||||
*/
|
||||
static secbool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, const uint8_t *data, uint16_t len)
|
||||
{
|
||||
if (sector >= NORCOW_SECTOR_COUNT) {
|
||||
return secfalse;
|
||||
}
|
||||
if (sectrue != flash_unlock()) {
|
||||
return secfalse;
|
||||
}
|
||||
// write prefix
|
||||
if (sectrue != flash_write_word_rel(norcow_sectors[sector], offset, prefix)) {
|
||||
flash_lock();
|
||||
return secfalse;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
// pad with zeroes
|
||||
for (; offset % 4; offset++) {
|
||||
if (sectrue != flash_write_byte_rel(norcow_sectors[sector], offset, 0x00)) {
|
||||
flash_lock();
|
||||
return secfalse;
|
||||
}
|
||||
}
|
||||
flash_lock();
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
#define ALIGN4(X) (X) = ((X) + 3) & ~3
|
||||
|
||||
/*
|
||||
* Reads one item starting from offset
|
||||
*/
|
||||
static secbool read_item(uint8_t sector, uint32_t offset, uint16_t *key, const void **val, uint16_t *len, uint32_t *pos)
|
||||
{
|
||||
*pos = offset;
|
||||
|
||||
const void *k = norcow_ptr(sector, *pos, 2);
|
||||
if (k == NULL) return secfalse;
|
||||
*pos += 2;
|
||||
memcpy(key, k, sizeof(uint16_t));
|
||||
if (*key == 0xFFFF) {
|
||||
return secfalse;
|
||||
}
|
||||
|
||||
const void *l = norcow_ptr(sector, *pos, 2);
|
||||
if (l == NULL) return secfalse;
|
||||
*pos += 2;
|
||||
memcpy(len, l, sizeof(uint16_t));
|
||||
|
||||
*val = norcow_ptr(sector, *pos, *len);
|
||||
if (*val == NULL) return secfalse;
|
||||
*pos += *len;
|
||||
ALIGN4(*pos);
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Writes one item starting from offset
|
||||
*/
|
||||
static secbool write_item(uint8_t sector, uint32_t offset, uint16_t key, const void *val, uint16_t len, uint32_t *pos)
|
||||
{
|
||||
uint32_t prefix = (len << 16) | key;
|
||||
*pos = offset + sizeof(uint32_t) + len;
|
||||
ALIGN4(*pos);
|
||||
return norcow_write(sector, offset, prefix, val, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Finds item in given sector
|
||||
*/
|
||||
static secbool find_item(uint8_t sector, uint16_t key, const void **val, uint16_t *len)
|
||||
{
|
||||
*val = 0;
|
||||
*len = 0;
|
||||
uint32_t offset = 0;
|
||||
for (;;) {
|
||||
uint16_t k, l;
|
||||
const void *v;
|
||||
uint32_t pos;
|
||||
if (sectrue != read_item(sector, offset, &k, &v, &l, &pos)) {
|
||||
break;
|
||||
}
|
||||
if (key == k) {
|
||||
*val = v;
|
||||
*len = l;
|
||||
}
|
||||
offset = pos;
|
||||
}
|
||||
return sectrue * (*val != NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Finds first unused offset in given sector
|
||||
*/
|
||||
static uint32_t find_free_offset(uint8_t sector)
|
||||
{
|
||||
uint32_t offset = 0;
|
||||
for (;;) {
|
||||
uint16_t key, len;
|
||||
const void *val;
|
||||
uint32_t pos;
|
||||
if (sectrue != read_item(sector, offset, &key, &val, &len, &pos)) {
|
||||
break;
|
||||
}
|
||||
offset = pos;
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compacts active sector and sets new active sector
|
||||
*/
|
||||
static void compact()
|
||||
{
|
||||
uint8_t norcow_next_sector = (norcow_active_sector + 1) % NORCOW_SECTOR_COUNT;
|
||||
|
||||
uint32_t offset = 0, offsetw = 0;
|
||||
|
||||
for (;;) {
|
||||
// read item
|
||||
uint16_t k, l;
|
||||
const void *v;
|
||||
uint32_t pos;
|
||||
secbool r = read_item(norcow_active_sector, offset, &k, &v, &l, &pos);
|
||||
if (sectrue != r) break;
|
||||
offset = pos;
|
||||
|
||||
// check if not already saved
|
||||
const void *v2;
|
||||
uint16_t l2;
|
||||
r = find_item(norcow_next_sector, k, &v2, &l2);
|
||||
if (sectrue == r) continue;
|
||||
|
||||
// scan for latest instance
|
||||
uint32_t offsetr = offset;
|
||||
for (;;) {
|
||||
uint16_t k2;
|
||||
uint32_t posr;
|
||||
r = read_item(norcow_active_sector, offsetr, &k2, &v2, &l2, &posr);
|
||||
if (sectrue != r) break;
|
||||
if (k == k2) {
|
||||
v = v2;
|
||||
l = l2;
|
||||
}
|
||||
offsetr = posr;
|
||||
}
|
||||
|
||||
// copy the last item
|
||||
uint32_t posw;
|
||||
r = write_item(norcow_next_sector, offsetw, k, v, l, &posw);
|
||||
if (sectrue != r) { } // TODO: error
|
||||
offsetw = posw;
|
||||
}
|
||||
|
||||
norcow_erase(norcow_active_sector);
|
||||
norcow_active_sector = norcow_next_sector;
|
||||
norcow_active_offset = find_free_offset(norcow_active_sector);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initializes storage
|
||||
*/
|
||||
secbool norcow_init(void)
|
||||
{
|
||||
// detect active sector (inactive sectors are empty = start with 0xFF)
|
||||
for (uint8_t i = 0; i < NORCOW_SECTOR_COUNT; i++) {
|
||||
const uint8_t *b = norcow_ptr(i, 0, 1);
|
||||
if (b != NULL && *b != 0xFF) {
|
||||
norcow_active_sector = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
norcow_active_offset = find_free_offset(norcow_active_sector);
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Wipe the storage
|
||||
*/
|
||||
secbool norcow_wipe(void)
|
||||
{
|
||||
for (uint8_t i = 0; i < NORCOW_SECTOR_COUNT; i++) {
|
||||
if (sectrue != norcow_erase(i)) {
|
||||
return secfalse;
|
||||
}
|
||||
}
|
||||
norcow_active_sector = 0;
|
||||
norcow_active_offset = 0;
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Looks for the given key, returns status of the operation
|
||||
*/
|
||||
secbool norcow_get(uint16_t key, const void **val, uint16_t *len)
|
||||
{
|
||||
return find_item(norcow_active_sector, key, val, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets the given key, returns status of the operation
|
||||
*/
|
||||
secbool norcow_set(uint16_t key, const void *val, uint16_t len)
|
||||
{
|
||||
// check whether there is enough free space
|
||||
// and compact if full
|
||||
if (norcow_active_offset + sizeof(uint32_t) + len > NORCOW_SECTOR_SIZE) {
|
||||
compact();
|
||||
}
|
||||
// write item
|
||||
uint32_t pos;
|
||||
secbool r = write_item(norcow_active_sector, norcow_active_offset, key, val, len, &pos);
|
||||
if (sectrue == r) {
|
||||
norcow_active_offset = pos;
|
||||
}
|
||||
return r;
|
||||
}
|
@ -1 +0,0 @@
|
||||
../../../vendor/norcow/norcow.h
|
@ -0,0 +1,34 @@
|
||||
#ifndef __NORCOW_H__
|
||||
#define __NORCOW_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include "../../trezorhal/secbool.h"
|
||||
|
||||
/*
|
||||
* Storage parameters:
|
||||
*/
|
||||
|
||||
#define NORCOW_SECTOR_COUNT 2
|
||||
#define NORCOW_SECTOR_SIZE (64*1024)
|
||||
|
||||
/*
|
||||
* Initialize storage
|
||||
*/
|
||||
secbool norcow_init(void);
|
||||
|
||||
/*
|
||||
* Wipe the storage
|
||||
*/
|
||||
secbool norcow_wipe(void);
|
||||
|
||||
/*
|
||||
* Looks for the given key, returns status of the operation
|
||||
*/
|
||||
secbool norcow_get(uint16_t key, const void **val, uint16_t *len);
|
||||
|
||||
/*
|
||||
* Sets the given key, returns status of the operation
|
||||
*/
|
||||
secbool norcow_set(uint16_t key, const void *val, uint16_t len);
|
||||
|
||||
#endif
|
@ -1,18 +0,0 @@
|
||||
#if defined TREZOR_STM32
|
||||
|
||||
#define NORCOW_STM32 1
|
||||
|
||||
#define NORCOW_SECTORS {4, 16}
|
||||
#define NORCOW_ADDRESSES {0x08010000, 0x08110000}
|
||||
|
||||
#elif defined TREZOR_UNIX
|
||||
|
||||
#define NORCOW_UNIX 1
|
||||
|
||||
#define NORCOW_FILE "/var/tmp/trezor.config"
|
||||
|
||||
#else
|
||||
|
||||
#error Unsupported TREZOR port. Only STM32 and UNIX ports are supported.
|
||||
|
||||
#endif
|
@ -0,0 +1,215 @@
|
||||
/*
|
||||
* Copyright (c) Pavol Rusnak, Jan Pochyla, SatoshiLabs
|
||||
*
|
||||
* Licensed under TREZOR License
|
||||
* see LICENSE file for details
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "norcow.h"
|
||||
#include "../../trezorhal/flash.h"
|
||||
|
||||
// Byte-length of flash sector containing fail counters.
|
||||
#define PIN_SECTOR_SIZE 0x4000
|
||||
|
||||
// Maximum number of failed unlock attempts.
|
||||
#define PIN_MAX_TRIES 15
|
||||
|
||||
// Norcow storage key of configured PIN.
|
||||
#define PIN_KEY 0x0000
|
||||
|
||||
// Maximum PIN length.
|
||||
#define PIN_MAXLEN 32
|
||||
|
||||
static secbool initialized = secfalse;
|
||||
static secbool unlocked = secfalse;
|
||||
|
||||
secbool storage_init(void)
|
||||
{
|
||||
initialized = secfalse;
|
||||
unlocked = secfalse;
|
||||
if (sectrue != flash_init()) {
|
||||
return secfalse;
|
||||
}
|
||||
if (sectrue != norcow_init()) {
|
||||
return secfalse;
|
||||
}
|
||||
initialized = sectrue;
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
static void pin_fails_reset(uint32_t ofs)
|
||||
{
|
||||
if (ofs + sizeof(uint32_t) >= PIN_SECTOR_SIZE) {
|
||||
// ofs points to the last word of the PIN fails area. Because there is
|
||||
// no space left, we recycle the sector (set all words to 0xffffffff).
|
||||
// On next unlock attempt, we start counting from the the first word.
|
||||
flash_erase_sectors((uint8_t[]) { FLASH_SECTOR_PIN_AREA }, 1, NULL);
|
||||
} else {
|
||||
// Mark this counter as exhausted. On next unlock attempt, pinfails_get
|
||||
// seeks to the next word.
|
||||
flash_unlock();
|
||||
flash_write_word_rel(FLASH_SECTOR_PIN_AREA, ofs, 0);
|
||||
flash_lock();
|
||||
}
|
||||
}
|
||||
|
||||
static secbool pin_fails_increase(uint32_t ofs)
|
||||
{
|
||||
uint32_t ctr = ~PIN_MAX_TRIES;
|
||||
if (sectrue != flash_read_word_rel(FLASH_SECTOR_PIN_AREA, ofs, &ctr)) {
|
||||
return secfalse;
|
||||
}
|
||||
ctr = ctr << 1;
|
||||
|
||||
flash_unlock();
|
||||
if (sectrue != flash_write_word_rel(FLASH_SECTOR_PIN_AREA, ofs, ctr)) {
|
||||
flash_lock();
|
||||
return secfalse;
|
||||
}
|
||||
flash_lock();
|
||||
|
||||
uint32_t check = 0;
|
||||
if (sectrue != flash_read_word_rel(FLASH_SECTOR_PIN_AREA, ofs, &check)) {
|
||||
return secfalse;
|
||||
}
|
||||
if (ctr != check) {
|
||||
return secfalse;
|
||||
}
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
static void pin_fails_check_max(uint32_t ctr)
|
||||
{
|
||||
if (~ctr >= 1 << PIN_MAX_TRIES) {
|
||||
for (;;) {
|
||||
if (norcow_wipe()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
static secbool pin_fails_read(uint32_t *ofs, uint32_t *ctr)
|
||||
{
|
||||
if (NULL == ofs || NULL == ctr) {
|
||||
return secfalse;
|
||||
}
|
||||
for (uint32_t o = 0; o < PIN_SECTOR_SIZE; o += sizeof(uint32_t)) {
|
||||
uint32_t c = 0;
|
||||
if (!flash_read_word_rel(FLASH_SECTOR_PIN_AREA, o, &c)) {
|
||||
return secfalse;
|
||||
}
|
||||
if (c != 0) {
|
||||
*ofs = o;
|
||||
*ctr = c;
|
||||
return sectrue;
|
||||
}
|
||||
}
|
||||
return secfalse;
|
||||
}
|
||||
|
||||
static secbool const_cmp(const uint8_t *pub, size_t publen, const uint8_t *sec, size_t seclen)
|
||||
{
|
||||
size_t diff = seclen ^ publen;
|
||||
for (size_t i = 0; i < publen; i++) {
|
||||
diff |= pub[i] ^ sec[i];
|
||||
}
|
||||
return sectrue * (0 == diff);
|
||||
}
|
||||
|
||||
static secbool pin_cmp(const uint8_t *pin, size_t pinlen)
|
||||
{
|
||||
const void *spin = NULL;
|
||||
uint16_t spinlen = 0;
|
||||
norcow_get(PIN_KEY, &spin, &spinlen);
|
||||
if (NULL != spin) {
|
||||
return const_cmp(pin, pinlen, spin, spinlen);
|
||||
} else {
|
||||
return sectrue * (0 == pinlen);
|
||||
}
|
||||
}
|
||||
|
||||
static secbool pin_check(const uint8_t *pin, size_t len)
|
||||
{
|
||||
uint32_t ofs;
|
||||
uint32_t ctr;
|
||||
if (sectrue != pin_fails_read(&ofs, &ctr)) {
|
||||
return secfalse;
|
||||
}
|
||||
pin_fails_check_max(ctr);
|
||||
|
||||
// Sleep for ~ctr seconds before checking the PIN.
|
||||
for (uint32_t wait = ~ctr; wait > 0; wait--) {
|
||||
hal_delay(1000);
|
||||
}
|
||||
|
||||
// First, we increase PIN fail counter in storage, even before checking the
|
||||
// PIN. If the PIN is correct, we reset the counter afterwards. If not, we
|
||||
// check if this is the last allowed attempt.
|
||||
if (sectrue != pin_fails_increase(ofs)) {
|
||||
return secfalse;
|
||||
}
|
||||
if (sectrue != pin_cmp(pin, len)) {
|
||||
pin_fails_check_max(ctr << 1);
|
||||
return secfalse;
|
||||
}
|
||||
pin_fails_reset(ofs);
|
||||
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
secbool storage_unlock(const uint8_t *pin, size_t len)
|
||||
{
|
||||
unlocked = secfalse;
|
||||
if (sectrue == initialized && sectrue == pin_check(pin, len)) {
|
||||
unlocked = sectrue;
|
||||
}
|
||||
return unlocked;
|
||||
}
|
||||
|
||||
secbool storage_get(uint16_t key, const void **val, uint16_t *len)
|
||||
{
|
||||
if (sectrue != initialized || sectrue != unlocked || PIN_KEY == key) {
|
||||
return secfalse;
|
||||
}
|
||||
return norcow_get(key, val, len);
|
||||
}
|
||||
|
||||
secbool storage_set(uint16_t key, const void *val, uint16_t len)
|
||||
{
|
||||
if (sectrue != initialized || sectrue != unlocked || PIN_KEY == key) {
|
||||
return secfalse;
|
||||
}
|
||||
return norcow_set(key, val, len);
|
||||
}
|
||||
|
||||
secbool storage_has_pin(void)
|
||||
{
|
||||
if (sectrue != initialized) {
|
||||
return secfalse;
|
||||
}
|
||||
const void *spin = NULL;
|
||||
uint16_t spinlen = 0;
|
||||
norcow_get(PIN_KEY, &spin, &spinlen);
|
||||
return sectrue * (0 != spinlen);
|
||||
}
|
||||
|
||||
secbool storage_change_pin(const uint8_t *pin, size_t len, const uint8_t *newpin, size_t newlen)
|
||||
{
|
||||
if (sectrue != initialized || sectrue != unlocked || newlen > PIN_MAXLEN) {
|
||||
return secfalse;
|
||||
}
|
||||
if (sectrue != pin_check(pin, len)) {
|
||||
return secfalse;
|
||||
}
|
||||
return norcow_set(PIN_KEY, newpin, newlen);
|
||||
}
|
||||
|
||||
secbool storage_wipe(void)
|
||||
{
|
||||
return norcow_wipe();
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) Pavol Rusnak, Jan Pochyla, SatoshiLabs
|
||||
*
|
||||
* Licensed under TREZOR License
|
||||
* see LICENSE file for details
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "../../trezorhal/secbool.h"
|
||||
|
||||
secbool storage_init(void);
|
||||
secbool storage_wipe(void);
|
||||
secbool storage_unlock(const uint8_t *pin, size_t len);
|
||||
secbool storage_has_pin(void);
|
||||
secbool storage_change_pin(const uint8_t *pin, size_t len, const uint8_t *newpin, size_t newlen);
|
||||
secbool storage_get(uint16_t key, const void **val, uint16_t *len);
|
||||
secbool storage_set(uint16_t key, const void *val, uint16_t len);
|
@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright (c) Jan Pochyla, SatoshiLabs
|
||||
*
|
||||
* Licensed under TREZOR License
|
||||
* see LICENSE file for details
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../trezorhal/flash.h"
|
||||
|
||||
#ifndef FLASH_FILE
|
||||
#define FLASH_FILE "/var/tmp/trezor.config"
|
||||
#endif
|
||||
|
||||
#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
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
secbool flash_init(void)
|
||||
{
|
||||
FILE *f = fopen(FLASH_FILE, "rb");
|
||||
size_t r = 0;
|
||||
if (f) {
|
||||
r = fread(flash_buffer, sizeof(flash_buffer), 1, f);
|
||||
fclose(f);
|
||||
}
|
||||
if (r != 1) {
|
||||
memset(flash_buffer, 0xFF, sizeof(flash_buffer));
|
||||
}
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
secbool flash_unlock(void)
|
||||
{
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
secbool flash_lock(void)
|
||||
{
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size)
|
||||
{
|
||||
if (sector >= SECTOR_COUNT) {
|
||||
return NULL;
|
||||
}
|
||||
const uint32_t sector_size = sector_table[sector + 1] - sector_table[sector];
|
||||
if (offset + size > sector_size) {
|
||||
return NULL;
|
||||
}
|
||||
const uint32_t sector_offset = sector_table[sector] - sector_table[0];
|
||||
return flash_buffer + sector_offset + offset;
|
||||
}
|
||||
|
||||
secbool 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++) {
|
||||
const uint8_t sector = sectors[i];
|
||||
const uint32_t offset = sector_table[sector] - sector_table[0];
|
||||
const 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 sectrue;
|
||||
}
|
||||
|
||||
secbool flash_write_byte_rel(uint8_t sector, uint32_t offset, uint8_t data)
|
||||
{
|
||||
uint8_t *flash = (uint8_t *)flash_get_address(sector, offset, sizeof(data));
|
||||
if (!flash) {
|
||||
return secfalse;
|
||||
}
|
||||
if ((flash[0] & data) != data) {
|
||||
return secfalse; // we cannot change zeroes to ones
|
||||
}
|
||||
flash[0] = data;
|
||||
flash_sync();
|
||||
return sectrue;
|
||||
}
|
||||
|
||||
secbool flash_write_word_rel(uint8_t sector, uint32_t offset, uint32_t data)
|
||||
{
|
||||
if (offset % 4) { // we write only at 4-byte boundary
|
||||
return secfalse;
|
||||
}
|
||||
uint32_t *flash = (uint32_t *)flash_get_address(sector, offset, sizeof(data));
|
||||
if (!flash) {
|
||||
return secfalse;
|
||||
}
|
||||
if ((flash[0] & data) != data) {
|
||||
return secfalse; // we cannot change zeroes to ones
|
||||
}
|
||||
flash[0] = data;
|
||||
flash_sync();
|
||||
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;
|
||||
}
|
||||
|
||||
secbool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint8_t datalen)
|
||||
{
|
||||
return secfalse;
|
||||
}
|
||||
|
||||
secbool flash_otp_lock(uint8_t block)
|
||||
{
|
||||
return secfalse;
|
||||
}
|
||||
|
||||
secbool flash_otp_is_locked(uint8_t block)
|
||||
{
|
||||
return secfalse;
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
from trezor import config
|
||||
from trezor import loop
|
||||
from trezor import ui
|
||||
|
||||
from apps.common.request_pin import request_pin
|
||||
|
||||
|
||||
async def unlock_layout():
|
||||
while True:
|
||||
if config.has_pin():
|
||||
pin = await request_pin()
|
||||
else:
|
||||
pin = ''
|
||||
if config.unlock(pin):
|
||||
return
|
||||
else:
|
||||
await unlock_failed()
|
||||
|
||||
|
||||
async def unlock_failed():
|
||||
pass
|
||||
|
||||
|
||||
config.init()
|
||||
ui.display.backlight(ui.BACKLIGHT_DIM)
|
||||
loop.schedule(unlock_layout())
|
||||
loop.run()
|
Loading…
Reference in new issue