blob: b3f2d5fbe37a8f3585123ceac4f7de2af6c33969 [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 Anderson88ef0b12018-08-09 10:40:00 -070029#include <fs_mgr_dm_linear.h>
David Andersond25f1c32018-11-09 20:41:33 -080030#include <liblp/builder.h>
David Anderson88ef0b12018-08-09 10:40:00 -070031#include <liblp/liblp.h>
David Anderson12211d12018-07-24 15:21:20 -070032
33#include "fastboot_device.h"
34
David Anderson88ef0b12018-08-09 10:40:00 -070035using namespace android::fs_mgr;
David Andersonc8ac4e72018-09-06 17:25:03 -070036using namespace std::chrono_literals;
David Anderson12211d12018-07-24 15:21:20 -070037using android::base::unique_fd;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070038using android::hardware::boot::V1_0::Slot;
39
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
David Anderson88ef0b12018-08-09 10:40:00 -070079bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle) {
80 // We prioritize logical partitions over physical ones, and do this
81 // consistently for other partition operations (like getvar:partition-size).
David Andersond25f1c32018-11-09 20:41:33 -080082 if (LogicalPartitionExists(device, name)) {
83 if (!OpenLogicalPartition(device, name, handle)) {
David Anderson88ef0b12018-08-09 10:40:00 -070084 return false;
85 }
86 } else if (!OpenPhysicalPartition(name, handle)) {
David Anderson12211d12018-07-24 15:21:20 -070087 LOG(ERROR) << "No such partition: " << name;
88 return false;
89 }
90
91 unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), O_WRONLY | O_EXCL)));
92 if (fd < 0) {
93 PLOG(ERROR) << "Failed to open block device: " << handle->path();
94 return false;
95 }
96 handle->set_fd(std::move(fd));
97 return true;
98}
99
100std::optional<std::string> FindPhysicalPartition(const std::string& name) {
Hridya Valsaraju99f37722018-10-08 11:06:38 -0700101 // Check for an invalid file name
102 if (android::base::StartsWith(name, "../") || name.find("/../") != std::string::npos) {
103 return {};
104 }
David Anderson12211d12018-07-24 15:21:20 -0700105 std::string path = "/dev/block/by-name/" + name;
Hridya Valsaraju3ffed212018-09-05 12:07:33 -0700106 if (access(path.c_str(), W_OK) < 0) {
David Anderson12211d12018-07-24 15:21:20 -0700107 return {};
108 }
109 return path;
110}
111
David Anderson88ef0b12018-08-09 10:40:00 -0700112static const LpMetadataPartition* FindLogicalPartition(const LpMetadata& metadata,
113 const std::string& name) {
114 for (const auto& partition : metadata.partitions) {
115 if (GetPartitionName(partition) == name) {
116 return &partition;
117 }
118 }
119 return nullptr;
120}
121
David Andersond25f1c32018-11-09 20:41:33 -0800122bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, bool* is_zero_length) {
123 std::string slot_suffix = GetSuperSlotSuffix(device, name);
124 uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
125 auto path = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number));
David Anderson88ef0b12018-08-09 10:40:00 -0700126 if (!path) {
127 return false;
128 }
129
David Anderson88ef0b12018-08-09 10:40:00 -0700130 std::unique_ptr<LpMetadata> metadata = ReadMetadata(path->c_str(), slot_number);
131 if (!metadata) {
132 return false;
133 }
134 const LpMetadataPartition* partition = FindLogicalPartition(*metadata.get(), name);
135 if (!partition) {
136 return false;
137 }
138 if (is_zero_length) {
139 *is_zero_length = (partition->num_extents == 0);
140 }
141 return true;
142}
143
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700144bool GetSlotNumber(const std::string& slot, Slot* number) {
145 if (slot.size() != 1) {
146 return false;
147 }
148 if (slot[0] < 'a' || slot[0] > 'z') {
149 return false;
150 }
151 *number = slot[0] - 'a';
152 return true;
153}
David Anderson0f626632018-08-31 16:44:25 -0700154
155std::vector<std::string> ListPartitions(FastbootDevice* device) {
156 std::vector<std::string> partitions;
157
158 // First get physical partitions.
159 struct dirent* de;
160 std::unique_ptr<DIR, decltype(&closedir)> by_name(opendir("/dev/block/by-name"), closedir);
161 while ((de = readdir(by_name.get())) != nullptr) {
162 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
163 continue;
164 }
165 struct stat s;
166 std::string path = "/dev/block/by-name/" + std::string(de->d_name);
167 if (!stat(path.c_str(), &s) && S_ISBLK(s.st_mode)) {
168 partitions.emplace_back(de->d_name);
169 }
170 }
171
David Andersond25f1c32018-11-09 20:41:33 -0800172 // Find metadata in each super partition (on retrofit devices, there will
173 // be two).
174 std::vector<std::unique_ptr<LpMetadata>> metadata_list;
175
176 uint32_t current_slot = SlotNumberForSlotSuffix(device->GetCurrentSlot());
177 std::string super_name = fs_mgr_get_super_partition_name(current_slot);
178 if (auto metadata = ReadMetadata(super_name, current_slot)) {
179 metadata_list.emplace_back(std::move(metadata));
180 }
181
182 uint32_t other_slot = (current_slot == 0) ? 1 : 0;
183 std::string other_super = fs_mgr_get_super_partition_name(other_slot);
184 if (super_name != other_super) {
185 if (auto metadata = ReadMetadata(other_super, other_slot)) {
186 metadata_list.emplace_back(std::move(metadata));
187 }
188 }
189
190 for (const auto& metadata : metadata_list) {
191 for (const auto& partition : metadata->partitions) {
192 std::string partition_name = GetPartitionName(partition);
193 if (std::find(partitions.begin(), partitions.end(), partition_name) ==
194 partitions.end()) {
David Anderson0f626632018-08-31 16:44:25 -0700195 partitions.emplace_back(partition_name);
196 }
197 }
198 }
199 return partitions;
200}
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700201
202bool GetDeviceLockStatus() {
203 std::string cmdline;
Hridya Valsarajubb12c5e2018-10-08 12:16:10 -0700204 // Return lock status true if unable to read kernel command line.
205 if (!android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
206 return true;
207 }
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700208 return cmdline.find("androidboot.verifiedbootstate=orange") == std::string::npos;
209}
David Andersond25f1c32018-11-09 20:41:33 -0800210
David Anderson4d307b02018-12-17 17:07:34 -0800211bool UpdateAllPartitionMetadata(FastbootDevice* device, const std::string& super_name,
David Andersond25f1c32018-11-09 20:41:33 -0800212 const android::fs_mgr::LpMetadata& metadata) {
David Anderson4d307b02018-12-17 17:07:34 -0800213 size_t num_slots = 1;
214 auto boot_control_hal = device->boot_control_hal();
215 if (boot_control_hal) {
216 num_slots = boot_control_hal->getNumberSlots();
217 }
218
David Andersond25f1c32018-11-09 20:41:33 -0800219 bool ok = true;
David Anderson4d307b02018-12-17 17:07:34 -0800220 for (size_t i = 0; i < num_slots; i++) {
David Andersond25f1c32018-11-09 20:41:33 -0800221 ok &= UpdatePartitionTable(super_name, metadata, i);
222 }
223 return ok;
224}
225
226std::string GetSuperSlotSuffix(FastbootDevice* device, const std::string& partition_name) {
227 // If the super partition does not have a slot suffix, this is not a
228 // retrofit device, and we should take the current slot.
229 std::string current_slot_suffix = device->GetCurrentSlot();
230 uint32_t current_slot_number = SlotNumberForSlotSuffix(current_slot_suffix);
231 std::string super_partition = fs_mgr_get_super_partition_name(current_slot_number);
232 if (GetPartitionSlotSuffix(super_partition).empty()) {
233 return current_slot_suffix;
234 }
235
236 // Otherwise, infer the slot from the partition name.
237 std::string slot_suffix = GetPartitionSlotSuffix(partition_name);
238 if (!slot_suffix.empty()) {
239 return slot_suffix;
240 }
241 return current_slot_suffix;
242}