]>
Commit | Line | Data |
---|---|---|
4c8274a0 ED |
1 | /** @file\r |
2 | File explorer related functions.\r | |
3 | \r | |
20fe1625 | 4 | Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r |
4c8274a0 ED |
5 | This program and the accompanying materials are licensed and made available under\r |
6 | the terms and conditions of the BSD License that accompanies this distribution.\r | |
7 | The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | \r | |
16 | #include "FileExplorer.h"\r | |
17 | \r | |
18 | EFI_GUID FileExplorerGuid = EFI_FILE_EXPLORE_FORMSET_GUID;\r | |
19 | \r | |
20 | ///\r | |
21 | /// File system selection menu\r | |
22 | ///\r | |
23 | MENU_OPTION mFsOptionMenu = {\r | |
24 | MENU_OPTION_SIGNATURE,\r | |
25 | {NULL},\r | |
26 | 0,\r | |
27 | FALSE\r | |
28 | };\r | |
29 | \r | |
30 | FILE_EXPLORER_CALLBACK_DATA gFileExplorerPrivate = {\r | |
31 | FILE_EXPLORER_CALLBACK_DATA_SIGNATURE,\r | |
32 | NULL,\r | |
33 | NULL,\r | |
34 | {\r | |
35 | LibExtractConfig,\r | |
36 | LibRouteConfig,\r | |
37 | LibCallback\r | |
38 | },\r | |
39 | NULL,\r | |
40 | &mFsOptionMenu,\r | |
41 | 0\r | |
42 | };\r | |
43 | \r | |
44 | HII_VENDOR_DEVICE_PATH *gHiiVendorDevicePath;\r | |
45 | \r | |
46 | HII_VENDOR_DEVICE_PATH FeHiiVendorDevicePath = {\r | |
47 | {\r | |
48 | {\r | |
49 | HARDWARE_DEVICE_PATH,\r | |
50 | HW_VENDOR_DP,\r | |
51 | {\r | |
52 | (UINT8) (sizeof (VENDOR_DEVICE_PATH)),\r | |
53 | (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)\r | |
54 | }\r | |
55 | },\r | |
56 | //\r | |
57 | // Will be replace with gEfiCallerIdGuid in code.\r | |
58 | //\r | |
59 | { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } }\r | |
60 | },\r | |
61 | {\r | |
62 | END_DEVICE_PATH_TYPE,\r | |
63 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
64 | { \r | |
65 | (UINT8) (END_DEVICE_PATH_LENGTH),\r | |
66 | (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)\r | |
67 | }\r | |
68 | }\r | |
69 | };\r | |
70 | \r | |
71 | VOID *mLibStartOpCodeHandle = NULL;\r | |
72 | VOID *mLibEndOpCodeHandle = NULL;\r | |
73 | EFI_IFR_GUID_LABEL *mLibStartLabel = NULL;\r | |
74 | EFI_IFR_GUID_LABEL *mLibEndLabel = NULL;\r | |
75 | \r | |
76 | /**\r | |
77 | This function allows a caller to extract the current configuration for one\r | |
78 | or more named elements from the target driver.\r | |
79 | \r | |
80 | \r | |
81 | @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r | |
82 | @param Request A null-terminated Unicode string in <ConfigRequest> format.\r | |
83 | @param Progress On return, points to a character in the Request string.\r | |
84 | Points to the string's null terminator if request was successful.\r | |
85 | Points to the most recent '&' before the first failing name/value\r | |
86 | pair (or the beginning of the string if the failure is in the\r | |
87 | first name/value pair) if the request was not successful.\r | |
88 | @param Results A null-terminated Unicode string in <ConfigAltResp> format which\r | |
89 | has all values filled in for the names in the Request string.\r | |
90 | String to be allocated by the called function.\r | |
91 | \r | |
92 | @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.\r | |
93 | @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.\r | |
94 | \r | |
95 | **/\r | |
96 | EFI_STATUS\r | |
97 | EFIAPI\r | |
98 | LibExtractConfig (\r | |
99 | IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r | |
100 | IN CONST EFI_STRING Request,\r | |
101 | OUT EFI_STRING *Progress,\r | |
102 | OUT EFI_STRING *Results\r | |
103 | )\r | |
104 | {\r | |
105 | if (Progress == NULL || Results == NULL) {\r | |
106 | return EFI_INVALID_PARAMETER;\r | |
107 | }\r | |
108 | \r | |
109 | *Progress = Request;\r | |
110 | return EFI_NOT_FOUND;\r | |
111 | }\r | |
112 | \r | |
113 | /**\r | |
114 | This function processes the results of changes in configuration.\r | |
115 | \r | |
116 | \r | |
117 | @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r | |
118 | @param Configuration A null-terminated Unicode string in <ConfigResp> format.\r | |
119 | @param Progress A pointer to a string filled in with the offset of the most\r | |
120 | recent '&' before the first failing name/value pair (or the\r | |
121 | beginning of the string if the failure is in the first\r | |
122 | name/value pair) or the terminating NULL if all was successful.\r | |
123 | \r | |
124 | @retval EFI_INVALID_PARAMETER Configuration is NULL.\r | |
125 | @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.\r | |
126 | \r | |
127 | **/\r | |
128 | EFI_STATUS\r | |
129 | EFIAPI\r | |
130 | LibRouteConfig (\r | |
131 | IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r | |
132 | IN CONST EFI_STRING Configuration,\r | |
133 | OUT EFI_STRING *Progress\r | |
134 | )\r | |
135 | {\r | |
136 | if (Configuration == NULL || Progress == NULL) {\r | |
137 | return EFI_INVALID_PARAMETER;\r | |
138 | }\r | |
139 | \r | |
140 | *Progress = Configuration;\r | |
141 | return EFI_NOT_FOUND;\r | |
142 | }\r | |
143 | \r | |
144 | /**\r | |
145 | This function processes the results of changes in configuration.\r | |
146 | When user select a interactive opcode, this callback will be triggered.\r | |
147 | Based on the Question(QuestionId) that triggers the callback, the corresponding\r | |
148 | actions is performed. It handles:\r | |
149 | \r | |
150 | 1) Process the axtra action or exit file explorer when user select one file .\r | |
151 | 2) update of file content if a dir is selected.\r | |
152 | \r | |
153 | @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r | |
154 | @param Action Specifies the type of action taken by the browser.\r | |
155 | @param QuestionId A unique value which is sent to the original exporting driver\r | |
156 | so that it can identify the type of data to expect.\r | |
157 | @param Type The type of value for the question.\r | |
158 | @param Value A pointer to the data being sent to the original exporting driver.\r | |
159 | @param ActionRequest On return, points to the action requested by the callback function.\r | |
160 | \r | |
161 | @retval EFI_SUCCESS The callback successfully handled the action.\r | |
162 | @retval other error Error occur when parse one directory.\r | |
163 | **/\r | |
164 | EFI_STATUS\r | |
165 | EFIAPI\r | |
166 | LibCallback (\r | |
167 | IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r | |
168 | IN EFI_BROWSER_ACTION Action,\r | |
169 | IN EFI_QUESTION_ID QuestionId,\r | |
170 | IN UINT8 Type,\r | |
171 | IN EFI_IFR_TYPE_VALUE *Value,\r | |
172 | OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest\r | |
173 | )\r | |
174 | {\r | |
175 | EFI_STATUS Status;\r | |
176 | BOOLEAN NeedExit;\r | |
177 | \r | |
178 | NeedExit = TRUE;\r | |
179 | \r | |
180 | if (Action != EFI_BROWSER_ACTION_CHANGING && Action != EFI_BROWSER_ACTION_CHANGED) {\r | |
181 | //\r | |
182 | // Do nothing for other UEFI Action. Only do call back when data is changed.\r | |
183 | //\r | |
184 | return EFI_UNSUPPORTED;\r | |
185 | }\r | |
186 | \r | |
4c8274a0 ED |
187 | if (Action == EFI_BROWSER_ACTION_CHANGED) {\r |
188 | if ((Value == NULL) || (ActionRequest == NULL)) {\r | |
189 | return EFI_INVALID_PARAMETER;\r | |
190 | }\r | |
191 | \r | |
192 | if (QuestionId >= FILE_OPTION_OFFSET) {\r | |
193 | LibGetDevicePath(QuestionId);\r | |
194 | \r | |
195 | //\r | |
196 | // Process the extra action.\r | |
197 | //\r | |
198 | if (gFileExplorerPrivate.ChooseHandler != NULL) {\r | |
199 | NeedExit = gFileExplorerPrivate.ChooseHandler (gFileExplorerPrivate.RetDevicePath);\r | |
200 | }\r | |
201 | \r | |
202 | if (NeedExit) {\r | |
203 | *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;\r | |
204 | }\r | |
205 | }\r | |
206 | } else if (Action == EFI_BROWSER_ACTION_CHANGING) {\r | |
207 | if (Value == NULL) {\r | |
208 | return EFI_INVALID_PARAMETER;\r | |
209 | }\r | |
210 | \r | |
211 | if (QuestionId >= FILE_OPTION_OFFSET) {\r | |
212 | Status = LibUpdateFileExplorer (QuestionId);\r | |
213 | if (EFI_ERROR (Status)) {\r | |
214 | return Status;\r | |
215 | }\r | |
216 | }\r | |
217 | }\r | |
218 | \r | |
219 | return EFI_SUCCESS;\r | |
220 | }\r | |
221 | \r | |
222 | /**\r | |
223 | Create a menu entry by given menu type.\r | |
224 | \r | |
225 | @retval NULL If failed to create the menu.\r | |
226 | @return the new menu entry.\r | |
227 | \r | |
228 | **/\r | |
229 | MENU_ENTRY *\r | |
230 | LibCreateMenuEntry (\r | |
231 | VOID\r | |
232 | )\r | |
233 | {\r | |
234 | MENU_ENTRY *MenuEntry;\r | |
235 | \r | |
236 | //\r | |
237 | // Create new menu entry\r | |
238 | //\r | |
239 | MenuEntry = AllocateZeroPool (sizeof (MENU_ENTRY));\r | |
240 | if (MenuEntry == NULL) {\r | |
241 | return NULL;\r | |
242 | }\r | |
243 | \r | |
244 | MenuEntry->VariableContext = AllocateZeroPool (sizeof (FILE_CONTEXT));\r | |
245 | if (MenuEntry->VariableContext == NULL) {\r | |
246 | FreePool (MenuEntry);\r | |
247 | return NULL;\r | |
248 | }\r | |
249 | \r | |
250 | MenuEntry->Signature = MENU_ENTRY_SIGNATURE;\r | |
251 | return MenuEntry;\r | |
252 | }\r | |
253 | \r | |
254 | \r | |
255 | /**\r | |
256 | Get the Menu Entry from the list in Menu Entry List.\r | |
257 | \r | |
258 | If MenuNumber is great or equal to the number of Menu\r | |
259 | Entry in the list, then ASSERT.\r | |
260 | \r | |
261 | @param MenuOption The Menu Entry List to read the menu entry.\r | |
262 | @param MenuNumber The index of Menu Entry.\r | |
263 | \r | |
264 | @return The Menu Entry.\r | |
265 | \r | |
266 | **/\r | |
267 | MENU_ENTRY *\r | |
268 | LibGetMenuEntry (\r | |
269 | MENU_OPTION *MenuOption,\r | |
270 | UINTN MenuNumber\r | |
271 | )\r | |
272 | {\r | |
273 | MENU_ENTRY *NewMenuEntry;\r | |
274 | UINTN Index;\r | |
275 | LIST_ENTRY *List;\r | |
276 | \r | |
277 | ASSERT (MenuNumber < MenuOption->MenuNumber);\r | |
278 | \r | |
279 | List = MenuOption->Head.ForwardLink;\r | |
280 | for (Index = 0; Index < MenuNumber; Index++) {\r | |
281 | List = List->ForwardLink;\r | |
282 | }\r | |
283 | \r | |
284 | NewMenuEntry = CR (List, MENU_ENTRY, Link, MENU_ENTRY_SIGNATURE);\r | |
285 | \r | |
286 | return NewMenuEntry;\r | |
287 | }\r | |
288 | \r | |
289 | /**\r | |
290 | Free up all resource allocated for a BM_MENU_ENTRY.\r | |
291 | \r | |
292 | @param MenuEntry A pointer to BM_MENU_ENTRY.\r | |
293 | \r | |
294 | **/\r | |
295 | VOID\r | |
296 | LibDestroyMenuEntry (\r | |
297 | MENU_ENTRY *MenuEntry\r | |
298 | )\r | |
299 | {\r | |
300 | FILE_CONTEXT *FileContext;\r | |
301 | \r | |
302 | FileContext = (FILE_CONTEXT *) MenuEntry->VariableContext;\r | |
303 | \r | |
304 | if (!FileContext->IsRoot) {\r | |
bbd6b010 DB |
305 | if (FileContext->DevicePath != NULL) {\r |
306 | FreePool (FileContext->DevicePath);\r | |
307 | }\r | |
4c8274a0 ED |
308 | } else {\r |
309 | if (FileContext->FileHandle != NULL) {\r | |
310 | FileContext->FileHandle->Close (FileContext->FileHandle);\r | |
311 | }\r | |
312 | }\r | |
313 | \r | |
314 | if (FileContext->FileName != NULL) {\r | |
315 | FreePool (FileContext->FileName);\r | |
316 | }\r | |
317 | \r | |
318 | FreePool (FileContext);\r | |
319 | \r | |
bbd6b010 DB |
320 | if (MenuEntry->DisplayString != NULL) {\r |
321 | FreePool (MenuEntry->DisplayString);\r | |
322 | }\r | |
4c8274a0 ED |
323 | if (MenuEntry->HelpString != NULL) {\r |
324 | FreePool (MenuEntry->HelpString);\r | |
325 | }\r | |
326 | \r | |
327 | FreePool (MenuEntry);\r | |
328 | }\r | |
329 | \r | |
330 | \r | |
331 | /**\r | |
332 | Free resources allocated in Allocate Rountine.\r | |
333 | \r | |
334 | @param FreeMenu Menu to be freed\r | |
335 | **/\r | |
336 | VOID\r | |
337 | LibFreeMenu (\r | |
338 | MENU_OPTION *FreeMenu\r | |
339 | )\r | |
340 | {\r | |
341 | MENU_ENTRY *MenuEntry;\r | |
342 | while (!IsListEmpty (&FreeMenu->Head)) {\r | |
343 | MenuEntry = CR (\r | |
344 | FreeMenu->Head.ForwardLink,\r | |
345 | MENU_ENTRY,\r | |
346 | Link,\r | |
347 | MENU_ENTRY_SIGNATURE\r | |
348 | );\r | |
349 | RemoveEntryList (&MenuEntry->Link);\r | |
350 | LibDestroyMenuEntry (MenuEntry);\r | |
351 | }\r | |
352 | FreeMenu->MenuNumber = 0;\r | |
353 | }\r | |
354 | \r | |
355 | /**\r | |
356 | \r | |
357 | Function opens and returns a file handle to the root directory of a volume.\r | |
358 | \r | |
359 | @param DeviceHandle A handle for a device\r | |
360 | \r | |
361 | @return A valid file handle or NULL is returned\r | |
362 | \r | |
363 | **/\r | |
364 | EFI_FILE_HANDLE\r | |
365 | LibOpenRoot (\r | |
366 | IN EFI_HANDLE DeviceHandle\r | |
367 | )\r | |
368 | {\r | |
369 | EFI_STATUS Status;\r | |
370 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r | |
371 | EFI_FILE_HANDLE File;\r | |
372 | \r | |
373 | File = NULL;\r | |
374 | \r | |
375 | //\r | |
376 | // File the file system interface to the device\r | |
377 | //\r | |
378 | Status = gBS->HandleProtocol (\r | |
379 | DeviceHandle,\r | |
380 | &gEfiSimpleFileSystemProtocolGuid,\r | |
381 | (VOID *) &Volume\r | |
382 | );\r | |
383 | \r | |
384 | //\r | |
385 | // Open the root directory of the volume\r | |
386 | //\r | |
387 | if (!EFI_ERROR (Status)) {\r | |
388 | Status = Volume->OpenVolume (\r | |
389 | Volume,\r | |
390 | &File\r | |
391 | );\r | |
392 | }\r | |
393 | //\r | |
394 | // Done\r | |
395 | //\r | |
396 | return EFI_ERROR (Status) ? NULL : File;\r | |
397 | }\r | |
398 | \r | |
399 | /**\r | |
400 | This function converts an input device structure to a Unicode string.\r | |
401 | \r | |
402 | @param DevPath A pointer to the device path structure.\r | |
403 | \r | |
404 | @return A new allocated Unicode string that represents the device path.\r | |
405 | \r | |
406 | **/\r | |
407 | CHAR16 *\r | |
408 | LibDevicePathToStr (\r | |
409 | IN EFI_DEVICE_PATH_PROTOCOL *DevPath\r | |
410 | )\r | |
411 | {\r | |
412 | EFI_STATUS Status;\r | |
413 | CHAR16 *ToText;\r | |
414 | EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevPathToText;\r | |
415 | \r | |
416 | if (DevPath == NULL) {\r | |
417 | return NULL;\r | |
418 | }\r | |
419 | \r | |
420 | Status = gBS->LocateProtocol (\r | |
421 | &gEfiDevicePathToTextProtocolGuid,\r | |
422 | NULL,\r | |
423 | (VOID **) &DevPathToText\r | |
424 | );\r | |
425 | ASSERT_EFI_ERROR (Status);\r | |
426 | ToText = DevPathToText->ConvertDevicePathToText (\r | |
427 | DevPath,\r | |
428 | FALSE,\r | |
429 | TRUE\r | |
430 | );\r | |
431 | ASSERT (ToText != NULL);\r | |
432 | \r | |
433 | return ToText;\r | |
434 | }\r | |
435 | \r | |
436 | /**\r | |
437 | Duplicate a string.\r | |
438 | \r | |
439 | @param Src The source.\r | |
440 | \r | |
441 | @return A new string which is duplicated copy of the source.\r | |
442 | @retval NULL If there is not enough memory.\r | |
443 | \r | |
444 | **/\r | |
445 | CHAR16 *\r | |
446 | LibStrDuplicate (\r | |
447 | IN CHAR16 *Src\r | |
448 | )\r | |
449 | {\r | |
450 | CHAR16 *Dest;\r | |
451 | UINTN Size;\r | |
452 | \r | |
453 | Size = StrSize (Src);\r | |
454 | Dest = AllocateZeroPool (Size);\r | |
455 | ASSERT (Dest != NULL);\r | |
456 | if (Dest != NULL) {\r | |
457 | CopyMem (Dest, Src, Size);\r | |
458 | }\r | |
459 | \r | |
460 | return Dest;\r | |
461 | }\r | |
462 | \r | |
463 | /**\r | |
464 | \r | |
465 | Function gets the file information from an open file descriptor, and stores it\r | |
466 | in a buffer allocated from pool.\r | |
467 | \r | |
468 | @param FHand File Handle.\r | |
469 | @param InfoType Info type need to get.\r | |
470 | \r | |
471 | @retval A pointer to a buffer with file information or NULL is returned\r | |
472 | \r | |
473 | **/\r | |
474 | VOID *\r | |
475 | LibFileInfo (\r | |
476 | IN EFI_FILE_HANDLE FHand,\r | |
477 | IN EFI_GUID *InfoType\r | |
478 | )\r | |
479 | {\r | |
480 | EFI_STATUS Status;\r | |
481 | EFI_FILE_INFO *Buffer;\r | |
482 | UINTN BufferSize;\r | |
483 | \r | |
484 | Buffer = NULL;\r | |
485 | BufferSize = 0;\r | |
486 | \r | |
487 | Status = FHand->GetInfo (\r | |
488 | FHand,\r | |
489 | InfoType,\r | |
490 | &BufferSize,\r | |
491 | Buffer\r | |
492 | );\r | |
493 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
494 | Buffer = AllocatePool (BufferSize);\r | |
495 | ASSERT (Buffer != NULL);\r | |
496 | }\r | |
497 | \r | |
498 | Status = FHand->GetInfo (\r | |
499 | FHand,\r | |
500 | InfoType,\r | |
501 | &BufferSize,\r | |
502 | Buffer\r | |
503 | );\r | |
504 | \r | |
505 | return Buffer;\r | |
506 | }\r | |
507 | \r | |
508 | /**\r | |
509 | \r | |
510 | Get file type base on the file name.\r | |
511 | Just cut the file name, from the ".". eg ".efi"\r | |
512 | \r | |
513 | @param FileName File need to be checked.\r | |
514 | \r | |
515 | @retval the file type string.\r | |
516 | \r | |
517 | **/\r | |
518 | CHAR16*\r | |
519 | LibGetTypeFromName (\r | |
520 | IN CHAR16 *FileName\r | |
521 | )\r | |
522 | {\r | |
523 | UINTN Index;\r | |
524 | \r | |
525 | Index = StrLen (FileName) - 1;\r | |
526 | while ((FileName[Index] != L'.') && (Index != 0)) {\r | |
527 | Index--;\r | |
528 | }\r | |
529 | \r | |
530 | return Index == 0 ? NULL : &FileName[Index];\r | |
531 | }\r | |
532 | \r | |
533 | /**\r | |
534 | Converts the unicode character of the string from uppercase to lowercase.\r | |
535 | This is a internal function.\r | |
536 | \r | |
537 | @param ConfigString String to be converted\r | |
538 | \r | |
539 | **/\r | |
540 | VOID\r | |
541 | LibToLowerString (\r | |
542 | IN CHAR16 *String\r | |
543 | )\r | |
544 | {\r | |
545 | CHAR16 *TmpStr;\r | |
546 | \r | |
547 | for (TmpStr = String; *TmpStr != L'\0'; TmpStr++) {\r | |
548 | if (*TmpStr >= L'A' && *TmpStr <= L'Z') {\r | |
549 | *TmpStr = (CHAR16) (*TmpStr - L'A' + L'a');\r | |
550 | }\r | |
551 | }\r | |
552 | }\r | |
553 | \r | |
554 | /**\r | |
555 | \r | |
556 | Check whether current FileName point to a valid\r | |
557 | Efi Image File.\r | |
558 | \r | |
559 | @param FileName File need to be checked.\r | |
560 | \r | |
561 | @retval TRUE Is Efi Image\r | |
562 | @retval FALSE Not a valid Efi Image\r | |
563 | \r | |
564 | **/\r | |
565 | BOOLEAN\r | |
566 | LibIsSupportedFileType (\r | |
567 | IN UINT16 *FileName\r | |
568 | )\r | |
569 | {\r | |
570 | CHAR16 *InputFileType;\r | |
571 | CHAR16 *TmpStr;\r | |
572 | BOOLEAN IsSupported;\r | |
573 | \r | |
574 | if (gFileExplorerPrivate.FileType == NULL) {\r | |
575 | return TRUE;\r | |
576 | }\r | |
577 | \r | |
578 | InputFileType = LibGetTypeFromName (FileName);\r | |
579 | //\r | |
580 | // If the file not has *.* style, always return TRUE.\r | |
581 | //\r | |
582 | if (InputFileType == NULL) {\r | |
583 | return TRUE;\r | |
584 | }\r | |
585 | \r | |
586 | TmpStr = AllocateCopyPool (StrSize (InputFileType), InputFileType);\r | |
d0d34cdf | 587 | ASSERT(TmpStr != NULL);\r |
4c8274a0 ED |
588 | LibToLowerString(TmpStr);\r |
589 | \r | |
590 | IsSupported = (StrStr (gFileExplorerPrivate.FileType, TmpStr) == NULL ? FALSE : TRUE);\r | |
591 | \r | |
592 | FreePool (TmpStr);\r | |
593 | return IsSupported;\r | |
594 | }\r | |
595 | \r | |
596 | /**\r | |
597 | \r | |
598 | Append file name to existing file name.\r | |
599 | \r | |
600 | @param Str1 The existing file name\r | |
601 | @param Str2 The file name to be appended\r | |
602 | \r | |
603 | @return Allocate a new string to hold the appended result.\r | |
604 | Caller is responsible to free the returned string.\r | |
605 | \r | |
606 | **/\r | |
607 | CHAR16 *\r | |
608 | LibAppendFileName (\r | |
609 | IN CHAR16 *Str1,\r | |
610 | IN CHAR16 *Str2\r | |
611 | )\r | |
612 | {\r | |
613 | UINTN Size1;\r | |
614 | UINTN Size2;\r | |
615 | UINTN MaxLen;\r | |
616 | CHAR16 *Str;\r | |
617 | CHAR16 *TmpStr;\r | |
618 | CHAR16 *Ptr;\r | |
619 | CHAR16 *LastSlash;\r | |
620 | \r | |
621 | Size1 = StrSize (Str1);\r | |
622 | Size2 = StrSize (Str2);\r | |
623 | MaxLen = (Size1 + Size2 + sizeof (CHAR16))/ sizeof (CHAR16);\r | |
624 | Str = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16));\r | |
625 | ASSERT (Str != NULL);\r | |
626 | \r | |
627 | TmpStr = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16)); \r | |
628 | ASSERT (TmpStr != NULL);\r | |
629 | \r | |
630 | StrCpyS (Str, MaxLen, Str1);\r | |
631 | if (!((*Str == '\\') && (*(Str + 1) == 0))) {\r | |
632 | StrCatS (Str, MaxLen, L"\\");\r | |
633 | }\r | |
634 | \r | |
635 | StrCatS (Str, MaxLen, Str2);\r | |
636 | \r | |
637 | Ptr = Str;\r | |
638 | LastSlash = Str;\r | |
639 | while (*Ptr != 0) {\r | |
640 | if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '.' && *(Ptr + 3) == L'\\') {\r | |
641 | //\r | |
642 | // Convert "\Name\..\" to "\"\r | |
643 | // DO NOT convert the .. if it is at the end of the string. This will\r | |
644 | // break the .. behavior in changing directories.\r | |
645 | //\r | |
646 | \r | |
647 | //\r | |
648 | // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings \r | |
649 | // that overlap.\r | |
650 | //\r | |
651 | StrCpyS (TmpStr, MaxLen, Ptr + 3);\r | |
652 | StrCpyS (LastSlash, MaxLen - (UINTN) (LastSlash - Str), TmpStr);\r | |
653 | Ptr = LastSlash;\r | |
654 | } else if (*Ptr == '\\' && *(Ptr + 1) == '.' && *(Ptr + 2) == '\\') {\r | |
655 | //\r | |
656 | // Convert a "\.\" to a "\"\r | |
657 | //\r | |
658 | \r | |
659 | //\r | |
660 | // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings \r | |
661 | // that overlap.\r | |
662 | //\r | |
663 | StrCpyS (TmpStr, MaxLen, Ptr + 2);\r | |
664 | StrCpyS (Ptr, MaxLen - (UINTN) (Ptr - Str), TmpStr);\r | |
665 | Ptr = LastSlash;\r | |
666 | } else if (*Ptr == '\\') {\r | |
667 | LastSlash = Ptr;\r | |
668 | }\r | |
669 | \r | |
670 | Ptr++;\r | |
671 | }\r | |
672 | \r | |
673 | FreePool (TmpStr);\r | |
674 | \r | |
675 | return Str;\r | |
676 | }\r | |
677 | \r | |
678 | /**\r | |
679 | This function build the FsOptionMenu list which records all\r | |
680 | available file system in the system. They includes all instances\r | |
681 | of EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, all instances of EFI_LOAD_FILE_SYSTEM.\r | |
682 | \r | |
683 | \r | |
684 | @retval EFI_SUCCESS Success find the file system\r | |
685 | @retval EFI_OUT_OF_RESOURCES Can not create menu entry\r | |
686 | \r | |
687 | **/\r | |
688 | EFI_STATUS\r | |
689 | LibFindFileSystem (\r | |
690 | VOID\r | |
691 | )\r | |
692 | {\r | |
693 | UINTN NoSimpleFsHandles;\r | |
694 | UINTN NoLoadFileHandles;\r | |
695 | EFI_HANDLE *SimpleFsHandle;\r | |
696 | EFI_HANDLE *LoadFileHandle;\r | |
697 | UINT16 *VolumeLabel;\r | |
698 | UINTN Index;\r | |
699 | EFI_STATUS Status;\r | |
700 | MENU_ENTRY *MenuEntry;\r | |
701 | FILE_CONTEXT *FileContext;\r | |
702 | UINTN OptionNumber;\r | |
703 | EFI_FILE_SYSTEM_VOLUME_LABEL *Info;\r | |
704 | \r | |
705 | NoSimpleFsHandles = 0;\r | |
706 | NoLoadFileHandles = 0;\r | |
707 | OptionNumber = 0;\r | |
708 | \r | |
709 | //\r | |
710 | // Locate Handles that support Simple File System protocol\r | |
711 | //\r | |
712 | Status = gBS->LocateHandleBuffer (\r | |
713 | ByProtocol,\r | |
714 | &gEfiSimpleFileSystemProtocolGuid,\r | |
715 | NULL,\r | |
716 | &NoSimpleFsHandles,\r | |
717 | &SimpleFsHandle\r | |
718 | );\r | |
719 | if (!EFI_ERROR (Status)) {\r | |
720 | //\r | |
721 | // Find all the instances of the File System prototocol\r | |
722 | //\r | |
723 | for (Index = 0; Index < NoSimpleFsHandles; Index++) {\r | |
724 | //\r | |
725 | // Allocate pool for this load option\r | |
726 | //\r | |
727 | MenuEntry = LibCreateMenuEntry ();\r | |
728 | if (NULL == MenuEntry) {\r | |
729 | FreePool (SimpleFsHandle);\r | |
730 | return EFI_OUT_OF_RESOURCES;\r | |
731 | }\r | |
732 | \r | |
733 | FileContext = (FILE_CONTEXT *) MenuEntry->VariableContext;\r | |
734 | FileContext->DeviceHandle = SimpleFsHandle[Index];\r | |
735 | FileContext->FileHandle = LibOpenRoot (FileContext->DeviceHandle);\r | |
736 | if (FileContext->FileHandle == NULL) {\r | |
737 | LibDestroyMenuEntry (MenuEntry);\r | |
738 | continue;\r | |
739 | }\r | |
740 | \r | |
741 | MenuEntry->HelpString = LibDevicePathToStr (DevicePathFromHandle (FileContext->DeviceHandle));\r | |
742 | FileContext->FileName = LibStrDuplicate (L"\\");\r | |
743 | FileContext->DevicePath = FileDevicePath (FileContext->DeviceHandle, FileContext->FileName);\r | |
744 | FileContext->IsDir = TRUE;\r | |
745 | FileContext->IsRoot = TRUE;\r | |
746 | \r | |
747 | //\r | |
748 | // Get current file system's Volume Label\r | |
749 | //\r | |
750 | Info = (EFI_FILE_SYSTEM_VOLUME_LABEL *) LibFileInfo (FileContext->FileHandle, &gEfiFileSystemVolumeLabelInfoIdGuid);\r | |
751 | if (Info == NULL) {\r | |
752 | VolumeLabel = L"NO FILE SYSTEM INFO";\r | |
753 | } else {\r | |
754 | if (Info->VolumeLabel == NULL) {\r | |
755 | VolumeLabel = L"NULL VOLUME LABEL";\r | |
756 | } else {\r | |
757 | VolumeLabel = Info->VolumeLabel;\r | |
758 | if (*VolumeLabel == 0x0000) {\r | |
759 | VolumeLabel = L"NO VOLUME LABEL";\r | |
760 | }\r | |
761 | }\r | |
762 | }\r | |
763 | MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);\r | |
764 | ASSERT (MenuEntry->DisplayString != NULL);\r | |
765 | UnicodeSPrint (\r | |
766 | MenuEntry->DisplayString,\r | |
767 | MAX_CHAR,\r | |
768 | L"%s, [%s]",\r | |
769 | VolumeLabel,\r | |
770 | MenuEntry->HelpString\r | |
771 | );\r | |
772 | MenuEntry->DisplayStringToken = HiiSetString (\r | |
773 | gFileExplorerPrivate.FeHiiHandle,\r | |
774 | 0,\r | |
775 | MenuEntry->DisplayString,\r | |
776 | NULL\r | |
777 | );\r | |
140cc800 MZ |
778 | \r |
779 | if (Info != NULL)\r | |
780 | FreePool (Info);\r | |
4c8274a0 ED |
781 | \r |
782 | OptionNumber++;\r | |
783 | InsertTailList (&gFileExplorerPrivate.FsOptionMenu->Head, &MenuEntry->Link);\r | |
784 | }\r | |
785 | }\r | |
786 | \r | |
787 | if (NoSimpleFsHandles != 0) {\r | |
788 | FreePool (SimpleFsHandle);\r | |
789 | }\r | |
790 | \r | |
791 | //\r | |
792 | // Searching for handles that support Load File protocol\r | |
793 | //\r | |
794 | Status = gBS->LocateHandleBuffer (\r | |
795 | ByProtocol,\r | |
796 | &gEfiLoadFileProtocolGuid,\r | |
797 | NULL,\r | |
798 | &NoLoadFileHandles,\r | |
799 | &LoadFileHandle\r | |
800 | );\r | |
801 | \r | |
802 | if (!EFI_ERROR (Status)) {\r | |
803 | for (Index = 0; Index < NoLoadFileHandles; Index++) {\r | |
804 | MenuEntry = LibCreateMenuEntry ();\r | |
805 | if (NULL == MenuEntry) {\r | |
806 | FreePool (LoadFileHandle);\r | |
807 | return EFI_OUT_OF_RESOURCES;\r | |
808 | }\r | |
809 | \r | |
810 | FileContext = (FILE_CONTEXT *) MenuEntry->VariableContext;\r | |
811 | FileContext->DeviceHandle = LoadFileHandle[Index];\r | |
812 | FileContext->IsRoot = TRUE;\r | |
813 | \r | |
814 | FileContext->DevicePath = DevicePathFromHandle (FileContext->DeviceHandle);\r | |
815 | FileContext->FileName = LibDevicePathToStr (FileContext->DevicePath);\r | |
816 | \r | |
817 | MenuEntry->HelpString = LibDevicePathToStr (FileContext->DevicePath);\r | |
818 | MenuEntry->DisplayString = AllocateZeroPool (MAX_CHAR);\r | |
819 | ASSERT (MenuEntry->DisplayString != NULL);\r | |
820 | UnicodeSPrint (\r | |
821 | MenuEntry->DisplayString,\r | |
822 | MAX_CHAR,\r | |
823 | L"Load File [%s]",\r | |
824 | MenuEntry->HelpString\r | |
825 | );\r | |
826 | MenuEntry->DisplayStringToken = HiiSetString (\r | |
827 | gFileExplorerPrivate.FeHiiHandle,\r | |
828 | 0,\r | |
829 | MenuEntry->DisplayString,\r | |
830 | NULL\r | |
831 | );\r | |
832 | \r | |
833 | OptionNumber++;\r | |
834 | InsertTailList (&gFileExplorerPrivate.FsOptionMenu->Head, &MenuEntry->Link);\r | |
835 | }\r | |
836 | }\r | |
837 | \r | |
838 | if (NoLoadFileHandles != 0) {\r | |
839 | FreePool (LoadFileHandle);\r | |
840 | }\r | |
841 | \r | |
842 | gFileExplorerPrivate.FsOptionMenu->MenuNumber = OptionNumber;\r | |
843 | \r | |
844 | return EFI_SUCCESS;\r | |
845 | }\r | |
846 | \r | |
847 | /**\r | |
848 | Find the file handle from the input menu info.\r | |
849 | \r | |
850 | @param MenuEntry Input Menu info.\r | |
851 | @param RetFileHandle Return the file handle for the input device path.\r | |
852 | \r | |
853 | @retval EFI_SUCESS Find the file handle success.\r | |
854 | @retval Other Find the file handle failure.\r | |
855 | **/\r | |
856 | EFI_STATUS\r | |
857 | LibGetFileHandleFromMenu (\r | |
858 | IN MENU_ENTRY *MenuEntry, \r | |
859 | OUT EFI_FILE_HANDLE *RetFileHandle\r | |
860 | )\r | |
861 | {\r | |
862 | EFI_FILE_HANDLE Dir;\r | |
863 | EFI_FILE_HANDLE NewDir;\r | |
864 | FILE_CONTEXT *FileContext;\r | |
865 | EFI_STATUS Status;\r | |
866 | \r | |
867 | FileContext = (FILE_CONTEXT *) MenuEntry->VariableContext;\r | |
868 | Dir = FileContext->FileHandle;\r | |
869 | \r | |
870 | //\r | |
871 | // Open current directory to get files from it\r | |
872 | //\r | |
873 | Status = Dir->Open (\r | |
874 | Dir,\r | |
875 | &NewDir,\r | |
876 | FileContext->FileName,\r | |
877 | EFI_FILE_READ_ONLY,\r | |
878 | 0\r | |
879 | );\r | |
880 | if (EFI_ERROR (Status)) {\r | |
881 | return Status;\r | |
882 | }\r | |
883 | \r | |
884 | if (!FileContext->IsRoot) {\r | |
885 | Dir->Close (Dir);\r | |
886 | }\r | |
887 | \r | |
888 | *RetFileHandle = NewDir;\r | |
889 | \r | |
890 | return EFI_SUCCESS;\r | |
891 | }\r | |
892 | \r | |
893 | /**\r | |
894 | Find the file handle from the input device path info.\r | |
895 | \r | |
896 | @param RootDirectory Device path info.\r | |
897 | @param RetFileHandle Return the file handle for the input device path.\r | |
898 | @param ParentFileName Parent file name.\r | |
899 | @param DeviceHandle Driver handle for this partition.\r | |
900 | \r | |
901 | @retval EFI_SUCESS Find the file handle success.\r | |
902 | @retval Other Find the file handle failure.\r | |
903 | **/\r | |
904 | EFI_STATUS\r | |
905 | LibGetFileHandleFromDevicePath (\r | |
906 | IN EFI_DEVICE_PATH_PROTOCOL *RootDirectory, \r | |
907 | OUT EFI_FILE_HANDLE *RetFileHandle,\r | |
908 | OUT UINT16 **ParentFileName,\r | |
909 | OUT EFI_HANDLE *DeviceHandle\r | |
910 | )\r | |
911 | {\r | |
912 | EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r | |
913 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePathNode;\r | |
914 | EFI_STATUS Status;\r | |
915 | EFI_HANDLE Handle;\r | |
916 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r | |
917 | EFI_FILE_HANDLE FileHandle;\r | |
918 | EFI_FILE_HANDLE LastHandle;\r | |
919 | CHAR16 *TempPath;\r | |
920 | \r | |
921 | *ParentFileName = NULL;\r | |
922 | \r | |
923 | //\r | |
924 | // Attempt to access the file via a file system interface\r | |
925 | //\r | |
926 | DevicePathNode = RootDirectory;\r | |
927 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathNode, &Handle);\r | |
928 | if (EFI_ERROR (Status)) {\r | |
929 | return Status;\r | |
930 | }\r | |
931 | \r | |
932 | Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&Volume);\r | |
933 | if (EFI_ERROR (Status)) {\r | |
934 | return Status;\r | |
935 | }\r | |
936 | \r | |
937 | //\r | |
938 | // Open the Volume to get the File System handle\r | |
939 | //\r | |
940 | Status = Volume->OpenVolume (Volume, &FileHandle);\r | |
941 | if (EFI_ERROR (Status)) {\r | |
942 | return Status;\r | |
943 | }\r | |
944 | \r | |
945 | *DeviceHandle = Handle;\r | |
946 | \r | |
947 | if (IsDevicePathEnd(DevicePathNode)) {\r | |
948 | *ParentFileName = AllocateCopyPool (StrSize (L"\\"), L"\\");\r | |
949 | *RetFileHandle = FileHandle;\r | |
950 | return EFI_SUCCESS;\r | |
951 | }\r | |
952 | \r | |
953 | //\r | |
954 | // Duplicate the device path to avoid the access to unaligned device path node.\r | |
955 | // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r | |
956 | // nodes, It assures the fields in device path nodes are 2 byte aligned.\r | |
957 | //\r | |
958 | TempDevicePathNode = DuplicateDevicePath (DevicePathNode);\r | |
959 | if (TempDevicePathNode == NULL) {\r | |
960 | \r | |
961 | //\r | |
962 | // Setting Status to an EFI_ERROR value will cause the rest of\r | |
963 | // the file system support below to be skipped.\r | |
964 | //\r | |
965 | Status = EFI_OUT_OF_RESOURCES;\r | |
966 | }\r | |
967 | \r | |
968 | //\r | |
969 | // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r | |
970 | // directory information and filename can be seperate. The goal is to inch\r | |
971 | // our way down each device path node and close the previous node\r | |
972 | //\r | |
973 | DevicePathNode = TempDevicePathNode;\r | |
974 | while (!EFI_ERROR (Status) && !IsDevicePathEnd (DevicePathNode)) {\r | |
975 | if (DevicePathType (DevicePathNode) != MEDIA_DEVICE_PATH ||\r | |
976 | DevicePathSubType (DevicePathNode) != MEDIA_FILEPATH_DP) {\r | |
977 | Status = EFI_UNSUPPORTED;\r | |
978 | goto Done;\r | |
979 | }\r | |
980 | \r | |
981 | LastHandle = FileHandle;\r | |
982 | FileHandle = NULL;\r | |
983 | \r | |
984 | Status = LastHandle->Open (\r | |
985 | LastHandle,\r | |
986 | &FileHandle,\r | |
987 | ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName,\r | |
988 | EFI_FILE_MODE_READ,\r | |
989 | 0\r | |
990 | );\r | |
991 | if (*ParentFileName == NULL) {\r | |
992 | *ParentFileName = AllocateCopyPool (StrSize (((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName), ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName);\r | |
993 | } else {\r | |
994 | TempPath = LibAppendFileName (*ParentFileName, ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName);\r | |
995 | FreePool (*ParentFileName);\r | |
996 | *ParentFileName = TempPath;\r | |
997 | }\r | |
998 | \r | |
999 | //\r | |
1000 | // Close the previous node\r | |
1001 | //\r | |
1002 | LastHandle->Close (LastHandle);\r | |
1003 | \r | |
1004 | DevicePathNode = NextDevicePathNode (DevicePathNode);\r | |
1005 | }\r | |
1006 | \r | |
1007 | if (EFI_ERROR (Status)) {\r | |
1008 | goto Done;\r | |
1009 | }\r | |
1010 | \r | |
1011 | *RetFileHandle = FileHandle;\r | |
1012 | \r | |
1013 | Status = EFI_SUCCESS;\r | |
1014 | \r | |
1015 | Done:\r | |
1016 | if (TempDevicePathNode != NULL) {\r | |
1017 | FreePool (TempDevicePathNode);\r | |
1018 | }\r | |
1019 | \r | |
1020 | if ((FileHandle != NULL) && (EFI_ERROR (Status))) {\r | |
1021 | FileHandle->Close (FileHandle);\r | |
1022 | }\r | |
1023 | \r | |
1024 | return Status;\r | |
1025 | }\r | |
1026 | \r | |
1027 | /**\r | |
1028 | Find files under current directory.\r | |
1029 | \r | |
1030 | All files and sub-directories in current directory\r | |
1031 | will be stored in DirectoryMenu for future use.\r | |
1032 | \r | |
1033 | @param FileHandle Parent file handle. \r | |
1034 | @param FileName Parent file name.\r | |
1035 | @param DeviceHandle Driver handle for this partition.\r | |
1036 | \r | |
1037 | @retval EFI_SUCCESS Get files from current dir successfully.\r | |
1038 | @return Other value if can't get files from current dir.\r | |
1039 | \r | |
1040 | **/\r | |
1041 | EFI_STATUS\r | |
1042 | LibFindFiles (\r | |
1043 | IN EFI_FILE_HANDLE FileHandle,\r | |
1044 | IN UINT16 *FileName,\r | |
1045 | IN EFI_HANDLE DeviceHandle\r | |
1046 | )\r | |
1047 | {\r | |
1048 | EFI_FILE_INFO *DirInfo;\r | |
1049 | UINTN BufferSize;\r | |
1050 | UINTN DirBufferSize;\r | |
1051 | MENU_ENTRY *NewMenuEntry;\r | |
1052 | FILE_CONTEXT *NewFileContext;\r | |
1053 | UINTN Pass;\r | |
1054 | EFI_STATUS Status;\r | |
1055 | UINTN OptionNumber;\r | |
1056 | \r | |
1057 | OptionNumber = 0;\r | |
1058 | \r | |
1059 | DirBufferSize = sizeof (EFI_FILE_INFO) + 1024;\r | |
1060 | DirInfo = AllocateZeroPool (DirBufferSize);\r | |
1061 | if (DirInfo == NULL) {\r | |
1062 | return EFI_OUT_OF_RESOURCES;\r | |
1063 | }\r | |
1064 | \r | |
1065 | //\r | |
1066 | // Get all files in current directory\r | |
1067 | // Pass 1 to get Directories\r | |
1068 | // Pass 2 to get files that are EFI images\r | |
1069 | //\r | |
1070 | for (Pass = 1; Pass <= 2; Pass++) {\r | |
1071 | FileHandle->SetPosition (FileHandle, 0);\r | |
1072 | for (;;) {\r | |
1073 | BufferSize = DirBufferSize;\r | |
1074 | Status = FileHandle->Read (FileHandle, &BufferSize, DirInfo);\r | |
1075 | if (EFI_ERROR (Status) || BufferSize == 0) {\r | |
1076 | break;\r | |
1077 | }\r | |
1078 | \r | |
1079 | if (((DirInfo->Attribute & EFI_FILE_DIRECTORY) != 0 && Pass == 2) ||\r | |
1080 | ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0 && Pass == 1)\r | |
1081 | ) {\r | |
1082 | //\r | |
1083 | // Pass 1 is for Directories\r | |
1084 | // Pass 2 is for file names\r | |
1085 | //\r | |
1086 | continue;\r | |
1087 | }\r | |
1088 | \r | |
1089 | if (!((DirInfo->Attribute & EFI_FILE_DIRECTORY) != 0 || LibIsSupportedFileType (DirInfo->FileName))) {\r | |
1090 | //\r | |
1091 | // Slip file unless it is a directory entry or a .EFI file\r | |
1092 | //\r | |
1093 | continue;\r | |
1094 | }\r | |
1095 | \r | |
1096 | NewMenuEntry = LibCreateMenuEntry ();\r | |
1097 | if (NULL == NewMenuEntry) {\r | |
1098 | return EFI_OUT_OF_RESOURCES;\r | |
1099 | }\r | |
1100 | \r | |
1101 | NewFileContext = (FILE_CONTEXT *) NewMenuEntry->VariableContext;\r | |
1102 | NewFileContext->DeviceHandle = DeviceHandle;\r | |
1103 | NewFileContext->FileName = LibAppendFileName (FileName, DirInfo->FileName);\r | |
1104 | NewFileContext->FileHandle = FileHandle;\r | |
1105 | NewFileContext->DevicePath = FileDevicePath (NewFileContext->DeviceHandle, NewFileContext->FileName);\r | |
1106 | NewMenuEntry->HelpString = NULL;\r | |
1107 | NewFileContext->IsDir = (BOOLEAN) ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY);\r | |
1108 | \r | |
1109 | if (NewFileContext->IsDir) {\r | |
1110 | BufferSize = StrLen (DirInfo->FileName) * 2 + 6;\r | |
1111 | NewMenuEntry->DisplayString = AllocateZeroPool (BufferSize);\r | |
1112 | UnicodeSPrint (\r | |
1113 | NewMenuEntry->DisplayString,\r | |
1114 | BufferSize,\r | |
1115 | L"<%s>",\r | |
1116 | DirInfo->FileName\r | |
1117 | );\r | |
1118 | } else {\r | |
1119 | NewMenuEntry->DisplayString = LibStrDuplicate (DirInfo->FileName);\r | |
1120 | }\r | |
1121 | \r | |
1122 | NewMenuEntry->DisplayStringToken = HiiSetString (\r | |
1123 | gFileExplorerPrivate.FeHiiHandle,\r | |
1124 | 0,\r | |
1125 | NewMenuEntry->DisplayString,\r | |
1126 | NULL\r | |
1127 | );\r | |
1128 | \r | |
1129 | NewFileContext->IsRoot = FALSE;\r | |
1130 | \r | |
1131 | OptionNumber++;\r | |
1132 | InsertTailList (&gFileExplorerPrivate.FsOptionMenu->Head, &NewMenuEntry->Link);\r | |
1133 | }\r | |
1134 | }\r | |
1135 | \r | |
1136 | gFileExplorerPrivate.FsOptionMenu->MenuNumber = OptionNumber;\r | |
1137 | \r | |
1138 | FreePool (DirInfo);\r | |
1139 | \r | |
1140 | return EFI_SUCCESS;\r | |
1141 | }\r | |
1142 | \r | |
1143 | /**\r | |
1144 | Refresh the global UpdateData structure.\r | |
1145 | \r | |
1146 | **/\r | |
1147 | VOID\r | |
1148 | LibRefreshUpdateData (\r | |
1149 | VOID\r | |
1150 | )\r | |
1151 | {\r | |
1152 | //\r | |
1153 | // Free current updated date\r | |
1154 | //\r | |
1155 | if (mLibStartOpCodeHandle != NULL) {\r | |
1156 | HiiFreeOpCodeHandle (mLibStartOpCodeHandle);\r | |
1157 | }\r | |
1158 | if (mLibEndOpCodeHandle != NULL) {\r | |
1159 | HiiFreeOpCodeHandle (mLibEndOpCodeHandle);\r | |
1160 | }\r | |
1161 | \r | |
1162 | //\r | |
1163 | // Create new OpCode Handle\r | |
1164 | //\r | |
1165 | mLibStartOpCodeHandle = HiiAllocateOpCodeHandle ();\r | |
1166 | mLibEndOpCodeHandle = HiiAllocateOpCodeHandle ();\r | |
1167 | \r | |
1168 | //\r | |
1169 | // Create Hii Extend Label OpCode as the start opcode\r | |
1170 | //\r | |
1171 | mLibStartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (\r | |
1172 | mLibStartOpCodeHandle,\r | |
1173 | &gEfiIfrTianoGuid,\r | |
1174 | NULL,\r | |
1175 | sizeof (EFI_IFR_GUID_LABEL)\r | |
1176 | );\r | |
1177 | mLibStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r | |
1178 | \r | |
1179 | mLibStartLabel->Number = FORM_FILE_EXPLORER_ID;\r | |
1180 | \r | |
1181 | //\r | |
1182 | // Create Hii Extend Label OpCode as the start opcode\r | |
1183 | //\r | |
1184 | mLibEndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (\r | |
1185 | mLibEndOpCodeHandle,\r | |
1186 | &gEfiIfrTianoGuid,\r | |
1187 | NULL,\r | |
1188 | sizeof (EFI_IFR_GUID_LABEL)\r | |
1189 | );\r | |
1190 | mLibEndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r | |
1191 | \r | |
1192 | mLibEndLabel->Number = LABEL_END;\r | |
1193 | }\r | |
1194 | \r | |
1195 | /**\r | |
1196 | \r | |
1197 | Update the File Explore page.\r | |
1198 | \r | |
1199 | **/\r | |
1200 | VOID\r | |
1201 | LibUpdateFileExplorePage (\r | |
1202 | VOID\r | |
1203 | )\r | |
1204 | {\r | |
1205 | UINTN Index;\r | |
1206 | MENU_ENTRY *NewMenuEntry;\r | |
1207 | FILE_CONTEXT *NewFileContext;\r | |
1208 | MENU_OPTION *MenuOption;\r | |
1209 | \r | |
1210 | NewMenuEntry = NULL;\r | |
1211 | NewFileContext = NULL;\r | |
1212 | \r | |
1213 | LibRefreshUpdateData ();\r | |
1214 | MenuOption = gFileExplorerPrivate.FsOptionMenu;\r | |
1215 | \r | |
1216 | for (Index = 0; Index < MenuOption->MenuNumber; Index++) {\r | |
1217 | NewMenuEntry = LibGetMenuEntry (MenuOption, Index);\r | |
1218 | NewFileContext = (FILE_CONTEXT *) NewMenuEntry->VariableContext;\r | |
1219 | \r | |
1220 | if (!NewFileContext->IsDir) {\r | |
1221 | //\r | |
1222 | // Create Text opcode for directory, also create Text opcode for file in FileExplorerStateBootFromFile.\r | |
1223 | //\r | |
1224 | HiiCreateActionOpCode (\r | |
1225 | mLibStartOpCodeHandle,\r | |
1226 | (UINT16) (FILE_OPTION_OFFSET + Index),\r | |
1227 | NewMenuEntry->DisplayStringToken,\r | |
1228 | STRING_TOKEN (STR_NULL_STRING),\r | |
1229 | EFI_IFR_FLAG_CALLBACK,\r | |
1230 | 0\r | |
1231 | );\r | |
1232 | } else {\r | |
1233 | //\r | |
1234 | // Create Goto opcode for file in FileExplorerStateAddBootOption or FileExplorerStateAddDriverOptionState.\r | |
1235 | //\r | |
1236 | HiiCreateGotoOpCode (\r | |
1237 | mLibStartOpCodeHandle,\r | |
1238 | FORM_FILE_EXPLORER_ID,\r | |
1239 | NewMenuEntry->DisplayStringToken,\r | |
1240 | STRING_TOKEN (STR_NULL_STRING),\r | |
1241 | EFI_IFR_FLAG_CALLBACK,\r | |
1242 | (UINT16) (FILE_OPTION_OFFSET + Index)\r | |
1243 | );\r | |
1244 | }\r | |
1245 | }\r | |
1246 | \r | |
1247 | HiiUpdateForm (\r | |
1248 | gFileExplorerPrivate.FeHiiHandle,\r | |
1249 | &FileExplorerGuid,\r | |
1250 | FORM_FILE_EXPLORER_ID,\r | |
1251 | mLibStartOpCodeHandle, // Label FORM_FILE_EXPLORER_ID\r | |
1252 | mLibEndOpCodeHandle // LABEL_END\r | |
1253 | );\r | |
1254 | }\r | |
1255 | \r | |
1256 | /**\r | |
1257 | Update the file explower page with the refershed file system.\r | |
1258 | \r | |
1259 | @param KeyValue Key value to identify the type of data to expect.\r | |
1260 | \r | |
1261 | @retval EFI_SUCCESS Update the file explorer form success.\r | |
1262 | @retval other errors Error occur when parse one directory.\r | |
1263 | \r | |
1264 | **/\r | |
1265 | EFI_STATUS\r | |
1266 | LibUpdateFileExplorer (\r | |
1267 | IN UINT16 KeyValue\r | |
1268 | )\r | |
1269 | {\r | |
1270 | UINT16 FileOptionMask;\r | |
1271 | MENU_ENTRY *NewMenuEntry;\r | |
1272 | FILE_CONTEXT *NewFileContext;\r | |
1273 | EFI_STATUS Status;\r | |
1274 | EFI_FILE_HANDLE FileHandle;\r | |
1275 | \r | |
1276 | Status = EFI_SUCCESS;\r | |
1277 | FileOptionMask = (UINT16) (FILE_OPTION_MASK & KeyValue);\r | |
1278 | NewMenuEntry = LibGetMenuEntry (gFileExplorerPrivate.FsOptionMenu, FileOptionMask);\r | |
1279 | NewFileContext = (FILE_CONTEXT *) NewMenuEntry->VariableContext;\r | |
1280 | \r | |
1281 | if (NewFileContext->IsDir) {\r | |
1282 | RemoveEntryList (&NewMenuEntry->Link);\r | |
1283 | LibFreeMenu (gFileExplorerPrivate.FsOptionMenu);\r | |
1284 | LibGetFileHandleFromMenu (NewMenuEntry, &FileHandle);\r | |
1285 | Status = LibFindFiles (FileHandle, NewFileContext->FileName, NewFileContext->DeviceHandle);\r | |
1286 | if (!EFI_ERROR (Status)) {\r | |
1287 | LibUpdateFileExplorePage ();\r | |
1288 | } else {\r | |
1289 | LibFreeMenu (gFileExplorerPrivate.FsOptionMenu);\r | |
1290 | }\r | |
1291 | LibDestroyMenuEntry (NewMenuEntry);\r | |
1292 | }\r | |
1293 | \r | |
1294 | return Status;\r | |
1295 | }\r | |
1296 | \r | |
1297 | /**\r | |
1298 | Get the device path info saved in the menu structure.\r | |
1299 | \r | |
1300 | @param KeyValue Key value to identify the type of data to expect.\r | |
1301 | \r | |
1302 | **/\r | |
1303 | VOID\r | |
1304 | LibGetDevicePath (\r | |
1305 | IN UINT16 KeyValue\r | |
1306 | )\r | |
1307 | {\r | |
1308 | UINT16 FileOptionMask;\r | |
1309 | MENU_ENTRY *NewMenuEntry;\r | |
1310 | FILE_CONTEXT *NewFileContext;\r | |
1311 | \r | |
1312 | FileOptionMask = (UINT16) (FILE_OPTION_MASK & KeyValue);\r | |
1313 | \r | |
1314 | NewMenuEntry = LibGetMenuEntry (gFileExplorerPrivate.FsOptionMenu, FileOptionMask);\r | |
1315 | \r | |
1316 | NewFileContext = (FILE_CONTEXT *) NewMenuEntry->VariableContext;\r | |
1317 | \r | |
1318 | if (gFileExplorerPrivate.RetDevicePath != NULL) {\r | |
1319 | FreePool (gFileExplorerPrivate.RetDevicePath);\r | |
1320 | }\r | |
1321 | gFileExplorerPrivate.RetDevicePath = DuplicateDevicePath (NewFileContext->DevicePath);\r | |
1322 | }\r | |
1323 | \r | |
1324 | /**\r | |
1325 | Choose a file in the specified directory. \r | |
1326 | \r | |
1327 | If user input NULL for the RootDirectory, will choose file in the system.\r | |
1328 | \r | |
1329 | If user input *File != NULL, function will return the allocate device path\r | |
1330 | info for the choosed file, caller has to free the memory after use it.\r | |
1331 | \r | |
1332 | @param RootDirectory Pointer to the root directory.\r | |
1333 | @param FileType The file type need to choose.\r | |
1334 | @param ChooseHandler Function pointer to the extra task need to do\r | |
1335 | after choose one file.\r | |
1336 | @param File Return the device path for the last time chosed file.\r | |
1337 | \r | |
1338 | @retval EFI_SUCESS Choose file success.\r | |
1339 | @retval EFI_INVALID_PARAMETER Both ChooseHandler and return device path are NULL\r | |
1340 | One of them must not NULL.\r | |
1341 | @retval Other errors Choose file failed.\r | |
1342 | **/\r | |
1343 | EFI_STATUS\r | |
1344 | EFIAPI\r | |
1345 | ChooseFile (\r | |
1346 | IN EFI_DEVICE_PATH_PROTOCOL *RootDirectory,\r | |
1347 | IN CHAR16 *FileType, OPTIONAL\r | |
1348 | IN CHOOSE_HANDLER ChooseHandler, OPTIONAL\r | |
1349 | OUT EFI_DEVICE_PATH_PROTOCOL **File OPTIONAL\r | |
1350 | )\r | |
1351 | {\r | |
1352 | EFI_FILE_HANDLE FileHandle;\r | |
1353 | EFI_STATUS Status;\r | |
1354 | UINT16 *FileName;\r | |
1355 | EFI_HANDLE DeviceHandle;\r | |
1356 | \r | |
1357 | if ((ChooseHandler == NULL) && (File == NULL)) {\r | |
1358 | return EFI_INVALID_PARAMETER;\r | |
1359 | }\r | |
1360 | \r | |
1361 | FileName = NULL;\r | |
1362 | \r | |
1363 | gFileExplorerPrivate.RetDevicePath = NULL;\r | |
1364 | gFileExplorerPrivate.ChooseHandler = ChooseHandler;\r | |
1365 | if (FileType != NULL) {\r | |
1366 | gFileExplorerPrivate.FileType = AllocateCopyPool (StrSize (FileType), FileType);\r | |
d0d34cdf | 1367 | ASSERT(gFileExplorerPrivate.FileType != NULL);\r |
4c8274a0 ED |
1368 | LibToLowerString(gFileExplorerPrivate.FileType);\r |
1369 | } else {\r | |
1370 | gFileExplorerPrivate.FileType = NULL;\r | |
1371 | }\r | |
1372 | \r | |
1373 | if (RootDirectory == NULL) {\r | |
1374 | Status = LibFindFileSystem();\r | |
1375 | } else {\r | |
1376 | Status = LibGetFileHandleFromDevicePath(RootDirectory, &FileHandle, &FileName, &DeviceHandle);\r | |
1377 | if (EFI_ERROR (Status)) {\r | |
1378 | goto Done;\r | |
1379 | }\r | |
1380 | \r | |
1381 | Status = LibFindFiles (FileHandle, FileName, DeviceHandle);\r | |
1382 | }\r | |
1383 | if (EFI_ERROR (Status)) {\r | |
1384 | goto Done;\r | |
1385 | }\r | |
1386 | \r | |
1387 | LibUpdateFileExplorePage();\r | |
1388 | \r | |
1389 | gFileExplorerPrivate.FormBrowser2->SendForm (\r | |
1390 | gFileExplorerPrivate.FormBrowser2,\r | |
1391 | &gFileExplorerPrivate.FeHiiHandle,\r | |
1392 | 1,\r | |
1393 | &FileExplorerGuid,\r | |
1394 | 0,\r | |
1395 | NULL,\r | |
1396 | NULL\r | |
1397 | );\r | |
1398 | \r | |
1399 | Done:\r | |
1400 | if ((Status == EFI_SUCCESS) && (File != NULL)) {\r | |
1401 | *File = gFileExplorerPrivate.RetDevicePath;\r | |
1402 | } else if (gFileExplorerPrivate.RetDevicePath != NULL) {\r | |
1403 | FreePool (gFileExplorerPrivate.RetDevicePath);\r | |
1404 | }\r | |
1405 | \r | |
1406 | if (gFileExplorerPrivate.FileType != NULL) {\r | |
1407 | FreePool (gFileExplorerPrivate.FileType);\r | |
1408 | }\r | |
1409 | \r | |
1410 | LibFreeMenu (gFileExplorerPrivate.FsOptionMenu);\r | |
1411 | \r | |
1412 | if (FileName != NULL) {\r | |
1413 | FreePool (FileName);\r | |
1414 | }\r | |
1415 | \r | |
1416 | return Status;\r | |
1417 | }\r | |
1418 | \r | |
1419 | /**\r | |
1420 | \r | |
1421 | Install Boot Manager Menu driver.\r | |
1422 | \r | |
1423 | @param ImageHandle The image handle.\r | |
1424 | @param SystemTable The system table.\r | |
1425 | \r | |
1426 | @retval EFI_SUCEESS Install File explorer library success.\r | |
1427 | \r | |
1428 | **/\r | |
1429 | EFI_STATUS\r | |
1430 | EFIAPI\r | |
1431 | FileExplorerLibConstructor (\r | |
1432 | IN EFI_HANDLE ImageHandle,\r | |
1433 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1434 | )\r | |
1435 | {\r | |
1436 | EFI_STATUS Status;\r | |
1437 | \r | |
1438 | gHiiVendorDevicePath = (HII_VENDOR_DEVICE_PATH*) DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL*)&FeHiiVendorDevicePath);\r | |
1439 | ASSERT (gHiiVendorDevicePath != NULL);\r | |
1440 | CopyGuid (&gHiiVendorDevicePath->VendorDevicePath.Guid, &gEfiCallerIdGuid);\r | |
1441 | \r | |
1442 | //\r | |
1443 | // Install Device Path Protocol and Config Access protocol to driver handle\r | |
1444 | //\r | |
1445 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1446 | &gFileExplorerPrivate.FeDriverHandle,\r | |
1447 | &gEfiDevicePathProtocolGuid,\r | |
1448 | gHiiVendorDevicePath,\r | |
1449 | &gEfiHiiConfigAccessProtocolGuid,\r | |
1450 | &gFileExplorerPrivate.FeConfigAccess,\r | |
1451 | NULL\r | |
1452 | );\r | |
20fe1625 ED |
1453 | if (Status == EFI_ALREADY_STARTED) {\r |
1454 | return EFI_SUCCESS;\r | |
1455 | }\r | |
1456 | if (EFI_ERROR (Status)) {\r | |
1457 | return Status;\r | |
1458 | }\r | |
4c8274a0 ED |
1459 | \r |
1460 | //\r | |
1461 | // Post our File Explorer VFR binary to the HII database.\r | |
1462 | //\r | |
1463 | gFileExplorerPrivate.FeHiiHandle = HiiAddPackages (\r | |
1464 | &FileExplorerGuid,\r | |
1465 | gFileExplorerPrivate.FeDriverHandle,\r | |
1466 | FileExplorerVfrBin,\r | |
1467 | FileExplorerLibStrings,\r | |
1468 | NULL\r | |
1469 | );\r | |
1470 | ASSERT (gFileExplorerPrivate.FeHiiHandle != NULL);\r | |
1471 | \r | |
1472 | //\r | |
1473 | // Locate Formbrowser2 protocol\r | |
1474 | //\r | |
1475 | Status = gBS->LocateProtocol (&gEfiFormBrowser2ProtocolGuid, NULL, (VOID **) &gFileExplorerPrivate.FormBrowser2);\r | |
1476 | ASSERT_EFI_ERROR (Status);\r | |
1477 | \r | |
1478 | InitializeListHead (&gFileExplorerPrivate.FsOptionMenu->Head);\r | |
1479 | \r | |
1480 | return EFI_SUCCESS;\r | |
1481 | }\r | |
1482 | \r | |
1483 | /**\r | |
1484 | Unloads the application and its installed protocol.\r | |
1485 | \r | |
1486 | @param[in] ImageHandle Handle that identifies the image to be unloaded.\r | |
1487 | @param[in] SystemTable The system table.\r | |
1488 | \r | |
1489 | @retval EFI_SUCCESS The image has been unloaded.\r | |
1490 | **/\r | |
1491 | EFI_STATUS\r | |
1492 | EFIAPI\r | |
1493 | FileExplorerLibDestructor (\r | |
1494 | IN EFI_HANDLE ImageHandle,\r | |
1495 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1496 | )\r | |
1497 | {\r | |
1498 | EFI_STATUS Status;\r | |
1499 | \r | |
1500 | ASSERT (gHiiVendorDevicePath != NULL);\r | |
1501 | \r | |
20fe1625 ED |
1502 | if (gFileExplorerPrivate.FeDriverHandle != NULL) {\r |
1503 | Status = gBS->UninstallMultipleProtocolInterfaces (\r | |
1504 | gFileExplorerPrivate.FeDriverHandle,\r | |
1505 | &gEfiDevicePathProtocolGuid,\r | |
1506 | gHiiVendorDevicePath,\r | |
1507 | &gEfiHiiConfigAccessProtocolGuid,\r | |
1508 | &gFileExplorerPrivate.FeConfigAccess,\r | |
1509 | NULL\r | |
1510 | );\r | |
1511 | ASSERT_EFI_ERROR (Status);\r | |
1512 | \r | |
1513 | HiiRemovePackages (gFileExplorerPrivate.FeHiiHandle);\r | |
1514 | }\r | |
4c8274a0 ED |
1515 | \r |
1516 | FreePool (gHiiVendorDevicePath);\r | |
1517 | \r | |
1518 | return EFI_SUCCESS;\r | |
1519 | }\r | |
1520 | \r |