]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/FileHandleWrappers.c
ShellPkg: Fix the bug that handling Ctrl-C improperly
[mirror_edk2.git] / ShellPkg / Application / Shell / FileHandleWrappers.c
CommitLineData
a405b86d 1/** @file\r
2 EFI_FILE_PROTOCOL wrappers for other items (Like Environment Variables,\r
3 StdIn, StdOut, StdErr, etc...).\r
4\r
9ed21946 5 Copyright 2016 Dell Inc.\r
5016d466 6 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
c011b6c9 7 (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r
a405b86d 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
c8d9d0e2
JD
21#define MEM_WRITE_REALLOC_OVERHEAD 1024\r
22\r
a405b86d 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
4ff7e37b
ED
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
a405b86d 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
4ff7e37b
ED
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
a405b86d 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
a405b86d 165 }\r
d73fc181
JD
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
a405b86d 170}\r
171\r
172/**\r
173 File style interface for StdIn (Write).\r
174 \r
4ff7e37b
ED
175 @param[in] This Ignored.\r
176 @param[in, out] BufferSize Ignored.\r
177 @param[in] Buffer Ignored.\r
a405b86d 178 \r
179 @retval EFI_UNSUPPORTED\r
180**/\r
181EFI_STATUS\r
182EFIAPI\r
183FileInterfaceStdInWrite(\r
733f138d 184 IN EFI_FILE_PROTOCOL *This,\r
185 IN OUT UINTN *BufferSize,\r
186 IN VOID *Buffer\r
a405b86d 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
4ff7e37b
ED
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
a405b86d 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
4ff7e37b
ED
217 @param[in] This Ignored.\r
218 @param[in, out] BufferSize Ignored.\r
219 @param[out] Buffer Ignored.\r
a405b86d 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
4ff7e37b
ED
237 @param[in] This Ignored.\r
238 @param[in, out] BufferSize Ignored.\r
239 @param[out] Buffer Ignored.\r
a405b86d 240 \r
733f138d 241 @retval EFI_UNSUPPORTED Always.\r
a405b86d 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
4ff7e37b
ED
257 @param[in] This Ignored.\r
258 @param[in, out] BufferSize Poiner to 0 upon return.\r
259 @param[out] Buffer Ignored.\r
a405b86d 260 \r
733f138d 261 @retval EFI_SUCCESS Always.\r
a405b86d 262**/\r
263EFI_STATUS\r
264EFIAPI\r
265FileInterfaceNulRead(\r
733f138d 266 IN EFI_FILE_PROTOCOL *This,\r
267 IN OUT UINTN *BufferSize,\r
268 OUT VOID *Buffer\r
a405b86d 269 )\r
270{\r
a49f6a2f 271 *BufferSize = 0;\r
a405b86d 272 return (EFI_SUCCESS);\r
273}\r
274\r
275/**\r
276 File style interface for NUL file (Write).\r
277 \r
4ff7e37b
ED
278 @param[in] This Ignored.\r
279 @param[in, out] BufferSize Ignored.\r
280 @param[in] Buffer Ignored.\r
a405b86d 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
9fcfa150
RN
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
8009b2e4
BD
301 @param[in, out] TabCompletionList Return the TAB completion list.\r
302 @param[in, out] TabUpdatePos Return the TAB update position.\r
9fcfa150
RN
303**/\r
304EFI_STATUS\r
9fcfa150
RN
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
db2e266a 370 // If file path doesn't contain ":", ...\r
9fcfa150
RN
371 //\r
372 Cwd = ShellInfoObject.NewEfiShellProtocol->GetCurDir (NULL);\r
373 if (Cwd != NULL) {\r
9fcfa150 374 if (InputString[TabPos] != L'\\') {\r
db2e266a
RN
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
9fcfa150 380 StrCatS (TabStr, (BufferSize) / sizeof (CHAR16), L"\\");\r
db2e266a
RN
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
9fcfa150
RN
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
a405b86d 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
9fcfa150 467 UINTN TabUpdatePos; // Start index of the string updated by TAB stroke\r
a405b86d 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
a405b86d 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
9fcfa150
RN
489 EFI_SHELL_FILE_INFO *TabCompleteList;\r
490 EFI_SHELL_FILE_INFO *TabCurrent;\r
a405b86d 491 UINTN EventIndex;\r
9fcfa150 492 CHAR16 *TabOutputStr;\r
a405b86d 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
9fcfa150 513 TabOutputStr = NULL;\r
a405b86d 514 TabUpdatePos = 0;\r
9fcfa150
RN
515 TabCompleteList = NULL;\r
516 TabCurrent = NULL;\r
a405b86d 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
b8f3601d
PL
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
4dd8c7af 547 break;\r
a405b86d 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
9fcfa150
RN
573 if (TabCompleteList != NULL) {\r
574 ShellInfoObject.NewEfiShellProtocol->FreeFileList (&TabCompleteList);\r
575 DEBUG_CODE(TabCompleteList = NULL;);\r
576 }\r
577 InTabScrolling = FALSE;\r
a405b86d 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
9fcfa150
RN
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
a405b86d 618 }\r
9fcfa150
RN
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
a405b86d 634 } else {\r
9fcfa150 635 TabCurrent = (EFI_SHELL_FILE_INFO*) GetNextNode (&TabCompleteList->Link, &TabCurrent->Link);\r
a405b86d 636 }\r
9fcfa150
RN
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
a405b86d 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
9fcfa150
RN
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
a405b86d 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
9fcfa150 796 OutputLength = StrLen (TabCurrent->FileName);\r
a405b86d 797 //\r
798 // if the output string contains blank space, quotation marks L'\"'\r
799 // should be added to the output.\r
800 //\r
9fcfa150 801 if (StrStr(TabCurrent->FileName, L" ") != NULL){\r
a405b86d 802 TabOutputStr[0] = L'\"';\r
9fcfa150 803 CopyMem (TabOutputStr + 1, TabCurrent->FileName, OutputLength * sizeof (CHAR16));\r
a405b86d 804 TabOutputStr[OutputLength + 1] = L'\"';\r
805 TabOutputStr[OutputLength + 2] = CHAR_NULL;\r
806 } else {\r
9fcfa150 807 CopyMem (TabOutputStr, TabCurrent->FileName, OutputLength * sizeof (CHAR16));\r
a405b86d 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
9fcfa150
RN
818\r
819 FreePool(TabOutputStr);\r
a405b86d 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
a405b86d 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
9fcfa150
RN
932 if (TabCompleteList != NULL) {\r
933 ShellInfoObject.NewEfiShellProtocol->FreeFileList (&TabCompleteList);\r
9c17810a 934 }\r
9fcfa150 935 ASSERT(TabCompleteList == NULL);\r
a405b86d 936\r
4dd8c7af 937 return Status;\r
a405b86d 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
416a423f
CP
1039 VOID* NewBuffer;\r
1040 UINTN NewSize;\r
1041 EFI_STATUS Status;\r
31e5b912 1042 BOOLEAN Volatile;\r
c5c994c5 1043 UINTN TotalSize;\r
416a423f
CP
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
c5c994c5 1053 TotalSize = 0;\r
416a423f 1054\r
31e5b912
RN
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
416a423f
CP
1060 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1061 if (Status == EFI_BUFFER_TOO_SMALL) {\r
c5c994c5
CC
1062 TotalSize = NewSize + sizeof (CHAR16);\r
1063 NewBuffer = AllocateZeroPool (TotalSize);\r
416a423f
CP
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
03bc7c2b 1070 if (!EFI_ERROR(Status) && NewBuffer != NULL) {\r
416a423f 1071 \r
c5c994c5
CC
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
5016d466
CC
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
416a423f 1081 }\r
31e5b912
RN
1082\r
1083 if (Volatile) {\r
c5c994c5
CC
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
416a423f 1098 } else {\r
c5c994c5
CC
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
416a423f
CP
1113 }\r
1114 }\r
1115 } \r
1116 \r
1117 SHELL_FREE_NON_NULL(NewBuffer);\r
a405b86d 1118 FreePool((EFI_FILE_PROTOCOL_ENVIRONMENT*)This);\r
416a423f 1119 return (Status);\r
a405b86d 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
4ff7e37b
ED
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
a405b86d 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
c5c994c5 1164 This function also caches the environment variable into gShellEnvVarList.\r
a405b86d 1165 \r
4ff7e37b
ED
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
a405b86d 1169 \r
c5c994c5
CC
1170 @retval EFI_SUCCESS The data was successfully write to variable.\r
1171 @retval SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
a405b86d 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
c5c994c5 1184 UINTN TotalSize;\r
a405b86d 1185\r
1186 NewBuffer = NULL;\r
1187 NewSize = 0;\r
c5c994c5 1188 TotalSize = 0;\r
a405b86d 1189\r
1190 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
c5c994c5
CC
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
a405b86d 1205 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1206 }\r
c5c994c5
CC
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
a405b86d 1232 }\r
c5c994c5
CC
1233\r
1234 FreePool (NewBuffer);\r
1235 return Status;\r
a405b86d 1236}\r
1237\r
1238\r
1239/**\r
1240 File style interface for Non Volatile Environment Variable (Write).\r
c5c994c5 1241 This function also caches the environment variable into gShellEnvVarList.\r
a405b86d 1242 \r
4ff7e37b
ED
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
a405b86d 1246 \r
c5c994c5
CC
1247 @retval EFI_SUCCESS The data was successfully write to variable.\r
1248 @retval SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
a405b86d 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
c5c994c5 1261 UINTN TotalSize;\r
a405b86d 1262\r
1263 NewBuffer = NULL;\r
1264 NewSize = 0;\r
c5c994c5 1265 TotalSize = 0;\r
a405b86d 1266\r
1267 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
c5c994c5
CC
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
a405b86d 1282 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1283 }\r
c5c994c5
CC
1284\r
1285 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) {\r
1286 FreePool (NewBuffer);\r
1287 return Status;\r
a405b86d 1288 }\r
c5c994c5
CC
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
a405b86d 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
a405b86d 1325CreateFileInterfaceEnv(\r
1326 IN CONST CHAR16 *EnvName\r
1327 )\r
1328{\r
31e5b912 1329 EFI_STATUS Status;\r
a405b86d 1330 EFI_FILE_PROTOCOL_ENVIRONMENT *EnvFileInterface;\r
323d3d11 1331 UINTN EnvNameSize;\r
31e5b912 1332 BOOLEAN Volatile;\r
a405b86d 1333\r
1334 if (EnvName == NULL) {\r
1335 return (NULL);\r
1336 }\r
1337\r
31e5b912
RN
1338 Status = IsVolatileEnv (EnvName, &Volatile);\r
1339 if (EFI_ERROR (Status)) {\r
1340 return NULL;\r
1341 }\r
1342\r
a405b86d 1343 //\r
1344 // Get some memory\r
1345 //\r
323d3d11
QS
1346 EnvNameSize = StrSize(EnvName);\r
1347 EnvFileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_ENVIRONMENT)+EnvNameSize);\r
a405b86d 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
323d3d11
QS
1365 \r
1366 CopyMem(EnvFileInterface->Name, EnvName, EnvNameSize);\r
a405b86d 1367\r
1368 //\r
1369 // Assign the different members for Volatile and Non-Volatile variables\r
1370 //\r
31e5b912 1371 if (Volatile) {\r
a405b86d 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
4ff7e37b
ED
1383 @param[in, out] Column Current column of the cursor position\r
1384 @param[in, out] Row Current row of the cursor position\r
a405b86d 1385**/\r
1386VOID\r
a405b86d 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
4ff7e37b
ED
1412 @param[in, out] Column Current column of the cursor position\r
1413 @param[in, out] Row Current row of the cursor position\r
a405b86d 1414**/\r
1415VOID\r
a405b86d 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
a405b86d 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
7bcd3ff6 1511 UINT64 FileSize;\r
a405b86d 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
7bcd3ff6 1530 if (Position <= ((EFI_FILE_PROTOCOL_MEM*)This)->FileSize) {\r
a405b86d 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 (Write).\r
1559 \r
4ff7e37b
ED
1560 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1561 @param[in, out] BufferSize Size in bytes of Buffer.\r
1562 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1563 \r
ecae5117 1564 @retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources.\r
1565 @retval EFI_SUCCESS The data was written.\r
a405b86d 1566**/\r
1567EFI_STATUS\r
1568EFIAPI\r
1569FileInterfaceMemWrite(\r
1570 IN EFI_FILE_PROTOCOL *This,\r
1571 IN OUT UINTN *BufferSize,\r
1572 IN VOID *Buffer\r
1573 )\r
1574{\r
9ed21946 1575 CHAR8 *AsciiBuffer;\r
1576 EFI_FILE_PROTOCOL_MEM *MemFile;\r
1577\r
1578 MemFile = (EFI_FILE_PROTOCOL_MEM *) This;\r
1579 if (MemFile->Unicode) {\r
a405b86d 1580 //\r
1581 // Unicode\r
1582 //\r
9ed21946 1583 if ((UINTN)(MemFile->Position + (*BufferSize)) > (UINTN)(MemFile->BufferSize)) {\r
c8d9d0e2
JD
1584 MemFile->Buffer = ReallocatePool((UINTN)(MemFile->BufferSize), (UINTN)(MemFile->BufferSize) + (*BufferSize) + MEM_WRITE_REALLOC_OVERHEAD, MemFile->Buffer);\r
1585 MemFile->BufferSize += (*BufferSize) + MEM_WRITE_REALLOC_OVERHEAD;\r
a405b86d 1586 }\r
9ed21946 1587 CopyMem(((UINT8*)MemFile->Buffer) + MemFile->Position, Buffer, *BufferSize);\r
1588 MemFile->Position += (*BufferSize);\r
7bcd3ff6 1589 MemFile->FileSize = MemFile->Position;\r
a405b86d 1590 return (EFI_SUCCESS);\r
1591 } else {\r
1592 //\r
1593 // Ascii\r
1594 //\r
733f138d 1595 AsciiBuffer = AllocateZeroPool(*BufferSize);\r
ecae5117 1596 if (AsciiBuffer == NULL) {\r
1597 return (EFI_OUT_OF_RESOURCES);\r
1598 }\r
a405b86d 1599 AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
9ed21946 1600 if ((UINTN)(MemFile->Position + AsciiStrSize(AsciiBuffer)) > (UINTN)(MemFile->BufferSize)) {\r
c8d9d0e2
JD
1601 MemFile->Buffer = ReallocatePool((UINTN)(MemFile->BufferSize), (UINTN)(MemFile->BufferSize) + AsciiStrSize(AsciiBuffer) + MEM_WRITE_REALLOC_OVERHEAD, MemFile->Buffer);\r
1602 MemFile->BufferSize += AsciiStrSize(AsciiBuffer) + MEM_WRITE_REALLOC_OVERHEAD;\r
a405b86d 1603 }\r
9ed21946 1604 CopyMem(((UINT8*)MemFile->Buffer) + MemFile->Position, AsciiBuffer, AsciiStrSize(AsciiBuffer));\r
1605 MemFile->Position += (*BufferSize / sizeof(CHAR16));\r
7bcd3ff6 1606 MemFile->FileSize = MemFile->Position;\r
a405b86d 1607 FreePool(AsciiBuffer);\r
1608 return (EFI_SUCCESS);\r
1609 }\r
1610}\r
1611\r
1612/**\r
1613 File style interface for Mem (Read).\r
1614 \r
4ff7e37b
ED
1615 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1616 @param[in, out] BufferSize Size in bytes of Buffer.\r
1617 @param[in] Buffer The pointer to the buffer to fill.\r
a405b86d 1618 \r
1619 @retval EFI_SUCCESS The data was read.\r
1620**/\r
1621EFI_STATUS\r
1622EFIAPI\r
1623FileInterfaceMemRead(\r
1624 IN EFI_FILE_PROTOCOL *This,\r
1625 IN OUT UINTN *BufferSize,\r
1626 IN VOID *Buffer\r
1627 )\r
1628{\r
9ed21946 1629 EFI_FILE_PROTOCOL_MEM *MemFile;\r
1630\r
1631 MemFile = (EFI_FILE_PROTOCOL_MEM *) This;\r
7bcd3ff6
JD
1632 if (*BufferSize > (UINTN)((MemFile->FileSize) - (UINTN)(MemFile->Position))) {\r
1633 (*BufferSize) = (UINTN)((MemFile->FileSize) - (UINTN)(MemFile->Position));\r
a405b86d 1634 }\r
9ed21946 1635 CopyMem(Buffer, ((UINT8*)MemFile->Buffer) + MemFile->Position, (*BufferSize));\r
1636 MemFile->Position = MemFile->Position + (*BufferSize);\r
a405b86d 1637 return (EFI_SUCCESS);\r
1638}\r
1639\r
1640/**\r
1641 File style interface for Mem (Close).\r
1642\r
1643 Frees all memory associated with this object.\r
1644 \r
1645 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1646 \r
1647 @retval EFI_SUCCESS The 'file' was closed.\r
1648**/ \r
1649EFI_STATUS\r
1650EFIAPI\r
1651FileInterfaceMemClose(\r
1652 IN EFI_FILE_PROTOCOL *This\r
1653 )\r
1654{\r
1655 SHELL_FREE_NON_NULL(((EFI_FILE_PROTOCOL_MEM*)This)->Buffer);\r
3c865f20 1656 SHELL_FREE_NON_NULL(This);\r
a405b86d 1657 return (EFI_SUCCESS);\r
1658}\r
1659\r
1660/**\r
1661 Creates a EFI_FILE_PROTOCOL (almost) object for using to access\r
1662 a file entirely in memory through file operations.\r
1663\r
1664 @param[in] Unicode Boolean value with TRUE for Unicode and FALSE for Ascii.\r
1665\r
1666 @retval NULL Memory could not be allocated.\r
1667 @return other A pointer to an EFI_FILE_PROTOCOL structure.\r
1668**/\r
1669EFI_FILE_PROTOCOL*\r
a405b86d 1670CreateFileInterfaceMem(\r
1671 IN CONST BOOLEAN Unicode\r
1672 )\r
1673{\r
1674 EFI_FILE_PROTOCOL_MEM *FileInterface;\r
1675\r
1676 //\r
1677 // Get some memory\r
1678 //\r
1679 FileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_MEM));\r
1680 if (FileInterface == NULL){\r
1681 return (NULL);\r
1682 }\r
1683\r
1684 //\r
1685 // Assign the generic members\r
1686 //\r
1687 FileInterface->Revision = EFI_FILE_REVISION;\r
1688 FileInterface->Open = FileInterfaceOpenNotFound;\r
1689 FileInterface->Close = FileInterfaceMemClose;\r
1690 FileInterface->GetPosition = FileInterfaceMemGetPosition;\r
1691 FileInterface->SetPosition = FileInterfaceMemSetPosition;\r
1692 FileInterface->GetInfo = FileInterfaceNopGetInfo;\r
1693 FileInterface->SetInfo = FileInterfaceNopSetInfo;\r
1694 FileInterface->Flush = FileInterfaceNopGeneric;\r
1695 FileInterface->Delete = FileInterfaceNopGeneric;\r
1696 FileInterface->Read = FileInterfaceMemRead;\r
1697 FileInterface->Write = FileInterfaceMemWrite;\r
1698 FileInterface->Unicode = Unicode;\r
1699\r
1700 ASSERT(FileInterface->Buffer == NULL);\r
1701 ASSERT(FileInterface->BufferSize == 0);\r
1702 ASSERT(FileInterface->Position == 0);\r
1703\r
9ed21946 1704 if (Unicode) {\r
1705 FileInterface->Buffer = AllocateZeroPool(sizeof(gUnicodeFileTag));\r
9eec4d38
QS
1706 if (FileInterface->Buffer == NULL) {\r
1707 FreePool (FileInterface);\r
1708 return NULL;\r
1709 }\r
9ed21946 1710 *((CHAR16 *) (FileInterface->Buffer)) = EFI_UNICODE_BYTE_ORDER_MARK;\r
1711 FileInterface->BufferSize = 2;\r
1712 FileInterface->Position = 2;\r
1713 }\r
1714\r
a405b86d 1715 return ((EFI_FILE_PROTOCOL *)FileInterface);\r
1716}\r
1717\r
1718typedef struct {\r
1719 UINT64 Revision;\r
1720 EFI_FILE_OPEN Open;\r
1721 EFI_FILE_CLOSE Close;\r
1722 EFI_FILE_DELETE Delete;\r
1723 EFI_FILE_READ Read;\r
1724 EFI_FILE_WRITE Write;\r
1725 EFI_FILE_GET_POSITION GetPosition;\r
1726 EFI_FILE_SET_POSITION SetPosition;\r
1727 EFI_FILE_GET_INFO GetInfo;\r
1728 EFI_FILE_SET_INFO SetInfo;\r
1729 EFI_FILE_FLUSH Flush;\r
1730 BOOLEAN Unicode;\r
1731 EFI_FILE_PROTOCOL *Orig;\r
1732} EFI_FILE_PROTOCOL_FILE;\r
1733\r
733f138d 1734/**\r
1735 Set a files current position\r
1736\r
1737 @param This Protocol instance pointer.\r
1738 @param Position Byte position from the start of the file.\r
1739 \r
1740 @retval EFI_SUCCESS Data was written.\r
1741 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.\r
1742\r
1743**/\r
1744EFI_STATUS\r
1745EFIAPI\r
1746FileInterfaceFileSetPosition(\r
1747 IN EFI_FILE_PROTOCOL *This,\r
1748 IN UINT64 Position\r
1749 )\r
1750{\r
1751 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->SetPosition(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, Position);\r
1752}\r
1753\r
1754/**\r
1755 Get a file's current position\r
1756\r
1757 @param This Protocol instance pointer.\r
1758 @param Position Byte position from the start of the file.\r
1759 \r
1760 @retval EFI_SUCCESS Data was written.\r
1761 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open..\r
1762\r
1763**/\r
1764EFI_STATUS\r
1765EFIAPI\r
1766FileInterfaceFileGetPosition(\r
1767 IN EFI_FILE_PROTOCOL *This,\r
1768 OUT UINT64 *Position\r
1769 )\r
1770{\r
1771 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetPosition(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, Position);\r
1772}\r
1773\r
1774/**\r
1775 Get information about a file.\r
1776\r
1777 @param This Protocol instance pointer.\r
1778 @param InformationType Type of information to return in Buffer.\r
1779 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1780 @param Buffer The buffer to return data.\r
1781\r
1782 @retval EFI_SUCCESS Data was returned.\r
1783 @retval EFI_UNSUPPORT InformationType is not supported.\r
1784 @retval EFI_NO_MEDIA The device has no media.\r
1785 @retval EFI_DEVICE_ERROR The device reported an error.\r
1786 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1787 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1788 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1789 @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.\r
1790\r
1791**/\r
1792EFI_STATUS\r
1793EFIAPI\r
1794FileInterfaceFileGetInfo(\r
1795 IN EFI_FILE_PROTOCOL *This,\r
1796 IN EFI_GUID *InformationType,\r
1797 IN OUT UINTN *BufferSize,\r
1798 OUT VOID *Buffer\r
1799 )\r
1800{\r
1801 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetInfo(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, InformationType, BufferSize, Buffer);\r
1802}\r
1803\r
1804/**\r
1805 Set information about a file\r
1806\r
ae724571 1807 @param This Protocol instance pointer.\r
733f138d 1808 @param InformationType Type of information in Buffer.\r
1809 @param BufferSize Size of buffer.\r
1810 @param Buffer The data to write.\r
1811\r
1812 @retval EFI_SUCCESS Data was returned.\r
1813 @retval EFI_UNSUPPORT InformationType is not supported.\r
1814 @retval EFI_NO_MEDIA The device has no media.\r
1815 @retval EFI_DEVICE_ERROR The device reported an error.\r
1816 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1817 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1818 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1819\r
1820**/\r
1821EFI_STATUS\r
1822EFIAPI\r
1823FileInterfaceFileSetInfo(\r
1824 IN EFI_FILE_PROTOCOL *This,\r
1825 IN EFI_GUID *InformationType,\r
1826 IN UINTN BufferSize,\r
1827 IN VOID *Buffer\r
1828 )\r
1829{\r
1830 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->SetInfo(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, InformationType, BufferSize, Buffer);\r
1831}\r
1832\r
1833/**\r
1834 Flush data back for the file handle.\r
1835\r
1836 @param This Protocol instance pointer.\r
1837\r
1838 @retval EFI_SUCCESS Data was written.\r
1839 @retval EFI_UNSUPPORT Writes to Open directory are not supported.\r
1840 @retval EFI_NO_MEDIA The device has no media.\r
1841 @retval EFI_DEVICE_ERROR The device reported an error.\r
1842 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1843 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1844 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1845 @retval EFI_VOLUME_FULL The volume is full.\r
1846\r
1847**/\r
1848EFI_STATUS\r
1849EFIAPI\r
1850FileInterfaceFileFlush(\r
1851 IN EFI_FILE_PROTOCOL *This\r
1852 )\r
1853{\r
1854 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Flush(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
1855}\r
1856\r
1857/**\r
1858 Read data from the file.\r
1859\r
1860 @param This Protocol instance pointer.\r
1861 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1862 @param Buffer The buffer in which data is read.\r
1863\r
1864 @retval EFI_SUCCESS Data was read.\r
1865 @retval EFI_NO_MEDIA The device has no media.\r
1866 @retval EFI_DEVICE_ERROR The device reported an error.\r
1867 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1868 @retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains required size.\r
1869\r
1870**/\r
1871EFI_STATUS\r
1872EFIAPI\r
1873FileInterfaceFileRead(\r
1874 IN EFI_FILE_PROTOCOL *This,\r
1875 IN OUT UINTN *BufferSize,\r
1876 OUT VOID *Buffer\r
1877 )\r
1878{\r
48cb33ec
QS
1879 CHAR8 *AsciiStrBuffer;\r
1880 CHAR16 *UscStrBuffer;\r
733f138d 1881 UINTN Size;\r
48cb33ec 1882 UINTN CharNum;\r
733f138d 1883 EFI_STATUS Status;\r
1884 if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {\r
1885 //\r
1886 // Unicode\r
1887 //\r
1888 return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));\r
1889 } else {\r
1890 //\r
1891 // Ascii\r
1892 //\r
48cb33ec
QS
1893 Size = (*BufferSize) / sizeof(CHAR16);\r
1894 AsciiStrBuffer = AllocateZeroPool(Size + sizeof(CHAR8));\r
1895 if (AsciiStrBuffer == NULL) {\r
1896 return EFI_OUT_OF_RESOURCES;\r
1897 }\r
1898 UscStrBuffer = AllocateZeroPool(*BufferSize + sizeof(CHAR16));\r
1899 if (UscStrBuffer== NULL) {\r
1900 SHELL_FREE_NON_NULL(AsciiStrBuffer);\r
1901 return EFI_OUT_OF_RESOURCES;\r
1902 }\r
1903 Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiStrBuffer));\r
1904 if (!EFI_ERROR(Status)) {\r
1905 CharNum = UnicodeSPrint(UscStrBuffer, *BufferSize + sizeof(CHAR16), L"%a", AsciiStrBuffer);\r
1906 if (CharNum == Size) {\r
1907 CopyMem (Buffer, UscStrBuffer, *BufferSize);\r
1908 } else {\r
1909 Status = EFI_UNSUPPORTED;\r
1910 }\r
1911 }\r
1912 SHELL_FREE_NON_NULL(AsciiStrBuffer);\r
1913 SHELL_FREE_NON_NULL(UscStrBuffer);\r
733f138d 1914 return (Status);\r
1915 }\r
1916}\r
1917\r
1918/**\r
1919 Opens a new file relative to the source file's location.\r
1920\r
1921 @param[in] This The protocol instance pointer.\r
1922 @param[out] NewHandle Returns File Handle for FileName.\r
1923 @param[in] FileName Null terminated string. "\", ".", and ".." are supported.\r
1924 @param[in] OpenMode Open mode for file.\r
1925 @param[in] Attributes Only used for EFI_FILE_MODE_CREATE.\r
1926\r
1927 @retval EFI_SUCCESS The device was opened.\r
1928 @retval EFI_NOT_FOUND The specified file could not be found on the device.\r
1929 @retval EFI_NO_MEDIA The device has no media.\r
1930 @retval EFI_MEDIA_CHANGED The media has changed.\r
1931 @retval EFI_DEVICE_ERROR The device reported an error.\r
1932 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1933 @retval EFI_ACCESS_DENIED The service denied access to the file.\r
1934 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.\r
1935 @retval EFI_VOLUME_FULL The volume is full.\r
1936**/\r
1937EFI_STATUS\r
1938EFIAPI\r
1939FileInterfaceFileOpen (\r
1940 IN EFI_FILE_PROTOCOL *This,\r
1941 OUT EFI_FILE_PROTOCOL **NewHandle,\r
1942 IN CHAR16 *FileName,\r
1943 IN UINT64 OpenMode,\r
1944 IN UINT64 Attributes\r
1945 )\r
1946{\r
1947 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Open(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, NewHandle, FileName, OpenMode, Attributes);\r
1948}\r
1949\r
1950/**\r
1951 Close and delete the file handle.\r
1952\r
1953 @param This Protocol instance pointer.\r
1954 \r
1955 @retval EFI_SUCCESS The device was opened.\r
1956 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.\r
1957\r
1958**/\r
1959EFI_STATUS\r
1960EFIAPI\r
1961FileInterfaceFileDelete(\r
1962 IN EFI_FILE_PROTOCOL *This\r
1963 )\r
1964{\r
1965 EFI_STATUS Status;\r
1966 Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Delete(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
1967 FreePool(This);\r
1968 return (Status);\r
1969}\r
1970\r
a405b86d 1971/**\r
1972 File style interface for File (Close).\r
1973 \r
1974 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1975 \r
1976 @retval EFI_SUCCESS The file was closed.\r
1977**/\r
1978EFI_STATUS\r
1979EFIAPI\r
1980FileInterfaceFileClose(\r
1981 IN EFI_FILE_PROTOCOL *This\r
1982 )\r
1983{\r
733f138d 1984 EFI_STATUS Status;\r
1985 Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Close(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
a405b86d 1986 FreePool(This);\r
733f138d 1987 return (Status);\r
a405b86d 1988}\r
1989\r
1990/**\r
1991 File style interface for File (Write).\r
1992\r
1993 If the file was opened with ASCII mode the data will be processed through \r
1994 AsciiSPrint before writing.\r
1995 \r
4ff7e37b
ED
1996 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1997 @param[in, out] BufferSize Size in bytes of Buffer.\r
1998 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1999 \r
2000 @retval EFI_SUCCESS The data was written.\r
2001**/\r
2002EFI_STATUS\r
2003EFIAPI\r
2004FileInterfaceFileWrite(\r
733f138d 2005 IN EFI_FILE_PROTOCOL *This,\r
2006 IN OUT UINTN *BufferSize,\r
2007 IN VOID *Buffer\r
a405b86d 2008 )\r
2009{\r
2010 CHAR8 *AsciiBuffer;\r
2011 UINTN Size;\r
2012 EFI_STATUS Status;\r
2013 if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {\r
2014 //\r
2015 // Unicode\r
2016 //\r
2017 return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Write(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));\r
2018 } else {\r
2019 //\r
2020 // Ascii\r
2021 //\r
733f138d 2022 AsciiBuffer = AllocateZeroPool(*BufferSize);\r
a405b86d 2023 AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
2024 Size = AsciiStrSize(AsciiBuffer) - 1; // (we dont need the null terminator)\r
2025 Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Write(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiBuffer));\r
2026 FreePool(AsciiBuffer);\r
2027 return (Status);\r
2028 }\r
2029}\r
2030\r
2031/**\r
2032 Create a file interface with unicode information.\r
2033\r
2034 This will create a new EFI_FILE_PROTOCOL identical to the Templace\r
2035 except that the new one has Unicode and Ascii knowledge.\r
2036 \r
2037 @param[in] Template A pointer to the EFI_FILE_PROTOCOL object.\r
2038 @param[in] Unicode TRUE for UCS-2, FALSE for ASCII.\r
2039 \r
2040 @return a new EFI_FILE_PROTOCOL object to be used instead of the template.\r
2041**/\r
2042EFI_FILE_PROTOCOL*\r
2043CreateFileInterfaceFile(\r
2044 IN CONST EFI_FILE_PROTOCOL *Template,\r
2045 IN CONST BOOLEAN Unicode\r
2046 )\r
2047{\r
2048 EFI_FILE_PROTOCOL_FILE *NewOne;\r
2049\r
733f138d 2050 NewOne = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_FILE));\r
3c865f20 2051 if (NewOne == NULL) {\r
2052 return (NULL);\r
2053 }\r
a405b86d 2054 CopyMem(NewOne, Template, sizeof(EFI_FILE_PROTOCOL_FILE));\r
733f138d 2055 NewOne->Orig = (EFI_FILE_PROTOCOL *)Template;\r
2056 NewOne->Unicode = Unicode;\r
2057 NewOne->Open = FileInterfaceFileOpen;\r
2058 NewOne->Close = FileInterfaceFileClose;\r
2059 NewOne->Delete = FileInterfaceFileDelete;\r
2060 NewOne->Read = FileInterfaceFileRead;\r
2061 NewOne->Write = FileInterfaceFileWrite;\r
2062 NewOne->GetPosition = FileInterfaceFileGetPosition;\r
2063 NewOne->SetPosition = FileInterfaceFileSetPosition;\r
2064 NewOne->GetInfo = FileInterfaceFileGetInfo;\r
2065 NewOne->SetInfo = FileInterfaceFileSetInfo;\r
2066 NewOne->Flush = FileInterfaceFileFlush;\r
a405b86d 2067\r
2068 return ((EFI_FILE_PROTOCOL *)NewOne);\r
2069}\r