EDK2 doxygen online documents - Firmware Encoding Index 1
EDK2 doxygen online documents - Firmware Encoding Index

ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.c

Go to the documentation of this file.
00001 
00015 #include "UefiShellDebug1CommandsLib.h"
00016 
00017 STATIC CONST CHAR16 mFileName[] = L"Debug1Commands";
00018 EFI_HANDLE gShellDebug1HiiHandle = NULL;
00019 
00026 CONST CHAR16*
00027 EFIAPI
00028 ShellCommandGetManFileNameDebug1 (
00029   VOID
00030   )
00031 {
00032   return (mFileName);
00033 }
00034 
00044 EFI_STATUS
00045 EFIAPI
00046 UefiShellDebug1CommandsLibConstructor (
00047   IN EFI_HANDLE        ImageHandle,
00048   IN EFI_SYSTEM_TABLE  *SystemTable
00049   )
00050 {
00051   //
00052   // check our bit of the profiles mask
00053   //
00054   if ((PcdGet8(PcdShellProfileMask) & BIT1) == 0) {
00055     return (EFI_SUCCESS);
00056   }
00057 
00058   //
00059   // install the HII stuff.
00060   //
00061   gShellDebug1HiiHandle = HiiAddPackages (&gShellDebug1HiiGuid, gImageHandle, UefiShellDebug1CommandsLibStrings, NULL);
00062   if (gShellDebug1HiiHandle == NULL) {
00063     return (EFI_DEVICE_ERROR);
00064   }
00065 
00066   //
00067   // install our shell command handlers that are always installed
00068   //
00069   ShellCommandRegisterCommandName(L"setsize",       ShellCommandRunSetSize            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETSIZE)      );
00070   ShellCommandRegisterCommandName(L"comp",          ShellCommandRunComp               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_COMP)         );
00071   ShellCommandRegisterCommandName(L"mode",          ShellCommandRunMode               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MODE)         );
00072   ShellCommandRegisterCommandName(L"memmap",        ShellCommandRunMemMap             , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MEMMAP)       );
00073   ShellCommandRegisterCommandName(L"eficompress",   ShellCommandRunEfiCompress        , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EFICOMPRESS)  );
00074   ShellCommandRegisterCommandName(L"efidecompress", ShellCommandRunEfiDecompress      , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EFIDCOMPRESS) );
00075   ShellCommandRegisterCommandName(L"dmem",          ShellCommandRunDmem               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DMEM)         );
00076   ShellCommandRegisterCommandName(L"loadpcirom",    ShellCommandRunLoadPciRom         , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_LOAD_PCI_ROM) );
00077   ShellCommandRegisterCommandName(L"mm",            ShellCommandRunMm                 , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MM)           );
00078   ShellCommandRegisterCommandName(L"setvar",        ShellCommandRunSetVar             , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETVAR)       );
00079   ShellCommandRegisterCommandName(L"sermode",       ShellCommandRunSerMode            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SERMODE)      );
00080   ShellCommandRegisterCommandName(L"pci",           ShellCommandRunPci                , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_PCI)          );
00081   ShellCommandRegisterCommandName(L"smbiosview",    ShellCommandRunSmbiosView         , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SMBIOSVIEW)   );
00082   ShellCommandRegisterCommandName(L"dmpstore",      ShellCommandRunDmpStore           , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DMPSTORE)     );
00083   ShellCommandRegisterCommandName(L"dblk",          ShellCommandRunDblk               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DBLK)         );
00084   ShellCommandRegisterCommandName(L"edit",          ShellCommandRunEdit               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EDIT)         );
00085   ShellCommandRegisterCommandName(L"hexedit",       ShellCommandRunHexEdit            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_HEXEDIT)      );
00086 
00087   //
00088   // check install profile bit of the profiles mask is set
00089   //
00090   if ((PcdGet8(PcdShellProfileMask) & BIT2) == 0) {
00091     ShellCommandRegisterCommandName(L"bcfg",        ShellCommandRunBcfg               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_BCFG)         );
00092   }
00093 
00094 
00095 
00096 
00097   ShellCommandRegisterAlias(L"dmem", L"mem");
00098 
00099   return (EFI_SUCCESS);
00100 }
00101 
00108 EFI_STATUS
00109 EFIAPI
00110 UefiShellDebug1CommandsLibDestructor (
00111   IN EFI_HANDLE        ImageHandle,
00112   IN EFI_SYSTEM_TABLE  *SystemTable
00113   )
00114 {
00115   if (gShellDebug1HiiHandle != NULL) {
00116     HiiRemovePackages(gShellDebug1HiiHandle);
00117   }
00118   return (EFI_SUCCESS);
00119 }
00120 
00121 STATIC CONST CHAR8 Hex[] = {
00122   '0',
00123   '1',
00124   '2',
00125   '3',
00126   '4',
00127   '5',
00128   '6',
00129   '7',
00130   '8',
00131   '9',
00132   'A',
00133   'B',
00134   'C',
00135   'D',
00136   'E',
00137   'F'
00138 };
00139 
00148 VOID
00149 DumpHex (
00150   IN UINTN        Indent,
00151   IN UINTN        Offset,
00152   IN UINTN        DataSize,
00153   IN VOID         *UserData
00154   )
00155 {
00156   UINT8 *Data;
00157 
00158   CHAR8 Val[50];
00159 
00160   CHAR8 Str[20];
00161 
00162   UINT8 TempByte;
00163   UINTN Size;
00164   UINTN Index;
00165 
00166   Data = UserData;
00167   while (DataSize != 0) {
00168     Size = 16;
00169     if (Size > DataSize) {
00170       Size = DataSize;
00171     }
00172 
00173     for (Index = 0; Index < Size; Index += 1) {
00174       TempByte            = Data[Index];
00175       Val[Index * 3 + 0]  = Hex[TempByte >> 4];
00176       Val[Index * 3 + 1]  = Hex[TempByte & 0xF];
00177       Val[Index * 3 + 2]  = (CHAR8) ((Index == 7) ? '-' : ' ');
00178       Str[Index]          = (CHAR8) ((TempByte < ' ' || TempByte > 'z') ? '.' : TempByte);
00179     }
00180 
00181     Val[Index * 3]  = 0;
00182     Str[Index]      = 0;
00183     ShellPrintEx(-1, -1, L"%*a%02X: %-.48a *%a*\r\n", Indent, "", Offset, Val, Str);
00184 
00185     Data += Size;
00186     Offset += Size;
00187     DataSize -= Size;
00188   }
00189 }
00190 
00208 CHAR16
00209 EFIAPI
00210 CharToUpper (
00211   IN      CHAR16                    Char
00212   )
00213 {
00214   if (Char >= L'a' && Char <= L'z') {
00215     return (CHAR16) (Char - (L'a' - L'A'));
00216   }
00217 
00218   return Char;
00219 }
00220 
00231 EFI_STATUS
00232 EFIAPI
00233 GetSystemConfigurationTable (
00234   IN EFI_GUID *TableGuid,
00235   IN OUT VOID **Table
00236   )
00237 {
00238   UINTN Index;
00239   ASSERT (Table != NULL);
00240 
00241   for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {
00242     if (CompareGuid (TableGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {
00243       *Table = gST->ConfigurationTable[Index].VendorTable;
00244       return EFI_SUCCESS;
00245     }
00246   }
00247 
00248   return EFI_NOT_FOUND;
00249 }
00250 
00264 UINTN
00265 EFIAPI
00266 HexCharToUintn (
00267   IN      CHAR16                    Char
00268   )
00269 {
00270   if (Char >= L'0' && Char <= L'9') {
00271     return Char - L'0';
00272   }
00273 
00274   return (UINTN) (10 + CharToUpper (Char) - L'A');
00275 }
00276 
00286 EFI_STATUS
00287 EFIAPI
00288 ConvertStringToGuid (
00289   IN CONST CHAR16 *StringGuid,
00290   IN OUT EFI_GUID *Guid
00291   )
00292 {
00293   CHAR16  *TempCopy;
00294   CHAR16  *TempSpot;
00295   CHAR16  *Walker;
00296   UINT64  TempVal;
00297   EFI_STATUS Status;
00298 
00299   if (StringGuid == NULL) {
00300     return (EFI_INVALID_PARAMETER);
00301   } else if (StrLen(StringGuid) != 36) {
00302     return (EFI_INVALID_PARAMETER);
00303   } 
00304   TempCopy = NULL;
00305   TempCopy = StrnCatGrow(&TempCopy, NULL, StringGuid, 0);
00306   if (TempCopy == NULL) {
00307     return (EFI_OUT_OF_RESOURCES);
00308   }
00309   Walker   = TempCopy;
00310   TempSpot = StrStr(Walker, L"-");
00311   if (TempSpot != NULL) {
00312     *TempSpot = CHAR_NULL;
00313   }
00314   Status = ShellConvertStringToUint64(Walker, &TempVal, TRUE, FALSE);
00315   if (EFI_ERROR(Status)) {
00316     FreePool(TempCopy);
00317     return (Status);
00318   }
00319   Guid->Data1 = (UINT32)TempVal;
00320   Walker += 9;
00321   TempSpot = StrStr(Walker, L"-");
00322   if (TempSpot != NULL) {
00323     *TempSpot = CHAR_NULL;
00324   }
00325   Status = ShellConvertStringToUint64(Walker, &TempVal, TRUE, FALSE);
00326   if (EFI_ERROR(Status)) {
00327     FreePool(TempCopy);
00328     return (Status);
00329   }
00330   Guid->Data2 = (UINT16)TempVal;
00331   Walker += 5;
00332   TempSpot = StrStr(Walker, L"-");
00333   if (TempSpot != NULL) {
00334     *TempSpot = CHAR_NULL;
00335   }
00336   Status = ShellConvertStringToUint64(Walker, &TempVal, TRUE, FALSE);
00337   if (EFI_ERROR(Status)) {
00338     FreePool(TempCopy);
00339     return (Status);
00340   }
00341   Guid->Data3 = (UINT16)TempVal;
00342   Walker += 5;
00343   Guid->Data4[0] = (UINT8)(HexCharToUintn(Walker[0]) * 16);
00344   Guid->Data4[0] = (UINT8)(Guid->Data4[0]+ (UINT8)HexCharToUintn(Walker[1]));
00345   Walker += 2;
00346   Guid->Data4[1] = (UINT8)(HexCharToUintn(Walker[0]) * 16);
00347   Guid->Data4[1] = (UINT8)(Guid->Data4[1] + (UINT8)HexCharToUintn(Walker[1]));
00348   Walker += 3;
00349   Guid->Data4[2] = (UINT8)(HexCharToUintn(Walker[0]) * 16);
00350   Guid->Data4[2] = (UINT8)(Guid->Data4[2] + (UINT8)HexCharToUintn(Walker[1]));
00351   Walker += 2;
00352   Guid->Data4[3] = (UINT8)(HexCharToUintn(Walker[0]) * 16);
00353   Guid->Data4[3] = (UINT8)(Guid->Data4[3] + (UINT8)HexCharToUintn(Walker[1]));
00354   Walker += 2;
00355   Guid->Data4[4] = (UINT8)(HexCharToUintn(Walker[0]) * 16);
00356   Guid->Data4[4] = (UINT8)(Guid->Data4[4] + (UINT8)HexCharToUintn(Walker[1]));
00357   Walker += 2;
00358   Guid->Data4[5] = (UINT8)(HexCharToUintn(Walker[0]) * 16);
00359   Guid->Data4[5] = (UINT8)(Guid->Data4[5] + (UINT8)HexCharToUintn(Walker[1]));
00360   Walker += 2;
00361   Guid->Data4[6] = (UINT8)(HexCharToUintn(Walker[0]) * 16);
00362   Guid->Data4[6] = (UINT8)(Guid->Data4[6] + (UINT8)HexCharToUintn(Walker[1]));
00363   Walker += 2;
00364   Guid->Data4[7] = (UINT8)(HexCharToUintn(Walker[0]) * 16);
00365   Guid->Data4[7] = (UINT8)(Guid->Data4[7] + (UINT8)HexCharToUintn(Walker[1]));
00366   FreePool(TempCopy);
00367   return (EFI_SUCCESS);
00368 }
00369 
00377 VOID
00378 EFIAPI
00379 EditorClearLine (
00380   IN UINTN Row,
00381   IN UINTN LastCol,
00382   IN UINTN LastRow
00383   )
00384 {
00385   CHAR16 Line[200];
00386 
00387   if (Row == 0) {
00388     Row = 1;
00389   }
00390 
00391   //
00392   // prepare a blank line
00393   //
00394   SetMem16(Line, LastCol*sizeof(CHAR16), L' ');
00395 
00396   if (Row == LastRow) {
00397     //
00398     // if CHAR_NULL is still at position 80, it will cause first line error
00399     //
00400     Line[LastCol - 1] = CHAR_NULL;
00401   } else {
00402     Line[LastCol] = CHAR_NULL;
00403   }
00404 
00405   //
00406   // print out the blank line
00407   //
00408   ShellPrintEx (0, ((INT32)Row) - 1, Line);
00409 }
00410 
00419 BOOLEAN
00420 EFIAPI
00421 IsValidFileNameChar (
00422   IN CONST CHAR16 Ch
00423   )
00424 {
00425   //
00426   // See if there are any illegal characters within the name
00427   //
00428   if (Ch < 0x20 || Ch == L'\"' || Ch == L'*' || Ch == L'/' || Ch == L'<' || Ch == L'>' || Ch == L'?' || Ch == L'|') {
00429     return FALSE;
00430   }
00431 
00432   return TRUE;
00433 }
00434 
00443 BOOLEAN
00444 EFIAPI
00445 IsValidFileName (
00446   IN CONST CHAR16 *Name
00447   )
00448 {
00449 
00450   UINTN Index;
00451   UINTN Len;
00452 
00453   //
00454   // check the length of Name
00455   //
00456   for (Len = 0, Index = StrLen (Name) - 1; Index + 1 != 0; Index--, Len++) {
00457     if (Name[Index] == '\\' || Name[Index] == ':') {
00458       break;
00459     }
00460   }
00461 
00462   if (Len == 0 || Len > 255) {
00463     return FALSE;
00464   }
00465   //
00466   // check whether any char in Name not appears in valid file name char
00467   //
00468   for (Index = 0; Index < StrLen (Name); Index++) {
00469     if (!IsValidFileNameChar (Name[Index])) {
00470       return FALSE;
00471     }
00472   }
00473 
00474   return TRUE;
00475 }
00476 
00485 CHAR16 *
00486 EFIAPI
00487 EditGetDefaultFileName (
00488   IN CONST CHAR16 *Extension
00489   )
00490 {
00491   EFI_STATUS         Status;
00492   UINTN              Suffix;
00493   BOOLEAN            FoundNewFile;
00494   CHAR16             *FileNameTmp;
00495 
00496   Suffix       = 0;
00497   FoundNewFile = FALSE;
00498 
00499   do {
00500     FileNameTmp = CatSPrint (NULL, L"NewFile%d.%s", Suffix, Extension);
00501 
00502     //
00503     // after that filename changed to path
00504     //
00505     Status = ShellFileExists (FileNameTmp);
00506 
00507     if (Status == EFI_NOT_FOUND) {
00508       return FileNameTmp;
00509     }
00510 
00511     FreePool (FileNameTmp);
00512     FileNameTmp = NULL;
00513     Suffix++;
00514   } while (Suffix != 0);
00515 
00516   FreePool (FileNameTmp);
00517   return NULL;
00518 }
00519 
00541 EFI_STATUS
00542 EFIAPI
00543 ReadFileIntoBuffer (
00544   IN CONST CHAR16 *FileName,
00545   OUT VOID        **Buffer,
00546   OUT UINTN       *BufferSize OPTIONAL,
00547   OUT BOOLEAN     *ReadOnly
00548   )
00549 {
00550   VOID              *InternalBuffer;
00551   UINTN             FileSize;
00552   SHELL_FILE_HANDLE FileHandle;
00553   BOOLEAN           CreateFile;
00554   EFI_STATUS        Status;
00555   EFI_FILE_INFO     *Info;
00556 
00557   InternalBuffer  = NULL;
00558   FileSize        = 0;
00559   FileHandle      = NULL;
00560   CreateFile      = FALSE;
00561   Status          = EFI_SUCCESS;
00562   Info            = NULL;
00563 
00564   if (FileName == NULL || Buffer == NULL || ReadOnly == NULL) {
00565     return (EFI_INVALID_PARAMETER);
00566   }
00567 
00568   //
00569   // try to open the file
00570   //
00571   Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ, 0);
00572 
00573   if (!EFI_ERROR(Status)) {
00574     ASSERT(CreateFile == FALSE);
00575     if (FileHandle == NULL) {
00576       return EFI_LOAD_ERROR;
00577     }
00578 
00579     Info = ShellGetFileInfo(FileHandle);
00580     
00581     if (Info->Attribute & EFI_FILE_DIRECTORY) {
00582       FreePool (Info);
00583       return EFI_INVALID_PARAMETER;
00584     }
00585 
00586     if (Info->Attribute & EFI_FILE_READ_ONLY) {
00587       *ReadOnly = TRUE;
00588     } else {
00589       *ReadOnly = FALSE;
00590     }
00591     //
00592     // get file size
00593     //
00594     FileSize = (UINTN) Info->FileSize;
00595 
00596     FreePool (Info);
00597   } else if (Status == EFI_NOT_FOUND) {
00598     //
00599     // file not exists.  add create and try again
00600     //
00601     Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);
00602     if (EFI_ERROR (Status)) {
00603       return Status;
00604     } else {
00605       //
00606       // it worked.  now delete it and move on with the name (now validated)
00607       //
00608       Status = ShellDeleteFile (&FileHandle);
00609       if (Status == EFI_WARN_DELETE_FAILURE) {
00610         Status = EFI_ACCESS_DENIED;
00611       }
00612       if (EFI_ERROR (Status)) {
00613         return Status;
00614       }
00615     }
00616     //
00617     // file doesn't exist, so set CreateFile to TRUE and can't be read-only
00618     //
00619     CreateFile = TRUE;
00620     *ReadOnly  = FALSE;
00621   }
00622 
00623   //
00624   // the file exists
00625   //
00626   if (!CreateFile) {
00627     //
00628     // allocate buffer to read file
00629     //
00630     InternalBuffer = AllocateZeroPool (FileSize);
00631     if (InternalBuffer == NULL) {
00632       return EFI_OUT_OF_RESOURCES;
00633     }
00634     //
00635     // read file into InternalBuffer
00636     //
00637     Status = ShellReadFile (FileHandle, &FileSize, InternalBuffer);
00638     ShellCloseFile(&FileHandle);
00639     FileHandle = NULL;
00640     if (EFI_ERROR (Status)) {
00641       SHELL_FREE_NON_NULL (InternalBuffer);
00642       return EFI_LOAD_ERROR;
00643     }
00644   }
00645   *Buffer = InternalBuffer;
00646   if (BufferSize != NULL) {
00647     *BufferSize = FileSize;
00648   }
00649   return (EFI_SUCCESS);
00650 
00651 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines