2 Main file for NULL named library for debug1 profile shell command functions.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "UefiShellDebug1CommandsLib.h"
10 #include <Library/BcfgCommandLib.h>
12 STATIC CONST CHAR16 mFileName
[] = L
"Debug1Commands";
13 EFI_HANDLE gShellDebug1HiiHandle
= NULL
;
16 Gets the debug file name. This will be used if HII is not working.
18 @retval NULL No file is available.
19 @return The NULL-terminated filename to get help from.
23 ShellCommandGetManFileNameDebug1 (
31 Constructor for the Shell Debug1 Commands library.
33 @param ImageHandle the image handle of the process
34 @param SystemTable the EFI System Table pointer
36 @retval EFI_SUCCESS the shell command handlers were installed sucessfully
37 @retval EFI_UNSUPPORTED the shell level required was not found.
41 UefiShellDebug1CommandsLibConstructor (
42 IN EFI_HANDLE ImageHandle
,
43 IN EFI_SYSTEM_TABLE
*SystemTable
47 // check our bit of the profiles mask
49 if ((PcdGet8(PcdShellProfileMask
) & BIT1
) == 0) {
54 // install the HII stuff.
56 gShellDebug1HiiHandle
= HiiAddPackages (&gShellDebug1HiiGuid
, gImageHandle
, UefiShellDebug1CommandsLibStrings
, NULL
);
57 if (gShellDebug1HiiHandle
== NULL
) {
58 return (EFI_DEVICE_ERROR
);
62 // install our shell command handlers that are always installed
64 ShellCommandRegisterCommandName(L
"setsize", ShellCommandRunSetSize
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SETSIZE
) );
65 ShellCommandRegisterCommandName(L
"comp", ShellCommandRunComp
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_COMP
) );
66 ShellCommandRegisterCommandName(L
"mode", ShellCommandRunMode
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MODE
) );
67 ShellCommandRegisterCommandName(L
"memmap", ShellCommandRunMemMap
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MEMMAP
) );
68 ShellCommandRegisterCommandName(L
"eficompress", ShellCommandRunEfiCompress
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EFICOMPRESS
) );
69 ShellCommandRegisterCommandName(L
"efidecompress", ShellCommandRunEfiDecompress
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EFIDCOMPRESS
) );
70 ShellCommandRegisterCommandName(L
"dmem", ShellCommandRunDmem
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DMEM
) );
71 ShellCommandRegisterCommandName(L
"loadpcirom", ShellCommandRunLoadPciRom
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_LOAD_PCI_ROM
) );
72 ShellCommandRegisterCommandName(L
"mm", ShellCommandRunMm
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MM
) );
73 ShellCommandRegisterCommandName(L
"setvar", ShellCommandRunSetVar
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SETVAR
) );
74 ShellCommandRegisterCommandName(L
"sermode", ShellCommandRunSerMode
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SERMODE
) );
75 ShellCommandRegisterCommandName(L
"pci", ShellCommandRunPci
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_PCI
) );
76 ShellCommandRegisterCommandName(L
"smbiosview", ShellCommandRunSmbiosView
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SMBIOSVIEW
) );
77 ShellCommandRegisterCommandName(L
"dmpstore", ShellCommandRunDmpStore
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DMPSTORE
) );
78 ShellCommandRegisterCommandName(L
"dblk", ShellCommandRunDblk
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DBLK
) );
79 ShellCommandRegisterCommandName(L
"edit", ShellCommandRunEdit
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EDIT
) );
80 ShellCommandRegisterCommandName(L
"hexedit", ShellCommandRunHexEdit
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_HEXEDIT
) );
82 ShellCommandRegisterAlias(L
"dmem", L
"mem");
84 BcfgLibraryRegisterBcfgCommand(ImageHandle
, SystemTable
, L
"Debug1");
90 Destructor for the library. free any resources.
92 @param ImageHandle The image handle of the process.
93 @param SystemTable The EFI System Table pointer.
97 UefiShellDebug1CommandsLibDestructor (
98 IN EFI_HANDLE ImageHandle
,
99 IN EFI_SYSTEM_TABLE
*SystemTable
102 if (gShellDebug1HiiHandle
!= NULL
) {
103 HiiRemovePackages(gShellDebug1HiiHandle
);
106 BcfgLibraryUnregisterBcfgCommand(ImageHandle
, SystemTable
);
107 return (EFI_SUCCESS
);
112 Function returns a system configuration table that is stored in the
113 EFI System Table based on the provided GUID.
115 @param[in] TableGuid A pointer to the table's GUID type.
116 @param[in, out] Table On exit, a pointer to a system configuration table.
118 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
119 @retval EFI_NOT_FOUND A configuration table matching TableGuid was not found.
122 GetSystemConfigurationTable (
123 IN EFI_GUID
*TableGuid
,
128 ASSERT (Table
!= NULL
);
130 for (Index
= 0; Index
< gST
->NumberOfTableEntries
; Index
++) {
131 if (CompareGuid (TableGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
132 *Table
= gST
->ConfigurationTable
[Index
].VendorTable
;
137 return EFI_NOT_FOUND
;
141 Clear the line at the specified Row.
143 @param[in] Row The row number to be cleared ( start from 1 )
144 @param[in] LastCol The last printable column.
145 @param[in] LastRow The last printable row.
162 // prepare a blank line
163 // If max column is larger, split to multiple prints.
165 SetMem16 (Line
, sizeof (Line
), L
' ');
166 Line
[ARRAY_SIZE (Line
) - 1] = CHAR_NULL
;
168 for (Col
= 1; Col
<= LastCol
; Col
+= ARRAY_SIZE (Line
) - 1) {
169 if (Col
+ ARRAY_SIZE (Line
) - 1 > LastCol
) {
170 if (Row
== LastRow
) {
172 // if CHAR_NULL is still at position LastCol, it will cause first line error
174 Line
[(LastCol
- 1) % (ARRAY_SIZE (Line
) - 1)] = CHAR_NULL
;
176 Line
[LastCol
% (ARRAY_SIZE (Line
) - 1)] = CHAR_NULL
;
181 // print out the blank line
183 ShellPrintEx ((INT32
) Col
- 1, (INT32
) Row
- 1, Line
);
188 Determine if the character is valid for a filename.
190 @param[in] Ch The character to test.
192 @retval TRUE The character is valid.
193 @retval FALSE The character is not valid.
196 IsValidFileNameChar (
201 // See if there are any illegal characters within the name
203 if (Ch
< 0x20 || Ch
== L
'\"' || Ch
== L
'*' || Ch
== L
'/' || Ch
== L
'<' || Ch
== L
'>' || Ch
== L
'?' || Ch
== L
'|') {
211 Check if file name has illegal characters.
213 @param Name The filename to check.
215 @retval TRUE The filename is ok.
216 @retval FALSE The filename is not ok.
220 IN CONST CHAR16
*Name
228 // check the length of Name
230 for (Len
= 0, Index
= StrLen (Name
) - 1; Index
+ 1 != 0; Index
--, Len
++) {
231 if (Name
[Index
] == '\\' || Name
[Index
] == ':') {
236 if (Len
== 0 || Len
> 255) {
240 // check whether any char in Name not appears in valid file name char
242 for (Index
= 0; Index
< StrLen (Name
); Index
++) {
243 if (!IsValidFileNameChar (Name
[Index
])) {
252 Find a filename that is valid (not taken) with the given extension.
254 @param[in] Extension The file extension.
256 @retval NULL Something went wrong.
257 @return the valid filename.
260 EditGetDefaultFileName (
261 IN CONST CHAR16
*Extension
271 FileNameTmp
= CatSPrint (NULL
, L
"NewFile%d.%s", Suffix
, Extension
);
274 // after that filename changed to path
276 Status
= ShellFileExists (FileNameTmp
);
278 if (Status
== EFI_NOT_FOUND
) {
282 FreePool (FileNameTmp
);
285 } while (Suffix
!= 0);
287 FreePool (FileNameTmp
);
292 Read a file into an allocated buffer. The buffer is the responsibility
293 of the caller to free.
295 @param[in] FileName The filename of the file to open.
296 @param[out] Buffer Upon successful return, the pointer to the
297 address of the allocated buffer.
298 @param[out] BufferSize If not NULL, then the pointer to the size
299 of the allocated buffer.
300 @param[out] ReadOnly Upon successful return TRUE if the file is
301 read only. FALSE otherwise.
303 @retval EFI_NOT_FOUND The filename did not represent a file in the
305 @retval EFI_SUCCESS The file was read into the buffer.
306 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
307 @retval EFI_LOAD_ERROR The file read operation failed.
308 @retval EFI_INVALID_PARAMETER A parameter was invalid.
309 @retval EFI_INVALID_PARAMETER FileName was NULL.
310 @retval EFI_INVALID_PARAMETER FileName was a directory.
314 IN CONST CHAR16
*FileName
,
316 OUT UINTN
*BufferSize OPTIONAL
,
317 OUT BOOLEAN
*ReadOnly
320 VOID
*InternalBuffer
;
322 SHELL_FILE_HANDLE FileHandle
;
327 InternalBuffer
= NULL
;
331 Status
= EFI_SUCCESS
;
334 if (FileName
== NULL
|| Buffer
== NULL
|| ReadOnly
== NULL
) {
335 return (EFI_INVALID_PARAMETER
);
339 // try to open the file
341 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
343 if (!EFI_ERROR(Status
)) {
344 ASSERT(CreateFile
== FALSE
);
345 if (FileHandle
== NULL
) {
346 return EFI_LOAD_ERROR
;
349 Info
= ShellGetFileInfo(FileHandle
);
351 if (Info
->Attribute
& EFI_FILE_DIRECTORY
) {
353 return EFI_INVALID_PARAMETER
;
356 if (Info
->Attribute
& EFI_FILE_READ_ONLY
) {
364 FileSize
= (UINTN
) Info
->FileSize
;
367 } else if (Status
== EFI_NOT_FOUND
) {
369 // file not exists. add create and try again
371 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
372 if (EFI_ERROR (Status
)) {
376 // it worked. now delete it and move on with the name (now validated)
378 Status
= ShellDeleteFile (&FileHandle
);
379 if (Status
== EFI_WARN_DELETE_FAILURE
) {
380 Status
= EFI_ACCESS_DENIED
;
382 if (EFI_ERROR (Status
)) {
387 // file doesn't exist, so set CreateFile to TRUE and can't be read-only
398 // allocate buffer to read file
400 InternalBuffer
= AllocateZeroPool (FileSize
);
401 if (InternalBuffer
== NULL
) {
402 return EFI_OUT_OF_RESOURCES
;
405 // read file into InternalBuffer
407 Status
= ShellReadFile (FileHandle
, &FileSize
, InternalBuffer
);
408 ShellCloseFile(&FileHandle
);
410 if (EFI_ERROR (Status
)) {
411 SHELL_FREE_NON_NULL (InternalBuffer
);
412 return EFI_LOAD_ERROR
;
415 *Buffer
= InternalBuffer
;
416 if (BufferSize
!= NULL
) {
417 *BufferSize
= FileSize
;
419 return (EFI_SUCCESS
);