void print_escape(EFI_FILE_HANDLE handle, CHAR8 *source) { CHAR8 c; CHAR8 s[8]; UINTN size; s[0] = 34; s[1] = 0; size = AsciiStrSize(s) - 1; FileHandleWrite(handle, &size, s); while (c = *source++, c) { switch (c) { case 34: s[0] = 92; s[1] = c; size = 2; break; case 92: s[0] = s[1] = c; size = 2; break; case 10: s[0] = 92; s[1] = 110; s[2] = 34; s[3] = c; s[4] = s[5] = 32; s[6] = 34; size = 7; break; default: s[0] = c; size = 1; } s[size] = 0; size = AsciiStrSize(s) - 1; FileHandleWrite(handle, &size, s); } s[0] = 34; s[1] = 0; size = AsciiStrSize(s) - 1; FileHandleWrite(handle, &size, s); }
/** function to write a line of unicode text to a file. if Handle is NULL, return error. if Buffer is NULL, do nothing. (return SUCCESS) @param[in] Handle FileHandle to write to @param[in] Buffer Buffer to write @retval EFI_SUCCESS the data was written. @retval other failure. @sa FileHandleWrite **/ EFI_STATUS EFIAPI FileHandleWriteLine( IN EFI_FILE_HANDLE Handle, IN CHAR16 *Buffer ) { EFI_STATUS Status; UINTN Size; if (Buffer == NULL) { return (EFI_SUCCESS); } if (Handle == NULL) { return (EFI_INVALID_PARAMETER); } Size = StrSize(Buffer) - sizeof(Buffer[0]); Status = FileHandleWrite(Handle, &Size, Buffer); if (EFI_ERROR(Status)) { return (Status); } Size = StrSize(L"\r\n") - sizeof(CHAR16); return FileHandleWrite(Handle, &Size, L"\r\n"); }
INTN EFIAPI ShellAppMain (IN UINTN Argc, IN CHAR16 **Argv) { CHAR8 c; int i; CHAR8 s[2]; CHAR8 *source2 = source; EFI_FILE_HANDLE handle; UINTN size; for (i = 0; i < 4; i++) write_file(filenames[i], files[i]); ShellOpenFileByName(L"Quine.c", (void**)&handle, EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0); while (c = *source2++, c) { if (36 == c) { print_escape(handle, files[i++-4]); } else if (126 == c) { print_escape(handle, source); } else { s[0] = c; s[1] = 0; size = AsciiStrSize(s) - 1; FileHandleWrite(handle, &size, s); } } FileHandleClose(handle); Print( L" _____ _____ ___ ___ _ \n" L"| ____| ___|_ _| / _ \\ _ _(_)_ __ ___ \n" L"| _| | |_ | | | | | | | | | | '_ \\ / _ \\\n" L"| |___| _| | | | |_| | |_| | | | | | __/\n" L"|_____|_| |___| \\__\\_\\\\__,_|_|_| |_|\\___|\n" ); return 0; }
void write_file(CHAR16 *filename, CHAR8 *source) { EFI_FILE_HANDLE handle; UINTN size = AsciiStrSize(source) - 1; ShellOpenFileByName(filename, (void**)&handle, EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0); FileHandleWrite(handle, &size, source); FileHandleClose(handle); }
/** Saves the non-volatile variables into the NvVars file on the given file system. @param[in] FsHandle - Handle for a gEfiSimpleFileSystemProtocolGuid instance @return EFI_STATUS based on the success or failure of load operation **/ EFI_STATUS SaveNvVarsToFs ( EFI_HANDLE FsHandle ) { EFI_STATUS Status; EFI_FILE_HANDLE File; UINTN WriteSize; UINTN VariableDataSize; VOID *VariableData; EFI_HANDLE SerializedVariables; SerializedVariables = NULL; Status = SerializeVariablesNewInstance (&SerializedVariables); if (EFI_ERROR (Status)) { return Status; } Status = SerializeVariablesIterateSystemVariables ( IterateVariablesCallbackAddAllNvVariables, (VOID*) SerializedVariables ); if (EFI_ERROR (Status)) { return Status; } VariableData = NULL; VariableDataSize = 0; Status = SerializeVariablesToBuffer ( SerializedVariables, NULL, &VariableDataSize ); if (Status == RETURN_BUFFER_TOO_SMALL) { VariableData = AllocatePool (VariableDataSize); if (VariableData == NULL) { Status = EFI_OUT_OF_RESOURCES; } else { Status = SerializeVariablesToBuffer ( SerializedVariables, VariableData, &VariableDataSize ); } } SerializeVariablesFreeInstance (SerializedVariables); if (EFI_ERROR (Status)) { return Status; } // // Open the NvVars file for writing. // Status = GetNvVarsFile (FsHandle, FALSE, &File); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_INFO, "FsAccess.c: Unable to open file to saved NV Variables\n")); return Status; } // // Empty the starting file contents. // Status = FileHandleEmpty (File); if (EFI_ERROR (Status)) { FileHandleClose (File); return Status; } WriteSize = VariableDataSize; Status = FileHandleWrite (File, &WriteSize, VariableData); if (EFI_ERROR (Status)) { return Status; } FileHandleClose (File); if (!EFI_ERROR (Status)) { // // Write a variable to indicate we've already loaded the // variable data. If it is found, we skip the loading on // subsequent attempts. // SetNvVarsVariable(); DEBUG ((EFI_D_INFO, "Saved NV Variables to NvVars file\n")); } return Status; }
/** Function to write a line of text to a file. If the file is a Unicode file (with UNICODE file tag) then write the unicode text. If the file is an ASCII file then write the ASCII text. If the size of file is zero (without file tag at the beginning) then write ASCII text as default. @param[in] Handle FileHandle to write to. @param[in] Buffer Buffer to write, if NULL the function will take no action and return EFI_SUCCESS. @retval EFI_SUCCESS The data was written. Buffer is NULL. @retval EFI_INVALID_PARAMETER Handle is NULL. @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII string due to out of resources. @sa FileHandleWrite **/ EFI_STATUS EFIAPI FileHandleWriteLine( IN EFI_FILE_HANDLE Handle, IN CHAR16 *Buffer ) { EFI_STATUS Status; CHAR16 CharBuffer; UINTN Size; UINTN Index; UINTN CharSize; UINT64 FileSize; UINT64 OriginalFilePosition; BOOLEAN Ascii; CHAR8 *AsciiBuffer; if (Buffer == NULL) { return (EFI_SUCCESS); } if (Handle == NULL) { return (EFI_INVALID_PARAMETER); } Ascii = FALSE; AsciiBuffer = NULL; Status = FileHandleGetPosition(Handle, &OriginalFilePosition); if (EFI_ERROR(Status)) { return Status; } Status = FileHandleSetPosition(Handle, 0); if (EFI_ERROR(Status)) { return Status; } Status = FileHandleGetSize(Handle, &FileSize); if (EFI_ERROR(Status)) { return Status; } if (FileSize == 0) { Ascii = TRUE; } else { CharSize = sizeof (CHAR16); Status = FileHandleRead (Handle, &CharSize, &CharBuffer); ASSERT_EFI_ERROR (Status); if (CharBuffer == gUnicodeFileTag) { Ascii = FALSE; } else { Ascii = TRUE; } } Status = FileHandleSetPosition(Handle, OriginalFilePosition); if (EFI_ERROR(Status)) { return Status; } if (Ascii) { Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8); AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size); if (AsciiBuffer == NULL) { return EFI_OUT_OF_RESOURCES; } UnicodeStrToAsciiStrS (Buffer, AsciiBuffer, Size); for (Index = 0; Index < Size; Index++) { if (!((AsciiBuffer[Index] >= 0) && (AsciiBuffer[Index] < 128))){ FreePool(AsciiBuffer); return EFI_INVALID_PARAMETER; } } Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8); Status = FileHandleWrite(Handle, &Size, AsciiBuffer); if (EFI_ERROR(Status)) { FreePool (AsciiBuffer); return (Status); } Size = AsciiStrSize("\r\n") - sizeof(CHAR8); Status = FileHandleWrite(Handle, &Size, "\r\n"); } else { if (OriginalFilePosition == 0) { Status = FileHandleSetPosition (Handle, sizeof(CHAR16)); if (EFI_ERROR(Status)) { return Status; } } Size = StrSize(Buffer) - sizeof(CHAR16); Status = FileHandleWrite(Handle, &Size, Buffer); if (EFI_ERROR(Status)) { return (Status); } Size = StrSize(L"\r\n") - sizeof(CHAR16); Status = FileHandleWrite(Handle, &Size, L"\r\n"); } if (AsciiBuffer != NULL) { FreePool (AsciiBuffer); } return Status; }