1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 20:19:23 +00:00

refactor(core): simplify and improve flash layout definitions

[no changelog]
This commit is contained in:
cepetr 2024-10-31 09:23:37 +01:00 committed by cepetr
parent 4af600d422
commit 9e28e96639
4 changed files with 161 additions and 169 deletions

View File

@ -1,102 +1,47 @@
/*
* 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/>.
*/
#include "flash.h"
#include "layout_helpers.h"
#include "model.h"
const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = {
{
.num_subareas = 1,
.subarea[0] =
{
.first_sector = STORAGE_1_SECTOR_START,
.num_sectors =
STORAGE_1_SECTOR_END - STORAGE_1_SECTOR_START + 1,
},
},
{
.num_subareas = 1,
.subarea[0] =
{
.first_sector = STORAGE_2_SECTOR_START,
.num_sectors =
STORAGE_2_SECTOR_END - STORAGE_2_SECTOR_START + 1,
},
},
};
// Convert sector number to address
//
// This conversion is used in static assert in definitions below
#define FLASH_SECTOR_TO_ADDR(sector) \
(FLASH_BASE + ((sector) / 12) * 0x100000 + \
(((sector) % 12) < 4 \
? ((sector) % 12) * 0x4000 \
: (((sector) % 12) < 5 ? 0x10000 : ((sector) % 12 - 4) * 0x20000)))
const flash_area_t BOARDLOADER_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = BOARDLOADER_SECTOR_START,
.num_sectors =
BOARDLOADER_SECTOR_END - BOARDLOADER_SECTOR_START + 1,
},
};
// Define all flash areas as `const flash_area_t ID = { .. };`
const flash_area_t BOOTLOADER_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = BOOTLOADER_SECTOR_START,
.num_sectors = BOOTLOADER_SECTOR_END - BOOTLOADER_SECTOR_START + 1,
},
};
const flash_area_t FIRMWARE_AREA = {
.num_subareas = 2,
.subarea[0] =
{
.first_sector = FIRMWARE_P1_SECTOR_START,
.num_sectors =
FIRMWARE_P1_SECTOR_END - FIRMWARE_P1_SECTOR_START + 1,
},
.subarea[1] =
{
.first_sector = FIRMWARE_P2_SECTOR_START,
.num_sectors =
FIRMWARE_P2_SECTOR_END - FIRMWARE_P2_SECTOR_START + 1,
},
};
DEFINE_ARRAY2_AREA(STORAGE_AREAS, STORAGE_1, STORAGE_2);
DEFINE_SINGLE_AREA(BOARDLOADER_AREA, BOARDLOADER);
DEFINE_SINGLE_AREA(BOOTLOADER_AREA, BOOTLOADER);
DEFINE_SPLIT2_AREA(FIRMWARE_AREA, FIRMWARE_P1, FIRMWARE_P2);
#ifdef SECRET_SECTOR_START
const flash_area_t SECRET_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = SECRET_SECTOR_START,
.num_sectors = SECRET_SECTOR_END - SECRET_SECTOR_START + 1,
},
};
DEFINE_SINGLE_AREA(SECRET_AREA, SECRET);
#else
const flash_area_t SECRET_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = 0,
.num_sectors = 0,
},
};
DEFINE_EMPTY_AREA(SECRET_AREA);
#endif
const flash_area_t ASSETS_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = ASSETS_SECTOR_START,
.num_sectors = ASSETS_SECTOR_END - ASSETS_SECTOR_START + 1,
},
};
const flash_area_t UNUSED_AREA = {
.num_subareas = 2,
.subarea[0] =
{
.first_sector = UNUSED_1_SECTOR_START,
.num_sectors = UNUSED_1_SECTOR_END - UNUSED_1_SECTOR_START + 1,
},
.subarea[1] =
{
.first_sector = UNUSED_2_SECTOR_START,
.num_sectors = UNUSED_2_SECTOR_END - UNUSED_2_SECTOR_START + 1,
},
};
DEFINE_SINGLE_AREA(ASSETS_AREA, ASSETS);
DEFINE_SPLIT2_AREA(UNUSED_AREA, UNUSED_1, UNUSED_2);

View File

