Skip to content

Commit

Permalink
Switching to explicit binary open when in windows for FW file
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn L Diviney <[email protected]>
  • Loading branch information
gldiviney committed Apr 10, 2019
1 parent 815b2eb commit 570870e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 14 deletions.
87 changes: 76 additions & 11 deletions DcpmPkg/common/Utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,63 @@ OpenFile(
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
IN BOOLEAN CreateFileFlag
)
{
return OpenFileWithFlag(pArgFilePath, pFileHandle, pCurrentDirectory, CreateFileFlag, FALSE);
}

/**
Open file or create new file in binary mode.
@param[in] pArgFilePath path to a file that will be opened
@param[out] pFileHandle output handler
@param[in, optional] pCurrentDirectory is the current directory path to where
we should start to search for the file.
@param[in] CreateFileFlag TRUE to create new file or FALSE to open
existing file
@retval EFI_SUCCESS
@retval EFI_INVALID_PARAMETER pFilePath is NULL or empty or pFileHandle is NULL
@retval EFI_PROTOCOL_ERROR if there is no EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
**/
EFI_STATUS
OpenFileBinary(
IN CHAR16 *pArgFilePath,
OUT EFI_FILE_HANDLE *pFileHandle,
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
IN BOOLEAN CreateFileFlag
)
{
#ifdef OS_BUILD
#ifdef _MSC_VER
return OpenFileWithFlag(pArgFilePath, pFileHandle, pCurrentDirectory, CreateFileFlag, TRUE);
#endif
#endif
return OpenFileWithFlag(pArgFilePath, pFileHandle, pCurrentDirectory, CreateFileFlag, FALSE);
}

/**
Open file or create new file with the proper flags.
@param[in] pArgFilePath path to a file that will be opened
@param[out] pFileHandle output handler
@param[in, optional] pCurrentDirectory is the current directory path to where
we should start to search for the file.
@param[in] CreateFileFlag - TRUE to create new file or FALSE to open
existing file
@param[in] binary - use binary open
@retval EFI_SUCCESS
@retval EFI_INVALID_PARAMETER pFilePath is NULL or empty or pFileHandle is NULL
@retval EFI_PROTOCOL_ERROR if there is no EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
**/
EFI_STATUS
OpenFileWithFlag(
IN CHAR16 *pArgFilePath,
OUT EFI_FILE_HANDLE *pFileHandle,
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
IN BOOLEAN CreateFileFlag,
BOOLEAN binary
)
{
EFI_STATUS Rc = EFI_SUCCESS;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pVolume = NULL;
Expand Down Expand Up @@ -1058,13 +1115,13 @@ OpenFile(
Get the file system protocol
**/
Rc = gBS->OpenProtocol(
pHandles[Index],
&gEfiSimpleFileSystemProtocolGuid,
(VOID *) &pVolume,
NULL,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
pHandles[Index],
&gEfiSimpleFileSystemProtocolGuid,
(VOID *) &pVolume,
NULL,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR(Rc)) {
goto AfterHandles;
}
Expand All @@ -1079,11 +1136,19 @@ OpenFile(

if (CreateFileFlag) {
// if EFI_FILE_MODE_CREATE then also EFI_FILE_MODE_READ and EFI_FILE_MODE_WRITE are needed.
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath,
EFI_FILE_MODE_CREATE|EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE,
0);
if (binary) {
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath,
EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_BINARY, 0);
} else {
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath,
EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0);
}
} else {
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath, EFI_FILE_MODE_READ, 0);
if (binary) {
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath, EFI_FILE_MODE_READ | EFI_FILE_MODE_BINARY, 0);
} else {
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath, EFI_FILE_MODE_READ, 0);
}
}

RootDirHandle->Close(RootDirHandle);
Expand Down
47 changes: 47 additions & 0 deletions DcpmPkg/common/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
int _fltused();
#endif

#define EFI_FILE_MODE_BINARY 0x8000000000000064ULL

