]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
Add missing status code in several modules.
[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
26c0ba77 1667 EFI_EVENT LegacyBootEvent;\r
5c08e117 1668\r
1669 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);\r
1670 if (EFI_ERROR (Status)) {\r
1671 //\r
1672 // If no LegacyBios protocol we do not support legacy boot\r
1673 //\r
1674 return EFI_UNSUPPORTED;\r
1675 }\r
1676 //\r
1677 // Notes: if we separate the int 19, then we don't need to refresh BBS\r
1678 //\r
1679 BdsRefreshBbsTableForBoot (Option);\r
1680\r
1681 //\r
cd6a3b15 1682 // Write boot to OS performance data for legacy boot.\r
5c08e117 1683 //\r
1684 PERF_CODE (\r
26c0ba77
SZ
1685 //\r
1686 // Create an event to be signalled when Legacy Boot occurs to write performance data.\r
1687 //\r
1688 Status = EfiCreateEventLegacyBootEx(\r
1689 TPL_NOTIFY,\r
1690 WriteBootToOsPerformanceData,\r
1691 NULL, \r
1692 &LegacyBootEvent\r
1693 );\r
1694 ASSERT_EFI_ERROR (Status);\r
5c08e117 1695 );\r
1696\r
1697 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Legacy Boot: %S\n", Option->Description));\r
1698 return LegacyBios->LegacyBoot (\r
1699 LegacyBios,\r
1700 (BBS_BBS_DEVICE_PATH *) Option->DevicePath,\r
1701 Option->LoadOptionsSize,\r
1702 Option->LoadOptions\r
1703 );\r
1704}\r
1705\r
3d7decbc 1706/**\r
1707 Internal function to check if the input boot option is a valid EFI NV Boot####.\r
1708\r
1709 @param OptionToCheck Boot option to be checked.\r
1710\r
1711 @retval TRUE This boot option matches a valid EFI NV Boot####.\r
1712 @retval FALSE If not.\r
128efbbc 1713\r
3d7decbc 1714**/\r
3d7decbc 1715BOOLEAN\r
1716IsBootOptionValidNVVarialbe (\r
1717 IN BDS_COMMON_OPTION *OptionToCheck\r
1718 )\r
1719{\r
1720 LIST_ENTRY TempList;\r
1721 BDS_COMMON_OPTION *BootOption;\r
1722 BOOLEAN Valid;\r
1723 CHAR16 OptionName[20];\r
1724\r
1725 Valid = FALSE;\r
1726\r
1727 InitializeListHead (&TempList);\r
1728 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionToCheck->BootCurrent);\r
5c08e117 1729\r
3d7decbc 1730 BootOption = BdsLibVariableToOption (&TempList, OptionName);\r
1731 if (BootOption == NULL) {\r
1732 return FALSE;\r
1733 }\r
1734\r
1735 //\r
128efbbc 1736 // If the Boot Option Number and Device Path matches, OptionToCheck matches a\r
3d7decbc 1737 // valid EFI NV Boot####.\r
1738 //\r
1739 if ((OptionToCheck->BootCurrent == BootOption->BootCurrent) &&\r
1740 (CompareMem (OptionToCheck->DevicePath, BootOption->DevicePath, GetDevicePathSize (OptionToCheck->DevicePath)) == 0))\r
1741 {\r
1742 Valid = TRUE;\r
1743 }\r
1744\r
1745 FreePool (BootOption);\r
128efbbc 1746\r
3d7decbc 1747 return Valid;\r
1748}\r
7389fdd0 1749\r
1750/**\r
1751 Check whether a USB device match the specified USB Class device path. This\r
1752 function follows "Load Option Processing" behavior in UEFI specification.\r
1753\r
1754 @param UsbIo USB I/O protocol associated with the USB device.\r
1755 @param UsbClass The USB Class device path to match.\r
1756\r
1757 @retval TRUE The USB device match the USB Class device path.\r
1758 @retval FALSE The USB device does not match the USB Class device path.\r
1759\r
1760**/\r
1761BOOLEAN\r
1762BdsMatchUsbClass (\r
1763 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
1764 IN USB_CLASS_DEVICE_PATH *UsbClass\r
1765 )\r
1766{\r
1767 EFI_STATUS Status;\r
1768 EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r
1769 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;\r
1770 UINT8 DeviceClass;\r
1771 UINT8 DeviceSubClass;\r
1772 UINT8 DeviceProtocol;\r
1773\r
1774 if ((DevicePathType (UsbClass) != MESSAGING_DEVICE_PATH) ||\r
1775 (DevicePathSubType (UsbClass) != MSG_USB_CLASS_DP)){\r
1776 return FALSE;\r
1777 }\r
1778\r
1779 //\r
1780 // Check Vendor Id and Product Id.\r
1781 //\r
1782 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);\r
1783 if (EFI_ERROR (Status)) {\r
1784 return FALSE;\r
1785 }\r
1786\r
1787 if ((UsbClass->VendorId != 0xffff) &&\r
1788 (UsbClass->VendorId != DevDesc.IdVendor)) {\r
1789 return FALSE;\r
1790 }\r
1791\r
1792 if ((UsbClass->ProductId != 0xffff) &&\r
1793 (UsbClass->ProductId != DevDesc.IdProduct)) {\r
1794 return FALSE;\r
1795 }\r
1796\r
1797 DeviceClass = DevDesc.DeviceClass;\r
1798 DeviceSubClass = DevDesc.DeviceSubClass;\r
1799 DeviceProtocol = DevDesc.DeviceProtocol;\r
1800 if (DeviceClass == 0) {\r
1801 //\r
1802 // If Class in Device Descriptor is set to 0, use the Class, SubClass and\r
1803 // Protocol in Interface Descriptor instead.\r
1804 //\r
1805 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);\r
1806 if (EFI_ERROR (Status)) {\r
1807 return FALSE;\r
1808 }\r
1809\r
1810 DeviceClass = IfDesc.InterfaceClass;\r
1811 DeviceSubClass = IfDesc.InterfaceSubClass;\r
1812 DeviceProtocol = IfDesc.InterfaceProtocol;\r
1813 }\r
1814\r
1815 //\r
1816 // Check Class, SubClass and Protocol.\r
1817 //\r
1818 if ((UsbClass->DeviceClass != 0xff) &&\r
1819 (UsbClass->DeviceClass != DeviceClass)) {\r
1820 return FALSE;\r
1821 }\r
1822\r
1823 if ((UsbClass->DeviceSubClass != 0xff) &&\r
1824 (UsbClass->DeviceSubClass != DeviceSubClass)) {\r
1825 return FALSE;\r
1826 }\r
1827\r
1828 if ((UsbClass->DeviceProtocol != 0xff) &&\r
1829 (UsbClass->DeviceProtocol != DeviceProtocol)) {\r
1830 return FALSE;\r
1831 }\r
1832\r
1833 return TRUE;\r
1834}\r
1835\r
1836/**\r
1837 Check whether a USB device match the specified USB WWID device path. This\r
1838 function follows "Load Option Processing" behavior in UEFI specification.\r
1839\r
1840 @param UsbIo USB I/O protocol associated with the USB device.\r
1841 @param UsbWwid The USB WWID device path to match.\r
1842\r
1843 @retval TRUE The USB device match the USB WWID device path.\r
1844 @retval FALSE The USB device does not match the USB WWID device path.\r
1845\r
1846**/\r
1847BOOLEAN\r
1848BdsMatchUsbWwid (\r
1849 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
1850 IN USB_WWID_DEVICE_PATH *UsbWwid\r
1851 )\r
1852{\r
1853 EFI_STATUS Status;\r
1854 EFI_USB_DEVICE_DESCRIPTOR DevDesc;\r
1855 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;\r
1856 UINT16 *LangIdTable;\r
1857 UINT16 TableSize;\r
1858 UINT16 Index;\r
1859 CHAR16 *CompareStr;\r
1860 UINTN CompareLen;\r
1861 CHAR16 *SerialNumberStr;\r
1862 UINTN Length;\r
1863\r
1864 if ((DevicePathType (UsbWwid) != MESSAGING_DEVICE_PATH) ||\r
1865 (DevicePathSubType (UsbWwid) != MSG_USB_WWID_DP )){\r
1866 return FALSE;\r
1867 }\r
1868\r
1869 //\r
1870 // Check Vendor Id and Product Id.\r
1871 //\r
1872 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);\r
1873 if (EFI_ERROR (Status)) {\r
1874 return FALSE;\r
1875 }\r
1876 if ((DevDesc.IdVendor != UsbWwid->VendorId) ||\r
1877 (DevDesc.IdProduct != UsbWwid->ProductId)) {\r
1878 return FALSE;\r
1879 }\r
1880\r
1881 //\r
1882 // Check Interface Number.\r
1883 //\r
1884 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);\r
1885 if (EFI_ERROR (Status)) {\r
1886 return FALSE;\r
1887 }\r
1888 if (IfDesc.InterfaceNumber != UsbWwid->InterfaceNumber) {\r
1889 return FALSE;\r
1890 }\r
1891\r
1892 //\r
1893 // Check Serial Number.\r
1894 //\r
1895 if (DevDesc.StrSerialNumber == 0) {\r
1896 return FALSE;\r
1897 }\r
1898\r
1899 //\r
1900 // Get all supported languages.\r
1901 //\r
1902 TableSize = 0;\r
1903 LangIdTable = NULL;\r
1904 Status = UsbIo->UsbGetSupportedLanguages (UsbIo, &LangIdTable, &TableSize);\r
1905 if (EFI_ERROR (Status) || (TableSize == 0) || (LangIdTable == NULL)) {\r
1906 return FALSE;\r
1907 }\r
1908\r
1909 //\r
1910 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.\r
1911 //\r
1912 CompareStr = (CHAR16 *) (UINTN) (UsbWwid + 1);\r
1913 CompareLen = (DevicePathNodeLength (UsbWwid) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16);\r
1914 if (CompareStr[CompareLen - 1] == L'\0') {\r
1915 CompareLen--;\r
1916 }\r
1917\r
1918 //\r
1919 // Compare serial number in each supported language.\r
1920 //\r
1921 for (Index = 0; Index < TableSize / sizeof (UINT16); Index++) {\r
1922 SerialNumberStr = NULL;\r
1923 Status = UsbIo->UsbGetStringDescriptor (\r
1924 UsbIo,\r
1925 LangIdTable[Index],\r
1926 DevDesc.StrSerialNumber,\r
1927 &SerialNumberStr\r
1928 );\r
1929 if (EFI_ERROR (Status) || (SerialNumberStr == NULL)) {\r
1930 continue;\r
1931 }\r
1932\r
1933 Length = StrLen (SerialNumberStr);\r
1934 if ((Length >= CompareLen) &&\r
1935 (CompareMem (SerialNumberStr + Length - CompareLen, CompareStr, CompareLen * sizeof (CHAR16)) == 0)) {\r
1936 FreePool (SerialNumberStr);\r
1937 return TRUE;\r
1938 }\r
1939\r
1940 FreePool (SerialNumberStr);\r
1941 }\r
1942\r
1943 return FALSE;\r
1944}\r
1945\r
1946/**\r
9972247d 1947 Find a USB device path which match the specified short-form device path start\r
1948 with USB Class or USB WWID device path and load the boot file then return the \r
1949 image handle. If ParentDevicePath is NULL, this function will search in all USB\r
1950 devices of the platform. If ParentDevicePath is not NULL,this function will only\r
1951 search in its child devices.\r
7389fdd0 1952\r
1953 @param ParentDevicePath The device path of the parent.\r
1954 @param ShortFormDevicePath The USB Class or USB WWID device path to match.\r
1955\r
9972247d 1956 @return The image Handle if find load file from specified short-form device path\r
1957 or NULL if not found.\r
7389fdd0 1958\r
1959**/\r
1960EFI_HANDLE *\r
1961BdsFindUsbDevice (\r
1962 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,\r
1963 IN EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath\r
1964 )\r
1965{\r
1966 EFI_STATUS Status;\r
1967 UINTN UsbIoHandleCount;\r
1968 EFI_HANDLE *UsbIoHandleBuffer;\r
1969 EFI_DEVICE_PATH_PROTOCOL *UsbIoDevicePath;\r
1970 EFI_USB_IO_PROTOCOL *UsbIo;\r
1971 UINTN Index;\r
1972 UINTN ParentSize;\r
1973 UINTN Size;\r
9972247d 1974 EFI_HANDLE ImageHandle;\r
1975 EFI_HANDLE Handle;\r
1976 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r
1977 EFI_DEVICE_PATH_PROTOCOL *NextDevicePath;\r
1978\r
1979 FullDevicePath = NULL;\r
1980 ImageHandle = NULL;\r
7389fdd0 1981\r
1982 //\r
1983 // Get all UsbIo Handles.\r
1984 //\r
1985 UsbIoHandleCount = 0;\r
1986 UsbIoHandleBuffer = NULL;\r
1987 Status = gBS->LocateHandleBuffer (\r
1988 ByProtocol,\r
1989 &gEfiUsbIoProtocolGuid,\r
1990 NULL,\r
1991 &UsbIoHandleCount,\r
1992 &UsbIoHandleBuffer\r
1993 );\r
1994 if (EFI_ERROR (Status) || (UsbIoHandleCount == 0) || (UsbIoHandleBuffer == NULL)) {\r
1995 return NULL;\r
1996 }\r
1997\r
7389fdd0 1998 ParentSize = (ParentDevicePath == NULL) ? 0 : GetDevicePathSize (ParentDevicePath);\r
1999 for (Index = 0; Index < UsbIoHandleCount; Index++) {\r
2000 //\r
2001 // Get the Usb IO interface.\r
2002 //\r
2003 Status = gBS->HandleProtocol(\r
2004 UsbIoHandleBuffer[Index],\r
2005 &gEfiUsbIoProtocolGuid,\r
2006 (VOID **) &UsbIo\r
2007 );\r
2008 if (EFI_ERROR (Status)) {\r
2009 continue;\r
2010 }\r
2011\r
9972247d 2012 UsbIoDevicePath = DevicePathFromHandle (UsbIoHandleBuffer[Index]);\r
2013 if (UsbIoDevicePath == NULL) {\r
2014 continue;\r
2015 }\r
2016\r
7389fdd0 2017 if (ParentDevicePath != NULL) {\r
2018 //\r
2019 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.\r
2020 //\r
7389fdd0 2021 Size = GetDevicePathSize (UsbIoDevicePath);\r
2022 if ((Size < ParentSize) ||\r
2023 (CompareMem (UsbIoDevicePath, ParentDevicePath, ParentSize - END_DEVICE_PATH_LENGTH) != 0)) {\r
2024 continue;\r
2025 }\r
2026 }\r
2027\r
2028 if (BdsMatchUsbClass (UsbIo, (USB_CLASS_DEVICE_PATH *) ShortFormDevicePath) ||\r
2029 BdsMatchUsbWwid (UsbIo, (USB_WWID_DEVICE_PATH *) ShortFormDevicePath)) {\r
9972247d 2030 //\r
2031 // Try to find if there is the boot file in this DevicePath\r
2032 //\r
2033 NextDevicePath = NextDevicePathNode (ShortFormDevicePath);\r
2034 if (!IsDevicePathEnd (NextDevicePath)) {\r
2035 FullDevicePath = AppendDevicePath (UsbIoDevicePath, NextDevicePath);\r
2036 //\r
2037 // Connect the full device path, so that Simple File System protocol\r
2038 // could be installed for this USB device.\r
2039 //\r
2040 BdsLibConnectDevicePath (FullDevicePath);\r
79b7a6a1 2041 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
9972247d 2042 Status = gBS->LoadImage (\r
2043 TRUE,\r
2044 gImageHandle,\r
2045 FullDevicePath,\r
2046 NULL,\r
2047 0,\r
2048 &ImageHandle\r
2049 );\r
2050 FreePool (FullDevicePath);\r
2051 } else {\r
2052 FullDevicePath = UsbIoDevicePath;\r
2053 Status = EFI_NOT_FOUND;\r
2054 }\r
2055\r
2056 //\r
2057 // If we didn't find an image directly, we need to try as if it is a removable device boot option\r
2058 // and load the image according to the default boot behavior for removable device.\r
2059 //\r
2060 if (EFI_ERROR (Status)) {\r
2061 //\r
2062 // check if there is a bootable removable media could be found in this device path ,\r
2063 // and get the bootable media handle\r
2064 //\r
2065 Handle = BdsLibGetBootableHandle(UsbIoDevicePath);\r
2066 if (Handle == NULL) {\r
2067 continue;\r
2068 }\r
2069 //\r
2070 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
2071 // machinename is ia32, ia64, x64, ...\r
2072 //\r
2073 FullDevicePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
2074 if (FullDevicePath != NULL) {\r
79b7a6a1 2075 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
9972247d 2076 Status = gBS->LoadImage (\r
2077 TRUE,\r
2078 gImageHandle,\r
2079 FullDevicePath,\r
2080 NULL,\r
2081 0,\r
2082 &ImageHandle\r
2083 );\r
2084 if (EFI_ERROR (Status)) {\r
2085 //\r
2086 // The DevicePath failed, and it's not a valid\r
2087 // removable media device.\r
2088 //\r
2089 continue;\r
2090 }\r
2091 } else {\r
2092 continue;\r
2093 }\r
2094 }\r
7389fdd0 2095 break;\r
2096 }\r
2097 }\r
2098\r
2099 FreePool (UsbIoHandleBuffer);\r
9972247d 2100 return ImageHandle;\r
7389fdd0 2101}\r
2102\r
2103/**\r
2104 Expand USB Class or USB WWID device path node to be full device path of a USB\r
9972247d 2105 device in platform then load the boot file on this full device path and return the \r
2106 image handle.\r
7389fdd0 2107\r
2108 This function support following 4 cases:\r
2109 1) Boot Option device path starts with a USB Class or USB WWID device path,\r
2110 and there is no Media FilePath device path in the end.\r
2111 In this case, it will follow Removable Media Boot Behavior.\r
2112 2) Boot Option device path starts with a USB Class or USB WWID device path,\r
2113 and ended with Media FilePath device path.\r
2114 3) Boot Option device path starts with a full device path to a USB Host Controller,\r
2115 contains a USB Class or USB WWID device path node, while not ended with Media\r
2116 FilePath device path. In this case, it will follow Removable Media Boot Behavior.\r
2117 4) Boot Option device path starts with a full device path to a USB Host Controller,\r
2118 contains a USB Class or USB WWID device path node, and ended with Media\r
2119 FilePath device path.\r
2120\r
2121 @param DevicePath The Boot Option device path.\r
2122\r
9972247d 2123 @return The image handle of boot file, or NULL if there is no boot file found in\r
2124 the specified USB Class or USB WWID device path.\r
7389fdd0 2125\r
2126**/\r
9972247d 2127EFI_HANDLE *\r
7389fdd0 2128BdsExpandUsbShortFormDevicePath (\r
2129 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
2130 )\r
2131{\r
9972247d 2132 EFI_HANDLE *ImageHandle;\r
7389fdd0 2133 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
7389fdd0 2134 EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath;\r
2135\r
2136 //\r
2137 // Search for USB Class or USB WWID device path node.\r
2138 //\r
2139 ShortFormDevicePath = NULL;\r
9972247d 2140 ImageHandle = NULL;\r
2141 TempDevicePath = DevicePath;\r
7389fdd0 2142 while (!IsDevicePathEnd (TempDevicePath)) {\r
2143 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r
2144 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||\r
2145 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {\r
2146 ShortFormDevicePath = TempDevicePath;\r
2147 break;\r
2148 }\r
7389fdd0 2149 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
2150 }\r
2151\r
2152 if (ShortFormDevicePath == NULL) {\r
2153 //\r
2154 // No USB Class or USB WWID device path node found, do nothing.\r
2155 //\r
2156 return NULL;\r
2157 }\r
2158\r
2159 if (ShortFormDevicePath == DevicePath) {\r
2160 //\r
2161 // Boot Option device path starts with USB Class or USB WWID device path.\r
2162 //\r
9972247d 2163 ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);\r
2164 if (ImageHandle == NULL) {\r
7389fdd0 2165 //\r
2166 // Failed to find a match in existing devices, connect the short form USB\r
2167 // device path and try again.\r
2168 //\r
2169 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath);\r
9972247d 2170 ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);\r
7389fdd0 2171 }\r
2172 } else {\r
2173 //\r
2174 // Boot Option device path contains USB Class or USB WWID device path node.\r
2175 //\r
2176\r
2177 //\r
2178 // Prepare the parent device path for search.\r
2179 //\r
2180 TempDevicePath = DuplicateDevicePath (DevicePath);\r
2181 ASSERT (TempDevicePath != NULL);\r
2182 SetDevicePathEndNode (((UINT8 *) TempDevicePath) + ((UINTN) ShortFormDevicePath - (UINTN) DevicePath));\r
2183\r
2184 //\r
9972247d 2185 // The USB Host Controller device path is already in Boot Option device path\r
7389fdd0 2186 // and USB Bus driver already support RemainingDevicePath starts with USB\r
2187 // Class or USB WWID device path, so just search in existing USB devices and\r
2188 // doesn't perform ConnectController here.\r
2189 //\r
9972247d 2190 ImageHandle = BdsFindUsbDevice (TempDevicePath, ShortFormDevicePath);\r
7389fdd0 2191 FreePool (TempDevicePath);\r
2192 }\r
2193\r
9972247d 2194 return ImageHandle;\r
7389fdd0 2195}\r
2196\r
5c08e117 2197/**\r
2198 Process the boot option follow the UEFI specification and\r
2199 special treat the legacy boot option with BBS_DEVICE_PATH.\r
2200\r
2201 @param Option The boot option need to be processed\r
2202 @param DevicePath The device path which describe where to load the\r
2203 boot image or the legacy BBS device path to boot\r
2204 the legacy OS\r
2205 @param ExitDataSize The size of exit data.\r
2206 @param ExitData Data returned when Boot image failed.\r
2207\r
2208 @retval EFI_SUCCESS Boot from the input boot option successfully.\r
2209 @retval EFI_NOT_FOUND If the Device Path is not found in the system\r
2210\r
2211**/\r
2212EFI_STATUS\r
2213EFIAPI\r
2214BdsLibBootViaBootOption (\r
2215 IN BDS_COMMON_OPTION *Option,\r
2216 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
2217 OUT UINTN *ExitDataSize,\r
2218 OUT CHAR16 **ExitData OPTIONAL\r
2219 )\r
2220{\r
2221 EFI_STATUS Status;\r
2df686c6 2222 EFI_STATUS StatusLogo;\r
5c08e117 2223 EFI_HANDLE Handle;\r
2224 EFI_HANDLE ImageHandle;\r
2225 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
2226 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
2227 EFI_DEVICE_PATH_PROTOCOL *WorkingDevicePath;\r
2228 EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;\r
2229 LIST_ENTRY TempBootLists;\r
2df686c6 2230 EFI_BOOT_LOGO_PROTOCOL *BootLogo;\r
5c08e117 2231\r
26c0ba77
SZ
2232 PERF_CODE (\r
2233 AllocateMemoryForPerformanceData ();\r
2234 );\r
5c08e117 2235\r
2236 *ExitDataSize = 0;\r
2237 *ExitData = NULL;\r
2238\r
5c08e117 2239 //\r
2240 // Notes: this code can be remove after the s3 script table\r
2241 // hook on the event EVT_SIGNAL_READY_TO_BOOT or\r
2242 // EVT_SIGNAL_LEGACY_BOOT\r
2243 //\r
2244 Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);\r
2245 if (!EFI_ERROR (Status)) {\r
2246 AcpiS3Save->S3Save (AcpiS3Save, NULL);\r
2247 }\r
2248 //\r
2249 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a\r
2250 // full device path\r
2251 //\r
2252 WorkingDevicePath = NULL;\r
2253 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
2254 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {\r
2255 WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r
2256 (HARDDRIVE_DEVICE_PATH *)DevicePath\r
2257 );\r
2258 if (WorkingDevicePath != NULL) {\r
2259 DevicePath = WorkingDevicePath;\r
2260 }\r
2261 }\r
7389fdd0 2262\r
5c08e117 2263 //\r
2264 // Set Boot Current\r
2265 //\r
3d7decbc 2266 if (IsBootOptionValidNVVarialbe (Option)) {\r
2267 //\r
2268 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.\r
2269 // In this case, "BootCurrent" is not created.\r
2270 // Only create the BootCurrent variable when it points to a valid Boot#### variable.\r
2271 //\r
2272 gRT->SetVariable (\r
2273 L"BootCurrent",\r
2274 &gEfiGlobalVariableGuid,\r
2275 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
2276 sizeof (UINT16),\r
2277 &Option->BootCurrent\r
2278 );\r
2279 }\r
5c08e117 2280\r
cb38c322 2281 //\r
2282 // Report Status Code to indicate ReadyToBoot event will be signalled\r
2283 //\r
2284 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT));\r
2285\r
72861c22 2286 //\r
2287 // Signal the EVT_SIGNAL_READY_TO_BOOT event\r
2288 //\r
2289 EfiSignalEventReadyToBoot();\r
2290\r
2291 //\r
2292 // Expand USB Class or USB WWID device path node to be full device path of a USB\r
2293 // device in platform then load the boot file on this full device path and get the\r
2294 // image handle.\r
2295 //\r
2296 ImageHandle = BdsExpandUsbShortFormDevicePath (DevicePath);\r
2297\r
2298 //\r
2299 // Adjust the different type memory page number just before booting\r
2300 // and save the updated info into the variable for next boot to use\r
2301 //\r
2302 BdsSetMemoryTypeInformationVariable ();\r
2303\r
5c08e117 2304 //\r
9972247d 2305 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.\r
2306 // Here get the ImageHandle for the non USB class or WWID device path.\r
5c08e117 2307 //\r
9972247d 2308 if (ImageHandle == NULL) {\r
2309 ASSERT (Option->DevicePath != NULL);\r
2310 if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&\r
2311 (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)\r
2312 ) {\r
2313 //\r
2314 // Check to see if we should legacy BOOT. If yes then do the legacy boot\r
2315 //\r
2316 return BdsLibDoLegacyBoot (Option);\r
5c08e117 2317 }\r
128efbbc 2318\r
5c08e117 2319 //\r
9972247d 2320 // If the boot option point to Internal FV shell, make sure it is valid\r
5c08e117 2321 //\r
9972247d 2322 Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));\r
2323 if (!EFI_ERROR(Status)) {\r
2324 if (Option->DevicePath != NULL) {\r
2325 FreePool(Option->DevicePath);\r
2326 }\r
2327 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
2328 ASSERT(Option->DevicePath != NULL);\r
2329 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
2330 //\r
2331 // Update the shell boot option\r
2332 //\r
2333 InitializeListHead (&TempBootLists);\r
2334 BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
2335\r
2336 //\r
2337 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r
2338 //\r
2339 FreePool (DevicePath);\r
2340 DevicePath = Option->DevicePath;\r
2341 }\r
5c08e117 2342\r
9972247d 2343 DEBUG_CODE_BEGIN();\r
5c08e117 2344\r
9aa7ba01 2345 if (Option->Description == NULL) {\r
2346 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));\r
2347 } else {\r
2348 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));\r
ab8cc80b 2349 }\r
9aa7ba01 2350 \r
9972247d 2351 DEBUG_CODE_END();\r
ab8cc80b 2352 \r
79b7a6a1 2353 //\r
2354 // Report status code for OS Loader LoadImage.\r
2355 //\r
2356 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
9972247d 2357 Status = gBS->LoadImage (\r
2358 TRUE,\r
2359 gImageHandle,\r
2360 DevicePath,\r
2361 NULL,\r
2362 0,\r
2363 &ImageHandle\r
2364 );\r
5c08e117 2365\r
5c08e117 2366 //\r
9972247d 2367 // If we didn't find an image directly, we need to try as if it is a removable device boot option\r
2368 // and load the image according to the default boot behavior for removable device.\r
5c08e117 2369 //\r
9972247d 2370 if (EFI_ERROR (Status)) {\r
2371 //\r
2372 // check if there is a bootable removable media could be found in this device path ,\r
2373 // and get the bootable media handle\r
2374 //\r
2375 Handle = BdsLibGetBootableHandle(DevicePath);\r
cb38c322 2376 if (Handle != NULL) {\r
2377 //\r
2378 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
2379 // machinename is ia32, ia64, x64, ...\r
2380 //\r
2381 FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
2382 if (FilePath != NULL) {\r
2383 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
2384 Status = gBS->LoadImage (\r
2385 TRUE,\r
2386 gImageHandle,\r
2387 FilePath,\r
2388 NULL,\r
2389 0,\r
2390 &ImageHandle\r
2391 );\r
9972247d 2392 }\r
2393 }\r
5c08e117 2394 }\r
5c08e117 2395 }\r
2396 //\r
2397 // Provide the image with it's load options\r
2398 //\r
cb38c322 2399 if ((ImageHandle == NULL) || (EFI_ERROR(Status))) {\r
2400 //\r
2401 // Report Status Code to indicate that the failure to load boot option\r
2402 //\r
2403 REPORT_STATUS_CODE (\r
2404 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
2405 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR)\r
2406 ); \r
9972247d 2407 goto Done;\r
2408 }\r
cb38c322 2409\r
5c08e117 2410 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
2411 ASSERT_EFI_ERROR (Status);\r
2412\r
2413 if (Option->LoadOptionsSize != 0) {\r
2414 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r
2415 ImageInfo->LoadOptions = Option->LoadOptions;\r
2416 }\r
bf0712ae
ED
2417\r
2418 //\r
2419 // Clean to NULL because the image is loaded directly from the firmwares boot manager.\r
2420 //\r
2421 ImageInfo->ParentHandle = NULL;\r
2422\r
5c08e117 2423 //\r
2424 // Before calling the image, enable the Watchdog Timer for\r
2425 // the 5 Minute period\r
2426 //\r
2427 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
2428\r
cd6a3b15 2429 //\r
2430 // Write boot to OS performance data for UEFI boot\r
2431 //\r
2432 PERF_CODE (\r
26c0ba77 2433 WriteBootToOsPerformanceData (NULL, NULL);\r
cd6a3b15 2434 );\r
2435\r
79b7a6a1 2436 //\r
2437 // Report status code for OS Loader StartImage.\r
2438 //\r
2439 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));\r
2440\r
5c08e117 2441 Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);\r
2442 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r
cb38c322 2443 if (EFI_ERROR (Status)) {\r
2444 //\r
2445 // Report Status Code to indicate that boot failure\r
2446 //\r
2447 REPORT_STATUS_CODE (\r
2448 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
2449 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED)\r
2450 );\r
2451 }\r
5c08e117 2452\r
2453 //\r
2454 // Clear the Watchdog Timer after the image returns\r
2455 //\r
2456 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
2457\r
2458Done:\r
2df686c6 2459 //\r
2460 // Set Logo status invalid after trying one boot option\r
2461 //\r
2462 BootLogo = NULL;\r
2463 StatusLogo = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);\r
2464 if (!EFI_ERROR (StatusLogo) && (BootLogo != NULL)) {\r
2465 BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);\r
2466 }\r
2467\r
5c08e117 2468 //\r
2469 // Clear Boot Current\r
2470 //\r
2471 gRT->SetVariable (\r
2472 L"BootCurrent",\r
2473 &gEfiGlobalVariableGuid,\r
2474 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
2475 0,\r
2476 &Option->BootCurrent\r
2477 );\r
2478\r
2479 return Status;\r
2480}\r
2481\r
2482\r
2483/**\r
2484 Expand a device path that starts with a hard drive media device path node to be a\r
2485 full device path that includes the full hardware path to the device. We need\r
2486 to do this so it can be booted. As an optimization the front match (the part point\r
2487 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r
2488 so a connect all is not required on every boot. All successful history device path\r
2489 which point to partition node (the front part) will be saved.\r
2490\r
2491 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard\r
2492 drive media device path.\r
2493 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path\r
2494 cannot be found.\r
2495\r
2496**/\r
2497EFI_DEVICE_PATH_PROTOCOL *\r
2498EFIAPI\r
2499BdsExpandPartitionPartialDevicePathToFull (\r
2500 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
2501 )\r
2502{\r
2503 EFI_STATUS Status;\r
2504 UINTN BlockIoHandleCount;\r
2505 EFI_HANDLE *BlockIoBuffer;\r
2506 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r
2507 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;\r
2508 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2509 UINTN Index;\r
2510 UINTN InstanceNum;\r
2511 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;\r
2512 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
2513 UINTN CachedDevicePathSize;\r
2514 BOOLEAN DeviceExist;\r
2515 BOOLEAN NeedAdjust;\r
2516 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
2517 UINTN Size;\r
2518\r
2519 FullDevicePath = NULL;\r
2520 //\r
e24fc103 2521 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.\r
5c08e117 2522 // If exist, search the front path which point to partition node in the variable instants.\r
e24fc103 2523 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system\r
5c08e117 2524 //\r
2525 CachedDevicePath = BdsLibGetVariableAndSize (\r
e24fc103
LG
2526 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2527 &gHdBootDevicePathVariablGuid,\r
5c08e117 2528 &CachedDevicePathSize\r
2529 );\r
128efbbc 2530\r
5c08e117 2531 if (CachedDevicePath != NULL) {\r
2532 TempNewDevicePath = CachedDevicePath;\r
2533 DeviceExist = FALSE;\r
2534 NeedAdjust = FALSE;\r
2535 do {\r
2536 //\r
2537 // Check every instance of the variable\r
2538 // First, check whether the instance contain the partition node, which is needed for distinguishing multi\r
2539 // partial partition boot option. Second, check whether the instance could be connected.\r
2540 //\r
2541 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r
2542 if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {\r
2543 //\r
2544 // Connect the device path instance, the device path point to hard drive media device path node\r
2545 // e.g. ACPI() /PCI()/ATA()/Partition()\r
2546 //\r
2547 Status = BdsLibConnectDevicePath (Instance);\r
2548 if (!EFI_ERROR (Status)) {\r
2549 DeviceExist = TRUE;\r
2550 break;\r
2551 }\r
2552 }\r
2553 //\r
2554 // Come here means the first instance is not matched\r
2555 //\r
2556 NeedAdjust = TRUE;\r
2557 FreePool(Instance);\r
2558 } while (TempNewDevicePath != NULL);\r
2559\r
2560 if (DeviceExist) {\r
2561 //\r
2562 // Find the matched device path.\r
2563 // Append the file path information from the boot option and return the fully expanded device path.\r
2564 //\r
2565 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
2566 FullDevicePath = AppendDevicePath (Instance, DevicePath);\r
2567\r
2568 //\r
e24fc103 2569 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.\r
5c08e117 2570 //\r
2571 if (NeedAdjust) {\r
2572 //\r
2573 // First delete the matched instance.\r
2574 //\r
2575 TempNewDevicePath = CachedDevicePath;\r
2576 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r
2577 FreePool (TempNewDevicePath);\r
128efbbc 2578\r
5c08e117 2579 //\r
2580 // Second, append the remaining path after the matched instance\r
2581 //\r
2582 TempNewDevicePath = CachedDevicePath;\r
2583 CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r
2584 FreePool (TempNewDevicePath);\r
2585 //\r
2586 // Save the matching Device Path so we don't need to do a connect all next time\r
2587 //\r
2588 Status = gRT->SetVariable (\r
e24fc103
LG
2589 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2590 &gHdBootDevicePathVariablGuid,\r
5c08e117 2591 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2592 GetDevicePathSize (CachedDevicePath),\r
2593 CachedDevicePath\r
2594 );\r
2595 }\r
128efbbc 2596\r
5c08e117 2597 FreePool (Instance);\r
2598 FreePool (CachedDevicePath);\r
2599 return FullDevicePath;\r
2600 }\r
2601 }\r
2602\r
2603 //\r
e24fc103 2604 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need\r
5c08e117 2605 // to search all devices in the system for a matched partition\r
2606 //\r
2607 BdsLibConnectAllDriversToAllControllers ();\r
2608 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r
2609 if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {\r
2610 //\r
2611 // If there was an error or there are no device handles that support\r
2612 // the BLOCK_IO Protocol, then return.\r
2613 //\r
2614 return NULL;\r
2615 }\r
2616 //\r
2617 // Loop through all the device handles that support the BLOCK_IO Protocol\r
2618 //\r
2619 for (Index = 0; Index < BlockIoHandleCount; Index++) {\r
2620\r
2621 Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);\r
2622 if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {\r
2623 continue;\r
2624 }\r
2625\r
2626 if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {\r
2627 //\r
2628 // Find the matched partition device path\r
2629 //\r
2630 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
2631 FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r
2632\r
2633 //\r
e24fc103 2634 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable\r
5c08e117 2635 //\r
2636 if (CachedDevicePath != NULL) {\r
2637 //\r
e24fc103 2638 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable\r
5c08e117 2639 //\r
2640 if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {\r
2641 TempNewDevicePath = CachedDevicePath;\r
2642 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r
2643 FreePool(TempNewDevicePath);\r
2644\r
2645 TempNewDevicePath = CachedDevicePath;\r
2646 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
cd730ec0 2647 if (TempNewDevicePath != NULL) {\r
2648 FreePool(TempNewDevicePath);\r
2649 }\r
5c08e117 2650 } else {\r
2651 TempNewDevicePath = CachedDevicePath;\r
2652 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
2653 FreePool(TempNewDevicePath);\r
2654 }\r
2655 //\r
2656 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r
e24fc103
LG
2657 // If the user try to boot many OS in different HDs or partitions, in theory, \r
2658 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.\r
5c08e117 2659 //\r
2660 InstanceNum = 0;\r
2661 ASSERT (CachedDevicePath != NULL);\r
2662 TempNewDevicePath = CachedDevicePath;\r
2663 while (!IsDevicePathEnd (TempNewDevicePath)) {\r
2664 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
2665 //\r
2666 // Parse one instance\r
2667 //\r
2668 while (!IsDevicePathEndType (TempNewDevicePath)) {\r
2669 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
2670 }\r
2671 InstanceNum++;\r
2672 //\r
2673 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.\r
2674 //\r
2675 if (InstanceNum >= 12) {\r
2676 SetDevicePathEndNode (TempNewDevicePath);\r
2677 break;\r
2678 }\r
2679 }\r
2680 } else {\r
2681 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r
2682 }\r
2683\r
2684 //\r
2685 // Save the matching Device Path so we don't need to do a connect all next time\r
2686 //\r
2687 Status = gRT->SetVariable (\r
e24fc103
LG
2688 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2689 &gHdBootDevicePathVariablGuid,\r
5c08e117 2690 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2691 GetDevicePathSize (CachedDevicePath),\r
2692 CachedDevicePath\r
2693 );\r
2694\r
2695 break;\r
2696 }\r
2697 }\r
128efbbc 2698\r
cd730ec0 2699 if (CachedDevicePath != NULL) {\r
2700 FreePool (CachedDevicePath);\r
2701 }\r
5c08e117 2702 if (BlockIoBuffer != NULL) {\r
2703 FreePool (BlockIoBuffer);\r
2704 }\r
2705 return FullDevicePath;\r
2706}\r
2707\r
2708/**\r
2709 Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r
2710 instances, has the same partition node with HardDriveDevicePath device path\r
2711\r
2712 @param BlockIoDevicePath Multi device path instances which need to check\r
2713 @param HardDriveDevicePath A device path which starts with a hard drive media\r
2714 device path.\r
2715\r
2716 @retval TRUE There is a matched device path instance.\r
2717 @retval FALSE There is no matched device path instance.\r
2718\r
2719**/\r
2720BOOLEAN\r
2721EFIAPI\r
2722MatchPartitionDevicePathNode (\r
2723 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,\r
2724 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
2725 )\r
2726{\r
2727 HARDDRIVE_DEVICE_PATH *TmpHdPath;\r
2728 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2729 BOOLEAN Match;\r
2730 EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;\r
2731\r
2732 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r
2733 return FALSE;\r
2734 }\r
128efbbc 2735\r
5c08e117 2736 //\r
2737 // Make PreviousDevicePath == the device path node before the end node\r
2738 //\r
2739 DevicePath = BlockIoDevicePath;\r
2740 BlockIoHdDevicePathNode = NULL;\r
2741\r
2742 //\r
2743 // find the partition device path node\r
2744 //\r
2745 while (!IsDevicePathEnd (DevicePath)) {\r
2746 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
2747 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
2748 ) {\r
2749 BlockIoHdDevicePathNode = DevicePath;\r
2750 break;\r
2751 }\r
2752\r
2753 DevicePath = NextDevicePathNode (DevicePath);\r
2754 }\r
2755\r
2756 if (BlockIoHdDevicePathNode == NULL) {\r
2757 return FALSE;\r
2758 }\r
2759 //\r
2760 // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
2761 //\r
2762 TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;\r
2763 Match = FALSE;\r
128efbbc 2764\r
5c08e117 2765 //\r
2766 // Check for the match\r
2767 //\r
2768 if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&\r
2769 (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {\r
2770 switch (TmpHdPath->SignatureType) {\r
2771 case SIGNATURE_TYPE_GUID:\r
2772 Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);\r
2773 break;\r
2774 case SIGNATURE_TYPE_MBR:\r
2775 Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));\r
2776 break;\r
2777 default:\r
2778 Match = FALSE;\r
2779 break;\r
2780 }\r
2781 }\r
2782\r
2783 return Match;\r
2784}\r
2785\r
2786/**\r
2787 Delete the boot option associated with the handle passed in.\r
2788\r
2789 @param Handle The handle which present the device path to create\r
2790 boot option\r
2791\r
2792 @retval EFI_SUCCESS Delete the boot option success\r
2793 @retval EFI_NOT_FOUND If the Device Path is not found in the system\r
2794 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
2795 @retval Other Error return value from SetVariable()\r
2796\r
2797**/\r
2798EFI_STATUS\r
2799BdsLibDeleteOptionFromHandle (\r
2800 IN EFI_HANDLE Handle\r
2801 )\r
2802{\r
2803 UINT16 *BootOrder;\r
2804 UINT8 *BootOptionVar;\r
2805 UINTN BootOrderSize;\r
2806 UINTN BootOptionSize;\r
2807 EFI_STATUS Status;\r
2808 UINTN Index;\r
2809 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
2810 UINTN DevicePathSize;\r
2811 UINTN OptionDevicePathSize;\r
2812 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2813 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
2814 UINT8 *TempPtr;\r
2815\r
2816 Status = EFI_SUCCESS;\r
2817 BootOrder = NULL;\r
2818 BootOrderSize = 0;\r
2819\r
2820 //\r
2821 // Check "BootOrder" variable, if no, means there is no any boot order.\r
2822 //\r
2823 BootOrder = BdsLibGetVariableAndSize (\r
2824 L"BootOrder",\r
2825 &gEfiGlobalVariableGuid,\r
2826 &BootOrderSize\r
2827 );\r
2828 if (BootOrder == NULL) {\r
2829 return EFI_NOT_FOUND;\r
2830 }\r
2831\r
2832 //\r
2833 // Convert device handle to device path protocol instance\r
2834 //\r
2835 DevicePath = DevicePathFromHandle (Handle);\r
2836 if (DevicePath == NULL) {\r
2837 return EFI_NOT_FOUND;\r
2838 }\r
2839 DevicePathSize = GetDevicePathSize (DevicePath);\r
2840\r
2841 //\r
2842 // Loop all boot order variable and find the matching device path\r
2843 //\r
2844 Index = 0;\r
2845 while (Index < BootOrderSize / sizeof (UINT16)) {\r
2846 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
2847 BootOptionVar = BdsLibGetVariableAndSize (\r
2848 BootOption,\r
2849 &gEfiGlobalVariableGuid,\r
2850 &BootOptionSize\r
2851 );\r
128efbbc 2852\r
5c08e117 2853 if (BootOptionVar == NULL) {\r
2854 FreePool (BootOrder);\r
2855 return EFI_OUT_OF_RESOURCES;\r
2856 }\r
2857\r
8c08a567
ED
2858 if (!ValidateOption(BootOptionVar, BootOptionSize)) {\r
2859 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
2860 FreePool (BootOptionVar);\r
2861 Index++;\r
2862 continue;\r
2863 }\r
2864\r
5c08e117 2865 TempPtr = BootOptionVar;\r
2866 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
2867 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
2868 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
2869 OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
2870\r
2871 //\r
2872 // Check whether the device path match\r
2873 //\r
2874 if ((OptionDevicePathSize == DevicePathSize) &&\r
2875 (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
2876 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
2877 FreePool (BootOptionVar);\r
2878 break;\r
2879 }\r
2880\r
2881 FreePool (BootOptionVar);\r
2882 Index++;\r
2883 }\r
2884\r
2885 //\r
2886 // Adjust number of boot option for "BootOrder" variable.\r
2887 //\r
2888 Status = gRT->SetVariable (\r
2889 L"BootOrder",\r
2890 &gEfiGlobalVariableGuid,\r
2891 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2892 BootOrderSize,\r
2893 BootOrder\r
2894 );\r
2895\r
2896 FreePool (BootOrder);\r
2897\r
2898 return Status;\r
2899}\r
2900\r
2901\r
2902/**\r
3384a9bc 2903 Delete all invalid EFI boot options.\r
5c08e117 2904\r
2905 @retval EFI_SUCCESS Delete all invalid boot option success\r
2906 @retval EFI_NOT_FOUND Variable "BootOrder" is not found\r
2907 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
2908 @retval Other Error return value from SetVariable()\r
2909\r
2910**/\r
2911EFI_STATUS\r
2912BdsDeleteAllInvalidEfiBootOption (\r
2913 VOID\r
2914 )\r
2915{\r
2916 UINT16 *BootOrder;\r
2917 UINT8 *BootOptionVar;\r
2918 UINTN BootOrderSize;\r
2919 UINTN BootOptionSize;\r
2920 EFI_STATUS Status;\r
2921 UINTN Index;\r
2922 UINTN Index2;\r
2923 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
2924 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
2925 UINT8 *TempPtr;\r
3384a9bc 2926 CHAR16 *Description;\r
8c08a567 2927 BOOLEAN Corrupted;\r
5c08e117 2928\r
8c08a567
ED
2929 Status = EFI_SUCCESS;\r
2930 BootOrder = NULL;\r
2931 Description = NULL;\r
2932 OptionDevicePath = NULL;\r
2933 BootOrderSize = 0;\r
2934 Corrupted = FALSE;\r
5c08e117 2935\r
2936 //\r
2937 // Check "BootOrder" variable firstly, this variable hold the number of boot options\r
2938 //\r
2939 BootOrder = BdsLibGetVariableAndSize (\r
2940 L"BootOrder",\r
2941 &gEfiGlobalVariableGuid,\r
2942 &BootOrderSize\r
2943 );\r
2944 if (NULL == BootOrder) {\r
2945 return EFI_NOT_FOUND;\r
2946 }\r
2947\r
2948 Index = 0;\r
2949 while (Index < BootOrderSize / sizeof (UINT16)) {\r
2950 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
2951 BootOptionVar = BdsLibGetVariableAndSize (\r
2952 BootOption,\r
2953 &gEfiGlobalVariableGuid,\r
2954 &BootOptionSize\r
2955 );\r
2956 if (NULL == BootOptionVar) {\r
2957 FreePool (BootOrder);\r
2958 return EFI_OUT_OF_RESOURCES;\r
2959 }\r
2960\r
8c08a567
ED
2961 if (!ValidateOption(BootOptionVar, BootOptionSize)) {\r
2962 Corrupted = TRUE;\r
2963 } else {\r
2964 TempPtr = BootOptionVar;\r
2965 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
2966 Description = (CHAR16 *) TempPtr;\r
2967 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
2968 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
5c08e117 2969\r
8c08a567
ED
2970 //\r
2971 // Skip legacy boot option (BBS boot device)\r
2972 //\r
2973 if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
2974 (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
2975 FreePool (BootOptionVar);\r
2976 Index++;\r
2977 continue;\r
2978 }\r
5c08e117 2979 }\r
2980\r
8c08a567 2981 if (Corrupted || !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {\r
5c08e117 2982 //\r
2983 // Delete this invalid boot option "Boot####"\r
2984 //\r
2985 Status = gRT->SetVariable (\r
2986 BootOption,\r
2987 &gEfiGlobalVariableGuid,\r
2988 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2989 0,\r
2990 NULL\r
2991 );\r
2992 //\r
2993 // Mark this boot option in boot order as deleted\r
2994 //\r
2995 BootOrder[Index] = 0xffff;\r
8c08a567 2996 Corrupted = FALSE;\r
5c08e117 2997 }\r
2998\r
2999 FreePool (BootOptionVar);\r
3000 Index++;\r
3001 }\r
3002\r
3003 //\r
3004 // Adjust boot order array\r
3005 //\r
3006 Index2 = 0;\r
3007 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {\r
3008 if (BootOrder[Index] != 0xffff) {\r
3009 BootOrder[Index2] = BootOrder[Index];\r
3010 Index2 ++;\r
3011 }\r
3012 }\r
3013 Status = gRT->SetVariable (\r
3014 L"BootOrder",\r
3015 &gEfiGlobalVariableGuid,\r
3016 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
3017 Index2 * sizeof (UINT16),\r
3018 BootOrder\r
3019 );\r
3020\r
3021 FreePool (BootOrder);\r
3022\r
3023 return Status;\r
3024}\r
3025\r
3026\r
3027/**\r
3384a9bc 3028 For EFI boot option, BDS separate them as six types:\r
128efbbc 3029 1. Network - The boot option points to the SimpleNetworkProtocol device.\r
3384a9bc 3030 Bds will try to automatically create this type boot option when enumerate.\r
128efbbc 3031 2. Shell - The boot option points to internal flash shell.\r
3384a9bc 3032 Bds will try to automatically create this type boot option when enumerate.\r
3033 3. Removable BlockIo - The boot option only points to the removable media\r
3034 device, like USB flash disk, DVD, Floppy etc.\r
3035 These device should contain a *removable* blockIo\r
3036 protocol in their device handle.\r
128efbbc 3037 Bds will try to automatically create this type boot option\r
3384a9bc 3038 when enumerate.\r
128efbbc 3039 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,\r
3384a9bc 3040 like HardDisk.\r
3041 These device should contain a *fixed* blockIo\r
3042 protocol in their device handle.\r
3043 BDS will skip fixed blockIo devices, and NOT\r
128efbbc 3044 automatically create boot option for them. But BDS\r
3045 will help to delete those fixed blockIo boot option,\r
3384a9bc 3046 whose description rule conflict with other auto-created\r
3047 boot options.\r
128efbbc 3048 5. Non-BlockIo Simplefile - The boot option points to a device whose handle\r
3384a9bc 3049 has SimpleFileSystem Protocol, but has no blockio\r
3050 protocol. These devices do not offer blockIo\r
128efbbc 3051 protocol, but BDS still can get the\r
3384a9bc 3052 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem\r
3053 Protocol.\r
128efbbc 3054 6. File - The boot option points to a file. These boot options are usually\r
3384a9bc 3055 created by user manually or OS loader. BDS will not delete or modify\r
128efbbc 3056 these boot options.\r
3057\r
3384a9bc 3058 This function will enumerate all possible boot device in the system, and\r
128efbbc 3059 automatically create boot options for Network, Shell, Removable BlockIo,\r
3384a9bc 3060 and Non-BlockIo Simplefile devices.\r
8d3b5aff 3061 It will only execute once of every boot.\r
128efbbc 3062\r
5c08e117 3063 @param BdsBootOptionList The header of the link list which indexed all\r
3064 current boot options\r
3065\r
3066 @retval EFI_SUCCESS Finished all the boot device enumerate and create\r
3067 the boot option base on that boot device\r
3068\r
e83c9064 3069 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list\r
5c08e117 3070**/\r
3071EFI_STATUS\r
3072EFIAPI\r
3073BdsLibEnumerateAllBootOption (\r
3074 IN OUT LIST_ENTRY *BdsBootOptionList\r
3075 )\r
3076{\r
3077 EFI_STATUS Status;\r
3078 UINT16 FloppyNumber;\r
889a4bc2 3079 UINT16 HarddriveNumber;\r
5c08e117 3080 UINT16 CdromNumber;\r
3081 UINT16 UsbNumber;\r
3082 UINT16 MiscNumber;\r
8d3b5aff 3083 UINT16 ScsiNumber;\r
5c08e117 3084 UINT16 NonBlockNumber;\r
3085 UINTN NumberBlockIoHandles;\r
3086 EFI_HANDLE *BlockIoHandles;\r
3087 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
35bc0e9f
RN
3088 BOOLEAN Removable[2];\r
3089 UINTN RemovableIndex;\r
5c08e117 3090 UINTN Index;\r
a7a523e0 3091 UINTN NumOfLoadFileHandles;\r
3092 EFI_HANDLE *LoadFileHandles;\r
5c08e117 3093 UINTN FvHandleCount;\r
3094 EFI_HANDLE *FvHandleBuffer;\r
3095 EFI_FV_FILETYPE Type;\r
3096 UINTN Size;\r
3097 EFI_FV_FILE_ATTRIBUTES Attributes;\r
3098 UINT32 AuthenticationStatus;\r
8d3b5aff 3099 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
3100 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
5c08e117 3101 UINTN DevicePathType;\r
3102 CHAR16 Buffer[40];\r
3103 EFI_HANDLE *FileSystemHandles;\r
3104 UINTN NumberFileSystemHandles;\r
3105 BOOLEAN NeedDelete;\r
3106 EFI_IMAGE_DOS_HEADER DosHeader;\r
9aa7ba01 3107 CHAR8 *PlatLang;\r
3108 CHAR8 *LastLang;\r
5c08e117 3109 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
3110 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
3111\r
889a4bc2
RN
3112 FloppyNumber = 0;\r
3113 HarddriveNumber = 0;\r
3114 CdromNumber = 0;\r
3115 UsbNumber = 0;\r
3116 MiscNumber = 0;\r
3117 ScsiNumber = 0;\r
3118 PlatLang = NULL;\r
3119 LastLang = NULL;\r
5c08e117 3120 ZeroMem (Buffer, sizeof (Buffer));\r
128efbbc 3121\r
5c08e117 3122 //\r
3123 // If the boot device enumerate happened, just get the boot\r
3124 // device from the boot order variable\r
3125 //\r
3126 if (mEnumBootDevice) {\r
f01b91ae
ED
3127 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME, &gLastEnumLangGuid, (VOID**)&LastLang, NULL);\r
3128 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatLang, NULL);\r
0fa3ac1b
RN
3129 ASSERT (PlatLang != NULL);\r
3130 if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {\r
9aa7ba01 3131 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
0fa3ac1b
RN
3132 FreePool (LastLang);\r
3133 FreePool (PlatLang);\r
9aa7ba01 3134 return Status;\r
3135 } else {\r
3136 Status = gRT->SetVariable (\r
e24fc103
LG
3137 LAST_ENUM_LANGUAGE_VARIABLE_NAME,\r
3138 &gLastEnumLangGuid,\r
9aa7ba01 3139 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
0fa3ac1b 3140 AsciiStrSize (PlatLang),\r
9aa7ba01 3141 PlatLang\r
3142 );\r
3143 ASSERT_EFI_ERROR (Status);\r
0fa3ac1b
RN
3144\r
3145 if (LastLang != NULL) {\r
3146 FreePool (LastLang);\r
3147 }\r
3148 FreePool (PlatLang);\r
9aa7ba01 3149 }\r
5c08e117 3150 }\r
128efbbc 3151\r
5c08e117 3152 //\r
3153 // Notes: this dirty code is to get the legacy boot option from the\r
3154 // BBS table and create to variable as the EFI boot option, it should\r
3155 // be removed after the CSM can provide legacy boot option directly\r
3156 //\r
3157 REFRESH_LEGACY_BOOT_OPTIONS;\r
3158\r
3159 //\r
3160 // Delete invalid boot option\r
3161 //\r
3162 BdsDeleteAllInvalidEfiBootOption ();\r
128efbbc 3163\r
5c08e117 3164 //\r
701e17e5
RN
3165 // Parse removable media followed by fixed media.\r
3166 // The Removable[] array is used by the for-loop below to create removable media boot options \r
3167 // at first, and then to create fixed media boot options.\r
5c08e117 3168 //\r
701e17e5
RN
3169 Removable[0] = FALSE;\r
3170 Removable[1] = TRUE;\r
3171\r
5c08e117 3172 gBS->LocateHandleBuffer (\r
3173 ByProtocol,\r
3174 &gEfiBlockIoProtocolGuid,\r
3175 NULL,\r
3176 &NumberBlockIoHandles,\r
3177 &BlockIoHandles\r
3178 );\r
128efbbc 3179\r
35bc0e9f
RN
3180 for (RemovableIndex = 0; RemovableIndex < 2; RemovableIndex++) {\r
3181 for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r
3182 Status = gBS->HandleProtocol (\r
3183 BlockIoHandles[Index],\r
3184 &gEfiBlockIoProtocolGuid,\r
3185 (VOID **) &BlkIo\r
3186 );\r
3187 //\r
3188 // skip the fixed block io then the removable block io\r
3189 //\r
3190 if (EFI_ERROR (Status) || (BlkIo->Media->RemovableMedia == Removable[RemovableIndex])) {\r
5c08e117 3191 continue;\r
3192 }\r
35bc0e9f
RN
3193 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);\r
3194 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);\r
5c08e117 3195\r
35bc0e9f
RN
3196 switch (DevicePathType) {\r
3197 case BDS_EFI_ACPI_FLOPPY_BOOT:\r
3198 if (FloppyNumber != 0) {\r
3199 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);\r
3200 } else {\r
3201 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));\r
3202 }\r
3203 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3204 FloppyNumber++;\r
3205 break;\r
128efbbc 3206\r
35bc0e9f 3207 //\r
889a4bc2 3208 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.\r
35bc0e9f
RN
3209 //\r
3210 case BDS_EFI_MESSAGE_ATAPI_BOOT:\r
3211 case BDS_EFI_MESSAGE_SATA_BOOT:\r
889a4bc2
RN
3212 if (BlkIo->Media->RemovableMedia) {\r
3213 if (CdromNumber != 0) {\r
3214 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);\r
3215 } else {\r
3216 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));\r
3217 }\r
3218 CdromNumber++;\r
35bc0e9f 3219 } else {\r
889a4bc2
RN
3220 if (HarddriveNumber != 0) {\r
3221 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)), HarddriveNumber);\r
3222 } else {\r
3223 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)));\r
3224 }\r
3225 HarddriveNumber++;\r
35bc0e9f
RN
3226 }\r
3227 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));\r
3228 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
35bc0e9f
RN
3229 break;\r
3230\r
3231 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:\r
3232 if (UsbNumber != 0) {\r
3233 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);\r
3234 } else {\r
3235 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));\r
3236 }\r
3237 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3238 UsbNumber++;\r
3239 break;\r
5c08e117 3240\r
35bc0e9f
RN
3241 case BDS_EFI_MESSAGE_SCSI_BOOT:\r
3242 if (ScsiNumber != 0) {\r
3243 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);\r
3244 } else {\r
3245 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));\r
3246 }\r
3247 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3248 ScsiNumber++;\r
3249 break;\r
5c08e117 3250\r
35bc0e9f
RN
3251 case BDS_EFI_MESSAGE_MISC_BOOT:\r
3252 if (MiscNumber != 0) {\r
3253 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);\r
3254 } else {\r
3255 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));\r
3256 }\r
3257 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3258 MiscNumber++;\r
3259 break;\r
5c08e117 3260\r
35bc0e9f
RN
3261 default:\r
3262 break;\r
9aa7ba01 3263 }\r
5c08e117 3264 }\r
3265 }\r
3266\r
3267 if (NumberBlockIoHandles != 0) {\r
3268 FreePool (BlockIoHandles);\r
3269 }\r
3270\r
3271 //\r
3272 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.\r
3273 //\r
3274 NonBlockNumber = 0;\r
3275 gBS->LocateHandleBuffer (\r
3276 ByProtocol,\r
3277 &gEfiSimpleFileSystemProtocolGuid,\r
3278 NULL,\r
3279 &NumberFileSystemHandles,\r
3280 &FileSystemHandles\r
3281 );\r
3282 for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r
3283 Status = gBS->HandleProtocol (\r
3284 FileSystemHandles[Index],\r
3285 &gEfiBlockIoProtocolGuid,\r
3286 (VOID **) &BlkIo\r
3287 );\r
3288 if (!EFI_ERROR (Status)) {\r
3289 //\r
3290 // Skip if the file system handle supports a BlkIo protocol,\r
3291 //\r
3292 continue;\r
3293 }\r
3294\r
3295 //\r
3296 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
3297 // machinename is ia32, ia64, x64, ...\r
3298 //\r
35bc0e9f 3299 Hdr.Union = &HdrData;\r
5c08e117 3300 NeedDelete = TRUE;\r
3301 Status = BdsLibGetImageHeader (\r
3302 FileSystemHandles[Index],\r
c62dbf31 3303 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 3304 &DosHeader,\r
3305 Hdr\r
3306 );\r
3307 if (!EFI_ERROR (Status) &&\r
3308 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
3309 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
3310 NeedDelete = FALSE;\r
3311 }\r
3312\r
3313 if (NeedDelete) {\r
3314 //\r
3315 // No such file or the file is not a EFI application, delete this boot option\r
3316 //\r
3317 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
3318 } else {\r
9aa7ba01 3319 if (NonBlockNumber != 0) {\r
3320 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);\r
3321 } else {\r
3322 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));\r
3323 }\r
5c08e117 3324 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);\r
3325 NonBlockNumber++;\r
3326 }\r
3327 }\r
3328\r
3329 if (NumberFileSystemHandles != 0) {\r
3330 FreePool (FileSystemHandles);\r
3331 }\r
3332\r
3333 //\r
3334 // Parse Network Boot Device\r
3335 //\r
a7a523e0 3336 NumOfLoadFileHandles = 0;\r
ff482c56 3337 //\r
a7a523e0 3338 // Search Load File protocol for PXE boot option.\r
ff482c56 3339 //\r
5c08e117 3340 gBS->LocateHandleBuffer (\r
3341 ByProtocol,\r
a7a523e0 3342 &gEfiLoadFileProtocolGuid,\r
5c08e117 3343 NULL,\r
a7a523e0 3344 &NumOfLoadFileHandles,\r
3345 &LoadFileHandles\r
5c08e117 3346 );\r
8d3b5aff 3347\r
a7a523e0 3348 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {\r
9aa7ba01 3349 if (Index != 0) {\r
3350 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);\r
3351 } else {\r
3352 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));\r
3353 }\r
a7a523e0 3354 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);\r
5c08e117 3355 }\r
3356\r
a7a523e0 3357 if (NumOfLoadFileHandles != 0) {\r
3358 FreePool (LoadFileHandles);\r
5c08e117 3359 }\r
3360\r
3361 //\r
3362 // Check if we have on flash shell\r
3363 //\r
3364 gBS->LocateHandleBuffer (\r
3365 ByProtocol,\r
3366 &gEfiFirmwareVolume2ProtocolGuid,\r
3367 NULL,\r
3368 &FvHandleCount,\r
3369 &FvHandleBuffer\r
3370 );\r
3371 for (Index = 0; Index < FvHandleCount; Index++) {\r
5c08e117 3372 gBS->HandleProtocol (\r
3373 FvHandleBuffer[Index],\r
3374 &gEfiFirmwareVolume2ProtocolGuid,\r
3375 (VOID **) &Fv\r
3376 );\r
3377\r
3378 Status = Fv->ReadFile (\r
3379 Fv,\r
d46f3632 3380 PcdGetPtr(PcdShellFile),\r
5c08e117 3381 NULL,\r
3382 &Size,\r
3383 &Type,\r
3384 &Attributes,\r
3385 &AuthenticationStatus\r
3386 );\r
3387 if (EFI_ERROR (Status)) {\r
3388 //\r
3389 // Skip if no shell file in the FV\r
3390 //\r
3391 continue;\r
3392 }\r
3393 //\r
3394 // Build the shell boot option\r
3395 //\r
3396 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
3397 }\r
3398\r
3399 if (FvHandleCount != 0) {\r
3400 FreePool (FvHandleBuffer);\r
3401 }\r
3402 //\r
3403 // Make sure every boot only have one time\r
3404 // boot device enumerate\r
3405 //\r
e83c9064 3406 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
5c08e117 3407 mEnumBootDevice = TRUE;\r
3408\r
e83c9064 3409 return Status;\r
5c08e117 3410}\r
3411\r
3412/**\r
3413 Build the boot option with the handle parsed in\r
3414\r
3415 @param Handle The handle which present the device path to create\r
3416 boot option\r
3417 @param BdsBootOptionList The header of the link list which indexed all\r
3418 current boot options\r
3419 @param String The description of the boot option.\r
3420\r
3421**/\r
3422VOID\r
3423EFIAPI\r
3424BdsLibBuildOptionFromHandle (\r
3425 IN EFI_HANDLE Handle,\r
3426 IN LIST_ENTRY *BdsBootOptionList,\r
3427 IN CHAR16 *String\r
3428 )\r
3429{\r
3430 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
128efbbc 3431\r
8d3b5aff 3432 DevicePath = DevicePathFromHandle (Handle);\r
5c08e117 3433\r
3434 //\r
3435 // Create and register new boot option\r
3436 //\r
3437 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r
3438}\r
3439\r
3440\r
3441/**\r
3442 Build the on flash shell boot option with the handle parsed in.\r
3443\r
3444 @param Handle The handle which present the device path to create\r
3445 on flash shell boot option\r
3446 @param BdsBootOptionList The header of the link list which indexed all\r
3447 current boot options\r
3448\r
3449**/\r
3450VOID\r
3451EFIAPI\r
3452BdsLibBuildOptionFromShell (\r
3453 IN EFI_HANDLE Handle,\r
3454 IN OUT LIST_ENTRY *BdsBootOptionList\r
3455 )\r
3456{\r
3457 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
3458 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
3459\r
3460 DevicePath = DevicePathFromHandle (Handle);\r
3461\r
3462 //\r
3463 // Build the shell device path\r
3464 //\r
d46f3632 3465 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));\r
5c08e117 3466\r
3467 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
3468\r
3469 //\r
3470 // Create and register the shell boot option\r
3471 //\r
3472 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
3473\r
3474}\r
3475\r
3476/**\r
3477 Boot from the UEFI spec defined "BootNext" variable.\r
3478\r
3479**/\r
3480VOID\r
3481EFIAPI\r
3482BdsLibBootNext (\r
3483 VOID\r
3484 )\r
3485{\r
3486 UINT16 *BootNext;\r
3487 UINTN BootNextSize;\r
3488 CHAR16 Buffer[20];\r
3489 BDS_COMMON_OPTION *BootOption;\r
3490 LIST_ENTRY TempList;\r
3491 UINTN ExitDataSize;\r
3492 CHAR16 *ExitData;\r
3493\r
3494 //\r
3495 // Init the boot option name buffer and temp link list\r
3496 //\r
3497 InitializeListHead (&TempList);\r
3498 ZeroMem (Buffer, sizeof (Buffer));\r
3499\r
3500 BootNext = BdsLibGetVariableAndSize (\r
3501 L"BootNext",\r
3502 &gEfiGlobalVariableGuid,\r
3503 &BootNextSize\r
3504 );\r
3505\r
3506 //\r
3507 // Clear the boot next variable first\r
3508 //\r
3509 if (BootNext != NULL) {\r
3510 gRT->SetVariable (\r
3511 L"BootNext",\r
3512 &gEfiGlobalVariableGuid,\r
3513 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
3514 0,\r
3515 BootNext\r
3516 );\r
3517\r
3518 //\r
3519 // Start to build the boot option and try to boot\r
3520 //\r
3521 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
3522 BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
3523 ASSERT (BootOption != NULL);\r
3524 BdsLibConnectDevicePath (BootOption->DevicePath);\r
3525 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
3526 }\r
3527\r
3528}\r
3529\r
3530/**\r
3531 Return the bootable media handle.\r
3532 First, check the device is connected\r
3533 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r
3534 Third, detect the the default boot file in the Media, and return the removable Media handle.\r
3535\r
e83c9064 3536 @param DevicePath Device Path to a bootable device\r
5c08e117 3537\r
e83c9064 3538 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.\r
5c08e117 3539\r
3540**/\r
3541EFI_HANDLE\r
3542EFIAPI\r
3543BdsLibGetBootableHandle (\r
3544 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3545 )\r
3546{\r
3547 EFI_STATUS Status;\r
ef949581 3548 EFI_TPL OldTpl;\r
5c08e117 3549 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
3550 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r
3551 EFI_HANDLE Handle;\r
3552 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
3553 VOID *Buffer;\r
3554 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3555 UINTN Size;\r
3556 UINTN TempSize;\r
3557 EFI_HANDLE ReturnHandle;\r
3558 EFI_HANDLE *SimpleFileSystemHandles;\r
3559\r
3560 UINTN NumberSimpleFileSystemHandles;\r
3561 UINTN Index;\r
3562 EFI_IMAGE_DOS_HEADER DosHeader;\r
3563 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
3564 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
3565\r
3566 UpdatedDevicePath = DevicePath;\r
128efbbc 3567\r
ef949581
RN
3568 //\r
3569 // Enter to critical section to protect the acquired BlockIo instance \r
3570 // from getting released due to the USB mass storage hotplug event\r
3571 //\r
3572 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
3573\r
5c08e117 3574 //\r
3575 // Check whether the device is connected\r
3576 //\r
3577 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r
3578 if (EFI_ERROR (Status)) {\r
3579 //\r
3580 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r
3581 //\r
3582 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r
3583 if (EFI_ERROR (Status)) {\r
3584 //\r
3585 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r
3586 //\r
3587 UpdatedDevicePath = DevicePath;\r
3588 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
3589 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
3590 }\r
3591 } else {\r
e74f510b
RN
3592 //\r
3593 // For removable device boot option, its contained device path only point to the removable device handle, \r
3594 // should make sure all its children handles (its child partion or media handles) are created and connected. \r
3595 //\r
3596 gBS->ConnectController (Handle, NULL, NULL, TRUE); \r
5c08e117 3597 //\r
3598 // Get BlockIo protocol and check removable attribute\r
3599 //\r
3600 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
ef949581
RN
3601 ASSERT_EFI_ERROR (Status);\r
3602\r
5c08e117 3603 //\r
3604 // Issue a dummy read to the device to check for media change.\r
3605 // When the removable media is changed, any Block IO read/write will\r
3606 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
3607 // returned. After the Block IO protocol is reinstalled, subsequent\r
3608 // Block IO read/write will success.\r
3609 //\r
3610 Buffer = AllocatePool (BlockIo->Media->BlockSize);\r
3611 if (Buffer != NULL) {\r
3612 BlockIo->ReadBlocks (\r
3613 BlockIo,\r
3614 BlockIo->Media->MediaId,\r
3615 0,\r
3616 BlockIo->Media->BlockSize,\r
3617 Buffer\r
3618 );\r
3619 FreePool(Buffer);\r
3620 }\r
3621 }\r
3622\r
3623 //\r
3624 // Detect the the default boot file from removable Media\r
3625 //\r
3626\r
3627 //\r
3628 // 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
3629 // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
3630 //\r
3631 DupDevicePath = DuplicateDevicePath (DevicePath);\r
3632 ASSERT (DupDevicePath != NULL);\r
128efbbc 3633\r
5c08e117 3634 UpdatedDevicePath = DupDevicePath;\r
3635 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
3636 //\r
3637 // 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
3638 // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r
3639 //\r
3640 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r
3641 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r
3642 //\r
3643 // Remove the usb node, let the device path only point to PCI node\r
3644 //\r
3645 SetDevicePathEndNode (UpdatedDevicePath);\r
3646 UpdatedDevicePath = DupDevicePath;\r
3647 } else {\r
3648 UpdatedDevicePath = DevicePath;\r
3649 }\r
3650\r
3651 //\r
3652 // Get the device path size of boot option\r
3653 //\r
3654 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
3655 ReturnHandle = NULL;\r
3656 gBS->LocateHandleBuffer (\r
3657 ByProtocol,\r
3658 &gEfiSimpleFileSystemProtocolGuid,\r
3659 NULL,\r
3660 &NumberSimpleFileSystemHandles,\r
3661 &SimpleFileSystemHandles\r
3662 );\r
3663 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
3664 //\r
3665 // Get the device path size of SimpleFileSystem handle\r
3666 //\r
3667 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
3668 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
3669 //\r
3670 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r
3671 //\r
3672 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r
3673 //\r
3674 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
3675 // machinename is ia32, ia64, x64, ...\r
3676 //\r
3677 Hdr.Union = &HdrData;\r
3678 Status = BdsLibGetImageHeader (\r
3679 SimpleFileSystemHandles[Index],\r
c62dbf31 3680 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 3681 &DosHeader,\r
3682 Hdr\r
3683 );\r
3684 if (!EFI_ERROR (Status) &&\r
3685 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
3686 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
3687 ReturnHandle = SimpleFileSystemHandles[Index];\r
3688 break;\r
3689 }\r
3690 }\r
3691 }\r
3692\r
3693 FreePool(DupDevicePath);\r
3694\r
3695 if (SimpleFileSystemHandles != NULL) {\r
3696 FreePool(SimpleFileSystemHandles);\r
3697 }\r
3698\r
ef949581
RN
3699 gBS->RestoreTPL (OldTpl);\r
3700\r
5c08e117 3701 return ReturnHandle;\r
3702}\r
3703\r
3704/**\r
3705 Check to see if the network cable is plugged in. If the DevicePath is not\r
3706 connected it will be connected.\r
3707\r
3708 @param DevicePath Device Path to check\r
3709\r
3710 @retval TRUE DevicePath points to an Network that is connected\r
3711 @retval FALSE DevicePath does not point to a bootable network\r
3712\r
3713**/\r
3714BOOLEAN\r
3715BdsLibNetworkBootWithMediaPresent (\r
3716 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3717 )\r
3718{\r
3719 EFI_STATUS Status;\r
3720 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
3721 EFI_HANDLE Handle;\r
3722 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
3723 BOOLEAN MediaPresent;\r
e51e619e 3724 UINT32 InterruptStatus;\r
5c08e117 3725\r
3726 MediaPresent = FALSE;\r
3727\r
3728 UpdatedDevicePath = DevicePath;\r
ff482c56 3729 //\r
a7a523e0 3730 // Locate Load File Protocol for PXE boot option first\r
ff482c56 3731 //\r
a7a523e0 3732 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 3733 if (EFI_ERROR (Status)) {\r
3734 //\r
3735 // Device not present so see if we need to connect it\r
3736 //\r
3737 Status = BdsLibConnectDevicePath (DevicePath);\r
3738 if (!EFI_ERROR (Status)) {\r
3739 //\r
3740 // This one should work after we did the connect\r
3741 //\r
a7a523e0 3742 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 3743 }\r
3744 }\r
3745\r
3746 if (!EFI_ERROR (Status)) {\r
3747 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r
ff482c56 3748 if (EFI_ERROR (Status)) {\r
3749 //\r
3750 // Failed to open SNP from this handle, try to get SNP from parent handle\r
3751 //\r
3752 UpdatedDevicePath = DevicePathFromHandle (Handle);\r
3753 if (UpdatedDevicePath != NULL) {\r
3754 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
3755 if (!EFI_ERROR (Status)) {\r
3756 //\r
3757 // SNP handle found, get SNP from it\r
3758 //\r
3759 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);\r
3760 }\r
3761 }\r
3762 }\r
3763\r
5c08e117 3764 if (!EFI_ERROR (Status)) {\r
3765 if (Snp->Mode->MediaPresentSupported) {\r
3766 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r
e51e619e 3767 //\r
3768 // Invoke Snp->GetStatus() to refresh the media status\r
3769 //\r
3770 Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
3771\r
5c08e117 3772 //\r
3773 // In case some one else is using the SNP check to see if it's connected\r
3774 //\r
3775 MediaPresent = Snp->Mode->MediaPresent;\r
3776 } else {\r
3777 //\r
3778 // No one is using SNP so we need to Start and Initialize so\r
3779 // MediaPresent will be valid.\r
3780 //\r
3781 Status = Snp->Start (Snp);\r
3782 if (!EFI_ERROR (Status)) {\r
3783 Status = Snp->Initialize (Snp, 0, 0);\r
3784 if (!EFI_ERROR (Status)) {\r
3785 MediaPresent = Snp->Mode->MediaPresent;\r
3786 Snp->Shutdown (Snp);\r
3787 }\r
3788 Snp->Stop (Snp);\r
3789 }\r
3790 }\r
3791 } else {\r
3792 MediaPresent = TRUE;\r
3793 }\r
3794 }\r
3795 }\r
3796\r
3797 return MediaPresent;\r
3798}\r
3799\r
3800/**\r
3801 For a bootable Device path, return its boot type.\r
3802\r
3803 @param DevicePath The bootable device Path to check\r
3804\r
128efbbc 3805 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
11c5022d 3806 which subtype is MEDIA_HARDDRIVE_DP\r
3807 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
3808 which subtype is MEDIA_CDROM_DP\r
3809 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node\r
3810 which HID is floppy device.\r
3811 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3812 and its last device path node's subtype is MSG_ATAPI_DP.\r
3813 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3814 and its last device path node's subtype is MSG_SCSI_DP.\r
3815 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3816 and its last device path node's subtype is MSG_USB_DP.\r
5c08e117 3817 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r
11c5022d 3818 its last device path node point to a message device path node.\r
3819 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.\r
128efbbc 3820 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,\r
5c08e117 3821\r
3822**/\r
3823UINT32\r
3824EFIAPI\r
3825BdsGetBootTypeFromDevicePath (\r
3826 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3827 )\r
3828{\r
3829 ACPI_HID_DEVICE_PATH *Acpi;\r
3830 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3831 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
ff482c56 3832 UINT32 BootType;\r
5c08e117 3833\r
3834 if (NULL == DevicePath) {\r
3835 return BDS_EFI_UNSUPPORT;\r
3836 }\r
3837\r
3838 TempDevicePath = DevicePath;\r
3839\r
3840 while (!IsDevicePathEndType (TempDevicePath)) {\r
3841 switch (DevicePathType (TempDevicePath)) {\r
3842 case BBS_DEVICE_PATH:\r
3843 return BDS_LEGACY_BBS_BOOT;\r
3844 case MEDIA_DEVICE_PATH:\r
3845 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r
3846 return BDS_EFI_MEDIA_HD_BOOT;\r
3847 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r
3848 return BDS_EFI_MEDIA_CDROM_BOOT;\r
128efbbc 3849 }\r
5c08e117 3850 break;\r
3851 case ACPI_DEVICE_PATH:\r
3852 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r
3853 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r
3854 return BDS_EFI_ACPI_FLOPPY_BOOT;\r
3855 }\r
3856 break;\r
3857 case MESSAGING_DEVICE_PATH:\r
3858 //\r
3859 // Get the last device path node\r
3860 //\r
3861 LastDeviceNode = NextDevicePathNode (TempDevicePath);\r
3862 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r
3863 //\r
3864 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r
ff482c56 3865 // skip it\r
5c08e117 3866 //\r
3867 LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r
3868 }\r
3869 //\r
3870 // if the device path not only point to driver device, it is not a messaging device path,\r
3871 //\r
3872 if (!IsDevicePathEndType (LastDeviceNode)) {\r
128efbbc 3873 break;\r
5c08e117 3874 }\r
3875\r
ff482c56 3876 switch (DevicePathSubType (TempDevicePath)) {\r
3877 case MSG_ATAPI_DP:\r
3878 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;\r
3879 break;\r
3880\r
3881 case MSG_USB_DP:\r
3882 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r
3883 break;\r
3884\r
3885 case MSG_SCSI_DP:\r
3886 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;\r
3887 break;\r
3888\r
3889 case MSG_SATA_DP:\r
3890 BootType = BDS_EFI_MESSAGE_SATA_BOOT;\r
3891 break;\r
3892\r
3893 case MSG_MAC_ADDR_DP:\r
3894 case MSG_VLAN_DP:\r
a7a523e0 3895 case MSG_IPv4_DP:\r
3896 case MSG_IPv6_DP:\r
ff482c56 3897 BootType = BDS_EFI_MESSAGE_MAC_BOOT;\r
3898 break;\r
3899\r
3900 default:\r
3901 BootType = BDS_EFI_MESSAGE_MISC_BOOT;\r
3902 break;\r
5c08e117 3903 }\r
ff482c56 3904 return BootType;\r
3905\r
5c08e117 3906 default:\r
3907 break;\r
3908 }\r
3909 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
3910 }\r
3911\r
3912 return BDS_EFI_UNSUPPORT;\r
3913}\r
3914\r
3915/**\r
3916 Check whether the Device path in a boot option point to a valid bootable device,\r
3917 And if CheckMedia is true, check the device is ready to boot now.\r
3918\r
3919 @param DevPath the Device path in a boot option\r
3920 @param CheckMedia if true, check the device is ready to boot now.\r
3921\r
3922 @retval TRUE the Device path is valid\r
3923 @retval FALSE the Device path is invalid .\r
3924\r
3925**/\r
3926BOOLEAN\r
3927EFIAPI\r
3928BdsLibIsValidEFIBootOptDevicePath (\r
3929 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
3930 IN BOOLEAN CheckMedia\r
3931 )\r
3384a9bc 3932{\r
3933 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);\r
3934}\r
3935\r
3936/**\r
3937 Check whether the Device path in a boot option point to a valid bootable device,\r
3938 And if CheckMedia is true, check the device is ready to boot now.\r
3939 If Description is not NULL and the device path point to a fixed BlockIo\r
3940 device, check the description whether conflict with other auto-created\r
3941 boot options.\r
3942\r
3943 @param DevPath the Device path in a boot option\r
3944 @param CheckMedia if true, check the device is ready to boot now.\r
3945 @param Description the description in a boot option\r
3946\r
3947 @retval TRUE the Device path is valid\r
3948 @retval FALSE the Device path is invalid .\r
3949\r
3950**/\r
3951BOOLEAN\r
3952EFIAPI\r
3953BdsLibIsValidEFIBootOptDevicePathExt (\r
3954 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
3955 IN BOOLEAN CheckMedia,\r
3956 IN CHAR16 *Description\r
3957 )\r
5c08e117 3958{\r
3959 EFI_STATUS Status;\r
3960 EFI_HANDLE Handle;\r
3961 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3962 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
3963 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
3964\r
3965 TempDevicePath = DevPath;\r
3966 LastDeviceNode = DevPath;\r
128efbbc 3967\r
5c08e117 3968 //\r
a7a523e0 3969 // Check if it's a valid boot option for network boot device.\r
3970 // Check if there is EfiLoadFileProtocol installed. \r
3971 // If yes, that means there is a boot option for network.\r
5c08e117 3972 //\r
3973 Status = gBS->LocateDevicePath (\r
a7a523e0 3974 &gEfiLoadFileProtocolGuid,\r
5c08e117 3975 &TempDevicePath,\r
3976 &Handle\r
3977 );\r
3978 if (EFI_ERROR (Status)) {\r
3979 //\r
3980 // Device not present so see if we need to connect it\r
3981 //\r
3982 TempDevicePath = DevPath;\r
3983 BdsLibConnectDevicePath (TempDevicePath);\r
3984 Status = gBS->LocateDevicePath (\r
a7a523e0 3985 &gEfiLoadFileProtocolGuid,\r
5c08e117 3986 &TempDevicePath,\r
3987 &Handle\r
3988 );\r
3989 }\r
128efbbc 3990\r
5c08e117 3991 if (!EFI_ERROR (Status)) {\r
a7a523e0 3992 if (!IsDevicePathEnd (TempDevicePath)) {\r
3993 //\r
3994 // LoadFile protocol is not installed on handle with exactly the same DevPath\r
3995 //\r
3996 return FALSE;\r
3997 }\r
ff482c56 3998\r
a7a523e0 3999 if (CheckMedia) {\r
4000 //\r
4001 // Test if it is ready to boot now\r
4002 //\r
4003 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r
5c08e117 4004 return TRUE;\r
4005 }\r
a7a523e0 4006 } else {\r
4007 return TRUE;\r
4008 } \r
5c08e117 4009 }\r
4010\r
4011 //\r
4012 // If the boot option point to a file, it is a valid EFI boot option,\r
4013 // and assume it is ready to boot now\r
4014 //\r
4015 while (!IsDevicePathEnd (TempDevicePath)) {\r
7389fdd0 4016 //\r
4017 // If there is USB Class or USB WWID device path node, treat it as valid EFI\r
4018 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it\r
4019 // to full device path.\r
4020 //\r
4021 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r
4022 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||\r
4023 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {\r
4024 return TRUE;\r
4025 }\r
4026\r
4027 LastDeviceNode = TempDevicePath;\r
4028 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
5c08e117 4029 }\r
4030 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
4031 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
4032 return TRUE;\r
4033 }\r
4034\r
4035 //\r
6617d838 4036 // Check if it's a valid boot option for internal FV application\r
5c08e117 4037 //\r
4038 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r
4039 //\r
6617d838 4040 // If the boot option point to internal FV application, make sure it is valid\r
5c08e117 4041 //\r
128efbbc 4042 TempDevicePath = DevPath;\r
6617d838
RN
4043 Status = BdsLibUpdateFvFileDevicePath (\r
4044 &TempDevicePath,\r
4045 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode)\r
4046 );\r
5c08e117 4047 if (Status == EFI_ALREADY_STARTED) {\r
4048 return TRUE;\r
4049 } else {\r
4050 if (Status == EFI_SUCCESS) {\r
128efbbc 4051 FreePool (TempDevicePath);\r
5c08e117 4052 }\r
4053 return FALSE;\r
4054 }\r
4055 }\r
128efbbc 4056\r
5c08e117 4057 //\r
3384a9bc 4058 // If the boot option point to a blockIO device:\r
8d3b5aff 4059 // if it is a removable blockIo device, it is valid.\r
128efbbc 4060 // if it is a fixed blockIo device, check its description confliction.\r
5c08e117 4061 //\r
4062 TempDevicePath = DevPath;\r
4063 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
4064 if (EFI_ERROR (Status)) {\r
4065 //\r
4066 // Device not present so see if we need to connect it\r
4067 //\r
4068 Status = BdsLibConnectDevicePath (DevPath);\r
4069 if (!EFI_ERROR (Status)) {\r
4070 //\r
4071 // Try again to get the Block Io protocol after we did the connect\r
4072 //\r
4073 TempDevicePath = DevPath;\r
4074 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
4075 }\r
4076 }\r
128efbbc 4077\r
5c08e117 4078 if (!EFI_ERROR (Status)) {\r
4079 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
4080 if (!EFI_ERROR (Status)) {\r
4081 if (CheckMedia) {\r
4082 //\r
4083 // Test if it is ready to boot now\r
4084 //\r
4085 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
4086 return TRUE;\r
4087 }\r
4088 } else {\r
4089 return TRUE;\r
4090 }\r
4091 }\r
4092 } else {\r
4093 //\r
4094 // 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
4095 //\r
4096 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r
4097 if (!EFI_ERROR (Status)) {\r
4098 if (CheckMedia) {\r
4099 //\r
4100 // Test if it is ready to boot now\r
4101 //\r
4102 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
4103 return TRUE;\r
4104 }\r
4105 } else {\r
4106 return TRUE;\r
4107 }\r
4108 }\r
4109 }\r
4110\r
4111 return FALSE;\r
4112}\r
4113\r
4114\r
4115/**\r
4116 According to a file guild, check a Fv file device path is valid. If it is invalid,\r
4117 try to return the valid device path.\r
4118 FV address maybe changes for memory layout adjust from time to time, use this function\r
4119 could promise the Fv file device path is right.\r
4120\r
4121 @param DevicePath on input, the Fv file device path need to check on\r
4122 output, the updated valid Fv file device path\r
4123 @param FileGuid the Fv file guild\r
4124\r
4125 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r
4126 parameter\r
4127 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r
4128 guild at all\r
4129 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r
4130 valid\r
4131 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r
4132 and return the updated device path in DevicePath\r
4133\r
4134**/\r
4135EFI_STATUS\r
4136EFIAPI\r
4137BdsLibUpdateFvFileDevicePath (\r
4138 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r
4139 IN EFI_GUID *FileGuid\r
4140 )\r
4141{\r
4142 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
4143 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
4144 EFI_STATUS Status;\r
4145 EFI_GUID *GuidPoint;\r
4146 UINTN Index;\r
4147 UINTN FvHandleCount;\r
4148 EFI_HANDLE *FvHandleBuffer;\r
4149 EFI_FV_FILETYPE Type;\r
4150 UINTN Size;\r
4151 EFI_FV_FILE_ATTRIBUTES Attributes;\r
4152 UINT32 AuthenticationStatus;\r
4153 BOOLEAN FindFvFile;\r
4154 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
4155 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
4156 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
4157 EFI_HANDLE FoundFvHandle;\r
4158 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
4159\r
4160 if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
4161 return EFI_INVALID_PARAMETER;\r
4162 }\r
4163 if (FileGuid == NULL) {\r
4164 return EFI_INVALID_PARAMETER;\r
4165 }\r
128efbbc 4166\r
5c08e117 4167 //\r
4168 // Check whether the device path point to the default the input Fv file\r
4169 //\r
4170 TempDevicePath = *DevicePath;\r
4171 LastDeviceNode = TempDevicePath;\r
4172 while (!IsDevicePathEnd (TempDevicePath)) {\r
4173 LastDeviceNode = TempDevicePath;\r
4174 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
4175 }\r
4176 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
4177 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
4178 );\r
4179 if (GuidPoint == NULL) {\r
4180 //\r
4181 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
4182 //\r
4183 return EFI_UNSUPPORTED;\r
4184 }\r
4185 if (!CompareGuid (GuidPoint, FileGuid)) {\r
4186 //\r
4187 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
4188 //\r
4189 return EFI_UNSUPPORTED;\r
4190 }\r
4191\r
4192 //\r
4193 // Check whether the input Fv file device path is valid\r
4194 //\r
4195 TempDevicePath = *DevicePath;\r
4196 FoundFvHandle = NULL;\r
4197 Status = gBS->LocateDevicePath (\r
4198 &gEfiFirmwareVolume2ProtocolGuid,\r
4199 &TempDevicePath,\r
4200 &FoundFvHandle\r
4201 );\r
4202 if (!EFI_ERROR (Status)) {\r
4203 Status = gBS->HandleProtocol (\r
4204 FoundFvHandle,\r
4205 &gEfiFirmwareVolume2ProtocolGuid,\r
4206 (VOID **) &Fv\r
4207 );\r
4208 if (!EFI_ERROR (Status)) {\r
4209 //\r
4210 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
4211 //\r
4212 Status = Fv->ReadFile (\r
4213 Fv,\r
4214 FileGuid,\r
4215 NULL,\r
4216 &Size,\r
4217 &Type,\r
4218 &Attributes,\r
4219 &AuthenticationStatus\r
4220 );\r
4221 if (!EFI_ERROR (Status)) {\r
4222 return EFI_ALREADY_STARTED;\r
4223 }\r
4224 }\r
4225 }\r
4226\r
4227 //\r
4228 // Look for the input wanted FV file in current FV\r
4229 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r
4230 //\r
4231 FindFvFile = FALSE;\r
4232 FoundFvHandle = NULL;\r
4233 Status = gBS->HandleProtocol (\r
fefefa4c 4234 gImageHandle,\r
5c08e117 4235 &gEfiLoadedImageProtocolGuid,\r
4236 (VOID **) &LoadedImage\r
4237 );\r
4238 if (!EFI_ERROR (Status)) {\r
4239 Status = gBS->HandleProtocol (\r
4240 LoadedImage->DeviceHandle,\r
4241 &gEfiFirmwareVolume2ProtocolGuid,\r
4242 (VOID **) &Fv\r
4243 );\r
4244 if (!EFI_ERROR (Status)) {\r
4245 Status = Fv->ReadFile (\r
4246 Fv,\r
4247 FileGuid,\r
4248 NULL,\r
4249 &Size,\r
4250 &Type,\r
4251 &Attributes,\r
4252 &AuthenticationStatus\r
4253 );\r
4254 if (!EFI_ERROR (Status)) {\r
4255 FindFvFile = TRUE;\r
4256 FoundFvHandle = LoadedImage->DeviceHandle;\r
4257 }\r
4258 }\r
4259 }\r
4260 //\r
4261 // Second, if fail to find, try to enumerate all FV\r
4262 //\r
4263 if (!FindFvFile) {\r
4264 FvHandleBuffer = NULL;\r
4265 gBS->LocateHandleBuffer (\r
4266 ByProtocol,\r
4267 &gEfiFirmwareVolume2ProtocolGuid,\r
4268 NULL,\r
4269 &FvHandleCount,\r
4270 &FvHandleBuffer\r
4271 );\r
4272 for (Index = 0; Index < FvHandleCount; Index++) {\r
4273 gBS->HandleProtocol (\r
4274 FvHandleBuffer[Index],\r
4275 &gEfiFirmwareVolume2ProtocolGuid,\r
4276 (VOID **) &Fv\r
4277 );\r
4278\r
4279 Status = Fv->ReadFile (\r
4280 Fv,\r
4281 FileGuid,\r
4282 NULL,\r
4283 &Size,\r
4284 &Type,\r
4285 &Attributes,\r
4286 &AuthenticationStatus\r
4287 );\r
4288 if (EFI_ERROR (Status)) {\r
4289 //\r
4290 // Skip if input Fv file not in the FV\r
4291 //\r
4292 continue;\r
4293 }\r
4294 FindFvFile = TRUE;\r
4295 FoundFvHandle = FvHandleBuffer[Index];\r
4296 break;\r
4297 }\r
4298\r
4299 if (FvHandleBuffer != NULL) {\r
128efbbc 4300 FreePool (FvHandleBuffer);\r
5c08e117 4301 }\r
4302 }\r
4303\r
4304 if (FindFvFile) {\r
4305 //\r
4306 // Build the shell device path\r
4307 //\r
4308 NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r
4309 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r
4310 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r
4311 *DevicePath = NewDevicePath;\r
4312 return EFI_SUCCESS;\r
4313 }\r
4314 return EFI_NOT_FOUND;\r
4315}\r