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