Support adding boot option from removable media, and removing invalid EFI boot option
authorxli24 <xli24@de2fecce-e211-0410-80a6-f3fac2684e05>
Fri, 8 Dec 2006 07:33:23 +0000 (07:33 +0000)
committerxli24 <xli24@de2fecce-e211-0410-80a6-f3fac2684e05>
Fri, 8 Dec 2006 07:33:23 +0000 (07:33 +0000)
git-svn-id: https://edk2.tianocore.org/svn/edk2/trunk@2078 de2fecce-e211-0410-80a6-f3fac2684e05

edk2/EdkNt32Pkg/Library/EdkGenericBdsLib/BdsBoot.c

index c9b1eed..05e1b26 100644 (file)
@@ -120,6 +120,8 @@ Returns:
   EFI_DEVICE_PATH_PROTOCOL  *FilePath;\r
   EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
   EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;\r
+  EFI_BLOCK_IO_PROTOCOL     *BlkIo;\r
+  VOID                      *Buffer;\r
 \r
   *ExitDataSize = 0;\r
   *ExitData     = NULL;\r
@@ -209,6 +211,32 @@ Returns:
     if (!EFI_ERROR (Status) && IsDevicePathEnd (TempDevicePath)) {\r
       FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
       if (FilePath) {\r
+        //\r
+        // Issue a dummy read to the device to check for media change.\r
+        // When the removable media is changed, any Block IO read/write will\r
+        // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
+        // returned. After the Block IO protocol is reinstalled, subsequent\r
+        // Block IO read/write will success.\r
+        //\r
+        Status = gBS->HandleProtocol (\r
+                        Handle,\r
+                        &gEfiBlockIoProtocolGuid,\r
+                        (VOID **) &BlkIo\r
+                        );\r
+        if (!EFI_ERROR (Status)) {\r
+          Buffer = AllocatePool (BlkIo->Media->BlockSize);\r
+          if (Buffer != NULL) {\r
+            BlkIo->ReadBlocks (\r
+                     BlkIo,\r
+                     BlkIo->Media->MediaId,\r
+                     0,\r
+                     BlkIo->Media->BlockSize,\r
+                     Buffer\r
+                     );\r
+            gBS->FreePool (Buffer);\r
+          }\r
+        }\r
+\r
         Status = gBS->LoadImage (\r
                         TRUE,\r
                         mBdsImageHandle,\r
@@ -429,6 +457,290 @@ Returns:
   return Status;\r
 }\r
 \r
+EFI_STATUS\r
+BdsLibDeleteOptionFromHandle (\r
+  IN  EFI_HANDLE                 Handle\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Delete the boot option associated with the handle passed in\r
+\r
+Arguments:\r
+\r
+  Handle - The handle which present the device path to create boot option\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS           - Delete the boot option success\r
+\r
+  EFI_NOT_FOUND         - If the Device Path is not found in the system\r
+\r
+  EFI_OUT_OF_RESOURCES  - Lack of memory resource\r
+\r
+  Other                 - Error return value from SetVariable()\r
+\r
+--*/\r
+{\r
+  UINT16                    *BootOrder;\r
+  UINT8                     *BootOptionVar;\r
+  UINTN                     BootOrderSize;\r
+  UINTN                     BootOptionSize;\r
+  EFI_STATUS                Status;\r
+  UINTN                     Index;\r
+  UINT16                    BootOption[BOOT_OPTION_MAX_CHAR];\r
+  UINTN                     DevicePathSize;\r
+  UINTN                     OptionDevicePathSize;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *OptionDevicePath;\r
+  UINT8                     *TempPtr;\r
+  CHAR16                    *Description;\r
+\r
+  Status        = EFI_SUCCESS;\r
+  BootOrder     = NULL;\r
+  BootOrderSize = 0;\r
+\r
+  BootOrder = BdsLibGetVariableAndSize (\r
+                L"BootOrder",\r
+                &gEfiGlobalVariableGuid,\r
+                &BootOrderSize\r
+                );\r
+  if (NULL == BootOrder) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  DevicePath = DevicePathFromHandle (Handle);\r
+  if (DevicePath == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  DevicePathSize = GetDevicePathSize (DevicePath);\r
+\r
+  Index = 0;\r
+  while (Index < BootOrderSize / sizeof (UINT16)) {\r
+    UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
+    BootOptionVar = BdsLibGetVariableAndSize (\r
+                      BootOption,\r
+                      &gEfiGlobalVariableGuid,\r
+                      &BootOptionSize\r
+                      );\r
+    if (NULL == BootOptionVar) {\r
+      gBS->FreePool (BootOrder);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    TempPtr = BootOptionVar;\r
+    TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
+    Description = (CHAR16 *) TempPtr;\r
+    TempPtr += StrSize ((CHAR16 *) TempPtr);\r
+    OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+    OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
+\r
+    //\r
+    // Check whether the device path match\r
+    //\r
+    if ((OptionDevicePathSize == DevicePathSize) &&\r
+        (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
+      BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
+      gBS->FreePool (BootOptionVar);\r
+      break;\r
+    }\r
+\r
+    gBS->FreePool (BootOptionVar);\r
+    Index++;\r
+  }\r
+\r
+  Status = gRT->SetVariable (\r
+                  L"BootOrder",\r
+                  &gEfiGlobalVariableGuid,\r
+                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                  BootOrderSize,\r
+                  BootOrder\r
+                  );\r
+\r
+  gBS->FreePool (BootOrder);\r
+\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+BdsDeleteAllInvalidEfiBootOption (\r
+  VOID\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Delete all invalid EFI boot options. The probable invalid boot option could\r
+  be Removable media or Network boot device.\r
+\r
+Arguments:\r
+\r
+  VOID\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS           - Delete all invalid boot option success\r
+\r
+  EFI_NOT_FOUND         - Variable "BootOrder" is not found\r
+\r
+  EFI_OUT_OF_RESOURCES  - Lack of memory resource\r
+\r
+  Other                 - Error return value from SetVariable()\r
+\r
+--*/\r
+{\r
+  UINT16                    *BootOrder;\r
+  UINT8                     *BootOptionVar;\r
+  UINTN                     BootOrderSize;\r
+  UINTN                     BootOptionSize;\r
+  EFI_STATUS                Status;\r
+  UINTN                     Index;\r
+  UINTN                     Index2;\r
+  UINT16                    BootOption[BOOT_OPTION_MAX_CHAR];\r
+  UINTN                     OptionDevicePathSize;\r
+  EFI_DEVICE_PATH_PROTOCOL  *TempDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *LastDeviceNode;\r
+  EFI_DEVICE_PATH_PROTOCOL  *OptionDevicePath;\r
+  UINT8                     *TempPtr;\r
+  CHAR16                    *Description;\r
+  EFI_HANDLE                Handle;\r
+  BOOLEAN                   NeedDelete;\r
+\r
+  Status        = EFI_SUCCESS;\r
+  BootOrder     = NULL;\r
+  BootOrderSize = 0;\r
+\r
+  BootOrder = BdsLibGetVariableAndSize (\r
+                L"BootOrder",\r
+                &gEfiGlobalVariableGuid,\r
+                &BootOrderSize\r
+                );\r
+  if (NULL == BootOrder) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  Index = 0;\r
+  while (Index < BootOrderSize / sizeof (UINT16)) {\r
+    UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
+    BootOptionVar = BdsLibGetVariableAndSize (\r
+                      BootOption,\r
+                      &gEfiGlobalVariableGuid,\r
+                      &BootOptionSize\r
+                      );\r
+    if (NULL == BootOptionVar) {\r
+      gBS->FreePool (BootOrder);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    TempPtr = BootOptionVar;\r
+    TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
+    Description = (CHAR16 *) TempPtr;\r
+    TempPtr += StrSize ((CHAR16 *) TempPtr);\r
+    OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+    OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
+\r
+    //\r
+    // Skip legacy boot option (BBS boot device)\r
+    //\r
+    if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
+        (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
+      gBS->FreePool (BootOptionVar);\r
+      Index++;\r
+      continue;\r
+    }\r
+\r
+    TempDevicePath = OptionDevicePath;\r
+    LastDeviceNode = OptionDevicePath;\r
+    while (!EfiIsDevicePathEnd (TempDevicePath)) {\r
+      LastDeviceNode = TempDevicePath;\r
+      TempDevicePath = EfiNextDevicePathNode (TempDevicePath);\r
+    }\r
+    //\r
+    // Skip the boot option that point to a file, since the device path in \r
+    // removable media boot option doesn't contains a file name.\r
+    //\r
+    if (((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
+         (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) ||\r
+        //\r
+        // Skip boot option for internal Shell, it's always valid\r
+        //\r
+        (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL)) {\r
+      gBS->FreePool (BootOptionVar);\r
+      Index++;\r
+      continue;\r
+    }\r
+\r
+    NeedDelete = TRUE;\r
+    //\r
+    // Check if it's a valid boot option for removable media\r
+    //\r
+    TempDevicePath = OptionDevicePath;\r
+    Status = gBS->LocateDevicePath (\r
+                    &gEfiSimpleFileSystemProtocolGuid,\r
+                    &TempDevicePath,\r
+                    &Handle\r
+                    );\r
+    if (!EFI_ERROR (Status)) {\r
+      NeedDelete = FALSE;\r
+    }\r
+    //\r
+    // Check if it's a valid boot option for network boot device\r
+    //\r
+    TempDevicePath = OptionDevicePath;\r
+    Status = gBS->LocateDevicePath (\r
+                    &gEfiLoadFileProtocolGuid,\r
+                    &TempDevicePath,\r
+                    &Handle\r
+                    );\r
+    if (!EFI_ERROR (Status)) {\r
+      NeedDelete = FALSE;\r
+    }\r
+\r
+    if (NeedDelete) {\r
+      //\r
+      // Delete this invalid boot option "Boot####"\r
+      //\r
+      Status = gRT->SetVariable (\r
+                      BootOption,\r
+                      &gEfiGlobalVariableGuid,\r
+                      EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                      0,\r
+                      NULL\r
+                      );\r
+      //\r
+      // Mark this boot option in boot order as deleted\r
+      //\r
+      BootOrder[Index] = 0xffff;\r
+    }\r
+\r
+    gBS->FreePool (BootOptionVar);\r
+    Index++;\r
+  }\r
+\r
+  //\r
+  // Adjust boot order array\r
+  //\r
+  Index2 = 0;\r
+  for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {\r
+    if (BootOrder[Index] != 0xffff) {\r
+      BootOrder[Index2] = BootOrder[Index];\r
+      Index2 ++;\r
+    }\r
+  }\r
+  Status = gRT->SetVariable (\r
+                  L"BootOrder",\r
+                  &gEfiGlobalVariableGuid,\r
+                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                  Index2 * sizeof (UINT16),\r
+                  BootOrder\r
+                  );\r
+\r
+  gBS->FreePool (BootOrder);\r
+\r
+  return Status;\r
+}\r
+\r
 EFI_STATUS\r
 BdsLibEnumerateAllBootOption (\r
   IN OUT LIST_ENTRY      *BdsBootOptionList\r
@@ -456,11 +768,8 @@ Returns:
   UINT16                        BootOptionNumber;\r
   UINTN                         NumberFileSystemHandles;\r
   EFI_HANDLE                    *FileSystemHandles;\r
-  UINTN                         NumberBlkIoHandles;\r
-  EFI_HANDLE                    *BlkIoHandles;\r
   EFI_BLOCK_IO_PROTOCOL         *BlkIo;\r
   UINTN                         Index;\r
-  EFI_DEVICE_PATH_PROTOCOL      *DevicePath;\r
   UINTN                         NumberLoadFileHandles;\r
   EFI_HANDLE                    *LoadFileHandles;\r
   VOID                          *ProtocolInstance;\r
@@ -471,6 +780,10 @@ Returns:
   UINTN                         Size;\r
   EFI_FV_FILE_ATTRIBUTES        Attributes;\r
   UINT32                        AuthenticationStatus;\r
+  EFI_DEVICE_PATH_PROTOCOL      *FilePath;\r
+  EFI_HANDLE                    ImageHandle;\r
+  EFI_LOADED_IMAGE_PROTOCOL     *ImageInfo;\r
+  BOOLEAN                       NeedDelete;\r
 \r
   BootOptionNumber = 0;\r
 \r
@@ -490,49 +803,11 @@ Returns:
   REFRESH_LEGACY_BOOT_OPTIONS;\r
 \r
   //\r
-  // Check all the block IO to create boot option\r
+  // Delete invalid boot option\r
   //\r
-  gBS->LocateHandleBuffer (\r
-        ByProtocol,\r
-        &gEfiBlockIoProtocolGuid,\r
-        NULL,\r
-        &NumberBlkIoHandles,\r
-        &BlkIoHandles\r
-        );\r
-  for (Index = 0; Index < NumberBlkIoHandles; Index++) {\r
-    Status = gBS->HandleProtocol (\r
-                    BlkIoHandles[Index],\r
-                    &gEfiBlockIoProtocolGuid,\r
-                    (VOID **) &BlkIo\r
-                    );\r
-    if (EFI_ERROR (Status)) {\r
-      continue;\r
-    }\r
-\r
-    if (!BlkIo->Media->RemovableMedia) {\r
-      //\r
-      // Skip fixed Media device on first loop interration\r
-      //\r
-      continue;\r
-    }\r
-\r
-    DevicePath = DevicePathFromHandle (BlkIoHandles[Index]);\r
-    if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && \r
-        (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
-        ) {\r
-      //\r
-      // Build the boot option\r
-      //\r
-      BdsLibBuildOptionFromHandle (BlkIoHandles[Index], BdsBootOptionList);\r
-      BootOptionNumber++;\r
-    }\r
-  }\r
-\r
-  if (NumberBlkIoHandles) {\r
-    gBS->FreePool (BlkIoHandles);\r
-  }\r
+  BdsDeleteAllInvalidEfiBootOption ();\r
   //\r
-  // Parse Fixed Disk Devices.\r
+  // Parse removable media\r
   //\r
   gBS->LocateHandleBuffer (\r
         ByProtocol,\r
@@ -548,7 +823,7 @@ Returns:
                     (VOID **) &BlkIo\r
                     );\r
     if (!EFI_ERROR (Status)) {\r
-      if (BlkIo->Media->RemovableMedia) {\r
+      if (!BlkIo->Media->RemovableMedia) {\r
         //\r
         // If the file system handle supports a BlkIo protocol,\r
         // skip the removable media devices\r
@@ -557,14 +832,38 @@ Returns:
       }\r
     }\r
 \r
-    DevicePath = DevicePathFromHandle (FileSystemHandles[Index]);\r
-    if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && \r
-        (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
-        ) {\r
+    //\r
+    // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
+    //  machinename is ia32, ia64, x64, ...\r
+    //\r
+    FilePath = FileDevicePath (FileSystemHandles[Index], EFI_REMOVABLE_MEDIA_FILE_NAME);\r
+    NeedDelete = TRUE;\r
+    Status = gBS->LoadImage (\r
+                    TRUE,\r
+                    mBdsImageHandle,\r
+                    FilePath,\r
+                    NULL,\r
+                    0,\r
+                    &ImageHandle\r
+                    );\r
+    if (!EFI_ERROR(Status)) {\r
+      //\r
+      // Verify the image is a EFI application (and not a driver)\r
+      //\r
+      Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
+      ASSERT (!EFI_ERROR(Status));\r
+\r
+      if (ImageInfo->ImageCodeType == EfiLoaderCode) {\r
+        NeedDelete = FALSE;\r
+      }\r
+    }\r
+\r
+    if (NeedDelete) {\r
       //\r
-      // If the FileSystem protocol does not contain a BlkIo protocol,\r
-      // then build it\r
+      // No such file or the file is not a EFI application, delete this boot option\r
       //\r
+      BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
+    } else {\r
       BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList);\r
       BootOptionNumber++;\r
     }\r