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