Skip to content

Commit a9e655f

Browse files
brgoingigaw
authored andcommitted
nvme: Open files using nvme_open_rawdata to fix Windows issues
Reading files that may have raw binary data on Windows without using the O_BINARY flag when opening can cause some bytes to be misinterpreted or mishandled. This can lead to bad data or truncated reads. Use nvme_open_rawdata to ensure that O_BINARY is added if supported. Fixes firmware file reading issues found during testing on Windows, as well as other potential issues on Windows. Signed-off-by: Broc Going <bgoing@micron.com>
1 parent 736b62c commit a9e655f

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

nvme-rpmb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static int read_file(const char *file, unsigned char **data, unsigned int *len)
171171

172172
if (file == NULL) return err;
173173

174-
if ((fd = open(file, O_RDONLY)) < 0) {
174+
if ((fd = nvme_open_rawdata(file, O_RDONLY)) < 0) {
175175
fprintf(stderr, "Failed to open %s: %s\n", file, libnvme_strerror(errno));
176176
return fd;
177177
}

nvme.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,7 @@ static int io_mgmt_send(int argc, char **argv, struct command *acmd, struct plug
21962196
}
21972197

21982198
if (cfg.file) {
2199-
dfd = open(cfg.file, O_RDONLY);
2199+
dfd = nvme_open_rawdata(cfg.file, O_RDONLY);
22002200
if (dfd < 0) {
22012201
nvme_show_perror(cfg.file);
22022202
return -errno;
@@ -5221,7 +5221,7 @@ static int fw_download(int argc, char **argv, struct command *acmd, struct plugi
52215221
return err;
52225222
}
52235223

5224-
fw_fd = open(cfg.fw, O_RDONLY);
5224+
fw_fd = nvme_open_rawdata(cfg.fw, O_RDONLY);
52255225
cfg.offset <<= 2;
52265226
if (fw_fd < 0) {
52275227
nvme_show_error("Failed to open firmware file %s: %s", cfg.fw, libnvme_strerror(errno));
@@ -7184,7 +7184,7 @@ static int set_feature(int argc, char **argv, struct command *acmd, struct plugi
71847184
memcpy(buf, &cfg.value, NVME_FEAT_TIMESTAMP_DATA_SIZE);
71857185
} else {
71867186
if (strlen(cfg.file))
7187-
ffd = open(cfg.file, O_RDONLY);
7187+
ffd = nvme_open_rawdata(cfg.file, O_RDONLY);
71887188

71897189
if (ffd < 0) {
71907190
nvme_show_error("Failed to open file %s: %s",
@@ -7296,7 +7296,7 @@ static int sec_send(int argc, char **argv, struct command *acmd, struct plugin *
72967296
sec_fd = STDIN_FILENO;
72977297
sec_size = cfg.tl;
72987298
} else {
7299-
sec_fd = open(cfg.file, O_RDONLY);
7299+
sec_fd = nvme_open_rawdata(cfg.file, O_RDONLY);
73007300
if (sec_fd < 0) {
73017301
nvme_show_error("Failed to open %s: %s", cfg.file, libnvme_strerror(errno));
73027302
return -EINVAL;
@@ -7433,7 +7433,7 @@ static int dir_send(int argc, char **argv, struct command *acmd, struct plugin *
74337433

74347434
if (buf) {
74357435
if (strlen(cfg.file)) {
7436-
ffd = open(cfg.file, O_RDONLY);
7436+
ffd = nvme_open_rawdata(cfg.file, O_RDONLY);
74377437
if (ffd <= 0) {
74387438
nvme_show_error("Failed to open file %s: %s",
74397439
cfg.file, libnvme_strerror(errno));

0 commit comments

Comments
 (0)