UPSTREAM: linux_mtd: Disable buffering on the mtd device

We open the device node for the MTD device with this:
  dev_fp = fopen(dev_path, "r+")

In C fopen() is allowed to provide _buffered_ access to the file.
That means that the standard library is allowed to read ahead and/or
return cached data.  That's really not what we want for something like
this.  Let's turn it off.

This fixes a problem where flashrom would sometimes fail to "verify"
that it erased the flash.  The error message would look something like
this:

Erasing and writing flash chip... FAILED at 0x0000e220! Expected=0xff, Found=0xe9, failed byte count from 0x0000e200-0x0000e2ff: 0xdc
 failed byte count from 0x0000e000-0x0000efff: 0xffffffff
 ERASE_FAILED
FAILED!
Uh oh. Erase/write failed. Checking if anything changed.

After the failure I could read the flash device with a new invocation
of flashrom and I would see that, indeed, the erase had worked.

Tracing in the kernel showed that when the failure happened we saw a
pattern that looked like this:
* Read 0x0b00 bytes starting at 0x0000d000
* Read 0x1000 bytes starting at 0x0000db00
* Erase 0x1000 bytes starting at 0x0000e000

...and then there was _not_ a read after the erase.  It can be assumed
that, since userspace had already read 0xdb00 - 0xeaff that it was
looking at old buffered data after the erase.

Signed-off-by: Douglas Anderson <dianders@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/50155
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-by: Patrick Rudolph <siro@das-labor.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
(cherry picked from commit a2f2f3f5ee1bb9c953f098f019c3ad09657cba38)

BRANCH=trogdor
BUG=b:178822068
TEST=Loop back and forth writing two BIOS files => no errors

Change-Id: I989afd83a33013b2756a0090d6b08245613215c6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/flashrom/+/2660502
Commit-Queue: Douglas Anderson <dianders@chromium.org>
Commit-Queue: Vadim Bendebury <vbendeb@chromium.org>
Tested-by: Douglas Anderson <dianders@chromium.org>
Auto-Submit: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Sean Paul <seanpaul@chromium.org>
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
(cherry picked from commit d45c6d041624b799751d33efc7bc48f5f2777e21)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/flashrom/+/2675716
Reviewed-by: Douglas Anderson <dianders@chromium.org>
1 file changed
tree: 1ef15f8598eafebf59bae1455cd6850692980fed
  1. Documentation/
  2. util/
  3. .gitattributes
  4. .gitignore
  5. 82802ab.c
  6. action_descriptor.c
  7. action_descriptor.h
  8. amd_imc.c
  9. archtest.c
  10. at45db.c
  11. atahpt.c
  12. atapromise.c
  13. atavia.c
  14. big_lock.c
  15. big_lock.h
  16. bitbang_spi.c
  17. board_enable.c
  18. buspirate_spi.c
  19. cbtable.c
  20. ch341a_spi.c
  21. chipdrivers.h
  22. chipset_enable.c
  23. cli_classic.c
  24. cli_common.c
  25. cli_output.c
  26. COPYING
  27. coreboot_tables.h
  28. cros_alias.c
  29. cros_ec.c
  30. cros_ec.h
  31. cros_ec_commands.h
  32. cros_ec_dev.c
  33. custom_baud.c
  34. custom_baud.h
  35. dediprog.c
  36. developerbox_spi.c
  37. digilent_spi.c
  38. dmi.c
  39. drkaiser.c
  40. dummyflasher.c
  41. edi.c
  42. edi.h
  43. en29lv640b.c
  44. endiantest.c
  45. ene.h
  46. ene_lpc.c
  47. file.c
  48. file.h
  49. file_lock.c
  50. flash.h
  51. flashchips.c
  52. flashchips.h
  53. flashrom.8.tmpl
  54. flashrom.c
  55. fmap.c
  56. fmap.h
  57. ft2232_spi.c
  58. gfxnvidia.c
  59. helpers.c
  60. hwaccess.c
  61. hwaccess.h
  62. i2c_helper.h
  63. i2c_helper_linux.c
  64. ich_descriptors.c
  65. ich_descriptors.h
  66. ichspi.c
  67. internal.c
  68. ipc_lock.h
  69. it8212.c
  70. it85spi.c
  71. it87spi.c
  72. jedec.c
  73. jlink_spi.c
  74. layout.c
  75. layout.h
  76. libflashrom.c
  77. libflashrom.h
  78. linux_mtd.c
  79. linux_spi.c
  80. lspcon_i2c_spi.c
  81. m29f400bt.c
  82. Makefile
  83. mcp6x_spi.c
  84. mec1308.c
  85. meson.build
  86. meson_options.txt
  87. mstarddc_spi.c
  88. ni845x_spi.c
  89. nic3com.c
  90. nicintel.c
  91. nicintel_eeprom.c
  92. nicintel_spi.c
  93. nicnatsemi.c
  94. nicrealtek.c
  95. ogp_spi.c
  96. opaque.c
  97. os.h
  98. OWNERS
  99. pcidev.c
  100. physmap.c
  101. pickit2_spi.c
  102. platform.h
  103. pony_spi.c
  104. power.c
  105. power.h
  106. PRESUBMIT.cfg
  107. print.c
  108. print_wiki.c
  109. processor_enable.c
  110. programmer.c
  111. programmer.h
  112. raiden_debug_spi.c
  113. rayer_spi.c
  114. README.chromiumos
  115. realtek_mst_i2c_spi.c
  116. s25f.c
  117. satamv.c
  118. satasii.c
  119. sb600spi.c
  120. search.c
  121. search.h
  122. serial.c
  123. serprog.c
  124. serprog.h
  125. sfdp.c
  126. spi.c
  127. spi.h
  128. spi25.c
  129. spi25_statusreg.c
  130. spi95.c
  131. sst28sf040.c
  132. sst49lfxxxc.c
  133. sst_fwhub.c
  134. stlinkv3_spi.c
  135. stm50.c
  136. stm50flw0x0x.c
  137. udelay.c
  138. usb_device.c
  139. usb_device.h
  140. usbblaster_spi.c
  141. usbdev.c
  142. w29ee011.c
  143. w39.c
  144. wbsio_spi.c
  145. writeprotect.c
  146. writeprotect.h