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 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellDebug1CommandsLib.h"
16 #include <Library/BcfgCommandLib.h>
18 STATIC CONST CHAR16 mFileName
[] = L
"Debug1Commands";
19 EFI_HANDLE gShellDebug1HiiHandle
= NULL
;
22 Gets the debug file name. This will be used if HII is not working.
24 @retval NULL No file is available.
25 @return The NULL-terminated filename to get help from.
29 ShellCommandGetManFileNameDebug1 (
37 Constructor for the Shell Debug1 Commands library.
39 @param ImageHandle the image handle of the process
40 @param SystemTable the EFI System Table pointer
42 @retval EFI_SUCCESS the shell command handlers were installed sucessfully
43 @retval EFI_UNSUPPORTED the shell level required was not found.
47 UefiShellDebug1CommandsLibConstructor (
48 IN EFI_HANDLE ImageHandle
,
49 IN EFI_SYSTEM_TABLE
*SystemTable
53 // check our bit of the profiles mask
55 if ((PcdGet8(PcdShellProfileMask
) & BIT1
) == 0) {
60 // install the HII stuff.
62 gShellDebug1HiiHandle
= HiiAddPackages (&gShellDebug1HiiGuid
, gImageHandle
, UefiShellDebug1CommandsLibStrings
, NULL
);
63 if (gShellDebug1HiiHandle
== NULL
) {
64 return (EFI_DEVICE_ERROR
);
68 // install our shell command handlers that are always installed
70 ShellCommandRegisterCommandName(L
"setsize", ShellCommandRunSetSize
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SETSIZE
) );
71 ShellCommandRegisterCommandName(L
"comp", ShellCommandRunComp
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_COMP
) );
72 ShellCommandRegisterCommandName(L
"mode", ShellCommandRunMode
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MODE
) );
73 ShellCommandRegisterCommandName(L
"memmap", ShellCommandRunMemMap
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MEMMAP
) );
74 ShellCommandRegisterCommandName(L
"eficompress", ShellCommandRunEfiCompress
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EFICOMPRESS
) );
75 ShellCommandRegisterCommandName(L
"efidecompress", ShellCommandRunEfiDecompress
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EFIDCOMPRESS
) );
76 ShellCommandRegisterCommandName(L
"dmem", ShellCommandRunDmem
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DMEM
) );
77 ShellCommandRegisterCommandName(L
"loadpcirom", ShellCommandRunLoadPciRom
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_LOAD_PCI_ROM
) );
78 ShellCommandRegisterCommandName(L
"mm", ShellCommandRunMm
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MM
) );
79 ShellCommandRegisterCommandName(L
"setvar", ShellCommandRunSetVar
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SETVAR
) );
80 ShellCommandRegisterCommandName(L
"sermode", ShellCommandRunSerMode
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SERMODE
) );
81 ShellCommandRegisterCommandName(L
"pci", ShellCommandRunPci
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_PCI
) );
82 ShellCommandRegisterCommandName(L
"smbiosview", ShellCommandRunSmbiosView
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SMBIOSVIEW
) );
83 ShellCommandRegisterCommandName(L
"dmpstore", ShellCommandRunDmpStore
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DMPSTORE
) );
84 ShellCommandRegisterCommandName(L
"dblk", ShellCommandRunDblk
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DBLK
) );
85 ShellCommandRegisterCommandName(L
"edit", ShellCommandRunEdit
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EDIT
) );
86 ShellCommandRegisterCommandName(L
"hexedit", ShellCommandRunHexEdit
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_HEXEDIT
) );
88 ShellCommandRegisterAlias(L
"dmem", L
"mem");
90 BcfgLibraryRegisterBcfgCommand(ImageHandle
, SystemTable
, L
"Debug1");
96 Destructor for the library. free any resources.
98 @param ImageHandle The image handle of the process.
99 @param SystemTable The EFI System Table pointer.
103 UefiShellDebug1CommandsLibDestructor (
104 IN EFI_HANDLE ImageHandle
,
105 IN EFI_SYSTEM_TABLE
*SystemTable
108 if (gShellDebug1HiiHandle
!= NULL
) {
109 HiiRemovePackages(gShellDebug1HiiHandle
);
112 BcfgLibraryUnregisterBcfgCommand(ImageHandle
, SystemTable
);
113 return (EFI_SUCCESS
);
118 Function returns a system configuration table that is stored in the
119 EFI System Table based on the provided GUID.
121 @param[in] TableGuid A pointer to the table's GUID type.
122 @param[in, out] Table On exit, a pointer to a system configuration table.
124 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
125 @retval EFI_NOT_FOUND A configuration table matching TableGuid was not found.
128 GetSystemConfigurationTable (
129 IN EFI_GUID
*TableGuid
,
134 ASSERT (Table
!= NULL
);
136 for (Index
= 0; Index
< gST
->NumberOfTableEntries
; Index
++) {
137 if (CompareGuid (TableGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
138 *Table
= gST
->ConfigurationTable
[Index
].VendorTable
;
143 return EFI_NOT_FOUND
;
147 Clear the line at the specified Row.
149 @param[in] Row The row number to be cleared ( start from 1 )
150 @param[in] LastCol The last printable column.
151 @param[in] LastRow The last printable row.
168 // prepare a blank line
169 // If max column is larger, split to multiple prints.
171 SetMem16 (Line
, sizeof (Line
), L
' ');
172 Line
[ARRAY_SIZE (Line
) - 1] = CHAR_NULL
;
174 for (Col
= 1; Col
<= LastCol
; Col
+= ARRAY_SIZE (Line
) - 1) {
175 if (Col
+ ARRAY_SIZE (Line
) - 1 > LastCol
) {
176 if (Row
== LastRow
) {
178 // if CHAR_NULL is still at position LastCol, it will cause first line error
180 Line
[(LastCol
- 1) % (ARRAY_SIZE (Line
) - 1)] = CHAR_NULL
;
182 Line
[LastCol
% (ARRAY_SIZE (Line
) - 1)] = CHAR_NULL
;
187 // print out the blank line
189 ShellPrintEx ((INT32
) Col
- 1, (INT32
) Row
- 1, Line
);
194 Determine if the character is valid for a filename.
196 @param[in] Ch The character to test.
198 @retval TRUE The character is valid.
199 @retval FALSE The character is not valid.
202 IsValidFileNameChar (
207 // See if there are any illegal characters within the name
209 if (Ch
< 0x20 || Ch
== L
'\"' || Ch
== L
'*' || Ch
== L
'/' || Ch
== L
'<' || Ch
== L
'>' || Ch
== L
'?' || Ch
== L
'|') {
217 Check if file name has illegal characters.
219 @param Name The filename to check.
221 @retval TRUE The filename is ok.
222 @retval FALSE The filename is not ok.
226 IN CONST CHAR16
*Name
234 // check the length of Name
236 for (Len
= 0, Index
= StrLen (Name
) - 1; Index
+ 1 != 0; Index
--, Len
++) {
237 if (Name
[Index
] == '\\' || Name
[Index
] == ':') {
242 if (Len
== 0 || Len
> 255) {
246 // check whether any char in Name not appears in valid file name char
248 for (Index
= 0; Index
< StrLen (Name
); Index
++) {
249 if (!IsValidFileNameChar (Name
[Index
])) {
258 Find a filename that is valid (not taken) with the given extension.
260 @param[in] Extension The file extension.
262 @retval NULL Something went wrong.
263 @return the valid filename.
266 EditGetDefaultFileName (
267 IN CONST CHAR16
*Extension
277 FileNameTmp
= CatSPrint (NULL
, L
"NewFile%d.%s", Suffix
, Extension
);
280 // after that filename changed to path
282 Status
= ShellFileExists (FileNameTmp
);
284 if (Status
== EFI_NOT_FOUND
) {
288 FreePool (FileNameTmp
);
291 } while (Suffix
!= 0);
293 FreePool (FileNameTmp
);
298 Read a file into an allocated buffer. The buffer is the responsibility
299 of the caller to free.
301 @param[in] FileName The filename of the file to open.
302 @param[out] Buffer Upon successful return, the pointer to the
303 address of the allocated buffer.
304 @param[out] BufferSize If not NULL, then the pointer to the size
305 of the allocated buffer.
306 @param[out] ReadOnly Upon successful return TRUE if the file is
307 read only. FALSE otherwise.
309 @retval EFI_NOT_FOUND The filename did not represent a file in the
311 @retval EFI_SUCCESS The file was read into the buffer.
312 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
313 @retval EFI_LOAD_ERROR The file read operation failed.
314 @retval EFI_INVALID_PARAMETER A parameter was invalid.
315 @retval EFI_INVALID_PARAMETER FileName was NULL.
316 @retval EFI_INVALID_PARAMETER FileName was a directory.
320 IN CONST CHAR16
*FileName
,
322 OUT UINTN
*BufferSize OPTIONAL
,
323 OUT BOOLEAN
*ReadOnly
326 VOID
*InternalBuffer
;
328 SHELL_FILE_HANDLE FileHandle
;
333 InternalBuffer
= NULL
;
337 Status
= EFI_SUCCESS
;
340 if (FileName
== NULL
|| Buffer
== NULL
|| ReadOnly
== NULL
) {
341 return (EFI_INVALID_PARAMETER
);
345 // try to open the file
347 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
349 if (!EFI_ERROR(Status
)) {
350 ASSERT(CreateFile
== FALSE
);
351 if (FileHandle
== NULL
) {
352 return EFI_LOAD_ERROR
;
355 Info
= ShellGetFileInfo(FileHandle
);
357 if (Info
->Attribute
& EFI_FILE_DIRECTORY
) {
359 return EFI_INVALID_PARAMETER
;
362 if (Info
->Attribute
& EFI_FILE_READ_ONLY
) {
370 FileSize
= (UINTN
) Info
->FileSize
;
373 } else if (Status
== EFI_NOT_FOUND
) {
375 // file not exists. add create and try again
377 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
378 if (EFI_ERROR (Status
)) {
382 // it worked. now delete it and move on with the name (now validated)
384 Status
= ShellDeleteFile (&FileHandle
);
385 if (Status
== EFI_WARN_DELETE_FAILURE
) {
386 Status
= EFI_ACCESS_DENIED
;
388 if (EFI_ERROR (Status
)) {
393 // file doesn't exist, so set CreateFile to TRUE and can't be read-only
404 // allocate buffer to read file
406 InternalBuffer
= AllocateZeroPool (FileSize
);
407 if (InternalBuffer
== NULL
) {
408 return EFI_OUT_OF_RESOURCES
;
411 // read file into InternalBuffer
413 Status
= ShellReadFile (FileHandle
, &FileSize
, InternalBuffer
);
414 ShellCloseFile(&FileHandle
);
416 if (EFI_ERROR (Status
)) {
417 SHELL_FREE_NON_NULL (InternalBuffer
);
418 return EFI_LOAD_ERROR
;
421 *Buffer
= InternalBuffer
;
422 if (BufferSize
!= NULL
) {
423 *BufferSize
= FileSize
;
425 return (EFI_SUCCESS
);