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