diff --git a/fastboot/device/flashing.cpp b/fastboot/device/flashing.cpp index 9b5d2cd25..3f9bcdc14 100644 --- a/fastboot/device/flashing.cpp +++ b/fastboot/device/flashing.cpp @@ -16,6 +16,7 @@ #include "flashing.h" #include +#include #include #include @@ -77,9 +78,20 @@ void WipeOverlayfsForPartition(FastbootDevice* device, const std::string& partit int FlashRawDataChunk(int fd, const char* data, size_t len) { size_t ret = 0; + const size_t max_write_size = 1048576; + void* aligned_buffer; + + if (posix_memalign(&aligned_buffer, 4096, max_write_size)) { + PLOG(ERROR) << "Failed to allocate write buffer"; + return -ENOMEM; + } + + auto aligned_buffer_unique_ptr = std::unique_ptr{aligned_buffer, free}; + while (ret < len) { - int this_len = std::min(static_cast(1048576UL * 8), len - ret); - int this_ret = write(fd, data, this_len); + int this_len = std::min(max_write_size, len - ret); + memcpy(aligned_buffer_unique_ptr.get(), data, this_len); + int this_ret = write(fd, aligned_buffer_unique_ptr.get(), this_len); if (this_ret < 0) { PLOG(ERROR) << "Failed to flash data of len " << len; return -1; @@ -147,7 +159,7 @@ static void CopyAVBFooter(std::vector* data, const uint64_t block_device_s int Flash(FastbootDevice* device, const std::string& partition_name) { PartitionHandle handle; - if (!OpenPartition(device, partition_name, &handle)) { + if (!OpenPartition(device, partition_name, &handle, O_WRONLY | O_DIRECT)) { return -ENOENT; }