+/**\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
+ SecondEntry = NULL;\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
+ if ((SelectedEntry != NULL) && (SecondEntry != NULL)) {\r
+ SwapListEntries (SelectedEntry, SecondEntry);\r
+ }\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
+EFI_STATUS\r
+UpdateFdtPath (\r
+ IN LIST_ENTRY *BootOptionsList\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ BDS_SUPPORTED_DEVICE *SupportedBootDevice;\r
+ EFI_DEVICE_PATH_PROTOCOL *FdtDevicePathNodes;\r
+ EFI_DEVICE_PATH_PROTOCOL *FdtDevicePath;\r
+ CHAR16 *FdtTextDevicePath;\r
+ EFI_PHYSICAL_ADDRESS FdtBlobBase;\r
+ UINTN FdtBlobSize;\r
+ UINTN NumPages;\r
+ EFI_PHYSICAL_ADDRESS FdtConfigurationTableBase;\r
+\r
+ SupportedBootDevice = NULL;\r
+\r
+ Status = SelectBootDevice (&SupportedBootDevice);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_ABORTED;\r
+ goto EXIT;\r
+ }\r
+\r
+ // Create the specific device path node\r
+ Status = SupportedBootDevice->Support->CreateDevicePathNode (L"FDT blob", &FdtDevicePathNodes);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_ABORTED;\r
+ goto EXIT;\r
+ }\r
+\r
+ if (FdtDevicePathNodes != NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+\r
+ FdtDevicePath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, FdtDevicePathNodes);\r
+ FreePool (FdtDevicePathNodes);\r
+ if (FdtDevicePath == NULL) {\r
+ goto EXIT;\r
+ }\r
+\r
+ FdtTextDevicePath = ConvertDevicePathToText (FdtDevicePath, TRUE, TRUE);\r
+ if (FdtTextDevicePath == NULL) {\r
+ goto EXIT;\r
+ }\r
+\r
+ Status = gRT->SetVariable (\r
+ (CHAR16*)L"Fdt",\r
+ &gFdtVariableGuid,\r
+ EFI_VARIABLE_RUNTIME_ACCESS |\r
+ EFI_VARIABLE_NON_VOLATILE |\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+ StrSize (FdtTextDevicePath),\r
+ FdtTextDevicePath\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ FreePool (FdtTextDevicePath);\r
+ } else {\r
+ Status = gRT->SetVariable (\r
+ (CHAR16*)L"Fdt",\r
+ &gFdtVariableGuid,\r
+ EFI_VARIABLE_RUNTIME_ACCESS |\r
+ EFI_VARIABLE_NON_VOLATILE |\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+ 0,\r
+ NULL\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Try to load FDT from the new EFI Device Path\r
+ //\r
+\r
+ //\r
+ // Load the FDT given its device path.\r
+ // This operation may fail if the device path is not supported.\r
+ //\r
+ FdtBlobBase = 0;\r
+ NumPages = 0;\r
+ Status = BdsLoadImage (FdtDevicePath, AllocateAnyPages, &FdtBlobBase, &FdtBlobSize);\r
+ FreePool (FdtDevicePath);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto EXIT_LOAD_FDT;\r
+ }\r
+\r
+ // Check the FDT header is valid. We only make this check in DEBUG mode in\r
+ // case the FDT header change on production device and this ASSERT() becomes\r
+ // not valid.\r
+ ASSERT (fdt_check_header ((VOID*)(UINTN)FdtBlobBase) == 0);\r
+\r
+ //\r
+ // Ensure the Size of the Device Tree is smaller than the size of the read file\r
+ //\r
+ ASSERT ((UINTN)fdt_totalsize ((VOID*)(UINTN)FdtBlobBase) <= FdtBlobSize);\r
+\r
+ //\r
+ // Store the FDT as Runtime Service Data to prevent the Kernel from\r
+ // overwritting its data.\r
+ //\r
+ NumPages = EFI_SIZE_TO_PAGES (FdtBlobSize);\r
+ Status = gBS->AllocatePages (\r
+ AllocateAnyPages, EfiRuntimeServicesData,\r
+ NumPages, &FdtConfigurationTableBase\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto EXIT_LOAD_FDT;\r
+ }\r
+ gBS->CopyMem (\r
+ (VOID*)(UINTN)FdtConfigurationTableBase,\r
+ (VOID*)(UINTN)FdtBlobBase,\r
+ FdtBlobSize\r
+ );\r
+\r
+ //\r
+ // Install the FDT into the Configuration Table\r
+ //\r
+ Status = gBS->InstallConfigurationTable (\r
+ &gFdtTableGuid,\r
+ (VOID*)(UINTN)FdtConfigurationTableBase\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ gBS->FreePages (FdtConfigurationTableBase, NumPages);\r
+ }\r
+\r
+EXIT_LOAD_FDT:\r
+ if (EFI_ERROR (Status)) {\r
+ Print (L"\nWarning: Did not manage to install the new device tree. Try to restart the platform.\n");\r
+ }\r
+\r
+ if (FdtBlobBase != 0) {\r
+ gBS->FreePages (FdtBlobBase, NumPages);\r
+ }\r
+\r
+EXIT:\r
+ if (Status == EFI_ABORTED) {\r
+ Print (L"\n");\r
+ }\r
+\r
+ if (SupportedBootDevice != NULL) {\r
+ FreePool (SupportedBootDevice);\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Set boot timeout\r
+\r
+ Ask for the boot timeout in seconds and if the input succeeds assign the\r
+ input value to the UEFI global variable "Timeout". This function is called\r
+ when the user selects the "Set Boot Timeout" of the boot manager menu.\r
+\r
+ @param[in] BootOptionsList List of the boot devices, not used here\r
+\r
+ @retval EFI_SUCCESS Boot timeout in second retrieved from the standard\r
+ input and assigned to the UEFI "Timeout" global\r
+ variable\r
+ @retval !EFI_SUCCESS Either the input or the setting of the UEFI global\r
+ variable "Timeout" has failed.\r
+**/\r
+EFI_STATUS\r
+STATIC\r
+BootMenuSetBootTimeout (\r
+ IN LIST_ENTRY *BootOptionsList\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN Input;\r
+ UINT16 Timeout;\r
+\r
+ Print (L"Timeout duration (in seconds): ");\r
+ Status = GetHIInputInteger (&Input);\r
+ if (EFI_ERROR (Status)) {\r
+ Print (L"\n");\r
+ goto ErrorExit;\r
+ }\r
+\r
+ Timeout = Input;\r
+ Status = gRT->SetVariable (\r
+ (CHAR16*)L"Timeout",\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_NON_VOLATILE |\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
+ EFI_VARIABLE_RUNTIME_ACCESS,\r
+ sizeof (UINT16),\r
+ &Timeout\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ErrorExit:\r
+ return Status;\r
+}\r
+\r