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