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