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