summaryrefslogtreecommitdiff
path: root/ShellPkg/Application/Shell/Shell.h
blob: 72dde69645b7dcc787db023851ac82a49831c8c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/** @file
  function definitions for internal to shell functions.

  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#ifndef _SHELL_INTERNAL_HEADER_
#define _SHELL_INTERNAL_HEADER_

#include <Uefi.h>
#include <ShellBase.h>

#include <Guid/ShellVariableGuid.h>
#include <Guid/ShellAliasGuid.h>

#include <Protocol/LoadedImage.h>
#include <Protocol/SimpleTextOut.h>
#include <Protocol/EfiShell.h>
#include <Protocol/EfiShellInterface.h>
#include <Protocol/EfiShellEnvironment2.h>
#include <Protocol/EfiShellParameters.h>
#include <Protocol/BlockIo.h>

#include <Library/BaseLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/UefiLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/ShellCommandLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DevicePathLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PcdLib.h>
#include <Library/ShellLib.h>
#include <Library/SortLib.h>
#include <Library/HiiLib.h>
#include <Library/PrintLib.h>
#include <Library/HandleParsingLib.h>
#include <Library/PathLib.h>
#include <Library/FileHandleLib.h>

#include "ShellParametersProtocol.h"
#include "ShellProtocol.h"
#include "ShellEnvVar.h"
#include "ConsoleLogger.h"
#include "ShellManParser.h"
#include "ConsoleWrappers.h"
#include "FileHandleWrappers.h"

typedef struct {
  LIST_ENTRY        Link;           ///< Standard linked list handler.
  SHELL_FILE_HANDLE *SplitStdOut;   ///< ConsoleOut for use in the split.
  SHELL_FILE_HANDLE *SplitStdIn;    ///< ConsoleIn for use in the split.
} SPLIT_LIST;

typedef struct {
  UINT32  Startup:1;      ///< Was "-startup"       found on command line.
  UINT32  NoStartup:1;    ///< Was "-nostartup"     found on command line.
  UINT32  NoConsoleOut:1; ///< Was "-noconsoleout"  found on command line.
  UINT32  NoConsoleIn:1;  ///< Was "-noconsolein"   found on command line.
  UINT32  NoInterrupt:1;  ///< Was "-nointerrupt"   found on command line.
  UINT32  NoMap:1;        ///< Was "-nomap"         found on command line.
  UINT32  NoVersion:1;    ///< Was "-noversion"     found on command line.
  UINT32  Delay:1;        ///< Was "-delay[:n]      found on command line
  UINT32  Exit:1;         ///< Was "-_exit"          found on command line
  UINT32  Reserved:7;     ///< Extra bits
} SHELL_BITS;

typedef union {
  SHELL_BITS  Bits;
  UINT16      AllBits;
} SHELL_BIT_UNION;

typedef struct {
  SHELL_BIT_UNION BitUnion;
  UINTN           Delay;          ///< Seconds of delay default:5.
  CHAR16          *FileName;      ///< Filename to run upon successful initialization.
  CHAR16          *FileOptions;   ///< Options to pass to FileName.
} SHELL_INIT_SETTINGS;

typedef struct {
  BUFFER_LIST                 CommandHistory;
  UINTN                       VisibleRowNumber;
  UINTN                       OriginalVisibleRowNumber;
  BOOLEAN                     InsertMode;           ///< Is the current typing mode insert (FALSE = overwrite).
} SHELL_VIEWING_SETTINGS;

typedef struct {
  EFI_SHELL_PARAMETERS_PROTOCOL *NewShellParametersProtocol;
  EFI_SHELL_PROTOCOL            *NewEfiShellProtocol;
  BOOLEAN                       PageBreakEnabled;
  BOOLEAN                       RootShellInstance;
  SHELL_INIT_SETTINGS           ShellInitSettings;
  BUFFER_LIST                   BufferToFreeList;     ///< List of buffers that were returned to the user to free.
  SHELL_VIEWING_SETTINGS        ViewingSettings;
  EFI_HII_HANDLE                HiiHandle;            ///< Handle from HiiLib.
  UINTN                         LogScreenCount;       ///< How many screens of log information to save.
  EFI_EVENT                     UserBreakTimer;       ///< Timer event for polling for CTRL-C.
  EFI_DEVICE_PATH_PROTOCOL      *ImageDevPath;        ///< DevicePath for ourselves.
  EFI_DEVICE_PATH_PROTOCOL      *FileDevPath;         ///< DevicePath for ourselves.
  CONSOLE_LOGGER_PRIVATE_DATA   *ConsoleInfo;         ///< Pointer for ConsoleInformation.
  EFI_SHELL_PARAMETERS_PROTOCOL *OldShellParameters;  ///< old shell parameters to reinstall upon exiting.
  SHELL_PROTOCOL_HANDLE_LIST    OldShellList;         ///< List of other instances to reinstall when closing.
  SPLIT_LIST                    SplitList;            ///< List of Splits in FILO stack.
  VOID                          *CtrlCNotifyHandle1;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
  VOID                          *CtrlCNotifyHandle2;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
  VOID                          *CtrlCNotifyHandle3;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
  VOID                          *CtrlCNotifyHandle4;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
  VOID                          *CtrlSNotifyHandle1;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
  VOID                          *CtrlSNotifyHandle2;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
  VOID                          *CtrlSNotifyHandle3;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
  VOID                          *CtrlSNotifyHandle4;  ///< The NotifyHandle returned from SimpleTextInputEx.RegisterKeyNotify.
  BOOLEAN                       HaltOutput;           ///< TRUE to start a CTRL-S halt.
} SHELL_INFO;

extern SHELL_INFO ShellInfoObject;

typedef enum {
  Internal_Command,
  Script_File_Name,
  Efi_Application,
  File_Sys_Change,
  Unknown_Invalid
} SHELL_OPERATION_TYPES;

/**
  Converts the command line to it's post-processed form.  this replaces variables and alias' per UEFI Shell spec.

  @param[in,out] CmdLine        pointer to the command line to update

  @retval EFI_SUCCESS           The operation was successful
  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.
  @return                       some other error occured
**/
EFI_STATUS
EFIAPI
ProcessCommandLineToFinal(
  IN OUT CHAR16 **CmdLine
  );

