From 81cc119eff846ccb55af53f80b2ac55a50a0f039 Mon Sep 17 00:00:00 2001 From: Konstantin Vyshetsky Date: Thu, 4 Nov 2021 10:27:06 -0700 Subject: [PATCH] fastbootd: allow passage of flags to open partition Allow caller to pass additional flags when opening a partition. Obsolete usage of boolean read, and make previous callers use O_RDONLY instead. Explicitly OR (O_EXCL | O_CLOEXEC | O_BINARY) to keep existing design working as is. Test: flash locally and reach home screen Bug: 205151372 Signed-off-by: Konstantin Vyshetsky Change-Id: I48fbca459a17fcf0b0926ab339585e3bd8e31e35 --- fastboot/device/commands.cpp | 2 +- fastboot/device/utility.cpp | 3 +-- fastboot/device/utility.h | 6 ++++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/fastboot/device/commands.cpp b/fastboot/device/commands.cpp index 0a72812c2..4042531e5 100644 --- a/fastboot/device/commands.cpp +++ b/fastboot/device/commands.cpp @@ -725,7 +725,7 @@ class PartitionFetcher { return false; } - if (!OpenPartition(device_, partition_name_, &handle_, true /* read */)) { + if (!OpenPartition(device_, partition_name_, &handle_, O_RDONLY)) { ret_ = device_->WriteFail( android::base::StringPrintf("Cannot open %s", partition_name_.c_str())); return false; diff --git a/fastboot/device/utility.cpp b/fastboot/device/utility.cpp index 07ad9028c..97b5ad409 100644 --- a/fastboot/device/utility.cpp +++ b/fastboot/device/utility.cpp @@ -78,7 +78,7 @@ bool OpenLogicalPartition(FastbootDevice* device, const std::string& partition_n } // namespace bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, - bool read) { + int flags) { // We prioritize logical partitions over physical ones, and do this // consistently for other partition operations (like getvar:partition-size). if (LogicalPartitionExists(device, name)) { @@ -90,7 +90,6 @@ bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHan return false; } - int flags = (read ? O_RDONLY : O_WRONLY); flags |= (O_EXCL | O_CLOEXEC | O_BINARY); unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), flags))); if (fd < 0) { diff --git a/fastboot/device/utility.h b/fastboot/device/utility.h index c2646d718..1d81b7a88 100644 --- a/fastboot/device/utility.h +++ b/fastboot/device/utility.h @@ -76,9 +76,11 @@ std::optional FindPhysicalPartition(const std::string& name); bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, 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 read = false); + int flags = O_WRONLY); bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number); std::vector ListPartitions(FastbootDevice* device);