]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigFileExplorer.c
1. Remove “Force clear PK” feature in AuthVarialbe driver.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / SecureBootConfigDxe / SecureBootConfigFileExplorer.c
CommitLineData
ecc722ad 1/** @file\r
2 Internal file explorer functions for SecureBoot configuration module.\r
3\r
4Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "SecureBootConfigImpl.h"\r
16\r
17///\r
18/// File system selection menu\r
19///\r
20SECUREBOOT_MENU_OPTION FsOptionMenu = {\r
21 SECUREBOOT_MENU_OPTION_SIGNATURE,\r
22 {NULL},\r
23 0\r
24};\r
25\r
26///\r
27/// Files and sub-directories in current directory menu\r
28///\r
29SECUREBOOT_MENU_OPTION DirectoryMenu = {\r
30 SECUREBOOT_MENU_OPTION_SIGNATURE,\r
31 {NULL},\r
32 0\r
33};\r
34\r
35VOID *mStartOpCodeHandle = NULL;\r
36VOID *mEndOpCodeHandle = NULL;\r
37EFI_IFR_GUID_LABEL *mStartLabel = NULL;\r
38EFI_IFR_GUID_LABEL *mEndLabel = NULL;\r
39\r
40/**\r
41 Duplicate a string.\r
42\r
43 @param[in] Src The source string.\r
44\r
45 @return A new string which is duplicated copy of the source,\r
46 or NULL if there is not enough memory.\r
47\r
48**/\r
49CHAR16 *\r
50StrDuplicate (\r
51 IN CHAR16 *Src\r
52 )\r
53{\r
54 CHAR16 *Dest;\r
55 UINTN Size;\r
56\r
57 Size = StrSize (Src);\r
58 Dest = AllocateZeroPool (Size);\r
59 ASSERT (Dest != NULL);\r
60 if (Dest != NULL) {\r
61 CopyMem (Dest, Src, Size);\r
62 }\r
63\r
64 return Dest;\r
65}\r
66\r
67/**\r
68 Helper function called as part of the code needed to allocate \r
69 the proper sized buffer for various EFI interfaces.\r
70\r
71 @param[in, out] Status Current status\r
72 @param[in, out] Buffer Current allocated buffer, or NULL\r
73 @param[in] BufferSize Current buffer size needed\r
74\r
75 @retval TRUE If the buffer was reallocated and the caller\r
76 should try the API again.\r
77 @retval FALSE The caller should not call this function again.\r
78\r
79**/\r
80BOOLEAN\r
81GrowBuffer (\r
82 IN OUT EFI_STATUS *Status,\r
83 IN OUT VOID **Buffer,\r
84 IN UINTN BufferSize\r
85 )\r
86{\r
87 BOOLEAN TryAgain;\r
88\r
89 //\r
90 // If this is an initial request, buffer will be null with a new buffer size\r
91 //\r
92 if ((*Buffer == NULL) && (BufferSize != 0)) {\r
93 *Status = EFI_BUFFER_TOO_SMALL;\r
94 }\r
95 //\r
96 // If the status code is "buffer too small", resize the buffer\r
97 //\r
98 TryAgain = FALSE;\r
99 if (*Status == EFI_BUFFER_TOO_SMALL) {\r
100\r
101 if (*Buffer != NULL) {\r
102 FreePool (*Buffer);\r
103 }\r
104\r
105 *Buffer = AllocateZeroPool (BufferSize);\r
106\r
107 if (*Buffer != NULL) {\r
108 TryAgain = TRUE;\r
109 } else {\r
110 *Status = EFI_OUT_OF_RESOURCES;\r
111 }\r
112 }\r
113 //\r
114 // If there's an error, free the buffer\r
115 //\r
116 if (!TryAgain && EFI_ERROR (*Status) && (*Buffer != NULL)) {\r
117 FreePool (*Buffer);\r
118 *Buffer = NULL;\r
119 }\r
120\r
121 return TryAgain;\r
122}\r
123\r
124/**\r
125 Append file name to existing file name, and allocate a new buffer \r
126 to hold the appended result.\r
127\r
128 @param[in] Str1 The existing file name\r
129 @param[in] Str2 The file name to be appended\r
130\r
131 @return A new string with appended result.\r
132\r
133**/\r
134CHAR16 *\r
135AppendFileName (\r
136 IN CHAR16 *Str1,\r
137 IN CHAR16 *Str2\r
138 )\r
139{\r
140 UINTN Size1;\r
141 UINTN Size2;\r
142 CHAR16 *Str;\r
143 CHAR16 *TmpStr;\r
144 CHAR16 *Ptr;\r
145 CHAR16 *LastSlash;\r
146\r
147 Size1 = StrSize (Str1);\r
148 Size2 = StrSize (Str2);\r
149 Str = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16));\r
150 ASSERT (Str != NULL);\r
151\r
152 TmpStr = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16)); \r
153 ASSERT (TmpStr != NULL);\r
154\r
155 StrCat (Str, Str1);\r
156 if (!((*Str == '\\') && (*(Str + 1) == 0))) {\r
157 StrCat (Str, L"\\");\r
158 }\r
159\r
160 StrCat (Str, Str2);\r
161\r
162 Ptr = Str;\r
163 LastSlash = Str;\r
164 while (*Ptr != 0) {\r
165 if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '.' && *(Ptr + 3) == L'\\') {\r
166 //\r
167 // Convert "\Name\..\" to "\"\r
168 // DO NOT convert the .. if it is at the end of the string. This will\r
169 // break the .. behavior in changing directories.\r
170 //\r
171\r
172 //\r
173 // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings \r
174 // that overlap.\r
175 //\r
176 StrCpy (TmpStr, Ptr + 3);\r
177 StrCpy (LastSlash, TmpStr);\r
178 Ptr = LastSlash;\r
179 } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {\r
180 //\r
181 // Convert a "\.\" to a "\"\r
182 //\r
183\r
184 //\r
185 // Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings \r
186 // that overlap.\r
187 //\r
188 StrCpy (TmpStr, Ptr + 2);\r
189 StrCpy (Ptr, TmpStr);\r
190 Ptr = LastSlash;\r
191 } else if (*Ptr == '\\') {\r
192 LastSlash = Ptr;\r
193 }\r
194\r
195 Ptr++;\r
196 }\r
197\r
198 FreePool (TmpStr);\r
199 \r
200 return Str;\r
201}\r
202\r
203/**\r
204 Create a SECUREBOOT_MENU_ENTRY, and stores it in a buffer allocated from the pool.\r
205\r
206 @return The new menu entry or NULL of error happens.\r
207\r
208**/\r
209SECUREBOOT_MENU_ENTRY *\r
210CreateMenuEntry (\r
211 VOID\r
212 )\r
213{\r
214 SECUREBOOT_MENU_ENTRY *MenuEntry;\r
215 UINTN ContextSize;\r
216\r
217 //\r
218 // Create new menu entry\r
219 //\r
220 MenuEntry = AllocateZeroPool (sizeof (SECUREBOOT_MENU_ENTRY));\r
221 if (MenuEntry == NULL) {\r
222 return NULL;\r
223 }\r
224\r
225 ContextSize = sizeof (SECUREBOOT_FILE_CONTEXT);\r
226 MenuEntry->FileContext = AllocateZeroPool (ContextSize);\r
227 if (MenuEntry->FileContext == NULL) {\r
228 FreePool (MenuEntry);\r
229 return NULL;\r
230 }\r
231\r
232 MenuEntry->Signature = SECUREBOOT_MENU_ENTRY_SIGNATURE;\r
233\r
234 return MenuEntry;\r
235}\r
236\r
237/**\r
238 Get Menu Entry from the Menu Entry List by MenuNumber.\r
239\r
240 If MenuNumber is great or equal to the number of Menu\r
241 Entry in the list, then ASSERT.\r
242\r
243 @param[in] MenuOption The Menu Entry List to read the menu entry.\r
244 @param[in] MenuNumber The index of Menu Entry.\r
245\r
246 @return The Menu Entry.\r
247\r
248**/\r
249SECUREBOOT_MENU_ENTRY *\r
250GetMenuEntry (\r
251 IN SECUREBOOT_MENU_OPTION *MenuOption,\r
252 IN UINTN MenuNumber\r
253 )\r
254{\r
255 SECUREBOOT_MENU_ENTRY *NewMenuEntry;\r
256 UINTN Index;\r
257 LIST_ENTRY *List;\r
258\r
259 ASSERT (MenuNumber < MenuOption->MenuNumber);\r
260\r
261 List = MenuOption->Head.ForwardLink;\r
262 for (Index = 0; Index < MenuNumber; Index++) {\r
263 List = List->ForwardLink;\r
264 }\r
265\r
266 NewMenuEntry = CR (List, SECUREBOOT_MENU_ENTRY, Link, SECUREBOOT_MENU_ENTRY_SIGNATURE);\r
267\r
268 return NewMenuEntry;\r
269}\r
270\r
271/**\r
272 Create string tokens for a menu from its help strings and display strings.\r
273\r
274 @param[in] HiiHandle Hii Handle of the package to be updated.\r
275 @param[in] MenuOption The Menu whose string tokens need to be created.\r
276\r
277**/\r
278VOID\r
279CreateMenuStringToken (\r
280 IN EFI_HII_HANDLE HiiHandle,\r
281 IN SECUREBOOT_MENU_OPTION *MenuOption\r
282 )\r
283{\r
284 SECUREBOOT_MENU_ENTRY *NewMenuEntry;\r
285 UINTN Index;\r
286\r
287 for (Index = 0; Index < MenuOption->MenuNumber; Index++) {\r
288 NewMenuEntry = GetMenuEntry (MenuOption, Index);\r
289\r
290 NewMenuEntry->DisplayStringToken = HiiSetString (\r
291 HiiHandle,\r
292 0,\r
293 NewMenuEntry->DisplayString,\r
294 NULL\r
295 );\r
296\r
297 if (NewMenuEntry->HelpString == NULL) {\r
298 NewMenuEntry->HelpStringToken = NewMenuEntry->DisplayStringToken;\r
299 } else {\r
300 NewMenuEntry->HelpStringToken = HiiSetString (\r
301 HiiHandle,\r
302 0,\r
303 NewMenuEntry->HelpString,\r
304 NULL\r
305 );\r
306 }\r
307 }\r
308}\r
309\r
310/**\r
311 Free up all resources allocated for a SECUREBOOT_MENU_ENTRY.\r
312\r
313 @param[in, out] MenuEntry A pointer to SECUREBOOT_MENU_ENTRY.\r
314\r
315**/\r
316VOID\r
317DestroyMenuEntry (\r
318 IN OUT SECUREBOOT_MENU_ENTRY *MenuEntry\r
319 )\r
320{\r
321 SECUREBOOT_FILE_CONTEXT *FileContext;\r
322\r
323\r
324 FileContext = (SECUREBOOT_FILE_CONTEXT *) MenuEntry->FileContext;\r
325\r
326 if (!FileContext->IsRoot) {\r
327 FreePool (FileContext->DevicePath);\r
328 } else {\r
329 if (FileContext->FHandle != NULL) {\r
330 FileContext->FHandle->Close (FileContext->FHandle);\r
331 }\r
332 }\r
333\r
334 if (FileContext->FileName != NULL) {\r
335 FreePool (FileContext->FileName);\r
336 }\r
337 if (FileContext->Info != NULL) {\r
338 FreePool (FileContext->Info);\r
339 }\r
340\r
341 FreePool (FileContext);\r
342\r
343 FreePool (MenuEntry->DisplayString);\r
344 if (MenuEntry->HelpString != NULL) {\r
345 FreePool (MenuEntry->HelpString);\r
346 }\r
347\r
348 FreePool (MenuEntry);\r
349}\r
350\r
351/**\r
352 Free resources allocated in Allocate Rountine.\r
353\r
354 @param[in, out] MenuOption Menu to be freed\r
355 \r
356**/\r
357VOID\r
358FreeMenu (\r
359 IN OUT SECUREBOOT_MENU_OPTION *MenuOption\r
360 )\r
361{\r
362 SECUREBOOT_MENU_ENTRY *MenuEntry;\r
363 while (!IsListEmpty (&MenuOption->Head)) {\r
364 MenuEntry = CR (\r
365 MenuOption->Head.ForwardLink,\r
366 SECUREBOOT_MENU_ENTRY,\r
367 Link,\r
368 SECUREBOOT_MENU_ENTRY_SIGNATURE\r
369 );\r
370 RemoveEntryList (&MenuEntry->Link);\r
371 DestroyMenuEntry (MenuEntry);\r
372 }\r
373 MenuOption->MenuNumber = 0;\r
374}\r
375\r
376/**\r
377 This function gets the file information from an open file descriptor, and stores it\r
378 in a buffer allocated from pool.\r
379\r
380 @param[in] FHand File Handle.\r
381\r
382 @return A pointer to a buffer with file information or NULL is returned\r
383\r
384**/\r
385EFI_FILE_INFO *\r
386FileInfo (\r
387 IN EFI_FILE_HANDLE FHand\r
388 )\r
389{\r
390 EFI_STATUS Status;\r
391 EFI_FILE_INFO *Buffer;\r
392 UINTN BufferSize;\r
393\r
394 //\r
395 // Initialize for GrowBuffer loop\r
396 //\r
397 Buffer = NULL;\r
398 BufferSize = SIZE_OF_EFI_FILE_INFO + 200;\r
399\r
400 //\r
401 // Call the real function\r
402 //\r
403 while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {\r
404 Status = FHand->GetInfo (\r
405 FHand,\r
406 &gEfiFileInfoGuid,\r
407 &BufferSize,\r
408 Buffer\r
409 );\r
410 }\r
411\r
412 return Buffer;\r
413}\r
414\r
415/**\r
416 This function gets the file system information from an open file descriptor,\r
417 and stores it in a buffer allocated from pool.\r
418\r
419 @param[in] FHand The file handle.\r
420\r
421 @return A pointer to a buffer with file information.\r
422 @retval NULL is returned if failed to get Vaolume Label Info.\r
423\r
424**/\r
425EFI_FILE_SYSTEM_VOLUME_LABEL *\r
426FileSystemVolumeLabelInfo (\r
427 IN EFI_FILE_HANDLE FHand\r
428 )\r
429{\r
430 EFI_STATUS Status;\r
431 EFI_FILE_SYSTEM_VOLUME_LABEL *Buffer;\r
432 UINTN BufferSize;\r
433 //\r
434 // Initialize for GrowBuffer loop\r
435 //\r
436 Buffer = NULL;\r
437 BufferSize = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL + 200;\r
438\r
439 //\r
440 // Call the real function\r
441 //\r
442 while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {\r
443 Status = FHand->GetInfo (\r
444 FHand,\r
445 &gEfiFileSystemVolumeLabelInfoIdGuid,\r
446 &BufferSize,\r
447 Buffer\r
448 );\r
449 }\r
450\r
451 return Buffer;\r
452}\r
453\r
454/**\r
455 This function will open a file or directory referenced by DevicePath.\r
456\r
457 This function opens a file with the open mode according to the file path. The\r
458 Attributes is valid only for EFI_FILE_MODE_CREATE.\r
459\r
460 @param[in, out] FilePath On input, the device path to the file. \r
461 On output, the remaining device path.\r
462 @param[out] FileHandle Pointer to the file handle.\r
463 @param[in] OpenMode The mode to open the file with.\r
464 @param[in] Attributes The file's file attributes.\r
465\r
466 @retval EFI_SUCCESS The information was set.\r
467 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
468 @retval EFI_UNSUPPORTED Could not open the file path.\r
469 @retval EFI_NOT_FOUND The specified file could not be found on the\r
470 device or the file system could not be found on\r
471 the device.\r
472 @retval EFI_NO_MEDIA The device has no medium.\r
473 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the\r
474 medium is no longer supported.\r
475 @retval EFI_DEVICE_ERROR The device reported an error.\r
476 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
477 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
478 @retval EFI_ACCESS_DENIED The file was opened read only.\r
479 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the\r
480 file.\r
481 @retval EFI_VOLUME_FULL The volume is full.\r
482**/\r
483EFI_STATUS\r
484EFIAPI\r
485OpenFileByDevicePath(\r
486 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
487 OUT EFI_FILE_HANDLE *FileHandle,\r
488 IN UINT64 OpenMode,\r
489 IN UINT64 Attributes\r
490 )\r
491{\r
492 EFI_STATUS Status;\r
493 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r
494 EFI_FILE_PROTOCOL *Handle1;\r
495 EFI_FILE_PROTOCOL *Handle2;\r
496 EFI_HANDLE DeviceHandle; \r
497\r
498 if ((FilePath == NULL || FileHandle == NULL)) {\r
499 return EFI_INVALID_PARAMETER;\r
500 }\r
501\r
502 Status = gBS->LocateDevicePath (\r
503 &gEfiSimpleFileSystemProtocolGuid,\r
504 FilePath,\r
505 &DeviceHandle\r
506 );\r
507 if (EFI_ERROR (Status)) {\r
508 return Status;\r
509 }\r
510\r
511 Status = gBS->OpenProtocol(\r
512 DeviceHandle,\r
513 &gEfiSimpleFileSystemProtocolGuid,\r
514 (VOID**)&EfiSimpleFileSystemProtocol,\r
515 gImageHandle,\r
516 NULL,\r
517 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
518 );\r
519 if (EFI_ERROR (Status)) {\r
520 return Status;\r
521 }\r
522\r
523 Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);\r
524 if (EFI_ERROR (Status)) {\r
525 FileHandle = NULL;\r
526 return Status;\r
527 }\r
528\r
529 //\r
530 // go down directories one node at a time.\r
531 //\r
532 while (!IsDevicePathEnd (*FilePath)) {\r
533 //\r
534 // For file system access each node should be a file path component\r
535 //\r
536 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
537 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
538 ) {\r
539 FileHandle = NULL;\r
540 return (EFI_INVALID_PARAMETER);\r
541 }\r
542 //\r
543 // Open this file path node\r
544 //\r
545 Handle2 = Handle1;\r
546 Handle1 = NULL;\r
547\r
548 //\r
549 // Try to test opening an existing file\r
550 //\r
551 Status = Handle2->Open (\r
552 Handle2,\r
553 &Handle1,\r
554 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
555 OpenMode &~EFI_FILE_MODE_CREATE,\r
556 0\r
557 );\r
558\r
559 //\r
560 // see if the error was that it needs to be created\r
561 //\r
562 if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r
563 Status = Handle2->Open (\r
564 Handle2,\r
565 &Handle1,\r
566 ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
567 OpenMode,\r
568 Attributes\r
569 );\r
570 }\r
571 //\r
572 // Close the last node\r
573 //\r
574 Handle2->Close (Handle2);\r
575\r
576 if (EFI_ERROR(Status)) {\r
577 return (Status);\r
578 }\r
579\r
580 //\r
581 // Get the next node\r
582 //\r
583 *FilePath = NextDevicePathNode (*FilePath);\r
584 }\r
585\r
586 //\r
587 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!\r
588 //\r
589 *FileHandle = (VOID*)Handle1;\r
590 return EFI_SUCCESS;\r
591}\r
592\r
593/**\r
594 Function opens and returns a file handle to the root directory of a volume.\r
595\r
596 @param[in] DeviceHandle A handle for a device\r
597\r
598 @return A valid file handle or NULL if error happens.\r
599\r
600**/\r
601EFI_FILE_HANDLE\r
602OpenRoot (\r
603 IN EFI_HANDLE DeviceHandle\r
604 )\r
605{\r
606 EFI_STATUS Status;\r
607 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
608 EFI_FILE_HANDLE File;\r
609\r
610 File = NULL;\r
611\r
612 //\r
613 // File the file system interface to the device\r
614 //\r
615 Status = gBS->HandleProtocol (\r
616 DeviceHandle,\r
617 &gEfiSimpleFileSystemProtocolGuid,\r
618 (VOID *) &Volume\r
619 );\r
620\r
621 //\r
622 // Open the root directory of the volume\r
623 //\r
624 if (!EFI_ERROR (Status)) {\r
625 Status = Volume->OpenVolume (\r
626 Volume,\r
627 &File\r
628 );\r
629 }\r
630 //\r
631 // Done\r
632 //\r
633 return EFI_ERROR (Status) ? NULL : File;\r
634}\r
635\r
636/**\r
637 This function builds the FsOptionMenu list which records all\r
638 available file system in the system. They include all instances\r
639 of EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, all instances of EFI_LOAD_FILE_SYSTEM\r
640 and all type of legacy boot device.\r
641\r
642 @retval EFI_SUCCESS Success find the file system\r
643 @retval EFI_OUT_OF_RESOURCES Can not create menu entry\r
644\r
645**/\r
646EFI_STATUS\r
647FindFileSystem (\r
648 VOID\r
649 )\r
650{\r
651 UINTN NoBlkIoHandles;\r
652 UINTN NoSimpleFsHandles;\r
653 UINTN NoLoadFileHandles;\r
654 EFI_HANDLE *BlkIoHandle;\r
655 EFI_HANDLE *SimpleFsHandle;\r
656 UINT16 *VolumeLabel;\r
657 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
658 UINTN Index;\r
659 EFI_STATUS Status;\r
660 SECUREBOOT_MENU_ENTRY *MenuEntry;\r
661 SECUREBOOT_FILE_CONTEXT *FileContext;\r
662 UINT16 *TempStr;\r
663 UINTN OptionNumber;\r
664 VOID *Buffer;\r
665\r
666 BOOLEAN RemovableMedia;\r
667\r
668\r
669 NoSimpleFsHandles = 0;\r
670 NoLoadFileHandles = 0;\r
671 OptionNumber = 0;\r
672 InitializeListHead (&FsOptionMenu.Head);\r
673\r
674 //\r
675 // Locate Handles that support BlockIo protocol\r
676 //\r
677 Status = gBS->LocateHandleBuffer (\r
678 ByProtocol,\r
679 &gEfiBlockIoProtocolGuid,\r
680 NULL,\r
681 &NoBlkIoHandles,\r
682 &BlkIoHandle\r
683 );\r
684 if (!EFI_ERROR (Status)) {\r
685\r
686 for (Index = 0; Index < NoBlkIoHandles; Index++) {\r
687 Status = gBS->HandleProtocol (\r
688 BlkIoHandle[Index],\r
689 &gEfiBlockIoProtocolGuid,\r
690 (VOID **) &BlkIo\r
691 );\r
692\r
693 if (EFI_ERROR (Status)) {\r
694 continue;\r
695 }\r
696\r
697 //\r
698 // Issue a dummy read to trigger reinstall of BlockIo protocol for removable media\r
699 //\r
700 if (BlkIo->Media->RemovableMedia) {\r
701 Buffer = AllocateZeroPool (BlkIo->Media->BlockSize);\r
702 if (NULL == Buffer) {\r
703 FreePool (BlkIoHandle);\r
704 return EFI_OUT_OF_RESOURCES;\r
705 }\r
706\r
707 BlkIo->ReadBlocks (\r
708 BlkIo,\r
709 BlkIo->Media->MediaId,\r
710 0,\r
711 BlkIo->Media->BlockSize,\r
712 Buffer\r
713 );\r
714 FreePool (Buffer);\r
715 }\r
716 }\r
717 FreePool (BlkIoHandle);\r
718 }\r
719\r
720 //\r
721 // Locate Handles that support Simple File System protocol\r
722 //\r
723 Status = gBS->LocateHandleBuffer (\r
724 ByProtocol,\r
725 &gEfiSimpleFileSystemProtocolGuid,\r
726 NULL,\r
727 &NoSimpleFsHandles,\r
728 &SimpleFsHandle\r
729 );\r
730 if (!EFI_ERROR (Status)) {\r
731 //\r
732 // Find all the instances of the File System prototocol\r
733 //\r
734 for (Index = 0; Index < NoSimpleFsHandles; Index++) {\r
735 Status = gBS->HandleProtocol (\r
736 SimpleFsHandle[Index],\r
737 &gEfiBlockIoProtocolGuid,\r
738 (VOID **) &BlkIo\r
739 );\r
740 if (EFI_ERROR (Status)) {\r
741 //\r
742 // If no block IO exists assume it's NOT a removable media\r
743 //\r
744 RemovableMedia = FALSE;\r
745 } else {\r
746 //\r
747 // If block IO exists check to see if it's remobable media\r
748 //\r
749 RemovableMedia = BlkIo->Media->RemovableMedia;\r
750 }\r
751\r
752 //\r
753 // Allocate pool for this instance.\r
754 //\r
755 MenuEntry = CreateMenuEntry ();\r
756 if (NULL == MenuEntry) {\r
757 FreePool (SimpleFsHandle);\r
758 return EFI_OUT_OF_RESOURCES;\r
759 }\r
760\r
761 FileContext = (SECUREBOOT_FILE_CONTEXT *) MenuEntry->FileContext;\r
762\r
763 FileContext->Handle = SimpleFsHandle[Index];\r
764 MenuEntry->OptionNumber = Index;\r
765 FileContext->FHandle = OpenRoot (FileContext->Handle);\r
766 if (FileContext->FHandle == NULL) {\r
767 DestroyMenuEntry (MenuEntry);\r
768 continue;\r
769 }\r
770\r
771 MenuEntry->HelpString = DevicePathToStr (DevicePathFromHandle (FileContext->Handle));\r
772 FileContext->Info = FileSystemVolumeLabelInfo (FileContext->FHandle);\r
773 FileContext->FileName = StrDuplicate (L"\\");\r
774 FileContext->DevicePath = FileDevicePath (\r
775 FileContext->Handle,\r
776 FileContext->FileName\r
777 );\r
778 FileContext->IsDir = TRUE;\r
779 FileContext->IsRoot = TRUE;\r
780 FileContext->IsRemovableMedia = RemovableMedia;\r
781 FileContext->IsLoadFile = FALSE;\r
782\r
783 //\r
784 // Get current file system's Volume Label\r
785 //\r
786 if (FileContext->Info == NULL) {\r
787 VolumeLabel = L"NO FILE SYSTEM INFO";\r
788 } else {\r
789 if (FileContext->Info->VolumeLabel == NULL) {\r
790 VolumeLabel = L"NULL VOLUME LABEL";\r
791 } else {\r
792 VolumeLabel = FileContext->Info->VolumeLabel;\r
793 if (*VolumeLabel == 0x0000) {\r
794 VolumeLabel = L"NO VOLUME LABEL";\r
795 }\r
796 }\r
797 }\r
798\r
799 TempStr = MenuEntry->HelpString;\r
800 MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);\r
801 ASSERT (MenuEntry->DisplayString != NULL);\r
802 UnicodeSPrint (\r
803 MenuEntry->DisplayString,\r
804 MAX_CHAR,\r
805 L"%s, [%s]",\r
806 VolumeLabel,\r
807 TempStr\r
808 );\r
809 OptionNumber++;\r
810 InsertTailList (&FsOptionMenu.Head, &MenuEntry->Link);\r
811 }\r
812 }\r
813\r
814 if (NoSimpleFsHandles != 0) {\r
815 FreePool (SimpleFsHandle);\r
816 }\r
817 \r
818 //\r
819 // Remember how many file system options are here\r
820 //\r
821 FsOptionMenu.MenuNumber = OptionNumber;\r
822 return EFI_SUCCESS;\r
823}\r
824\r
825\r
826/**\r
827 Find files under the current directory. All files and sub-directories \r
828 in current directory will be stored in DirectoryMenu for future use.\r
829\r
830 @param[in] MenuEntry The Menu Entry.\r
831\r
832 @retval EFI_SUCCESS Get files from current dir successfully.\r
833 @return Other Can't get files from current dir.\r
834\r
835**/\r
836EFI_STATUS\r
837FindFiles (\r
838 IN SECUREBOOT_MENU_ENTRY *MenuEntry\r
839 )\r
840{\r
841 EFI_FILE_HANDLE NewDir;\r
842 EFI_FILE_HANDLE Dir;\r
843 EFI_FILE_INFO *DirInfo;\r
844 UINTN BufferSize;\r
845 UINTN DirBufferSize;\r
846 SECUREBOOT_MENU_ENTRY *NewMenuEntry;\r
847 SECUREBOOT_FILE_CONTEXT *FileContext;\r
848 SECUREBOOT_FILE_CONTEXT *NewFileContext;\r
849 UINTN Pass;\r
850 EFI_STATUS Status;\r
851 UINTN OptionNumber;\r
852\r
853 FileContext = (SECUREBOOT_FILE_CONTEXT *) MenuEntry->FileContext;\r
854 Dir = FileContext->FHandle;\r
855 OptionNumber = 0;\r
856 //\r
857 // Open current directory to get files from it\r
858 //\r
859 Status = Dir->Open (\r
860 Dir,\r
861 &NewDir,\r
862 FileContext->FileName,\r
863 EFI_FILE_READ_ONLY,\r
864 0\r
865 );\r
866 if (!FileContext->IsRoot) {\r
867 Dir->Close (Dir);\r
868 }\r
869\r
870 if (EFI_ERROR (Status)) {\r
871 return Status;\r
872 }\r
873\r
874 DirInfo = FileInfo (NewDir);\r
875 if (DirInfo == NULL) {\r
876 return EFI_NOT_FOUND;\r
877 }\r
878\r
879 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r
880 return EFI_INVALID_PARAMETER;\r
881 }\r
882\r
883 FileContext->DevicePath = FileDevicePath (\r
884 FileContext->Handle,\r
885 FileContext->FileName\r
886 );\r
887\r
888 DirBufferSize = sizeof (EFI_FILE_INFO) + 1024;\r
889 DirInfo = AllocateZeroPool (DirBufferSize);\r
890 if (DirInfo == NULL) {\r
891 return EFI_OUT_OF_RESOURCES;\r
892 }\r
893 \r
894 //\r
895 // Get all files in current directory\r
896 // Pass 1 to get Directories\r
897 // Pass 2 to get files that are EFI images\r
898 //\r
899 for (Pass = 1; Pass <= 2; Pass++) {\r
900 NewDir->SetPosition (NewDir, 0);\r
901 for (;;) {\r
902 BufferSize = DirBufferSize;\r
903 Status = NewDir->Read (NewDir, &BufferSize, DirInfo);\r
904 if (EFI_ERROR (Status) || BufferSize == 0) {\r
905 break;\r
906 }\r
907\r
908 if (((DirInfo->Attribute & EFI_FILE_DIRECTORY) != 0 && Pass == 2) ||\r
909 ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0 && Pass == 1)\r
910 ) {\r
911 //\r
912 // Pass 1 is for Directories\r
913 // Pass 2 is for file names\r
914 //\r
915 continue;\r
916 }\r
917\r
918 NewMenuEntry = CreateMenuEntry ();\r
919 if (NULL == NewMenuEntry) {\r
920 return EFI_OUT_OF_RESOURCES;\r
921 }\r
922\r
923 NewFileContext = (SECUREBOOT_FILE_CONTEXT *) NewMenuEntry->FileContext;\r
924 NewFileContext->Handle = FileContext->Handle;\r
925 NewFileContext->FileName = AppendFileName (\r
926 FileContext->FileName,\r
927 DirInfo->FileName\r
928 );\r
929 NewFileContext->FHandle = NewDir;\r
930 NewFileContext->DevicePath = FileDevicePath (\r
931 NewFileContext->Handle,\r
932 NewFileContext->FileName\r
933 );\r
934 NewMenuEntry->HelpString = NULL;\r
935 \r
936 NewFileContext->IsDir = (BOOLEAN) ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY);\r
937 if (NewFileContext->IsDir) {\r
938 BufferSize = StrLen (DirInfo->FileName) * 2 + 6;\r
939 NewMenuEntry->DisplayString = AllocateZeroPool (BufferSize);\r
940\r
941 UnicodeSPrint (\r
942 NewMenuEntry->DisplayString,\r
943 BufferSize,\r
944 L"<%s>",\r
945 DirInfo->FileName\r
946 );\r
947\r
948 } else {\r
949 NewMenuEntry->DisplayString = StrDuplicate (DirInfo->FileName);\r
950 }\r
951\r
952 NewFileContext->IsRoot = FALSE;\r
953 NewFileContext->IsLoadFile = FALSE;\r
954 NewFileContext->IsRemovableMedia = FALSE;\r
955\r
956 NewMenuEntry->OptionNumber = OptionNumber;\r
957 OptionNumber++;\r
958 InsertTailList (&DirectoryMenu.Head, &NewMenuEntry->Link);\r
959 }\r
960 }\r
961\r
962 DirectoryMenu.MenuNumber = OptionNumber;\r
963 FreePool (DirInfo);\r
964 return EFI_SUCCESS;\r
965}\r
966\r
967/**\r
968 Refresh the global UpdateData structure.\r
969\r
970**/\r
971VOID\r
972RefreshUpdateData (\r
973 VOID\r
974 )\r
975{\r
976 //\r
977 // Free current updated date\r
978 // \r
979 if (mStartOpCodeHandle != NULL) {\r
980 HiiFreeOpCodeHandle (mStartOpCodeHandle);\r
981 }\r
982\r
983 //\r
984 // Create new OpCode Handle\r
985 //\r
986 mStartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
987\r
988 //\r
989 // Create Hii Extend Label OpCode as the start opcode\r
990 //\r
991 mStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (\r
992 mStartOpCodeHandle,\r
993 &gEfiIfrTianoGuid,\r
994 NULL,\r
995 sizeof (EFI_IFR_GUID_LABEL)\r
996 );\r
997 mStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
998}\r
999\r
1000/**\r
1001 Update the File Explore page.\r
1002\r
1003 @param[in] HiiHandle Hii Handle of the package to be updated.\r
1004 @param[in] MenuOption The Menu whose string tokens need to be updated.\r
1005 @param[in] FeCurrentState Current file explorer state.\r
1006\r
1007**/\r
1008VOID\r
1009UpdateFileExplorePage (\r
1010 IN EFI_HII_HANDLE HiiHandle,\r
1011 IN SECUREBOOT_MENU_OPTION *MenuOption,\r
1012 IN FILE_EXPLORER_STATE FeCurrentState\r
1013 )\r
1014{\r
1015 UINTN Index;\r
1016 SECUREBOOT_MENU_ENTRY *NewMenuEntry;\r
1017 SECUREBOOT_FILE_CONTEXT *NewFileContext;\r
1018 EFI_FORM_ID FormId;\r
1019 EFI_FORM_ID FileFormId;\r
1020\r
1021 if (FeCurrentState == FileExplorerStateEnrollPkFile) {\r
1022 FormId = SECUREBOOT_ADD_PK_FILE_FORM_ID;\r
1023 FileFormId = FORM_FILE_EXPLORER_ID_PK;\r
1024 } else if (FeCurrentState == FileExplorerStateEnrollKekFile) {\r
1025 FormId = FORMID_ENROLL_KEK_FORM;\r
1026 FileFormId = FORM_FILE_EXPLORER_ID_KEK;\r
1027 } else if (FeCurrentState == FileExplorerStateEnrollSignatureFileToDb) {\r
1028 FormId = SECUREBOOT_ENROLL_SIGNATURE_TO_DB;\r
1029 FileFormId = FORM_FILE_EXPLORER_ID_DB;\r
1030 } else if (FeCurrentState == FileExplorerStateEnrollSignatureFileToDbx) {\r
1031 FormId = SECUREBOOT_ENROLL_SIGNATURE_TO_DBX;\r
1032 FileFormId = FORM_FILE_EXPLORER_ID_DBX;\r
1033 } else {\r
1034 return;\r
1035 }\r
1036\r
1037 NewMenuEntry = NULL;\r
1038 NewFileContext = NULL;\r
1039\r
1040 RefreshUpdateData ();\r
1041 mStartLabel->Number = FORM_FILE_EXPLORER_ID;\r
1042\r
1043 for (Index = 0; Index < MenuOption->MenuNumber; Index++) {\r
1044 NewMenuEntry = GetMenuEntry (MenuOption, Index);\r
1045 NewFileContext = (SECUREBOOT_FILE_CONTEXT *) NewMenuEntry->FileContext;\r
1046\r
1047 if (NewFileContext->IsDir) {\r
1048 //\r
1049 // Create Text opcode for directory.\r
1050 //\r
1051 HiiCreateActionOpCode (\r
1052 mStartOpCodeHandle,\r
1053 (UINT16) (FILE_OPTION_OFFSET + Index),\r
1054 NewMenuEntry->DisplayStringToken,\r
1055 STRING_TOKEN (STR_NULL),\r
1056 EFI_IFR_FLAG_CALLBACK,\r
1057 0\r
1058 );\r
1059 } else {\r
1060\r
1061 //\r
1062 // Create Goto opcode for file.\r
1063 //\r
1064 HiiCreateGotoOpCode (\r
1065 mStartOpCodeHandle,\r
1066 FormId,\r
1067 NewMenuEntry->DisplayStringToken,\r
1068 STRING_TOKEN (STR_NULL),\r
1069 EFI_IFR_FLAG_CALLBACK,\r
1070 (UINT16) (FILE_OPTION_OFFSET + Index)\r
1071 );\r
1072 }\r
1073 }\r
1074\r
1075 HiiUpdateForm (\r
1076 HiiHandle,\r
1077 &gSecureBootConfigFormSetGuid,\r
1078 FileFormId,\r
1079 mStartOpCodeHandle, // Label FORM_FILE_EXPLORER_ID\r
1080 mEndOpCodeHandle // LABEL_END\r
1081 );\r
1082}\r
1083\r
1084/**\r
1085 Update the file explorer page with the refreshed file system.\r
1086\r
1087 @param[in] PrivateData Module private data.\r
1088 @param[in] KeyValue Key value to identify the type of data to expect.\r
1089\r
1090 @retval TRUE Inform the caller to create a callback packet to exit file explorer.\r
1091 @retval FALSE Indicate that there is no need to exit file explorer.\r
1092\r
1093**/\r
1094BOOLEAN\r
1095UpdateFileExplorer (\r
1096 IN SECUREBOOT_CONFIG_PRIVATE_DATA *PrivateData,\r
1097 IN UINT16 KeyValue\r
1098 )\r
1099{\r
1100 UINT16 FileOptionMask;\r
1101 SECUREBOOT_MENU_ENTRY *NewMenuEntry;\r
1102 SECUREBOOT_FILE_CONTEXT *NewFileContext;\r
1103 EFI_FORM_ID FormId;\r
1104 BOOLEAN ExitFileExplorer;\r
1105 EFI_STATUS Status;\r
1106 EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath;\r
1107\r
1108 NewMenuEntry = NULL;\r
1109 NewFileContext = NULL;\r
1110 ExitFileExplorer = FALSE;\r
1111 FileOptionMask = (UINT16) (FILE_OPTION_MASK & KeyValue);\r
1112\r
1113 if (PrivateData->FeDisplayContext == FileExplorerDisplayUnknown) {\r
1114 //\r
1115 // First in, display file system.\r
1116 //\r
1117 FreeMenu (&FsOptionMenu);\r
1118 FindFileSystem ();\r
1119 \r
1120 CreateMenuStringToken (PrivateData->HiiHandle, &FsOptionMenu);\r
1121 UpdateFileExplorePage (PrivateData->HiiHandle, &FsOptionMenu, PrivateData->FeCurrentState);\r
1122\r
1123 PrivateData->FeDisplayContext = FileExplorerDisplayFileSystem;\r
1124 } else {\r
1125 if (PrivateData->FeDisplayContext == FileExplorerDisplayFileSystem) {\r
1126 NewMenuEntry = GetMenuEntry (&FsOptionMenu, FileOptionMask);\r
1127 } else if (PrivateData->FeDisplayContext == FileExplorerDisplayDirectory) {\r
1128 NewMenuEntry = GetMenuEntry (&DirectoryMenu, FileOptionMask);\r
1129 }\r
1130\r
1131 NewFileContext = (SECUREBOOT_FILE_CONTEXT *) NewMenuEntry->FileContext;\r
1132\r
1133 if (NewFileContext->IsDir ) {\r
1134 PrivateData->FeDisplayContext = FileExplorerDisplayDirectory;\r
1135\r
1136 RemoveEntryList (&NewMenuEntry->Link);\r
1137 FreeMenu (&DirectoryMenu);\r
1138 Status = FindFiles (NewMenuEntry);\r
1139 if (EFI_ERROR (Status)) {\r
1140 ExitFileExplorer = TRUE;\r
1141 goto OnExit;\r
1142 }\r
1143 CreateMenuStringToken (PrivateData->HiiHandle, &DirectoryMenu);\r
1144 DestroyMenuEntry (NewMenuEntry);\r
1145\r
1146 UpdateFileExplorePage (PrivateData->HiiHandle, &DirectoryMenu, PrivateData->FeCurrentState);\r
1147\r
1148 } else {\r
1149 if (PrivateData->FeCurrentState == FileExplorerStateEnrollPkFile) {\r
1150 FormId = SECUREBOOT_ADD_PK_FILE_FORM_ID;\r
1151 } else if (PrivateData->FeCurrentState == FileExplorerStateEnrollKekFile) {\r
1152 FormId = FORMID_ENROLL_KEK_FORM;\r
1153 } else if (PrivateData->FeCurrentState == FileExplorerStateEnrollSignatureFileToDb) {\r
1154 FormId = SECUREBOOT_ENROLL_SIGNATURE_TO_DB;\r
1155 } else if (PrivateData->FeCurrentState == FileExplorerStateEnrollSignatureFileToDbx) {\r
1156 FormId = SECUREBOOT_ENROLL_SIGNATURE_TO_DBX;\r
1157 } else {\r
1158 return FALSE;\r
1159 }\r
1160\r
1161 PrivateData->MenuEntry = NewMenuEntry;\r
1162 PrivateData->FileContext->FileName = NewFileContext->FileName;\r
1163 \r
1164 TmpDevicePath = NewFileContext->DevicePath;\r
1165 OpenFileByDevicePath (\r
1166 &TmpDevicePath,\r
1167 &PrivateData->FileContext->FHandle,\r
1168 EFI_FILE_MODE_READ,\r
1169 0\r
1170 );\r
1171\r
1172 //\r
1173 // Create Subtitle op-code for the display string of the option.\r
1174 //\r
1175 RefreshUpdateData ();\r
1176 mStartLabel->Number = FormId;\r
1177\r
1178 HiiCreateSubTitleOpCode (\r
1179 mStartOpCodeHandle,\r
1180 NewMenuEntry->DisplayStringToken,\r
1181 0,\r
1182 0,\r
1183 0\r
1184 );\r
1185\r
1186 HiiUpdateForm (\r
1187 PrivateData->HiiHandle,\r
1188 &gSecureBootConfigFormSetGuid,\r
1189 FormId,\r
1190 mStartOpCodeHandle, // Label FormId\r
1191 mEndOpCodeHandle // LABEL_END\r
1192 );\r
1193 }\r
1194 }\r
1195\r
1196OnExit:\r
1197 return ExitFileExplorer;\r
1198}\r
1199\r
1200/**\r
1201 Clean up the dynamic opcode at label and form specified by both LabelId. \r
1202\r
1203 @param[in] LabelId It is both the Form ID and Label ID for opcode deletion.\r
1204 @param[in] PrivateData Module private data.\r
1205\r
1206**/\r
1207VOID\r
1208CleanUpPage (\r
1209 IN UINT16 LabelId,\r
1210 IN SECUREBOOT_CONFIG_PRIVATE_DATA *PrivateData\r
1211 )\r
1212{\r
1213 RefreshUpdateData ();\r
1214\r
1215 //\r
1216 // Remove all op-codes from dynamic page\r
1217 //\r
1218 mStartLabel->Number = LabelId;\r
1219 HiiUpdateForm (\r
1220 PrivateData->HiiHandle,\r
1221 &gSecureBootConfigFormSetGuid,\r
1222 LabelId,\r
1223 mStartOpCodeHandle, // Label LabelId\r
1224 mEndOpCodeHandle // LABEL_END\r
1225 );\r
1226}\r
1227\r