TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
its_flash_nand.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_nand.h"
10 #include "Driver_Flash.h"
11 #include "tfm_memory_utils.h"
12 
13 static uint32_t buf_block_id = ITS_BLOCK_INVALID_ID;
14 /* FIXME: calculation duplicated from flash info */
15 static uint8_t write_buf[ITS_SECTOR_SIZE * ITS_SECTORS_PER_BLOCK];
16 
26 static uint32_t get_phys_address(const struct its_flash_info_t *info,
27  uint32_t block_id, size_t offset)
28 {
29  return info->fs_info.flash_area_addr + (block_id * info->block_size)
30  + offset;
31 }
32 
34 {
35  int32_t err;
36 
37  err = ((ARM_DRIVER_FLASH *)info->flash_dev)->Initialize(NULL);
38  if (err != ARM_DRIVER_OK) {
40  }
41 
42  return PSA_SUCCESS;
43 }
44 
46  uint32_t block_id, uint8_t *buff,
47  size_t offset, size_t size)
48 {
49  int32_t err;
50  uint32_t addr = get_phys_address(info, block_id, offset);
51 
52  err = ((ARM_DRIVER_FLASH *)info->flash_dev)->ReadData(addr, buff, size);
53  if (err != ARM_DRIVER_OK) {
55  }
56 
57  return PSA_SUCCESS;
58 }
59 
61  uint32_t block_id, const uint8_t *buff,
62  size_t offset, size_t size)
63 {
64  (void)info;
65 
66  if (buf_block_id == ITS_BLOCK_INVALID_ID) {
67  buf_block_id = block_id;
68  } else if (buf_block_id != block_id) {
70  }
71 
72  /* Buffer the write data */
73  (void)tfm_memcpy(write_buf + offset, buff, size);
74 
75  return PSA_SUCCESS;
76 }
77 
79 {
80  int32_t err;
81  uint32_t addr = get_phys_address(info, buf_block_id, 0);
82 
83  /* Flush the buffered write data to flash*/
84  err = ((ARM_DRIVER_FLASH *)info->flash_dev)->ProgramData(addr, write_buf,
85  info->block_size);
86  if (err != ARM_DRIVER_OK) {
88  }
89 
90  /* Clear the write buffer */
91  (void)tfm_memset(write_buf, 0, sizeof(write_buf));
92  buf_block_id = ITS_BLOCK_INVALID_ID;
93 
94  return PSA_SUCCESS;
95 }
96 
98  uint32_t block_id)
99 {
100  int32_t err;
101  uint32_t addr;
102  size_t offset;
103 
104  for (offset = 0; offset < info->block_size; offset += info->sector_size) {
105  addr = get_phys_address(info, block_id, offset);
106 
107  err = ((ARM_DRIVER_FLASH *)info->flash_dev)->EraseSector(addr);
108  if (err != ARM_DRIVER_OK) {
110  }
111  }
112 
113  return PSA_SUCCESS;
114 }
uint32_t flash_area_addr
Definition: its_flash.h:63
psa_status_t its_flash_nand_flush(const struct its_flash_info_t *info)
Flushes modifications to a block to flash.
#define PSA_ERROR_STORAGE_FAILURE
__STATIC_INLINE void * tfm_memset(void *ptr, int value, size_t num)
#define PSA_SUCCESS
Definition: crypto_values.h:35
uint16_t block_size
Definition: its_flash.h:160
Implementations of the flash interface functions for a NAND flash device. See its_flash.h for full documentation of functions.
psa_status_t its_flash_nand_read(const struct its_flash_info_t *info, uint32_t block_id, uint8_t *buff, size_t offset, size_t size)
Reads block data from the position specified by block ID and offset.
#define PSA_ERROR_PROGRAMMER_ERROR
Definition: error.h:32
psa_status_t its_flash_nand_init(const struct its_flash_info_t *info)
Initialize the Flash Interface.
void * flash_dev
Definition: its_flash.h:155
struct flash_fs_info_t fs_info
Definition: its_flash.h:156
__STATIC_INLINE void * tfm_memcpy(void *dest, const void *src, size_t num)
psa_status_t its_flash_nand_write(const struct its_flash_info_t *info, uint32_t block_id, const uint8_t *buff, size_t offset, size_t size)
Writes block data to the position specified by block ID and offset.
uint16_t sector_size
Definition: its_flash.h:157
psa_status_t its_flash_nand_erase(const struct its_flash_info_t *info, uint32_t block_id)
Erases block ID data.
Structure containing the required information about a flash device to be used by the ITS Flash FS...
Definition: its_flash.h:73
#define ITS_BLOCK_INVALID_ID
Definition: its_flash.h:24
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:43