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

310 lines
8.3 KiB
C
Raw Normal View History

2018-02-26 13:06:10 +00:00
/*
* This file is part of the TREZOR project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2017-10-18 16:36:26 +00:00
#include <string.h>
#include "norcow.h"
2017-12-08 10:42:18 +00:00
#include "common.h"
2017-12-14 23:06:09 +00:00
#include "flash.h"
2017-10-18 16:36:26 +00:00
#ifndef NORCOW_SECTORS
2017-12-09 17:18:42 +00:00
#define NORCOW_SECTORS {FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2}
2017-10-18 16:36:26 +00:00
#endif
2017-12-08 10:42:18 +00:00
// NRCW = 4e524357
#define NORCOW_MAGIC ((uint32_t)0x5743524e)
#define NORCOW_MAGIC_LEN (sizeof(uint32_t))
static const uint8_t norcow_sectors[NORCOW_SECTOR_COUNT] = NORCOW_SECTORS;
2017-10-18 16:36:26 +00:00
static uint8_t norcow_active_sector = 0;
2017-12-08 10:42:18 +00:00
static uint32_t norcow_active_offset = NORCOW_MAGIC_LEN;
2017-10-18 16:36:26 +00:00
/*
* 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)
{
2017-12-08 10:42:18 +00:00
ensure(sectrue * (sector <= NORCOW_SECTOR_COUNT), "invalid sector");
return flash_get_address(norcow_sectors[sector], offset, size);
2017-10-18 16:36:26 +00:00
}
/*
* Writes data to given sector, starting from offset
*/
2017-10-27 15:49:30 +00:00
static secbool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, const uint8_t *data, uint16_t len)
2017-10-18 16:36:26 +00:00
{
if (sector >= NORCOW_SECTOR_COUNT) {
2017-10-27 15:49:30 +00:00
return secfalse;
2017-10-18 16:36:26 +00:00
}
ensure(flash_unlock(), NULL);
// write prefix
ensure(flash_write_word(norcow_sectors[sector], offset, prefix), NULL);
2017-12-08 10:42:18 +00:00
if (len > 0) {
offset += sizeof(uint32_t);
// write data
for (uint16_t i = 0; i < len; i++, offset++) {
ensure(flash_write_byte(norcow_sectors[sector], offset, data[i]), NULL);
2017-10-18 16:36:26 +00:00
}
2017-12-08 10:42:18 +00:00
// pad with zeroes
for (; offset % 4; offset++) {
ensure(flash_write_byte(norcow_sectors[sector], offset, 0x00), NULL);
}
2017-10-18 16:36:26 +00:00
}
ensure(flash_lock(), NULL);
2017-10-27 15:49:30 +00:00
return sectrue;
2017-10-18 16:36:26 +00:00
}
2017-12-08 10:42:18 +00:00
/*
* Erases sector (and sets a magic)
*/
static void norcow_erase(uint8_t sector, secbool set_magic)
{
ensure(sectrue * (sector <= NORCOW_SECTOR_COUNT), "invalid sector");
ensure(flash_erase_sector(norcow_sectors[sector]), "erase failed");
2017-12-08 10:42:18 +00:00
if (sectrue == set_magic) {
2017-12-08 15:15:19 +00:00
ensure(norcow_write(sector, 0, NORCOW_MAGIC, NULL, 0), "set magic failed");
2017-12-08 10:42:18 +00:00
}
}
2017-10-18 16:36:26 +00:00
#define ALIGN4(X) (X) = ((X) + 3) & ~3
/*
* Reads one item starting from offset
*/
2017-10-27 15:49:30 +00:00
static secbool read_item(uint8_t sector, uint32_t offset, uint16_t *key, const void **val, uint16_t *len, uint32_t *pos)
2017-10-18 16:36:26 +00:00
{
*pos = offset;
const void *k = norcow_ptr(sector, *pos, 2);
2017-10-27 15:49:30 +00:00
if (k == NULL) return secfalse;
2017-10-18 16:36:26 +00:00
*pos += 2;
memcpy(key, k, sizeof(uint16_t));
if (*key == 0xFFFF) {
2017-10-27 15:49:30 +00:00
return secfalse;
2017-10-18 16:36:26 +00:00
}
const void *l = norcow_ptr(sector, *pos, 2);
2017-10-27 15:49:30 +00:00
if (l == NULL) return secfalse;
2017-10-18 16:36:26 +00:00
*pos += 2;
memcpy(len, l, sizeof(uint16_t));
*val = norcow_ptr(sector, *pos, *len);
2017-10-27 15:49:30 +00:00
if (*val == NULL) return secfalse;
2017-10-18 16:36:26 +00:00
*pos += *len;
ALIGN4(*pos);
2017-10-27 15:49:30 +00:00
return sectrue;
2017-10-18 16:36:26 +00:00
}
/*
* Writes one item starting from offset
*/
2017-10-27 15:49:30 +00:00
static secbool write_item(uint8_t sector, uint32_t offset, uint16_t key, const void *val, uint16_t len, uint32_t *pos)
2017-10-18 16:36:26 +00:00
{
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
*/
2017-10-27 15:49:30 +00:00
static secbool find_item(uint8_t sector, uint16_t key, const void **val, uint16_t *len)
2017-10-18 16:36:26 +00:00
{
*val = 0;
*len = 0;
2017-12-08 10:42:18 +00:00
uint32_t offset = NORCOW_MAGIC_LEN;
2017-10-18 16:36:26 +00:00
for (;;) {
uint16_t k, l;
const void *v;
uint32_t pos;
2017-10-27 15:49:30 +00:00
if (sectrue != read_item(sector, offset, &k, &v, &l, &pos)) {
break;
}
2017-10-18 16:36:26 +00:00
if (key == k) {
*val = v;
*len = l;
}
offset = pos;
}
2017-10-27 15:49:30 +00:00
return sectrue * (*val != NULL);
2017-10-18 16:36:26 +00:00
}
/*
* Finds first unused offset in given sector
*/
static uint32_t find_free_offset(uint8_t sector)
{
2017-12-08 10:42:18 +00:00
uint32_t offset = NORCOW_MAGIC_LEN;
2017-10-18 16:36:26 +00:00
for (;;) {
uint16_t key, len;
const void *val;
uint32_t pos;
2017-10-27 15:49:30 +00:00
if (sectrue != read_item(sector, offset, &key, &val, &len, &pos)) {
break;
}
2017-10-18 16:36:26 +00:00
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;
2017-12-08 10:42:18 +00:00
norcow_erase(norcow_next_sector, sectrue);
2017-10-18 16:36:26 +00:00
2017-12-08 10:42:18 +00:00
uint32_t offset = NORCOW_MAGIC_LEN, offsetw = NORCOW_MAGIC_LEN;
2017-10-18 16:36:26 +00:00
for (;;) {
// read item
uint16_t k, l;
const void *v;
uint32_t pos;
2017-10-27 15:49:30 +00:00
secbool r = read_item(norcow_active_sector, offset, &k, &v, &l, &pos);
2017-12-08 10:42:18 +00:00
if (sectrue != r) {
break;
}
2017-10-18 16:36:26 +00:00
offset = pos;
// check if not already saved
const void *v2;
uint16_t l2;
r = find_item(norcow_next_sector, k, &v2, &l2);
2017-12-08 10:42:18 +00:00
if (sectrue == r) {
continue;
}
2017-10-18 16:36:26 +00:00
// 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);
2017-12-08 10:42:18 +00:00
if (sectrue != r) {
break;
}
2017-10-18 16:36:26 +00:00
if (k == k2) {
v = v2;
l = l2;
}
offsetr = posr;
}
// copy the last item
uint32_t posw;
2018-02-12 13:51:41 +00:00
ensure(write_item(norcow_next_sector, offsetw, k, v, l, &posw), "compaction write failed");
2017-10-18 16:36:26 +00:00
offsetw = posw;
}
2017-12-08 10:42:18 +00:00
norcow_erase(norcow_active_sector, secfalse);
2017-10-18 16:36:26 +00:00
norcow_active_sector = norcow_next_sector;
2017-12-08 15:46:13 +00:00
norcow_active_offset = find_free_offset(norcow_active_sector);
2017-10-18 16:36:26 +00:00
}
/*
* Initializes storage
*/
void norcow_init(void)
2017-10-18 16:36:26 +00:00
{
2017-12-08 10:42:18 +00:00
secbool found = secfalse;
// detect active sector - starts with magic
2017-10-18 16:36:26 +00:00
for (uint8_t i = 0; i < NORCOW_SECTOR_COUNT; i++) {
2017-12-08 10:42:18 +00:00
const uint32_t *magic = norcow_ptr(i, 0, NORCOW_MAGIC_LEN);
if (magic != NULL && *magic == NORCOW_MAGIC) {
found = sectrue;
2017-10-18 16:36:26 +00:00
norcow_active_sector = i;
break;
}
}
2017-12-08 10:42:18 +00:00
// no active sectors found - let's erase
2017-12-08 15:15:19 +00:00
if (sectrue == found) {
norcow_active_offset = find_free_offset(norcow_active_sector);
} else {
norcow_wipe();
2017-12-08 10:42:18 +00:00
}
2017-10-18 16:36:26 +00:00
}
/*
* Wipe the storage
*/
void norcow_wipe(void)
2017-10-18 16:36:26 +00:00
{
2017-12-08 10:42:18 +00:00
norcow_erase(0, sectrue);
for (uint8_t i = 1; i < NORCOW_SECTOR_COUNT; i++) {
norcow_erase(i, secfalse);
2017-10-18 16:36:26 +00:00
}
norcow_active_sector = 0;
2017-12-08 10:42:18 +00:00
norcow_active_offset = NORCOW_MAGIC_LEN;
2017-10-18 16:36:26 +00:00
}
/*
* Looks for the given key, returns status of the operation
*/
2017-10-27 15:49:30 +00:00
secbool norcow_get(uint16_t key, const void **val, uint16_t *len)
2017-10-18 16:36:26 +00:00
{
return find_item(norcow_active_sector, key, val, len);
}
/*
* Sets the given key, returns status of the operation
*/
2017-10-27 15:49:30 +00:00
secbool norcow_set(uint16_t key, const void *val, uint16_t len)
2017-10-18 16:36:26 +00:00
{
// 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;
2017-10-27 15:49:30 +00:00
secbool r = write_item(norcow_active_sector, norcow_active_offset, key, val, len, &pos);
if (sectrue == r) {
2017-10-18 16:36:26 +00:00
norcow_active_offset = pos;
}
return r;
}
/*
* Update a word in flash at the given pointer. The pointer must point
* into the NORCOW area.
*/
secbool norcow_update(uint16_t key, uint16_t offset, uint32_t value)
{
const void *ptr;
uint16_t len;
if (sectrue != find_item(norcow_active_sector, key, &ptr, &len)) {
return secfalse;
}
if ((offset & 3) != 0 || offset >= len) {
return secfalse;
}
uint32_t sector_offset = (const uint8_t*) ptr - (const uint8_t *)norcow_ptr(norcow_active_sector, 0, NORCOW_SECTOR_SIZE) + offset;
ensure(flash_unlock(), NULL);
ensure(flash_write_word(norcow_sectors[norcow_active_sector], sector_offset, value), NULL);
ensure(flash_lock(), NULL);
return sectrue;
}