]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
Fix default VENDOR_CLASS in DHCP discover.
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsBoot.c
CommitLineData
5c08e117 1/** @file\r
2 BDS Lib functions which relate with create or process the boot option.\r
3\r
69fc8f08 4Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>\r
180a5a35 5This program and the accompanying materials\r
5c08e117 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 "InternalBdsLib.h"\r
9aa7ba01 16#include "String.h"\r
5c08e117 17\r
18BOOLEAN mEnumBootDevice = FALSE;\r
9aa7ba01 19EFI_HII_HANDLE gBdsLibStringPackHandle = NULL;\r
5c08e117 20\r
9aa7ba01 21/**\r
22 The constructor function register UNI strings into imageHandle.\r
23 \r
24 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
25\r
26 @param ImageHandle The firmware allocated handle for the EFI image.\r
27 @param SystemTable A pointer to the EFI System Table.\r
28 \r
29 @retval EFI_SUCCESS The constructor successfully added string package.\r
30 @retval Other value The constructor can't add string package.\r
31\r
32**/\r
33EFI_STATUS\r
34EFIAPI\r
35GenericBdsLibConstructor (\r
36 IN EFI_HANDLE ImageHandle,\r
37 IN EFI_SYSTEM_TABLE *SystemTable\r
38 )\r
39{\r
40\r
41 gBdsLibStringPackHandle = HiiAddPackages (\r
e24fc103 42 &gBdsLibStringPackageGuid,\r
09363eca 43 ImageHandle,\r
9aa7ba01 44 GenericBdsLibStrings,\r
45 NULL\r
46 );\r
47\r
48 ASSERT (gBdsLibStringPackHandle != NULL);\r
49\r
50 return EFI_SUCCESS;\r
51}\r
52\r
16e5944a
RN
53/**\r
54 Deletete the Boot Option from EFI Variable. The Boot Order Arrray\r
55 is also updated.\r
56\r
57 @param OptionNumber The number of Boot option want to be deleted.\r
58 @param BootOrder The Boot Order array.\r
59 @param BootOrderSize The size of the Boot Order Array.\r
60\r
61 @retval EFI_SUCCESS The Boot Option Variable was found and removed\r
62 @retval EFI_UNSUPPORTED The Boot Option Variable store was inaccessible\r
63 @retval EFI_NOT_FOUND The Boot Option Variable was not found\r
64**/\r
65EFI_STATUS\r
66EFIAPI\r
67BdsDeleteBootOption (\r
68 IN UINTN OptionNumber,\r
69 IN OUT UINT16 *BootOrder,\r
70 IN OUT UINTN *BootOrderSize\r
71 )\r
72{\r
73 CHAR16 BootOption[9];\r
74 UINTN Index;\r
75 EFI_STATUS Status;\r
76\r
77 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", OptionNumber);\r
78 Status = gRT->SetVariable (\r
79 BootOption,\r
80 &gEfiGlobalVariableGuid,\r
81 0,\r
82 0,\r
83 NULL\r
84 );\r
69fc8f08
RN
85 //\r
86 // Deleting variable with existing variable implementation shouldn't fail.\r
87 //\r
88 ASSERT_EFI_ERROR (Status);\r
16e5944a
RN
89\r
90 //\r
91 // adjust boot order array\r
92 //\r
93 for (Index = 0; Index < *BootOrderSize / sizeof (UINT16); Index++) {\r
94 if (BootOrder[Index] == OptionNumber) {\r
95 CopyMem (&BootOrder[Index], &BootOrder[Index+1], *BootOrderSize - (Index+1) * sizeof (UINT16));\r
96 *BootOrderSize -= sizeof (UINT16);\r
97 break;\r
98 }\r
99 }\r
100\r
101 return Status;\r
102}\r
103/**\r
104\r
105 Translate the first n characters of an Ascii string to\r
106 Unicode characters. The count n is indicated by parameter\r
107 Size. If Size is greater than the length of string, then\r
108 the entire string is translated.\r
109\r
110\r
111 @param AStr Pointer to input Ascii string.\r
112 @param Size The number of characters to translate.\r
113 @param UStr Pointer to output Unicode string buffer.\r
114\r
115**/\r
116VOID\r
117AsciiToUnicodeSize (\r
118 IN UINT8 *AStr,\r
119 IN UINTN Size,\r
120 OUT UINT16 *UStr\r
121 )\r
122{\r
123 UINTN Idx;\r
124\r
125 Idx = 0;\r
126 while (AStr[Idx] != 0) {\r
127 UStr[Idx] = (CHAR16) AStr[Idx];\r
128 if (Idx == Size) {\r
129 break;\r
130 }\r
131\r
132 Idx++;\r
133 }\r
134 UStr[Idx] = 0;\r
135}\r
136\r
137/**\r
138 Build Legacy Device Name String according.\r
139\r
140 @param CurBBSEntry BBS Table.\r
141 @param Index Index.\r
142 @param BufSize The buffer size.\r
143 @param BootString The output string.\r
144\r
145**/\r
146VOID\r
147BdsBuildLegacyDevNameString (\r
148 IN BBS_TABLE *CurBBSEntry,\r
149 IN UINTN Index,\r
150 IN UINTN BufSize,\r
151 OUT CHAR16 *BootString\r
152 )\r
153{\r
154 CHAR16 *Fmt;\r
155 CHAR16 *Type;\r
156 UINT8 *StringDesc;\r
157 CHAR16 Temp[80];\r
158\r
159 switch (Index) {\r
160 //\r
161 // Primary Master\r
162 //\r
163 case 1:\r
164 Fmt = L"Primary Master %s";\r
165 break;\r
166\r
167 //\r
168 // Primary Slave\r
169 //\r
170 case 2:\r
171 Fmt = L"Primary Slave %s";\r
172 break;\r
173\r
174 //\r
175 // Secondary Master\r
176 //\r
177 case 3:\r
178 Fmt = L"Secondary Master %s";\r
179 break;\r
180\r
181 //\r
182 // Secondary Slave\r
183 //\r
184 case 4:\r
185 Fmt = L"Secondary Slave %s";\r
186 break;\r
187\r
188 default:\r
189 Fmt = L"%s";\r
190 break;\r
191 }\r
192\r
193 switch (CurBBSEntry->DeviceType) {\r
194 case BBS_FLOPPY:\r
195 Type = L"Floppy";\r
196 break;\r
197\r
198 case BBS_HARDDISK:\r
199 Type = L"Harddisk";\r
200 break;\r
201\r
202 case BBS_CDROM:\r
203 Type = L"CDROM";\r
204 break;\r
205\r
206 case BBS_PCMCIA:\r
207 Type = L"PCMCIAe";\r
208 break;\r
209\r
210 case BBS_USB:\r
211 Type = L"USB";\r
212 break;\r
213\r
214 case BBS_EMBED_NETWORK:\r
215 Type = L"Network";\r
216 break;\r
217\r
218 case BBS_BEV_DEVICE:\r
219 Type = L"BEVe";\r
220 break;\r
221\r
222 case BBS_UNKNOWN:\r
223 default:\r
224 Type = L"Unknown";\r
225 break;\r
226 }\r
227 //\r
228 // If current BBS entry has its description then use it.\r
229 //\r
230 StringDesc = (UINT8 *) (UINTN) ((CurBBSEntry->DescStringSegment << 4) + CurBBSEntry->DescStringOffset);\r
231 if (NULL != StringDesc) {\r
232 //\r
233 // Only get fisrt 32 characters, this is suggested by BBS spec\r
234 //\r
235 AsciiToUnicodeSize (StringDesc, 32, Temp);\r
236 Fmt = L"%s";\r
237 Type = Temp;\r
238 }\r
239\r
240 //\r
241 // BbsTable 16 entries are for onboard IDE.\r
242 // Set description string for SATA harddisks, Harddisk 0 ~ Harddisk 11\r
243 //\r
244 if (Index >= 5 && Index <= 16 && (CurBBSEntry->DeviceType == BBS_HARDDISK || CurBBSEntry->DeviceType == BBS_CDROM)) {\r
245 Fmt = L"%s %d";\r
246 UnicodeSPrint (BootString, BufSize, Fmt, Type, Index - 5);\r
247 } else {\r
248 UnicodeSPrint (BootString, BufSize, Fmt, Type);\r
249 }\r
250}\r
251\r
252/**\r
253\r
254 Create a legacy boot option for the specified entry of\r
255 BBS table, save it as variable, and append it to the boot\r
256 order list.\r
257\r
258\r
259 @param CurrentBbsEntry Pointer to current BBS table.\r
260 @param CurrentBbsDevPath Pointer to the Device Path Protocol instance of BBS\r
261 @param Index Index of the specified entry in BBS table.\r
262 @param BootOrderList On input, the original boot order list.\r
263 On output, the new boot order list attached with the\r
264 created node.\r
265 @param BootOrderListSize On input, the original size of boot order list.\r
266 On output, the size of new boot order list.\r
267\r
268 @retval EFI_SUCCESS Boot Option successfully created.\r
269 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.\r
270 @retval Other Error occurs while setting variable.\r
271\r
272**/\r
273EFI_STATUS\r
274BdsCreateLegacyBootOption (\r
275 IN BBS_TABLE *CurrentBbsEntry,\r
276 IN EFI_DEVICE_PATH_PROTOCOL *CurrentBbsDevPath,\r
277 IN UINTN Index,\r
278 IN OUT UINT16 **BootOrderList,\r
279 IN OUT UINTN *BootOrderListSize\r
280 )\r
281{\r
282 EFI_STATUS Status;\r
283 UINT16 CurrentBootOptionNo;\r
284 UINT16 BootString[10];\r
285 CHAR16 BootDesc[100];\r
286 CHAR8 HelpString[100];\r
287 UINT16 *NewBootOrderList;\r
288 UINTN BufferSize;\r
289 UINTN StringLen;\r
290 VOID *Buffer;\r
291 UINT8 *Ptr;\r
292 UINT16 CurrentBbsDevPathSize;\r
293 UINTN BootOrderIndex;\r
294 UINTN BootOrderLastIndex;\r
295 UINTN ArrayIndex;\r
296 BOOLEAN IndexNotFound;\r
297 BBS_BBS_DEVICE_PATH *NewBbsDevPathNode;\r
298\r
299 if ((*BootOrderList) == NULL) {\r
300 CurrentBootOptionNo = 0;\r
301 } else {\r
302 for (ArrayIndex = 0; ArrayIndex < (UINTN) (*BootOrderListSize / sizeof (UINT16)); ArrayIndex++) {\r
303 IndexNotFound = TRUE;\r
304 for (BootOrderIndex = 0; BootOrderIndex < (UINTN) (*BootOrderListSize / sizeof (UINT16)); BootOrderIndex++) {\r
305 if ((*BootOrderList)[BootOrderIndex] == ArrayIndex) {\r
306 IndexNotFound = FALSE;\r
307 break;\r
308 }\r
309 }\r
310\r
311 if (!IndexNotFound) {\r
312 continue;\r
313 } else {\r
314 break;\r
315 }\r
316 }\r
317\r
318 CurrentBootOptionNo = (UINT16) ArrayIndex;\r
319 }\r
320\r
321 UnicodeSPrint (\r
322 BootString,\r
323 sizeof (BootString),\r
324 L"Boot%04x",\r
325 CurrentBootOptionNo\r
326 );\r
327\r
328 BdsBuildLegacyDevNameString (CurrentBbsEntry, Index, sizeof (BootDesc), BootDesc);\r
329\r
330 //\r
331 // Create new BBS device path node with description string\r
332 //\r
333 UnicodeStrToAsciiStr (BootDesc, HelpString);\r
334\r
335 StringLen = AsciiStrLen (HelpString);\r
336 NewBbsDevPathNode = AllocateZeroPool (sizeof (BBS_BBS_DEVICE_PATH) + StringLen);\r
337 if (NewBbsDevPathNode == NULL) {\r
338 return EFI_OUT_OF_RESOURCES;\r
339 }\r
340 CopyMem (NewBbsDevPathNode, CurrentBbsDevPath, sizeof (BBS_BBS_DEVICE_PATH));\r
341 CopyMem (NewBbsDevPathNode->String, HelpString, StringLen + 1);\r
342 SetDevicePathNodeLength (&(NewBbsDevPathNode->Header), sizeof (BBS_BBS_DEVICE_PATH) + StringLen);\r
343\r
344 //\r
345 // Create entire new CurrentBbsDevPath with end node\r
346 //\r
347 CurrentBbsDevPath = AppendDevicePathNode (\r
348 NULL,\r
349 (EFI_DEVICE_PATH_PROTOCOL *) NewBbsDevPathNode\r
350 );\r
351 if (CurrentBbsDevPath == NULL) {\r
352 FreePool (NewBbsDevPathNode);\r
353 return EFI_OUT_OF_RESOURCES;\r
354 }\r
355\r
356 CurrentBbsDevPathSize = (UINT16) (GetDevicePathSize (CurrentBbsDevPath));\r
357\r
358 BufferSize = sizeof (UINT32) +\r
359 sizeof (UINT16) +\r
360 StrSize (BootDesc) +\r
361 CurrentBbsDevPathSize +\r
362 sizeof (BBS_TABLE) +\r
363 sizeof (UINT16);\r
364\r
365 Buffer = AllocateZeroPool (BufferSize);\r
366 if (Buffer == NULL) {\r
367 FreePool (NewBbsDevPathNode);\r
368 FreePool (CurrentBbsDevPath);\r
369 return EFI_OUT_OF_RESOURCES;\r
370 }\r
371\r
372 Ptr = (UINT8 *) Buffer;\r
373\r
374 *((UINT32 *) Ptr) = LOAD_OPTION_ACTIVE;\r
375 Ptr += sizeof (UINT32);\r
376\r
377 *((UINT16 *) Ptr) = CurrentBbsDevPathSize;\r
378 Ptr += sizeof (UINT16);\r
379\r
380 CopyMem (\r
381 Ptr,\r
382 BootDesc,\r
383 StrSize (BootDesc)\r
384 );\r
385 Ptr += StrSize (BootDesc);\r
386\r
387 CopyMem (\r
388 Ptr,\r
389 CurrentBbsDevPath,\r
390 CurrentBbsDevPathSize\r
391 );\r
392 Ptr += CurrentBbsDevPathSize;\r
393\r
394 CopyMem (\r
395 Ptr,\r
396 CurrentBbsEntry,\r
397 sizeof (BBS_TABLE)\r
398 );\r
399\r
400 Ptr += sizeof (BBS_TABLE);\r
401 *((UINT16 *) Ptr) = (UINT16) Index;\r
402\r
403 Status = gRT->SetVariable (\r
404 BootString,\r
405 &gEfiGlobalVariableGuid,\r
406 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
407 BufferSize,\r
408 Buffer\r
409 );\r
5c08e117 410\r
16e5944a
RN
411 FreePool (Buffer);\r
412 \r
413 Buffer = NULL;\r
414\r
415 NewBootOrderList = AllocateZeroPool (*BootOrderListSize + sizeof (UINT16));\r
416 if (NULL == NewBootOrderList) {\r
417 FreePool (NewBbsDevPathNode);\r
418 FreePool (CurrentBbsDevPath);\r
419 return EFI_OUT_OF_RESOURCES;\r
420 }\r
421\r
422 if (*BootOrderList != NULL) {\r
423 CopyMem (NewBootOrderList, *BootOrderList, *BootOrderListSize);\r
424 FreePool (*BootOrderList);\r
425 }\r
426\r
427 BootOrderLastIndex = (UINTN) (*BootOrderListSize / sizeof (UINT16));\r
428 NewBootOrderList[BootOrderLastIndex] = CurrentBootOptionNo;\r
429 *BootOrderListSize += sizeof (UINT16);\r
430 *BootOrderList = NewBootOrderList;\r
431\r
432 FreePool (NewBbsDevPathNode);\r
433 FreePool (CurrentBbsDevPath);\r
434 return Status;\r
435}\r
436\r
437/**\r
438 Check if the boot option is a legacy one.\r
439\r
440 @param BootOptionVar The boot option data payload.\r
441 @param BbsEntry The BBS Table.\r
442 @param BbsIndex The table index.\r
443\r
444 @retval TRUE It is a legacy boot option.\r
445 @retval FALSE It is not a legacy boot option.\r
446\r
447**/\r
448BOOLEAN\r
449BdsIsLegacyBootOption (\r
450 IN UINT8 *BootOptionVar,\r
451 OUT BBS_TABLE **BbsEntry,\r
452 OUT UINT16 *BbsIndex\r
453 )\r
454{\r
455 UINT8 *Ptr;\r
456 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
457 BOOLEAN Ret;\r
458 UINT16 DevPathLen;\r
459\r
460 Ptr = BootOptionVar;\r
461 Ptr += sizeof (UINT32);\r
462 DevPathLen = *(UINT16 *) Ptr;\r
463 Ptr += sizeof (UINT16);\r
464 Ptr += StrSize ((UINT16 *) Ptr);\r
465 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Ptr;\r
466 if ((BBS_DEVICE_PATH == DevicePath->Type) && (BBS_BBS_DP == DevicePath->SubType)) {\r
467 Ptr += DevPathLen;\r
468 *BbsEntry = (BBS_TABLE *) Ptr;\r
469 Ptr += sizeof (BBS_TABLE);\r
470 *BbsIndex = *(UINT16 *) Ptr;\r
471 Ret = TRUE;\r
472 } else {\r
473 *BbsEntry = NULL;\r
474 Ret = FALSE;\r
475 }\r
476\r
477 return Ret;\r
478}\r
479\r
480/**\r
481 Delete all the invalid legacy boot options.\r
482\r
483 @retval EFI_SUCCESS All invalide legacy boot options are deleted.\r
484 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.\r
485 @retval EFI_NOT_FOUND Fail to retrive variable of boot order.\r
486**/\r
487EFI_STATUS\r
488EFIAPI\r
489BdsDeleteAllInvalidLegacyBootOptions (\r
490 VOID\r
491 )\r
492{\r
493 UINT16 *BootOrder;\r
494 UINT8 *BootOptionVar;\r
495 UINTN BootOrderSize;\r
496 UINTN BootOptionSize;\r
497 EFI_STATUS Status;\r
498 UINT16 HddCount;\r
499 UINT16 BbsCount;\r
500 HDD_INFO *LocalHddInfo;\r
501 BBS_TABLE *LocalBbsTable;\r
502 BBS_TABLE *BbsEntry;\r
503 UINT16 BbsIndex;\r
504 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
505 UINTN Index;\r
506 UINT16 BootOption[10];\r
507 UINT16 BootDesc[100];\r
508 BOOLEAN DescStringMatch;\r
509\r
510 Status = EFI_SUCCESS;\r
511 BootOrder = NULL;\r
512 BootOrderSize = 0;\r
513 HddCount = 0;\r
514 BbsCount = 0;\r
515 LocalHddInfo = NULL;\r
516 LocalBbsTable = NULL;\r
517 BbsEntry = NULL;\r
518\r
519 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);\r
520 if (EFI_ERROR (Status)) {\r
521 return Status;\r
522 }\r
523\r
524 LegacyBios->GetBbsInfo (\r
525 LegacyBios,\r
526 &HddCount,\r
527 &LocalHddInfo,\r
528 &BbsCount,\r
529 &LocalBbsTable\r
530 );\r
531\r
532 BootOrder = BdsLibGetVariableAndSize (\r
533 L"BootOrder",\r
534 &gEfiGlobalVariableGuid,\r
535 &BootOrderSize\r
536 );\r
537 if (BootOrder == NULL) {\r
538 BootOrderSize = 0;\r
539 }\r
540\r
541 Index = 0;\r
542 while (Index < BootOrderSize / sizeof (UINT16)) {\r
543 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
544 BootOptionVar = BdsLibGetVariableAndSize (\r
545 BootOption,\r
546 &gEfiGlobalVariableGuid,\r
547 &BootOptionSize\r
548 );\r
549 if (NULL == BootOptionVar) {\r
550 BootOptionSize = 0;\r
551 Status = gRT->GetVariable (\r
552 BootOption,\r
553 &gEfiGlobalVariableGuid,\r
554 NULL,\r
555 &BootOptionSize,\r
556 BootOptionVar\r
557 );\r
558 if (Status == EFI_NOT_FOUND) {\r
559 //\r
560 // Update BootOrder\r
561 //\r
562 BdsDeleteBootOption (\r
563 BootOrder[Index],\r
564 BootOrder,\r
565 &BootOrderSize\r
566 );\r
567 continue;\r
568 } else {\r
569 FreePool (BootOrder);\r
570 return EFI_OUT_OF_RESOURCES;\r
571 }\r
572 }\r
573 \r
574 //\r
575 // Skip Non-Legacy boot option\r
576 // \r
577 if (!BdsIsLegacyBootOption (BootOptionVar, &BbsEntry, &BbsIndex)) {\r
578 if (BootOptionVar!= NULL) {\r
579 FreePool (BootOptionVar);\r
580 }\r
581 Index++;\r
582 continue;\r
583 }\r
584\r
585 if (BbsIndex < BbsCount) {\r
586 //\r
587 // Check if BBS Description String is changed\r
588 //\r
589 DescStringMatch = FALSE;\r
590 BdsBuildLegacyDevNameString (\r
591 &LocalBbsTable[BbsIndex],\r
592 BbsIndex,\r
593 sizeof (BootDesc),\r
594 BootDesc\r
595 );\r
596\r
597 if (StrCmp (BootDesc, (UINT16*)(BootOptionVar + sizeof (UINT32) + sizeof (UINT16))) == 0) {\r
598 DescStringMatch = TRUE;\r
599 }\r
600\r
601 if (!((LocalBbsTable[BbsIndex].BootPriority == BBS_IGNORE_ENTRY) ||\r
602 (LocalBbsTable[BbsIndex].BootPriority == BBS_DO_NOT_BOOT_FROM)) &&\r
603 (LocalBbsTable[BbsIndex].DeviceType == BbsEntry->DeviceType) &&\r
604 DescStringMatch) {\r
605 Index++;\r
606 continue;\r
607 }\r
608 }\r
609\r
610 if (BootOptionVar != NULL) {\r
611 FreePool (BootOptionVar);\r
612 }\r
613 //\r
614 // should delete\r
615 //\r
616 BdsDeleteBootOption (\r
617 BootOrder[Index],\r
618 BootOrder,\r
619 &BootOrderSize\r
620 );\r
621 }\r
622\r
623 //\r
624 // Adjust the number of boot options.\r
625 //\r
626 Status = gRT->SetVariable (\r
627 L"BootOrder",\r
628 &gEfiGlobalVariableGuid,\r
629 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
630 BootOrderSize,\r
631 BootOrder\r
632 );\r
69fc8f08
RN
633 //\r
634 // Shrinking variable with existing variable implementation shouldn't fail.\r
635 //\r
636 ASSERT_EFI_ERROR (Status);\r
16e5944a
RN
637 if (BootOrder != NULL) {\r
638 FreePool (BootOrder);\r
639 }\r
640\r
641 return Status;\r
642}\r
643\r
644/**\r
645 Find all legacy boot option by device type.\r
646\r
647 @param BootOrder The boot order array.\r
648 @param BootOptionNum The number of boot option.\r
649 @param DevType Device type.\r
650 @param DevName Device name.\r
651 @param Attribute The boot option attribute.\r
652 @param BbsIndex The BBS table index.\r
653 @param OptionNumber The boot option index.\r
654\r
655 @retval TRUE The Legacy boot option is found.\r
656 @retval FALSE The legacy boot option is not found.\r
657\r
658**/\r
659BOOLEAN\r
660BdsFindLegacyBootOptionByDevTypeAndName (\r
661 IN UINT16 *BootOrder,\r
662 IN UINTN BootOptionNum,\r
663 IN UINT16 DevType,\r
664 IN CHAR16 *DevName,\r
665 OUT UINT32 *Attribute,\r
666 OUT UINT16 *BbsIndex,\r
667 OUT UINT16 *OptionNumber\r
668 )\r
669{\r
670 UINTN Index;\r
671 CHAR16 BootOption[9];\r
672 UINTN BootOptionSize;\r
673 UINT8 *BootOptionVar;\r
674 BBS_TABLE *BbsEntry;\r
675 BOOLEAN Found;\r
676\r
677 BbsEntry = NULL;\r
678 Found = FALSE;\r
679\r
680 if (NULL == BootOrder) {\r
681 return Found;\r
682 }\r
683\r
684 //\r
685 // Loop all boot option from variable\r
686 //\r
687 for (Index = 0; Index < BootOptionNum; Index++) {\r
688 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", (UINTN) BootOrder[Index]);\r
689 BootOptionVar = BdsLibGetVariableAndSize (\r
690 BootOption,\r
691 &gEfiGlobalVariableGuid,\r
692 &BootOptionSize\r
693 );\r
694 if (NULL == BootOptionVar) {\r
695 continue;\r
696 }\r
697\r
698 //\r
699 // Skip Non-legacy boot option\r
700 //\r
701 if (!BdsIsLegacyBootOption (BootOptionVar, &BbsEntry, BbsIndex)) {\r
702 FreePool (BootOptionVar);\r
703 continue;\r
704 }\r
705\r
706 if (\r
707 (BbsEntry->DeviceType != DevType) ||\r
708 (StrCmp (DevName, (CHAR16*)(BootOptionVar + sizeof (UINT32) + sizeof (UINT16))) != 0)\r
709 ) {\r
710 FreePool (BootOptionVar);\r
711 continue;\r
712 }\r
713\r
714 *Attribute = *(UINT32 *) BootOptionVar;\r
715 *OptionNumber = BootOrder[Index];\r
716 Found = TRUE;\r
717 FreePool (BootOptionVar);\r
718 break;\r
719 }\r
720\r
721 return Found;\r
722}\r
723\r
724/**\r
725 Create a legacy boot option.\r
726\r
727 @param BbsItem The BBS Table entry.\r
728 @param Index Index of the specified entry in BBS table.\r
729 @param BootOrderList The boot order list.\r
730 @param BootOrderListSize The size of boot order list.\r
731\r
732 @retval EFI_OUT_OF_RESOURCE No enough memory.\r
733 @retval EFI_SUCCESS The function complete successfully.\r
734 @return Other value if the legacy boot option is not created.\r
735\r
736**/\r
737EFI_STATUS\r
738BdsCreateOneLegacyBootOption (\r
739 IN BBS_TABLE *BbsItem,\r
740 IN UINTN Index,\r
741 IN OUT UINT16 **BootOrderList,\r
742 IN OUT UINTN *BootOrderListSize\r
743 )\r
744{\r
745 BBS_BBS_DEVICE_PATH BbsDevPathNode;\r
746 EFI_STATUS Status;\r
747 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
748\r
749 DevPath = NULL;\r
750\r
751 //\r
752 // Create device path node.\r
753 //\r
754 BbsDevPathNode.Header.Type = BBS_DEVICE_PATH;\r
755 BbsDevPathNode.Header.SubType = BBS_BBS_DP;\r
756 SetDevicePathNodeLength (&BbsDevPathNode.Header, sizeof (BBS_BBS_DEVICE_PATH));\r
757 BbsDevPathNode.DeviceType = BbsItem->DeviceType;\r
758 CopyMem (&BbsDevPathNode.StatusFlag, &BbsItem->StatusFlags, sizeof (UINT16));\r
759\r
760 DevPath = AppendDevicePathNode (\r
761 NULL,\r
762 (EFI_DEVICE_PATH_PROTOCOL *) &BbsDevPathNode\r
763 );\r
764 if (NULL == DevPath) {\r
765 return EFI_OUT_OF_RESOURCES;\r
766 }\r
767\r
768 Status = BdsCreateLegacyBootOption (\r
769 BbsItem,\r
770 DevPath,\r
771 Index,\r
772 BootOrderList,\r
773 BootOrderListSize\r
774 );\r
775 BbsItem->BootPriority = 0x00;\r
776\r
777 FreePool (DevPath);\r
778\r
779 return Status;\r
780}\r
781\r
782/**\r
783 Add the legacy boot options from BBS table if they do not exist.\r
784\r
785 @retval EFI_SUCCESS The boot options are added successfully \r
786 or they are already in boot options.\r
787 @retval EFI_NOT_FOUND No legacy boot options is found.\r
788 @retval EFI_OUT_OF_RESOURCE No enough memory.\r
789 @return Other value LegacyBoot options are not added.\r
790**/\r
791EFI_STATUS\r
792EFIAPI\r
793BdsAddNonExistingLegacyBootOptions (\r
794 VOID\r
795 )\r
796{\r
797 UINT16 *BootOrder;\r
798 UINTN BootOrderSize;\r
799 EFI_STATUS Status;\r
800 CHAR16 Desc[100];\r
801 UINT16 HddCount;\r
802 UINT16 BbsCount;\r
803 HDD_INFO *LocalHddInfo;\r
804 BBS_TABLE *LocalBbsTable;\r
805 UINT16 BbsIndex;\r
806 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
807 UINT16 Index;\r
808 UINT32 Attribute;\r
809 UINT16 OptionNumber;\r
810 BOOLEAN Exist;\r
811\r
812 HddCount = 0;\r
813 BbsCount = 0;\r
814 LocalHddInfo = NULL;\r
815 LocalBbsTable = NULL;\r
816\r
817 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);\r
818 if (EFI_ERROR (Status)) {\r
819 return Status;\r
820 }\r
821\r
822 LegacyBios->GetBbsInfo (\r
823 LegacyBios,\r
824 &HddCount,\r
825 &LocalHddInfo,\r
826 &BbsCount,\r
827 &LocalBbsTable\r
828 );\r
829\r
830 BootOrder = BdsLibGetVariableAndSize (\r
831 L"BootOrder",\r
832 &gEfiGlobalVariableGuid,\r
833 &BootOrderSize\r
834 );\r
835 if (BootOrder == NULL) {\r
836 BootOrderSize = 0;\r
837 }\r
838\r
839 for (Index = 0; Index < BbsCount; Index++) {\r
840 if ((LocalBbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) ||\r
841 (LocalBbsTable[Index].BootPriority == BBS_DO_NOT_BOOT_FROM)\r
842 ) {\r
843 continue;\r
844 }\r
845\r
846 BdsBuildLegacyDevNameString (&LocalBbsTable[Index], Index, sizeof (Desc), Desc);\r
847\r
848 Exist = BdsFindLegacyBootOptionByDevTypeAndName (\r
849 BootOrder,\r
850 BootOrderSize / sizeof (UINT16),\r
851 LocalBbsTable[Index].DeviceType,\r
852 Desc,\r
853 &Attribute,\r
854 &BbsIndex,\r
855 &OptionNumber\r
856 );\r
857 if (!Exist) {\r
858 //\r
859 // Not found such type of legacy device in boot options or we found but it's disabled\r
860 // so we have to create one and put it to the tail of boot order list\r
861 //\r
862 Status = BdsCreateOneLegacyBootOption (\r
863 &LocalBbsTable[Index],\r
864 Index,\r
865 &BootOrder,\r
866 &BootOrderSize\r
867 );\r
69fc8f08
RN
868 if (!EFI_ERROR (Status)) {\r
869 BbsIndex = Index;\r
870 OptionNumber = BootOrder[BootOrderSize / sizeof (UINT16) - 1];\r
16e5944a 871 }\r
16e5944a
RN
872 }\r
873\r
874 ASSERT (BbsIndex == Index);\r
875 }\r
876\r
877 Status = gRT->SetVariable (\r
878 L"BootOrder",\r
879 &gEfiGlobalVariableGuid,\r
880 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
881 BootOrderSize,\r
882 BootOrder\r
883 );\r
884 if (BootOrder != NULL) {\r
885 FreePool (BootOrder);\r
886 }\r
887\r
888 return Status;\r
889}\r
890\r
891/**\r
892 Fill the device order buffer.\r
893\r
894 @param BbsTable The BBS table.\r
895 @param BbsType The BBS Type.\r
896 @param BbsCount The BBS Count.\r
897 @param Buf device order buffer.\r
898\r
899 @return The device order buffer.\r
900\r
901**/\r
902UINT16 *\r
903BdsFillDevOrderBuf (\r
904 IN BBS_TABLE *BbsTable,\r
905 IN BBS_TYPE BbsType,\r
906 IN UINTN BbsCount,\r
907 OUT UINT16 *Buf\r
908 )\r
909{\r
910 UINTN Index;\r
911\r
912 for (Index = 0; Index < BbsCount; Index++) {\r
913 if (BbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) {\r
914 continue;\r
915 }\r
916\r
917 if (BbsTable[Index].DeviceType != BbsType) {\r
918 continue;\r
919 }\r
920\r
921 *Buf = (UINT16) (Index & 0xFF);\r
922 Buf++;\r
923 }\r
924\r
925 return Buf;\r
926}\r
927\r
928/**\r
929 Create the device order buffer.\r
930\r
931 @param BbsTable The BBS table.\r
932 @param BbsCount The BBS Count.\r
933\r
934 @retval EFI_SUCCES The buffer is created and the EFI variable named \r
935 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is\r
936 set correctly.\r
937 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.\r
938 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail\r
939 because of hardware error.\r
940**/\r
941EFI_STATUS\r
942BdsCreateDevOrder (\r
943 IN BBS_TABLE *BbsTable,\r
944 IN UINT16 BbsCount\r
945 )\r
946{\r
947 UINTN Index;\r
948 UINTN FDCount;\r
949 UINTN HDCount;\r
950 UINTN CDCount;\r
951 UINTN NETCount;\r
952 UINTN BEVCount;\r
953 UINTN TotalSize;\r
954 UINTN HeaderSize;\r
955 LEGACY_DEV_ORDER_ENTRY *DevOrder;\r
956 LEGACY_DEV_ORDER_ENTRY *DevOrderPtr;\r
957 EFI_STATUS Status;\r
958\r
959 FDCount = 0;\r
960 HDCount = 0;\r
961 CDCount = 0;\r
962 NETCount = 0;\r
963 BEVCount = 0;\r
964 TotalSize = 0;\r
965 HeaderSize = sizeof (BBS_TYPE) + sizeof (UINT16);\r
966 DevOrder = NULL;\r
967 Status = EFI_SUCCESS;\r
968\r
969 //\r
970 // Count all boot devices\r
971 //\r
972 for (Index = 0; Index < BbsCount; Index++) {\r
973 if (BbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) {\r
974 continue;\r
975 }\r
976\r
977 switch (BbsTable[Index].DeviceType) {\r
978 case BBS_FLOPPY:\r
979 FDCount++;\r
980 break;\r
981\r
982 case BBS_HARDDISK:\r
983 HDCount++;\r
984 break;\r
985\r
986 case BBS_CDROM:\r
987 CDCount++;\r
988 break;\r
989\r
990 case BBS_EMBED_NETWORK:\r
991 NETCount++;\r
992 break;\r
993\r
994 case BBS_BEV_DEVICE:\r
995 BEVCount++;\r
996 break;\r
997\r
998 default:\r
999 break;\r
1000 }\r
1001 }\r
1002\r
1003 TotalSize += (HeaderSize + sizeof (UINT16) * FDCount);\r
1004 TotalSize += (HeaderSize + sizeof (UINT16) * HDCount);\r
1005 TotalSize += (HeaderSize + sizeof (UINT16) * CDCount);\r
1006 TotalSize += (HeaderSize + sizeof (UINT16) * NETCount);\r
1007 TotalSize += (HeaderSize + sizeof (UINT16) * BEVCount);\r
1008\r
1009 //\r
1010 // Create buffer to hold all boot device order\r
1011 //\r
1012 DevOrder = AllocateZeroPool (TotalSize);\r
1013 if (NULL == DevOrder) {\r
1014 return EFI_OUT_OF_RESOURCES;\r
1015 }\r
1016 DevOrderPtr = DevOrder;\r
1017\r
1018 DevOrderPtr->BbsType = BBS_FLOPPY;\r
1019 DevOrderPtr->Length = (UINT16) (sizeof (DevOrderPtr->Length) + FDCount * sizeof (UINT16));\r
1020 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_FLOPPY, BbsCount, DevOrderPtr->Data);\r
1021\r
1022 DevOrderPtr->BbsType = BBS_HARDDISK;\r
1023 DevOrderPtr->Length = (UINT16) (sizeof (UINT16) + HDCount * sizeof (UINT16));\r
1024 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_HARDDISK, BbsCount, DevOrderPtr->Data);\r
1025 \r
1026 DevOrderPtr->BbsType = BBS_CDROM;\r
1027 DevOrderPtr->Length = (UINT16) (sizeof (UINT16) + CDCount * sizeof (UINT16));\r
1028 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_CDROM, BbsCount, DevOrderPtr->Data);\r
1029 \r
1030 DevOrderPtr->BbsType = BBS_EMBED_NETWORK;\r
1031 DevOrderPtr->Length = (UINT16) (sizeof (UINT16) + NETCount * sizeof (UINT16));\r
1032 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_EMBED_NETWORK, BbsCount, DevOrderPtr->Data);\r
1033\r
1034 DevOrderPtr->BbsType = BBS_BEV_DEVICE;\r
1035 DevOrderPtr->Length = (UINT16) (sizeof (UINT16) + BEVCount * sizeof (UINT16));\r
1036 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_BEV_DEVICE, BbsCount, DevOrderPtr->Data);\r
1037\r
1038 ASSERT (TotalSize == (UINTN) ((UINT8 *) DevOrderPtr - (UINT8 *) DevOrder));\r
1039\r
1040 //\r
1041 // Save device order for legacy boot device to variable.\r
1042 //\r
1043 Status = gRT->SetVariable (\r
1044 VAR_LEGACY_DEV_ORDER,\r
1045 &gEfiLegacyDevOrderVariableGuid,\r
1046 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1047 TotalSize,\r
1048 DevOrder\r
1049 );\r
1050 FreePool (DevOrder);\r
1051\r
1052 return Status;\r
1053}\r
1054\r
1055/**\r
1056 Add the legacy boot devices from BBS table into \r
1057 the legacy device boot order.\r
1058\r
1059 @retval EFI_SUCCESS The boot devices are added successfully.\r
1060 @retval EFI_NOT_FOUND The legacy boot devices are not found.\r
1061 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.\r
1062 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable\r
1063 because of hardware error.\r
1064**/\r
1065EFI_STATUS\r
1066EFIAPI\r
1067BdsUpdateLegacyDevOrder (\r
1068 VOID\r
1069 )\r
1070{\r
1071 LEGACY_DEV_ORDER_ENTRY *DevOrder;\r
1072 LEGACY_DEV_ORDER_ENTRY *NewDevOrder;\r
1073 LEGACY_DEV_ORDER_ENTRY *Ptr;\r
1074 LEGACY_DEV_ORDER_ENTRY *NewPtr;\r
1075 UINTN DevOrderSize;\r
1076 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
1077 EFI_STATUS Status;\r
1078 UINT16 HddCount;\r
1079 UINT16 BbsCount;\r
1080 HDD_INFO *LocalHddInfo;\r
1081 BBS_TABLE *LocalBbsTable;\r
1082 UINTN Index;\r
1083 UINTN Index2;\r
1084 UINTN *Idx;\r
1085 UINTN FDCount;\r
1086 UINTN HDCount;\r
1087 UINTN CDCount;\r
1088 UINTN NETCount;\r
1089 UINTN BEVCount;\r
1090 UINTN TotalSize;\r
1091 UINTN HeaderSize;\r
1092 UINT16 *NewFDPtr;\r
1093 UINT16 *NewHDPtr;\r
1094 UINT16 *NewCDPtr;\r
1095 UINT16 *NewNETPtr;\r
1096 UINT16 *NewBEVPtr;\r
1097 UINT16 *NewDevPtr;\r
1098 UINTN FDIndex;\r
1099 UINTN HDIndex;\r
1100 UINTN CDIndex;\r
1101 UINTN NETIndex;\r
1102 UINTN BEVIndex;\r
1103\r
1104 Idx = NULL;\r
1105 FDCount = 0;\r
1106 HDCount = 0;\r
1107 CDCount = 0;\r
1108 NETCount = 0;\r
1109 BEVCount = 0;\r
1110 TotalSize = 0;\r
1111 HeaderSize = sizeof (BBS_TYPE) + sizeof (UINT16);\r
1112 FDIndex = 0;\r
1113 HDIndex = 0;\r
1114 CDIndex = 0;\r
1115 NETIndex = 0;\r
1116 BEVIndex = 0;\r
1117 NewDevPtr = NULL;\r
1118\r
1119 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);\r
1120 if (EFI_ERROR (Status)) {\r
1121 return Status;\r
1122 }\r
1123\r
1124 Status = LegacyBios->GetBbsInfo (\r
1125 LegacyBios,\r
1126 &HddCount,\r
1127 &LocalHddInfo,\r
1128 &BbsCount,\r
1129 &LocalBbsTable\r
1130 );\r
1131 if (EFI_ERROR (Status)) {\r
1132 return Status;\r
1133 }\r
1134\r
1135 DevOrder = BdsLibGetVariableAndSize (\r
1136 VAR_LEGACY_DEV_ORDER,\r
1137 &gEfiLegacyDevOrderVariableGuid,\r
1138 &DevOrderSize\r
1139 );\r
1140 if (NULL == DevOrder) {\r
1141 return BdsCreateDevOrder (LocalBbsTable, BbsCount);\r
1142 }\r
1143 //\r
1144 // First we figure out how many boot devices with same device type respectively\r
1145 //\r
1146 for (Index = 0; Index < BbsCount; Index++) {\r
1147 if ((LocalBbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) ||\r
1148 (LocalBbsTable[Index].BootPriority == BBS_DO_NOT_BOOT_FROM)\r
1149 ) {\r
1150 continue;\r
1151 }\r
1152\r
1153 switch (LocalBbsTable[Index].DeviceType) {\r
1154 case BBS_FLOPPY:\r
1155 FDCount++;\r
1156 break;\r
1157\r
1158 case BBS_HARDDISK:\r
1159 HDCount++;\r
1160 break;\r
1161\r
1162 case BBS_CDROM:\r
1163 CDCount++;\r
1164 break;\r
1165\r
1166 case BBS_EMBED_NETWORK:\r
1167 NETCount++;\r
1168 break;\r
1169\r
1170 case BBS_BEV_DEVICE:\r
1171 BEVCount++;\r
1172 break;\r
1173\r
1174 default:\r
1175 break;\r
1176 }\r
1177 }\r
1178\r
1179 TotalSize += (HeaderSize + FDCount * sizeof (UINT16));\r
1180 TotalSize += (HeaderSize + HDCount * sizeof (UINT16));\r
1181 TotalSize += (HeaderSize + CDCount * sizeof (UINT16));\r
1182 TotalSize += (HeaderSize + NETCount * sizeof (UINT16));\r
1183 TotalSize += (HeaderSize + BEVCount * sizeof (UINT16));\r
1184\r
1185 NewDevOrder = AllocateZeroPool (TotalSize);\r
1186 if (NULL == NewDevOrder) {\r
1187 return EFI_OUT_OF_RESOURCES;\r
1188 }\r
1189\r
1190\r
1191\r
1192 //\r
1193 // copy FD\r
1194 //\r
1195 Ptr = DevOrder;\r
1196 NewPtr = NewDevOrder;\r
1197 NewPtr->BbsType = Ptr->BbsType;\r
1198 NewPtr->Length = (UINT16) (sizeof (UINT16) + FDCount * sizeof (UINT16));\r
1199 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {\r
1200 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||\r
1201 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||\r
1202 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_FLOPPY\r
1203 ) {\r
1204 continue;\r
1205 }\r
1206\r
1207 NewPtr->Data[FDIndex] = Ptr->Data[Index];\r
1208 FDIndex++;\r
1209 }\r
1210 NewFDPtr = NewPtr->Data;\r
1211\r
1212 //\r
1213 // copy HD\r
1214 //\r
1215 Ptr = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);\r
1216 NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);\r
1217 NewPtr->BbsType = Ptr->BbsType;\r
1218 NewPtr->Length = (UINT16) (sizeof (UINT16) + HDCount * sizeof (UINT16));\r
1219 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {\r
1220 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||\r
1221 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||\r
1222 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||\r
1223 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_HARDDISK\r
1224 ) {\r
1225 continue;\r
1226 }\r
1227\r
1228 NewPtr->Data[HDIndex] = Ptr->Data[Index];\r
1229 HDIndex++;\r
1230 }\r
1231 NewHDPtr = NewPtr->Data;\r
1232\r
1233 //\r
1234 // copy CD\r
1235 //\r
1236 Ptr = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);\r
1237 NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);\r
1238 NewPtr->BbsType = Ptr->BbsType;\r
1239 NewPtr->Length = (UINT16) (sizeof (UINT16) + CDCount * sizeof (UINT16));\r
1240 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {\r
1241 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||\r
1242 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||\r
1243 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||\r
1244 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_CDROM\r
1245 ) {\r
1246 continue;\r
1247 }\r
1248\r
1249 NewPtr->Data[CDIndex] = Ptr->Data[Index];\r
1250 CDIndex++;\r
1251 }\r
1252 NewCDPtr = NewPtr->Data;\r
1253\r
1254 //\r
1255 // copy NET\r
1256 //\r
1257 Ptr = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);\r
1258 NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);\r
1259 NewPtr->BbsType = Ptr->BbsType;\r
1260 NewPtr->Length = (UINT16) (sizeof (UINT16) + NETCount * sizeof (UINT16));\r
1261 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {\r
1262 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||\r
1263 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||\r
1264 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||\r
1265 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_EMBED_NETWORK\r
1266 ) {\r
1267 continue;\r
1268 }\r
1269\r
1270 NewPtr->Data[NETIndex] = Ptr->Data[Index];\r
1271 NETIndex++;\r
1272 }\r
1273 NewNETPtr = NewPtr->Data;\r
1274 \r
1275 //\r
1276 // copy BEV\r
1277 //\r
1278 Ptr = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);\r
1279 NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);\r
1280 NewPtr->BbsType = Ptr->BbsType;\r
1281 NewPtr->Length = (UINT16) (sizeof (UINT16) + BEVCount * sizeof (UINT16));\r
1282 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {\r
1283 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||\r
1284 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||\r
1285 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||\r
1286 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_BEV_DEVICE\r
1287 ) {\r
1288 continue;\r
1289 }\r
1290\r
1291 NewPtr->Data[BEVIndex] = Ptr->Data[Index];\r
1292 BEVIndex++;\r
1293 }\r
1294 NewBEVPtr = NewPtr->Data;\r
1295\r
1296 for (Index = 0; Index < BbsCount; Index++) {\r
1297 if ((LocalBbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) ||\r
1298 (LocalBbsTable[Index].BootPriority == BBS_DO_NOT_BOOT_FROM)\r
1299 ) {\r
1300 continue;\r
1301 }\r
1302\r
1303 switch (LocalBbsTable[Index].DeviceType) {\r
1304 case BBS_FLOPPY:\r
1305 Idx = &FDIndex;\r
1306 NewDevPtr = NewFDPtr;\r
1307 break;\r
1308\r
1309 case BBS_HARDDISK:\r
1310 Idx = &HDIndex;\r
1311 NewDevPtr = NewHDPtr;\r
1312 break;\r
1313\r
1314 case BBS_CDROM:\r
1315 Idx = &CDIndex;\r
1316 NewDevPtr = NewCDPtr;\r
1317 break;\r
1318\r
1319 case BBS_EMBED_NETWORK:\r
1320 Idx = &NETIndex;\r
1321 NewDevPtr = NewNETPtr;\r
1322 break;\r
1323\r
1324 case BBS_BEV_DEVICE:\r
1325 Idx = &BEVIndex;\r
1326 NewDevPtr = NewBEVPtr;\r
1327 break;\r
1328\r
1329 default:\r
1330 Idx = NULL;\r
1331 break;\r
1332 }\r
1333 //\r
1334 // at this point we have copied those valid indexes to new buffer\r
1335 // and we should check if there is any new appeared boot device\r
1336 //\r
1337 if (Idx != NULL) {\r
1338 for (Index2 = 0; Index2 < *Idx; Index2++) {\r
1339 if ((NewDevPtr[Index2] & 0xFF) == (UINT16) Index) {\r
1340 break;\r
1341 }\r
1342 }\r
1343\r
1344 if (Index2 == *Idx) {\r
1345 //\r
1346 // Index2 == *Idx means we didn't find Index\r
1347 // so Index is a new appeared device's index in BBS table\r
1348 // insert it before disabled indexes.\r
1349 //\r
1350 for (Index2 = 0; Index2 < *Idx; Index2++) {\r
1351 if ((NewDevPtr[Index2] & 0xFF00) == 0xFF00) {\r
1352 break;\r
1353 }\r
1354 }\r
1355 CopyMem (&NewDevPtr[Index2 + 1], &NewDevPtr[Index2], (*Idx - Index2) * sizeof (UINT16));\r
1356 NewDevPtr[Index2] = (UINT16) (Index & 0xFF);\r
1357 (*Idx)++;\r
1358 }\r
1359 }\r
1360 }\r
1361\r
1362 FreePool (DevOrder);\r
1363\r
1364 Status = gRT->SetVariable (\r
1365 VAR_LEGACY_DEV_ORDER,\r
1366 &gEfiLegacyDevOrderVariableGuid,\r
1367 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1368 TotalSize,\r
1369 NewDevOrder\r
1370 );\r
1371 FreePool (NewDevOrder);\r
1372\r
1373 return Status;\r
1374}\r
1375\r
1376/**\r
1377 Set Boot Priority for specified device type.\r
1378\r
1379 @param DeviceType The device type.\r
1380 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.\r
1381 @param LocalBbsTable The BBS table.\r
1382 @param Priority The prority table.\r
1383\r
1384 @retval EFI_SUCCESS The function completes successfully.\r
1385 @retval EFI_NOT_FOUND Failed to find device.\r
1386 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.\r
1387\r
1388**/\r
1389EFI_STATUS\r
1390BdsSetBootPriority4SameTypeDev (\r
1391 IN UINT16 DeviceType,\r
1392 IN UINTN BbsIndex,\r
1393 IN OUT BBS_TABLE *LocalBbsTable,\r
1394 IN OUT UINT16 *Priority\r
1395 )\r
1396{\r
1397 LEGACY_DEV_ORDER_ENTRY *DevOrder;\r
1398 LEGACY_DEV_ORDER_ENTRY *DevOrderPtr;\r
1399 UINTN DevOrderSize;\r
1400 UINTN Index;\r
1401\r
1402 DevOrder = BdsLibGetVariableAndSize (\r
1403 VAR_LEGACY_DEV_ORDER,\r
1404 &gEfiLegacyDevOrderVariableGuid,\r
1405 &DevOrderSize\r
1406 );\r
1407 if (NULL == DevOrder) {\r
1408 return EFI_OUT_OF_RESOURCES;\r
1409 }\r
1410\r
1411 DevOrderPtr = DevOrder;\r
1412 while ((UINT8 *) DevOrderPtr < (UINT8 *) DevOrder + DevOrderSize) {\r
1413 if (DevOrderPtr->BbsType == DeviceType) {\r
1414 break;\r
1415 }\r
1416\r
1417 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) ((UINT8 *) DevOrderPtr + sizeof (BBS_TYPE) + DevOrderPtr->Length);\r
1418 }\r
1419\r
1420 if ((UINT8 *) DevOrderPtr >= (UINT8 *) DevOrder + DevOrderSize) {\r
1421 FreePool (DevOrder);\r
1422 return EFI_NOT_FOUND;\r
1423 }\r
1424\r
1425 if (BbsIndex != (UINTN) -1) {\r
1426 LocalBbsTable[BbsIndex].BootPriority = *Priority;\r
1427 (*Priority)++;\r
1428 }\r
1429 //\r
1430 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.\r
1431 //\r
1432 for (Index = 0; Index < DevOrderPtr->Length / sizeof (UINT16) - 1; Index++) {\r
1433 if ((DevOrderPtr->Data[Index] & 0xFF00) == 0xFF00) {\r
1434 //\r
1435 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;\r
1436 //\r
1437 } else if (DevOrderPtr->Data[Index] != BbsIndex) {\r
1438 LocalBbsTable[DevOrderPtr->Data[Index]].BootPriority = *Priority;\r
1439 (*Priority)++;\r
1440 }\r
1441 }\r
1442\r
1443 FreePool (DevOrder);\r
1444 return EFI_SUCCESS;\r
1445}\r
1446\r
1447/**\r
1448 Print the BBS Table.\r
1449\r
1450 @param LocalBbsTable The BBS table.\r
1451 @param BbsCount The count of entry in BBS table.\r
1452**/\r
1453VOID\r
1454PrintBbsTable (\r
1455 IN BBS_TABLE *LocalBbsTable,\r
1456 IN UINT16 BbsCount\r
1457 )\r
1458{\r
1459 UINT16 Idx;\r
1460\r
1461 DEBUG ((DEBUG_ERROR, "\n"));\r
1462 DEBUG ((DEBUG_ERROR, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));\r
1463 DEBUG ((DEBUG_ERROR, "=============================================\n"));\r
1464 for (Idx = 0; Idx < BbsCount; Idx++) {\r
1465 if ((LocalBbsTable[Idx].BootPriority == BBS_IGNORE_ENTRY) ||\r
1466 (LocalBbsTable[Idx].BootPriority == BBS_DO_NOT_BOOT_FROM) ||\r
1467 (LocalBbsTable[Idx].BootPriority == BBS_LOWEST_PRIORITY)\r
1468 ) {\r
1469 continue;\r
1470 }\r
1471\r
1472 DEBUG (\r
1473 (DEBUG_ERROR,\r
1474 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",\r
1475 (UINTN) Idx,\r
1476 (UINTN) LocalBbsTable[Idx].BootPriority,\r
1477 (UINTN) LocalBbsTable[Idx].Bus,\r
1478 (UINTN) LocalBbsTable[Idx].Device,\r
1479 (UINTN) LocalBbsTable[Idx].Function,\r
1480 (UINTN) LocalBbsTable[Idx].Class,\r
1481 (UINTN) LocalBbsTable[Idx].SubClass,\r
1482 (UINTN) LocalBbsTable[Idx].DeviceType,\r
1483 (UINTN) * (UINT16 *) &LocalBbsTable[Idx].StatusFlags,\r
1484 (UINTN) LocalBbsTable[Idx].BootHandlerSegment,\r
1485 (UINTN) LocalBbsTable[Idx].BootHandlerOffset,\r
1486 (UINTN) ((LocalBbsTable[Idx].MfgStringSegment << 4) + LocalBbsTable[Idx].MfgStringOffset),\r
1487 (UINTN) ((LocalBbsTable[Idx].DescStringSegment << 4) + LocalBbsTable[Idx].DescStringOffset))\r
1488 );\r
1489 }\r
1490\r
1491 DEBUG ((DEBUG_ERROR, "\n"));\r
1492}\r
1493\r
1494/**\r
1495 Set the boot priority for BBS entries based on boot option entry and boot order.\r
1496\r
1497 @param Entry The boot option is to be checked for refresh BBS table.\r
1498 \r
1499 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.\r
1500 @retval EFI_NOT_FOUND BBS entries can't be found.\r
1501 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.\r
1502**/\r
1503EFI_STATUS\r
1504EFIAPI\r
1505BdsRefreshBbsTableForBoot (\r
1506 IN BDS_COMMON_OPTION *Entry\r
1507 )\r
1508{\r
1509 EFI_STATUS Status;\r
1510 UINT16 BbsIndex;\r
1511 UINT16 HddCount;\r
1512 UINT16 BbsCount;\r
1513 HDD_INFO *LocalHddInfo;\r
1514 BBS_TABLE *LocalBbsTable;\r
1515 UINT16 DevType;\r
1516 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
1517 UINTN Index;\r
1518 UINT16 Priority;\r
1519 UINT16 *BootOrder;\r
1520 UINTN BootOrderSize;\r
1521 UINT8 *BootOptionVar;\r
1522 UINTN BootOptionSize;\r
1523 CHAR16 BootOption[9];\r
1524 UINT8 *Ptr;\r
1525 UINT16 DevPathLen;\r
1526 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
1527 UINT16 *DeviceType;\r
1528 UINTN DeviceTypeCount;\r
1529 UINTN DeviceTypeIndex;\r
1530\r
1531 HddCount = 0;\r
1532 BbsCount = 0;\r
1533 LocalHddInfo = NULL;\r
1534 LocalBbsTable = NULL;\r
1535 DevType = BBS_UNKNOWN;\r
1536\r
1537 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);\r
1538 if (EFI_ERROR (Status)) {\r
1539 return Status;\r
1540 }\r
1541\r
1542 LegacyBios->GetBbsInfo (\r
1543 LegacyBios,\r
1544 &HddCount,\r
1545 &LocalHddInfo,\r
1546 &BbsCount,\r
1547 &LocalBbsTable\r
1548 );\r
1549 //\r
1550 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY\r
1551 // We will set them according to the settings setup by user\r
1552 //\r
1553 for (Index = 0; Index < BbsCount; Index++) {\r
1554 if (!((BBS_IGNORE_ENTRY == LocalBbsTable[Index].BootPriority) ||\r
1555 (BBS_DO_NOT_BOOT_FROM == LocalBbsTable[Index].BootPriority) ||\r
1556 (BBS_LOWEST_PRIORITY == LocalBbsTable[Index].BootPriority))) {\r
1557 LocalBbsTable[Index].BootPriority = BBS_UNPRIORITIZED_ENTRY;\r
1558 }\r
1559 }\r
1560 //\r
1561 // boot priority always starts at 0\r
1562 //\r
1563 Priority = 0;\r
1564 if (Entry->LoadOptionsSize == sizeof (BBS_TABLE) + sizeof (UINT16)) {\r
1565 //\r
1566 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.\r
1567 //\r
1568 DevType = ((BBS_TABLE *) Entry->LoadOptions)->DeviceType;\r
1569 BbsIndex = *(UINT16 *) ((BBS_TABLE *) Entry->LoadOptions + 1);\r
1570 Status = BdsSetBootPriority4SameTypeDev (\r
1571 DevType,\r
1572 BbsIndex,\r
1573 LocalBbsTable,\r
1574 &Priority\r
1575 );\r
1576 if (EFI_ERROR (Status)) {\r
1577 return Status;\r
1578 }\r
1579 }\r
1580 //\r
1581 // we have to set the boot priority for other BBS entries with different device types\r
1582 //\r
1583 BootOrder = BdsLibGetVariableAndSize (\r
1584 L"BootOrder",\r
1585 &gEfiGlobalVariableGuid,\r
1586 &BootOrderSize\r
1587 );\r
1588 DeviceType = AllocatePool (BootOrderSize + sizeof (UINT16));\r
1589 ASSERT (DeviceType != NULL);\r
1590\r
1591 DeviceType[0] = DevType;\r
1592 DeviceTypeCount = 1;\r
1593 for (Index = 0; ((BootOrder != NULL) && (Index < BootOrderSize / sizeof (UINT16))); Index++) {\r
1594 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
1595 BootOptionVar = BdsLibGetVariableAndSize (\r
1596 BootOption,\r
1597 &gEfiGlobalVariableGuid,\r
1598 &BootOptionSize\r
1599 );\r
1600 if (NULL == BootOptionVar) {\r
1601 continue;\r
1602 }\r
1603\r
1604 Ptr = BootOptionVar;\r
1605\r
1606 Ptr += sizeof (UINT32);\r
1607 DevPathLen = *(UINT16 *) Ptr;\r
1608 Ptr += sizeof (UINT16);\r
1609 Ptr += StrSize ((UINT16 *) Ptr);\r
1610 DevPath = (EFI_DEVICE_PATH_PROTOCOL *) Ptr;\r
1611 if (BBS_DEVICE_PATH != DevPath->Type || BBS_BBS_DP != DevPath->SubType) {\r
1612 FreePool (BootOptionVar);\r
1613 continue;\r
1614 }\r
1615\r
1616 Ptr += DevPathLen;\r
1617 DevType = ((BBS_TABLE *) Ptr)->DeviceType;\r
1618 for (DeviceTypeIndex = 0; DeviceTypeIndex < DeviceTypeCount; DeviceTypeIndex++) {\r
1619 if (DeviceType[DeviceTypeIndex] == DevType) {\r
1620 break;\r
1621 }\r
1622 }\r
1623 if (DeviceTypeIndex < DeviceTypeCount) {\r
1624 //\r
1625 // We don't want to process twice for a device type\r
1626 //\r
1627 FreePool (BootOptionVar);\r
1628 continue;\r
1629 }\r
1630\r
1631 DeviceType[DeviceTypeCount] = DevType;\r
1632 DeviceTypeCount++;\r
1633\r
1634 Status = BdsSetBootPriority4SameTypeDev (\r
1635 DevType,\r
1636 (UINTN) -1,\r
1637 LocalBbsTable,\r
1638 &Priority\r
1639 );\r
1640 FreePool (BootOptionVar);\r
1641 if (EFI_ERROR (Status)) {\r
1642 break;\r
1643 }\r
1644 }\r
1645\r
1646 if (BootOrder != NULL) {\r
1647 FreePool (BootOrder);\r
1648 }\r
1649\r
1650 DEBUG_CODE_BEGIN();\r
1651 PrintBbsTable (LocalBbsTable, BbsCount);\r
1652 DEBUG_CODE_END();\r
1653\r
1654 return Status;\r
1655}\r
5c08e117 1656\r
1657/**\r
1658 Boot the legacy system with the boot option\r
1659\r
1660 @param Option The legacy boot option which have BBS device path\r
1661\r
1662 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support\r
1663 legacy boot.\r
1664 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().\r
1665\r
1666**/\r
1667EFI_STATUS\r
1668BdsLibDoLegacyBoot (\r
1669 IN BDS_COMMON_OPTION *Option\r
1670 )\r
1671{\r
1672 EFI_STATUS Status;\r
1673 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
26c0ba77 1674 EFI_EVENT LegacyBootEvent;\r
5c08e117 1675\r
1676 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);\r
1677 if (EFI_ERROR (Status)) {\r
1678 //\r
1679 // If no LegacyBios protocol we do not support legacy boot\r
1680 //\r
1681 return EFI_UNSUPPORTED;\r
1682 }\r
1683 //\r
1684 // Notes: if we separate the int 19, then we don't need to refresh BBS\r
1685 //\r
1686 BdsRefreshBbsTableForBoot (Option);\r
1687\r
1688 //\r
cd6a3b15 1689 // Write boot to OS performance data for legacy boot.\r
5c08e117 1690 //\r
1691 PERF_CODE (\r
26c0ba77
SZ
1692 //\r
1693 // Create an event to be signalled when Legacy Boot occurs to write performance data.\r
1694 //\r
1695 Status = EfiCreateEventLegacyBootEx(\r
1696 TPL_NOTIFY,\r
1697 WriteBootToOsPerformanceData,\r
1698 NULL, \r
1699 &LegacyBootEvent\r
1700 );\r
1701 ASSERT_EFI_ERROR (Status);\r
5c08e117 1702 );\r
1703\r
1704 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Legacy Boot: %S\n", Option->Description));\r
1705 return LegacyBios->LegacyBoot (\r
1706 LegacyBios,\r
1707 (BBS_BBS_DEVICE_PATH *) Option->DevicePath,\r
1708 Option->LoadOptionsSize,\r
1709 Option->LoadOptions\r
1710 );\r
1711}\r
1712\r
3d7decbc 1713/**\r
1714 Internal function to check if the input boot option is a valid EFI NV Boot####.\r
1715\r
1716 @param OptionToCheck Boot option to be checked.\r
1717\r
1718 @retval TRUE This boot option matches a valid EFI NV Boot####.\r
1719 @retval FALSE If not.\r
128efbbc 1720\r
3d7decbc 1721**/\r
3d7decbc 1722BOOLEAN\r
1723IsBootOptionValidNVVarialbe (\r
1724 IN BDS_COMMON_OPTION *OptionToCheck\r
1725 )\r
1726{\r
1727 LIST_ENTRY TempList;\r
1728 BDS_COMMON_OPTION *BootOption;\r
1729 BOOLEAN Valid;\r
1730 CHAR16 OptionName[20];\r
1731\r
1732 Valid = FALSE;\r
1733\r
1734 InitializeListHead (&TempList);\r
1735 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionToCheck->BootCurrent);\r
5c08e117 1736\r
3d7decbc 1737 BootOption = BdsLibVariableToOption (&TempList, OptionName);\r
1738 if (BootOption == NULL) {\r
1739 return FALSE;\r
1740 }\r
1741\r
1742 //\r
128efbbc 1743 // If the Boot Option Number and Device Path matches, OptionToCheck matches a\r
3d7decbc 1744 // valid EFI NV Boot####.\r
1745 //\r
1746 if ((OptionToCheck->BootCurrent == BootOption->BootCurrent) &&\r
1747 (CompareMem (OptionToCheck->DevicePath, BootOption->DevicePath, GetDevicePathSize (OptionToCheck->DevicePath)) == 0))\r
1748 {\r
1749 Valid = TRUE;\r
1750 }\r
1751\r
1752 FreePool (BootOption);\r
128efbbc 1753\r
3d7decbc 1754 return Valid;\r
1755}\r
7389fdd0 1756\r
1757/**\r
1758 Check whether a USB device match the specified USB Class device path. This\r
1759 function follows "Load Option Processing" behavior in UEFI specification.\r
1760\r
1761 @param UsbIo USB I/O protocol associated with the USB device.\r
1762 @param UsbClass The USB Class device path to match.\r
1763\r
1764 @retval TRUE The USB device match the USB Class device path.\r
1765 @retval FALSE The USB device does not match the USB Class device path.\r
1766\r
1767**/\r
1768BOOLEAN\r
1769BdsMatchUsbClass (\r
1770 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
1771 IN USB_CLASS_DEVICE_PATH *UsbClass\r
1772 )\r
1773{\r
1774 EFI_STATUS Status;\r
1775 EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r
1776 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;\r
1777 UINT8 DeviceClass;\r
1778 UINT8 DeviceSubClass;\r
1779 UINT8 DeviceProtocol;\r
1780\r
1781 if ((DevicePathType (UsbClass) != MESSAGING_DEVICE_PATH) ||\r
1782 (DevicePathSubType (UsbClass) != MSG_USB_CLASS_DP)){\r
1783 return FALSE;\r
1784 }\r
1785\r
1786 //\r
1787 // Check Vendor Id and Product Id.\r
1788 //\r
1789 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);\r
1790 if (EFI_ERROR (Status)) {\r
1791 return FALSE;\r
1792 }\r
1793\r
1794 if ((UsbClass->VendorId != 0xffff) &&\r
1795 (UsbClass->VendorId != DevDesc.IdVendor)) {\r
1796 return FALSE;\r
1797 }\r
1798\r
1799 if ((UsbClass->ProductId != 0xffff) &&\r
1800 (UsbClass->ProductId != DevDesc.IdProduct)) {\r
1801 return FALSE;\r
1802 }\r
1803\r
1804 DeviceClass = DevDesc.DeviceClass;\r
1805 DeviceSubClass = DevDesc.DeviceSubClass;\r
1806 DeviceProtocol = DevDesc.DeviceProtocol;\r
1807 if (DeviceClass == 0) {\r
1808 //\r
1809 // If Class in Device Descriptor is set to 0, use the Class, SubClass and\r
1810 // Protocol in Interface Descriptor instead.\r
1811 //\r
1812 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);\r
1813 if (EFI_ERROR (Status)) {\r
1814 return FALSE;\r
1815 }\r
1816\r
1817 DeviceClass = IfDesc.InterfaceClass;\r
1818 DeviceSubClass = IfDesc.InterfaceSubClass;\r
1819 DeviceProtocol = IfDesc.InterfaceProtocol;\r
1820 }\r
1821\r
1822 //\r
1823 // Check Class, SubClass and Protocol.\r
1824 //\r
1825 if ((UsbClass->DeviceClass != 0xff) &&\r
1826 (UsbClass->DeviceClass != DeviceClass)) {\r
1827 return FALSE;\r
1828 }\r
1829\r
1830 if ((UsbClass->DeviceSubClass != 0xff) &&\r
1831 (UsbClass->DeviceSubClass != DeviceSubClass)) {\r
1832 return FALSE;\r
1833 }\r
1834\r
1835 if ((UsbClass->DeviceProtocol != 0xff) &&\r
1836 (UsbClass->DeviceProtocol != DeviceProtocol)) {\r
1837 return FALSE;\r
1838 }\r
1839\r
1840 return TRUE;\r
1841}\r
1842\r
1843/**\r
1844 Check whether a USB device match the specified USB WWID device path. This\r
1845 function follows "Load Option Processing" behavior in UEFI specification.\r
1846\r
1847 @param UsbIo USB I/O protocol associated with the USB device.\r
1848 @param UsbWwid The USB WWID device path to match.\r
1849\r
1850 @retval TRUE The USB device match the USB WWID device path.\r
1851 @retval FALSE The USB device does not match the USB WWID device path.\r
1852\r
1853**/\r
1854BOOLEAN\r
1855BdsMatchUsbWwid (\r
1856 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
1857 IN USB_WWID_DEVICE_PATH *UsbWwid\r
1858 )\r
1859{\r
1860 EFI_STATUS Status;\r
1861 EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r
1862 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;\r
1863 UINT16 *LangIdTable;\r
1864 UINT16 TableSize;\r
1865 UINT16 Index;\r
1866 CHAR16 *CompareStr;\r
1867 UINTN CompareLen;\r
1868 CHAR16 *SerialNumberStr;\r
1869 UINTN Length;\r
1870\r
1871 if ((DevicePathType (UsbWwid) != MESSAGING_DEVICE_PATH) ||\r
1872 (DevicePathSubType (UsbWwid) != MSG_USB_WWID_DP )){\r
1873 return FALSE;\r
1874 }\r
1875\r
1876 //\r
1877 // Check Vendor Id and Product Id.\r
1878 //\r
1879 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);\r
1880 if (EFI_ERROR (Status)) {\r
1881 return FALSE;\r
1882 }\r
1883 if ((DevDesc.IdVendor != UsbWwid->VendorId) ||\r
1884 (DevDesc.IdProduct != UsbWwid->ProductId)) {\r
1885 return FALSE;\r
1886 }\r
1887\r
1888 //\r
1889 // Check Interface Number.\r
1890 //\r
1891 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);\r
1892 if (EFI_ERROR (Status)) {\r
1893 return FALSE;\r
1894 }\r
1895 if (IfDesc.InterfaceNumber != UsbWwid->InterfaceNumber) {\r
1896 return FALSE;\r
1897 }\r
1898\r
1899 //\r
1900 // Check Serial Number.\r
1901 //\r
1902 if (DevDesc.StrSerialNumber == 0) {\r
1903 return FALSE;\r
1904 }\r
1905\r
1906 //\r
1907 // Get all supported languages.\r
1908 //\r
1909 TableSize = 0;\r
1910 LangIdTable = NULL;\r
1911 Status = UsbIo->UsbGetSupportedLanguages (UsbIo, &LangIdTable, &TableSize);\r
1912 if (EFI_ERROR (Status) || (TableSize == 0) || (LangIdTable == NULL)) {\r
1913 return FALSE;\r
1914 }\r
1915\r
1916 //\r
1917 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.\r
1918 //\r
1919 CompareStr = (CHAR16 *) (UINTN) (UsbWwid + 1);\r
1920 CompareLen = (DevicePathNodeLength (UsbWwid) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16);\r
1921 if (CompareStr[CompareLen - 1] == L'\0') {\r
1922 CompareLen--;\r
1923 }\r
1924\r
1925 //\r
1926 // Compare serial number in each supported language.\r
1927 //\r
1928 for (Index = 0; Index < TableSize / sizeof (UINT16); Index++) {\r
1929 SerialNumberStr = NULL;\r
1930 Status = UsbIo->UsbGetStringDescriptor (\r
1931 UsbIo,\r
1932 LangIdTable[Index],\r
1933 DevDesc.StrSerialNumber,\r
1934 &SerialNumberStr\r
1935 );\r
1936 if (EFI_ERROR (Status) || (SerialNumberStr == NULL)) {\r
1937 continue;\r
1938 }\r
1939\r
1940 Length = StrLen (SerialNumberStr);\r
1941 if ((Length >= CompareLen) &&\r
1942 (CompareMem (SerialNumberStr + Length - CompareLen, CompareStr, CompareLen * sizeof (CHAR16)) == 0)) {\r
1943 FreePool (SerialNumberStr);\r
1944 return TRUE;\r
1945 }\r
1946\r
1947 FreePool (SerialNumberStr);\r
1948 }\r
1949\r
1950 return FALSE;\r
1951}\r
1952\r
1953/**\r
9972247d 1954 Find a USB device path which match the specified short-form device path start\r
1955 with USB Class or USB WWID device path and load the boot file then return the \r
1956 image handle. If ParentDevicePath is NULL, this function will search in all USB\r
1957 devices of the platform. If ParentDevicePath is not NULL,this function will only\r
1958 search in its child devices.\r
7389fdd0 1959\r
1960 @param ParentDevicePath The device path of the parent.\r
1961 @param ShortFormDevicePath The USB Class or USB WWID device path to match.\r
1962\r
9972247d 1963 @return The image Handle if find load file from specified short-form device path\r
1964 or NULL if not found.\r
7389fdd0 1965\r
1966**/\r
1967EFI_HANDLE *\r
1968BdsFindUsbDevice (\r
1969 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,\r
1970 IN EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath\r
1971 )\r
1972{\r
1973 EFI_STATUS Status;\r
1974 UINTN UsbIoHandleCount;\r
1975 EFI_HANDLE *UsbIoHandleBuffer;\r
1976 EFI_DEVICE_PATH_PROTOCOL *UsbIoDevicePath;\r
1977 EFI_USB_IO_PROTOCOL *UsbIo;\r
1978 UINTN Index;\r
1979 UINTN ParentSize;\r
1980 UINTN Size;\r
9972247d 1981 EFI_HANDLE ImageHandle;\r
1982 EFI_HANDLE Handle;\r
1983 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r
1984 EFI_DEVICE_PATH_PROTOCOL *NextDevicePath;\r
1985\r
1986 FullDevicePath = NULL;\r
1987 ImageHandle = NULL;\r
7389fdd0 1988\r
1989 //\r
1990 // Get all UsbIo Handles.\r
1991 //\r
1992 UsbIoHandleCount = 0;\r
1993 UsbIoHandleBuffer = NULL;\r
1994 Status = gBS->LocateHandleBuffer (\r
1995 ByProtocol,\r
1996 &gEfiUsbIoProtocolGuid,\r
1997 NULL,\r
1998 &UsbIoHandleCount,\r
1999 &UsbIoHandleBuffer\r
2000 );\r
2001 if (EFI_ERROR (Status) || (UsbIoHandleCount == 0) || (UsbIoHandleBuffer == NULL)) {\r
2002 return NULL;\r
2003 }\r
2004\r
7389fdd0 2005 ParentSize = (ParentDevicePath == NULL) ? 0 : GetDevicePathSize (ParentDevicePath);\r
2006 for (Index = 0; Index < UsbIoHandleCount; Index++) {\r
2007 //\r
2008 // Get the Usb IO interface.\r
2009 //\r
2010 Status = gBS->HandleProtocol(\r
2011 UsbIoHandleBuffer[Index],\r
2012 &gEfiUsbIoProtocolGuid,\r
2013 (VOID **) &UsbIo\r
2014 );\r
2015 if (EFI_ERROR (Status)) {\r
2016 continue;\r
2017 }\r
2018\r
9972247d 2019 UsbIoDevicePath = DevicePathFromHandle (UsbIoHandleBuffer[Index]);\r
2020 if (UsbIoDevicePath == NULL) {\r
2021 continue;\r
2022 }\r
2023\r
7389fdd0 2024 if (ParentDevicePath != NULL) {\r
2025 //\r
2026 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.\r
2027 //\r
7389fdd0 2028 Size = GetDevicePathSize (UsbIoDevicePath);\r
2029 if ((Size < ParentSize) ||\r
2030 (CompareMem (UsbIoDevicePath, ParentDevicePath, ParentSize - END_DEVICE_PATH_LENGTH) != 0)) {\r
2031 continue;\r
2032 }\r
2033 }\r
2034\r
2035 if (BdsMatchUsbClass (UsbIo, (USB_CLASS_DEVICE_PATH *) ShortFormDevicePath) ||\r
2036 BdsMatchUsbWwid (UsbIo, (USB_WWID_DEVICE_PATH *) ShortFormDevicePath)) {\r
9972247d 2037 //\r
2038 // Try to find if there is the boot file in this DevicePath\r
2039 //\r
2040 NextDevicePath = NextDevicePathNode (ShortFormDevicePath);\r
2041 if (!IsDevicePathEnd (NextDevicePath)) {\r
2042 FullDevicePath = AppendDevicePath (UsbIoDevicePath, NextDevicePath);\r
2043 //\r
2044 // Connect the full device path, so that Simple File System protocol\r
2045 // could be installed for this USB device.\r
2046 //\r
2047 BdsLibConnectDevicePath (FullDevicePath);\r
79b7a6a1 2048 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
9972247d 2049 Status = gBS->LoadImage (\r
2050 TRUE,\r
2051 gImageHandle,\r
2052 FullDevicePath,\r
2053 NULL,\r
2054 0,\r
2055 &ImageHandle\r
2056 );\r
2057 FreePool (FullDevicePath);\r
2058 } else {\r
2059 FullDevicePath = UsbIoDevicePath;\r
2060 Status = EFI_NOT_FOUND;\r
2061 }\r
2062\r
2063 //\r
2064 // If we didn't find an image directly, we need to try as if it is a removable device boot option\r
2065 // and load the image according to the default boot behavior for removable device.\r
2066 //\r
2067 if (EFI_ERROR (Status)) {\r
2068 //\r
2069 // check if there is a bootable removable media could be found in this device path ,\r
2070 // and get the bootable media handle\r
2071 //\r
2072 Handle = BdsLibGetBootableHandle(UsbIoDevicePath);\r
2073 if (Handle == NULL) {\r
2074 continue;\r
2075 }\r
2076 //\r
2077 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
2078 // machinename is ia32, ia64, x64, ...\r
2079 //\r
2080 FullDevicePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
2081 if (FullDevicePath != NULL) {\r
79b7a6a1 2082 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
9972247d 2083 Status = gBS->LoadImage (\r
2084 TRUE,\r
2085 gImageHandle,\r
2086 FullDevicePath,\r
2087 NULL,\r
2088 0,\r
2089 &ImageHandle\r
2090 );\r
2091 if (EFI_ERROR (Status)) {\r
2092 //\r
2093 // The DevicePath failed, and it's not a valid\r
2094 // removable media device.\r
2095 //\r
2096 continue;\r
2097 }\r
2098 } else {\r
2099 continue;\r
2100 }\r
2101 }\r
7389fdd0 2102 break;\r
2103 }\r
2104 }\r
2105\r
2106 FreePool (UsbIoHandleBuffer);\r
9972247d 2107 return ImageHandle;\r
7389fdd0 2108}\r
2109\r
2110/**\r
2111 Expand USB Class or USB WWID device path node to be full device path of a USB\r
9972247d 2112 device in platform then load the boot file on this full device path and return the \r
2113 image handle.\r
7389fdd0 2114\r
2115 This function support following 4 cases:\r
2116 1) Boot Option device path starts with a USB Class or USB WWID device path,\r
2117 and there is no Media FilePath device path in the end.\r
2118 In this case, it will follow Removable Media Boot Behavior.\r
2119 2) Boot Option device path starts with a USB Class or USB WWID device path,\r
2120 and ended with Media FilePath device path.\r
2121 3) Boot Option device path starts with a full device path to a USB Host Controller,\r
2122 contains a USB Class or USB WWID device path node, while not ended with Media\r
2123 FilePath device path. In this case, it will follow Removable Media Boot Behavior.\r
2124 4) Boot Option device path starts with a full device path to a USB Host Controller,\r
2125 contains a USB Class or USB WWID device path node, and ended with Media\r
2126 FilePath device path.\r
2127\r
2128 @param DevicePath The Boot Option device path.\r
2129\r
9972247d 2130 @return The image handle of boot file, or NULL if there is no boot file found in\r
2131 the specified USB Class or USB WWID device path.\r
7389fdd0 2132\r
2133**/\r
9972247d 2134EFI_HANDLE *\r
7389fdd0 2135BdsExpandUsbShortFormDevicePath (\r
2136 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
2137 )\r
2138{\r
9972247d 2139 EFI_HANDLE *ImageHandle;\r
7389fdd0 2140 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
7389fdd0 2141 EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath;\r
2142\r
2143 //\r
2144 // Search for USB Class or USB WWID device path node.\r
2145 //\r
2146 ShortFormDevicePath = NULL;\r
9972247d 2147 ImageHandle = NULL;\r
2148 TempDevicePath = DevicePath;\r
7389fdd0 2149 while (!IsDevicePathEnd (TempDevicePath)) {\r
2150 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r
2151 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||\r
2152 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {\r
2153 ShortFormDevicePath = TempDevicePath;\r
2154 break;\r
2155 }\r
7389fdd0 2156 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
2157 }\r
2158\r
2159 if (ShortFormDevicePath == NULL) {\r
2160 //\r
2161 // No USB Class or USB WWID device path node found, do nothing.\r
2162 //\r
2163 return NULL;\r
2164 }\r
2165\r
2166 if (ShortFormDevicePath == DevicePath) {\r
2167 //\r
2168 // Boot Option device path starts with USB Class or USB WWID device path.\r
2169 //\r
9972247d 2170 ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);\r
2171 if (ImageHandle == NULL) {\r
7389fdd0 2172 //\r
2173 // Failed to find a match in existing devices, connect the short form USB\r
2174 // device path and try again.\r
2175 //\r
2176 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath);\r
9972247d 2177 ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);\r
7389fdd0 2178 }\r
2179 } else {\r
2180 //\r
2181 // Boot Option device path contains USB Class or USB WWID device path node.\r
2182 //\r
2183\r
2184 //\r
2185 // Prepare the parent device path for search.\r
2186 //\r
2187 TempDevicePath = DuplicateDevicePath (DevicePath);\r
2188 ASSERT (TempDevicePath != NULL);\r
2189 SetDevicePathEndNode (((UINT8 *) TempDevicePath) + ((UINTN) ShortFormDevicePath - (UINTN) DevicePath));\r
2190\r
2191 //\r
9972247d 2192 // The USB Host Controller device path is already in Boot Option device path\r
7389fdd0 2193 // and USB Bus driver already support RemainingDevicePath starts with USB\r
2194 // Class or USB WWID device path, so just search in existing USB devices and\r
2195 // doesn't perform ConnectController here.\r
2196 //\r
9972247d 2197 ImageHandle = BdsFindUsbDevice (TempDevicePath, ShortFormDevicePath);\r
7389fdd0 2198 FreePool (TempDevicePath);\r
2199 }\r
2200\r
9972247d 2201 return ImageHandle;\r
7389fdd0 2202}\r
2203\r
5c08e117 2204/**\r
2205 Process the boot option follow the UEFI specification and\r
2206 special treat the legacy boot option with BBS_DEVICE_PATH.\r
2207\r
2208 @param Option The boot option need to be processed\r
2209 @param DevicePath The device path which describe where to load the\r
2210 boot image or the legacy BBS device path to boot\r
2211 the legacy OS\r
2212 @param ExitDataSize The size of exit data.\r
2213 @param ExitData Data returned when Boot image failed.\r
2214\r
2215 @retval EFI_SUCCESS Boot from the input boot option successfully.\r
2216 @retval EFI_NOT_FOUND If the Device Path is not found in the system\r
2217\r
2218**/\r
2219EFI_STATUS\r
2220EFIAPI\r
2221BdsLibBootViaBootOption (\r
2222 IN BDS_COMMON_OPTION *Option,\r
2223 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
2224 OUT UINTN *ExitDataSize,\r
2225 OUT CHAR16 **ExitData OPTIONAL\r
2226 )\r
2227{\r
2228 EFI_STATUS Status;\r
2df686c6 2229 EFI_STATUS StatusLogo;\r
5c08e117 2230 EFI_HANDLE Handle;\r
2231 EFI_HANDLE ImageHandle;\r
2232 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
2233 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
2234 EFI_DEVICE_PATH_PROTOCOL *WorkingDevicePath;\r
2235 EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;\r
2236 LIST_ENTRY TempBootLists;\r
2df686c6 2237 EFI_BOOT_LOGO_PROTOCOL *BootLogo;\r
5c08e117 2238\r
5c08e117 2239 *ExitDataSize = 0;\r
2240 *ExitData = NULL;\r
2241\r
5c08e117 2242 //\r
2243 // Notes: this code can be remove after the s3 script table\r
2244 // hook on the event EVT_SIGNAL_READY_TO_BOOT or\r
2245 // EVT_SIGNAL_LEGACY_BOOT\r
2246 //\r
2247 Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);\r
2248 if (!EFI_ERROR (Status)) {\r
2249 AcpiS3Save->S3Save (AcpiS3Save, NULL);\r
2250 }\r
2251 //\r
2252 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a\r
2253 // full device path\r
2254 //\r
2255 WorkingDevicePath = NULL;\r
2256 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
2257 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {\r
2258 WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r
2259 (HARDDRIVE_DEVICE_PATH *)DevicePath\r
2260 );\r
2261 if (WorkingDevicePath != NULL) {\r
2262 DevicePath = WorkingDevicePath;\r
2263 }\r
2264 }\r
7389fdd0 2265\r
5c08e117 2266 //\r
2267 // Set Boot Current\r
2268 //\r
3d7decbc 2269 if (IsBootOptionValidNVVarialbe (Option)) {\r
2270 //\r
2271 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.\r
2272 // In this case, "BootCurrent" is not created.\r
2273 // Only create the BootCurrent variable when it points to a valid Boot#### variable.\r
2274 //\r
69fc8f08 2275 SetVariableAndReportStatusCodeOnError (\r
3d7decbc 2276 L"BootCurrent",\r
2277 &gEfiGlobalVariableGuid,\r
2278 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
2279 sizeof (UINT16),\r
2280 &Option->BootCurrent\r
2281 );\r
2282 }\r
5c08e117 2283\r
cb38c322 2284 //\r
2285 // Report Status Code to indicate ReadyToBoot event will be signalled\r
2286 //\r
2287 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT));\r
2288\r
72861c22 2289 //\r
2290 // Signal the EVT_SIGNAL_READY_TO_BOOT event\r
2291 //\r
2292 EfiSignalEventReadyToBoot();\r
2293\r
2294 //\r
2295 // Expand USB Class or USB WWID device path node to be full device path of a USB\r
2296 // device in platform then load the boot file on this full device path and get the\r
2297 // image handle.\r
2298 //\r
2299 ImageHandle = BdsExpandUsbShortFormDevicePath (DevicePath);\r
2300\r
2301 //\r
2302 // Adjust the different type memory page number just before booting\r
2303 // and save the updated info into the variable for next boot to use\r
2304 //\r
2305 BdsSetMemoryTypeInformationVariable ();\r
2306\r
5c08e117 2307 //\r
9972247d 2308 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.\r
2309 // Here get the ImageHandle for the non USB class or WWID device path.\r
5c08e117 2310 //\r
9972247d 2311 if (ImageHandle == NULL) {\r
2312 ASSERT (Option->DevicePath != NULL);\r
2313 if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&\r
2314 (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)\r
2315 ) {\r
2316 //\r
2317 // Check to see if we should legacy BOOT. If yes then do the legacy boot\r
2318 //\r
2319 return BdsLibDoLegacyBoot (Option);\r
5c08e117 2320 }\r
128efbbc 2321\r
5c08e117 2322 //\r
9972247d 2323 // If the boot option point to Internal FV shell, make sure it is valid\r
5c08e117 2324 //\r
9972247d 2325 Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));\r
2326 if (!EFI_ERROR(Status)) {\r
2327 if (Option->DevicePath != NULL) {\r
2328 FreePool(Option->DevicePath);\r
2329 }\r
2330 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
2331 ASSERT(Option->DevicePath != NULL);\r
2332 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
2333 //\r
2334 // Update the shell boot option\r
2335 //\r
2336 InitializeListHead (&TempBootLists);\r
2337 BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
2338\r
2339 //\r
2340 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r
2341 //\r
2342 FreePool (DevicePath);\r
2343 DevicePath = Option->DevicePath;\r
2344 }\r
5c08e117 2345\r
9972247d 2346 DEBUG_CODE_BEGIN();\r
5c08e117 2347\r
9aa7ba01 2348 if (Option->Description == NULL) {\r
2349 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));\r
2350 } else {\r
2351 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));\r
ab8cc80b 2352 }\r
9aa7ba01 2353 \r
9972247d 2354 DEBUG_CODE_END();\r
ab8cc80b 2355 \r
79b7a6a1 2356 //\r
2357 // Report status code for OS Loader LoadImage.\r
2358 //\r
2359 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
9972247d 2360 Status = gBS->LoadImage (\r
2361 TRUE,\r
2362 gImageHandle,\r
2363 DevicePath,\r
2364 NULL,\r
2365 0,\r
2366 &ImageHandle\r
2367 );\r
5c08e117 2368\r
5c08e117 2369 //\r
9972247d 2370 // If we didn't find an image directly, we need to try as if it is a removable device boot option\r
2371 // and load the image according to the default boot behavior for removable device.\r
5c08e117 2372 //\r
9972247d 2373 if (EFI_ERROR (Status)) {\r
2374 //\r
2375 // check if there is a bootable removable media could be found in this device path ,\r
2376 // and get the bootable media handle\r
2377 //\r
2378 Handle = BdsLibGetBootableHandle(DevicePath);\r
cb38c322 2379 if (Handle != NULL) {\r
2380 //\r
2381 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
2382 // machinename is ia32, ia64, x64, ...\r
2383 //\r
2384 FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
2385 if (FilePath != NULL) {\r
2386 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
2387 Status = gBS->LoadImage (\r
2388 TRUE,\r
2389 gImageHandle,\r
2390 FilePath,\r
2391 NULL,\r
2392 0,\r
2393 &ImageHandle\r
2394 );\r
9972247d 2395 }\r
2396 }\r
5c08e117 2397 }\r
5c08e117 2398 }\r
2399 //\r
2400 // Provide the image with it's load options\r
2401 //\r
cb38c322 2402 if ((ImageHandle == NULL) || (EFI_ERROR(Status))) {\r
2403 //\r
2404 // Report Status Code to indicate that the failure to load boot option\r
2405 //\r
2406 REPORT_STATUS_CODE (\r
2407 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
2408 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR)\r
2409 ); \r
9972247d 2410 goto Done;\r
2411 }\r
cb38c322 2412\r
5c08e117 2413 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
2414 ASSERT_EFI_ERROR (Status);\r
2415\r
2416 if (Option->LoadOptionsSize != 0) {\r
2417 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r
2418 ImageInfo->LoadOptions = Option->LoadOptions;\r
2419 }\r
bf0712ae
ED
2420\r
2421 //\r
2422 // Clean to NULL because the image is loaded directly from the firmwares boot manager.\r
2423 //\r
2424 ImageInfo->ParentHandle = NULL;\r
2425\r
5c08e117 2426 //\r
2427 // Before calling the image, enable the Watchdog Timer for\r
2428 // the 5 Minute period\r
2429 //\r
2430 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
2431\r
cd6a3b15 2432 //\r
2433 // Write boot to OS performance data for UEFI boot\r
2434 //\r
2435 PERF_CODE (\r
26c0ba77 2436 WriteBootToOsPerformanceData (NULL, NULL);\r
cd6a3b15 2437 );\r
2438\r
79b7a6a1 2439 //\r
2440 // Report status code for OS Loader StartImage.\r
2441 //\r
2442 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));\r
2443\r
5c08e117 2444 Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);\r
2445 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r
cb38c322 2446 if (EFI_ERROR (Status)) {\r
2447 //\r
2448 // Report Status Code to indicate that boot failure\r
2449 //\r
2450 REPORT_STATUS_CODE (\r
2451 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
2452 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED)\r
2453 );\r
2454 }\r
5c08e117 2455\r
2456 //\r
2457 // Clear the Watchdog Timer after the image returns\r
2458 //\r
2459 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
2460\r
2461Done:\r
2df686c6 2462 //\r
2463 // Set Logo status invalid after trying one boot option\r
2464 //\r
2465 BootLogo = NULL;\r
2466 StatusLogo = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);\r
2467 if (!EFI_ERROR (StatusLogo) && (BootLogo != NULL)) {\r
2468 BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);\r
2469 }\r
2470\r
5c08e117 2471 //\r
2472 // Clear Boot Current\r
69fc8f08 2473 // Deleting variable with current implementation shouldn't fail.\r
5c08e117 2474 //\r
2475 gRT->SetVariable (\r
2476 L"BootCurrent",\r
2477 &gEfiGlobalVariableGuid,\r
2478 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
2479 0,\r
69fc8f08 2480 NULL\r
5c08e117 2481 );\r
2482\r
2483 return Status;\r
2484}\r
2485\r
2486\r
2487/**\r
2488 Expand a device path that starts with a hard drive media device path node to be a\r
2489 full device path that includes the full hardware path to the device. We need\r
2490 to do this so it can be booted. As an optimization the front match (the part point\r
2491 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r
2492 so a connect all is not required on every boot. All successful history device path\r
2493 which point to partition node (the front part) will be saved.\r
2494\r
2495 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard\r
2496 drive media device path.\r
2497 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path\r
2498 cannot be found.\r
2499\r
2500**/\r
2501EFI_DEVICE_PATH_PROTOCOL *\r
2502EFIAPI\r
2503BdsExpandPartitionPartialDevicePathToFull (\r
2504 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
2505 )\r
2506{\r
2507 EFI_STATUS Status;\r
2508 UINTN BlockIoHandleCount;\r
2509 EFI_HANDLE *BlockIoBuffer;\r
2510 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r
2511 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;\r
2512 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2513 UINTN Index;\r
2514 UINTN InstanceNum;\r
2515 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;\r
2516 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
2517 UINTN CachedDevicePathSize;\r
2518 BOOLEAN DeviceExist;\r
2519 BOOLEAN NeedAdjust;\r
2520 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
2521 UINTN Size;\r
2522\r
2523 FullDevicePath = NULL;\r
2524 //\r
e24fc103 2525 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.\r
5c08e117 2526 // If exist, search the front path which point to partition node in the variable instants.\r
e24fc103 2527 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system\r
5c08e117 2528 //\r
2529 CachedDevicePath = BdsLibGetVariableAndSize (\r
e24fc103
LG
2530 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2531 &gHdBootDevicePathVariablGuid,\r
5c08e117 2532 &CachedDevicePathSize\r
2533 );\r
128efbbc 2534\r
5c08e117 2535 if (CachedDevicePath != NULL) {\r
2536 TempNewDevicePath = CachedDevicePath;\r
2537 DeviceExist = FALSE;\r
2538 NeedAdjust = FALSE;\r
2539 do {\r
2540 //\r
2541 // Check every instance of the variable\r
2542 // First, check whether the instance contain the partition node, which is needed for distinguishing multi\r
2543 // partial partition boot option. Second, check whether the instance could be connected.\r
2544 //\r
2545 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r
2546 if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {\r
2547 //\r
2548 // Connect the device path instance, the device path point to hard drive media device path node\r
2549 // e.g. ACPI() /PCI()/ATA()/Partition()\r
2550 //\r
2551 Status = BdsLibConnectDevicePath (Instance);\r
2552 if (!EFI_ERROR (Status)) {\r
2553 DeviceExist = TRUE;\r
2554 break;\r
2555 }\r
2556 }\r
2557 //\r
2558 // Come here means the first instance is not matched\r
2559 //\r
2560 NeedAdjust = TRUE;\r
2561 FreePool(Instance);\r
2562 } while (TempNewDevicePath != NULL);\r
2563\r
2564 if (DeviceExist) {\r
2565 //\r
2566 // Find the matched device path.\r
2567 // Append the file path information from the boot option and return the fully expanded device path.\r
2568 //\r
2569 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
2570 FullDevicePath = AppendDevicePath (Instance, DevicePath);\r
2571\r
2572 //\r
e24fc103 2573 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.\r
5c08e117 2574 //\r
2575 if (NeedAdjust) {\r
2576 //\r
2577 // First delete the matched instance.\r
2578 //\r
2579 TempNewDevicePath = CachedDevicePath;\r
2580 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r
2581 FreePool (TempNewDevicePath);\r
128efbbc 2582\r
5c08e117 2583 //\r
2584 // Second, append the remaining path after the matched instance\r
2585 //\r
2586 TempNewDevicePath = CachedDevicePath;\r
2587 CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r
2588 FreePool (TempNewDevicePath);\r
2589 //\r
2590 // Save the matching Device Path so we don't need to do a connect all next time\r
69fc8f08 2591 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.\r
5c08e117 2592 //\r
2593 Status = gRT->SetVariable (\r
e24fc103
LG
2594 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2595 &gHdBootDevicePathVariablGuid,\r
5c08e117 2596 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2597 GetDevicePathSize (CachedDevicePath),\r
2598 CachedDevicePath\r
2599 );\r
2600 }\r
128efbbc 2601\r
5c08e117 2602 FreePool (Instance);\r
2603 FreePool (CachedDevicePath);\r
2604 return FullDevicePath;\r
2605 }\r
2606 }\r
2607\r
2608 //\r
e24fc103 2609 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need\r
5c08e117 2610 // to search all devices in the system for a matched partition\r
2611 //\r
2612 BdsLibConnectAllDriversToAllControllers ();\r
2613 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r
2614 if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {\r
2615 //\r
2616 // If there was an error or there are no device handles that support\r
2617 // the BLOCK_IO Protocol, then return.\r
2618 //\r
2619 return NULL;\r
2620 }\r
2621 //\r
2622 // Loop through all the device handles that support the BLOCK_IO Protocol\r
2623 //\r
2624 for (Index = 0; Index < BlockIoHandleCount; Index++) {\r
2625\r
2626 Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);\r
2627 if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {\r
2628 continue;\r
2629 }\r
2630\r
2631 if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {\r
2632 //\r
2633 // Find the matched partition device path\r
2634 //\r
2635 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
2636 FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r
2637\r
2638 //\r
e24fc103 2639 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable\r
5c08e117 2640 //\r
2641 if (CachedDevicePath != NULL) {\r
2642 //\r
e24fc103 2643 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable\r
5c08e117 2644 //\r
2645 if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {\r
2646 TempNewDevicePath = CachedDevicePath;\r
2647 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r
2648 FreePool(TempNewDevicePath);\r
e5a9b63c 2649 }\r
5c08e117 2650\r
e5a9b63c 2651 if (CachedDevicePath != NULL) {\r
5c08e117 2652 TempNewDevicePath = CachedDevicePath;\r
2653 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
2654 FreePool(TempNewDevicePath);\r
e5a9b63c
RN
2655 } else {\r
2656 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r
5c08e117 2657 }\r
e5a9b63c 2658\r
5c08e117 2659 //\r
2660 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r
e24fc103
LG
2661 // If the user try to boot many OS in different HDs or partitions, in theory, \r
2662 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.\r
5c08e117 2663 //\r
2664 InstanceNum = 0;\r
2665 ASSERT (CachedDevicePath != NULL);\r
2666 TempNewDevicePath = CachedDevicePath;\r
2667 while (!IsDevicePathEnd (TempNewDevicePath)) {\r
2668 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
2669 //\r
2670 // Parse one instance\r
2671 //\r
2672 while (!IsDevicePathEndType (TempNewDevicePath)) {\r
2673 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
2674 }\r
2675 InstanceNum++;\r
2676 //\r
2677 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.\r
2678 //\r
2679 if (InstanceNum >= 12) {\r
2680 SetDevicePathEndNode (TempNewDevicePath);\r
2681 break;\r
2682 }\r
2683 }\r
2684 } else {\r
2685 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r
2686 }\r
2687\r
2688 //\r
2689 // Save the matching Device Path so we don't need to do a connect all next time\r
69fc8f08 2690 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.\r
5c08e117 2691 //\r
2692 Status = gRT->SetVariable (\r
e24fc103
LG
2693 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2694 &gHdBootDevicePathVariablGuid,\r
5c08e117 2695 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2696 GetDevicePathSize (CachedDevicePath),\r
2697 CachedDevicePath\r
2698 );\r
2699\r
2700 break;\r
2701 }\r
2702 }\r
128efbbc 2703\r
cd730ec0 2704 if (CachedDevicePath != NULL) {\r
2705 FreePool (CachedDevicePath);\r
2706 }\r
5c08e117 2707 if (BlockIoBuffer != NULL) {\r
2708 FreePool (BlockIoBuffer);\r
2709 }\r
2710 return FullDevicePath;\r
2711}\r
2712\r
2713/**\r
2714 Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r
2715 instances, has the same partition node with HardDriveDevicePath device path\r
2716\r
2717 @param BlockIoDevicePath Multi device path instances which need to check\r
2718 @param HardDriveDevicePath A device path which starts with a hard drive media\r
2719 device path.\r
2720\r
2721 @retval TRUE There is a matched device path instance.\r
2722 @retval FALSE There is no matched device path instance.\r
2723\r
2724**/\r
2725BOOLEAN\r
2726EFIAPI\r
2727MatchPartitionDevicePathNode (\r
2728 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,\r
2729 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
2730 )\r
2731{\r
2732 HARDDRIVE_DEVICE_PATH *TmpHdPath;\r
2733 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2734 BOOLEAN Match;\r
2735 EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;\r
2736\r
2737 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r
2738 return FALSE;\r
2739 }\r
128efbbc 2740\r
5c08e117 2741 //\r
2742 // Make PreviousDevicePath == the device path node before the end node\r
2743 //\r
2744 DevicePath = BlockIoDevicePath;\r
2745 BlockIoHdDevicePathNode = NULL;\r
2746\r
2747 //\r
2748 // find the partition device path node\r
2749 //\r
2750 while (!IsDevicePathEnd (DevicePath)) {\r
2751 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
2752 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
2753 ) {\r
2754 BlockIoHdDevicePathNode = DevicePath;\r
2755 break;\r
2756 }\r
2757\r
2758 DevicePath = NextDevicePathNode (DevicePath);\r
2759 }\r
2760\r
2761 if (BlockIoHdDevicePathNode == NULL) {\r
2762 return FALSE;\r
2763 }\r
2764 //\r
2765 // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
2766 //\r
2767 TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;\r
2768 Match = FALSE;\r
128efbbc 2769\r
5c08e117 2770 //\r
2771 // Check for the match\r
2772 //\r
2773 if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&\r
2774 (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {\r
2775 switch (TmpHdPath->SignatureType) {\r
2776 case SIGNATURE_TYPE_GUID:\r
2777 Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);\r
2778 break;\r
2779 case SIGNATURE_TYPE_MBR:\r
2780 Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));\r
2781 break;\r
2782 default:\r
2783 Match = FALSE;\r
2784 break;\r
2785 }\r
2786 }\r
2787\r
2788 return Match;\r
2789}\r
2790\r
2791/**\r
2792 Delete the boot option associated with the handle passed in.\r
2793\r
2794 @param Handle The handle which present the device path to create\r
2795 boot option\r
2796\r
2797 @retval EFI_SUCCESS Delete the boot option success\r
2798 @retval EFI_NOT_FOUND If the Device Path is not found in the system\r
2799 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
2800 @retval Other Error return value from SetVariable()\r
2801\r
2802**/\r
2803EFI_STATUS\r
2804BdsLibDeleteOptionFromHandle (\r
2805 IN EFI_HANDLE Handle\r
2806 )\r
2807{\r
2808 UINT16 *BootOrder;\r
2809 UINT8 *BootOptionVar;\r
2810 UINTN BootOrderSize;\r
2811 UINTN BootOptionSize;\r
2812 EFI_STATUS Status;\r
2813 UINTN Index;\r
2814 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
2815 UINTN DevicePathSize;\r
2816 UINTN OptionDevicePathSize;\r
2817 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2818 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
2819 UINT8 *TempPtr;\r
2820\r
2821 Status = EFI_SUCCESS;\r
2822 BootOrder = NULL;\r
2823 BootOrderSize = 0;\r
2824\r
2825 //\r
2826 // Check "BootOrder" variable, if no, means there is no any boot order.\r
2827 //\r
2828 BootOrder = BdsLibGetVariableAndSize (\r
2829 L"BootOrder",\r
2830 &gEfiGlobalVariableGuid,\r
2831 &BootOrderSize\r
2832 );\r
2833 if (BootOrder == NULL) {\r
2834 return EFI_NOT_FOUND;\r
2835 }\r
2836\r
2837 //\r
2838 // Convert device handle to device path protocol instance\r
2839 //\r
2840 DevicePath = DevicePathFromHandle (Handle);\r
2841 if (DevicePath == NULL) {\r
2842 return EFI_NOT_FOUND;\r
2843 }\r
2844 DevicePathSize = GetDevicePathSize (DevicePath);\r
2845\r
2846 //\r
2847 // Loop all boot order variable and find the matching device path\r
2848 //\r
2849 Index = 0;\r
2850 while (Index < BootOrderSize / sizeof (UINT16)) {\r
2851 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
2852 BootOptionVar = BdsLibGetVariableAndSize (\r
2853 BootOption,\r
2854 &gEfiGlobalVariableGuid,\r
2855 &BootOptionSize\r
2856 );\r
128efbbc 2857\r
5c08e117 2858 if (BootOptionVar == NULL) {\r
2859 FreePool (BootOrder);\r
2860 return EFI_OUT_OF_RESOURCES;\r
2861 }\r
2862\r
8c08a567
ED
2863 if (!ValidateOption(BootOptionVar, BootOptionSize)) {\r
2864 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
2865 FreePool (BootOptionVar);\r
2866 Index++;\r
2867 continue;\r
2868 }\r
2869\r
5c08e117 2870 TempPtr = BootOptionVar;\r
2871 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
2872 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
2873 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
2874 OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
2875\r
2876 //\r
2877 // Check whether the device path match\r
2878 //\r
2879 if ((OptionDevicePathSize == DevicePathSize) &&\r
2880 (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
2881 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
2882 FreePool (BootOptionVar);\r
2883 break;\r
2884 }\r
2885\r
2886 FreePool (BootOptionVar);\r
2887 Index++;\r
2888 }\r
2889\r
2890 //\r
2891 // Adjust number of boot option for "BootOrder" variable.\r
2892 //\r
2893 Status = gRT->SetVariable (\r
2894 L"BootOrder",\r
2895 &gEfiGlobalVariableGuid,\r
2896 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2897 BootOrderSize,\r
2898 BootOrder\r
2899 );\r
69fc8f08
RN
2900 //\r
2901 // Shrinking variable with existing variable implementation shouldn't fail.\r
2902 //\r
2903 ASSERT_EFI_ERROR (Status);\r
5c08e117 2904\r
2905 FreePool (BootOrder);\r
2906\r
2907 return Status;\r
2908}\r
2909\r
2910\r
2911/**\r
3384a9bc 2912 Delete all invalid EFI boot options.\r
5c08e117 2913\r
2914 @retval EFI_SUCCESS Delete all invalid boot option success\r
2915 @retval EFI_NOT_FOUND Variable "BootOrder" is not found\r
2916 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
2917 @retval Other Error return value from SetVariable()\r
2918\r
2919**/\r
2920EFI_STATUS\r
2921BdsDeleteAllInvalidEfiBootOption (\r
2922 VOID\r
2923 )\r
2924{\r
2925 UINT16 *BootOrder;\r
2926 UINT8 *BootOptionVar;\r
2927 UINTN BootOrderSize;\r
2928 UINTN BootOptionSize;\r
2929 EFI_STATUS Status;\r
2930 UINTN Index;\r
2931 UINTN Index2;\r
2932 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
2933 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
2934 UINT8 *TempPtr;\r
3384a9bc 2935 CHAR16 *Description;\r
8c08a567 2936 BOOLEAN Corrupted;\r
5c08e117 2937\r
8c08a567
ED
2938 Status = EFI_SUCCESS;\r
2939 BootOrder = NULL;\r
2940 Description = NULL;\r
2941 OptionDevicePath = NULL;\r
2942 BootOrderSize = 0;\r
2943 Corrupted = FALSE;\r
5c08e117 2944\r
2945 //\r
2946 // Check "BootOrder" variable firstly, this variable hold the number of boot options\r
2947 //\r
2948 BootOrder = BdsLibGetVariableAndSize (\r
2949 L"BootOrder",\r
2950 &gEfiGlobalVariableGuid,\r
2951 &BootOrderSize\r
2952 );\r
2953 if (NULL == BootOrder) {\r
2954 return EFI_NOT_FOUND;\r
2955 }\r
2956\r
2957 Index = 0;\r
2958 while (Index < BootOrderSize / sizeof (UINT16)) {\r
2959 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
2960 BootOptionVar = BdsLibGetVariableAndSize (\r
2961 BootOption,\r
2962 &gEfiGlobalVariableGuid,\r
2963 &BootOptionSize\r
2964 );\r
2965 if (NULL == BootOptionVar) {\r
2966 FreePool (BootOrder);\r
2967 return EFI_OUT_OF_RESOURCES;\r
2968 }\r
2969\r
8c08a567
ED
2970 if (!ValidateOption(BootOptionVar, BootOptionSize)) {\r
2971 Corrupted = TRUE;\r
2972 } else {\r
2973 TempPtr = BootOptionVar;\r
2974 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
2975 Description = (CHAR16 *) TempPtr;\r
2976 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
2977 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
5c08e117 2978\r
8c08a567
ED
2979 //\r
2980 // Skip legacy boot option (BBS boot device)\r
2981 //\r
2982 if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
2983 (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
2984 FreePool (BootOptionVar);\r
2985 Index++;\r
2986 continue;\r
2987 }\r
5c08e117 2988 }\r
2989\r
8c08a567 2990 if (Corrupted || !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {\r
5c08e117 2991 //\r
2992 // Delete this invalid boot option "Boot####"\r
2993 //\r
2994 Status = gRT->SetVariable (\r
2995 BootOption,\r
2996 &gEfiGlobalVariableGuid,\r
2997 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2998 0,\r
2999 NULL\r
3000 );\r
3001 //\r
69fc8f08
RN
3002 // Deleting variable with current variable implementation shouldn't fail.\r
3003 //\r
3004 ASSERT_EFI_ERROR (Status);\r
3005 //\r
5c08e117 3006 // Mark this boot option in boot order as deleted\r
3007 //\r
3008 BootOrder[Index] = 0xffff;\r
8c08a567 3009 Corrupted = FALSE;\r
5c08e117 3010 }\r
3011\r
3012 FreePool (BootOptionVar);\r
3013 Index++;\r
3014 }\r
3015\r
3016 //\r
3017 // Adjust boot order array\r
3018 //\r
3019 Index2 = 0;\r
3020 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {\r
3021 if (BootOrder[Index] != 0xffff) {\r
3022 BootOrder[Index2] = BootOrder[Index];\r
3023 Index2 ++;\r
3024 }\r
3025 }\r
3026 Status = gRT->SetVariable (\r
3027 L"BootOrder",\r
3028 &gEfiGlobalVariableGuid,\r
3029 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
3030 Index2 * sizeof (UINT16),\r
3031 BootOrder\r
3032 );\r
69fc8f08
RN
3033 //\r
3034 // Shrinking variable with current variable implementation shouldn't fail.\r
3035 //\r
3036 ASSERT_EFI_ERROR (Status);\r
5c08e117 3037\r
3038 FreePool (BootOrder);\r
3039\r
3040 return Status;\r
3041}\r
3042\r
3043\r
3044/**\r
3384a9bc 3045 For EFI boot option, BDS separate them as six types:\r
128efbbc 3046 1. Network - The boot option points to the SimpleNetworkProtocol device.\r
3384a9bc 3047 Bds will try to automatically create this type boot option when enumerate.\r
128efbbc 3048 2. Shell - The boot option points to internal flash shell.\r
3384a9bc 3049 Bds will try to automatically create this type boot option when enumerate.\r
3050 3. Removable BlockIo - The boot option only points to the removable media\r
3051 device, like USB flash disk, DVD, Floppy etc.\r
3052 These device should contain a *removable* blockIo\r
3053 protocol in their device handle.\r
128efbbc 3054 Bds will try to automatically create this type boot option\r
3384a9bc 3055 when enumerate.\r
128efbbc 3056 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,\r
3384a9bc 3057 like HardDisk.\r
3058 These device should contain a *fixed* blockIo\r
3059 protocol in their device handle.\r
3060 BDS will skip fixed blockIo devices, and NOT\r
128efbbc 3061 automatically create boot option for them. But BDS\r
3062 will help to delete those fixed blockIo boot option,\r
3384a9bc 3063 whose description rule conflict with other auto-created\r
3064 boot options.\r
128efbbc 3065 5. Non-BlockIo Simplefile - The boot option points to a device whose handle\r
3384a9bc 3066 has SimpleFileSystem Protocol, but has no blockio\r
3067 protocol. These devices do not offer blockIo\r
128efbbc 3068 protocol, but BDS still can get the\r
3384a9bc 3069 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem\r
3070 Protocol.\r
128efbbc 3071 6. File - The boot option points to a file. These boot options are usually\r
3384a9bc 3072 created by user manually or OS loader. BDS will not delete or modify\r
128efbbc 3073 these boot options.\r
3074\r
3384a9bc 3075 This function will enumerate all possible boot device in the system, and\r
128efbbc 3076 automatically create boot options for Network, Shell, Removable BlockIo,\r
3384a9bc 3077 and Non-BlockIo Simplefile devices.\r
8d3b5aff 3078 It will only execute once of every boot.\r
128efbbc 3079\r
5c08e117 3080 @param BdsBootOptionList The header of the link list which indexed all\r
3081 current boot options\r
3082\r
3083 @retval EFI_SUCCESS Finished all the boot device enumerate and create\r
3084 the boot option base on that boot device\r
3085\r
e83c9064 3086 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list\r
5c08e117 3087**/\r
3088EFI_STATUS\r
3089EFIAPI\r
3090BdsLibEnumerateAllBootOption (\r
3091 IN OUT LIST_ENTRY *BdsBootOptionList\r
3092 )\r
3093{\r
3094 EFI_STATUS Status;\r
3095 UINT16 FloppyNumber;\r
889a4bc2 3096 UINT16 HarddriveNumber;\r
5c08e117 3097 UINT16 CdromNumber;\r
3098 UINT16 UsbNumber;\r
3099 UINT16 MiscNumber;\r
8d3b5aff 3100 UINT16 ScsiNumber;\r
5c08e117 3101 UINT16 NonBlockNumber;\r
3102 UINTN NumberBlockIoHandles;\r
3103 EFI_HANDLE *BlockIoHandles;\r
3104 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
35bc0e9f
RN
3105 BOOLEAN Removable[2];\r
3106 UINTN RemovableIndex;\r
5c08e117 3107 UINTN Index;\r
a7a523e0 3108 UINTN NumOfLoadFileHandles;\r
3109 EFI_HANDLE *LoadFileHandles;\r
5c08e117 3110 UINTN FvHandleCount;\r
3111 EFI_HANDLE *FvHandleBuffer;\r
3112 EFI_FV_FILETYPE Type;\r
3113 UINTN Size;\r
3114 EFI_FV_FILE_ATTRIBUTES Attributes;\r
3115 UINT32 AuthenticationStatus;\r
8d3b5aff 3116 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
3117 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
5c08e117 3118 UINTN DevicePathType;\r
3119 CHAR16 Buffer[40];\r
3120 EFI_HANDLE *FileSystemHandles;\r
3121 UINTN NumberFileSystemHandles;\r
3122 BOOLEAN NeedDelete;\r
3123 EFI_IMAGE_DOS_HEADER DosHeader;\r
9aa7ba01 3124 CHAR8 *PlatLang;\r
3125 CHAR8 *LastLang;\r
5c08e117 3126 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
3127 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
3128\r
889a4bc2
RN
3129 FloppyNumber = 0;\r
3130 HarddriveNumber = 0;\r
3131 CdromNumber = 0;\r
3132 UsbNumber = 0;\r
3133 MiscNumber = 0;\r
3134 ScsiNumber = 0;\r
3135 PlatLang = NULL;\r
3136 LastLang = NULL;\r
5c08e117 3137 ZeroMem (Buffer, sizeof (Buffer));\r
128efbbc 3138\r
5c08e117 3139 //\r
3140 // If the boot device enumerate happened, just get the boot\r
3141 // device from the boot order variable\r
3142 //\r
3143 if (mEnumBootDevice) {\r
f01b91ae
ED
3144 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME, &gLastEnumLangGuid, (VOID**)&LastLang, NULL);\r
3145 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatLang, NULL);\r
0fa3ac1b
RN
3146 ASSERT (PlatLang != NULL);\r
3147 if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {\r
9aa7ba01 3148 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
0fa3ac1b
RN
3149 FreePool (LastLang);\r
3150 FreePool (PlatLang);\r
9aa7ba01 3151 return Status;\r
3152 } else {\r
3153 Status = gRT->SetVariable (\r
e24fc103
LG
3154 LAST_ENUM_LANGUAGE_VARIABLE_NAME,\r
3155 &gLastEnumLangGuid,\r
9aa7ba01 3156 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
0fa3ac1b 3157 AsciiStrSize (PlatLang),\r
9aa7ba01 3158 PlatLang\r
3159 );\r
69fc8f08
RN
3160 //\r
3161 // Failure to set the variable only impacts the performance next time enumerating the boot options.\r
3162 //\r
0fa3ac1b
RN
3163\r
3164 if (LastLang != NULL) {\r
3165 FreePool (LastLang);\r
3166 }\r
3167 FreePool (PlatLang);\r
9aa7ba01 3168 }\r
5c08e117 3169 }\r
128efbbc 3170\r
5c08e117 3171 //\r
3172 // Notes: this dirty code is to get the legacy boot option from the\r
3173 // BBS table and create to variable as the EFI boot option, it should\r
3174 // be removed after the CSM can provide legacy boot option directly\r
3175 //\r
3176 REFRESH_LEGACY_BOOT_OPTIONS;\r
3177\r
3178 //\r
3179 // Delete invalid boot option\r
3180 //\r
3181 BdsDeleteAllInvalidEfiBootOption ();\r
128efbbc 3182\r
5c08e117 3183 //\r
701e17e5
RN
3184 // Parse removable media followed by fixed media.\r
3185 // The Removable[] array is used by the for-loop below to create removable media boot options \r
3186 // at first, and then to create fixed media boot options.\r
5c08e117 3187 //\r
701e17e5
RN
3188 Removable[0] = FALSE;\r
3189 Removable[1] = TRUE;\r
3190\r
5c08e117 3191 gBS->LocateHandleBuffer (\r
3192 ByProtocol,\r
3193 &gEfiBlockIoProtocolGuid,\r
3194 NULL,\r
3195 &NumberBlockIoHandles,\r
3196 &BlockIoHandles\r
3197 );\r
128efbbc 3198\r
35bc0e9f
RN
3199 for (RemovableIndex = 0; RemovableIndex < 2; RemovableIndex++) {\r
3200 for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r
3201 Status = gBS->HandleProtocol (\r
3202 BlockIoHandles[Index],\r
3203 &gEfiBlockIoProtocolGuid,\r
3204 (VOID **) &BlkIo\r
3205 );\r
3206 //\r
3207 // skip the fixed block io then the removable block io\r
3208 //\r
3209 if (EFI_ERROR (Status) || (BlkIo->Media->RemovableMedia == Removable[RemovableIndex])) {\r
5c08e117 3210 continue;\r
3211 }\r
35bc0e9f
RN
3212 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);\r
3213 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);\r
5c08e117 3214\r
35bc0e9f
RN
3215 switch (DevicePathType) {\r
3216 case BDS_EFI_ACPI_FLOPPY_BOOT:\r
3217 if (FloppyNumber != 0) {\r
3218 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);\r
3219 } else {\r
3220 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));\r
3221 }\r
3222 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3223 FloppyNumber++;\r
3224 break;\r
128efbbc 3225\r
35bc0e9f 3226 //\r
889a4bc2 3227 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.\r
35bc0e9f
RN
3228 //\r
3229 case BDS_EFI_MESSAGE_ATAPI_BOOT:\r
3230 case BDS_EFI_MESSAGE_SATA_BOOT:\r
889a4bc2
RN
3231 if (BlkIo->Media->RemovableMedia) {\r
3232 if (CdromNumber != 0) {\r
3233 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);\r
3234 } else {\r
3235 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));\r
3236 }\r
3237 CdromNumber++;\r
35bc0e9f 3238 } else {\r
889a4bc2
RN
3239 if (HarddriveNumber != 0) {\r
3240 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)), HarddriveNumber);\r
3241 } else {\r
3242 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)));\r
3243 }\r
3244 HarddriveNumber++;\r
35bc0e9f
RN
3245 }\r
3246 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));\r
3247 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
35bc0e9f
RN
3248 break;\r
3249\r
3250 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:\r
3251 if (UsbNumber != 0) {\r
3252 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);\r
3253 } else {\r
3254 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));\r
3255 }\r
3256 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3257 UsbNumber++;\r
3258 break;\r
5c08e117 3259\r
35bc0e9f
RN
3260 case BDS_EFI_MESSAGE_SCSI_BOOT:\r
3261 if (ScsiNumber != 0) {\r
3262 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);\r
3263 } else {\r
3264 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));\r
3265 }\r
3266 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3267 ScsiNumber++;\r
3268 break;\r
5c08e117 3269\r
35bc0e9f
RN
3270 case BDS_EFI_MESSAGE_MISC_BOOT:\r
3271 if (MiscNumber != 0) {\r
3272 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);\r
3273 } else {\r
3274 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));\r
3275 }\r
3276 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3277 MiscNumber++;\r
3278 break;\r
5c08e117 3279\r
35bc0e9f
RN
3280 default:\r
3281 break;\r
9aa7ba01 3282 }\r
5c08e117 3283 }\r
3284 }\r
3285\r
3286 if (NumberBlockIoHandles != 0) {\r
3287 FreePool (BlockIoHandles);\r
3288 }\r
3289\r
3290 //\r
3291 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.\r
3292 //\r
3293 NonBlockNumber = 0;\r
3294 gBS->LocateHandleBuffer (\r
3295 ByProtocol,\r
3296 &gEfiSimpleFileSystemProtocolGuid,\r
3297 NULL,\r
3298 &NumberFileSystemHandles,\r
3299 &FileSystemHandles\r
3300 );\r
3301 for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r
3302 Status = gBS->HandleProtocol (\r
3303 FileSystemHandles[Index],\r
3304 &gEfiBlockIoProtocolGuid,\r
3305 (VOID **) &BlkIo\r
3306 );\r
3307 if (!EFI_ERROR (Status)) {\r
3308 //\r
3309 // Skip if the file system handle supports a BlkIo protocol,\r
3310 //\r
3311 continue;\r
3312 }\r
3313\r
3314 //\r
3315 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
3316 // machinename is ia32, ia64, x64, ...\r
3317 //\r
35bc0e9f 3318 Hdr.Union = &HdrData;\r
5c08e117 3319 NeedDelete = TRUE;\r
3320 Status = BdsLibGetImageHeader (\r
3321 FileSystemHandles[Index],\r
c62dbf31 3322 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 3323 &DosHeader,\r
3324 Hdr\r
3325 );\r
3326 if (!EFI_ERROR (Status) &&\r
3327 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
3328 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
3329 NeedDelete = FALSE;\r
3330 }\r
3331\r
3332 if (NeedDelete) {\r
3333 //\r
3334 // No such file or the file is not a EFI application, delete this boot option\r
3335 //\r
3336 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
3337 } else {\r
9aa7ba01 3338 if (NonBlockNumber != 0) {\r
3339 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);\r
3340 } else {\r
3341 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));\r
3342 }\r
5c08e117 3343 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);\r
3344 NonBlockNumber++;\r
3345 }\r
3346 }\r
3347\r
3348 if (NumberFileSystemHandles != 0) {\r
3349 FreePool (FileSystemHandles);\r
3350 }\r
3351\r
3352 //\r
3353 // Parse Network Boot Device\r
3354 //\r
a7a523e0 3355 NumOfLoadFileHandles = 0;\r
ff482c56 3356 //\r
a7a523e0 3357 // Search Load File protocol for PXE boot option.\r
ff482c56 3358 //\r
5c08e117 3359 gBS->LocateHandleBuffer (\r
3360 ByProtocol,\r
a7a523e0 3361 &gEfiLoadFileProtocolGuid,\r
5c08e117 3362 NULL,\r
a7a523e0 3363 &NumOfLoadFileHandles,\r
3364 &LoadFileHandles\r
5c08e117 3365 );\r
8d3b5aff 3366\r
a7a523e0 3367 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {\r
9aa7ba01 3368 if (Index != 0) {\r
3369 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);\r
3370 } else {\r
3371 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));\r
3372 }\r
a7a523e0 3373 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);\r
5c08e117 3374 }\r
3375\r
a7a523e0 3376 if (NumOfLoadFileHandles != 0) {\r
3377 FreePool (LoadFileHandles);\r
5c08e117 3378 }\r
3379\r
3380 //\r
3381 // Check if we have on flash shell\r
3382 //\r
3383 gBS->LocateHandleBuffer (\r
3384 ByProtocol,\r
3385 &gEfiFirmwareVolume2ProtocolGuid,\r
3386 NULL,\r
3387 &FvHandleCount,\r
3388 &FvHandleBuffer\r
3389 );\r
3390 for (Index = 0; Index < FvHandleCount; Index++) {\r
5c08e117 3391 gBS->HandleProtocol (\r
3392 FvHandleBuffer[Index],\r
3393 &gEfiFirmwareVolume2ProtocolGuid,\r
3394 (VOID **) &Fv\r
3395 );\r
3396\r
3397 Status = Fv->ReadFile (\r
3398 Fv,\r
d46f3632 3399 PcdGetPtr(PcdShellFile),\r
5c08e117 3400 NULL,\r
3401 &Size,\r
3402 &Type,\r
3403 &Attributes,\r
3404 &AuthenticationStatus\r
3405 );\r
3406 if (EFI_ERROR (Status)) {\r
3407 //\r
3408 // Skip if no shell file in the FV\r
3409 //\r
3410 continue;\r
3411 }\r
3412 //\r
3413 // Build the shell boot option\r
3414 //\r
3415 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
3416 }\r
3417\r
3418 if (FvHandleCount != 0) {\r
3419 FreePool (FvHandleBuffer);\r
3420 }\r
3421 //\r
3422 // Make sure every boot only have one time\r
3423 // boot device enumerate\r
3424 //\r
e83c9064 3425 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
5c08e117 3426 mEnumBootDevice = TRUE;\r
3427\r
e83c9064 3428 return Status;\r
5c08e117 3429}\r
3430\r
3431/**\r
3432 Build the boot option with the handle parsed in\r
3433\r
3434 @param Handle The handle which present the device path to create\r
3435 boot option\r
3436 @param BdsBootOptionList The header of the link list which indexed all\r
3437 current boot options\r
3438 @param String The description of the boot option.\r
3439\r
3440**/\r
3441VOID\r
3442EFIAPI\r
3443BdsLibBuildOptionFromHandle (\r
3444 IN EFI_HANDLE Handle,\r
3445 IN LIST_ENTRY *BdsBootOptionList,\r
3446 IN CHAR16 *String\r
3447 )\r
3448{\r
3449 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
128efbbc 3450\r
8d3b5aff 3451 DevicePath = DevicePathFromHandle (Handle);\r
5c08e117 3452\r
3453 //\r
3454 // Create and register new boot option\r
3455 //\r
3456 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r
3457}\r
3458\r
3459\r
3460/**\r
3461 Build the on flash shell boot option with the handle parsed in.\r
3462\r
3463 @param Handle The handle which present the device path to create\r
3464 on flash shell boot option\r
3465 @param BdsBootOptionList The header of the link list which indexed all\r
3466 current boot options\r
3467\r
3468**/\r
3469VOID\r
3470EFIAPI\r
3471BdsLibBuildOptionFromShell (\r
3472 IN EFI_HANDLE Handle,\r
3473 IN OUT LIST_ENTRY *BdsBootOptionList\r
3474 )\r
3475{\r
3476 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
3477 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
3478\r
3479 DevicePath = DevicePathFromHandle (Handle);\r
3480\r
3481 //\r
3482 // Build the shell device path\r
3483 //\r
d46f3632 3484 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));\r
5c08e117 3485\r
3486 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
3487\r
3488 //\r
3489 // Create and register the shell boot option\r
3490 //\r
3491 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
3492\r
3493}\r
3494\r
3495/**\r
3496 Boot from the UEFI spec defined "BootNext" variable.\r
3497\r
3498**/\r
3499VOID\r
3500EFIAPI\r
3501BdsLibBootNext (\r
3502 VOID\r
3503 )\r
3504{\r
69fc8f08 3505 EFI_STATUS Status;\r
5c08e117 3506 UINT16 *BootNext;\r
3507 UINTN BootNextSize;\r
3508 CHAR16 Buffer[20];\r
3509 BDS_COMMON_OPTION *BootOption;\r
3510 LIST_ENTRY TempList;\r
3511 UINTN ExitDataSize;\r
3512 CHAR16 *ExitData;\r
3513\r
3514 //\r
3515 // Init the boot option name buffer and temp link list\r
3516 //\r
3517 InitializeListHead (&TempList);\r
3518 ZeroMem (Buffer, sizeof (Buffer));\r
3519\r
3520 BootNext = BdsLibGetVariableAndSize (\r
3521 L"BootNext",\r
3522 &gEfiGlobalVariableGuid,\r
3523 &BootNextSize\r
3524 );\r
3525\r
3526 //\r
3527 // Clear the boot next variable first\r
3528 //\r
3529 if (BootNext != NULL) {\r
69fc8f08
RN
3530 Status = gRT->SetVariable (\r
3531 L"BootNext",\r
3532 &gEfiGlobalVariableGuid,\r
3533 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
3534 0,\r
3535 NULL\r
3536 );\r
3537 //\r
3538 // Deleting variable with current variable implementation shouldn't fail.\r
3539 //\r
3540 ASSERT_EFI_ERROR (Status);\r
5c08e117 3541\r
3542 //\r
3543 // Start to build the boot option and try to boot\r
3544 //\r
3545 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
3546 BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
3547 ASSERT (BootOption != NULL);\r
3548 BdsLibConnectDevicePath (BootOption->DevicePath);\r
3549 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
3550 }\r
3551\r
3552}\r
3553\r
3554/**\r
3555 Return the bootable media handle.\r
3556 First, check the device is connected\r
3557 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r
3558 Third, detect the the default boot file in the Media, and return the removable Media handle.\r
3559\r
e83c9064 3560 @param DevicePath Device Path to a bootable device\r
5c08e117 3561\r
e83c9064 3562 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.\r
5c08e117 3563\r
3564**/\r
3565EFI_HANDLE\r
3566EFIAPI\r
3567BdsLibGetBootableHandle (\r
3568 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3569 )\r
3570{\r
3571 EFI_STATUS Status;\r
ef949581 3572 EFI_TPL OldTpl;\r
5c08e117 3573 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
3574 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r
3575 EFI_HANDLE Handle;\r
3576 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
3577 VOID *Buffer;\r
3578 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3579 UINTN Size;\r
3580 UINTN TempSize;\r
3581 EFI_HANDLE ReturnHandle;\r
3582 EFI_HANDLE *SimpleFileSystemHandles;\r
3583\r
3584 UINTN NumberSimpleFileSystemHandles;\r
3585 UINTN Index;\r
3586 EFI_IMAGE_DOS_HEADER DosHeader;\r
3587 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
3588 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
3589\r
3590 UpdatedDevicePath = DevicePath;\r
128efbbc 3591\r
ef949581
RN
3592 //\r
3593 // Enter to critical section to protect the acquired BlockIo instance \r
3594 // from getting released due to the USB mass storage hotplug event\r
3595 //\r
3596 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
3597\r
5c08e117 3598 //\r
3599 // Check whether the device is connected\r
3600 //\r
3601 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r
3602 if (EFI_ERROR (Status)) {\r
3603 //\r
3604 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r
3605 //\r
3606 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r
3607 if (EFI_ERROR (Status)) {\r
3608 //\r
3609 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r
3610 //\r
3611 UpdatedDevicePath = DevicePath;\r
3612 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
3613 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
3614 }\r
3615 } else {\r
e74f510b
RN
3616 //\r
3617 // For removable device boot option, its contained device path only point to the removable device handle, \r
3618 // should make sure all its children handles (its child partion or media handles) are created and connected. \r
3619 //\r
3620 gBS->ConnectController (Handle, NULL, NULL, TRUE); \r
5c08e117 3621 //\r
3622 // Get BlockIo protocol and check removable attribute\r
3623 //\r
3624 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
ef949581
RN
3625 ASSERT_EFI_ERROR (Status);\r
3626\r
5c08e117 3627 //\r
3628 // Issue a dummy read to the device to check for media change.\r
3629 // When the removable media is changed, any Block IO read/write will\r
3630 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
3631 // returned. After the Block IO protocol is reinstalled, subsequent\r
3632 // Block IO read/write will success.\r
3633 //\r
3634 Buffer = AllocatePool (BlockIo->Media->BlockSize);\r
3635 if (Buffer != NULL) {\r
3636 BlockIo->ReadBlocks (\r
3637 BlockIo,\r
3638 BlockIo->Media->MediaId,\r
3639 0,\r
3640 BlockIo->Media->BlockSize,\r
3641 Buffer\r
3642 );\r
3643 FreePool(Buffer);\r
3644 }\r
3645 }\r
3646\r
3647 //\r
3648 // Detect the the default boot file from removable Media\r
3649 //\r
3650\r
3651 //\r
3652 // If fail to get bootable handle specified by a USB boot option, the BDS should try to find other bootable device in the same USB bus\r
3653 // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
3654 //\r
3655 DupDevicePath = DuplicateDevicePath (DevicePath);\r
3656 ASSERT (DupDevicePath != NULL);\r
128efbbc 3657\r
5c08e117 3658 UpdatedDevicePath = DupDevicePath;\r
3659 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
3660 //\r
3661 // if the resulting device path point to a usb node, and the usb node is a dummy node, should only let device path only point to the previous Pci node\r
3662 // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r
3663 //\r
3664 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r
3665 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r
3666 //\r
3667 // Remove the usb node, let the device path only point to PCI node\r
3668 //\r
3669 SetDevicePathEndNode (UpdatedDevicePath);\r
3670 UpdatedDevicePath = DupDevicePath;\r
3671 } else {\r
3672 UpdatedDevicePath = DevicePath;\r
3673 }\r
3674\r
3675 //\r
3676 // Get the device path size of boot option\r
3677 //\r
3678 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
3679 ReturnHandle = NULL;\r
3680 gBS->LocateHandleBuffer (\r
3681 ByProtocol,\r
3682 &gEfiSimpleFileSystemProtocolGuid,\r
3683 NULL,\r
3684 &NumberSimpleFileSystemHandles,\r
3685 &SimpleFileSystemHandles\r
3686 );\r
3687 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
3688 //\r
3689 // Get the device path size of SimpleFileSystem handle\r
3690 //\r
3691 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
3692 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
3693 //\r
3694 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r
3695 //\r
3696 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r
3697 //\r
3698 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
3699 // machinename is ia32, ia64, x64, ...\r
3700 //\r
3701 Hdr.Union = &HdrData;\r
3702 Status = BdsLibGetImageHeader (\r
3703 SimpleFileSystemHandles[Index],\r
c62dbf31 3704 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 3705 &DosHeader,\r
3706 Hdr\r
3707 );\r
3708 if (!EFI_ERROR (Status) &&\r
3709 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
3710 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
3711 ReturnHandle = SimpleFileSystemHandles[Index];\r
3712 break;\r
3713 }\r
3714 }\r
3715 }\r
3716\r
3717 FreePool(DupDevicePath);\r
3718\r
3719 if (SimpleFileSystemHandles != NULL) {\r
3720 FreePool(SimpleFileSystemHandles);\r
3721 }\r
3722\r
ef949581
RN
3723 gBS->RestoreTPL (OldTpl);\r
3724\r
5c08e117 3725 return ReturnHandle;\r
3726}\r
3727\r
3728/**\r
3729 Check to see if the network cable is plugged in. If the DevicePath is not\r
3730 connected it will be connected.\r
3731\r
3732 @param DevicePath Device Path to check\r
3733\r
3734 @retval TRUE DevicePath points to an Network that is connected\r
3735 @retval FALSE DevicePath does not point to a bootable network\r
3736\r
3737**/\r
3738BOOLEAN\r
3739BdsLibNetworkBootWithMediaPresent (\r
3740 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3741 )\r
3742{\r
3743 EFI_STATUS Status;\r
3744 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
3745 EFI_HANDLE Handle;\r
3746 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
3747 BOOLEAN MediaPresent;\r
e51e619e 3748 UINT32 InterruptStatus;\r
5c08e117 3749\r
3750 MediaPresent = FALSE;\r
3751\r
3752 UpdatedDevicePath = DevicePath;\r
ff482c56 3753 //\r
a7a523e0 3754 // Locate Load File Protocol for PXE boot option first\r
ff482c56 3755 //\r
a7a523e0 3756 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 3757 if (EFI_ERROR (Status)) {\r
3758 //\r
3759 // Device not present so see if we need to connect it\r
3760 //\r
3761 Status = BdsLibConnectDevicePath (DevicePath);\r
3762 if (!EFI_ERROR (Status)) {\r
3763 //\r
3764 // This one should work after we did the connect\r
3765 //\r
a7a523e0 3766 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 3767 }\r
3768 }\r
3769\r
3770 if (!EFI_ERROR (Status)) {\r
3771 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r
ff482c56 3772 if (EFI_ERROR (Status)) {\r
3773 //\r
3774 // Failed to open SNP from this handle, try to get SNP from parent handle\r
3775 //\r
3776 UpdatedDevicePath = DevicePathFromHandle (Handle);\r
3777 if (UpdatedDevicePath != NULL) {\r
3778 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
3779 if (!EFI_ERROR (Status)) {\r
3780 //\r
3781 // SNP handle found, get SNP from it\r
3782 //\r
3783 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);\r
3784 }\r
3785 }\r
3786 }\r
3787\r
5c08e117 3788 if (!EFI_ERROR (Status)) {\r
3789 if (Snp->Mode->MediaPresentSupported) {\r
3790 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r
e51e619e 3791 //\r
3792 // Invoke Snp->GetStatus() to refresh the media status\r
3793 //\r
3794 Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
3795\r
5c08e117 3796 //\r
3797 // In case some one else is using the SNP check to see if it's connected\r
3798 //\r
3799 MediaPresent = Snp->Mode->MediaPresent;\r
3800 } else {\r
3801 //\r
3802 // No one is using SNP so we need to Start and Initialize so\r
3803 // MediaPresent will be valid.\r
3804 //\r
3805 Status = Snp->Start (Snp);\r
3806 if (!EFI_ERROR (Status)) {\r
3807 Status = Snp->Initialize (Snp, 0, 0);\r
3808 if (!EFI_ERROR (Status)) {\r
3809 MediaPresent = Snp->Mode->MediaPresent;\r
3810 Snp->Shutdown (Snp);\r
3811 }\r
3812 Snp->Stop (Snp);\r
3813 }\r
3814 }\r
3815 } else {\r
3816 MediaPresent = TRUE;\r
3817 }\r
3818 }\r
3819 }\r
3820\r
3821 return MediaPresent;\r
3822}\r
3823\r
3824/**\r
3825 For a bootable Device path, return its boot type.\r
3826\r
3827 @param DevicePath The bootable device Path to check\r
3828\r
128efbbc 3829 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
11c5022d 3830 which subtype is MEDIA_HARDDRIVE_DP\r
3831 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
3832 which subtype is MEDIA_CDROM_DP\r
3833 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node\r
3834 which HID is floppy device.\r
3835 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3836 and its last device path node's subtype is MSG_ATAPI_DP.\r
3837 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3838 and its last device path node's subtype is MSG_SCSI_DP.\r
3839 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3840 and its last device path node's subtype is MSG_USB_DP.\r
5c08e117 3841 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r
11c5022d 3842 its last device path node point to a message device path node.\r
3843 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.\r
128efbbc 3844 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,\r
5c08e117 3845\r
3846**/\r
3847UINT32\r
3848EFIAPI\r
3849BdsGetBootTypeFromDevicePath (\r
3850 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3851 )\r
3852{\r
3853 ACPI_HID_DEVICE_PATH *Acpi;\r
3854 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3855 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
ff482c56 3856 UINT32 BootType;\r
5c08e117 3857\r
3858 if (NULL == DevicePath) {\r
3859 return BDS_EFI_UNSUPPORT;\r
3860 }\r
3861\r
3862 TempDevicePath = DevicePath;\r
3863\r
3864 while (!IsDevicePathEndType (TempDevicePath)) {\r
3865 switch (DevicePathType (TempDevicePath)) {\r
3866 case BBS_DEVICE_PATH:\r
3867 return BDS_LEGACY_BBS_BOOT;\r
3868 case MEDIA_DEVICE_PATH:\r
3869 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r
3870 return BDS_EFI_MEDIA_HD_BOOT;\r
3871 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r
3872 return BDS_EFI_MEDIA_CDROM_BOOT;\r
128efbbc 3873 }\r
5c08e117 3874 break;\r
3875 case ACPI_DEVICE_PATH:\r
3876 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r
3877 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r
3878 return BDS_EFI_ACPI_FLOPPY_BOOT;\r
3879 }\r
3880 break;\r
3881 case MESSAGING_DEVICE_PATH:\r
3882 //\r
3883 // Get the last device path node\r
3884 //\r
3885 LastDeviceNode = NextDevicePathNode (TempDevicePath);\r
3886 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r
3887 //\r
3888 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r
ff482c56 3889 // skip it\r
5c08e117 3890 //\r
3891 LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r
3892 }\r
3893 //\r
3894 // if the device path not only point to driver device, it is not a messaging device path,\r
3895 //\r
3896 if (!IsDevicePathEndType (LastDeviceNode)) {\r
128efbbc 3897 break;\r
5c08e117 3898 }\r
3899\r
ff482c56 3900 switch (DevicePathSubType (TempDevicePath)) {\r
3901 case MSG_ATAPI_DP:\r
3902 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;\r
3903 break;\r
3904\r
3905 case MSG_USB_DP:\r
3906 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r
3907 break;\r
3908\r
3909 case MSG_SCSI_DP:\r
3910 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;\r
3911 break;\r
3912\r
3913 case MSG_SATA_DP:\r
3914 BootType = BDS_EFI_MESSAGE_SATA_BOOT;\r
3915 break;\r
3916\r
3917 case MSG_MAC_ADDR_DP:\r
3918 case MSG_VLAN_DP:\r
a7a523e0 3919 case MSG_IPv4_DP:\r
3920 case MSG_IPv6_DP:\r
ff482c56 3921 BootType = BDS_EFI_MESSAGE_MAC_BOOT;\r
3922 break;\r
3923\r
3924 default:\r
3925 BootType = BDS_EFI_MESSAGE_MISC_BOOT;\r
3926 break;\r
5c08e117 3927 }\r
ff482c56 3928 return BootType;\r
3929\r
5c08e117 3930 default:\r
3931 break;\r
3932 }\r
3933 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
3934 }\r
3935\r
3936 return BDS_EFI_UNSUPPORT;\r
3937}\r
3938\r
3939/**\r
3940 Check whether the Device path in a boot option point to a valid bootable device,\r
3941 And if CheckMedia is true, check the device is ready to boot now.\r
3942\r
3943 @param DevPath the Device path in a boot option\r
3944 @param CheckMedia if true, check the device is ready to boot now.\r
3945\r
3946 @retval TRUE the Device path is valid\r
3947 @retval FALSE the Device path is invalid .\r
3948\r
3949**/\r
3950BOOLEAN\r
3951EFIAPI\r
3952BdsLibIsValidEFIBootOptDevicePath (\r
3953 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
3954 IN BOOLEAN CheckMedia\r
3955 )\r
3384a9bc 3956{\r
3957 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);\r
3958}\r
3959\r
3960/**\r
3961 Check whether the Device path in a boot option point to a valid bootable device,\r
3962 And if CheckMedia is true, check the device is ready to boot now.\r
3963 If Description is not NULL and the device path point to a fixed BlockIo\r
3964 device, check the description whether conflict with other auto-created\r
3965 boot options.\r
3966\r
3967 @param DevPath the Device path in a boot option\r
3968 @param CheckMedia if true, check the device is ready to boot now.\r
3969 @param Description the description in a boot option\r
3970\r
3971 @retval TRUE the Device path is valid\r
3972 @retval FALSE the Device path is invalid .\r
3973\r
3974**/\r
3975BOOLEAN\r
3976EFIAPI\r
3977BdsLibIsValidEFIBootOptDevicePathExt (\r
3978 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
3979 IN BOOLEAN CheckMedia,\r
3980 IN CHAR16 *Description\r
3981 )\r
5c08e117 3982{\r
3983 EFI_STATUS Status;\r
3984 EFI_HANDLE Handle;\r
3985 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3986 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
3987 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
3988\r
3989 TempDevicePath = DevPath;\r
3990 LastDeviceNode = DevPath;\r
128efbbc 3991\r
5c08e117 3992 //\r
a7a523e0 3993 // Check if it's a valid boot option for network boot device.\r
3994 // Check if there is EfiLoadFileProtocol installed. \r
3995 // If yes, that means there is a boot option for network.\r
5c08e117 3996 //\r
3997 Status = gBS->LocateDevicePath (\r
a7a523e0 3998 &gEfiLoadFileProtocolGuid,\r
5c08e117 3999 &TempDevicePath,\r
4000 &Handle\r
4001 );\r
4002 if (EFI_ERROR (Status)) {\r
4003 //\r
4004 // Device not present so see if we need to connect it\r
4005 //\r
4006 TempDevicePath = DevPath;\r
4007 BdsLibConnectDevicePath (TempDevicePath);\r
4008 Status = gBS->LocateDevicePath (\r
a7a523e0 4009 &gEfiLoadFileProtocolGuid,\r
5c08e117 4010 &TempDevicePath,\r
4011 &Handle\r
4012 );\r
4013 }\r
128efbbc 4014\r
5c08e117 4015 if (!EFI_ERROR (Status)) {\r
a7a523e0 4016 if (!IsDevicePathEnd (TempDevicePath)) {\r
4017 //\r
4018 // LoadFile protocol is not installed on handle with exactly the same DevPath\r
4019 //\r
4020 return FALSE;\r
4021 }\r
ff482c56 4022\r
a7a523e0 4023 if (CheckMedia) {\r
4024 //\r
4025 // Test if it is ready to boot now\r
4026 //\r
4027 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r
5c08e117 4028 return TRUE;\r
4029 }\r
a7a523e0 4030 } else {\r
4031 return TRUE;\r
4032 } \r
5c08e117 4033 }\r
4034\r
4035 //\r
4036 // If the boot option point to a file, it is a valid EFI boot option,\r
4037 // and assume it is ready to boot now\r
4038 //\r
4039 while (!IsDevicePathEnd (TempDevicePath)) {\r
7389fdd0 4040 //\r
4041 // If there is USB Class or USB WWID device path node, treat it as valid EFI\r
4042 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it\r
4043 // to full device path.\r
4044 //\r
4045 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r
4046 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||\r
4047 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {\r
4048 return TRUE;\r
4049 }\r
4050\r
4051 LastDeviceNode = TempDevicePath;\r
4052 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
5c08e117 4053 }\r
4054 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
4055 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
4056 return TRUE;\r
4057 }\r
4058\r
4059 //\r
6617d838 4060 // Check if it's a valid boot option for internal FV application\r
5c08e117 4061 //\r
4062 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r
4063 //\r
6617d838 4064 // If the boot option point to internal FV application, make sure it is valid\r
5c08e117 4065 //\r
128efbbc 4066 TempDevicePath = DevPath;\r
6617d838
RN
4067 Status = BdsLibUpdateFvFileDevicePath (\r
4068 &TempDevicePath,\r
4069 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode)\r
4070 );\r
5c08e117 4071 if (Status == EFI_ALREADY_STARTED) {\r
4072 return TRUE;\r
4073 } else {\r
4074 if (Status == EFI_SUCCESS) {\r
128efbbc 4075 FreePool (TempDevicePath);\r
5c08e117 4076 }\r
4077 return FALSE;\r
4078 }\r
4079 }\r
128efbbc 4080\r
5c08e117 4081 //\r
3384a9bc 4082 // If the boot option point to a blockIO device:\r
8d3b5aff 4083 // if it is a removable blockIo device, it is valid.\r
128efbbc 4084 // if it is a fixed blockIo device, check its description confliction.\r
5c08e117 4085 //\r
4086 TempDevicePath = DevPath;\r
4087 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
4088 if (EFI_ERROR (Status)) {\r
4089 //\r
4090 // Device not present so see if we need to connect it\r
4091 //\r
4092 Status = BdsLibConnectDevicePath (DevPath);\r
4093 if (!EFI_ERROR (Status)) {\r
4094 //\r
4095 // Try again to get the Block Io protocol after we did the connect\r
4096 //\r
4097 TempDevicePath = DevPath;\r
4098 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
4099 }\r
4100 }\r
128efbbc 4101\r
5c08e117 4102 if (!EFI_ERROR (Status)) {\r
4103 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
4104 if (!EFI_ERROR (Status)) {\r
4105 if (CheckMedia) {\r
4106 //\r
4107 // Test if it is ready to boot now\r
4108 //\r
4109 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
4110 return TRUE;\r
4111 }\r
4112 } else {\r
4113 return TRUE;\r
4114 }\r
4115 }\r
4116 } else {\r
4117 //\r
4118 // if the boot option point to a simple file protocol which does not consume block Io protocol, it is also a valid EFI boot option,\r
4119 //\r
4120 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r
4121 if (!EFI_ERROR (Status)) {\r
4122 if (CheckMedia) {\r
4123 //\r
4124 // Test if it is ready to boot now\r
4125 //\r
4126 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
4127 return TRUE;\r
4128 }\r
4129 } else {\r
4130 return TRUE;\r
4131 }\r
4132 }\r
4133 }\r
4134\r
4135 return FALSE;\r
4136}\r
4137\r
4138\r
4139/**\r
4140 According to a file guild, check a Fv file device path is valid. If it is invalid,\r
4141 try to return the valid device path.\r
4142 FV address maybe changes for memory layout adjust from time to time, use this function\r
4143 could promise the Fv file device path is right.\r
4144\r
4145 @param DevicePath on input, the Fv file device path need to check on\r
4146 output, the updated valid Fv file device path\r
4147 @param FileGuid the Fv file guild\r
4148\r
4149 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r
4150 parameter\r
4151 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r
4152 guild at all\r
4153 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r
4154 valid\r
4155 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r
4156 and return the updated device path in DevicePath\r
4157\r
4158**/\r
4159EFI_STATUS\r
4160EFIAPI\r
4161BdsLibUpdateFvFileDevicePath (\r
4162 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r
4163 IN EFI_GUID *FileGuid\r
4164 )\r
4165{\r
4166 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
4167 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
4168 EFI_STATUS Status;\r
4169 EFI_GUID *GuidPoint;\r
4170 UINTN Index;\r
4171 UINTN FvHandleCount;\r
4172 EFI_HANDLE *FvHandleBuffer;\r
4173 EFI_FV_FILETYPE Type;\r
4174 UINTN Size;\r
4175 EFI_FV_FILE_ATTRIBUTES Attributes;\r
4176 UINT32 AuthenticationStatus;\r
4177 BOOLEAN FindFvFile;\r
4178 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
4179 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
4180 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
4181 EFI_HANDLE FoundFvHandle;\r
4182 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
4183\r
4184 if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
4185 return EFI_INVALID_PARAMETER;\r
4186 }\r
4187 if (FileGuid == NULL) {\r
4188 return EFI_INVALID_PARAMETER;\r
4189 }\r
128efbbc 4190\r
5c08e117 4191 //\r
4192 // Check whether the device path point to the default the input Fv file\r
4193 //\r
4194 TempDevicePath = *DevicePath;\r
4195 LastDeviceNode = TempDevicePath;\r
4196 while (!IsDevicePathEnd (TempDevicePath)) {\r
4197 LastDeviceNode = TempDevicePath;\r
4198 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
4199 }\r
4200 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
4201 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
4202 );\r
4203 if (GuidPoint == NULL) {\r
4204 //\r
4205 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
4206 //\r
4207 return EFI_UNSUPPORTED;\r
4208 }\r
4209 if (!CompareGuid (GuidPoint, FileGuid)) {\r
4210 //\r
4211 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
4212 //\r
4213 return EFI_UNSUPPORTED;\r
4214 }\r
4215\r
4216 //\r
4217 // Check whether the input Fv file device path is valid\r
4218 //\r
4219 TempDevicePath = *DevicePath;\r
4220 FoundFvHandle = NULL;\r
4221 Status = gBS->LocateDevicePath (\r
4222 &gEfiFirmwareVolume2ProtocolGuid,\r
4223 &TempDevicePath,\r
4224 &FoundFvHandle\r
4225 );\r
4226 if (!EFI_ERROR (Status)) {\r
4227 Status = gBS->HandleProtocol (\r
4228 FoundFvHandle,\r
4229 &gEfiFirmwareVolume2ProtocolGuid,\r
4230 (VOID **) &Fv\r
4231 );\r
4232 if (!EFI_ERROR (Status)) {\r
4233 //\r
4234 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
4235 //\r
4236 Status = Fv->ReadFile (\r
4237 Fv,\r
4238 FileGuid,\r
4239 NULL,\r
4240 &Size,\r
4241 &Type,\r
4242 &Attributes,\r
4243 &AuthenticationStatus\r
4244 );\r
4245 if (!EFI_ERROR (Status)) {\r
4246 return EFI_ALREADY_STARTED;\r
4247 }\r
4248 }\r
4249 }\r
4250\r
4251 //\r
4252 // Look for the input wanted FV file in current FV\r
4253 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r
4254 //\r
4255 FindFvFile = FALSE;\r
4256 FoundFvHandle = NULL;\r
4257 Status = gBS->HandleProtocol (\r
fefefa4c 4258 gImageHandle,\r
5c08e117 4259 &gEfiLoadedImageProtocolGuid,\r
4260 (VOID **) &LoadedImage\r
4261 );\r
4262 if (!EFI_ERROR (Status)) {\r
4263 Status = gBS->HandleProtocol (\r
4264 LoadedImage->DeviceHandle,\r
4265 &gEfiFirmwareVolume2ProtocolGuid,\r
4266 (VOID **) &Fv\r
4267 );\r
4268 if (!EFI_ERROR (Status)) {\r
4269 Status = Fv->ReadFile (\r
4270 Fv,\r
4271 FileGuid,\r
4272 NULL,\r
4273 &Size,\r
4274 &Type,\r
4275 &Attributes,\r
4276 &AuthenticationStatus\r
4277 );\r
4278 if (!EFI_ERROR (Status)) {\r
4279 FindFvFile = TRUE;\r
4280 FoundFvHandle = LoadedImage->DeviceHandle;\r
4281 }\r
4282 }\r
4283 }\r
4284 //\r
4285 // Second, if fail to find, try to enumerate all FV\r
4286 //\r
4287 if (!FindFvFile) {\r
4288 FvHandleBuffer = NULL;\r
4289 gBS->LocateHandleBuffer (\r
4290 ByProtocol,\r
4291 &gEfiFirmwareVolume2ProtocolGuid,\r
4292 NULL,\r
4293 &FvHandleCount,\r
4294 &FvHandleBuffer\r
4295 );\r
4296 for (Index = 0; Index < FvHandleCount; Index++) {\r
4297 gBS->HandleProtocol (\r
4298 FvHandleBuffer[Index],\r
4299 &gEfiFirmwareVolume2ProtocolGuid,\r
4300 (VOID **) &Fv\r
4301 );\r
4302\r
4303 Status = Fv->ReadFile (\r
4304 Fv,\r
4305 FileGuid,\r
4306 NULL,\r
4307 &Size,\r
4308 &Type,\r
4309 &Attributes,\r
4310 &AuthenticationStatus\r
4311 );\r
4312 if (EFI_ERROR (Status)) {\r
4313 //\r
4314 // Skip if input Fv file not in the FV\r
4315 //\r
4316 continue;\r
4317 }\r
4318 FindFvFile = TRUE;\r
4319 FoundFvHandle = FvHandleBuffer[Index];\r
4320 break;\r
4321 }\r
4322\r
4323 if (FvHandleBuffer != NULL) {\r
128efbbc 4324 FreePool (FvHandleBuffer);\r
5c08e117 4325 }\r
4326 }\r
4327\r
4328 if (FindFvFile) {\r
4329 //\r
4330 // Build the shell device path\r
4331 //\r
4332 NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r
4333 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r
4334 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r
4335 *DevicePath = NewDevicePath;\r
4336 return EFI_SUCCESS;\r
4337 }\r
4338 return EFI_NOT_FOUND;\r
4339}\r