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

ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c

Go to the documentation of this file.
00001 
00015 #include "UefiShellDebug1CommandsLib.h"
00016 #include "HexEditor.h"
00017 
00018 //
00019 // Global Variables
00020 //
00021 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
00022   {L"-f", TypeFlag},
00023   {L"-d", TypeFlag},
00024   {L"-m", TypeFlag},
00025   {NULL, TypeMax}
00026   };
00027 
00034 SHELL_STATUS
00035 EFIAPI
00036 ShellCommandRunHexEdit (
00037   IN EFI_HANDLE        ImageHandle,
00038   IN EFI_SYSTEM_TABLE  *SystemTable
00039   )
00040 {
00041   EFI_STATUS              Status;
00042   CHAR16                  *Buffer;
00043   CHAR16                  *ProblemParam;
00044   SHELL_STATUS            ShellStatus;
00045   LIST_ENTRY              *Package;
00046   CONST CHAR16            *Cwd;
00047   CHAR16                  *NewName;
00048   CHAR16                  *Spot;
00049   CONST CHAR16            *Name;
00050   UINTN                   Offset;
00051   UINTN                   Size;
00052   UINT64                  LastOffset;
00053   EDIT_FILE_TYPE          WhatToDo;
00054 
00055   Buffer      = NULL;
00056   ShellStatus = SHELL_SUCCESS;
00057   NewName         = NULL;
00058   Cwd         = NULL;
00059   Buffer      = NULL;
00060   Name        = NULL;
00061   Spot        = NULL;
00062   Offset      = 0;
00063   Size        = 0;
00064   LastOffset  = 0;
00065   WhatToDo    = FileTypeNone;
00066 
00067   //
00068   // initialize the shell lib (we must be in non-auto-init...)
00069   //
00070   Status = ShellInitialize();
00071   ASSERT_EFI_ERROR(Status);
00072 
00073   Status = CommandInit();
00074   ASSERT_EFI_ERROR(Status);
00075 
00076   //
00077   // parse the command line
00078   //
00079   Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
00080   if (EFI_ERROR(Status)) {
00081     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
00082       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
00083       FreePool(ProblemParam);
00084       ShellStatus = SHELL_INVALID_PARAMETER;
00085     } else {
00086       ASSERT(FALSE);
00087     }
00088   } else {
00089     //
00090     // Check for -d
00091     //
00092     if (ShellCommandLineGetFlag(Package, L"-d")){
00093       if (ShellCommandLineGetCount(Package) < 4) {
00094         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
00095         ShellStatus = SHELL_INVALID_PARAMETER;
00096       } else if (ShellCommandLineGetCount(Package) > 4) {
00097         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
00098         ShellStatus = SHELL_INVALID_PARAMETER;
00099       } else {
00100         WhatToDo = FileTypeDiskBuffer;
00101         Name    = ShellCommandLineGetRawValue(Package, 1);
00102         Offset  = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2));
00103         Size    = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 3));
00104       }
00105       if (Offset == (UINTN)-1 || Size == (UINTN)-1) {
00106         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, L"-d");
00107         ShellStatus = SHELL_INVALID_PARAMETER;
00108       }
00109     }
00110 
00111     //
00112     // check for -f
00113     //
00114     if (ShellCommandLineGetFlag(Package, L"-f") && (WhatToDo == FileTypeNone)){
00115       if (ShellCommandLineGetCount(Package) < 2) {
00116         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
00117         ShellStatus = SHELL_INVALID_PARAMETER;
00118       } else if (ShellCommandLineGetCount(Package) > 2) {
00119         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
00120         ShellStatus = SHELL_INVALID_PARAMETER;
00121       } else {
00122         Name      = ShellCommandLineGetRawValue(Package, 1);
00123         if (Name == NULL || !IsValidFileName(Name)) {
00124           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Name);
00125           ShellStatus = SHELL_INVALID_PARAMETER;
00126         } else {
00127           WhatToDo  = FileTypeFileBuffer;
00128         }
00129       }
00130     }
00131 
00132     //
00133     // check for -m
00134     //
00135     if (ShellCommandLineGetFlag(Package, L"-m") && (WhatToDo == FileTypeNone)){
00136       if (ShellCommandLineGetCount(Package) < 3) {
00137         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
00138         ShellStatus = SHELL_INVALID_PARAMETER;
00139       } else if (ShellCommandLineGetCount(Package) > 3) {
00140         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
00141         ShellStatus = SHELL_INVALID_PARAMETER;
00142       } else {
00143         WhatToDo = FileTypeMemBuffer;
00144         Offset  = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 1));
00145         Size    = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2));
00146       }
00147     }
00148     Name = ShellCommandLineGetRawValue(Package, 1);
00149     if (WhatToDo == FileTypeNone && Name != NULL) {
00150       if (ShellCommandLineGetCount(Package) > 2) {
00151         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
00152         ShellStatus = SHELL_INVALID_PARAMETER;
00153       } else if (!IsValidFileName(Name)) {
00154         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Name);
00155         ShellStatus = SHELL_INVALID_PARAMETER;
00156       } else {
00157         WhatToDo  = FileTypeFileBuffer;
00158       }
00159     } else if (WhatToDo == FileTypeNone) {
00160       if (gEfiShellProtocol->GetCurDir(NULL) == NULL) {
00161         ShellStatus = SHELL_NOT_FOUND;
00162         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellDebug1HiiHandle);
00163       } else {
00164         NewName = EditGetDefaultFileName(L"bin");
00165         Name = NewName;
00166         WhatToDo  = FileTypeFileBuffer;
00167       }
00168     }
00169 
00170     if (ShellStatus == SHELL_SUCCESS && WhatToDo == FileTypeNone) {
00171       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
00172       ShellStatus = SHELL_INVALID_PARAMETER;
00173     } else if (WhatToDo == FileTypeFileBuffer && ShellGetCurrentDir(NULL) == NULL) {
00174       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellDebug1HiiHandle);
00175       ShellStatus = SHELL_INVALID_PARAMETER;
00176     }
00177 
00178     if (ShellStatus == SHELL_SUCCESS) {
00179       //
00180       // Do the editor
00181       //
00182       Status = HMainEditorInit ();
00183       if (EFI_ERROR (Status)) {
00184         gST->ConOut->ClearScreen (gST->ConOut);
00185         gST->ConOut->EnableCursor (gST->ConOut, TRUE);
00186         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_INIT_FAILED), gShellDebug1HiiHandle);
00187       } else {
00188         HMainEditorBackup ();
00189         switch (WhatToDo) {
00190         case FileTypeFileBuffer:
00191           Status = HBufferImageRead (
00192                     Name==NULL?L"":Name,
00193                     NULL,
00194                     0,
00195                     0,
00196                     0,
00197                     0,
00198                     FileTypeFileBuffer,
00199                     FALSE
00200                     );
00201           break;
00202 
00203         case FileTypeDiskBuffer:
00204           Status = HBufferImageRead (
00205                     NULL,
00206                     Name==NULL?L"":Name,
00207                     Offset,
00208                     Size,
00209                     0,
00210                     0,
00211                     FileTypeDiskBuffer,
00212                     FALSE
00213                     );
00214           break;
00215 
00216         case FileTypeMemBuffer:
00217           Status = HBufferImageRead (
00218                     NULL,
00219                     NULL,
00220                     0,
00221                     0,
00222                     (UINT32) Offset,
00223                     Size,
00224                     FileTypeMemBuffer,
00225                     FALSE
00226                     );
00227           break;
00228 
00229         default:
00230           Status = EFI_NOT_FOUND;
00231           break;
00232         }
00233         if (!EFI_ERROR (Status)) {
00234           HMainEditorRefresh ();
00235           Status = HMainEditorKeyInput ();
00236         }
00237         if (Status != EFI_OUT_OF_RESOURCES) {
00238           //
00239           // back up the status string
00240           //
00241           Buffer = CatSPrint (NULL, L"%s\r\n", StatusBarGetString());
00242         }
00243       }
00244 
00245       //
00246       // cleanup
00247       //
00248       HMainEditorCleanup ();
00249 
00250       if (!EFI_ERROR (Status)) {
00251         if (ShellStatus == SHELL_SUCCESS) {
00252           ShellStatus = SHELL_UNSUPPORTED;
00253         }
00254       }
00255 
00256       //
00257       // print editor exit code on screen
00258       //
00259       if (Status == EFI_OUT_OF_RESOURCES) {
00260         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
00261       } else if (EFI_ERROR(Status)){
00262         if (Buffer != NULL) {
00263           if (StrCmp (Buffer, L"") != 0) {
00264             //
00265             // print out the status string
00266             //
00267             ShellPrintEx(-1, -1, L"%s", Buffer);
00268           } else {
00269             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
00270           }
00271         } else {
00272           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle);
00273         }
00274       }
00275     }
00276     ShellCommandLineFreeVarList (Package);
00277   }
00278 
00279   SHELL_FREE_NON_NULL (Buffer);
00280   SHELL_FREE_NON_NULL (NewName);
00281   return ShellStatus;
00282 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines