STATIC EFI_STATUS PrepareFirmwareImage ( IN LIST_ENTRY *CheckPackage, IN OUT SHELL_FILE_HANDLE *FileHandle, IN OUT UINTN **FileBuffer, IN OUT UINT64 *FileSize ) { CONST CHAR16 *FileStr; EFI_STATUS Status; UINT64 OpenMode; UINTN *Buffer; // Parse string from commandline FileStr = ShellCommandLineGetRawValue (CheckPackage, 1); if (FileStr == NULL) { Print (L"%s: No image specified\n", CMD_NAME_STRING); return EFI_INVALID_PARAMETER; } else { Status = ShellIsFile (FileStr); if (EFI_ERROR(Status)) { Print (L"%s: File not found\n", CMD_NAME_STRING); return EFI_INVALID_PARAMETER; } } // Obtain file size OpenMode = EFI_FILE_MODE_READ; Status = ShellOpenFileByName (FileStr, FileHandle, OpenMode, 0); if (EFI_ERROR (Status)) { Print (L"%s: Cannot open Image file\n", CMD_NAME_STRING); return EFI_DEVICE_ERROR; } Status = FileHandleGetSize (*FileHandle, FileSize); if (EFI_ERROR (Status)) { Print (L"%s: Cannot get Image file size\n", CMD_NAME_STRING); } // Read Image header into buffer Buffer = AllocateZeroPool (*FileSize); Status = FileHandleRead (*FileHandle, (UINTN *)FileSize, Buffer); if (EFI_ERROR (Status)) { Print (L"%s: Cannot read Image file header\n", CMD_NAME_STRING); ShellCloseFile (FileHandle); FreePool (Buffer); return EFI_DEVICE_ERROR; } *FileBuffer = Buffer; return EFI_SUCCESS; }
/** 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; }
EFI_STATUS ParseCommandLine ( IN UINTN Argc, IN CHAR16 **Argv ) { EFI_STATUS Status; UINTN Index; // // Check to make sure that the command line has enough arguments for minimal // operation. The minimum is just the file name. // if (Argc < 2 || Argc > 4) { return EFI_INVALID_PARAMETER; } // // Loop through command line arguments. // for (Index = 1; Index < Argc; Index++) { // // Make sure the string is valid. // if (StrLen (Argv[Index]) == 0) {; PrintToken (STRING_TOKEN (STR_FWUPDATE_ZEROLENGTH_ARG), HiiHandle); return EFI_INVALID_PARAMETER; } // // Check to see if this is an option or the file name. // if ((Argv[Index])[0] == L'-' || (Argv[Index])[0] == L'/') { // // Parse the arguments. // if ((StrCmp (Argv[Index], L"-h") == 0) || (StrCmp (Argv[Index], L"--help") == 0) || (StrCmp (Argv[Index], L"/?") == 0) || (StrCmp (Argv[Index], L"/h") == 0)) { // // Print Help Information. // return EFI_INVALID_PARAMETER; } else if (StrCmp (Argv[Index], L"-m") == 0) { // // Parse the MAC address here. // Status = ConvertMac(Argv[Index+1]); if (EFI_ERROR(Status)) { PrintToken (STRING_TOKEN (STR_FWUPDATE_INVAILD_MAC), HiiHandle); return Status; } // // Save the MAC address to mInputData.MacValue. // mInputData.UpdateMac= TRUE; Index++; } else { // // Invalid option was provided. // return EFI_INVALID_PARAMETER; } } if ((Index == Argc - 1) && (StrCmp (Argv[Index - 1], L"-m") != 0)) { // // The only parameter that is not an option is the firmware image. Check // to make sure that the file exists. // Status = ShellIsFile (Argv[Index]); if (EFI_ERROR (Status)) { PrintToken (STRING_TOKEN (STR_FWUPDATE_FILE_NOT_FOUND_ERROR), HiiHandle, Argv[Index]); return EFI_INVALID_PARAMETER; } if (StrLen (Argv[Index]) > INPUT_STRING_LEN) { PrintToken (STRING_TOKEN (STR_FWUPDATE_PATH_ERROR), HiiHandle, Argv[Index]); return EFI_INVALID_PARAMETER; } StrCpy (mInputData.FileName, Argv[Index]); mInputData.UpdateFromFile = TRUE; } } return EFI_SUCCESS; }