uefi: Simplify finding RW images.

Instead of sometimes accepting command line arguments and sometimes not,
sometimes using the standard shell interface and sometimes not, lets just
always look for the RW images in a standard location on the media that held
depthcharge.

Change-Id: Id2968904efb6c9dde6dd5193d9ae2d2804cf9e17
diff --git a/src/module/uefi/fwdb.c b/src/module/uefi/fwdb.c
index bf3549f..14b77fe 100644
--- a/src/module/uefi/fwdb.c
+++ b/src/module/uefi/fwdb.c
@@ -27,9 +27,6 @@
 #include "base/fwdb.h"
 #include "base/physmem.h"
 #include "module/uefi/fwdb.h"
-#include "uefi/edk/Protocol/EfiShell.h"
-#include "uefi/edk/Protocol/EfiShellInterface.h"
-#include "uefi/edk/Protocol/EfiShellParameters.h"
 #include "uefi/edk/Protocol/LoadedImage.h"
 #include "uefi/edk/Protocol/SimpleFileSystem.h"
 #include "uefi/uefi.h"
@@ -43,126 +40,14 @@
 
 
 
-static EFI_GUID shell_parameters_protocol_guid =
-	EFI_SHELL_PARAMETERS_PROTOCOL_GUID;
-static EFI_GUID shell_protocol_guid = EFI_SHELL_PROTOCOL_GUID;
-static EFI_GUID shell_interface_guid = SHELL_INTERFACE_PROTOCOL_GUID;
-
 static EFI_GUID file_info_guid = EFI_FILE_INFO_ID;
 static EFI_GUID loaded_image_protocol_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
 static EFI_GUID simple_fs_protocol_guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
 
 
 
