]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c
1. Add new API supports for PEM & X509 key retrieving & verification;
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsMisc.c
index 4c59b832434bf3cb2e5e6c8f36d41e56c7daa1c1..1e88922630889d81e312f272349d542148d10174 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Misc BDS library function\r
 \r
-Copyright (c) 2004 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are 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
@@ -79,7 +79,7 @@ BdsLibLoadDrivers (
     //\r
     Status = gBS->LoadImage (\r
                     FALSE,\r
-                    mBdsImageHandle,\r
+                    gImageHandle,\r
                     Option->DevicePath,\r
                     NULL,\r
                     0,\r
@@ -499,7 +499,7 @@ BdsLibVariableToOption (
   // Unicode stream to ASCII without any loss in meaning.\r
   //\r
   if (*VariableName == 'B') {\r
-    NumOff = sizeof (L"Boot")/sizeof(CHAR16) -1 ;\r
+    NumOff = (UINT8) (sizeof (L"Boot") / sizeof(CHAR16) - 1);\r
     Option->BootCurrent = (UINT16) ((VariableName[NumOff]  -'0') * 0x1000);\r
     Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100));\r
     Option->BootCurrent = (UINT16) (Option->BootCurrent +  ((VariableName[NumOff+2]-'0') * 0x10));\r
@@ -572,9 +572,9 @@ BdsLibBuildOptionFromVar (
     }\r
 \r
     Option              = BdsLibVariableToOption (BdsCommonOptionList, OptionName);\r
-    ASSERT (Option != NULL);\r
-    Option->BootCurrent = OptionOrder[Index];\r
-\r
+    if (Option != NULL) {\r
+      Option->BootCurrent = OptionOrder[Index];\r
+    }\r
   }\r
 \r
   FreePool (OptionOrder);\r
@@ -795,7 +795,7 @@ BdsLibOutputStrings (
     // If String is NULL, then it's the end of the list\r
     //\r
     String = VA_ARG (Args, CHAR16 *);\r
-    if (String != NULL) {\r
+    if (String == NULL) {\r
       break;\r
     }\r
 \r
@@ -811,7 +811,7 @@ BdsLibOutputStrings (
 }\r
 \r
 //\r
-//  Following are BDS Lib functions which  contain all the code about setup browser reset reminder feature.\r
+//  Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.\r
 //  Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser  if\r
 //  user change any option setting which needs a reset to be effective, and  the reset will be applied according to  the user selection.\r
 //\r
@@ -1006,11 +1006,12 @@ BdsLibGetImageHeader (
     Root = NULL;\r
     goto Done;\r
   }\r
-\r
+  ASSERT (Root != NULL);\r
   Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);\r
   if (EFI_ERROR (Status)) {\r
     goto Done;\r
   }\r
+  ASSERT (ThisFile != NULL);\r
 \r
   //\r
   // Get file size\r
@@ -1095,36 +1096,48 @@ BdsLibGetImageHeader (
 }\r
 \r
 /**\r
-\r
-  This routine is a notification function for legayc boot or exit boot\r
-  service event. It will adjust the memory information for different\r
-  memory type and save them into the variables for next boot.\r
-\r
-\r
-  @param Event           The event that triggered this notification function.\r
-  @param Context         Pointer to the notification functions context.\r
-\r
+  This routine adjust the memory information for different memory type and \r
+  save them into the variables for next boot.\r
 **/\r
 VOID\r
-EFIAPI\r
 BdsSetMemoryTypeInformationVariable (\r
-  EFI_EVENT  Event,\r
-  VOID       *Context\r
+  VOID\r
   )\r
 {\r
   EFI_STATUS                   Status;\r
   EFI_MEMORY_TYPE_INFORMATION  *PreviousMemoryTypeInformation;\r
   EFI_MEMORY_TYPE_INFORMATION  *CurrentMemoryTypeInformation;\r
   UINTN                        VariableSize;\r
-  BOOLEAN                      UpdateRequired;\r
   UINTN                        Index;\r
   UINTN                        Index1;\r
   UINT32                       Previous;\r
   UINT32                       Current;\r
   UINT32                       Next;\r
   EFI_HOB_GUID_TYPE            *GuidHob;\r
+  BOOLEAN                      MemoryTypeInformationModified;\r
+  BOOLEAN                      MemoryTypeInformationVariableExists;\r
+\r
+  MemoryTypeInformationModified       = FALSE;\r
+  MemoryTypeInformationVariableExists = FALSE;\r
 \r
-  UpdateRequired = FALSE;\r
+  //\r
+  // Only get the the Memory Type Information variable in the boot mode \r
+  // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
+  // Information is not valid in this boot mode.\r
+  //\r
+  if (GetBootModeHob () != BOOT_WITH_DEFAULT_SETTINGS) {\r
+    VariableSize = 0;\r
+    Status = gRT->GetVariable (\r
+                    EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
+                    &gEfiMemoryTypeInformationGuid,\r
+                    NULL, \r
+                    &VariableSize, \r
+                    NULL\r
+                    );\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      MemoryTypeInformationVariableExists = TRUE;\r
+    }\r
+  }\r
 \r
   //\r
   // Retrieve the current memory usage statistics.  If they are not found, then\r
@@ -1156,6 +1169,10 @@ BdsSetMemoryTypeInformationVariable (
   //\r
   // Use a heuristic to adjust the Memory Type Information for the next boot\r
   //\r
+  DEBUG ((EFI_D_INFO, "Memory  Previous  Current    Next   \n"));\r
+  DEBUG ((EFI_D_INFO, " Type    Pages     Pages     Pages  \n"));\r
+  DEBUG ((EFI_D_INFO, "======  ========  ========  ========\n"));\r
+\r
   for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
 \r
     Current = 0;\r
@@ -1175,7 +1192,9 @@ BdsSetMemoryTypeInformationVariable (
     //\r
     // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail\r
     //\r
-    if (Current > Previous) {\r
+    if (!MemoryTypeInformationVariableExists && Current < Previous) {\r
+      Next = Current + (Current >> 2);\r
+    } else if (Current > Previous) {\r
       Next = Current + (Current >> 2);\r
     } else {\r
       Next = Previous;\r
@@ -1186,31 +1205,39 @@ BdsSetMemoryTypeInformationVariable (
 \r
     if (Next != Previous) {\r
       PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
-      UpdateRequired = TRUE;\r
+      MemoryTypeInformationModified = TRUE;\r
     }\r
 \r
+    DEBUG ((EFI_D_INFO, "  %02x    %08x  %08x  %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));\r
   }\r
 \r
   //\r
-  // If any changes were made to the Memory Type Information settings, then set the new variable value\r
+  // If any changes were made to the Memory Type Information settings, then set the new variable value;\r
+  // Or create the variable in first boot.\r
   //\r
-  if (UpdateRequired) {\r
+  if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
     Status = gRT->SetVariable (\r
-          EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
-          &gEfiMemoryTypeInformationGuid,\r
-          EFI_VARIABLE_NON_VOLATILE  | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
-          VariableSize,\r
-          PreviousMemoryTypeInformation\r
-          );\r
-  }\r
+                    EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
+                    &gEfiMemoryTypeInformationGuid,\r
+                    EFI_VARIABLE_NON_VOLATILE  | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                    VariableSize,\r
+                    PreviousMemoryTypeInformation\r
+                    );\r
 \r
-  return;\r
+    //\r
+    // If the Memory Type Information settings have been modified, then reset the platform\r
+    // so the new Memory Type Information setting will be used to guarantee that an S4\r
+    // entry/resume cycle will not fail.\r
+    //\r
+    if (MemoryTypeInformationModified && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
+      DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
+      gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
+    }\r
+  }\r
 }\r
 \r
 /**\r
-  This routine register a function to adjust the different type memory page number\r
-  just before booting and save the updated info into the variable for next boot to use.\r
-\r
+  This routine is kept for backward compatibility.\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1218,19 +1245,37 @@ BdsLibSaveMemoryTypeInformation (
   VOID\r
   )\r
 {\r
-  EFI_STATUS                   Status;\r
-  EFI_EVENT                    ReadyToBootEvent;\r
-\r
-  Status = EfiCreateEventReadyToBootEx (\r
-           TPL_CALLBACK,\r
-           BdsSetMemoryTypeInformationVariable,\r
-           NULL,\r
-           &ReadyToBootEvent\r
-           );\r
+}\r
+\r
+\r
+/**\r
+  Identify a user and, if authenticated, returns the current user profile handle.\r
+\r
+  @param[out]  User           Point to user profile handle.\r
+  \r
+  @retval EFI_SUCCESS         User is successfully identified, or user identification\r
+                              is not supported.\r
+  @retval EFI_ACCESS_DENIED   User is not successfully identified\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BdsLibUserIdentify (\r
+  OUT EFI_USER_PROFILE_HANDLE         *User\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_USER_MANAGER_PROTOCOL           *Manager;\r
+  \r
+  Status = gBS->LocateProtocol (\r
+                  &gEfiUserManagerProtocolGuid,\r
+                  NULL,\r
+                  (VOID **) &Manager\r
+                  );\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_ERROR,"Bds Set Memory Type Informationa Variable Fails\n"));\r
+    return EFI_SUCCESS;\r
   }\r
 \r
+  return Manager->Identify (Manager, User);\r
 }\r
 \r
-\r