1 /** @file
2   function definitions for internal to shell functions.
3 
4   (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
5   Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #ifndef _SHELL_INTERNAL_HEADER_
17 #define _SHELL_INTERNAL_HEADER_
18 
19 #include <Uefi.h>
20 #include <ShellBase.h>
21 
22 #include <Guid/ShellVariableGuid.h>
23 #include <Guid/ShellAliasGuid.h>
24 
25 #include <Protocol/LoadedImage.h>
26 #include <Protocol/SimpleTextOut.h>
27 #include <Protocol/EfiShell.h>
28 #include <Protocol/EfiShellInterface.h>
29 #include <Protocol/EfiShellEnvironment2.h>
30 #include <Protocol/EfiShellParameters.h>
31 #include <Protocol/BlockIo.h>
32 
33 #include <Library/BaseLib.h>
34 #include <Library/UefiApplicationEntryPoint.h>
35 #include <Library/UefiLib.h>
36 #include <Library/DebugLib.h>
37 #include <Library/MemoryAllocationLib.h>
38 #include <Library/ShellCommandLib.h>
39 #include <Library/UefiRuntimeServicesTableLib.h>
40 #include <Library/UefiBootServicesTableLib.h>
41 #include <Library/DevicePathLib.h>
42 #include <Library/BaseMemoryLib.h>
43 #include <Library/PcdLib.h>
44 #include <Library/ShellLib.h>
45 #include <Library/SortLib.h>
46 #include <Library/HiiLib.h>
47 #include <Library/PrintLib.h>
48 #include <Library/HandleParsingLib.h>
49 #include <Library/FileHandleLib.h>
50 
51 #include "ShellParametersProtocol.h"
52 #include "ShellProtocol.h"
53 #include "ShellEnvVar.h"
54 #include "ConsoleLogger.h"
55 #include "ShellManParser.h"
56 #include "ConsoleWrappers.h"
57 #include "FileHandleWrappers.h"
58 
59 typedef struct {
60   LIST_ENTRY        Link;           ///< Standard linked list handler.
61   SHELL_FILE_HANDLE *SplitStdOut;   ///< ConsoleOut for use in the split.
62   SHELL_FILE_HANDLE *SplitStdIn;    ///< ConsoleIn for use in the split.
63 } SPLIT_LIST;
64 
65 typedef struct {
66   UINT32  Startup:1;      ///< Was "-startup"       found on command line.
67   UINT32  NoStartup:1;    ///< Was "-nostartup"     found on command line.
68   UINT32  NoConsoleOut:1; ///< Was "-noconsoleout"  found on command line.
69   UINT32  NoConsoleIn:1;  ///< Was "-noconsolein"   found on command line.
70   UINT32  NoInterrupt:1;  ///< Was "-nointerrupt"   found on command line.
71   UINT32  NoMap:1;        ///< Was "-nomap"         found on command line.
72   UINT32  NoVersion:1;    ///< Was "-noversion"     found on command line.
73   UINT32  Delay:1;        ///< Was "-delay[:n]      found on command line
74   UINT32  Exit:1;         ///< Was "-_exit"          found on command line
75   UINT32  Reserved:7;     ///< Extra bits
76 } SHELL_BITS;
77 
78 typedef union {
79   SHELL_BITS  Bits;
80   UINT16      AllBits;
81 } SHELL_BIT_UNION;
82 
83 typedef struct {
84   SHELL_BIT_UNION BitUnion;
85   UINTN           Delay;          ///< Seconds of delay default:5.
86   CHAR16          *FileName;      ///< Filename to run upon successful initialization.
87   CHAR16          *FileOptions;   ///< Options to pass to FileName.
88 } SHELL_INIT_SETTINGS;
89 
90 typedef struct {
91   BUFFER_LIST                 CommandHistory;
92   UINTN                       VisibleRowNumber;
93   UINTN                       OriginalVisibleRowNumber;
94   BOOLEAN                     InsertMode;           ///< Is the current typing mode insert (FALSE = overwrite).
95 } SHELL_VIEWING_SETTINGS;
96 
97 typedef struct {
98   EFI_SHELL_PARAMETERS_PROTOCOL *NewShellParametersProtocol;
99   EFI_SHELL_PROTOCOL            *NewEfiShellProtocol;
100   BOOLEAN                       PageBreakEnabled;
101   BOOLEAN                       RootShellInstance;
102   SHELL_INIT_SETTINGS           ShellInitSettings;
103   BUFFER_LIST                   BufferToFreeList;     ///< List of buffers that were returned to the user to free.
104   SHELL_VIEWING_SETTINGS        ViewingSettings;
105   EFI_HII_HANDLE                HiiHandle;            ///< Handle from HiiLib.
106   UINTN                         LogScreenCount;       ///< How many screens of log information to save.
107   EFI_EVENT                     UserBreakTimer;       ///< Timer event for polling for CTRL-C.
108   EFI_DEVICE_PATH_PROTOCOL      *ImageDevPath;        ///< DevicePath for ourselves.
109   EFI_DEVICE_PATH_PROTOCOL      *FileDevPath;         ///< DevicePath for ourselves.
110   CONSOLE_LOGGER_PRIVATE_DATA   *ConsoleInfo;         ///< Pointer for ConsoleInformation.
111   EFI_SHELL_PARAMETERS_PROTOCOL *OldShellParameters;  ///< old shell parameters to reinstall upon exiting.
112   SHELL_PROTOCOL_HANDLE_LIST    OldShellList;         ///< List of other instances to reinstall when closing.
113   SPLIT_LIST                    SplitList;            ///< List of Splits in FILO stack.
114   VOID                          *CtrlCNotifyHandle1;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
115   VOID                          *CtrlCNotifyHandle2;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
116   VOID                          *CtrlCNotifyHandle3;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
117   VOID                          *CtrlCNotifyHandle4;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
118   VOID                          *CtrlSNotifyHandle1;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
119   VOID                          *CtrlSNotifyHandle2;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
120   VOID                          *CtrlSNotifyHandle3;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
121   VOID                          *CtrlSNotifyHandle4;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
122   BOOLEAN                       HaltOutput;           ///< TRUE to start a CTRL-S halt.
123 } SHELL_INFO;
124 
125 extern SHELL_INFO ShellInfoObject;
126 
127 /**
128   Converts the command line to it's post-processed form.  this replaces variables and alias' per UEFI Shell spec.
129 
130   @param[in,out] CmdLine        pointer to the command line to update
131 
132   @retval EFI_SUCCESS           The operation was successful
133   @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.
134   @return                       some other error occurred
135 **/
136 EFI_STATUS
137 EFIAPI
138 ProcessCommandLineToFinal(
139   IN OUT CHAR16 **CmdLine
140   );
141 
142 /**
143   Function to update the shell variable "lasterror".
144 
145   @param[in] ErrorCode      the error code to put into lasterror
146 **/
147 EFI_STATUS
148 EFIAPI
149 SetLastError(
150   IN CONST SHELL_STATUS   ErrorCode
151   );
152 
153 /**
154   Sets all the alias' that were registered with the ShellCommandLib library.
155 
156   @retval EFI_SUCCESS           all init commands were run successfully.
157 **/
158 EFI_STATUS
159 EFIAPI
160 SetBuiltInAlias(
161   VOID
162   );
163 
164 /**
165   This function will populate the 2 device path protocol parameters based on the
166   global gImageHandle.  the DevPath will point to the device path for the handle that has
167   loaded image protocol installed on it.  the FilePath will point to the device path
168   for the file that was loaded.
169 
170   @param[in, out] DevPath       on a successful return the device path to the loaded image
171   @param[in, out] FilePath      on a successful return the device path to the file
172 
173   @retval EFI_SUCCESS           the 2 device paths were successfully returned.
174   @return other                 a error from gBS->HandleProtocol
175 
176   @sa HandleProtocol
177 **/
178 EFI_STATUS
179 EFIAPI
180 GetDevicePathsForImageAndFile (
181   IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,
182   IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath
183   );
184 
185 /**
186   Process all Uefi Shell 2.0 command line options.
187 
188   see Uefi Shell 2.0 section 3.2 for full details.
189 
190   the command line should resemble the following:
191 
192   shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
193 
194   ShellOpt options  Options which control the initialization behavior of the shell.
195                     These options are read from the EFI global variable "ShellOpt"
196                     and are processed before options or file-name.
197 
198   options           Options which control the initialization behavior of the shell.
199 
200   file-name         The name of a UEFI shell application or script to be executed
201                     after initialization is complete. By default, if file-name is
202                     specified, then -nostartup is implied. Scripts are not supported
203                     by level 0.
204 
205   file-nameoptions  The command-line options that are passed to file-name when it
206                     is invoked.
207 
208   This will initialize the ShellInitSettings global variable.
209 
210   @retval EFI_SUCCESS           the variable is initialized.
211 **/
212 EFI_STATUS
213 EFIAPI
214 ProcessCommandLine(
215   VOID
216   );
217 
218 /**
219   Handles all interaction with the default startup script.
220 
221   this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
222 
223   @param[in] ImagePath          The path to the image for shell.  The first place to look for the startup script.
224   @param[in] FilePath           The path to the file for shell.  The second place to look for the startup script.
225 
226   @retval EFI_SUCCESS           The variable is initialized.
227 **/
228 EFI_STATUS
229 EFIAPI
230 DoStartupScript(
231   IN EFI_DEVICE_PATH_PROTOCOL *ImagePath,
232   IN EFI_DEVICE_PATH_PROTOCOL *FilePath
233   );
234 
235 /**
236   Function to perform the shell prompt looping.  It will do a single prompt,
237   dispatch the result, and then return.  It is expected that the caller will
238   call this function in a loop many times.
239 
240   @retval EFI_SUCCESS
241   @retval RETURN_ABORTED
242 **/
243 EFI_STATUS
244 EFIAPI
245 DoShellPrompt (
246   VOID
247   );
248 
249 /**
250   Add a buffer to the Buffer To Free List for safely returning buffers to other
251   places without risking letting them modify internal shell information.
252 
253   @param Buffer   Something to pass to FreePool when the shell is exiting.
254 **/
255 VOID*
256 EFIAPI
257 AddBufferToFreeList(
258   VOID *Buffer
259   );
260 
261 /**
262   Add a buffer to the Command History List.
263 
264   @param Buffer[in]     The line buffer to add.
265 **/
266 VOID
267 EFIAPI
268 AddLineToCommandHistory(
269   IN CONST CHAR16 *Buffer
270   );
271 
272 /**
273   Function will process and run a command line.
274 
275   This will determine if the command line represents an internal shell command or dispatch an external application.
276 
277   @param[in] CmdLine  the command line to parse
278 
279   @retval EFI_SUCCESS     the command was completed
280   @retval EFI_ABORTED     the command's operation was aborted
281 **/
282 EFI_STATUS
283 EFIAPI
284 RunCommand(
285   IN CONST CHAR16   *CmdLine
286   );
287 
288 /**
289   Function will process and run a command line.
290 
291   This will determine if the command line represents an internal shell
292   command or dispatch an external application.
293 
294   @param[in] CmdLine      The command line to parse.
295   @param[out] CommandStatus   The status from the command line.
296 
297   @retval EFI_SUCCESS     The command was completed.
298   @retval EFI_ABORTED     The command's operation was aborted.
299 **/
300 EFI_STATUS
301 EFIAPI
302 RunShellCommand(
303   IN CONST CHAR16   *CmdLine,
304   OUT EFI_STATUS    *CommandStatus
305   );
306 
307 /**
308   Function determines if the CommandName COULD be a valid command.  It does not determine whether
309   this is a valid command.  It only checks for invalid characters.
310 
311   @param[in] CommandName    The name to check
312 
313   @retval TRUE              CommandName could be a command name
314   @retval FALSE             CommandName could not be a valid command name
315 **/
316 BOOLEAN
317 EFIAPI
318 IsValidCommandName(
319   IN CONST CHAR16     *CommandName
320   );
321 
322 /**
323   Function to process a NSH script file via SHELL_FILE_HANDLE.
324 
325   @param[in] Handle             The handle to the already opened file.
326   @param[in] Name               The name of the script file.
327 
328   @retval EFI_SUCCESS           the script completed successfully
329 **/
330 EFI_STATUS
331 EFIAPI
332 RunScriptFileHandle (
333   IN SHELL_FILE_HANDLE  Handle,
334   IN CONST CHAR16       *Name
335   );
336 
337 /**
338   Function to process a NSH script file.
339 
340   @param[in] ScriptPath         Pointer to the script file name (including file system path).
341   @param[in] Handle             the handle of the script file already opened.
342   @param[in] CmdLine            the command line to run.
343   @param[in] ParamProtocol      the shell parameters protocol pointer
344 
345   @retval EFI_SUCCESS           the script completed successfully
346 **/
347 EFI_STATUS
348 EFIAPI
349 RunScriptFile (
350   IN CONST CHAR16                   *ScriptPath,
351   IN SHELL_FILE_HANDLE              Handle OPTIONAL,
352   IN CONST CHAR16                   *CmdLine,
353   IN EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol
354   );
355 
356 /**
357   Return the pointer to the first occurrence of any character from a list of characters.
358 
359   @param[in] String           the string to parse
360   @param[in] CharacterList    the list of character to look for
361   @param[in] EscapeCharacter  An escape character to skip
362 
363   @return the location of the first character in the string
364   @retval CHAR_NULL no instance of any character in CharacterList was found in String
365 **/
366 CONST CHAR16*
367 EFIAPI
368 FindFirstCharacter(
369   IN CONST CHAR16 *String,
370   IN CONST CHAR16 *CharacterList,
371   IN CONST CHAR16 EscapeCharacter
372   );
373 
374 /**
375   Cleans off leading and trailing spaces and tabs.
376 
377   @param[in] String pointer to the string to trim them off.
378 **/
379 EFI_STATUS
380 EFIAPI
381 TrimSpaces(
382   IN CHAR16 **String
383   );
384 
385 /**
386 
387   Create a new buffer list and stores the old one to OldBufferList
388 
389   @param OldBufferList   The temporary list head used to store the nodes in BufferToFreeList.
390 **/
391 VOID
392 SaveBufferList (
393   OUT LIST_ENTRY     *OldBufferList
394   );
395 
396 /**
397   Restore previous nodes into BufferToFreeList .
398 
399   @param OldBufferList   The temporary list head used to store the nodes in BufferToFreeList.
400 **/
401 VOID
402 RestoreBufferList (
403   IN OUT LIST_ENTRY     *OldBufferList
404   );
405 
406 
407 
408 #endif //_SHELL_INTERNAL_HEADER_
409 
410