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 | |
Hridya Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame^] | 24 | #include <android-base/file.h> |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 26 | #include <fs_mgr_dm_linear.h> |
| 27 | #include <liblp/liblp.h> |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 28 | |
| 29 | #include "fastboot_device.h" |
| 30 | |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 31 | using namespace android::fs_mgr; |
David Anderson | c8ac4e7 | 2018-09-06 17:25:03 -0700 | [diff] [blame] | 32 | using namespace std::chrono_literals; |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 33 | using android::base::unique_fd; |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 34 | using android::hardware::boot::V1_0::Slot; |
| 35 | |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 36 | static 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 Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 45 | static 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 Anderson | c8ac4e7 | 2018-09-06 17:25:03 -0700 | [diff] [blame] | 53 | if (!CreateLogicalPartition(path->c_str(), slot_number, name, true, 5s, &dm_path)) { |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 54 | LOG(ERROR) << "Could not map partition: " << name; |
| 55 | return false; |
| 56 | } |
David Anderson | c8ac4e7 | 2018-09-06 17:25:03 -0700 | [diff] [blame] | 57 | auto closer = [name]() -> void { DestroyLogicalPartition(name, 5s); }; |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 58 | *handle = PartitionHandle(dm_path, std::move(closer)); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool 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 Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 70 | 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 | |
| 83 | std::optional<std::string> FindPhysicalPartition(const std::string& name) { |
| 84 | std::string path = "/dev/block/by-name/" + name; |
Hridya Valsaraju | 3ffed21 | 2018-09-05 12:07:33 -0700 | [diff] [blame] | 85 | if (access(path.c_str(), W_OK) < 0) { |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 86 | return {}; |
| 87 | } |
| 88 | return path; |
| 89 | } |
| 90 | |
David Anderson | 88ef0b1 | 2018-08-09 10:40:00 -0700 | [diff] [blame] | 91 | static 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 | |
| 101 | bool 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 Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 123 | bool 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 Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 133 | |
| 134 | std::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 Valsaraju | dca328d | 2018-09-24 16:01:35 -0700 | [diff] [blame^] | 163 | |
| 164 | bool GetDeviceLockStatus() { |
| 165 | std::string cmdline; |
| 166 | android::base::ReadFileToString("/proc/cmdline", &cmdline); |
| 167 | return cmdline.find("androidboot.verifiedbootstate=orange") == std::string::npos; |
| 168 | } |