]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/BdsDxe/BootMaint/BootOption.c
Fix bug that some boot option can *not* be displayed correct in boot manager and...
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / BootMaint / BootOption.c
1 /** @file
2 Provide boot option support for Application "BootMaint"
3
4 Include file system navigation, system handle selection
5
6 Boot option manipulation
7
8 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19 #include "BootMaint.h"
20 #include "BBSsupport.h"
21
22 /**
23 Create a menu entry by given menu type.
24
25 @param MenuType The Menu type to be created.
26
27 @retval NULL If failed to create the menu.
28 @return the new menu entry.
29
30 **/
31 BM_MENU_ENTRY *
32 BOpt_CreateMenuEntry (
33 UINTN MenuType
34 )
35 {
36 BM_MENU_ENTRY *MenuEntry;
37 UINTN ContextSize;
38
39 //
40 // Get context size according to menu type
41 //
42 switch (MenuType) {
43 case BM_LOAD_CONTEXT_SELECT:
44 ContextSize = sizeof (BM_LOAD_CONTEXT);
45 break;
46
47 case BM_FILE_CONTEXT_SELECT:
48 ContextSize = sizeof (BM_FILE_CONTEXT);
49 break;
50
51 case BM_CONSOLE_CONTEXT_SELECT:
52 ContextSize = sizeof (BM_CONSOLE_CONTEXT);
53 break;
54
55 case BM_TERMINAL_CONTEXT_SELECT:
56 ContextSize = sizeof (BM_TERMINAL_CONTEXT);
57 break;
58
59 case BM_HANDLE_CONTEXT_SELECT:
60 ContextSize = sizeof (BM_HANDLE_CONTEXT);
61 break;
62
63 case BM_LEGACY_DEV_CONTEXT_SELECT:
64 ContextSize = sizeof (BM_LEGACY_DEVICE_CONTEXT);
65 break;
66
67 default:
68 ContextSize = 0;
69 break;
70 }
71
72 if (ContextSize == 0) {
73 return NULL;
74 }
75
76 //
77 // Create new menu entry
78 //
79 MenuEntry = AllocateZeroPool (sizeof (BM_MENU_ENTRY));
80 if (MenuEntry == NULL) {
81 return NULL;
82 }
83
84 MenuEntry->VariableContext = AllocateZeroPool (ContextSize);
85 if (MenuEntry->VariableContext == NULL) {
86 FreePool (MenuEntry);
87 return NULL;
88 }
89
90 MenuEntry->Signature = BM_MENU_ENTRY_SIGNATURE;
91 MenuEntry->ContextSelection = MenuType;
92 return MenuEntry;
93 }
94
95 /**
96 Free up all resource allocated for a BM_MENU_ENTRY.
97
98 @param MenuEntry A pointer to BM_MENU_ENTRY.
99
100 **/
101 VOID
102 BOpt_DestroyMenuEntry (
103 BM_MENU_ENTRY *MenuEntry
104 )
105 {
106 BM_LOAD_CONTEXT *LoadContext;
107 BM_FILE_CONTEXT *FileContext;
108 BM_CONSOLE_CONTEXT *ConsoleContext;
109 BM_TERMINAL_CONTEXT *TerminalContext;
110 BM_HANDLE_CONTEXT *HandleContext;
111 BM_LEGACY_DEVICE_CONTEXT *LegacyDevContext;
112
113 //
114 // Select by the type in Menu entry for current context type
115 //
116 switch (MenuEntry->ContextSelection) {
117 case BM_LOAD_CONTEXT_SELECT:
118 LoadContext = (BM_LOAD_CONTEXT *) MenuEntry->VariableContext;
119 FreePool (LoadContext->FilePathList);
120 FreePool (LoadContext->LoadOption);
121 if (LoadContext->OptionalData != NULL) {
122 FreePool (LoadContext->OptionalData);
123 }
124 FreePool (LoadContext);
125 break;
126
127 case BM_FILE_CONTEXT_SELECT:
128 FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;
129
130 if (!FileContext->IsRoot) {
131 FreePool (FileContext->DevicePath);
132 } else {
133 if (FileContext->FHandle != NULL) {
134 FileContext->FHandle->Close (FileContext->FHandle);
135 }
136 }
137
138 if (FileContext->FileName != NULL) {
139 FreePool (FileContext->FileName);
140 }
141 if (FileContext->Info != NULL) {
142 FreePool (FileContext->Info);
143 }
144 FreePool (FileContext);
145 break;
146
147 case BM_CONSOLE_CONTEXT_SELECT:
148 ConsoleContext = (BM_CONSOLE_CONTEXT *) MenuEntry->VariableContext;
149 FreePool (ConsoleContext->DevicePath);
150 FreePool (ConsoleContext);
151 break;
152
153 case BM_TERMINAL_CONTEXT_SELECT:
154 TerminalContext = (BM_TERMINAL_CONTEXT *) MenuEntry->VariableContext;
155 FreePool (TerminalContext->DevicePath);
156 FreePool (TerminalContext);
157 break;
158
159 case BM_HANDLE_CONTEXT_SELECT:
160 HandleContext = (BM_HANDLE_CONTEXT *) MenuEntry->VariableContext;
161 FreePool (HandleContext);
162 break;
163
164 case BM_LEGACY_DEV_CONTEXT_SELECT:
165 LegacyDevContext = (BM_LEGACY_DEVICE_CONTEXT *) MenuEntry->VariableContext;
166 FreePool (LegacyDevContext);
167
168 default:
169 break;
170 }
171
172 FreePool (MenuEntry->DisplayString);
173 if (MenuEntry->HelpString != NULL) {
174 FreePool (MenuEntry->HelpString);
175 }
176
177 FreePool (MenuEntry);
178 }
179
180 /**
181 Get the Menu Entry from the list in Menu Entry List.
182
183 If MenuNumber is great or equal to the number of Menu
184 Entry in the list, then ASSERT.
185
186 @param MenuOption The Menu Entry List to read the menu entry.
187 @param MenuNumber The index of Menu Entry.
188
189 @return The Menu Entry.
190
191 **/
192 BM_MENU_ENTRY *
193 BOpt_GetMenuEntry (
194 BM_MENU_OPTION *MenuOption,
195 UINTN MenuNumber
196 )
197 {
198 BM_MENU_ENTRY *NewMenuEntry;
199 UINTN Index;
200 LIST_ENTRY *List;
201
202 ASSERT (MenuNumber < MenuOption->MenuNumber);
203
204 List = MenuOption->Head.ForwardLink;
205 for (Index = 0; Index < MenuNumber; Index++) {
206 List = List->ForwardLink;
207 }
208
209 NewMenuEntry = CR (List, BM_MENU_ENTRY, Link, BM_MENU_ENTRY_SIGNATURE);
210
211 return NewMenuEntry;
212 }
213
214 /**
215 This function build the FsOptionMenu list which records all
216 available file system in the system. They includes all instances
217 of EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, all instances of EFI_LOAD_FILE_SYSTEM
218 and all type of legacy boot device.
219
220 @param CallbackData BMM context data
221
222 @retval EFI_SUCCESS Success find the file system
223 @retval EFI_OUT_OF_RESOURCES Can not create menu entry
224
225 **/
226 EFI_STATUS
227 BOpt_FindFileSystem (
228 IN BMM_CALLBACK_DATA *CallbackData
229 )
230 {
231 UINTN NoBlkIoHandles;
232 UINTN NoSimpleFsHandles;
233 UINTN NoLoadFileHandles;
234 EFI_HANDLE *BlkIoHandle;
235 EFI_HANDLE *SimpleFsHandle;
236 EFI_HANDLE *LoadFileHandle;
237 UINT16 *VolumeLabel;
238 EFI_BLOCK_IO_PROTOCOL *BlkIo;
239 UINTN Index;
240 EFI_STATUS Status;
241 BM_MENU_ENTRY *MenuEntry;
242 BM_FILE_CONTEXT *FileContext;
243 UINT16 *TempStr;
244 UINTN OptionNumber;
245 VOID *Buffer;
246 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
247 UINT16 DeviceType;
248 BBS_BBS_DEVICE_PATH BbsDevicePathNode;
249 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
250 BOOLEAN RemovableMedia;
251
252
253 NoSimpleFsHandles = 0;
254 NoLoadFileHandles = 0;
255 OptionNumber = 0;
256 InitializeListHead (&FsOptionMenu.Head);
257
258 //
259 // Locate Handles that support BlockIo protocol
260 //
261 Status = gBS->LocateHandleBuffer (
262 ByProtocol,
263 &gEfiBlockIoProtocolGuid,
264 NULL,
265 &NoBlkIoHandles,
266 &BlkIoHandle
267 );
268 if (!EFI_ERROR (Status)) {
269
270 for (Index = 0; Index < NoBlkIoHandles; Index++) {
271 Status = gBS->HandleProtocol (
272 BlkIoHandle[Index],
273 &gEfiBlockIoProtocolGuid,
274 (VOID **) &BlkIo
275 );
276
277 if (EFI_ERROR (Status)) {
278 continue;
279 }
280
281 //
282 // Issue a dummy read to trigger reinstall of BlockIo protocol for removable media
283 //
284 if (BlkIo->Media->RemovableMedia) {
285 Buffer = AllocateZeroPool (BlkIo->Media->BlockSize);
286 if (NULL == Buffer) {
287 FreePool (BlkIoHandle);
288 return EFI_OUT_OF_RESOURCES;
289 }
290
291 BlkIo->ReadBlocks (
292 BlkIo,
293 BlkIo->Media->MediaId,
294 0,
295 BlkIo->Media->BlockSize,
296 Buffer
297 );
298 FreePool (Buffer);
299 }
300 }
301 FreePool (BlkIoHandle);
302 }
303
304 //
305 // Locate Handles that support Simple File System protocol
306 //
307 Status = gBS->LocateHandleBuffer (
308 ByProtocol,
309 &gEfiSimpleFileSystemProtocolGuid,
310 NULL,
311 &NoSimpleFsHandles,
312 &SimpleFsHandle
313 );
314 if (!EFI_ERROR (Status)) {
315 //
316 // Find all the instances of the File System prototocol
317 //
318 for (Index = 0; Index < NoSimpleFsHandles; Index++) {
319 Status = gBS->HandleProtocol (
320 SimpleFsHandle[Index],
321 &gEfiBlockIoProtocolGuid,
322 (VOID **) &BlkIo
323 );
324 if (EFI_ERROR (Status)) {
325 //
326 // If no block IO exists assume it's NOT a removable media
327 //
328 RemovableMedia = FALSE;
329 } else {
330 //
331 // If block IO exists check to see if it's remobable media
332 //
333 RemovableMedia = BlkIo->Media->RemovableMedia;
334 }
335
336 //
337 // Allocate pool for this load option
338 //
339 MenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);
340 if (NULL == MenuEntry) {
341 FreePool (SimpleFsHandle);
342 return EFI_OUT_OF_RESOURCES;
343 }
344
345 FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;
346
347 FileContext->Handle = SimpleFsHandle[Index];
348 MenuEntry->OptionNumber = Index;
349 FileContext->FHandle = EfiLibOpenRoot (FileContext->Handle);
350 if (FileContext->FHandle == NULL) {
351 BOpt_DestroyMenuEntry (MenuEntry);
352 continue;
353 }
354
355 MenuEntry->HelpString = DevicePathToStr (DevicePathFromHandle (FileContext->Handle));
356 FileContext->Info = EfiLibFileSystemVolumeLabelInfo (FileContext->FHandle);
357 FileContext->FileName = EfiStrDuplicate (L"\\");
358 FileContext->DevicePath = FileDevicePath (
359 FileContext->Handle,
360 FileContext->FileName
361 );
362 FileContext->IsDir = TRUE;
363 FileContext->IsRoot = TRUE;
364 FileContext->IsRemovableMedia = RemovableMedia;
365 FileContext->IsLoadFile = FALSE;
366
367 //
368 // Get current file system's Volume Label
369 //
370 if (FileContext->Info == NULL) {
371 VolumeLabel = L"NO FILE SYSTEM INFO";
372 } else {
373 if (FileContext->Info->VolumeLabel == NULL) {
374 VolumeLabel = L"NULL VOLUME LABEL";
375 } else {
376 VolumeLabel = FileContext->Info->VolumeLabel;
377 if (*VolumeLabel == 0x0000) {
378 VolumeLabel = L"NO VOLUME LABEL";
379 }
380 }
381 }
382
383 TempStr = MenuEntry->HelpString;
384 MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);
385 ASSERT (MenuEntry->DisplayString != NULL);
386 UnicodeSPrint (
387 MenuEntry->DisplayString,
388 MAX_CHAR,
389 L"%s, [%s]",
390 VolumeLabel,
391 TempStr
392 );
393 OptionNumber++;
394 InsertTailList (&FsOptionMenu.Head, &MenuEntry->Link);
395 }
396 }
397
398 if (NoSimpleFsHandles != 0) {
399 FreePool (SimpleFsHandle);
400 }
401 //
402 // Searching for handles that support Load File protocol
403 //
404 Status = gBS->LocateHandleBuffer (
405 ByProtocol,
406 &gEfiLoadFileProtocolGuid,
407 NULL,
408 &NoLoadFileHandles,
409 &LoadFileHandle
410 );
411
412 if (!EFI_ERROR (Status)) {
413 for (Index = 0; Index < NoLoadFileHandles; Index++) {
414 MenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);
415 if (NULL == MenuEntry) {
416 FreePool (LoadFileHandle);
417 return EFI_OUT_OF_RESOURCES;
418 }
419
420 FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;
421 FileContext->IsRemovableMedia = FALSE;
422 FileContext->IsLoadFile = TRUE;
423 FileContext->Handle = LoadFileHandle[Index];
424 FileContext->IsRoot = TRUE;
425
426 FileContext->DevicePath = DevicePathFromHandle (FileContext->Handle);
427
428 MenuEntry->HelpString = DevicePathToStr (FileContext->DevicePath);
429
430 TempStr = MenuEntry->HelpString;
431 MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);
432 ASSERT (MenuEntry->DisplayString != NULL);
433 UnicodeSPrint (
434 MenuEntry->DisplayString,
435 MAX_CHAR,
436 L"Load File [%s]",
437 TempStr
438 );
439
440 MenuEntry->OptionNumber = OptionNumber;
441 OptionNumber++;
442 InsertTailList (&FsOptionMenu.Head, &MenuEntry->Link);
443 }
444 }
445
446 if (NoLoadFileHandles != 0) {
447 FreePool (LoadFileHandle);
448 }
449
450 //
451 // Add Legacy Boot Option Support Here
452 //
453 Status = gBS->LocateProtocol (
454 &gEfiLegacyBiosProtocolGuid,
455 NULL,
456 (VOID **) &LegacyBios
457 );
458 if (!EFI_ERROR (Status)) {
459
460 for (Index = BBS_TYPE_FLOPPY; Index <= BBS_TYPE_EMBEDDED_NETWORK; Index++) {
461 MenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);
462 if (NULL == MenuEntry) {
463 return EFI_OUT_OF_RESOURCES;
464 }
465
466 FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;
467
468 FileContext->IsRemovableMedia = FALSE;
469 FileContext->IsLoadFile = TRUE;
470 FileContext->IsBootLegacy = TRUE;
471 DeviceType = (UINT16) Index;
472 BbsDevicePathNode.Header.Type = BBS_DEVICE_PATH;
473 BbsDevicePathNode.Header.SubType = BBS_BBS_DP;
474 SetDevicePathNodeLength (
475 &BbsDevicePathNode.Header,
476 sizeof (BBS_BBS_DEVICE_PATH)
477 );
478 BbsDevicePathNode.DeviceType = DeviceType;
479 BbsDevicePathNode.StatusFlag = 0;
480 BbsDevicePathNode.String[0] = 0;
481 DevicePath = AppendDevicePathNode (
482 EndDevicePath,
483 (EFI_DEVICE_PATH_PROTOCOL *) &BbsDevicePathNode
484 );
485
486 FileContext->DevicePath = DevicePath;
487 MenuEntry->HelpString = DevicePathToStr (FileContext->DevicePath);
488
489 TempStr = MenuEntry->HelpString;
490 MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);
491 ASSERT (MenuEntry->DisplayString != NULL);
492 UnicodeSPrint (
493 MenuEntry->DisplayString,
494 MAX_CHAR,
495 L"Boot Legacy [%s]",
496 TempStr
497 );
498 MenuEntry->OptionNumber = OptionNumber;
499 OptionNumber++;
500 InsertTailList (&FsOptionMenu.Head, &MenuEntry->Link);
501 }
502 }
503 //
504 // Remember how many file system options are here
505 //
506 FsOptionMenu.MenuNumber = OptionNumber;
507 return EFI_SUCCESS;
508 }
509
510 /**
511 Free resources allocated in Allocate Rountine.
512
513 @param FreeMenu Menu to be freed
514 **/
515 VOID
516 BOpt_FreeMenu (
517 BM_MENU_OPTION *FreeMenu
518 )
519 {
520 BM_MENU_ENTRY *MenuEntry;
521 while (!IsListEmpty (&FreeMenu->Head)) {
522 MenuEntry = CR (
523 FreeMenu->Head.ForwardLink,
524 BM_MENU_ENTRY,
525 Link,
526 BM_MENU_ENTRY_SIGNATURE
527 );
528 RemoveEntryList (&MenuEntry->Link);
529 BOpt_DestroyMenuEntry (MenuEntry);
530 }
531 }
532
533 /**
534 Find files under current directory
535 All files and sub-directories in current directory
536 will be stored in DirectoryMenu for future use.
537
538 @param CallbackData The BMM context data.
539 @param MenuEntry The Menu Entry.
540
541 @retval EFI_SUCCESS Get files from current dir successfully.
542 @return Other value if can't get files from current dir.
543
544 **/
545 EFI_STATUS
546 BOpt_FindFiles (
547 IN BMM_CALLBACK_DATA *CallbackData,
548 IN BM_MENU_ENTRY *MenuEntry
549 )
550 {
551 EFI_FILE_HANDLE NewDir;
552 EFI_FILE_HANDLE Dir;
553 EFI_FILE_INFO *DirInfo;
554 UINTN BufferSize;
555 UINTN DirBufferSize;
556 BM_MENU_ENTRY *NewMenuEntry;
557 BM_FILE_CONTEXT *FileContext;
558 BM_FILE_CONTEXT *NewFileContext;
559 UINTN Pass;
560 EFI_STATUS Status;
561 UINTN OptionNumber;
562
563 FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;
564 Dir = FileContext->FHandle;
565 OptionNumber = 0;
566 //
567 // Open current directory to get files from it
568 //
569 Status = Dir->Open (
570 Dir,
571 &NewDir,
572 FileContext->FileName,
573 EFI_FILE_READ_ONLY,
574 0
575 );
576 if (!FileContext->IsRoot) {
577 Dir->Close (Dir);
578 }
579
580 if (EFI_ERROR (Status)) {
581 return Status;
582 }
583
584 DirInfo = EfiLibFileInfo (NewDir);
585 if (DirInfo == NULL) {
586 return EFI_NOT_FOUND;
587 }
588
589 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
590 return EFI_INVALID_PARAMETER;
591 }
592
593 FileContext->DevicePath = FileDevicePath (
594 FileContext->Handle,
595 FileContext->FileName
596 );
597
598 DirBufferSize = sizeof (EFI_FILE_INFO) + 1024;
599 DirInfo = AllocateZeroPool (DirBufferSize);
600 if (DirInfo == NULL) {
601 return EFI_OUT_OF_RESOURCES;
602 }
603 //
604 // Get all files in current directory
605 // Pass 1 to get Directories
606 // Pass 2 to get files that are EFI images
607 //
608 for (Pass = 1; Pass <= 2; Pass++) {
609 NewDir->SetPosition (NewDir, 0);
610 for (;;) {
611 BufferSize = DirBufferSize;
612 Status = NewDir->Read (NewDir, &BufferSize, DirInfo);
613 if (EFI_ERROR (Status) || BufferSize == 0) {
614 break;
615 }
616
617 if (((DirInfo->Attribute & EFI_FILE_DIRECTORY) != 0 && Pass == 2) ||
618 ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0 && Pass == 1)
619 ) {
620 //
621 // Pass 1 is for Directories
622 // Pass 2 is for file names
623 //
624 continue;
625 }
626
627 if (!(BOpt_IsEfiImageName (DirInfo->FileName) || (DirInfo->Attribute & EFI_FILE_DIRECTORY) != 0)) {
628 //
629 // Slip file unless it is a directory entry or a .EFI file
630 //
631 continue;
632 }
633
634 NewMenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);
635 if (NULL == NewMenuEntry) {
636 return EFI_OUT_OF_RESOURCES;
637 }
638
639 NewFileContext = (BM_FILE_CONTEXT *) NewMenuEntry->VariableContext;
640 NewFileContext->Handle = FileContext->Handle;
641 NewFileContext->FileName = BOpt_AppendFileName (
642 FileContext->FileName,
643 DirInfo->FileName
644 );
645 NewFileContext->FHandle = NewDir;
646 NewFileContext->DevicePath = FileDevicePath (
647 NewFileContext->Handle,
648 NewFileContext->FileName
649 );
650 NewMenuEntry->HelpString = NULL;
651
652 MenuEntry->DisplayStringToken = GetStringTokenFromDepository (
653 CallbackData,
654 FileOptionStrDepository
655 );
656
657 NewFileContext->IsDir = (BOOLEAN) ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY);
658
659 if (NewFileContext->IsDir) {
660 BufferSize = StrLen (DirInfo->FileName) * 2 + 6;
661 NewMenuEntry->DisplayString = AllocateZeroPool (BufferSize);
662
663 UnicodeSPrint (
664 NewMenuEntry->DisplayString,
665 BufferSize,
666 L"<%s>",
667 DirInfo->FileName
668 );
669
670 } else {
671 NewMenuEntry->DisplayString = EfiStrDuplicate (DirInfo->FileName);
672 }
673
674 NewFileContext->IsRoot = FALSE;
675 NewFileContext->IsLoadFile = FALSE;
676 NewFileContext->IsRemovableMedia = FALSE;
677
678 NewMenuEntry->OptionNumber = OptionNumber;
679 OptionNumber++;
680 InsertTailList (&DirectoryMenu.Head, &NewMenuEntry->Link);
681 }
682 }
683
684 DirectoryMenu.MenuNumber = OptionNumber;
685 FreePool (DirInfo);
686 return EFI_SUCCESS;
687 }
688
689 /**
690 Build the LegacyFDMenu LegacyHDMenu LegacyCDMenu according to LegacyBios.GetBbsInfo().
691
692 @retval EFI_SUCCESS The function complete successfully.
693 @retval EFI_OUT_OF_RESOURCES No enough memory to complete this function.
694
695 **/
696 EFI_STATUS
697 BOpt_GetLegacyOptions (
698 VOID
699 )
700 {
701 BM_MENU_ENTRY *NewMenuEntry;
702 BM_LEGACY_DEVICE_CONTEXT *NewLegacyDevContext;
703 EFI_STATUS Status;
704 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
705 UINT16 HddCount;
706 HDD_INFO *HddInfo;
707 UINT16 BbsCount;
708 BBS_TABLE *BbsTable;
709 UINTN Index;
710 CHAR16 DescString[100];
711 UINTN FDNum;
712 UINTN HDNum;
713 UINTN CDNum;
714 UINTN NETNum;
715 UINTN BEVNum;
716
717 NewMenuEntry = NULL;
718 HddInfo = NULL;
719 BbsTable = NULL;
720 BbsCount = 0;
721
722 //
723 // Initialize Bbs Table Context from BBS info data
724 //
725 InitializeListHead (&LegacyFDMenu.Head);
726 InitializeListHead (&LegacyHDMenu.Head);
727 InitializeListHead (&LegacyCDMenu.Head);
728 InitializeListHead (&LegacyNETMenu.Head);
729 InitializeListHead (&LegacyBEVMenu.Head);
730
731 Status = gBS->LocateProtocol (
732 &gEfiLegacyBiosProtocolGuid,
733 NULL,
734 (VOID **) &LegacyBios
735 );
736 if (!EFI_ERROR (Status)) {
737 Status = LegacyBios->GetBbsInfo (
738 LegacyBios,
739 &HddCount,
740 &HddInfo,
741 &BbsCount,
742 &BbsTable
743 );
744 if (EFI_ERROR (Status)) {
745 return Status;
746 }
747 }
748
749 FDNum = 0;
750 HDNum = 0;
751 CDNum = 0;
752 NETNum = 0;
753 BEVNum = 0;
754
755 for (Index = 0; Index < BbsCount; Index++) {
756 if ((BBS_IGNORE_ENTRY == BbsTable[Index].BootPriority) ||
757 (BBS_DO_NOT_BOOT_FROM == BbsTable[Index].BootPriority)
758 ) {
759 continue;
760 }
761
762 NewMenuEntry = BOpt_CreateMenuEntry (BM_LEGACY_DEV_CONTEXT_SELECT);
763 if (NULL == NewMenuEntry) {
764 break;
765 }
766
767 NewLegacyDevContext = (BM_LEGACY_DEVICE_CONTEXT *) NewMenuEntry->VariableContext;
768 NewLegacyDevContext->BbsTable = &BbsTable[Index];
769 NewLegacyDevContext->Index = Index;
770 NewLegacyDevContext->BbsCount = BbsCount;
771 BdsBuildLegacyDevNameString (
772 &BbsTable[Index],
773 Index,
774 sizeof (DescString),
775 DescString
776 );
777 NewLegacyDevContext->Description = AllocateZeroPool (StrSize (DescString));
778 if (NULL == NewLegacyDevContext->Description) {
779 break;
780 }
781
782 CopyMem (NewLegacyDevContext->Description, DescString, StrSize (DescString));
783 NewMenuEntry->DisplayString = NewLegacyDevContext->Description;
784 NewMenuEntry->HelpString = NULL;
785
786 switch (BbsTable[Index].DeviceType) {
787 case BBS_FLOPPY:
788 InsertTailList (&LegacyFDMenu.Head, &NewMenuEntry->Link);
789 FDNum++;
790 break;
791
792 case BBS_HARDDISK:
793 InsertTailList (&LegacyHDMenu.Head, &NewMenuEntry->Link);
794 HDNum++;
795 break;
796
797 case BBS_CDROM:
798 InsertTailList (&LegacyCDMenu.Head, &NewMenuEntry->Link);
799 CDNum++;
800 break;
801
802 case BBS_EMBED_NETWORK:
803 InsertTailList (&LegacyNETMenu.Head, &NewMenuEntry->Link);
804 NETNum++;
805 break;
806
807 case BBS_BEV_DEVICE:
808 InsertTailList (&LegacyBEVMenu.Head, &NewMenuEntry->Link);
809 BEVNum++;
810 break;
811 }
812 }
813
814 if (Index != BbsCount) {
815 BOpt_FreeLegacyOptions ();
816 return EFI_OUT_OF_RESOURCES;
817 }
818
819 LegacyFDMenu.MenuNumber = FDNum;
820 LegacyHDMenu.MenuNumber = HDNum;
821 LegacyCDMenu.MenuNumber = CDNum;
822 LegacyNETMenu.MenuNumber = NETNum;
823 LegacyBEVMenu.MenuNumber = BEVNum;
824 return EFI_SUCCESS;
825 }
826
827 /**
828 Free out resouce allocated from Legacy Boot Options.
829
830 **/
831 VOID
832 BOpt_FreeLegacyOptions (
833 VOID
834 )
835 {
836 BOpt_FreeMenu (&LegacyFDMenu);
837 BOpt_FreeMenu (&LegacyHDMenu);
838 BOpt_FreeMenu (&LegacyCDMenu);
839 BOpt_FreeMenu (&LegacyNETMenu);
840 BOpt_FreeMenu (&LegacyBEVMenu);
841 }
842
843 /**
844
845 Build the BootOptionMenu according to BootOrder Variable.
846 This Routine will access the Boot#### to get EFI_LOAD_OPTION.
847
848 @param CallbackData The BMM context data.
849
850 @return EFI_NOT_FOUND Fail to find "BootOrder" variable.
851 @return EFI_SUCESS Success build boot option menu.
852
853 **/
854 EFI_STATUS
855 BOpt_GetBootOptions (
856 IN BMM_CALLBACK_DATA *CallbackData
857 )
858 {
859 UINTN Index;
860 UINT16 BootString[10];
861 UINT8 *LoadOptionFromVar;
862 UINT8 *LoadOption;
863 UINTN BootOptionSize;
864 BOOLEAN BootNextFlag;
865 UINT16 *BootOrderList;
866 UINTN BootOrderListSize;
867 UINT16 *BootNext;
868 UINTN BootNextSize;
869 BM_MENU_ENTRY *NewMenuEntry;
870 BM_LOAD_CONTEXT *NewLoadContext;
871 UINT8 *LoadOptionPtr;
872 UINTN StringSize;
873 UINTN OptionalDataSize;
874 UINT8 *LoadOptionEnd;
875 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
876 UINTN MenuCount;
877 UINT8 *Ptr;
878 UINTN DevicePathType;
879 CHAR16 *HiiString;
880
881 MenuCount = 0;
882 BootOrderListSize = 0;
883 BootNextSize = 0;
884 BootOrderList = NULL;
885 BootNext = NULL;
886 LoadOptionFromVar = NULL;
887 BOpt_FreeMenu (&BootOptionMenu);
888 InitializeListHead (&BootOptionMenu.Head);
889
890 //
891 // Get the BootOrder from the Var
892 //
893 BootOrderList = BdsLibGetVariableAndSize (
894 L"BootOrder",
895 &gEfiGlobalVariableGuid,
896 &BootOrderListSize
897 );
898 if (BootOrderList == NULL) {
899 return EFI_NOT_FOUND;
900 }
901
902 //
903 // Get the BootNext from the Var
904 //
905 BootNext = BdsLibGetVariableAndSize (
906 L"BootNext",
907 &gEfiGlobalVariableGuid,
908 &BootNextSize
909 );
910
911 if (BootNext != NULL) {
912 if (BootNextSize != sizeof (UINT16)) {
913 FreePool (BootNext);
914 BootNext = NULL;
915 }
916 }
917
918 for (Index = 0; Index < BootOrderListSize / sizeof (UINT16); Index++) {
919 UnicodeSPrint (BootString, sizeof (BootString), L"Boot%04x", BootOrderList[Index]);
920 //
921 // Get all loadoptions from the VAR
922 //
923 LoadOptionFromVar = BdsLibGetVariableAndSize (
924 BootString,
925 &gEfiGlobalVariableGuid,
926 &BootOptionSize
927 );
928 if (LoadOptionFromVar == NULL) {
929 continue;
930 }
931
932 LoadOption = AllocateZeroPool (BootOptionSize);
933 if (LoadOption == NULL) {
934 continue;
935 }
936
937 CopyMem (LoadOption, LoadOptionFromVar, BootOptionSize);
938 FreePool (LoadOptionFromVar);
939
940 if (BootNext != NULL) {
941 BootNextFlag = (BOOLEAN) (*BootNext == BootOrderList[Index]);
942 } else {
943 BootNextFlag = FALSE;
944 }
945
946 if (0 == (*((UINT32 *) LoadOption) & LOAD_OPTION_ACTIVE)) {
947 FreePool (LoadOption);
948 continue;
949 }
950 //
951 // BUGBUG: could not return EFI_OUT_OF_RESOURCES here directly.
952 // the buffer allocated already should be freed before returning.
953 //
954 NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT);
955 if (NULL == NewMenuEntry) {
956 return EFI_OUT_OF_RESOURCES;
957 }
958
959 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;
960
961 LoadOptionPtr = LoadOption;
962 LoadOptionEnd = LoadOption + BootOptionSize;
963
964 NewMenuEntry->OptionNumber = BootOrderList[Index];
965 NewLoadContext->LoadOptionModified = FALSE;
966 NewLoadContext->Deleted = FALSE;
967 NewLoadContext->IsBootNext = BootNextFlag;
968
969 //
970 // Is a Legacy Device?
971 //
972 Ptr = (UINT8 *) LoadOption;
973
974 //
975 // Attribute = *(UINT32 *)Ptr;
976 //
977 Ptr += sizeof (UINT32);
978
979 //
980 // FilePathSize = *(UINT16 *)Ptr;
981 //
982 Ptr += sizeof (UINT16);
983
984 //
985 // Description = (CHAR16 *)Ptr;
986 //
987 Ptr += StrSize ((CHAR16 *) Ptr);
988
989 //
990 // Now Ptr point to Device Path
991 //
992 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Ptr;
993 if ((BBS_DEVICE_PATH == DevicePath->Type) && (BBS_BBS_DP == DevicePath->SubType)) {
994 NewLoadContext->IsLegacy = TRUE;
995 } else {
996 NewLoadContext->IsLegacy = FALSE;
997 }
998 //
999 // LoadOption is a pointer type of UINT8
1000 // for easy use with following LOAD_OPTION
1001 // embedded in this struct
1002 //
1003 NewLoadContext->LoadOption = LoadOption;
1004 NewLoadContext->LoadOptionSize = BootOptionSize;
1005
1006 NewLoadContext->Attributes = *(UINT32 *) LoadOptionPtr;
1007 NewLoadContext->IsActive = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_ACTIVE);
1008
1009 NewLoadContext->ForceReconnect = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_FORCE_RECONNECT);
1010
1011 LoadOptionPtr += sizeof (UINT32);
1012
1013 NewLoadContext->FilePathListLength = *(UINT16 *) LoadOptionPtr;
1014 LoadOptionPtr += sizeof (UINT16);
1015
1016 StringSize = StrSize((UINT16*)LoadOptionPtr);
1017 //
1018 // Get Hii description string according to device path type
1019 //
1020 HiiString = NULL;
1021 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);
1022 switch (DevicePathType) {
1023 case BDS_EFI_ACPI_FLOPPY_BOOT:
1024 HiiString = GetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY));
1025 break;
1026 case BDS_EFI_MESSAGE_SATA_BOOT:
1027 case BDS_EFI_MESSAGE_ATAPI_BOOT:
1028 case BDS_EFI_MEDIA_CDROM_BOOT:
1029 HiiString = GetStringById (STRING_TOKEN (STR_DESCRIPTION_DVD));
1030 break;
1031 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:
1032 HiiString = GetStringById (STRING_TOKEN (STR_DESCRIPTION_USB));
1033 break;
1034 case BDS_EFI_MESSAGE_SCSI_BOOT:
1035 HiiString = GetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI));
1036 break;
1037 case BDS_EFI_MESSAGE_MISC_BOOT:
1038 HiiString = GetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC));
1039 break;
1040 case BDS_EFI_MESSAGE_MAC_BOOT:
1041 HiiString = GetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK));
1042 break;
1043 case BBS_DEVICE_PATH:
1044 //
1045 // Do nothing for legacy boot option.
1046 //
1047 break;
1048 default:
1049 DEBUG((EFI_D_INFO, "Can not find HiiString for given device path type 0x%x\n", DevicePathType));
1050 }
1051
1052 if (HiiString != NULL) {
1053 NewLoadContext->Description = AllocateZeroPool(StrSize((UINT16*)LoadOptionPtr) + StrSize(HiiString));
1054 StrCpy (NewLoadContext->Description, HiiString);
1055 if (StrnCmp ((UINT16*)LoadOptionPtr, L"0", 1) != 0) {
1056 StrCat (NewLoadContext->Description, L" ");
1057 StrCat (NewLoadContext->Description, (UINT16*)LoadOptionPtr);
1058 }
1059
1060 FreePool (HiiString);
1061 } else {
1062 NewLoadContext->Description = AllocateZeroPool (StrSize((UINT16*)LoadOptionPtr));
1063 StrCpy(NewLoadContext->Description, (UINT16*)LoadOptionPtr);
1064 }
1065
1066 ASSERT (NewLoadContext->Description != NULL);
1067 NewMenuEntry->DisplayString = NewLoadContext->Description;
1068
1069 LoadOptionPtr += StringSize;
1070
1071 NewLoadContext->FilePathList = AllocateZeroPool (NewLoadContext->FilePathListLength);
1072 ASSERT (NewLoadContext->FilePathList != NULL);
1073 CopyMem (
1074 NewLoadContext->FilePathList,
1075 (EFI_DEVICE_PATH_PROTOCOL *) LoadOptionPtr,
1076 NewLoadContext->FilePathListLength
1077 );
1078
1079 NewMenuEntry->HelpString = DevicePathToStr (NewLoadContext->FilePathList);
1080 NewMenuEntry->DisplayStringToken = GetStringTokenFromDepository (
1081 CallbackData,
1082 BootOptionStrDepository
1083 );
1084 NewMenuEntry->HelpStringToken = GetStringTokenFromDepository (
1085 CallbackData,
1086 BootOptionHelpStrDepository
1087 );
1088 LoadOptionPtr += NewLoadContext->FilePathListLength;
1089
1090 if (LoadOptionPtr < LoadOptionEnd) {
1091 OptionalDataSize = BootOptionSize -
1092 sizeof (UINT32) -
1093 sizeof (UINT16) -
1094 StringSize -
1095 NewLoadContext->FilePathListLength;
1096
1097 NewLoadContext->OptionalData = AllocateZeroPool (OptionalDataSize);
1098 ASSERT (NewLoadContext->OptionalData != NULL);
1099 CopyMem (
1100 NewLoadContext->OptionalData,
1101 LoadOptionPtr,
1102 OptionalDataSize
1103 );
1104
1105 NewLoadContext->OptionalDataSize = OptionalDataSize;
1106 }
1107
1108 InsertTailList (&BootOptionMenu.Head, &NewMenuEntry->Link);
1109 MenuCount++;
1110 }
1111
1112 if (BootNext != NULL) {
1113 FreePool (BootNext);
1114 }
1115 if (BootOrderList != NULL) {
1116 FreePool (BootOrderList);
1117 }
1118 BootOptionMenu.MenuNumber = MenuCount;
1119 return EFI_SUCCESS;
1120 }
1121
1122 /**
1123
1124 Append file name to existing file name.
1125
1126 @param Str1 The existing file name
1127 @param Str2 The file name to be appended
1128
1129 @return Allocate a new string to hold the appended result.
1130 Caller is responsible to free the returned string.
1131
1132 **/
1133 CHAR16 *
1134 BOpt_AppendFileName (
1135 IN CHAR16 *Str1,
1136 IN CHAR16 *Str2
1137 )
1138 {
1139 UINTN Size1;
1140 UINTN Size2;
1141 CHAR16 *Str;
1142 CHAR16 *TmpStr;
1143 CHAR16 *Ptr;
1144 CHAR16 *LastSlash;
1145
1146 Size1 = StrSize (Str1);
1147 Size2 = StrSize (Str2);
1148 Str = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16));
1149 ASSERT (Str != NULL);
1150
1151 TmpStr = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16));
1152 ASSERT (TmpStr != NULL);
1153
1154 StrCat (Str, Str1);
1155 if (!((*Str == '\\') && (*(Str + 1) == 0))) {
1156 StrCat (Str, L"\\");
1157 }
1158
1159 StrCat (Str, Str2);
1160
1161 Ptr = Str;
1162 LastSlash = Str;
1163 while (*Ptr != 0) {
1164 if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '.' && *(Ptr + 3) == L'\\') {
1165 //
1166 // Convert "\Name\..\" to "\"
1167 // DO NOT convert the .. if it is at the end of the string. This will
1168 // break the .. behavior in changing directories.
1169 //
1170
1171 //
1172 // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings
1173 // that overlap.
1174 //
1175 StrCpy (TmpStr, Ptr + 3);
1176 StrCpy (LastSlash, TmpStr);
1177 Ptr = LastSlash;
1178 } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {
1179 //
1180 // Convert a "\.\" to a "\"
1181 //
1182
1183 //
1184 // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings
1185 // that overlap.
1186 //
1187 StrCpy (TmpStr, Ptr + 2);
1188 StrCpy (Ptr, TmpStr);
1189 Ptr = LastSlash;
1190 } else if (*Ptr == '\\') {
1191 LastSlash = Ptr;
1192 }
1193
1194 Ptr++;
1195 }
1196
1197 FreePool (TmpStr);
1198
1199 return Str;
1200 }
1201
1202 /**
1203
1204 Check whether current FileName point to a valid
1205 Efi Image File.
1206
1207 @param FileName File need to be checked.
1208
1209 @retval TRUE Is Efi Image
1210 @retval FALSE Not a valid Efi Image
1211
1212 **/
1213 BOOLEAN
1214 BOpt_IsEfiImageName (
1215 IN UINT16 *FileName
1216 )
1217 {
1218 //
1219 // Search for ".efi" extension
1220 //
1221 while (*FileName != L'\0') {
1222 if (FileName[0] == '.') {
1223 if (FileName[1] == 'e' || FileName[1] == 'E') {
1224 if (FileName[2] == 'f' || FileName[2] == 'F') {
1225 if (FileName[3] == 'i' || FileName[3] == 'I') {
1226 return TRUE;
1227 } else if (FileName[3] == 0x0000) {
1228 return FALSE;
1229 }
1230 } else if (FileName[2] == 0x0000) {
1231 return FALSE;
1232 }
1233 } else if (FileName[1] == 0x0000) {
1234 return FALSE;
1235 }
1236 }
1237
1238 FileName += 1;
1239 }
1240
1241 return FALSE;
1242 }
1243
1244 /**
1245
1246 Check whether current FileName point to a valid Efi Application
1247
1248 @param Dir Pointer to current Directory
1249 @param FileName Pointer to current File name.
1250
1251 @retval TRUE Is a valid Efi Application
1252 @retval FALSE not a valid Efi Application
1253
1254 **/
1255 BOOLEAN
1256 BOpt_IsEfiApp (
1257 IN EFI_FILE_HANDLE Dir,
1258 IN UINT16 *FileName
1259 )
1260 {
1261 UINTN BufferSize;
1262 EFI_IMAGE_DOS_HEADER DosHdr;
1263 UINT16 Subsystem;
1264 EFI_FILE_HANDLE File;
1265 EFI_STATUS Status;
1266 EFI_IMAGE_OPTIONAL_HEADER_UNION PeHdr;
1267
1268 Status = Dir->Open (Dir, &File, FileName, EFI_FILE_MODE_READ, 0);
1269
1270 if (EFI_ERROR (Status)) {
1271 return FALSE;
1272 }
1273
1274 BufferSize = sizeof (EFI_IMAGE_DOS_HEADER);
1275 File->Read (File, &BufferSize, &DosHdr);
1276 if (DosHdr.e_magic != EFI_IMAGE_DOS_SIGNATURE) {
1277 File->Close (File);
1278 return FALSE;
1279 }
1280
1281 File->SetPosition (File, DosHdr.e_lfanew);
1282 BufferSize = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
1283 File->Read (File, &BufferSize, &PeHdr);
1284 if (PeHdr.Pe32.Signature != EFI_IMAGE_NT_SIGNATURE) {
1285 File->Close (File);
1286 return FALSE;
1287 }
1288 //
1289 // Determine PE type and read subsytem
1290 //
1291 if (PeHdr.Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1292 Subsystem = PeHdr.Pe32.OptionalHeader.Subsystem;
1293 } else if (PeHdr.Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
1294 Subsystem = PeHdr.Pe32Plus.OptionalHeader.Subsystem;
1295 } else {
1296 return FALSE;
1297 }
1298
1299 if (Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
1300 File->Close (File);
1301 return TRUE;
1302 } else {
1303 File->Close (File);
1304 return FALSE;
1305 }
1306 }
1307
1308 /**
1309
1310 Find drivers that will be added as Driver#### variables from handles
1311 in current system environment
1312 All valid handles in the system except those consume SimpleFs, LoadFile
1313 are stored in DriverMenu for future use.
1314
1315 @retval EFI_SUCCESS The function complets successfully.
1316 @return Other value if failed to build the DriverMenu.
1317
1318 **/
1319 EFI_STATUS
1320 BOpt_FindDrivers (
1321 VOID
1322 )
1323 {
1324 UINTN NoDevicePathHandles;
1325 EFI_HANDLE *DevicePathHandle;
1326 UINTN Index;
1327 EFI_STATUS Status;
1328 BM_MENU_ENTRY *NewMenuEntry;
1329 BM_HANDLE_CONTEXT *NewHandleContext;
1330 EFI_HANDLE CurHandle;
1331 UINTN OptionNumber;
1332 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFs;
1333 EFI_LOAD_FILE_PROTOCOL *LoadFile;
1334
1335 SimpleFs = NULL;
1336 LoadFile = NULL;
1337
1338 InitializeListHead (&DriverMenu.Head);
1339
1340 //
1341 // At first, get all handles that support Device Path
1342 // protocol which is the basic requirement for
1343 // Driver####
1344 //
1345 Status = gBS->LocateHandleBuffer (
1346 ByProtocol,
1347 &gEfiDevicePathProtocolGuid,
1348 NULL,
1349 &NoDevicePathHandles,
1350 &DevicePathHandle
1351 );
1352 if (EFI_ERROR (Status)) {
1353 return Status;
1354 }
1355
1356 OptionNumber = 0;
1357 for (Index = 0; Index < NoDevicePathHandles; Index++) {
1358 CurHandle = DevicePathHandle[Index];
1359
1360 Status = gBS->HandleProtocol (
1361 CurHandle,
1362 &gEfiSimpleFileSystemProtocolGuid,
1363 (VOID **) &SimpleFs
1364 );
1365 if (Status == EFI_SUCCESS) {
1366 continue;
1367 }
1368
1369 Status = gBS->HandleProtocol (
1370 CurHandle,
1371 &gEfiLoadFileProtocolGuid,
1372 (VOID **) &LoadFile
1373 );
1374 if (Status == EFI_SUCCESS) {
1375 continue;
1376 }
1377
1378 NewMenuEntry = BOpt_CreateMenuEntry (BM_HANDLE_CONTEXT_SELECT);
1379 if (NULL == NewMenuEntry) {
1380 FreePool (DevicePathHandle);
1381 return EFI_OUT_OF_RESOURCES;
1382 }
1383
1384 NewHandleContext = (BM_HANDLE_CONTEXT *) NewMenuEntry->VariableContext;
1385 NewHandleContext->Handle = CurHandle;
1386 NewHandleContext->DevicePath = DevicePathFromHandle (CurHandle);
1387 NewMenuEntry->DisplayString = DevicePathToStr (NewHandleContext->DevicePath);
1388 NewMenuEntry->HelpString = NULL;
1389 NewMenuEntry->OptionNumber = OptionNumber;
1390 OptionNumber++;
1391 InsertTailList (&DriverMenu.Head, &NewMenuEntry->Link);
1392
1393 }
1394
1395 if (DevicePathHandle != NULL) {
1396 FreePool (DevicePathHandle);
1397 }
1398
1399 DriverMenu.MenuNumber = OptionNumber;
1400 return EFI_SUCCESS;
1401 }
1402
1403 /**
1404
1405 Get the Option Number that has not been allocated for use.
1406
1407 @return The available Option Number.
1408
1409 **/
1410 UINT16
1411 BOpt_GetBootOptionNumber (
1412 VOID
1413 )
1414 {
1415 BM_MENU_ENTRY *NewMenuEntry;
1416 UINT16 *BootOrderList;
1417 UINTN BootOrderListSize;
1418 UINT16 Number;
1419 UINTN Index;
1420 UINTN Index2;
1421 BOOLEAN Found;
1422 CHAR16 StrTemp[100];
1423 UINT16 *OptionBuffer;
1424 UINTN OptionSize;
1425
1426 BootOrderListSize = 0;
1427 BootOrderList = NULL;
1428
1429 BootOrderList = BdsLibGetVariableAndSize (
1430 L"BootOrder",
1431 &gEfiGlobalVariableGuid,
1432 &BootOrderListSize
1433 );
1434 if (BootOrderList != NULL) {
1435 //
1436 // already have Boot####
1437 //
1438 // AlreadyBootNumbers = BootOrderListSize / sizeof(UINT16);
1439 //
1440 for (Index = 0; Index < BootOrderListSize / sizeof (UINT16); Index++) {
1441 Found = TRUE;
1442 for (Index2 = 0; Index2 < BootOptionMenu.MenuNumber; Index2++) {
1443 NewMenuEntry = BOpt_GetMenuEntry (&BootOptionMenu, Index2);
1444 if (Index == NewMenuEntry->OptionNumber) {
1445 Found = FALSE;
1446 break;
1447 }
1448 }
1449
1450 if (Found) {
1451 UnicodeSPrint (StrTemp, 100, L"Boot%04x", Index);
1452 DEBUG((DEBUG_ERROR,"INdex= %s\n", StrTemp));
1453 OptionBuffer = BdsLibGetVariableAndSize (
1454 StrTemp,
1455 &gEfiGlobalVariableGuid,
1456 &OptionSize
1457 );
1458 if (NULL == OptionBuffer) {
1459 break;
1460 }
1461 }
1462 }
1463 //
1464 // end for Index
1465 //
1466 Number = (UINT16) Index;
1467 } else {
1468 //
1469 // No Boot####
1470 //
1471 Number = 0;
1472 }
1473
1474 return Number;
1475 }
1476
1477 /**
1478
1479 Get the Option Number that is not in use.
1480
1481 @return The unused Option Number.
1482
1483 **/
1484 UINT16
1485 BOpt_GetDriverOptionNumber (
1486 VOID
1487 )
1488 {
1489 BM_MENU_ENTRY *NewMenuEntry;
1490 UINT16 *DriverOrderList;
1491 UINTN DriverOrderListSize;
1492 UINT16 Number;
1493 UINTN Index;
1494 UINTN Index2;
1495 BOOLEAN Found;
1496
1497 DriverOrderListSize = 0;
1498 DriverOrderList = NULL;
1499
1500 DriverOrderList = BdsLibGetVariableAndSize (
1501 L"DriverOrder",
1502 &gEfiGlobalVariableGuid,
1503 &DriverOrderListSize
1504 );
1505 if (DriverOrderList != NULL) {
1506 //
1507 // already have Driver####
1508 //
1509 // AlreadyDriverNumbers = DriverOrderListSize / sizeof(UINT16);
1510 //
1511 for (Index = 0; Index < DriverOrderListSize / sizeof (UINT16); Index++) {
1512 Found = TRUE;
1513 for (Index2 = 0; Index2 < DriverOptionMenu.MenuNumber; Index2++) {
1514 NewMenuEntry = BOpt_GetMenuEntry (&DriverOptionMenu, Index2);
1515 if (Index == NewMenuEntry->OptionNumber) {
1516 Found = FALSE;
1517 break;
1518 }
1519 }
1520
1521 if (Found) {
1522 break;
1523 }
1524 }
1525 //
1526 // end for Index
1527 //
1528 Number = (UINT16) Index;
1529 } else {
1530 //
1531 // No Driver####
1532 //
1533 Number = 0;
1534 }
1535
1536 return Number;
1537 }
1538
1539 /**
1540
1541 Build up all DriverOptionMenu
1542
1543 @param CallbackData The BMM context data.
1544
1545 @retval EFI_SUCESS The functin completes successfully.
1546 @retval EFI_OUT_OF_RESOURCES Not enough memory to compete the operation.
1547 @retval EFI_NOT_FOUND Fail to get "DriverOrder" variable.
1548
1549 **/
1550 EFI_STATUS
1551 BOpt_GetDriverOptions (
1552 IN BMM_CALLBACK_DATA *CallbackData
1553 )
1554 {
1555 UINTN Index;
1556 UINT16 DriverString[12];
1557 UINT8 *LoadOptionFromVar;
1558 UINT8 *LoadOption;
1559 UINTN DriverOptionSize;
1560
1561 UINT16 *DriverOrderList;
1562 UINTN DriverOrderListSize;
1563 BM_MENU_ENTRY *NewMenuEntry;
1564 BM_LOAD_CONTEXT *NewLoadContext;
1565 UINT8 *LoadOptionPtr;
1566 UINTN StringSize;
1567 UINTN OptionalDataSize;
1568 UINT8 *LoadOptionEnd;
1569
1570 DriverOrderListSize = 0;
1571 DriverOrderList = NULL;
1572 DriverOptionSize = 0;
1573 LoadOptionFromVar = NULL;
1574 BOpt_FreeMenu (&DriverOptionMenu);
1575 InitializeListHead (&DriverOptionMenu.Head);
1576 //
1577 // Get the DriverOrder from the Var
1578 //
1579 DriverOrderList = BdsLibGetVariableAndSize (
1580 L"DriverOrder",
1581 &gEfiGlobalVariableGuid,
1582 &DriverOrderListSize
1583 );
1584 if (DriverOrderList == NULL) {
1585 return EFI_NOT_FOUND;
1586 }
1587
1588 for (Index = 0; Index < DriverOrderListSize / sizeof (UINT16); Index++) {
1589 UnicodeSPrint (
1590 DriverString,
1591 sizeof (DriverString),
1592 L"Driver%04x",
1593 DriverOrderList[Index]
1594 );
1595 //
1596 // Get all loadoptions from the VAR
1597 //
1598 LoadOptionFromVar = BdsLibGetVariableAndSize (
1599 DriverString,
1600 &gEfiGlobalVariableGuid,
1601 &DriverOptionSize
1602 );
1603 if (LoadOptionFromVar == NULL) {
1604 continue;
1605 }
1606
1607 LoadOption = AllocateZeroPool (DriverOptionSize);
1608 if (LoadOption == NULL) {
1609 continue;
1610 }
1611
1612 CopyMem (LoadOption, LoadOptionFromVar, DriverOptionSize);
1613 FreePool (LoadOptionFromVar);
1614
1615 NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT);
1616 if (NULL == NewMenuEntry) {
1617 return EFI_OUT_OF_RESOURCES;
1618 }
1619
1620 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;
1621 LoadOptionPtr = LoadOption;
1622 LoadOptionEnd = LoadOption + DriverOptionSize;
1623 NewMenuEntry->OptionNumber = DriverOrderList[Index];
1624 NewLoadContext->LoadOptionModified = FALSE;
1625 NewLoadContext->Deleted = FALSE;
1626 NewLoadContext->IsLegacy = FALSE;
1627
1628 //
1629 // LoadOption is a pointer type of UINT8
1630 // for easy use with following LOAD_OPTION
1631 // embedded in this struct
1632 //
1633 NewLoadContext->LoadOption = LoadOption;
1634 NewLoadContext->LoadOptionSize = DriverOptionSize;
1635
1636 NewLoadContext->Attributes = *(UINT32 *) LoadOptionPtr;
1637 NewLoadContext->IsActive = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_ACTIVE);
1638
1639 NewLoadContext->ForceReconnect = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_FORCE_RECONNECT);
1640
1641 LoadOptionPtr += sizeof (UINT32);
1642
1643 NewLoadContext->FilePathListLength = *(UINT16 *) LoadOptionPtr;
1644 LoadOptionPtr += sizeof (UINT16);
1645
1646 StringSize = StrSize ((UINT16 *) LoadOptionPtr);
1647 NewLoadContext->Description = AllocateZeroPool (StringSize);
1648 ASSERT (NewLoadContext->Description != NULL);
1649 CopyMem (
1650 NewLoadContext->Description,
1651 (UINT16 *) LoadOptionPtr,
1652 StringSize
1653 );
1654 NewMenuEntry->DisplayString = NewLoadContext->Description;
1655
1656 LoadOptionPtr += StringSize;
1657
1658 NewLoadContext->FilePathList = AllocateZeroPool (NewLoadContext->FilePathListLength);
1659 ASSERT (NewLoadContext->FilePathList != NULL);
1660 CopyMem (
1661 NewLoadContext->FilePathList,
1662 (EFI_DEVICE_PATH_PROTOCOL *) LoadOptionPtr,
1663 NewLoadContext->FilePathListLength
1664 );
1665
1666 NewMenuEntry->HelpString = DevicePathToStr (NewLoadContext->FilePathList);
1667 NewMenuEntry->DisplayStringToken = GetStringTokenFromDepository (
1668 CallbackData,
1669 DriverOptionStrDepository
1670 );
1671 NewMenuEntry->HelpStringToken = GetStringTokenFromDepository (
1672 CallbackData,
1673 DriverOptionHelpStrDepository
1674 );
1675 LoadOptionPtr += NewLoadContext->FilePathListLength;
1676
1677 if (LoadOptionPtr < LoadOptionEnd) {
1678 OptionalDataSize = DriverOptionSize -
1679 sizeof (UINT32) -
1680 sizeof (UINT16) -
1681 StringSize -
1682 NewLoadContext->FilePathListLength;
1683
1684 NewLoadContext->OptionalData = AllocateZeroPool (OptionalDataSize);
1685 ASSERT (NewLoadContext->OptionalData != NULL);
1686 CopyMem (
1687 NewLoadContext->OptionalData,
1688 LoadOptionPtr,
1689 OptionalDataSize
1690 );
1691
1692 NewLoadContext->OptionalDataSize = OptionalDataSize;
1693 }
1694
1695 InsertTailList (&DriverOptionMenu.Head, &NewMenuEntry->Link);
1696
1697 }
1698
1699 if (DriverOrderList != NULL) {
1700 FreePool (DriverOrderList);
1701 }
1702 DriverOptionMenu.MenuNumber = Index;
1703 return EFI_SUCCESS;
1704
1705 }
1706