| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 | #pragma once | 
|  | 17 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 18 | #include <optional> | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 19 | #include <string> | 
|  | 20 |  | 
| Konstantin Vyshetsky | 1cee2ed | 2022-03-17 17:57:28 -0700 | [diff] [blame] | 21 | #include <android-base/file.h> | 
|  | 22 | #include <android-base/logging.h> | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 23 | #include <android-base/unique_fd.h> | 
| David Anderson | 2324349 | 2019-12-17 00:58:31 -0800 | [diff] [blame] | 24 | #include <fstab/fstab.h> | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 25 | #include <liblp/liblp.h> | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 26 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 27 | // Logical partitions are only mapped to a block device as needed, and | 
|  | 28 | // immediately unmapped when no longer needed. In order to enforce this we | 
|  | 29 | // require accessing partitions through a Handle abstraction, which may perform | 
|  | 30 | // additional operations after closing its file descriptor. | 
|  | 31 | class PartitionHandle { | 
|  | 32 | public: | 
|  | 33 | PartitionHandle() {} | 
|  | 34 | explicit PartitionHandle(const std::string& path) : path_(path) {} | 
| David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 35 | PartitionHandle(const std::string& path, std::function<void()>&& closer) | 
|  | 36 | : path_(path), closer_(std::move(closer)) {} | 
|  | 37 | PartitionHandle(PartitionHandle&& other) = default; | 
|  | 38 | PartitionHandle& operator=(PartitionHandle&& other) = default; | 
|  | 39 | ~PartitionHandle() { | 
|  | 40 | if (closer_) { | 
|  | 41 | // Make sure the device is closed first. | 
|  | 42 | fd_ = {}; | 
|  | 43 | closer_(); | 
|  | 44 | } | 
|  | 45 | } | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 46 | const std::string& path() const { return path_; } | 
|  | 47 | int fd() const { return fd_.get(); } | 
| Konstantin Vyshetsky | 1cee2ed | 2022-03-17 17:57:28 -0700 | [diff] [blame] | 48 | bool Open(int flags) { | 
|  | 49 | flags |= (O_EXCL | O_CLOEXEC | O_BINARY); | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 50 |  | 
| Konstantin Vyshetsky | 1cee2ed | 2022-03-17 17:57:28 -0700 | [diff] [blame] | 51 | // Attempts to open a second device can fail with EBUSY if the device is already open. | 
|  | 52 | // Explicitly close any previously opened devices as unique_fd won't close them until | 
|  | 53 | // after the attempt to open. | 
|  | 54 | fd_.reset(); | 
|  | 55 |  | 
|  | 56 | fd_ = android::base::unique_fd(TEMP_FAILURE_RETRY(open(path_.c_str(), flags))); | 
|  | 57 | if (fd_ < 0) { | 
|  | 58 | PLOG(ERROR) << "Failed to open block device: " << path_; | 
|  | 59 | return false; | 
|  | 60 | } | 
|  | 61 | flags_ = flags; | 
|  | 62 |  | 
|  | 63 | return true; | 
|  | 64 | } | 
|  | 65 | bool Reset(int flags) { | 
|  | 66 | if (fd_.ok() && (flags | O_EXCL | O_CLOEXEC | O_BINARY) == flags_) { | 
|  | 67 | return true; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | off_t offset = fd_.ok() ? lseek(fd_.get(), 0, SEEK_CUR) : 0; | 
|  | 71 | if (offset < 0) { | 
|  | 72 | PLOG(ERROR) << "Failed lseek on block device: " << path_; | 
|  | 73 | return false; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | sync(); | 
|  | 77 |  | 
|  | 78 | if (Open(flags) == false) { | 
|  | 79 | return false; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | if (lseek(fd_.get(), offset, SEEK_SET) != offset) { | 
|  | 83 | PLOG(ERROR) << "Failed lseek on block device: " << path_; | 
|  | 84 | return false; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | return true; | 
|  | 88 | } | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 89 | private: | 
|  | 90 | std::string path_; | 
|  | 91 | android::base::unique_fd fd_; | 
| Konstantin Vyshetsky | 1cee2ed | 2022-03-17 17:57:28 -0700 | [diff] [blame] | 92 | int flags_; | 
| David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 93 | std::function<void()> closer_; | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 94 | }; | 
|  | 95 |  | 
| David Anderson | 2324349 | 2019-12-17 00:58:31 -0800 | [diff] [blame] | 96 | class AutoMountMetadata { | 
|  | 97 | public: | 
|  | 98 | AutoMountMetadata(); | 
|  | 99 | ~AutoMountMetadata(); | 
|  | 100 | explicit operator bool() const { return mounted_; } | 
|  | 101 |  | 
|  | 102 | private: | 
|  | 103 | android::fs_mgr::Fstab fstab_; | 
|  | 104 | bool mounted_ = false; | 
|  | 105 | bool should_unmount_ = false; | 
|  | 106 | }; | 
|  | 107 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 108 | class FastbootDevice; | 
|  | 109 |  | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 110 | // On normal devices, the super partition is always named "super". On retrofit | 
|  | 111 | // devices, the name must be derived from the partition name or current slot. | 
|  | 112 | // This helper assists in choosing the correct super for a given partition | 
|  | 113 | // name. | 
|  | 114 | std::string GetSuperSlotSuffix(FastbootDevice* device, const std::string& partition_name); | 
|  | 115 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 116 | std::optional<std::string> FindPhysicalPartition(const std::string& name); | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 117 | bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, | 
| David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 118 | bool* is_zero_length = nullptr); | 
| Yifan Hong | c4073d3 | 2021-02-18 12:35:42 -0800 | [diff] [blame] | 119 |  | 
| Konstantin Vyshetsky | 81cc119 | 2021-11-04 10:27:06 -0700 | [diff] [blame] | 120 | // Partition is O_WRONLY by default, caller should pass O_RDONLY for reading. | 
|  | 121 | // Caller may pass additional flags if needed. (O_EXCL | O_CLOEXEC | O_BINARY) | 
|  | 122 | // will be logically ORed internally. | 
| Yifan Hong | c4073d3 | 2021-02-18 12:35:42 -0800 | [diff] [blame] | 123 | bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle, | 
| Konstantin Vyshetsky | 81cc119 | 2021-11-04 10:27:06 -0700 | [diff] [blame] | 124 | int flags = O_WRONLY); | 
| Yifan Hong | c4073d3 | 2021-02-18 12:35:42 -0800 | [diff] [blame] | 125 |  | 
| Kelvin Zhang | 6befe78 | 2022-06-21 14:20:54 -0700 | [diff] [blame] | 126 | bool GetSlotNumber(const std::string& slot, int32_t* number); | 
| David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 127 | std::vector<std::string> ListPartitions(FastbootDevice* device); | 
| Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 128 | bool GetDeviceLockStatus(); | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 129 |  | 
|  | 130 | // Update all copies of metadata. | 
| David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 131 | bool UpdateAllPartitionMetadata(FastbootDevice* device, const std::string& super_name, | 
| David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 132 | const android::fs_mgr::LpMetadata& metadata); |