aboutsummaryrefslogtreecommitdiff
path: root/components/service/secure_storage/provider/secure_flash_store/flash_fs/sfs_flash_fs.h
blob: 704c7935011b1e0d8526cb3ea983a3cde31e67e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

/**
 * \file  sfs_flash_fs.h
 *
 * \brief The purpose of this abstraction is to have the ability to plug-in
 *        other filesystems or filesystem proxies (supplicant).
 */

#ifndef __SFS_FLASH_FS_H__
#define __SFS_FLASH_FS_H__

#include <stddef.h>
#include <stdint.h>

#include "sfs_flash_fs_mblock.h"
#include <protocols/service/psa/packed-c/status.h>
#include "../flash/sfs_flash.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief SFS flash filesytem context type, used to maintain state across FS
 *        operations.
 *
 * \details The user should allocate a variable of this type, initialised to
 *          zero, before calling sfs_flash_fs_prepare, and then pass it to each
 *          subsequent FS operation. The contents are internal to the filesytem.
 */
typedef struct sfs_flash_fs_ctx_t sfs_flash_fs_ctx_t;

/*!
 * \struct sfs_file_info_t
 *
 * \brief Structure to store the file information.
 */
struct sfs_file_info_t {
    size_t size_current; /*!< The current size of the flash file data */
    size_t size_max;     /*!< The maximum size of the flash file data in
                          * bytes.
                          */
    uint32_t flags;      /*!< Flags set when the file was created */
};

/**
 * \brief Prepares the filesystem to accept operations on the files.
 *
 * \param[in,out] fs_ctx      Filesystem context to prepare. Must have been
 *                            initialised by the caller.
 * \param[in]     flash_info  Struct containing information about the flash
 *                            device to associate with the context.
 *
 * \return Returns error code as specified in \ref psa_status_t
 */
psa_status_t sfs_flash_fs_prepare(sfs_flash_fs_ctx_t *fs_ctx,
                                  const struct sfs_flash_info_t *flash_info);

/**
 * \brief Wipes all files from the filesystem.
 *
 * \param[in,out] fs_ctx  Filesystem context to wipe. Must be prepared again
 *                        before further use.
 *
 * \return Returns error code as specified in \ref psa_status_t
 */
psa_status_t sfs_flash_fs_wipe_all(sfs_flash_fs_ctx_t *fs_ctx);

/**
 * \brief Checks if a file exists in the filesystem.
 *
 * \param[in,out] fs_ctx  Filesystem context
 * \param[in]     fid     File ID
 *
 * \return Returns PSA_SUCCESS if the file exists. If file does not exist, it
 *         returns PSA_ERROR_DOES_NOT_EXIST. Otherwise, it returns error code as
 *         specified in \ref psa_status_t.
 */
psa_status_t sfs_flash_fs_file_exist(sfs_flash_fs_ctx_t *fs_ctx,
                                     const uint8_t *fid);

/**
 * \brief Creates a file in the filesystem.
 *
 * \param[in,out] fs_ctx     Filesystem context
 * \param[in]     fid        File ID
 * \param[in]     max_size   Size of the file to be created
 * \param[in]     data_size  Size of the incoming buffer. This parameter is set
 *                           to 0 when the file is empty after the creation.
 * \param[in]     flags      Flags of the file
 * \param[in]     data       Pointer to buffer containing the initial data.
 *                           This parameter is set to NULL when the file is
 *                           empty after the creation.
 *
 * \return Returns PSA_SUCCESS if the file has been created correctly. If the
 *         fid is in use, it returns PSA_ERROR_INVALID_ARGUMENT. Otherwise, it
 *         returns error code as specified in \ref psa_status_t.
 */
psa_status_t sfs_flash_fs_file_create(sfs_flash_fs_ctx_t *fs_ctx,
                                      const uint8_t *fid,
                                      size_t max_size,
                                      size_t data_size,
                                      uint32_t flags,
                                      const uint8_t *data);

/**
 * \brief Gets the file information referenced by the file ID.
 *
 * \param[in,out] fs_ctx  Filesystem context
 * \param[in]     fid     File ID
 * \param[out]    info    Pointer to the information structure to store the
 *                        file information values \ref sfs_file_info_t
 *
 * \return Returns error code specified in \ref psa_status_t
 */
psa_status_t sfs_flash_fs_file_get_info(sfs_flash_fs_ctx_t *fs_ctx,
                                        const uint8_t *fid,
                                        struct sfs_file_info_t *info);

/**
 * \brief Writes data to an existing file.
 *
 * \param[in,out] fs_ctx  Filesystem context
 * \param[in]     fid     File ID
 * \param[in]     size    Size of the incoming buffer
 * \param[in]     offset  Offset in the file
 * \param[in]     data    Pointer to buffer containing data to be written
 *
 * \return Returns error code as specified in \ref psa_status_t
 */
psa_status_t sfs_flash_fs_file_write(sfs_flash_fs_ctx_t *fs_ctx,
                                     const uint8_t *fid,
                                     size_t size,
                                     size_t offset,
                                     const uint8_t *data);

/**
 * \brief Reads data from an existing file.
 *
 * \param[in,out] fs_ctx  Filesystem context
 * \param[in]     fid     File ID
 * \param[in]     size    Size to be read
 * \param[in]     offset  Offset in the file
 * \param[out]    data    Pointer to buffer to store the data
 *
 * \return Returns error code as specified in \ref psa_status_t
 */
psa_status_t sfs_flash_fs_file_read(sfs_flash_fs_ctx_t *fs_ctx,
                                    const uint8_t *fid,
                                    size_t size,
                                    size_t offset,
                                    uint8_t *data);

/**
 * \brief Deletes file referenced by the file ID.
 *
 * \param[in,out] fs_ctx  Filesystem context
 * \param[in]     fid     File ID
 *
 * \return Returns error code as specified in \ref psa_status_t
 */
psa_status_t sfs_flash_fs_file_delete(sfs_flash_fs_ctx_t *fs_ctx,
                                      const uint8_t *fid);

#ifdef __cplusplus
}
#endif

#endif /* __SFS_FLASH_FS_H__ */