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