Skip to content

Commit 570870e

Browse files
committed
Switching to explicit binary open when in windows for FW file
Signed-off-by: Glenn L Diviney <[email protected]>
1 parent 815b2eb commit 570870e

File tree

4 files changed

+136
-14
lines changed

4 files changed

+136
-14
lines changed

DcpmPkg/common/Utility.c

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,63 @@ OpenFile(
998998
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
999999
IN BOOLEAN CreateFileFlag
10001000
)
1001+
{
1002+
return OpenFileWithFlag(pArgFilePath, pFileHandle, pCurrentDirectory, CreateFileFlag, FALSE);
1003+
}
1004+
1005+
/**
1006+
Open file or create new file in binary mode.
1007+
1008+
@param[in] pArgFilePath path to a file that will be opened
1009+
@param[out] pFileHandle output handler
1010+
@param[in, optional] pCurrentDirectory is the current directory path to where
1011+
we should start to search for the file.
1012+
@param[in] CreateFileFlag TRUE to create new file or FALSE to open
1013+
existing file
1014+
1015+
@retval EFI_SUCCESS
1016+
@retval EFI_INVALID_PARAMETER pFilePath is NULL or empty or pFileHandle is NULL
1017+
@retval EFI_PROTOCOL_ERROR if there is no EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
1018+
**/
1019+
EFI_STATUS
1020+
OpenFileBinary(
1021+
IN CHAR16 *pArgFilePath,
1022+
OUT EFI_FILE_HANDLE *pFileHandle,
1023+
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
1024+
IN BOOLEAN CreateFileFlag
1025+
)
1026+
{
1027+
#ifdef OS_BUILD
1028+
#ifdef _MSC_VER
1029+
return OpenFileWithFlag(pArgFilePath, pFileHandle, pCurrentDirectory, CreateFileFlag, TRUE);
1030+
#endif
1031+
#endif
1032+
return OpenFileWithFlag(pArgFilePath, pFileHandle, pCurrentDirectory, CreateFileFlag, FALSE);
1033+
}
1034+
1035+
/**
1036+
Open file or create new file with the proper flags.
1037+
1038+
@param[in] pArgFilePath path to a file that will be opened
1039+
@param[out] pFileHandle output handler
1040+
@param[in, optional] pCurrentDirectory is the current directory path to where
1041+
we should start to search for the file.
1042+
@param[in] CreateFileFlag - TRUE to create new file or FALSE to open
1043+
existing file
1044+
@param[in] binary - use binary open
1045+
1046+
@retval EFI_SUCCESS
1047+
@retval EFI_INVALID_PARAMETER pFilePath is NULL or empty or pFileHandle is NULL
1048+
@retval EFI_PROTOCOL_ERROR if there is no EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
1049+
**/
1050+
EFI_STATUS
1051+
OpenFileWithFlag(
1052+
IN CHAR16 *pArgFilePath,
1053+
OUT EFI_FILE_HANDLE *pFileHandle,
1054+
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
1055+
IN BOOLEAN CreateFileFlag,
1056+
BOOLEAN binary
1057+
)
10011058
{
10021059
EFI_STATUS Rc = EFI_SUCCESS;
10031060
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pVolume = NULL;
@@ -1058,13 +1115,13 @@ OpenFile(
10581115
Get the file system protocol
10591116
**/
10601117
Rc = gBS->OpenProtocol(
1061-
pHandles[Index],
1062-
&gEfiSimpleFileSystemProtocolGuid,
1063-
(VOID *) &pVolume,
1064-
NULL,
1065-
NULL,
1066-
EFI_OPEN_PROTOCOL_GET_PROTOCOL
1067-
);
1118+
pHandles[Index],
1119+
&gEfiSimpleFileSystemProtocolGuid,
1120+
(VOID *) &pVolume,
1121+
NULL,
1122+
NULL,
1123+
EFI_OPEN_PROTOCOL_GET_PROTOCOL
1124+
);
10681125
if (EFI_ERROR(Rc)) {
10691126
goto AfterHandles;
10701127
}
@@ -1079,11 +1136,19 @@ OpenFile(
10791136

10801137
if (CreateFileFlag) {
10811138
// if EFI_FILE_MODE_CREATE then also EFI_FILE_MODE_READ and EFI_FILE_MODE_WRITE are needed.
1082-
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath,
1083-
EFI_FILE_MODE_CREATE|EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE,
1084-
0);
1139+
if (binary) {
1140+
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath,
1141+
EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_BINARY, 0);
1142+
} else {
1143+
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath,
1144+
EFI_FILE_MODE_CREATE | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0);
1145+
}
10851146
} else {
1086-
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath, EFI_FILE_MODE_READ, 0);
1147+
if (binary) {
1148+
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath, EFI_FILE_MODE_READ | EFI_FILE_MODE_BINARY, 0);
1149+
} else {
1150+
Rc = RootDirHandle->Open(RootDirHandle, pFileHandle, pFilePath, EFI_FILE_MODE_READ, 0);
1151+
}
10871152
}
10881153

