blob: 0652d93996141904d0052afc57bd41c6cc0a52a6 [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
17#include "utility.h"
18
David Anderson0f626632018-08-31 16:44:25 -070019#include <dirent.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
Hridya Valsarajudca328d2018-09-24 16:01:35 -070024#include <android-base/file.h>
David Anderson12211d12018-07-24 15:21:20 -070025#include <android-base/logging.h>
David Andersond25f1c32018-11-09 20:41:33 -080026#include <android-base/properties.h>
Hridya Valsaraju99f37722018-10-08 11:06:38 -070027#include <android-base/strings.h>
David Anderson5cbd2e42018-09-27 10:53:04 -070028#include <fs_mgr.h>
David Anderson23243492019-12-17 00:58:31 -080029#include <fs_mgr/roots.h>
David Anderson88ef0b12018-08-09 10:40:00 -070030#include <fs_mgr_dm_linear.h>
David Andersond25f1c32018-11-09 20:41:33 -080031#include <liblp/builder.h>
David Anderson88ef0b12018-08-09 10:40:00 -070032#include <liblp/liblp.h>
David Anderson12211d12018-07-24 15:21:20 -070033
34#include "fastboot_device.h"
35
David Anderson88ef0b12018-08-09 10:40:00 -070036using namespace android::fs_mgr;
David Andersonc8ac4e72018-09-06 17:25:03 -070037using namespace std::chrono_literals;
David Anderson12211d12018-07-24 15:21:20 -070038using android::base::unique_fd;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070039
David Andersond25f1c32018-11-09 20:41:33 -080040namespace {
41
42bool OpenPhysicalPartition(const std::string& name, PartitionHandle* handle) {
David Anderson12211d12018-07-24 15:21:20 -070043 std::optional<std::string> path = FindPhysicalPartition(name);
44 if (!path) {
45 return false;
46 }
47 *handle = PartitionHandle(*path);
48 return true;
49}
50
David Andersond25f1c32018-11-09 20:41:33 -080051bool OpenLogicalPartition(FastbootDevice* device, const std::string& partition_name,
52 PartitionHandle* handle) {
53 std::string slot_suffix = GetSuperSlotSuffix(device, partition_name);
54 uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
55 auto path = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number));
David Anderson88ef0b12018-08-09 10:40:00 -070056 if (!path) {
57 return false;
58 }
David Anderson15aa9542019-08-12 17:07:50 -070059
60 CreateLogicalPartitionParams params = {
61 .block_device = *path,
62 .metadata_slot = slot_number,
63 .partition_name = partition_name,
64 .force_writable = true,
65 .timeout_ms = 5s,
66 };
David Anderson88ef0b12018-08-09 10:40:00 -070067 std::string dm_path;
David Anderson15aa9542019-08-12 17:07:50 -070068 if (!CreateLogicalPartition(params, &dm_path)) {
David Andersond25f1c32018-11-09 20:41:33 -080069 LOG(ERROR) << "Could not map partition: " << partition_name;
David Anderson88ef0b12018-08-09 10:40:00 -070070 return false;
71 }
David Anderson470fe2b2019-07-10 18:09:50 -070072 auto closer = [partition_name]() -> void { DestroyLogicalPartition(partition_name); };
David Anderson88ef0b12018-08-09 10:40:00 -070073 *handle = PartitionHandle(dm_path, std::move(closer));
74 return true;
75}
76
David Andersond25f1c32018-11-09 20:41:33 -080077} // namespace
78
Yifan Hongc4073d32021-02-18 12:35:42 -080079bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle,
Konstantin Vyshetsky81cc1192021-11-04 10:27:06 -070080 int flags) {
David Anderson88ef0b12018-08-09 10:40:00 -070081 // We prioritize logical partitions over physical ones, and do this
82 // consistently for other partition operations (like getvar:partition-size).
David Andersond25f1c32018-11-09 20:41:33 -080083 if (LogicalPartitionExists(device, name)) {
84 if (!OpenLogicalPartition(device, name, handle)) {
David Anderson88ef0b12018-08-09 10:40:00 -070085 return false;
86 }
87 } else if (!OpenPhysicalPartition(name, handle)) {
David Anderson12211d12018-07-24 15:21:20 -070088 LOG(ERROR) << "No such partition: " << name;
89 return false;
90 }
91
Konstantin Vyshetsky1cee2ed2022-03-17 17:57:28 -070092 return handle->Open(flags);
David Anderson12211d12018-07-24 15:21:20 -070093}
94
95std::optional<std::string> FindPhysicalPartition(const std::string& name) {
Hridya Valsaraju99f37722018-10-08 11:06:38 -070096 // Check for an invalid file name
97 if (android::base::StartsWith(name, "../") || name.find("/../") != std::string::npos) {
98 return {};
99 }
David Anderson12211d12018-07-24 15:21:20 -0700100 std::string path = "/dev/block/by-name/" + name;
Hridya Valsaraju3ffed212018-09-05 12:07:33 -0700101 if (access(path.c_str(), W_OK) < 0) {
David Anderson12211d12018-07-24 15:21:20 -0700102 return {};
103 }
104 return path;
105}
106
David Anderson88ef0b12018-08-09 10:40:00 -0700107static const LpMetadataPartition* FindLogicalPartition(const LpMetadata& metadata,
108 const std::string& name) {
109 for (const auto& partition : metadata.partitions) {
110 if (GetPartitionName(partition) == name) {
111 return &partition;
112 }
113 }
114 return nullptr;
115}
116
David Andersond25f1c32018-11-09 20:41:33 -0800117bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, bool* is_zero_length) {
118 std::string slot_suffix = GetSuperSlotSuffix(device, name);
119 uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
120 auto path = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number));
David Anderson88ef0b12018-08-09 10:40:00 -0700121 if (!path) {
122 return false;
123 }
124
David Anderson88ef0b12018-08-09 10:40:00 -0700125 std::unique_ptr<LpMetadata> metadata = ReadMetadata(path->c_str(), slot_number);
126 if (!metadata) {
127 return false;
128 }
129 const LpMetadataPartition* partition = FindLogicalPartition(*metadata.get(), name);
130 if (!partition) {
131 return false;
132 }
133 if (is_zero_length) {
134 *is_zero_length = (partition->num_extents == 0);
135 }
136 return true;
137}
138
Kelvin Zhang6befe782022-06-21 14:20:54 -0700139bool GetSlotNumber(const std::string& slot, int32_t* number) {
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700140 if (slot.size() != 1) {
141 return false;
142 }
143 if (slot[0] < 'a' || slot[0] > 'z') {
144 return false;
145 }
146 *number = slot[0] - 'a';
147 return true;
148}
David Anderson0f626632018-08-31 16:44:25 -0700149
150std::vector<std::string> ListPartitions(FastbootDevice* device) {
151 std::vector<std::string> partitions;
152
153 // First get physical partitions.
154 struct dirent* de;
155 std::unique_ptr<DIR, decltype(&closedir)> by_name(opendir("/dev/block/by-name"), closedir);
156 while ((de = readdir(by_name.get())) != nullptr) {
157 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
158 continue;
159 }
160 struct stat s;
161 std::string path = "/dev/block/by-name/" + std::string(de->d_name);
162 if (!stat(path.c_str(), &s) && S_ISBLK(s.st_mode)) {
163 partitions.emplace_back(de->d_name);
164 }
165 }
166
David Andersond25f1c32018-11-09 20:41:33 -0800167 // Find metadata in each super partition (on retrofit devices, there will
168 // be two).
169 std::vector<std::unique_ptr<LpMetadata>> metadata_list;
170
171 uint32_t current_slot = SlotNumberForSlotSuffix(device->GetCurrentSlot());
172 std::string super_name = fs_mgr_get_super_partition_name(current_slot);
173 if (auto metadata = ReadMetadata(super_name, current_slot)) {
174 metadata_list.emplace_back(std::move(metadata));
175 }
176
177 uint32_t other_slot = (current_slot == 0) ? 1 : 0;
178 std::string other_super = fs_mgr_get_super_partition_name(other_slot);
179 if (super_name != other_super) {
180 if (auto metadata = ReadMetadata(other_super, other_slot)) {
181 metadata_list.emplace_back(std::move(metadata));
182 }
183 }
184
185 for (const auto& metadata : metadata_list) {
186 for (const auto& partition : metadata->partitions) {
187 std::string partition_name = GetPartitionName(partition);
188 if (std::find(partitions.begin(), partitions.end(), partition_name) ==
189 partitions.end()) {
David Anderson0f626632018-08-31 16:44:25 -0700190 partitions.emplace_back(partition_name);
191 }
192 }
193 }
194 return partitions;
195}
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700196
197bool GetDeviceLockStatus() {
maxwendcc2daa2019-12-16 19:42:28 +0100198 return false;
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700199}
David Andersond25f1c32018-11-09 20:41:33 -0800200
David Anderson4d307b02018-12-17 17:07:34 -0800201bool UpdateAllPartitionMetadata(FastbootDevice* device, const std::string& super_name,
David Andersond25f1c32018-11-09 20:41:33 -0800202 const android::fs_mgr::LpMetadata& metadata) {
David Anderson4d307b02018-12-17 17:07:34 -0800203 size_t num_slots = 1;
204 auto boot_control_hal = device->boot_control_hal();
205 if (boot_control_hal) {
Kelvin Zhang6befe782022-06-21 14:20:54 -0700206 num_slots = boot_control_hal->GetNumSlots();
David Anderson4d307b02018-12-17 17:07:34 -0800207 }
208
David Andersond25f1c32018-11-09 20:41:33 -0800209 bool ok = true;
David Anderson4d307b02018-12-17 17:07:34 -0800210 for (size_t i = 0; i < num_slots; i++) {
David Andersond25f1c32018-11-09 20:41:33 -0800211 ok &= UpdatePartitionTable(super_name, metadata, i);
212 }
213 return ok;
214}
215
216std::string GetSuperSlotSuffix(FastbootDevice* device, const std::string& partition_name) {
217 // If the super partition does not have a slot suffix, this is not a
218 // retrofit device, and we should take the current slot.
219 std::string current_slot_suffix = device->GetCurrentSlot();
220 uint32_t current_slot_number = SlotNumberForSlotSuffix(current_slot_suffix);
221 std::string super_partition = fs_mgr_get_super_partition_name(current_slot_number);
222 if (GetPartitionSlotSuffix(super_partition).empty()) {
223 return current_slot_suffix;
224 }
225
226 // Otherwise, infer the slot from the partition name.
227 std::string slot_suffix = GetPartitionSlotSuffix(partition_name);
228 if (!slot_suffix.empty()) {
229 return slot_suffix;
230 }
231 return current_slot_suffix;
232}
David Anderson23243492019-12-17 00:58:31 -0800233
234AutoMountMetadata::AutoMountMetadata() {
235 android::fs_mgr::Fstab proc_mounts;
236 if (!ReadFstabFromFile("/proc/mounts", &proc_mounts)) {
237 LOG(ERROR) << "Could not read /proc/mounts";
238 return;
239 }
240
241 if (GetEntryForMountPoint(&proc_mounts, "/metadata")) {
242 mounted_ = true;
243 return;
244 }
245
246 if (!ReadDefaultFstab(&fstab_)) {
247 LOG(ERROR) << "Could not read default fstab";
248 return;
249 }
250 mounted_ = EnsurePathMounted(&fstab_, "/metadata");
251 should_unmount_ = true;
252}
253
254AutoMountMetadata::~AutoMountMetadata() {
255 if (mounted_ && should_unmount_) {
256 EnsurePathUnmounted(&fstab_, "/metadata");
257 }
258}