EFI_STATUS EFIAPI RemoveFileLock( IN EFI_FILE_HANDLE CurDir, IN CHAR16 * FileNameUser ) { EFI_STATUS Status = EFI_SUCCESS; EFI_FILE_HANDLE FileHandle; CHAR16* FileNameElite; FileNameElite = AllocateZeroPool(260*sizeof(CHAR16)); StrCpy(FileNameElite,FileNameUser); StrCat(FileNameElite,FILE_NAME_ELITE); StrCat(FileNameElite,g_NAME_ELITE); Status = CurDir->Open (CurDir, &FileHandle, FileNameElite, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0); FreePool(FileNameElite); if (EFI_ERROR(Status)) { #ifdef FORCE_DEBUG Print(L"Error Open File RemoveFileLock\n"); #endif return Status; } else { #ifdef FORCE_DEBUG Print(L"RemoveFileLock OK\n"); #endif } Status=FileHandle->Delete(FileHandle); if( Status != EFI_SUCCESS ) { #ifdef FORCE_DEBUG Print(L"Write RemoveFileLock File Failed\n"); #endif return Status; } return EFI_SUCCESS; }
EFI_STATUS EFIAPI TOLOpen ( IN EFI_TEST_OUTPUT_LIBRARY_PROTOCOL *This, IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN CHAR16 *FileName, IN BOOLEAN OverwriteFile, OUT EFI_FILE **FileHandle ) /*++ Routine Description: One interface function of the TestOutputLibrary to open a file. Arguments: This - the protocol instance structure. DevicePath - the file's root device path. FileName - the file's name relative to the root. OverwriteFile - whether to overwrite the file. FileHandle - return the file's handle. Returns: EFI_SUCCESS - open the file successfully. EFI_NOT_READY - to overwrite an opened file is not allowed. EFI_OUT_OF_RESOURCES - not enough memory. --*/ { EFI_STATUS Status; TEST_OUTPUT_FILE *OutputFile; TEST_OUTPUT_PRIVATE_DATA *Private; EFI_HANDLE DeviceHandle; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol; EFI_FILE_HANDLE RootDir; EFI_FILE_HANDLE Handle; UINTN BufSize; CHAR8 Buffer[2]; EFI_DEVICE_PATH_PROTOCOL *PreDevicePath; Private = TEST_OUTPUT_PRIVATE_DATA_FROM_THIS (This); // // Search the file in OutputFileList to see whether the file has been opened // OutputFile = Private->OutputFileList; while (OutputFile != NULL) { if ((SctDevicePathCompare (DevicePath, OutputFile->DevicePath) == 0) && (StrCmp (FileName, OutputFile->FileName) == 0)) { break; } OutputFile = OutputFile->Next; } if (OutputFile == NULL) { // // Not found, open the file and add to the list // PreDevicePath = DevicePath; // // Determine device handle for fs protocol on specified device path // Status = BS->LocateDevicePath ( &gEfiSimpleFileSystemProtocolGuid, &PreDevicePath, &DeviceHandle ); if (EFI_ERROR (Status)) { return Status; } // // Determine volume for file system on device handle // Status = BS->HandleProtocol ( DeviceHandle, &gEfiSimpleFileSystemProtocolGuid, (VOID*)&Vol ); if (EFI_ERROR (Status)) { return Status; } // // Open volume for file system on device path // Status = Vol->OpenVolume (Vol, &RootDir); if (EFI_ERROR (Status)) { return Status; } // // Determine the existence of the file // Status = RootDir->Open ( RootDir, &Handle, FileName, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, 0 ); if (Status == EFI_NOT_FOUND) { // // The file not exist, create it // Status = SctCreateFile (RootDir, FileName, &Handle); if (EFI_ERROR (Status)) { RootDir->Close (RootDir); return Status; } // // Write the head of Unicode text file // Buffer[0] = 0xff; Buffer[1] = 0xfe; BufSize = 2; Status = Handle->Write (Handle, &BufSize, Buffer); if (EFI_ERROR (Status)) { Handle->Close (Handle); return Status; } } else if (EFI_ERROR (Status)) { RootDir->Close(RootDir); return Status; } if (OverwriteFile) { // // Overwrite the file // // // Delete the file // Status = Handle->Delete (Handle); // // EFI_FILE.Delete() return a warning status // if (Status != EFI_SUCCESS) { RootDir->Close (RootDir); return EFI_UNSUPPORTED; } // // Recreate the file // Status = RootDir->Open ( RootDir, &Handle, FileName, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, 0 ); if (EFI_ERROR (Status)) { RootDir->Close (RootDir); return Status; } // // Write the head of Unicode text file // Buffer[0] = 0xff; Buffer[1] = 0xfe; BufSize = 2; Status = Handle->Write (Handle, &BufSize, Buffer); if (EFI_ERROR (Status)) { Handle->Close (Handle); return Status; } } else { // // Append the file // // // Set position to the end of file // Status = Handle->SetPosition (Handle, (UINT64)-1); if (EFI_ERROR (Status)) { RootDir->Close (RootDir); return Status; } } RootDir->Close (RootDir); // // Add the opened file to the OutputFileList // Status = BS->AllocatePool ( EfiBootServicesData, sizeof(TEST_OUTPUT_FILE), (VOID **)&OutputFile ); if (EFI_ERROR (Status)) { Handle->Close (Handle); return Status; } ZeroMem (OutputFile, sizeof(TEST_OUTPUT_FILE)); OutputFile->DevicePath = DuplicateDevicePath (DevicePath); if (OutputFile->DevicePath == NULL) { Handle->Close (Handle); BS->FreePool (OutputFile); return EFI_OUT_OF_RESOURCES; } OutputFile->FileName = StrDuplicate (FileName); if (OutputFile->FileName == NULL) { Handle->Close (Handle); BS->FreePool (OutputFile->DevicePath); BS->FreePool (OutputFile); return EFI_OUT_OF_RESOURCES; } OutputFile->FileHandle = Handle; OutputFile->Next = Private->OutputFileList; Private->OutputFileList = OutputFile; } // // Add the open count and return the file handle // OutputFile->OpenCount ++; *FileHandle = OutputFile->FileHandle; return EFI_SUCCESS; }
EFI_STATUS EFIAPI TrlWriteResetRecord ( IN EFI_TEST_RECOVERY_LIBRARY_PROTOCOL *This, IN UINTN Size, IN VOID *Buffer ) /*++ Routine Description: One interface function of the TestRecoveryLibrary to write reset record. Arguments: This - the protocol instance structure. Size - the bytes to be write, it can't bigger than 1024Bytes. Buffer - buffer contain the record to be written. Returns: EFI_SUCCESS - write the record successfully. EFI_INVALID_PARAMETER - invalid parameters. --*/ { EFI_STATUS Status; EFI_HANDLE DeviceHandle; EFI_FILE_HANDLE RootDir; EFI_FILE_HANDLE Handle; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol; TEST_RECOVERY_PRIVATE_DATA *Private; EFI_DEVICE_PATH_PROTOCOL *PreDevicePath; Private = TEST_RECOVERY_PRIVATE_DATA_FROM_TRL (This); // // Determine device handle for fs protocol on specified device path // PreDevicePath = Private->DevicePath; Status = gBS->LocateDevicePath ( &gEfiSimpleFileSystemProtocolGuid, &PreDevicePath, &DeviceHandle ); if (EFI_ERROR (Status)) { return Status; } // // Determine volume for file system on device handle // Status = gBS->HandleProtocol ( DeviceHandle, &gEfiSimpleFileSystemProtocolGuid, (VOID*)&Vol ); if (EFI_ERROR (Status)) { return Status; } // // Open volume for file system on device path // Status = Vol->OpenVolume (Vol, &RootDir); if (EFI_ERROR (Status)) { return Status; } // // Open file for read // Status = RootDir->Open ( RootDir, &Handle, Private->FileName, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0 ); if (Status == EFI_NOT_FOUND) { // // The file not exist, create it // Status = RootDir->Open ( RootDir, &Handle, Private->FileName, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, 0 ); if (EFI_ERROR (Status)) { RootDir->Close (RootDir); return Status; } } else if (Status == EFI_SUCCESS) { // // The file exist, delete it // Status = Handle->Delete (Handle); // // EFI_FILE.Delete() return a warning status // if (Status != EFI_SUCCESS) { Handle->Close (Handle); RootDir->Close (RootDir); return EFI_UNSUPPORTED; } // // Recreate the file // Status = RootDir->Open ( RootDir, &Handle, Private->FileName, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, 0 ); if (EFI_ERROR (Status)) { RootDir->Close (RootDir); return Status; } } else { RootDir->Close (RootDir); return Status; } // // Write buffer // Status = Handle->Write (Handle, &Size, Buffer); Handle->Close (Handle); RootDir->Close (RootDir); return Status; }
/** Write a file. @param[in] FileName The file to be written. @param[in] BufferSize The file buffer size @param[in] Buffer The file buffer @retval EFI_SUCCESS Write file successfully **/ EFI_STATUS WriteFileFromBuffer ( IN CHAR16 *FileName, IN UINTN BufferSize, IN VOID *Buffer ) { EFI_STATUS Status; EFI_FILE_HANDLE RootDir; EFI_FILE_HANDLE Handle; UINTN TempBufferSize; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol; Vol = GetMyVol(); if (Vol == NULL) { return EFI_NOT_FOUND; } // // Open the root directory // Status = Vol->OpenVolume (Vol, &RootDir); if (EFI_ERROR (Status)) { return Status; } // // Open the file // Status = RootDir->Open ( RootDir, &Handle, FileName, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE| EFI_FILE_MODE_CREATE, 0 ); if (EFI_ERROR (Status)) { RootDir->Close (RootDir); return Status; } // // Delete file // Status = Handle->Delete(Handle); if (EFI_ERROR(Status)) { return Status; } // // Open the file again // Status = RootDir->Open ( RootDir, &Handle, FileName, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE| EFI_FILE_MODE_CREATE, 0 ); if (EFI_ERROR (Status)) { RootDir->Close (RootDir); return Status; } RootDir->Close (RootDir); // // Write the file data from the buffer // TempBufferSize = BufferSize; Status = Handle->Write ( Handle, &TempBufferSize, Buffer ); if (EFI_ERROR (Status)) { Handle->Close (Handle); return Status; } Handle->Close (Handle); return EFI_SUCCESS; }
/** * Write a file to floppy disk. */ EFI_STATUS WriteFloppyFile ( IN CHAR16 *FileName, IN OUT UINT32 Length, IN VOID *Buffer ) { EFI_STATUS Status; EFI_HANDLE DeviceHandle; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol; EFI_FILE_HANDLE RootDir; EFI_FILE_HANDLE Handle; EFI_DEVICE_PATH_PROTOCOL *DevicePath; UINTN BufLen; // // Get floppy device path // Status = GetFloppyDevicePath (&DevicePath); if (EFI_ERROR(Status)) { return Status; } // // Determine device handle for fs protocol on floppy device path // Status = gtBS->LocateDevicePath ( &gEfiSimpleFileSystemProtocolGuid, &DevicePath, &DeviceHandle ); if (EFI_ERROR(Status) ) { return Status; } // // Determine volume for file system on device handle // Status = gtBS->HandleProtocol ( DeviceHandle, &gEfiSimpleFileSystemProtocolGuid, (VOID*)&Vol ); if (EFI_ERROR(Status) ) { return Status; } // // Open volume for file system on device path // Status = Vol->OpenVolume (Vol, &RootDir); if (Status == EFI_MEDIA_CHANGED) { // // Reopen the volume // Status = gtBS->HandleProtocol ( DeviceHandle, &gEfiSimpleFileSystemProtocolGuid, (VOID*)&Vol ); if (EFI_ERROR(Status) ) { return Status; } Status = Vol->OpenVolume (Vol, &RootDir); } if (EFI_ERROR(Status) ) { return Status; } // // Determine the existence of the file // Status = RootDir->Open ( RootDir, &Handle, FileName, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0 ); if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_FOUND)) { RootDir->Close (RootDir); return Status; } if (Status == EFI_SUCCESS) { // // Delete the existent file // Status = Handle->Delete (Handle); if (Status != EFI_SUCCESS) { Handle->Close (Handle); RootDir->Close (RootDir); return Status; } } // // Create the file // Status = RootDir->Open ( RootDir, &Handle, FileName, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, 0 ); if (EFI_ERROR(Status)) { RootDir->Close (RootDir); return Status; } // // Write the file // BufLen = Length; Status = Handle->Write (Handle, &BufLen, Buffer); Handle->Close (Handle); RootDir->Close (RootDir); return Status; }