]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/HiiLib/HiiLib.c
Clean up IfrSupportLib.
[mirror_edk2.git] / MdePkg / Library / HiiLib / HiiLib.c
index a1797c930f7d9f863fed9ee08e53fbde81494dcc..be3a31704d2f61799a16444bb0818d00c48fe2fd 100644 (file)
 \r
 #include "InternalHiiLib.h"\r
 \r
-EFI_HII_DATABASE_PROTOCOL   *mHiiDatabaseProt;\r
-EFI_HII_STRING_PROTOCOL     *mHiiStringProt;\r
+CONST EFI_HII_DATABASE_PROTOCOL   *mHiiDatabaseProt;\r
+CONST EFI_HII_STRING_PROTOCOL     *mHiiStringProt;\r
+BOOLEAN mHiiProtocolsInitialized = FALSE;\r
+\r
 \r
 /**\r
-  The constructor function of Hii Library.\r
-  \r
-  The constructor function caches the value of default HII protocol instances.\r
 \r
-  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
-  @param  SystemTable   A pointer to the EFI System Table.\r
-  \r
-  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.\r
+  This function locate Hii relative protocols for later usage.\r
 \r
 **/\r
-EFI_STATUS\r
-EFIAPI\r
-UefiHiiLibConstructor (\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE     *SystemTable\r
+VOID\r
+LocateHiiProtocols (\r
+  VOID\r
   )\r
 {\r
-  EFI_STATUS Status;\r
-  \r
-  Status = gBS->LocateProtocol (\r
-      &gEfiHiiDatabaseProtocolGuid,\r
-      NULL,\r
-      (VOID **) &mHiiDatabaseProt\r
-    );\r
+  EFI_STATUS  Status;\r
+\r
+  if (mHiiProtocolsInitialized) {\r
+    //\r
+    // Only need to initialize the protocol instance once.\r
+    //\r
+    return;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gEfiHiiDatabaseProtocolGuid, NULL, (VOID **) &mHiiDatabaseProt);\r
   ASSERT_EFI_ERROR (Status);\r
-  ASSERT (mHiiDatabaseProt != NULL);\r
 \r
-  Status = gBS->LocateProtocol (\r
-      &gEfiHiiStringProtocolGuid,\r
-      NULL,\r
-      (VOID **) &mHiiStringProt\r
-    );\r
+  Status = gBS->LocateProtocol (&gEfiHiiStringProtocolGuid, NULL, (VOID **) &mHiiStringProt);\r
   ASSERT_EFI_ERROR (Status);\r
-  ASSERT (mHiiStringProt != NULL);\r
 \r
-  return EFI_SUCCESS;\r
+  mHiiProtocolsInitialized = TRUE;\r
 }\r
 \r
 \r
 \r
+/**\r
+  This funciton build the package list based on the package number,\r
+  the GUID of the package list and the list of pointer which point to\r
+  package header that defined by UEFI VFR compiler and StringGather\r
+  tool.\r
+\r
+  #pragma pack (push, 1)\r
+  typedef struct {\r
+    UINT32                  BinaryLength;\r
+    EFI_HII_PACKAGE_HEADER  PackageHeader;\r
+  } TIANO_AUTOGEN_PACKAGES_HEADER;\r
+  #pragma pack (pop)\r
+\r
+  If there is not enough resource for the new package list,\r
+  the function will ASSERT.\r
+\r
+  @param NumberOfPackages The number of packages be \r
+  @param GuidId          The GUID for the package list to be generated.\r
+  @param Marker          The variable argument list. Each entry represent a specific package header that is\r
+                         generated by VFR compiler and StrGather tool. The first 4 bytes is a UINT32 value\r
+                         that indicate the overall length of the package.\r
+\r
+  @return The pointer to the package list header.\r
+\r
+**/\r
 EFI_HII_PACKAGE_LIST_HEADER *\r
 InternalHiiLibPreparePackages (\r
   IN UINTN           NumberOfPackages,\r
-  IN CONST EFI_GUID  *GuidId, OPTIONAL\r
-  VA_LIST            Marker\r
+  IN CONST EFI_GUID  *GuidId,\r
+  IN VA_LIST         Marker\r
   )\r
 {\r
   EFI_HII_PACKAGE_LIST_HEADER *PackageListHeader;\r
@@ -80,6 +96,9 @@ InternalHiiLibPreparePackages (
   \r
   for (Index = 0; Index < NumberOfPackages; Index++) {\r
     CopyMem (&PackageLength, VA_ARG (Marker, VOID *), sizeof (UINT32));\r
+    //\r
+    // Do not count the BinaryLength field.\r
+    //\r
     PackageListLength += (PackageLength - sizeof (UINT32));\r
   }\r
 \r
@@ -89,6 +108,7 @@ InternalHiiLibPreparePackages (
   PackageListLength += sizeof (EFI_HII_PACKAGE_HEADER);\r
   PackageListHeader = AllocateZeroPool (PackageListLength);\r
   ASSERT (PackageListHeader != NULL);\r
+  \r
   CopyMem (&PackageListHeader->PackageListGuid, GuidId, sizeof (EFI_GUID));\r
   PackageListHeader->PackageLength = PackageListLength;\r
 \r
@@ -114,6 +134,20 @@ InternalHiiLibPreparePackages (
   return PackageListHeader;\r
 }\r
 \r
+/**\r
+  Assemble EFI_HII_PACKAGE_LIST according to the passed in packages.\r
+\r
+  If GuidId is NULL, then ASSERT.\r
+  If not enough resource to complete the operation, then ASSERT.\r
+\r
+  @param  NumberOfPackages       Number of packages.\r
+  @param  GuidId                 Package GUID.\r
+  @param  ...                    Variable argument list for packages to be assembled.\r
+\r
+  @return EFI_HII_PACKAGE_LIST_HEADER Pointer of EFI_HII_PACKAGE_LIST_HEADER. The function will ASSERT if system has\r
+                                      not enough resource to complete the operation.\r
+\r
+**/\r
 EFI_HII_PACKAGE_LIST_HEADER *\r
 EFIAPI\r
 HiiLibPreparePackageList (\r
@@ -135,13 +169,37 @@ HiiLibPreparePackageList (
 }\r
 \r
 \r
+/**\r
+  This function allocates pool for an EFI_HII_PACKAGE_LIST structure\r
+  with additional space that is big enough to host all packages described by the variable \r
+  argument list of package pointers.  The allocated structure is initialized using NumberOfPackages, \r
+  GuidId,  and the variable length argument list of package pointers.\r
+\r
+  Then, EFI_HII_PACKAGE_LIST will be register to the default System HII Database. The\r
+  Handle to the newly registered Package List is returned throught HiiHandle.\r
+\r
+  If HiiHandle is NULL, then ASSERT.\r
+\r
+  @param  NumberOfPackages    The number of HII packages to register.\r
+  @param  GuidId              Package List GUID ID.\r
+  @param  DriverHandle        Optional. If not NULL, the DriverHandle on which an instance of DEVICE_PATH_PROTOCOL is installed.\r
+                              This DriverHandle uniquely defines the device that the added packages are associated with.\r
+  @param  HiiHandle           On output, the HiiHandle is update with the handle which can be used to retrieve the Package \r
+                              List later. If the functions failed to add the package to the default HII database, this value will\r
+                              be set to NULL.\r
+  @param  ...                 The variable argument list describing all HII Package.\r
+\r
+  @return  EFI_SUCCESS         If the packages are successfully added to the default HII database.\r
+  @return  EFI_OUT_OF_RESOURCE Not enough resource to complete the operation.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 HiiLibAddPackages (\r
   IN       UINTN               NumberOfPackages,\r
   IN CONST EFI_GUID            *GuidId,\r
   IN       EFI_HANDLE          DriverHandle, OPTIONAL\r
-  OUT      EFI_HII_HANDLE      *HiiHandle, OPTIONAL\r
+  OUT      EFI_HII_HANDLE      *HiiHandle,\r
   ...\r
   )\r
 {\r
@@ -151,6 +209,8 @@ HiiLibAddPackages (
 \r
   ASSERT (HiiHandle != NULL);\r
 \r
+  LocateHiiProtocols ();\r
+\r
   VA_START (Args, HiiHandle);\r
   PackageListHeader = InternalHiiLibPreparePackages (NumberOfPackages, GuidId, Args);\r
 \r
@@ -167,6 +227,16 @@ HiiLibAddPackages (
   return Status;\r
 }\r
 \r
+/**\r
+  Removes a package list from the default HII database.\r
+\r
+  If HiiHandle is NULL, then ASSERT.\r
+  If HiiHandle is not a valid EFI_HII_HANDLE in the default HII database, then ASSERT.\r
+\r
+  @param  HiiHandle                The handle that was previously registered to the data base that is requested for removal.\r
+                                             List later.\r
+\r
+**/\r
 VOID\r
 EFIAPI\r
 HiiLibRemovePackages (\r
@@ -174,13 +244,30 @@ HiiLibRemovePackages (
   )\r
 {\r
   EFI_STATUS Status;\r
+  ASSERT (IsHiiHandleRegistered (HiiHandle));\r
+\r
+  LocateHiiProtocols ();\r
 \r
-  ASSERT (HiiHandle != NULL);\r
   Status = mHiiDatabaseProt->RemovePackageList (mHiiDatabaseProt, HiiHandle);\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 \r
+/**\r
+  Determines the handles that are currently active in the database.\r
+  It's the caller's responsibility to free handle buffer.\r
+\r
+  If HandleBufferLength is NULL, then ASSERT.\r
+  If HiiHandleBuffer is NULL, then ASSERT.\r
+\r
+  @param  HandleBufferLength     On input, a pointer to the length of the handle\r
+                                 buffer. On output, the length of the handle buffer\r
+                                 that is required for the handles found.\r
+  @param  HiiHandleBuffer        Pointer to an array of Hii Handles returned.\r
+\r
+  @retval EFI_SUCCESS            Get an array of Hii Handles successfully.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 HiiLibGetHiiHandles (\r
@@ -196,6 +283,8 @@ HiiLibGetHiiHandles (
 \r
   BufferLength = 0;\r
 \r
+  LocateHiiProtocols ();\r
+\r
   //\r
   // Try to find the actual buffer size for HiiHandle Buffer.\r
   //\r
@@ -227,6 +316,18 @@ HiiLibGetHiiHandles (
   return Status;\r
 }\r
 \r
+/**\r
+  Extract Hii package list GUID for given HII handle.\r
+\r
+  If HiiHandle could not be found in the default HII database, then ASSERT.\r
+  If Guid is NULL, then ASSERT.\r
+\r
+  @param  Handle              Hii handle\r
+  @param  Guid                Package list GUID\r
+\r
+  @retval EFI_SUCCESS            Successfully extract GUID from Hii database.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 HiiLibExtractGuidFromHiiHandle (\r
@@ -239,12 +340,16 @@ HiiLibExtractGuidFromHiiHandle (
   EFI_HII_PACKAGE_LIST_HEADER  *HiiPackageList;\r
 \r
   ASSERT (Guid != NULL);\r
+  ASSERT (IsHiiHandleRegistered (Handle));\r
 \r
   //\r
   // Get HII PackageList\r
   //\r
   BufferSize = 0;\r
   HiiPackageList = NULL;\r
+\r
+  LocateHiiProtocols ();\r
+\r
   Status = mHiiDatabaseProt->ExportPackageLists (mHiiDatabaseProt, Handle, &BufferSize, HiiPackageList);\r
   ASSERT (Status != EFI_NOT_FOUND);\r
   \r
@@ -263,11 +368,24 @@ HiiLibExtractGuidFromHiiHandle (
   //\r
   CopyMem (Guid, &HiiPackageList->PackageListGuid, sizeof (EFI_GUID));\r
 \r
-  gBS->FreePool (HiiPackageList);\r
+  FreePool (HiiPackageList);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Find HII Handle in the default HII database associated with given Device Path.\r
+\r
+  If DevicePath is NULL, then ASSERT.\r
+\r
+  @param  DevicePath             Device Path associated with the HII package list\r
+                                 handle.\r
+\r
+  @retval Handle                 HII package list Handle associated with the Device\r
+                                        Path.\r
+  @retval NULL                   Hii Package list handle is not found.\r
+\r
+**/\r
 EFI_HII_HANDLE\r
 EFIAPI\r
 HiiLibDevicePathToHiiHandle (\r
@@ -320,12 +438,14 @@ HiiLibDevicePathToHiiHandle (
       break;\r
     }\r
   }\r
-  gBS->FreePool (Handles);\r
+  FreePool (Handles);\r
 \r
   if (DriverHandle == NULL) {\r
     return NULL;\r
   }\r
 \r
+  LocateHiiProtocols ();\r
+\r
   //\r
   // Retrieve all Hii Handles from HII database\r
   //\r
@@ -340,7 +460,7 @@ HiiLibDevicePathToHiiHandle (
                           HiiHandles\r
                           );\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
-    gBS->FreePool (HiiHandles);\r
+    FreePool (HiiHandles);\r
     HiiHandles = AllocatePool (BufferSize);\r
     ASSERT (HiiHandles != NULL);\r
 \r
@@ -354,7 +474,7 @@ HiiLibDevicePathToHiiHandle (
   }\r
 \r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (HiiHandles);\r
+    FreePool (HiiHandles);\r
     return NULL;\r
   }\r
 \r
@@ -375,10 +495,19 @@ HiiLibDevicePathToHiiHandle (
     }\r
   }\r
 \r
-  gBS->FreePool (HiiHandles);\r
+  FreePool (HiiHandles);\r
   return HiiHandle;\r
 }\r
 \r
+/**\r
+  This function check if the Hii Handle is a valid handle registered\r
+  in the HII database.\r
+\r
+  @param HiiHandle The HII Handle.\r
+\r
+  @retval TRUE If it is a valid HII handle.\r
+  @retval FALSE If it is a invalid HII handle.\r
+**/\r
 BOOLEAN\r
 IsHiiHandleRegistered (\r
   EFI_HII_HANDLE    HiiHandle\r
@@ -392,6 +521,9 @@ IsHiiHandleRegistered (
 \r
   HiiPackageList = NULL;\r
   BufferSize = 0;\r
+\r
+  LocateHiiProtocols ();\r
+\r
   Status = mHiiDatabaseProt->ExportPackageLists (\r
              mHiiDatabaseProt,\r
              HiiHandle,\r