10891154
RootDirHandle->Close(RootDirHandle);

DcpmPkg/common/Utility.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
int _fltused();
2323
#endif
2424

25+
#define EFI_FILE_MODE_BINARY 0x8000000000000064ULL
2526

2627
/**
2728
The Config Protocol version bytes definition
@@ -660,6 +661,52 @@ OpenFile(
660661
IN BOOLEAN CreateFileFlag
661662
);
662663

664+
/**
665+
Open file or create new file in binary mode.
666+
667+
@param[in] pArgFilePath path to a file that will be opened
668+
@param[out] pFileHandle output handler
669+
@param[in, optional] pCurrentDirectory is the current directory path to where
670+
we should start to search for the file.
671+
@param[in] CreateFileFlag TRUE to create new file or FALSE to open
672+
existing file
673+
674+
@retval EFI_SUCCESS
675+
@retval EFI_INVALID_PARAMETER pFilePath is NULL or empty or pFileHandle is NULL
676+
@retval EFI_PROTOCOL_ERROR if there is no EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
677+
**/
678+
EFI_STATUS
679+
OpenFileBinary(
680+
IN CHAR16 *pArgFilePath,
681+
OUT EFI_FILE_HANDLE *pFileHandle,
682+
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
683+
IN BOOLEAN CreateFileFlag
684+
);
685+
686+
/**
687+
Open file or create new file with the proper flags.
688+
689+
@param[in] pArgFilePath path to a file that will be opened
690+
@param[out] pFileHandle output handler
691+
@param[in, optional] pCurrentDirectory is the current directory path to where
692+
we should start to search for the file.
693+
@param[in] CreateFileFlag - TRUE to create new file or FALSE to open
694+
existing file
695+
@param[in] binary - use binary open
696+
697+
@retval EFI_SUCCESS
698+
@retval EFI_INVALID_PARAMETER pFilePath is NULL or empty or pFileHandle is NULL
699+
@retval EFI_PROTOCOL_ERROR if there is no EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
700+
**/
701+
EFI_STATUS
702+
OpenFileWithFlag(
703+
IN CHAR16 *pArgFilePath,
704+
OUT EFI_FILE_HANDLE *pFileHandle,
705+
IN CONST CHAR16 *pCurrentDirectory OPTIONAL,
706+
IN BOOLEAN CreateFileFlag,
707+
BOOLEAN binary
708+
);
709+
663710
/**
664711
Open file or create new file based on device path protocol.
665712

DcpmPkg/driver/Protocol/Driver/NvmDimmConfig.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5369,7 +5369,7 @@ UpdateFw(
53695369
goto Finish;
53705370
}
53715371

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

5399-
ReturnCode = OpenFile(pFileName, &FileHandle, pWorkingDirectory, FALSE);
5399+
ReturnCode = OpenFileBinary(pFileName, &FileHandle, pWorkingDirectory, FALSE);
54005400
if (EFI_ERROR(ReturnCode)) {
54015401
NVDIMM_ERR("Failed to open the file");
54025402
pCommandStatus->GeneralStatus = NVM_ERR_UNKNOWN;

src/os/efi_shim/os_efi_simple_file_protocol.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ file_open(
119119
swprintf_s(pFc->filename, MAX_W_FILE_NAME_SIZE, FORMAT_STR, FileName);
120120

121121
if ((OpenMode & EFI_FILE_MODE_READ) &&
122-
(OpenMode & EFI_FILE_MODE_WRITE))
122+
(OpenMode & EFI_FILE_MODE_WRITE))
123123
{
124124
flags |= O_RDWR;
125125
}
@@ -137,6 +137,16 @@ file_open(
137137
flags |= O_CREAT;
138138
}
139139

140+
#ifdef OS_BUILD
141+
#ifdef _MSC_VER
142+
if (OpenMode & EFI_FILE_MODE_BINARY)
143+
{
144+
flags |= _O_BINARY;
145+
}
146+
#endif
147+
#endif
148+
149+
140150
#ifndef _MSC_VER
141151
pFc->fd = _open(pFc->filename_ascii, flags, FILE_MODE);
142152
#else

0 commit comments

Comments
 (0)