Douglas Anderson has uploaded this change for review.

View Change

linux_mtd: Switch fopen() to open() for MTD devices

There's no benefit to using the higher level "FILE *" for file
operations for writing to mtd devices. Let's move to the lower level
ones which matches the kernel more closely.

Signed-off-by: Douglas Anderson <dianders@chromium.org>
Change-Id: Ifeb00b049ba5aa0bc8fdc591ac1f9861ad5d428d
---
M linux_mtd.c
1 file changed, 16 insertions(+), 17 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/06/50206/1
diff --git a/linux_mtd.c b/linux_mtd.c
index 22702e9..0af0c0b 100644
--- a/linux_mtd.c
+++ b/linux_mtd.c
@@ -31,7 +31,7 @@
#define LINUX_DEV_ROOT "/dev"
#define LINUX_MTD_SYSFS_ROOT "/sys/class/mtd"

-static FILE *dev_fp = NULL;
+static int dev_fileno = -1;

static int mtd_device_is_writeable;

@@ -189,7 +189,7 @@
unsigned int eb_size = flash->chip->block_erasers[0].eraseblocks[0].size;
unsigned int i;

- if (fseek(dev_fp, start, SEEK_SET) != 0) {
+ if (lseek(dev_fileno, start, SEEK_SET) == (off_t)-1) {
msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno));
return 1;
}
@@ -203,7 +203,7 @@
unsigned int step = eb_size - ((start + i) % eb_size);
step = min(step, len - i);

- if (fread(buf + i, step, 1, dev_fp) != 1) {
+ if (read(dev_fileno, buf + i, step) != step) {
msg_perr("Cannot read 0x%06x bytes at 0x%06x: %s\n",
step, start + i, strerror(errno));
return 1;
@@ -225,7 +225,7 @@
if (!mtd_device_is_writeable)
return 1;

- if (fseek(dev_fp, start, SEEK_SET) != 0) {
+ if (lseek(dev_fileno, start, SEEK_SET) == (off_t)-1) {
msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno));
return 1;
}
@@ -240,15 +240,17 @@
unsigned int step = chunksize - ((start + i) % chunksize);
step = min(step, len - i);

- if (fwrite(buf + i, step, 1, dev_fp) != 1) {
+ if (write(dev_fileno, buf + i, step) != step) {
msg_perr("Cannot write 0x%06x bytes at 0x%06x\n", step, start + i);
return 1;
}

- if (fflush(dev_fp) == EOF) {
- msg_perr("Failed to flush buffer: %s\n", strerror(errno));
- return 1;
- }
+ /*
+ * MTD layer on Linux doesn't implement flush so the next call
+ * is expected to return an error. We'll still call it just
+ * in case it's ever useful.
+ */
+ fsync(dev_fileno);

i += step;
}
@@ -280,7 +282,7 @@
.length = mtd_erasesize,
};

- if (ioctl(fileno(dev_fp), MEMERASE, &erase_info) == -1) {
+ if (ioctl(dev_fileno, MEMERASE, &erase_info) == -1) {
msg_perr("%s: ioctl: %s\n", __func__, strerror(errno));
return 1;
}
@@ -336,13 +338,10 @@
goto linux_mtd_setup_exit;

/* open file stream and go! */
- if ((dev_fp = fopen(dev_path, "r+")) == NULL) {
+ if ((dev_fileno = open(dev_path, O_RDWR)) == -1) {
msg_perr("Cannot open file stream for %s\n", dev_path);
goto linux_mtd_setup_exit;
}
- ret = setvbuf(dev_fp, NULL, _IONBF, 0);
- if (ret)
- msg_pwarn("Failed to set MTD device to unbuffered: %d\n", ret);

msg_pinfo("Opened %s successfully\n", dev_path);

@@ -353,9 +352,9 @@

static int linux_mtd_shutdown(void *data)
{
- if (dev_fp != NULL) {
- fclose(dev_fp);
- dev_fp = NULL;
+ if (dev_fileno != -1) {
+ close(dev_fileno);
+ dev_fileno = -1;
}

return 0;

To view, visit change 50206. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Ifeb00b049ba5aa0bc8fdc591ac1f9861ad5d428d
Gerrit-Change-Number: 50206
Gerrit-PatchSet: 1
Gerrit-Owner: Douglas Anderson <dianders@chromium.org>
Gerrit-MessageType: newchange