From 57b23d25eb7cebf58acbe8d9bbdebc2bb07d7b9a Mon Sep 17 00:00:00 2001 From: Konstantin Vyshetsky Date: Fri, 18 Mar 2022 10:25:46 -0700 Subject: [PATCH] fastbootd: reset file descriptor on unaligned writes Writes on file descriptors opened with O_DIRECT will fail if the buffer is not page aligned. This CL will reset the file descriptor without the O_DIRECT flag for such instances. Bug: 225108941 Signed-off-by: Konstantin Vyshetsky Change-Id: I841c84f5d2c0b9435b394c48b1bfcc2d51d771bb --- fastboot/device/flashing.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fastboot/device/flashing.cpp b/fastboot/device/flashing.cpp index e2b7cd2c6..06ffe0f68 100644 --- a/fastboot/device/flashing.cpp +++ b/fastboot/device/flashing.cpp @@ -91,6 +91,14 @@ int FlashRawDataChunk(PartitionHandle* handle, const char* data, size_t len) { while (ret < len) { int this_len = std::min(max_write_size, len - ret); memcpy(aligned_buffer_unique_ptr.get(), data, this_len); + // In case of non 4KB aligned writes, reopen without O_DIRECT flag + if (this_len & 0xFFF) { + if (handle->Reset(O_WRONLY) != true) { + PLOG(ERROR) << "Failed to reset file descriptor"; + return -1; + } + } + int this_ret = write(handle->fd(), aligned_buffer_unique_ptr.get(), this_len); if (this_ret < 0) { PLOG(ERROR) << "Failed to flash data of len " << len;