TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
its_flash_info_internal.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3  * Copyright (c) 2020, Cypress Semiconductor Corporation. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  */
8 
9 #include "its_flash.h"
10 
11 #include "Driver_Flash.h"
12 #include "flash_layout.h"
13 #include "its_utils.h"
14 
15 #ifndef ITS_FLASH_DEV_NAME
16 #error "ITS_FLASH_DEV_NAME must be defined by the target in flash_layout.h"
17 #endif
18 
19 /* Adjust to match the size of the flash device's physical erase unit */
20 #ifndef ITS_SECTOR_SIZE
21 #error "ITS_SECTOR_SIZE must be defined by the target in flash_layout.h"
22 #endif
23 
24 /* Adjust so that the maximum required asset size will fit in one block */
25 #ifndef ITS_SECTORS_PER_BLOCK
26 #error "ITS_SECTORS_PER_BLOCK must be defined by the target in flash_layout.h"
27 #endif
28 
29 /* Adjust to match the size of the flash device's physical program unit */
30 #ifndef ITS_FLASH_PROGRAM_UNIT
31 #error "ITS_FLASH_PROGRAM_UNIT must be defined by the target in flash_layout.h"
32 #elif (ITS_FLASH_PROGRAM_UNIT < 1 || ITS_FLASH_PROGRAM_UNIT > ITS_SECTOR_SIZE)
33 #error "ITS_FLASH_PROGRAM_UNIT must be between 1 and ITS_SECTOR_SIZE inclusive"
34 #elif (ITS_FLASH_PROGRAM_UNIT & (ITS_FLASH_PROGRAM_UNIT - 1) != 0)
35 #error "ITS_FLASH_PROGRAM_UNIT must be a power of two"
36 #endif
37 
38 /* Include the correct flash interface implementation */
39 #ifdef ITS_RAM_FS
40 #include "its_flash_ram.h"
41 #ifndef ITS_RAM_FS_SIZE
42 #error "ITS_RAM_FS_SIZE must be defined by the target in flash_layout.h"
43 #endif
44 #define FLASH_INFO_INIT its_flash_ram_init
45 #define FLASH_INFO_READ its_flash_ram_read
46 #define FLASH_INFO_WRITE its_flash_ram_write
47 #define FLASH_INFO_FLUSH its_flash_ram_flush
48 #define FLASH_INFO_ERASE its_flash_ram_erase
49 
50 #elif (ITS_FLASH_PROGRAM_UNIT <= 16)
51 #include "its_flash_nor.h"
52 #define FLASH_INFO_INIT its_flash_nor_init
53 #define FLASH_INFO_READ its_flash_nor_read
54 #define FLASH_INFO_WRITE its_flash_nor_write
55 #define FLASH_INFO_FLUSH its_flash_nor_flush
56 #define FLASH_INFO_ERASE its_flash_nor_erase
57 
58 /* Require each file in the filesystem to be aligned to the program unit */
59 #define ITS_FLASH_ALIGNMENT ITS_FLASH_PROGRAM_UNIT
60 
61 #else
62 #include "its_flash_nand.h"
63 #define FLASH_INFO_INIT its_flash_nand_init
64 #define FLASH_INFO_READ its_flash_nand_read
65 #define FLASH_INFO_WRITE its_flash_nand_write
66 #define FLASH_INFO_FLUSH its_flash_nand_flush
67 #define FLASH_INFO_ERASE its_flash_nand_erase
68 
69 /* The flash block is programmed in one shot, so no filesystem alignment is
70  * required.
71  */
72 #define ITS_FLASH_ALIGNMENT 1
73 #endif
74 
75 /* Calculate the block layout */
76 #define FLASH_INFO_BLOCK_SIZE (ITS_SECTOR_SIZE * ITS_SECTORS_PER_BLOCK)
77 
78 /* Maximum file size */
79 #define FLASH_INFO_MAX_FILE_SIZE ITS_UTILS_ALIGN(ITS_MAX_ASSET_SIZE, \
80  ITS_FLASH_ALIGNMENT)
81 
82 /* Maximum number of files */
83 #define FLASH_INFO_MAX_NUM_FILES (ITS_NUM_ASSETS + 1)
84 
85 /* Default value of each byte in the flash when erased */
86 #define FLASH_INFO_ERASE_VAL 0xFFU
87 
88 #ifdef ITS_RAM_FS
89 /* Allocate a static buffer to emulate storage in RAM */
90 static uint8_t its_block_data[ITS_RAM_FS_SIZE];
91 #define FLASH_INFO_DEV its_block_data
92 #else
93 /* Import the CMSIS flash device driver */
94 extern ARM_DRIVER_FLASH ITS_FLASH_DEV_NAME;
95 #define FLASH_INFO_DEV &ITS_FLASH_DEV_NAME
96 #endif
97 
100  .read = FLASH_INFO_READ,
101  .write = FLASH_INFO_WRITE,
102  .flush = FLASH_INFO_FLUSH,
103  .erase = FLASH_INFO_ERASE,
104  .flash_dev = (void *)FLASH_INFO_DEV,
105  .fs_info = {0, 0}, /* Filled by its_flash_get_info() */
106  .sector_size = ITS_SECTOR_SIZE,
107  .block_size = FLASH_INFO_BLOCK_SIZE,
108  .num_blocks = 0, /* Filled by its_flash_get_info() */
109  .program_unit = ITS_FLASH_ALIGNMENT,
110  .max_file_size = FLASH_INFO_MAX_FILE_SIZE,
111  .max_num_files = FLASH_INFO_MAX_NUM_FILES,
112  .erase_val = FLASH_INFO_ERASE_VAL,
113 };
ARM_DRIVER_FLASH ITS_FLASH_DEV_NAME
#define FLASH_INFO_MAX_FILE_SIZE
Implementations of the flash interface functions for a NOR flash device. See its_flash.h for full documentation of functions.
#define FLASH_INFO_MAX_NUM_FILES
#define FLASH_INFO_INIT
struct its_flash_info_t its_flash_info_internal
#define FLASH_INFO_ERASE
Implementations of the flash interface functions for a NAND flash device. See its_flash.h for full documentation of functions.
Implementations of the flash interface functions for an emulated flash device using RAM...
#define FLASH_INFO_ERASE_VAL
#define ITS_FLASH_ALIGNMENT
#define FLASH_INFO_READ
#define FLASH_INFO_WRITE
#define FLASH_INFO_DEV
struct flash_fs_info_t fs_info
Definition: its_flash.h:156
#define FLASH_INFO_FLUSH
#define FLASH_INFO_BLOCK_SIZE
Structure containing the required information about a flash device to be used by the ITS Flash FS...
Definition: its_flash.h:73
psa_status_t(* init)(const struct its_flash_info_t *info)
Initialize the Flash Interface.
Definition: its_flash.h:82