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> |
David Anderson | 2324349 | 2019-12-17 00:58:31 -0800 | [diff] [blame] | 23 | #include <fstab/fstab.h> |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 24 | #include <liblp/liblp.h> |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 25 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 26 | // Logical partitions are only mapped to a block device as needed, and |
| 27 | // immediately unmapped when no longer needed. In order to enforce this we |
| 28 | // require accessing partitions through a Handle abstraction, which may perform |
| 29 | // additional operations after closing its file descriptor. |
| 30 | class PartitionHandle { |
| 31 | public: |
| 32 | PartitionHandle() {} |
| 33 | explicit PartitionHandle(const std::string& path) : path_(path) {} |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 34 | PartitionHandle(const std::string& path, std::function<void()>&& closer) |
| 35 | : path_(path), closer_(std::move(closer)) {} |
| 36 | PartitionHandle(PartitionHandle&& other) = default; |
| 37 | PartitionHandle& operator=(PartitionHandle&& other) = default; |
| 38 | ~PartitionHandle() { |
| 39 | if (closer_) { |
| 40 | // Make sure the device is closed first. |
| 41 | fd_ = {}; |
| 42 | closer_(); |
| 43 | } |
| 44 | } |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 45 | const std::string& path() const { return path_; } |
| 46 | int fd() const { return fd_.get(); } |
| 47 | void set_fd(android::base::unique_fd&& fd) { fd_ = std::move(fd); } |
| 48 | |
| 49 | private: |
| 50 | std::string path_; |
| 51 | android::base::unique_fd fd_; |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 52 | std::function<void()> closer_; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
David Anderson | 2324349 | 2019-12-17 00:58:31 -0800 | [diff] [blame] | 55 | class AutoMountMetadata { |
| 56 | public: |
| 57 | AutoMountMetadata(); |
| 58 | ~AutoMountMetadata(); |
| 59 | explicit operator bool() const { return mounted_; } |
| 60 | |
| 61 | private: |
| 62 | android::fs_mgr::Fstab fstab_; |
| 63 | bool mounted_ = false; |
| 64 | bool should_unmount_ = false; |
| 65 | }; |
| 66 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 67 | class FastbootDevice; |
| 68 | |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 69 | // On normal devices, the super partition is always named "super". On retrofit |
| 70 | // devices, the name must be derived from the partition name or current slot. |
| 71 | // This helper assists in choosing the correct super for a given partition |
| 72 | // name. |
| 73 | std::string GetSuperSlotSuffix(FastbootDevice* device, const std::string& partition_name); |
| 74 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 75 | std::optional<std::string> FindPhysicalPartition(const std::string& name); |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 76 | bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 77 | bool* is_zero_length = nullptr); |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 78 | bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle); |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 79 | bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number); |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 80 | std::vector<std::string> ListPartitions(FastbootDevice* device); |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame] | 81 | bool GetDeviceLockStatus(); |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 82 | |
| 83 | // Update all copies of metadata. |
David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 84 | bool UpdateAllPartitionMetadata(FastbootDevice* device, const std::string& super_name, |
David Anderson | d25f1c3 | 2018-11-09 20:41:33 -0800 | [diff] [blame] | 85 | const android::fs_mgr::LpMetadata& metadata); |