Add block_store flash support to secure flash store

Refactors the secure flash store to accommodate alternative flash
drivers using its existing driver model. Adds a new driver that
adapts to the block_store interface to enable SFS to be used
with shared storage partitions. This commit includes tests
that run through psa storage operations using using a
partitioned block store for storage.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: I8b0e81734b1c59a0be98cee94ae54a56ed0200db
diff --git a/components/service/crypto/client/test/standalone/standalone_crypto_client.cpp b/components/service/crypto/client/test/standalone/standalone_crypto_client.cpp
index b5a7ca0..b36264d 100644
--- a/components/service/crypto/client/test/standalone/standalone_crypto_client.cpp
+++ b/components/service/crypto/client/test/standalone/standalone_crypto_client.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -8,6 +8,7 @@
 #include <service/crypto/factory/crypto_provider_factory.h>
 #include <service/crypto/backend/mbedcrypto/mbedcrypto_backend.h>
 #include <service/secure_storage/backend/secure_flash_store/secure_flash_store.h>
+#include <service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.h>
 
 standalone_crypto_client::standalone_crypto_client() :
     test_crypto_client(),
@@ -37,7 +38,7 @@
         if (!is_fault_injected(FAILED_TO_DISCOVER_SECURE_STORAGE)) {
 
             /* Establish rpc session with storage provider */
-            struct storage_backend *storage_backend = sfs_init();
+            struct storage_backend *storage_backend = sfs_init(sfs_flash_ram_instance());
             struct rpc_interface *storage_ep = secure_storage_provider_init(&m_storage_provider,
                                                                 storage_backend);
             storage_caller = direct_caller_init_default(&m_storage_caller, storage_ep);
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/component.cmake b/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/component.cmake
new file mode 100644
index 0000000..be04011
--- /dev/null
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/component.cmake
@@ -0,0 +1,13 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+if (NOT DEFINED TGT)
+	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+target_sources(${TGT} PRIVATE
+	"${CMAKE_CURRENT_LIST_DIR}/sfs_flash_block_store_adapter.c"
+	)
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/sfs_flash_block_store_adapter.c b/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/sfs_flash_block_store_adapter.c
new file mode 100644
index 0000000..9c05b30
--- /dev/null
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/sfs_flash_block_store_adapter.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+#include "sfs_flash_block_store_adapter.h"
+
+static inline struct sfs_flash_block_store_adapter *get_context(
+	const struct sfs_flash_info_t *info)
+{
+	return (struct sfs_flash_block_store_adapter *)info->flash_dev;
+}
+
+static psa_status_t sfs_flash_init(
+	const struct sfs_flash_info_t *info)
+{
+	struct sfs_flash_block_store_adapter *context;
+
+	/* Ensure that adapter has already been initialised */
+	if (!info || (context = get_context(info), !context->block_store))
+		return PSA_ERROR_BAD_STATE;
+
+	return PSA_SUCCESS;
+}
+
+static psa_status_t sfs_flash_read(
+	const struct sfs_flash_info_t *info,
+	uint32_t block_id,
+	uint8_t *buff,
+	size_t offset,
+	size_t size)
+{
+	psa_status_t status = PSA_ERROR_GENERIC_ERROR;
+	struct sfs_flash_block_store_adapter *context = get_context(info);
+	size_t bytes_read = 0;
+
+	status = block_store_read(context->block_store,
+		context->client_id,
+		context->partition_handle,
+		block_id,
+		offset,
+		size,
+		buff,
+		&bytes_read);
+
+	if ((status == PSA_SUCCESS) && (bytes_read != size))
+		/* Requested size invalid */
+		status = PSA_ERROR_INVALID_ARGUMENT;
+
+	return status;
+}
+
+static psa_status_t sfs_flash_write(
+	const struct sfs_flash_info_t *info,
+	uint32_t block_id,
+	const uint8_t *buff,
+	size_t offset,
+	size_t size)
+{
+	psa_status_t status = PSA_ERROR_GENERIC_ERROR;
+	struct sfs_flash_block_store_adapter *context = get_context(info);
+	size_t bytes_written = 0;
+
+	status = block_store_write(context->block_store,
+		context->client_id,
+		context->partition_handle,
+		block_id,
+		offset,
+		buff,
+		size,
+		&bytes_written);
+
+	if ((status == PSA_SUCCESS) && (bytes_written != size))
+		/* Requested size invalid */
+		status = PSA_ERROR_INVALID_ARGUMENT;
+
+	return status;
+}
+
+static psa_status_t sfs_flash_flush(
+	const struct sfs_flash_info_t *info)
+{
+	/* Not supported by block_store */
+	(void)info;
+	return PSA_SUCCESS;
+}
+
+static psa_status_t sfs_flash_erase(
+	const struct sfs_flash_info_t *info,
+ 	uint32_t block_id)
+{
+	psa_status_t status = PSA_ERROR_GENERIC_ERROR;
+	struct sfs_flash_block_store_adapter *context = get_context(info);
+
+	status = block_store_erase(context->block_store,
+		context->client_id,
+		context->partition_handle,
+		block_id, 1);
+
+	return status;
+}
+
+psa_status_t sfs_flash_block_store_adapter_init(
+	struct sfs_flash_block_store_adapter *context,
+	uint32_t client_id,
+	struct block_store *block_store,
+	const struct uuid_octets *partition_guid,
+	size_t max_num_files,
+	const struct sfs_flash_info_t **flash_info)
+{
+	psa_status_t status = PSA_SUCCESS;
+	struct sfs_flash_info_t *info = &context->flash_info;
+	struct storage_partition_info partition_info;
+
+	*flash_info = info;
+
+	/* Associate with the underlying block_store */
+	context->block_store = block_store;
+	context->client_id = client_id;
+
+	/* Get information about the assigned storage partition */
+	status = block_store_get_partition_info(context->block_store,
+		partition_guid, &partition_info);
+	if (status != PSA_SUCCESS)
+		return status;
+
+	/* Initialise interface used by sfs */
+	info->flash_dev = context;
+	info->init = sfs_flash_init,
+	info->read = sfs_flash_read,
+	info->write = sfs_flash_write,
+	info->flush = sfs_flash_flush,
+	info->erase = sfs_flash_erase,
+
+	/* Attributes that are fixed when using a block_store */
+	info->flash_area_addr = 0;
+	info->program_unit = sizeof(uint8_t);
+	info->erase_val = 0xff;
+
+	/* Underlying storage partition parameters */
+	info->sector_size = (uint16_t)partition_info.block_size;
+	info->block_size = (uint16_t)partition_info.block_size;
+	info->num_blocks = (uint16_t)partition_info.num_blocks;
+
+	/* sfs specific configuration */
+	info->max_file_size = (uint16_t)info->block_size;
+	info->max_num_files = (uint16_t)max_num_files;
+
+	/* Open the storage partition so that it's ready to use */
+	status = block_store_open(context->block_store,
+		context->client_id,
+		partition_guid,
+		&context->partition_handle);
+
+	return status;
+}
+
+psa_status_t sfs_flash_block_store_adapter_deinit(
+	struct sfs_flash_block_store_adapter *context)
+{
+	/* Close the storage partition */
+	psa_status_t status = block_store_close(context->block_store,
+		context->client_id,
+		context->partition_handle);
+
+	return status;
+}
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/sfs_flash_block_store_adapter.h b/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/sfs_flash_block_store_adapter.h
new file mode 100644
index 0000000..a88f1b9
--- /dev/null
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/sfs_flash_block_store_adapter.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+/**
+ * \file sfs_flash_block_store_adapter.h
+ *
+ * \brief Adapts the sfs flash interface to block_store interface
+ *
+ * Can be used to access a storage partition exposed by a concrete
+ * block_store. An example use is to interface to a block_store that
+ * acts as a client to the block storage service.
+ */
+
+#ifndef __SFS_FLASH_BLOCK_STORE_ADAPTER_H__
+#define __SFS_FLASH_BLOCK_STORE_ADAPTER_H__
+
+#include <stdint.h>
+#include "../sfs_flash.h"
+#include "service/block_storage/block_store/block_store.h"
+#include "common/uuid/uuid.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief sfs_flash_block_store_adapter structure
+ */
+struct sfs_flash_block_store_adapter
+{
+	struct sfs_flash_info_t flash_info;
+	struct block_store *block_store;
+	storage_partition_handle_t partition_handle;
+	uint32_t client_id;
+};
+
+/**
+ * \brief Initialize the block store adapter
+ *
+ * On success, the underlying block store is opened and the flash_info
+ * structure in initialised, based on the info queried from the block
+ * store.
+ *
+ * \param[in] context      This adapter context
+ * \param[in] client_id    The client id for the environment
+ * \param[in] block_store  The associated block store
+ * \param[in] partition_guid The storage partition to use
+ * \param[in] max_num_files	An sfs configuration parameter
+ * \param[out] flash_info  The sfs flash interface structure
+ *
+ * \return PSA_SUCCESS when successful
+ */
+psa_status_t sfs_flash_block_store_adapter_init(
+	struct sfs_flash_block_store_adapter *context,
+	uint32_t client_id,
+	struct block_store *block_store,
+	const struct uuid_octets *partition_guid,
+	size_t max_num_files,
+	const struct sfs_flash_info_t **flash_info);
+
+/**
+ * \brief Deinitialize the block store adapter
+ *
+ * \param[in] context      This adapter context
+ *
+* \return PSA_SUCCESS when successful
+ */
+psa_status_t sfs_flash_block_store_adapter_deinit(
+	struct sfs_flash_block_store_adapter *context);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SFS_FLASH_BLOCK_STORE_ADAPTER_H__ */
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/component.cmake b/components/service/secure_storage/backend/secure_flash_store/flash/component.cmake
index a2f34e7..b85dd51 100644
--- a/components/service/secure_storage/backend/secure_flash_store/flash/component.cmake
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/component.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -9,8 +9,5 @@
 endif()
 
 target_sources(${TGT} PRIVATE
-	"${CMAKE_CURRENT_LIST_DIR}/sfs_flash_info.c"
-	"${CMAKE_CURRENT_LIST_DIR}/sfs_flash_ram.c"
 	"${CMAKE_CURRENT_LIST_DIR}/sfs_flash.c"
 	)
-
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/ram/component.cmake b/components/service/secure_storage/backend/secure_flash_store/flash/ram/component.cmake
new file mode 100644
index 0000000..45887fa
--- /dev/null
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/ram/component.cmake
@@ -0,0 +1,14 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+if (NOT DEFINED TGT)
+	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+target_sources(${TGT} PRIVATE
+	"${CMAKE_CURRENT_LIST_DIR}/sfs_flash_info.c"
+	"${CMAKE_CURRENT_LIST_DIR}/sfs_flash_ram.c"
+	)
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_info.c b/components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_info.c
similarity index 85%
rename from components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_info.c
rename to components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_info.c
index 7dfe803..8b72b59 100644
--- a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_info.c
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_info.c
@@ -1,12 +1,12 @@
 /*
- * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include "sfs_flash.h"
+#include "../sfs_flash.h"
 #include "sfs_flash_ram.h"
-#include "../sfs_utils.h"
+#include "../../sfs_utils.h"
 
 #define SFS_FLASH_AREA_ADDR (0x0)
 
@@ -46,7 +46,7 @@
 static uint8_t sfs_block_data[FLASH_INFO_BLOCK_SIZE * FLASH_INFO_NUM_BLOCKS];
 #define FLASH_INFO_DEV sfs_block_data
 
-const struct sfs_flash_info_t sfs_flash_info_internal = {
+static const struct sfs_flash_info_t sfs_flash_info_ram = {
     .init = sfs_flash_ram_init,
     .read = sfs_flash_ram_read,
     .write = sfs_flash_ram_write,
@@ -63,5 +63,10 @@
     .erase_val = FLASH_INFO_ERASE_VAL,
 };
 
+const struct sfs_flash_info_t *sfs_flash_ram_instance(void)
+{
+    return &sfs_flash_info_ram;
+}
+
 /* Checks at compile time that the flash device configuration is valid */
-#include "../flash_fs/sfs_flash_fs_check_info.h"
+#include "../../flash_fs/sfs_flash_fs_check_info.h"
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_ram.c b/components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.c
similarity index 96%
rename from components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_ram.c
rename to components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.c
index e4af6e6..c3c17e4 100644
--- a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_ram.c
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_ram.h b/components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.h
similarity index 70%
rename from components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_ram.h
rename to components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.h
index eecc5e5..d70cb1b 100644
--- a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash_ram.h
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.h
@@ -1,10 +1,13 @@
 /*
- * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
+#ifndef __SFS_FLASH_RAM_H__
+#define __SFS_FLASH_RAM_H__
+
 /**
  * \file sfs_flash_ram.h
  *
@@ -12,7 +15,19 @@
  *        device using RAM. See sfs_flash.h for full documentation of functions.
  */
 
-#include "sfs_flash.h"
+#include "../sfs_flash.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Get the sfs_flash_ram sfs_flash_info_t instance
+ *
+ * The sfs_ram_flash component has been implemented as a singleton. This
+ * returns a pointer to the singleton sfs_flash_info_t structure.
+ */
+const struct sfs_flash_info_t *sfs_flash_ram_instance(void);
 
 /**
  * \brief Initialize the Flash Interface.
@@ -43,3 +58,9 @@
  */
 psa_status_t sfs_flash_ram_erase(const struct sfs_flash_info_t *info,
                                  uint32_t block_id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SFS_FLASH_RAM_H__ */
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash.c b/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash.c
index fce7968..89990fb 100644
--- a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash.c
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
  * Copyright (c) 2020 Cypress Semiconductor Corporation. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
@@ -12,13 +12,6 @@
 #define SFS_MAX_BLOCK_DATA_COPY 256
 #endif
 
-extern const struct sfs_flash_info_t sfs_flash_info_internal;
-
-const struct sfs_flash_info_t *sfs_flash_get_info(void)
-{
-    return &sfs_flash_info_internal;
-}
-
 psa_status_t sfs_flash_block_to_block_move(const struct sfs_flash_info_t *info,
                                            uint32_t dst_block,
                                            size_t dst_offset,
diff --git a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash.h b/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash.h
index 18361f2..f995fee 100644
--- a/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash.h
+++ b/components/service/secure_storage/backend/secure_flash_store/flash/sfs_flash.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -142,13 +142,6 @@
 };
 
 /**
- * \brief Gets the flash info structure for the provided flash device.
- *
- * \return Pointer to the flash info struct.
- */
-const struct sfs_flash_info_t *sfs_flash_get_info(void);
-
-/**
  * \brief Moves data from source block ID to destination block ID.
  *
  * \param[in] info        Flash device information
diff --git a/components/service/secure_storage/backend/secure_flash_store/secure_flash_store.c b/components/service/secure_storage/backend/secure_flash_store/secure_flash_store.c
index 81fc0d1..c503dcf 100644
--- a/components/service/secure_storage/backend/secure_flash_store/secure_flash_store.c
+++ b/components/service/secure_storage/backend/secure_flash_store/secure_flash_store.c
@@ -336,14 +336,13 @@
     return 0;
 }
 
-
-struct storage_backend *sfs_init(void)
+struct storage_backend *sfs_init(const struct sfs_flash_info_t *flash_binding)
 {
     psa_status_t status;
 
     /* Initialise the SFS context */
-    status = sfs_flash_fs_prepare(&fs_ctx_sfs,
-                                  sfs_flash_get_info());
+    status = sfs_flash_fs_prepare(&fs_ctx_sfs, flash_binding);
+
 #ifdef SFS_CREATE_FLASH_LAYOUT
     /* If SFS_CREATE_FLASH_LAYOUT is set, it indicates that it is required to
      * create a SFS flash layout. SFS service will generate an empty and valid
@@ -366,8 +365,7 @@
         }
 
         /* Attempt to initialise again */
-        status = sfs_flash_fs_prepare(&fs_ctx_sfs,
-                                     sfs_flash_get_info());
+        status = sfs_flash_fs_prepare(&fs_ctx_sfs, flash_binding);
 
         if (status != PSA_SUCCESS) {
             return NULL;
diff --git a/components/service/secure_storage/backend/secure_flash_store/secure_flash_store.h b/components/service/secure_storage/backend/secure_flash_store/secure_flash_store.h
index ac8d9b6..cf6d680 100644
--- a/components/service/secure_storage/backend/secure_flash_store/secure_flash_store.h
+++ b/components/service/secure_storage/backend/secure_flash_store/secure_flash_store.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -14,12 +14,15 @@
 extern "C" {
 #endif
 
+struct sfs_flash_info_t;
+
 /**
  * \brief Initializes the secure flash store backend
  *
+ * \param[in] flash_binding Binding to the flash driver
  * \return Pointer to storage backend or NULL on failure
  */
-struct storage_backend *sfs_init(void);
+struct storage_backend *sfs_init(const struct sfs_flash_info_t *flash_binding);
 
 #ifdef __cplusplus
 }
diff --git a/components/service/secure_storage/backend/secure_flash_store/test/component.cmake b/components/service/secure_storage/backend/secure_flash_store/test/component.cmake
index 67a6211..3c161e6 100644
--- a/components/service/secure_storage/backend/secure_flash_store/test/component.cmake
+++ b/components/service/secure_storage/backend/secure_flash_store/test/component.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -9,6 +9,6 @@
 endif()
 
 target_sources(${TGT} PRIVATE
-	"${CMAKE_CURRENT_LIST_DIR}/sfs_tests.cpp"
+	"${CMAKE_CURRENT_LIST_DIR}/sfs_ram_tests.cpp"
+	"${CMAKE_CURRENT_LIST_DIR}/sfs_block_store_tests.cpp"
 	)
-
diff --git a/components/service/secure_storage/backend/secure_flash_store/test/sfs_block_store_tests.cpp b/components/service/secure_storage/backend/secure_flash_store/test/sfs_block_store_tests.cpp
new file mode 100644
index 0000000..c77ad27
--- /dev/null
+++ b/components/service/secure_storage/backend/secure_flash_store/test/sfs_block_store_tests.cpp
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <CppUTest/TestHarness.h>
+#include <service/secure_storage/frontend/psa/its/its_frontend.h>
+#include <service/secure_storage/frontend/psa/its/test/its_api_tests.h>
+#include <service/secure_storage/frontend/psa/ps/ps_frontend.h>
+#include <service/secure_storage/frontend/psa/ps/test/ps_api_tests.h>
+#include <service/secure_storage/backend/secure_flash_store/secure_flash_store.h>
+#include <service/secure_storage/backend/secure_flash_store/flash/block_store_adapter/sfs_flash_block_store_adapter.h>
+#include <service/block_storage/factory/ref_ram/block_store_factory.h>
+#include <service/block_storage/config/ref/ref_partition_configurator.h>
+
+/**
+ * Tests the secure flash store with a block_store flash driver.
+ */
+TEST_GROUP(SfsBlockStoreTests)
+{
+    void setup()
+    {
+        struct uuid_octets guid;
+        const struct sfs_flash_info_t *flash_info = NULL;
+
+        uuid_parse_to_octets(REF_PARTITION_1_GUID, guid.octets, sizeof(guid.octets));
+
+        block_store = ref_ram_block_store_factory_create();
+        CHECK_TRUE(block_store);
+
+        psa_status_t status = sfs_flash_block_store_adapter_init(
+	        &sfs_flash_adapter,
+	        CLIENT_ID,
+	        block_store,
+	        &guid,
+	        MAX_NUM_FILES,
+	        &flash_info);
+
+        LONGS_EQUAL(PSA_SUCCESS, status);
+        CHECK_TRUE(flash_info);
+
+        struct storage_backend *storage_backend = sfs_init(flash_info);
+        CHECK_TRUE(storage_backend);
+
+        psa_its_frontend_init(storage_backend);
+        psa_ps_frontend_init(storage_backend);
+    }
+
+    void teardown()
+    {
+        sfs_flash_block_store_adapter_deinit(&sfs_flash_adapter);
+        ref_ram_block_store_factory_destroy(block_store);
+
+        block_store = NULL;
+    }
+
+    static const uint32_t CLIENT_ID = 10;
+    static const size_t MAX_NUM_FILES = 2;
+
+    struct block_store *block_store;
+    struct sfs_flash_block_store_adapter sfs_flash_adapter;
+};
+
+TEST(SfsBlockStoreTests, itsStoreNewItem)
+{
+    its_api_tests::storeNewItem();
+}
+
+TEST(SfsBlockStoreTests, itsStorageLimitTest)
+{
+    its_api_tests::storageLimitTest(5000);
+}
+
+TEST(SfsBlockStoreTests, psCreateAndSet)
+{
+    ps_api_tests::createAndSet();
+}
+
+TEST(SfsBlockStoreTests, psCreateAndSetExtended)
+{
+    ps_api_tests::createAndSetExtended();
+}
diff --git a/components/service/secure_storage/backend/secure_flash_store/test/sfs_tests.cpp b/components/service/secure_storage/backend/secure_flash_store/test/sfs_ram_tests.cpp
similarity index 60%
rename from components/service/secure_storage/backend/secure_flash_store/test/sfs_tests.cpp
rename to components/service/secure_storage/backend/secure_flash_store/test/sfs_ram_tests.cpp
index 33bdcde..d2a5df0 100644
--- a/components/service/secure_storage/backend/secure_flash_store/test/sfs_tests.cpp
+++ b/components/service/secure_storage/backend/secure_flash_store/test/sfs_ram_tests.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -10,35 +10,38 @@
 #include <service/secure_storage/frontend/psa/ps/ps_frontend.h>
 #include <service/secure_storage/frontend/psa/ps/test/ps_api_tests.h>
 #include <service/secure_storage/backend/secure_flash_store/secure_flash_store.h>
+#include <service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.h>
 
-
-TEST_GROUP(SfsTests)
+/**
+ * Tests the secure flash store with a ram flash driver.
+ */
+TEST_GROUP(SfsRamTests)
 {
     void setup()
     {
-        struct storage_backend *storage_backend = sfs_init();
+        struct storage_backend *storage_backend = sfs_init(sfs_flash_ram_instance());
 
         psa_its_frontend_init(storage_backend);
         psa_ps_frontend_init(storage_backend);
     }
 };
 
-TEST(SfsTests, itsStoreNewItem)
+TEST(SfsRamTests, itsStoreNewItem)
 {
     its_api_tests::storeNewItem();
 }
 
-TEST(SfsTests, itsStorageLimitTest)
+TEST(SfsRamTests, itsStorageLimitTest)
 {
     its_api_tests::storageLimitTest(5000);
 }
 
-TEST(SfsTests, psCreateAndSet)
+TEST(SfsRamTests, psCreateAndSet)
 {
     ps_api_tests::createAndSet();
 }
 
-TEST(SfsTests, psCreateAndSetExtended)
+TEST(SfsRamTests, psCreateAndSetExtended)
 {
     ps_api_tests::createAndSetExtended();
 }
diff --git a/components/service/secure_storage/factory/common/sfs/storage_factory.c b/components/service/secure_storage/factory/common/sfs/storage_factory.c
index 81f708d..a579887 100644
--- a/components/service/secure_storage/factory/common/sfs/storage_factory.c
+++ b/components/service/secure_storage/factory/common/sfs/storage_factory.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -8,6 +8,7 @@
 #include <rpc/ffarpc/caller/sp/ffarpc_caller.h>
 #include <protocols/rpc/common/packed-c/status.h>
 #include <service/secure_storage/backend/secure_flash_store/secure_flash_store.h>
+#include <service/secure_storage/backend/secure_flash_store/flash/ram/sfs_flash_ram.h>
 #include <service/secure_storage/factory/storage_factory.h>
 
 /**
@@ -21,7 +22,7 @@
 			enum storage_factory_security_class security_class)
 {
 	(void)security_class;
-	return sfs_init();
+	return sfs_init(sfs_flash_ram_instance());
 }
 
 void storage_factory_destroy(struct storage_backend *backend)
diff --git a/deployments/component-test/component-test.cmake b/deployments/component-test/component-test.cmake
index ad67ad7..b2ca483 100644
--- a/deployments/component-test/component-test.cmake
+++ b/deployments/component-test/component-test.cmake
@@ -137,6 +137,8 @@
 		"components/service/secure_storage/backend/secure_flash_store/test"
 		"components/service/secure_storage/backend/secure_flash_store/flash_fs"
 		"components/service/secure_storage/backend/secure_flash_store/flash"
+		"components/service/secure_storage/backend/secure_flash_store/flash/ram"
+		"components/service/secure_storage/backend/secure_flash_store/flash/block_store_adapter"
 		"components/service/test_runner/provider"
 		"components/service/test_runner/provider/serializer/packed-c"
 		"components/service/test_runner/provider/backend/null"
diff --git a/deployments/internal-trusted-storage/internal-trusted-storage.cmake b/deployments/internal-trusted-storage/internal-trusted-storage.cmake
index 3ce5564..c024ed4 100644
--- a/deployments/internal-trusted-storage/internal-trusted-storage.cmake
+++ b/deployments/internal-trusted-storage/internal-trusted-storage.cmake
@@ -20,6 +20,7 @@
 		components/service/secure_storage/backend/secure_flash_store
 		components/service/secure_storage/backend/secure_flash_store/flash_fs
 		components/service/secure_storage/backend/secure_flash_store/flash
+		components/service/secure_storage/backend/secure_flash_store/flash/ram
 		components/service/secure_storage/factory/common/sfs
 		protocols/rpc/common/packed-c
 		protocols/service/secure_storage/packed-c
diff --git a/deployments/protected-storage/protected-storage.cmake b/deployments/protected-storage/protected-storage.cmake
index 48b3bde..d56c026 100644
--- a/deployments/protected-storage/protected-storage.cmake
+++ b/deployments/protected-storage/protected-storage.cmake
@@ -23,6 +23,7 @@
 		components/service/secure_storage/backend/secure_flash_store
 		components/service/secure_storage/backend/secure_flash_store/flash_fs
 		components/service/secure_storage/backend/secure_flash_store/flash
+		components/service/secure_storage/backend/secure_flash_store/flash/ram
 		components/service/secure_storage/factory/common/sfs
 		protocols/rpc/common/packed-c
 		protocols/service/secure_storage/packed-c