Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 25 | #include <fs_mgr_dm_linear.h> |
| 26 | #include <liblp/liblp.h> |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 27 | |
| 28 | #include "fastboot_device.h" |
| 29 | |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 30 | using namespace android::fs_mgr; |
David Anderson | c8ac4e7 | 2018-09-06 17:25:03 -0700 | [diff] [blame^] | 31 | using namespace std::chrono_literals; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 32 | using android::base::unique_fd; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 33 | using android::hardware::boot::V1_0::Slot; |
| 34 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 35 | static 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 Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 44 | static 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 Anderson | c8ac4e7 | 2018-09-06 17:25:03 -0700 | [diff] [blame^] | 52 | if (!CreateLogicalPartition(path->c_str(), slot_number, name, true, 5s, &dm_path)) { |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 53 | LOG(ERROR) << "Could not map partition: " << name; |
| 54 | return false; |
| 55 | } |
David Anderson | c8ac4e7 | 2018-09-06 17:25:03 -0700 | [diff] [blame^] | 56 | auto closer = [name]() -> void { DestroyLogicalPartition(name, 5s); }; |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 57 | *handle = PartitionHandle(dm_path, std::move(closer)); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool 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 Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 69 | 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 | |
| 82 | std::optional<std::string> FindPhysicalPartition(const std::string& name) { |
| 83 | std::string path = "/dev/block/by-name/" + name; |
Hridya Valsaraju | 3ffed21 | 2018-09-05 12:07:33 -0700 | [diff] [blame] | 84 | if (access(path.c_str(), W_OK) < 0) { |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 85 | return {}; |
| 86 | } |
| 87 | return path; |
| 88 | } |
| 89 | |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 90 | static 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 | |
| 100 | bool 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 Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 122 | bool 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 Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 132 | |
| 133 | std::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 | } |