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