Skip to content

Commit

Permalink
chore(core, legacy, storage): Refactor flash drivers
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
cepetr committed Nov 14, 2023
1 parent d693e1b commit cabba22
Show file tree
Hide file tree
Showing 47 changed files with 919 additions and 1,013 deletions.
1 change: 1 addition & 0 deletions core/SConscript.boardloader
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SOURCE_MOD += [
'vendor/trezor-crypto/ed25519-donna/modm-donna-32bit.c',
'vendor/trezor-crypto/memzero.c',
'vendor/trezor-crypto/sha2.c',
'vendor/trezor-storage/flash_area.c',
]

# modtrezorui
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.bootloader
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ SOURCE_MOD += [
'vendor/trezor-crypto/memzero.c',
'vendor/trezor-crypto/rand.c',
'vendor/trezor-crypto/sha2.c',
'vendor/trezor-storage/flash_area.c',
]

# modtrezorui
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.bootloader_ci
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ SOURCE_MOD += [
'vendor/trezor-crypto/memzero.c',
'vendor/trezor-crypto/rand.c',
'vendor/trezor-crypto/sha2.c',
'vendor/trezor-storage/flash_area.c',
]

# modtrezorui
Expand Down
7 changes: 2 additions & 5 deletions core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,9 @@ SOURCE_MOD += [
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
'vendor/micropython/lib/uzlib/tinflate.c',
'vendor/trezor-storage/flash_area.c',
]

if TREZOR_MODEL in ('1', 'T', 'R', 'DISC1'):
SOURCE_MOD += [
'vendor/trezor-storage/flash_common_f4.c',
]

