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