Add fwu_app for test and demonstration

Adds the fwu_app class to facilitate deployment of the update agent
as part of an application e.g. a user-space or UEFI app. It uses
the standard FWU service components but operates on a disk image
file rather than on a flash device. The disk image file needs to
be a UEFI formated image with MBR+GPT with partitions for metadata
and firmware banks. Will be initially used to test operation with
boot images generated from build tools.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: Ia4151841115010333a922c217352614062f8f86a
diff --git a/components/service/fwu/app/metadata_reader.cpp b/components/service/fwu/app/metadata_reader.cpp
new file mode 100644
index 0000000..6423aa9
--- /dev/null
+++ b/components/service/fwu/app/metadata_reader.cpp
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <media/volume/volume.h>
+#include <media/volume/index/volume_index.h>
+#include <service/fwu/fw_store/banked/volume_id.h>
+#include "metadata_reader.h"
+
+extern "C" {
+#include <trace.h>
+}
+
+metadata_reader::metadata_reader() :
+	registered_readers()
+{
+
+}
+
+metadata_reader::~metadata_reader()
+{
+
+}
+
+metadata_reader *metadata_reader::instance()
+{
+	static metadata_reader the_instance;
+	return &the_instance;
+}
+
+void metadata_reader::register_reader(metadata_version_specific_reader *reader)
+{
+	registered_readers.push_back(reader);
+}
+
+int metadata_reader::get_boot_info(
+	unsigned int &active_index,
+	unsigned int &metadata_version) const
+{
+	struct volume *volume;
+
+	int status = volume_index_find(BANKED_VOLUME_ID_PRIMARY_METADATA, &volume);
+
+	if (status) {
+
+		IMSG("Failed to find metadata volume");
+		return status;
+	}
+
+	status = volume_open(volume);
+
+	if (!status) {
+
+		/* Assume whatever metadata version is in-use, it will fit in the buffer */
+		size_t len_read = 0;
+		uint8_t buf[1000];
+
+		status = volume_read(volume, (uintptr_t)buf, sizeof(buf), &len_read);
+
+		if (!status) {
+
+			bool is_handled = false;
+
+			for (unsigned int i = 0; i < registered_readers.size(); i++) {
+
+				metadata_version_specific_reader *reader = registered_readers[i];
+
+				if (reader->is_supported(buf, len_read)) {
+
+					reader->get_version(buf, len_read, metadata_version);
+					reader->get_active_index(buf, len_read, active_index);
+
+					is_handled = true;
+					break;
+				}
+			}
+
+			if (!is_handled)
+				IMSG("No metadata recognised");
+
+		} else
+			IMSG("Failed to read metadata volume");
+
+		volume_close(volume);
+
+	} else
+		IMSG("Failed to open metadata volume");
+
+	return status;
+}
+