]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.c
ShellPkg/[hex]edit: use SimpleTextInEx to read console
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / UefiShellDebug1CommandsLib.c
CommitLineData
5d73d92f 1/** @file\r
2 Main file for NULL named library for debug1 profile shell command functions.\r
3\r
7f6511ee 4 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
5d73d92f 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "UefiShellDebug1CommandsLib.h"\r
fba9b6ba 16#include <Library/BcfgCommandLib.h>\r
5d73d92f 17\r
18STATIC CONST CHAR16 mFileName[] = L"Debug1Commands";\r
19EFI_HANDLE gShellDebug1HiiHandle = NULL;\r
5d73d92f 20\r
3737ac2b 21/**\r
22 Gets the debug file name. This will be used if HII is not working.\r
23\r
24 @retval NULL No file is available.\r
25 @return The NULL-terminated filename to get help from.\r
26**/\r
5d73d92f 27CONST CHAR16*\r
28EFIAPI\r
29ShellCommandGetManFileNameDebug1 (\r
30 VOID\r
31 )\r
32{\r
33 return (mFileName);\r
34}\r
35\r
36/**\r
37 Constructor for the Shell Debug1 Commands library.\r
38\r
39 @param ImageHandle the image handle of the process\r
40 @param SystemTable the EFI System Table pointer\r
41\r
42 @retval EFI_SUCCESS the shell command handlers were installed sucessfully\r
43 @retval EFI_UNSUPPORTED the shell level required was not found.\r
44**/\r
45EFI_STATUS\r
46EFIAPI\r
47UefiShellDebug1CommandsLibConstructor (\r
48 IN EFI_HANDLE ImageHandle,\r
49 IN EFI_SYSTEM_TABLE *SystemTable\r
50 )\r
51{\r
52 //\r
53 // check our bit of the profiles mask\r
54 //\r
55 if ((PcdGet8(PcdShellProfileMask) & BIT1) == 0) {\r
82571fb5 56 return (EFI_SUCCESS);\r
5d73d92f 57 }\r
58\r
59 //\r
60 // install the HII stuff.\r
61 //\r
62 gShellDebug1HiiHandle = HiiAddPackages (&gShellDebug1HiiGuid, gImageHandle, UefiShellDebug1CommandsLibStrings, NULL);\r
63 if (gShellDebug1HiiHandle == NULL) {\r
64 return (EFI_DEVICE_ERROR);\r
65 }\r
66\r
67 //\r
68 // install our shell command handlers that are always installed\r
69 //\r
3737ac2b 70 ShellCommandRegisterCommandName(L"setsize", ShellCommandRunSetSize , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETSIZE) );\r
5d73d92f 71 ShellCommandRegisterCommandName(L"comp", ShellCommandRunComp , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_COMP) );\r
72 ShellCommandRegisterCommandName(L"mode", ShellCommandRunMode , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MODE) );\r
73 ShellCommandRegisterCommandName(L"memmap", ShellCommandRunMemMap , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MEMMAP) );\r
74 ShellCommandRegisterCommandName(L"eficompress", ShellCommandRunEfiCompress , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EFICOMPRESS) );\r
75 ShellCommandRegisterCommandName(L"efidecompress", ShellCommandRunEfiDecompress , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EFIDCOMPRESS) );\r
76 ShellCommandRegisterCommandName(L"dmem", ShellCommandRunDmem , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DMEM) );\r
3737ac2b 77 ShellCommandRegisterCommandName(L"loadpcirom", ShellCommandRunLoadPciRom , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_LOAD_PCI_ROM) );\r
5d73d92f 78 ShellCommandRegisterCommandName(L"mm", ShellCommandRunMm , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MM) );\r
3737ac2b 79 ShellCommandRegisterCommandName(L"setvar", ShellCommandRunSetVar , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETVAR) );\r
80 ShellCommandRegisterCommandName(L"sermode", ShellCommandRunSerMode , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SERMODE) );\r
81 ShellCommandRegisterCommandName(L"pci", ShellCommandRunPci , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_PCI) );\r
5d73d92f 82 ShellCommandRegisterCommandName(L"smbiosview", ShellCommandRunSmbiosView , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SMBIOSVIEW) );\r
83 ShellCommandRegisterCommandName(L"dmpstore", ShellCommandRunDmpStore , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DMPSTORE) );\r
84 ShellCommandRegisterCommandName(L"dblk", ShellCommandRunDblk , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DBLK) );\r
3737ac2b 85 ShellCommandRegisterCommandName(L"edit", ShellCommandRunEdit , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EDIT) );\r
86 ShellCommandRegisterCommandName(L"hexedit", ShellCommandRunHexEdit , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_HEXEDIT) );\r
5d73d92f 87\r
5d73d92f 88 ShellCommandRegisterAlias(L"dmem", L"mem");\r
89\r
fba9b6ba
JC
90 BcfgLibraryRegisterBcfgCommand(ImageHandle, SystemTable, L"Debug1");\r
91\r
5d73d92f 92 return (EFI_SUCCESS);\r
93}\r
94\r
95/**\r
96 Destructor for the library. free any resources.\r
3737ac2b 97\r
98 @param ImageHandle The image handle of the process.\r
99 @param SystemTable The EFI System Table pointer.\r
5d73d92f 100**/\r
101EFI_STATUS\r
102EFIAPI\r
103UefiShellDebug1CommandsLibDestructor (\r
104 IN EFI_HANDLE ImageHandle,\r
105 IN EFI_SYSTEM_TABLE *SystemTable\r
106 )\r
107{\r
108 if (gShellDebug1HiiHandle != NULL) {\r
109 HiiRemovePackages(gShellDebug1HiiHandle);\r
110 }\r
fba9b6ba
JC
111\r
112 BcfgLibraryUnregisterBcfgCommand(ImageHandle, SystemTable);\r
5d73d92f 113 return (EFI_SUCCESS);\r
114}\r
115\r
5d73d92f 116/**\r
117 Convert a Unicode character to upper case only if\r
118 it maps to a valid small-case ASCII character.\r
119\r
120 This internal function only deal with Unicode character\r
121 which maps to a valid small-case ASCII character, i.e.\r
122 L'a' to L'z'. For other Unicode character, the input character\r
123 is returned directly.\r
124\r
125 @param Char The character to convert.\r
126\r
127 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
128 @retval Unchanged Otherwise.\r
129\r
130\r
131 //Stolen from MdePkg Baselib\r
132**/\r
133CHAR16\r
5d73d92f 134CharToUpper (\r
135 IN CHAR16 Char\r
136 )\r
137{\r
138 if (Char >= L'a' && Char <= L'z') {\r
139 return (CHAR16) (Char - (L'a' - L'A'));\r
140 }\r
141\r
142 return Char;\r
143}\r
144\r
145/**\r
146 Function returns a system configuration table that is stored in the\r
147 EFI System Table based on the provided GUID.\r
148\r
4ff7e37b
ED
149 @param[in] TableGuid A pointer to the table's GUID type.\r
150 @param[in, out] Table On exit, a pointer to a system configuration table.\r
5d73d92f 151\r
152 @retval EFI_SUCCESS A configuration table matching TableGuid was found.\r
153 @retval EFI_NOT_FOUND A configuration table matching TableGuid was not found.\r
154**/\r
155EFI_STATUS\r
5d73d92f 156GetSystemConfigurationTable (\r
157 IN EFI_GUID *TableGuid,\r
158 IN OUT VOID **Table\r
159 )\r
160{\r
161 UINTN Index;\r
162 ASSERT (Table != NULL);\r
163\r
164 for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {\r
3737ac2b 165 if (CompareGuid (TableGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
5d73d92f 166 *Table = gST->ConfigurationTable[Index].VendorTable;\r
167 return EFI_SUCCESS;\r
168 }\r
169 }\r
170\r
171 return EFI_NOT_FOUND;\r
172}\r
173\r
3737ac2b 174/**\r
175 Clear the line at the specified Row.\r
176 \r
177 @param[in] Row The row number to be cleared ( start from 1 )\r
178 @param[in] LastCol The last printable column.\r
179 @param[in] LastRow The last printable row.\r
180**/\r
181VOID\r
3737ac2b 182EditorClearLine (\r
183 IN UINTN Row,\r
184 IN UINTN LastCol,\r
185 IN UINTN LastRow\r
186 )\r
187{\r
1a421c2c 188 UINTN Col;\r
3737ac2b 189 CHAR16 Line[200];\r
190\r
191 if (Row == 0) {\r
192 Row = 1;\r
193 }\r
194\r
195 //\r
196 // prepare a blank line\r
1a421c2c 197 // If max column is larger, split to multiple prints.\r
3737ac2b 198 //\r
1a421c2c
RN
199 SetMem16 (Line, sizeof (Line), L' ');\r
200 Line[ARRAY_SIZE (Line) - 1] = CHAR_NULL;\r
201\r
202 for (Col = 1; Col <= LastCol; Col += ARRAY_SIZE (Line) - 1) {\r
203 if (Col + ARRAY_SIZE (Line) - 1 > LastCol) {\r
204 if (Row == LastRow) {\r
205 //\r
206 // if CHAR_NULL is still at position LastCol, it will cause first line error\r
207 //\r
452676ff 208 Line[(LastCol - 1) % (ARRAY_SIZE (Line) - 1)] = CHAR_NULL;\r
1a421c2c
RN
209 } else {\r
210 Line[LastCol % (ARRAY_SIZE (Line) - 1)] = CHAR_NULL;\r
211 }\r
212 }\r
213 \r
3737ac2b 214 //\r
1a421c2c 215 // print out the blank line\r
3737ac2b 216 //\r
1a421c2c 217 ShellPrintEx ((INT32) Col - 1, (INT32) Row - 1, Line);\r
3737ac2b 218 }\r
3737ac2b 219}\r
220\r
221/**\r
222 Determine if the character is valid for a filename.\r
223\r
224 @param[in] Ch The character to test.\r
225\r
226 @retval TRUE The character is valid.\r
227 @retval FALSE The character is not valid.\r
228**/\r
229BOOLEAN\r
3737ac2b 230IsValidFileNameChar (\r
231 IN CONST CHAR16 Ch\r
232 )\r
233{\r
234 //\r
235 // See if there are any illegal characters within the name\r
236 //\r
d16efcae 237 if (Ch < 0x20 || Ch == L'\"' || Ch == L'*' || Ch == L'/' || Ch == L'<' || Ch == L'>' || Ch == L'?' || Ch == L'|') {\r
3737ac2b 238 return FALSE;\r
239 }\r
240\r
241 return TRUE;\r
242}\r
243\r
244/**\r
245 Check if file name has illegal characters.\r
246 \r
247 @param Name The filename to check.\r
248\r
249 @retval TRUE The filename is ok.\r
250 @retval FALSE The filename is not ok.\r
251**/\r
252BOOLEAN\r
3737ac2b 253IsValidFileName (\r
254 IN CONST CHAR16 *Name\r
255 )\r
256{\r
257\r
258 UINTN Index;\r
259 UINTN Len;\r
260\r
261 //\r
262 // check the length of Name\r
263 //\r
264 for (Len = 0, Index = StrLen (Name) - 1; Index + 1 != 0; Index--, Len++) {\r
265 if (Name[Index] == '\\' || Name[Index] == ':') {\r
266 break;\r
267 }\r
268 }\r
269\r
270 if (Len == 0 || Len > 255) {\r
271 return FALSE;\r
272 }\r
273 //\r
274 // check whether any char in Name not appears in valid file name char\r
275 //\r
276 for (Index = 0; Index < StrLen (Name); Index++) {\r
277 if (!IsValidFileNameChar (Name[Index])) {\r
278 return FALSE;\r
279 }\r
5d73d92f 280 }\r
3737ac2b 281\r
282 return TRUE;\r
283}\r
284\r
285/**\r
286 Find a filename that is valid (not taken) with the given extension.\r
287\r
288 @param[in] Extension The file extension.\r
289\r
290 @retval NULL Something went wrong.\r
291 @return the valid filename.\r
292**/\r
293CHAR16 *\r
3737ac2b 294EditGetDefaultFileName (\r
295 IN CONST CHAR16 *Extension\r
296 )\r
297{\r
298 EFI_STATUS Status;\r
299 UINTN Suffix;\r
3737ac2b 300 CHAR16 *FileNameTmp;\r
301\r
302 Suffix = 0;\r
3737ac2b 303\r
304 do {\r
305 FileNameTmp = CatSPrint (NULL, L"NewFile%d.%s", Suffix, Extension);\r
306\r
307 //\r
308 // after that filename changed to path\r
309 //\r
310 Status = ShellFileExists (FileNameTmp);\r
311\r
312 if (Status == EFI_NOT_FOUND) {\r
313 return FileNameTmp;\r
314 }\r
315\r
316 FreePool (FileNameTmp);\r
317 FileNameTmp = NULL;\r
318 Suffix++;\r
319 } while (Suffix != 0);\r
320\r
321 FreePool (FileNameTmp);\r
322 return NULL;\r
5d73d92f 323}\r
324\r
3737ac2b 325/**\r
326 Read a file into an allocated buffer. The buffer is the responsibility \r
327 of the caller to free.\r
328\r
329 @param[in] FileName The filename of the file to open.\r
330 @param[out] Buffer Upon successful return, the pointer to the \r
331 address of the allocated buffer. \r
332 @param[out] BufferSize If not NULL, then the pointer to the size\r
333 of the allocated buffer.\r
334 @param[out] ReadOnly Upon successful return TRUE if the file is\r
335 read only. FALSE otherwise.\r
336\r
337 @retval EFI_NOT_FOUND The filename did not represent a file in the \r
338 file system.\r
339 @retval EFI_SUCCESS The file was read into the buffer.\r
340 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
341 @retval EFI_LOAD_ERROR The file read operation failed.\r
342 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
343 @retval EFI_INVALID_PARAMETER FileName was NULL.\r
344 @retval EFI_INVALID_PARAMETER FileName was a directory.\r
345**/\r
346EFI_STATUS\r
3737ac2b 347ReadFileIntoBuffer (\r
348 IN CONST CHAR16 *FileName,\r
349 OUT VOID **Buffer,\r
350 OUT UINTN *BufferSize OPTIONAL,\r
351 OUT BOOLEAN *ReadOnly\r
352 )\r
353{\r
354 VOID *InternalBuffer;\r
355 UINTN FileSize;\r
356 SHELL_FILE_HANDLE FileHandle;\r
357 BOOLEAN CreateFile;\r
358 EFI_STATUS Status;\r
359 EFI_FILE_INFO *Info;\r
360\r
361 InternalBuffer = NULL;\r
362 FileSize = 0;\r
363 FileHandle = NULL;\r
364 CreateFile = FALSE;\r
365 Status = EFI_SUCCESS;\r
366 Info = NULL;\r
367\r
368 if (FileName == NULL || Buffer == NULL || ReadOnly == NULL) {\r
369 return (EFI_INVALID_PARAMETER);\r
370 }\r
371\r
372 //\r
373 // try to open the file\r
374 //\r
375 Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ, 0);\r
376\r
377 if (!EFI_ERROR(Status)) {\r
378 ASSERT(CreateFile == FALSE);\r
379 if (FileHandle == NULL) {\r
380 return EFI_LOAD_ERROR;\r
381 }\r
382\r
383 Info = ShellGetFileInfo(FileHandle);\r
384 \r
385 if (Info->Attribute & EFI_FILE_DIRECTORY) {\r
386 FreePool (Info);\r
387 return EFI_INVALID_PARAMETER;\r
388 }\r
389\r
390 if (Info->Attribute & EFI_FILE_READ_ONLY) {\r
391 *ReadOnly = TRUE;\r
392 } else {\r
393 *ReadOnly = FALSE;\r
394 }\r
395 //\r
396 // get file size\r
397 //\r
398 FileSize = (UINTN) Info->FileSize;\r
399\r
400 FreePool (Info);\r
401 } else if (Status == EFI_NOT_FOUND) {\r
402 //\r
403 // file not exists. add create and try again\r
404 //\r
405 Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r
406 if (EFI_ERROR (Status)) {\r
407 return Status;\r
408 } else {\r
409 //\r
410 // it worked. now delete it and move on with the name (now validated)\r
411 //\r
412 Status = ShellDeleteFile (&FileHandle);\r
413 if (Status == EFI_WARN_DELETE_FAILURE) {\r
414 Status = EFI_ACCESS_DENIED;\r
415 }\r
416 if (EFI_ERROR (Status)) {\r
417 return Status;\r
418 }\r
419 }\r
420 //\r
421 // file doesn't exist, so set CreateFile to TRUE and can't be read-only\r
422 //\r
423 CreateFile = TRUE;\r
424 *ReadOnly = FALSE;\r
425 }\r
426\r
427 //\r
428 // the file exists\r
429 //\r
430 if (!CreateFile) {\r
431 //\r
432 // allocate buffer to read file\r
433 //\r
434 InternalBuffer = AllocateZeroPool (FileSize);\r
435 if (InternalBuffer == NULL) {\r
436 return EFI_OUT_OF_RESOURCES;\r
437 }\r
438 //\r
439 // read file into InternalBuffer\r
440 //\r
441 Status = ShellReadFile (FileHandle, &FileSize, InternalBuffer);\r
442 ShellCloseFile(&FileHandle);\r
443 FileHandle = NULL;\r
444 if (EFI_ERROR (Status)) {\r
445 SHELL_FREE_NON_NULL (InternalBuffer);\r
446 return EFI_LOAD_ERROR;\r
447 }\r
448 }\r
449 *Buffer = InternalBuffer;\r
450 if (BufferSize != NULL) {\r
451 *BufferSize = FileSize;\r
452 }\r
453 return (EFI_SUCCESS);\r
454\r
455}\r