]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/FileHandleWrappers.c
ShellPkg: Fix Shell ASSERT when read 'TAB' key fail from 'ConIn'.
[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
295/**\r
296 File style interface for console (Read).\r
297\r
298 This will return a single line of input from the console.\r
299\r
300 @param This A pointer to the EFI_FILE_PROTOCOL instance that is the\r
301 file handle to read data from. Not used.\r
302 @param BufferSize On input, the size of the Buffer. On output, the amount\r
303 of data returned in Buffer. In both cases, the size is\r
304 measured in bytes.\r
305 @param Buffer The buffer into which the data is read.\r
306\r
307\r
308 @retval EFI_SUCCESS The data was read.\r
309 @retval EFI_NO_MEDIA The device has no medium.\r
310 @retval EFI_DEVICE_ERROR The device reported an error.\r
311 @retval EFI_DEVICE_ERROR An attempt was made to read from a deleted file.\r
312 @retval EFI_DEVICE_ERROR On entry, the current file position is beyond the end of the file.\r
313 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
314 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory\r
315 entry. BufferSize has been updated with the size\r
316 needed to complete the request.\r
317 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
318**/\r
319EFI_STATUS\r
320EFIAPI\r
321FileInterfaceStdInRead(\r
322 IN EFI_FILE_PROTOCOL *This,\r
323 IN OUT UINTN *BufferSize,\r
324 OUT VOID *Buffer\r
325 )\r
326{\r
327 CHAR16 *CurrentString;\r
328 BOOLEAN Done;\r
329 UINTN Column; // Column of current cursor\r
330 UINTN Row; // Row of current cursor\r
331 UINTN StartColumn; // Column at the beginning of the line\r
332 UINTN Update; // Line index for update\r
333 UINTN Delete; // Num of chars to delete from console after update\r
334 UINTN StringLen; // Total length of the line\r
335 UINTN StringCurPos; // Line index corresponding to the cursor\r
336 UINTN MaxStr; // Maximum possible line length\r
337 UINTN Index;\r
338 UINTN TotalColumn; // Num of columns in the console\r
339 UINTN TotalRow; // Num of rows in the console\r
340 UINTN SkipLength;\r
341 UINTN OutputLength; // Length of the update string\r
342 UINTN TailRow; // Row of end of line\r
343 UINTN TailColumn; // Column of end of line\r
344 EFI_INPUT_KEY Key;\r
345\r
346 BUFFER_LIST *LinePos;\r
347 BUFFER_LIST *NewPos;\r
348 BOOLEAN InScrolling;\r
349 EFI_STATUS Status;\r
350 BOOLEAN InTabScrolling; // Whether in TAB-completion state\r
351 EFI_SHELL_FILE_INFO *FoundFileList;\r
352 EFI_SHELL_FILE_INFO *TabLinePos;\r
353 EFI_SHELL_FILE_INFO *TempPos;\r
354 CHAR16 *TabStr;\r
355 CHAR16 *TabOutputStr;\r
356 BOOLEAN InQuotationMode;\r
357 CHAR16 *TempStr;\r
358 UINTN TabPos; // Start index of the string to search for TAB completion.\r
359 UINTN TabUpdatePos; // Start index of the string updated by TAB stroke\r
360// UINTN Count;\r
361 UINTN EventIndex;\r
362 CONST CHAR16 *Cwd;\r
363\r
364 //\r
365 // If buffer is not large enough to hold a CHAR16, return minimum buffer size\r
366 //\r
367 if (*BufferSize < sizeof (CHAR16) * 2) {\r
368 *BufferSize = sizeof (CHAR16) * 2;\r
369 return (EFI_BUFFER_TOO_SMALL);\r
370 }\r
371\r
372 Done = FALSE;\r
373 CurrentString = Buffer;\r
374 StringLen = 0;\r
375 StringCurPos = 0;\r
376 OutputLength = 0;\r
377 Update = 0;\r
378 Delete = 0;\r
379 LinePos = NewPos = (BUFFER_LIST*)(&ShellInfoObject.ViewingSettings.CommandHistory);\r
380 InScrolling = FALSE;\r
381 InTabScrolling = FALSE;\r
382 Status = EFI_SUCCESS;\r
383 TabLinePos = NULL;\r
384 FoundFileList = NULL;\r
385 TempPos = NULL;\r
386 TabPos = 0;\r
387 TabUpdatePos = 0;\r
388\r
389 //\r
390 // Allocate buffers\r
391 //\r
392 TabStr = AllocateZeroPool (*BufferSize);\r
393 if (TabStr == NULL) {\r
394 return EFI_OUT_OF_RESOURCES;\r
395 }\r
396 TabOutputStr = AllocateZeroPool (*BufferSize);\r
397 if (TabOutputStr == NULL) {\r
398 FreePool(TabStr);\r
399 return EFI_OUT_OF_RESOURCES;\r
400 }\r
401\r
402 //\r
403 // Get the screen setting and the current cursor location\r
404 //\r
405 Column = StartColumn = gST->ConOut->Mode->CursorColumn;\r
406 Row = gST->ConOut->Mode->CursorRow;\r
407 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &TotalColumn, &TotalRow);\r
408\r
409 //\r
410 // Limit the line length to the buffer size or the minimun size of the\r
411 // screen. (The smaller takes effect)\r
412 //\r
413 MaxStr = TotalColumn * (TotalRow - 1) - StartColumn;\r
414 if (MaxStr > *BufferSize / sizeof (CHAR16)) {\r
415 MaxStr = *BufferSize / sizeof (CHAR16);\r
416 }\r
417 ZeroMem (CurrentString, MaxStr * sizeof (CHAR16));\r
418 do {\r
419 //\r
420 // Read a key\r
421 //\r
422 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
423 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
424 if (EFI_ERROR (Status)) {\r
4dd8c7af 425 break;\r
a405b86d 426 }\r
427\r
428 //\r
429 // Press PageUp or PageDown to scroll the history screen up or down.\r
430 // Press any other key to quit scrolling.\r
431 //\r
432 if (Key.UnicodeChar == 0 && (Key.ScanCode == SCAN_PAGE_UP || Key.ScanCode == SCAN_PAGE_DOWN)) {\r
433 if (Key.ScanCode == SCAN_PAGE_UP) {\r
434 ConsoleLoggerDisplayHistory(FALSE, 0, ShellInfoObject.ConsoleInfo);\r
435 } else if (Key.ScanCode == SCAN_PAGE_DOWN) {\r
436 ConsoleLoggerDisplayHistory(TRUE, 0, ShellInfoObject.ConsoleInfo);\r
437 }\r
438\r
439 InScrolling = TRUE;\r
440 } else {\r
441 if (InScrolling) {\r
442 ConsoleLoggerStopHistory(ShellInfoObject.ConsoleInfo);\r
443 InScrolling = FALSE;\r
444 }\r
445 }\r
446\r
447 //\r
448 // If we are quitting TAB scrolling...\r
449 //\r
450 if (InTabScrolling && Key.UnicodeChar != CHAR_TAB) {\r
451 if (FoundFileList != NULL) {\r
452 ShellInfoObject.NewEfiShellProtocol->FreeFileList (&FoundFileList);\r
453 DEBUG_CODE(FoundFileList = NULL;);\r
454 }\r
455 InTabScrolling = FALSE;\r
456 }\r
457\r
458 switch (Key.UnicodeChar) {\r
459 case CHAR_CARRIAGE_RETURN:\r
460 //\r
461 // All done, print a newline at the end of the string\r
462 //\r
463 TailRow = Row + (StringLen - StringCurPos + Column) / TotalColumn;\r
464 TailColumn = (StringLen - StringCurPos + Column) % TotalColumn;\r
465 ShellPrintEx ((INT32)TailColumn, (INT32)TailRow, L"%N\n");\r
466 Done = TRUE;\r
467 break;\r
468\r
469 case CHAR_BACKSPACE:\r
470 if (StringCurPos != 0) {\r
471 //\r
472 // If not move back beyond string beginning, move all characters behind\r
473 // the current position one character forward\r
474 //\r
475 StringCurPos--;\r
476 Update = StringCurPos;\r
477 Delete = 1;\r
478 CopyMem (CurrentString + StringCurPos, CurrentString + StringCurPos + 1, sizeof (CHAR16) * (StringLen - StringCurPos));\r
479\r
480 //\r
481 // Adjust the current column and row\r
482 //\r
483 MoveCursorBackward (TotalColumn, &Column, &Row);\r
484 }\r
485 break;\r
486\r
487 case CHAR_TAB:\r
488 //\r
489 // handle auto complete of file and directory names...\r
490 //\r
491 if (InTabScrolling) {\r
492 ASSERT(FoundFileList != NULL);\r
493 ASSERT(TabLinePos != NULL);\r
494 TabLinePos = (EFI_SHELL_FILE_INFO*)GetNextNode(&(FoundFileList->Link), &TabLinePos->Link);\r
495 if (IsNull(&(FoundFileList->Link), &TabLinePos->Link)) {\r
496 TabLinePos = (EFI_SHELL_FILE_INFO*)GetNextNode(&(FoundFileList->Link), &TabLinePos->Link);\r
497 }\r
498 } else {\r
499 TabPos = 0;\r
500 TabUpdatePos = 0;\r
501 InQuotationMode = FALSE;\r
502 for (Index = 0; Index < StringLen; Index++) {\r
503 if (CurrentString[Index] == L'\"') {\r
504 InQuotationMode = (BOOLEAN)(!InQuotationMode);\r
505 }\r
506 if (CurrentString[Index] == L' ' && !InQuotationMode) {\r
507 TabPos = Index + 1;\r
508 TabUpdatePos = Index + 1;\r
509 }\r
510 if (CurrentString[Index] == L'\\') {\r
511 TabUpdatePos = Index + 1;\r
512 }\r
513 }\r
514 if (StrStr(CurrentString + TabPos, L":") == NULL) {\r
515 Cwd = ShellInfoObject.NewEfiShellProtocol->GetCurDir(NULL);\r
516 if (Cwd != NULL) {\r
4dc0d578 517 StrnCpyS(TabStr, (*BufferSize)/sizeof(CHAR16), Cwd, (*BufferSize)/sizeof(CHAR16) - 1);\r
fbd2dfad 518 StrCatS(TabStr, (*BufferSize)/sizeof(CHAR16), L"\\");\r
a405b86d 519 if (TabStr[StrLen(TabStr)-1] == L'\\' && *(CurrentString + TabPos) == L'\\' ) {\r
520 TabStr[StrLen(TabStr)-1] = CHAR_NULL;\r
521 }\r
e75390f0
QS
522 StrnCatS( TabStr, \r
523 (*BufferSize)/sizeof(CHAR16), \r
524 CurrentString + TabPos, \r
6956ecfe 525 StringLen - TabPos\r
e75390f0 526 );\r
a405b86d 527 } else {\r
7f79b01e 528 *TabStr = CHAR_NULL;\r
6956ecfe 529 StrnCatS(TabStr, (*BufferSize)/sizeof(CHAR16), CurrentString + TabPos, StringLen - TabPos);\r
a405b86d 530 }\r
531 } else {\r
4dc0d578 532 StrnCpyS(TabStr, (*BufferSize)/sizeof(CHAR16), CurrentString + TabPos, (*BufferSize)/sizeof(CHAR16) - 1);\r
a405b86d 533 }\r
e75390f0 534 StrnCatS(TabStr, (*BufferSize)/sizeof(CHAR16), L"*", (*BufferSize)/sizeof(CHAR16) - 1 - StrLen(TabStr));\r
a405b86d 535 FoundFileList = NULL;\r
a405b86d 536 Status = ShellInfoObject.NewEfiShellProtocol->FindFiles(TabStr, &FoundFileList);\r
537 for ( TempStr = CurrentString\r
538 ; *TempStr == L' '\r
539 ; TempStr++); // note the ';'... empty for loop\r
540 //\r
541 // make sure we have a list before we do anything more...\r
542 //\r
543 if (EFI_ERROR (Status) || FoundFileList == NULL) {\r
544 InTabScrolling = FALSE;\r
545 TabLinePos = NULL;\r
546 continue;\r
547 } else {\r
548 //\r
549 // enumerate through the list of files\r
550 //\r
551 for ( TempPos = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(FoundFileList->Link))\r
552 ; !IsNull(&FoundFileList->Link, &TempPos->Link)\r
553 ; TempPos = (EFI_SHELL_FILE_INFO*)GetNextNode(&(FoundFileList->Link), &(TempPos->Link))\r
554 ){\r
555 //\r
556 // If "cd" is typed, only directory name will be auto-complete filled\r
557 // in either case . and .. will be removed.\r
558 //\r
559 if ((((TempStr[0] == L'c' || TempStr[0] == L'C') &&\r
560 (TempStr[1] == L'd' || TempStr[1] == L'D')\r
561 ) && ((ShellIsDirectory(TempPos->FullName) != EFI_SUCCESS)\r
562 ||(StrCmp(TempPos->FileName, L".") == 0)\r
563 ||(StrCmp(TempPos->FileName, L"..") == 0)\r
564 )) || ((StrCmp(TempPos->FileName, L".") == 0)\r
565 ||(StrCmp(TempPos->FileName, L"..") == 0))){\r
566 TabLinePos = TempPos;\r
567 TempPos = (EFI_SHELL_FILE_INFO*)(RemoveEntryList(&(TempPos->Link))->BackLink);\r
568 InternalFreeShellFileInfoNode(TabLinePos);\r
569 }\r
570 }\r
571 if (FoundFileList != NULL && !IsListEmpty(&FoundFileList->Link)) {\r
572 TabLinePos = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FoundFileList->Link);\r
573 InTabScrolling = TRUE;\r
574 } else {\r
9c17810a 575 ShellInfoObject.NewEfiShellProtocol->FreeFileList (&FoundFileList);\r
a405b86d 576 }\r
577 }\r
578 }\r
579 break;\r
580\r
581 default:\r
582 if (Key.UnicodeChar >= ' ') {\r
583 //\r
584 // If we are at the buffer's end, drop the key\r
585 //\r
586 if (StringLen == MaxStr - 1 && (ShellInfoObject.ViewingSettings.InsertMode || StringCurPos == StringLen)) {\r
587 break;\r
588 }\r
589 //\r
590 // If in insert mode, make space by moving each other character 1\r
591 // space higher in the array\r
592 //\r
593 if (ShellInfoObject.ViewingSettings.InsertMode) {\r
594 CopyMem(CurrentString + StringCurPos + 1, CurrentString + StringCurPos, (StringLen - StringCurPos)*sizeof(CurrentString[0]));\r
595 }\r
596\r
597 CurrentString[StringCurPos] = Key.UnicodeChar;\r
598 Update = StringCurPos;\r
599\r
600 StringCurPos += 1;\r
601 OutputLength = 1;\r
602 }\r
603 break;\r
604\r
605 case 0:\r
606 switch (Key.ScanCode) {\r
607 case SCAN_DELETE:\r
608 //\r
609 // Move characters behind current position one character forward\r
610 //\r
611 if (StringLen != 0) {\r
612 Update = StringCurPos;\r
613 Delete = 1;\r
614 CopyMem (CurrentString + StringCurPos, CurrentString + StringCurPos + 1, sizeof (CHAR16) * (StringLen - StringCurPos));\r
615 }\r
616 break;\r
617\r
618 case SCAN_UP:\r
619 //\r
620 // Prepare to print the previous command\r
621 //\r
622 NewPos = (BUFFER_LIST*)GetPreviousNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link);\r
623 if (IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link)) {\r
624 NewPos = (BUFFER_LIST*)GetPreviousNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link);\r
625 }\r
626 break;\r
627\r
628 case SCAN_DOWN:\r
629 //\r
630 // Prepare to print the next command\r
631 //\r
632 NewPos = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link);\r
633 if (NewPos == (BUFFER_LIST*)(&ShellInfoObject.ViewingSettings.CommandHistory)) {\r
634 NewPos = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &LinePos->Link);\r
635 }\r
636 break;\r
637\r
638 case SCAN_LEFT:\r
639 //\r
640 // Adjust current cursor position\r
641 //\r
642 if (StringCurPos != 0) {\r
643 --StringCurPos;\r
644 MoveCursorBackward (TotalColumn, &Column, &Row);\r
645 }\r
646 break;\r
647\r
648 case SCAN_RIGHT:\r
649 //\r
650 // Adjust current cursor position\r
651 //\r
652 if (StringCurPos < StringLen) {\r
653 ++StringCurPos;\r
654 MoveCursorForward (TotalColumn, TotalRow, &Column, &Row);\r
655 }\r
656 break;\r
657\r
658 case SCAN_HOME:\r
659 //\r
660 // Move current cursor position to the beginning of the command line\r
661 //\r
662 Row -= (StringCurPos + StartColumn) / TotalColumn;\r
663 Column = StartColumn;\r
664 StringCurPos = 0;\r
665 break;\r
666\r
667 case SCAN_END:\r
668 //\r
669 // Move current cursor position to the end of the command line\r
670 //\r
671 TailRow = Row + (StringLen - StringCurPos + Column) / TotalColumn;\r
672 TailColumn = (StringLen - StringCurPos + Column) % TotalColumn;\r
673 Row = TailRow;\r
674 Column = TailColumn;\r
675 StringCurPos = StringLen;\r
676 break;\r
677\r
678 case SCAN_ESC:\r
679 //\r
680 // Prepare to clear the current command line\r
681 //\r
682 CurrentString[0] = 0;\r
683 Update = 0;\r
684 Delete = StringLen;\r
685 Row -= (StringCurPos + StartColumn) / TotalColumn;\r
686 Column = StartColumn;\r
687 OutputLength = 0;\r
688 break;\r
689\r
690 case SCAN_INSERT:\r
691 //\r
692 // Toggle the SEnvInsertMode flag\r
693 //\r
694 ShellInfoObject.ViewingSettings.InsertMode = (BOOLEAN)!ShellInfoObject.ViewingSettings.InsertMode;\r
695 break;\r
696\r
697 case SCAN_F7:\r
698 //\r
699 // Print command history\r
700 //\r
701 PrintCommandHistory (TotalColumn, TotalRow, 4);\r
702 *CurrentString = CHAR_NULL;\r
703 Done = TRUE;\r
704 break;\r
705 }\r
706 }\r
707\r
708 if (Done) {\r
709 break;\r
710 }\r
711\r
712 //\r
713 // If we are in auto-complete mode, we are preparing to print\r
714 // the next file or directory name\r
715 //\r
716 if (InTabScrolling) {\r
717 //\r
718 // Adjust the column and row to the start of TAB-completion string.\r
719 //\r
720 Column = (StartColumn + TabUpdatePos) % TotalColumn;\r
721 Row -= (StartColumn + StringCurPos) / TotalColumn - (StartColumn + TabUpdatePos) / TotalColumn;\r
722 OutputLength = StrLen (TabLinePos->FileName);\r
723 //\r
724 // if the output string contains blank space, quotation marks L'\"'\r
725 // should be added to the output.\r
726 //\r
727 if (StrStr(TabLinePos->FileName, L" ") != NULL){\r
728 TabOutputStr[0] = L'\"';\r
729 CopyMem (TabOutputStr + 1, TabLinePos->FileName, OutputLength * sizeof (CHAR16));\r
730 TabOutputStr[OutputLength + 1] = L'\"';\r
731 TabOutputStr[OutputLength + 2] = CHAR_NULL;\r
732 } else {\r
733 CopyMem (TabOutputStr, TabLinePos->FileName, OutputLength * sizeof (CHAR16));\r
734 TabOutputStr[OutputLength] = CHAR_NULL;\r
735 }\r
736 OutputLength = StrLen (TabOutputStr) < MaxStr - 1 ? StrLen (TabOutputStr) : MaxStr - 1;\r
737 CopyMem (CurrentString + TabUpdatePos, TabOutputStr, OutputLength * sizeof (CHAR16));\r
738 CurrentString[TabUpdatePos + OutputLength] = CHAR_NULL;\r
739 StringCurPos = TabUpdatePos + OutputLength;\r
740 Update = TabUpdatePos;\r
741 if (StringLen > TabUpdatePos + OutputLength) {\r
742 Delete = StringLen - TabUpdatePos - OutputLength;\r
743 }\r
744 }\r
745\r
746 //\r
747 // If we have a new position, we are preparing to print a previous or\r
748 // next command.\r
749 //\r
750 if (NewPos != (BUFFER_LIST*)(&ShellInfoObject.ViewingSettings.CommandHistory)) {\r
751 Column = StartColumn;\r
752 Row -= (StringCurPos + StartColumn) / TotalColumn;\r
753\r
754 LinePos = NewPos;\r
755 NewPos = (BUFFER_LIST*)(&ShellInfoObject.ViewingSettings.CommandHistory);\r
756\r
757 OutputLength = StrLen (LinePos->Buffer) < MaxStr - 1 ? StrLen (LinePos->Buffer) : MaxStr - 1;\r
758 CopyMem (CurrentString, LinePos->Buffer, OutputLength * sizeof (CHAR16));\r
759 CurrentString[OutputLength] = CHAR_NULL;\r
760\r
761 StringCurPos = OutputLength;\r
762\r
763 //\r
764 // Draw new input string\r
765 //\r
766 Update = 0;\r
767 if (StringLen > OutputLength) {\r
768 //\r
769 // If old string was longer, blank its tail\r
770 //\r
771 Delete = StringLen - OutputLength;\r
772 }\r
773 }\r
774 //\r
775 // If we need to update the output do so now\r
776 //\r
777 if (Update != (UINTN) -1) {\r
778 ShellPrintEx ((INT32)Column, (INT32)Row, L"%s%.*s", CurrentString + Update, Delete, L"");\r
779 StringLen = StrLen (CurrentString);\r
780\r
781 if (Delete != 0) {\r
782 SetMem (CurrentString + StringLen, Delete * sizeof (CHAR16), CHAR_NULL);\r
783 }\r
784\r
785 if (StringCurPos > StringLen) {\r
786 StringCurPos = StringLen;\r
787 }\r
788\r
789 Update = (UINTN) -1;\r
790\r
791 //\r
792 // After using print to reflect newly updates, if we're not using\r
793 // BACKSPACE and DELETE, we need to move the cursor position forward,\r
794 // so adjust row and column here.\r
795 //\r
796 if (Key.UnicodeChar != CHAR_BACKSPACE && !(Key.UnicodeChar == 0 && Key.ScanCode == SCAN_DELETE)) {\r
797 //\r
798 // Calulate row and column of the tail of current string\r
799 //\r
800 TailRow = Row + (StringLen - StringCurPos + Column + OutputLength) / TotalColumn;\r
801 TailColumn = (StringLen - StringCurPos + Column + OutputLength) % TotalColumn;\r
802\r
803 //\r
804 // If the tail of string reaches screen end, screen rolls up, so if\r
805 // Row does not equal TailRow, Row should be decremented\r
806 //\r
807 // (if we are recalling commands using UPPER and DOWN key, and if the\r
808 // old command is too long to fit the screen, TailColumn must be 79.\r
809 //\r
810 if (TailColumn == 0 && TailRow >= TotalRow && Row != TailRow) {\r
811 Row--;\r
812 }\r
813 //\r
814 // Calculate the cursor position after current operation. If cursor\r
815 // reaches line end, update both row and column, otherwise, only\r
816 // column will be changed.\r
817 //\r
818 if (Column + OutputLength >= TotalColumn) {\r
819 SkipLength = OutputLength - (TotalColumn - Column);\r
820\r
821 Row += SkipLength / TotalColumn + 1;\r
822 if (Row > TotalRow - 1) {\r
823 Row = TotalRow - 1;\r
824 }\r
825\r
826 Column = SkipLength % TotalColumn;\r
827 } else {\r
828 Column += OutputLength;\r
829 }\r
830 }\r
831\r
832 Delete = 0;\r
833 }\r
834 //\r
835 // Set the cursor position for this key\r
836 //\r
837 gST->ConOut->SetCursorPosition (gST->ConOut, Column, Row);\r
838 } while (!Done);\r
839\r
840 if (CurrentString != NULL && StrLen(CurrentString) > 0) {\r
841 //\r
842 // add the line to the history buffer\r
843 //\r
844 AddLineToCommandHistory(CurrentString);\r
845 }\r
846\r
847 FreePool (TabStr);\r
848 FreePool (TabOutputStr);\r
849 //\r
850 // Return the data to the caller\r
851 //\r
852 *BufferSize = StringLen * sizeof (CHAR16);\r
853\r
854 //\r
855 // if this was used it should be deallocated by now...\r
856 // prevent memory leaks...\r
857 //\r
9c17810a
QS
858 if (FoundFileList != NULL) {\r
859 ShellInfoObject.NewEfiShellProtocol->FreeFileList (&FoundFileList);\r
860 }\r
a405b86d 861 ASSERT(FoundFileList == NULL);\r
862\r
4dd8c7af 863 return Status;\r
a405b86d 864}\r
865\r
866//\r
867// FILE sytle interfaces for StdIn/StdOut/StdErr\r
868//\r
869EFI_FILE_PROTOCOL FileInterfaceStdIn = {\r
870 EFI_FILE_REVISION,\r
871 FileInterfaceOpenNotFound,\r
872 FileInterfaceNopGeneric,\r
873 FileInterfaceNopGeneric,\r
874 FileInterfaceStdInRead,\r
875 FileInterfaceStdInWrite,\r
876 FileInterfaceNopGetPosition,\r
877 FileInterfaceNopSetPosition,\r
878 FileInterfaceNopGetInfo,\r
879 FileInterfaceNopSetInfo,\r
880 FileInterfaceNopGeneric\r
881};\r
882\r
883EFI_FILE_PROTOCOL FileInterfaceStdOut = {\r
884 EFI_FILE_REVISION,\r
885 FileInterfaceOpenNotFound,\r
886 FileInterfaceNopGeneric,\r
887 FileInterfaceNopGeneric,\r
888 FileInterfaceStdOutRead,\r
889 FileInterfaceStdOutWrite,\r
890 FileInterfaceNopGetPosition,\r
891 FileInterfaceNopSetPosition,\r
892 FileInterfaceNopGetInfo,\r
893 FileInterfaceNopSetInfo,\r
894 FileInterfaceNopGeneric\r
895};\r
896\r
897EFI_FILE_PROTOCOL FileInterfaceStdErr = {\r
898 EFI_FILE_REVISION,\r
899 FileInterfaceOpenNotFound,\r
900 FileInterfaceNopGeneric,\r
901 FileInterfaceNopGeneric,\r
902 FileInterfaceStdErrRead,\r
903 FileInterfaceStdErrWrite,\r
904 FileInterfaceNopGetPosition,\r
905 FileInterfaceNopSetPosition,\r
906 FileInterfaceNopGetInfo,\r
907 FileInterfaceNopSetInfo,\r
908 FileInterfaceNopGeneric\r
909};\r
910\r
911EFI_FILE_PROTOCOL FileInterfaceNulFile = {\r
912 EFI_FILE_REVISION,\r
913 FileInterfaceOpenNotFound,\r
914 FileInterfaceNopGeneric,\r
915 FileInterfaceNopGeneric,\r
916 FileInterfaceNulRead,\r
917 FileInterfaceNulWrite,\r
918 FileInterfaceNopGetPosition,\r
919 FileInterfaceNopSetPosition,\r
920 FileInterfaceNopGetInfo,\r
921 FileInterfaceNopSetInfo,\r
922 FileInterfaceNopGeneric\r
923};\r
924\r
925\r
926\r
927\r
928//\r
929// This is identical to EFI_FILE_PROTOCOL except for the additional member\r
930// for the name.\r
931//\r
932\r
933typedef struct {\r
934 UINT64 Revision;\r
935 EFI_FILE_OPEN Open;\r
936 EFI_FILE_CLOSE Close;\r
937 EFI_FILE_DELETE Delete;\r
938 EFI_FILE_READ Read;\r
939 EFI_FILE_WRITE Write;\r
940 EFI_FILE_GET_POSITION GetPosition;\r
941 EFI_FILE_SET_POSITION SetPosition;\r
942 EFI_FILE_GET_INFO GetInfo;\r
943 EFI_FILE_SET_INFO SetInfo;\r
944 EFI_FILE_FLUSH Flush;\r
945 CHAR16 Name[1];\r
946} EFI_FILE_PROTOCOL_ENVIRONMENT;\r
947//ANSI compliance helper to get size of the struct.\r
948#define SIZE_OF_EFI_FILE_PROTOCOL_ENVIRONMENT EFI_FIELD_OFFSET (EFI_FILE_PROTOCOL_ENVIRONMENT, Name)\r
949\r
950/**\r
951 File style interface for Environment Variable (Close).\r
952\r
953 Frees the memory for this object.\r
954 \r
955 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
956 \r
957 @retval EFI_SUCCESS\r
958**/\r
959EFI_STATUS\r
960EFIAPI\r
961FileInterfaceEnvClose(\r
962 IN EFI_FILE_PROTOCOL *This\r
963 )\r
964{\r
416a423f
CP
965 VOID* NewBuffer;\r
966 UINTN NewSize;\r
967 EFI_STATUS Status;\r
968\r
969 //\r
970 // Most if not all UEFI commands will have an '\r\n' at the end of any output. \r
971 // Since the output was redirected to a variable, it does not make sense to \r
972 // keep this. So, before closing, strip the trailing '\r\n' from the variable\r
973 // if it exists.\r
974 //\r
975 NewBuffer = NULL;\r
976 NewSize = 0;\r
977\r
978 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
979 if (Status == EFI_BUFFER_TOO_SMALL) {\r
980 NewBuffer = AllocateZeroPool(NewSize + sizeof(CHAR16));\r
981 if (NewBuffer == NULL) {\r
982 return EFI_OUT_OF_RESOURCES;\r
983 } \r
984 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
985 }\r
986 \r
03bc7c2b 987 if (!EFI_ERROR(Status) && NewBuffer != NULL) {\r
416a423f
CP
988 \r
989 if (StrSize(NewBuffer) > 6)\r
990 {\r
991 if ((((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 2] == CHAR_LINEFEED) \r
992 && (((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 3] == CHAR_CARRIAGE_RETURN)) {\r
993 ((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 3] = CHAR_NULL; \r
994 }\r
995 \r
996 if (IsVolatileEnv(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name)) {\r
997 Status = SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer);\r
998 } else {\r
999 Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer);\r
1000 }\r
1001 }\r
1002 } \r
1003 \r
1004 SHELL_FREE_NON_NULL(NewBuffer);\r
a405b86d 1005 FreePool((EFI_FILE_PROTOCOL_ENVIRONMENT*)This);\r
416a423f 1006 return (Status);\r
a405b86d 1007}\r
1008\r
1009/**\r
1010 File style interface for Environment Variable (Delete).\r
1011 \r
1012 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1013 \r
1014 @retval The return value from FileInterfaceEnvClose().\r
1015**/\r
1016EFI_STATUS\r
1017EFIAPI\r
1018FileInterfaceEnvDelete(\r
1019 IN EFI_FILE_PROTOCOL *This\r
1020 )\r
1021{\r
1022 SHELL_DELETE_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name);\r
1023 return (FileInterfaceEnvClose(This));\r
1024}\r
1025\r
1026/**\r
1027 File style interface for Environment Variable (Read).\r
1028 \r
4ff7e37b
ED
1029 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1030 @param[in, out] BufferSize Size in bytes of Buffer.\r
1031 @param[out] Buffer The pointer to the buffer to fill.\r
a405b86d 1032 \r
1033 @retval EFI_SUCCESS The data was read.\r
1034**/\r
1035EFI_STATUS\r
1036EFIAPI\r
1037FileInterfaceEnvRead(\r
1038 IN EFI_FILE_PROTOCOL *This,\r
1039 IN OUT UINTN *BufferSize,\r
1040 OUT VOID *Buffer\r
1041 )\r
1042{\r
1043 return (SHELL_GET_ENVIRONMENT_VARIABLE(\r
1044 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1045 BufferSize,\r
1046 Buffer));\r
1047}\r
1048\r
1049/**\r
1050 File style interface for Volatile Environment Variable (Write).\r
1051 \r
4ff7e37b
ED
1052 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1053 @param[in, out] BufferSize Size in bytes of Buffer.\r
1054 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1055 \r
1056 @retval EFI_SUCCESS The data was read.\r
1057**/\r
1058EFI_STATUS\r
1059EFIAPI\r
1060FileInterfaceEnvVolWrite(\r
1061 IN EFI_FILE_PROTOCOL *This,\r
1062 IN OUT UINTN *BufferSize,\r
1063 IN VOID *Buffer\r
1064 )\r
1065{\r
1066 VOID* NewBuffer;\r
1067 UINTN NewSize;\r
1068 EFI_STATUS Status;\r
1069\r
1070 NewBuffer = NULL;\r
1071 NewSize = 0;\r
1072\r
1073 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1074 if (Status == EFI_BUFFER_TOO_SMALL){\r
1075 NewBuffer = AllocateZeroPool(NewSize + *BufferSize + sizeof(CHAR16));\r
1076 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1077 }\r
1078 if (!EFI_ERROR(Status) && NewBuffer != NULL) {\r
1079 while (((CHAR16*)NewBuffer)[NewSize/2] == CHAR_NULL) {\r
1080 //\r
1081 // We want to overwrite the CHAR_NULL\r
1082 //\r
1083 NewSize -= 2;\r
1084 }\r
1085 CopyMem((UINT8*)NewBuffer + NewSize + 2, Buffer, *BufferSize);\r
1086 Status = SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer);\r
1087 FreePool(NewBuffer);\r
1088 return (Status);\r
1089 } else {\r
1090 SHELL_FREE_NON_NULL(NewBuffer);\r
1091 return (SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, *BufferSize, Buffer));\r
1092 }\r
1093}\r
1094\r
1095\r
1096/**\r
1097 File style interface for Non Volatile Environment Variable (Write).\r
1098 \r
4ff7e37b
ED
1099 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1100 @param[in, out] BufferSize Size in bytes of Buffer.\r
1101 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1102 \r
1103 @retval EFI_SUCCESS The data was read.\r
1104**/\r
1105EFI_STATUS\r
1106EFIAPI\r
1107FileInterfaceEnvNonVolWrite(\r
1108 IN EFI_FILE_PROTOCOL *This,\r
1109 IN OUT UINTN *BufferSize,\r
1110 IN VOID *Buffer\r
1111 )\r
1112{\r
1113 VOID* NewBuffer;\r
1114 UINTN NewSize;\r
1115 EFI_STATUS Status;\r
1116\r
1117 NewBuffer = NULL;\r
1118 NewSize = 0;\r
1119\r
1120 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1121 if (Status == EFI_BUFFER_TOO_SMALL){\r
1122 NewBuffer = AllocateZeroPool(NewSize + *BufferSize);\r
1123 Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
1124 }\r
1125 if (!EFI_ERROR(Status)) {\r
1126 CopyMem((UINT8*)NewBuffer + NewSize, Buffer, *BufferSize);\r
1127 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(\r
1128 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1129 NewSize + *BufferSize,\r
1130 NewBuffer));\r
1131 } else {\r
1132 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(\r
1133 ((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name,\r
1134 *BufferSize,\r
1135 Buffer));\r
1136 }\r
1137}\r
1138\r
1139/**\r
1140 Creates a EFI_FILE_PROTOCOL (almost) object for using to access\r
1141 environment variables through file operations.\r
1142\r
1143 @param EnvName The name of the Environment Variable to be operated on.\r
1144\r
1145 @retval NULL Memory could not be allocated.\r
1146 @return other a pointer to an EFI_FILE_PROTOCOL structure\r
1147**/\r
1148EFI_FILE_PROTOCOL*\r
1149EFIAPI\r
1150CreateFileInterfaceEnv(\r
1151 IN CONST CHAR16 *EnvName\r
1152 )\r
1153{\r
1154 EFI_FILE_PROTOCOL_ENVIRONMENT *EnvFileInterface;\r
323d3d11 1155 UINTN EnvNameSize;\r
a405b86d 1156\r
1157 if (EnvName == NULL) {\r
1158 return (NULL);\r
1159 }\r
1160\r
1161 //\r
1162 // Get some memory\r
1163 //\r
323d3d11
QS
1164 EnvNameSize = StrSize(EnvName);\r
1165 EnvFileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_ENVIRONMENT)+EnvNameSize);\r
a405b86d 1166 if (EnvFileInterface == NULL){\r
1167 return (NULL);\r
1168 }\r
1169\r
1170 //\r
1171 // Assign the generic members\r
1172 //\r
1173 EnvFileInterface->Revision = EFI_FILE_REVISION;\r
1174 EnvFileInterface->Open = FileInterfaceOpenNotFound;\r
1175 EnvFileInterface->Close = FileInterfaceEnvClose;\r
1176 EnvFileInterface->GetPosition = FileInterfaceNopGetPosition;\r
1177 EnvFileInterface->SetPosition = FileInterfaceNopSetPosition;\r
1178 EnvFileInterface->GetInfo = FileInterfaceNopGetInfo;\r
1179 EnvFileInterface->SetInfo = FileInterfaceNopSetInfo;\r
1180 EnvFileInterface->Flush = FileInterfaceNopGeneric;\r
1181 EnvFileInterface->Delete = FileInterfaceEnvDelete;\r
1182 EnvFileInterface->Read = FileInterfaceEnvRead;\r
323d3d11
QS
1183 \r
1184 CopyMem(EnvFileInterface->Name, EnvName, EnvNameSize);\r
a405b86d 1185\r
1186 //\r
1187 // Assign the different members for Volatile and Non-Volatile variables\r
1188 //\r
1189 if (IsVolatileEnv(EnvName)) {\r
1190 EnvFileInterface->Write = FileInterfaceEnvVolWrite;\r
1191 } else {\r
1192 EnvFileInterface->Write = FileInterfaceEnvNonVolWrite;\r
1193 }\r
1194 return ((EFI_FILE_PROTOCOL *)EnvFileInterface);\r
1195}\r
1196\r
1197/**\r
1198 Move the cursor position one character backward.\r
1199\r
1200 @param[in] LineLength Length of a line. Get it by calling QueryMode\r
4ff7e37b
ED
1201 @param[in, out] Column Current column of the cursor position\r
1202 @param[in, out] Row Current row of the cursor position\r
a405b86d 1203**/\r
1204VOID\r
1205EFIAPI\r
1206MoveCursorBackward (\r
1207 IN UINTN LineLength,\r
1208 IN OUT UINTN *Column,\r
1209 IN OUT UINTN *Row\r
1210 )\r
1211{\r
1212 //\r
1213 // If current column is 0, move to the last column of the previous line,\r
1214 // otherwise, just decrement column.\r
1215 //\r
1216 if (*Column == 0) {\r
1217 *Column = LineLength - 1;\r
1218 if (*Row > 0) {\r
1219 (*Row)--;\r
1220 }\r
1221 return;\r
1222 }\r
1223 (*Column)--;\r
1224}\r
1225\r
1226/**\r
1227 Move the cursor position one character forward.\r
1228\r
1229 @param[in] LineLength Length of a line.\r
1230 @param[in] TotalRow Total row of a screen\r
4ff7e37b
ED
1231 @param[in, out] Column Current column of the cursor position\r
1232 @param[in, out] Row Current row of the cursor position\r
a405b86d 1233**/\r
1234VOID\r
1235EFIAPI\r
1236MoveCursorForward (\r
1237 IN UINTN LineLength,\r
1238 IN UINTN TotalRow,\r
1239 IN OUT UINTN *Column,\r
1240 IN OUT UINTN *Row\r
1241 )\r
1242{\r
1243 //\r
1244 // Increment Column.\r
1245 // If this puts column past the end of the line, move to first column\r
1246 // of the next row.\r
1247 //\r
1248 (*Column)++;\r
1249 if (*Column >= LineLength) {\r
1250 (*Column) = 0;\r
1251 if ((*Row) < TotalRow - 1) {\r
1252 (*Row)++;\r
1253 }\r
1254 }\r
1255}\r
1256\r
1257/**\r
1258 Prints out each previously typed command in the command list history log.\r
1259\r
1260 When each screen is full it will pause for a key before continuing.\r
1261\r
1262 @param[in] TotalCols How many columns are on the screen\r
1263 @param[in] TotalRows How many rows are on the screen\r
1264 @param[in] StartColumn which column to start at\r
1265**/\r
1266VOID\r
1267EFIAPI\r
1268PrintCommandHistory (\r
1269 IN CONST UINTN TotalCols,\r
1270 IN CONST UINTN TotalRows,\r
1271 IN CONST UINTN StartColumn\r
1272 )\r
1273{\r
1274 BUFFER_LIST *Node;\r
1275 UINTN Index;\r
1276 UINTN LineNumber;\r
1277 UINTN LineCount;\r
1278\r
1279 ShellPrintEx (-1, -1, L"\n");\r
1280 Index = 0;\r
1281 LineNumber = 0;\r
1282 //\r
1283 // go through history list...\r
1284 //\r
1285 for ( Node = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link)\r
1286 ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link)\r
1287 ; Node = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link)\r
1288 ){\r
1289 Index++;\r
1290 LineCount = ((StrLen (Node->Buffer) + StartColumn + 1) / TotalCols) + 1;\r
1291\r
1292 if (LineNumber + LineCount >= TotalRows) {\r
1293 ShellPromptForResponseHii(\r
1294 ShellPromptResponseTypeEnterContinue,\r
1295 STRING_TOKEN (STR_SHELL_ENTER_TO_CONT),\r
1296 ShellInfoObject.HiiHandle,\r
1297 NULL\r
1298 );\r
1299 LineNumber = 0;\r
1300 }\r
1301 ShellPrintEx (-1, -1, L"%2d. %s\n", Index, Node->Buffer);\r
1302 LineNumber += LineCount;\r
1303 }\r
1304}\r
1305\r
1306\r
1307\r
1308\r
1309\r
1310\r
1311//\r
1312// This is identical to EFI_FILE_PROTOCOL except for the additional members\r
1313// for the buffer, size, and position.\r
1314//\r
1315\r
1316typedef struct {\r
1317 UINT64 Revision;\r
1318 EFI_FILE_OPEN Open;\r
1319 EFI_FILE_CLOSE Close;\r
1320 EFI_FILE_DELETE Delete;\r
1321 EFI_FILE_READ Read;\r
1322 EFI_FILE_WRITE Write;\r
1323 EFI_FILE_GET_POSITION GetPosition;\r
1324 EFI_FILE_SET_POSITION SetPosition;\r
1325 EFI_FILE_GET_INFO GetInfo;\r
1326 EFI_FILE_SET_INFO SetInfo;\r
1327 EFI_FILE_FLUSH Flush;\r
1328 VOID *Buffer;\r
1329 UINT64 Position;\r
1330 UINT64 BufferSize;\r
1331 BOOLEAN Unicode;\r
7bcd3ff6 1332 UINT64 FileSize;\r
a405b86d 1333} EFI_FILE_PROTOCOL_MEM;\r
1334\r
1335/**\r
1336 File style interface for Mem (SetPosition).\r
1337 \r
1338 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1339 @param[out] Position The position to set.\r
1340 \r
1341 @retval EFI_SUCCESS The position was successfully changed.\r
1342 @retval EFI_INVALID_PARAMETER The Position was invalid.\r
1343**/\r
1344EFI_STATUS\r
1345EFIAPI\r
1346FileInterfaceMemSetPosition(\r
1347 IN EFI_FILE_PROTOCOL *This,\r
1348 OUT UINT64 Position\r
1349 )\r
1350{\r
7bcd3ff6 1351 if (Position <= ((EFI_FILE_PROTOCOL_MEM*)This)->FileSize) {\r
a405b86d 1352 ((EFI_FILE_PROTOCOL_MEM*)This)->Position = Position;\r
1353 return (EFI_SUCCESS);\r
1354 } else {\r
1355 return (EFI_INVALID_PARAMETER);\r
1356 }\r
1357}\r
1358\r
1359/**\r
1360 File style interface for Mem (GetPosition).\r
1361 \r
1362 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1363 @param[out] Position The pointer to the position.\r
1364 \r
1365 @retval EFI_SUCCESS The position was retrieved.\r
1366**/ \r
1367EFI_STATUS\r
1368EFIAPI\r
1369FileInterfaceMemGetPosition(\r
1370 IN EFI_FILE_PROTOCOL *This,\r
1371 OUT UINT64 *Position\r
1372 )\r
1373{\r
1374 *Position = ((EFI_FILE_PROTOCOL_MEM*)This)->Position;\r
1375 return (EFI_SUCCESS);\r
1376}\r
1377\r
1378/**\r
1379 File style interface for Mem (Write).\r
1380 \r
4ff7e37b
ED
1381 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1382 @param[in, out] BufferSize Size in bytes of Buffer.\r
1383 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1384 \r
ecae5117 1385 @retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources.\r
1386 @retval EFI_SUCCESS The data was written.\r
a405b86d 1387**/\r
1388EFI_STATUS\r
1389EFIAPI\r
1390FileInterfaceMemWrite(\r
1391 IN EFI_FILE_PROTOCOL *This,\r
1392 IN OUT UINTN *BufferSize,\r
1393 IN VOID *Buffer\r
1394 )\r
1395{\r
9ed21946 1396 CHAR8 *AsciiBuffer;\r
1397 EFI_FILE_PROTOCOL_MEM *MemFile;\r
1398\r
1399 MemFile = (EFI_FILE_PROTOCOL_MEM *) This;\r
1400 if (MemFile->Unicode) {\r
a405b86d 1401 //\r
1402 // Unicode\r
1403 //\r
9ed21946 1404 if ((UINTN)(MemFile->Position + (*BufferSize)) > (UINTN)(MemFile->BufferSize)) {\r
c8d9d0e2
JD
1405 MemFile->Buffer = ReallocatePool((UINTN)(MemFile->BufferSize), (UINTN)(MemFile->BufferSize) + (*BufferSize) + MEM_WRITE_REALLOC_OVERHEAD, MemFile->Buffer);\r
1406 MemFile->BufferSize += (*BufferSize) + MEM_WRITE_REALLOC_OVERHEAD;\r
a405b86d 1407 }\r
9ed21946 1408 CopyMem(((UINT8*)MemFile->Buffer) + MemFile->Position, Buffer, *BufferSize);\r
1409 MemFile->Position += (*BufferSize);\r
7bcd3ff6 1410 MemFile->FileSize = MemFile->Position;\r
a405b86d 1411 return (EFI_SUCCESS);\r
1412 } else {\r
1413 //\r
1414 // Ascii\r
1415 //\r
733f138d 1416 AsciiBuffer = AllocateZeroPool(*BufferSize);\r
ecae5117 1417 if (AsciiBuffer == NULL) {\r
1418 return (EFI_OUT_OF_RESOURCES);\r
1419 }\r
a405b86d 1420 AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
9ed21946 1421 if ((UINTN)(MemFile->Position + AsciiStrSize(AsciiBuffer)) > (UINTN)(MemFile->BufferSize)) {\r
c8d9d0e2
JD
1422 MemFile->Buffer = ReallocatePool((UINTN)(MemFile->BufferSize), (UINTN)(MemFile->BufferSize) + AsciiStrSize(AsciiBuffer) + MEM_WRITE_REALLOC_OVERHEAD, MemFile->Buffer);\r
1423 MemFile->BufferSize += AsciiStrSize(AsciiBuffer) + MEM_WRITE_REALLOC_OVERHEAD;\r
a405b86d 1424 }\r
9ed21946 1425 CopyMem(((UINT8*)MemFile->Buffer) + MemFile->Position, AsciiBuffer, AsciiStrSize(AsciiBuffer));\r
1426 MemFile->Position += (*BufferSize / sizeof(CHAR16));\r
7bcd3ff6 1427 MemFile->FileSize = MemFile->Position;\r
a405b86d 1428 FreePool(AsciiBuffer);\r
1429 return (EFI_SUCCESS);\r
1430 }\r
1431}\r
1432\r
1433/**\r
1434 File style interface for Mem (Read).\r
1435 \r
4ff7e37b
ED
1436 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1437 @param[in, out] BufferSize Size in bytes of Buffer.\r
1438 @param[in] Buffer The pointer to the buffer to fill.\r
a405b86d 1439 \r
1440 @retval EFI_SUCCESS The data was read.\r
1441**/\r
1442EFI_STATUS\r
1443EFIAPI\r
1444FileInterfaceMemRead(\r
1445 IN EFI_FILE_PROTOCOL *This,\r
1446 IN OUT UINTN *BufferSize,\r
1447 IN VOID *Buffer\r
1448 )\r
1449{\r
9ed21946 1450 EFI_FILE_PROTOCOL_MEM *MemFile;\r
1451\r
1452 MemFile = (EFI_FILE_PROTOCOL_MEM *) This;\r
7bcd3ff6
JD
1453 if (*BufferSize > (UINTN)((MemFile->FileSize) - (UINTN)(MemFile->Position))) {\r
1454 (*BufferSize) = (UINTN)((MemFile->FileSize) - (UINTN)(MemFile->Position));\r
a405b86d 1455 }\r
9ed21946 1456 CopyMem(Buffer, ((UINT8*)MemFile->Buffer) + MemFile->Position, (*BufferSize));\r
1457 MemFile->Position = MemFile->Position + (*BufferSize);\r
a405b86d 1458 return (EFI_SUCCESS);\r
1459}\r
1460\r
1461/**\r
1462 File style interface for Mem (Close).\r
1463\r
1464 Frees all memory associated with this object.\r
1465 \r
1466 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1467 \r
1468 @retval EFI_SUCCESS The 'file' was closed.\r
1469**/ \r
1470EFI_STATUS\r
1471EFIAPI\r
1472FileInterfaceMemClose(\r
1473 IN EFI_FILE_PROTOCOL *This\r
1474 )\r
1475{\r
1476 SHELL_FREE_NON_NULL(((EFI_FILE_PROTOCOL_MEM*)This)->Buffer);\r
3c865f20 1477 SHELL_FREE_NON_NULL(This);\r
a405b86d 1478 return (EFI_SUCCESS);\r
1479}\r
1480\r
1481/**\r
1482 Creates a EFI_FILE_PROTOCOL (almost) object for using to access\r
1483 a file entirely in memory through file operations.\r
1484\r
1485 @param[in] Unicode Boolean value with TRUE for Unicode and FALSE for Ascii.\r
1486\r
1487 @retval NULL Memory could not be allocated.\r
1488 @return other A pointer to an EFI_FILE_PROTOCOL structure.\r
1489**/\r
1490EFI_FILE_PROTOCOL*\r
1491EFIAPI\r
1492CreateFileInterfaceMem(\r
1493 IN CONST BOOLEAN Unicode\r
1494 )\r
1495{\r
1496 EFI_FILE_PROTOCOL_MEM *FileInterface;\r
1497\r
1498 //\r
1499 // Get some memory\r
1500 //\r
1501 FileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_MEM));\r
1502 if (FileInterface == NULL){\r
1503 return (NULL);\r
1504 }\r
1505\r
1506 //\r
1507 // Assign the generic members\r
1508 //\r
1509 FileInterface->Revision = EFI_FILE_REVISION;\r
1510 FileInterface->Open = FileInterfaceOpenNotFound;\r
1511 FileInterface->Close = FileInterfaceMemClose;\r
1512 FileInterface->GetPosition = FileInterfaceMemGetPosition;\r
1513 FileInterface->SetPosition = FileInterfaceMemSetPosition;\r
1514 FileInterface->GetInfo = FileInterfaceNopGetInfo;\r
1515 FileInterface->SetInfo = FileInterfaceNopSetInfo;\r
1516 FileInterface->Flush = FileInterfaceNopGeneric;\r
1517 FileInterface->Delete = FileInterfaceNopGeneric;\r
1518 FileInterface->Read = FileInterfaceMemRead;\r
1519 FileInterface->Write = FileInterfaceMemWrite;\r
1520 FileInterface->Unicode = Unicode;\r
1521\r
1522 ASSERT(FileInterface->Buffer == NULL);\r
1523 ASSERT(FileInterface->BufferSize == 0);\r
1524 ASSERT(FileInterface->Position == 0);\r
1525\r
9ed21946 1526 if (Unicode) {\r
1527 FileInterface->Buffer = AllocateZeroPool(sizeof(gUnicodeFileTag));\r
9eec4d38
QS
1528 if (FileInterface->Buffer == NULL) {\r
1529 FreePool (FileInterface);\r
1530 return NULL;\r
1531 }\r
9ed21946 1532 *((CHAR16 *) (FileInterface->Buffer)) = EFI_UNICODE_BYTE_ORDER_MARK;\r
1533 FileInterface->BufferSize = 2;\r
1534 FileInterface->Position = 2;\r
1535 }\r
1536\r
a405b86d 1537 return ((EFI_FILE_PROTOCOL *)FileInterface);\r
1538}\r
1539\r
1540typedef struct {\r
1541 UINT64 Revision;\r
1542 EFI_FILE_OPEN Open;\r
1543 EFI_FILE_CLOSE Close;\r
1544 EFI_FILE_DELETE Delete;\r
1545 EFI_FILE_READ Read;\r
1546 EFI_FILE_WRITE Write;\r
1547 EFI_FILE_GET_POSITION GetPosition;\r
1548 EFI_FILE_SET_POSITION SetPosition;\r
1549 EFI_FILE_GET_INFO GetInfo;\r
1550 EFI_FILE_SET_INFO SetInfo;\r
1551 EFI_FILE_FLUSH Flush;\r
1552 BOOLEAN Unicode;\r
1553 EFI_FILE_PROTOCOL *Orig;\r
1554} EFI_FILE_PROTOCOL_FILE;\r
1555\r
733f138d 1556/**\r
1557 Set a files current position\r
1558\r
1559 @param This Protocol instance pointer.\r
1560 @param Position Byte position from the start of the file.\r
1561 \r
1562 @retval EFI_SUCCESS Data was written.\r
1563 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.\r
1564\r
1565**/\r
1566EFI_STATUS\r
1567EFIAPI\r
1568FileInterfaceFileSetPosition(\r
1569 IN EFI_FILE_PROTOCOL *This,\r
1570 IN UINT64 Position\r
1571 )\r
1572{\r
1573 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->SetPosition(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, Position);\r
1574}\r
1575\r
1576/**\r
1577 Get a file's current position\r
1578\r
1579 @param This Protocol instance pointer.\r
1580 @param Position Byte position from the start of the file.\r
1581 \r
1582 @retval EFI_SUCCESS Data was written.\r
1583 @retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open..\r
1584\r
1585**/\r
1586EFI_STATUS\r
1587EFIAPI\r
1588FileInterfaceFileGetPosition(\r
1589 IN EFI_FILE_PROTOCOL *This,\r
1590 OUT UINT64 *Position\r
1591 )\r
1592{\r
1593 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetPosition(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, Position);\r
1594}\r
1595\r
1596/**\r
1597 Get information about a file.\r
1598\r
1599 @param This Protocol instance pointer.\r
1600 @param InformationType Type of information to return in Buffer.\r
1601 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1602 @param Buffer The buffer to return data.\r
1603\r
1604 @retval EFI_SUCCESS Data was returned.\r
1605 @retval EFI_UNSUPPORT InformationType is not supported.\r
1606 @retval EFI_NO_MEDIA The device has no media.\r
1607 @retval EFI_DEVICE_ERROR The device reported an error.\r
1608 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1609 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1610 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1611 @retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.\r
1612\r
1613**/\r
1614EFI_STATUS\r
1615EFIAPI\r
1616FileInterfaceFileGetInfo(\r
1617 IN EFI_FILE_PROTOCOL *This,\r
1618 IN EFI_GUID *InformationType,\r
1619 IN OUT UINTN *BufferSize,\r
1620 OUT VOID *Buffer\r
1621 )\r
1622{\r
1623 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->GetInfo(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, InformationType, BufferSize, Buffer);\r
1624}\r
1625\r
1626/**\r
1627 Set information about a file\r
1628\r
ae724571 1629 @param This Protocol instance pointer.\r
733f138d 1630 @param InformationType Type of information in Buffer.\r
1631 @param BufferSize Size of buffer.\r
1632 @param Buffer The data to write.\r
1633\r
1634 @retval EFI_SUCCESS Data was returned.\r
1635 @retval EFI_UNSUPPORT InformationType is not supported.\r
1636 @retval EFI_NO_MEDIA The device has no media.\r
1637 @retval EFI_DEVICE_ERROR The device reported an error.\r
1638 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1639 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1640 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1641\r
1642**/\r
1643EFI_STATUS\r
1644EFIAPI\r
1645FileInterfaceFileSetInfo(\r
1646 IN EFI_FILE_PROTOCOL *This,\r
1647 IN EFI_GUID *InformationType,\r
1648 IN UINTN BufferSize,\r
1649 IN VOID *Buffer\r
1650 )\r
1651{\r
1652 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->SetInfo(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, InformationType, BufferSize, Buffer);\r
1653}\r
1654\r
1655/**\r
1656 Flush data back for the file handle.\r
1657\r
1658 @param This Protocol instance pointer.\r
1659\r
1660 @retval EFI_SUCCESS Data was written.\r
1661 @retval EFI_UNSUPPORT Writes to Open directory are not supported.\r
1662 @retval EFI_NO_MEDIA The device has no media.\r
1663 @retval EFI_DEVICE_ERROR The device reported an error.\r
1664 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1665 @retval EFI_WRITE_PROTECTED The device is write protected.\r
1666 @retval EFI_ACCESS_DENIED The file was open for read only.\r
1667 @retval EFI_VOLUME_FULL The volume is full.\r
1668\r
1669**/\r
1670EFI_STATUS\r
1671EFIAPI\r
1672FileInterfaceFileFlush(\r
1673 IN EFI_FILE_PROTOCOL *This\r
1674 )\r
1675{\r
1676 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Flush(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
1677}\r
1678\r
1679/**\r
1680 Read data from the file.\r
1681\r
1682 @param This Protocol instance pointer.\r
1683 @param BufferSize On input size of buffer, on output amount of data in buffer.\r
1684 @param Buffer The buffer in which data is read.\r
1685\r
1686 @retval EFI_SUCCESS Data was read.\r
1687 @retval EFI_NO_MEDIA The device has no media.\r
1688 @retval EFI_DEVICE_ERROR The device reported an error.\r
1689 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1690 @retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains required size.\r
1691\r
1692**/\r
1693EFI_STATUS\r
1694EFIAPI\r
1695FileInterfaceFileRead(\r
1696 IN EFI_FILE_PROTOCOL *This,\r
1697 IN OUT UINTN *BufferSize,\r
1698 OUT VOID *Buffer\r
1699 )\r
1700{\r
48cb33ec
QS
1701 CHAR8 *AsciiStrBuffer;\r
1702 CHAR16 *UscStrBuffer;\r
733f138d 1703 UINTN Size;\r
48cb33ec 1704 UINTN CharNum;\r
733f138d 1705 EFI_STATUS Status;\r
1706 if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {\r
1707 //\r
1708 // Unicode\r
1709 //\r
1710 return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));\r
1711 } else {\r
1712 //\r
1713 // Ascii\r
1714 //\r
48cb33ec
QS
1715 Size = (*BufferSize) / sizeof(CHAR16);\r
1716 AsciiStrBuffer = AllocateZeroPool(Size + sizeof(CHAR8));\r
1717 if (AsciiStrBuffer == NULL) {\r
1718 return EFI_OUT_OF_RESOURCES;\r
1719 }\r
1720 UscStrBuffer = AllocateZeroPool(*BufferSize + sizeof(CHAR16));\r
1721 if (UscStrBuffer== NULL) {\r
1722 SHELL_FREE_NON_NULL(AsciiStrBuffer);\r
1723 return EFI_OUT_OF_RESOURCES;\r
1724 }\r
1725 Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Read(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiStrBuffer));\r
1726 if (!EFI_ERROR(Status)) {\r
1727 CharNum = UnicodeSPrint(UscStrBuffer, *BufferSize + sizeof(CHAR16), L"%a", AsciiStrBuffer);\r
1728 if (CharNum == Size) {\r
1729 CopyMem (Buffer, UscStrBuffer, *BufferSize);\r
1730 } else {\r
1731 Status = EFI_UNSUPPORTED;\r
1732 }\r
1733 }\r
1734 SHELL_FREE_NON_NULL(AsciiStrBuffer);\r
1735 SHELL_FREE_NON_NULL(UscStrBuffer);\r
733f138d 1736 return (Status);\r
1737 }\r
1738}\r
1739\r
1740/**\r
1741 Opens a new file relative to the source file's location.\r
1742\r
1743 @param[in] This The protocol instance pointer.\r
1744 @param[out] NewHandle Returns File Handle for FileName.\r
1745 @param[in] FileName Null terminated string. "\", ".", and ".." are supported.\r
1746 @param[in] OpenMode Open mode for file.\r
1747 @param[in] Attributes Only used for EFI_FILE_MODE_CREATE.\r
1748\r
1749 @retval EFI_SUCCESS The device was opened.\r
1750 @retval EFI_NOT_FOUND The specified file could not be found on the device.\r
1751 @retval EFI_NO_MEDIA The device has no media.\r
1752 @retval EFI_MEDIA_CHANGED The media has changed.\r
1753 @retval EFI_DEVICE_ERROR The device reported an error.\r
1754 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1755 @retval EFI_ACCESS_DENIED The service denied access to the file.\r
1756 @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.\r
1757 @retval EFI_VOLUME_FULL The volume is full.\r
1758**/\r
1759EFI_STATUS\r
1760EFIAPI\r
1761FileInterfaceFileOpen (\r
1762 IN EFI_FILE_PROTOCOL *This,\r
1763 OUT EFI_FILE_PROTOCOL **NewHandle,\r
1764 IN CHAR16 *FileName,\r
1765 IN UINT64 OpenMode,\r
1766 IN UINT64 Attributes\r
1767 )\r
1768{\r
1769 return ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Open(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, NewHandle, FileName, OpenMode, Attributes);\r
1770}\r
1771\r
1772/**\r
1773 Close and delete the file handle.\r
1774\r
1775 @param This Protocol instance pointer.\r
1776 \r
1777 @retval EFI_SUCCESS The device was opened.\r
1778 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.\r
1779\r
1780**/\r
1781EFI_STATUS\r
1782EFIAPI\r
1783FileInterfaceFileDelete(\r
1784 IN EFI_FILE_PROTOCOL *This\r
1785 )\r
1786{\r
1787 EFI_STATUS Status;\r
1788 Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Delete(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
1789 FreePool(This);\r
1790 return (Status);\r
1791}\r
1792\r
a405b86d 1793/**\r
1794 File style interface for File (Close).\r
1795 \r
1796 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1797 \r
1798 @retval EFI_SUCCESS The file was closed.\r
1799**/\r
1800EFI_STATUS\r
1801EFIAPI\r
1802FileInterfaceFileClose(\r
1803 IN EFI_FILE_PROTOCOL *This\r
1804 )\r
1805{\r
733f138d 1806 EFI_STATUS Status;\r
1807 Status = ((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Close(((EFI_FILE_PROTOCOL_FILE*)This)->Orig);\r
a405b86d 1808 FreePool(This);\r
733f138d 1809 return (Status);\r
a405b86d 1810}\r
1811\r
1812/**\r
1813 File style interface for File (Write).\r
1814\r
1815 If the file was opened with ASCII mode the data will be processed through \r
1816 AsciiSPrint before writing.\r
1817 \r
4ff7e37b
ED
1818 @param[in] This The pointer to the EFI_FILE_PROTOCOL object.\r
1819 @param[in, out] BufferSize Size in bytes of Buffer.\r
1820 @param[in] Buffer The pointer to the buffer to write.\r
a405b86d 1821 \r
1822 @retval EFI_SUCCESS The data was written.\r
1823**/\r
1824EFI_STATUS\r
1825EFIAPI\r
1826FileInterfaceFileWrite(\r
733f138d 1827 IN EFI_FILE_PROTOCOL *This,\r
1828 IN OUT UINTN *BufferSize,\r
1829 IN VOID *Buffer\r
a405b86d 1830 )\r
1831{\r
1832 CHAR8 *AsciiBuffer;\r
1833 UINTN Size;\r
1834 EFI_STATUS Status;\r
1835 if (((EFI_FILE_PROTOCOL_FILE*)This)->Unicode) {\r
1836 //\r
1837 // Unicode\r
1838 //\r
1839 return (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Write(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, BufferSize, Buffer));\r
1840 } else {\r
1841 //\r
1842 // Ascii\r
1843 //\r
733f138d 1844 AsciiBuffer = AllocateZeroPool(*BufferSize);\r
a405b86d 1845 AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
1846 Size = AsciiStrSize(AsciiBuffer) - 1; // (we dont need the null terminator)\r
1847 Status = (((EFI_FILE_PROTOCOL_FILE*)This)->Orig->Write(((EFI_FILE_PROTOCOL_FILE*)This)->Orig, &Size, AsciiBuffer));\r
1848 FreePool(AsciiBuffer);\r
1849 return (Status);\r
1850 }\r
1851}\r
1852\r
1853/**\r
1854 Create a file interface with unicode information.\r
1855\r
1856 This will create a new EFI_FILE_PROTOCOL identical to the Templace\r
1857 except that the new one has Unicode and Ascii knowledge.\r
1858 \r
1859 @param[in] Template A pointer to the EFI_FILE_PROTOCOL object.\r
1860 @param[in] Unicode TRUE for UCS-2, FALSE for ASCII.\r
1861 \r
1862 @return a new EFI_FILE_PROTOCOL object to be used instead of the template.\r
1863**/\r
1864EFI_FILE_PROTOCOL*\r
1865CreateFileInterfaceFile(\r
1866 IN CONST EFI_FILE_PROTOCOL *Template,\r
1867 IN CONST BOOLEAN Unicode\r
1868 )\r
1869{\r
1870 EFI_FILE_PROTOCOL_FILE *NewOne;\r
1871\r
733f138d 1872 NewOne = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_FILE));\r
3c865f20 1873 if (NewOne == NULL) {\r
1874 return (NULL);\r
1875 }\r
a405b86d 1876 CopyMem(NewOne, Template, sizeof(EFI_FILE_PROTOCOL_FILE));\r
733f138d 1877 NewOne->Orig = (EFI_FILE_PROTOCOL *)Template;\r
1878 NewOne->Unicode = Unicode;\r
1879 NewOne->Open = FileInterfaceFileOpen;\r
1880 NewOne->Close = FileInterfaceFileClose;\r
1881 NewOne->Delete = FileInterfaceFileDelete;\r
1882 NewOne->Read = FileInterfaceFileRead;\r
1883 NewOne->Write = FileInterfaceFileWrite;\r
1884 NewOne->GetPosition = FileInterfaceFileGetPosition;\r
1885 NewOne->SetPosition = FileInterfaceFileSetPosition;\r
1886 NewOne->GetInfo = FileInterfaceFileGetInfo;\r
1887 NewOne->SetInfo = FileInterfaceFileSetInfo;\r
1888 NewOne->Flush = FileInterfaceFileFlush;\r
a405b86d 1889\r
1890 return ((EFI_FILE_PROTOCOL *)NewOne);\r
1891}\r