]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/BdsDxe/BootMaint/BootOption.c
IntelFrameworkModulePkg BdsDxe: Remove redundant functions
[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 - 2018, Intel Corporation. All rights reserved.<BR>
9 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 VolumeLabel = FileContext->Info->VolumeLabel;
374 if (*VolumeLabel == 0x0000) {
375 VolumeLabel = L"NO VOLUME LABEL";
376 }
377 }
378
379 TempStr = MenuEntry->HelpString;
380 MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);
381 ASSERT (MenuEntry->DisplayString != NULL);
382 UnicodeSPrint (
383 MenuEntry->DisplayString,
384 MAX_CHAR,
385 L"%s, [%s]",
386 VolumeLabel,
387 TempStr
388 );
389 OptionNumber++;
390 InsertTailList (&FsOptionMenu.Head, &MenuEntry->Link);
391 }
392 }
393
394 if (NoSimpleFsHandles != 0) {
395 FreePool (SimpleFsHandle);
396 }
397 //
398 // Searching for handles that support Load File protocol
399 //
400 Status = gBS->LocateHandleBuffer (
401 ByProtocol,
402 &gEfiLoadFileProtocolGuid,
403 NULL,
404 &NoLoadFileHandles,
405 &LoadFileHandle
406 );
407
408 if (!EFI_ERROR (Status)) {
409 for (Index = 0; Index < NoLoadFileHandles; Index++) {
410 MenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);
411 if (NULL == MenuEntry) {
412 FreePool (LoadFileHandle);
413 return EFI_OUT_OF_RESOURCES;
414 }
415
416 FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;
417 FileContext->IsRemovableMedia = FALSE;
418 FileContext->IsLoadFile = TRUE;
419 FileContext->Handle = LoadFileHandle[Index];
420 FileContext->IsRoot = TRUE;
421
422 FileContext->DevicePath = DevicePathFromHandle (FileContext->Handle);
423 FileContext->FileName = DevicePathToStr (FileContext->DevicePath);
424
425 MenuEntry->HelpString = DevicePathToStr (FileContext->DevicePath);
426
427 TempStr = MenuEntry->HelpString;
428 MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);
429 ASSERT (MenuEntry->DisplayString != NULL);
430 UnicodeSPrint (
431 MenuEntry->DisplayString,
432 MAX_CHAR,
433 L"Load File [%s]",
434 TempStr
435 );
436
437 MenuEntry->OptionNumber = OptionNumber;
438 OptionNumber++;
439 InsertTailList (&FsOptionMenu.Head, &MenuEntry->Link);
440 }
441 }
442
443 if (NoLoadFileHandles != 0) {
444 FreePool (LoadFileHandle);
445 }
446
447 //
448 // Add Legacy Boot Option Support Here
449 //
450 Status = gBS->LocateProtocol (
451 &gEfiLegacyBiosProtocolGuid,
452 NULL,
453 (VOID **) &LegacyBios
454 );
455 if (!EFI_ERROR (Status)) {
456
457 for (Index = BBS_TYPE_FLOPPY; Index <= BBS_TYPE_EMBEDDED_NETWORK; Index++) {
458 MenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);
459 if (NULL == MenuEntry) {
460 return EFI_OUT_OF_RESOURCES;
461 }
462
463 FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;
464
465 FileContext->IsRemovableMedia = FALSE;
466 FileContext->IsLoadFile = TRUE;
467 FileContext->IsBootLegacy = TRUE;
468 DeviceType = (UINT16) Index;
469 BbsDevicePathNode.Header.Type = BBS_DEVICE_PATH;
470 BbsDevicePathNode.Header.SubType = BBS_BBS_DP;
471 SetDevicePathNodeLength (
472 &BbsDevicePathNode.Header,
473 sizeof (BBS_BBS_DEVICE_PATH)
474 );
475 BbsDevicePathNode.DeviceType = DeviceType;
476 BbsDevicePathNode.StatusFlag = 0;
477 BbsDevicePathNode.String[0] = 0;
478 DevicePath = AppendDevicePathNode (
479 EndDevicePath,
480 (EFI_DEVICE_PATH_PROTOCOL *) &BbsDevicePathNode
481 );
482
483 FileContext->DevicePath = DevicePath;
484 MenuEntry->HelpString = DevicePathToStr (FileContext->DevicePath);
485
486 TempStr = MenuEntry->HelpString;
487 MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);
488 ASSERT (MenuEntry->DisplayString != NULL);
489 UnicodeSPrint (
490 MenuEntry->DisplayString,
491 MAX_CHAR,
492 L"Boot Legacy [%s]",
493 TempStr
494 );
495 MenuEntry->OptionNumber = OptionNumber;
496 OptionNumber++;
497 InsertTailList (&FsOptionMenu.Head, &MenuEntry->Link);
498 }
499 }
500 //
501 // Remember how many file system options are here
502 //
503 FsOptionMenu.MenuNumber = OptionNumber;
504 return EFI_SUCCESS;
505 }
506
507 /**
508 Free resources allocated in Allocate Rountine.
509
510 @param FreeMenu Menu to be freed
511 **/
512 VOID
513 BOpt_FreeMenu (
514 BM_MENU_OPTION *FreeMenu
515 )
516 {
517 BM_MENU_ENTRY *MenuEntry;
518 while (!IsListEmpty (&FreeMenu->Head)) {
519 MenuEntry = CR (
520 FreeMenu->Head.ForwardLink,
521 BM_MENU_ENTRY,
522 Link,
523 BM_MENU_ENTRY_SIGNATURE
524 );
525 RemoveEntryList (&MenuEntry->Link);
526 BOpt_DestroyMenuEntry (MenuEntry);
527 }
528 FreeMenu->MenuNumber = 0;
529 }
530
531 /**
532 Find files under current directory
533 All files and sub-directories in current directory
534 will be stored in DirectoryMenu for future use.
535
536 @param CallbackData The BMM context data.
537 @param MenuEntry The Menu Entry.
538
539 @retval EFI_SUCCESS Get files from current dir successfully.
540 @return Other value if can't get files from current dir.
541
542 **/
543 EFI_STATUS
544 BOpt_FindFiles (
545 IN BMM_CALLBACK_DATA *CallbackData,
546 IN BM_MENU_ENTRY *MenuEntry
547 )
548 {
549 EFI_FILE_HANDLE NewDir;
550 EFI_FILE_HANDLE Dir;
551 EFI_FILE_INFO *DirInfo;
552 UINTN BufferSize;
553 UINTN DirBufferSize;
554 BM_MENU_ENTRY *NewMenuEntry;
555 BM_FILE_CONTEXT *FileContext;
556 BM_FILE_CONTEXT *NewFileContext;
557 UINTN Pass;
558 EFI_STATUS Status;
559 UINTN OptionNumber;
560
561 FileContext = (BM_FILE_CONTEXT *) MenuEntry->VariableContext;
562 Dir = FileContext->FHandle;
563 OptionNumber = 0;
564 //
565 // Open current directory to get files from it
566 //
567 Status = Dir->Open (
568 Dir,
569 &NewDir,
570 FileContext->FileName,
571 EFI_FILE_READ_ONLY,
572 0
573 );
574 if (!FileContext->IsRoot) {
575 Dir->Close (Dir);
576 }
577
578 if (EFI_ERROR (Status)) {
579 return Status;
580 }
581
582 DirInfo = EfiLibFileInfo (NewDir);
583 if (DirInfo == NULL) {
584 return EFI_NOT_FOUND;
585 }
586
587 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
588 return EFI_INVALID_PARAMETER;
589 }
590
591 FileContext->DevicePath = FileDevicePath (
592 FileContext->Handle,
593 FileContext->FileName
594 );
595
596 DirBufferSize = sizeof (EFI_FILE_INFO) + 1024;
597 DirInfo = AllocateZeroPool (DirBufferSize);
598 if (DirInfo == NULL) {
599 return EFI_OUT_OF_RESOURCES;
600 }
601 //
602 // Get all files in current directory
603 // Pass 1 to get Directories
604 // Pass 2 to get files that are EFI images
605 //
606 for (Pass = 1; Pass <= 2; Pass++) {
607 NewDir->SetPosition (NewDir, 0);
608 for (;;) {
609 BufferSize = DirBufferSize;
610 Status = NewDir->Read (NewDir, &BufferSize, DirInfo);
611 if (EFI_ERROR (Status) || BufferSize == 0) {
612 break;
613 }
614
615 if (((DirInfo->Attribute & EFI_FILE_DIRECTORY) != 0 && Pass == 2) ||
616 ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0 && Pass == 1)
617 ) {
618 //
619 // Pass 1 is for Directories
620 // Pass 2 is for file names
621 //
622 continue;
623 }
624
625 if (!(BOpt_IsEfiImageName (DirInfo->FileName) || (DirInfo->Attribute & EFI_FILE_DIRECTORY) != 0)) {
626 //
627 // Slip file unless it is a directory entry or a .EFI file
628 //
629 continue;
630 }
631
632 NewMenuEntry = BOpt_CreateMenuEntry (BM_FILE_CONTEXT_SELECT);
633 if (NULL == NewMenuEntry) {
634 return EFI_OUT_OF_RESOURCES;
635 }
636
637 NewFileContext = (BM_FILE_CONTEXT *) NewMenuEntry->VariableContext;
638 NewFileContext->Handle = FileContext->Handle;
639 NewFileContext->FileName = BOpt_AppendFileName (
640 FileContext->FileName,
641 DirInfo->FileName
642 );
643 NewFileContext->FHandle = NewDir;
644 NewFileContext->DevicePath = FileDevicePath (
645 NewFileContext->Handle,
646 NewFileContext->FileName
647 );
648 NewMenuEntry->HelpString = NULL;
649
650 MenuEntry->DisplayStringToken = GetStringTokenFromDepository (
651 CallbackData,
652 FileOptionStrDepository
653 );
654
655 NewFileContext->IsDir = (BOOLEAN) ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY);
656
657 if (NewFileContext->IsDir) {
658 BufferSize = StrLen (DirInfo->FileName) * 2 + 6;
659 NewMenuEntry->DisplayString = AllocateZeroPool (BufferSize);
660
661 UnicodeSPrint (
662 NewMenuEntry->DisplayString,
663 BufferSize,
664 L"<%s>",
665 DirInfo->FileName
666 );
667
668 } else {
669 NewMenuEntry->DisplayString = EfiStrDuplicate (DirInfo->FileName);
670 }
671
672 NewFileContext->IsRoot = FALSE;
673 NewFileContext->IsLoadFile = FALSE;
674 NewFileContext->IsRemovableMedia = FALSE;
675
676 NewMenuEntry->OptionNumber = OptionNumber;
677 OptionNumber++;
678 InsertTailList (&DirectoryMenu.Head, &NewMenuEntry->Link);
679 }
680 }
681
682 DirectoryMenu.MenuNumber = OptionNumber;
683 FreePool (DirInfo);
684 return EFI_SUCCESS;
685 }
686
687 /**
688 Build the LegacyFDMenu LegacyHDMenu LegacyCDMenu according to LegacyBios.GetBbsInfo().
689
690 @retval EFI_SUCCESS The function complete successfully.
691 @retval EFI_OUT_OF_RESOURCES No enough memory to complete this function.
692
693 **/
694 EFI_STATUS
695 BOpt_GetLegacyOptions (
696 VOID
697 )
698 {
699 BM_MENU_ENTRY *NewMenuEntry;
700 BM_LEGACY_DEVICE_CONTEXT *NewLegacyDevContext;
701 EFI_STATUS Status;
702 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
703 UINT16 HddCount;
704 HDD_INFO *HddInfo;
705 UINT16 BbsCount;
706 BBS_TABLE *BbsTable;
707 UINT16 Index;
708 CHAR16 DescString[100];
709 UINTN FDNum;
710 UINTN HDNum;
711 UINTN CDNum;
712 UINTN NETNum;
713 UINTN BEVNum;
714
715 NewMenuEntry = NULL;
716 HddInfo = NULL;
717 BbsTable = NULL;
718 BbsCount = 0;
719
720 //
721 // Initialize Bbs Table Context from BBS info data
722 //
723 InitializeListHead (&LegacyFDMenu.Head);
724 InitializeListHead (&LegacyHDMenu.Head);
725 InitializeListHead (&LegacyCDMenu.Head);
726 InitializeListHead (&LegacyNETMenu.Head);
727 InitializeListHead (&LegacyBEVMenu.Head);
728
729 Status = gBS->LocateProtocol (
730 &gEfiLegacyBiosProtocolGuid,
731 NULL,
732 (VOID **) &LegacyBios
733 );
734 if (!EFI_ERROR (Status)) {
735 Status = LegacyBios->GetBbsInfo (
736 LegacyBios,
737 &HddCount,
738 &HddInfo,
739 &BbsCount,
740 &BbsTable
741 );
742 if (EFI_ERROR (Status)) {
743 return Status;
744 }
745 }
746
747 FDNum = 0;
748 HDNum = 0;
749 CDNum = 0;
750 NETNum = 0;
751 BEVNum = 0;
752
753 for (Index = 0; Index < BbsCount; Index++) {
754 if ((BBS_IGNORE_ENTRY == BbsTable[Index].BootPriority) ||
755 (BBS_DO_NOT_BOOT_FROM == BbsTable[Index].BootPriority)
756 ) {
757 continue;
758 }
759
760 NewMenuEntry = BOpt_CreateMenuEntry (BM_LEGACY_DEV_CONTEXT_SELECT);
761 if (NULL == NewMenuEntry) {
762 break;
763 }
764
765 NewLegacyDevContext = (BM_LEGACY_DEVICE_CONTEXT *) NewMenuEntry->VariableContext;
766 NewLegacyDevContext->BbsEntry = &BbsTable[Index];
767 NewLegacyDevContext->BbsIndex = Index;
768 NewLegacyDevContext->BbsCount = BbsCount;
769 BdsBuildLegacyDevNameString (
770 &BbsTable[Index],
771 Index,
772 sizeof (DescString),
773 DescString
774 );
775 NewLegacyDevContext->Description = AllocateCopyPool (StrSize (DescString), DescString);
776 if (NULL == NewLegacyDevContext->Description) {
777 break;
778 }
779
780 NewMenuEntry->DisplayString = NewLegacyDevContext->Description;
781 NewMenuEntry->HelpString = NULL;
782
783 switch (BbsTable[Index].DeviceType) {
784 case BBS_FLOPPY:
785 InsertTailList (&LegacyFDMenu.Head, &NewMenuEntry->Link);
786 FDNum++;
787 break;
788
789 case BBS_HARDDISK:
790 InsertTailList (&LegacyHDMenu.Head, &NewMenuEntry->Link);
791 HDNum++;
792 break;
793
794 case BBS_CDROM:
795 InsertTailList (&LegacyCDMenu.Head, &NewMenuEntry->Link);
796 CDNum++;
797 break;
798
799 case BBS_EMBED_NETWORK:
800 InsertTailList (&LegacyNETMenu.Head, &NewMenuEntry->Link);
801 NETNum++;
802 break;
803
804 case BBS_BEV_DEVICE:
805 InsertTailList (&LegacyBEVMenu.Head, &NewMenuEntry->Link);
806 BEVNum++;
807 break;
808 }
809 }
810
811 if (Index != BbsCount) {
812 BOpt_FreeLegacyOptions ();
813 return EFI_OUT_OF_RESOURCES;
814 }
815
816 LegacyFDMenu.MenuNumber = FDNum;
817 LegacyHDMenu.MenuNumber = HDNum;
818 LegacyCDMenu.MenuNumber = CDNum;
819 LegacyNETMenu.MenuNumber = NETNum;
820 LegacyBEVMenu.MenuNumber = BEVNum;
821 return EFI_SUCCESS;
822 }
823
824 /**
825 Free out resouce allocated from Legacy Boot Options.
826
827 **/
828 VOID
829 BOpt_FreeLegacyOptions (
830 VOID
831 )
832 {
833 BOpt_FreeMenu (&LegacyFDMenu);
834 BOpt_FreeMenu (&LegacyHDMenu);
835 BOpt_FreeMenu (&LegacyCDMenu);
836 BOpt_FreeMenu (&LegacyNETMenu);
837 BOpt_FreeMenu (&LegacyBEVMenu);
838 }
839
840 /**
841
842 Build the BootOptionMenu according to BootOrder Variable.
843 This Routine will access the Boot#### to get EFI_LOAD_OPTION.
844
845 @param CallbackData The BMM context data.
846
847 @return EFI_NOT_FOUND Fail to find "BootOrder" variable.
848 @return EFI_SUCESS Success build boot option menu.
849
850 **/
851 EFI_STATUS
852 BOpt_GetBootOptions (
853 IN BMM_CALLBACK_DATA *CallbackData
854 )
855 {
856 UINTN Index;
857 UINT16 BootString[10];
858 UINT8 *LoadOptionFromVar;
859 UINT8 *LoadOption;
860 UINTN BootOptionSize;
861 BOOLEAN BootNextFlag;
862 UINT16 *BootOrderList;
863 UINTN BootOrderListSize;
864 UINT16 *BootNext;
865 UINTN BootNextSize;
866 BM_MENU_ENTRY *NewMenuEntry;
867 BM_LOAD_CONTEXT *NewLoadContext;
868 UINT8 *LoadOptionPtr;
869 UINTN StringSize;
870 UINTN OptionalDataSize;
871 UINT8 *LoadOptionEnd;
872 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
873 UINTN MenuCount;
874 UINT8 *Ptr;
875
876 MenuCount = 0;
877 BootOrderListSize = 0;
878 BootNextSize = 0;
879 BootOrderList = NULL;
880 BootNext = NULL;
881 LoadOptionFromVar = NULL;
882 BOpt_FreeMenu (&BootOptionMenu);
883 InitializeListHead (&BootOptionMenu.Head);
884
885 //
886 // Get the BootOrder from the Var
887 //
888 BootOrderList = BdsLibGetVariableAndSize (
889 L"BootOrder",
890 &gEfiGlobalVariableGuid,
891 &BootOrderListSize
892 );
893 if (BootOrderList == NULL) {
894 return EFI_NOT_FOUND;
895 }
896
897 //
898 // Get the BootNext from the Var
899 //
900 BootNext = BdsLibGetVariableAndSize (
901 L"BootNext",
902 &gEfiGlobalVariableGuid,
903 &BootNextSize
904 );
905
906 if (BootNext != NULL) {
907 if (BootNextSize != sizeof (UINT16)) {
908 FreePool (BootNext);
909 BootNext = NULL;
910 }
911 }
912
913 for (Index = 0; Index < BootOrderListSize / sizeof (UINT16); Index++) {
914 UnicodeSPrint (BootString, sizeof (BootString), L"Boot%04x", BootOrderList[Index]);
915 //
916 // Get all loadoptions from the VAR
917 //
918 LoadOptionFromVar = BdsLibGetVariableAndSize (
919 BootString,
920 &gEfiGlobalVariableGuid,
921 &BootOptionSize
922 );
923 if (LoadOptionFromVar == NULL) {
924 continue;
925 }
926
927 LoadOption = AllocateZeroPool (BootOptionSize);
928 if (LoadOption == NULL) {
929 continue;
930 }
931
932 CopyMem (LoadOption, LoadOptionFromVar, BootOptionSize);
933 FreePool (LoadOptionFromVar);
934
935 if (BootNext != NULL) {
936 BootNextFlag = (BOOLEAN) (*BootNext == BootOrderList[Index]);
937 } else {
938 BootNextFlag = FALSE;
939 }
940
941 if (0 == (*((UINT32 *) LoadOption) & LOAD_OPTION_ACTIVE)) {
942 FreePool (LoadOption);
943 continue;
944 }
945 //
946 // BUGBUG: could not return EFI_OUT_OF_RESOURCES here directly.
947 // the buffer allocated already should be freed before returning.
948 //
949 NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT);
950 if (NULL == NewMenuEntry) {
951 return EFI_OUT_OF_RESOURCES;
952 }
953
954 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;
955
956 LoadOptionPtr = LoadOption;
957 LoadOptionEnd = LoadOption + BootOptionSize;
958
959 NewMenuEntry->OptionNumber = BootOrderList[Index];
960 NewLoadContext->LoadOptionModified = FALSE;
961 NewLoadContext->Deleted = FALSE;
962 NewLoadContext->IsBootNext = BootNextFlag;
963
964 //
965 // Is a Legacy Device?
966 //
967 Ptr = (UINT8 *) LoadOption;
968
969 //
970 // Attribute = *(UINT32 *)Ptr;
971 //
972 Ptr += sizeof (UINT32);
973
974 //
975 // FilePathSize = *(UINT16 *)Ptr;
976 //
977 Ptr += sizeof (UINT16);
978
979 //
980 // Description = (CHAR16 *)Ptr;
981 //
982 Ptr += StrSize ((CHAR16 *) Ptr);
983
984 //
985 // Now Ptr point to Device Path
986 //
987 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Ptr;
988 if ((BBS_DEVICE_PATH == DevicePath->Type) && (BBS_BBS_DP == DevicePath->SubType)) {
989 NewLoadContext->IsLegacy = TRUE;
990 } else {
991 NewLoadContext->IsLegacy = FALSE;
992 }
993 //
994 // LoadOption is a pointer type of UINT8
995 // for easy use with following LOAD_OPTION
996 // embedded in this struct
997 //
998 NewLoadContext->LoadOption = LoadOption;
999 NewLoadContext->LoadOptionSize = BootOptionSize;
1000
1001 NewLoadContext->Attributes = *(UINT32 *) LoadOptionPtr;
1002 NewLoadContext->IsActive = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_ACTIVE);
1003
1004 NewLoadContext->ForceReconnect = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_FORCE_RECONNECT);
1005
1006 LoadOptionPtr += sizeof (UINT32);
1007
1008 NewLoadContext->FilePathListLength = *(UINT16 *) LoadOptionPtr;
1009 LoadOptionPtr += sizeof (UINT16);
1010
1011 StringSize = StrSize((UINT16*)LoadOptionPtr);
1012
1013 NewLoadContext->Description = AllocateCopyPool (StrSize((UINT16*)LoadOptionPtr), LoadOptionPtr);
1014 ASSERT (NewLoadContext->Description != NULL);
1015
1016 NewMenuEntry->DisplayString = NewLoadContext->Description;
1017
1018 LoadOptionPtr += StringSize;
1019
1020 NewLoadContext->FilePathList = AllocateZeroPool (NewLoadContext->FilePathListLength);
1021 ASSERT (NewLoadContext->FilePathList != NULL);
1022 CopyMem (
1023 NewLoadContext->FilePathList,
1024 (EFI_DEVICE_PATH_PROTOCOL *) LoadOptionPtr,
1025 NewLoadContext->FilePathListLength
1026 );
1027
1028 NewMenuEntry->HelpString = DevicePathToStr (NewLoadContext->FilePathList);
1029 NewMenuEntry->DisplayStringToken = GetStringTokenFromDepository (
1030 CallbackData,
1031 BootOptionStrDepository
1032 );
1033 NewMenuEntry->HelpStringToken = GetStringTokenFromDepository (
1034 CallbackData,
1035 BootOptionHelpStrDepository
1036 );
1037 LoadOptionPtr += NewLoadContext->FilePathListLength;
1038
1039 if (LoadOptionPtr < LoadOptionEnd) {
1040 OptionalDataSize = BootOptionSize -
1041 sizeof (UINT32) -
1042 sizeof (UINT16) -
1043 StringSize -
1044 NewLoadContext->FilePathListLength;
1045
1046 NewLoadContext->OptionalData = AllocateZeroPool (OptionalDataSize);
1047 ASSERT (NewLoadContext->OptionalData != NULL);
1048 CopyMem (
1049 NewLoadContext->OptionalData,
1050 LoadOptionPtr,
1051 OptionalDataSize
1052 );
1053
1054 NewLoadContext->OptionalDataSize = OptionalDataSize;
1055 }
1056
1057 InsertTailList (&BootOptionMenu.Head, &NewMenuEntry->Link);
1058 MenuCount++;
1059 }
1060
1061 if (BootNext != NULL) {
1062 FreePool (BootNext);
1063 }
1064 if (BootOrderList != NULL) {
1065 FreePool (BootOrderList);
1066 }
1067 BootOptionMenu.MenuNumber = MenuCount;
1068 return EFI_SUCCESS;
1069 }
1070
1071 /**
1072
1073 Append file name to existing file name.
1074
1075 @param Str1 The existing file name
1076 @param Str2 The file name to be appended
1077
1078 @return Allocate a new string to hold the appended result.
1079 Caller is responsible to free the returned string.
1080
1081 **/
1082 CHAR16 *
1083 BOpt_AppendFileName (
1084 IN CHAR16 *Str1,
1085 IN CHAR16 *Str2
1086 )
1087 {
1088 UINTN Size1;
1089 UINTN Size2;
1090 UINTN MaxLen;
1091 CHAR16 *Str;
1092 CHAR16 *TmpStr;
1093 CHAR16 *Ptr;
1094 CHAR16 *LastSlash;
1095
1096 Size1 = StrSize (Str1);
1097 Size2 = StrSize (Str2);
1098 MaxLen = (Size1 + Size2 + sizeof (CHAR16)) / sizeof (CHAR16);
1099 Str = AllocateZeroPool (MaxLen * sizeof (CHAR16));
1100 ASSERT (Str != NULL);
1101
1102 TmpStr = AllocateZeroPool (MaxLen * sizeof (CHAR16));
1103 ASSERT (TmpStr != NULL);
1104
1105 StrCatS (Str, MaxLen, Str1);
1106 if (!((*Str == '\\') && (*(Str + 1) == 0))) {
1107 StrCatS (Str, MaxLen, L"\\");
1108 }
1109
1110 StrCatS (Str, MaxLen, Str2);
1111
1112 Ptr = Str;
1113 LastSlash = Str;
1114 while (*Ptr != 0) {
1115 if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '.' && *(Ptr + 3) == L'\\') {
1116 //
1117 // Convert "\Name\..\" to "\"
1118 // DO NOT convert the .. if it is at the end of the string. This will
1119 // break the .. behavior in changing directories.
1120 //
1121
1122 //
1123 // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings
1124 // that overlap.
1125 //
1126 StrCpyS (TmpStr, MaxLen, Ptr + 3);
1127 StrCpyS (LastSlash, MaxLen - ((UINTN) LastSlash - (UINTN) Str) / sizeof (CHAR16), TmpStr);
1128 Ptr = LastSlash;
1129 } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {
1130 //
1131 // Convert a "\.\" to a "\"
1132 //
1133
1134 //
1135 // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings
1136 // that overlap.
1137 //
1138 StrCpyS (TmpStr, MaxLen, Ptr + 2);
1139 StrCpyS (Ptr, MaxLen - ((UINTN) Ptr - (UINTN) Str) / sizeof (CHAR16), TmpStr);
1140 Ptr = LastSlash;
1141 } else if (*Ptr == '\\') {
1142 LastSlash = Ptr;
1143 }
1144
1145 Ptr++;
1146 }
1147
1148 FreePool (TmpStr);
1149
1150 return Str;
1151 }
1152
1153 /**
1154
1155 Check whether current FileName point to a valid
1156 Efi Image File.
1157
1158 @param FileName File need to be checked.
1159
1160 @retval TRUE Is Efi Image
1161 @retval FALSE Not a valid Efi Image
1162
1163 **/
1164 BOOLEAN
1165 BOpt_IsEfiImageName (
1166 IN UINT16 *FileName
1167 )
1168 {
1169 //
1170 // Search for ".efi" extension
1171 //
1172 while (*FileName != L'\0') {
1173 if (FileName[0] == '.') {
1174 if (FileName[1] == 'e' || FileName[1] == 'E') {
1175 if (FileName[2] == 'f' || FileName[2] == 'F') {
1176 if (FileName[3] == 'i' || FileName[3] == 'I') {
1177 return TRUE;
1178 } else if (FileName[3] == 0x0000) {
1179 return FALSE;
1180 }
1181 } else if (FileName[2] == 0x0000) {
1182 return FALSE;
1183 }
1184 } else if (FileName[1] == 0x0000) {
1185 return FALSE;
1186 }
1187 }
1188
1189 FileName += 1;
1190 }
1191
1192 return FALSE;
1193 }
1194
1195
1196
1197 /**
1198
1199 Find drivers that will be added as Driver#### variables from handles
1200 in current system environment
1201 All valid handles in the system except those consume SimpleFs, LoadFile
1202 are stored in DriverMenu for future use.
1203
1204 @retval EFI_SUCCESS The function complets successfully.
1205 @return Other value if failed to build the DriverMenu.
1206
1207 **/
1208 EFI_STATUS
1209 BOpt_FindDrivers (
1210 VOID
1211 )
1212 {
1213 UINTN NoDevicePathHandles;
1214 EFI_HANDLE *DevicePathHandle;
1215 UINTN Index;
1216 EFI_STATUS Status;
1217 BM_MENU_ENTRY *NewMenuEntry;
1218 BM_HANDLE_CONTEXT *NewHandleContext;
1219 EFI_HANDLE CurHandle;
1220 UINTN OptionNumber;
1221 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFs;
1222 EFI_LOAD_FILE_PROTOCOL *LoadFile;
1223
1224 SimpleFs = NULL;
1225 LoadFile = NULL;
1226
1227 InitializeListHead (&DriverMenu.Head);
1228
1229 //
1230 // At first, get all handles that support Device Path
1231 // protocol which is the basic requirement for
1232 // Driver####
1233 //
1234 Status = gBS->LocateHandleBuffer (
1235 ByProtocol,
1236 &gEfiDevicePathProtocolGuid,
1237 NULL,
1238 &NoDevicePathHandles,
1239 &DevicePathHandle
1240 );
1241 if (EFI_ERROR (Status)) {
1242 return Status;
1243 }
1244
1245 OptionNumber = 0;
1246 for (Index = 0; Index < NoDevicePathHandles; Index++) {
1247 CurHandle = DevicePathHandle[Index];
1248
1249 Status = gBS->HandleProtocol (
1250 CurHandle,
1251 &gEfiSimpleFileSystemProtocolGuid,
1252 (VOID **) &SimpleFs
1253 );
1254 if (Status == EFI_SUCCESS) {
1255 continue;
1256 }
1257
1258 Status = gBS->HandleProtocol (
1259 CurHandle,
1260 &gEfiLoadFileProtocolGuid,
1261 (VOID **) &LoadFile
1262 );
1263 if (Status == EFI_SUCCESS) {
1264 continue;
1265 }
1266
1267 NewMenuEntry = BOpt_CreateMenuEntry (BM_HANDLE_CONTEXT_SELECT);
1268 if (NULL == NewMenuEntry) {
1269 FreePool (DevicePathHandle);
1270 return EFI_OUT_OF_RESOURCES;
1271 }
1272
1273 NewHandleContext = (BM_HANDLE_CONTEXT *) NewMenuEntry->VariableContext;
1274 NewHandleContext->Handle = CurHandle;
1275 NewHandleContext->DevicePath = DevicePathFromHandle (CurHandle);
1276 NewMenuEntry->DisplayString = DevicePathToStr (NewHandleContext->DevicePath);
1277 NewMenuEntry->HelpString = NULL;
1278 NewMenuEntry->OptionNumber = OptionNumber;
1279 OptionNumber++;
1280 InsertTailList (&DriverMenu.Head, &NewMenuEntry->Link);
1281
1282 }
1283
1284 if (DevicePathHandle != NULL) {
1285 FreePool (DevicePathHandle);
1286 }
1287
1288 DriverMenu.MenuNumber = OptionNumber;
1289 return EFI_SUCCESS;
1290 }
1291
1292 /**
1293
1294 Get the Option Number that has not been allocated for use.
1295
1296 @param Type The type of Option.
1297
1298 @return The available Option Number.
1299
1300 **/
1301 UINT16
1302 BOpt_GetOptionNumber (
1303 CHAR16 *Type
1304 )
1305 {
1306 UINT16 *OrderList;
1307 UINTN OrderListSize;
1308 UINTN Index;
1309 CHAR16 StrTemp[20];
1310 UINT16 *OptionBuffer;
1311 UINT16 OptionNumber;
1312 UINTN OptionSize;
1313
1314 OrderListSize = 0;
1315 OrderList = NULL;
1316 OptionNumber = 0;
1317 Index = 0;
1318
1319 UnicodeSPrint (StrTemp, sizeof (StrTemp), L"%sOrder", Type);
1320
1321 OrderList = BdsLibGetVariableAndSize (
1322 StrTemp,
1323 &gEfiGlobalVariableGuid,
1324 &OrderListSize
1325 );
1326
1327 for (OptionNumber = 0; ; OptionNumber++) {
1328 if (OrderList != NULL) {
1329 for (Index = 0; Index < OrderListSize / sizeof (UINT16); Index++) {
1330 if (OptionNumber == OrderList[Index]) {
1331 break;
1332 }
1333 }
1334 }
1335
1336 if (Index < OrderListSize / sizeof (UINT16)) {
1337 //
1338 // The OptionNumber occurs in the OrderList, continue to use next one
1339 //
1340 continue;
1341 }
1342 UnicodeSPrint (StrTemp, sizeof (StrTemp), L"%s%04x", Type, (UINTN) OptionNumber);
1343 DEBUG((EFI_D_ERROR,"Option = %s\n", StrTemp));
1344 OptionBuffer = BdsLibGetVariableAndSize (
1345 StrTemp,
1346 &gEfiGlobalVariableGuid,
1347 &OptionSize
1348 );
1349 if (NULL == OptionBuffer) {
1350 //
1351 // The Boot[OptionNumber] / Driver[OptionNumber] NOT occurs, we found it
1352 //
1353 break;
1354 }
1355 }
1356
1357 return OptionNumber;
1358 }
1359
1360 /**
1361
1362 Get the Option Number for Boot#### that does not used.
1363
1364 @return The available Option Number.
1365
1366 **/
1367 UINT16
1368 BOpt_GetBootOptionNumber (
1369 VOID
1370 )
1371 {
1372 return BOpt_GetOptionNumber (L"Boot");
1373 }
1374
1375 /**
1376
1377 Get the Option Number for Driver#### that does not used.
1378
1379 @return The unused Option Number.
1380
1381 **/
1382 UINT16
1383 BOpt_GetDriverOptionNumber (
1384 VOID
1385 )
1386 {
1387 return BOpt_GetOptionNumber (L"Driver");
1388 }
1389
1390 /**
1391
1392 Build up all DriverOptionMenu
1393
1394 @param CallbackData The BMM context data.
1395
1396 @retval EFI_SUCESS The functin completes successfully.
1397 @retval EFI_OUT_OF_RESOURCES Not enough memory to compete the operation.
1398 @retval EFI_NOT_FOUND Fail to get "DriverOrder" variable.
1399
1400 **/
1401 EFI_STATUS
1402 BOpt_GetDriverOptions (
1403 IN BMM_CALLBACK_DATA *CallbackData
1404 )
1405 {
1406 UINTN Index;
1407 UINT16 DriverString[12];
1408 UINT8 *LoadOptionFromVar;
1409 UINT8 *LoadOption;
1410 UINTN DriverOptionSize;
1411
1412 UINT16 *DriverOrderList;
1413 UINTN DriverOrderListSize;
1414 BM_MENU_ENTRY *NewMenuEntry;
1415 BM_LOAD_CONTEXT *NewLoadContext;
1416 UINT8 *LoadOptionPtr;
1417 UINTN StringSize;
1418 UINTN OptionalDataSize;
1419 UINT8 *LoadOptionEnd;
1420
1421 DriverOrderListSize = 0;
1422 DriverOrderList = NULL;
1423 DriverOptionSize = 0;
1424 LoadOptionFromVar = NULL;
1425 BOpt_FreeMenu (&DriverOptionMenu);
1426 InitializeListHead (&DriverOptionMenu.Head);
1427 //
1428 // Get the DriverOrder from the Var
1429 //
1430 DriverOrderList = BdsLibGetVariableAndSize (
1431 L"DriverOrder",
1432 &gEfiGlobalVariableGuid,
1433 &DriverOrderListSize
1434 );
1435 if (DriverOrderList == NULL) {
1436 return EFI_NOT_FOUND;
1437 }
1438
1439 for (Index = 0; Index < DriverOrderListSize / sizeof (UINT16); Index++) {
1440 UnicodeSPrint (
1441 DriverString,
1442 sizeof (DriverString),
1443 L"Driver%04x",
1444 DriverOrderList[Index]
1445 );
1446 //
1447 // Get all loadoptions from the VAR
1448 //
1449 LoadOptionFromVar = BdsLibGetVariableAndSize (
1450 DriverString,
1451 &gEfiGlobalVariableGuid,
1452 &DriverOptionSize
1453 );
1454 if (LoadOptionFromVar == NULL) {
1455 continue;
1456 }
1457
1458 LoadOption = AllocateZeroPool (DriverOptionSize);
1459 if (LoadOption == NULL) {
1460 continue;
1461 }
1462
1463 CopyMem (LoadOption, LoadOptionFromVar, DriverOptionSize);
1464 FreePool (LoadOptionFromVar);
1465
1466 NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT);
1467 if (NULL == NewMenuEntry) {
1468 return EFI_OUT_OF_RESOURCES;
1469 }
1470
1471 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;
1472 LoadOptionPtr = LoadOption;
1473 LoadOptionEnd = LoadOption + DriverOptionSize;
1474 NewMenuEntry->OptionNumber = DriverOrderList[Index];
1475 NewLoadContext->LoadOptionModified = FALSE;
1476 NewLoadContext->Deleted = FALSE;
1477 NewLoadContext->IsLegacy = FALSE;
1478
1479 //
1480 // LoadOption is a pointer type of UINT8
1481 // for easy use with following LOAD_OPTION
1482 // embedded in this struct
1483 //
1484 NewLoadContext->LoadOption = LoadOption;
1485 NewLoadContext->LoadOptionSize = DriverOptionSize;
1486
1487 NewLoadContext->Attributes = *(UINT32 *) LoadOptionPtr;
1488 NewLoadContext->IsActive = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_ACTIVE);
1489
1490 NewLoadContext->ForceReconnect = (BOOLEAN) (NewLoadContext->Attributes & LOAD_OPTION_FORCE_RECONNECT);
1491
1492 LoadOptionPtr += sizeof (UINT32);
1493
1494 NewLoadContext->FilePathListLength = *(UINT16 *) LoadOptionPtr;
1495 LoadOptionPtr += sizeof (UINT16);
1496
1497 StringSize = StrSize ((UINT16 *) LoadOptionPtr);
1498 NewLoadContext->Description = AllocateZeroPool (StringSize);
1499 ASSERT (NewLoadContext->Description != NULL);
1500 CopyMem (
1501 NewLoadContext->Description,
1502 (UINT16 *) LoadOptionPtr,
1503 StringSize
1504 );
1505 NewMenuEntry->DisplayString = NewLoadContext->Description;
1506
1507 LoadOptionPtr += StringSize;
1508
1509 NewLoadContext->FilePathList = AllocateZeroPool (NewLoadContext->FilePathListLength);
1510 ASSERT (NewLoadContext->FilePathList != NULL);
1511 CopyMem (
1512 NewLoadContext->FilePathList,
1513 (EFI_DEVICE_PATH_PROTOCOL *) LoadOptionPtr,
1514 NewLoadContext->FilePathListLength
1515 );
1516
1517 NewMenuEntry->HelpString = DevicePathToStr (NewLoadContext->FilePathList);
1518 NewMenuEntry->DisplayStringToken = GetStringTokenFromDepository (
1519 CallbackData,
1520 DriverOptionStrDepository
1521 );
1522 NewMenuEntry->HelpStringToken = GetStringTokenFromDepository (
1523 CallbackData,
1524 DriverOptionHelpStrDepository
1525 );
1526 LoadOptionPtr += NewLoadContext->FilePathListLength;
1527
1528 if (LoadOptionPtr < LoadOptionEnd) {
1529 OptionalDataSize = DriverOptionSize -
1530 sizeof (UINT32) -
1531 sizeof (UINT16) -
1532 StringSize -
1533 NewLoadContext->FilePathListLength;
1534
1535 NewLoadContext->OptionalData = AllocateZeroPool (OptionalDataSize);
1536 ASSERT (NewLoadContext->OptionalData != NULL);
1537 CopyMem (
1538 NewLoadContext->OptionalData,
1539 LoadOptionPtr,
1540 OptionalDataSize
1541 );
1542
1543 NewLoadContext->OptionalDataSize = OptionalDataSize;
1544 }
1545
1546 InsertTailList (&DriverOptionMenu.Head, &NewMenuEntry->Link);
1547
1548 }
1549
1550 if (DriverOrderList != NULL) {
1551 FreePool (DriverOrderList);
1552 }
1553 DriverOptionMenu.MenuNumber = Index;
1554 return EFI_SUCCESS;
1555
1556 }
1557
1558 /**
1559 Get option number according to Boot#### and BootOrder variable.
1560 The value is saved as #### + 1.
1561
1562 @param CallbackData The BMM context data.
1563 **/
1564 VOID
1565 GetBootOrder (
1566 IN BMM_CALLBACK_DATA *CallbackData
1567 )
1568 {
1569 BMM_FAKE_NV_DATA *BmmConfig;
1570 UINT16 Index;
1571 UINT16 OptionOrderIndex;
1572 UINTN DeviceType;
1573 BM_MENU_ENTRY *NewMenuEntry;
1574 BM_LOAD_CONTEXT *NewLoadContext;
1575
1576 ASSERT (CallbackData != NULL);
1577
1578 DeviceType = (UINTN) -1;
1579 BmmConfig = &CallbackData->BmmFakeNvData;
1580 ZeroMem (BmmConfig->BootOptionOrder, sizeof (BmmConfig->BootOptionOrder));
1581
1582 for (Index = 0, OptionOrderIndex = 0; ((Index < BootOptionMenu.MenuNumber) &&
1583 (OptionOrderIndex < (sizeof (BmmConfig->BootOptionOrder) / sizeof (BmmConfig->BootOptionOrder[0]))));
1584 Index++) {
1585 NewMenuEntry = BOpt_GetMenuEntry (&BootOptionMenu, Index);
1586 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;
1587
1588 if (NewLoadContext->IsLegacy) {
1589 if (((BBS_BBS_DEVICE_PATH *) NewLoadContext->FilePathList)->DeviceType != DeviceType) {
1590 DeviceType = ((BBS_BBS_DEVICE_PATH *) NewLoadContext->FilePathList)->DeviceType;
1591 } else {
1592 //
1593 // Only show one legacy boot option for the same device type
1594 // assuming the boot options are grouped by the device type
1595 //
1596 continue;
1597 }
1598 }
1599 BmmConfig->BootOptionOrder[OptionOrderIndex++] = (UINT32) (NewMenuEntry->OptionNumber + 1);
1600 }
1601 }
1602
1603 /**
1604 According to LegacyDevOrder variable to get legacy FD\HD\CD\NET\BEV
1605 devices list .
1606
1607 @param CallbackData The BMM context data.
1608 **/
1609 VOID
1610 GetLegacyDeviceOrder (
1611 IN BMM_CALLBACK_DATA *CallbackData
1612 )
1613 {
1614 UINTN Index;
1615 UINTN OptionIndex;
1616 UINT16 PageIdList[5];
1617 UINTN PageNum;
1618 UINTN VarSize;
1619 UINT8 *VarData;
1620 UINT8 *WorkingVarData;
1621 LEGACY_DEV_ORDER_ENTRY *DevOrder;
1622 UINT16 VarDevOrder;
1623 UINT8 *DisMap;
1624 BM_MENU_OPTION *OptionMenu;
1625 BBS_TYPE BbsType;
1626 UINT8 *LegacyOrder;
1627 UINT8 *OldData;
1628 UINTN Pos;
1629 UINTN Bit;
1630
1631 ASSERT (CallbackData != NULL);
1632
1633 PageIdList[0] = FORM_SET_FD_ORDER_ID;
1634 PageIdList[1] = FORM_SET_HD_ORDER_ID;
1635 PageIdList[2] = FORM_SET_CD_ORDER_ID;
1636 PageIdList[3] = FORM_SET_NET_ORDER_ID;
1637 PageIdList[4] = FORM_SET_BEV_ORDER_ID;
1638 OptionMenu = NULL;
1639 BbsType = 0;
1640 LegacyOrder = NULL;
1641 OldData = NULL;
1642 DisMap = ZeroMem (CallbackData->BmmFakeNvData.DisableMap, sizeof (CallbackData->BmmFakeNvData.DisableMap));
1643 PageNum = ARRAY_SIZE (PageIdList);
1644 VarData = BdsLibGetVariableAndSize (
1645 VAR_LEGACY_DEV_ORDER,
1646 &gEfiLegacyDevOrderVariableGuid,
1647 &VarSize
1648 );
1649
1650 for (Index = 0; Index < PageNum; Index++) {
1651 switch (PageIdList[Index]) {
1652
1653 case FORM_SET_FD_ORDER_ID:
1654 OptionMenu = (BM_MENU_OPTION *) &LegacyFDMenu;
1655 BbsType = BBS_FLOPPY;
1656 LegacyOrder = CallbackData->BmmFakeNvData.LegacyFD;
1657 OldData = CallbackData->BmmOldFakeNVData.LegacyFD;
1658 break;
1659
1660 case FORM_SET_HD_ORDER_ID:
1661 OptionMenu = (BM_MENU_OPTION *) &LegacyHDMenu;
1662 BbsType = BBS_HARDDISK;
1663 LegacyOrder = CallbackData->BmmFakeNvData.LegacyHD;
1664 OldData = CallbackData->BmmOldFakeNVData.LegacyHD;
1665 break;
1666
1667 case FORM_SET_CD_ORDER_ID:
1668 OptionMenu = (BM_MENU_OPTION *) &LegacyCDMenu;
1669 BbsType = BBS_CDROM;
1670 LegacyOrder = CallbackData->BmmFakeNvData.LegacyCD;
1671 OldData = CallbackData->BmmOldFakeNVData.LegacyCD;
1672 break;
1673
1674 case FORM_SET_NET_ORDER_ID:
1675 OptionMenu = (BM_MENU_OPTION *) &LegacyNETMenu;
1676 BbsType = BBS_EMBED_NETWORK;
1677 LegacyOrder = CallbackData->BmmFakeNvData.LegacyNET;
1678 OldData = CallbackData->BmmOldFakeNVData.LegacyNET;
1679 break;
1680
1681 default:
1682 ASSERT (PageIdList[Index] == FORM_SET_BEV_ORDER_ID);
1683 OptionMenu = (BM_MENU_OPTION *) &LegacyBEVMenu;
1684 BbsType = BBS_BEV_DEVICE;
1685 LegacyOrder = CallbackData->BmmFakeNvData.LegacyBEV;
1686 OldData = CallbackData->BmmOldFakeNVData.LegacyBEV;
1687 break;
1688 }
1689
1690 if (NULL != VarData) {
1691 WorkingVarData = VarData;
1692 DevOrder = (LEGACY_DEV_ORDER_ENTRY *) WorkingVarData;
1693 while (WorkingVarData < VarData + VarSize) {
1694 if (DevOrder->BbsType == BbsType) {
1695 break;
1696 }
1697
1698 WorkingVarData = (UINT8 *)((UINTN)WorkingVarData + sizeof (BBS_TYPE));
1699 WorkingVarData += *(UINT16 *) WorkingVarData;
1700 DevOrder = (LEGACY_DEV_ORDER_ENTRY *) WorkingVarData;
1701 }
1702 for (OptionIndex = 0; OptionIndex < OptionMenu->MenuNumber; OptionIndex++) {
1703 VarDevOrder = *(UINT16 *) ((UINTN) DevOrder + sizeof (BBS_TYPE) + sizeof (UINT16) + OptionIndex * sizeof (UINT16));
1704 if (0xFF00 == (VarDevOrder & 0xFF00)) {
1705 LegacyOrder[OptionIndex] = 0xFF;
1706 Pos = (VarDevOrder & 0xFF) / 8;
1707 Bit = 7 - ((VarDevOrder & 0xFF) % 8);
1708 DisMap[Pos] = (UINT8) (DisMap[Pos] | (UINT8) (1 << Bit));
1709 } else {
1710 LegacyOrder[OptionIndex] = (UINT8) (VarDevOrder & 0xFF);
1711 }
1712 }
1713 CopyMem (OldData, LegacyOrder, 100);
1714 }
1715 }
1716 }
1717
1718 /**
1719 Get driver option order from globalc DriverOptionMenu.
1720
1721 @param CallbackData The BMM context data.
1722
1723 **/
1724 VOID
1725 GetDriverOrder (
1726 IN BMM_CALLBACK_DATA *CallbackData
1727 )
1728 {
1729 BMM_FAKE_NV_DATA *BmmConfig;
1730 UINT16 Index;
1731 UINT16 OptionOrderIndex;
1732 UINTN DeviceType;
1733 BM_MENU_ENTRY *NewMenuEntry;
1734 BM_LOAD_CONTEXT *NewLoadContext;
1735
1736 ASSERT (CallbackData != NULL);
1737
1738 DeviceType = (UINTN) -1;
1739 BmmConfig = &CallbackData->BmmFakeNvData;
1740 ZeroMem (BmmConfig->DriverOptionOrder, sizeof (BmmConfig->DriverOptionOrder));
1741
1742 for (Index = 0, OptionOrderIndex = 0; ((Index < DriverOptionMenu.MenuNumber) &&
1743 (OptionOrderIndex < (sizeof (BmmConfig->DriverOptionOrder) / sizeof (BmmConfig->DriverOptionOrder[0]))));
1744 Index++) {
1745 NewMenuEntry = BOpt_GetMenuEntry (&DriverOptionMenu, Index);
1746 NewLoadContext = (BM_LOAD_CONTEXT *) NewMenuEntry->VariableContext;
1747
1748 if (NewLoadContext->IsLegacy) {
1749 if (((BBS_BBS_DEVICE_PATH *) NewLoadContext->FilePathList)->DeviceType != DeviceType) {
1750 DeviceType = ((BBS_BBS_DEVICE_PATH *) NewLoadContext->FilePathList)->DeviceType;
1751 } else {
1752 //
1753 // Only show one legacy boot option for the same device type
1754 // assuming the boot options are grouped by the device type
1755 //
1756 continue;
1757 }
1758 }
1759 BmmConfig->DriverOptionOrder[OptionOrderIndex++] = (UINT32) (NewMenuEntry->OptionNumber + 1);
1760 }
1761 }