]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
Fix wrong parameter usage in GenericBdsLibConstructor
[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
f6c07313 4Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.<BR>\r
180a5a35 5This program and the accompanying materials\r
5c08e117 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "InternalBdsLib.h"\r
9aa7ba01 16#include "String.h"\r
5c08e117 17\r
18BOOLEAN mEnumBootDevice = FALSE;\r
9aa7ba01 19EFI_HII_HANDLE gBdsLibStringPackHandle = NULL;\r
5c08e117 20\r
9aa7ba01 21/**\r
22 The constructor function register UNI strings into imageHandle.\r
23 \r
24 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
25\r
26 @param ImageHandle The firmware allocated handle for the EFI image.\r
27 @param SystemTable A pointer to the EFI System Table.\r
28 \r
29 @retval EFI_SUCCESS The constructor successfully added string package.\r
30 @retval Other value The constructor can't add string package.\r
31\r
32**/\r
33EFI_STATUS\r
34EFIAPI\r
35GenericBdsLibConstructor (\r
36 IN EFI_HANDLE ImageHandle,\r
37 IN EFI_SYSTEM_TABLE *SystemTable\r
38 )\r
39{\r
40\r
41 gBdsLibStringPackHandle = HiiAddPackages (\r
e24fc103 42 &gBdsLibStringPackageGuid,\r
09363eca 43 ImageHandle,\r
9aa7ba01 44 GenericBdsLibStrings,\r
45 NULL\r
46 );\r
47\r
48 ASSERT (gBdsLibStringPackHandle != NULL);\r
49\r
50 return EFI_SUCCESS;\r
51}\r
52\r
16e5944a
RN
53/**\r
54 Deletete the Boot Option from EFI Variable. The Boot Order Arrray\r
55 is also updated.\r
56\r
57 @param OptionNumber The number of Boot option want to be deleted.\r
58 @param BootOrder The Boot Order array.\r
59 @param BootOrderSize The size of the Boot Order Array.\r
60\r
61 @retval EFI_SUCCESS The Boot Option Variable was found and removed\r
62 @retval EFI_UNSUPPORTED The Boot Option Variable store was inaccessible\r
63 @retval EFI_NOT_FOUND The Boot Option Variable was not found\r
64**/\r
65EFI_STATUS\r
66EFIAPI\r
67BdsDeleteBootOption (\r
68 IN UINTN OptionNumber,\r
69 IN OUT UINT16 *BootOrder,\r
70 IN OUT UINTN *BootOrderSize\r
71 )\r
72{\r
73 CHAR16 BootOption[9];\r
74 UINTN Index;\r
75 EFI_STATUS Status;\r
76\r
77 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", OptionNumber);\r
78 Status = gRT->SetVariable (\r
79 BootOption,\r
80 &gEfiGlobalVariableGuid,\r
81 0,\r
82 0,\r
83 NULL\r
84 );\r
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
5c08e117 2232 *ExitDataSize = 0;\r
2233 *ExitData = NULL;\r
2234\r
5c08e117 2235 //\r
2236 // Notes: this code can be remove after the s3 script table\r
2237 // hook on the event EVT_SIGNAL_READY_TO_BOOT or\r
2238 // EVT_SIGNAL_LEGACY_BOOT\r
2239 //\r
2240 Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);\r
2241 if (!EFI_ERROR (Status)) {\r
2242 AcpiS3Save->S3Save (AcpiS3Save, NULL);\r
2243 }\r
2244 //\r
2245 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a\r
2246 // full device path\r
2247 //\r
2248 WorkingDevicePath = NULL;\r
2249 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
2250 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {\r
2251 WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r
2252 (HARDDRIVE_DEVICE_PATH *)DevicePath\r
2253 );\r
2254 if (WorkingDevicePath != NULL) {\r
2255 DevicePath = WorkingDevicePath;\r
2256 }\r
2257 }\r
7389fdd0 2258\r
5c08e117 2259 //\r
2260 // Set Boot Current\r
2261 //\r
3d7decbc 2262 if (IsBootOptionValidNVVarialbe (Option)) {\r
2263 //\r
2264 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.\r
2265 // In this case, "BootCurrent" is not created.\r
2266 // Only create the BootCurrent variable when it points to a valid Boot#### variable.\r
2267 //\r
2268 gRT->SetVariable (\r
2269 L"BootCurrent",\r
2270 &gEfiGlobalVariableGuid,\r
2271 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
2272 sizeof (UINT16),\r
2273 &Option->BootCurrent\r
2274 );\r
2275 }\r
5c08e117 2276\r
cb38c322 2277 //\r
2278 // Report Status Code to indicate ReadyToBoot event will be signalled\r
2279 //\r
2280 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT));\r
2281\r
72861c22 2282 //\r
2283 // Signal the EVT_SIGNAL_READY_TO_BOOT event\r
2284 //\r
2285 EfiSignalEventReadyToBoot();\r
2286\r
2287 //\r
2288 // Expand USB Class or USB WWID device path node to be full device path of a USB\r
2289 // device in platform then load the boot file on this full device path and get the\r
2290 // image handle.\r
2291 //\r
2292 ImageHandle = BdsExpandUsbShortFormDevicePath (DevicePath);\r
2293\r
2294 //\r
2295 // Adjust the different type memory page number just before booting\r
2296 // and save the updated info into the variable for next boot to use\r
2297 //\r
2298 BdsSetMemoryTypeInformationVariable ();\r
2299\r
5c08e117 2300 //\r
9972247d 2301 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.\r
2302 // Here get the ImageHandle for the non USB class or WWID device path.\r
5c08e117 2303 //\r
9972247d 2304 if (ImageHandle == NULL) {\r
2305 ASSERT (Option->DevicePath != NULL);\r
2306 if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&\r
2307 (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)\r
2308 ) {\r
2309 //\r
2310 // Check to see if we should legacy BOOT. If yes then do the legacy boot\r
2311 //\r
2312 return BdsLibDoLegacyBoot (Option);\r
5c08e117 2313 }\r
128efbbc 2314\r
5c08e117 2315 //\r
9972247d 2316 // If the boot option point to Internal FV shell, make sure it is valid\r
5c08e117 2317 //\r
9972247d 2318 Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));\r
2319 if (!EFI_ERROR(Status)) {\r
2320 if (Option->DevicePath != NULL) {\r
2321 FreePool(Option->DevicePath);\r
2322 }\r
2323 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
2324 ASSERT(Option->DevicePath != NULL);\r
2325 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
2326 //\r
2327 // Update the shell boot option\r
2328 //\r
2329 InitializeListHead (&TempBootLists);\r
2330 BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
2331\r
2332 //\r
2333 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r
2334 //\r
2335 FreePool (DevicePath);\r
2336 DevicePath = Option->DevicePath;\r
2337 }\r
5c08e117 2338\r
9972247d 2339 DEBUG_CODE_BEGIN();\r
5c08e117 2340\r
9aa7ba01 2341 if (Option->Description == NULL) {\r
2342 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));\r
2343 } else {\r
2344 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));\r
ab8cc80b 2345 }\r
9aa7ba01 2346 \r
9972247d 2347 DEBUG_CODE_END();\r
ab8cc80b 2348 \r
79b7a6a1 2349 //\r
2350 // Report status code for OS Loader LoadImage.\r
2351 //\r
2352 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
9972247d 2353 Status = gBS->LoadImage (\r
2354 TRUE,\r
2355 gImageHandle,\r
2356 DevicePath,\r
2357 NULL,\r
2358 0,\r
2359 &ImageHandle\r
2360 );\r
5c08e117 2361\r
5c08e117 2362 //\r
9972247d 2363 // If we didn't find an image directly, we need to try as if it is a removable device boot option\r
2364 // and load the image according to the default boot behavior for removable device.\r
5c08e117 2365 //\r
9972247d 2366 if (EFI_ERROR (Status)) {\r
2367 //\r
2368 // check if there is a bootable removable media could be found in this device path ,\r
2369 // and get the bootable media handle\r
2370 //\r
2371 Handle = BdsLibGetBootableHandle(DevicePath);\r
cb38c322 2372 if (Handle != NULL) {\r
2373 //\r
2374 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
2375 // machinename is ia32, ia64, x64, ...\r
2376 //\r
2377 FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
2378 if (FilePath != NULL) {\r
2379 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
2380 Status = gBS->LoadImage (\r
2381 TRUE,\r
2382 gImageHandle,\r
2383 FilePath,\r
2384 NULL,\r
2385 0,\r
2386 &ImageHandle\r
2387 );\r
9972247d 2388 }\r
2389 }\r
5c08e117 2390 }\r
5c08e117 2391 }\r
2392 //\r
2393 // Provide the image with it's load options\r
2394 //\r
cb38c322 2395 if ((ImageHandle == NULL) || (EFI_ERROR(Status))) {\r
2396 //\r
2397 // Report Status Code to indicate that the failure to load boot option\r
2398 //\r
2399 REPORT_STATUS_CODE (\r
2400 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
2401 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR)\r
2402 ); \r
9972247d 2403 goto Done;\r
2404 }\r
cb38c322 2405\r
5c08e117 2406 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
2407 ASSERT_EFI_ERROR (Status);\r
2408\r
2409 if (Option->LoadOptionsSize != 0) {\r
2410 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r
2411 ImageInfo->LoadOptions = Option->LoadOptions;\r
2412 }\r
bf0712ae
ED
2413\r
2414 //\r
2415 // Clean to NULL because the image is loaded directly from the firmwares boot manager.\r
2416 //\r
2417 ImageInfo->ParentHandle = NULL;\r
2418\r
5c08e117 2419 //\r
2420 // Before calling the image, enable the Watchdog Timer for\r
2421 // the 5 Minute period\r
2422 //\r
2423 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
2424\r
cd6a3b15 2425 //\r
2426 // Write boot to OS performance data for UEFI boot\r
2427 //\r
2428 PERF_CODE (\r
26c0ba77 2429 WriteBootToOsPerformanceData (NULL, NULL);\r
cd6a3b15 2430 );\r
2431\r
79b7a6a1 2432 //\r
2433 // Report status code for OS Loader StartImage.\r
2434 //\r
2435 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));\r
2436\r
5c08e117 2437 Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);\r
2438 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r
cb38c322 2439 if (EFI_ERROR (Status)) {\r
2440 //\r
2441 // Report Status Code to indicate that boot failure\r
2442 //\r
2443 REPORT_STATUS_CODE (\r
2444 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
2445 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED)\r
2446 );\r
2447 }\r
5c08e117 2448\r
2449 //\r
2450 // Clear the Watchdog Timer after the image returns\r
2451 //\r
2452 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
2453\r
2454Done:\r
2df686c6 2455 //\r
2456 // Set Logo status invalid after trying one boot option\r
2457 //\r
2458 BootLogo = NULL;\r
2459 StatusLogo = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);\r
2460 if (!EFI_ERROR (StatusLogo) && (BootLogo != NULL)) {\r
2461 BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);\r
2462 }\r
2463\r
5c08e117 2464 //\r
2465 // Clear Boot Current\r
2466 //\r
2467 gRT->SetVariable (\r
2468 L"BootCurrent",\r
2469 &gEfiGlobalVariableGuid,\r
2470 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
2471 0,\r
2472 &Option->BootCurrent\r
2473 );\r
2474\r
2475 return Status;\r
2476}\r
2477\r
2478\r
2479/**\r
2480 Expand a device path that starts with a hard drive media device path node to be a\r
2481 full device path that includes the full hardware path to the device. We need\r
2482 to do this so it can be booted. As an optimization the front match (the part point\r
2483 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r
2484 so a connect all is not required on every boot. All successful history device path\r
2485 which point to partition node (the front part) will be saved.\r
2486\r
2487 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard\r
2488 drive media device path.\r
2489 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path\r
2490 cannot be found.\r
2491\r
2492**/\r
2493EFI_DEVICE_PATH_PROTOCOL *\r
2494EFIAPI\r
2495BdsExpandPartitionPartialDevicePathToFull (\r
2496 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
2497 )\r
2498{\r
2499 EFI_STATUS Status;\r
2500 UINTN BlockIoHandleCount;\r
2501 EFI_HANDLE *BlockIoBuffer;\r
2502 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r
2503 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;\r
2504 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2505 UINTN Index;\r
2506 UINTN InstanceNum;\r
2507 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;\r
2508 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
2509 UINTN CachedDevicePathSize;\r
2510 BOOLEAN DeviceExist;\r
2511 BOOLEAN NeedAdjust;\r
2512 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
2513 UINTN Size;\r
2514\r
2515 FullDevicePath = NULL;\r
2516 //\r
e24fc103 2517 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.\r
5c08e117 2518 // If exist, search the front path which point to partition node in the variable instants.\r
e24fc103 2519 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system\r
5c08e117 2520 //\r
2521 CachedDevicePath = BdsLibGetVariableAndSize (\r
e24fc103
LG
2522 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2523 &gHdBootDevicePathVariablGuid,\r
5c08e117 2524 &CachedDevicePathSize\r
2525 );\r
128efbbc 2526\r
5c08e117 2527 if (CachedDevicePath != NULL) {\r
2528 TempNewDevicePath = CachedDevicePath;\r
2529 DeviceExist = FALSE;\r
2530 NeedAdjust = FALSE;\r
2531 do {\r
2532 //\r
2533 // Check every instance of the variable\r
2534 // First, check whether the instance contain the partition node, which is needed for distinguishing multi\r
2535 // partial partition boot option. Second, check whether the instance could be connected.\r
2536 //\r
2537 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r
2538 if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {\r
2539 //\r
2540 // Connect the device path instance, the device path point to hard drive media device path node\r
2541 // e.g. ACPI() /PCI()/ATA()/Partition()\r
2542 //\r
2543 Status = BdsLibConnectDevicePath (Instance);\r
2544 if (!EFI_ERROR (Status)) {\r
2545 DeviceExist = TRUE;\r
2546 break;\r
2547 }\r
2548 }\r
2549 //\r
2550 // Come here means the first instance is not matched\r
2551 //\r
2552 NeedAdjust = TRUE;\r
2553 FreePool(Instance);\r
2554 } while (TempNewDevicePath != NULL);\r
2555\r
2556 if (DeviceExist) {\r
2557 //\r
2558 // Find the matched device path.\r
2559 // Append the file path information from the boot option and return the fully expanded device path.\r
2560 //\r
2561 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
2562 FullDevicePath = AppendDevicePath (Instance, DevicePath);\r
2563\r
2564 //\r
e24fc103 2565 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.\r
5c08e117 2566 //\r
2567 if (NeedAdjust) {\r
2568 //\r
2569 // First delete the matched instance.\r
2570 //\r
2571 TempNewDevicePath = CachedDevicePath;\r
2572 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r
2573 FreePool (TempNewDevicePath);\r
128efbbc 2574\r
5c08e117 2575 //\r
2576 // Second, append the remaining path after the matched instance\r
2577 //\r
2578 TempNewDevicePath = CachedDevicePath;\r
2579 CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r
2580 FreePool (TempNewDevicePath);\r
2581 //\r
2582 // Save the matching Device Path so we don't need to do a connect all next time\r
2583 //\r
2584 Status = gRT->SetVariable (\r
e24fc103
LG
2585 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2586 &gHdBootDevicePathVariablGuid,\r
5c08e117 2587 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2588 GetDevicePathSize (CachedDevicePath),\r
2589 CachedDevicePath\r
2590 );\r
2591 }\r
128efbbc 2592\r
5c08e117 2593 FreePool (Instance);\r
2594 FreePool (CachedDevicePath);\r
2595 return FullDevicePath;\r
2596 }\r
2597 }\r
2598\r
2599 //\r
e24fc103 2600 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need\r
5c08e117 2601 // to search all devices in the system for a matched partition\r
2602 //\r
2603 BdsLibConnectAllDriversToAllControllers ();\r
2604 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r
2605 if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {\r
2606 //\r
2607 // If there was an error or there are no device handles that support\r
2608 // the BLOCK_IO Protocol, then return.\r
2609 //\r
2610 return NULL;\r
2611 }\r
2612 //\r
2613 // Loop through all the device handles that support the BLOCK_IO Protocol\r
2614 //\r
2615 for (Index = 0; Index < BlockIoHandleCount; Index++) {\r
2616\r
2617 Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);\r
2618 if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {\r
2619 continue;\r
2620 }\r
2621\r
2622 if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {\r
2623 //\r
2624 // Find the matched partition device path\r
2625 //\r
2626 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
2627 FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r
2628\r
2629 //\r
e24fc103 2630 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable\r
5c08e117 2631 //\r
2632 if (CachedDevicePath != NULL) {\r
2633 //\r
e24fc103 2634 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable\r
5c08e117 2635 //\r
2636 if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {\r
2637 TempNewDevicePath = CachedDevicePath;\r
2638 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r
2639 FreePool(TempNewDevicePath);\r
e5a9b63c 2640 }\r
5c08e117 2641\r
e5a9b63c 2642 if (CachedDevicePath != NULL) {\r
5c08e117 2643 TempNewDevicePath = CachedDevicePath;\r
2644 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
2645 FreePool(TempNewDevicePath);\r
e5a9b63c
RN
2646 } else {\r
2647 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r
5c08e117 2648 }\r
e5a9b63c 2649\r
5c08e117 2650 //\r
2651 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r
e24fc103
LG
2652 // If the user try to boot many OS in different HDs or partitions, in theory, \r
2653 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.\r
5c08e117 2654 //\r
2655 InstanceNum = 0;\r
2656 ASSERT (CachedDevicePath != NULL);\r
2657 TempNewDevicePath = CachedDevicePath;\r
2658 while (!IsDevicePathEnd (TempNewDevicePath)) {\r
2659 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
2660 //\r
2661 // Parse one instance\r
2662 //\r
2663 while (!IsDevicePathEndType (TempNewDevicePath)) {\r
2664 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
2665 }\r
2666 InstanceNum++;\r
2667 //\r
2668 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.\r
2669 //\r
2670 if (InstanceNum >= 12) {\r
2671 SetDevicePathEndNode (TempNewDevicePath);\r
2672 break;\r
2673 }\r
2674 }\r
2675 } else {\r
2676 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r
2677 }\r
2678\r
2679 //\r
2680 // Save the matching Device Path so we don't need to do a connect all next time\r
2681 //\r
2682 Status = gRT->SetVariable (\r
e24fc103
LG
2683 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,\r
2684 &gHdBootDevicePathVariablGuid,\r
5c08e117 2685 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2686 GetDevicePathSize (CachedDevicePath),\r
2687 CachedDevicePath\r
2688 );\r
2689\r
2690 break;\r
2691 }\r
2692 }\r
128efbbc 2693\r
cd730ec0 2694 if (CachedDevicePath != NULL) {\r
2695 FreePool (CachedDevicePath);\r
2696 }\r
5c08e117 2697 if (BlockIoBuffer != NULL) {\r
2698 FreePool (BlockIoBuffer);\r
2699 }\r
2700 return FullDevicePath;\r
2701}\r
2702\r
2703/**\r
2704 Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r
2705 instances, has the same partition node with HardDriveDevicePath device path\r
2706\r
2707 @param BlockIoDevicePath Multi device path instances which need to check\r
2708 @param HardDriveDevicePath A device path which starts with a hard drive media\r
2709 device path.\r
2710\r
2711 @retval TRUE There is a matched device path instance.\r
2712 @retval FALSE There is no matched device path instance.\r
2713\r
2714**/\r
2715BOOLEAN\r
2716EFIAPI\r
2717MatchPartitionDevicePathNode (\r
2718 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,\r
2719 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
2720 )\r
2721{\r
2722 HARDDRIVE_DEVICE_PATH *TmpHdPath;\r
2723 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2724 BOOLEAN Match;\r
2725 EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;\r
2726\r
2727 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r
2728 return FALSE;\r
2729 }\r
128efbbc 2730\r
5c08e117 2731 //\r
2732 // Make PreviousDevicePath == the device path node before the end node\r
2733 //\r
2734 DevicePath = BlockIoDevicePath;\r
2735 BlockIoHdDevicePathNode = NULL;\r
2736\r
2737 //\r
2738 // find the partition device path node\r
2739 //\r
2740 while (!IsDevicePathEnd (DevicePath)) {\r
2741 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
2742 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
2743 ) {\r
2744 BlockIoHdDevicePathNode = DevicePath;\r
2745 break;\r
2746 }\r
2747\r
2748 DevicePath = NextDevicePathNode (DevicePath);\r
2749 }\r
2750\r
2751 if (BlockIoHdDevicePathNode == NULL) {\r
2752 return FALSE;\r
2753 }\r
2754 //\r
2755 // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
2756 //\r
2757 TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;\r
2758 Match = FALSE;\r
128efbbc 2759\r
5c08e117 2760 //\r
2761 // Check for the match\r
2762 //\r
2763 if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&\r
2764 (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {\r
2765 switch (TmpHdPath->SignatureType) {\r
2766 case SIGNATURE_TYPE_GUID:\r
2767 Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);\r
2768 break;\r
2769 case SIGNATURE_TYPE_MBR:\r
2770 Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));\r
2771 break;\r
2772 default:\r
2773 Match = FALSE;\r
2774 break;\r
2775 }\r
2776 }\r
2777\r
2778 return Match;\r
2779}\r
2780\r
2781/**\r
2782 Delete the boot option associated with the handle passed in.\r
2783\r
2784 @param Handle The handle which present the device path to create\r
2785 boot option\r
2786\r
2787 @retval EFI_SUCCESS Delete the boot option success\r
2788 @retval EFI_NOT_FOUND If the Device Path is not found in the system\r
2789 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
2790 @retval Other Error return value from SetVariable()\r
2791\r
2792**/\r
2793EFI_STATUS\r
2794BdsLibDeleteOptionFromHandle (\r
2795 IN EFI_HANDLE Handle\r
2796 )\r
2797{\r
2798 UINT16 *BootOrder;\r
2799 UINT8 *BootOptionVar;\r
2800 UINTN BootOrderSize;\r
2801 UINTN BootOptionSize;\r
2802 EFI_STATUS Status;\r
2803 UINTN Index;\r
2804 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
2805 UINTN DevicePathSize;\r
2806 UINTN OptionDevicePathSize;\r
2807 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2808 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
2809 UINT8 *TempPtr;\r
2810\r
2811 Status = EFI_SUCCESS;\r
2812 BootOrder = NULL;\r
2813 BootOrderSize = 0;\r
2814\r
2815 //\r
2816 // Check "BootOrder" variable, if no, means there is no any boot order.\r
2817 //\r
2818 BootOrder = BdsLibGetVariableAndSize (\r
2819 L"BootOrder",\r
2820 &gEfiGlobalVariableGuid,\r
2821 &BootOrderSize\r
2822 );\r
2823 if (BootOrder == NULL) {\r
2824 return EFI_NOT_FOUND;\r
2825 }\r
2826\r
2827 //\r
2828 // Convert device handle to device path protocol instance\r
2829 //\r
2830 DevicePath = DevicePathFromHandle (Handle);\r
2831 if (DevicePath == NULL) {\r
2832 return EFI_NOT_FOUND;\r
2833 }\r
2834 DevicePathSize = GetDevicePathSize (DevicePath);\r
2835\r
2836 //\r
2837 // Loop all boot order variable and find the matching device path\r
2838 //\r
2839 Index = 0;\r
2840 while (Index < BootOrderSize / sizeof (UINT16)) {\r
2841 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
2842 BootOptionVar = BdsLibGetVariableAndSize (\r
2843 BootOption,\r
2844 &gEfiGlobalVariableGuid,\r
2845 &BootOptionSize\r
2846 );\r
128efbbc 2847\r
5c08e117 2848 if (BootOptionVar == NULL) {\r
2849 FreePool (BootOrder);\r
2850 return EFI_OUT_OF_RESOURCES;\r
2851 }\r
2852\r
8c08a567
ED
2853 if (!ValidateOption(BootOptionVar, BootOptionSize)) {\r
2854 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
2855 FreePool (BootOptionVar);\r
2856 Index++;\r
2857 continue;\r
2858 }\r
2859\r
5c08e117 2860 TempPtr = BootOptionVar;\r
2861 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
2862 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
2863 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
2864 OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
2865\r
2866 //\r
2867 // Check whether the device path match\r
2868 //\r
2869 if ((OptionDevicePathSize == DevicePathSize) &&\r
2870 (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
2871 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
2872 FreePool (BootOptionVar);\r
2873 break;\r
2874 }\r
2875\r
2876 FreePool (BootOptionVar);\r
2877 Index++;\r
2878 }\r
2879\r
2880 //\r
2881 // Adjust number of boot option for "BootOrder" variable.\r
2882 //\r
2883 Status = gRT->SetVariable (\r
2884 L"BootOrder",\r
2885 &gEfiGlobalVariableGuid,\r
2886 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2887 BootOrderSize,\r
2888 BootOrder\r
2889 );\r
2890\r
2891 FreePool (BootOrder);\r
2892\r
2893 return Status;\r
2894}\r
2895\r
2896\r
2897/**\r
3384a9bc 2898 Delete all invalid EFI boot options.\r
5c08e117 2899\r
2900 @retval EFI_SUCCESS Delete all invalid boot option success\r
2901 @retval EFI_NOT_FOUND Variable "BootOrder" is not found\r
2902 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
2903 @retval Other Error return value from SetVariable()\r
2904\r
2905**/\r
2906EFI_STATUS\r
2907BdsDeleteAllInvalidEfiBootOption (\r
2908 VOID\r
2909 )\r
2910{\r
2911 UINT16 *BootOrder;\r
2912 UINT8 *BootOptionVar;\r
2913 UINTN BootOrderSize;\r
2914 UINTN BootOptionSize;\r
2915 EFI_STATUS Status;\r
2916 UINTN Index;\r
2917 UINTN Index2;\r
2918 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
2919 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
2920 UINT8 *TempPtr;\r
3384a9bc 2921 CHAR16 *Description;\r
8c08a567 2922 BOOLEAN Corrupted;\r
5c08e117 2923\r
8c08a567
ED
2924 Status = EFI_SUCCESS;\r
2925 BootOrder = NULL;\r
2926 Description = NULL;\r
2927 OptionDevicePath = NULL;\r
2928 BootOrderSize = 0;\r
2929 Corrupted = FALSE;\r
5c08e117 2930\r
2931 //\r
2932 // Check "BootOrder" variable firstly, this variable hold the number of boot options\r
2933 //\r
2934 BootOrder = BdsLibGetVariableAndSize (\r
2935 L"BootOrder",\r
2936 &gEfiGlobalVariableGuid,\r
2937 &BootOrderSize\r
2938 );\r
2939 if (NULL == BootOrder) {\r
2940 return EFI_NOT_FOUND;\r
2941 }\r
2942\r
2943 Index = 0;\r
2944 while (Index < BootOrderSize / sizeof (UINT16)) {\r
2945 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
2946 BootOptionVar = BdsLibGetVariableAndSize (\r
2947 BootOption,\r
2948 &gEfiGlobalVariableGuid,\r
2949 &BootOptionSize\r
2950 );\r
2951 if (NULL == BootOptionVar) {\r
2952 FreePool (BootOrder);\r
2953 return EFI_OUT_OF_RESOURCES;\r
2954 }\r
2955\r
8c08a567
ED
2956 if (!ValidateOption(BootOptionVar, BootOptionSize)) {\r
2957 Corrupted = TRUE;\r
2958 } else {\r
2959 TempPtr = BootOptionVar;\r
2960 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
2961 Description = (CHAR16 *) TempPtr;\r
2962 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
2963 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
5c08e117 2964\r
8c08a567
ED
2965 //\r
2966 // Skip legacy boot option (BBS boot device)\r
2967 //\r
2968 if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
2969 (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
2970 FreePool (BootOptionVar);\r
2971 Index++;\r
2972 continue;\r
2973 }\r
5c08e117 2974 }\r
2975\r
8c08a567 2976 if (Corrupted || !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {\r
5c08e117 2977 //\r
2978 // Delete this invalid boot option "Boot####"\r
2979 //\r
2980 Status = gRT->SetVariable (\r
2981 BootOption,\r
2982 &gEfiGlobalVariableGuid,\r
2983 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
2984 0,\r
2985 NULL\r
2986 );\r
2987 //\r
2988 // Mark this boot option in boot order as deleted\r
2989 //\r
2990 BootOrder[Index] = 0xffff;\r
8c08a567 2991 Corrupted = FALSE;\r
5c08e117 2992 }\r
2993\r
2994 FreePool (BootOptionVar);\r
2995 Index++;\r
2996 }\r
2997\r
2998 //\r
2999 // Adjust boot order array\r
3000 //\r
3001 Index2 = 0;\r
3002 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {\r
3003 if (BootOrder[Index] != 0xffff) {\r
3004 BootOrder[Index2] = BootOrder[Index];\r
3005 Index2 ++;\r
3006 }\r
3007 }\r
3008 Status = gRT->SetVariable (\r
3009 L"BootOrder",\r
3010 &gEfiGlobalVariableGuid,\r
3011 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
3012 Index2 * sizeof (UINT16),\r
3013 BootOrder\r
3014 );\r
3015\r
3016 FreePool (BootOrder);\r
3017\r
3018 return Status;\r
3019}\r
3020\r
3021\r
3022/**\r
3384a9bc 3023 For EFI boot option, BDS separate them as six types:\r
128efbbc 3024 1. Network - The boot option points to the SimpleNetworkProtocol device.\r
3384a9bc 3025 Bds will try to automatically create this type boot option when enumerate.\r
128efbbc 3026 2. Shell - The boot option points to internal flash shell.\r
3384a9bc 3027 Bds will try to automatically create this type boot option when enumerate.\r
3028 3. Removable BlockIo - The boot option only points to the removable media\r
3029 device, like USB flash disk, DVD, Floppy etc.\r
3030 These device should contain a *removable* blockIo\r
3031 protocol in their device handle.\r
128efbbc 3032 Bds will try to automatically create this type boot option\r
3384a9bc 3033 when enumerate.\r
128efbbc 3034 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,\r
3384a9bc 3035 like HardDisk.\r
3036 These device should contain a *fixed* blockIo\r
3037 protocol in their device handle.\r
3038 BDS will skip fixed blockIo devices, and NOT\r
128efbbc 3039 automatically create boot option for them. But BDS\r
3040 will help to delete those fixed blockIo boot option,\r
3384a9bc 3041 whose description rule conflict with other auto-created\r
3042 boot options.\r
128efbbc 3043 5. Non-BlockIo Simplefile - The boot option points to a device whose handle\r
3384a9bc 3044 has SimpleFileSystem Protocol, but has no blockio\r
3045 protocol. These devices do not offer blockIo\r
128efbbc 3046 protocol, but BDS still can get the\r
3384a9bc 3047 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem\r
3048 Protocol.\r
128efbbc 3049 6. File - The boot option points to a file. These boot options are usually\r
3384a9bc 3050 created by user manually or OS loader. BDS will not delete or modify\r
128efbbc 3051 these boot options.\r
3052\r
3384a9bc 3053 This function will enumerate all possible boot device in the system, and\r
128efbbc 3054 automatically create boot options for Network, Shell, Removable BlockIo,\r
3384a9bc 3055 and Non-BlockIo Simplefile devices.\r
8d3b5aff 3056 It will only execute once of every boot.\r
128efbbc 3057\r
5c08e117 3058 @param BdsBootOptionList The header of the link list which indexed all\r
3059 current boot options\r
3060\r
3061 @retval EFI_SUCCESS Finished all the boot device enumerate and create\r
3062 the boot option base on that boot device\r
3063\r
e83c9064 3064 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list\r
5c08e117 3065**/\r
3066EFI_STATUS\r
3067EFIAPI\r
3068BdsLibEnumerateAllBootOption (\r
3069 IN OUT LIST_ENTRY *BdsBootOptionList\r
3070 )\r
3071{\r
3072 EFI_STATUS Status;\r
3073 UINT16 FloppyNumber;\r
889a4bc2 3074 UINT16 HarddriveNumber;\r
5c08e117 3075 UINT16 CdromNumber;\r
3076 UINT16 UsbNumber;\r
3077 UINT16 MiscNumber;\r
8d3b5aff 3078 UINT16 ScsiNumber;\r
5c08e117 3079 UINT16 NonBlockNumber;\r
3080 UINTN NumberBlockIoHandles;\r
3081 EFI_HANDLE *BlockIoHandles;\r
3082 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
35bc0e9f
RN
3083 BOOLEAN Removable[2];\r
3084 UINTN RemovableIndex;\r
5c08e117 3085 UINTN Index;\r
a7a523e0 3086 UINTN NumOfLoadFileHandles;\r
3087 EFI_HANDLE *LoadFileHandles;\r
5c08e117 3088 UINTN FvHandleCount;\r
3089 EFI_HANDLE *FvHandleBuffer;\r
3090 EFI_FV_FILETYPE Type;\r
3091 UINTN Size;\r
3092 EFI_FV_FILE_ATTRIBUTES Attributes;\r
3093 UINT32 AuthenticationStatus;\r
8d3b5aff 3094 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
3095 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
5c08e117 3096 UINTN DevicePathType;\r
3097 CHAR16 Buffer[40];\r
3098 EFI_HANDLE *FileSystemHandles;\r
3099 UINTN NumberFileSystemHandles;\r
3100 BOOLEAN NeedDelete;\r
3101 EFI_IMAGE_DOS_HEADER DosHeader;\r
9aa7ba01 3102 CHAR8 *PlatLang;\r
3103 CHAR8 *LastLang;\r
5c08e117 3104 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
3105 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
3106\r
889a4bc2
RN
3107 FloppyNumber = 0;\r
3108 HarddriveNumber = 0;\r
3109 CdromNumber = 0;\r
3110 UsbNumber = 0;\r
3111 MiscNumber = 0;\r
3112 ScsiNumber = 0;\r
3113 PlatLang = NULL;\r
3114 LastLang = NULL;\r
5c08e117 3115 ZeroMem (Buffer, sizeof (Buffer));\r
128efbbc 3116\r
5c08e117 3117 //\r
3118 // If the boot device enumerate happened, just get the boot\r
3119 // device from the boot order variable\r
3120 //\r
3121 if (mEnumBootDevice) {\r
f01b91ae
ED
3122 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME, &gLastEnumLangGuid, (VOID**)&LastLang, NULL);\r
3123 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatLang, NULL);\r
0fa3ac1b
RN
3124 ASSERT (PlatLang != NULL);\r
3125 if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {\r
9aa7ba01 3126 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
0fa3ac1b
RN
3127 FreePool (LastLang);\r
3128 FreePool (PlatLang);\r
9aa7ba01 3129 return Status;\r
3130 } else {\r
3131 Status = gRT->SetVariable (\r
e24fc103
LG
3132 LAST_ENUM_LANGUAGE_VARIABLE_NAME,\r
3133 &gLastEnumLangGuid,\r
9aa7ba01 3134 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
0fa3ac1b 3135 AsciiStrSize (PlatLang),\r
9aa7ba01 3136 PlatLang\r
3137 );\r
3138 ASSERT_EFI_ERROR (Status);\r
0fa3ac1b
RN
3139\r
3140 if (LastLang != NULL) {\r
3141 FreePool (LastLang);\r
3142 }\r
3143 FreePool (PlatLang);\r
9aa7ba01 3144 }\r
5c08e117 3145 }\r
128efbbc 3146\r
5c08e117 3147 //\r
3148 // Notes: this dirty code is to get the legacy boot option from the\r
3149 // BBS table and create to variable as the EFI boot option, it should\r
3150 // be removed after the CSM can provide legacy boot option directly\r
3151 //\r
3152 REFRESH_LEGACY_BOOT_OPTIONS;\r
3153\r
3154 //\r
3155 // Delete invalid boot option\r
3156 //\r
3157 BdsDeleteAllInvalidEfiBootOption ();\r
128efbbc 3158\r
5c08e117 3159 //\r
701e17e5
RN
3160 // Parse removable media followed by fixed media.\r
3161 // The Removable[] array is used by the for-loop below to create removable media boot options \r
3162 // at first, and then to create fixed media boot options.\r
5c08e117 3163 //\r
701e17e5
RN
3164 Removable[0] = FALSE;\r
3165 Removable[1] = TRUE;\r
3166\r
5c08e117 3167 gBS->LocateHandleBuffer (\r
3168 ByProtocol,\r
3169 &gEfiBlockIoProtocolGuid,\r
3170 NULL,\r
3171 &NumberBlockIoHandles,\r
3172 &BlockIoHandles\r
3173 );\r
128efbbc 3174\r
35bc0e9f
RN
3175 for (RemovableIndex = 0; RemovableIndex < 2; RemovableIndex++) {\r
3176 for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r
3177 Status = gBS->HandleProtocol (\r
3178 BlockIoHandles[Index],\r
3179 &gEfiBlockIoProtocolGuid,\r
3180 (VOID **) &BlkIo\r
3181 );\r
3182 //\r
3183 // skip the fixed block io then the removable block io\r
3184 //\r
3185 if (EFI_ERROR (Status) || (BlkIo->Media->RemovableMedia == Removable[RemovableIndex])) {\r
5c08e117 3186 continue;\r
3187 }\r
35bc0e9f
RN
3188 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);\r
3189 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);\r
5c08e117 3190\r
35bc0e9f
RN
3191 switch (DevicePathType) {\r
3192 case BDS_EFI_ACPI_FLOPPY_BOOT:\r
3193 if (FloppyNumber != 0) {\r
3194 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);\r
3195 } else {\r
3196 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));\r
3197 }\r
3198 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3199 FloppyNumber++;\r
3200 break;\r
128efbbc 3201\r
35bc0e9f 3202 //\r
889a4bc2 3203 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.\r
35bc0e9f
RN
3204 //\r
3205 case BDS_EFI_MESSAGE_ATAPI_BOOT:\r
3206 case BDS_EFI_MESSAGE_SATA_BOOT:\r
889a4bc2
RN
3207 if (BlkIo->Media->RemovableMedia) {\r
3208 if (CdromNumber != 0) {\r
3209 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);\r
3210 } else {\r
3211 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));\r
3212 }\r
3213 CdromNumber++;\r
35bc0e9f 3214 } else {\r
889a4bc2
RN
3215 if (HarddriveNumber != 0) {\r
3216 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)), HarddriveNumber);\r
3217 } else {\r
3218 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)));\r
3219 }\r
3220 HarddriveNumber++;\r
35bc0e9f
RN
3221 }\r
3222 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));\r
3223 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
35bc0e9f
RN
3224 break;\r
3225\r
3226 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:\r
3227 if (UsbNumber != 0) {\r
3228 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);\r
3229 } else {\r
3230 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));\r
3231 }\r
3232 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3233 UsbNumber++;\r
3234 break;\r
5c08e117 3235\r
35bc0e9f
RN
3236 case BDS_EFI_MESSAGE_SCSI_BOOT:\r
3237 if (ScsiNumber != 0) {\r
3238 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);\r
3239 } else {\r
3240 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));\r
3241 }\r
3242 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3243 ScsiNumber++;\r
3244 break;\r
5c08e117 3245\r
35bc0e9f
RN
3246 case BDS_EFI_MESSAGE_MISC_BOOT:\r
3247 if (MiscNumber != 0) {\r
3248 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);\r
3249 } else {\r
3250 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));\r
3251 }\r
3252 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
3253 MiscNumber++;\r
3254 break;\r
5c08e117 3255\r
35bc0e9f
RN
3256 default:\r
3257 break;\r
9aa7ba01 3258 }\r
5c08e117 3259 }\r
3260 }\r
3261\r
3262 if (NumberBlockIoHandles != 0) {\r
3263 FreePool (BlockIoHandles);\r
3264 }\r
3265\r
3266 //\r
3267 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.\r
3268 //\r
3269 NonBlockNumber = 0;\r
3270 gBS->LocateHandleBuffer (\r
3271 ByProtocol,\r
3272 &gEfiSimpleFileSystemProtocolGuid,\r
3273 NULL,\r
3274 &NumberFileSystemHandles,\r
3275 &FileSystemHandles\r
3276 );\r
3277 for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r
3278 Status = gBS->HandleProtocol (\r
3279 FileSystemHandles[Index],\r
3280 &gEfiBlockIoProtocolGuid,\r
3281 (VOID **) &BlkIo\r
3282 );\r
3283 if (!EFI_ERROR (Status)) {\r
3284 //\r
3285 // Skip if the file system handle supports a BlkIo protocol,\r
3286 //\r
3287 continue;\r
3288 }\r
3289\r
3290 //\r
3291 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
3292 // machinename is ia32, ia64, x64, ...\r
3293 //\r
35bc0e9f 3294 Hdr.Union = &HdrData;\r
5c08e117 3295 NeedDelete = TRUE;\r
3296 Status = BdsLibGetImageHeader (\r
3297 FileSystemHandles[Index],\r
c62dbf31 3298 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 3299 &DosHeader,\r
3300 Hdr\r
3301 );\r
3302 if (!EFI_ERROR (Status) &&\r
3303 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
3304 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
3305 NeedDelete = FALSE;\r
3306 }\r
3307\r
3308 if (NeedDelete) {\r
3309 //\r
3310 // No such file or the file is not a EFI application, delete this boot option\r
3311 //\r
3312 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
3313 } else {\r
9aa7ba01 3314 if (NonBlockNumber != 0) {\r
3315 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);\r
3316 } else {\r
3317 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));\r
3318 }\r
5c08e117 3319 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);\r
3320 NonBlockNumber++;\r
3321 }\r
3322 }\r
3323\r
3324 if (NumberFileSystemHandles != 0) {\r
3325 FreePool (FileSystemHandles);\r
3326 }\r
3327\r
3328 //\r
3329 // Parse Network Boot Device\r
3330 //\r
a7a523e0 3331 NumOfLoadFileHandles = 0;\r
ff482c56 3332 //\r
a7a523e0 3333 // Search Load File protocol for PXE boot option.\r
ff482c56 3334 //\r
5c08e117 3335 gBS->LocateHandleBuffer (\r
3336 ByProtocol,\r
a7a523e0 3337 &gEfiLoadFileProtocolGuid,\r
5c08e117 3338 NULL,\r
a7a523e0 3339 &NumOfLoadFileHandles,\r
3340 &LoadFileHandles\r
5c08e117 3341 );\r
8d3b5aff 3342\r
a7a523e0 3343 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {\r
9aa7ba01 3344 if (Index != 0) {\r
3345 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);\r
3346 } else {\r
3347 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));\r
3348 }\r
a7a523e0 3349 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);\r
5c08e117 3350 }\r
3351\r
a7a523e0 3352 if (NumOfLoadFileHandles != 0) {\r
3353 FreePool (LoadFileHandles);\r
5c08e117 3354 }\r
3355\r
3356 //\r
3357 // Check if we have on flash shell\r
3358 //\r
3359 gBS->LocateHandleBuffer (\r
3360 ByProtocol,\r
3361 &gEfiFirmwareVolume2ProtocolGuid,\r
3362 NULL,\r
3363 &FvHandleCount,\r
3364 &FvHandleBuffer\r
3365 );\r
3366 for (Index = 0; Index < FvHandleCount; Index++) {\r
5c08e117 3367 gBS->HandleProtocol (\r
3368 FvHandleBuffer[Index],\r
3369 &gEfiFirmwareVolume2ProtocolGuid,\r
3370 (VOID **) &Fv\r
3371 );\r
3372\r
3373 Status = Fv->ReadFile (\r
3374 Fv,\r
d46f3632 3375 PcdGetPtr(PcdShellFile),\r
5c08e117 3376 NULL,\r
3377 &Size,\r
3378 &Type,\r
3379 &Attributes,\r
3380 &AuthenticationStatus\r
3381 );\r
3382 if (EFI_ERROR (Status)) {\r
3383 //\r
3384 // Skip if no shell file in the FV\r
3385 //\r
3386 continue;\r
3387 }\r
3388 //\r
3389 // Build the shell boot option\r
3390 //\r
3391 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
3392 }\r
3393\r
3394 if (FvHandleCount != 0) {\r
3395 FreePool (FvHandleBuffer);\r
3396 }\r
3397 //\r
3398 // Make sure every boot only have one time\r
3399 // boot device enumerate\r
3400 //\r
e83c9064 3401 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
5c08e117 3402 mEnumBootDevice = TRUE;\r
3403\r
e83c9064 3404 return Status;\r
5c08e117 3405}\r
3406\r
3407/**\r
3408 Build the boot option with the handle parsed in\r
3409\r
3410 @param Handle The handle which present the device path to create\r
3411 boot option\r
3412 @param BdsBootOptionList The header of the link list which indexed all\r
3413 current boot options\r
3414 @param String The description of the boot option.\r
3415\r
3416**/\r
3417VOID\r
3418EFIAPI\r
3419BdsLibBuildOptionFromHandle (\r
3420 IN EFI_HANDLE Handle,\r
3421 IN LIST_ENTRY *BdsBootOptionList,\r
3422 IN CHAR16 *String\r
3423 )\r
3424{\r
3425 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
128efbbc 3426\r
8d3b5aff 3427 DevicePath = DevicePathFromHandle (Handle);\r
5c08e117 3428\r
3429 //\r
3430 // Create and register new boot option\r
3431 //\r
3432 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r
3433}\r
3434\r
3435\r
3436/**\r
3437 Build the on flash shell boot option with the handle parsed in.\r
3438\r
3439 @param Handle The handle which present the device path to create\r
3440 on flash shell boot option\r
3441 @param BdsBootOptionList The header of the link list which indexed all\r
3442 current boot options\r
3443\r
3444**/\r
3445VOID\r
3446EFIAPI\r
3447BdsLibBuildOptionFromShell (\r
3448 IN EFI_HANDLE Handle,\r
3449 IN OUT LIST_ENTRY *BdsBootOptionList\r
3450 )\r
3451{\r
3452 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
3453 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
3454\r
3455 DevicePath = DevicePathFromHandle (Handle);\r
3456\r
3457 //\r
3458 // Build the shell device path\r
3459 //\r
d46f3632 3460 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));\r
5c08e117 3461\r
3462 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
3463\r
3464 //\r
3465 // Create and register the shell boot option\r
3466 //\r
3467 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
3468\r
3469}\r
3470\r
3471/**\r
3472 Boot from the UEFI spec defined "BootNext" variable.\r
3473\r
3474**/\r
3475VOID\r
3476EFIAPI\r
3477BdsLibBootNext (\r
3478 VOID\r
3479 )\r
3480{\r
3481 UINT16 *BootNext;\r
3482 UINTN BootNextSize;\r
3483 CHAR16 Buffer[20];\r
3484 BDS_COMMON_OPTION *BootOption;\r
3485 LIST_ENTRY TempList;\r
3486 UINTN ExitDataSize;\r
3487 CHAR16 *ExitData;\r
3488\r
3489 //\r
3490 // Init the boot option name buffer and temp link list\r
3491 //\r
3492 InitializeListHead (&TempList);\r
3493 ZeroMem (Buffer, sizeof (Buffer));\r
3494\r
3495 BootNext = BdsLibGetVariableAndSize (\r
3496 L"BootNext",\r
3497 &gEfiGlobalVariableGuid,\r
3498 &BootNextSize\r
3499 );\r
3500\r
3501 //\r
3502 // Clear the boot next variable first\r
3503 //\r
3504 if (BootNext != NULL) {\r
3505 gRT->SetVariable (\r
3506 L"BootNext",\r
3507 &gEfiGlobalVariableGuid,\r
3508 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
3509 0,\r
3510 BootNext\r
3511 );\r
3512\r
3513 //\r
3514 // Start to build the boot option and try to boot\r
3515 //\r
3516 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
3517 BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
3518 ASSERT (BootOption != NULL);\r
3519 BdsLibConnectDevicePath (BootOption->DevicePath);\r
3520 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
3521 }\r
3522\r
3523}\r
3524\r
3525/**\r
3526 Return the bootable media handle.\r
3527 First, check the device is connected\r
3528 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r
3529 Third, detect the the default boot file in the Media, and return the removable Media handle.\r
3530\r
e83c9064 3531 @param DevicePath Device Path to a bootable device\r
5c08e117 3532\r
e83c9064 3533 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.\r
5c08e117 3534\r
3535**/\r
3536EFI_HANDLE\r
3537EFIAPI\r
3538BdsLibGetBootableHandle (\r
3539 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3540 )\r
3541{\r
3542 EFI_STATUS Status;\r
ef949581 3543 EFI_TPL OldTpl;\r
5c08e117 3544 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
3545 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r
3546 EFI_HANDLE Handle;\r
3547 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
3548 VOID *Buffer;\r
3549 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3550 UINTN Size;\r
3551 UINTN TempSize;\r
3552 EFI_HANDLE ReturnHandle;\r
3553 EFI_HANDLE *SimpleFileSystemHandles;\r
3554\r
3555 UINTN NumberSimpleFileSystemHandles;\r
3556 UINTN Index;\r
3557 EFI_IMAGE_DOS_HEADER DosHeader;\r
3558 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
3559 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
3560\r
3561 UpdatedDevicePath = DevicePath;\r
128efbbc 3562\r
ef949581
RN
3563 //\r
3564 // Enter to critical section to protect the acquired BlockIo instance \r
3565 // from getting released due to the USB mass storage hotplug event\r
3566 //\r
3567 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
3568\r
5c08e117 3569 //\r
3570 // Check whether the device is connected\r
3571 //\r
3572 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r
3573 if (EFI_ERROR (Status)) {\r
3574 //\r
3575 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r
3576 //\r
3577 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r
3578 if (EFI_ERROR (Status)) {\r
3579 //\r
3580 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r
3581 //\r
3582 UpdatedDevicePath = DevicePath;\r
3583 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
3584 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
3585 }\r
3586 } else {\r
e74f510b
RN
3587 //\r
3588 // For removable device boot option, its contained device path only point to the removable device handle, \r
3589 // should make sure all its children handles (its child partion or media handles) are created and connected. \r
3590 //\r
3591 gBS->ConnectController (Handle, NULL, NULL, TRUE); \r
5c08e117 3592 //\r
3593 // Get BlockIo protocol and check removable attribute\r
3594 //\r
3595 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
ef949581
RN
3596 ASSERT_EFI_ERROR (Status);\r
3597\r
5c08e117 3598 //\r
3599 // Issue a dummy read to the device to check for media change.\r
3600 // When the removable media is changed, any Block IO read/write will\r
3601 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
3602 // returned. After the Block IO protocol is reinstalled, subsequent\r
3603 // Block IO read/write will success.\r
3604 //\r
3605 Buffer = AllocatePool (BlockIo->Media->BlockSize);\r
3606 if (Buffer != NULL) {\r
3607 BlockIo->ReadBlocks (\r
3608 BlockIo,\r
3609 BlockIo->Media->MediaId,\r
3610 0,\r
3611 BlockIo->Media->BlockSize,\r
3612 Buffer\r
3613 );\r
3614 FreePool(Buffer);\r
3615 }\r
3616 }\r
3617\r
3618 //\r
3619 // Detect the the default boot file from removable Media\r
3620 //\r
3621\r
3622 //\r
3623 // 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
3624 // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
3625 //\r
3626 DupDevicePath = DuplicateDevicePath (DevicePath);\r
3627 ASSERT (DupDevicePath != NULL);\r
128efbbc 3628\r
5c08e117 3629 UpdatedDevicePath = DupDevicePath;\r
3630 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
3631 //\r
3632 // 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
3633 // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r
3634 //\r
3635 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r
3636 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r
3637 //\r
3638 // Remove the usb node, let the device path only point to PCI node\r
3639 //\r
3640 SetDevicePathEndNode (UpdatedDevicePath);\r
3641 UpdatedDevicePath = DupDevicePath;\r
3642 } else {\r
3643 UpdatedDevicePath = DevicePath;\r
3644 }\r
3645\r
3646 //\r
3647 // Get the device path size of boot option\r
3648 //\r
3649 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
3650 ReturnHandle = NULL;\r
3651 gBS->LocateHandleBuffer (\r
3652 ByProtocol,\r
3653 &gEfiSimpleFileSystemProtocolGuid,\r
3654 NULL,\r
3655 &NumberSimpleFileSystemHandles,\r
3656 &SimpleFileSystemHandles\r
3657 );\r
3658 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
3659 //\r
3660 // Get the device path size of SimpleFileSystem handle\r
3661 //\r
3662 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
3663 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
3664 //\r
3665 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r
3666 //\r
3667 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r
3668 //\r
3669 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
3670 // machinename is ia32, ia64, x64, ...\r
3671 //\r
3672 Hdr.Union = &HdrData;\r
3673 Status = BdsLibGetImageHeader (\r
3674 SimpleFileSystemHandles[Index],\r
c62dbf31 3675 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 3676 &DosHeader,\r
3677 Hdr\r
3678 );\r
3679 if (!EFI_ERROR (Status) &&\r
3680 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
3681 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
3682 ReturnHandle = SimpleFileSystemHandles[Index];\r
3683 break;\r
3684 }\r
3685 }\r
3686 }\r
3687\r
3688 FreePool(DupDevicePath);\r
3689\r
3690 if (SimpleFileSystemHandles != NULL) {\r
3691 FreePool(SimpleFileSystemHandles);\r
3692 }\r
3693\r
ef949581
RN
3694 gBS->RestoreTPL (OldTpl);\r
3695\r
5c08e117 3696 return ReturnHandle;\r
3697}\r
3698\r
3699/**\r
3700 Check to see if the network cable is plugged in. If the DevicePath is not\r
3701 connected it will be connected.\r
3702\r
3703 @param DevicePath Device Path to check\r
3704\r
3705 @retval TRUE DevicePath points to an Network that is connected\r
3706 @retval FALSE DevicePath does not point to a bootable network\r
3707\r
3708**/\r
3709BOOLEAN\r
3710BdsLibNetworkBootWithMediaPresent (\r
3711 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3712 )\r
3713{\r
3714 EFI_STATUS Status;\r
3715 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
3716 EFI_HANDLE Handle;\r
3717 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
3718 BOOLEAN MediaPresent;\r
e51e619e 3719 UINT32 InterruptStatus;\r
5c08e117 3720\r
3721 MediaPresent = FALSE;\r
3722\r
3723 UpdatedDevicePath = DevicePath;\r
ff482c56 3724 //\r
a7a523e0 3725 // Locate Load File Protocol for PXE boot option first\r
ff482c56 3726 //\r
a7a523e0 3727 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 3728 if (EFI_ERROR (Status)) {\r
3729 //\r
3730 // Device not present so see if we need to connect it\r
3731 //\r
3732 Status = BdsLibConnectDevicePath (DevicePath);\r
3733 if (!EFI_ERROR (Status)) {\r
3734 //\r
3735 // This one should work after we did the connect\r
3736 //\r
a7a523e0 3737 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 3738 }\r
3739 }\r
3740\r
3741 if (!EFI_ERROR (Status)) {\r
3742 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r
ff482c56 3743 if (EFI_ERROR (Status)) {\r
3744 //\r
3745 // Failed to open SNP from this handle, try to get SNP from parent handle\r
3746 //\r
3747 UpdatedDevicePath = DevicePathFromHandle (Handle);\r
3748 if (UpdatedDevicePath != NULL) {\r
3749 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
3750 if (!EFI_ERROR (Status)) {\r
3751 //\r
3752 // SNP handle found, get SNP from it\r
3753 //\r
3754 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);\r
3755 }\r
3756 }\r
3757 }\r
3758\r
5c08e117 3759 if (!EFI_ERROR (Status)) {\r
3760 if (Snp->Mode->MediaPresentSupported) {\r
3761 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r
e51e619e 3762 //\r
3763 // Invoke Snp->GetStatus() to refresh the media status\r
3764 //\r
3765 Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
3766\r
5c08e117 3767 //\r
3768 // In case some one else is using the SNP check to see if it's connected\r
3769 //\r
3770 MediaPresent = Snp->Mode->MediaPresent;\r
3771 } else {\r
3772 //\r
3773 // No one is using SNP so we need to Start and Initialize so\r
3774 // MediaPresent will be valid.\r
3775 //\r
3776 Status = Snp->Start (Snp);\r
3777 if (!EFI_ERROR (Status)) {\r
3778 Status = Snp->Initialize (Snp, 0, 0);\r
3779 if (!EFI_ERROR (Status)) {\r
3780 MediaPresent = Snp->Mode->MediaPresent;\r
3781 Snp->Shutdown (Snp);\r
3782 }\r
3783 Snp->Stop (Snp);\r
3784 }\r
3785 }\r
3786 } else {\r
3787 MediaPresent = TRUE;\r
3788 }\r
3789 }\r
3790 }\r
3791\r
3792 return MediaPresent;\r
3793}\r
3794\r
3795/**\r
3796 For a bootable Device path, return its boot type.\r
3797\r
3798 @param DevicePath The bootable device Path to check\r
3799\r
128efbbc 3800 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
11c5022d 3801 which subtype is MEDIA_HARDDRIVE_DP\r
3802 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
3803 which subtype is MEDIA_CDROM_DP\r
3804 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node\r
3805 which HID is floppy device.\r
3806 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3807 and its last device path node's subtype is MSG_ATAPI_DP.\r
3808 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3809 and its last device path node's subtype is MSG_SCSI_DP.\r
3810 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
3811 and its last device path node's subtype is MSG_USB_DP.\r
5c08e117 3812 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r
11c5022d 3813 its last device path node point to a message device path node.\r
3814 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.\r
128efbbc 3815 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,\r
5c08e117 3816\r
3817**/\r
3818UINT32\r
3819EFIAPI\r
3820BdsGetBootTypeFromDevicePath (\r
3821 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
3822 )\r
3823{\r
3824 ACPI_HID_DEVICE_PATH *Acpi;\r
3825 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3826 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
ff482c56 3827 UINT32 BootType;\r
5c08e117 3828\r
3829 if (NULL == DevicePath) {\r
3830 return BDS_EFI_UNSUPPORT;\r
3831 }\r
3832\r
3833 TempDevicePath = DevicePath;\r
3834\r
3835 while (!IsDevicePathEndType (TempDevicePath)) {\r
3836 switch (DevicePathType (TempDevicePath)) {\r
3837 case BBS_DEVICE_PATH:\r
3838 return BDS_LEGACY_BBS_BOOT;\r
3839 case MEDIA_DEVICE_PATH:\r
3840 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r
3841 return BDS_EFI_MEDIA_HD_BOOT;\r
3842 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r
3843 return BDS_EFI_MEDIA_CDROM_BOOT;\r
128efbbc 3844 }\r
5c08e117 3845 break;\r
3846 case ACPI_DEVICE_PATH:\r
3847 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r
3848 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r
3849 return BDS_EFI_ACPI_FLOPPY_BOOT;\r
3850 }\r
3851 break;\r
3852 case MESSAGING_DEVICE_PATH:\r
3853 //\r
3854 // Get the last device path node\r
3855 //\r
3856 LastDeviceNode = NextDevicePathNode (TempDevicePath);\r
3857 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r
3858 //\r
3859 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r
ff482c56 3860 // skip it\r
5c08e117 3861 //\r
3862 LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r
3863 }\r
3864 //\r
3865 // if the device path not only point to driver device, it is not a messaging device path,\r
3866 //\r
3867 if (!IsDevicePathEndType (LastDeviceNode)) {\r
128efbbc 3868 break;\r
5c08e117 3869 }\r
3870\r
ff482c56 3871 switch (DevicePathSubType (TempDevicePath)) {\r
3872 case MSG_ATAPI_DP:\r
3873 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;\r
3874 break;\r
3875\r
3876 case MSG_USB_DP:\r
3877 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r
3878 break;\r
3879\r
3880 case MSG_SCSI_DP:\r
3881 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;\r
3882 break;\r
3883\r
3884 case MSG_SATA_DP:\r
3885 BootType = BDS_EFI_MESSAGE_SATA_BOOT;\r
3886 break;\r
3887\r
3888 case MSG_MAC_ADDR_DP:\r
3889 case MSG_VLAN_DP:\r
a7a523e0 3890 case MSG_IPv4_DP:\r
3891 case MSG_IPv6_DP:\r
ff482c56 3892 BootType = BDS_EFI_MESSAGE_MAC_BOOT;\r
3893 break;\r
3894\r
3895 default:\r
3896 BootType = BDS_EFI_MESSAGE_MISC_BOOT;\r
3897 break;\r
5c08e117 3898 }\r
ff482c56 3899 return BootType;\r
3900\r
5c08e117 3901 default:\r
3902 break;\r
3903 }\r
3904 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
3905 }\r
3906\r
3907 return BDS_EFI_UNSUPPORT;\r
3908}\r
3909\r
3910/**\r
3911 Check whether the Device path in a boot option point to a valid bootable device,\r
3912 And if CheckMedia is true, check the device is ready to boot now.\r
3913\r
3914 @param DevPath the Device path in a boot option\r
3915 @param CheckMedia if true, check the device is ready to boot now.\r
3916\r
3917 @retval TRUE the Device path is valid\r
3918 @retval FALSE the Device path is invalid .\r
3919\r
3920**/\r
3921BOOLEAN\r
3922EFIAPI\r
3923BdsLibIsValidEFIBootOptDevicePath (\r
3924 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
3925 IN BOOLEAN CheckMedia\r
3926 )\r
3384a9bc 3927{\r
3928 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);\r
3929}\r
3930\r
3931/**\r
3932 Check whether the Device path in a boot option point to a valid bootable device,\r
3933 And if CheckMedia is true, check the device is ready to boot now.\r
3934 If Description is not NULL and the device path point to a fixed BlockIo\r
3935 device, check the description whether conflict with other auto-created\r
3936 boot options.\r
3937\r
3938 @param DevPath the Device path in a boot option\r
3939 @param CheckMedia if true, check the device is ready to boot now.\r
3940 @param Description the description in a boot option\r
3941\r
3942 @retval TRUE the Device path is valid\r
3943 @retval FALSE the Device path is invalid .\r
3944\r
3945**/\r
3946BOOLEAN\r
3947EFIAPI\r
3948BdsLibIsValidEFIBootOptDevicePathExt (\r
3949 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
3950 IN BOOLEAN CheckMedia,\r
3951 IN CHAR16 *Description\r
3952 )\r
5c08e117 3953{\r
3954 EFI_STATUS Status;\r
3955 EFI_HANDLE Handle;\r
3956 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
3957 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
3958 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
3959\r
3960 TempDevicePath = DevPath;\r
3961 LastDeviceNode = DevPath;\r
128efbbc 3962\r
5c08e117 3963 //\r
a7a523e0 3964 // Check if it's a valid boot option for network boot device.\r
3965 // Check if there is EfiLoadFileProtocol installed. \r
3966 // If yes, that means there is a boot option for network.\r
5c08e117 3967 //\r
3968 Status = gBS->LocateDevicePath (\r
a7a523e0 3969 &gEfiLoadFileProtocolGuid,\r
5c08e117 3970 &TempDevicePath,\r
3971 &Handle\r
3972 );\r
3973 if (EFI_ERROR (Status)) {\r
3974 //\r
3975 // Device not present so see if we need to connect it\r
3976 //\r
3977 TempDevicePath = DevPath;\r
3978 BdsLibConnectDevicePath (TempDevicePath);\r
3979 Status = gBS->LocateDevicePath (\r
a7a523e0 3980 &gEfiLoadFileProtocolGuid,\r
5c08e117 3981 &TempDevicePath,\r
3982 &Handle\r
3983 );\r
3984 }\r
128efbbc 3985\r
5c08e117 3986 if (!EFI_ERROR (Status)) {\r
a7a523e0 3987 if (!IsDevicePathEnd (TempDevicePath)) {\r
3988 //\r
3989 // LoadFile protocol is not installed on handle with exactly the same DevPath\r
3990 //\r
3991 return FALSE;\r
3992 }\r
ff482c56 3993\r
a7a523e0 3994 if (CheckMedia) {\r
3995 //\r
3996 // Test if it is ready to boot now\r
3997 //\r
3998 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r
5c08e117 3999 return TRUE;\r
4000 }\r
a7a523e0 4001 } else {\r
4002 return TRUE;\r
4003 } \r
5c08e117 4004 }\r
4005\r
4006 //\r
4007 // If the boot option point to a file, it is a valid EFI boot option,\r
4008 // and assume it is ready to boot now\r
4009 //\r
4010 while (!IsDevicePathEnd (TempDevicePath)) {\r
7389fdd0 4011 //\r
4012 // If there is USB Class or USB WWID device path node, treat it as valid EFI\r
4013 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it\r
4014 // to full device path.\r
4015 //\r
4016 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r
4017 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||\r
4018 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {\r
4019 return TRUE;\r
4020 }\r
4021\r
4022 LastDeviceNode = TempDevicePath;\r
4023 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
5c08e117 4024 }\r
4025 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
4026 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
4027 return TRUE;\r
4028 }\r
4029\r
4030 //\r
6617d838 4031 // Check if it's a valid boot option for internal FV application\r
5c08e117 4032 //\r
4033 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r
4034 //\r
6617d838 4035 // If the boot option point to internal FV application, make sure it is valid\r
5c08e117 4036 //\r
128efbbc 4037 TempDevicePath = DevPath;\r
6617d838
RN
4038 Status = BdsLibUpdateFvFileDevicePath (\r
4039 &TempDevicePath,\r
4040 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode)\r
4041 );\r
5c08e117 4042 if (Status == EFI_ALREADY_STARTED) {\r
4043 return TRUE;\r
4044 } else {\r
4045 if (Status == EFI_SUCCESS) {\r
128efbbc 4046 FreePool (TempDevicePath);\r
5c08e117 4047 }\r
4048 return FALSE;\r
4049 }\r
4050 }\r
128efbbc 4051\r
5c08e117 4052 //\r
3384a9bc 4053 // If the boot option point to a blockIO device:\r
8d3b5aff 4054 // if it is a removable blockIo device, it is valid.\r
128efbbc 4055 // if it is a fixed blockIo device, check its description confliction.\r
5c08e117 4056 //\r
4057 TempDevicePath = DevPath;\r
4058 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
4059 if (EFI_ERROR (Status)) {\r
4060 //\r
4061 // Device not present so see if we need to connect it\r
4062 //\r
4063 Status = BdsLibConnectDevicePath (DevPath);\r
4064 if (!EFI_ERROR (Status)) {\r
4065 //\r
4066 // Try again to get the Block Io protocol after we did the connect\r
4067 //\r
4068 TempDevicePath = DevPath;\r
4069 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
4070 }\r
4071 }\r
128efbbc 4072\r
5c08e117 4073 if (!EFI_ERROR (Status)) {\r
4074 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
4075 if (!EFI_ERROR (Status)) {\r
4076 if (CheckMedia) {\r
4077 //\r
4078 // Test if it is ready to boot now\r
4079 //\r
4080 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
4081 return TRUE;\r
4082 }\r
4083 } else {\r
4084 return TRUE;\r
4085 }\r
4086 }\r
4087 } else {\r
4088 //\r
4089 // 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
4090 //\r
4091 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r
4092 if (!EFI_ERROR (Status)) {\r
4093 if (CheckMedia) {\r
4094 //\r
4095 // Test if it is ready to boot now\r
4096 //\r
4097 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
4098 return TRUE;\r
4099 }\r
4100 } else {\r
4101 return TRUE;\r
4102 }\r
4103 }\r
4104 }\r
4105\r
4106 return FALSE;\r
4107}\r
4108\r
4109\r
4110/**\r
4111 According to a file guild, check a Fv file device path is valid. If it is invalid,\r
4112 try to return the valid device path.\r
4113 FV address maybe changes for memory layout adjust from time to time, use this function\r
4114 could promise the Fv file device path is right.\r
4115\r
4116 @param DevicePath on input, the Fv file device path need to check on\r
4117 output, the updated valid Fv file device path\r
4118 @param FileGuid the Fv file guild\r
4119\r
4120 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r
4121 parameter\r
4122 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r
4123 guild at all\r
4124 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r
4125 valid\r
4126 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r
4127 and return the updated device path in DevicePath\r
4128\r
4129**/\r
4130EFI_STATUS\r
4131EFIAPI\r
4132BdsLibUpdateFvFileDevicePath (\r
4133 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r
4134 IN EFI_GUID *FileGuid\r
4135 )\r
4136{\r
4137 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
4138 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
4139 EFI_STATUS Status;\r
4140 EFI_GUID *GuidPoint;\r
4141 UINTN Index;\r
4142 UINTN FvHandleCount;\r
4143 EFI_HANDLE *FvHandleBuffer;\r
4144 EFI_FV_FILETYPE Type;\r
4145 UINTN Size;\r
4146 EFI_FV_FILE_ATTRIBUTES Attributes;\r
4147 UINT32 AuthenticationStatus;\r
4148 BOOLEAN FindFvFile;\r
4149 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
4150 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
4151 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
4152 EFI_HANDLE FoundFvHandle;\r
4153 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
4154\r
4155 if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
4156 return EFI_INVALID_PARAMETER;\r
4157 }\r
4158 if (FileGuid == NULL) {\r
4159 return EFI_INVALID_PARAMETER;\r
4160 }\r
128efbbc 4161\r
5c08e117 4162 //\r
4163 // Check whether the device path point to the default the input Fv file\r
4164 //\r
4165 TempDevicePath = *DevicePath;\r
4166 LastDeviceNode = TempDevicePath;\r
4167 while (!IsDevicePathEnd (TempDevicePath)) {\r
4168 LastDeviceNode = TempDevicePath;\r
4169 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
4170 }\r
4171 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
4172 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
4173 );\r
4174 if (GuidPoint == NULL) {\r
4175 //\r
4176 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
4177 //\r
4178 return EFI_UNSUPPORTED;\r
4179 }\r
4180 if (!CompareGuid (GuidPoint, FileGuid)) {\r
4181 //\r
4182 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
4183 //\r
4184 return EFI_UNSUPPORTED;\r
4185 }\r
4186\r
4187 //\r
4188 // Check whether the input Fv file device path is valid\r
4189 //\r
4190 TempDevicePath = *DevicePath;\r
4191 FoundFvHandle = NULL;\r
4192 Status = gBS->LocateDevicePath (\r
4193 &gEfiFirmwareVolume2ProtocolGuid,\r
4194 &TempDevicePath,\r
4195 &FoundFvHandle\r
4196 );\r
4197 if (!EFI_ERROR (Status)) {\r
4198 Status = gBS->HandleProtocol (\r
4199 FoundFvHandle,\r
4200 &gEfiFirmwareVolume2ProtocolGuid,\r
4201 (VOID **) &Fv\r
4202 );\r
4203 if (!EFI_ERROR (Status)) {\r
4204 //\r
4205 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
4206 //\r
4207 Status = Fv->ReadFile (\r
4208 Fv,\r
4209 FileGuid,\r
4210 NULL,\r
4211 &Size,\r
4212 &Type,\r
4213 &Attributes,\r
4214 &AuthenticationStatus\r
4215 );\r
4216 if (!EFI_ERROR (Status)) {\r
4217 return EFI_ALREADY_STARTED;\r
4218 }\r
4219 }\r
4220 }\r
4221\r
4222 //\r
4223 // Look for the input wanted FV file in current FV\r
4224 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r
4225 //\r
4226 FindFvFile = FALSE;\r
4227 FoundFvHandle = NULL;\r
4228 Status = gBS->HandleProtocol (\r
fefefa4c 4229 gImageHandle,\r
5c08e117 4230 &gEfiLoadedImageProtocolGuid,\r
4231 (VOID **) &LoadedImage\r
4232 );\r
4233 if (!EFI_ERROR (Status)) {\r
4234 Status = gBS->HandleProtocol (\r
4235 LoadedImage->DeviceHandle,\r
4236 &gEfiFirmwareVolume2ProtocolGuid,\r
4237 (VOID **) &Fv\r
4238 );\r
4239 if (!EFI_ERROR (Status)) {\r
4240 Status = Fv->ReadFile (\r
4241 Fv,\r
4242 FileGuid,\r
4243 NULL,\r
4244 &Size,\r
4245 &Type,\r
4246 &Attributes,\r
4247 &AuthenticationStatus\r
4248 );\r
4249 if (!EFI_ERROR (Status)) {\r
4250 FindFvFile = TRUE;\r
4251 FoundFvHandle = LoadedImage->DeviceHandle;\r
4252 }\r
4253 }\r
4254 }\r
4255 //\r
4256 // Second, if fail to find, try to enumerate all FV\r
4257 //\r
4258 if (!FindFvFile) {\r
4259 FvHandleBuffer = NULL;\r
4260 gBS->LocateHandleBuffer (\r
4261 ByProtocol,\r
4262 &gEfiFirmwareVolume2ProtocolGuid,\r
4263 NULL,\r
4264 &FvHandleCount,\r
4265 &FvHandleBuffer\r
4266 );\r
4267 for (Index = 0; Index < FvHandleCount; Index++) {\r
4268 gBS->HandleProtocol (\r
4269 FvHandleBuffer[Index],\r
4270 &gEfiFirmwareVolume2ProtocolGuid,\r
4271 (VOID **) &Fv\r
4272 );\r
4273\r
4274 Status = Fv->ReadFile (\r
4275 Fv,\r
4276 FileGuid,\r
4277 NULL,\r
4278 &Size,\r
4279 &Type,\r
4280 &Attributes,\r
4281 &AuthenticationStatus\r
4282 );\r
4283 if (EFI_ERROR (Status)) {\r
4284 //\r
4285 // Skip if input Fv file not in the FV\r
4286 //\r
4287 continue;\r
4288 }\r
4289 FindFvFile = TRUE;\r
4290 FoundFvHandle = FvHandleBuffer[Index];\r
4291 break;\r
4292 }\r
4293\r
4294 if (FvHandleBuffer != NULL) {\r
128efbbc 4295 FreePool (FvHandleBuffer);\r
5c08e117 4296 }\r
4297 }\r
4298\r
4299 if (FindFvFile) {\r
4300 //\r
4301 // Build the shell device path\r
4302 //\r
4303 NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r
4304 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r
4305 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r
4306 *DevicePath = NewDevicePath;\r
4307 return EFI_SUCCESS;\r
4308 }\r
4309 return EFI_NOT_FOUND;\r
4310}\r