blob: 6e1453f8363bcf17d2ca0ca38f1b6713e06187cf [file] [log] [blame]
Hridya Valsaraju31d2c262018-07-20 13:35:50 -07001/*
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 Anderson12211d12018-07-24 15:21:20 -070018#include <optional>
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070019#include <string>
20
Konstantin Vyshetsky1cee2ed2022-03-17 17:57:28 -070021#include <android-base/file.h>
22#include <android-base/logging.h>
David Anderson12211d12018-07-24 15:21:20 -070023#include <android-base/unique_fd.h>
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070024#include <android/hardware/boot/1.0/IBootControl.h>
David Anderson23243492019-12-17 00:58:31 -080025#include <fstab/fstab.h>
David Andersond25f1c32018-11-09 20:41:33 -080026#include <liblp/liblp.h>
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070027
David Anderson12211d12018-07-24 15:21:20 -070028// Logical partitions are only mapped to a block device as needed, and
29// immediately unmapped when no longer needed. In order to enforce this we
30// require accessing partitions through a Handle abstraction, which may perform
31// additional operations after closing its file descriptor.
32class PartitionHandle {
33 public:
34 PartitionHandle() {}
35 explicit PartitionHandle(const std::string& path) : path_(path) {}
David Anderson88ef0b12018-08-09 10:40:00 -070036 PartitionHandle(const std::string& path, std::function<void()>&& closer)
37 : path_(path), closer_(std::move(closer)) {}
38 PartitionHandle(PartitionHandle&& other) = default;
39 PartitionHandle& operator=(PartitionHandle&& other) = default;
40 ~PartitionHandle() {
41 if (closer_) {
42 // Make sure the device is closed first.
43 fd_ = {};
44 closer_();
45 }
46 }
David Anderson12211d12018-07-24 15:21:20 -070047 const std::string& path() const { return path_; }
48 int fd() const { return fd_.get(); }
Konstantin Vyshetsky1cee2ed2022-03-17 17:57:28 -070049 bool Open(int flags) {
50 flags |= (O_EXCL | O_CLOEXEC | O_BINARY);
David Anderson12211d12018-07-24 15:21:20 -070051
Konstantin Vyshetsky1cee2ed2022-03-17 17:57:28 -070052 // Attempts to open a second device can fail with EBUSY if the device is already open.
53 // Explicitly close any previously opened devices as unique_fd won't close them until
54 // after the attempt to open.
55 fd_.reset();
56
57 fd_ = android::base::unique_fd(TEMP_FAILURE_RETRY(open(path_.c_str(), flags)));
58 if (fd_ < 0) {
59 PLOG(ERROR) << "Failed to open block device: " << path_;
60 return false;
61 }
62 flags_ = flags;
63
64 return true;
65 }
66 bool Reset(int flags) {
67 if (fd_.ok() && (flags | O_EXCL | O_CLOEXEC | O_BINARY) == flags_) {
68 return true;
69 }
70
71 off_t offset = fd_.ok() ? lseek(fd_.get(), 0, SEEK_CUR) : 0;
72 if (offset < 0) {
73 PLOG(ERROR) << "Failed lseek on block device: " << path_;
74 return false;
75 }
76
77 sync();
78
79 if (Open(flags) == false) {
80 return false;
81 }
82
83 if (lseek(fd_.get(), offset, SEEK_SET) != offset) {
84 PLOG(ERROR) << "Failed lseek on block device: " << path_;
85 return false;
86 }
87
88 return true;
89 }
David Anderson12211d12018-07-24 15:21:20 -070090 private:
91 std::string path_;
92 android::base::unique_fd fd_;
Konstantin Vyshetsky1cee2ed2022-03-17 17:57:28 -070093 int flags_;
David Anderson88ef0b12018-08-09 10:40:00 -070094 std::function<void()> closer_;
David Anderson12211d12018-07-24 15:21:20 -070095};
96
David Anderson23243492019-12-17 00:58:31 -080097class AutoMountMetadata {
98 public:
99 AutoMountMetadata();
100 ~AutoMountMetadata();
101 explicit operator bool() const { return mounted_; }
102
103 private:
104 android::fs_mgr::Fstab fstab_;
105 bool mounted_ = false;
106 bool should_unmount_ = false;
107};
108
David Anderson12211d12018-07-24 15:21:20 -0700109class FastbootDevice;
110
David Andersond25f1c32018-11-09 20:41:33 -0800111// On normal devices, the super partition is always named "super". On retrofit
112// devices, the name must be derived from the partition name or current slot.
113// This helper assists in choosing the correct super for a given partition
114// name.
115std::string GetSuperSlotSuffix(FastbootDevice* device, const std::string& partition_name);
116
David Anderson12211d12018-07-24 15:21:20 -0700117std::optional<std::string> FindPhysicalPartition(const std::string& name);
David Andersond25f1c32018-11-09 20:41:33 -0800118bool LogicalPartitionExists(FastbootDevice* device, const std::string& name,
David Anderson88ef0b12018-08-09 10:40:00 -0700119 bool* is_zero_length = nullptr);
Yifan Hongc4073d32021-02-18 12:35:42 -0800120
Konstantin Vyshetsky81cc1192021-11-04 10:27:06 -0700121// Partition is O_WRONLY by default, caller should pass O_RDONLY for reading.
122// Caller may pass additional flags if needed. (O_EXCL | O_CLOEXEC | O_BINARY)
123// will be logically ORed internally.
Yifan Hongc4073d32021-02-18 12:35:42 -0800124bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle,
Konstantin Vyshetsky81cc1192021-11-04 10:27:06 -0700125 int flags = O_WRONLY);
Yifan Hongc4073d32021-02-18 12:35:42 -0800126
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700127bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number);
David Anderson0f626632018-08-31 16:44:25 -0700128std::vector<std::string> ListPartitions(FastbootDevice* device);
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700129bool GetDeviceLockStatus();
David Andersond25f1c32018-11-09 20:41:33 -0800130
131// Update all copies of metadata.
David Anderson4d307b02018-12-17 17:07:34 -0800132bool UpdateAllPartitionMetadata(FastbootDevice* device, const std::string& super_name,
David Andersond25f1c32018-11-09 20:41:33 -0800133 const android::fs_mgr::LpMetadata& metadata);