| 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 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 21 | #include <android-base/unique_fd.h> | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 22 | #include <android/hardware/boot/1.0/IBootControl.h> | 
 | 23 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 24 | // Logical partitions are only mapped to a block device as needed, and | 
 | 25 | // immediately unmapped when no longer needed. In order to enforce this we | 
 | 26 | // require accessing partitions through a Handle abstraction, which may perform | 
 | 27 | // additional operations after closing its file descriptor. | 
 | 28 | class PartitionHandle { | 
 | 29 |   public: | 
 | 30 |     PartitionHandle() {} | 
 | 31 |     explicit PartitionHandle(const std::string& path) : path_(path) {} | 
| David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 32 |     PartitionHandle(const std::string& path, std::function<void()>&& closer) | 
 | 33 |         : path_(path), closer_(std::move(closer)) {} | 
 | 34 |     PartitionHandle(PartitionHandle&& other) = default; | 
 | 35 |     PartitionHandle& operator=(PartitionHandle&& other) = default; | 
 | 36 |     ~PartitionHandle() { | 
 | 37 |         if (closer_) { | 
 | 38 |             // Make sure the device is closed first. | 
 | 39 |             fd_ = {}; | 
 | 40 |             closer_(); | 
 | 41 |         } | 
 | 42 |     } | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 43 |     const std::string& path() const { return path_; } | 
 | 44 |     int fd() const { return fd_.get(); } | 
 | 45 |     void set_fd(android::base::unique_fd&& fd) { fd_ = std::move(fd); } | 
 | 46 |  | 
 | 47 |   private: | 
 | 48 |     std::string path_; | 
 | 49 |     android::base::unique_fd fd_; | 
| David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 50 |     std::function<void()> closer_; | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 51 | }; | 
 | 52 |  | 
 | 53 | class FastbootDevice; | 
 | 54 |  | 
 | 55 | std::optional<std::string> FindPhysicalPartition(const std::string& name); | 
| David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 56 | bool LogicalPartitionExists(const std::string& name, const std::string& slot_suffix, | 
 | 57 |                             bool* is_zero_length = nullptr); | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 58 | bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle); | 
 | 59 |  | 
| Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 60 | bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number); |