aboutsummaryrefslogtreecommitdiff
path: root/components/service/secure_storage/provider/secure_flash_store/sfs_utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'components/service/secure_storage/provider/secure_flash_store/sfs_utils.h')
-rw-r--r--components/service/secure_storage/provider/secure_flash_store/sfs_utils.h99
1 files changed, 99 insertions, 0 deletions
diff --git a/components/service/secure_storage/provider/secure_flash_store/sfs_utils.h b/components/service/secure_storage/provider/secure_flash_store/sfs_utils.h
new file mode 100644
index 000000000..4a06d4b33
--- /dev/null
+++ b/components/service/secure_storage/provider/secure_flash_store/sfs_utils.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __SFS_UTILS_H__
+#define __SFS_UTILS_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <protocols/service/psa/packed-c/status.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define SFS_FILE_ID_SIZE 12
+#define SFS_DEFAULT_EMPTY_BUFF_VAL 0
+
+/**
+ * \brief Macro to check, at compilation time, if data fits in data buffer
+ *
+ * \param[in] err_msg Error message which will be displayed in first
+ * instance if the error is triggered
+ * \param[in] data_size Data size to check if it fits
+ * \param[in] data_buf_size Size of the data buffer
+ *
+ * \return Triggers a compilation error if data_size is bigger than
+ * data_buf_size. The compilation error should be
+ * "... error: 'err_msg' declared as an array with a negative size"
+ */
+#define SFS_UTILS_BOUND_CHECK(err_msg, data_size, data_buf_size) \
+typedef char err_msg[(data_size <= data_buf_size)*2 - 1]
+
+/**
+ * \brief Evaluates to the minimum of the two parameters.
+ */
+#define SFS_UTILS_MIN(x, y) (((x) < (y)) ? (x) : (y))
+
+/**
+ * \brief Evaluates to the maximum of the two parameters.
+ */
+#define SFS_UTILS_MAX(x, y) (((x) > (y)) ? (x) : (y))
+
+/**
+ * \brief Aligns a value up to the provided alignment.
+ *
+ * \param[in] x Value to be aligned
+ * \param[in] a Alignment (must be a power of two)
+ *
+ * \return The least value not less than \p x that is aligned to \p a.
+ */
+#define SFS_UTILS_ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
+
+/**
+ * \brief Checks that a value is aligned to the provided alignment.
+ *
+ * \param[in] x Value to check for alignment
+ * \param[in] a Alignment (must be a power of two)
+ *
+ * \return 1 if \p x is aligned to \p a, 0 otherwise.
+ */
+#define SFS_UTILS_IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
+
+/**
+ * \brief Checks if a subset region is fully contained within a superset region.
+ *
+ * \param[in] superset_size Size of superset region
+ * \param[in] subset_offset Offset of start of subset region from start of
+ * superset region
+ * \param[in] subset_size Size of subset region
+ *
+ * \return Returns error code as specified in \ref psa_status_t
+ *
+ * \retval PSA_SUCCESS The subset is contained within the
+ * superset
+ * \retval PSA_ERROR_INVALID_ARGUMENT Otherwise
+ */
+psa_status_t sfs_utils_check_contained_in(size_t superset_size,
+ size_t subset_offset,
+ size_t subset_size);
+
+/**
+ * \brief Validates file ID
+ *
+ * \param[in] fid File ID
+ *
+ * \return Returns error code as specified in \ref psa_status_t
+ */
+psa_status_t sfs_utils_validate_fid(const uint8_t *fid);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SFS_UTILS_H__ */