blob: 528abec49dea95aa821a6893d459223b4ae1396c [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 Anderson5cbd2e42018-09-27 10:53:04 -070026#include <fs_mgr.h>
David Anderson88ef0b12018-08-09 10:40:00 -070027#include <fs_mgr_dm_linear.h>
28#include <liblp/liblp.h>
David Anderson12211d12018-07-24 15:21:20 -070029
30#include "fastboot_device.h"
31
David Anderson88ef0b12018-08-09 10:40:00 -070032using namespace android::fs_mgr;
David Andersonc8ac4e72018-09-06 17:25:03 -070033using namespace std::chrono_literals;
David Anderson12211d12018-07-24 15:21:20 -070034using android::base::unique_fd;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070035using android::hardware::boot::V1_0::Slot;
36
David Anderson12211d12018-07-24 15:21:20 -070037static bool OpenPhysicalPartition(const std::string& name, PartitionHandle* handle) {
38 std::optional<std::string> path = FindPhysicalPartition(name);
39 if (!path) {
40 return false;
41 }
42 *handle = PartitionHandle(*path);
43 return true;
44}
45
David Anderson88ef0b12018-08-09 10:40:00 -070046static bool OpenLogicalPartition(const std::string& name, const std::string& slot,
47 PartitionHandle* handle) {
David Anderson5cbd2e42018-09-27 10:53:04 -070048 std::optional<std::string> path = FindPhysicalPartition(fs_mgr_get_super_partition_name());
David Anderson88ef0b12018-08-09 10:40:00 -070049 if (!path) {
50 return false;
51 }
52 uint32_t slot_number = SlotNumberForSlotSuffix(slot);
53 std::string dm_path;
David Andersonc8ac4e72018-09-06 17:25:03 -070054 if (!CreateLogicalPartition(path->c_str(), slot_number, name, true, 5s, &dm_path)) {
David Anderson88ef0b12018-08-09 10:40:00 -070055 LOG(ERROR) << "Could not map partition: " << name;
56 return false;
57 }
David Andersonc8ac4e72018-09-06 17:25:03 -070058 auto closer = [name]() -> void { DestroyLogicalPartition(name, 5s); };
David Anderson88ef0b12018-08-09 10:40:00 -070059 *handle = PartitionHandle(dm_path, std::move(closer));
60 return true;
61}
62
63bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle) {
64 // We prioritize logical partitions over physical ones, and do this
65 // consistently for other partition operations (like getvar:partition-size).
66 if (LogicalPartitionExists(name, device->GetCurrentSlot())) {
67 if (!OpenLogicalPartition(name, device->GetCurrentSlot(), handle)) {
68 return false;
69 }
70 } else if (!OpenPhysicalPartition(name, handle)) {
David Anderson12211d12018-07-24 15:21:20 -070071 LOG(ERROR) << "No such partition: " << name;
72 return false;
73 }
74
75 unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), O_WRONLY | O_EXCL)));
76 if (fd < 0) {
77 PLOG(ERROR) << "Failed to open block device: " << handle->path();
78 return false;
79 }
80 handle->set_fd(std::move(fd));
81 return true;
82}
83
84std::optional<std::string> FindPhysicalPartition(const std::string& name) {
85 std::string path = "/dev/block/by-name/" + name;
Hridya Valsaraju3ffed212018-09-05 12:07:33 -070086 if (access(path.c_str(), W_OK) < 0) {
David Anderson12211d12018-07-24 15:21:20 -070087 return {};
88 }
89 return path;
90}
91
David Anderson88ef0b12018-08-09 10:40:00 -070092static const LpMetadataPartition* FindLogicalPartition(const LpMetadata& metadata,
93 const std::string& name) {
94 for (const auto& partition : metadata.partitions) {
95 if (GetPartitionName(partition) == name) {
96 return &partition;
97 }
98 }
99 return nullptr;
100}
101
102bool LogicalPartitionExists(const std::string& name, const std::string& slot_suffix,
103 bool* is_zero_length) {
David Anderson5cbd2e42018-09-27 10:53:04 -0700104 auto path = FindPhysicalPartition(fs_mgr_get_super_partition_name());
David Anderson88ef0b12018-08-09 10:40:00 -0700105 if (!path) {
106 return false;
107 }
108
109 uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
110 std::unique_ptr<LpMetadata> metadata = ReadMetadata(path->c_str(), slot_number);
111 if (!metadata) {
112 return false;
113 }
114 const LpMetadataPartition* partition = FindLogicalPartition(*metadata.get(), name);
115 if (!partition) {
116 return false;
117 }
118 if (is_zero_length) {
119 *is_zero_length = (partition->num_extents == 0);
120 }
121 return true;
122}
123
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700124bool GetSlotNumber(const std::string& slot, Slot* number) {
125 if (slot.size() != 1) {
126 return false;
127 }
128 if (slot[0] < 'a' || slot[0] > 'z') {
129 return false;
130 }
131 *number = slot[0] - 'a';
132 return true;
133}
David Anderson0f626632018-08-31 16:44:25 -0700134
135std::vector<std::string> ListPartitions(FastbootDevice* device) {
136 std::vector<std::string> partitions;
137
138 // First get physical partitions.
139 struct dirent* de;
140 std::unique_ptr<DIR, decltype(&closedir)> by_name(opendir("/dev/block/by-name"), closedir);
141 while ((de = readdir(by_name.get())) != nullptr) {
142 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
143 continue;
144 }
145 struct stat s;
146 std::string path = "/dev/block/by-name/" + std::string(de->d_name);
147 if (!stat(path.c_str(), &s) && S_ISBLK(s.st_mode)) {
148 partitions.emplace_back(de->d_name);
149 }
150 }
151
152 // Next get logical partitions.
David Anderson5cbd2e42018-09-27 10:53:04 -0700153 if (auto path = FindPhysicalPartition(fs_mgr_get_super_partition_name())) {
David Anderson0f626632018-08-31 16:44:25 -0700154 uint32_t slot_number = SlotNumberForSlotSuffix(device->GetCurrentSlot());
155 if (auto metadata = ReadMetadata(path->c_str(), slot_number)) {
156 for (const auto& partition : metadata->partitions) {
157 std::string partition_name = GetPartitionName(partition);
158 partitions.emplace_back(partition_name);
159 }
160 }
161 }
162 return partitions;
163}
Hridya Valsarajudca328d2018-09-24 16:01:35 -0700164
165bool GetDeviceLockStatus() {
166 std::string cmdline;
167 android::base::ReadFileToString("/proc/cmdline", &cmdline);
168 return cmdline.find("androidboot.verifiedbootstate=orange") == std::string::npos;
169}