]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
Remove the RT attribute for HDDP variable and validate the variable content before...
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsBoot.c
index 4b4333bd88bd14574cc97d9c99700da26fcefeef..de69a04b04be9b1bda379f5c33b5f220e0b93a53 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   BDS Lib functions which relate with create or process the boot option.\r
 \r
-Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2014, 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
@@ -40,7 +40,7 @@ GenericBdsLibConstructor (
 \r
   gBdsLibStringPackHandle = HiiAddPackages (\r
                               &gBdsLibStringPackageGuid,\r
-                              &ImageHandle,\r
+                              ImageHandle,\r
                               GenericBdsLibStrings,\r
                               NULL\r
                               );\r
@@ -82,6 +82,10 @@ BdsDeleteBootOption (
                   0,\r
                   NULL\r
                   );\r
+  //\r
+  // Deleting variable with existing variable implementation shouldn't fail.\r
+  //\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
   // adjust boot order array\r
@@ -517,23 +521,23 @@ BdsDeleteAllInvalidLegacyBootOptions (
     return Status;\r
   }\r
 \r
-  LegacyBios->GetBbsInfo (\r
-                LegacyBios,\r
-                &HddCount,\r
-                &LocalHddInfo,\r
-                &BbsCount,\r
-                &LocalBbsTable\r
-                );\r
-\r
   BootOrder = BdsLibGetVariableAndSize (\r
                 L"BootOrder",\r
                 &gEfiGlobalVariableGuid,\r
                 &BootOrderSize\r
                 );\r
   if (BootOrder == NULL) {\r
-    BootOrderSize = 0;\r
+    return EFI_NOT_FOUND;\r
   }\r
 \r
+  LegacyBios->GetBbsInfo (\r
+                LegacyBios,\r
+                &HddCount,\r
+                &LocalHddInfo,\r
+                &BbsCount,\r
+                &LocalBbsTable\r
+                );\r
+\r
   Index = 0;\r
   while (Index < BootOrderSize / sizeof (UINT16)) {\r
     UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
@@ -626,9 +630,11 @@ BdsDeleteAllInvalidLegacyBootOptions (
                   BootOrderSize,\r
                   BootOrder\r
                   );\r
-  if (BootOrder != NULL) {\r
-    FreePool (BootOrder);\r
-  }\r
+  //\r
+  // Shrinking variable with existing variable implementation shouldn't fail.\r
+  //\r
+  ASSERT_EFI_ERROR (Status);\r
+  FreePool (BootOrder);\r
 \r
   return Status;\r
 }\r
@@ -857,11 +863,10 @@ BdsAddNonExistingLegacyBootOptions (
                 &BootOrder,\r
                 &BootOrderSize\r
                 );\r
-      if (EFI_ERROR (Status)) {\r
-        break;\r
+      if (!EFI_ERROR (Status)) {\r
+        BbsIndex     = Index;\r
+        OptionNumber = BootOrder[BootOrderSize / sizeof (UINT16) - 1];\r
       }\r
-      BbsIndex     = Index;\r
-      OptionNumber = BootOrder[BootOrderSize / sizeof (UINT16) - 1];\r
     }\r
 \r
     ASSERT (BbsIndex == Index);\r
@@ -2229,10 +2234,6 @@ BdsLibBootViaBootOption (
   LIST_ENTRY                TempBootLists;\r
   EFI_BOOT_LOGO_PROTOCOL    *BootLogo;\r
 \r
-  PERF_CODE (\r
-    AllocateMemoryForPerformanceData ();\r
-  );\r
-\r
   *ExitDataSize = 0;\r
   *ExitData     = NULL;\r
 \r
@@ -2269,7 +2270,7 @@ BdsLibBootViaBootOption (
     // In this case, "BootCurrent" is not created.\r
     // Only create the BootCurrent variable when it points to a valid Boot#### variable.\r
     //\r
-    gRT->SetVariable (\r
+    SetVariableAndReportStatusCodeOnError (\r
           L"BootCurrent",\r
           &gEfiGlobalVariableGuid,\r
           EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
@@ -2278,6 +2279,11 @@ BdsLibBootViaBootOption (
           );\r
   }\r
 \r
+  //\r
+  // Report Status Code to indicate ReadyToBoot event will be signalled\r
+  //\r
+  REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT));\r
+\r
   //\r
   // Signal the EVT_SIGNAL_READY_TO_BOOT event\r
   //\r
@@ -2368,47 +2374,40 @@ BdsLibBootViaBootOption (
       // and get the bootable media handle\r
       //\r
       Handle = BdsLibGetBootableHandle(DevicePath);\r
-      if (Handle == NULL) {\r
-        goto Done;\r
-      }\r
-      //\r
-      // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
-      //  machinename is ia32, ia64, x64, ...\r
-      //\r
-      FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
-      if (FilePath != NULL) {\r
-        REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
-        Status = gBS->LoadImage (\r
-                        TRUE,\r
-                        gImageHandle,\r
-                        FilePath,\r
-                        NULL,\r
-                        0,\r
-                        &ImageHandle\r
-                        );\r
-       if (EFI_ERROR (Status)) {\r
-          //\r
-          // The DevicePath failed, and it's not a valid\r
-          // removable media device.\r
-          //\r
-          goto Done;\r
+      if (Handle != NULL) {\r
+        //\r
+        // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
+        //  machinename is ia32, ia64, x64, ...\r
+        //\r
+        FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
+        if (FilePath != NULL) {\r
+          REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
+          Status = gBS->LoadImage (\r
+                          TRUE,\r
+                          gImageHandle,\r
+                          FilePath,\r
+                          NULL,\r
+                          0,\r
+                          &ImageHandle\r
+                          );\r
         }\r
       }\r
     }\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      //\r
-      // It there is any error from the Boot attempt exit now.\r
-      //\r
-      goto Done;\r
-    }\r
   }\r
   //\r
   // Provide the image with it's load options\r
   //\r
-  if (ImageHandle == NULL) {\r
+  if ((ImageHandle == NULL) || (EFI_ERROR(Status))) {\r
+    //\r
+    // Report Status Code to indicate that the failure to load boot option\r
+    //\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+      (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR)\r
+      );    \r
     goto Done;\r
   }\r
+\r
   Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
@@ -2442,6 +2441,15 @@ BdsLibBootViaBootOption (
 \r
   Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);\r
   DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // Report Status Code to indicate that boot failure\r
+    //\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+      (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED)\r
+      );\r
+  }\r
 \r
   //\r
   // Clear the Watchdog Timer after the image returns\r
