+/**\r
+ Reorder boot options\r
+\r
+ Ask for the boot option to move and then move it when up or down arrows\r
+ are pressed. This function is called when the user selects the "Reorder Boot\r
+ Device Entries" entry in the boot manager menu.\r
+ The order of the boot options in BootOptionList and in the UEFI BootOrder\r
+ global variable are kept coherent until the user confirm his reordering (ie:\r
+ he does not exit by pressing escape).\r
+\r
+ @param[in] BootOptionsList List of the boot devices constructed in\r
+ BootMenuMain()\r
+\r
+ @retval EFI_SUCCESS No error encountered.\r
+ @retval !EFI_SUCCESS An error has occured either in the selection of the\r
+ boot option to move or while interacting with the user.\r
+\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+BootMenuReorderBootOptions (\r
+ IN LIST_ENTRY *BootOptionsList\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ BDS_LOAD_OPTION_ENTRY *BootOptionEntry;\r
+ LIST_ENTRY *SelectedEntry;\r
+ LIST_ENTRY *PrevEntry;\r
+ BOOLEAN Move;\r
+ BOOLEAN Save;\r
+ BOOLEAN Cancel;\r
+ UINTN WaitIndex;\r
+ EFI_INPUT_KEY Key;\r
+ LIST_ENTRY *SecondEntry;\r
+ UINTN BootOrderSize;\r
+ UINT16 *BootOrder;\r
+ LIST_ENTRY *Entry;\r
+ UINTN Index;\r
+\r
+ DisplayBootOptions (BootOptionsList);\r
+\r
+ // Ask to select the boot option to move\r
+ while (TRUE) {\r
+ Status = SelectBootOption (BootOptionsList, MOVE_BOOT_ENTRY, &BootOptionEntry);\r
+ if (EFI_ERROR (Status)) {\r
+ goto ErrorExit;\r
+ }\r
+\r
+ SelectedEntry = &BootOptionEntry->Link;\r
+ // Note down the previous entry in the list to be able to cancel changes\r
+ PrevEntry = GetPreviousNode (BootOptionsList, SelectedEntry);\r
+\r
+ // Start of interaction\r
+ while (TRUE) {\r
+ Print (\r
+ L"* Use up/down arrows to move the entry '%s'",\r
+ BootOptionEntry->BdsLoadOption->Description\r
+ );\r
+\r
+ // Wait for a move, save or cancel request\r
+ Move = FALSE;\r
+ Save = FALSE;\r
+ Cancel = FALSE;\r
+ do {\r
+ Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &WaitIndex);\r
+ if (!EFI_ERROR (Status)) {\r
+ Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
+ }\r
+ if (EFI_ERROR (Status)) {\r
+ Print (L"\n");\r
+ goto ErrorExit;\r
+ }\r
+\r
+ switch (Key.ScanCode) {\r
+ case SCAN_NULL:\r
+ Save = (Key.UnicodeChar == CHAR_LINEFEED) ||\r
+ (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) ||\r
+ (Key.UnicodeChar == 0x7f);\r
+ break;\r
+\r
+ case SCAN_UP:\r
+ SecondEntry = GetPreviousNode (BootOptionsList, SelectedEntry);\r
+ Move = SecondEntry != BootOptionsList;\r
+ break;\r
+\r
+ case SCAN_DOWN:\r
+ SecondEntry = GetNextNode (BootOptionsList, SelectedEntry);\r
+ Move = SecondEntry != BootOptionsList;\r
+ break;\r
+\r
+ case SCAN_ESC:\r
+ Cancel = TRUE;\r
+ break;\r
+ }\r
+ } while ((!Move) && (!Save) && (!Cancel));\r
+\r
+ if (Move) {\r
+ SwapListEntries (SelectedEntry, SecondEntry);\r
+ } else {\r
+ if (Save) {\r
+ Status = GetGlobalEnvironmentVariable (\r
+ L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder\r
+ );\r
+ BootOrderSize /= sizeof (UINT16);\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ // The order of the boot options in the 'BootOptionsList' is the\r
+ // new order that has been just defined by the user. Save this new\r
+ // order in "BootOrder" UEFI global variable.\r
+ Entry = GetFirstNode (BootOptionsList);\r
+ for (Index = 0; Index < BootOrderSize; Index++) {\r
+ BootOrder[Index] = (LOAD_OPTION_FROM_LINK (Entry))->LoadOptionIndex;\r
+ Entry = GetNextNode (BootOptionsList, Entry);\r
+ }\r
+ Status = gRT->SetVariable (\r
+ (CHAR16*)L"BootOrder",\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_NON_VOLATILE |\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
+ EFI_VARIABLE_RUNTIME_ACCESS,\r
+ BootOrderSize * sizeof (UINT16),\r
+ BootOrder\r
+ );\r
+ FreePool (BootOrder);\r
+ }\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ Print (L"\nAn error occurred, move not completed!\n");\r
+ Cancel = TRUE;\r
+ }\r
+ }\r
+\r
+ if (Cancel) {\r
+ //\r
+ // Restore initial position of the selected boot option\r
+ //\r
+ RemoveEntryList (SelectedEntry);\r
+ InsertHeadList (PrevEntry, SelectedEntry);\r
+ }\r
+ }\r
+\r
+ Print (L"\n");\r
+ DisplayBootOptions (BootOptionsList);\r
+ // Saved or cancelled, back to the choice of boot option to move\r
+ if (!Move) {\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ErrorExit:\r
+ return Status ;\r
+}\r
+\r