]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.c
ShellPkg: Update header file including style
[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
188 CHAR16 Line[200];\r
189\r
190 if (Row == 0) {\r
191 Row = 1;\r
192 }\r
193\r
194 //\r
195 // prepare a blank line\r
196 //\r
197 SetMem16(Line, LastCol*sizeof(CHAR16), L' ');\r
198\r
199 if (Row == LastRow) {\r
200 //\r
201 // if CHAR_NULL is still at position 80, it will cause first line error\r
202 //\r
203 Line[LastCol - 1] = CHAR_NULL;\r
5d73d92f 204 } else {\r
3737ac2b 205 Line[LastCol] = CHAR_NULL;\r
206 }\r
207\r
208 //\r
209 // print out the blank line\r
210 //\r
211 ShellPrintEx (0, ((INT32)Row) - 1, Line);\r
212}\r
213\r
214/**\r
215 Determine if the character is valid for a filename.\r
216\r
217 @param[in] Ch The character to test.\r
218\r
219 @retval TRUE The character is valid.\r
220 @retval FALSE The character is not valid.\r
221**/\r
222BOOLEAN\r
3737ac2b 223IsValidFileNameChar (\r
224 IN CONST CHAR16 Ch\r
225 )\r
226{\r
227 //\r
228 // See if there are any illegal characters within the name\r
229 //\r
d16efcae 230 if (Ch < 0x20 || Ch == L'\"' || Ch == L'*' || Ch == L'/' || Ch == L'<' || Ch == L'>' || Ch == L'?' || Ch == L'|') {\r
3737ac2b 231 return FALSE;\r
232 }\r
233\r
234 return TRUE;\r
235}\r
236\r
237/**\r
238 Check if file name has illegal characters.\r
239 \r
240 @param Name The filename to check.\r
241\r
242 @retval TRUE The filename is ok.\r
243 @retval FALSE The filename is not ok.\r
244**/\r
245BOOLEAN\r
3737ac2b 246IsValidFileName (\r
247 IN CONST CHAR16 *Name\r
248 )\r
249{\r
250\r
251 UINTN Index;\r
252 UINTN Len;\r
253\r
254 //\r
255 // check the length of Name\r
256 //\r
257 for (Len = 0, Index = StrLen (Name) - 1; Index + 1 != 0; Index--, Len++) {\r
258 if (Name[Index] == '\\' || Name[Index] == ':') {\r
259 break;\r
260 }\r
261 }\r
262\r
263 if (Len == 0 || Len > 255) {\r
264 return FALSE;\r
265 }\r
266 //\r
267 // check whether any char in Name not appears in valid file name char\r
268 //\r
269 for (Index = 0; Index < StrLen (Name); Index++) {\r
270 if (!IsValidFileNameChar (Name[Index])) {\r
271 return FALSE;\r
272 }\r
5d73d92f 273 }\r
3737ac2b 274\r
275 return TRUE;\r
276}\r
277\r
278/**\r
279 Find a filename that is valid (not taken) with the given extension.\r
280\r
281 @param[in] Extension The file extension.\r
282\r
283 @retval NULL Something went wrong.\r
284 @return the valid filename.\r
285**/\r
286CHAR16 *\r
3737ac2b 287EditGetDefaultFileName (\r
288 IN CONST CHAR16 *Extension\r
289 )\r
290{\r
291 EFI_STATUS Status;\r
292 UINTN Suffix;\r
3737ac2b 293 CHAR16 *FileNameTmp;\r
294\r
295 Suffix = 0;\r
3737ac2b 296\r
297 do {\r
298 FileNameTmp = CatSPrint (NULL, L"NewFile%d.%s", Suffix, Extension);\r
299\r
300 //\r
301 // after that filename changed to path\r
302 //\r
303 Status = ShellFileExists (FileNameTmp);\r
304\r
305 if (Status == EFI_NOT_FOUND) {\r
306 return FileNameTmp;\r
307 }\r
308\r
309 FreePool (FileNameTmp);\r
310 FileNameTmp = NULL;\r
311 Suffix++;\r
312 } while (Suffix != 0);\r
313\r
314 FreePool (FileNameTmp);\r
315 return NULL;\r
5d73d92f 316}\r
317\r
3737ac2b 318/**\r
319 Read a file into an allocated buffer. The buffer is the responsibility \r
320 of the caller to free.\r
321\r
322 @param[in] FileName The filename of the file to open.\r
323 @param[out] Buffer Upon successful return, the pointer to the \r
324 address of the allocated buffer. \r
325 @param[out] BufferSize If not NULL, then the pointer to the size\r
326 of the allocated buffer.\r
327 @param[out] ReadOnly Upon successful return TRUE if the file is\r
328 read only. FALSE otherwise.\r
329\r
330 @retval EFI_NOT_FOUND The filename did not represent a file in the \r
331 file system.\r
332 @retval EFI_SUCCESS The file was read into the buffer.\r
333 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
334 @retval EFI_LOAD_ERROR The file read operation failed.\r
335 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
336 @retval EFI_INVALID_PARAMETER FileName was NULL.\r
337 @retval EFI_INVALID_PARAMETER FileName was a directory.\r
338**/\r
339EFI_STATUS\r
3737ac2b 340ReadFileIntoBuffer (\r
341 IN CONST CHAR16 *FileName,\r
342 OUT VOID **Buffer,\r
343 OUT UINTN *BufferSize OPTIONAL,\r
344 OUT BOOLEAN *ReadOnly\r
345 )\r
346{\r
347 VOID *InternalBuffer;\r
348 UINTN FileSize;\r
349 SHELL_FILE_HANDLE FileHandle;\r
350 BOOLEAN CreateFile;\r
351 EFI_STATUS Status;\r
352 EFI_FILE_INFO *Info;\r
353\r
354 InternalBuffer = NULL;\r
355 FileSize = 0;\r
356 FileHandle = NULL;\r
357 CreateFile = FALSE;\r
358 Status = EFI_SUCCESS;\r
359 Info = NULL;\r
360\r
361 if (FileName == NULL || Buffer == NULL || ReadOnly == NULL) {\r
362 return (EFI_INVALID_PARAMETER);\r
363 }\r
364\r
365 //\r
366 // try to open the file\r
367 //\r
368 Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ, 0);\r
369\r
370 if (!EFI_ERROR(Status)) {\r
371 ASSERT(CreateFile == FALSE);\r
372 if (FileHandle == NULL) {\r
373 return EFI_LOAD_ERROR;\r
374 }\r
375\r
376 Info = ShellGetFileInfo(FileHandle);\r
377 \r
378 if (Info->Attribute & EFI_FILE_DIRECTORY) {\r
379 FreePool (Info);\r
380 return EFI_INVALID_PARAMETER;\r
381 }\r
382\r
383 if (Info->Attribute & EFI_FILE_READ_ONLY) {\r
384 *ReadOnly = TRUE;\r
385 } else {\r
386 *ReadOnly = FALSE;\r
387 }\r
388 //\r
389 // get file size\r
390 //\r
391 FileSize = (UINTN) Info->FileSize;\r
392\r
393 FreePool (Info);\r
394 } else if (Status == EFI_NOT_FOUND) {\r
395 //\r
396 // file not exists. add create and try again\r
397 //\r
398 Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r
399 if (EFI_ERROR (Status)) {\r
400 return Status;\r
401 } else {\r
402 //\r
403 // it worked. now delete it and move on with the name (now validated)\r
404 //\r
405 Status = ShellDeleteFile (&FileHandle);\r
406 if (Status == EFI_WARN_DELETE_FAILURE) {\r
407 Status = EFI_ACCESS_DENIED;\r
408 }\r
409 if (EFI_ERROR (Status)) {\r
410 return Status;\r
411 }\r
412 }\r
413 //\r
414 // file doesn't exist, so set CreateFile to TRUE and can't be read-only\r
415 //\r
416 CreateFile = TRUE;\r
417 *ReadOnly = FALSE;\r
418 }\r
419\r
420 //\r
421 // the file exists\r
422 //\r
423 if (!CreateFile) {\r
424 //\r
425 // allocate buffer to read file\r
426 //\r
427 InternalBuffer = AllocateZeroPool (FileSize);\r
428 if (InternalBuffer == NULL) {\r
429 return EFI_OUT_OF_RESOURCES;\r
430 }\r
431 //\r
432 // read file into InternalBuffer\r
433 //\r
434 Status = ShellReadFile (FileHandle, &FileSize, InternalBuffer);\r
435 ShellCloseFile(&FileHandle);\r
436 FileHandle = NULL;\r
437 if (EFI_ERROR (Status)) {\r
438 SHELL_FREE_NON_NULL (InternalBuffer);\r
439 return EFI_LOAD_ERROR;\r
440 }\r
441 }\r
442 *Buffer = InternalBuffer;\r
443 if (BufferSize != NULL) {\r
444 *BufferSize = FileSize;\r
445 }\r
446 return (EFI_SUCCESS);\r
447\r
448}\r