]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/Bds/BootMenu.c
ArmPlatformPkg/Bds: Decoupled OptionalData for the EFI application boot entry from...
[mirror_edk2.git] / ArmPlatformPkg / Bds / BootMenu.c
CommitLineData
ea46ebbe 1/** @file\r
2*\r
aeaf64d6 3* Copyright (c) 2011-2013, ARM Limited. All rights reserved.\r
ea46ebbe 4*\r
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12*\r
13**/\r
14\r
15#include "BdsInternal.h"\r
16\r
aeaf64d6 17#include <Guid/ArmGlobalVariableHob.h>\r
18\r
ea46ebbe 19extern EFI_HANDLE mImageHandle;\r
20extern BDS_LOAD_OPTION_SUPPORT *BdsLoadOptionSupportList;\r
21\r
aeaf64d6 22\r
ea46ebbe 23EFI_STATUS\r
656416bc 24SelectBootDevice (\r
25 OUT BDS_SUPPORTED_DEVICE** SupportedBootDevice\r
ea46ebbe 26 )\r
27{\r
656416bc 28 EFI_STATUS Status;\r
ea46ebbe 29 LIST_ENTRY SupportedDeviceList;\r
30 UINTN SupportedDeviceCount;\r
ea46ebbe 31 LIST_ENTRY* Entry;\r
32 UINTN SupportedDeviceSelected;\r
ea46ebbe 33 UINTN Index;\r
ea46ebbe 34\r
35 //\r
36 // List the Boot Devices supported\r
37 //\r
38\r
39 // Start all the drivers first\r
40 BdsConnectAllDrivers ();\r
41\r
42 // List the supported devices\r
43 Status = BootDeviceListSupportedInit (&SupportedDeviceList);\r
44 ASSERT_EFI_ERROR(Status);\r
45\r
46 SupportedDeviceCount = 0;\r
47 for (Entry = GetFirstNode (&SupportedDeviceList);\r
48 !IsNull (&SupportedDeviceList,Entry);\r
49 Entry = GetNextNode (&SupportedDeviceList,Entry)\r
50 )\r
51 {\r
656416bc 52 *SupportedBootDevice = SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry);\r
53 Print(L"[%d] %s\n",SupportedDeviceCount+1,(*SupportedBootDevice)->Description);\r
ea46ebbe 54\r
55 DEBUG_CODE_BEGIN();\r
56 CHAR16* DevicePathTxt;\r
57 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\r
58\r
ff7666c5 59 Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
ea46ebbe 60 ASSERT_EFI_ERROR(Status);\r
ff7666c5 61 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText ((*SupportedBootDevice)->DevicePathProtocol,TRUE,TRUE);\r
ea46ebbe 62\r
63 Print(L"\t- %s\n",DevicePathTxt);\r
64\r
65 FreePool(DevicePathTxt);\r
66 DEBUG_CODE_END();\r
67\r
68 SupportedDeviceCount++;\r
69 }\r
70\r
71 if (SupportedDeviceCount == 0) {\r
72 Print(L"There is no supported device.\n");\r
73 Status = EFI_ABORTED;\r
74 goto EXIT;\r
75 }\r
76\r
77 //\r
78 // Select the Boot Device\r
79 //\r
80 SupportedDeviceSelected = 0;\r
81 while (SupportedDeviceSelected == 0) {\r
82 Print(L"Select the Boot Device: ");\r
83 Status = GetHIInputInteger (&SupportedDeviceSelected);\r
84 if (EFI_ERROR(Status)) {\r
85 Status = EFI_ABORTED;\r
86 goto EXIT;\r
87 } else if ((SupportedDeviceSelected == 0) || (SupportedDeviceSelected > SupportedDeviceCount)) {\r
ff7666c5 88 Print(L"Invalid input (max %d)\n",SupportedDeviceCount);\r
ea46ebbe 89 SupportedDeviceSelected = 0;\r
90 }\r
91 }\r
92\r
93 //\r
94 // Get the Device Path for the selected boot device\r
95 //\r
96 Index = 1;\r
97 for (Entry = GetFirstNode (&SupportedDeviceList);\r
98 !IsNull (&SupportedDeviceList,Entry);\r
99 Entry = GetNextNode (&SupportedDeviceList,Entry)\r
100 )\r
101 {\r
102 if (Index == SupportedDeviceSelected) {\r
656416bc 103 *SupportedBootDevice = SUPPORTED_BOOT_DEVICE_FROM_LINK(Entry);\r
ea46ebbe 104 break;\r
105 }\r
106 Index++;\r
107 }\r
aeaf64d6 108\r
656416bc 109EXIT:\r
110 BootDeviceListSupportedFree (&SupportedDeviceList, *SupportedBootDevice);\r
111 return Status;\r
112}\r
113\r
114EFI_STATUS\r
115BootMenuAddBootOption (\r
116 IN LIST_ENTRY *BootOptionsList\r
117 )\r
118{\r
2ccfb71e 119 EFI_STATUS Status;\r
120 BDS_SUPPORTED_DEVICE* SupportedBootDevice;\r
121 ARM_BDS_LOADER_ARGUMENTS* BootArguments;\r
74b96132 122 CHAR16 BootDescription[BOOT_DEVICE_DESCRIPTION_MAX];\r
2ccfb71e 123 CHAR8 CmdLine[BOOT_DEVICE_OPTION_MAX];\r
124 UINT32 Attributes;\r
125 ARM_BDS_LOADER_TYPE BootType;\r
a6e97d28 126 BDS_LOAD_OPTION_ENTRY *BdsLoadOptionEntry;\r
2ccfb71e 127 EFI_DEVICE_PATH *DevicePath;\r
ecc62d13 128 EFI_DEVICE_PATH_PROTOCOL *DevicePathNodes;\r
129 EFI_DEVICE_PATH_PROTOCOL *InitrdPathNodes;\r
2ccfb71e 130 EFI_DEVICE_PATH_PROTOCOL *InitrdPath;\r
131 UINTN CmdLineSize;\r
22a262c8 132 BOOLEAN InitrdSupport;\r
2ccfb71e 133 UINTN InitrdSize;\r
90a44ec4
OM
134 UINT8* OptionalData;\r
135 UINTN OptionalDataSize;\r
656416bc 136\r
137 Attributes = 0;\r
138 SupportedBootDevice = NULL;\r
139\r
140 // List the Boot Devices supported\r
2ccfb71e 141 Status = SelectBootDevice (&SupportedBootDevice);\r
656416bc 142 if (EFI_ERROR(Status)) {\r
143 Status = EFI_ABORTED;\r
144 goto EXIT;\r
145 }\r
ea46ebbe 146\r
147 // Create the specific device path node\r
ecc62d13 148 Status = SupportedBootDevice->Support->CreateDevicePathNode (L"EFI Application or the kernel", &DevicePathNodes, &BootType, &Attributes);\r
ea46ebbe 149 if (EFI_ERROR(Status)) {\r
150 Status = EFI_ABORTED;\r
151 goto EXIT;\r
152 }\r
ecc62d13 153 // Append the Device Path to the selected device path\r
154 DevicePath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, (CONST EFI_DEVICE_PATH_PROTOCOL *)DevicePathNodes);\r
155 if (DevicePath == NULL) {\r
156 Status = EFI_OUT_OF_RESOURCES;\r
157 goto EXIT;\r
158 }\r
ea46ebbe 159\r
2ccfb71e 160 if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
22a262c8 161 Print(L"Add an initrd: ");\r
162 Status = GetHIInputBoolean (&InitrdSupport);\r
163 if (EFI_ERROR(Status)) {\r
656416bc 164 Status = EFI_ABORTED;\r
165 goto EXIT;\r
166 }\r
167\r
22a262c8 168 if (InitrdSupport) {\r
169 // Create the specific device path node\r
ecc62d13 170 Status = SupportedBootDevice->Support->CreateDevicePathNode (L"initrd", &InitrdPathNodes, NULL, NULL);\r
22a262c8 171 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) { // EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd\r
172 Status = EFI_ABORTED;\r
173 goto EXIT;\r
174 }\r
175\r
ecc62d13 176 if (InitrdPathNodes != NULL) {\r
177 // Append the Device Path to the selected device path\r
178 InitrdPath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNodes);\r
179 if (InitrdPath == NULL) {\r
180 Status = EFI_OUT_OF_RESOURCES;\r
181 goto EXIT;\r
182 }\r
22a262c8 183 } else {\r
184 InitrdPath = NULL;\r
185 }\r
656416bc 186 } else {\r
2ccfb71e 187 InitrdPath = NULL;\r
656416bc 188 }\r
189\r
190 Print(L"Arguments to pass to the binary: ");\r
2ccfb71e 191 Status = GetHIInputAscii (CmdLine,BOOT_DEVICE_OPTION_MAX);\r
656416bc 192 if (EFI_ERROR(Status)) {\r
193 Status = EFI_ABORTED;\r
194 goto FREE_DEVICE_PATH;\r
195 }\r
2ccfb71e 196\r
197 CmdLineSize = AsciiStrSize (CmdLine);\r
198 InitrdSize = GetDevicePathSize (InitrdPath);\r
199\r
90a44ec4
OM
200 OptionalDataSize = sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize;\r
201 BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (OptionalDataSize);\r
aeaf64d6 202\r
2ccfb71e 203 BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;\r
204 BootArguments->LinuxArguments.InitrdSize = InitrdSize;\r
205 CopyMem ((VOID*)(&BootArguments->LinuxArguments + 1), CmdLine, CmdLineSize);\r
206 CopyMem ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);\r
90a44ec4
OM
207\r
208 OptionalData = (UINT8*)BootArguments;\r
2ccfb71e 209 } else {\r
90a44ec4
OM
210 OptionalData = NULL;\r
211 OptionalDataSize = 0;\r
ea46ebbe 212 }\r
213\r
214 Print(L"Description for this new Entry: ");\r
74b96132 215 Status = GetHIInputStr (BootDescription, BOOT_DEVICE_DESCRIPTION_MAX);\r
ea46ebbe 216 if (EFI_ERROR(Status)) {\r
217 Status = EFI_ABORTED;\r
218 goto FREE_DEVICE_PATH;\r
219 }\r
220\r
ea46ebbe 221 // Create new entry\r
a6e97d28 222 BdsLoadOptionEntry = (BDS_LOAD_OPTION_ENTRY*)AllocatePool (sizeof(BDS_LOAD_OPTION_ENTRY));\r
90a44ec4 223 Status = BootOptionCreate (Attributes, BootDescription, DevicePath, BootType, OptionalData, OptionalDataSize, &BdsLoadOptionEntry->BdsLoadOption);\r
ea46ebbe 224 if (!EFI_ERROR(Status)) {\r
a6e97d28 225 InsertTailList (BootOptionsList, &BdsLoadOptionEntry->Link);\r
ea46ebbe 226 }\r
227\r
ea46ebbe 228FREE_DEVICE_PATH:\r
229 FreePool (DevicePath);\r
230\r
231EXIT:\r
656416bc 232 if (Status == EFI_ABORTED) {\r
233 Print(L"\n");\r
234 }\r
235 FreePool(SupportedBootDevice);\r
ea46ebbe 236 return Status;\r
237}\r
238\r
239STATIC\r
240EFI_STATUS\r
241BootMenuSelectBootOption (\r
a6e97d28 242 IN LIST_ENTRY* BootOptionsList,\r
243 IN CONST CHAR16* InputStatement,\r
244 IN BOOLEAN OnlyArmBdsBootEntry,\r
245 OUT BDS_LOAD_OPTION_ENTRY** BdsLoadOptionEntry\r
ea46ebbe 246 )\r
247{\r
a6e97d28 248 EFI_STATUS Status;\r
249 LIST_ENTRY* Entry;\r
250 BDS_LOAD_OPTION* BdsLoadOption;\r
251 UINTN BootOptionSelected;\r
252 UINTN BootOptionCount;\r
253 UINTN Index;\r
ea46ebbe 254\r
255 // Display the list of supported boot devices\r
c93ab96c 256 BootOptionCount = 0;\r
ea46ebbe 257 for (Entry = GetFirstNode (BootOptionsList);\r
258 !IsNull (BootOptionsList,Entry);\r
a6e97d28 259 Entry = GetNextNode (BootOptionsList, Entry)\r
ea46ebbe 260 )\r
261 {\r
a6e97d28 262 BdsLoadOption = LOAD_OPTION_FROM_LINK(Entry);\r
263\r
264 if (OnlyArmBdsBootEntry && !IS_ARM_BDS_BOOTENTRY (BdsLoadOption)) {\r
265 continue;\r
266 }\r
267\r
c93ab96c 268 Print (L"[%d] %s\n", (BootOptionCount + 1), BdsLoadOption->Description);\r
ea46ebbe 269\r
270 DEBUG_CODE_BEGIN();\r
271 CHAR16* DevicePathTxt;\r
272 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\r
2ccfb71e 273 ARM_BDS_LOADER_TYPE LoaderType;\r
a6e97d28 274 ARM_BDS_LOADER_OPTIONAL_DATA* OptionalData;\r
ea46ebbe 275\r
276 Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
277 ASSERT_EFI_ERROR(Status);\r
a6e97d28 278 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText(BdsLoadOption->FilePathList,TRUE,TRUE);\r
ea46ebbe 279\r
280 Print(L"\t- %s\n",DevicePathTxt);\r
2ccfb71e 281 OptionalData = BdsLoadOption->OptionalData;\r
282 LoaderType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((CONST UINT32*)&OptionalData->Header.LoaderType);\r
283 if ((LoaderType == BDS_LOADER_KERNEL_LINUX_ATAG) || (LoaderType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
284 Print (L"\t- Arguments: %a\n",&OptionalData->Arguments.LinuxArguments + 1);\r
ea46ebbe 285 }\r
286\r
287 FreePool(DevicePathTxt);\r
288 DEBUG_CODE_END();\r
289\r
290 BootOptionCount++;\r
291 }\r
292\r
c93ab96c 293 // Check if a valid boot option(s) is found\r
a6e97d28 294 if (BootOptionCount == 0) {\r
c93ab96c 295 if (StrCmp (InputStatement, DELETE_BOOT_ENTRY) == 0) {\r
296 Print (L"Nothing to remove!\n");\r
ecc62d13 297 } else if (StrCmp (InputStatement, UPDATE_BOOT_ENTRY) == 0) {\r
c93ab96c 298 Print (L"Couldn't find valid boot entries\n");\r
299 } else{\r
300 Print (L"No supported Boot Entry.\n");\r
301 }\r
302\r
a6e97d28 303 return EFI_NOT_FOUND;\r
304 }\r
305\r
ea46ebbe 306 // Get the index of the boot device to delete\r
307 BootOptionSelected = 0;\r
308 while (BootOptionSelected == 0) {\r
309 Print(InputStatement);\r
310 Status = GetHIInputInteger (&BootOptionSelected);\r
311 if (EFI_ERROR(Status)) {\r
312 return Status;\r
c93ab96c 313 } else if ((BootOptionSelected == 0) || (BootOptionSelected > BootOptionCount)) {\r
314 Print(L"Invalid input (max %d)\n",BootOptionCount);\r
ea46ebbe 315 BootOptionSelected = 0;\r
316 }\r
317 }\r
318\r
319 // Get the structure of the Boot device to delete\r
320 Index = 1;\r
321 for (Entry = GetFirstNode (BootOptionsList);\r
ff7666c5 322 !IsNull (BootOptionsList, Entry);\r
ea46ebbe 323 Entry = GetNextNode (BootOptionsList,Entry)\r
324 )\r
325 {\r
326 if (Index == BootOptionSelected) {\r
a6e97d28 327 *BdsLoadOptionEntry = LOAD_OPTION_ENTRY_FROM_LINK(Entry);\r
ea46ebbe 328 break;\r
329 }\r
330 Index++;\r
331 }\r
332\r
333 return EFI_SUCCESS;\r
334}\r
335\r
336EFI_STATUS\r
337BootMenuRemoveBootOption (\r
338 IN LIST_ENTRY *BootOptionsList\r
339 )\r
340{\r
a6e97d28 341 EFI_STATUS Status;\r
342 BDS_LOAD_OPTION_ENTRY* BootOptionEntry;\r
ea46ebbe 343\r
11c20f4e 344 Status = BootMenuSelectBootOption (BootOptionsList, DELETE_BOOT_ENTRY, FALSE, &BootOptionEntry);\r
ea46ebbe 345 if (EFI_ERROR(Status)) {\r
346 return Status;\r
347 }\r
348\r
a6e97d28 349 // If the Boot Option was attached to a list remove it\r
350 if (!IsListEmpty (&BootOptionEntry->Link)) {\r
351 // Remove the entry from the list\r
352 RemoveEntryList (&BootOptionEntry->Link);\r
353 }\r
354\r
ea46ebbe 355 // Delete the BDS Load option structures\r
a6e97d28 356 BootOptionDelete (BootOptionEntry->BdsLoadOption);\r
ea46ebbe 357\r
358 return EFI_SUCCESS;\r
359}\r
360\r
361EFI_STATUS\r
362BootMenuUpdateBootOption (\r
363 IN LIST_ENTRY *BootOptionsList\r
364 )\r
365{\r
2ccfb71e 366 EFI_STATUS Status;\r
a6e97d28 367 BDS_LOAD_OPTION_ENTRY *BootOptionEntry;\r
2ccfb71e 368 BDS_LOAD_OPTION *BootOption;\r
369 BDS_LOAD_OPTION_SUPPORT* DeviceSupport;\r
370 ARM_BDS_LOADER_ARGUMENTS* BootArguments;\r
74b96132 371 CHAR16 BootDescription[BOOT_DEVICE_DESCRIPTION_MAX];\r
2ccfb71e 372 CHAR8 CmdLine[BOOT_DEVICE_OPTION_MAX];\r
14238a61 373 EFI_DEVICE_PATH *DevicePath;\r
374 EFI_DEVICE_PATH *TempInitrdPath;\r
2ccfb71e 375 ARM_BDS_LOADER_TYPE BootType;\r
90a44ec4 376 ARM_BDS_LOADER_OPTIONAL_DATA* LoaderOptionalData;\r
2ccfb71e 377 ARM_BDS_LINUX_ARGUMENTS* LinuxArguments;\r
ecc62d13 378 EFI_DEVICE_PATH *InitrdPathNodes;\r
22a262c8 379 EFI_DEVICE_PATH *InitrdPath;\r
2ccfb71e 380 UINTN InitrdSize;\r
381 UINTN CmdLineSize;\r
22a262c8 382 BOOLEAN InitrdSupport;\r
90a44ec4
OM
383 UINT8* OptionalData;\r
384 UINTN OptionalDataSize;\r
ea46ebbe 385\r
11c20f4e 386 Status = BootMenuSelectBootOption (BootOptionsList, UPDATE_BOOT_ENTRY, TRUE, &BootOptionEntry);\r
ea46ebbe 387 if (EFI_ERROR(Status)) {\r
388 return Status;\r
389 }\r
a6e97d28 390 BootOption = BootOptionEntry->BdsLoadOption;\r
ea46ebbe 391\r
392 // Get the device support for this Boot Option\r
22a262c8 393 Status = BootDeviceGetDeviceSupport (BootOption->FilePathList, &DeviceSupport);\r
ea46ebbe 394 if (EFI_ERROR(Status)) {\r
ff7666c5 395 Print(L"Not possible to retrieve the supported device for the update\n");\r
ea46ebbe 396 return EFI_UNSUPPORTED;\r
397 }\r
398\r
22a262c8 399 Status = DeviceSupport->UpdateDevicePathNode (BootOption->FilePathList, L"EFI Application or the kernel", &DevicePath, NULL, NULL);\r
ea46ebbe 400 if (EFI_ERROR(Status)) {\r
401 Status = EFI_ABORTED;\r
402 goto EXIT;\r
403 }\r
404\r
90a44ec4
OM
405 LoaderOptionalData = BootOption->OptionalData;\r
406 BootType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((UINT32 *)(&LoaderOptionalData->Header.LoaderType));\r
656416bc 407\r
2ccfb71e 408 if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
90a44ec4 409 LinuxArguments = &LoaderOptionalData->Arguments.LinuxArguments;\r
2ccfb71e 410\r
411 CmdLineSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->CmdLineSize);\r
2ccfb71e 412\r
3e710183 413 InitrdSize = ReadUnaligned16 ((CONST UINT16*)&LinuxArguments->InitrdSize);\r
414 if (InitrdSize > 0) {\r
22a262c8 415 Print(L"Keep the initrd: ");\r
416 } else {\r
417 Print(L"Add an initrd: ");\r
418 }\r
419 Status = GetHIInputBoolean (&InitrdSupport);\r
420 if (EFI_ERROR(Status)) {\r
421 Status = EFI_ABORTED;\r
422 goto EXIT;\r
423 }\r
424\r
425 if (InitrdSupport) {\r
426 if (InitrdSize > 0) {\r
427 // Case we update the initrd device path\r
49a25d84 428 Status = DeviceSupport->UpdateDevicePathNode ((EFI_DEVICE_PATH*)((UINTN)(LinuxArguments + 1) + CmdLineSize), L"initrd", &InitrdPath, NULL, NULL);\r
22a262c8 429 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) {// EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd\r
430 Status = EFI_ABORTED;\r
431 goto EXIT;\r
432 }\r
433 InitrdSize = GetDevicePathSize (InitrdPath);\r
434 } else {\r
435 // Case we create the initrd device path\r
436\r
ecc62d13 437 Status = DeviceSupport->CreateDevicePathNode (L"initrd", &InitrdPathNodes, NULL, NULL);\r
22a262c8 438 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) { // EFI_NOT_FOUND is returned on empty input string, but we can boot without an initrd\r
439 Status = EFI_ABORTED;\r
440 goto EXIT;\r
441 }\r
442\r
ecc62d13 443 if (InitrdPathNodes != NULL) {\r
22a262c8 444 // Duplicate Linux kernel Device Path\r
14238a61 445 TempInitrdPath = DuplicateDevicePath (BootOption->FilePathList);\r
22a262c8 446 // Replace Linux kernel Node by EndNode\r
14238a61 447 SetDevicePathEndNode (GetLastDevicePathNode (TempInitrdPath));\r
ecc62d13 448 // Append the Device Path to the selected device path\r
449 InitrdPath = AppendDevicePath (TempInitrdPath, (CONST EFI_DEVICE_PATH_PROTOCOL *)InitrdPathNodes);\r
14238a61 450 FreePool (TempInitrdPath);\r
ecc62d13 451 if (InitrdPath == NULL) {\r
452 Status = EFI_OUT_OF_RESOURCES;\r
453 goto EXIT;\r
454 }\r
14238a61 455 InitrdSize = GetDevicePathSize (InitrdPath);\r
22a262c8 456 } else {\r
457 InitrdPath = NULL;\r
458 }\r
3e710183 459 }\r
22a262c8 460 } else {\r
461 InitrdSize = 0;\r
656416bc 462 }\r
463\r
aeaf64d6 464 Print(L"Arguments to pass to the binary: ");\r
2ccfb71e 465 if (CmdLineSize > 0) {\r
466 AsciiStrnCpy(CmdLine, (CONST CHAR8*)(LinuxArguments + 1), CmdLineSize);\r
656416bc 467 } else {\r
2ccfb71e 468 CmdLine[0] = '\0';\r
656416bc 469 }\r
2ccfb71e 470 Status = EditHIInputAscii (CmdLine, BOOT_DEVICE_OPTION_MAX);\r
656416bc 471 if (EFI_ERROR(Status)) {\r
472 Status = EFI_ABORTED;\r
473 goto FREE_DEVICE_PATH;\r
474 }\r
2ccfb71e 475\r
476 CmdLineSize = AsciiStrSize (CmdLine);\r
2ccfb71e 477\r
90a44ec4
OM
478 OptionalDataSize = sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize;\r
479 BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (OptionalDataSize);\r
2ccfb71e 480 BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;\r
481 BootArguments->LinuxArguments.InitrdSize = InitrdSize;\r
482 CopyMem (&BootArguments->LinuxArguments + 1, CmdLine, CmdLineSize);\r
49a25d84 483 CopyMem ((VOID*)((UINTN)(&BootArguments->LinuxArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);\r
90a44ec4
OM
484\r
485 OptionalData = (UINT8*)BootArguments;\r
2ccfb71e 486 } else {\r
90a44ec4
OM
487 OptionalData = NULL;\r
488 OptionalDataSize = 0;\r
ea46ebbe 489 }\r
490\r
491 Print(L"Description for this new Entry: ");\r
3e710183 492 StrnCpy (BootDescription, BootOption->Description, BOOT_DEVICE_DESCRIPTION_MAX);\r
74b96132 493 Status = EditHIInputStr (BootDescription, BOOT_DEVICE_DESCRIPTION_MAX);\r
ea46ebbe 494 if (EFI_ERROR(Status)) {\r
495 Status = EFI_ABORTED;\r
496 goto FREE_DEVICE_PATH;\r
497 }\r
498\r
ea46ebbe 499 // Update the entry\r
90a44ec4 500 Status = BootOptionUpdate (BootOption, BootOption->Attributes, BootDescription, DevicePath, BootType, OptionalData, OptionalDataSize);\r
ea46ebbe 501\r
ea46ebbe 502FREE_DEVICE_PATH:\r
503 FreePool (DevicePath);\r
504\r
505EXIT:\r
326d1df9 506 if (Status == EFI_ABORTED) {\r
507 Print(L"\n");\r
508 }\r
ea46ebbe 509 return Status;\r
510}\r
511\r
95b3580f 512EFI_STATUS\r
513UpdateFdtPath (\r
514 IN LIST_ENTRY *BootOptionsList\r
515 )\r
516{\r
c0658bd6 517 EFI_STATUS Status;\r
518 UINTN FdtDevicePathSize;\r
519 BDS_SUPPORTED_DEVICE *SupportedBootDevice;\r
ecc62d13 520 EFI_DEVICE_PATH_PROTOCOL *FdtDevicePathNodes;\r
c0658bd6 521 EFI_DEVICE_PATH_PROTOCOL *FdtDevicePath;\r
95b3580f 522\r
523 Status = SelectBootDevice (&SupportedBootDevice);\r
524 if (EFI_ERROR(Status)) {\r
525 Status = EFI_ABORTED;\r
526 goto EXIT;\r
527 }\r
528\r
529 // Create the specific device path node\r
ecc62d13 530 Status = SupportedBootDevice->Support->CreateDevicePathNode (L"FDT blob", &FdtDevicePathNodes, NULL, NULL);\r
95b3580f 531 if (EFI_ERROR(Status)) {\r
532 Status = EFI_ABORTED;\r
533 goto EXIT;\r
534 }\r
535\r
ecc62d13 536 if (FdtDevicePathNodes != NULL) {\r
95b3580f 537 // Append the Device Path node to the select device path\r
ecc62d13 538 FdtDevicePath = AppendDevicePath (SupportedBootDevice->DevicePathProtocol, FdtDevicePathNodes);\r
c0658bd6 539 FdtDevicePathSize = GetDevicePathSize (FdtDevicePath);\r
aeaf64d6 540 Status = gRT->SetVariable (\r
541 (CHAR16*)L"Fdt",\r
542 &gArmGlobalVariableGuid,\r
543 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
544 FdtDevicePathSize,\r
545 FdtDevicePath\r
546 );\r
95b3580f 547 ASSERT_EFI_ERROR(Status);\r
548 } else {\r
aeaf64d6 549 gRT->SetVariable (\r
550 (CHAR16*)L"Fdt",\r
551 &gArmGlobalVariableGuid,\r
552 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
553 0,\r
554 NULL\r
555 );\r
95b3580f 556 ASSERT_EFI_ERROR(Status);\r
557 }\r
558\r
559EXIT:\r
560 if (Status == EFI_ABORTED) {\r
561 Print(L"\n");\r
562 }\r
563 FreePool(SupportedBootDevice);\r
564 return Status;\r
565}\r
566\r
ea46ebbe 567struct BOOT_MANAGER_ENTRY {\r
568 CONST CHAR16* Description;\r
569 EFI_STATUS (*Callback) (IN LIST_ENTRY *BootOptionsList);\r
570} BootManagerEntries[] = {\r
571 { L"Add Boot Device Entry", BootMenuAddBootOption },\r
572 { L"Update Boot Device Entry", BootMenuUpdateBootOption },\r
573 { L"Remove Boot Device Entry", BootMenuRemoveBootOption },\r
95b3580f 574 { L"Update FDT path", UpdateFdtPath },\r
ea46ebbe 575};\r
576\r
577EFI_STATUS\r
578BootMenuManager (\r
579 IN LIST_ENTRY *BootOptionsList\r
580 )\r
581{\r
582 UINTN Index;\r
583 UINTN OptionSelected;\r
584 UINTN BootManagerEntryCount;\r
585 EFI_STATUS Status;\r
586\r
587 BootManagerEntryCount = sizeof(BootManagerEntries) / sizeof(struct BOOT_MANAGER_ENTRY);\r
588\r
589 while (TRUE) {\r
590 // Display Boot Manager menu\r
591 for (Index = 0; Index < BootManagerEntryCount; Index++) {\r
592 Print(L"[%d] %s\n",Index+1,BootManagerEntries[Index]);\r
593 }\r
594 Print(L"[%d] Return to main menu\n",Index+1);\r
595\r
596 // Select which entry to call\r
597 Print(L"Choice: ");\r
598 Status = GetHIInputInteger (&OptionSelected);\r
599 if (EFI_ERROR(Status) || (OptionSelected == (BootManagerEntryCount+1))) {\r
326d1df9 600 if (EFI_ERROR(Status)) {\r
601 Print(L"\n");\r
602 }\r
ea46ebbe 603 return EFI_SUCCESS;\r
604 } else if ((OptionSelected > 0) && (OptionSelected <= BootManagerEntryCount)) {\r
ff7666c5 605 BootManagerEntries[OptionSelected-1].Callback (BootOptionsList);\r
ea46ebbe 606 }\r
607 }\r
11c20f4e 608 // Should never go here\r
ea46ebbe 609}\r
610\r
611EFI_STATUS\r
8213627e 612BootShell (\r
ea46ebbe 613 IN LIST_ENTRY *BootOptionsList\r
614 )\r
615{\r
616 EFI_STATUS Status;\r
617\r
618 // Start EFI Shell\r
8213627e 619 Status = BdsLoadApplication (mImageHandle, L"Shell", 0, NULL);\r
ea46ebbe 620 if (Status == EFI_NOT_FOUND) {\r
621 Print (L"Error: EFI Application not found.\n");\r
622 } else if (EFI_ERROR(Status)) {\r
623 Print (L"Error: Status Code: 0x%X\n",(UINT32)Status);\r
624 }\r
625\r
626 return Status;\r
627}\r
628\r
629struct BOOT_MAIN_ENTRY {\r
630 CONST CHAR16* Description;\r
631 EFI_STATUS (*Callback) (IN LIST_ENTRY *BootOptionsList);\r
632} BootMainEntries[] = {\r
8213627e 633 { L"Shell", BootShell },\r
ea46ebbe 634 { L"Boot Manager", BootMenuManager },\r
635};\r
636\r
637\r
638EFI_STATUS\r
639BootMenuMain (\r
640 VOID\r
641 )\r
642{\r
2ccfb71e 643 LIST_ENTRY BootOptionsList;\r
644 UINTN OptionCount;\r
645 UINTN BootOptionCount;\r
646 EFI_STATUS Status;\r
647 LIST_ENTRY* Entry;\r
648 BDS_LOAD_OPTION* BootOption;\r
649 UINTN BootOptionSelected;\r
650 UINTN Index;\r
651 UINTN BootMainEntryCount;\r
ea46ebbe 652\r
e862cd50 653 BootOption = NULL;\r
ea46ebbe 654 BootMainEntryCount = sizeof(BootMainEntries) / sizeof(struct BOOT_MAIN_ENTRY);\r
a6caee65 655\r
ea46ebbe 656 while (TRUE) {\r
a6e97d28 657 // Get Boot#### list\r
658 BootOptionList (&BootOptionsList);\r
659\r
ea46ebbe 660 OptionCount = 1;\r
661\r
662 // Display the Boot options\r
663 for (Entry = GetFirstNode (&BootOptionsList);\r
664 !IsNull (&BootOptionsList,Entry);\r
665 Entry = GetNextNode (&BootOptionsList,Entry)\r
666 )\r
667 {\r
668 BootOption = LOAD_OPTION_FROM_LINK(Entry);\r
669\r
ff7666c5 670 Print(L"[%d] %s\n", OptionCount, BootOption->Description);\r
ea46ebbe 671\r
672 DEBUG_CODE_BEGIN();\r
673 CHAR16* DevicePathTxt;\r
674 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\r
2ccfb71e 675 ARM_BDS_LOADER_OPTIONAL_DATA* OptionalData;\r
676 UINTN CmdLineSize;\r
3e710183 677 ARM_BDS_LOADER_TYPE LoaderType;\r
ea46ebbe 678\r
ff7666c5 679 Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
aa95e2f7 680 if (EFI_ERROR(Status)) {\r
681 // You must provide an implementation of DevicePathToTextProtocol in your firmware (eg: DevicePathDxe)\r
682 DEBUG((EFI_D_ERROR,"Error: Bds requires DevicePathToTextProtocol\n"));\r
683 return Status;\r
684 }\r
ff7666c5 685 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (BootOption->FilePathList, TRUE, TRUE);\r
ea46ebbe 686\r
687 Print(L"\t- %s\n",DevicePathTxt);\r
2ccfb71e 688\r
689 // If it is a supported BootEntry then print its details\r
690 if (IS_ARM_BDS_BOOTENTRY (BootOption)) {\r
691 OptionalData = BootOption->OptionalData;\r
692 LoaderType = (ARM_BDS_LOADER_TYPE)ReadUnaligned32 ((CONST UINT32*)&OptionalData->Header.LoaderType);\r
693 if ((LoaderType == BDS_LOADER_KERNEL_LINUX_ATAG) || (LoaderType == BDS_LOADER_KERNEL_LINUX_FDT)) {\r
694 if (ReadUnaligned16 (&OptionalData->Arguments.LinuxArguments.InitrdSize) > 0) {\r
695 CmdLineSize = ReadUnaligned16 (&OptionalData->Arguments.LinuxArguments.CmdLineSize);\r
696 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (\r
697 GetAlignedDevicePath ((EFI_DEVICE_PATH*)((UINTN)(&OptionalData->Arguments.LinuxArguments + 1) + CmdLineSize)), TRUE, TRUE);\r
698 Print(L"\t- Initrd: %s\n", DevicePathTxt);\r
699 }\r
700 Print(L"\t- Arguments: %a\n", (&OptionalData->Arguments.LinuxArguments + 1));\r
ea46ebbe 701 }\r
7135d76d 702\r
703 switch (LoaderType) {\r
704 case BDS_LOADER_EFI_APPLICATION:\r
705 Print(L"\t- LoaderType: EFI Application\n");\r
706 break;\r
707\r
708 case BDS_LOADER_KERNEL_LINUX_ATAG:\r
709 Print(L"\t- LoaderType: Linux kernel with ATAG support\n");\r
710 break;\r
711\r
712 case BDS_LOADER_KERNEL_LINUX_FDT:\r
713 Print(L"\t- LoaderType: Linux kernel with FDT support\n");\r
714 break;\r
715\r
716 default:\r
717 Print(L"\t- LoaderType: Not recognized (%d)\n", LoaderType);\r
718 }\r
ea46ebbe 719 }\r
ea46ebbe 720 FreePool(DevicePathTxt);\r
721 DEBUG_CODE_END();\r
722\r
723 OptionCount++;\r
724 }\r
725 BootOptionCount = OptionCount-1;\r
726\r
727 // Display the hardcoded Boot entries\r
728 for (Index = 0; Index < BootMainEntryCount; Index++) {\r
729 Print(L"[%d] %s\n",OptionCount,BootMainEntries[Index]);\r
730 OptionCount++;\r
731 }\r
732\r
733 // Request the boot entry from the user\r
734 BootOptionSelected = 0;\r
735 while (BootOptionSelected == 0) {\r
736 Print(L"Start: ");\r
737 Status = GetHIInputInteger (&BootOptionSelected);\r
738 if (EFI_ERROR(Status) || (BootOptionSelected == 0) || (BootOptionSelected > OptionCount)) {\r
739 Print(L"Invalid input (max %d)\n",(OptionCount-1));\r
740 BootOptionSelected = 0;\r
741 }\r
742 }\r
743\r
744 // Start the selected entry\r
745 if (BootOptionSelected > BootOptionCount) {\r
746 // Start the hardcoded entry\r
747 Status = BootMainEntries[BootOptionSelected - BootOptionCount - 1].Callback (&BootOptionsList);\r
748 } else {\r
749 // Find the selected entry from the Boot#### list\r
750 Index = 1;\r
751 for (Entry = GetFirstNode (&BootOptionsList);\r
752 !IsNull (&BootOptionsList,Entry);\r
753 Entry = GetNextNode (&BootOptionsList,Entry)\r
754 )\r
755 {\r
756 if (Index == BootOptionSelected) {\r
757 BootOption = LOAD_OPTION_FROM_LINK(Entry);\r
758 break;\r
759 }\r
760 Index++;\r
761 }\r
762\r
763 Status = BootOptionStart (BootOption);\r
764 }\r
765 }\r
11c20f4e 766 // Should never go here\r
ea46ebbe 767}\r