From c4073d3e13ac7a769adbf95d5042407096e22f8a Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Thu, 18 Feb 2021 12:35:42 -0800 Subject: [PATCH] fastbootd: add read arg to OpenPartition Allow it to read partitions. Bug: 173654501 Test: pass Change-Id: I115e84734dd258243ca3f4f1b373b06adcaa4080 --- fastboot/device/utility.cpp | 6 ++++-- fastboot/device/utility.h | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/fastboot/device/utility.cpp b/fastboot/device/utility.cpp index 7c6ac8993..4c847987f 100644 --- a/fastboot/device/utility.cpp +++ b/fastboot/device/utility.cpp @@ -77,7 +77,8 @@ bool OpenLogicalPartition(FastbootDevice* device, const std::string& partition_n } // namespace -bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle) { +bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, + bool read) { // We prioritize logical partitions over physical ones, and do this // consistently for other partition operations (like getvar:partition-size). if (LogicalPartitionExists(device, name)) { @@ -89,7 +90,8 @@ bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHan return false; } - unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), O_WRONLY | O_EXCL))); + int flags = O_EXCL | (read ? O_RDONLY : O_WRONLY); + unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), flags))); if (fd < 0) { PLOG(ERROR) << "Failed to open block device: " << handle->path(); return false; diff --git a/fastboot/device/utility.h b/fastboot/device/utility.h index 3b71ef006..c2646d718 100644 --- a/fastboot/device/utility.h +++ b/fastboot/device/utility.h @@ -75,7 +75,11 @@ std::string GetSuperSlotSuffix(FastbootDevice* device, const std::string& partit std::optional FindPhysicalPartition(const std::string& name); bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, bool* is_zero_length = nullptr); -bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle); + +// If read, partition is readonly. Else it is write only. +bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, + bool read = false); + bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number); std::vector ListPartitions(FastbootDevice* device); bool GetDeviceLockStatus();