@@ -2460,13 +2468,14 @@ Done:
 \r
   //\r
   // Clear Boot Current\r
+  // Deleting variable with current implementation shouldn't fail.\r
   //\r
   gRT->SetVariable (\r
         L"BootCurrent",\r
         &gEfiGlobalVariableGuid,\r
         EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
         0,\r
-        &Option->BootCurrent\r
+        NULL\r
         );\r
 \r
   return Status;\r
@@ -2515,11 +2524,28 @@ BdsExpandPartitionPartialDevicePathToFull (
   // If exist, search the front path which point to partition node in the variable instants.\r
   // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system\r
   //\r
-  CachedDevicePath = BdsLibGetVariableAndSize (\r
-                      HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
-                      &gHdBootDevicePathVariablGuid,\r
-                      &CachedDevicePathSize\r
-                      );\r
+  GetVariable2 (\r
+    HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
+    &gHdBootDevicePathVariablGuid,\r
+    (VOID **) &CachedDevicePath,\r
+    &CachedDevicePathSize\r
+    );\r
+\r
+  //\r
+  // Delete the invalid HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.\r
+  //\r
+  if ((CachedDevicePath != NULL) && !IsDevicePathValid (CachedDevicePath, CachedDevicePathSize)) {\r
+    FreePool (CachedDevicePath);\r
+    CachedDevicePath = NULL;\r
+    Status = gRT->SetVariable (\r
+                    HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
+                    &gHdBootDevicePathVariablGuid,\r
+                    0,\r
+                    0,\r
+                    NULL\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
 \r
   if (CachedDevicePath != NULL) {\r
     TempNewDevicePath = CachedDevicePath;\r
@@ -2577,11 +2603,12 @@ BdsExpandPartitionPartialDevicePathToFull (
         FreePool (TempNewDevicePath);\r
         //\r
         // Save the matching Device Path so we don't need to do a connect all next time\r
+        // Failure to set the variable only impacts the performance when next time expanding the short-form device path.\r
         //\r
         Status = gRT->SetVariable (\r
                         HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
                         &gHdBootDevicePathVariablGuid,\r
-                        EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                        EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
                         GetDevicePathSize (CachedDevicePath),\r
                         CachedDevicePath\r
                         );\r
@@ -2634,17 +2661,16 @@ BdsExpandPartitionPartialDevicePathToFull (
           TempNewDevicePath = CachedDevicePath;\r
           CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r
           FreePool(TempNewDevicePath);\r
+        }\r
 \r
-          TempNewDevicePath = CachedDevicePath;\r
-          CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
-          if (TempNewDevicePath != NULL) {\r
-            FreePool(TempNewDevicePath);\r
-          }\r
-        } else {\r
+        if (CachedDevicePath != NULL) {\r
           TempNewDevicePath = CachedDevicePath;\r
           CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
           FreePool(TempNewDevicePath);\r
+        } else {\r
+          CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r
         }\r
+\r
         //\r
         // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r
         // If the user try to boot many OS in different HDs or partitions, in theory, \r
@@ -2676,11 +2702,12 @@ BdsExpandPartitionPartialDevicePathToFull (
 \r
       //\r
       // Save the matching Device Path so we don't need to do a connect all next time\r
+      // Failure to set the variable only impacts the performance when next time expanding the short-form device path.\r
       //\r
       Status = gRT->SetVariable (\r
                       HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
                       &gHdBootDevicePathVariablGuid,\r
-                      EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                      EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
                       GetDevicePathSize (CachedDevicePath),\r
                       CachedDevicePath\r
                       );\r
@@ -2885,6 +2912,10 @@ BdsLibDeleteOptionFromHandle (
                   BootOrderSize,\r
                   BootOrder\r
                   );\r
+  //\r
+  // Shrinking variable with existing variable implementation shouldn't fail.\r
+  //\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   FreePool (BootOrder);\r
 \r
@@ -2983,6 +3014,10 @@ BdsDeleteAllInvalidEfiBootOption (
                       NULL\r
                       );\r
       //\r
+      // Deleting variable with current variable implementation shouldn't fail.\r
+      //\r
+      ASSERT_EFI_ERROR (Status);\r
+      //\r
       // Mark this boot option in boot order as deleted\r
       //\r
       BootOrder[Index] = 0xffff;\r
@@ -3010,6 +3045,10 @@ BdsDeleteAllInvalidEfiBootOption (
                   Index2 * sizeof (UINT16),\r
                   BootOrder\r
                   );\r
+  //\r
+  // Shrinking variable with current variable implementation shouldn't fail.\r
+  //\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   FreePool (BootOrder);\r
 \r
@@ -3133,7 +3172,9 @@ BdsLibEnumerateAllBootOption (
         AsciiStrSize (PlatLang),\r
         PlatLang\r
         );\r
-      ASSERT_EFI_ERROR (Status);\r
+      //\r
+      // Failure to set the variable only impacts the performance next time enumerating the boot options.\r
+      //\r
 \r
       if (LastLang != NULL) {\r
         FreePool (LastLang);\r
@@ -3476,6 +3517,7 @@ BdsLibBootNext (
   VOID\r
   )\r
 {\r
+  EFI_STATUS        Status;\r
   UINT16            *BootNext;\r
   UINTN             BootNextSize;\r
   CHAR16            Buffer[20];\r
@@ -3500,13 +3542,17 @@ BdsLibBootNext (
   // Clear the boot next variable first\r
   //\r
   if (BootNext != NULL) {\r
-    gRT->SetVariable (\r
-          L"BootNext",\r
-          &gEfiGlobalVariableGuid,\r
-          EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
-          0,\r
-          BootNext\r
-          );\r
+    Status = gRT->SetVariable (\r
+                    L"BootNext",\r
+                    &gEfiGlobalVariableGuid,\r
+                    EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                    0,\r
+                    NULL\r
+                    );\r
+    //\r
+    // Deleting variable with current variable implementation shouldn't fail.\r
+    //\r
+    ASSERT_EFI_ERROR (Status);\r
 \r
     //\r
     // Start to build the boot option and try to boot\r