]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.c
ShellBinPkg: Replace BSD License with BSD+Patent License
[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
ba0014b9 4 Copyright (c) 2010 - 2018, 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/**\r
118 Function returns a system configuration table that is stored in the\r
119 EFI System Table based on the provided GUID.\r
120\r
4ff7e37b
ED
121 @param[in] TableGuid A pointer to the table's GUID type.\r
122 @param[in, out] Table On exit, a pointer to a system configuration table.\r
5d73d92f 123\r
124 @retval EFI_SUCCESS A configuration table matching TableGuid was found.\r
125 @retval EFI_NOT_FOUND A configuration table matching TableGuid was not found.\r
126**/\r
127EFI_STATUS\r
5d73d92f 128GetSystemConfigurationTable (\r
129 IN EFI_GUID *TableGuid,\r
130 IN OUT VOID **Table\r
131 )\r
132{\r
133 UINTN Index;\r
134 ASSERT (Table != NULL);\r
135\r
136 for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {\r
3737ac2b 137 if (CompareGuid (TableGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
5d73d92f 138 *Table = gST->ConfigurationTable[Index].VendorTable;\r
139 return EFI_SUCCESS;\r
140 }\r
141 }\r
142\r
143 return EFI_NOT_FOUND;\r
144}\r
145\r
3737ac2b 146/**\r
147 Clear the line at the specified Row.\r
ba0014b9 148\r
3737ac2b 149 @param[in] Row The row number to be cleared ( start from 1 )\r
150 @param[in] LastCol The last printable column.\r
151 @param[in] LastRow The last printable row.\r
152**/\r
153VOID\r
3737ac2b 154EditorClearLine (\r
155 IN UINTN Row,\r
156 IN UINTN LastCol,\r
157 IN UINTN LastRow\r
158 )\r
159{\r
1a421c2c 160 UINTN Col;\r
3737ac2b 161 CHAR16 Line[200];\r
162\r
163 if (Row == 0) {\r
164 Row = 1;\r
165 }\r
166\r
167 //\r
168 // prepare a blank line\r
1a421c2c 169 // If max column is larger, split to multiple prints.\r
3737ac2b 170 //\r
1a421c2c
RN
171 SetMem16 (Line, sizeof (Line), L' ');\r
172 Line[ARRAY_SIZE (Line) - 1] = CHAR_NULL;\r
173\r
174 for (Col = 1; Col <= LastCol; Col += ARRAY_SIZE (Line) - 1) {\r
175 if (Col + ARRAY_SIZE (Line) - 1 > LastCol) {\r
176 if (Row == LastRow) {\r
177 //\r
178 // if CHAR_NULL is still at position LastCol, it will cause first line error\r
179 //\r
452676ff 180 Line[(LastCol - 1) % (ARRAY_SIZE (Line) - 1)] = CHAR_NULL;\r
1a421c2c
RN
181 } else {\r
182 Line[LastCol % (ARRAY_SIZE (Line) - 1)] = CHAR_NULL;\r
183 }\r
184 }\r
ba0014b9 185\r
3737ac2b 186 //\r
1a421c2c 187 // print out the blank line\r
3737ac2b 188 //\r
1a421c2c 189 ShellPrintEx ((INT32) Col - 1, (INT32) Row - 1, Line);\r
3737ac2b 190 }\r
3737ac2b 191}\r
192\r
193/**\r
194 Determine if the character is valid for a filename.\r
195\r
196 @param[in] Ch The character to test.\r
197\r
198 @retval TRUE The character is valid.\r
199 @retval FALSE The character is not valid.\r
200**/\r
201BOOLEAN\r
3737ac2b 202IsValidFileNameChar (\r
203 IN CONST CHAR16 Ch\r
204 )\r
205{\r
206 //\r
207 // See if there are any illegal characters within the name\r
208 //\r
d16efcae 209 if (Ch < 0x20 || Ch == L'\"' || Ch == L'*' || Ch == L'/' || Ch == L'<' || Ch == L'>' || Ch == L'?' || Ch == L'|') {\r
3737ac2b 210 return FALSE;\r
211 }\r
212\r
213 return TRUE;\r
214}\r
215\r
216/**\r
217 Check if file name has illegal characters.\r
ba0014b9 218\r
3737ac2b 219 @param Name The filename to check.\r
220\r
221 @retval TRUE The filename is ok.\r
222 @retval FALSE The filename is not ok.\r
223**/\r
224BOOLEAN\r
3737ac2b 225IsValidFileName (\r
226 IN CONST CHAR16 *Name\r
227 )\r
228{\r
229\r
230 UINTN Index;\r
231 UINTN Len;\r
232\r
233 //\r
234 // check the length of Name\r
235 //\r
236 for (Len = 0, Index = StrLen (Name) - 1; Index + 1 != 0; Index--, Len++) {\r
237 if (Name[Index] == '\\' || Name[Index] == ':') {\r
238 break;\r
239 }\r
240 }\r
241\r
242 if (Len == 0 || Len > 255) {\r
243 return FALSE;\r
244 }\r
245 //\r
246 // check whether any char in Name not appears in valid file name char\r
247 //\r
248 for (Index = 0; Index < StrLen (Name); Index++) {\r
249 if (!IsValidFileNameChar (Name[Index])) {\r
250 return FALSE;\r
251 }\r
5d73d92f 252 }\r
3737ac2b 253\r
254 return TRUE;\r
255}\r
256\r
257/**\r
258 Find a filename that is valid (not taken) with the given extension.\r
259\r
260 @param[in] Extension The file extension.\r
261\r
262 @retval NULL Something went wrong.\r
263 @return the valid filename.\r
264**/\r
265CHAR16 *\r
3737ac2b 266EditGetDefaultFileName (\r
267 IN CONST CHAR16 *Extension\r
268 )\r
269{\r
270 EFI_STATUS Status;\r
271 UINTN Suffix;\r
3737ac2b 272 CHAR16 *FileNameTmp;\r
273\r
274 Suffix = 0;\r
3737ac2b 275\r
276 do {\r
277 FileNameTmp = CatSPrint (NULL, L"NewFile%d.%s", Suffix, Extension);\r
278\r
279 //\r
280 // after that filename changed to path\r
281 //\r
282 Status = ShellFileExists (FileNameTmp);\r
283\r
284 if (Status == EFI_NOT_FOUND) {\r
285 return FileNameTmp;\r
286 }\r
287\r
288 FreePool (FileNameTmp);\r
289 FileNameTmp = NULL;\r
290 Suffix++;\r
291 } while (Suffix != 0);\r
292\r
293 FreePool (FileNameTmp);\r
294 return NULL;\r
5d73d92f 295}\r
296\r
3737ac2b 297/**\r
ba0014b9 298 Read a file into an allocated buffer. The buffer is the responsibility\r
3737ac2b 299 of the caller to free.\r
300\r
301 @param[in] FileName The filename of the file to open.\r
ba0014b9
LG
302 @param[out] Buffer Upon successful return, the pointer to the\r
303 address of the allocated buffer.\r
3737ac2b 304 @param[out] BufferSize If not NULL, then the pointer to the size\r
305 of the allocated buffer.\r
306 @param[out] ReadOnly Upon successful return TRUE if the file is\r
307 read only. FALSE otherwise.\r
308\r
ba0014b9 309 @retval EFI_NOT_FOUND The filename did not represent a file in the\r
3737ac2b 310 file system.\r
311 @retval EFI_SUCCESS The file was read into the buffer.\r
312 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
313 @retval EFI_LOAD_ERROR The file read operation failed.\r
314 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
315 @retval EFI_INVALID_PARAMETER FileName was NULL.\r
316 @retval EFI_INVALID_PARAMETER FileName was a directory.\r
317**/\r
318EFI_STATUS\r
3737ac2b 319ReadFileIntoBuffer (\r
320 IN CONST CHAR16 *FileName,\r
321 OUT VOID **Buffer,\r
322 OUT UINTN *BufferSize OPTIONAL,\r
323 OUT BOOLEAN *ReadOnly\r
324 )\r
325{\r
326 VOID *InternalBuffer;\r
327 UINTN FileSize;\r
328 SHELL_FILE_HANDLE FileHandle;\r
329 BOOLEAN CreateFile;\r
330 EFI_STATUS Status;\r
331 EFI_FILE_INFO *Info;\r
332\r
333 InternalBuffer = NULL;\r
334 FileSize = 0;\r
335 FileHandle = NULL;\r
336 CreateFile = FALSE;\r
337 Status = EFI_SUCCESS;\r
338 Info = NULL;\r
339\r
340 if (FileName == NULL || Buffer == NULL || ReadOnly == NULL) {\r
341 return (EFI_INVALID_PARAMETER);\r
342 }\r
343\r
344 //\r
345 // try to open the file\r
346 //\r
347 Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ, 0);\r
348\r
349 if (!EFI_ERROR(Status)) {\r
350 ASSERT(CreateFile == FALSE);\r
351 if (FileHandle == NULL) {\r
352 return EFI_LOAD_ERROR;\r
353 }\r
354\r
355 Info = ShellGetFileInfo(FileHandle);\r
ba0014b9 356\r
3737ac2b 357 if (Info->Attribute & EFI_FILE_DIRECTORY) {\r
358 FreePool (Info);\r
359 return EFI_INVALID_PARAMETER;\r
360 }\r
361\r
362 if (Info->Attribute & EFI_FILE_READ_ONLY) {\r
363 *ReadOnly = TRUE;\r
364 } else {\r
365 *ReadOnly = FALSE;\r
366 }\r
367 //\r
368 // get file size\r
369 //\r
370 FileSize = (UINTN) Info->FileSize;\r
371\r
372 FreePool (Info);\r
373 } else if (Status == EFI_NOT_FOUND) {\r
374 //\r
375 // file not exists. add create and try again\r
376 //\r
377 Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r
378 if (EFI_ERROR (Status)) {\r
379 return Status;\r
380 } else {\r
381 //\r
382 // it worked. now delete it and move on with the name (now validated)\r
383 //\r
384 Status = ShellDeleteFile (&FileHandle);\r
385 if (Status == EFI_WARN_DELETE_FAILURE) {\r
386 Status = EFI_ACCESS_DENIED;\r
387 }\r
388 if (EFI_ERROR (Status)) {\r
389 return Status;\r
390 }\r
391 }\r
392 //\r
393 // file doesn't exist, so set CreateFile to TRUE and can't be read-only\r
394 //\r
395 CreateFile = TRUE;\r
396 *ReadOnly = FALSE;\r
397 }\r
398\r
399 //\r
400 // the file exists\r
401 //\r
402 if (!CreateFile) {\r
403 //\r
404 // allocate buffer to read file\r
405 //\r
406 InternalBuffer = AllocateZeroPool (FileSize);\r
407 if (InternalBuffer == NULL) {\r
408 return EFI_OUT_OF_RESOURCES;\r
409 }\r
410 //\r
411 // read file into InternalBuffer\r
412 //\r
413 Status = ShellReadFile (FileHandle, &FileSize, InternalBuffer);\r
414 ShellCloseFile(&FileHandle);\r
415 FileHandle = NULL;\r
416 if (EFI_ERROR (Status)) {\r
417 SHELL_FREE_NON_NULL (InternalBuffer);\r
418 return EFI_LOAD_ERROR;\r
419 }\r
420 }\r
421 *Buffer = InternalBuffer;\r
422 if (BufferSize != NULL) {\r
423 *BufferSize = FileSize;\r
424 }\r
425 return (EFI_SUCCESS);\r
426\r
427}\r