]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ShellPkg/Application/Shell/FileHandleWrappers.c
ShellPkg: Clean up source files
[mirror_edk2.git] / ShellPkg / Application / Shell / FileHandleWrappers.c
... / ...
CommitLineData
1/** @file\r
2 EFI_FILE_PROTOCOL wrappers for other items (Like Environment Variables,\r
3 StdIn, StdOut, StdErr, etc...).\r
4\r
5 Copyright 2016 Dell Inc.\r
6 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
7 (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include "Shell.h"\r
19#include "FileHandleInternal.h"\r
20\r
21#define MEM_WRITE_REALLOC_OVERHEAD 1024\r
22\r
23/**\r
24 File style interface for console (Open).\r
25\r
26 @param[in] This Ignored.\r
27 @param[out] NewHandle Ignored.\r
28 @param[in] FileName Ignored.\r
29 @param[in] OpenMode Ignored.\r
30 @param[in] Attributes Ignored.\r
31\r
32 @retval EFI_NOT_FOUND\r
33**/\r
34EFI_STATUS\r
35EFIAPI\r
36FileInterfaceOpenNotFound(\r
37 IN EFI_FILE_PROTOCOL *This,\r
38 OUT EFI_FILE_PROTOCOL **NewHandle,\r
39 IN CHAR16 *FileName,\r
40 IN UINT64 OpenMode,\r
41 IN UINT64 Attributes\r
42 )\r
43{\r
44 return (EFI_NOT_FOUND);\r
45}\r
46\r
47/**\r
48 File style interface for console (Close, Delete, & Flush)\r
49\r
50 @param[in] This Ignored.\r
51\r
52 @retval EFI_SUCCESS\r
53**/\r
54EFI_STATUS\r
55EFIAPI\r
56FileInterfaceNopGeneric(\r
57 IN EFI_FILE_PROTOCOL *This\r
58 )\r
59{\r
60 return (EFI_SUCCESS);\r
61}\r
62\r
63/**\r
64 File style interface for console (GetPosition).\r
65\r
66 @param[in] This Ignored.\r
67 @param[out] Position Ignored.\r
68\r
69 @retval EFI_UNSUPPORTED\r
70**/\r
71EFI_STATUS\r
72EFIAPI\r
73FileInterfaceNopGetPosition(\r
74 IN EFI_FILE_PROTOCOL *This,\r
75 OUT UINT64 *Position\r
76 )\r
77{\r
78 return (EFI_UNSUPPORTED);\r
79}\r
80\r
81/**\r
82 File style interface for console (SetPosition).\r
83\r
84 @param[in] This Ignored.\r
85 @param[in] Position Ignored.\r
86\r
87 @retval EFI_UNSUPPORTED\r
88**/\r
89EFI_STATUS\r
90EFIAPI\r
91FileInterfaceNopSetPosition(\r
92 IN EFI_FILE_PROTOCOL *This,\r
93 IN UINT64 Position\r
94 )\r
95{\r
96 return (EFI_UNSUPPORTED);\r
97}\r
98\r
99/**\r
100 File style interface for console (GetInfo).\r
101\r
102 @param[in] This Ignored.\r
103 @param[in] InformationType Ignored.\r
104 @param[in, out] BufferSize Ignored.\r
105 @param[out] Buffer Ignored.\r
106\r
107 @retval EFI_UNSUPPORTED\r
108**/\r
109EFI_STATUS\r
110EFIAPI\r
111FileInterfaceNopGetInfo(\r
112 IN EFI_FILE_PROTOCOL *This,\r
113 IN EFI_GUID *InformationType,\r
114 IN OUT UINTN *BufferSize,\r
115 OUT VOID *Buffer\r
116 )\r
117{\r
118 return (EFI_UNSUPPORTED);\r
119}\r
120\r
121/**\r
122 File style interface for console (SetInfo).\r
123\r
124 @param[in] This Ignored.\r
125 @param[in] InformationType Ignored.\r
126 @param[in] BufferSize Ignored.\r
127 @param[in] Buffer Ignored.\r
128\r
129 @retval EFI_UNSUPPORTED\r
130**/\r
131EFI_STATUS\r
132EFIAPI\r
133FileInterfaceNopSetInfo(\r
134 IN EFI_FILE_PROTOCOL *This,\r
135 IN EFI_GUID *InformationType,\r
136 IN UINTN BufferSize,\r
137 IN VOID *Buffer\r
138 )\r
139{\r
140 return (EFI_UNSUPPORTED);\r
141}\r
142\r
143/**\r
144 File style interface for StdOut (Write).\r
145\r
146 Writes data to the screen.\r
147\r
148 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
149 @param[in, out] BufferSize Size in bytes of Buffer.\r
150 @param[in] Buffer The pointer to the buffer to write.\r
151\r
152 @retval EFI_UNSUPPORTED No output console is supported.\r
153 @return A return value from gST->ConOut->OutputString.\r
154**/\r
155EFI_STATUS\r
156EFIAPI\r
157FileInterfaceStdOutWrite(\r
158 IN EFI_FILE_PROTOCOL *This,\r
159 IN OUT UINTN *BufferSize,\r
160 IN VOID *Buffer\r
161 )\r
162{\r
163 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut) {\r
164 return (EFI_UNSUPPORTED);\r
165 }\r
166 if (*((CHAR16 *)Buffer) == gUnicodeFileTag) {\r
167 return (gST->ConOut->OutputString(gST->ConOut, (CHAR16 *)Buffer + 1));\r
168 }\r
169 return (gST->ConOut->OutputString(gST->ConOut, Buffer));\r
170}\r
171\r
172/**\r
173 File style interface for StdIn (Write).\r
174\r
175 @param[in] This Ignored.\r
176 @param[in, out] BufferSize Ignored.\r
177 @param[in] Buffer Ignored.\r
178\r
179 @retval EFI_UNSUPPORTED\r
180**/\r
181EFI_STATUS\r
182EFIAPI\r
183FileInterfaceStdInWrite(\r
184 IN EFI_FILE_PROTOCOL *This,\r
185 IN OUT UINTN *BufferSize,\r
186 IN VOID *Buffer\r
187 )\r
188{\r
189 return (EFI_UNSUPPORTED);\r
190}\r
191\r
192/**\r
193 File style interface for console StdErr (Write).\r
194\r
195 Writes error to the error output.\r
196\r
197 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
198 @param[in, out] BufferSize Size in bytes of Buffer.\r
199 @param[in] Buffer The pointer to the buffer to write.\r
200\r
201 @return A return value from gST->StdErr->OutputString.\r
202**/\r
203EFI_STATUS\r
204EFIAPI\r
205FileInterfaceStdErrWrite(\r
206 IN EFI_FILE_PROTOCOL *This,\r
207 IN OUT UINTN *BufferSize,\r
208 IN VOID *Buffer\r
209 )\r
210{\r
211 return (gST->StdErr->OutputString(gST->StdErr, Buffer));\r
212}\r
213\r
214/**\r
215 File style interface for console StdOut (Read).\r
216\r
217 @param[in] This Ignored.\r
218 @param[in, out] BufferSize Ignored.\r
219 @param[out] Buffer Ignored.\r
220\r
221 @retval EFI_UNSUPPORTED\r
222**/\r
223EFI_STATUS\r
224EFIAPI\r
225FileInterfaceStdOutRead(\r
226 IN EFI_FILE_PROTOCOL *This,\r
227 IN OUT UINTN *BufferSize,\r
228 OUT VOID *Buffer\r
229 )\r
230{\r
231 return (EFI_UNSUPPORTED);\r
232}\r
233\r
234/**\r
235 File style interface for console StdErr (Read).\r
236\r
237 @param[in] This Ignored.\r
238 @param[in, out] BufferSize Ignored.\r
239 @param[out] Buffer Ignored.\r
240\r
241 @retval EFI_UNSUPPORTED Always.\r
242**/\r
243EFI_STATUS\r
244EFIAPI\r
245FileInterfaceStdErrRead(\r
246 IN EFI_FILE_PROTOCOL *This,\r
247 IN OUT UINTN *BufferSize,\r
248 OUT VOID *Buffer\r
249 )\r
250{\r
251 return (EFI_UNSUPPORTED);\r
252}\r
253\r
254/**\r
255 File style interface for NUL file (Read).\r
256\r
257 @param[in] This Ignored.\r
258 @param[in, out] BufferSize Poiner to 0 upon return.\r
259 @param[out] Buffer Ignored.\r
260\r
261 @retval EFI_SUCCESS Always.\r
262**/\r
263EFI_STATUS\r
264EFIAPI\r
265FileInterfaceNulRead(\r
266 IN EFI_FILE_PROTOCOL *This,\r
267 IN OUT UINTN *BufferSize,\r
268 OUT VOID *Buffer\r
269 )\r
270{\r
271 *BufferSize = 0;\r
272 return (EFI_SUCCESS);\r
273}\r
274\r
275/**\r
276 File style interface for NUL file (Write).\r
277\r
278 @param[in] This Ignored.\r
279 @param[in, out] BufferSize Ignored.\r
280 @param[in] Buffer Ignored.\r
281\r
282 @retval EFI_SUCCESS\r
283**/\r
284EFI_STATUS\r
285EFIAPI\r
286FileInterfaceNulWrite(\r
287 IN EFI_FILE_PROTOCOL *This,\r
288 IN OUT UINTN *BufferSize,\r
289 IN VOID *Buffer\r
290 )\r
291{\r
292 return (EFI_SUCCESS);\r
293}\r
294\r
295/**\r
296 Create the TAB completion list.\r
297\r
298 @param[in] InputString The command line to expand.\r
299 @param[in] StringLen Length of the command line.\r
300 @param[in] BufferSize Buffer size.\r
301 @param[in, out] TabCompletionList Return the TAB completion list.\r
302 @param[in, out] TabUpdatePos Return the TAB update position.\r
303**/\r
304EFI_STATUS\r
305CreateTabCompletionList (\r
306 IN CONST CHAR16 *InputString,\r
307 IN CONST UINTN StringLen,\r
308 IN CONST UINTN BufferSize,\r
309 IN OUT EFI_SHELL_FILE_INFO **TabCompletionList,\r
310 IN OUT UINTN *TabUpdatePos\r
311)\r
312{\r
313 BOOLEAN InQuotation;\r
314 UINTN TabPos;\r
315 UINTN Index;\r
316 CONST CHAR16 *Cwd;\r
317 EFI_STATUS Status;\r
318 CHAR16 *TabStr;\r
319 EFI_SHELL_FILE_INFO *FileList;\r
320 EFI_SHELL_FILE_INFO *FileInfo;\r
321 EFI_SHELL_FILE_INFO *TempFileInfo;\r
322\r
323 //\r
324 // Allocate buffers\r
325 //\r
326 TabStr = AllocateZeroPool (BufferSize);\r
327 if (TabStr == NULL) {\r
328 return EFI_OUT_OF_RESOURCES;\r
329 }\r
330\r
331 //\r
332 // handle auto complete of file and directory names...\r
333 // E.g.: cd fs0:\EFI\Bo<TAB>\r
334 // ^ ^\r
335 // TabPos TabUpdatePos\r
336 //\r
337 TabPos = 0;\r
338 *TabUpdatePos = 0;\r
339 FileList = NULL;\r
340 InQuotation = FALSE;\r
341 for (Index = 0; Index < StringLen; Index++) {\r
342 switch (InputString[Index]) {\r
343 case L'\"':\r
344 InQuotation = (BOOLEAN) (!InQuotation);\r
345 break;\r
346\r
347 case L' ':\r
348 if (!InQuotation) {\r
349 TabPos = Index + 1;\r
350 *TabUpdatePos = TabPos;\r
351 }\r
352 break;\r
353\r
354 case L':':\r
355 //\r
356 // handle the case "fs0:<TAB>"\r
357 // Update the TabUpdatePos as well.\r
358 //\r
359 case L'\\':\r
360 *TabUpdatePos = Index + 1;\r
361 break;\r
362\r
363 default:\r
364 break;\r
365 }\r
366 }\r
367\r
368 if (StrStr (InputString + TabPos, L":") == NULL) {\r
369 //\r
370 // If file path doesn't contain ":", ...\r
371 //\r
372 Cwd = ShellInfoObject.NewEfiShellProtocol->GetCurDir (NULL);\r
373 if (Cwd != NULL) {\r
374 if (InputString[TabPos] != L'\\') {\r
375 //\r
376 // and it doesn't begin with "\\", it's a path relative to current directory.\r
377 // TabStr = "<cwd>\\"\r
378 //\r
379 StrnCpyS (TabStr, BufferSize / sizeof (CHAR16), Cwd, (BufferSize) / sizeof (CHAR16) - 1);\r
380 StrCatS (TabStr, (BufferSize) / sizeof (CHAR16), L"\\");\r
381 } else {\r
382 //\r
383 // and it begins with "\\", it's a path pointing to root directory of current map.\r
384 // TabStr = "fsx:"\r
385 //\r
386 Index = StrStr (Cwd, L":") - Cwd + 1;\r
387 StrnCpyS (TabStr, BufferSize / sizeof (CHAR16), Cwd, Index);\r
388 }\r
389 }\r
390 }\r
391 StrnCatS (TabStr, (BufferSize) / sizeof (CHAR16), InputString + TabPos, StringLen - TabPos);\r
392 StrnCatS (TabStr, (BufferSize) / sizeof (CHAR16), L"*", (BufferSize) / sizeof (CHAR16) - 1 - StrLen (TabStr));\r
393 Status = ShellInfoObject.NewEfiShellProtocol->FindFiles(TabStr, &FileList);\r
394\r
395 //\r
396 // Filter out the non-directory for "CD" command\r
397 // Filter "." and ".." for all\r
398 //\r
399 if (!EFI_ERROR (Status) && FileList != NULL) {\r
400 //\r
401 // Skip the spaces in the beginning\r
402 //\r
403 while (*InputString == L' ') {\r
404 InputString++;\r
405 }\r
406\r
407 for (FileInfo = (EFI_SHELL_FILE_INFO *) GetFirstNode (&FileList->Link); !IsNull (&FileList->Link, &FileInfo->Link); ) {\r
408 if (((StrCmp (FileInfo->FileName, L".") == 0) || (StrCmp (FileInfo->FileName, L"..") == 0)) ||\r
409 (((InputString[0] == L'c' || InputString[0] == L'C') && (InputString[1] == L'd' || InputString[1] == L'D')) &&\r
410 (ShellIsDirectory (FileInfo->FullName) != EFI_SUCCESS))) {\r
411 TempFileInfo = FileInfo;\r
412 FileInfo = (EFI_SHELL_FILE_INFO *) RemoveEntryList (&FileInfo->Link);\r
413 InternalFreeShellFileInfoNode (TempFileInfo);\r
414 } else {\r
415 FileInfo = (EFI_SHELL_FILE_INFO *) GetNextNode (&FileList->Link, &FileInfo->Link);\r
416 }\r
417 }\r
418 }\r
419\r
420 if (FileList != NULL && !IsListEmpty (&FileList->Link)) {\r
421 Status = EFI_SUCCESS;\r
422 } else {\r
423 ShellInfoObject.NewEfiShellProtocol->FreeFileList (&FileList);\r
424 Status = EFI_NOT_FOUND;\r
425 }\r
426\r
427 FreePool (TabStr);\r
428\r
429 *TabCompletionList = FileList;\r
430 return Status;\r
431}\r
432\r
433/**\r
434 File style interface for console (Read).\r
435\r
436 This will return a single line of input from the console.\r
437\r
438 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
439 file handle to read data from. Not used.\r
440 @param BufferSize On input, the size of the Buffer. On output, the amount\r
441 of data returned in Buffer. In both cases, the size is\r
442 measured in bytes.\r
443 @param Buffer The buffer into which the data is read.\r
444\r
445\r
446 @retval EFI_SUCCESS The data was read.\r
447 @retval EFI_NO_MEDIA The device has no medium.\r
448 @retval EFI_DEVICE_ERROR The device reported an error.\r
449 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted file.\r
450 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond the end of the file.\r
451 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
452 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory\r
453 entry. BufferSize has been updated with the size\r
454 needed to complete the request.\r
455 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
456**/\r
457EFI_STATUS\r
458EFIAPI\r
459FileInterfaceStdInRead(\r
460 IN EFI_FILE_PROTOCOL *This,\r
461 IN OUT UINTN *BufferSize,\r
462 OUT VOID *Buffer\r
463 )\r
464{\r
465 CHAR16 *CurrentString;\r
466 BOOLEAN Done;\r
467 UINTN TabUpdatePos; // Start index of the string updated by TAB stroke\r
468 UINTN Column; // Column of current cursor\r
469 UINTN Row; // Row of current cursor\r
470 UINTN StartColumn; // Column at the beginning of the line\r
471 UINTN Update; // Line index for update\r
472 UINTN Delete; // Num of chars to delete from console after update\r
473 UINTN StringLen; // Total length of the line\r
474 UINTN StringCurPos; // Line index corresponding to the cursor\r
475 UINTN MaxStr; // Maximum possible line length\r
476 UINTN TotalColumn; // Num of columns in the console\r
477 UINTN TotalRow; // Num of rows in the console\r
478 UINTN SkipLength;\r
479 UINTN OutputLength; // Length of the update string\r
480 UINTN TailRow; // Row of end of line\r
481 UINTN TailColumn; // Column of end of line\r
482 EFI_INPUT_KEY Key;\r
483\r
484 BUFFER_LIST *LinePos;\r
485 BUFFER_LIST *NewPos;\r
486 BOOLEAN InScrolling;\r
487 EFI_STATUS Status;\r
488 BOOLEAN InTabScrolling; // Whether in TAB-completion state\r
489 EFI_SHELL_FILE_INFO *TabCompleteList;\r
490 EFI_SHELL_FILE_INFO *TabCurrent;\r
491 UINTN EventIndex;\r
492 CHAR16 *TabOutputStr;\r
493\r
494 //\r
495 // If buffer is not large enough to hold a CHAR16, return minimum buffer size\r
496 //\r
497 if (*BufferSize < sizeof (CHAR16) * 2) {\r
498 *BufferSize = sizeof (CHAR16) * 2;\r
499 return (EFI_BUFFER_TOO_SMALL);\r
500 }\r
501\r
502 Done = FALSE;\r
503 CurrentString = Buffer;\r
504 StringLen = 0;\r
505 StringCurPos = 0;\r
506 OutputLength = 0;\r
507 Update = 0;\r
508 Delete = 0;\r
509 LinePos = NewPos = (BUFFER_LIST*)(&ShellInfoObject.ViewingSettings.CommandHistory);\r
510 InScrolling = FALSE;\r
511 InTabScrolling = FALSE;\r
512 Status = EFI_SUCCESS;\r
513 TabOutputStr = NULL;\r
514 TabUpdatePos = 0;\r
515 TabCompleteList = NULL;\r
516 TabCurrent = NULL;\r
517\r
518 //\r
519 // Get the screen setting and the current cursor location\r
520 //\r
521 Column = StartColumn = gST->ConOut->Mode->CursorColumn;\r
522 Row = gST->ConOut->Mode->CursorRow;\r
523 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &TotalColumn, &TotalRow);\r
524\r
525 //\r
526 // Limit the line length to the buffer size or the minimun size of the\r
527 // screen. (The smaller takes effect)\r
528 //\r
529 MaxStr = TotalColumn * (TotalRow - 1) - StartColumn;\r
530 if (MaxStr > *BufferSize / sizeof (CHAR16)) {\r
531 MaxStr = *BufferSize / sizeof (CHAR16);\r
532 }\r
533 ZeroMem (CurrentString, MaxStr * sizeof (CHAR16));\r
534 do {\r
535 //\r
536 // Read a key\r
537 //\r
538 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
539 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
540 if (EFI_ERROR (Status)) {\r
541\r
542 if (Status == EFI_NOT_READY)\r
543 continue;\r
544\r
545 ZeroMem (CurrentString, MaxStr * sizeof(CHAR16));\r
546 StringLen = 0;\r
547 break;\r
548 }\r
549\r
550 //\r
551 // Press PageUp or PageDown to scroll the history screen up or down.\r
552 // Press any other key to quit scrolling.\r
553 //\r
554 if (Key.UnicodeChar == 0 && (Key.ScanCode == SCAN_PAGE_UP || Key.ScanCode == SCAN_PAGE_DOWN)) {\r
555 if (Key.ScanCode == SCAN_PAGE_UP) {\r
556 ConsoleLoggerDisplayHistory(FALSE, 0, ShellInfoObject.ConsoleInfo);\r
557 } else if (Key.ScanCode == SCAN_PAGE_DOWN) {\r
558 ConsoleLoggerDisplayHistory(TRUE, 0, ShellInfoObject.ConsoleInfo);\r
559 }\r
560\r
561 InScrolling = TRUE;\r
562 } else {\r
563 if (InScrolling) {\r
564 ConsoleLoggerStopHistory(ShellInfoObject.ConsoleInfo);\r
565 InScrolling = FALSE;\r
566 }\r
567 }\r
568\r
569 //\r
570 // If we are quitting TAB scrolling...\r
571 //\r
572 if (InTabScrolling && Key.UnicodeChar != CHAR_TAB) {\r
573 if (TabCompleteList != NULL) {\r
574 ShellInfoObject.NewEfiShellProtocol->FreeFileList (&TabCompleteList);\r
575 DEBUG_CODE(TabCompleteList = NULL;);\r
576 }\r
577 InTabScrolling = FALSE;\r
578 }\r
579\r
580 switch (Key.UnicodeChar) {\r
581 case CHAR_CARRIAGE_RETURN:\r
582 //\r
583 // All done, print a newline at the end of the string\r
584 //\r
585 TailRow = Row + (StringLen - StringCurPos + Column) / TotalColumn;\r
586 TailColumn = (StringLen - StringCurPos + Column) % TotalColumn;\r
587 ShellPrintEx ((INT32)TailColumn, (INT32)TailRow, L"%N\n");\r
588 Done = TRUE;\r
589 break;\r
590\r
591 case CHAR_BACKSPACE:\r
592 if (StringCurPos != 0) {\r
593 //\r
594 // If not move back beyond string beginning, move all characters behind\r
595 // the current position one character forward\r
596 //\r
597 StringCurPos--;\r
598 Update = StringCurPos;\r
599 Delete = 1;\r
600 CopyMem (CurrentString + StringCurPos, CurrentString + StringCurPos + 1, sizeof (CHAR16) * (StringLen - StringCurPos));\r
601\r
602 //\r
603 // Adjust the current column and row\r
604 //\r
605 MoveCursorBackward (TotalColumn, &Column, &Row);\r
606 }\r
607 break;\r
608\r
609 case CHAR_TAB:\r
610 if (!InTabScrolling) {\r
611 TabCurrent = NULL;\r
612 //\r
613 // Initialize a tab complete operation.\r
614 //\r
615 Status = CreateTabCompletionList (CurrentString, StringLen, *BufferSize, &TabCompleteList, &TabUpdatePos);\r
616 if (!EFI_ERROR(Status)) {\r
617 InTabScrolling = TRUE;\r
618 }\r
619\r
620 //\r
621 // We do not set up the replacement.\r
622 // The next section will do that.\r
623 //\r
624 }\r
625\r
626 if (InTabScrolling) {\r
627 //\r
628 // We are in a tab complete operation.\r
629 // set up the next replacement.\r
630 //\r
631 ASSERT(TabCompleteList != NULL);\r
632 if (TabCurrent == NULL) {\r
633 TabCurrent = (EFI_SHELL_FILE_INFO*) GetFirstNode (&TabCompleteList->Link);\r
634 } else {\r
635 TabCurrent = (EFI_SHELL_FILE_INFO*) GetNextNode (&TabCompleteList->Link, &TabCurrent->Link);\r
636 }\r
637\r
638 //\r
639 // Skip over the empty list beginning node\r
640 //\r
641 if (IsNull(&TabCompleteList->Link, &TabCurrent->Link)) {\r
642 TabCurrent = (EFI_SHELL_FILE_INFO*) GetNextNode (&TabCompleteList->Link, &TabCurrent->Link);\r
643 }\r
644 }\r
645 break;\r
646\r
647 default:\r
648 if (Key.UnicodeChar >= ' ') {\r
649 //\r
650 // If we are at the buffer's end, drop the key\r
651 //\r
652 if (StringLen == MaxStr - 1 && (ShellInfoObject.ViewingSettings.InsertMode || StringCurPos == StringLen)) {\r
653 break;\r
654 }\r
655 //\r
656 // If in insert mode, make space by moving each other character 1\r
657 // space higher in the array\r
658 //\r
659 if (ShellInfoObject.ViewingSettings.InsertMode) {\r
660 CopyMem(CurrentString + StringCurPos + 1, CurrentString + StringCurPos, (StringLen - StringCurPos)*sizeof(CurrentString[0]));\r
661 }\r
662\r
663 CurrentString[StringCurPos] = Key.UnicodeChar;\r
664 Update = StringCurPos;\r
665\r
666 StringCurPos += 1;\r
667 OutputLength = 1;\r
668 }\r
669 break;\r
670\r
671 case 0:\r
672 switch (Key.ScanCode) {\r
673 case SCAN_DELETE:\r
674 //\r
675 // Move characters behind current position one character forward\r
676 //\r
677 if (StringLen != 0) {\r
678 Update = StringCurPos;\r
679 Delete = 1;\r
680 CopyMem (CurrentString + StringCurPos, CurrentString + StringCurPos + 1, sizeof (CHAR16) * (StringLen - StringCurPos));\r
681 }\r
682 break;\r
683\r
684 case SCAN_UP:\r
685 //\r
686 // Prepare to print the previous command\r
687 //\r
688 NewPos = (BUFFER_LIST*)GetPreviousNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link);\r
689 if (IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link)) {\r
690 NewPos = (BUFFER_LIST*)GetPreviousNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link);\r
691 }\r
692 break;\r
693\r
694 case SCAN_DOWN:\r
695 //\r
696 // Prepare to print the next command\r
697 //\r
698 NewPos = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link);\r
699 if (NewPos == (BUFFER_LIST*)(&ShellInfoObject.ViewingSettings.CommandHistory)) {\r
700 NewPos = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link);\r
701 }\r
702 break;\r
703\r
704 case SCAN_LEFT:\r
705 //\r
706 // Adjust current cursor position\r
707 //\r
708 if (StringCurPos != 0) {\r
709 --StringCurPos;\r
710 MoveCursorBackward (TotalColumn, &Column, &Row);\r
711 }\r
712 break;\r
713\r
714 case SCAN_RIGHT:\r
715 //\r
716 // Adjust current cursor position\r
717 //\r
718 if (StringCurPos < StringLen) {\r
719 ++StringCurPos;\r
720 MoveCursorForward (TotalColumn, TotalRow, &Column, &Row);\r
721 }\r
722 break;\r
723\r
724 case SCAN_HOME:\r
725 //\r
726 // Move current cursor position to the beginning of the command line\r
727 //\r
728 Row -= (StringCurPos + StartColumn) / TotalColumn;\r
729 Column = StartColumn;\r
730 StringCurPos = 0;\r
731 break;\r
732\r
733 case SCAN_END:\r
734 //\r
735 // Move current cursor position to the end of the command line\r
736 //\r
737 TailRow = Row + (StringLen - StringCurPos + Column) / TotalColumn;\r
738 TailColumn = (StringLen - StringCurPos + Column) % TotalColumn;\r
739 Row = TailRow;\r
740 Column = TailColumn;\r
741 StringCurPos = StringLen;\r
742 break;\r
743\r
744 case SCAN_ESC:\r
745 //\r
746 // Prepare to clear the current command line\r
747 //\r
748 CurrentString[0] = 0;\r
749 Update = 0;\r
750 Delete = StringLen;\r
751 Row -= (StringCurPos + StartColumn) / TotalColumn;\r
752 Column = StartColumn;\r
753 OutputLength = 0;\r
754 break;\r
755\r
756 case SCAN_INSERT:\r
757 //\r
758 // Toggle the SEnvInsertMode flag\r
759 //\r
760 ShellInfoObject.ViewingSettings.InsertMode = (BOOLEAN)!ShellInfoObject.ViewingSettings.InsertMode;\r
761 break;\r
762\r
763 case SCAN_F7:\r
764 //\r
765 // Print command history\r
766 //\r
767 PrintCommandHistory (TotalColumn, TotalRow, 4);\r
768 *CurrentString = CHAR_NULL;\r
769 Done = TRUE;\r
770 break;\r
771 }\r
772 }\r
773\r
774 if (Done) {\r
775 break;\r
776 }\r
777\r
778 //\r
779 // If we are in auto-complete mode, we are preparing to print\r
780 // the next file or directory name\r
781 //\r
782 if (InTabScrolling) {\r
783 TabOutputStr = AllocateZeroPool (*BufferSize);\r
784 if (TabOutputStr == NULL) {\r
785 Status = EFI_OUT_OF_RESOURCES;\r
786 }\r
787 }\r
788\r
789 if (InTabScrolling && TabOutputStr != NULL) {\r
790\r
791 //\r
792 // Adjust the column and row to the start of TAB-completion string.\r
793 //\r
794 Column = (StartColumn + TabUpdatePos) % TotalColumn;\r
795 Row -= (StartColumn + StringCurPos) / TotalColumn - (StartColumn + TabUpdatePos) / TotalColumn;\r
796 OutputLength = StrLen (TabCurrent->FileName);\r
797 //\r
798 // if the output string contains blank space, quotation marks L'\"'\r
799 // should be added to the output.\r
800 //\r
801 if (StrStr(TabCurrent->FileName, L" ") != NULL){\r
802 TabOutputStr[0] = L'\"';\r
803 CopyMem (TabOutputStr + 1, TabCurrent->FileName, OutputLength * sizeof (CHAR16));\r
804 TabOutputStr[OutputLength + 1] = L'\"';\r
805 TabOutputStr[OutputLength + 2] = CHAR_NULL;\r
806 } else {\r
807 CopyMem (TabOutputStr, TabCurrent->FileName, OutputLength * sizeof (CHAR16));\r
808 TabOutputStr[OutputLength] = CHAR_NULL;\r
809 }\r
810 OutputLength = StrLen (TabOutputStr) < MaxStr - 1 ? StrLen (TabOutputStr) : MaxStr - 1;\r
811 CopyMem (CurrentString + TabUpdatePos, TabOutputStr, OutputLength * sizeof (CHAR16));\r
812 CurrentString[TabUpdatePos + OutputLength] = CHAR_NULL;\r
813 StringCurPos = TabUpdatePos + OutputLength;\r
814 Update = TabUpdatePos;\r
815 if (StringLen > TabUpdatePos + OutputLength) {\r
816 Delete = StringLen - TabUpdatePos - OutputLength;\r
817 }\r
818\r
819 FreePool(TabOutputStr);\r
820 }\r
821\r
822 //\r
823 // If we have a new position, we are preparing to print a previous or\r
824 // next command.\r
825 //\r
826 if (NewPos != (BUFFER_LIST*)(&ShellInfoObject.ViewingSettings.CommandHistory)) {\r
827 Column = StartColumn;\r
828 Row -= (StringCurPos + StartColumn) / TotalColumn;\r
829\r
830 LinePos = NewPos;\r
831 NewPos = (BUFFER_LIST*)(&ShellInfoObject.ViewingSettings.CommandHistory);\r
832\r
833 OutputLength = StrLen (LinePos->Buffer) < MaxStr - 1 ? StrLen (LinePos->Buffer) : MaxStr - 1;\r
834 CopyMem (CurrentString, LinePos->Buffer, OutputLength * sizeof (CHAR16));\r
835 CurrentString[OutputLength] = CHAR_NULL;\r
836\r
837 StringCurPos = OutputLength;\r
838\r
839 //\r
840 // Draw new input string\r
841 //\r
842 Update = 0;\r
843 if (StringLen > OutputLength) {\r
844 //\r
845 // If old string was longer, blank its tail\r
846 //\r
847 Delete = StringLen - OutputLength;\r
848 }\r
849 }\r
850 //\r
851 // If we need to update the output do so now\r
852 //\r
853 if (Update != (UINTN) -1) {\r
854 ShellPrintEx ((INT32)Column, (INT32)Row, L"%s%.*s", CurrentString + Update, Delete, L"");\r
855 StringLen = StrLen (CurrentString);\r
856\r
857 if (Delete != 0) {\r
858 SetMem (CurrentString + StringLen, Delete * sizeof (CHAR16), CHAR_NULL);\r
859 }\r
860\r
861 if (StringCurPos > StringLen) {\r
862 StringCurPos = StringLen;\r
863 }\r
864\r
865 Update = (UINTN) -1;\r
866\r
867 //\r
868 // After using print to reflect newly updates, if we're not using\r
869 // BACKSPACE and DELETE, we need to move the cursor position forward,\r
870 // so adjust row and column here.\r
871 //\r
872 if (Key.UnicodeChar != CHAR_BACKSPACE && !(Key.UnicodeChar == 0 && Key.ScanCode == SCAN_DELETE)) {\r
873 //\r
874 // Calulate row and column of the tail of current string\r
875 //\r
876 TailRow = Row + (StringLen - StringCurPos + Column + OutputLength) / TotalColumn;\r
877 TailColumn = (StringLen - StringCurPos + Column + OutputLength) % TotalColumn;\r
878\r
879 //\r
880 // If the tail of string reaches screen end, screen rolls up, so if\r
881 // Row does not equal TailRow, Row should be decremented\r
882 //\r
883 // (if we are recalling commands using UPPER and DOWN key, and if the\r
884 // old command is too long to fit the screen, TailColumn must be 79.\r
885 //\r
886 if (TailColumn == 0 && TailRow >= TotalRow && Row != TailRow) {\r
887 Row--;\r
888 }\r
889 //\r
890 // Calculate the cursor position after current operation. If cursor\r
891 // reaches line end, update both row and column, otherwise, only\r
892 // column will be changed.\r
893 //\r
894 if (Column + OutputLength >= TotalColumn) {\r
895 SkipLength = OutputLength - (TotalColumn - Column);\r
896\r
897 Row += SkipLength / TotalColumn + 1;\r
898 if (Row > TotalRow - 1) {\r
899 Row = TotalRow - 1;\r
900 }\r
901\r
902 Column = SkipLength % TotalColumn;\r
903 } else {\r
904 Column += OutputLength;\r
905 }\r
906 }\r
907\r
908 Delete = 0;\r
909 }\r
910 //\r
911 // Set the cursor position for this key\r
912 //\r
913 gST->ConOut->SetCursorPosition (gST->ConOut, Column, Row);\r
914 } while (!Done);\r
915\r
916 if (CurrentString != NULL && StrLen(CurrentString) > 0) {\r
917 //\r
918 // add the line to the history buffer\r
919 //\r
920 AddLineToCommandHistory(CurrentString);\r
921 }\r
922\r
923 //\r
924 // Return the data to the caller\r
925 //\r
926 *BufferSize = StringLen * sizeof (CHAR16);\r
927\r
928 //\r
929 // if this was used it should be deallocated by now...\r
930 // prevent memory leaks...\r
931 //\r
932 if (TabCompleteList != NULL) {\r
933 ShellInfoObject.NewEfiShellProtocol->FreeFileList (&TabCompleteList);\r
934 }\r
935 ASSERT(TabCompleteList == NULL);\r
936\r
937 return Status;\r
938}\r
939\r
940//\r
941// FILE sytle interfaces for StdIn/StdOut/StdErr\r
942//\r
943EFI_FILE_PROTOCOL FileInterfaceStdIn = {\r
944 EFI_FILE_REVISION,\r
945 FileInterfaceOpenNotFound,\r
946 FileInterfaceNopGeneric,\r
947 FileInterfaceNopGeneric,\r
948 FileInterfaceStdInRead,\r
949 FileInterfaceStdInWrite,\r
950 FileInterfaceNopGetPosition,\r
951 FileInterfaceNopSetPosition,\r
952 FileInterfaceNopGetInfo,\r
953 FileInterfaceNopSetInfo,\r
954 FileInterfaceNopGeneric\r
955};\r
956\r
957EFI_FILE_PROTOCOL FileInterfaceStdOut = {\r
958 EFI_FILE_REVISION,\r
959 FileInterfaceOpenNotFound,\r
960 FileInterfaceNopGeneric,\r
961 FileInterfaceNopGeneric,\r
962 FileInterfaceStdOutRead,\r
963 FileInterfaceStdOutWrite,\r
964 FileInterfaceNopGetPosition,\r
965 FileInterfaceNopSetPosition,\r
966 FileInterfaceNopGetInfo,\r
967 FileInterfaceNopSetInfo,\r
968 FileInterfaceNopGeneric\r
969};\r
970\r
971EFI_FILE_PROTOCOL FileInterfaceStdErr = {\r
972 EFI_FILE_REVISION,\r
973 FileInterfaceOpenNotFound,\r
974 FileInterfaceNopGeneric,\r
975 FileInterfaceNopGeneric,\r
976 FileInterfaceStdErrRead,\r
977 FileInterfaceStdErrWrite,\r
978 FileInterfaceNopGetPosition,\r
979 FileInterfaceNopSetPosition,\r
980 FileInterfaceNopGetInfo,\r
981 FileInterfaceNopSetInfo,\r
982 FileInterfaceNopGeneric\r
983};\r
984\r
985EFI_FILE_PROTOCOL FileInterfaceNulFile = {\r
986 EFI_FILE_REVISION,\r
987 FileInterfaceOpenNotFound,\r
988 FileInterfaceNopGeneric,\r
989 FileInterfaceNopGeneric,\r
990 FileInterfaceNulRead,\r
991 FileInterfaceNulWrite,\r
992 FileInterfaceNopGetPosition,\r
993 FileInterfaceNopSetPosition,\r
994 FileInterfaceNopGetInfo,\r
995 FileInterfaceNopSetInfo,\r
996 FileInterfaceNopGeneric\r
997};\r
998\r
999\r
1000\r
1001\r
1002//\r
1003// This is identical to EFI_FILE_PROTOCOL except for the additional member\r
1004// for the name.\r
1005//\r
1006\r
1007typedef struct {\r
1008 UINT64 Revision;\r
1009 EFI_FILE_OPEN Open;\r
1010 EFI_FILE_CLOSE Close;\r
1011 EFI_FILE_DELETE Delete;\r
1012 EFI_FILE_READ Read;\r
1013 EFI_FILE_WRITE Write;\r
1014 EFI_FILE_GET_POSITION GetPosition;\r
1015 EFI_FILE_SET_POSITION SetPosition;\r
1016 EFI_FILE_GET_INFO GetInfo;\r
1017 EFI_FILE_SET_INFO SetInfo;\r
1018 EFI_FILE_FLUSH Flush;\r
1019 CHAR16 Name[1];\r
1020} EFI_FILE_PROTOCOL_ENVIRONMENT;\r
1021//ANSI compliance helper to get size of the struct.\r
1022#define SIZE_OF_EFI_FILE_PROTOCOL_ENVIRONMENT EFI_FIELD_OFFSET (EFI_FILE_PROTOCOL_ENVIRONMENT, Name)\r
1023\r
1024/**\r
1025 File style interface for Environment Variable (Close).\r
1026\r
1027 Frees the memory for this object.\r
1028\r
1029 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1030\r
1031 @retval EFI_SUCCESS\r
1032**/\r
1033EFI_STATUS\r
1034EFIAPI\r
1035FileInterfaceEnvClose(\r
1036 IN EFI_FILE_PROTOCOL *This\r
1037 )\r
1038{\r
1039 VOID* NewBuffer;\r
1040 UINTN NewSize;\r
1041 EFI_STATUS Status;\r
1042 BOOLEAN Volatile;\r
1043 UINTN TotalSize;\r
1044\r
1045 //\r
1046 // Most if not all UEFI commands will have an '\r\n' at the end of any output.\r
1047 // Since the output was redirected to a variable, it does not make sense to\r
1048 // keep this. So, before closing, strip the trailing '\r\n' from the variable\r
1049 // if it exists.\r
1050 //\r
1051 NewBuffer = NULL;\r
1052 NewSize = 0;\r
1053 TotalSize = 0;\r
1054\r
1055 Status = IsVolatileEnv (((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &Volatile);\r
1056 if (EFI_ERROR (Status)) {\r
1057 return Status;\r
1058 }\r
1059\r
1060 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1061 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1062 TotalSize = NewSize + sizeof (CHAR16);\r
1063 NewBuffer = AllocateZeroPool (TotalSize);\r
1064 if (NewBuffer == NULL) {\r
1065 return EFI_OUT_OF_RESOURCES;\r
1066 }\r
1067 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1068 }\r
1069\r
1070 if (!EFI_ERROR(Status) && NewBuffer != NULL) {\r
1071\r
1072 if (TotalSize / sizeof (CHAR16) >= 3) {\r
1073 if ( (((CHAR16*)NewBuffer)[TotalSize / sizeof (CHAR16) - 2] == CHAR_LINEFEED) &&\r
1074 (((CHAR16*)NewBuffer)[TotalSize / sizeof (CHAR16) - 3] == CHAR_CARRIAGE_RETURN)\r
1075 ) {\r
1076 ((CHAR16*)NewBuffer)[TotalSize / sizeof (CHAR16) - 3] = CHAR_NULL;\r
1077 //\r
1078 // If the NewBuffer end with \r\n\0, We will repace '\r' by '\0' and then update TotalSize.\r
1079 //\r
1080 TotalSize -= sizeof(CHAR16) * 2;\r
1081 }\r
1082\r
1083 if (Volatile) {\r
1084 Status = SHELL_SET_ENVIRONMENT_VARIABLE_V (\r
1085 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1086 TotalSize - sizeof (CHAR16),\r
1087 NewBuffer\r
1088 );\r
1089\r
1090 if (!EFI_ERROR(Status)) {\r
1091 Status = ShellAddEnvVarToList (\r
1092 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1093 NewBuffer,\r
1094 TotalSize,\r
1095 EFI_VARIABLE_BOOTSERVICE_ACCESS\r
1096 );\r
1097 }\r
1098 } else {\r
1099 Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV (\r
1100 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1101 TotalSize - sizeof (CHAR16),\r
1102 NewBuffer\r
1103 );\r
1104\r
1105 if (!EFI_ERROR(Status)) {\r
1106 Status = ShellAddEnvVarToList (\r
1107 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1108 NewBuffer,\r
1109 TotalSize,\r
1110 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
1111 );\r
1112 }\r
1113 }\r
1114 }\r
1115 }\r
1116\r
1117 SHELL_FREE_NON_NULL(NewBuffer);\r
1118 FreePool((EFI_FILE_PROTOCOL_ENVIRONMENT*)This);\r
1119 return (Status);\r
1120}\r
1121\r
1122/**\r
1123 File style interface for Environment Variable (Delete).\r
1124\r
1125 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1126\r
1127 @retval The return value from FileInterfaceEnvClose().\r
1128**/\r
1129EFI_STATUS\r
1130EFIAPI\r
1131FileInterfaceEnvDelete(\r
1132 IN EFI_FILE_PROTOCOL *This\r
1133 )\r
1134{\r
1135 SHELL_DELETE_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name);\r
1136 return (FileInterfaceEnvClose(This));\r
1137}\r
1138\r
1139/**\r
1140 File style interface for Environment Variable (Read).\r
1141\r
1142 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1143 @param[in, out] BufferSize Size in bytes of Buffer.\r
1144 @param[out] Buffer The pointer to the buffer to fill.\r
1145\r
1146 @retval EFI_SUCCESS The data was read.\r
1147**/\r
1148EFI_STATUS\r
1149EFIAPI\r
1150FileInterfaceEnvRead(\r
1151 IN EFI_FILE_PROTOCOL *This,\r
1152 IN OUT UINTN *BufferSize,\r
1153 OUT VOID *Buffer\r
1154 )\r
1155{\r
1156 return (SHELL_GET_ENVIRONMENT_VARIABLE(\r
1157 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1158 BufferSize,\r
1159 Buffer));\r
1160}\r
1161\r
1162/**\r
1163 File style interface for Volatile Environment Variable (Write).\r
1164 This function also caches the environment variable into gShellEnvVarList.\r
1165\r
1166 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1167 @param[in, out] BufferSize Size in bytes of Buffer.\r
1168 @param[in] Buffer The pointer to the buffer to write.\r
1169\r
1170 @retval EFI_SUCCESS The data was successfully write to variable.\r
1171 @retval SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
1172**/\r
1173EFI_STATUS\r
1174EFIAPI\r
1175FileInterfaceEnvVolWrite(\r
1176 IN EFI_FILE_PROTOCOL *This,\r
1177 IN OUT UINTN *BufferSize,\r
1178 IN VOID *Buffer\r
1179 )\r
1180{\r
1181 VOID* NewBuffer;\r
1182 UINTN NewSize;\r
1183 EFI_STATUS Status;\r
1184 UINTN TotalSize;\r
1185\r
1186 NewBuffer = NULL;\r
1187 NewSize = 0;\r
1188 TotalSize = 0;\r
1189\r
1190 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1191 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1192 TotalSize = NewSize + *BufferSize + sizeof (CHAR16);\r
1193 } else if (Status == EFI_NOT_FOUND) {\r
1194 TotalSize = *BufferSize + sizeof(CHAR16);\r
1195 } else {\r
1196 return Status;\r
1197 }\r
1198\r
1199 NewBuffer = AllocateZeroPool (TotalSize);\r
1200 if (NewBuffer == NULL) {\r
1201 return EFI_OUT_OF_RESOURCES;\r
1202 }\r
1203\r
1204 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1205 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1206 }\r
1207\r
1208 if (EFI_ERROR (Status) && Status != EFI_NOT_FOUND) {\r
1209 FreePool (NewBuffer);\r
1210 return Status;\r
1211 }\r
1212\r
1213 CopyMem ((UINT8*)NewBuffer + NewSize, Buffer, *BufferSize);\r
1214 Status = ShellAddEnvVarToList (\r
1215 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1216 NewBuffer,\r
1217 TotalSize,\r
1218 EFI_VARIABLE_BOOTSERVICE_ACCESS\r
1219 );\r
1220 if (EFI_ERROR(Status)) {\r
1221 FreePool (NewBuffer);\r
1222 return Status;\r
1223 }\r
1224\r
1225 Status = SHELL_SET_ENVIRONMENT_VARIABLE_V (\r
1226 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1227 TotalSize - sizeof (CHAR16),\r
1228 NewBuffer\r
1229 );\r
1230 if (EFI_ERROR(Status)) {\r
1231 ShellRemvoeEnvVarFromList (((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name);\r
1232 }\r
1233\r
1234 FreePool (NewBuffer);\r
1235 return Status;\r
1236}\r
1237\r
1238\r
1239/**\r
1240 File style interface for Non Volatile Environment Variable (Write).\r
1241 This function also caches the environment variable into gShellEnvVarList.\r
1242\r
1243 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1244 @param[in, out] BufferSize Size in bytes of Buffer.\r
1245 @param[in] Buffer The pointer to the buffer to write.\r
1246\r
1247 @retval EFI_SUCCESS The data was successfully write to variable.\r
1248 @retval SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
1249**/\r
1250EFI_STATUS\r
1251EFIAPI\r
1252FileInterfaceEnvNonVolWrite(\r
1253 IN EFI_FILE_PROTOCOL *This,\r
1254 IN OUT UINTN *BufferSize,\r
1255 IN VOID *Buffer\r
1256 )\r
1257{\r
1258 VOID* NewBuffer;\r
1259 UINTN NewSize;\r
1260 EFI_STATUS Status;\r
1261 UINTN TotalSize;\r
1262\r
1263 NewBuffer = NULL;\r
1264 NewSize = 0;\r
1265 TotalSize = 0;\r
1266\r
1267 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1268 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1269 TotalSize = NewSize + *BufferSize + sizeof (CHAR16);\r
1270 } else if (Status == EFI_NOT_FOUND) {\r
1271 TotalSize = *BufferSize + sizeof (CHAR16);\r
1272 } else {\r
1273 return Status;\r
1274 }\r
1275\r
1276 NewBuffer = AllocateZeroPool (TotalSize);\r
1277 if (NewBuffer == NULL) {\r
1278 return EFI_OUT_OF_RESOURCES;\r
1279 }\r
1280\r
1281 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1282 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1283 }\r
1284\r
1285 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) {\r
1286 FreePool (NewBuffer);\r
1287 return Status;\r
1288 }\r
1289\r
1290 CopyMem ((UINT8*) NewBuffer + NewSize, Buffer, *BufferSize);\r
1291 Status = ShellAddEnvVarToList (\r
1292 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1293 NewBuffer,\r
1294 TotalSize,\r
1295 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
1296 );\r
1297 if (EFI_ERROR (Status)) {\r
1298 FreePool (NewBuffer);\r
1299 return Status;\r
1300 }\r
1301\r
1302 Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV (\r
1303 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1304 TotalSize - sizeof (CHAR16),\r
1305 NewBuffer\r
1306 );\r
1307 if (EFI_ERROR (Status)) {\r
1308 ShellRemvoeEnvVarFromList (((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name);\r
1309 }\r
1310\r
1311 FreePool (NewBuffer);\r
1312 return Status;\r
1313}\r
1314\r
1315/**\r
1316 Creates a EFI_FILE_PROTOCOL (almost) object for using to access\r
1317 environment variables through file operations.\r
1318\r
1319 @param EnvName The name of the Environment Variable to be operated on.\r
1320\r
1321 @retval NULL Memory could not be allocated.\r
1322 @return other a pointer to an EFI_FILE_PROTOCOL structure\r
1323**/\r
1324EFI_FILE_PROTOCOL*\r
1325CreateFileInterfaceEnv(\r
1326 IN CONST CHAR16 *EnvName\r
1327 )\r
1328{\r
1329 EFI_STATUS Status;\r
1330 EFI_FILE_PROTOCOL_ENVIRONMENT *EnvFileInterface;\r
1331 UINTN EnvNameSize;\r
1332 BOOLEAN Volatile;\r
1333\r
1334 if (EnvName == NULL) {\r
1335 return (NULL);\r
1336 }\r
1337\r
1338 Status = IsVolatileEnv (EnvName, &Volatile);\r
1339 if (EFI_ERROR (Status)) {\r
1340 return NULL;\r
1341 }\r
1342\r
1343 //\r
1344 // Get some memory\r
1345 //\r
1346 EnvNameSize = StrSize(EnvName);\r
1347 EnvFileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_ENVIRONMENT)+EnvNameSize);\r
1348 if (EnvFileInterface == NULL){\r
1349 return (NULL);\r
1350 }\r
1351\r
1352 //\r
1353 // Assign the generic members\r
1354 //\r
1355 EnvFileInterface->Revision = EFI_FILE_REVISION;\r
1356 EnvFileInterface->Open = FileInterfaceOpenNotFound;\r
1357 EnvFileInterface->Close = FileInterfaceEnvClose;\r
1358 EnvFileInterface->GetPosition = FileInterfaceNopGetPosition;\r
1359 EnvFileInterface->SetPosition = FileInterfaceNopSetPosition;\r
1360 EnvFileInterface->GetInfo = FileInterfaceNopGetInfo;\r
1361 EnvFileInterface->SetInfo = FileInterfaceNopSetInfo;\r
1362 EnvFileInterface->Flush = FileInterfaceNopGeneric;\r
1363 EnvFileInterface->Delete = FileInterfaceEnvDelete;\r
1364 EnvFileInterface->Read = FileInterfaceEnvRead;\r
1365\r
1366 CopyMem(EnvFileInterface->Name, EnvName, EnvNameSize);\r
1367\r
1368 //\r
1369 // Assign the different members for Volatile and Non-Volatile variables\r
1370 //\r
1371 if (Volatile) {\r
1372 EnvFileInterface->Write = FileInterfaceEnvVolWrite;\r
1373 } else {\r
1374 EnvFileInterface->Write = FileInterfaceEnvNonVolWrite;\r
1375 }\r
1376 return ((EFI_FILE_PROTOCOL *)EnvFileInterface);\r
1377}\r
1378\r
1379/**\r
1380 Move the cursor position one character backward.\r
1381\r
1382 @param[in] LineLength Length of a line. Get it by calling QueryMode\r
1383 @param[in, out] Column Current column of the cursor position\r
1384 @param[in, out] Row Current row of the cursor position\r
1385**/\r
1386VOID\r
1387MoveCursorBackward (\r
1388 IN UINTN LineLength,\r
1389 IN OUT UINTN *Column,\r
1390 IN OUT UINTN *Row\r
1391 )\r
1392{\r
1393 //\r
1394 // If current column is 0, move to the last column of the previous line,\r
1395 // otherwise, just decrement column.\r
1396 //\r
1397 if (*Column == 0) {\r
1398 *Column = LineLength - 1;\r
1399 if (*Row > 0) {\r
1400 (*Row)--;\r
1401 }\r
1402 return;\r
1403 }\r
1404 (*Column)--;\r
1405}\r
1406\r
1407/**\r
1408 Move the cursor position one character forward.\r
1409\r
1410 @param[in] LineLength Length of a line.\r
1411 @param[in] TotalRow Total row of a screen\r
1412 @param[in, out] Column Current column of the cursor position\r
1413 @param[in, out] Row Current row of the cursor position\r
1414**/\r
1415VOID\r
1416MoveCursorForward (\r
1417 IN UINTN LineLength,\r
1418 IN UINTN TotalRow,\r
1419 IN OUT UINTN *Column,\r
1420 IN OUT UINTN *Row\r
1421 )\r
1422{\r
1423 //\r
1424 // Increment Column.\r
1425 // If this puts column past the end of the line, move to first column\r
1426 // of the next row.\r
1427 //\r
1428 (*Column)++;\r
1429 if (*Column >= LineLength) {\r
1430 (*Column) = 0;\r
1431 if ((*Row) < TotalRow - 1) {\r
1432 (*Row)++;\r
1433 }\r
1434 }\r
1435}\r
1436\r
1437/**\r
1438 Prints out each previously typed command in the command list history log.\r
1439\r
1440 When each screen is full it will pause for a key before continuing.\r
1441\r
1442 @param[in] TotalCols How many columns are on the screen\r
1443 @param[in] TotalRows How many rows are on the screen\r
1444 @param[in] StartColumn which column to start at\r
1445**/\r
1446VOID\r
1447PrintCommandHistory (\r
1448 IN CONST UINTN TotalCols,\r
1449 IN CONST UINTN TotalRows,\r
1450 IN CONST UINTN StartColumn\r
1451 )\r
1452{\r
1453 BUFFER_LIST *Node;\r
1454 UINTN Index;\r
1455 UINTN LineNumber;\r
1456 UINTN LineCount;\r
1457\r
1458 ShellPrintEx (-1, -1, L"\n");\r
1459 Index = 0;\r
1460 LineNumber = 0;\r
1461 //\r
1462 // go through history list...\r
1463 //\r
1464 for ( Node = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link)\r
1465 ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link)\r
1466 ; Node = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link)\r
1467 ){\r
1468 Index++;\r
1469 LineCount = ((StrLen (Node->Buffer) + StartColumn + 1) / TotalCols) + 1;\r
1470\r
1471 if (LineNumber + LineCount >= TotalRows) {\r
1472 ShellPromptForResponseHii(\r
1473 ShellPromptResponseTypeEnterContinue,\r
1474 STRING_TOKEN (STR_SHELL_ENTER_TO_CONT),\r
1475 ShellInfoObject.HiiHandle,\r
1476 NULL\r
1477 );\r
1478 LineNumber = 0;\r
1479 }\r
1480 ShellPrintEx (-1, -1, L"%2d. %s\n", Index, Node->Buffer);\r
1481 LineNumber += LineCount;\r
1482 }\r
1483}\r
1484\r
1485\r
1486\r
1487\r
1488\r
1489\r
1490//\r
1491// This is identical to EFI_FILE_PROTOCOL except for the additional members\r
1492// for the buffer, size, and position.\r
1493//\r
1494\r
1495typedef struct {\r
1496 UINT64 Revision;\r
1497 EFI_FILE_OPEN Open;\r
1498 EFI_FILE_CLOSE Close;\r
1499 EFI_FILE_DELETE Delete;\r
1500 EFI_FILE_READ Read;\r
1501 EFI_FILE_WRITE Write;\r
1502 EFI_FILE_GET_POSITION GetPosition;\r
1503 EFI_FILE_SET_POSITION SetPosition;\r
1504 EFI_FILE_GET_INFO GetInfo;\r
1505 EFI_FILE_SET_INFO SetInfo;\r
1506 EFI_FILE_FLUSH Flush;\r
1507 VOID *Buffer;\r
1508 UINT64 Position;\r
1509 UINT64 BufferSize;\r
1510 BOOLEAN Unicode;\r
1511 UINT64 FileSize;\r
1512} EFI_FILE_PROTOCOL_MEM;\r
1513\r
1514/**\r
1515 File style interface for Mem (SetPosition).\r
1516\r
1517 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1518 @param[out] Position The position to set.\r
1519\r
1520 @retval EFI_SUCCESS The position was successfully changed.\r
1521 @retval EFI_INVALID_PARAMETER The Position was invalid.\r
1522**/\r
1523EFI_STATUS\r
1524EFIAPI\r
1525FileInterfaceMemSetPosition(\r
1526 IN EFI_FILE_PROTOCOL *This,\r
1527 OUT UINT64 Position\r
1528 )\r
1529{\r
1530 if (Position <= ((EFI_FILE_PROTOCOL_MEM*)This)->FileSize) {\r
1531 ((EFI_FILE_PROTOCOL_MEM*)This)->Position = Position;\r
1532 return (EFI_SUCCESS);\r
1533 } else {\r
1534 return (EFI_INVALID_PARAMETER);\r
1535 }\r
1536}\r
1537\r
1538/**\r
1539 File style interface for Mem (GetPosition).\r
1540\r
1541 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1542 @param[out] Position The pointer to the position.\r
1543\r
1544 @retval EFI_SUCCESS The position was retrieved.\r
1545**/\r
1546EFI_STATUS\r
1547EFIAPI\r
1548FileInterfaceMemGetPosition(\r
1549 IN EFI_FILE_PROTOCOL *This,\r
1550 OUT UINT64 *Position\r
1551 )\r
1552{\r
1553 *Position = ((EFI_FILE_PROTOCOL_MEM*)This)->Position;\r
1554 return (EFI_SUCCESS);\r
1555}\r
1556\r
1557/**\r
1558 File style interface for Mem (GetInfo).\r
1559\r
1560 @param This Protocol instance pointer.\r
1561 @param InformationType Type of information to return in Buffer.\r
1562 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1563 @param Buffer The buffer to return data.\r
1564\r
1565 @retval EFI_SUCCESS Data was returned.\r
1566 @retval EFI_UNSUPPORT InformationType is not supported.\r
1567 @retval EFI_NO_MEDIA The device has no media.\r
1568 @retval EFI_DEVICE_ERROR The device reported an error.\r
1569 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1570 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1571 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1572 @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.\r
1573\r
1574**/\r
1575EFI_STATUS\r
1576EFIAPI\r
1577FileInterfaceMemGetInfo(\r
1578 IN EFI_FILE_PROTOCOL *This,\r
1579 IN EFI_GUID *InformationType,\r
1580 IN OUT UINTN *BufferSize,\r
1581 OUT VOID *Buffer\r
1582 )\r
1583{\r
1584 EFI_FILE_INFO *FileInfo;\r
1585\r
1586 if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {\r
1587 if (*BufferSize < sizeof (EFI_FILE_INFO)) {\r
1588 *BufferSize = sizeof (EFI_FILE_INFO);\r
1589 return EFI_BUFFER_TOO_SMALL;\r
1590 }\r
1591 if (Buffer == NULL) {\r
1592 return EFI_INVALID_PARAMETER;\r
1593 }\r
1594 FileInfo = (EFI_FILE_INFO *)Buffer;\r
1595 FileInfo->Size = sizeof (*FileInfo);\r
1596 ZeroMem (FileInfo, sizeof (*FileInfo));\r
1597 FileInfo->FileSize = ((EFI_FILE_PROTOCOL_MEM*)This)->FileSize;\r
1598 FileInfo->PhysicalSize = FileInfo->FileSize;\r
1599 return EFI_SUCCESS;\r
1600 }\r
1601\r
1602 return EFI_UNSUPPORTED;\r
1603}\r
1604\r
1605/**\r
1606 File style interface for Mem (Write).\r
1607\r
1608 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1609 @param[in, out] BufferSize Size in bytes of Buffer.\r
1610 @param[in] Buffer The pointer to the buffer to write.\r
1611\r
1612 @retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources.\r
1613 @retval EFI_SUCCESS The data was written.\r
1614**/\r
1615EFI_STATUS\r
1616EFIAPI\r
1617FileInterfaceMemWrite(\r
1618 IN EFI_FILE_PROTOCOL *This,\r
1619 IN OUT UINTN *BufferSize,\r
1620 IN VOID *Buffer\r
1621 )\r
1622{\r
1623 CHAR8 *AsciiBuffer;\r
1624 EFI_FILE_PROTOCOL_MEM *MemFile;\r
1625\r
1626 MemFile = (EFI_FILE_PROTOCOL_MEM *) This;\r
1627 if (MemFile->Unicode) {\r
1628 //\r
1629 // Unicode\r
1630 //\r
1631 if ((UINTN)(MemFile->Position + (*BufferSize)) > (UINTN)(MemFile->BufferSize)) {\r
1632 MemFile->Buffer = ReallocatePool((UINTN)(MemFile->BufferSize), (UINTN)(MemFile->BufferSize) + (*BufferSize) + MEM_WRITE_REALLOC_OVERHEAD, MemFile->Buffer);\r
1633 MemFile->BufferSize += (*BufferSize) + MEM_WRITE_REALLOC_OVERHEAD;\r
1634 }\r
1635 CopyMem(((UINT8*)MemFile->Buffer) + MemFile->Position, Buffer, *BufferSize);\r
1636 MemFile->Position += (*BufferSize);\r
1637 MemFile->FileSize = MemFile->Position;\r
1638 return (EFI_SUCCESS);\r
1639 } else {\r
1640 //\r
1641 // Ascii\r
1642 //\r
1643 AsciiBuffer = AllocateZeroPool(*BufferSize);\r
1644 if (AsciiBuffer == NULL) {\r
1645 return (EFI_OUT_OF_RESOURCES);\r
1646 }\r
1647 AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
1648 if ((UINTN)(MemFile->Position + AsciiStrSize(AsciiBuffer)) > (UINTN)(MemFile->BufferSize)) {\r
1649 MemFile->Buffer = ReallocatePool((UINTN)(MemFile->BufferSize), (UINTN)(MemFile->BufferSize) + AsciiStrSize(AsciiBuffer) + MEM_WRITE_REALLOC_OVERHEAD, MemFile->Buffer);\r
1650 MemFile->BufferSize += AsciiStrSize(AsciiBuffer) + MEM_WRITE_REALLOC_OVERHEAD;\r
1651 }\r
1652 CopyMem(((UINT8*)MemFile->Buffer) + MemFile->Position, AsciiBuffer, AsciiStrSize(AsciiBuffer));\r
1653 MemFile->Position += (*BufferSize / sizeof(CHAR16));\r
1654 MemFile->FileSize = MemFile->Position;\r
1655 FreePool(AsciiBuffer);\r
1656 return (EFI_SUCCESS);\r
1657 }\r
1658}\r
1659\r
1660/**\r
1661 File style interface for Mem (Read).\r
1662\r
1663 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1664 @param[in, out] BufferSize Size in bytes of Buffer.\r
1665 @param[in] Buffer The pointer to the buffer to fill.\r
1666\r
1667 @retval EFI_SUCCESS The data was read.\r
1668**/\r
1669EFI_STATUS\r
1670EFIAPI\r
1671FileInterfaceMemRead(\r
1672 IN EFI_FILE_PROTOCOL *This,\r
1673 IN OUT UINTN *BufferSize,\r
1674 IN VOID *Buffer\r
1675 )\r
1676{\r
1677 EFI_FILE_PROTOCOL_MEM *MemFile;\r
1678\r
1679 MemFile = (EFI_FILE_PROTOCOL_MEM *) This;\r
1680 if (*BufferSize > (UINTN)((MemFile->FileSize) - (UINTN)(MemFile->Position))) {\r
1681 (*BufferSize) = (UINTN)((MemFile->FileSize) - (UINTN)(MemFile->Position));\r
1682 }\r
1683 CopyMem(Buffer, ((UINT8*)MemFile->Buffer) + MemFile->Position, (*BufferSize));\r
1684 MemFile->Position = MemFile->Position + (*BufferSize);\r
1685 return (EFI_SUCCESS);\r
1686}\r
1687\r
1688/**\r
1689 File style interface for Mem (Close).\r
1690\r
1691 Frees all memory associated with this object.\r
1692\r
1693 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1694\r
1695 @retval EFI_SUCCESS The 'file' was closed.\r
1696**/\r
1697EFI_STATUS\r
1698EFIAPI\r
1699FileInterfaceMemClose(\r
1700 IN EFI_FILE_PROTOCOL *This\r
1701 )\r
1702{\r
1703 SHELL_FREE_NON_NULL(((EFI_FILE_PROTOCOL_MEM*)This)->Buffer);\r
1704 SHELL_FREE_NON_NULL(This);\r
1705 return (EFI_SUCCESS);\r
1706}\r
1707\r
1708/**\r
1709 Creates a EFI_FILE_PROTOCOL (almost) object for using to access\r
1710 a file entirely in memory through file operations.\r
1711\r
1712 @param[in] Unicode Boolean value with TRUE for Unicode and FALSE for Ascii.\r
1713\r
1714 @retval NULL Memory could not be allocated.\r
1715 @return other A pointer to an EFI_FILE_PROTOCOL structure.\r
1716**/\r
1717EFI_FILE_PROTOCOL*\r
1718CreateFileInterfaceMem(\r
1719 IN CONST BOOLEAN Unicode\r
1720 )\r
1721{\r
1722 EFI_FILE_PROTOCOL_MEM *FileInterface;\r
1723\r
1724 //\r
1725 // Get some memory\r
1726 //\r
1727 FileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_MEM));\r
1728 if (FileInterface == NULL){\r
1729 return (NULL);\r
1730 }\r
1731\r
1732 //\r
1733 // Assign the generic members\r
1734 //\r
1735 FileInterface->Revision = EFI_FILE_REVISION;\r
1736 FileInterface->Open = FileInterfaceOpenNotFound;\r
1737 FileInterface->Close = FileInterfaceMemClose;\r
1738 FileInterface->GetPosition = FileInterfaceMemGetPosition;\r
1739 FileInterface->SetPosition = FileInterfaceMemSetPosition;\r
1740 FileInterface->GetInfo = FileInterfaceMemGetInfo;\r
1741 FileInterface->SetInfo = FileInterfaceNopSetInfo;\r
1742 FileInterface->Flush = FileInterfaceNopGeneric;\r
1743 FileInterface->Delete = FileInterfaceNopGeneric;\r
1744 FileInterface->Read = FileInterfaceMemRead;\r
1745 FileInterface->Write = FileInterfaceMemWrite;\r
1746 FileInterface->Unicode = Unicode;\r
1747\r
1748 ASSERT(FileInterface->Buffer == NULL);\r
1749 ASSERT(FileInterface->BufferSize == 0);\r
1750 ASSERT(FileInterface->Position == 0);\r
1751\r
1752 if (Unicode) {\r
1753 FileInterface->Buffer = AllocateZeroPool(sizeof(gUnicodeFileTag));\r
1754 if (FileInterface->Buffer == NULL) {\r
1755 FreePool (FileInterface);\r
1756 return NULL;\r
1757 }\r
1758 *((CHAR16 *) (FileInterface->Buffer)) = EFI_UNICODE_BYTE_ORDER_MARK;\r
1759 FileInterface->BufferSize = 2;\r
1760 FileInterface->Position = 2;\r
1761 }\r
1762\r
1763 return ((EFI_FILE_PROTOCOL *)FileInterface);\r
1764}\r
1765\r
1766typedef struct {\r
1767 UINT64 Revision;\r
1768 EFI_FILE_OPEN Open;\r
1769 EFI_FILE_CLOSE Close;\r
1770 EFI_FILE_DELETE Delete;\r
1771 EFI_FILE_READ Read;\r
1772 EFI_FILE_WRITE Write;\r
1773 EFI_FILE_GET_POSITION GetPosition;\r
1774 EFI_FILE_SET_POSITION SetPosition;\r
1775 EFI_FILE_GET_INFO GetInfo;\r
1776 EFI_FILE_SET_INFO SetInfo;\r
1777 EFI_FILE_FLUSH Flush;\r
1778 BOOLEAN Unicode;\r
1779 EFI_FILE_PROTOCOL *Orig;\r
1780} EFI_FILE_PROTOCOL_FILE;\r
1781\r
1782/**\r
1783 Set a files current position\r
1784\r
1785 @param This Protocol instance pointer.\r
1786 @param Position Byte position from the start of the file.\r
1787\r
1788 @retval EFI_SUCCESS Data was written.\r
1789 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.\r
1790\r
1791**/\r
1792EFI_STATUS\r
1793EFIAPI\r
1794FileInterfaceFileSetPosition(\r
1795 IN EFI_FILE_PROTOCOL *This,\r
1796 IN UINT64 Position\r
1797 )\r
1798{\r
1799 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->SetPosition(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, Position);\r
1800}\r
1801\r
1802/**\r
1803 Get a file's current position\r
1804\r
1805 @param This Protocol instance pointer.\r
1806 @param Position Byte position from the start of the file.\r
1807\r
1808 @retval EFI_SUCCESS Data was written.\r
1809 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open..\r
1810\r
1811**/\r
1812EFI_STATUS\r
1813EFIAPI\r
1814FileInterfaceFileGetPosition(\r
1815 IN EFI_FILE_PROTOCOL *This,\r
1816 OUT UINT64 *Position\r
1817 )\r
1818{\r
1819 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetPosition(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, Position);\r
1820}\r
1821\r
1822/**\r
1823 Get information about a file.\r
1824\r
1825 @param This Protocol instance pointer.\r
1826 @param InformationType Type of information to return in Buffer.\r
1827 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1828 @param Buffer The buffer to return data.\r
1829\r
1830 @retval EFI_SUCCESS Data was returned.\r
1831 @retval EFI_UNSUPPORT InformationType is not supported.\r
1832 @retval EFI_NO_MEDIA The device has no media.\r
1833 @retval EFI_DEVICE_ERROR The device reported an error.\r
1834 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1835 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1836 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1837 @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.\r
1838\r
1839**/\r
1840EFI_STATUS\r
1841EFIAPI\r
1842FileInterfaceFileGetInfo(\r
1843 IN EFI_FILE_PROTOCOL *This,\r
1844 IN EFI_GUID *InformationType,\r
1845 IN OUT UINTN *BufferSize,\r
1846 OUT VOID *Buffer\r
1847 )\r
1848{\r
1849 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetInfo(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, InformationType, BufferSize, Buffer);\r
1850}\r
1851\r
1852/**\r
1853 Set information about a file\r
1854\r
1855 @param This Protocol instance pointer.\r
1856 @param InformationType Type of information in Buffer.\r
1857 @param BufferSize Size of buffer.\r
1858 @param Buffer The data to write.\r
1859\r
1860 @retval EFI_SUCCESS Data was returned.\r
1861 @retval EFI_UNSUPPORT InformationType is not supported.\r
1862 @retval EFI_NO_MEDIA The device has no media.\r
1863 @retval EFI_DEVICE_ERROR The device reported an error.\r
1864 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1865 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1866 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1867\r
1868**/\r
1869EFI_STATUS\r
1870EFIAPI\r
1871FileInterfaceFileSetInfo(\r
1872 IN EFI_FILE_PROTOCOL *This,\r
1873 IN EFI_GUID *InformationType,\r
1874 IN UINTN BufferSize,\r
1875 IN VOID *Buffer\r
1876 )\r
1877{\r
1878 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->SetInfo(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, InformationType, BufferSize, Buffer);\r
1879}\r
1880\r
1881/**\r
1882 Flush data back for the file handle.\r
1883\r
1884 @param This Protocol instance pointer.\r
1885\r
1886 @retval EFI_SUCCESS Data was written.\r
1887 @retval EFI_UNSUPPORT Writes to Open directory are not supported.\r
1888 @retval EFI_NO_MEDIA The device has no media.\r
1889 @retval EFI_DEVICE_ERROR The device reported an error.\r
1890 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1891 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1892 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1893 @retval EFI_VOLUME_FULL The volume is full.\r
1894\r
1895**/\r
1896EFI_STATUS\r
1897EFIAPI\r
1898FileInterfaceFileFlush(\r
1899 IN EFI_FILE_PROTOCOL *This\r
1900 )\r
1901{\r
1902 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Flush(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
1903}\r
1904\r
1905/**\r
1906 Read data from the file.\r
1907\r
1908 @param This Protocol instance pointer.\r
1909 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1910 @param Buffer The buffer in which data is read.\r
1911\r
1912 @retval EFI_SUCCESS Data was read.\r
1913 @retval EFI_NO_MEDIA The device has no media.\r
1914 @retval EFI_DEVICE_ERROR The device reported an error.\r
1915 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1916 @retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains required size.\r
1917\r
1918**/\r
1919EFI_STATUS\r
1920EFIAPI\r
1921FileInterfaceFileRead(\r
1922 IN EFI_FILE_PROTOCOL *This,\r
1923 IN OUT UINTN *BufferSize,\r
1924 OUT VOID *Buffer\r
1925 )\r
1926{\r
1927 CHAR8 *AsciiStrBuffer;\r
1928 CHAR16 *UscStrBuffer;\r
1929 UINTN Size;\r
1930 UINTN CharNum;\r
1931 EFI_STATUS Status;\r
1932 if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {\r
1933 //\r
1934 // Unicode\r
1935 //\r
1936 return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));\r
1937 } else {\r
1938 //\r
1939 // Ascii\r
1940 //\r
1941 Size = (*BufferSize) / sizeof(CHAR16);\r
1942 AsciiStrBuffer = AllocateZeroPool(Size + sizeof(CHAR8));\r
1943 if (AsciiStrBuffer == NULL) {\r
1944 return EFI_OUT_OF_RESOURCES;\r
1945 }\r
1946 UscStrBuffer = AllocateZeroPool(*BufferSize + sizeof(CHAR16));\r
1947 if (UscStrBuffer== NULL) {\r
1948 SHELL_FREE_NON_NULL(AsciiStrBuffer);\r
1949 return EFI_OUT_OF_RESOURCES;\r
1950 }\r
1951 Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiStrBuffer));\r
1952 if (!EFI_ERROR(Status)) {\r
1953 CharNum = UnicodeSPrint(UscStrBuffer, *BufferSize + sizeof(CHAR16), L"%a", AsciiStrBuffer);\r
1954 if (CharNum == Size) {\r
1955 CopyMem (Buffer, UscStrBuffer, *BufferSize);\r
1956 } else {\r
1957 Status = EFI_UNSUPPORTED;\r
1958 }\r
1959 }\r
1960 SHELL_FREE_NON_NULL(AsciiStrBuffer);\r
1961 SHELL_FREE_NON_NULL(UscStrBuffer);\r
1962 return (Status);\r
1963 }\r
1964}\r
1965\r
1966/**\r
1967 Opens a new file relative to the source file's location.\r
1968\r
1969 @param[in] This The protocol instance pointer.\r
1970 @param[out] NewHandle Returns File Handle for FileName.\r
1971 @param[in] FileName Null terminated string. "\", ".", and ".." are supported.\r
1972 @param[in] OpenMode Open mode for file.\r
1973 @param[in] Attributes Only used for EFI_FILE_MODE_CREATE.\r
1974\r
1975 @retval EFI_SUCCESS The device was opened.\r
1976 @retval EFI_NOT_FOUND The specified file could not be found on the device.\r
1977 @retval EFI_NO_MEDIA The device has no media.\r
1978 @retval EFI_MEDIA_CHANGED The media has changed.\r
1979 @retval EFI_DEVICE_ERROR The device reported an error.\r
1980 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1981 @retval EFI_ACCESS_DENIED The service denied access to the file.\r
1982 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.\r
1983 @retval EFI_VOLUME_FULL The volume is full.\r
1984**/\r
1985EFI_STATUS\r
1986EFIAPI\r
1987FileInterfaceFileOpen (\r
1988 IN EFI_FILE_PROTOCOL *This,\r
1989 OUT EFI_FILE_PROTOCOL **NewHandle,\r
1990 IN CHAR16 *FileName,\r
1991 IN UINT64 OpenMode,\r
1992 IN UINT64 Attributes\r
1993 )\r
1994{\r
1995 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Open(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, NewHandle, FileName, OpenMode, Attributes);\r
1996}\r
1997\r
1998/**\r
1999 Close and delete the file handle.\r
2000\r
2001 @param This Protocol instance pointer.\r
2002\r
2003 @retval EFI_SUCCESS The device was opened.\r
2004 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.\r
2005\r
2006**/\r
2007EFI_STATUS\r
2008EFIAPI\r
2009FileInterfaceFileDelete(\r
2010 IN EFI_FILE_PROTOCOL *This\r
2011 )\r
2012{\r
2013 EFI_STATUS Status;\r
2014 Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Delete(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
2015 FreePool(This);\r
2016 return (Status);\r
2017}\r
2018\r
2019/**\r
2020 File style interface for File (Close).\r
2021\r
2022 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
2023\r
2024 @retval EFI_SUCCESS The file was closed.\r
2025**/\r
2026EFI_STATUS\r
2027EFIAPI\r
2028FileInterfaceFileClose(\r
2029 IN EFI_FILE_PROTOCOL *This\r
2030 )\r
2031{\r
2032 EFI_STATUS Status;\r
2033 Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Close(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
2034 FreePool(This);\r
2035 return (Status);\r
2036}\r
2037\r
2038/**\r
2039 File style interface for File (Write).\r
2040\r
2041 If the file was opened with ASCII mode the data will be processed through\r
2042 AsciiSPrint before writing.\r
2043\r
2044 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
2045 @param[in, out] BufferSize Size in bytes of Buffer.\r
2046 @param[in] Buffer The pointer to the buffer to write.\r
2047\r
2048 @retval EFI_SUCCESS The data was written.\r
2049**/\r
2050EFI_STATUS\r
2051EFIAPI\r
2052FileInterfaceFileWrite(\r
2053 IN EFI_FILE_PROTOCOL *This,\r
2054 IN OUT UINTN *BufferSize,\r
2055 IN VOID *Buffer\r
2056 )\r
2057{\r
2058 CHAR8 *AsciiBuffer;\r
2059 UINTN Size;\r
2060 EFI_STATUS Status;\r
2061 if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {\r
2062 //\r
2063 // Unicode\r
2064 //\r
2065 return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Write(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));\r
2066 } else {\r
2067 //\r
2068 // Ascii\r
2069 //\r
2070 AsciiBuffer = AllocateZeroPool(*BufferSize);\r
2071 AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
2072 Size = AsciiStrSize(AsciiBuffer) - 1; // (we dont need the null terminator)\r
2073 Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Write(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiBuffer));\r
2074 FreePool(AsciiBuffer);\r
2075 return (Status);\r
2076 }\r
2077}\r
2078\r
2079/**\r
2080 Create a file interface with unicode information.\r
2081\r
2082 This will create a new EFI_FILE_PROTOCOL identical to the Templace\r
2083 except that the new one has Unicode and Ascii knowledge.\r
2084\r
2085 @param[in] Template A pointer to the EFI_FILE_PROTOCOL object.\r
2086 @param[in] Unicode TRUE for UCS-2, FALSE for ASCII.\r
2087\r
2088 @return a new EFI_FILE_PROTOCOL object to be used instead of the template.\r
2089**/\r
2090EFI_FILE_PROTOCOL*\r
2091CreateFileInterfaceFile(\r
2092 IN CONST EFI_FILE_PROTOCOL *Template,\r
2093 IN CONST BOOLEAN Unicode\r
2094 )\r
2095{\r
2096 EFI_FILE_PROTOCOL_FILE *NewOne;\r
2097\r
2098 NewOne = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_FILE));\r
2099 if (NewOne == NULL) {\r
2100 return (NULL);\r
2101 }\r
2102 CopyMem(NewOne, Template, sizeof(EFI_FILE_PROTOCOL_FILE));\r
2103 NewOne->Orig = (EFI_FILE_PROTOCOL *)Template;\r
2104 NewOne->Unicode = Unicode;\r
2105 NewOne->Open = FileInterfaceFileOpen;\r
2106 NewOne->Close = FileInterfaceFileClose;\r
2107 NewOne->Delete = FileInterfaceFileDelete;\r
2108 NewOne->Read = FileInterfaceFileRead;\r
2109 NewOne->Write = FileInterfaceFileWrite;\r
2110 NewOne->GetPosition = FileInterfaceFileGetPosition;\r
2111 NewOne->SetPosition = FileInterfaceFileSetPosition;\r
2112 NewOne->GetInfo = FileInterfaceFileGetInfo;\r
2113 NewOne->SetInfo = FileInterfaceFileSetInfo;\r
2114 NewOne->Flush = FileInterfaceFileFlush;\r
2115\r
2116 return ((EFI_FILE_PROTOCOL *)NewOne);\r
2117}\r