Add disk formatter component
Provides a way of initializing a storage volume. Initially, only
the clone method is currently supported where the contents of a
uint8_t array is used as the initialization data. Uses an
io_dev to initialize the storage volume so it can be used with
any supported storage.
Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: Ieac317abba605451e4ba81773d62fd680309ab19
diff --git a/components/media/disk/formatter/component.cmake b/components/media/disk/formatter/component.cmake
new file mode 100644
index 0000000..27b1110
--- /dev/null
+++ b/components/media/disk/formatter/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}/disk_formatter.c"
+)
\ No newline at end of file
diff --git a/components/media/disk/formatter/disk_formatter.c b/components/media/disk/formatter/disk_formatter.c
new file mode 100644
index 0000000..6b6d146
--- /dev/null
+++ b/components/media/disk/formatter/disk_formatter.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <media/volume/index/volume_index.h>
+#include <media/volume/base_io_dev/base_io_dev.h>
+#include "disk_formatter.h"
+
+int disk_formatter_clone(
+ uintptr_t dev_handle,
+ uintptr_t volume_spec,
+ const uint8_t *source_image,
+ size_t source_image_size)
+{
+ uintptr_t volume_handle;
+ int result;
+
+ result = io_open(dev_handle, volume_spec, &volume_handle);
+ if (result != 0)
+ return result;
+
+ result = io_seek(volume_handle, IO_SEEK_SET, 0);
+
+ if (result == 0) {
+
+ size_t length_written = 0;
+
+ result = io_write(volume_handle,
+ (const uintptr_t)source_image,
+ source_image_size,
+ &length_written);
+ }
+
+ io_close(volume_handle);
+ return result;
+}
\ No newline at end of file
diff --git a/components/media/disk/formatter/disk_formatter.h b/components/media/disk/formatter/disk_formatter.h
new file mode 100644
index 0000000..95b058e
--- /dev/null
+++ b/components/media/disk/formatter/disk_formatter.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef MEDIA_DISK_FORMATTER_H
+#define MEDIA_DISK_FORMATTER_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Format a storage volume by cloning a disk image
+ *
+ * @param[in] dev_handle IO device handle
+ * @param[in] volume_spec Opaque volume spec
+ * @param[in] source_image The source disk image to clone
+ * @param[in] source_image_size The size of the source image
+ *
+ * @return 0 on success
+ */
+int disk_formatter_clone(
+ uintptr_t dev_handle,
+ uintptr_t volume_spec,
+ const uint8_t *source_image,
+ size_t source_image_size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* MEDIA_DISK_FORMATTER_H */
diff --git a/components/media/disk/test/partition_table_tests.cpp b/components/media/disk/test/partition_table_tests.cpp
index c02e7a9..5e2e61f 100644
--- a/components/media/disk/test/partition_table_tests.cpp
+++ b/components/media/disk/test/partition_table_tests.cpp
@@ -14,6 +14,7 @@
#include <media/volume/block_io_dev/block_io_dev.h>
#include <media/volume/base_io_dev/base_io_dev.h>
#include <media/disk/disk_images/ref_partition.h>
+#include <media/disk/formatter/disk_formatter.h>
#include <media/disk/partition_table.h>
#include <CppUTest/TestHarness.h>
@@ -27,7 +28,7 @@
m_block_store = ram_block_store_init(&m_ram_block_store,
NULL,
- num_blocks, block_size, ref_partition_data);
+ num_blocks, block_size);
CHECK_TRUE(m_block_store);
@@ -44,6 +45,12 @@
CHECK_TRUE(m_dev_handle);
CHECK_TRUE(m_volume_spec);
+ result = disk_formatter_clone(
+ m_dev_handle, m_volume_spec,
+ ref_partition_data, ref_partition_data_length);
+
+ LONGS_EQUAL(0, result);
+
volume_index_init();
volume_index_add(TEST_VOLUME_ID, m_dev_handle, m_volume_spec);
}