/**
The Config Protocol version bytes definition
Expand Down Expand Up @@ -660,6 +661,52 @@ OpenFile(
IN BOOLEAN CreateFileFlag
);

/**
Open file or create new file in binary mode.
@param[in] pArgFilePath path to a file that will be opened
@param[out] pFileHandle output handler
@param[in, optional] pCurrentDirectory is the current directory path to where
we should start to search for the file.
@param[in] CreateFileFlag TRUE to create new file or FALSE to open
existing file
@retval EFI_SUCCESS
@retval EFI_INVALID_PARAMETER pFilePath is NULL or empty or pFileHandle is NULL
@retval EFI_PROTOCOL_ERROR if there is no EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
**/
EFI_STATUS
OpenFileBinary(
IN CHAR16 *pArgFilePath,
OUT EFI_FILE_HANDLE *pFileHandle,
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
IN BOOLEAN CreateFileFlag
);

/**
Open file or create new file with the proper flags.
@param[in] pArgFilePath path to a file that will be opened
@param[out] pFileHandle output handler
@param[in, optional] pCurrentDirectory is the current directory path to where
we should start to search for the file.
@param[in] CreateFileFlag - TRUE to create new file or FALSE to open
existing file
@param[in] binary - use binary open
@retval EFI_SUCCESS
@retval EFI_INVALID_PARAMETER pFilePath is NULL or empty or pFileHandle is NULL
@retval EFI_PROTOCOL_ERROR if there is no EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
**/
EFI_STATUS
OpenFileWithFlag(
IN CHAR16 *pArgFilePath,
OUT EFI_FILE_HANDLE *pFileHandle,
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
IN BOOLEAN CreateFileFlag,
BOOLEAN binary
);

/**
Open file or create new file based on device path protocol.
Expand Down
4 changes: 2 additions & 2 deletions DcpmPkg/driver/Protocol/Driver/NvmDimmConfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -5369,7 +5369,7 @@ UpdateFw(
goto Finish;
}

ReturnCode = OpenFile(pFileName, &FileHandle, pWorkingDirectory, FALSE);
ReturnCode = OpenFileBinary(pFileName, &FileHandle, pWorkingDirectory, FALSE);
if (EFI_ERROR(ReturnCode)) {
NVDIMM_DBG("OpenFile returned: " FORMAT_EFI_STATUS ".\n", ReturnCode);
pCommandStatus->GeneralStatus = NVM_ERR_FILE_NOT_FOUND;
Expand All @@ -5396,7 +5396,7 @@ UpdateFw(
pFwImageInfo->Size = pFileHeader->Size;
}

ReturnCode = OpenFile(pFileName, &FileHandle, pWorkingDirectory, FALSE);
ReturnCode = OpenFileBinary(pFileName, &FileHandle, pWorkingDirectory, FALSE);
if (EFI_ERROR(ReturnCode)) {
NVDIMM_ERR("Failed to open the file");
pCommandStatus->GeneralStatus = NVM_ERR_UNKNOWN;
Expand Down
12 changes: 11 additions & 1 deletion src/os/efi_shim/os_efi_simple_file_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ file_open(
swprintf_s(pFc->filename, MAX_W_FILE_NAME_SIZE, FORMAT_STR, FileName);

if ((OpenMode & EFI_FILE_MODE_READ) &&
(OpenMode & EFI_FILE_MODE_WRITE))
(OpenMode & EFI_FILE_MODE_WRITE))
{
flags |= O_RDWR;
}
Expand All @@ -137,6 +137,16 @@ file_open(
flags |= O_CREAT;
}

#ifdef OS_BUILD
#ifdef _MSC_VER
if (OpenMode & EFI_FILE_MODE_BINARY)
{
flags |= _O_BINARY;
}
#endif
#endif


#ifndef _MSC_VER
pFc->fd = _open(pFc->filename_ascii, flags, FILE_MODE);
#else
Expand Down

0 comments on commit 570870e

Please sign in to comment.