@ -0,0 +1,90 @@
/*
* 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/>.
*/
#ifndef TREZORHAL_LAYOUT_HELPERS_H
#define TREZORHAL_LAYOUT_HELPERS_H
#ifdef TREZOR_EMULATOR
#define ENSURE_SECTOR_AT(addr, sector)
#else
// Static assertions ensuring that the flash address coresponds
// to the expected sector start. This macro is used in the
// definitions below.
#define ENSURE_SECTOR_AT(addr, sector) \
_Static_assert(FLASH_SECTOR_TO_ADDR(EVAL(sector)) == EVAL(addr), \
"Sector address mismatch")
#endif
// Helper that expands to its argument
#define EVAL(x) x
// Defines flash_subarea_t structure
#define SUBAREA(_first_sector, _end_sectors) \
{ \
.first_sector = (_first_sector), \
.num_sectors = (_end_sectors) - (_first_sector) + 1, \
}
// Defines flash area containing 1 subarea
#define DEFINE_SINGLE_AREA(id, prefix) \
ENSURE_SECTOR_AT(prefix##_START, prefix##_SECTOR_START); \
ENSURE_SECTOR_AT(prefix##_START + prefix##_MAXSIZE, \
prefix##_SECTOR_END + 1); \
const flash_area_t prefix##_AREA = { \
.num_subareas = 1, \
.subarea[0] = SUBAREA(prefix##_SECTOR_START, prefix##_SECTOR_END), \
}
// Defines flash area containing two subareas from two
// different blocks giveng by their prefixes
#define DEFINE_SPLIT2_AREA(id, prefix1, prefix2) \
ENSURE_SECTOR_AT(prefix1##_START, prefix1##_SECTOR_START); \
ENSURE_SECTOR_AT(prefix1##_START + prefix1##_MAXSIZE, \
prefix1##_SECTOR_END + 1); \
ENSURE_SECTOR_AT(prefix2##_START, prefix2##_SECTOR_START); \
ENSURE_SECTOR_AT(prefix2##_START + prefix2##_MAXSIZE, \
prefix2##_SECTOR_END + 1); \
const flash_area_t id = { \
.num_subareas = 2, \
.subarea[0] = SUBAREA(prefix1##_SECTOR_START, prefix1##_SECTOR_END), \
.subarea[1] = SUBAREA(prefix2##_SECTOR_START, prefix2##_SECTOR_END), \
}
// Defines array of two flash areas from two differenc blocks
// given by their prefixes
#define DEFINE_ARRAY2_AREA(id, prefix1, prefix2) \
ENSURE_SECTOR_AT(prefix1##_START, prefix1##_SECTOR_START); \
ENSURE_SECTOR_AT(prefix1##_START + prefix1##_MAXSIZE, \
prefix1##_SECTOR_END + 1); \
ENSURE_SECTOR_AT(prefix2##_START, prefix2##_SECTOR_START); \
ENSURE_SECTOR_AT(prefix2##_START + prefix2##_MAXSIZE, \
prefix2##_SECTOR_END + 1); \
const flash_area_t id[] = { \
{ \
.num_subareas = 1, \
.subarea[0] = SUBAREA(prefix1##_SECTOR_START, prefix1##_SECTOR_END), \
}, \
{ \
.num_subareas = 1, \
.subarea[0] = SUBAREA(prefix2##_SECTOR_START, prefix2##_SECTOR_END), \
}}
#define DEFINE_EMPTY_AREA(id) const flash_area_t id = {.num_subareas = 0}
#endif // TREZORHAL_LAYOUT_HELPERS_H

View File

@ -1,82 +1,38 @@
/*
* 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/>.
*/
#include "flash.h"
#include "layout_helpers.h"
#include "model.h"
const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = {
{
.num_subareas = 1,
.subarea[0] =
{
.first_sector = STORAGE_1_SECTOR_START,
.num_sectors =
STORAGE_1_SECTOR_END - STORAGE_1_SECTOR_START + 1,
},
},
{
.num_subareas = 1,
.subarea[0] =
{
.first_sector = STORAGE_2_SECTOR_START,
.num_sectors =
STORAGE_2_SECTOR_END - STORAGE_2_SECTOR_START + 1,
},
},
};
// Convert sector number to address
//
// This conversion is used in static assert in definitions below
#define FLASH_SECTOR_TO_ADDR(sector) (FLASH_BASE_S + (sector) * FLASH_PAGE_SIZE)
const flash_area_t BOARDLOADER_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = BOARDLOADER_SECTOR_START,
.num_sectors =
BOARDLOADER_SECTOR_END - BOARDLOADER_SECTOR_START + 1,
},
};
// Define all flash areas as `const flash_area_t ID = { .. };`
const flash_area_t BOOTLOADER_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = BOOTLOADER_SECTOR_START,
.num_sectors = BOOTLOADER_SECTOR_END - BOOTLOADER_SECTOR_START + 1,
},
};
const flash_area_t FIRMWARE_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = FIRMWARE_SECTOR_START,
.num_sectors = FIRMWARE_SECTOR_END - FIRMWARE_SECTOR_START + 1,
},
};
const flash_area_t SECRET_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = SECRET_SECTOR_START,
.num_sectors = SECRET_SECTOR_END - SECRET_SECTOR_START + 1,
},
};
const flash_area_t BHK_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = BHK_SECTOR_START,
.num_sectors = BHK_SECTOR_END - BHK_SECTOR_START + 1,
},
};
const flash_area_t ASSETS_AREA = {
.num_subareas = 1,
.subarea[0] =
{
.first_sector = ASSETS_SECTOR_START,
.num_sectors = ASSETS_SECTOR_END - ASSETS_SECTOR_START + 1,
},
};
const flash_area_t UNUSED_AREA = {
.num_subareas = 0,
};
DEFINE_ARRAY2_AREA(STORAGE_AREAS, STORAGE_1, STORAGE_2);
DEFINE_SINGLE_AREA(BOARDLOADER_AREA, BOARDLOADER);
DEFINE_SINGLE_AREA(BOOTLOADER_AREA, BOOTLOADER);
DEFINE_SINGLE_AREA(FIRMWARE_AREA, FIRMWARE);
DEFINE_SINGLE_AREA(SECRET_AREA, SECRET);
DEFINE_SINGLE_AREA(BHK_AREA, BHK);
DEFINE_SINGLE_AREA(ASSETS_AREA, ASSETS);
DEFINE_EMPTY_AREA(UNUSED_AREA);

View File

@ -0,0 +1 @@
../stm32f4/layout_helpers.h