]> git.proxmox.com Git - mirror_edk2.git/blobdiff - RedfishPkg/RedfishConfigHandler/RedfishConfigHandlerCommon.c
RedfishPkg/RedfishConfigHandler: EDKII RedfishConfigHandler Protocol
[mirror_edk2.git] / RedfishPkg / RedfishConfigHandler / RedfishConfigHandlerCommon.c
diff --git a/RedfishPkg/RedfishConfigHandler/RedfishConfigHandlerCommon.c b/RedfishPkg/RedfishConfigHandler/RedfishConfigHandlerCommon.c
new file mode 100644 (file)
index 0000000..ff465d9
--- /dev/null
@@ -0,0 +1,265 @@
+/** @file\r
+  The common code of EDKII Redfish Configuration Handler driver.\r
+\r
+  (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "RedfishConfigHandlerCommon.h"\r
+\r
+REDFISH_CONFIG_DRIVER_DATA      gRedfishConfigData; // Only one Redfish service supproted\r
+                                                    // on platform for the BIOS\r
+                                                    // Redfish configuration.\r
+EFI_EVENT  gEndOfDxeEvent = NULL;\r
+EFI_EVENT  gExitBootServiceEvent = NULL;\r
+EDKII_REDFISH_CREDENTIAL_PROTOCOL *gCredential = NULL;\r
+\r
+/**\r
+  Callback function executed when the EndOfDxe event group is signaled.\r
+\r
+  @param[in]   Event    Event whose notification function is being invoked.\r
+  @param[out]  Context  Pointer to the Context buffer.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+RedfishConfigOnEndOfDxe (\r
+  IN  EFI_EVENT  Event,\r
+  OUT VOID       *Context\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+\r
+  Status = gCredential->StopService (gCredential, ServiceStopTypeSecureBootDisabled);\r
+  if (EFI_ERROR(Status) && Status != EFI_UNSUPPORTED) {\r
+    DEBUG ((DEBUG_ERROR, "Redfish credential protocol faied to stop service on EndOfDxe: %r", Status));\r
+  }\r
+\r
+  //\r
+  // Close event, so it will not be invoked again.\r
+  //\r
+  gBS->CloseEvent (gEndOfDxeEvent);\r
+  gEndOfDxeEvent = NULL;\r
+}\r
+\r
+/**\r
+  Callback function executed when the ExitBootService event group is signaled.\r
+\r
+  @param[in]   Event    Event whose notification function is being invoked.\r
+  @param[out]  Context  Pointer to the Context buffer\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+RedfishConfigOnExitBootService (\r
+  IN  EFI_EVENT  Event,\r
+  OUT VOID       *Context\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+\r
+  Status = gCredential->StopService (gCredential, ServiceStopTypeExitBootService);\r
+  if (EFI_ERROR(Status) && Status != EFI_UNSUPPORTED) {\r
+    DEBUG ((DEBUG_ERROR, "Redfish credential protocol faied to stop service on ExitBootService: %r", Status));\r
+  }\r
+}\r
+\r
+/**\r
+  Unloads an image.\r
+\r
+  @param[in]  ImageHandle       Handle that identifies the image to be unloaded.\r
+\r
+  @retval EFI_SUCCESS           The image has been unloaded.\r
+\r
+**/\r
+EFI_STATUS\r
+RedfishConfigDriverCommonUnload (\r
+  IN EFI_HANDLE  ImageHandle\r
+  )\r
+{\r
+  if (gEndOfDxeEvent != NULL) {\r
+    gBS->CloseEvent (gEndOfDxeEvent);\r
+    gEndOfDxeEvent = NULL;\r
+  }\r
+\r
+  if (gExitBootServiceEvent != NULL) {\r
+    gBS->CloseEvent (gExitBootServiceEvent);\r
+    gExitBootServiceEvent = NULL;\r
+  }\r
+\r
+  if (gRedfishConfigData.Event != NULL) {\r
+    gBS->CloseEvent (gRedfishConfigData.Event);\r
+    gRedfishConfigData.Event = NULL;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  This is the common code for Redfish configuration UEFI and DXE driver\r
+  initialization.\r
+\r
+  @param[in]  ImageHandle       The firmware allocated handle for the UEFI image.\r
+  @param[in]  SystemTable       A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS           The operation completed successfully.\r
+  @retval Others                An unexpected error occurred.\r
+**/\r
+EFI_STATUS\r
+RedfishConfigCommonInit (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  //\r
+  // Locate Redfish Credential Protocol to get credential for\r
+  // accessing to Redfish service.\r
+  //\r
+  Status = gBS->LocateProtocol (&gEdkIIRedfishCredentialProtocolGuid, NULL, (VOID **) &gCredential);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_INFO, "%a: No Redfish Credential Protocol is installed on system.", __FUNCTION__));\r
+    return Status;\r
+  }\r
+  //\r
+  // Create EndOfDxe Event.\r
+  //\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  RedfishConfigOnEndOfDxe,\r
+                  NULL,\r
+                  &gEfiEndOfDxeEventGroupGuid,\r
+                  &gEndOfDxeEvent\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "%a: Fail to register End Of DXE event.", __FUNCTION__));\r
+    return Status;\r
+  }\r
+  //\r
+  // Create Exit Boot Service event.\r
+  //\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  RedfishConfigOnExitBootService,\r
+                  NULL,\r
+                  &gEfiEventExitBootServicesGuid,\r
+                  &gExitBootServiceEvent\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    gBS->CloseEvent (gEndOfDxeEvent);\r
+    gEndOfDxeEvent = NULL;\r
+    DEBUG ((DEBUG_ERROR, "%a: Fail to register Exit Boot Service event.", __FUNCTION__));\r
+    return Status;\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+/**\r
+  This is the common code to stop EDK2 Redfish feature driver.\r
+\r
+  @retval EFI_SUCCESS    All EDK2 Redfish feature drivers are\r
+                         stopped.\r
+  @retval Others         An unexpected error occurred.\r
+**/\r
+EFI_STATUS\r
+RedfishConfigCommonStop (\r
+  VOID\r
+)\r
+{\r
+  EFI_STATUS   Status;\r
+  EFI_HANDLE  *HandleBuffer;\r
+  UINTN        NumberOfHandles;\r
+  UINTN        Index;\r
+  EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL *ConfigHandler;\r
+\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEdkIIRedfishConfigHandlerProtocolGuid,\r
+                  NULL,\r
+                  &NumberOfHandles,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status) && Status != EFI_NOT_FOUND) {\r
+    return Status;\r
+  }\r
+\r
+  Status = EFI_SUCCESS;\r
+  for (Index = 0; Index < NumberOfHandles; Index++) {\r
+    Status = gBS->HandleProtocol (\r
+                     HandleBuffer[Index],\r
+                     &gEdkIIRedfishConfigHandlerProtocolGuid,\r
+                     (VOID**) &ConfigHandler\r
+                     );\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    Status = ConfigHandler->Stop (ConfigHandler);\r
+    if (EFI_ERROR (Status) && Status != EFI_UNSUPPORTED) {\r
+      DEBUG ((DEBUG_ERROR, "ERROR: Failed to stop Redfish config handler %p.\n", ConfigHandler));\r
+      break;\r
+    }\r
+  }\r
+  return Status;\r
+}\r
+/**\r
+  Callback function executed when a Redfish Config Handler Protocol is installed\r
+  by EDK2 Redfish Feature Drivers.\r
+\r
+**/\r
+VOID\r
+RedfishConfigHandlerInitialization (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                            Status;\r
+  EFI_HANDLE                            *HandleBuffer;\r
+  UINTN                                 NumberOfHandles;\r
+  EDKII_REDFISH_CONFIG_HANDLER_PROTOCOL *ConfigHandler;\r
+  UINTN                                 Index;\r
+  UINT32                                Id;\r
+\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEdkIIRedfishConfigHandlerProtocolGuid,\r
+                  NULL,\r
+                  &NumberOfHandles,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return;\r
+  }\r
+\r
+  for (Index = 0; Index < NumberOfHandles; Index++) {\r
+    Status = gBS->HandleProtocol (\r
+                    HandleBuffer [Index],\r
+                    &gEfiCallerIdGuid,\r
+                    (VOID **) &Id\r
+                    );\r
+    if (!EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    Status = gBS->HandleProtocol (\r
+                     HandleBuffer [Index],\r
+                     &gEdkIIRedfishConfigHandlerProtocolGuid,\r
+                     (VOID**) &ConfigHandler\r
+                     );\r
+    ASSERT_EFI_ERROR (Status);\r
+    Status = ConfigHandler->Init (ConfigHandler, &gRedfishConfigData.RedfishServiceInfo);\r
+    if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r
+      DEBUG ((DEBUG_ERROR, "ERROR: Failed to init Redfish config handler %p.\n", ConfigHandler));\r
+    }\r
+    //\r
+    // Install caller ID to indicate Redfish Configure Handler is initialized.\r
+    //\r
+    Status = gBS->InstallProtocolInterface (\r
+                  &HandleBuffer [Index],\r
+                  &gEfiCallerIdGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  (VOID *)&gRedfishConfigData.CallerId\r
+                  );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+}\r