TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
its_flash_fs_dblock.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "its_flash_fs_dblock.h"
9 
10 #include "flash/its_flash.h"
11 
20 static uint32_t its_dblock_lo_to_phy(struct its_flash_fs_ctx_t *fs_ctx,
21  uint32_t lblock)
22 {
23  struct its_block_meta_t block_meta;
24  psa_status_t err;
25 
26  err = its_flash_fs_mblock_read_block_metadata(fs_ctx, lblock, &block_meta);
27  if (err != PSA_SUCCESS) {
28  return ITS_BLOCK_INVALID_ID;
29  }
30 
31  return block_meta.phy_id;
32 }
33 
35  struct its_flash_fs_ctx_t *fs_ctx,
36  uint32_t lblock,
37  size_t free_size,
38  size_t src_offset,
39  size_t dst_offset,
40  size_t size)
41 {
42  struct its_block_meta_t block_meta;
43  psa_status_t err;
44  uint32_t scratch_id = 0;
45 
46  /* Read current block meta */
47  err = its_flash_fs_mblock_read_block_metadata(fs_ctx, lblock, &block_meta);
48  if (err != PSA_SUCCESS) {
49  return err;
50  }
51 
52  /* Release data from block meta */
53  block_meta.free_size += free_size;
54 
55  /* Save scratch data block physical IDs */
56  scratch_id = its_flash_fs_mblock_cur_data_scratch_id(fs_ctx, lblock);
57 
58  /* Check if there are bytes to be compacted */
59  if (size > 0) {
60  /* Move data from source offset in current data block to scratch block
61  * destination offset.
62  */
63  err = its_flash_block_to_block_move(fs_ctx->flash_info, scratch_id,
64  dst_offset, block_meta.phy_id,
65  src_offset, size);
66  if (err != PSA_SUCCESS) {
68  }
69  }
70 
71  if (dst_offset > block_meta.data_start) {
72  /* Copy data from the beginning of data block until
73  * the position where the data will be reallocated later
74  */
75  err = its_flash_block_to_block_move(fs_ctx->flash_info, scratch_id,
76  block_meta.data_start,
77  block_meta.phy_id,
78  block_meta.data_start,
79  (dst_offset-block_meta.data_start));
80  if (err != PSA_SUCCESS) {
82  }
83  }
84 
85  /* Swap the scratch and current data blocks. Must swap even with nothing
86  * to compact so that deleted file is left in scratch and erased as part
87  * of finalization.
88  */
89  its_flash_fs_mblock_set_data_scratch(fs_ctx, block_meta.phy_id, lblock);
90 
91  /* Set scratch block ID as the one which contains the new data block */
92  block_meta.phy_id = scratch_id;
93 
94  /* Update block metadata in scratch metadata block */
96  &block_meta);
97  if (err != PSA_SUCCESS) {
98  /* Swap back the data block as there was an issue in the process */
99  its_flash_fs_mblock_set_data_scratch(fs_ctx, scratch_id, lblock);
100  return err;
101  }
102 
103  /* Commit data block modifications to flash, unless the data is in logical
104  * data block 0, in which case it will be flushed at the end of the metadata
105  * block update.
106  */
107  if (lblock != ITS_LOGICAL_DBLOCK0) {
108  err = fs_ctx->flash_info->flush(fs_ctx->flash_info);
109  }
110 
111  return err;
112 }
113 
115  struct its_flash_fs_ctx_t *fs_ctx,
116  const struct its_file_meta_t *file_meta,
117  size_t offset,
118  size_t size,
119  uint8_t *buf)
120 {
121  uint32_t phys_block;
122  size_t pos;
123 
124  phys_block = its_dblock_lo_to_phy(fs_ctx, file_meta->lblock);
125  if (phys_block == ITS_BLOCK_INVALID_ID) {
127  }
128 
129  pos = (file_meta->data_idx + offset);
130 
131  return fs_ctx->flash_info->read(fs_ctx->flash_info, phys_block, buf, pos,
132  size);
133 }
134 
136  struct its_flash_fs_ctx_t *fs_ctx,
137  const struct its_block_meta_t *block_meta,
138  const struct its_file_meta_t *file_meta,
139  size_t offset,
140  size_t size,
141  const uint8_t *data)
142 {
143  psa_status_t err;
144  uint32_t scratch_id;
145  size_t pos;
146  size_t num_bytes;
147 
148  scratch_id = its_flash_fs_mblock_cur_data_scratch_id(fs_ctx,
149  file_meta->lblock);
150 
151  /* Calculate the position of the new file data in the block */
152  pos = file_meta->data_idx + offset;
153 
154  /* Move data up to the new file data position */
156  scratch_id,
157  block_meta->data_start,
158  block_meta->phy_id,
159  block_meta->data_start,
160  pos - block_meta->data_start);
161  if (err != PSA_SUCCESS) {
162  return err;
163  }
164 
165  /* Write the new file data */
166  err = fs_ctx->flash_info->write(fs_ctx->flash_info, scratch_id, data, pos,
167  size);
168  if (err != PSA_SUCCESS) {
169  return err;
170  }
171 
172  /* Calculate the position of the end of the file */
173  pos = file_meta->data_idx + file_meta->max_size;
174 
175  /* Calculate the size of the data in the block after the end of the file */
176  num_bytes = (fs_ctx->flash_info->block_size - block_meta->free_size) - pos;
177 
178  /* Move data between the end of the file and the end of the block data */
179  err = its_flash_block_to_block_move(fs_ctx->flash_info, scratch_id, pos,
180  block_meta->phy_id, pos, num_bytes);
181  if (err != PSA_SUCCESS) {
182  return err;
183  }
184 
185  /* Commit data block modifications to flash, unless the data is in logical
186  * data block 0, in which case it will be flushed at the end of the metadata
187  * block update.
188  */
189  if (file_meta->lblock != ITS_LOGICAL_DBLOCK0) {
190  err = fs_ctx->flash_info->flush(fs_ctx->flash_info);
191  }
192 
193  return err;
194 }
psa_status_t its_flash_fs_mblock_update_scratch_block_meta(struct its_flash_fs_ctx_t *fs_ctx, uint32_t lblock, struct its_block_meta_t *block_meta)
Puts logical block's metadata in scratch metadata block.
const struct its_flash_info_t * flash_info
#define ITS_LOGICAL_DBLOCK0
Defines logical data block 0 ID.
#define PSA_SUCCESS
Definition: crypto_values.h:35
psa_status_t its_flash_fs_dblock_write_file(struct its_flash_fs_ctx_t *fs_ctx, const struct its_block_meta_t *block_meta, const struct its_file_meta_t *file_meta, size_t offset, size_t size, const uint8_t *data)
Writes scratch data block content with requested data and the rest of the data from the given logical...
uint16_t block_size
Definition: its_flash.h:160
Structure to store information about each physical flash memory block.
psa_status_t its_flash_fs_mblock_read_block_metadata(struct its_flash_fs_ctx_t *fs_ctx, uint32_t lblock, struct its_block_meta_t *block_meta)
Reads specified logical block metadata.
psa_status_t its_flash_block_to_block_move(const struct its_flash_info_t *info, uint32_t dst_block, size_t dst_offset, uint32_t src_block, size_t src_offset, size_t size)
Moves data from source block ID to destination block ID.
Definition: its_flash.c:56
uint32_t its_flash_fs_mblock_cur_data_scratch_id(struct its_flash_fs_ctx_t *fs_ctx, uint32_t lblock)
Gets current scratch datablock physical ID.
void its_flash_fs_mblock_set_data_scratch(struct its_flash_fs_ctx_t *fs_ctx, uint32_t phy_id, uint32_t lblock)
Sets current data scratch block.
psa_status_t(* 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.
Definition: its_flash.h:101
#define PSA_ERROR_GENERIC_ERROR
Definition: crypto_values.h:43
Structure to store file metadata.
psa_status_t(* flush)(const struct its_flash_info_t *info)
Flushes modifications to a block to flash. Must be called after a sequence of calls to write() (inclu...
Definition: its_flash.h:139
psa_status_t its_flash_fs_dblock_read_file(struct its_flash_fs_ctx_t *fs_ctx, const struct its_file_meta_t *file_meta, size_t offset, size_t size, uint8_t *buf)
Reads the file content.
psa_status_t its_flash_fs_dblock_compact_block(struct its_flash_fs_ctx_t *fs_ctx, uint32_t lblock, size_t free_size, size_t src_offset, size_t dst_offset, size_t size)
Compacts block data for the given logical block.
Structure to store the ITS flash file system context.
#define ITS_BLOCK_INVALID_ID
Definition: its_flash.h:24
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:43
psa_status_t(* 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.
Definition: its_flash.h:121