--- /dev/null
+/** @file\r
+* Manage XenBus device path and I/O handles\r
+*\r
+* Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>\r
+*\r
+* This program and the accompanying materials are\r
+* licensed and made available under the terms and conditions of the BSD License\r
+* which accompanies this distribution. The full text of the license may be found at\r
+* http://opensource.org/licenses/bsd-license.php\r
+*\r
+* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+*\r
+**/\r
+\r
+#ifndef _XENIO_MMIO_DEVICE_LIB_H_\r
+#define _XENIO_MMIO_DEVICE_LIB_H_\r
+\r
+/**\r
+\r
+ Install the XENBUS_ROOT_DEVICE_PATH and XENIO_PROTOCOL protocols on\r
+ the handle pointed to by @Handle, or on a new handle if it points to\r
+ NULL\r
+\r
+ @param Handle Pointer to the handle to install the protocols\r
+ on, may point to a NULL handle.\r
+\r
+ @param GrantTableAddress The address of the Xen grant table\r
+\r
+ @retval EFI_SUCCESS Protocols were installed successfully\r
+\r
+ @retval EFI_OUT_OF_RESOURCES The function failed to allocate memory required\r
+ by the XenIo MMIO and device path protocols\r
+\r
+ @return Status code returned by the boot service\r
+ InstallMultipleProtocolInterfaces ()\r
+\r
+**/\r
+EFI_STATUS\r
+XenIoMmioInstall (\r
+ IN OUT EFI_HANDLE *Handle,\r
+ IN EFI_PHYSICAL_ADDRESS GrantTableAddress\r
+ );\r
+\r
+\r
+/**\r
+\r
+ Uninstall the XENBUS_ROOT_DEVICE_PATH and XENIO_PROTOCOL protocols\r
+\r
+ @param Handle Handle onto which the protocols have been installed\r
+ earlier by XenIoMmioInstall ()\r
+\r
+ @retval EFI_SUCCESS Protocols were uninstalled successfully\r
+\r
+ @return Status code returned by the boot service\r
+ UninstallMultipleProtocolInterfaces ()\r
+\r
+**/\r
+EFI_STATUS\r
+XenIoMmioUninstall (\r
+ IN EFI_HANDLE Handle\r
+ );\r
+\r
+#endif\r
--- /dev/null
+/** @file\r
+* Manage XenBus device path and I/O handles\r
+*\r
+* Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>\r
+*\r
+* This program and the accompanying materials are\r
+* licensed and made available under the terms and conditions of the BSD License\r
+* which accompanies this distribution. The full text of the license may be found at\r
+* http://opensource.org/licenses/bsd-license.php\r
+*\r
+* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+*\r
+**/\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/DevicePathLib.h>\r
+#include <Library/XenIoMmioLib.h>\r
+\r
+#include <Protocol/XenIo.h>\r
+#include <Guid/XenBusRootDevice.h>\r
+\r
+#pragma pack (1)\r
+typedef struct {\r
+ VENDOR_DEVICE_PATH Vendor;\r
+ EFI_PHYSICAL_ADDRESS GrantTableAddress;\r
+ EFI_DEVICE_PATH_PROTOCOL End;\r
+} XENBUS_ROOT_DEVICE_PATH;\r
+#pragma pack ()\r
+\r
+STATIC CONST XENBUS_ROOT_DEVICE_PATH mXenBusRootDevicePathTemplate = {\r
+ {\r
+ {\r
+ HARDWARE_DEVICE_PATH,\r
+ HW_VENDOR_DP,\r
+ { sizeof (VENDOR_DEVICE_PATH) + sizeof (EFI_PHYSICAL_ADDRESS), 0 }\r
+ },\r
+ XENBUS_ROOT_DEVICE_GUID,\r
+ },\r
+ 0,\r
+ {\r
+ END_DEVICE_PATH_TYPE,\r
+ END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
+ { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }\r
+ }\r
+};\r
+\r
+/**\r
+\r
+ Install the XENBUS_ROOT_DEVICE_PATH and XENIO_PROTOCOL protocols on\r
+ the handle pointed to by @Handle, or on a new handle if it points to\r
+ NULL\r
+\r
+ @param Handle Pointer to the handle to install the protocols\r
+ on, may point to a NULL handle.\r
+\r
+ @param GrantTableAddress The address of the Xen grant table\r
+\r
+ @retval EFI_SUCCESS Protocols were installed successfully\r
+\r
+ @retval EFI_OUT_OF_RESOURCES The function failed to allocate memory required\r
+ by the XenIo MMIO and device path protocols\r
+\r
+ @return Status code returned by the boot service\r
+ InstallMultipleProtocolInterfaces ()\r
+\r
+**/\r
+EFI_STATUS\r
+XenIoMmioInstall (\r
+ IN OUT EFI_HANDLE *Handle,\r
+ IN EFI_PHYSICAL_ADDRESS GrantTableAddress\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ XENIO_PROTOCOL *XenIo;\r
+ XENBUS_ROOT_DEVICE_PATH *XenBusDevicePath;\r
+ EFI_HANDLE OutHandle;\r
+\r
+ ASSERT (Handle != NULL);\r
+\r
+ OutHandle = *Handle;\r
+\r
+ XenIo = AllocateZeroPool (sizeof *XenIo);\r
+ if (!XenIo) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ XenIo->GrantTableAddress = GrantTableAddress;\r
+\r
+ XenBusDevicePath = AllocateCopyPool (sizeof *XenBusDevicePath,\r
+ &mXenBusRootDevicePathTemplate);\r
+ if (!XenBusDevicePath) {\r
+ DEBUG ((EFI_D_ERROR, "%a: Out of memory\n", __FUNCTION__));\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto FreeXenIo;\r
+ }\r
+ XenBusDevicePath->GrantTableAddress = GrantTableAddress;\r
+\r
+ Status = gBS->InstallMultipleProtocolInterfaces (&OutHandle,\r
+ &gEfiDevicePathProtocolGuid, XenBusDevicePath,\r
+ &gXenIoProtocolGuid, XenIo,\r
+ NULL);\r
+ if (!EFI_ERROR (Status)) {\r
+ *Handle = OutHandle;\r
+ return EFI_SUCCESS;\r
+ }\r
+ \r
+ DEBUG ((EFI_D_ERROR, "%a: Failed to install the EFI_DEVICE_PATH and "\r
+ "XENIO_PROTOCOL protocols on handle %p (Status == %r)\n",\r
+ __FUNCTION__, OutHandle, Status));\r
+\r
+ FreePool (XenBusDevicePath);\r
+\r
+FreeXenIo:\r
+ FreePool (XenIo);\r
+ return Status;\r
+}\r
+\r
+/**\r
+\r
+ Uninstall the XENBUS_ROOT_DEVICE_PATH and XENIO_PROTOCOL protocols\r
+\r
+ @param Handle Handle onto which the protocols have been installed\r
+ earlier by XenIoMmioInstall ()\r
+\r
+ @retval EFI_SUCCESS Protocols were uninstalled successfully\r
+\r
+ @return Status code returned by the boot service\r
+ UninstallMultipleProtocolInterfaces ()\r
+\r
+**/\r
+EFI_STATUS\r
+XenIoMmioUninstall (\r
+ IN EFI_HANDLE Handle\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ VOID *XenIo;\r
+ VOID *XenBusDevicePath;\r
+\r
+ XenBusDevicePath = NULL;\r
+ gBS->OpenProtocol (Handle, &gEfiDevicePathProtocolGuid, &XenBusDevicePath,\r
+ NULL, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
+\r
+ XenIo = NULL;\r
+ gBS->OpenProtocol (Handle, &gXenIoProtocolGuid, &XenIo,\r
+ NULL, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
+\r
+ Status = gBS->UninstallMultipleProtocolInterfaces (Handle,\r
+ &gEfiDevicePathProtocolGuid, XenBusDevicePath,\r
+ &gXenIoProtocolGuid, XenIo,\r
+ NULL);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ FreePool (XenBusDevicePath);\r
+ FreePool (XenIo);\r
+\r
+ return EFI_SUCCESS;\r
+}\r