ConsoleWrappers.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /** @file
  2. Function definitions for shell simple text in and out on top of file handles.
  3. (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>
  4. Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Shell.h"
  8. extern BOOLEAN AsciiRedirection;
  9. typedef struct {
  10. EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleTextIn;
  11. SHELL_FILE_HANDLE FileHandle;
  12. EFI_HANDLE TheHandle;
  13. UINT64 RemainingBytesOfInputFile;
  14. } SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL;
  15. typedef struct {
  16. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL SimpleTextOut;
  17. SHELL_FILE_HANDLE FileHandle;
  18. EFI_HANDLE TheHandle;
  19. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalSimpleTextOut;
  20. } SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL;
  21. /**
  22. Event notification function for EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey event
  23. Signal the event if there is key available
  24. @param Event Indicates the event that invoke this function.
  25. @param Context Indicates the calling context.
  26. **/
  27. VOID
  28. EFIAPI
  29. ConInWaitForKey (
  30. IN EFI_EVENT Event,
  31. IN VOID *Context
  32. )
  33. {
  34. gBS->SignalEvent (Event);
  35. }
  36. /**
  37. Reset function for the fake simple text input.
  38. @param[in] This A pointer to the SimpleTextIn structure.
  39. @param[in] ExtendedVerification TRUE for extra validation, FALSE otherwise.
  40. @retval EFI_SUCCESS The reset was successful.
  41. **/
  42. EFI_STATUS
  43. EFIAPI
  44. FileBasedSimpleTextInReset (
  45. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  46. IN BOOLEAN ExtendedVerification
  47. )
  48. {
  49. return (EFI_SUCCESS);
  50. }
  51. /**
  52. ReadKeyStroke function for the fake simple text input.
  53. @param[in] This A pointer to the SimpleTextIn structure.
  54. @param[in, out] Key A pointer to the Key structure to fill.
  55. @retval EFI_SUCCESS The read was successful.
  56. **/
  57. EFI_STATUS
  58. EFIAPI
  59. FileBasedSimpleTextInReadKeyStroke (
  60. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
  61. IN OUT EFI_INPUT_KEY *Key
  62. )
  63. {
  64. UINTN Size;
  65. UINTN CharSize;
  66. //
  67. // Verify the parameters
  68. //
  69. if ((Key == NULL) || (This == NULL)) {
  70. return (EFI_INVALID_PARAMETER);
  71. }
  72. //
  73. // Check if we have any characters left in the stream.
  74. //
  75. if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile == 0) {
  76. return (EFI_NOT_READY);
  77. }
  78. Size = sizeof (CHAR16);
  79. if (!AsciiRedirection) {
  80. CharSize = sizeof (CHAR16);
  81. } else {
  82. CharSize = sizeof (CHAR8);
  83. }
  84. //
  85. // Decrement the amount of free space by Size or set to zero (for odd length files)
  86. //
  87. if (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile > CharSize) {
  88. ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile -= CharSize;
  89. } else {
  90. ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->RemainingBytesOfInputFile = 0;
  91. }
  92. Key->ScanCode = 0;
  93. return (ShellInfoObject.NewEfiShellProtocol->ReadFile (
  94. ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)This)->FileHandle,
  95. &Size,
  96. &Key->UnicodeChar
  97. ));
  98. }
  99. /**
  100. Function to create a EFI_SIMPLE_TEXT_INPUT_PROTOCOL on top of a
  101. SHELL_FILE_HANDLE to support redirecting input from a file.
  102. @param[in] FileHandleToUse The pointer to the SHELL_FILE_HANDLE to use.
  103. @param[in] HandleLocation The pointer of a location to copy handle with protocol to.
  104. @retval NULL There was insufficient memory available.
  105. @return A pointer to the allocated protocol structure;
  106. **/
  107. EFI_SIMPLE_TEXT_INPUT_PROTOCOL *
  108. CreateSimpleTextInOnFile (
  109. IN SHELL_FILE_HANDLE FileHandleToUse,
  110. IN EFI_HANDLE *HandleLocation
  111. )
  112. {
  113. SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ProtocolToReturn;
  114. EFI_STATUS Status;
  115. UINT64 CurrentPosition;
  116. UINT64 FileSize;
  117. if ((HandleLocation == NULL) || (FileHandleToUse == NULL)) {
  118. return (NULL);
  119. }
  120. ProtocolToReturn = AllocateZeroPool (sizeof (SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL));
  121. if (ProtocolToReturn == NULL) {
  122. return (NULL);
  123. }
  124. ShellGetFileSize (FileHandleToUse, &FileSize);
  125. ShellGetFilePosition (FileHandleToUse, &CurrentPosition);
  126. //
  127. // Initialize the protocol members
  128. //
  129. ProtocolToReturn->RemainingBytesOfInputFile = FileSize - CurrentPosition;
  130. ProtocolToReturn->FileHandle = FileHandleToUse;
  131. ProtocolToReturn->SimpleTextIn.Reset = FileBasedSimpleTextInReset;
  132. ProtocolToReturn->SimpleTextIn.ReadKeyStroke = FileBasedSimpleTextInReadKeyStroke;
  133. Status = gBS->CreateEvent (
  134. EVT_NOTIFY_WAIT,
  135. TPL_NOTIFY,
  136. ConInWaitForKey,
  137. &ProtocolToReturn->SimpleTextIn,
  138. &ProtocolToReturn->SimpleTextIn.WaitForKey
  139. );
  140. if (EFI_ERROR (Status)) {
  141. FreePool (ProtocolToReturn);
  142. return (NULL);
  143. }
  144. /// @todo possibly also install SimpleTextInputEx on the handle at this point.
  145. Status = gBS->InstallProtocolInterface (
  146. &(ProtocolToReturn->TheHandle),
  147. &gEfiSimpleTextInProtocolGuid,
  148. EFI_NATIVE_INTERFACE,
  149. &(ProtocolToReturn->SimpleTextIn)
  150. );
  151. if (!EFI_ERROR (Status)) {
  152. *HandleLocation = ProtocolToReturn->TheHandle;
  153. return ((EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)ProtocolToReturn);
  154. } else {
  155. FreePool (ProtocolToReturn);
  156. return (NULL);
  157. }
  158. }
  159. /**
  160. Function to close a EFI_SIMPLE_TEXT_INPUT_PROTOCOL on top of a
  161. SHELL_FILE_HANDLE to support redirecting input from a file.
  162. @param[in] SimpleTextIn The pointer to the SimpleTextIn to close.
  163. @retval EFI_SUCCESS The object was closed.
  164. **/
  165. EFI_STATUS
  166. CloseSimpleTextInOnFile (
  167. IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *SimpleTextIn
  168. )
  169. {
  170. EFI_STATUS Status;
  171. EFI_STATUS Status1;
  172. if (SimpleTextIn == NULL) {
  173. return (EFI_INVALID_PARAMETER);
  174. }
  175. Status = gBS->CloseEvent (((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)SimpleTextIn)->SimpleTextIn.WaitForKey);
  176. Status1 = gBS->UninstallProtocolInterface (
  177. ((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)SimpleTextIn)->TheHandle,
  178. &gEfiSimpleTextInProtocolGuid,
  179. &(((SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL *)SimpleTextIn)->SimpleTextIn)
  180. );
  181. FreePool (SimpleTextIn);
  182. if (!EFI_ERROR (Status)) {
  183. return (Status1);
  184. } else {
  185. return (Status);
  186. }
  187. }
  188. /**
  189. Reset the text output device hardware and optionally run diagnostics.
  190. @param This pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
  191. @param ExtendedVerification Indicates that a more extensive test may be performed
  192. @retval EFI_SUCCESS The text output device was reset.
  193. **/
  194. EFI_STATUS
  195. EFIAPI
  196. FileBasedSimpleTextOutReset (
  197. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
  198. IN BOOLEAN ExtendedVerification
  199. )
  200. {
  201. return (EFI_SUCCESS);
  202. }
  203. /**
  204. Verifies that all characters in a Unicode string can be output to the
  205. target device.
  206. @param[in] This Protocol instance pointer.
  207. @param[in] WString The NULL-terminated Unicode string to be examined.
  208. @retval EFI_SUCCESS The device(s) are capable of rendering the output string.
  209. **/
  210. EFI_STATUS
  211. EFIAPI
  212. FileBasedSimpleTextOutTestString (
  213. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
  214. IN CHAR16 *WString
  215. )
  216. {
  217. return (EFI_SUCCESS);
  218. }
  219. /**
  220. Returns information for an available text mode that the output device(s)
  221. supports.
  222. @param[in] This Protocol instance pointer.
  223. @param[in] ModeNumber The mode number to return information on.
  224. @param[out] Columns Upon return, the number of columns in the selected geometry
  225. @param[out] Rows Upon return, the number of rows in the selected geometry
  226. @retval EFI_UNSUPPORTED The mode number was not valid.
  227. **/
  228. EFI_STATUS
  229. EFIAPI
  230. FileBasedSimpleTextOutQueryMode (
  231. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
  232. IN UINTN ModeNumber,
  233. OUT UINTN *Columns,
  234. OUT UINTN *Rows
  235. )
  236. {
  237. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *PassThruProtocol;
  238. PassThruProtocol = ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->OriginalSimpleTextOut;
  239. // Pass the QueryMode call thru to the original SimpleTextOutProtocol
  240. return (PassThruProtocol->QueryMode (
  241. PassThruProtocol,
  242. ModeNumber,
  243. Columns,
  244. Rows
  245. ));
  246. }
  247. /**
  248. Sets the output device(s) to a specified mode.
  249. @param[in] This Protocol instance pointer.
  250. @param[in] ModeNumber The mode number to set.
  251. @retval EFI_UNSUPPORTED The mode number was not valid.
  252. **/
  253. EFI_STATUS
  254. EFIAPI
  255. FileBasedSimpleTextOutSetMode (
  256. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
  257. IN UINTN ModeNumber
  258. )
  259. {
  260. return (EFI_UNSUPPORTED);
  261. }
  262. /**
  263. Sets the background and foreground colors for the OutputString () and
  264. ClearScreen () functions.
  265. @param[in] This Protocol instance pointer.
  266. @param[in] Attribute The attribute to set. Bits 0..3 are the foreground color, and
  267. bits 4..6 are the background color. All other bits are undefined
  268. and must be zero. The valid Attributes are defined in this file.
  269. @retval EFI_SUCCESS The attribute was set.
  270. **/
  271. EFI_STATUS
  272. EFIAPI
  273. FileBasedSimpleTextOutSetAttribute (
  274. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
  275. IN UINTN Attribute
  276. )
  277. {
  278. return (EFI_SUCCESS);
  279. }
  280. /**
  281. Clears the output device(s) display to the currently selected background
  282. color.
  283. @param[in] This Protocol instance pointer.
  284. @retval EFI_UNSUPPORTED The output device is not in a valid text mode.
  285. **/
  286. EFI_STATUS
  287. EFIAPI
  288. FileBasedSimpleTextOutClearScreen (
  289. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This
  290. )
  291. {
  292. return (EFI_SUCCESS);
  293. }
  294. /**
  295. Sets the current coordinates of the cursor position
  296. @param[in] This Protocol instance pointer.
  297. @param[in] Column Column to put the cursor in. Must be between zero and Column returned from QueryMode
  298. @param[in] Row Row to put the cursor in. Must be between zero and Row returned from QueryMode
  299. @retval EFI_SUCCESS The operation completed successfully.
  300. **/
  301. EFI_STATUS
  302. EFIAPI
  303. FileBasedSimpleTextOutSetCursorPosition (
  304. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
  305. IN UINTN Column,
  306. IN UINTN Row
  307. )
  308. {
  309. return (EFI_SUCCESS);
  310. }
  311. /**
  312. Makes the cursor visible or invisible
  313. @param[in] This Protocol instance pointer.
  314. @param[in] Visible If TRUE, the cursor is set to be visible. If FALSE, the cursor is
  315. set to be invisible.
  316. @retval EFI_SUCCESS The operation completed successfully.
  317. **/
  318. EFI_STATUS
  319. EFIAPI
  320. FileBasedSimpleTextOutEnableCursor (
  321. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
  322. IN BOOLEAN Visible
  323. )
  324. {
  325. return (EFI_SUCCESS);
  326. }
  327. /**
  328. Write a Unicode string to the output device.
  329. @param[in] This Protocol instance pointer.
  330. @param[in] WString The NULL-terminated Unicode string to be displayed on the output
  331. device(s). All output devices must also support the Unicode
  332. drawing defined in this file.
  333. @retval EFI_SUCCESS The string was output to the device.
  334. @retval EFI_DEVICE_ERROR The device reported an error while attempting to output
  335. the text.
  336. @retval EFI_UNSUPPORTED The output device's mode is not currently in a
  337. defined text mode.
  338. @retval EFI_WARN_UNKNOWN_GLYPH This warning code indicates that some of the
  339. characters in the Unicode string could not be
  340. rendered and were skipped.
  341. **/
  342. EFI_STATUS
  343. EFIAPI
  344. FileBasedSimpleTextOutOutputString (
  345. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
  346. IN CHAR16 *WString
  347. )
  348. {
  349. UINTN Size;
  350. Size = StrLen (WString) * sizeof (CHAR16);
  351. return (ShellInfoObject.NewEfiShellProtocol->WriteFile (
  352. ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)This)->FileHandle,
  353. &Size,
  354. WString
  355. ));
  356. }
  357. /**
  358. Function to create a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a
  359. SHELL_FILE_HANDLE to support redirecting output from a file.
  360. @param[in] FileHandleToUse The pointer to the SHELL_FILE_HANDLE to use.
  361. @param[in] HandleLocation The pointer of a location to copy handle with protocol to.
  362. @param[in] OriginalProtocol The pointer to the original output protocol for pass thru of functions.
  363. @retval NULL There was insufficient memory available.
  364. @return A pointer to the allocated protocol structure;
  365. **/
  366. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *
  367. CreateSimpleTextOutOnFile (
  368. IN SHELL_FILE_HANDLE FileHandleToUse,
  369. IN EFI_HANDLE *HandleLocation,
  370. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *OriginalProtocol
  371. )
  372. {
  373. SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ProtocolToReturn;
  374. EFI_STATUS Status;
  375. if ((HandleLocation == NULL) || (FileHandleToUse == NULL)) {
  376. return (NULL);
  377. }
  378. ProtocolToReturn = AllocateZeroPool (sizeof (SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL));
  379. if (ProtocolToReturn == NULL) {
  380. return (NULL);
  381. }
  382. ProtocolToReturn->FileHandle = FileHandleToUse;
  383. ProtocolToReturn->OriginalSimpleTextOut = OriginalProtocol;
  384. ProtocolToReturn->SimpleTextOut.Reset = FileBasedSimpleTextOutReset;
  385. ProtocolToReturn->SimpleTextOut.TestString = FileBasedSimpleTextOutTestString;
  386. ProtocolToReturn->SimpleTextOut.QueryMode = FileBasedSimpleTextOutQueryMode;
  387. ProtocolToReturn->SimpleTextOut.SetMode = FileBasedSimpleTextOutSetMode;
  388. ProtocolToReturn->SimpleTextOut.SetAttribute = FileBasedSimpleTextOutSetAttribute;
  389. ProtocolToReturn->SimpleTextOut.ClearScreen = FileBasedSimpleTextOutClearScreen;
  390. ProtocolToReturn->SimpleTextOut.SetCursorPosition = FileBasedSimpleTextOutSetCursorPosition;
  391. ProtocolToReturn->SimpleTextOut.EnableCursor = FileBasedSimpleTextOutEnableCursor;
  392. ProtocolToReturn->SimpleTextOut.OutputString = FileBasedSimpleTextOutOutputString;
  393. ProtocolToReturn->SimpleTextOut.Mode = AllocateZeroPool (sizeof (EFI_SIMPLE_TEXT_OUTPUT_MODE));
  394. if (ProtocolToReturn->SimpleTextOut.Mode == NULL) {
  395. FreePool (ProtocolToReturn);
  396. return (NULL);
  397. }
  398. ProtocolToReturn->SimpleTextOut.Mode->MaxMode = OriginalProtocol->Mode->MaxMode;
  399. ProtocolToReturn->SimpleTextOut.Mode->Mode = OriginalProtocol->Mode->Mode;
  400. ProtocolToReturn->SimpleTextOut.Mode->Attribute = OriginalProtocol->Mode->Attribute;
  401. ProtocolToReturn->SimpleTextOut.Mode->CursorColumn = OriginalProtocol->Mode->CursorColumn;
  402. ProtocolToReturn->SimpleTextOut.Mode->CursorRow = OriginalProtocol->Mode->CursorRow;
  403. ProtocolToReturn->SimpleTextOut.Mode->CursorVisible = OriginalProtocol->Mode->CursorVisible;
  404. Status = gBS->InstallProtocolInterface (
  405. &(ProtocolToReturn->TheHandle),
  406. &gEfiSimpleTextOutProtocolGuid,
  407. EFI_NATIVE_INTERFACE,
  408. &(ProtocolToReturn->SimpleTextOut)
  409. );
  410. if (!EFI_ERROR (Status)) {
  411. *HandleLocation = ProtocolToReturn->TheHandle;
  412. return ((EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)ProtocolToReturn);
  413. } else {
  414. SHELL_FREE_NON_NULL (ProtocolToReturn->SimpleTextOut.Mode);
  415. SHELL_FREE_NON_NULL (ProtocolToReturn);
  416. return (NULL);
  417. }
  418. }
  419. /**
  420. Function to close a EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL on top of a
  421. SHELL_FILE_HANDLE to support redirecting output from a file.
  422. @param[in] SimpleTextOut The pointer to the SimpleTextOUT to close.
  423. @retval EFI_SUCCESS The object was closed.
  424. **/
  425. EFI_STATUS
  426. CloseSimpleTextOutOnFile (
  427. IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOut
  428. )
  429. {
  430. EFI_STATUS Status;
  431. if (SimpleTextOut == NULL) {
  432. return (EFI_INVALID_PARAMETER);
  433. }
  434. Status = gBS->UninstallProtocolInterface (
  435. ((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)SimpleTextOut)->TheHandle,
  436. &gEfiSimpleTextOutProtocolGuid,
  437. &(((SHELL_EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)SimpleTextOut)->SimpleTextOut)
  438. );
  439. FreePool (SimpleTextOut->Mode);
  440. FreePool (SimpleTextOut);
  441. return (Status);
  442. }