EFIAPI EditGetDefaultFileName ( IN CONST CHAR16 *Extension ) { EFI_STATUS Status; UINTN Suffix; CHAR16 *FileNameTmp; Suffix = 0; do { FileNameTmp = CatSPrint (NULL, L"NewFile%d.%s", Suffix, Extension); // // after that filename changed to path // Status = ShellFileExists (FileNameTmp); if (Status == EFI_NOT_FOUND) { return FileNameTmp; } FreePool (FileNameTmp); FileNameTmp = NULL; Suffix++; } while (Suffix != 0); FreePool (FileNameTmp); return NULL; }
/** Duplicate a EFI_EDITOR_LINE structure. @param Src The line structure to copy from. @retval NULL A memory allocation failed. @return a pointer to the newly allcoated line. **/ EFI_EDITOR_LINE * LineDup ( IN EFI_EDITOR_LINE *Src ) { EFI_EDITOR_LINE *Dest; // // allocate for the line structure // Dest = AllocateZeroPool (sizeof (EFI_EDITOR_LINE)); if (Dest == NULL) { return NULL; } // // allocate and set the line buffer // Dest->Buffer = CatSPrint (NULL, L"%s", Src->Buffer); if (Dest->Buffer == NULL) { FreePool (Dest); return NULL; } // // set the other structure members // Dest->Signature = LINE_LIST_SIGNATURE; Dest->Size = Src->Size; Dest->TotalSize = Dest->Size; Dest->Type = Src->Type; Dest->Link = Src->Link; return Dest; }
/** Initialize a title bar. @param[in] Prompt The prompt to print in the title bar. @retval EFI_SUCCESS The initialization was successful. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. **/ EFI_STATUS MainTitleBarInit ( CONST CHAR16 *Prompt ) { SHELL_FREE_NON_NULL (Title); if (Prompt == NULL) { Title = CatSPrint (NULL, L""); } else { // // set Title // Title = CatSPrint (NULL, L"%s", Prompt); } if (Title == NULL) { return EFI_OUT_OF_RESOURCES; } return EFI_SUCCESS; }
/** Backup function for HFileImage. Only a few fields need to be backup. This is for making the file buffer refresh as few as possible. @retval EFI_SUCCESS The operation was successful. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. **/ EFI_STATUS HFileImageBackup ( VOID ) { SHELL_FREE_NON_NULL (HFileImageBackupVar.FileName); HFileImageBackupVar.FileName = CatSPrint(NULL, L"%s", HFileImage.FileName); if (HFileImageBackupVar.FileName == NULL) { return EFI_OUT_OF_RESOURCES; } return EFI_SUCCESS; }
/** SetPrompt and wait for input. @param[in] Str The prompt string. @retval EFI_SUCCESS The operation was successful. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. **/ EFI_STATUS InputBarSetPrompt ( IN CONST CHAR16 *Str ) { // // FREE the old mPrompt string // SHELL_FREE_NON_NULL (mPrompt); mPrompt = CatSPrint (NULL, L"%s ", Str); if (mPrompt == NULL) { return EFI_OUT_OF_RESOURCES; } return EFI_SUCCESS; }
/** Set the status string text part. @param[in] Str The string to use. @retval EFI_SUCCESS The operation was successful. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. **/ EFI_STATUS StatusBarSetStatusString ( IN CHAR16 *Str ) { StatusStringChanged = TRUE; // // free the old status string // SHELL_FREE_NON_NULL (StatusString); StatusString = CatSPrint (NULL, L"%s", Str); if (StatusString == NULL) { return EFI_OUT_OF_RESOURCES; } return EFI_SUCCESS; }
/** Backup function for HDiskImage. Only a few fields need to be backup. This is for making the Disk buffer refresh as few as possible. @retval EFI_SUCCESS The operation was successful. @retval EFI_OUT_OF_RESOURCES gST->ConOut of resources. **/ EFI_STATUS HDiskImageBackup ( VOID ) { // // backup the disk name, offset and size // // SHELL_FREE_NON_NULL (HDiskImageBackupVar.Name); HDiskImageBackupVar.Name = CatSPrint(NULL, L"%s", HDiskImage.Name); if (HDiskImageBackupVar.Name == NULL) { return EFI_OUT_OF_RESOURCES; } HDiskImageBackupVar.Offset = HDiskImage.Offset; HDiskImageBackupVar.Size = HDiskImage.Size; return EFI_SUCCESS; }
/** Read a disk from disk into HBufferImage. @param[in] DeviceName filename to read. @param[in] Offset The offset. @param[in] Size The size. @param[in] Recover if is for recover, no information print. @retval EFI_SUCCESS The operation was successful. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. @retval EFI_LOAD_ERROR A load error occured. @retval EFI_INVALID_PARAMETER A parameter was invalid. **/ EFI_STATUS HDiskImageRead ( IN CONST CHAR16 *DeviceName, IN UINTN Offset, IN UINTN Size, IN BOOLEAN Recover ) { CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath; EFI_DEVICE_PATH_PROTOCOL *DupDevicePath; EFI_DEVICE_PATH_PROTOCOL *DupDevicePathForFree; EFI_HANDLE Handle; EFI_BLOCK_IO_PROTOCOL *BlkIo; EFI_STATUS Status; VOID *Buffer; CHAR16 *Str; UINTN Bytes; HEFI_EDITOR_LINE *Line; UINT64 ByteOffset; EDIT_FILE_TYPE BufferTypeBackup; BufferTypeBackup = HBufferImage.BufferType; HBufferImage.BufferType = FileTypeDiskBuffer; DevicePath = gEfiShellProtocol->GetDevicePathFromMap(DeviceName); if (DevicePath == NULL) { StatusBarSetStatusString (L"Cannot Find Device"); return EFI_INVALID_PARAMETER; } DupDevicePath = DuplicateDevicePath(DevicePath); DupDevicePathForFree = DupDevicePath; // // get blkio interface // Status = gBS->LocateDevicePath(&gEfiBlockIoProtocolGuid,&DupDevicePath,&Handle); FreePool(DupDevicePathForFree); if (EFI_ERROR (Status)) { StatusBarSetStatusString (L"Read Disk Failed"); return Status; } Status = gBS->OpenProtocol(Handle, &gEfiBlockIoProtocolGuid, (VOID**)&BlkIo, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (EFI_ERROR (Status)) { StatusBarSetStatusString (L"Read Disk Failed"); return Status; } // // if Offset exceeds LastBlock, // return error // if (Offset > BlkIo->Media->LastBlock || Offset + Size > BlkIo->Media->LastBlock) { StatusBarSetStatusString (L"Invalid Offset + Size"); return EFI_LOAD_ERROR; } Bytes = BlkIo->Media->BlockSize * Size; Buffer = AllocateZeroPool (Bytes); if (Buffer == NULL) { StatusBarSetStatusString (L"Read Disk Failed"); return EFI_OUT_OF_RESOURCES; } ByteOffset = MultU64x32 (Offset, BlkIo->Media->BlockSize); // // read from disk // Status = BlkIo->ReadBlocks ( BlkIo, BlkIo->Media->MediaId, Offset, Bytes, Buffer ); if (EFI_ERROR (Status)) { FreePool (Buffer); StatusBarSetStatusString (L"Read Disk Failed"); return EFI_LOAD_ERROR; } HBufferImageFree (); // // convert buffer to line list // Status = HBufferImageBufferToList (Buffer, Bytes); FreePool (Buffer); if (EFI_ERROR (Status)) { StatusBarSetStatusString (L"Read Disk Failed"); return Status; } Status = HDiskImageSetDiskNameOffsetSize (DeviceName, Offset, Size); if (EFI_ERROR (Status)) { StatusBarSetStatusString (L"Read Disk Failed"); return EFI_OUT_OF_RESOURCES; } // // initialize some variables // HDiskImage.BlockSize = BlkIo->Media->BlockSize; HBufferImage.DisplayPosition.Row = 2; HBufferImage.DisplayPosition.Column = 10; HBufferImage.MousePosition.Row = 2; HBufferImage.MousePosition.Column = 10; HBufferImage.LowVisibleRow = 1; HBufferImage.HighBits = TRUE; HBufferImage.BufferPosition.Row = 1; HBufferImage.BufferPosition.Column = 1; if (!Recover) { Str = CatSPrint(NULL, L"%d Lines Read", HBufferImage.NumLines); if (Str == NULL) { StatusBarSetStatusString (L"Read Disk Failed"); return EFI_OUT_OF_RESOURCES; } StatusBarSetStatusString (Str); SHELL_FREE_NON_NULL (Str); HMainEditor.SelectStart = 0; HMainEditor.SelectEnd = 0; } // // has line // if (HBufferImage.Lines != NULL) { HBufferImage.CurrentLine = CR ( HBufferImage.ListHead->ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST ); } else { // // create a dummy line // Line = HBufferImageCreateLine (); if (Line == NULL) { StatusBarSetStatusString (L"Read Disk Failed"); return EFI_OUT_OF_RESOURCES; } HBufferImage.CurrentLine = Line; } HBufferImage.Modified = FALSE; HBufferImageNeedRefresh = TRUE; HBufferImageOnlyLineNeedRefresh = FALSE; HBufferImageMouseNeedRefresh = TRUE; return EFI_SUCCESS; }
/** Function for 'hexedit' command. @param[in] ImageHandle Handle to the Image (NULL if Internal). @param[in] SystemTable Pointer to the System Table (NULL if Internal). **/ SHELL_STATUS EFIAPI ShellCommandRunHexEdit ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { EFI_STATUS Status; CHAR16 *Buffer; CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; LIST_ENTRY *Package; CHAR16 *NewName; CONST CHAR16 *Name; UINTN Offset; UINTN Size; EDIT_FILE_TYPE WhatToDo; Buffer = NULL; ShellStatus = SHELL_SUCCESS; NewName = NULL; Buffer = NULL; Name = NULL; Offset = 0; Size = 0; WhatToDo = FileTypeNone; // // initialize the shell lib (we must be in non-auto-init...) // Status = ShellInitialize(); // ASSERT_EFI_ERROR(Status); Status = CommandInit(); // ASSERT_EFI_ERROR(Status); if (EFI_ERROR(Status)) { return SHELL_UNSUPPORTED; } // // parse the command line // Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE); if (EFI_ERROR(Status)) { if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"hexedit", ProblemParam); FreePool(ProblemParam); ShellStatus = SHELL_INVALID_PARAMETER; } /* else { ASSERT(FALSE); } */ } else { // // Check for -d // if (ShellCommandLineGetFlag(Package, L"-d")){ if (ShellCommandLineGetCount(Package) < 4) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } else if (ShellCommandLineGetCount(Package) > 4) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } else { WhatToDo = FileTypeDiskBuffer; Name = ShellCommandLineGetRawValue(Package, 1); Offset = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2)); Size = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 3)); } if (Offset == (UINTN)-1 || Size == (UINTN)-1) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"hexedit", L"-d"); ShellStatus = SHELL_INVALID_PARAMETER; } } // // check for -f // if (ShellCommandLineGetFlag(Package, L"-f") && (WhatToDo == FileTypeNone)){ if (ShellCommandLineGetCount(Package) < 2) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } else if (ShellCommandLineGetCount(Package) > 2) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } else { Name = ShellCommandLineGetRawValue(Package, 1); if (Name == NULL || !IsValidFileName(Name)) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"hexedit", Name); ShellStatus = SHELL_INVALID_PARAMETER; } else { WhatToDo = FileTypeFileBuffer; } } } // // check for -m // if (ShellCommandLineGetFlag(Package, L"-m") && (WhatToDo == FileTypeNone)){ if (ShellCommandLineGetCount(Package) < 3) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } else if (ShellCommandLineGetCount(Package) > 3) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } else { WhatToDo = FileTypeMemBuffer; Offset = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 1)); Size = ShellStrToUintn(ShellCommandLineGetRawValue(Package, 2)); } } Name = ShellCommandLineGetRawValue(Package, 1); if (WhatToDo == FileTypeNone && Name != NULL) { if (ShellCommandLineGetCount(Package) > 2) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } else if (!IsValidFileName(Name)) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"hexedit", Name); ShellStatus = SHELL_INVALID_PARAMETER; } else { WhatToDo = FileTypeFileBuffer; } } else if (WhatToDo == FileTypeNone) { if (gEfiShellProtocol->GetCurDir(NULL) == NULL) { ShellStatus = SHELL_NOT_FOUND; ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellDebug1HiiHandle, L"hexedit"); } else { NewName = EditGetDefaultFileName(L"bin"); Name = NewName; WhatToDo = FileTypeFileBuffer; } } if (ShellStatus == SHELL_SUCCESS && WhatToDo == FileTypeNone) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } else if (WhatToDo == FileTypeFileBuffer && ShellGetCurrentDir(NULL) == NULL) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellDebug1HiiHandle, L"hexedit"); ShellStatus = SHELL_INVALID_PARAMETER; } if (ShellStatus == SHELL_SUCCESS) { // // Do the editor // Status = HMainEditorInit (); if (EFI_ERROR (Status)) { gST->ConOut->ClearScreen (gST->ConOut); gST->ConOut->EnableCursor (gST->ConOut, TRUE); ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_INIT_FAILED), gShellDebug1HiiHandle); } else { HMainEditorBackup (); switch (WhatToDo) { case FileTypeFileBuffer: Status = HBufferImageRead ( Name==NULL?L"":Name, NULL, 0, 0, 0, 0, FileTypeFileBuffer, FALSE ); break; case FileTypeDiskBuffer: Status = HBufferImageRead ( NULL, Name==NULL?L"":Name, Offset, Size, 0, 0, FileTypeDiskBuffer, FALSE ); break; case FileTypeMemBuffer: Status = HBufferImageRead ( NULL, NULL, 0, 0, (UINT32) Offset, Size, FileTypeMemBuffer, FALSE ); break; default: Status = EFI_NOT_FOUND; break; } if (!EFI_ERROR (Status)) { HMainEditorRefresh (); Status = HMainEditorKeyInput (); } if (Status != EFI_OUT_OF_RESOURCES) { // // back up the status string // Buffer = CatSPrint (NULL, L"%s\r\n", StatusBarGetString()); } } // // cleanup // HMainEditorCleanup (); if (EFI_ERROR (Status)) { if (ShellStatus == SHELL_SUCCESS) { ShellStatus = SHELL_UNSUPPORTED; } } // // print editor exit code on screen // if (Status == EFI_OUT_OF_RESOURCES) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"hexedit"); } else if (EFI_ERROR(Status)){ if (Buffer != NULL) { if (StrCmp (Buffer, L"") != 0) { // // print out the status string // ShellPrintEx(-1, -1, L"%s", Buffer); } else { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle); } } else { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_HEXEDIT_UNKNOWN_EDITOR), gShellDebug1HiiHandle); } } } ShellCommandLineFreeVarList (Package); } SHELL_FREE_NON_NULL (Buffer); SHELL_FREE_NON_NULL (NewName); return ShellStatus; }
/** Save lines in HBufferImage to disk. @param[in] FileName The file name. @retval EFI_SUCCESS The operation was successful. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. @retval EFI_LOAD_ERROR A load error occured. **/ EFI_STATUS HFileImageSave ( IN CHAR16 *FileName ) { LIST_ENTRY *Link; HEFI_EDITOR_LINE *Line; CHAR16 *Str; EFI_STATUS Status; UINTN NumLines; SHELL_FILE_HANDLE FileHandle; UINTN TotalSize; UINT8 *Buffer; UINT8 *Ptr; EDIT_FILE_TYPE BufferTypeBackup; BufferTypeBackup = HBufferImage.BufferType; HBufferImage.BufferType = FileTypeFileBuffer; // // if is the old file // if (HFileImage.FileName != NULL && FileName != NULL && StrCmp (FileName, HFileImage.FileName) == 0) { // // check whether file exists on disk // if (ShellIsFile(FileName) == EFI_SUCCESS) { // // current file exists on disk // so if not modified, then not save // if (HBufferImage.Modified == FALSE) { return EFI_SUCCESS; } // // if file is read-only, set error // if (HFileImage.ReadOnly == TRUE) { StatusBarSetStatusString (L"Read Only File Can Not Be Saved"); return EFI_SUCCESS; } } } if (ShellIsDirectory(FileName) == EFI_SUCCESS) { StatusBarSetStatusString (L"Directory Can Not Be Saved"); return EFI_LOAD_ERROR; } Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0); if (!EFI_ERROR (Status)) { // // the file exits, delete it // Status = ShellDeleteFile (&FileHandle); if (EFI_ERROR (Status) || Status == EFI_WARN_DELETE_FAILURE) { StatusBarSetStatusString (L"Write File Failed"); return EFI_LOAD_ERROR; } } // // write all the lines back to disk // NumLines = 0; TotalSize = 0; for (Link = HBufferImage.ListHead->ForwardLink; Link != HBufferImage.ListHead; Link = Link->ForwardLink) { Line = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST); if (Line->Size != 0) { TotalSize += Line->Size; } // // end of if Line -> Size != 0 // NumLines++; } // // end of for Link // Buffer = AllocateZeroPool (TotalSize); if (Buffer == NULL) { return EFI_OUT_OF_RESOURCES; } Ptr = Buffer; for (Link = HBufferImage.ListHead->ForwardLink; Link != HBufferImage.ListHead; Link = Link->ForwardLink) { Line = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST); if (Line->Size != 0) { CopyMem (Ptr, Line->Buffer, Line->Size); Ptr += Line->Size; } // // end of if Line -> Size != 0 // } Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0); if (EFI_ERROR (Status)) { StatusBarSetStatusString (L"Create File Failed"); return EFI_LOAD_ERROR; } Status = ShellWriteFile (FileHandle, &TotalSize, Buffer); FreePool (Buffer); if (EFI_ERROR (Status)) { ShellDeleteFile (&FileHandle); return EFI_LOAD_ERROR; } ShellCloseFile(&FileHandle); HBufferImage.Modified = FALSE; // // set status string // Str = CatSPrint(NULL, L"%d Lines Wrote", NumLines); StatusBarSetStatusString (Str); FreePool (Str); // // now everything is ready , you can set the new file name to filebuffer // if ((BufferTypeBackup != FileTypeFileBuffer && FileName != NULL) || (FileName != NULL && HFileImage.FileName != NULL && StringNoCaseCompare (&FileName, &HFileImage.FileName) != 0)){ // // not the same // HFileImageSetFileName (FileName); if (HFileImage.FileName == NULL) { return EFI_OUT_OF_RESOURCES; } } HFileImage.ReadOnly = FALSE; return EFI_SUCCESS; }
/** Read a file from disk into HBufferImage. @param[in] FileName filename to read. @param[in] Recover if is for recover, no information print. @retval EFI_SUCCESS The operation was successful. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. @retval EFI_LOAD_ERROR A load error occured. **/ EFI_STATUS HFileImageRead ( IN CONST CHAR16 *FileName, IN BOOLEAN Recover ) { HEFI_EDITOR_LINE *Line; UINT8 *Buffer; CHAR16 *UnicodeBuffer; EFI_STATUS Status; // // variable initialization // Line = NULL; // // in this function, when you return error ( except EFI_OUT_OF_RESOURCES ) // you should set status string // since this function maybe called before the editorhandleinput loop // so any error will cause editor return // so if you want to print the error status // you should set the status string // Status = ReadFileIntoBuffer (FileName, (VOID**)&Buffer, &HFileImage.Size, &HFileImage.ReadOnly); // // NULL pointer is only also a failure for a non-zero file size. // if ((EFI_ERROR(Status)) || (Buffer == NULL && HFileImage.Size != 0)) { UnicodeBuffer = CatSPrint(NULL, L"Read error on file &s: %r", FileName, Status); if (UnicodeBuffer == NULL) { SHELL_FREE_NON_NULL(Buffer); return EFI_OUT_OF_RESOURCES; } StatusBarSetStatusString (UnicodeBuffer); FreePool (UnicodeBuffer); return EFI_OUT_OF_RESOURCES; } HFileImageSetFileName (FileName); // // free the old lines // HBufferImageFree (); Status = HBufferImageBufferToList (Buffer, HFileImage.Size); SHELL_FREE_NON_NULL (Buffer); if (EFI_ERROR (Status)) { StatusBarSetStatusString (L"Error parsing file."); return Status; } HBufferImage.DisplayPosition.Row = 2; HBufferImage.DisplayPosition.Column = 10; HBufferImage.MousePosition.Row = 2; HBufferImage.MousePosition.Column = 10; HBufferImage.LowVisibleRow = 1; HBufferImage.HighBits = TRUE; HBufferImage.BufferPosition.Row = 1; HBufferImage.BufferPosition.Column = 1; HBufferImage.BufferType = FileTypeFileBuffer; if (!Recover) { UnicodeBuffer = CatSPrint(NULL, L"%d Lines Read", HBufferImage.NumLines); if (UnicodeBuffer == NULL) { SHELL_FREE_NON_NULL(Buffer); return EFI_OUT_OF_RESOURCES; } StatusBarSetStatusString (UnicodeBuffer); FreePool (UnicodeBuffer); HMainEditor.SelectStart = 0; HMainEditor.SelectEnd = 0; } // // has line // if (HBufferImage.Lines != 0) { HBufferImage.CurrentLine = CR (HBufferImage.ListHead->ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST); } else { // // create a dummy line // Line = HBufferImageCreateLine (); if (Line == NULL) { SHELL_FREE_NON_NULL(Buffer); return EFI_OUT_OF_RESOURCES; } HBufferImage.CurrentLine = Line; } HBufferImage.Modified = FALSE; HBufferImageNeedRefresh = TRUE; HBufferImageOnlyLineNeedRefresh = FALSE; HBufferImageMouseNeedRefresh = TRUE; return EFI_SUCCESS; }
/** Read a disk from disk into HBufferImage. @param[in] Offset The offset. @param[in] Size The size. @param[in] Recover if is for recover, no information print. @retval EFI_LOAD_ERROR A load error occured. @retval EFI_SUCCESS The operation was successful. @retval EFI_OUT_OF_RESOURCES A memory allocation failed. **/ EFI_STATUS HMemImageRead ( IN UINTN Offset, IN UINTN Size, IN BOOLEAN Recover ) { EFI_STATUS Status; void *Buffer; CHAR16 *Str; HEFI_EDITOR_LINE *Line; HBufferImage.BufferType = FileTypeMemBuffer; Buffer = AllocateZeroPool (Size); if (Buffer == NULL) { StatusBarSetStatusString (L"Read Memory Failed"); return EFI_OUT_OF_RESOURCES; } Status = HMemImage.IoFncs->Mem.Read ( HMemImage.IoFncs, EfiCpuIoWidthUint8, Offset, Size, Buffer ); if (EFI_ERROR (Status)) { FreePool (Buffer); StatusBarSetStatusString (L"Memory Specified Not Accessible"); return EFI_LOAD_ERROR; } HBufferImageFree (); Status = HBufferImageBufferToList (Buffer, Size); FreePool (Buffer); if (EFI_ERROR (Status)) { StatusBarSetStatusString (L"Read Memory Failed"); return Status; } Status = HMemImageSetMemOffsetSize (Offset, Size); HBufferImage.DisplayPosition.Row = 2; HBufferImage.DisplayPosition.Column = 10; HBufferImage.MousePosition.Row = 2; HBufferImage.MousePosition.Column = 10; HBufferImage.LowVisibleRow = 1; HBufferImage.HighBits = TRUE; HBufferImage.BufferPosition.Row = 1; HBufferImage.BufferPosition.Column = 1; if (!Recover) { Str = CatSPrint(NULL, L"%d Lines Read", HBufferImage.NumLines); if (Str == NULL) { StatusBarSetStatusString (L"Read Memory Failed"); return EFI_OUT_OF_RESOURCES; } StatusBarSetStatusString (Str); SHELL_FREE_NON_NULL (Str); HMainEditor.SelectStart = 0; HMainEditor.SelectEnd = 0; } // // has line // if (HBufferImage.Lines != NULL) { HBufferImage.CurrentLine = CR (HBufferImage.ListHead->ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST); } else { // // create a dummy line // Line = HBufferImageCreateLine (); if (Line == NULL) { StatusBarSetStatusString (L"Read Memory Failed"); return EFI_OUT_OF_RESOURCES; } HBufferImage.CurrentLine = Line; } HBufferImage.Modified = FALSE; HBufferImageNeedRefresh = TRUE; HBufferImageOnlyLineNeedRefresh = FALSE; HBufferImageMouseNeedRefresh = TRUE; return EFI_SUCCESS; }
/** Function for 'cd' command. @param[in] ImageHandle Handle to the Image (NULL if Internal). @param[in] SystemTable Pointer to the System Table (NULL if Internal). **/ SHELL_STATUS EFIAPI ShellCommandRunCd ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { EFI_STATUS Status; LIST_ENTRY *Package; CONST CHAR16 *Cwd; CHAR16 *Path; CHAR16 *Drive; CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; CONST CHAR16 *Param1; CHAR16 *Param1Copy; CHAR16 *Walker; CHAR16 *Splitter; CHAR16 *TempBuffer; UINTN TotalSize; ProblemParam = NULL; ShellStatus = SHELL_SUCCESS; Cwd = NULL; Path = NULL; Drive = NULL; Splitter = NULL; TempBuffer = NULL; TotalSize = 0; Status = CommandInit(); ASSERT_EFI_ERROR(Status); // // initialize the shell lib (we must be in non-auto-init...) // Status = ShellInitialize(); ASSERT_EFI_ERROR(Status); // // parse the command line // Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); if (EFI_ERROR(Status)) { if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"cd", ProblemParam); FreePool(ProblemParam); ShellStatus = SHELL_INVALID_PARAMETER; } else { ASSERT(FALSE); } } // // check for "-?" // if (ShellCommandLineGetFlag(Package, L"-?")) { ASSERT(FALSE); } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"cd"); ShellStatus = SHELL_INVALID_PARAMETER; } else { // // remember that param 0 is the command name // If there are 0 value parameters, then print the current directory // else If there are 2 value parameters, then print the error message // else If there is 1 value paramerer , then change the directory // Cwd = ShellGetCurrentDir (NULL); if (Cwd == NULL) { ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); ShellStatus = SHELL_NOT_FOUND; } else { Param1 = ShellCommandLineGetRawValue (Package, 1); if (Param1 == NULL) { // // display the current directory // ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_CD_PRINT), gShellLevel2HiiHandle, Cwd); } else { Param1Copy = CatSPrint (NULL, L"%s", Param1, NULL); for (Walker = Param1Copy; Walker != NULL && *Walker != CHAR_NULL; Walker++) { if (*Walker == L'\"') { CopyMem (Walker, Walker + 1, StrSize(Walker) - sizeof(Walker[0])); } } if (Param1Copy != NULL && IsCurrentFileSystem (Param1Copy, Cwd)) { Status = ReplaceDriveWithCwd (&Param1Copy,Cwd); if (!EFI_ERROR (Status)) { Param1Copy = PathCleanUpDirectories (Param1Copy); } } else { // // Can't use cd command to change filesystem. // ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); Status = EFI_NOT_FOUND; } if (!EFI_ERROR(Status) && Param1Copy != NULL) { Splitter = StrStr (Cwd, L":"); if (Param1Copy[0] == L'\\') { // // Absolute Path on current drive letter. // TotalSize = ((Splitter - Cwd + 1) * sizeof(CHAR16)) + StrSize(Param1Copy); TempBuffer = AllocateZeroPool (TotalSize); if (TempBuffer == NULL) { Status = EFI_OUT_OF_RESOURCES; } else { StrnCpyS (TempBuffer, TotalSize / sizeof(CHAR16), Cwd, (Splitter - Cwd + 1)); StrCatS (TempBuffer, TotalSize / sizeof(CHAR16), Param1Copy); FreePool (Param1Copy); Param1Copy = TempBuffer; TempBuffer = NULL; } } else { if (StrStr (Param1Copy,L":") == NULL) { TotalSize = StrSize (Cwd) + StrSize (Param1Copy); TempBuffer = AllocateZeroPool (TotalSize); if (TempBuffer == NULL) { Status = EFI_OUT_OF_RESOURCES; } else { StrCpyS (TempBuffer, TotalSize / sizeof (CHAR16), Cwd); StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), L"\\"); StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), Param1Copy); FreePool (Param1Copy); Param1Copy = PathCleanUpDirectories (TempBuffer); } } } } if (!EFI_ERROR(Status)) { Status = ExtractDriveAndPath (Param1Copy, &Drive, &Path); } if (!EFI_ERROR (Status) && Drive != NULL && Path != NULL) { if (EFI_ERROR(ShellIsDirectory (Param1Copy))) { ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cd", Param1Copy); ShellStatus = SHELL_NOT_FOUND; } else { Status = gEfiShellProtocol->SetCurDir (Drive, Path + 1); if (EFI_ERROR (Status)) { ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cd", Param1Copy); ShellStatus = SHELL_NOT_FOUND; } } } if (Drive != NULL) { FreePool (Drive); } if (Path != NULL) { FreePool (Path); } FreePool (Param1Copy); } } } // // free the command line package // ShellCommandLineFreeVarList (Package); // // return the status // return (ShellStatus); }
EFI_STATUS BootAndroidBootImg ( IN UINTN BufferSize, IN VOID *Buffer ) { EFI_STATUS Status; CHAR8 KernelArgs[ANDROID_BOOTIMG_KERNEL_ARGS_SIZE]; VOID *Kernel; UINTN KernelSize; VOID *Ramdisk; UINTN RamdiskSize; MEMORY_DEVICE_PATH KernelDevicePath; CHAR16 *LoadOptions, *NewLoadOptions; Status = ParseAndroidBootImg ( Buffer, &Kernel, &KernelSize, &Ramdisk, &RamdiskSize, KernelArgs ); if (EFI_ERROR (Status)) { return Status; } KernelDevicePath = MemoryDevicePathTemplate; // Have to cast to UINTN before casting to EFI_PHYSICAL_ADDRESS in order to // appease GCC. KernelDevicePath.Node1.StartingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel; KernelDevicePath.Node1.EndingAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Kernel + KernelSize; // Initialize Linux command line LoadOptions = CatSPrint (NULL, L"%a", KernelArgs); if (LoadOptions == NULL) { return EFI_OUT_OF_RESOURCES; } if (RamdiskSize != 0) { NewLoadOptions = CatSPrint (LoadOptions, L" initrd=0x%x,0x%x", (UINTN)Ramdisk, RamdiskSize); FreePool (LoadOptions); if (NewLoadOptions == NULL) { return EFI_OUT_OF_RESOURCES; } LoadOptions = NewLoadOptions; } Status = StartEfiApplication (gImageHandle, (EFI_DEVICE_PATH_PROTOCOL *) &KernelDevicePath, StrSize (LoadOptions), LoadOptions); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "Couldn't Boot Linux: %d\n", Status)); Status = EFI_DEVICE_ERROR; goto FreeLoadOptions; } // If we got here we do a confused face because BootLinuxFdt returned, // reporting success. DEBUG ((EFI_D_ERROR, "WARNING: BdsBootLinuxFdt returned EFI_SUCCESS.\n")); return EFI_SUCCESS; FreeLoadOptions: FreePool (LoadOptions); return Status; }
/** Function for 'edit' command. @param[in] ImageHandle Handle to the Image (NULL if Internal). @param[in] SystemTable Pointer to the System Table (NULL if Internal). **/ SHELL_STATUS EFIAPI ShellCommandRunEdit ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { EFI_STATUS Status; CHAR16 *Buffer; CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; LIST_ENTRY *Package; CONST CHAR16 *Cwd; CHAR16 *Nfs; CHAR16 *Spot; CONST CHAR16 *TempParam; // SHELL_FILE_HANDLE TempHandle; Buffer = NULL; ShellStatus = SHELL_SUCCESS; Nfs = NULL; // // initialize the shell lib (we must be in non-auto-init...) // Status = ShellInitialize(); ASSERT_EFI_ERROR(Status); Status = CommandInit(); ASSERT_EFI_ERROR(Status); // // parse the command line // Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); if (EFI_ERROR(Status)) { if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"edit", ProblemParam); FreePool(ProblemParam); ShellStatus = SHELL_INVALID_PARAMETER; } else { ASSERT(FALSE); } } else { if (ShellCommandLineGetCount(Package) > 2) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"edit"); ShellStatus = SHELL_INVALID_PARAMETER; } else { Cwd = gEfiShellProtocol->GetCurDir(NULL); if (Cwd == NULL) { Cwd = ShellGetEnvironmentVariable(L"path"); if (Cwd != NULL) { Nfs = StrnCatGrow(&Nfs, NULL, Cwd+3, 0); if (Nfs != NULL) { Spot = StrStr(Nfs, L";"); if (Spot != NULL) { *Spot = CHAR_NULL; } Spot = StrStr(Nfs, L"\\"); if (Spot != NULL) { Spot[1] = CHAR_NULL; } gEfiShellProtocol->SetCurDir(NULL, Nfs); FreePool(Nfs); } } } Status = MainEditorInit (); if (EFI_ERROR (Status)) { gST->ConOut->ClearScreen (gST->ConOut); gST->ConOut->EnableCursor (gST->ConOut, TRUE); ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_MAIN_INIT_FAILED), gShellDebug1HiiHandle); } else { MainEditorBackup (); // // if editor launched with file named // if (ShellCommandLineGetCount(Package) == 2) { TempParam = ShellCommandLineGetRawValue(Package, 1); ASSERT(TempParam != NULL); FileBufferSetFileName (TempParam); // if (EFI_ERROR(ShellFileExists(MainEditor.FileBuffer->FileName))) { // Status = ShellOpenFileByName(MainEditor.FileBuffer->FileName, &TempHandle, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0); // if (!EFI_ERROR(Status)) { // ShellCloseFile(&TempHandle); // } // } } Status = FileBufferRead (MainEditor.FileBuffer->FileName, FALSE); if (!EFI_ERROR (Status)) { MainEditorRefresh (); Status = MainEditorKeyInput (); } if (Status != EFI_OUT_OF_RESOURCES) { // // back up the status string // Buffer = CatSPrint (NULL, L"%s", StatusBarGetString()); } MainEditorCleanup (); // // print editor exit code on screen // if (Status == EFI_SUCCESS) { } else if (Status == EFI_OUT_OF_RESOURCES) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"edit"); } else { if (Buffer != NULL) { if (StrCmp (Buffer, L"") != 0) { // // print out the status string // ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_MAIN_BUFFER), gShellDebug1HiiHandle, Buffer); } else { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_MAIN_UNKNOWN_EDITOR_ERR), gShellDebug1HiiHandle); } } else { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_MAIN_UNKNOWN_EDITOR_ERR), gShellDebug1HiiHandle); } } if (Status != EFI_OUT_OF_RESOURCES) { SHELL_FREE_NON_NULL (Buffer); } } } ShellCommandLineFreeVarList (Package); } return ShellStatus; }