/**
  Function to update the shell variable "lasterror".

  @param[in] ErrorCode      the error code to put into lasterror
**/
EFI_STATUS
EFIAPI
SetLastError(
  IN CONST SHELL_STATUS   ErrorCode
  );

/**
  Sets all the alias' that were registered with the ShellCommandLib library.

  @retval EFI_SUCCESS           all init commands were run sucessfully.
**/
EFI_STATUS
EFIAPI
SetBuiltInAlias(
  VOID
  );

/**
  This function will populate the 2 device path protocol parameters based on the
  global gImageHandle.  the DevPath will point to the device path for the handle that has
  loaded image protocol installed on it.  the FilePath will point to the device path
  for the file that was loaded.

  @param[in, out] DevPath       on a sucessful return the device path to the loaded image
  @param[in, out] FilePath      on a sucessful return the device path to the file

  @retval EFI_SUCCESS           the 2 device paths were sucessfully returned.
  @return other                 a error from gBS->HandleProtocol

  @sa HandleProtocol
**/
EFI_STATUS
EFIAPI
GetDevicePathsForImageAndFile (
  IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,
  IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath
  );

/**
  Process all Uefi Shell 2.0 command line options.

  see Uefi Shell 2.0 section 3.2 for full details.

  the command line should resemble the following:

  shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]

  ShellOpt options  Options which control the initialization behavior of the shell.
                    These options are read from the EFI global variable "ShellOpt"
                    and are processed before options or file-name.

  options           Options which control the initialization behavior of the shell.

  file-name         The name of a UEFI shell application or script to be executed
                    after initialization is complete. By default, if file-name is
                    specified, then -nostartup is implied. Scripts are not supported
                    by level 0.

  file-nameoptions  The command-line options that are passed to file-name when it
                    is invoked.

  This will initialize the ShellInitSettings global variable.

  @retval EFI_SUCCESS           the variable is initialized.
**/
EFI_STATUS
EFIAPI
ProcessCommandLine(
  VOID
  );

