2 Main file for NULL named library for debug1 profile shell command functions.
4 Copyright (c) 2010 - 2011, 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
);
116 STATIC CONST CHAR8 Hex
[] = {
136 Dump some hexadecimal data to the screen.
138 @param[in] Indent How many spaces to indent the output.
139 @param[in] Offset The offset of the printing.
140 @param[in] DataSize The size in bytes of UserData.
141 @param[in] UserData The data to print out.
162 while (DataSize
!= 0) {
164 if (Size
> DataSize
) {
168 for (Index
= 0; Index
< Size
; Index
+= 1) {
169 TempByte
= Data
[Index
];
170 Val
[Index
* 3 + 0] = Hex
[TempByte
>> 4];
171 Val
[Index
* 3 + 1] = Hex
[TempByte
& 0xF];
172 Val
[Index
* 3 + 2] = (CHAR8
) ((Index
== 7) ? '-' : ' ');
173 Str
[Index
] = (CHAR8
) ((TempByte
< ' ' || TempByte
> 'z') ? '.' : TempByte
);
178 ShellPrintEx(-1, -1, L
"%*a%08X: %-48a *%a*\r\n", Indent
, "", Offset
, Val
, Str
);
187 Convert a Unicode character to upper case only if
188 it maps to a valid small-case ASCII character.
190 This internal function only deal with Unicode character
191 which maps to a valid small-case ASCII character, i.e.
192 L'a' to L'z'. For other Unicode character, the input character
193 is returned directly.
195 @param Char The character to convert.
197 @retval LowerCharacter If the Char is with range L'a' to L'z'.
198 @retval Unchanged Otherwise.
201 //Stolen from MdePkg Baselib
209 if (Char
>= L
'a' && Char
<= L
'z') {
210 return (CHAR16
) (Char
- (L
'a' - L
'A'));
217 Function returns a system configuration table that is stored in the
218 EFI System Table based on the provided GUID.
220 @param[in] TableGuid A pointer to the table's GUID type.
221 @param[in, out] Table On exit, a pointer to a system configuration table.
223 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
224 @retval EFI_NOT_FOUND A configuration table matching TableGuid was not found.
228 GetSystemConfigurationTable (
229 IN EFI_GUID
*TableGuid
,
234 ASSERT (Table
!= NULL
);
236 for (Index
= 0; Index
< gST
->NumberOfTableEntries
; Index
++) {
237 if (CompareGuid (TableGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
238 *Table
= gST
->ConfigurationTable
[Index
].VendorTable
;
243 return EFI_NOT_FOUND
;
247 Convert a Unicode character to numerical value.
249 This internal function only deal with Unicode character
250 which maps to a valid hexadecimal ASII character, i.e.
251 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
252 Unicode character, the value returned does not make sense.
254 @param Char The character to convert.
256 @return The numerical value converted.
265 if (Char
>= L
'0' && Char
<= L
'9') {
269 return (UINTN
) (10 + CharToUpper (Char
) - L
'A');
273 Convert a string representation of a guid to a Guid value.
275 @param[in] StringGuid The pointer to the string of a guid.
276 @param[in, out] Guid The pointer to the GUID structure to populate.
278 @retval EFI_INVALID_PARAMETER A parameter was invalid.
279 @retval EFI_SUCCESS The conversion was successful.
283 ConvertStringToGuid (
284 IN CONST CHAR16
*StringGuid
,
285 IN OUT EFI_GUID
*Guid
294 if (StringGuid
== NULL
) {
295 return (EFI_INVALID_PARAMETER
);
296 } else if (StrLen(StringGuid
) != 36) {
297 return (EFI_INVALID_PARAMETER
);
300 TempCopy
= StrnCatGrow(&TempCopy
, NULL
, StringGuid
, 0);
301 if (TempCopy
== NULL
) {
302 return (EFI_OUT_OF_RESOURCES
);
305 TempSpot
= StrStr(Walker
, L
"-");
306 if (TempSpot
!= NULL
) {
307 *TempSpot
= CHAR_NULL
;
309 Status
= ShellConvertStringToUint64(Walker
, &TempVal
, TRUE
, FALSE
);
310 if (EFI_ERROR(Status
)) {
314 Guid
->Data1
= (UINT32
)TempVal
;
316 TempSpot
= StrStr(Walker
, L
"-");
317 if (TempSpot
!= NULL
) {
318 *TempSpot
= CHAR_NULL
;
320 Status
= ShellConvertStringToUint64(Walker
, &TempVal
, TRUE
, FALSE
);
321 if (EFI_ERROR(Status
)) {
325 Guid
->Data2
= (UINT16
)TempVal
;
327 TempSpot
= StrStr(Walker
, L
"-");
328 if (TempSpot
!= NULL
) {
329 *TempSpot
= CHAR_NULL
;
331 Status
= ShellConvertStringToUint64(Walker
, &TempVal
, TRUE
, FALSE
);
332 if (EFI_ERROR(Status
)) {
336 Guid
->Data3
= (UINT16
)TempVal
;
338 Guid
->Data4
[0] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
339 Guid
->Data4
[0] = (UINT8
)(Guid
->Data4
[0]+ (UINT8
)HexCharToUintn(Walker
[1]));
341 Guid
->Data4
[1] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
342 Guid
->Data4
[1] = (UINT8
)(Guid
->Data4
[1] + (UINT8
)HexCharToUintn(Walker
[1]));
344 Guid
->Data4
[2] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
345 Guid
->Data4
[2] = (UINT8
)(Guid
->Data4
[2] + (UINT8
)HexCharToUintn(Walker
[1]));
347 Guid
->Data4
[3] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
348 Guid
->Data4
[3] = (UINT8
)(Guid
->Data4
[3] + (UINT8
)HexCharToUintn(Walker
[1]));
350 Guid
->Data4
[4] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
351 Guid
->Data4
[4] = (UINT8
)(Guid
->Data4
[4] + (UINT8
)HexCharToUintn(Walker
[1]));
353 Guid
->Data4
[5] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
354 Guid
->Data4
[5] = (UINT8
)(Guid
->Data4
[5] + (UINT8
)HexCharToUintn(Walker
[1]));
356 Guid
->Data4
[6] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
357 Guid
->Data4
[6] = (UINT8
)(Guid
->Data4
[6] + (UINT8
)HexCharToUintn(Walker
[1]));
359 Guid
->Data4
[7] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
360 Guid
->Data4
[7] = (UINT8
)(Guid
->Data4
[7] + (UINT8
)HexCharToUintn(Walker
[1]));
362 return (EFI_SUCCESS
);
366 Clear the line at the specified Row.
368 @param[in] Row The row number to be cleared ( start from 1 )
369 @param[in] LastCol The last printable column.
370 @param[in] LastRow The last printable row.
387 // prepare a blank line
389 SetMem16(Line
, LastCol
*sizeof(CHAR16
), L
' ');
391 if (Row
== LastRow
) {
393 // if CHAR_NULL is still at position 80, it will cause first line error
395 Line
[LastCol
- 1] = CHAR_NULL
;
397 Line
[LastCol
] = CHAR_NULL
;
401 // print out the blank line
403 ShellPrintEx (0, ((INT32
)Row
) - 1, Line
);
407 Determine if the character is valid for a filename.
409 @param[in] Ch The character to test.
411 @retval TRUE The character is valid.
412 @retval FALSE The character is not valid.
416 IsValidFileNameChar (
421 // See if there are any illegal characters within the name
423 if (Ch
< 0x20 || Ch
== L
'\"' || Ch
== L
'*' || Ch
== L
'/' || Ch
== L
'<' || Ch
== L
'>' || Ch
== L
'?' || Ch
== L
'|') {
431 Check if file name has illegal characters.
433 @param Name The filename to check.
435 @retval TRUE The filename is ok.
436 @retval FALSE The filename is not ok.
441 IN CONST CHAR16
*Name
449 // check the length of Name
451 for (Len
= 0, Index
= StrLen (Name
) - 1; Index
+ 1 != 0; Index
--, Len
++) {
452 if (Name
[Index
] == '\\' || Name
[Index
] == ':') {
457 if (Len
== 0 || Len
> 255) {
461 // check whether any char in Name not appears in valid file name char
463 for (Index
= 0; Index
< StrLen (Name
); Index
++) {
464 if (!IsValidFileNameChar (Name
[Index
])) {
473 Find a filename that is valid (not taken) with the given extension.
475 @param[in] Extension The file extension.
477 @retval NULL Something went wrong.
478 @return the valid filename.
482 EditGetDefaultFileName (
483 IN CONST CHAR16
*Extension
493 FileNameTmp
= CatSPrint (NULL
, L
"NewFile%d.%s", Suffix
, Extension
);
496 // after that filename changed to path
498 Status
= ShellFileExists (FileNameTmp
);
500 if (Status
== EFI_NOT_FOUND
) {
504 FreePool (FileNameTmp
);
507 } while (Suffix
!= 0);
509 FreePool (FileNameTmp
);
514 Read a file into an allocated buffer. The buffer is the responsibility
515 of the caller to free.
517 @param[in] FileName The filename of the file to open.
518 @param[out] Buffer Upon successful return, the pointer to the
519 address of the allocated buffer.
520 @param[out] BufferSize If not NULL, then the pointer to the size
521 of the allocated buffer.
522 @param[out] ReadOnly Upon successful return TRUE if the file is
523 read only. FALSE otherwise.
525 @retval EFI_NOT_FOUND The filename did not represent a file in the
527 @retval EFI_SUCCESS The file was read into the buffer.
528 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
529 @retval EFI_LOAD_ERROR The file read operation failed.
530 @retval EFI_INVALID_PARAMETER A parameter was invalid.
531 @retval EFI_INVALID_PARAMETER FileName was NULL.
532 @retval EFI_INVALID_PARAMETER FileName was a directory.
537 IN CONST CHAR16
*FileName
,
539 OUT UINTN
*BufferSize OPTIONAL
,
540 OUT BOOLEAN
*ReadOnly
543 VOID
*InternalBuffer
;
545 SHELL_FILE_HANDLE FileHandle
;
550 InternalBuffer
= NULL
;
554 Status
= EFI_SUCCESS
;
557 if (FileName
== NULL
|| Buffer
== NULL
|| ReadOnly
== NULL
) {
558 return (EFI_INVALID_PARAMETER
);
562 // try to open the file
564 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
566 if (!EFI_ERROR(Status
)) {
567 ASSERT(CreateFile
== FALSE
);
568 if (FileHandle
== NULL
) {
569 return EFI_LOAD_ERROR
;
572 Info
= ShellGetFileInfo(FileHandle
);
574 if (Info
->Attribute
& EFI_FILE_DIRECTORY
) {
576 return EFI_INVALID_PARAMETER
;
579 if (Info
->Attribute
& EFI_FILE_READ_ONLY
) {
587 FileSize
= (UINTN
) Info
->FileSize
;
590 } else if (Status
== EFI_NOT_FOUND
) {
592 // file not exists. add create and try again
594 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
595 if (EFI_ERROR (Status
)) {
599 // it worked. now delete it and move on with the name (now validated)
601 Status
= ShellDeleteFile (&FileHandle
);
602 if (Status
== EFI_WARN_DELETE_FAILURE
) {
603 Status
= EFI_ACCESS_DENIED
;
605 if (EFI_ERROR (Status
)) {
610 // file doesn't exist, so set CreateFile to TRUE and can't be read-only
621 // allocate buffer to read file
623 InternalBuffer
= AllocateZeroPool (FileSize
);
624 if (InternalBuffer
== NULL
) {
625 return EFI_OUT_OF_RESOURCES
;
628 // read file into InternalBuffer
630 Status
= ShellReadFile (FileHandle
, &FileSize
, InternalBuffer
);
631 ShellCloseFile(&FileHandle
);
633 if (EFI_ERROR (Status
)) {
634 SHELL_FREE_NON_NULL (InternalBuffer
);
635 return EFI_LOAD_ERROR
;
638 *Buffer
= InternalBuffer
;
639 if (BufferSize
!= NULL
) {
640 *BufferSize
= FileSize
;
642 return (EFI_SUCCESS
);