]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/FileHandleWrappers.c
SecurityPkg Tcg2ConfigDxe: Add setup option to configure PPI version
[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
9eec4d38 6 Copyright (c) 2009 - 2016, 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
416a423f 1077 }\r
31e5b912
RN
1078\r
1079 if (Volatile) {\r
c5c994c5
CC
1080 Status = SHELL_SET_ENVIRONMENT_VARIABLE_V (\r
1081 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1082 TotalSize - sizeof (CHAR16),\r
1083 NewBuffer\r
1084 );\r
1085\r
1086 if (!EFI_ERROR(Status)) {\r
1087 Status = ShellAddEnvVarToList (\r
1088 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1089 NewBuffer,\r
1090 TotalSize,\r
1091 EFI_VARIABLE_BOOTSERVICE_ACCESS\r
1092 );\r
1093 }\r
416a423f 1094 } else {\r
c5c994c5
CC
1095 Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV (\r
1096 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1097 TotalSize - sizeof (CHAR16),\r
1098 NewBuffer\r
1099 );\r
1100\r
1101 if (!EFI_ERROR(Status)) {\r
1102 Status = ShellAddEnvVarToList (\r
1103 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1104 NewBuffer,\r
1105 TotalSize,\r
1106 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
1107 );\r
1108 }\r
416a423f
CP
1109 }\r
1110 }\r
1111 } \r
1112 \r
1113 SHELL_FREE_NON_NULL(NewBuffer);\r
a405b86d 1114 FreePool((EFI_FILE_PROTOCOL_ENVIRONMENT*)This);\r
416a423f 1115 return (Status);\r
a405b86d 1116}\r
1117\r
1118/**\r
1119 File style interface for Environment Variable (Delete).\r
1120 \r
1121 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1122 \r
1123 @retval The return value from FileInterfaceEnvClose().\r
1124**/\r
1125EFI_STATUS\r
1126EFIAPI\r
1127FileInterfaceEnvDelete(\r
1128 IN EFI_FILE_PROTOCOL *This\r
1129 )\r
1130{\r
1131 SHELL_DELETE_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name);\r
1132 return (FileInterfaceEnvClose(This));\r
1133}\r
1134\r
1135/**\r
1136 File style interface for Environment Variable (Read).\r
1137 \r
4ff7e37b
ED
1138 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1139 @param[in, out] BufferSize Size in bytes of Buffer.\r
1140 @param[out] Buffer The pointer to the buffer to fill.\r
a405b86d 1141 \r
1142 @retval EFI_SUCCESS The data was read.\r
1143**/\r
1144EFI_STATUS\r
1145EFIAPI\r
1146FileInterfaceEnvRead(\r
1147 IN EFI_FILE_PROTOCOL *This,\r
1148 IN OUT UINTN *BufferSize,\r
1149 OUT VOID *Buffer\r
1150 )\r
1151{\r
1152 return (SHELL_GET_ENVIRONMENT_VARIABLE(\r
1153 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1154 BufferSize,\r
1155 Buffer));\r
1156}\r
1157\r
1158/**\r
1159 File style interface for Volatile Environment Variable (Write).\r
c5c994c5 1160 This function also caches the environment variable into gShellEnvVarList.\r
a405b86d 1161 \r
4ff7e37b
ED
1162 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1163 @param[in, out] BufferSize Size in bytes of Buffer.\r
1164 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1165 \r
c5c994c5
CC
1166 @retval EFI_SUCCESS The data was successfully write to variable.\r
1167 @retval SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
a405b86d 1168**/\r
1169EFI_STATUS\r
1170EFIAPI\r
1171FileInterfaceEnvVolWrite(\r
1172 IN EFI_FILE_PROTOCOL *This,\r
1173 IN OUT UINTN *BufferSize,\r
1174 IN VOID *Buffer\r
1175 )\r
1176{\r
1177 VOID* NewBuffer;\r
1178 UINTN NewSize;\r
1179 EFI_STATUS Status;\r
c5c994c5 1180 UINTN TotalSize;\r
a405b86d 1181\r
1182 NewBuffer = NULL;\r
1183 NewSize = 0;\r
c5c994c5 1184 TotalSize = 0;\r
a405b86d 1185\r
1186 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
c5c994c5
CC
1187 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1188 TotalSize = NewSize + *BufferSize + sizeof (CHAR16);\r
1189 } else if (Status == EFI_NOT_FOUND) {\r
1190 TotalSize = *BufferSize + sizeof(CHAR16);\r
1191 } else {\r
1192 return Status;\r
1193 }\r
1194\r
1195 NewBuffer = AllocateZeroPool (TotalSize);\r
1196 if (NewBuffer == NULL) {\r
1197 return EFI_OUT_OF_RESOURCES;\r
1198 }\r
1199\r
1200 if (Status == EFI_BUFFER_TOO_SMALL) {\r
a405b86d 1201 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1202 }\r
c5c994c5
CC
1203\r
1204 if (EFI_ERROR (Status) && Status != EFI_NOT_FOUND) {\r
1205 FreePool (NewBuffer);\r
1206 return Status;\r
1207 }\r
1208\r
1209 CopyMem ((UINT8*)NewBuffer + NewSize, Buffer, *BufferSize);\r
1210 Status = ShellAddEnvVarToList (\r
1211 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1212 NewBuffer,\r
1213 TotalSize,\r
1214 EFI_VARIABLE_BOOTSERVICE_ACCESS\r
1215 );\r
1216 if (EFI_ERROR(Status)) {\r
1217 FreePool (NewBuffer);\r
1218 return Status;\r
1219 }\r
1220\r
1221 Status = SHELL_SET_ENVIRONMENT_VARIABLE_V (\r
1222 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1223 TotalSize - sizeof (CHAR16),\r
1224 NewBuffer\r
1225 );\r
1226 if (EFI_ERROR(Status)) {\r
1227 ShellRemvoeEnvVarFromList (((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name);\r
a405b86d 1228 }\r
c5c994c5
CC
1229\r
1230 FreePool (NewBuffer);\r
1231 return Status;\r
a405b86d 1232}\r
1233\r
1234\r
1235/**\r
1236 File style interface for Non Volatile Environment Variable (Write).\r
c5c994c5 1237 This function also caches the environment variable into gShellEnvVarList.\r
a405b86d 1238 \r
4ff7e37b
ED
1239 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1240 @param[in, out] BufferSize Size in bytes of Buffer.\r
1241 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1242 \r
c5c994c5
CC
1243 @retval EFI_SUCCESS The data was successfully write to variable.\r
1244 @retval SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
a405b86d 1245**/\r
1246EFI_STATUS\r
1247EFIAPI\r
1248FileInterfaceEnvNonVolWrite(\r
1249 IN EFI_FILE_PROTOCOL *This,\r
1250 IN OUT UINTN *BufferSize,\r
1251 IN VOID *Buffer\r
1252 )\r
1253{\r
1254 VOID* NewBuffer;\r
1255 UINTN NewSize;\r
1256 EFI_STATUS Status;\r
c5c994c5 1257 UINTN TotalSize;\r
a405b86d 1258\r
1259 NewBuffer = NULL;\r
1260 NewSize = 0;\r
c5c994c5 1261 TotalSize = 0;\r
a405b86d 1262\r
1263 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
c5c994c5
CC
1264 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1265 TotalSize = NewSize + *BufferSize + sizeof (CHAR16);\r
1266 } else if (Status == EFI_NOT_FOUND) {\r
1267 TotalSize = *BufferSize + sizeof (CHAR16);\r
1268 } else {\r
1269 return Status;\r
1270 }\r
1271\r
1272 NewBuffer = AllocateZeroPool (TotalSize);\r
1273 if (NewBuffer == NULL) {\r
1274 return EFI_OUT_OF_RESOURCES;\r
1275 }\r
1276\r
1277 if (Status == EFI_BUFFER_TOO_SMALL) {\r
a405b86d 1278 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1279 }\r
c5c994c5
CC
1280\r
1281 if (EFI_ERROR(Status) && Status != EFI_NOT_FOUND) {\r
1282 FreePool (NewBuffer);\r
1283 return Status;\r
a405b86d 1284 }\r
c5c994c5
CC
1285\r
1286 CopyMem ((UINT8*) NewBuffer + NewSize, Buffer, *BufferSize);\r
1287 Status = ShellAddEnvVarToList (\r
1288 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1289 NewBuffer,\r
1290 TotalSize,\r
1291 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
1292 );\r
1293 if (EFI_ERROR (Status)) {\r
1294 FreePool (NewBuffer);\r
1295 return Status;\r
1296 }\r
1297\r
1298 Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV (\r
1299 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1300 TotalSize - sizeof (CHAR16),\r
1301 NewBuffer\r
1302 );\r
1303 if (EFI_ERROR (Status)) {\r
1304 ShellRemvoeEnvVarFromList (((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name);\r
1305 }\r
1306\r
1307 FreePool (NewBuffer);\r
1308 return Status;\r
a405b86d 1309}\r
1310\r
1311/**\r
1312 Creates a EFI_FILE_PROTOCOL (almost) object for using to access\r
1313 environment variables through file operations.\r
1314\r
1315 @param EnvName The name of the Environment Variable to be operated on.\r
1316\r
1317 @retval NULL Memory could not be allocated.\r
1318 @return other a pointer to an EFI_FILE_PROTOCOL structure\r
1319**/\r
1320EFI_FILE_PROTOCOL*\r
a405b86d 1321CreateFileInterfaceEnv(\r
1322 IN CONST CHAR16 *EnvName\r
1323 )\r
1324{\r
31e5b912 1325 EFI_STATUS Status;\r
a405b86d 1326 EFI_FILE_PROTOCOL_ENVIRONMENT *EnvFileInterface;\r
323d3d11 1327 UINTN EnvNameSize;\r
31e5b912 1328 BOOLEAN Volatile;\r
a405b86d 1329\r
1330 if (EnvName == NULL) {\r
1331 return (NULL);\r
1332 }\r
1333\r
31e5b912
RN
1334 Status = IsVolatileEnv (EnvName, &Volatile);\r
1335 if (EFI_ERROR (Status)) {\r
1336 return NULL;\r
1337 }\r
1338\r
a405b86d 1339 //\r
1340 // Get some memory\r
1341 //\r
323d3d11
QS
1342 EnvNameSize = StrSize(EnvName);\r
1343 EnvFileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_ENVIRONMENT)+EnvNameSize);\r
a405b86d 1344 if (EnvFileInterface == NULL){\r
1345 return (NULL);\r
1346 }\r
1347\r
1348 //\r
1349 // Assign the generic members\r
1350 //\r
1351 EnvFileInterface->Revision = EFI_FILE_REVISION;\r
1352 EnvFileInterface->Open = FileInterfaceOpenNotFound;\r
1353 EnvFileInterface->Close = FileInterfaceEnvClose;\r
1354 EnvFileInterface->GetPosition = FileInterfaceNopGetPosition;\r
1355 EnvFileInterface->SetPosition = FileInterfaceNopSetPosition;\r
1356 EnvFileInterface->GetInfo = FileInterfaceNopGetInfo;\r
1357 EnvFileInterface->SetInfo = FileInterfaceNopSetInfo;\r
1358 EnvFileInterface->Flush = FileInterfaceNopGeneric;\r
1359 EnvFileInterface->Delete = FileInterfaceEnvDelete;\r
1360 EnvFileInterface->Read = FileInterfaceEnvRead;\r
323d3d11
QS
1361 \r
1362 CopyMem(EnvFileInterface->Name, EnvName, EnvNameSize);\r
a405b86d 1363\r
1364 //\r
1365 // Assign the different members for Volatile and Non-Volatile variables\r
1366 //\r
31e5b912 1367 if (Volatile) {\r
a405b86d 1368 EnvFileInterface->Write = FileInterfaceEnvVolWrite;\r
1369 } else {\r
1370 EnvFileInterface->Write = FileInterfaceEnvNonVolWrite;\r
1371 }\r
1372 return ((EFI_FILE_PROTOCOL *)EnvFileInterface);\r
1373}\r
1374\r
1375/**\r
1376 Move the cursor position one character backward.\r
1377\r
1378 @param[in] LineLength Length of a line. Get it by calling QueryMode\r
4ff7e37b
ED
1379 @param[in, out] Column Current column of the cursor position\r
1380 @param[in, out] Row Current row of the cursor position\r
a405b86d 1381**/\r
1382VOID\r
a405b86d 1383MoveCursorBackward (\r
1384 IN UINTN LineLength,\r
1385 IN OUT UINTN *Column,\r
1386 IN OUT UINTN *Row\r
1387 )\r
1388{\r
1389 //\r
1390 // If current column is 0, move to the last column of the previous line,\r
1391 // otherwise, just decrement column.\r
1392 //\r
1393 if (*Column == 0) {\r
1394 *Column = LineLength - 1;\r
1395 if (*Row > 0) {\r
1396 (*Row)--;\r
1397 }\r
1398 return;\r
1399 }\r
1400 (*Column)--;\r
1401}\r
1402\r
1403/**\r
1404 Move the cursor position one character forward.\r
1405\r
1406 @param[in] LineLength Length of a line.\r
1407 @param[in] TotalRow Total row of a screen\r
4ff7e37b
ED
1408 @param[in, out] Column Current column of the cursor position\r
1409 @param[in, out] Row Current row of the cursor position\r
a405b86d 1410**/\r
1411VOID\r
a405b86d 1412MoveCursorForward (\r
1413 IN UINTN LineLength,\r
1414 IN UINTN TotalRow,\r
1415 IN OUT UINTN *Column,\r
1416 IN OUT UINTN *Row\r
1417 )\r
1418{\r
1419 //\r
1420 // Increment Column.\r
1421 // If this puts column past the end of the line, move to first column\r
1422 // of the next row.\r
1423 //\r
1424 (*Column)++;\r
1425 if (*Column >= LineLength) {\r
1426 (*Column) = 0;\r
1427 if ((*Row) < TotalRow - 1) {\r
1428 (*Row)++;\r
1429 }\r
1430 }\r
1431}\r
1432\r
1433/**\r
1434 Prints out each previously typed command in the command list history log.\r
1435\r
1436 When each screen is full it will pause for a key before continuing.\r
1437\r
1438 @param[in] TotalCols How many columns are on the screen\r
1439 @param[in] TotalRows How many rows are on the screen\r
1440 @param[in] StartColumn which column to start at\r
1441**/\r
1442VOID\r
a405b86d 1443PrintCommandHistory (\r
1444 IN CONST UINTN TotalCols,\r
1445 IN CONST UINTN TotalRows,\r
1446 IN CONST UINTN StartColumn\r
1447 )\r
1448{\r
1449 BUFFER_LIST *Node;\r
1450 UINTN Index;\r
1451 UINTN LineNumber;\r
1452 UINTN LineCount;\r
1453\r
1454 ShellPrintEx (-1, -1, L"\n");\r
1455 Index = 0;\r
1456 LineNumber = 0;\r
1457 //\r
1458 // go through history list...\r
1459 //\r
1460 for ( Node = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link)\r
1461 ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link)\r
1462 ; Node = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link)\r
1463 ){\r
1464 Index++;\r
1465 LineCount = ((StrLen (Node->Buffer) + StartColumn + 1) / TotalCols) + 1;\r
1466\r
1467 if (LineNumber + LineCount >= TotalRows) {\r
1468 ShellPromptForResponseHii(\r
1469 ShellPromptResponseTypeEnterContinue,\r
1470 STRING_TOKEN (STR_SHELL_ENTER_TO_CONT),\r
1471 ShellInfoObject.HiiHandle,\r
1472 NULL\r
1473 );\r
1474 LineNumber = 0;\r
1475 }\r
1476 ShellPrintEx (-1, -1, L"%2d. %s\n", Index, Node->Buffer);\r
1477 LineNumber += LineCount;\r
1478 }\r
1479}\r
1480\r
1481\r
1482\r
1483\r
1484\r
1485\r
1486//\r
1487// This is identical to EFI_FILE_PROTOCOL except for the additional members\r
1488// for the buffer, size, and position.\r
1489//\r
1490\r
1491typedef struct {\r
1492 UINT64 Revision;\r
1493 EFI_FILE_OPEN Open;\r
1494 EFI_FILE_CLOSE Close;\r
1495 EFI_FILE_DELETE Delete;\r
1496 EFI_FILE_READ Read;\r
1497 EFI_FILE_WRITE Write;\r
1498 EFI_FILE_GET_POSITION GetPosition;\r
1499 EFI_FILE_SET_POSITION SetPosition;\r
1500 EFI_FILE_GET_INFO GetInfo;\r
1501 EFI_FILE_SET_INFO SetInfo;\r
1502 EFI_FILE_FLUSH Flush;\r
1503 VOID *Buffer;\r
1504 UINT64 Position;\r
1505 UINT64 BufferSize;\r
1506 BOOLEAN Unicode;\r
7bcd3ff6 1507 UINT64 FileSize;\r
a405b86d 1508} EFI_FILE_PROTOCOL_MEM;\r
1509\r
1510/**\r
1511 File style interface for Mem (SetPosition).\r
1512 \r
1513 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1514 @param[out] Position The position to set.\r
1515 \r
1516 @retval EFI_SUCCESS The position was successfully changed.\r
1517 @retval EFI_INVALID_PARAMETER The Position was invalid.\r
1518**/\r
1519EFI_STATUS\r
1520EFIAPI\r
1521FileInterfaceMemSetPosition(\r
1522 IN EFI_FILE_PROTOCOL *This,\r
1523 OUT UINT64 Position\r
1524 )\r
1525{\r
7bcd3ff6 1526 if (Position <= ((EFI_FILE_PROTOCOL_MEM*)This)->FileSize) {\r
a405b86d 1527 ((EFI_FILE_PROTOCOL_MEM*)This)->Position = Position;\r
1528 return (EFI_SUCCESS);\r
1529 } else {\r
1530 return (EFI_INVALID_PARAMETER);\r
1531 }\r
1532}\r
1533\r
1534/**\r
1535 File style interface for Mem (GetPosition).\r
1536 \r
1537 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1538 @param[out] Position The pointer to the position.\r
1539 \r
1540 @retval EFI_SUCCESS The position was retrieved.\r
1541**/ \r
1542EFI_STATUS\r
1543EFIAPI\r
1544FileInterfaceMemGetPosition(\r
1545 IN EFI_FILE_PROTOCOL *This,\r
1546 OUT UINT64 *Position\r
1547 )\r
1548{\r
1549 *Position = ((EFI_FILE_PROTOCOL_MEM*)This)->Position;\r
1550 return (EFI_SUCCESS);\r
1551}\r
1552\r
1553/**\r
1554 File style interface for Mem (Write).\r
1555 \r
4ff7e37b
ED
1556 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1557 @param[in, out] BufferSize Size in bytes of Buffer.\r
1558 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1559 \r
ecae5117 1560 @retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources.\r
1561 @retval EFI_SUCCESS The data was written.\r
a405b86d 1562**/\r
1563EFI_STATUS\r
1564EFIAPI\r
1565FileInterfaceMemWrite(\r
1566 IN EFI_FILE_PROTOCOL *This,\r
1567 IN OUT UINTN *BufferSize,\r
1568 IN VOID *Buffer\r
1569 )\r
1570{\r
9ed21946 1571 CHAR8 *AsciiBuffer;\r
1572 EFI_FILE_PROTOCOL_MEM *MemFile;\r
1573\r
1574 MemFile = (EFI_FILE_PROTOCOL_MEM *) This;\r
1575 if (MemFile->Unicode) {\r
a405b86d 1576 //\r
1577 // Unicode\r
1578 //\r
9ed21946 1579 if ((UINTN)(MemFile->Position + (*BufferSize)) > (UINTN)(MemFile->BufferSize)) {\r
c8d9d0e2
JD
1580 MemFile->Buffer = ReallocatePool((UINTN)(MemFile->BufferSize), (UINTN)(MemFile->BufferSize) + (*BufferSize) + MEM_WRITE_REALLOC_OVERHEAD, MemFile->Buffer);\r
1581 MemFile->BufferSize += (*BufferSize) + MEM_WRITE_REALLOC_OVERHEAD;\r
a405b86d 1582 }\r
9ed21946 1583 CopyMem(((UINT8*)MemFile->Buffer) + MemFile->Position, Buffer, *BufferSize);\r
1584 MemFile->Position += (*BufferSize);\r
7bcd3ff6 1585 MemFile->FileSize = MemFile->Position;\r
a405b86d 1586 return (EFI_SUCCESS);\r
1587 } else {\r
1588 //\r
1589 // Ascii\r
1590 //\r
733f138d 1591 AsciiBuffer = AllocateZeroPool(*BufferSize);\r
ecae5117 1592 if (AsciiBuffer == NULL) {\r
1593 return (EFI_OUT_OF_RESOURCES);\r
1594 }\r
a405b86d 1595 AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
9ed21946 1596 if ((UINTN)(MemFile->Position + AsciiStrSize(AsciiBuffer)) > (UINTN)(MemFile->BufferSize)) {\r
c8d9d0e2
JD
1597 MemFile->Buffer = ReallocatePool((UINTN)(MemFile->BufferSize), (UINTN)(MemFile->BufferSize) + AsciiStrSize(AsciiBuffer) + MEM_WRITE_REALLOC_OVERHEAD, MemFile->Buffer);\r
1598 MemFile->BufferSize += AsciiStrSize(AsciiBuffer) + MEM_WRITE_REALLOC_OVERHEAD;\r
a405b86d 1599 }\r
9ed21946 1600 CopyMem(((UINT8*)MemFile->Buffer) + MemFile->Position, AsciiBuffer, AsciiStrSize(AsciiBuffer));\r
1601 MemFile->Position += (*BufferSize / sizeof(CHAR16));\r
7bcd3ff6 1602 MemFile->FileSize = MemFile->Position;\r
a405b86d 1603 FreePool(AsciiBuffer);\r
1604 return (EFI_SUCCESS);\r
1605 }\r
1606}\r
1607\r
1608/**\r
1609 File style interface for Mem (Read).\r
1610 \r
4ff7e37b
ED
1611 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1612 @param[in, out] BufferSize Size in bytes of Buffer.\r
1613 @param[in] Buffer The pointer to the buffer to fill.\r
a405b86d 1614 \r
1615 @retval EFI_SUCCESS The data was read.\r
1616**/\r
1617EFI_STATUS\r
1618EFIAPI\r
1619FileInterfaceMemRead(\r
1620 IN EFI_FILE_PROTOCOL *This,\r
1621 IN OUT UINTN *BufferSize,\r
1622 IN VOID *Buffer\r
1623 )\r
1624{\r
9ed21946 1625 EFI_FILE_PROTOCOL_MEM *MemFile;\r
1626\r
1627 MemFile = (EFI_FILE_PROTOCOL_MEM *) This;\r
7bcd3ff6
JD
1628 if (*BufferSize > (UINTN)((MemFile->FileSize) - (UINTN)(MemFile->Position))) {\r
1629 (*BufferSize) = (UINTN)((MemFile->FileSize) - (UINTN)(MemFile->Position));\r
a405b86d 1630 }\r
9ed21946 1631 CopyMem(Buffer, ((UINT8*)MemFile->Buffer) + MemFile->Position, (*BufferSize));\r
1632 MemFile->Position = MemFile->Position + (*BufferSize);\r
a405b86d 1633 return (EFI_SUCCESS);\r
1634}\r
1635\r
1636/**\r
1637 File style interface for Mem (Close).\r
1638\r
1639 Frees all memory associated with this object.\r
1640 \r
1641 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1642 \r
1643 @retval EFI_SUCCESS The 'file' was closed.\r
1644**/ \r
1645EFI_STATUS\r
1646EFIAPI\r
1647FileInterfaceMemClose(\r
1648 IN EFI_FILE_PROTOCOL *This\r
1649 )\r
1650{\r
1651 SHELL_FREE_NON_NULL(((EFI_FILE_PROTOCOL_MEM*)This)->Buffer);\r
3c865f20 1652 SHELL_FREE_NON_NULL(This);\r
a405b86d 1653 return (EFI_SUCCESS);\r
1654}\r
1655\r
1656/**\r
1657 Creates a EFI_FILE_PROTOCOL (almost) object for using to access\r
1658 a file entirely in memory through file operations.\r
1659\r
1660 @param[in] Unicode Boolean value with TRUE for Unicode and FALSE for Ascii.\r
1661\r
1662 @retval NULL Memory could not be allocated.\r
1663 @return other A pointer to an EFI_FILE_PROTOCOL structure.\r
1664**/\r
1665EFI_FILE_PROTOCOL*\r
a405b86d 1666CreateFileInterfaceMem(\r
1667 IN CONST BOOLEAN Unicode\r
1668 )\r
1669{\r
1670 EFI_FILE_PROTOCOL_MEM *FileInterface;\r
1671\r
1672 //\r
1673 // Get some memory\r
1674 //\r
1675 FileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_MEM));\r
1676 if (FileInterface == NULL){\r
1677 return (NULL);\r
1678 }\r
1679\r
1680 //\r
1681 // Assign the generic members\r
1682 //\r
1683 FileInterface->Revision = EFI_FILE_REVISION;\r
1684 FileInterface->Open = FileInterfaceOpenNotFound;\r
1685 FileInterface->Close = FileInterfaceMemClose;\r
1686 FileInterface->GetPosition = FileInterfaceMemGetPosition;\r
1687 FileInterface->SetPosition = FileInterfaceMemSetPosition;\r
1688 FileInterface->GetInfo = FileInterfaceNopGetInfo;\r
1689 FileInterface->SetInfo = FileInterfaceNopSetInfo;\r
1690 FileInterface->Flush = FileInterfaceNopGeneric;\r
1691 FileInterface->Delete = FileInterfaceNopGeneric;\r
1692 FileInterface->Read = FileInterfaceMemRead;\r
1693 FileInterface->Write = FileInterfaceMemWrite;\r
1694 FileInterface->Unicode = Unicode;\r
1695\r
1696 ASSERT(FileInterface->Buffer == NULL);\r
1697 ASSERT(FileInterface->BufferSize == 0);\r
1698 ASSERT(FileInterface->Position == 0);\r
1699\r
9ed21946 1700 if (Unicode) {\r
1701 FileInterface->Buffer = AllocateZeroPool(sizeof(gUnicodeFileTag));\r
9eec4d38
QS
1702 if (FileInterface->Buffer == NULL) {\r
1703 FreePool (FileInterface);\r
1704 return NULL;\r
1705 }\r
9ed21946 1706 *((CHAR16 *) (FileInterface->Buffer)) = EFI_UNICODE_BYTE_ORDER_MARK;\r
1707 FileInterface->BufferSize = 2;\r
1708 FileInterface->Position = 2;\r
1709 }\r
1710\r
a405b86d 1711 return ((EFI_FILE_PROTOCOL *)FileInterface);\r
1712}\r
1713\r
1714typedef struct {\r
1715 UINT64 Revision;\r
1716 EFI_FILE_OPEN Open;\r
1717 EFI_FILE_CLOSE Close;\r
1718 EFI_FILE_DELETE Delete;\r
1719 EFI_FILE_READ Read;\r
1720 EFI_FILE_WRITE Write;\r
1721 EFI_FILE_GET_POSITION GetPosition;\r
1722 EFI_FILE_SET_POSITION SetPosition;\r
1723 EFI_FILE_GET_INFO GetInfo;\r
1724 EFI_FILE_SET_INFO SetInfo;\r
1725 EFI_FILE_FLUSH Flush;\r
1726 BOOLEAN Unicode;\r
1727 EFI_FILE_PROTOCOL *Orig;\r
1728} EFI_FILE_PROTOCOL_FILE;\r
1729\r
733f138d 1730/**\r
1731 Set a files current position\r
1732\r
1733 @param This Protocol instance pointer.\r
1734 @param Position Byte position from the start of the file.\r
1735 \r
1736 @retval EFI_SUCCESS Data was written.\r
1737 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.\r
1738\r
1739**/\r
1740EFI_STATUS\r
1741EFIAPI\r
1742FileInterfaceFileSetPosition(\r
1743 IN EFI_FILE_PROTOCOL *This,\r
1744 IN UINT64 Position\r
1745 )\r
1746{\r
1747 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->SetPosition(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, Position);\r
1748}\r
1749\r
1750/**\r
1751 Get a file's current position\r
1752\r
1753 @param This Protocol instance pointer.\r
1754 @param Position Byte position from the start of the file.\r
1755 \r
1756 @retval EFI_SUCCESS Data was written.\r
1757 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open..\r
1758\r
1759**/\r
1760EFI_STATUS\r
1761EFIAPI\r
1762FileInterfaceFileGetPosition(\r
1763 IN EFI_FILE_PROTOCOL *This,\r
1764 OUT UINT64 *Position\r
1765 )\r
1766{\r
1767 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetPosition(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, Position);\r
1768}\r
1769\r
1770/**\r
1771 Get information about a file.\r
1772\r
1773 @param This Protocol instance pointer.\r
1774 @param InformationType Type of information to return in Buffer.\r
1775 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1776 @param Buffer The buffer to return data.\r
1777\r
1778 @retval EFI_SUCCESS Data was returned.\r
1779 @retval EFI_UNSUPPORT InformationType is not supported.\r
1780 @retval EFI_NO_MEDIA The device has no media.\r
1781 @retval EFI_DEVICE_ERROR The device reported an error.\r
1782 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1783 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1784 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1785 @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.\r
1786\r
1787**/\r
1788EFI_STATUS\r
1789EFIAPI\r
1790FileInterfaceFileGetInfo(\r
1791 IN EFI_FILE_PROTOCOL *This,\r
1792 IN EFI_GUID *InformationType,\r
1793 IN OUT UINTN *BufferSize,\r
1794 OUT VOID *Buffer\r
1795 )\r
1796{\r
1797 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetInfo(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, InformationType, BufferSize, Buffer);\r
1798}\r
1799\r
1800/**\r
1801 Set information about a file\r
1802\r
ae724571 1803 @param This Protocol instance pointer.\r
733f138d 1804 @param InformationType Type of information in Buffer.\r
1805 @param BufferSize Size of buffer.\r
1806 @param Buffer The data to write.\r
1807\r
1808 @retval EFI_SUCCESS Data was returned.\r
1809 @retval EFI_UNSUPPORT InformationType is not supported.\r
1810 @retval EFI_NO_MEDIA The device has no media.\r
1811 @retval EFI_DEVICE_ERROR The device reported an error.\r
1812 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1813 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1814 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1815\r
1816**/\r
1817EFI_STATUS\r
1818EFIAPI\r
1819FileInterfaceFileSetInfo(\r
1820 IN EFI_FILE_PROTOCOL *This,\r
1821 IN EFI_GUID *InformationType,\r
1822 IN UINTN BufferSize,\r
1823 IN VOID *Buffer\r
1824 )\r
1825{\r
1826 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->SetInfo(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, InformationType, BufferSize, Buffer);\r
1827}\r
1828\r
1829/**\r
1830 Flush data back for the file handle.\r
1831\r
1832 @param This Protocol instance pointer.\r
1833\r
1834 @retval EFI_SUCCESS Data was written.\r
1835 @retval EFI_UNSUPPORT Writes to Open directory are not supported.\r
1836 @retval EFI_NO_MEDIA The device has no media.\r
1837 @retval EFI_DEVICE_ERROR The device reported an error.\r
1838 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1839 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1840 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1841 @retval EFI_VOLUME_FULL The volume is full.\r
1842\r
1843**/\r
1844EFI_STATUS\r
1845EFIAPI\r
1846FileInterfaceFileFlush(\r
1847 IN EFI_FILE_PROTOCOL *This\r
1848 )\r
1849{\r
1850 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Flush(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
1851}\r
1852\r
1853/**\r
1854 Read data from the file.\r
1855\r
1856 @param This Protocol instance pointer.\r
1857 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1858 @param Buffer The buffer in which data is read.\r
1859\r
1860 @retval EFI_SUCCESS Data was read.\r
1861 @retval EFI_NO_MEDIA The device has no media.\r
1862 @retval EFI_DEVICE_ERROR The device reported an error.\r
1863 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1864 @retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains required size.\r
1865\r
1866**/\r
1867EFI_STATUS\r
1868EFIAPI\r
1869FileInterfaceFileRead(\r
1870 IN EFI_FILE_PROTOCOL *This,\r
1871 IN OUT UINTN *BufferSize,\r
1872 OUT VOID *Buffer\r
1873 )\r
1874{\r
48cb33ec
QS
1875 CHAR8 *AsciiStrBuffer;\r
1876 CHAR16 *UscStrBuffer;\r
733f138d 1877 UINTN Size;\r
48cb33ec 1878 UINTN CharNum;\r
733f138d 1879 EFI_STATUS Status;\r
1880 if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {\r
1881 //\r
1882 // Unicode\r
1883 //\r
1884 return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));\r
1885 } else {\r
1886 //\r
1887 // Ascii\r
1888 //\r
48cb33ec
QS
1889 Size = (*BufferSize) / sizeof(CHAR16);\r
1890 AsciiStrBuffer = AllocateZeroPool(Size + sizeof(CHAR8));\r
1891 if (AsciiStrBuffer == NULL) {\r
1892 return EFI_OUT_OF_RESOURCES;\r
1893 }\r
1894 UscStrBuffer = AllocateZeroPool(*BufferSize + sizeof(CHAR16));\r
1895 if (UscStrBuffer== NULL) {\r
1896 SHELL_FREE_NON_NULL(AsciiStrBuffer);\r
1897 return EFI_OUT_OF_RESOURCES;\r
1898 }\r
1899 Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiStrBuffer));\r
1900 if (!EFI_ERROR(Status)) {\r
1901 CharNum = UnicodeSPrint(UscStrBuffer, *BufferSize + sizeof(CHAR16), L"%a", AsciiStrBuffer);\r
1902 if (CharNum == Size) {\r
1903 CopyMem (Buffer, UscStrBuffer, *BufferSize);\r
1904 } else {\r
1905 Status = EFI_UNSUPPORTED;\r
1906 }\r
1907 }\r
1908 SHELL_FREE_NON_NULL(AsciiStrBuffer);\r
1909 SHELL_FREE_NON_NULL(UscStrBuffer);\r
733f138d 1910 return (Status);\r
1911 }\r
1912}\r
1913\r
1914/**\r
1915 Opens a new file relative to the source file's location.\r
1916\r
1917 @param[in] This The protocol instance pointer.\r
1918 @param[out] NewHandle Returns File Handle for FileName.\r
1919 @param[in] FileName Null terminated string. "\", ".", and ".." are supported.\r
1920 @param[in] OpenMode Open mode for file.\r
1921 @param[in] Attributes Only used for EFI_FILE_MODE_CREATE.\r
1922\r
1923 @retval EFI_SUCCESS The device was opened.\r
1924 @retval EFI_NOT_FOUND The specified file could not be found on the device.\r
1925 @retval EFI_NO_MEDIA The device has no media.\r
1926 @retval EFI_MEDIA_CHANGED The media has changed.\r
1927 @retval EFI_DEVICE_ERROR The device reported an error.\r
1928 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1929 @retval EFI_ACCESS_DENIED The service denied access to the file.\r
1930 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.\r
1931 @retval EFI_VOLUME_FULL The volume is full.\r
1932**/\r
1933EFI_STATUS\r
1934EFIAPI\r
1935FileInterfaceFileOpen (\r
1936 IN EFI_FILE_PROTOCOL *This,\r
1937 OUT EFI_FILE_PROTOCOL **NewHandle,\r
1938 IN CHAR16 *FileName,\r
1939 IN UINT64 OpenMode,\r
1940 IN UINT64 Attributes\r
1941 )\r
1942{\r
1943 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Open(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, NewHandle, FileName, OpenMode, Attributes);\r
1944}\r
1945\r
1946/**\r
1947 Close and delete the file handle.\r
1948\r
1949 @param This Protocol instance pointer.\r
1950 \r
1951 @retval EFI_SUCCESS The device was opened.\r
1952 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.\r
1953\r
1954**/\r
1955EFI_STATUS\r
1956EFIAPI\r
1957FileInterfaceFileDelete(\r
1958 IN EFI_FILE_PROTOCOL *This\r
1959 )\r
1960{\r
1961 EFI_STATUS Status;\r
1962 Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Delete(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
1963 FreePool(This);\r
1964 return (Status);\r
1965}\r
1966\r
a405b86d 1967/**\r
1968 File style interface for File (Close).\r
1969 \r
1970 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1971 \r
1972 @retval EFI_SUCCESS The file was closed.\r
1973**/\r
1974EFI_STATUS\r
1975EFIAPI\r
1976FileInterfaceFileClose(\r
1977 IN EFI_FILE_PROTOCOL *This\r
1978 )\r
1979{\r
733f138d 1980 EFI_STATUS Status;\r
1981 Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Close(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
a405b86d 1982 FreePool(This);\r
733f138d 1983 return (Status);\r
a405b86d 1984}\r
1985\r
1986/**\r
1987 File style interface for File (Write).\r
1988\r
1989 If the file was opened with ASCII mode the data will be processed through \r
1990 AsciiSPrint before writing.\r
1991 \r
4ff7e37b
ED
1992 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1993 @param[in, out] BufferSize Size in bytes of Buffer.\r
1994 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1995 \r
1996 @retval EFI_SUCCESS The data was written.\r
1997**/\r
1998EFI_STATUS\r
1999EFIAPI\r
2000FileInterfaceFileWrite(\r
733f138d 2001 IN EFI_FILE_PROTOCOL *This,\r
2002 IN OUT UINTN *BufferSize,\r
2003 IN VOID *Buffer\r
a405b86d 2004 )\r
2005{\r
2006 CHAR8 *AsciiBuffer;\r
2007 UINTN Size;\r
2008 EFI_STATUS Status;\r
2009 if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {\r
2010 //\r
2011 // Unicode\r
2012 //\r
2013 return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Write(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));\r
2014 } else {\r
2015 //\r
2016 // Ascii\r
2017 //\r
733f138d 2018 AsciiBuffer = AllocateZeroPool(*BufferSize);\r
a405b86d 2019 AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
2020 Size = AsciiStrSize(AsciiBuffer) - 1; // (we dont need the null terminator)\r
2021 Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Write(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiBuffer));\r
2022 FreePool(AsciiBuffer);\r
2023 return (Status);\r
2024 }\r
2025}\r
2026\r
2027/**\r
2028 Create a file interface with unicode information.\r
2029\r
2030 This will create a new EFI_FILE_PROTOCOL identical to the Templace\r
2031 except that the new one has Unicode and Ascii knowledge.\r
2032 \r
2033 @param[in] Template A pointer to the EFI_FILE_PROTOCOL object.\r
2034 @param[in] Unicode TRUE for UCS-2, FALSE for ASCII.\r
2035 \r
2036 @return a new EFI_FILE_PROTOCOL object to be used instead of the template.\r
2037**/\r
2038EFI_FILE_PROTOCOL*\r
2039CreateFileInterfaceFile(\r
2040 IN CONST EFI_FILE_PROTOCOL *Template,\r
2041 IN CONST BOOLEAN Unicode\r
2042 )\r
2043{\r
2044 EFI_FILE_PROTOCOL_FILE *NewOne;\r
2045\r
733f138d 2046 NewOne = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_FILE));\r
3c865f20 2047 if (NewOne == NULL) {\r
2048 return (NULL);\r
2049 }\r
a405b86d 2050 CopyMem(NewOne, Template, sizeof(EFI_FILE_PROTOCOL_FILE));\r
733f138d 2051 NewOne->Orig = (EFI_FILE_PROTOCOL *)Template;\r
2052 NewOne->Unicode = Unicode;\r
2053 NewOne->Open = FileInterfaceFileOpen;\r
2054 NewOne->Close = FileInterfaceFileClose;\r
2055 NewOne->Delete = FileInterfaceFileDelete;\r
2056 NewOne->Read = FileInterfaceFileRead;\r
2057 NewOne->Write = FileInterfaceFileWrite;\r
2058 NewOne->GetPosition = FileInterfaceFileGetPosition;\r
2059 NewOne->SetPosition = FileInterfaceFileSetPosition;\r
2060 NewOne->GetInfo = FileInterfaceFileGetInfo;\r
2061 NewOne->SetInfo = FileInterfaceFileSetInfo;\r
2062 NewOne->Flush = FileInterfaceFileFlush;\r
a405b86d 2063\r
2064 return ((EFI_FILE_PROTOCOL *)NewOne);\r
2065}\r