/**
  Handles all interaction with the default startup script.

  this will check that the correct command line parameters were passed, handle the delay, and then start running the script.

  @param[in] ImagePath          The path to the image for shell.  The first place to look for the startup script.
  @param[in] FilePath           The path to the file for shell.  The second place to look for the startup script.
  @param[out] ExitStatus        The exit code of the script. Ignored if NULL.
                                Invalid when this function returns an error.

  @retval EFI_SUCCESS           The variable is initialized.
**/
EFI_STATUS
EFIAPI
DoStartupScript(
  IN  EFI_DEVICE_PATH_PROTOCOL *ImagePath,
  IN  EFI_DEVICE_PATH_PROTOCOL *FilePath,
  OUT SHELL_STATUS             *ExitStatus
  );

/**
  Function to perform the shell prompt looping.  It will do a single prompt,
  dispatch the result, and then return.  It is expected that the caller will
  call this function in a loop many times.

  @retval EFI_SUCCESS
  @retval RETURN_ABORTED
**/
EFI_STATUS
EFIAPI
DoShellPrompt (
  VOID
  );

/**
  Add a buffer to the Buffer To Free List for safely returning buffers to other
  places without risking letting them modify internal shell information.

  @param Buffer   Something to pass to FreePool when the shell is exiting.
**/
VOID*
EFIAPI
AddBufferToFreeList(
  VOID *Buffer
  );

/**
  Add a buffer to the Command History List.

  @param Buffer[in]     The line buffer to add.
**/
VOID
EFIAPI
AddLineToCommandHistory(
  IN CONST CHAR16 *Buffer
  );

/**
  Function will process and run a command line.

  This will determine if the command line represents an internal shell command or dispatch an external application.

  @param[in]  CmdLine     the command line to parse
  @param[out] ExitStatus  The exit status of the command. Ignored if NULL.

  @retval EFI_SUCCESS     the command was completed
  @retval EFI_ABORTED     the command's operation was aborted
**/
EFI_STATUS
EFIAPI
RunCommand(
  IN  CONST CHAR16         *CmdLine,
  OUT       SHELL_STATUS   *ExitStatus
  );

/**
  Function determins if the CommandName COULD be a valid command.  It does not determine whether
  this is a valid command.  It only checks for invalid characters.

  @param[in] CommandName    The name to check

  @retval TRUE              CommandName could be a command name
  @retval FALSE             CommandName could not be a valid command name
**/
BOOLEAN
EFIAPI
IsValidCommandName(
  IN CONST CHAR16     *CommandName
  );

/**
  Function to process a NSH script file via SHELL_FILE_HANDLE.

  @param[in] Handle             The handle to the already opened file.
  @param[in] Name               The name of the script file.

  @param[out] ExitStatus      The exit code of the script. Ignored if NULL.
                              Invalid when this function returns an error.

  @retval EFI_SUCCESS           the script completed sucessfully
**/
EFI_STATUS
EFIAPI
RunScriptFileHandle (
  IN  SHELL_FILE_HANDLE  Handle,
  IN  CONST CHAR16       *Name,
  OUT SHELL_STATUS       *ExitStatus
  );

/**
  Function to process a NSH script file.

  @param[in] ScriptPath         Pointer to the script file name (including file system path).
  @param[in] Handle             the handle of the script file already opened.
  @param[in] CmdLine            the command line to run.
  @param[in] ParamProtocol      the shell parameters protocol pointer

  @param[out] ExitStatus      The exit code of the script. Ignored if NULL.
                              Invalid when this function returns an error.

  @retval EFI_SUCCESS           the script completed sucessfully
**/
EFI_STATUS
EFIAPI
RunScriptFile (
  IN  CONST CHAR16                   *ScriptPath,
  IN  SHELL_FILE_HANDLE              Handle OPTIONAL,
  IN  CONST CHAR16                   *CmdLine,
  IN  EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol,
  OUT SHELL_STATUS                   *ExitStatus
  );

#endif //_SHELL_INTERNAL_HEADER_