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