TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
its_flash_info_external.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 #include "ps_object_defs.h"
15 
16 #ifndef PS_FLASH_DEV_NAME
17 #error "PS_FLASH_DEV_NAME must be defined by the target in flash_layout.h"
18 #endif
19 
20 /* Adjust to match the size of the flash device's physical erase unit */
21 #ifndef PS_SECTOR_SIZE
22 #error "PS_SECTOR_SIZE must be defined by the target in flash_layout.h"
23 #endif
24 
25 /* Adjust so that the maximum required asset size will fit in one block */
26 #ifndef PS_SECTORS_PER_BLOCK
27 #error "PS_SECTORS_PER_BLOCK must be defined by the target in flash_layout.h"
28 #endif
29 
30 /* Adjust to match the size of the flash device's physical program unit */
31 #ifndef PS_FLASH_PROGRAM_UNIT
32 #error "PS_FLASH_PROGRAM_UNIT must be defined by the target in flash_layout.h"
33 #elif (PS_FLASH_PROGRAM_UNIT < 1 || PS_FLASH_PROGRAM_UNIT > PS_SECTOR_SIZE)
34 #error "PS_FLASH_PROGRAM_UNIT must be between 1 and PS_SECTOR_SIZE inclusive"
35 #elif (PS_FLASH_PROGRAM_UNIT & (PS_FLASH_PROGRAM_UNIT - 1) != 0)
36 #error "PS_FLASH_PROGRAM_UNIT must be a power of two"
37 #endif
38 
39 /* Include the correct flash interface implementation */
40 #ifdef PS_RAM_FS
41 #include "its_flash_ram.h"
42 #ifndef PS_RAM_FS_SIZE
43 #error "PS_RAM_FS_SIZE must be defined by the target in flash_layout.h"
44 #endif
45 #define FLASH_INFO_INIT its_flash_ram_init
46 #define FLASH_INFO_READ its_flash_ram_read
47 #define FLASH_INFO_WRITE its_flash_ram_write
48 #define FLASH_INFO_FLUSH its_flash_ram_flush
49 #define FLASH_INFO_ERASE its_flash_ram_erase
50 
51 #elif (PS_FLASH_PROGRAM_UNIT <= 16)
52 #include "its_flash_nor.h"
53 #define FLASH_INFO_INIT its_flash_nor_init
54 #define FLASH_INFO_READ its_flash_nor_read
55 #define FLASH_INFO_WRITE its_flash_nor_write
56 #define FLASH_INFO_FLUSH its_flash_nor_flush
57 #define FLASH_INFO_ERASE its_flash_nor_erase
58 
59 /* Require each file in the filesystem to be aligned to the program unit */
60 #define PS_FLASH_ALIGNMENT PS_FLASH_PROGRAM_UNIT
61 
62 #else
63 #include "its_flash_nand.h"
64 #define FLASH_INFO_INIT its_flash_nand_init
65 #define FLASH_INFO_READ its_flash_nand_read
66 #define FLASH_INFO_WRITE its_flash_nand_write
67 #define FLASH_INFO_FLUSH its_flash_nand_flush
68 #define FLASH_INFO_ERASE its_flash_nand_erase
69 
70 /* The flash block is programmed in one shot, so no filesystem alignment is
71  * required.
72  */
73 #define PS_FLASH_ALIGNMENT 1
74 #endif
75 
76 /* Calculate the block layout */
77 #define FLASH_INFO_BLOCK_SIZE (PS_SECTOR_SIZE * PS_SECTORS_PER_BLOCK)
78 
79 /* Maximum file size */
80 #define FLASH_INFO_MAX_FILE_SIZE ITS_UTILS_ALIGN(PS_MAX_OBJECT_SIZE, \
81  PS_FLASH_ALIGNMENT)
82 
83 /* Maximum number of files */
84 #define FLASH_INFO_MAX_NUM_FILES PS_MAX_NUM_OBJECTS
85 
86 /* Default value of each byte in the flash when erased */
87 #define FLASH_INFO_ERASE_VAL 0xFFU
88 
89 #ifdef PS_RAM_FS
90 /* Allocate a static buffer to emulate storage in RAM */
91 static uint8_t ps_block_data[PS_RAM_FS_SIZE];
92 #define FLASH_INFO_DEV ps_block_data
93 #else
94 /* Import the CMSIS flash device driver */
95 extern ARM_DRIVER_FLASH PS_FLASH_DEV_NAME;
96 #define FLASH_INFO_DEV &PS_FLASH_DEV_NAME
97 #endif
98 
101  .read = FLASH_INFO_READ,
102  .write = FLASH_INFO_WRITE,
103  .flush = FLASH_INFO_FLUSH,
104  .erase = FLASH_INFO_ERASE,
105  .flash_dev = (void *)FLASH_INFO_DEV,
106  .fs_info = {0, 0}, /* Filled by its_flash_get_info() */
107  .sector_size = PS_SECTOR_SIZE,
108  .block_size = FLASH_INFO_BLOCK_SIZE,
109  .num_blocks = 0, /* Filled by its_flash_get_info() */
110  .program_unit = PS_FLASH_ALIGNMENT,
111  .max_file_size = FLASH_INFO_MAX_FILE_SIZE,
112  .max_num_files = FLASH_INFO_MAX_NUM_FILES,
113  .erase_val = FLASH_INFO_ERASE_VAL,
114 };
#define FLASH_INFO_READ
#define FLASH_INFO_BLOCK_SIZE
#define FLASH_INFO_INIT
Implementations of the flash interface functions for a NOR flash device. See its_flash.h for full documentation of functions.
#define FLASH_INFO_WRITE
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_MAX_NUM_FILES
#define FLASH_INFO_ERASE_VAL
#define FLASH_INFO_FLUSH
struct its_flash_info_t its_flash_info_external
struct flash_fs_info_t fs_info
Definition: its_flash.h:156
#define FLASH_INFO_MAX_FILE_SIZE
#define FLASH_INFO_ERASE
ARM_DRIVER_FLASH PS_FLASH_DEV_NAME
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
#define FLASH_INFO_DEV
#define PS_FLASH_ALIGNMENT