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