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