Add fwu update agent deployment for linux-pc/posix

The fwu update agent can be deployed in different
execution environments to suite platform requirements.
This commit adds a deployment where the fwu update
agent runs within a command-line app for Posix
environments where the firmware image file is accessible
via standard file io. Can be built to run in a native PC
environment to enable a disk image file to be inspected.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: I3a652c7ab201c5b7059f8dd1bb6b590e6d8c4c37
diff --git a/components/service/fwu/app/metadata_reader.cpp b/components/service/fwu/app/metadata_reader.cpp
index 6423aa9..d1507f5 100644
--- a/components/service/fwu/app/metadata_reader.cpp
+++ b/components/service/fwu/app/metadata_reader.cpp
@@ -77,8 +77,11 @@
 				}
 			}
 
-			if (!is_handled)
-				IMSG("No metadata recognised");
+			if (!is_handled) {
+
+				/* This is normal on first-boot */
+				status = -1;
+			}
 
 		} else
 			IMSG("Failed to read metadata volume");
diff --git a/deployments/fwu/config/fwu-app-linux-pc/CMakeLists.txt b/deployments/fwu/config/fwu-app-linux-pc/CMakeLists.txt
new file mode 100644
index 0000000..416ee01
--- /dev/null
+++ b/deployments/fwu/config/fwu-app-linux-pc/CMakeLists.txt
@@ -0,0 +1,55 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
+include(../../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+# The CMakeLists.txt for building the fwu deployment for linux-pc
+#
+# This configuration builds the FWU update agent into a command-line app
+# that can be used to apply updates to disk image files.
+#-------------------------------------------------------------------------------
+project(trusted-services LANGUAGES CXX C)
+add_executable(fwu)
+target_include_directories(fwu PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+
+#-------------------------------------------------------------------------------
+#  Configure trace output for command-line app
+#
+#-------------------------------------------------------------------------------
+set(TRACE_PREFIX "FWU" CACHE STRING "Trace prefix")
+set(TRACE_LEVEL "TRACE_LEVEL_DEBUG" CACHE STRING "Trace level")
+
+#-------------------------------------------------------------------------------
+#  Deployment specific build configuration
+#
+#-------------------------------------------------------------------------------
+
+# Scale number of partitions for pretty complicated fw images
+target_compile_definitions("fwu" PRIVATE
+    PARTITIONED_BLOCK_STORE_MAX_PARTITIONS=24)
+
+#-------------------------------------------------------------------------------
+# This configuration builds for linux-pc
+#
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/linux-pc/env.cmake)
+
+#-------------------------------------------------------------------------------
+#  External project source-level dependencies
+#
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/external/tf_a/tf-a.cmake)
+add_tfa_dependency(TARGET "fwu")
+
+#-------------------------------------------------------------------------------
+#  Deployment specific components
+#
+#-------------------------------------------------------------------------------
+include(../../env/posix/fwu-posix.cmake REQUIRED)
+include(../../fwu.cmake REQUIRED)
+include(../../infra/file-block-store.cmake REQUIRED)
diff --git a/deployments/fwu/env/posix/cmd_print_image_dir.cpp b/deployments/fwu/env/posix/cmd_print_image_dir.cpp
new file mode 100644
index 0000000..0060ebd
--- /dev/null
+++ b/deployments/fwu/env/posix/cmd_print_image_dir.cpp
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <vector>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <common/uuid/uuid.h>
+#include <protocols/service/fwu/packed-c/fwu_proto.h>
+#include "cmd_print_image_dir.h"
+#include "print_uuid.h"
+
+void cmd_print_image_dir(
+	fwu_app &app)
+{
+	std::vector<uint8_t> fetched_object;
+	struct uuid_octets object_uuid;
+
+	uuid_guid_octets_from_canonical(&object_uuid, FWU_DIRECTORY_CANONICAL_UUID);
+
+	int status = app.read_object(object_uuid, fetched_object);
+
+	if (status) {
+
+		printf("Error: failed to read image directory\n");
+		return;
+	}
+
+	if (fetched_object.size() < offsetof(ts_fwu_image_directory, img_info_entry)) {
+
+		printf("Error: invalid image directory size\n");
+		return;
+	}
+
+	const struct ts_fwu_image_directory *img_dir =
+		(const struct ts_fwu_image_directory *)fetched_object.data();
+
+	printf("\nimage_directory (size %ld bytes) :\n", fetched_object.size());
+	printf("\tdirectory_version : %d\n", img_dir->directory_version);
+	printf("\tnum_images : %d\n", img_dir->num_images);
+	printf("\tcorrect_boot : %d\n", img_dir->correct_boot);
+
+	for (unsigned int i = 0; i < img_dir->num_images; i++) {
+
+		printf("\timg_info_entry[%d]:\n", i);
+		printf("\t\timg_type_uuid : %s\n",
+			print_uuid(img_dir->img_info_entry[i].img_type_uuid).c_str());
+		printf("\t\tclient_permissions : 0x%x\n",
+			img_dir->img_info_entry[i].client_permissions);
+		printf("\t\timg_max_size : %d\n",
+			img_dir->img_info_entry[i].img_max_size);
+		printf("\t\tlowest_accepted_version : %d\n",
+			img_dir->img_info_entry[i].lowest_accepted_version);
+		printf("\t\timg_version : %d\n",
+			img_dir->img_info_entry[i].img_version);
+		printf("\t\taccepted : %d\n",
+			img_dir->img_info_entry[i].accepted);
+	}
+}
diff --git a/deployments/fwu/env/posix/cmd_print_image_dir.h b/deployments/fwu/env/posix/cmd_print_image_dir.h
new file mode 100644
index 0000000..1630734
--- /dev/null
+++ b/deployments/fwu/env/posix/cmd_print_image_dir.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2023, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef CMD_PRINT_IMAGE_DIR_H
+#define CMD_PRINT_IMAGE_DIR_H
+
+#include <service/fwu/app/fwu_app.h>
+
+void cmd_print_image_dir(fwu_app &app);
+
+#endif /* CMD_PRINT_IMAGE_DIR_H */
diff --git a/deployments/fwu/env/posix/cmd_print_metadata_v1.cpp b/deployments/fwu/env/posix/cmd_print_metadata_v1.cpp
new file mode 100644
index 0000000..3762745
--- /dev/null
+++ b/deployments/fwu/env/posix/cmd_print_metadata_v1.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <vector>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <common/uuid/uuid.h>
+#include <protocols/service/fwu/packed-c/fwu_proto.h>
+#include <protocols/service/fwu/packed-c/metadata_v1.h>
+#include "cmd_print_metadata_v1.h"
+#include "print_uuid.h"
+
+
+void cmd_print_metadata_v1(
+	fwu_app &app)
+{
+	std::vector<uint8_t> fetched_object;
+	struct uuid_octets object_uuid;
+
+	uuid_guid_octets_from_canonical(&object_uuid, FWU_METADATA_CANONICAL_UUID);
+
+	int status = app.read_object(object_uuid, fetched_object);
+
+	if (status) {
+
+		printf("Error: failed to read metadata\n");
+		return;
+	}
+
+	if (fetched_object.size() < sizeof(struct fwu_metadata)) {
+
+		printf("Error: invalid metadata size\n");
+		return;
+	}
+
+	const struct fwu_metadata *metadata =
+		(const struct fwu_metadata *)fetched_object.data();
+
+	printf("\nfwu_metadata (size %ld bytes) :\n", fetched_object.size());
+	printf("\tcrc_32 : 0x%x\n", metadata->crc_32);
+	printf("\tversion : %d\n", metadata->version);
+	printf("\tactive_index : %d\n", metadata->active_index);
+	printf("\tprevious_active_index : %d\n", metadata->previous_active_index);
+
+	for (unsigned int i = 0; i < FWU_METADATA_NUM_IMAGE_ENTRIES; i++) {
+
+		printf("\timg_entry[%d]:\n", i);
+		printf("\t\timg_type_uuid : %s\n",
+			print_uuid(metadata->img_entry[i].img_type_uuid).c_str());
+		printf("\t\tlocation_uuid : %s\n",
+			print_uuid(metadata->img_entry[i].location_uuid).c_str());
+
+		for (unsigned int bank_index = 0; bank_index < FWU_METADATA_NUM_BANKS; bank_index++) {
+
+			printf("\t\timg_props[%d]:\n", bank_index);
+			printf("\t\t\timg_uuid : %s\n",
+				print_uuid(metadata->img_entry[i].img_props[bank_index].img_uuid).c_str());
+			printf("\t\t\taccepted : %d\n",
+				metadata->img_entry[i].img_props[bank_index].accepted);
+		}
+	}
+}
diff --git a/deployments/fwu/env/posix/cmd_print_metadata_v1.h b/deployments/fwu/env/posix/cmd_print_metadata_v1.h
new file mode 100644
index 0000000..0dc5eac
--- /dev/null
+++ b/deployments/fwu/env/posix/cmd_print_metadata_v1.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2023, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef CMD_PRINT_METADATA_V1_H
+#define CMD_PRINT_METADATA_V1_H
+
+#include <service/fwu/app/fwu_app.h>
+
+void cmd_print_metadata_v1(fwu_app &app);
+
+#endif /* CMD_PRINT_METADATA_V1_H */
diff --git a/deployments/fwu/env/posix/cmd_print_metadata_v2.cpp b/deployments/fwu/env/posix/cmd_print_metadata_v2.cpp
new file mode 100644
index 0000000..5fee2d9
--- /dev/null
+++ b/deployments/fwu/env/posix/cmd_print_metadata_v2.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <vector>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <common/uuid/uuid.h>
+#include <protocols/service/fwu/packed-c/fwu_proto.h>
+#include <protocols/service/fwu/packed-c/metadata_v2.h>
+#include "cmd_print_metadata_v2.h"
+#include "print_uuid.h"
+
+void cmd_print_metadata_v2(
+	fwu_app &app)
+{
+	std::vector<uint8_t> fetched_object;
+	struct uuid_octets object_uuid;
+
+	uuid_guid_octets_from_canonical(&object_uuid, FWU_METADATA_CANONICAL_UUID);
+
+	int status = app.read_object(object_uuid, fetched_object);
+
+	if (status) {
+
+		printf("Error: failed to read metadata\n");
+		return;
+	}
+
+	if (fetched_object.size() < sizeof(struct fwu_metadata)) {
+
+		printf("Error: invalid metadata size\n");
+		return;
+	}
+
+	/* Print mandatory metadata header */
+	const struct fwu_metadata *metadata =
+		(const struct fwu_metadata *)fetched_object.data();
+
+	printf("\nfwu_metadata (size %ld bytes) :\n", fetched_object.size());
+	printf("\tcrc_32 : 0x%x\n", metadata->crc_32);
+	printf("\tversion : %d\n", metadata->version);
+	printf("\tmetadata_size : %d\n", metadata->metadata_size);
+	printf("\theader_size : %d\n", metadata->header_size);
+	printf("\tactive_index : %d\n", metadata->active_index);
+	printf("\tprevious_active_index : %d\n", metadata->previous_active_index);
+	printf("\tbank_state : 0x%x 0x%x\n",
+		metadata->bank_state[0], metadata->bank_state[1]);
+
+	if (metadata->metadata_size <= metadata->header_size)
+		return;
+
+	size_t fw_store_desc_size = metadata->metadata_size - metadata->header_size;
+
+	if (fw_store_desc_size < sizeof(fwu_fw_store_desc)) {
+
+		printf("\tInsufficient space for fw store descriptor\n");
+		return;
+	}
+
+	/* Print optional fw store descriptor */
+	struct fwu_fw_store_desc *fw_store_desc =
+		(struct fwu_fw_store_desc *)&fetched_object[metadata->header_size];
+
+	printf("\tfw_store_desc :\n");
+	printf("\t\tnum_banks : %d\n", fw_store_desc->num_banks);
+	printf("\t\tnum_images : %d\n", fw_store_desc->num_images);
+	printf("\t\timg_entry_size : %d\n", fw_store_desc->img_entry_size);
+	printf("\t\tbank_entry_size : %d\n", fw_store_desc->bank_entry_size);
+
+	for (unsigned int i = 0; i < fw_store_desc->num_images; i++) {
+
+		struct fwu_image_entry *img_entry = &fw_store_desc->img_entry[i];
+
+		printf("\t\timg_entry[%d] :\n", i);
+		printf("\t\t\timg_type_uuid : %s\n", print_uuid(img_entry->img_type_uuid).c_str());
+		printf("\t\t\tlocation_uuid : %s\n", print_uuid(img_entry->location_uuid).c_str());
+
+		for (unsigned int i = 0; i < fw_store_desc->num_banks; i++) {
+
+			struct fwu_img_bank_info *bank_info = &img_entry->img_bank_info[i];
+
+			printf("\t\t\timg_bank_info[%d] :\n", i);
+			printf("\t\t\t\timg_uuid : %s\n", print_uuid(bank_info->img_uuid).c_str());
+			printf("\t\t\t\taccepted : %d\n", bank_info->accepted);
+		}
+	}
+}
diff --git a/deployments/fwu/env/posix/cmd_print_metadata_v2.h b/deployments/fwu/env/posix/cmd_print_metadata_v2.h
new file mode 100644
index 0000000..2943f93
--- /dev/null
+++ b/deployments/fwu/env/posix/cmd_print_metadata_v2.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2023, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef CMD_PRINT_METADATA_V2_H
+#define CMD_PRINT_METADATA_V2_H
+
+#include <service/fwu/app/fwu_app.h>
+
+void cmd_print_metadata_v2(fwu_app &app);
+
+#endif /* CMD_PRINT_METADATA_V2_H */
diff --git a/deployments/fwu/env/posix/cmd_update_image.cpp b/deployments/fwu/env/posix/cmd_update_image.cpp
new file mode 100644
index 0000000..8e1014f
--- /dev/null
+++ b/deployments/fwu/env/posix/cmd_update_image.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <common/uuid/uuid.h>
+#include "cmd_update_image.h"
+
+
+int cmd_update_image(
+	fwu_app &app,
+	const std::string &img_type_uuid,
+	const std::string &img_filename)
+{
+	FILE *fp = fopen(img_filename.c_str(), "rb");
+
+	if (!fp) {
+
+		printf("Error: failed to open image file: %s\n", img_filename.c_str());
+		return -1;
+	}
+
+	/* Get file size */
+	fseek(fp, 0, SEEK_END);
+	size_t img_size = ftell(fp);
+	rewind(fp);
+
+	/* Allocate buffer for image data */
+	uint8_t *img_buf = (uint8_t *)malloc(img_size);
+
+	if (!img_buf) {
+
+		fclose(fp);
+		printf("Error: failed to allocate image buffer\n");
+		return -1;
+	}
+
+	/* Read file contents into buffer */
+	if (fread(img_buf, 1, img_size, fp)) {
+
+		fclose(fp);
+		printf("Error: failed to read image file\n");
+		return -1;
+	}
+
+	fclose(fp);
+
+	/* Apply update */
+	struct uuid_octets uuid;
+
+	uuid_guid_octets_from_canonical(&uuid, img_type_uuid.c_str());
+
+	int status = app.update_image(uuid, img_buf, img_size);
+
+	if (status)
+		printf("Error: update image failed\n");
+
+	free(img_buf);
+
+	return status;
+}
diff --git a/deployments/fwu/env/posix/cmd_update_image.h b/deployments/fwu/env/posix/cmd_update_image.h
new file mode 100644
index 0000000..b0fe6ae
--- /dev/null
+++ b/deployments/fwu/env/posix/cmd_update_image.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2023, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef CMD_UPDATE_IMAGE_H
+#define CMD_UPDATE_IMAGE_H
+
+#include <string>
+#include <service/fwu/app/fwu_app.h>
+
+int cmd_update_image(
+	fwu_app &app,
+	const std::string &img_type_uuid,
+	const std::string &img_filename);
+
+#endif /* CMD_UPDATE_IMAGE_H */
diff --git a/deployments/fwu/env/posix/fwu-posix.cmake b/deployments/fwu/env/posix/fwu-posix.cmake
new file mode 100644
index 0000000..8818fb4
--- /dev/null
+++ b/deployments/fwu/env/posix/fwu-posix.cmake
@@ -0,0 +1,33 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Includes components needed for deploying the fwu update_agent within a
+# Posix environment as a command-line application. Can be used to apply an
+# update to a disk image file. Uses the same fwu components as a fw deployment
+# of the fwu service.
+#-------------------------------------------------------------------------------
+
+#-------------------------------------------------------------------------------
+# Common components for fwu posix deployments
+#
+#-------------------------------------------------------------------------------
+add_components(TARGET "fwu"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"environments/posix"
+		"components/common/crc32/native"
+		"components/common/trace"
+		"components/common/utils"
+		"components/service/fwu/app"
+)
+
+target_sources(fwu PRIVATE
+	${CMAKE_CURRENT_LIST_DIR}/fwu_main.cpp
+	${CMAKE_CURRENT_LIST_DIR}/cmd_update_image.cpp
+	${CMAKE_CURRENT_LIST_DIR}/cmd_print_image_dir.cpp
+	${CMAKE_CURRENT_LIST_DIR}/cmd_print_metadata_v1.cpp
+	${CMAKE_CURRENT_LIST_DIR}/cmd_print_metadata_v2.cpp
+	${CMAKE_CURRENT_LIST_DIR}/print_uuid.cpp
+)
\ No newline at end of file
diff --git a/deployments/fwu/env/posix/fwu_main.cpp b/deployments/fwu/env/posix/fwu_main.cpp
new file mode 100644
index 0000000..e01640a
--- /dev/null
+++ b/deployments/fwu/env/posix/fwu_main.cpp
@@ -0,0 +1,252 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <sstream>
+#include <sys/stat.h>
+#include <common/uuid/uuid.h>
+#include <service/fwu/app/fwu_app.h>
+#include "cmd_update_image.h"
+#include "cmd_print_image_dir.h"
+#include "cmd_print_metadata_v1.h"
+#include "cmd_print_metadata_v2.h"
+
+static bool option_selected(
+	const char *option_switch,
+	int argc, char *argv[]);
+
+static std::string parse_string_option(
+	const char *option_switch,
+	int argc, char *argv[],
+	const char *default_val);
+
+static int parse_numeric_option(
+	const char *option_switch,
+	int argc, char *argv[],
+	int default_val);
+
+static bool file_exists(
+	const std::string &filename);
+
+static void print_usage(void);
+static void print_help(void);
+
+
+int main(int argc, char *argv[])
+{
+	fwu_app app;
+	std::string disk_img_filename;
+	std::string update_img_filename;
+	std::string img_type_uuid;
+
+	/* Check for help */
+	if (option_selected("-h", argc, argv) ||
+		option_selected("-help", argc, argv) ||
+		option_selected("--help", argc, argv)) {
+
+		print_help();
+		return 0;
+	}
+
+	/* Handle mandatory disk image filename. Must be first argument */
+	if (argc > 1)
+		disk_img_filename = std::string(argv[1]);
+	else {
+
+		printf("Error: missing disk-filename argument\n");
+		print_usage();
+		return -1;
+	}
+
+	/* Check if disk image file exists */
+	if (!file_exists(disk_img_filename)) {
+
+		printf("Error: %s does not exist\n", disk_img_filename.c_str());
+		return -1;
+	}
+
+	/* Create fwu configuration based on the input disk image */
+	int status = app.configure(disk_img_filename.c_str());
+
+	if (status) {
+
+		printf("Error: failed to configure with status: %d\n", status);
+		return -1;
+	}
+
+	/* Attempt to derive boot info from metadata. Assume bootloader booted from the
+	 * active index. This can be overridden via command-line parameter.
+	 */
+	unsigned int boot_index;
+	unsigned int metadata_version;
+
+	status = app.get_boot_info(boot_index, metadata_version);
+
+	if (status) {
+
+		printf("No recognised metadata, assume default boot index and version\n");
+
+		boot_index = 0;
+		metadata_version = 2;
+	}
+
+	/* Allow for command-line overrides */
+	boot_index = parse_numeric_option("-boot-index", argc, argv, boot_index);
+	metadata_version = parse_numeric_option("-meta-ver", argc, argv, metadata_version);
+
+	/* Options for printing fwu info */
+	bool is_print_img_dir = option_selected("-dir", argc, argv);
+	bool is_print_metadata = option_selected("-meta", argc, argv);
+
+	/* Parse input image related parameters*/
+	update_img_filename = parse_string_option("-img", argc, argv, "");
+	img_type_uuid = parse_string_option("-img-type", argc, argv, "");
+
+	/* Check if image file exists (if one was specified) */
+	if (!update_img_filename.empty() && !file_exists(update_img_filename)) {
+
+		printf("Error: %s does not exist\n", update_img_filename.c_str());
+		return -1;
+	}
+
+	/* Check if img type canonical uuid is well formed */
+	if (!img_type_uuid.empty() && !uuid_is_valid(img_type_uuid.c_str())) {
+
+		printf("Error: image type uuid invalid\n");
+		return -1;
+	}
+
+	/* Initialise the update_agent. Missing or corrupt metadata will get repaired
+	 */
+	status = app.init_update_agent(boot_index, metadata_version);
+
+	if (!status) {
+
+		printf("Update agent started: boot index: %d metadata ver: %d\n",
+			boot_index, metadata_version);
+
+		if (is_print_img_dir)
+			cmd_print_image_dir(app);
+
+		if (is_print_metadata) {
+
+			if (metadata_version == 1)
+				cmd_print_metadata_v1(app);
+			else if (metadata_version == 2)
+				cmd_print_metadata_v2(app);
+			else
+				printf("Unsupported metadata version\n");
+
+		}
+
+		if (!update_img_filename.empty() && !img_type_uuid.empty()) {
+
+			status = cmd_update_image(app, img_type_uuid, update_img_filename);
+
+		} else if (!update_img_filename.empty() || !img_type_uuid.empty()) {
+
+			printf("Error: both image filename and uuid arguments are needed\n");
+			return -1;
+		}
+	}
+
+	if (!status)
+		printf("OK\n");
+	else
+		printf("Error status: %d\n", status);
+
+	return status;
+}
+
+static bool option_selected(
+	const char *option_switch,
+	int argc, char *argv[])
+{
+	bool is_selected = false;
+
+	for (int i = 1; (i < argc) && !is_selected; ++i) {
+
+		is_selected = (strcmp(argv[i], option_switch) == 0);
+	}
+
+	return is_selected;
+}
+
+static std::string parse_string_option(
+	const char *option_switch,
+	int argc, char *argv[],
+	const char *default_val)
+{
+	std::string option = std::string(default_val);
+
+	for (int i = 1; i + 1 < argc; ++i) {
+
+		if (strcmp(argv[i], option_switch) == 0) {
+
+			option = std::string(argv[i + 1]);
+			break;
+		}
+	}
+
+	return option;
+}
+
+static int parse_numeric_option(
+	const char *option_switch,
+	int argc, char *argv[],
+	int default_val)
+{
+	int option = default_val;
+
+	for (int i = 1; i + 1 < argc; ++i) {
+
+		if (strcmp(argv[i], option_switch) == 0) {
+
+			std::istringstream iss(argv[i + 1]);
+			int val;
+
+			iss >> val;
+
+			if (!iss.fail())
+				option = val;
+
+			break;
+		}
+	}
+
+	return option;
+}
+
+static bool file_exists(const std::string &filename)
+{
+	struct stat stat_buf;
+
+	return stat(filename.c_str(), &stat_buf) == 0;
+}
+
+static void print_usage(void)
+{
+	printf("Usage: fwu disk-filename [-dir -meta] [-boot-index number -meta-ver number] "
+		   "[-img filename -img-type uuid]\n");
+}
+
+static void print_help(void)
+{
+	print_usage();
+
+	printf("\n");
+	printf("\tdisk-filename\tDisk image file to update\n");
+	printf("\t-dir\t\tPrint image directory\n");
+	printf("\t-meta\t\tPrint FWU metadata\n");
+	printf("\t-boot-index\tOverride default boot index [0..n]\n");
+	printf("\t-meta-ver\tSpecify FWU metadata to use\n");
+	printf("\t-img\t\tFile containing image update\n");
+	printf("\t-img-type\tCanonical UUID of image to update\n");
+}
+
diff --git a/deployments/fwu/env/posix/print_uuid.cpp b/deployments/fwu/env/posix/print_uuid.cpp
new file mode 100644
index 0000000..6cfbdcd
--- /dev/null
+++ b/deployments/fwu/env/posix/print_uuid.cpp
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <common/uuid/uuid.h>
+#include "print_uuid.h"
+
+std::string print_uuid(const uint8_t *uuid_octets)
+{
+	struct uuid_canonical canonical_uuid;
+
+	uuid_canonical_from_guid_octets(&canonical_uuid, (const struct uuid_octets *)uuid_octets);
+
+	return std::string(canonical_uuid.characters);
+}
diff --git a/deployments/fwu/env/posix/print_uuid.h b/deployments/fwu/env/posix/print_uuid.h
new file mode 100644
index 0000000..2e94c85
--- /dev/null
+++ b/deployments/fwu/env/posix/print_uuid.h
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2023, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef PRINT_UUID_H
+#define PRINT_UUID_H
+
+#include <cstdint>
+#include <string>
+
+std::string print_uuid(const uint8_t *uuid_octets);
+
+#endif /* PRINT_UUID_H */
diff --git a/deployments/fwu/fwu.cmake b/deployments/fwu/fwu.cmake
new file mode 100644
index 0000000..7276360
--- /dev/null
+++ b/deployments/fwu/fwu.cmake
@@ -0,0 +1,39 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Common components used for any deployment of the fwu service provider.
+#-------------------------------------------------------------------------------
+
+#-------------------------------------------------------------------------------
+#  Components common to all deployments
+#
+#-------------------------------------------------------------------------------
+add_components(TARGET "fwu"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/common/uuid"
+		"components/common/endian"
+		"components/media/disk/gpt_iterator"
+		"components/media/volume/index"
+		"components/service/common/include"
+		"components/service/fwu/agent"
+		"components/service/fwu/config"
+		"components/service/fwu/config/gpt"
+		"components/service/fwu/fw_store/banked"
+		"components/service/fwu/fw_store/banked/metadata_serializer/v1"
+		"components/service/fwu/fw_store/banked/metadata_serializer/v2"
+		"components/service/fwu/installer"
+		"components/service/fwu/installer/raw"
+		"components/service/fwu/installer/copy"
+		"components/service/fwu/installer/factory/default"
+		"components/service/fwu/inspector/direct"
+)
+
+#################################################################
+
+target_include_directories(fwu PRIVATE
+	${TS_ROOT}
+	${TS_ROOT}/components
+)
diff --git a/deployments/fwu/infra/file-block-store.cmake b/deployments/fwu/infra/file-block-store.cmake
new file mode 100644
index 0000000..d86f7d0
--- /dev/null
+++ b/deployments/fwu/infra/file-block-store.cmake
@@ -0,0 +1,30 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Lists components that provide an infrastructure layer for the block-storage
+# service provider that uses a ram-backed block store, partitioned by default
+# using the 'ref' configuration. This infrastructure is intended for test
+# purposes.
+#-------------------------------------------------------------------------------
+
+#-------------------------------------------------------------------------------
+# Infrastructure components
+#
+#-------------------------------------------------------------------------------
+add_components(TARGET "fwu"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/media/disk"
+		"components/media/volume"
+		"components/media/volume/base_io_dev"
+		"components/media/volume/block_volume"
+		"components/media/volume/factory/single_flash"
+		"components/service/block_storage/block_store"
+		"components/service/block_storage/block_store/device"
+		"components/service/block_storage/block_store/device/file"
+		"components/service/block_storage/block_store/partitioned"
+		"components/service/block_storage/factory/file"
+		"components/service/block_storage/config/gpt"
+)
\ No newline at end of file
diff --git a/environments/posix/component.cmake b/environments/posix/component.cmake
index 1cf17dd..bcdb6ad 100644
--- a/environments/posix/component.cmake
+++ b/environments/posix/component.cmake
@@ -8,6 +8,18 @@
 	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
 endif()
 
+if (NOT DEFINED TRACE_PREFIX)
+	message(FATAL_ERROR "mandatory parameter TRACE_PREFIX is not defined.")
+endif()
+
+# Default to trace output disabled
+set(TRACE_LEVEL "TRACE_LEVEL_NONE" CACHE STRING "Trace level")
+
+target_compile_definitions(${TGT} PRIVATE
+	TRACE_LEVEL=${TRACE_LEVEL}
+	TRACE_PREFIX="${TRACE_PREFIX}"
+)
+
 target_sources(${TGT} PRIVATE
 	"${CMAKE_CURRENT_LIST_DIR}/posix_trace.c"
 	)