-static EFI_SHELL_PARAMETERS_PROTOCOL *get_shell_parameters(
-	EFI_BOOT_SERVICES *bs)
-{
-	EFI_HANDLE handle;
-	if (uefi_image_handle(&handle))
-		return NULL;
-
-	EFI_SHELL_PARAMETERS_PROTOCOL *shell_params;
-	EFI_STATUS status = bs->HandleProtocol(
-		handle, &shell_parameters_protocol_guid,
-		(void **)&shell_params);
-
-	if (status != EFI_SUCCESS) {
-		printf("No shell parameter protocol found.\n");
-		return NULL;
-	}
-
-	return shell_params;
-}
-
-static EFI_SHELL_INTERFACE *get_shell_interface(EFI_BOOT_SERVICES *bs)
-{
-	EFI_HANDLE handle;
-	if (uefi_image_handle(&handle))
-		return NULL;
-
-	EFI_SHELL_INTERFACE *shell_int;
-	EFI_STATUS status = bs->HandleProtocol(
-		handle, &shell_interface_guid, (void **)&shell_int);
-
-	if (status != EFI_SUCCESS) {
-		printf("No shell interface found.\n");
-		return NULL;
-	}
-
-	return shell_int;
-}
-
-static EFI_SHELL_PROTOCOL *get_shell_protocol(EFI_BOOT_SERVICES *bs)
-{
-	EFI_SHELL_PROTOCOL *shell_prot;
-
-	UINTN buf_size = 0;
-	EFI_HANDLE dummy;
-	EFI_STATUS status = bs->LocateHandle(
-		ByProtocol, &shell_protocol_guid, NULL, &buf_size, &dummy);
-	if (status == EFI_NOT_FOUND) {
-		printf("No shell protocol found.\n");
-		return NULL;
-	}
-	if (status != EFI_BUFFER_TOO_SMALL) {
-		printf("Error retrieving shell protocol handles.\n");
-		return NULL;
-	}
-
-	EFI_HANDLE *handles = xmalloc(buf_size);
-
-	status = bs->LocateHandle(ByProtocol, &shell_protocol_guid,
-				  NULL, &buf_size, handles);
-	if (status != EFI_SUCCESS) {
-		printf("Failed to retrieve shell protocol handles.\n");
-		return NULL;
-	}
-
-	int handle_count = buf_size / sizeof(dummy);
-	if (handle_count > 1)
-		printf("More than one shell found?\n");
-	EFI_HANDLE handle = handles[0];
-	free(handles);
-
-	status = bs->HandleProtocol(handle, &shell_protocol_guid,
-				    (void **)&shell_prot);
-	if (status != EFI_SUCCESS) {
-		printf("Failed to retrieve shell protocol.\n");
-		return NULL;
-	}
-
-	return shell_prot;
-}
-
-
-
-static int insert_file_into_fwdb(EFI_SHELL_PROTOCOL *shell_prot,
-				 SHELL_FILE_HANDLE file, const char *name)
-{
-	UINT64 size;
-	EFI_STATUS status = shell_prot->GetFileSize(file, &size);
-	if (status != EFI_SUCCESS) {
-		printf("Failed to get file size.\n");
-		return 1;
-	}
-
-	FwdbEntry entry = { .ptr = NULL, .size = size };
-	if (fwdb_access(name, NULL, &entry) || fwdb_access(name, &entry, NULL))
-		return 1;
-
-	UINTN buffer_size = size;
-	status = shell_prot->ReadFile(file, &buffer_size, entry.ptr);
-	if (status != EFI_SUCCESS) {
-		printf("Failed to read file.\n");
-		return 1;
-	}
-
-	return 0;
-}
-
-static int insert_file_name_into_fwdb_from_fs(EFI_FILE_PROTOCOL *root,
-					      CHAR16 *file_name,
-					      const char *name)
+static int insert_file_into_fwdb(EFI_FILE_PROTOCOL *root, CHAR16 *file_name,
+				 const char *name)
 {
 	EFI_FILE_PROTOCOL *file;
 	EFI_STATUS status = root->Open(root, &file, file_name,
@@ -201,21 +86,6 @@
 	return 0;
 }
 
-static int insert_file_name_into_fwdb(EFI_SHELL_PROTOCOL *shell_prot,
-				      CHAR16 *file_name, const char *name)
-{
-	SHELL_FILE_HANDLE file;
-	EFI_STATUS status = shell_prot->OpenFileByName(
-		file_name, &file, EFI_FILE_MODE_READ);
-	if (status != EFI_SUCCESS) {
-		printf("Failed to open read/write image.\n");
-		return 1;
-	}
-	int ret = insert_file_into_fwdb(shell_prot, file, name);
-	shell_prot->CloseFile(file);
-	return ret;
-}
-
 int uefi_prepare_fwdb_storage(void)
 {
 	FwdbEntry ro_image_entry = {
@@ -230,89 +100,47 @@
 		return 1;
 	EFI_BOOT_SERVICES *bs = st->BootServices;
 
-	EFI_SHELL_PROTOCOL *shell_prot = get_shell_protocol(bs);
-	EFI_SHELL_PARAMETERS_PROTOCOL *shell_params = NULL;
-	EFI_SHELL_INTERFACE *shell_int = NULL;
+	EFI_HANDLE handle;
+	if (uefi_image_handle(&handle))
+		return 1;
 
-	if (shell_prot) {
-		printf("UEFI standard shell protocol found.\n");
-
-		shell_params = get_shell_parameters(bs);
-		if (!shell_params)
-			return 1;
-
-		if (shell_params->Argc != 3) {
-			printf("Bad number of arguments.\n");
-			printf("Usage: dc <rwa image> <rwb image>\n");
-			return 1;
-		}
-
-		if (insert_file_name_into_fwdb(
-			shell_prot, shell_params->Argv[1], "uefi_rw_a_image") ||
-		    insert_file_name_into_fwdb(
-			shell_prot, shell_params->Argv[2], "uefi_rw_b_image")) {
-			return 1;
-		}
-	} else {
-		printf("Falling back to non-standard shell "
-		       "interface protocol.\n");
-		shell_int = get_shell_interface(bs);
-
-		if (shell_int && shell_int->Argc != 1) {
-			printf("Bad number of arguments.\n");
-			printf("Usage: dc\n");
-			printf("When using the non-standard interface, rwa "
-			       "and rwb are assumed to be on \n");
-			printf("the same device as the main depthcharge "
-			       "executable at the path \n");
-			printf("\\depthcharge\\rwa and \\depthcharge\\rwb.\n");
-			return 1;
-		}
-
-		EFI_HANDLE handle;
-		if (uefi_image_handle(&handle))
-			return 1;
-
-		EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
-		EFI_STATUS status =
-			bs->HandleProtocol(handle, &loaded_image_protocol_guid,
-					   (void **)&loaded_image);
-		if (status != EFI_SUCCESS) {
-			printf("Failed to open loaded image protocol.\n");
-			return 1;
-		}
-
-		EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *simple_fs;
-		status = bs->HandleProtocol(loaded_image->DeviceHandle,
-					   &simple_fs_protocol_guid,
-					   (void **)&simple_fs);
-		if (status != EFI_SUCCESS) {
-			printf("Failed to open simple fs protocol.\n");
-			return 1;
-		}
-
-		EFI_FILE_PROTOCOL *root;
-		status = simple_fs->OpenVolume(simple_fs, &root);
-		if (status != EFI_SUCCESS) {
-			printf("Failed to open simple fs root.\n");
-			return 1;
-		}
-
-		int ret = insert_file_name_into_fwdb_from_fs(
-			root, L"depthcharge\\rwa", "uefi_rw_a_image");
-		ret = ret || insert_file_name_into_fwdb_from_fs(
-			root, L"depthcharge\\rwb", "uefi_rw_b_image");
-
-		status = root->Close(root);
-		if (status != EFI_SUCCESS) {
-			printf("Failed to close fs root.\n");
-			return 1;
-		}
-
-		return ret;
+	EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
+	EFI_STATUS status =
+		bs->HandleProtocol(handle, &loaded_image_protocol_guid,
+				   (void **)&loaded_image);
+	if (status != EFI_SUCCESS) {
+		printf("Failed to open loaded image protocol.\n");
+		return 1;
 	}
 
-	return 0;
+	EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *simple_fs;
+	status = bs->HandleProtocol(loaded_image->DeviceHandle,
+				   &simple_fs_protocol_guid,
+				   (void **)&simple_fs);
+	if (status != EFI_SUCCESS) {
+		printf("Failed to open simple fs protocol.\n");
+		return 1;
+	}
+
+	EFI_FILE_PROTOCOL *root;
+	status = simple_fs->OpenVolume(simple_fs, &root);
+	if (status != EFI_SUCCESS) {
+		printf("Failed to open simple fs root.\n");
+		return 1;
+	}
+
+	int ret = insert_file_into_fwdb(root, L"depthcharge\\rwa",
+					"uefi_rw_a_image");
+	ret = ret || insert_file_into_fwdb(root, L"depthcharge\\rwb",
+					   "uefi_rw_b_image");
+
+	status = root->Close(root);
+	if (status != EFI_SUCCESS) {
+		printf("Failed to close fs root.\n");
+		return 1;
+	}
+
+	return ret;
 }