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