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"
17 STATIC CONST CHAR16 mFileName
[] = L
"Debug1Commands";
18 EFI_HANDLE gShellDebug1HiiHandle
= NULL
;
21 Gets the debug file name. This will be used if HII is not working.
23 @retval NULL No file is available.
24 @return The NULL-terminated filename to get help from.
28 ShellCommandGetManFileNameDebug1 (
36 Constructor for the Shell Debug1 Commands library.
38 @param ImageHandle the image handle of the process
39 @param SystemTable the EFI System Table pointer
41 @retval EFI_SUCCESS the shell command handlers were installed sucessfully
42 @retval EFI_UNSUPPORTED the shell level required was not found.
46 UefiShellDebug1CommandsLibConstructor (
47 IN EFI_HANDLE ImageHandle
,
48 IN EFI_SYSTEM_TABLE
*SystemTable
52 // check our bit of the profiles mask
54 if ((PcdGet8(PcdShellProfileMask
) & BIT1
) == 0) {
59 // install the HII stuff.
61 gShellDebug1HiiHandle
= HiiAddPackages (&gShellDebug1HiiGuid
, gImageHandle
, UefiShellDebug1CommandsLibStrings
, NULL
);
62 if (gShellDebug1HiiHandle
== NULL
) {
63 return (EFI_DEVICE_ERROR
);
67 // install our shell command handlers that are always installed
69 ShellCommandRegisterCommandName(L
"setsize", ShellCommandRunSetSize
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SETSIZE
) );
70 ShellCommandRegisterCommandName(L
"comp", ShellCommandRunComp
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_COMP
) );
71 ShellCommandRegisterCommandName(L
"mode", ShellCommandRunMode
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MODE
) );
72 ShellCommandRegisterCommandName(L
"memmap", ShellCommandRunMemMap
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MEMMAP
) );
73 ShellCommandRegisterCommandName(L
"eficompress", ShellCommandRunEfiCompress
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EFICOMPRESS
) );
74 ShellCommandRegisterCommandName(L
"efidecompress", ShellCommandRunEfiDecompress
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EFIDCOMPRESS
) );
75 ShellCommandRegisterCommandName(L
"dmem", ShellCommandRunDmem
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DMEM
) );
76 ShellCommandRegisterCommandName(L
"loadpcirom", ShellCommandRunLoadPciRom
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_LOAD_PCI_ROM
) );
77 ShellCommandRegisterCommandName(L
"mm", ShellCommandRunMm
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_MM
) );
78 ShellCommandRegisterCommandName(L
"setvar", ShellCommandRunSetVar
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SETVAR
) );
79 ShellCommandRegisterCommandName(L
"sermode", ShellCommandRunSerMode
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SERMODE
) );
80 ShellCommandRegisterCommandName(L
"pci", ShellCommandRunPci
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_PCI
) );
81 ShellCommandRegisterCommandName(L
"smbiosview", ShellCommandRunSmbiosView
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_SMBIOSVIEW
) );
82 ShellCommandRegisterCommandName(L
"dmpstore", ShellCommandRunDmpStore
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DMPSTORE
) );
83 ShellCommandRegisterCommandName(L
"dblk", ShellCommandRunDblk
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_DBLK
) );
84 ShellCommandRegisterCommandName(L
"edit", ShellCommandRunEdit
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_EDIT
) );
85 ShellCommandRegisterCommandName(L
"hexedit", ShellCommandRunHexEdit
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_HEXEDIT
) );
88 // check install profile bit of the profiles mask is set
90 if ((PcdGet8(PcdShellProfileMask
) & BIT2
) == 0) {
91 ShellCommandRegisterCommandName(L
"bcfg", ShellCommandRunBcfg
, ShellCommandGetManFileNameDebug1
, 0, L
"Debug1", TRUE
, gShellDebug1HiiHandle
, STRING_TOKEN(STR_GET_HELP_BCFG
) );
97 ShellCommandRegisterAlias(L
"dmem", L
"mem");
103 Destructor for the library. free any resources.
105 @param ImageHandle The image handle of the process.
106 @param SystemTable The EFI System Table pointer.
110 UefiShellDebug1CommandsLibDestructor (
111 IN EFI_HANDLE ImageHandle
,
112 IN EFI_SYSTEM_TABLE
*SystemTable
115 if (gShellDebug1HiiHandle
!= NULL
) {
116 HiiRemovePackages(gShellDebug1HiiHandle
);
118 return (EFI_SUCCESS
);
121 STATIC CONST CHAR8 Hex
[] = {
141 Dump some hexadecimal data to the screen.
143 @param[in] Indent How many spaces to indent the output.
144 @param[in] Offset The offset of the printing.
145 @param[in] DataSize The size in bytes of UserData.
146 @param[in] UserData The data to print out.
167 while (DataSize
!= 0) {
169 if (Size
> DataSize
) {
173 for (Index
= 0; Index
< Size
; Index
+= 1) {
174 TempByte
= Data
[Index
];
175 Val
[Index
* 3 + 0] = Hex
[TempByte
>> 4];
176 Val
[Index
* 3 + 1] = Hex
[TempByte
& 0xF];
177 Val
[Index
* 3 + 2] = (CHAR8
) ((Index
== 7) ? '-' : ' ');
178 Str
[Index
] = (CHAR8
) ((TempByte
< ' ' || TempByte
> 'z') ? '.' : TempByte
);
183 ShellPrintEx(-1, -1, L
"%*a%02X: %-.48a *%a*\r\n", Indent
, "", Offset
, Val
, Str
);
192 Convert a Unicode character to upper case only if
193 it maps to a valid small-case ASCII character.
195 This internal function only deal with Unicode character
196 which maps to a valid small-case ASCII character, i.e.
197 L'a' to L'z'. For other Unicode character, the input character
198 is returned directly.
200 @param Char The character to convert.
202 @retval LowerCharacter If the Char is with range L'a' to L'z'.
203 @retval Unchanged Otherwise.
206 //Stolen from MdePkg Baselib
214 if (Char
>= L
'a' && Char
<= L
'z') {
215 return (CHAR16
) (Char
- (L
'a' - L
'A'));
222 Function returns a system configuration table that is stored in the
223 EFI System Table based on the provided GUID.
225 @param[in] TableGuid A pointer to the table's GUID type.
226 @param[in, out] Table On exit, a pointer to a system configuration table.
228 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
229 @retval EFI_NOT_FOUND A configuration table matching TableGuid was not found.
233 GetSystemConfigurationTable (
234 IN EFI_GUID
*TableGuid
,
239 ASSERT (Table
!= NULL
);
241 for (Index
= 0; Index
< gST
->NumberOfTableEntries
; Index
++) {
242 if (CompareGuid (TableGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
243 *Table
= gST
->ConfigurationTable
[Index
].VendorTable
;
248 return EFI_NOT_FOUND
;
252 Convert a Unicode character to numerical value.
254 This internal function only deal with Unicode character
255 which maps to a valid hexadecimal ASII character, i.e.
256 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
257 Unicode character, the value returned does not make sense.
259 @param Char The character to convert.
261 @return The numerical value converted.
270 if (Char
>= L
'0' && Char
<= L
'9') {
274 return (UINTN
) (10 + CharToUpper (Char
) - L
'A');
278 Convert a string representation of a guid to a Guid value.
280 @param[in] StringGuid The pointer to the string of a guid.
281 @param[in, out] Guid The pointer to the GUID structure to populate.
283 @retval EFI_INVALID_PARAMETER A parameter was invalid.
284 @retval EFI_SUCCESS The conversion was successful.
288 ConvertStringToGuid (
289 IN CONST CHAR16
*StringGuid
,
290 IN OUT EFI_GUID
*Guid
299 if (StringGuid
== NULL
) {
300 return (EFI_INVALID_PARAMETER
);
301 } else if (StrLen(StringGuid
) != 36) {
302 return (EFI_INVALID_PARAMETER
);
305 TempCopy
= StrnCatGrow(&TempCopy
, NULL
, StringGuid
, 0);
306 if (TempCopy
== NULL
) {
307 return (EFI_OUT_OF_RESOURCES
);
310 TempSpot
= StrStr(Walker
, L
"-");
311 if (TempSpot
!= NULL
) {
312 *TempSpot
= CHAR_NULL
;
314 Status
= ShellConvertStringToUint64(Walker
, &TempVal
, TRUE
, FALSE
);
315 if (EFI_ERROR(Status
)) {
319 Guid
->Data1
= (UINT32
)TempVal
;
321 TempSpot
= StrStr(Walker
, L
"-");
322 if (TempSpot
!= NULL
) {
323 *TempSpot
= CHAR_NULL
;
325 Status
= ShellConvertStringToUint64(Walker
, &TempVal
, TRUE
, FALSE
);
326 if (EFI_ERROR(Status
)) {
330 Guid
->Data2
= (UINT16
)TempVal
;
332 TempSpot
= StrStr(Walker
, L
"-");
333 if (TempSpot
!= NULL
) {
334 *TempSpot
= CHAR_NULL
;
336 Status
= ShellConvertStringToUint64(Walker
, &TempVal
, TRUE
, FALSE
);
337 if (EFI_ERROR(Status
)) {
341 Guid
->Data3
= (UINT16
)TempVal
;
343 Guid
->Data4
[0] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
344 Guid
->Data4
[0] = (UINT8
)(Guid
->Data4
[0]+ (UINT8
)HexCharToUintn(Walker
[1]));
346 Guid
->Data4
[1] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
347 Guid
->Data4
[1] = (UINT8
)(Guid
->Data4
[1] + (UINT8
)HexCharToUintn(Walker
[1]));
349 Guid
->Data4
[2] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
350 Guid
->Data4
[2] = (UINT8
)(Guid
->Data4
[2] + (UINT8
)HexCharToUintn(Walker
[1]));
352 Guid
->Data4
[3] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
353 Guid
->Data4
[3] = (UINT8
)(Guid
->Data4
[3] + (UINT8
)HexCharToUintn(Walker
[1]));
355 Guid
->Data4
[4] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
356 Guid
->Data4
[4] = (UINT8
)(Guid
->Data4
[4] + (UINT8
)HexCharToUintn(Walker
[1]));
358 Guid
->Data4
[5] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
359 Guid
->Data4
[5] = (UINT8
)(Guid
->Data4
[5] + (UINT8
)HexCharToUintn(Walker
[1]));
361 Guid
->Data4
[6] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
362 Guid
->Data4
[6] = (UINT8
)(Guid
->Data4
[6] + (UINT8
)HexCharToUintn(Walker
[1]));
364 Guid
->Data4
[7] = (UINT8
)(HexCharToUintn(Walker
[0]) * 16);
365 Guid
->Data4
[7] = (UINT8
)(Guid
->Data4
[7] + (UINT8
)HexCharToUintn(Walker
[1]));
367 return (EFI_SUCCESS
);
371 Clear the line at the specified Row.
373 @param[in] Row The row number to be cleared ( start from 1 )
374 @param[in] LastCol The last printable column.
375 @param[in] LastRow The last printable row.
392 // prepare a blank line
394 SetMem16(Line
, LastCol
*sizeof(CHAR16
), L
' ');
396 if (Row
== LastRow
) {
398 // if CHAR_NULL is still at position 80, it will cause first line error
400 Line
[LastCol
- 1] = CHAR_NULL
;
402 Line
[LastCol
] = CHAR_NULL
;
406 // print out the blank line
408 ShellPrintEx (0, ((INT32
)Row
) - 1, Line
);
412 Determine if the character is valid for a filename.
414 @param[in] Ch The character to test.
416 @retval TRUE The character is valid.
417 @retval FALSE The character is not valid.
421 IsValidFileNameChar (
426 // See if there are any illegal characters within the name
428 if (Ch
< 0x20 || Ch
== L
'\"' || Ch
== L
'*' || Ch
== L
'/' || Ch
== L
'<' || Ch
== L
'>' || Ch
== L
'?' || Ch
== L
'|') {
436 Check if file name has illegal characters.
438 @param Name The filename to check.
440 @retval TRUE The filename is ok.
441 @retval FALSE The filename is not ok.
446 IN CONST CHAR16
*Name
454 // check the length of Name
456 for (Len
= 0, Index
= StrLen (Name
) - 1; Index
+ 1 != 0; Index
--, Len
++) {
457 if (Name
[Index
] == '\\' || Name
[Index
] == ':') {
462 if (Len
== 0 || Len
> 255) {
466 // check whether any char in Name not appears in valid file name char
468 for (Index
= 0; Index
< StrLen (Name
); Index
++) {
469 if (!IsValidFileNameChar (Name
[Index
])) {
478 Find a filename that is valid (not taken) with the given extension.
480 @param[in] Extension The file extension.
482 @retval NULL Something went wrong.
483 @return the valid filename.
487 EditGetDefaultFileName (
488 IN CONST CHAR16
*Extension
493 BOOLEAN FoundNewFile
;
497 FoundNewFile
= FALSE
;
500 FileNameTmp
= CatSPrint (NULL
, L
"NewFile%d.%s", Suffix
, Extension
);
503 // after that filename changed to path
505 Status
= ShellFileExists (FileNameTmp
);
507 if (Status
== EFI_NOT_FOUND
) {
511 FreePool (FileNameTmp
);
514 } while (Suffix
!= 0);
516 FreePool (FileNameTmp
);
521 Read a file into an allocated buffer. The buffer is the responsibility
522 of the caller to free.
524 @param[in] FileName The filename of the file to open.
525 @param[out] Buffer Upon successful return, the pointer to the
526 address of the allocated buffer.
527 @param[out] BufferSize If not NULL, then the pointer to the size
528 of the allocated buffer.
529 @param[out] ReadOnly Upon successful return TRUE if the file is
530 read only. FALSE otherwise.
532 @retval EFI_NOT_FOUND The filename did not represent a file in the
534 @retval EFI_SUCCESS The file was read into the buffer.
535 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
536 @retval EFI_LOAD_ERROR The file read operation failed.
537 @retval EFI_INVALID_PARAMETER A parameter was invalid.
538 @retval EFI_INVALID_PARAMETER FileName was NULL.
539 @retval EFI_INVALID_PARAMETER FileName was a directory.
544 IN CONST CHAR16
*FileName
,
546 OUT UINTN
*BufferSize OPTIONAL
,
547 OUT BOOLEAN
*ReadOnly
550 VOID
*InternalBuffer
;
552 SHELL_FILE_HANDLE FileHandle
;
557 InternalBuffer
= NULL
;
561 Status
= EFI_SUCCESS
;
564 if (FileName
== NULL
|| Buffer
== NULL
|| ReadOnly
== NULL
) {
565 return (EFI_INVALID_PARAMETER
);
569 // try to open the file
571 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
573 if (!EFI_ERROR(Status
)) {
574 ASSERT(CreateFile
== FALSE
);
575 if (FileHandle
== NULL
) {
576 return EFI_LOAD_ERROR
;
579 Info
= ShellGetFileInfo(FileHandle
);
581 if (Info
->Attribute
& EFI_FILE_DIRECTORY
) {
583 return EFI_INVALID_PARAMETER
;
586 if (Info
->Attribute
& EFI_FILE_READ_ONLY
) {
594 FileSize
= (UINTN
) Info
->FileSize
;
597 } else if (Status
== EFI_NOT_FOUND
) {
599 // file not exists. add create and try again
601 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
602 if (EFI_ERROR (Status
)) {
606 // it worked. now delete it and move on with the name (now validated)
608 Status
= ShellDeleteFile (&FileHandle
);
609 if (Status
== EFI_WARN_DELETE_FAILURE
) {
610 Status
= EFI_ACCESS_DENIED
;
612 if (EFI_ERROR (Status
)) {
617 // file doesn't exist, so set CreateFile to TRUE and can't be read-only
628 // allocate buffer to read file
630 InternalBuffer
= AllocateZeroPool (FileSize
);
631 if (InternalBuffer
== NULL
) {
632 return EFI_OUT_OF_RESOURCES
;
635 // read file into InternalBuffer
637 Status
= ShellReadFile (FileHandle
, &FileSize
, InternalBuffer
);
638 ShellCloseFile(&FileHandle
);
640 if (EFI_ERROR (Status
)) {
641 SHELL_FREE_NON_NULL (InternalBuffer
);
642 return EFI_LOAD_ERROR
;
645 *Buffer
= InternalBuffer
;
646 if (BufferSize
!= NULL
) {
647 *BufferSize
= FileSize
;
649 return (EFI_SUCCESS
);