]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
MdeModulePkg/Bds: Wide match HTTP boot option.
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmLoadOption.c
CommitLineData
067ed98a
RN
1/** @file\r
2 Load option library functions which relate with creating and processing load options.\r
3\r
2e26862c 4Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
5d3a9896 5(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
067ed98a
RN
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "InternalBm.h"\r
17\r
18GLOBAL_REMOVE_IF_UNREFERENCED\r
19 CHAR16 *mBmLoadOptionName[] = {\r
20 L"Driver",\r
21 L"SysPrep",\r
780e05ca
RN
22 L"Boot",\r
23 L"PlatformRecovery"\r
067ed98a
RN
24 };\r
25\r
26GLOBAL_REMOVE_IF_UNREFERENCED\r
27 CHAR16 *mBmLoadOptionOrderName[] = {\r
28 EFI_DRIVER_ORDER_VARIABLE_NAME,\r
29 EFI_SYS_PREP_ORDER_VARIABLE_NAME,\r
780e05ca
RN
30 EFI_BOOT_ORDER_VARIABLE_NAME,\r
31 NULL // PlatformRecovery#### doesn't have associated *Order variable\r
067ed98a
RN
32 };\r
33\r
34/**\r
35 Call Visitor function for each variable in variable storage.\r
36\r
37 @param Visitor Visitor function.\r
38 @param Context The context passed to Visitor function.\r
39**/\r
40VOID\r
41BmForEachVariable (\r
d95ff8e8 42 BM_VARIABLE_VISITOR Visitor,\r
067ed98a
RN
43 VOID *Context\r
44 )\r
45{\r
46 EFI_STATUS Status;\r
47 CHAR16 *Name;\r
48 EFI_GUID Guid;\r
49 UINTN NameSize;\r
50 UINTN NewNameSize;\r
51\r
52 NameSize = sizeof (CHAR16);\r
53 Name = AllocateZeroPool (NameSize);\r
54 ASSERT (Name != NULL);\r
55 while (TRUE) {\r
56 NewNameSize = NameSize;\r
57 Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);\r
58 if (Status == EFI_BUFFER_TOO_SMALL) {\r
59 Name = ReallocatePool (NameSize, NewNameSize, Name);\r
60 ASSERT (Name != NULL);\r
61 Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);\r
62 NameSize = NewNameSize;\r
63 }\r
64\r
65 if (Status == EFI_NOT_FOUND) {\r
66 break;\r
67 }\r
68 ASSERT_EFI_ERROR (Status);\r
69\r
70 Visitor (Name, &Guid, Context);\r
71 }\r
72\r
73 FreePool (Name);\r
74}\r
75\r
76/**\r
77 Get the Option Number that wasn't used.\r
78\r
79 @param LoadOptionType The load option type.\r
80 @param FreeOptionNumber Return the minimal free option number.\r
81\r
82 @retval EFI_SUCCESS The option number is found and will be returned.\r
83 @retval EFI_OUT_OF_RESOURCES There is no free option number that can be used.\r
84 @retval EFI_INVALID_PARAMETER FreeOptionNumber is NULL\r
85\r
86**/\r
87EFI_STATUS\r
88BmGetFreeOptionNumber (\r
89 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType,\r
90 OUT UINT16 *FreeOptionNumber\r
91 )\r
92{\r
93 \r
94 UINTN OptionNumber;\r
95 UINTN Index;\r
96 UINT16 *OptionOrder;\r
97 UINTN OptionOrderSize;\r
98 UINT16 *BootNext;\r
99\r
100 ASSERT (FreeOptionNumber != NULL);\r
101 ASSERT (LoadOptionType == LoadOptionTypeDriver || \r
102 LoadOptionType == LoadOptionTypeBoot ||\r
103 LoadOptionType == LoadOptionTypeSysPrep);\r
104\r
105 GetEfiGlobalVariable2 (mBmLoadOptionOrderName[LoadOptionType], (VOID **) &OptionOrder, &OptionOrderSize);\r
eef53857
RN
106 ASSERT ((OptionOrder != NULL && OptionOrderSize != 0) || (OptionOrder == NULL && OptionOrderSize == 0));\r
107\r
067ed98a
RN
108 BootNext = NULL;\r
109 if (LoadOptionType == LoadOptionTypeBoot) {\r
110 GetEfiGlobalVariable2 (L"BootNext", (VOID**) &BootNext, NULL);\r
111 }\r
112\r
113 for (OptionNumber = 0; \r
114 OptionNumber < OptionOrderSize / sizeof (UINT16)\r
115 + ((BootNext != NULL) ? 1 : 0); \r
116 OptionNumber++\r
117 ) {\r
118 //\r
119 // Search in OptionOrder whether the OptionNumber exists\r
120 //\r
121 for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {\r
122 if (OptionNumber == OptionOrder[Index]) {\r
123 break;\r
124 }\r
125 }\r
126\r
127 //\r
128 // We didn't find it in the ****Order array and it doesn't equal to BootNext \r
129 // Otherwise, OptionNumber equals to OptionOrderSize / sizeof (UINT16) + 1\r
130 //\r
131 if ((Index == OptionOrderSize / sizeof (UINT16)) && \r
132 ((BootNext == NULL) || (OptionNumber != *BootNext))\r
133 ) {\r
134 break;\r
135 }\r
136 }\r
137 if (OptionOrder != NULL) {\r
138 FreePool (OptionOrder);\r
139 }\r
140\r
141 if (BootNext != NULL) {\r
142 FreePool (BootNext);\r
143 }\r
144\r
145 //\r
146 // When BootOrder & BootNext conver all numbers in the range [0 ... 0xffff],\r
147 // OptionNumber equals to 0x10000 which is not valid.\r
148 //\r
149 ASSERT (OptionNumber <= 0x10000);\r
150 if (OptionNumber == 0x10000) {\r
151 return EFI_OUT_OF_RESOURCES;\r
152 } else {\r
153 *FreeOptionNumber = (UINT16) OptionNumber;\r
154 return EFI_SUCCESS;\r
155 }\r
156}\r
157\r
158/**\r
780e05ca
RN
159 Create the Boot####, Driver####, SysPrep####, PlatformRecovery#### variable\r
160 from the load option.\r
161\r
067ed98a
RN
162 @param LoadOption Pointer to the load option.\r
163\r
164 @retval EFI_SUCCESS The variable was created.\r
165 @retval Others Error status returned by RT->SetVariable.\r
166**/\r
167EFI_STATUS\r
168EFIAPI\r
169EfiBootManagerLoadOptionToVariable (\r
170 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Option\r
171 )\r
172{\r
780e05ca 173 EFI_STATUS Status;\r
067ed98a
RN
174 UINTN VariableSize;\r
175 UINT8 *Variable;\r
176 UINT8 *Ptr;\r
177 CHAR16 OptionName[BM_OPTION_NAME_LEN];\r
178 CHAR16 *Description;\r
179 CHAR16 NullChar;\r
780e05ca 180 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;\r
067ed98a
RN
181 UINT32 VariableAttributes;\r
182\r
183 if ((Option->OptionNumber == LoadOptionNumberUnassigned) ||\r
184 (Option->FilePath == NULL) ||\r
185 ((UINT32) Option->OptionType >= LoadOptionTypeMax)\r
186 ) {\r
187 return EFI_INVALID_PARAMETER;\r
188 }\r
189\r
190 //\r
191 // Convert NULL description to empty description\r
192 //\r
193 NullChar = L'\0';\r
194 Description = Option->Description;\r
195 if (Description == NULL) {\r
196 Description = &NullChar;\r
197 }\r
198\r
199 /*\r
200 UINT32 Attributes;\r
201 UINT16 FilePathListLength;\r
202 CHAR16 Description[];\r
203 EFI_DEVICE_PATH_PROTOCOL FilePathList[];\r
204 UINT8 OptionalData[];\r
205TODO: FilePathList[] IS:\r
206A packed array of UEFI device paths. The first element of the \r
207array is a device path that describes the device and location of the \r
208Image for this load option. The FilePathList[0] is specific \r
209to the device type. Other device paths may optionally exist in the \r
210FilePathList, but their usage is OSV specific. Each element \r
211in the array is variable length, and ends at the device path end \r
212structure.\r
213 */\r
214 VariableSize = sizeof (Option->Attributes)\r
215 + sizeof (UINT16)\r
216 + StrSize (Description)\r
217 + GetDevicePathSize (Option->FilePath)\r
218 + Option->OptionalDataSize;\r
219\r
220 Variable = AllocatePool (VariableSize);\r
221 ASSERT (Variable != NULL);\r
222\r
223 Ptr = Variable;\r
224 WriteUnaligned32 ((UINT32 *) Ptr, Option->Attributes);\r
225 Ptr += sizeof (Option->Attributes);\r
226\r
227 WriteUnaligned16 ((UINT16 *) Ptr, (UINT16) GetDevicePathSize (Option->FilePath));\r
228 Ptr += sizeof (UINT16);\r
229\r
230 CopyMem (Ptr, Description, StrSize (Description));\r
231 Ptr += StrSize (Description);\r
232\r
233 CopyMem (Ptr, Option->FilePath, GetDevicePathSize (Option->FilePath));\r
234 Ptr += GetDevicePathSize (Option->FilePath);\r
235\r
236 CopyMem (Ptr, Option->OptionalData, Option->OptionalDataSize);\r
237\r
238 UnicodeSPrint (OptionName, sizeof (OptionName), L"%s%04x", mBmLoadOptionName[Option->OptionType], Option->OptionNumber);\r
239\r
240 VariableAttributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE;\r
780e05ca
RN
241 if (Option->OptionType == LoadOptionTypePlatformRecovery) {\r
242 //\r
243 // Lock the PlatformRecovery####\r
244 //\r
245 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);\r
246 if (!EFI_ERROR (Status)) {\r
247 Status = VariableLock->RequestToLock (VariableLock, OptionName, &gEfiGlobalVariableGuid);\r
248 ASSERT_EFI_ERROR (Status);\r
249 }\r
250 VariableAttributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
251 }\r
067ed98a
RN
252\r
253 return gRT->SetVariable (\r
254 OptionName,\r
255 &gEfiGlobalVariableGuid,\r
256 VariableAttributes,\r
257 VariableSize,\r
258 Variable\r
259 );\r
260}\r
261\r
262/**\r
263 Update order variable .\r
264\r
265 @param OptionOrderName Order variable name which need to be updated.\r
266 @param OptionNumber Option number for the new option.\r
267 @param Position Position of the new load option to put in the ****Order variable.\r
268\r
269 @retval EFI_SUCCESS The boot#### or driver#### have been successfully registered.\r
270 @retval EFI_ALREADY_STARTED The option number of Option is being used already.\r
271 @retval EFI_STATUS Return the status of gRT->SetVariable ().\r
272\r
273**/\r
274EFI_STATUS\r
275BmAddOptionNumberToOrderVariable (\r
276 IN CHAR16 *OptionOrderName,\r
277 IN UINT16 OptionNumber,\r
278 IN UINTN Position\r
279 )\r
280{\r
281 EFI_STATUS Status;\r
282 UINTN Index;\r
283 UINT16 *OptionOrder;\r
284 UINT16 *NewOptionOrder;\r
285 UINTN OptionOrderSize;\r
286 //\r
287 // Update the option order variable\r
288 //\r
289 GetEfiGlobalVariable2 (OptionOrderName, (VOID **) &OptionOrder, &OptionOrderSize);\r
eef53857 290 ASSERT ((OptionOrder != NULL && OptionOrderSize != 0) || (OptionOrder == NULL && OptionOrderSize == 0));\r
067ed98a
RN
291\r
292 Status = EFI_SUCCESS;\r
293 for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {\r
294 if (OptionOrder[Index] == OptionNumber) {\r
295 Status = EFI_ALREADY_STARTED;\r
296 break;\r
297 }\r
298 }\r
299\r
300 if (!EFI_ERROR (Status)) {\r
301 Position = MIN (Position, OptionOrderSize / sizeof (UINT16));\r
302\r
303 NewOptionOrder = AllocatePool (OptionOrderSize + sizeof (UINT16));\r
304 ASSERT (NewOptionOrder != NULL);\r
305 if (OptionOrderSize != 0) {\r
306 CopyMem (NewOptionOrder, OptionOrder, Position * sizeof (UINT16));\r
307 CopyMem (&NewOptionOrder[Position + 1], &OptionOrder[Position], OptionOrderSize - Position * sizeof (UINT16));\r
308 }\r
309 NewOptionOrder[Position] = OptionNumber;\r
310\r
311 Status = gRT->SetVariable (\r
312 OptionOrderName,\r
313 &gEfiGlobalVariableGuid,\r
314 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
315 OptionOrderSize + sizeof (UINT16),\r
316 NewOptionOrder\r
317 );\r
318 FreePool (NewOptionOrder);\r
319 }\r
320\r
321 if (OptionOrder != NULL) {\r
322 FreePool (OptionOrder);\r
323 }\r
324\r
325 return Status;\r
326}\r
327\r
328/**\r
329 This function will register the new Boot####, Driver#### or SysPrep#### option.\r
330 After the *#### is updated, the *Order will also be updated.\r
331\r
332 @param Option Pointer to load option to add.\r
333 @param Position Position of the new load option to put in the ****Order variable.\r
334\r
335 @retval EFI_SUCCESS The *#### have been successfully registered.\r
336 @retval EFI_INVALID_PARAMETER The option number exceeds 0xFFFF.\r
337 @retval EFI_ALREADY_STARTED The option number of Option is being used already.\r
338 Note: this API only adds new load option, no replacement support.\r
339 @retval EFI_OUT_OF_RESOURCES There is no free option number that can be used when the\r
340 option number specified in the Option is LoadOptionNumberUnassigned.\r
341 @retval EFI_STATUS Return the status of gRT->SetVariable ().\r
342\r
343**/\r
344EFI_STATUS\r
345EFIAPI\r
346EfiBootManagerAddLoadOptionVariable (\r
347 IN EFI_BOOT_MANAGER_LOAD_OPTION *Option,\r
348 IN UINTN Position\r
349 )\r
350{\r
351 EFI_STATUS Status;\r
352 UINT16 OptionNumber;\r
353\r
354 if (Option == NULL) {\r
355 return EFI_INVALID_PARAMETER;\r
356 }\r
357\r
358 if (Option->OptionType != LoadOptionTypeDriver && \r
359 Option->OptionType != LoadOptionTypeSysPrep &&\r
360 Option->OptionType != LoadOptionTypeBoot\r
361 ) {\r
362 return EFI_INVALID_PARAMETER;\r
363 }\r
364\r
365 //\r
366 // Get the free option number if the option number is unassigned\r
367 //\r
368 if (Option->OptionNumber == LoadOptionNumberUnassigned) {\r
369 Status = BmGetFreeOptionNumber (Option->OptionType, &OptionNumber);\r
370 if (EFI_ERROR (Status)) {\r
371 return Status;\r
372 }\r
373 Option->OptionNumber = OptionNumber;\r
374 }\r
375\r
376 if (Option->OptionNumber >= LoadOptionNumberMax) {\r
377 return EFI_INVALID_PARAMETER;\r
378 }\r
379\r
380 Status = BmAddOptionNumberToOrderVariable (mBmLoadOptionOrderName[Option->OptionType], (UINT16) Option->OptionNumber, Position);\r
381 if (!EFI_ERROR (Status)) {\r
382 //\r
383 // Save the Boot#### or Driver#### variable\r
384 //\r
385 Status = EfiBootManagerLoadOptionToVariable (Option);\r
386 if (EFI_ERROR (Status)) {\r
387 //\r
388 // Remove the #### from *Order variable when the Driver####/SysPrep####/Boot#### cannot be saved.\r
389 //\r
390 EfiBootManagerDeleteLoadOptionVariable (Option->OptionNumber, Option->OptionType);\r
391 }\r
392 }\r
393\r
394 return Status;\r
395}\r
396\r
397/**\r
398 Sort the load option. The DriverOrder or BootOrder will be re-created to \r
399 reflect the new order.\r
400\r
401 @param OptionType Load option type\r
402 @param CompareFunction The comparator\r
403**/\r
404VOID\r
405EFIAPI\r
406EfiBootManagerSortLoadOptionVariable (\r
407 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType,\r
408 SORT_COMPARE CompareFunction\r
409 )\r
410{\r
411 EFI_STATUS Status;\r
412 EFI_BOOT_MANAGER_LOAD_OPTION *LoadOption;\r
413 UINTN LoadOptionCount;\r
414 UINTN Index;\r
415 UINT16 *OptionOrder;\r
416\r
417 LoadOption = EfiBootManagerGetLoadOptions (&LoadOptionCount, OptionType);\r
418\r
419 //\r
420 // Insertion sort algorithm\r
421 //\r
422 PerformQuickSort (\r
423 LoadOption,\r
424 LoadOptionCount,\r
425 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION),\r
426 CompareFunction\r
427 );\r
428\r
429 //\r
430 // Create new ****Order variable\r
431 //\r
432 OptionOrder = AllocatePool (LoadOptionCount * sizeof (UINT16));\r
433 ASSERT (OptionOrder != NULL);\r
434 for (Index = 0; Index < LoadOptionCount; Index++) {\r
435 OptionOrder[Index] = (UINT16) LoadOption[Index].OptionNumber;\r
436 }\r
437\r
438 Status = gRT->SetVariable (\r
439 mBmLoadOptionOrderName[OptionType],\r
440 &gEfiGlobalVariableGuid,\r
441 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
442 LoadOptionCount * sizeof (UINT16),\r
443 OptionOrder\r
444 );\r
445 //\r
446 // Changing the *Order content without increasing its size with current variable implementation shouldn't fail.\r
447 //\r
448 ASSERT_EFI_ERROR (Status);\r
449\r
450 FreePool (OptionOrder);\r
451 EfiBootManagerFreeLoadOptions (LoadOption, LoadOptionCount);\r
452}\r
453\r
454/**\r
455 Initialize a load option.\r
456\r
457 @param Option Pointer to the load option to be initialized.\r
458 @param OptionNumber Option number of the load option.\r
459 @param OptionType Type of the load option.\r
460 @param Attributes Attributes of the load option.\r
461 @param Description Description of the load option.\r
462 @param FilePath Device path of the load option.\r
463 @param OptionalData Optional data of the load option.\r
464 @param OptionalDataSize Size of the optional data of the load option.\r
465\r
466 @retval EFI_SUCCESS The load option was initialized successfully.\r
467 @retval EFI_INVALID_PARAMETER Option, Description or FilePath is NULL.\r
468**/\r
469EFI_STATUS\r
470EFIAPI\r
471EfiBootManagerInitializeLoadOption (\r
472 IN OUT EFI_BOOT_MANAGER_LOAD_OPTION *Option,\r
473 IN UINTN OptionNumber,\r
474 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType,\r
475 IN UINT32 Attributes,\r
476 IN CHAR16 *Description,\r
477 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
478 IN UINT8 *OptionalData, OPTIONAL\r
479 IN UINT32 OptionalDataSize\r
480 )\r
481{\r
482 if ((Option == NULL) || (Description == NULL) || (FilePath == NULL)) {\r
483 return EFI_INVALID_PARAMETER;\r
484 }\r
485\r
486 if (((OptionalData != NULL) && (OptionalDataSize == 0)) ||\r
487 ((OptionalData == NULL) && (OptionalDataSize != 0))) {\r
488 return EFI_INVALID_PARAMETER;\r
489 }\r
490\r
491 if ((UINT32) OptionType >= LoadOptionTypeMax) {\r
492 return EFI_INVALID_PARAMETER;\r
493 }\r
494\r
495 ZeroMem (Option, sizeof (EFI_BOOT_MANAGER_LOAD_OPTION));\r
496 Option->OptionNumber = OptionNumber;\r
497 Option->OptionType = OptionType;\r
498 Option->Attributes = Attributes;\r
499 Option->Description = AllocateCopyPool (StrSize (Description), Description);\r
500 Option->FilePath = DuplicateDevicePath (FilePath);\r
501 if (OptionalData != NULL) {\r
502 Option->OptionalData = AllocateCopyPool (OptionalDataSize, OptionalData);\r
503 Option->OptionalDataSize = OptionalDataSize;\r
504 }\r
505\r
506 return EFI_SUCCESS;\r
507}\r
508\r
509\r
510/**\r
511 Return the index of the load option in the load option array.\r
512\r
513 The function consider two load options are equal when the \r
514 OptionType, Attributes, Description, FilePath and OptionalData are equal.\r
515\r
516 @param Key Pointer to the load option to be found.\r
517 @param Array Pointer to the array of load options to be found.\r
518 @param Count Number of entries in the Array.\r
519\r
520 @retval -1 Key wasn't found in the Array.\r
521 @retval 0 ~ Count-1 The index of the Key in the Array.\r
522**/\r
523INTN\r
5d3a9896
SW
524EFIAPI\r
525EfiBootManagerFindLoadOption (\r
067ed98a
RN
526 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Key,\r
527 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION *Array,\r
528 IN UINTN Count\r
529 )\r
530{\r
531 UINTN Index;\r
532\r
533 for (Index = 0; Index < Count; Index++) {\r
534 if ((Key->OptionType == Array[Index].OptionType) &&\r
535 (Key->Attributes == Array[Index].Attributes) &&\r
536 (StrCmp (Key->Description, Array[Index].Description) == 0) &&\r
537 (CompareMem (Key->FilePath, Array[Index].FilePath, GetDevicePathSize (Key->FilePath)) == 0) &&\r
538 (Key->OptionalDataSize == Array[Index].OptionalDataSize) &&\r
539 (CompareMem (Key->OptionalData, Array[Index].OptionalData, Key->OptionalDataSize) == 0)) {\r
540 return (INTN) Index;\r
541 }\r
542 }\r
543\r
544 return -1;\r
545}\r
546\r
547/**\r
548 Delete the load option.\r
549\r
550 @param OptionNumber Indicate the option number of load option\r
551 @param OptionType Indicate the type of load option\r
552\r
553 @retval EFI_INVALID_PARAMETER OptionType or OptionNumber is invalid.\r
554 @retval EFI_NOT_FOUND The load option cannot be found\r
555 @retval EFI_SUCCESS The load option was deleted\r
556 @retval others Status of RT->SetVariable()\r
557**/\r
558EFI_STATUS\r
559EFIAPI\r
560EfiBootManagerDeleteLoadOptionVariable (\r
561 IN UINTN OptionNumber,\r
562 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType\r
563 )\r
564{\r
565 UINT16 *OptionOrder;\r
566 UINTN OptionOrderSize;\r
067ed98a 567 UINTN Index;\r
780e05ca 568 CHAR16 OptionName[BM_OPTION_NAME_LEN];\r
067ed98a
RN
569\r
570 if (((UINT32) OptionType >= LoadOptionTypeMax) || (OptionNumber >= LoadOptionNumberMax)) {\r
571 return EFI_INVALID_PARAMETER;\r
572 }\r
573\r
067ed98a
RN
574 if (OptionType == LoadOptionTypeDriver || OptionType == LoadOptionTypeSysPrep || OptionType == LoadOptionTypeBoot) {\r
575 //\r
2e26862c
RN
576 // If the associated *Order exists, firstly remove the reference in *Order for\r
577 // Driver####, SysPrep#### and Boot####.\r
067ed98a 578 //\r
833a8349 579 GetEfiGlobalVariable2 (mBmLoadOptionOrderName[OptionType], (VOID **) &OptionOrder, &OptionOrderSize);\r
eef53857
RN
580 ASSERT ((OptionOrder != NULL && OptionOrderSize != 0) || (OptionOrder == NULL && OptionOrderSize == 0));\r
581\r
067ed98a
RN
582 for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {\r
583 if (OptionOrder[Index] == OptionNumber) {\r
584 OptionOrderSize -= sizeof (UINT16);\r
585 CopyMem (&OptionOrder[Index], &OptionOrder[Index + 1], OptionOrderSize - Index * sizeof (UINT16));\r
2e26862c
RN
586 gRT->SetVariable (\r
587 mBmLoadOptionOrderName[OptionType],\r
588 &gEfiGlobalVariableGuid,\r
589 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
590 OptionOrderSize,\r
591 OptionOrder\r
592 );\r
067ed98a
RN
593 break;\r
594 }\r
595 }\r
596 if (OptionOrder != NULL) {\r
597 FreePool (OptionOrder);\r
598 }\r
599 }\r
600\r
2e26862c
RN
601 //\r
602 // Remove the Driver####, SysPrep####, Boot#### or PlatformRecovery#### itself.\r
603 //\r
604 UnicodeSPrint (OptionName, sizeof (OptionName), L"%s%04x", mBmLoadOptionName[OptionType], OptionNumber);\r
605 return gRT->SetVariable (\r
606 OptionName,\r
607 &gEfiGlobalVariableGuid,\r
608 0,\r
609 0,\r
610 NULL\r
611 );\r
067ed98a
RN
612}\r
613\r
067ed98a
RN
614/**\r
615 Returns the size of a device path in bytes.\r
616\r
617 This function returns the size, in bytes, of the device path data structure \r
618 specified by DevicePath including the end of device path node. If DevicePath \r
619 is NULL, then 0 is returned. If the length of the device path is bigger than\r
620 MaxSize, also return 0 to indicate this is an invalidate device path.\r
621\r
622 @param DevicePath A pointer to a device path data structure.\r
623 @param MaxSize Max valid device path size. If big than this size, \r
624 return error.\r
625 \r
626 @retval 0 An invalid device path.\r
627 @retval Others The size of a device path in bytes.\r
628\r
629**/\r
630UINTN\r
631BmGetDevicePathSizeEx (\r
632 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
633 IN UINTN MaxSize\r
634 )\r
635{\r
636 UINTN Size;\r
637 UINTN NodeSize;\r
638\r
639 if (DevicePath == NULL) {\r
640 return 0;\r
641 }\r
642\r
643 //\r
644 // Search for the end of the device path structure\r
645 //\r
646 Size = 0;\r
647 while (!IsDevicePathEnd (DevicePath)) {\r
648 NodeSize = DevicePathNodeLength (DevicePath);\r
649 if (NodeSize == 0) {\r
650 return 0;\r
651 }\r
652 Size += NodeSize;\r
653 if (Size > MaxSize) {\r
654 return 0;\r
655 }\r
656 DevicePath = NextDevicePathNode (DevicePath);\r
657 }\r
658 Size += DevicePathNodeLength (DevicePath);\r
659 if (Size > MaxSize) {\r
660 return 0;\r
661 }\r
662\r
663 return Size;\r
664}\r
665\r
666/**\r
667 Returns the length of a Null-terminated Unicode string. If the length is \r
668 bigger than MaxStringLen, return length 0 to indicate that this is an \r
669 invalidate string.\r
670\r
671 This function returns the number of Unicode characters in the Null-terminated\r
672 Unicode string specified by String. \r
673\r
674 If String is NULL, then ASSERT().\r
675 If String is not aligned on a 16-bit boundary, then ASSERT().\r
676\r
677 @param String A pointer to a Null-terminated Unicode string.\r
678 @param MaxStringLen Max string len in this string.\r
679\r
680 @retval 0 An invalid string.\r
681 @retval Others The length of String.\r
682\r
683**/\r
684UINTN\r
685BmStrSizeEx (\r
686 IN CONST CHAR16 *String,\r
687 IN UINTN MaxStringLen\r
688 )\r
689{\r
690 UINTN Length;\r
691\r
692 ASSERT (String != NULL && MaxStringLen != 0);\r
693 ASSERT (((UINTN) String & BIT0) == 0);\r
694\r
695 for (Length = 0; *String != L'\0' && MaxStringLen != Length; String++, Length+=2);\r
696\r
697 if (*String != L'\0' && MaxStringLen == Length) {\r
698 return 0;\r
699 }\r
700\r
701 return Length + 2;\r
702}\r
703\r
704/**\r
780e05ca
RN
705 Validate the Boot####, Driver####, SysPrep#### and PlatformRecovery####\r
706 variable (VendorGuid/Name)\r
067ed98a
RN
707\r
708 @param Variable The variable data.\r
709 @param VariableSize The variable size.\r
710\r
711 @retval TRUE The variable data is correct.\r
712 @retval FALSE The variable data is corrupted.\r
713\r
714**/\r
715BOOLEAN \r
716BmValidateOption (\r
717 UINT8 *Variable,\r
718 UINTN VariableSize\r
719 )\r
720{\r
721 UINT16 FilePathSize;\r
722 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
723 UINTN DescriptionSize;\r
724\r
725 if (VariableSize <= sizeof (UINT16) + sizeof (UINT32)) {\r
726 return FALSE;\r
727 }\r
728\r
729 //\r
730 // Skip the option attribute\r
731 //\r
732 Variable += sizeof (UINT32);\r
733\r
734 //\r
735 // Get the option's device path size\r
736 //\r
737 FilePathSize = ReadUnaligned16 ((UINT16 *) Variable);\r
738 Variable += sizeof (UINT16);\r
739\r
740 //\r
741 // Get the option's description string size\r
742 //\r
743 DescriptionSize = BmStrSizeEx ((CHAR16 *) Variable, VariableSize - sizeof (UINT16) - sizeof (UINT32));\r
744 Variable += DescriptionSize;\r
745\r
746 //\r
747 // Get the option's device path\r
748 //\r
749 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Variable;\r
750\r
751 //\r
752 // Validation boot option variable.\r
753 //\r
754 if ((FilePathSize == 0) || (DescriptionSize == 0)) {\r
755 return FALSE;\r
756 }\r
757\r
758 if (sizeof (UINT32) + sizeof (UINT16) + DescriptionSize + FilePathSize > VariableSize) {\r
759 return FALSE;\r
760 }\r
761\r
762 return (BOOLEAN) (BmGetDevicePathSizeEx (DevicePath, FilePathSize) != 0);\r
763}\r
764\r
765/**\r
766 Check whether the VariableName is a valid load option variable name\r
767 and return the load option type and option number.\r
768\r
769 @param VariableName The name of the load option variable.\r
770 @param OptionType Return the load option type.\r
771 @param OptionNumber Return the load option number.\r
772\r
773 @retval TRUE The variable name is valid; The load option type and\r
774 load option number is returned.\r
775 @retval FALSE The variable name is NOT valid.\r
776**/\r
777BOOLEAN\r
778BmIsValidLoadOptionVariableName (\r
779 IN CHAR16 *VariableName,\r
780 OUT EFI_BOOT_MANAGER_LOAD_OPTION_TYPE *OptionType,\r
781 OUT UINT16 *OptionNumber\r
782 )\r
783{\r
784 UINTN VariableNameLen;\r
785 UINTN Index;\r
786 UINTN Uint;\r
787\r
788 VariableNameLen = StrLen (VariableName);\r
789\r
790 if (VariableNameLen <= 4) {\r
791 return FALSE;\r
792 }\r
793\r
794 for (Index = 0; Index < sizeof (mBmLoadOptionName) / sizeof (mBmLoadOptionName[0]); Index++) {\r
795 if ((VariableNameLen - 4 == StrLen (mBmLoadOptionName[Index])) &&\r
796 (StrnCmp (VariableName, mBmLoadOptionName[Index], VariableNameLen - 4) == 0)\r
797 ) {\r
798 break;\r
799 }\r
800 }\r
801\r
802 if (Index == sizeof (mBmLoadOptionName) / sizeof (mBmLoadOptionName[0])) {\r
803 return FALSE;\r
804 }\r
805\r
806 *OptionType = (EFI_BOOT_MANAGER_LOAD_OPTION_TYPE) Index;\r
807 *OptionNumber = 0;\r
808 for (Index = VariableNameLen - 4; Index < VariableNameLen; Index++) {\r
809 Uint = BmCharToUint (VariableName[Index]);\r
810 if (Uint == -1) {\r
811 break;\r
812 } else {\r
813 *OptionNumber = (UINT16) Uint + *OptionNumber * 0x10;\r
814 }\r
815 }\r
816\r
817 return (BOOLEAN) (Index == VariableNameLen);\r
818}\r
819\r
820/**\r
821 Build the Boot#### or Driver#### option from the VariableName.\r
822\r
823 @param VariableName Variable name of the load option\r
824 @param VendorGuid Variable GUID of the load option\r
825 @param Option Return the load option.\r
826\r
827 @retval EFI_SUCCESS Get the option just been created\r
828 @retval EFI_NOT_FOUND Failed to get the new option\r
829\r
830**/\r
831EFI_STATUS\r
832EFIAPI\r
833EfiBootManagerVariableToLoadOptionEx (\r
834 IN CHAR16 *VariableName,\r
835 IN EFI_GUID *VendorGuid,\r
836 IN OUT EFI_BOOT_MANAGER_LOAD_OPTION *Option \r
837 )\r
838{\r
839 EFI_STATUS Status;\r
840 UINT32 Attribute;\r
841 UINT16 FilePathSize;\r
842 UINT8 *Variable;\r
843 UINT8 *VariablePtr;\r
844 UINTN VariableSize;\r
845 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
846 UINT8 *OptionalData;\r
847 UINT32 OptionalDataSize;\r
848 CHAR16 *Description;\r
849 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType;\r
850 UINT16 OptionNumber;\r
851\r
852 if ((VariableName == NULL) || (Option == NULL)) {\r
853 return EFI_INVALID_PARAMETER;\r
854 }\r
855\r
856 if (!BmIsValidLoadOptionVariableName (VariableName, &OptionType, &OptionNumber)) {\r
857 return EFI_INVALID_PARAMETER;\r
858 }\r
859\r
860 //\r
861 // Read the variable\r
862 //\r
863 GetVariable2 (VariableName, VendorGuid, (VOID **) &Variable, &VariableSize);\r
864 if (Variable == NULL) {\r
865 return EFI_NOT_FOUND;\r
866 }\r
867\r
868 //\r
869 // Validate *#### variable data.\r
870 //\r
871 if (!BmValidateOption(Variable, VariableSize)) {\r
872 FreePool (Variable);\r
873 return EFI_INVALID_PARAMETER;\r
874 }\r
875\r
876 //\r
877 // Get the option attribute\r
878 //\r
879 VariablePtr = Variable;\r
880 Attribute = ReadUnaligned32 ((UINT32 *) VariablePtr);\r
881 VariablePtr += sizeof (UINT32);\r
882\r
883 //\r
884 // Get the option's device path size\r
885 //\r
886 FilePathSize = ReadUnaligned16 ((UINT16 *) VariablePtr);\r
887 VariablePtr += sizeof (UINT16);\r
888\r
889 //\r
890 // Get the option's description string\r
891 //\r
892 Description = (CHAR16 *) VariablePtr;\r
893\r
894 //\r
895 // Get the option's description string size\r
896 //\r
897 VariablePtr += StrSize ((CHAR16 *) VariablePtr);\r
898\r
899 //\r
900 // Get the option's device path\r
901 //\r
902 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) VariablePtr;\r
903 VariablePtr += FilePathSize;\r
904\r
905 OptionalDataSize = (UINT32) (VariableSize - (UINTN) (VariablePtr - Variable));\r
906 if (OptionalDataSize == 0) {\r
907 OptionalData = NULL;\r
908 } else {\r
909 OptionalData = VariablePtr;\r
910 }\r
911\r
912 Status = EfiBootManagerInitializeLoadOption (\r
913 Option,\r
914 OptionNumber,\r
915 OptionType,\r
916 Attribute,\r
917 Description,\r
918 FilePath,\r
919 OptionalData,\r
920 OptionalDataSize\r
921 );\r
922 ASSERT_EFI_ERROR (Status);\r
923\r
924 CopyGuid (&Option->VendorGuid, VendorGuid);\r
925\r
926 FreePool (Variable);\r
927 return Status;\r
928}\r
929\r
930/**\r
931Build the Boot#### or Driver#### option from the VariableName.\r
932\r
933@param VariableName EFI Variable name indicate if it is Boot#### or Driver####\r
934@param Option Return the Boot#### or Driver#### option.\r
935\r
936@retval EFI_SUCCESS Get the option just been created\r
937@retval EFI_NOT_FOUND Failed to get the new option\r
938**/\r
939EFI_STATUS\r
940EFIAPI\r
941EfiBootManagerVariableToLoadOption (\r
942 IN CHAR16 *VariableName,\r
943 IN OUT EFI_BOOT_MANAGER_LOAD_OPTION *Option\r
944 )\r
945{\r
946 return EfiBootManagerVariableToLoadOptionEx (VariableName, &gEfiGlobalVariableGuid, Option);\r
947}\r
948\r
780e05ca
RN
949typedef struct {\r
950 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType;\r
951 EFI_GUID *Guid;\r
952 EFI_BOOT_MANAGER_LOAD_OPTION *Options;\r
953 UINTN OptionCount;\r
954} BM_COLLECT_LOAD_OPTIONS_PARAM;\r
955\r
956/**\r
957 Visitor function to collect the Platform Recovery load options or OS Recovery\r
958 load options from NV storage.\r
959\r
960 @param Name Variable name.\r
961 @param Guid Variable GUID.\r
962 @param Context The same context passed to BmForEachVariable.\r
963**/\r
964VOID\r
965BmCollectLoadOptions (\r
966 IN CHAR16 *Name,\r
967 IN EFI_GUID *Guid,\r
968 IN VOID *Context\r
969 )\r
970{\r
971 EFI_STATUS Status;\r
972 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType;\r
973 UINT16 OptionNumber;\r
974 EFI_BOOT_MANAGER_LOAD_OPTION Option;\r
975 UINTN Index;\r
976 BM_COLLECT_LOAD_OPTIONS_PARAM *Param;\r
977\r
978 Param = (BM_COLLECT_LOAD_OPTIONS_PARAM *) Context;\r
979\r
980 if (CompareGuid (Guid, Param->Guid) && (\r
981 Param->OptionType == LoadOptionTypePlatformRecovery &&\r
982 BmIsValidLoadOptionVariableName (Name, &OptionType, &OptionNumber) &&\r
983 OptionType == LoadOptionTypePlatformRecovery\r
984 )) {\r
985 Status = EfiBootManagerVariableToLoadOptionEx (Name, Guid, &Option);\r
986 if (!EFI_ERROR (Status)) {\r
987 for (Index = 0; Index < Param->OptionCount; Index++) {\r
988 if (Param->Options[Index].OptionNumber > Option.OptionNumber) {\r
989 break;\r
990 }\r
991 }\r
992 Param->Options = ReallocatePool (\r
993 Param->OptionCount * sizeof (EFI_BOOT_MANAGER_LOAD_OPTION),\r
994 (Param->OptionCount + 1) * sizeof (EFI_BOOT_MANAGER_LOAD_OPTION),\r
995 Param->Options\r
996 );\r
997 ASSERT (Param->Options != NULL);\r
998 CopyMem (&Param->Options[Index + 1], &Param->Options[Index], (Param->OptionCount - Index) * sizeof (EFI_BOOT_MANAGER_LOAD_OPTION));\r
999 CopyMem (&Param->Options[Index], &Option, sizeof (EFI_BOOT_MANAGER_LOAD_OPTION));\r
1000 Param->OptionCount++;\r
1001 }\r
1002 }\r
1003}\r
1004\r
067ed98a
RN
1005/**\r
1006 Returns an array of load options based on the EFI variable\r
1007 L"BootOrder"/L"DriverOrder" and the L"Boot####"/L"Driver####" variables impled by it.\r
1008 #### is the hex value of the UINT16 in each BootOrder/DriverOrder entry. \r
1009\r
1010 @param LoadOptionCount Returns number of entries in the array.\r
1011 @param LoadOptionType The type of the load option.\r
1012\r
1013 @retval NULL No load options exist.\r
1014 @retval !NULL Array of load option entries.\r
1015\r
1016**/\r
1017EFI_BOOT_MANAGER_LOAD_OPTION *\r
1018EFIAPI\r
1019EfiBootManagerGetLoadOptions (\r
1020 OUT UINTN *OptionCount,\r
1021 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType\r
1022 )\r
1023{\r
780e05ca
RN
1024 EFI_STATUS Status;\r
1025 UINT16 *OptionOrder;\r
1026 UINTN OptionOrderSize;\r
1027 UINTN Index;\r
1028 UINTN OptionIndex;\r
1029 EFI_BOOT_MANAGER_LOAD_OPTION *Options;\r
1030 CHAR16 OptionName[BM_OPTION_NAME_LEN];\r
1031 UINT16 OptionNumber;\r
1032 BM_COLLECT_LOAD_OPTIONS_PARAM Param;\r
067ed98a
RN
1033\r
1034 *OptionCount = 0;\r
780e05ca 1035 Options = NULL;\r
067ed98a
RN
1036\r
1037 if (LoadOptionType == LoadOptionTypeDriver || LoadOptionType == LoadOptionTypeSysPrep || LoadOptionType == LoadOptionTypeBoot) {\r
1038 //\r
1039 // Read the BootOrder, or DriverOrder variable.\r
1040 //\r
1041 GetEfiGlobalVariable2 (mBmLoadOptionOrderName[LoadOptionType], (VOID **) &OptionOrder, &OptionOrderSize);\r
1042 if (OptionOrder == NULL) {\r
1043 return NULL;\r
1044 }\r
1045\r
1046 *OptionCount = OptionOrderSize / sizeof (UINT16);\r
1047\r
1048 Options = AllocatePool (*OptionCount * sizeof (EFI_BOOT_MANAGER_LOAD_OPTION));\r
1049 ASSERT (Options != NULL);\r
1050\r
1051 OptionIndex = 0;\r
1052 for (Index = 0; Index < *OptionCount; Index++) {\r
1053 OptionNumber = OptionOrder[Index];\r
1054 UnicodeSPrint (OptionName, sizeof (OptionName), L"%s%04x", mBmLoadOptionName[LoadOptionType], OptionNumber);\r
1055\r
1056 Status = EfiBootManagerVariableToLoadOption (OptionName, &Options[OptionIndex]);\r
1057 if (EFI_ERROR (Status)) {\r
1058 DEBUG ((EFI_D_INFO, "[Bds] %s doesn't exist - Update ****Order variable to remove the reference!!", OptionName));\r
1059 EfiBootManagerDeleteLoadOptionVariable (OptionNumber, LoadOptionType);\r
1060 } else {\r
1061 ASSERT (Options[OptionIndex].OptionNumber == OptionNumber);\r
1062 OptionIndex++;\r
1063 }\r
1064 }\r
1065\r
1066 if (OptionOrder != NULL) {\r
1067 FreePool (OptionOrder);\r
1068 }\r
1069\r
1070 if (OptionIndex < *OptionCount) {\r
1071 Options = ReallocatePool (*OptionCount * sizeof (EFI_BOOT_MANAGER_LOAD_OPTION), OptionIndex * sizeof (EFI_BOOT_MANAGER_LOAD_OPTION), Options);\r
1072 ASSERT (Options != NULL);\r
1073 *OptionCount = OptionIndex;\r
1074 }\r
1075\r
780e05ca
RN
1076 } else if (LoadOptionType == LoadOptionTypePlatformRecovery) {\r
1077 Param.OptionType = LoadOptionTypePlatformRecovery;\r
1078 Param.Options = NULL;\r
1079 Param.OptionCount = 0;\r
1080 Param.Guid = &gEfiGlobalVariableGuid;\r
1081\r
1082 BmForEachVariable (BmCollectLoadOptions, (VOID *) &Param);\r
1083\r
1084 *OptionCount = Param.OptionCount;\r
1085 Options = Param.Options;\r
067ed98a
RN
1086 }\r
1087\r
1088 return Options;\r
1089}\r
1090\r
1091/**\r
1092 Free an EFI_BOOT_MANGER_LOAD_OPTION entry that was allocate by the library.\r
1093\r
1094 @param LoadOption Pointer to boot option to Free.\r
1095\r
1096 @return EFI_SUCCESS BootOption was freed \r
1097 @return EFI_NOT_FOUND BootOption == NULL \r
1098\r
1099**/\r
1100EFI_STATUS\r
1101EFIAPI\r
1102EfiBootManagerFreeLoadOption (\r
1103 IN EFI_BOOT_MANAGER_LOAD_OPTION *LoadOption\r
1104 )\r
1105{\r
1106 if (LoadOption == NULL) {\r
1107 return EFI_NOT_FOUND;\r
1108 }\r
1109\r
1110 if (LoadOption->Description != NULL) {\r
1111 FreePool (LoadOption->Description);\r
1112 }\r
1113 if (LoadOption->FilePath != NULL) {\r
1114 FreePool (LoadOption->FilePath);\r
1115 }\r
1116 if (LoadOption->OptionalData != NULL) {\r
1117 FreePool (LoadOption->OptionalData);\r
1118 }\r
1119\r
1120 return EFI_SUCCESS;\r
1121}\r
1122\r
1123/**\r
1124 Free an EFI_BOOT_MANGER_LOAD_OPTION array that was allocated by \r
1125 EfiBootManagerGetLoadOptions().\r
1126\r
1127 @param Option Pointer to boot option array to free.\r
1128 @param OptionCount Number of array entries in BootOption\r
1129\r
1130 @return EFI_SUCCESS BootOption was freed \r
1131 @return EFI_NOT_FOUND BootOption == NULL \r
1132\r
1133**/\r
1134EFI_STATUS\r
1135EFIAPI\r
1136EfiBootManagerFreeLoadOptions (\r
1137 IN EFI_BOOT_MANAGER_LOAD_OPTION *Option,\r
1138 IN UINTN OptionCount\r
1139 )\r
1140{\r
1141 UINTN Index;\r
1142\r
1143 if (Option == NULL) {\r
1144 return EFI_NOT_FOUND;\r
1145 }\r
1146\r
1147 for (Index = 0;Index < OptionCount; Index++) {\r
1148 EfiBootManagerFreeLoadOption (&Option[Index]);\r
1149 }\r
1150\r
1151 FreePool (Option);\r
1152\r
1153 return EFI_SUCCESS;\r
1154}\r
1155\r
1156/**\r
1157 Return whether the PE header of the load option is valid or not.\r
1158\r
1159 @param[in] Type The load option type.\r
1160 @param[in] FileBuffer The PE file buffer of the load option.\r
1161 @param[in] FileSize The size of the load option file.\r
1162\r
1163 @retval TRUE The PE header of the load option is valid.\r
1164 @retval FALSE The PE header of the load option is not valid.\r
1165**/\r
1166BOOLEAN\r
1167BmIsLoadOptionPeHeaderValid (\r
1168 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE Type,\r
1169 IN VOID *FileBuffer,\r
1170 IN UINTN FileSize\r
1171 )\r
1172{\r
1173 EFI_IMAGE_DOS_HEADER *DosHeader;\r
1174 EFI_IMAGE_OPTIONAL_HEADER_UNION *PeHeader;\r
1175 EFI_IMAGE_OPTIONAL_HEADER32 *OptionalHeader;\r
1176 UINT16 Subsystem;\r
1177\r
1178 if (FileBuffer == NULL || FileSize == 0) {\r
1179 return FALSE;\r
1180 }\r
1181\r
1182 //\r
1183 // Read dos header\r
1184 //\r
1185 DosHeader = (EFI_IMAGE_DOS_HEADER *) FileBuffer;\r
1186 if (FileSize >= sizeof (EFI_IMAGE_DOS_HEADER) &&\r
1187 FileSize > DosHeader->e_lfanew && DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE\r
1188 ) {\r
1189 //\r
1190 // Read and check PE signature\r
1191 //\r
1192 PeHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINT8 *) FileBuffer + DosHeader->e_lfanew);\r
1193 if (FileSize >= DosHeader->e_lfanew + sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION) &&\r
1194 PeHeader->Pe32.Signature == EFI_IMAGE_NT_SIGNATURE\r
1195 ) {\r
1196 //\r
1197 // Check PE32 or PE32+ magic, and machine type\r
1198 //\r
1199 OptionalHeader = (EFI_IMAGE_OPTIONAL_HEADER32 *) &PeHeader->Pe32.OptionalHeader;\r
1200 if ((OptionalHeader->Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC || \r
1201 OptionalHeader->Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) &&\r
1202 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeHeader->Pe32.FileHeader.Machine)\r
1203 ) {\r
1204 //\r
1205 // Check the Subsystem:\r
1206 // Driver#### must be of type BootServiceDriver or RuntimeDriver\r
1207 // SysPrep####, Boot####, OsRecovery####, PlatformRecovery#### must be of type Application\r
1208 //\r
1209 Subsystem = OptionalHeader->Subsystem;\r
1210 if ((Type == LoadOptionTypeDriver && Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) ||\r
1211 (Type == LoadOptionTypeDriver && Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) ||\r
1212 (Type == LoadOptionTypeSysPrep && Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) ||\r
780e05ca
RN
1213 (Type == LoadOptionTypeBoot && Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) ||\r
1214 (Type == LoadOptionTypePlatformRecovery && Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)\r
067ed98a
RN
1215 ) {\r
1216 return TRUE;\r
1217 }\r
1218 }\r
1219 }\r
1220 }\r
1221\r
1222 return FALSE;\r
1223}\r
1224\r
1225/**\r
1226 Process (load and execute) the load option.\r
1227\r
1228 @param LoadOption Pointer to the load option.\r
1229\r
1230 @retval EFI_INVALID_PARAMETER The load option type is invalid, \r
1231 or the load option file path doesn't point to a valid file.\r
1232 @retval EFI_UNSUPPORTED The load option type is of LoadOptionTypeBoot.\r
1233 @retval EFI_SUCCESS The load option is inactive, or successfully loaded and executed.\r
1234**/\r
1235EFI_STATUS\r
1236EFIAPI\r
1237EfiBootManagerProcessLoadOption (\r
1238 IN EFI_BOOT_MANAGER_LOAD_OPTION *LoadOption\r
1239 )\r
1240{\r
1241 EFI_STATUS Status;\r
1242 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
1243 EFI_HANDLE ImageHandle;\r
1244 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
1245 VOID *FileBuffer;\r
1246 UINTN FileSize;\r
1247\r
1248 if ((UINT32) LoadOption->OptionType >= LoadOptionTypeMax) {\r
1249 return EFI_INVALID_PARAMETER;\r
1250 }\r
1251\r
1252 if (LoadOption->OptionType == LoadOptionTypeBoot) {\r
1253 return EFI_UNSUPPORTED;\r
1254 }\r
1255\r
1256 //\r
1257 // If a load option is not marked as LOAD_OPTION_ACTIVE,\r
1258 // the boot manager will not automatically load the option.\r
1259 //\r
1260 if ((LoadOption->Attributes & LOAD_OPTION_ACTIVE) == 0) {\r
1261 return EFI_SUCCESS;\r
1262 }\r
1263\r
1264 Status = EFI_INVALID_PARAMETER;\r
1265\r
1266 //\r
1267 // Load and start the load option.\r
1268 //\r
1269 DEBUG ((\r
1270 DEBUG_INFO | DEBUG_LOAD, "Process Load Option (%s%04x) ...\n",\r
1271 mBmLoadOptionName[LoadOption->OptionType], LoadOption->OptionNumber\r
1272 ));\r
1273 ImageHandle = NULL;\r
1274 FileBuffer = BmGetLoadOptionBuffer (LoadOption->FilePath, &FilePath, &FileSize);\r
1275 DEBUG_CODE (\r
1276 if (FileBuffer != NULL && CompareMem (LoadOption->FilePath, FilePath, GetDevicePathSize (FilePath)) != 0) {\r
1277 DEBUG ((EFI_D_INFO, "[Bds] DevicePath expand: "));\r
1278 BmPrintDp (LoadOption->FilePath);\r
1279 DEBUG ((EFI_D_INFO, " -> "));\r
1280 BmPrintDp (FilePath);\r
1281 DEBUG ((EFI_D_INFO, "\n"));\r
1282 }\r
1283 );\r
1284 if (BmIsLoadOptionPeHeaderValid (LoadOption->OptionType, FileBuffer, FileSize)) {\r
1285 Status = gBS->LoadImage (\r
1286 FALSE,\r
1287 gImageHandle,\r
1288 FilePath,\r
1289 FileBuffer,\r
1290 FileSize,\r
1291 &ImageHandle\r
1292 );\r
1293 }\r
1294 if (FilePath != NULL) {\r
1295 FreePool (FilePath);\r
1296 }\r
1297 if (FileBuffer != NULL) {\r
1298 FreePool (FileBuffer);\r
1299 }\r
1300\r
1301 if (!EFI_ERROR (Status)) {\r
1302 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
1303 ASSERT_EFI_ERROR (Status);\r
1304\r
1305 ImageInfo->LoadOptionsSize = LoadOption->OptionalDataSize;\r
1306 ImageInfo->LoadOptions = LoadOption->OptionalData;\r
1307 //\r
1308 // Before calling the image, enable the Watchdog Timer for the 5-minute period\r
1309 //\r
1310 gBS->SetWatchdogTimer (5 * 60, 0, 0, NULL);\r
1311\r
1312 LoadOption->Status = gBS->StartImage (ImageHandle, &LoadOption->ExitDataSize, &LoadOption->ExitData);\r
1313 DEBUG ((\r
1314 DEBUG_INFO | DEBUG_LOAD, "Load Option (%s%04x) Return Status = %r\n",\r
1315 mBmLoadOptionName[LoadOption->OptionType], LoadOption->OptionNumber, LoadOption->Status\r
1316 ));\r
1317\r
1318 //\r
1319 // Clear the Watchdog Timer after the image returns\r
1320 //\r
1321 gBS->SetWatchdogTimer (0, 0, 0, NULL);\r
1322 }\r
1323\r
1324 return Status;\r
1325}\r