if TREZOR_MODEL in ('1', ):
SOURCE_MOD += [
'embed/models/model_T1B1_layout.c',
Expand Down Expand Up @@ -128,6 +124,7 @@ SOURCE_TREZORHAL = [
'embed/trezorhal/unix/boot_args.c',
'embed/trezorhal/unix/display-unix.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/flash_otp.c',
'embed/trezorhal/unix/common.c',
'embed/trezorhal/unix/touch/touch.c',
'embed/trezorhal/unix/rng.c',
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.firmware
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ SOURCE_MOD += [
'embed/extmod/modtrezorconfig/modtrezorconfig.c',
'vendor/trezor-storage/norcow.c',
'vendor/trezor-storage/storage.c',
'vendor/trezor-storage/flash_area.c',
]

# modtrezorcrypto
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.prodtest
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ SOURCE_MOD += [
'vendor/trezor-crypto/secp256k1.c',
'vendor/trezor-crypto/sha2.c',
'vendor/trezor-crypto/tls_prf.c',
'vendor/trezor-storage/flash_area.c',
]

# modtrezorui
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.reflash
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ SOURCE_MOD += [
'vendor/micropython/lib/uzlib/adler32.c',
'vendor/micropython/lib/uzlib/crc32.c',
'vendor/micropython/lib/uzlib/tinflate.c',
'vendor/trezor-storage/flash_area.c',
]

# fonts
Expand Down
7 changes: 2 additions & 5 deletions core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@ SOURCE_MOD += [
'embed/extmod/modtrezorconfig/modtrezorconfig.c',
'vendor/trezor-storage/norcow.c',
'vendor/trezor-storage/storage.c',
'vendor/trezor-storage/flash_area.c',
]

if TREZOR_MODEL in ('1', 'T', 'R', 'DISC1'):
SOURCE_MOD += [
'vendor/trezor-storage/flash_common_f4.c',
]

# modtrezorcrypto
CCFLAGS_MOD += '-Wno-sequence-point '
CPPPATH_MOD += [
Expand Down Expand Up @@ -376,6 +372,7 @@ SOURCE_UNIX = [
'embed/trezorhal/unix/common.c',
'embed/trezorhal/unix/display-unix.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/flash_otp.c',
'embed/trezorhal/unix/random_delays.c',
'embed/trezorhal/unix/rng.c',
'embed/trezorhal/unix/usb.c',
Expand Down
3 changes: 3 additions & 0 deletions core/embed/bootloader/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "common.h"
#include "display.h"
#include "flash.h"
#include "flash_otp.h"
#include "model.h"
#include "rust_ui.h"
#ifdef USE_OPTIGA
Expand Down Expand Up @@ -111,6 +112,8 @@ __attribute__((noreturn)) void display_error_and_die(const char *message,

__attribute__((noreturn)) int main(int argc, char **argv) {
flash_init();
flash_otp_init();

FIRMWARE_START = (uint8_t *)flash_area_get_address(&FIRMWARE_AREA, 0, 0);

// simulate non-empty storage so that we know whether it was erased or not
Expand Down
1 change: 1 addition & 0 deletions core/embed/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "common.h"
#include "display.h"
#include "flash.h"
#include "flash_otp.h"
#include "image.h"
#include "lowlevel.h"
#include "messages.pb.h"
Expand Down
2 changes: 1 addition & 1 deletion core/embed/extmod/modtrezorio/modtrezorio-flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "flash.h"
#include "flash_otp.h"

#include "embed/extmod/trezorobj.h"

Expand Down
2 changes: 1 addition & 1 deletion core/embed/lib/unit_variant.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "unit_variant.h"
#include "flash.h"
#include "flash_otp.h"
#include "model.h"

static uint8_t unit_variant_color = 0;
Expand Down
2 changes: 1 addition & 1 deletion core/embed/models/layout_common.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef LAYOUT_COMMON_H
#define LAYOUT_COMMON_H

#include "flash.h"
#include "flash_area.h"

// OTP blocks allocation
#define FLASH_OTP_BLOCK_BATCH 0
Expand Down
9 changes: 0 additions & 9 deletions core/embed/models/model_D002_layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = {
{
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 0x18,
Expand All @@ -13,7 +12,6 @@ const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = {
},
{
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 0x20,
Expand All @@ -24,7 +22,6 @@ const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = {

const flash_area_t BOARDLOADER_AREA = {
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 1,
Expand All @@ -34,7 +31,6 @@ const flash_area_t BOARDLOADER_AREA = {

const flash_area_t BOOTLOADER_AREA = {
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 0x08,
Expand All @@ -44,7 +40,6 @@ const flash_area_t BOOTLOADER_AREA = {

const flash_area_t FIRMWARE_AREA = {
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 0x28,
Expand All @@ -54,7 +49,6 @@ const flash_area_t FIRMWARE_AREA = {

const flash_area_t SECRET_AREA = {
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 0,
Expand All @@ -64,7 +58,6 @@ const flash_area_t SECRET_AREA = {

const flash_area_t BHK_AREA = {
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 1,
Expand All @@ -74,7 +67,6 @@ const flash_area_t BHK_AREA = {

const flash_area_t WIPE_AREA = {
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 0x18,
Expand All @@ -84,7 +76,6 @@ const flash_area_t WIPE_AREA = {

const flash_area_t ALL_WIPE_AREA = {
.num_subareas = 1,
.secure_area = true,
.subarea[0] =
{
.first_sector = 0x08,
Expand Down
42 changes: 2 additions & 40 deletions core/embed/trezorhal/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,12 @@

#include <stdint.h>
#include <stdlib.h>

#include "platform.h"
#include "secbool.h"

#define FLASH_OTP_NUM_BLOCKS 16
#define FLASH_OTP_BLOCK_SIZE 32

/**
* Flash driver interface is designed to abstract away differences between
* various MCUs used in Trezor devices.
*
* Generally, flash memory is divided into sectors. On different MCUs, sectors
* may have different sizes, and therefore, different number of sectors are used
* for a given purpose. For example, on STM32F4, the sectors are relatively
* large so we use single sector for Storage. On STM32U5, the sectors are
* smaller, so we use multiple sectors for the Storage. Storage implementation
* should not care about this, and should use flash_area_t interface to access
* the flash memory.
*
* flash_area_t represents a location in flash memory. It may be contiguous, or
* it may be composed of multiple non-contiguous subareas.
*
* flash_subarea_t represents a contiguous area in flash memory, specified by
* first_sector and num_sectors.
*/

#include "flash_common.h"
#include "flash_ll.h"

void flash_init(void);

uint32_t flash_wait_and_clear_status_flags(void);

// Erases the single sector in the designated flash area
// The 'offset' parameter must indicate the relative sector offset within the
// flash area If 'offset' is outside the bounds of the flash area,
// 'bytes_erased' is set to 0 otherwise, 'bytes_erased' is set to the size of
// the erased sector
secbool flash_area_erase_partial(const flash_area_t *area, uint32_t offset,
uint32_t *bytes_erased);

secbool __wur flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data,
uint8_t datalen);
secbool __wur flash_otp_write(uint8_t block, uint8_t offset,
const uint8_t *data, uint8_t datalen);
secbool __wur flash_otp_lock(uint8_t block);
secbool __wur flash_otp_is_locked(uint8_t block);

#endif // TREZORHAL_FLASH_H
18 changes: 18 additions & 0 deletions core/embed/trezorhal/flash_otp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef TREZORHAL_FLASH_OTP_H
#define TREZORHAL_FLASH_OTP_H

#include <common.h>

#define FLASH_OTP_NUM_BLOCKS 16
#define FLASH_OTP_BLOCK_SIZE 32

void flash_otp_init(void);

secbool __wur flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data,
uint8_t datalen);
secbool __wur flash_otp_write(uint8_t block, uint8_t offset,
const uint8_t *data, uint8_t datalen);
secbool __wur flash_otp_lock(uint8_t block);
secbool __wur flash_otp_is_locked(uint8_t block);

#endif // TREZORHAL_FLASH_OTP_H
2 changes: 1 addition & 1 deletion core/embed/trezorhal/stm32f4/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#ifdef FANCY_FATAL_ERROR
#include "rust_ui.h"
#endif
#include "flash.h"
#include "flash_otp.h"
#include "platform.h"
#include "rand.h"
#include "supervise.h"
Expand Down
Loading

0 comments on commit cabba22

Please sign in to comment.