Loading fastboot/device/commands.cpp +1 −1 Original line number Original line Diff line number Diff line Loading @@ -725,7 +725,7 @@ class PartitionFetcher { return false; return false; } } if (!OpenPartition(device_, partition_name_, &handle_, true /* read */)) { if (!OpenPartition(device_, partition_name_, &handle_, O_RDONLY)) { ret_ = device_->WriteFail( ret_ = device_->WriteFail( android::base::StringPrintf("Cannot open %s", partition_name_.c_str())); android::base::StringPrintf("Cannot open %s", partition_name_.c_str())); return false; return false; Loading fastboot/device/flashing.cpp +15 −3 Original line number Original line Diff line number Diff line Loading @@ -16,6 +16,7 @@ #include "flashing.h" #include "flashing.h" #include <fcntl.h> #include <fcntl.h> #include <string.h> #include <sys/stat.h> #include <sys/stat.h> #include <unistd.h> #include <unistd.h> Loading Loading @@ -77,9 +78,20 @@ void WipeOverlayfsForPartition(FastbootDevice* device, const std::string& partit int FlashRawDataChunk(int fd, const char* data, size_t len) { int FlashRawDataChunk(int fd, const char* data, size_t len) { size_t ret = 0; 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<void, decltype(&free)>{aligned_buffer, free}; while (ret < len) { while (ret < len) { int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret); int this_len = std::min(max_write_size, len - ret); int this_ret = write(fd, data, this_len); 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) { if (this_ret < 0) { PLOG(ERROR) << "Failed to flash data of len " << len; PLOG(ERROR) << "Failed to flash data of len " << len; return -1; return -1; Loading Loading @@ -147,7 +159,7 @@ static void CopyAVBFooter(std::vector<char>* data, const uint64_t block_device_s int Flash(FastbootDevice* device, const std::string& partition_name) { int Flash(FastbootDevice* device, const std::string& partition_name) { PartitionHandle handle; PartitionHandle handle; if (!OpenPartition(device, partition_name, &handle)) { if (!OpenPartition(device, partition_name, &handle, O_WRONLY | O_DIRECT)) { return -ENOENT; return -ENOENT; } } Loading fastboot/device/utility.cpp +1 −2 Original line number Original line Diff line number Diff line Loading @@ -78,7 +78,7 @@ bool OpenLogicalPartition(FastbootDevice* device, const std::string& partition_n } // namespace } // namespace bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, bool read) { int flags) { // We prioritize logical partitions over physical ones, and do this // We prioritize logical partitions over physical ones, and do this // consistently for other partition operations (like getvar:partition-size). // consistently for other partition operations (like getvar:partition-size). if (LogicalPartitionExists(device, name)) { if (LogicalPartitionExists(device, name)) { Loading @@ -90,7 +90,6 @@ bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHan return false; return false; } } int flags = (read ? O_RDONLY : O_WRONLY); flags |= (O_EXCL | O_CLOEXEC | O_BINARY); flags |= (O_EXCL | O_CLOEXEC | O_BINARY); unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), flags))); unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), flags))); if (fd < 0) { if (fd < 0) { Loading fastboot/device/utility.h +4 −2 Original line number Original line Diff line number Diff line Loading @@ -76,9 +76,11 @@ std::optional<std::string> FindPhysicalPartition(const std::string& name); bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, bool* is_zero_length = nullptr); bool* is_zero_length = nullptr); // If read, partition is readonly. Else it is write only. // Partition is O_WRONLY by default, caller should pass O_RDONLY for reading. // Caller may pass additional flags if needed. (O_EXCL | O_CLOEXEC | O_BINARY) // will be logically ORed internally. bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, bool read = false); int flags = O_WRONLY); bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number); bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number); std::vector<std::string> ListPartitions(FastbootDevice* device); std::vector<std::string> ListPartitions(FastbootDevice* device); Loading Loading
fastboot/device/commands.cpp +1 −1 Original line number Original line Diff line number Diff line Loading @@ -725,7 +725,7 @@ class PartitionFetcher { return false; return false; } } if (!OpenPartition(device_, partition_name_, &handle_, true /* read */)) { if (!OpenPartition(device_, partition_name_, &handle_, O_RDONLY)) { ret_ = device_->WriteFail( ret_ = device_->WriteFail( android::base::StringPrintf("Cannot open %s", partition_name_.c_str())); android::base::StringPrintf("Cannot open %s", partition_name_.c_str())); return false; return false; Loading
fastboot/device/flashing.cpp +15 −3 Original line number Original line Diff line number Diff line Loading @@ -16,6 +16,7 @@ #include "flashing.h" #include "flashing.h" #include <fcntl.h> #include <fcntl.h> #include <string.h> #include <sys/stat.h> #include <sys/stat.h> #include <unistd.h> #include <unistd.h> Loading Loading @@ -77,9 +78,20 @@ void WipeOverlayfsForPartition(FastbootDevice* device, const std::string& partit int FlashRawDataChunk(int fd, const char* data, size_t len) { int FlashRawDataChunk(int fd, const char* data, size_t len) { size_t ret = 0; 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<void, decltype(&free)>{aligned_buffer, free}; while (ret < len) { while (ret < len) { int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret); int this_len = std::min(max_write_size, len - ret); int this_ret = write(fd, data, this_len); 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) { if (this_ret < 0) { PLOG(ERROR) << "Failed to flash data of len " << len; PLOG(ERROR) << "Failed to flash data of len " << len; return -1; return -1; Loading Loading @@ -147,7 +159,7 @@ static void CopyAVBFooter(std::vector<char>* data, const uint64_t block_device_s int Flash(FastbootDevice* device, const std::string& partition_name) { int Flash(FastbootDevice* device, const std::string& partition_name) { PartitionHandle handle; PartitionHandle handle; if (!OpenPartition(device, partition_name, &handle)) { if (!OpenPartition(device, partition_name, &handle, O_WRONLY | O_DIRECT)) { return -ENOENT; return -ENOENT; } } Loading
fastboot/device/utility.cpp +1 −2 Original line number Original line Diff line number Diff line Loading @@ -78,7 +78,7 @@ bool OpenLogicalPartition(FastbootDevice* device, const std::string& partition_n } // namespace } // namespace bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, bool read) { int flags) { // We prioritize logical partitions over physical ones, and do this // We prioritize logical partitions over physical ones, and do this // consistently for other partition operations (like getvar:partition-size). // consistently for other partition operations (like getvar:partition-size). if (LogicalPartitionExists(device, name)) { if (LogicalPartitionExists(device, name)) { Loading @@ -90,7 +90,6 @@ bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHan return false; return false; } } int flags = (read ? O_RDONLY : O_WRONLY); flags |= (O_EXCL | O_CLOEXEC | O_BINARY); flags |= (O_EXCL | O_CLOEXEC | O_BINARY); unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), flags))); unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), flags))); if (fd < 0) { if (fd < 0) { Loading
fastboot/device/utility.h +4 −2 Original line number Original line Diff line number Diff line Loading @@ -76,9 +76,11 @@ std::optional<std::string> FindPhysicalPartition(const std::string& name); bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, bool* is_zero_length = nullptr); bool* is_zero_length = nullptr); // If read, partition is readonly. Else it is write only. // Partition is O_WRONLY by default, caller should pass O_RDONLY for reading. // Caller may pass additional flags if needed. (O_EXCL | O_CLOEXEC | O_BINARY) // will be logically ORed internally. bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, bool read = false); int flags = O_WRONLY); bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number); bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number); std::vector<std::string> ListPartitions(FastbootDevice* device); std::vector<std::string> ListPartitions(FastbootDevice* device); Loading