]> code.delx.au - refind/blobdiff - EfiLib/legacy.c
Fixed uninitialized-pointer bug that manifested as a crash with
[refind] / EfiLib / legacy.c
index 7728f97a5002ac5b0d66d9404276a71b6303b04a..3e5edee4746903c1dae588ded1d3e22f19ca1e10 100644 (file)
  *
  */
 
+#ifdef __MAKEWITH_GNUEFI
+#include "efi.h"
+#include "efilib.h"
+#include "gnuefi-helper.h"
+#define EFI_DEVICE_PATH_PROTOCOL EFI_DEVICE_PATH
+#define EfiReallocatePool ReallocatePool
+#define EfiLibLocateProtocol LibLocateProtocol
+#else
 #include "../include/tiano_includes.h"
+#endif
 #include "legacy.h"
+#include "GenericBdsLib.h"
+#include "../refind/global.h"
+#include "../include/refit_call_wrapper.h"
 
 BOOT_OPTION_BBS_MAPPING  *mBootOptionBbsMapping     = NULL;
 UINTN                    mBootOptionBbsMappingCount = 0;
@@ -244,7 +256,7 @@ BdsFindLegacyBootOptionByDevTypeAndName (
   )
 {
   UINTN     Index;
-  CHAR16    BootOption[9];
+  CHAR16    BootOption[10];
   UINTN     BootOptionSize;
   UINT8     *BootOptionVar;
   BBS_TABLE *BbsEntry;
@@ -268,7 +280,7 @@ BdsFindLegacyBootOptionByDevTypeAndName (
                       &BootOptionSize
                       );
     if (NULL == BootOptionVar) {
-      continue;
+       continue;
     }
 
     //
@@ -448,7 +460,7 @@ BdsCreateLegacyBootOption (
   Ptr += sizeof (BBS_TABLE);
   *((UINT16 *) Ptr) = (UINT16) Index;
 
-  Status = gRT->SetVariable (
+  Status = refit_call5_wrapper(gRT->SetVariable,
                   BootString,
                   &gEfiGlobalVariableGuid,
                   VAR_FLAG,
@@ -646,7 +658,7 @@ EfiLibGetVariable (
 
   @param VarName           A Null-terminated Unicode string that is
                            the name of the vendor's variable.
-                         
+
   @param VarGuid           A unique identifier for the vendor.
 
   @retval  EFI_SUCCESS           The variable was found and removed
@@ -671,7 +683,7 @@ EfiLibDeleteVariable (
     //
     // Delete variable from Storage
     //
-    Status = gRT->SetVariable (VarName, VarGuid, VAR_FLAG, 0, NULL);
+    Status = refit_call5_wrapper(gRT->SetVariable, VarName, VarGuid, VAR_FLAG, 0, NULL);
     ASSERT (!EFI_ERROR (Status));
     FreePool (VarBuf);
   }
@@ -689,7 +701,6 @@ EfiLibDeleteVariable (
   @return Other value          LegacyBoot options are not added.
 **/
 EFI_STATUS
-EFIAPI
 BdsAddNonExistingLegacyBootOptions (
   VOID
   )
@@ -709,7 +720,6 @@ BdsAddNonExistingLegacyBootOptions (
   UINT16                    OptionNumber;
   BOOLEAN                   Exist;
 
-  Print(L"Entering BdsAddNonExistingLegacyBootOptions()\n");
   HddCount      = 0;
   BbsCount      = 0;
   LocalHddInfo  = NULL;
@@ -727,7 +737,7 @@ BdsAddNonExistingLegacyBootOptions (
     mBootOptionBbsMappingCount = 0;
   }
 
-  LegacyBios->GetBbsInfo (
+  refit_call5_wrapper(LegacyBios->GetBbsInfo,
                 LegacyBios,
                 &HddCount,
                 &LocalHddInfo,
@@ -762,7 +772,6 @@ BdsAddNonExistingLegacyBootOptions (
               &BbsIndex,
               &OptionNumber
               );
-    Print(L"Boot entry #%d %s exist.\n", Index, Exist ? L"does" : L"does not");
     if (!Exist) {
       //
       // Not found such type of legacy device in boot options or we found but it's disabled
@@ -785,10 +794,10 @@ BdsAddNonExistingLegacyBootOptions (
     //
     // Save the BbsIndex
     //
-    mBootOptionBbsMapping = ReallocatePool (
+    mBootOptionBbsMapping = EfiReallocatePool (
+                              mBootOptionBbsMapping,
                               mBootOptionBbsMappingCount * sizeof (BOOT_OPTION_BBS_MAPPING),
-                              (mBootOptionBbsMappingCount + 1) * sizeof (BOOT_OPTION_BBS_MAPPING),
-                              mBootOptionBbsMapping
+                              (mBootOptionBbsMappingCount + 1) * sizeof (BOOT_OPTION_BBS_MAPPING)
                               );
     ASSERT (mBootOptionBbsMapping != NULL);
     mBootOptionBbsMapping[mBootOptionBbsMappingCount].BootOptionNumber = OptionNumber;
@@ -806,7 +815,7 @@ BdsAddNonExistingLegacyBootOptions (
     );
 
   if (BootOrderSize > 0) {
-    Status = gRT->SetVariable (
+    Status = refit_call5_wrapper(gRT->SetVariable,
                     L"BootOrder",
                     &gEfiGlobalVariableGuid,
                     VAR_FLAG,
@@ -821,8 +830,6 @@ BdsAddNonExistingLegacyBootOptions (
     FreePool (BootOrder);
   }
 
-  Print(L"Exiting BdsAddNonExistingLegacyBootOptions()\n");
-
   return Status;
 }
 
@@ -839,7 +846,6 @@ BdsAddNonExistingLegacyBootOptions (
   @retval  EFI_NOT_FOUND         The Boot Option Variable was not found
 **/
 EFI_STATUS
-EFIAPI
 BdsDeleteBootOption (
   IN UINTN                       OptionNumber,
   IN OUT UINT16                  *BootOrder,
@@ -889,7 +895,6 @@ BdsDeleteBootOption (
   @retval EFI_NOT_FOUND           Fail to retrive variable of boot order.
 **/
 EFI_STATUS
-EFIAPI
 BdsDeleteAllInvalidLegacyBootOptions (
   VOID
   )
@@ -925,7 +930,7 @@ BdsDeleteAllInvalidLegacyBootOptions (
     return Status;
   }
 
-  LegacyBios->GetBbsInfo (
+  refit_call5_wrapper(LegacyBios->GetBbsInfo,
                 LegacyBios,
                 &HddCount,
                 &LocalHddInfo,
@@ -952,7 +957,7 @@ BdsDeleteAllInvalidLegacyBootOptions (
                       );
     if (NULL == BootOptionVar) {
       BootOptionSize = 0;
-      Status = gRT->GetVariable (
+      Status = refit_call5_wrapper(gRT->GetVariable,
                       BootOption,
                       &gEfiGlobalVariableGuid,
                       NULL,
@@ -963,7 +968,6 @@ BdsDeleteAllInvalidLegacyBootOptions (
         //
         // Update BootOrder
         //
-        Print(L"About to call BdsDeleteBootOption() on Index == %d\n", Index);
         BdsDeleteBootOption (
           BootOrder[Index],
           BootOrder,
@@ -978,7 +982,7 @@ BdsDeleteAllInvalidLegacyBootOptions (
 
     //
     // Skip Non-Legacy boot option
-    // 
+    //
     if (!BdsIsLegacyBootOption (BootOptionVar, &BbsEntry, &BbsIndex)) {
       if (BootOptionVar!= NULL) {
         FreePool (BootOptionVar);
@@ -1018,7 +1022,6 @@ BdsDeleteAllInvalidLegacyBootOptions (
     //
     // should delete
     //
-    Print(L"Second About to call BdsDeleteBootOption(), Index = %d\n", Index);
     BdsDeleteBootOption (
       BootOrder[Index],
       BootOrder,
@@ -1030,7 +1033,7 @@ BdsDeleteAllInvalidLegacyBootOptions (
   // Adjust the number of boot options.
   //
   if (BootOrderSize != 0) {
-    Status = gRT->SetVariable (
+    Status = refit_call5_wrapper(gRT->SetVariable,
                     L"BootOrder",
                     &gEfiGlobalVariableGuid,
                     VAR_FLAG,
@@ -1084,473 +1087,3 @@ BdsFillDevOrderBuf (
 
   return Buf;
 }
-
-/**
-  Create the device order buffer.
-
-  @param BbsTable        The BBS table.
-  @param BbsCount        The BBS Count.
-
-  @retval EFI_SUCCES             The buffer is created and the EFI variable named 
-                                 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
-                                 set correctly.
-  @retval EFI_OUT_OF_RESOURCES   Memmory or storage is not enough.
-  @retval EFI_DEVICE_ERROR       Fail to add the device order into EFI variable fail
-                                 because of hardware error.
-**/
-EFI_STATUS
-BdsCreateDevOrder (
-  IN BBS_TABLE                  *BbsTable,
-  IN UINT16                     BbsCount
-  )
-{
-  UINTN                       Index;
-  UINTN                       FDCount;
-  UINTN                       HDCount;
-  UINTN                       CDCount;
-  UINTN                       NETCount;
-  UINTN                       BEVCount;
-  UINTN                       TotalSize;
-  UINTN                       HeaderSize;
-  LEGACY_DEV_ORDER_ENTRY      *DevOrder;
-  LEGACY_DEV_ORDER_ENTRY      *DevOrderPtr;
-  EFI_STATUS                  Status;
-
-  FDCount     = 0;
-  HDCount     = 0;
-  CDCount     = 0;
-  NETCount    = 0;
-  BEVCount    = 0;
-  TotalSize   = 0;
-  HeaderSize  = sizeof (BBS_TYPE) + sizeof (UINT16);
-  DevOrder    = NULL;
-  Status      = EFI_SUCCESS;
-
-  //
-  // Count all boot devices
-  //
-  for (Index = 0; Index < BbsCount; Index++) {
-    if (BbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) {
-      continue;
-    }
-
-    switch (BbsTable[Index].DeviceType) {
-    case BBS_FLOPPY:
-      FDCount++;
-      break;
-
-    case BBS_HARDDISK:
-      HDCount++;
-      break;
-
-    case BBS_CDROM:
-      CDCount++;
-      break;
-
-    case BBS_EMBED_NETWORK:
-      NETCount++;
-      break;
-
-    case BBS_BEV_DEVICE:
-      BEVCount++;
-      break;
-
-    default:
-      break;
-    }
-  }
-
-  TotalSize += (HeaderSize + sizeof (UINT16) * FDCount);
-  TotalSize += (HeaderSize + sizeof (UINT16) * HDCount);
-  TotalSize += (HeaderSize + sizeof (UINT16) * CDCount);
-  TotalSize += (HeaderSize + sizeof (UINT16) * NETCount);
-  TotalSize += (HeaderSize + sizeof (UINT16) * BEVCount);
-
-  //
-  // Create buffer to hold all boot device order
-  //
-  DevOrder = AllocateZeroPool (TotalSize);
-  if (NULL == DevOrder) {
-    return EFI_OUT_OF_RESOURCES;
-  }
-  DevOrderPtr          = DevOrder;
-
-  DevOrderPtr->BbsType = BBS_FLOPPY;
-  DevOrderPtr->Length  = (UINT16) (sizeof (DevOrderPtr->Length) + FDCount * sizeof (UINT16));
-  DevOrderPtr          = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_FLOPPY, BbsCount, DevOrderPtr->Data);
-
-  DevOrderPtr->BbsType = BBS_HARDDISK;
-  DevOrderPtr->Length  = (UINT16) (sizeof (UINT16) + HDCount * sizeof (UINT16));
-  DevOrderPtr          = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_HARDDISK, BbsCount, DevOrderPtr->Data);
-  
-  DevOrderPtr->BbsType = BBS_CDROM;
-  DevOrderPtr->Length  = (UINT16) (sizeof (UINT16) + CDCount * sizeof (UINT16));
-  DevOrderPtr          = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_CDROM, BbsCount, DevOrderPtr->Data);
-  
-  DevOrderPtr->BbsType = BBS_EMBED_NETWORK;
-  DevOrderPtr->Length  = (UINT16) (sizeof (UINT16) + NETCount * sizeof (UINT16));
-  DevOrderPtr          = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_EMBED_NETWORK, BbsCount, DevOrderPtr->Data);
-
-  DevOrderPtr->BbsType = BBS_BEV_DEVICE;
-  DevOrderPtr->Length  = (UINT16) (sizeof (UINT16) + BEVCount * sizeof (UINT16));
-  DevOrderPtr          = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_BEV_DEVICE, BbsCount, DevOrderPtr->Data);
-
-  ASSERT (TotalSize == (UINTN) ((UINT8 *) DevOrderPtr - (UINT8 *) DevOrder));
-
-  //
-  // Save device order for legacy boot device to variable.
-  //
-  Status = gRT->SetVariable (
-                  VAR_LEGACY_DEV_ORDER,
-                  &gEfiLegacyDevOrderVariableGuid,
-                  VAR_FLAG,
-                  TotalSize,
-                  DevOrder
-                  );
-  FreePool (DevOrder);
-
-  return Status;
-}
-
-/**
-  Add the legacy boot devices from BBS table into 
-  the legacy device boot order.
-
-  @retval EFI_SUCCESS           The boot devices are added successfully.
-  @retval EFI_NOT_FOUND         The legacy boot devices are not found.
-  @retval EFI_OUT_OF_RESOURCES  Memmory or storage is not enough.
-  @retval EFI_DEVICE_ERROR      Fail to add the legacy device boot order into EFI variable
-                                because of hardware error.
-**/
-EFI_STATUS
-EFIAPI
-BdsUpdateLegacyDevOrder (
-  VOID
-  )
-{
-  LEGACY_DEV_ORDER_ENTRY      *DevOrder;
-  LEGACY_DEV_ORDER_ENTRY      *NewDevOrder;
-  LEGACY_DEV_ORDER_ENTRY      *Ptr;
-  LEGACY_DEV_ORDER_ENTRY      *NewPtr;
-  UINTN                       DevOrderSize;
-  EFI_LEGACY_BIOS_PROTOCOL    *LegacyBios;
-  EFI_STATUS                  Status;
-  UINT16                      HddCount;
-  UINT16                      BbsCount;
-  HDD_INFO                    *LocalHddInfo;
-  BBS_TABLE                   *LocalBbsTable;
-  INTN                        Index;
-  INTN                        Index2;
-  UINTN                       *Idx;
-  UINTN                       FDCount;
-  UINTN                       HDCount;
-  UINTN                       CDCount;
-  UINTN                       NETCount;
-  UINTN                       BEVCount;
-  UINTN                       TotalSize;
-  UINTN                       HeaderSize;
-  UINT16                      *NewFDPtr;
-  UINT16                      *NewHDPtr;
-  UINT16                      *NewCDPtr;
-  UINT16                      *NewNETPtr;
-  UINT16                      *NewBEVPtr;
-  UINT16                      *NewDevPtr;
-  UINTN                       FDIndex;
-  UINTN                       HDIndex;
-  UINTN                       CDIndex;
-  UINTN                       NETIndex;
-  UINTN                       BEVIndex;
-
-  Print(L"Entering BdsUpdateLegacyDevOrder()\n");
-
-  Idx           = NULL;
-  FDCount       = 0;
-  HDCount       = 0;
-  CDCount       = 0;
-  NETCount      = 0;
-  BEVCount      = 0;
-  TotalSize     = 0;
-  HeaderSize    = sizeof (BBS_TYPE) + sizeof (UINT16);
-  FDIndex       = 0;
-  HDIndex       = 0;
-  CDIndex       = 0;
-  NETIndex      = 0;
-  BEVIndex      = 0;
-  NewDevPtr     = NULL;
-
-  Print(L"About to call EfiLibLocateProtocol()\n");
-  Status        = EfiLibLocateProtocol (&gEfiLegacyBiosProtocolGuid, (VOID **) &LegacyBios);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  Print(L"About to call GetBbsInfo()\n");
-  Status = LegacyBios->GetBbsInfo (
-                         LegacyBios,
-                         &HddCount,
-                         &LocalHddInfo,
-                         &BbsCount,
-                         &LocalBbsTable
-                         );
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  Print(L"About to call BdsLibGetVariableAndSize()\n");
-  DevOrder = BdsLibGetVariableAndSize (
-               VAR_LEGACY_DEV_ORDER,
-               &gEfiLegacyDevOrderVariableGuid,
-               &DevOrderSize
-               );
-  if (NULL == DevOrder) {
-    Print(L"DevOrder was NULL\n");
-    return BdsCreateDevOrder (LocalBbsTable, BbsCount);
-  }
-  //
-  // First we figure out how many boot devices with same device type respectively
-  //
-  Print(L"About to enter for() loop, BbsCount = %d\n", BbsCount);
-  for (Index = 0; Index < BbsCount; Index++) {
-//    Print(L" Loop: %d\n", Index);
-    if ((LocalBbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) ||
-        (LocalBbsTable[Index].BootPriority == BBS_DO_NOT_BOOT_FROM)
-        ) {
-      continue;
-    }
-
-    switch (LocalBbsTable[Index].DeviceType) {
-    case BBS_FLOPPY:
-      FDCount++;
-      break;
-
-    case BBS_HARDDISK:
-      HDCount++;
-      break;
-
-    case BBS_CDROM:
-      CDCount++;
-      break;
-
-    case BBS_EMBED_NETWORK:
-      NETCount++;
-      break;
-
-    case BBS_BEV_DEVICE:
-      BEVCount++;
-      break;
-
-    default:
-      break;
-    }
-  }
-
-  Print(L"Done with for() loop; counted:\n Floppies: %d\n Hard disks: %d\n CD-ROMs: %d\n", FDCount, HDCount, CDCount);
-  TotalSize += (HeaderSize + FDCount * sizeof (UINT16));
-  TotalSize += (HeaderSize + HDCount * sizeof (UINT16));
-  TotalSize += (HeaderSize + CDCount * sizeof (UINT16));
-  TotalSize += (HeaderSize + NETCount * sizeof (UINT16));
-  TotalSize += (HeaderSize + BEVCount * sizeof (UINT16));
-
-  Print(L"About to allocate memory for NewDevOrder\n");
-  NewDevOrder = AllocateZeroPool (TotalSize);
-  if (NULL == NewDevOrder) {
-    return EFI_OUT_OF_RESOURCES;
-  }
-
-
-  Print(L"About to copy FD entries\n");
-  //
-  // copy FD
-  //
-  Ptr             = DevOrder;
-  NewPtr          = NewDevOrder;
-  NewPtr->BbsType = Ptr->BbsType;
-  NewPtr->Length  = (UINT16) (sizeof (UINT16) + FDCount * sizeof (UINT16));
-  Print(L" Copying FD entries; about to loop from 0 to %d\n", Ptr->Length / sizeof (UINT16) - 1);
-  for (Index = 0; Index < (INTN) (Ptr->Length / sizeof (UINT16) - 1); Index++) {
-//    Print(L"  Looping: Index = %d\n", Index);
-    if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_FLOPPY
-        ) {
-      continue;
-    }
-
-//    Print(L"  About to set data\n");
-    NewPtr->Data[FDIndex] = Ptr->Data[Index];
-    FDIndex++;
-  }
-  Print(L" Done with for() loop\n");
-  NewFDPtr = NewPtr->Data;
-
-  Print(L"About to copy HD entries\n");
-  //
-  // copy HD
-  //
-  Ptr             = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);
-  NewPtr          = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);
-  NewPtr->BbsType = Ptr->BbsType;
-  NewPtr->Length  = (sizeof (UINT16) + HDCount * sizeof (UINT16));
-  for (Index = 0; Index < (INTN) (Ptr->Length / sizeof (UINT16) - 1); Index++) {
-    if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_HARDDISK
-        ) {
-      continue;
-    }
-
-    NewPtr->Data[HDIndex] = Ptr->Data[Index];
-    HDIndex++;
-  }
-  NewHDPtr = NewPtr->Data;
-
-  Print(L"About to copy CD entries\n");
-  //
-  // copy CD
-  //
-  Ptr    = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);
-  NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);
-  NewPtr->BbsType = Ptr->BbsType;
-  NewPtr->Length  = (UINT16) (sizeof (UINT16) + CDCount * sizeof (UINT16));
-  for (Index = 0; Index < (INTN) (Ptr->Length / sizeof (UINT16) - 1); Index++) {
-    if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_CDROM
-        ) {
-      continue;
-    }
-
-    NewPtr->Data[CDIndex] = Ptr->Data[Index];
-    CDIndex++;
-  }
-  NewCDPtr = NewPtr->Data;
-
-  //
-  // copy NET
-  //
-  Ptr    = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);
-  NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);
-  NewPtr->BbsType = Ptr->BbsType;
-  NewPtr->Length  = (UINT16) (sizeof (UINT16) + NETCount * sizeof (UINT16));
-  for (Index = 0; Index < (INTN) (Ptr->Length / sizeof (UINT16) - 1); Index++) {
-    if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_EMBED_NETWORK
-        ) {
-      continue;
-    }
-
-    NewPtr->Data[NETIndex] = Ptr->Data[Index];
-    NETIndex++;
-  }
-  NewNETPtr = NewPtr->Data;
-  
-  //
-  // copy BEV
-  //
-  Ptr    = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);
-  NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);
-  NewPtr->BbsType = Ptr->BbsType;
-  NewPtr->Length  = (UINT16) (sizeof (UINT16) + BEVCount * sizeof (UINT16));
-  for (Index = 0; Index < (INTN) (Ptr->Length / sizeof (UINT16) - 1); Index++) {
-    if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||
-        LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_BEV_DEVICE
-        ) {
-      continue;
-    }
-
-    NewPtr->Data[BEVIndex] = Ptr->Data[Index];
-    BEVIndex++;
-  }
-  NewBEVPtr = NewPtr->Data;
-
-  Print(L"About to enter another loop; BbsCount = %d\n", BbsCount);
-  for (Index = 0; Index < BbsCount; Index++) {
-//    Print(L" Looping: Index = %d\n", Index);
-    if ((LocalBbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) ||
-        (LocalBbsTable[Index].BootPriority == BBS_DO_NOT_BOOT_FROM)
-        ) {
-      continue;
-    }
-
-    switch (LocalBbsTable[Index].DeviceType) {
-    case BBS_FLOPPY:
-      Idx       = &FDIndex;
-      NewDevPtr = NewFDPtr;
-      break;
-
-    case BBS_HARDDISK:
-      Idx       = &HDIndex;
-      NewDevPtr = NewHDPtr;
-      break;
-
-    case BBS_CDROM:
-      Idx       = &CDIndex;
-      NewDevPtr = NewCDPtr;
-      break;
-
-    case BBS_EMBED_NETWORK:
-      Idx       = &NETIndex;
-      NewDevPtr = NewNETPtr;
-      break;
-
-    case BBS_BEV_DEVICE:
-      Idx       = &BEVIndex;
-      NewDevPtr = NewBEVPtr;
-      break;
-
-    default:
-      Idx = NULL;
-      break;
-    }
-    //
-    // at this point we have copied those valid indexes to new buffer
-    // and we should check if there is any new appeared boot device
-    //
-    Print(L" Checking for new devices\n");
-    if (Idx != NULL) {
-      for (Index2 = 0; Index2 < *Idx; Index2++) {
-        if ((NewDevPtr[Index2] & 0xFF) == (UINT16) Index) {
-          break;
-        }
-      }
-
-      if (Index2 == *Idx) {
-        //
-        // Index2 == *Idx means we didn't find Index
-        // so Index is a new appeared device's index in BBS table
-        // insert it before disabled indexes.
-        //
-        for (Index2 = 0; Index2 < *Idx; Index2++) {
-          if ((NewDevPtr[Index2] & 0xFF00) == 0xFF00) {
-            break;
-          }
-        }
-        CopyMem (&NewDevPtr[Index2 + 1], &NewDevPtr[Index2], (*Idx - Index2) * sizeof (UINT16));
-        NewDevPtr[Index2] = (UINT16) (Index & 0xFF);
-        (*Idx)++;
-      }
-    }
-  }
-
-  FreePool (DevOrder);
-
-  Print(L"About to call SetVariable()\n");
-  Status = gRT->SetVariable (
-                  VAR_LEGACY_DEV_ORDER,
-                  &gEfiLegacyDevOrderVariableGuid,
-                  VAR_FLAG,
-                  TotalSize,
-                  NewDevOrder
-                  );
-  FreePool (NewDevOrder);
-
-  Print(L"Returning from BdsUpdateLegacyDevOrder()\n");
-  return Status;
-}
-