Shell.h 13 KB

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