David Anderson | 4124123 | 2018-06-13 16:50:11 -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 <getopt.h> |
| 18 | #include <inttypes.h> |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 19 | #include <sys/mount.h> |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 21 | #include <sys/statvfs.h> |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sysexits.h> |
| 24 | #include <unistd.h> |
| 25 | |
Yifan Hong | 6ba2d0c | 2019-09-23 18:01:20 -0700 | [diff] [blame] | 26 | #include <algorithm> |
Yifan Hong | b48d79f | 2019-03-19 16:18:11 -0700 | [diff] [blame] | 27 | #include <iostream> |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 28 | #include <optional> |
| 29 | #include <regex> |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 30 | #include <string> |
David Anderson | 1c5907a | 2018-11-07 18:27:14 -0800 | [diff] [blame] | 31 | #include <vector> |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 32 | |
| 33 | #include <android-base/parseint.h> |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 34 | #include <android-base/properties.h> |
| 35 | #include <android-base/strings.h> |
David Anderson | 299abc7 | 2018-12-12 13:36:43 -0800 | [diff] [blame] | 36 | #ifdef __ANDROID__ |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 37 | #include <cutils/android_get_control_file.h> |
David Anderson | 299abc7 | 2018-12-12 13:36:43 -0800 | [diff] [blame] | 38 | #include <fs_mgr.h> |
| 39 | #endif |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 40 | #include <jsonpb/jsonpb.h> |
| 41 | #include <liblp/builder.h> |
David Anderson | 0640c67 | 2018-07-12 13:10:57 -0700 | [diff] [blame] | 42 | #include <liblp/liblp.h> |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 43 | |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 44 | #include "dynamic_partitions_device_info.pb.h" |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 45 | using namespace android; |
| 46 | using namespace android::fs_mgr; |
| 47 | |
Yifan Hong | b48d79f | 2019-03-19 16:18:11 -0700 | [diff] [blame] | 48 | static int usage(int /* argc */, char* argv[], std::ostream& cerr) { |
| 49 | cerr << argv[0] |
| 50 | << " - command-line tool for dumping Android Logical Partition images.\n" |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 51 | "\n" |
| 52 | "Usage:\n" |
Yifan Hong | b48d79f | 2019-03-19 16:18:11 -0700 | [diff] [blame] | 53 | " " |
| 54 | << argv[0] |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 55 | << " [-s <SLOT#>|--slot=<SLOT#>] [-j|--json] [FILE|DEVICE]\n" |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 56 | "\n" |
| 57 | "Options:\n" |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 58 | " -s, --slot=N Slot number or suffix.\n" |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 59 | " -j, --json Print in JSON format.\n" |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 60 | " -d, --dump-metadata-size\n" |
| 61 | " Print the space reserved for metadata to stdout\n" |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 62 | " in bytes.\n" |
| 63 | " -a, --all Dump all slots (not available in JSON mode).\n"; |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 64 | return EX_USAGE; |
| 65 | } |
| 66 | |
David Anderson | bdcadc7 | 2018-11-16 21:38:40 -0800 | [diff] [blame] | 67 | static std::string BuildFlagString(const std::vector<std::string>& strings) { |
| 68 | return strings.empty() ? "none" : android::base::Join(strings, ","); |
| 69 | } |
| 70 | |
David Anderson | ab1b095 | 2019-12-12 17:12:27 -0800 | [diff] [blame] | 71 | static std::string BuildHeaderFlagString(uint32_t flags) { |
| 72 | std::vector<std::string> strings; |
| 73 | |
David Anderson | 7b14a8d | 2019-12-13 15:34:54 -0800 | [diff] [blame] | 74 | if (flags & LP_HEADER_FLAG_VIRTUAL_AB_DEVICE) { |
| 75 | strings.emplace_back("virtual_ab_device"); |
| 76 | flags &= ~LP_HEADER_FLAG_VIRTUAL_AB_DEVICE; |
| 77 | } |
| 78 | |
David Anderson | ab1b095 | 2019-12-12 17:12:27 -0800 | [diff] [blame] | 79 | for (uint32_t i = 0; i < sizeof(flags) * 8; i++) { |
Greg Kaiser | 669932f | 2019-12-17 05:43:47 -0800 | [diff] [blame] | 80 | if (!(flags & (1U << i))) { |
David Anderson | ab1b095 | 2019-12-12 17:12:27 -0800 | [diff] [blame] | 81 | continue; |
| 82 | } |
| 83 | strings.emplace_back("unknown_flag_bit_" + std::to_string(i)); |
| 84 | } |
| 85 | return BuildFlagString(strings); |
| 86 | } |
| 87 | |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 88 | static std::string BuildAttributeString(uint32_t attrs) { |
David Anderson | 1c5907a | 2018-11-07 18:27:14 -0800 | [diff] [blame] | 89 | std::vector<std::string> strings; |
| 90 | if (attrs & LP_PARTITION_ATTR_READONLY) strings.emplace_back("readonly"); |
| 91 | if (attrs & LP_PARTITION_ATTR_SLOT_SUFFIXED) strings.emplace_back("slot-suffixed"); |
David Anderson | 817cab0 | 2019-08-27 13:44:48 -0700 | [diff] [blame] | 92 | if (attrs & LP_PARTITION_ATTR_UPDATED) strings.emplace_back("updated"); |
David Anderson | 8ac2c11 | 2019-12-18 15:11:45 -0800 | [diff] [blame] | 93 | if (attrs & LP_PARTITION_ATTR_DISABLED) strings.emplace_back("disabled"); |
David Anderson | bdcadc7 | 2018-11-16 21:38:40 -0800 | [diff] [blame] | 94 | return BuildFlagString(strings); |
| 95 | } |
| 96 | |
| 97 | static std::string BuildGroupFlagString(uint32_t flags) { |
| 98 | std::vector<std::string> strings; |
| 99 | if (flags & LP_GROUP_SLOT_SUFFIXED) strings.emplace_back("slot-suffixed"); |
| 100 | return BuildFlagString(strings); |
| 101 | } |
| 102 | |
| 103 | static std::string BuildBlockDeviceFlagString(uint32_t flags) { |
| 104 | std::vector<std::string> strings; |
| 105 | if (flags & LP_BLOCK_DEVICE_SLOT_SUFFIXED) strings.emplace_back("slot-suffixed"); |
| 106 | return BuildFlagString(strings); |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 109 | // Reimplementation of fs_mgr_get_slot_suffix() without reading |
| 110 | // kernel commandline. |
| 111 | static std::string GetSlotSuffix() { |
| 112 | return base::GetProperty("ro.boot.slot_suffix", ""); |
| 113 | } |
| 114 | |
| 115 | // Reimplementation of fs_mgr_get_super_partition_name() without reading |
| 116 | // kernel commandline. Always return the super partition at current slot. |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 117 | static std::string GetSuperPartitionName(const std::optional<uint32_t>& slot = {}) { |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 118 | std::string super_partition = base::GetProperty("ro.boot.super_partition", ""); |
| 119 | if (super_partition.empty()) { |
| 120 | return LP_METADATA_DEFAULT_PARTITION_NAME; |
| 121 | } |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 122 | if (slot.has_value()) { |
| 123 | return super_partition + SlotSuffixForSlotNumber(slot.value()); |
| 124 | } |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 125 | return super_partition + GetSlotSuffix(); |
| 126 | } |
| 127 | |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 128 | static std::string RemoveSuffix(const std::string& s, const std::string& suffix) { |
| 129 | if (base::EndsWith(s, suffix)) { |
| 130 | return s.substr(0, s.length() - suffix.length()); |
| 131 | } |
| 132 | return s; |
| 133 | } |
| 134 | |
| 135 | // Merge proto with information from metadata. |
| 136 | static bool MergeMetadata(const LpMetadata* metadata, |
| 137 | DynamicPartitionsDeviceInfoProto* proto) { |
| 138 | if (!metadata) return false; |
| 139 | auto builder = MetadataBuilder::New(*metadata); |
| 140 | if (!builder) return false; |
| 141 | |
| 142 | std::string slot_suffix = GetSlotSuffix(); |
| 143 | |
| 144 | for (const auto& group_name : builder->ListGroups()) { |
| 145 | auto group = builder->FindGroup(group_name); |
| 146 | if (!group) continue; |
| 147 | if (!base::EndsWith(group_name, slot_suffix)) continue; |
| 148 | auto group_proto = proto->add_groups(); |
| 149 | group_proto->set_name(RemoveSuffix(group_name, slot_suffix)); |
| 150 | group_proto->set_maximum_size(group->maximum_size()); |
| 151 | |
| 152 | for (auto partition : builder->ListPartitionsInGroup(group_name)) { |
| 153 | auto partition_name = partition->name(); |
| 154 | if (!base::EndsWith(partition_name, slot_suffix)) continue; |
| 155 | auto partition_proto = proto->add_partitions(); |
| 156 | partition_proto->set_name(RemoveSuffix(partition_name, slot_suffix)); |
| 157 | partition_proto->set_group_name(RemoveSuffix(group_name, slot_suffix)); |
| 158 | partition_proto->set_size(partition->size()); |
| 159 | partition_proto->set_is_dynamic(true); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | for (const auto& block_device : metadata->block_devices) { |
| 164 | std::string name = GetBlockDevicePartitionName(block_device); |
| 165 | BlockDeviceInfo info; |
| 166 | if (!builder->GetBlockDeviceInfo(name, &info)) { |
| 167 | continue; |
| 168 | } |
| 169 | auto block_device_proto = proto->add_block_devices(); |
| 170 | block_device_proto->set_name(RemoveSuffix(name, slot_suffix)); |
| 171 | block_device_proto->set_size(info.size); |
| 172 | block_device_proto->set_block_size(info.logical_block_size); |
| 173 | block_device_proto->set_alignment(info.alignment); |
| 174 | block_device_proto->set_alignment_offset(info.alignment_offset); |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | #ifdef __ANDROID__ |
| 180 | static DynamicPartitionsDeviceInfoProto::Partition* FindPartition( |
| 181 | DynamicPartitionsDeviceInfoProto* proto, const std::string& partition) { |
| 182 | for (DynamicPartitionsDeviceInfoProto::Partition& p : *proto->mutable_partitions()) { |
| 183 | if (p.name() == partition) { |
| 184 | return &p; |
| 185 | } |
| 186 | } |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
| 190 | static std::optional<std::string> GetReadonlyPartitionName(const android::fs_mgr::FstabEntry& entry) { |
| 191 | // Only report readonly partitions. |
| 192 | if ((entry.flags & MS_RDONLY) == 0) return std::nullopt; |
| 193 | std::regex regex("/([a-zA-Z_]*)$"); |
| 194 | std::smatch match; |
| 195 | if (!std::regex_match(entry.mount_point, match, regex)) return std::nullopt; |
| 196 | // On system-as-root devices, fstab lists / for system partition. |
| 197 | std::string partition = match[1]; |
| 198 | return partition.empty() ? "system" : partition; |
| 199 | } |
| 200 | |
| 201 | static bool MergeFsUsage(DynamicPartitionsDeviceInfoProto* proto, |
| 202 | std::ostream& cerr) { |
| 203 | using namespace std::string_literals; |
| 204 | Fstab fstab; |
| 205 | if (!ReadDefaultFstab(&fstab)) { |
| 206 | cerr << "Cannot read fstab\n"; |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | for (const auto& entry : fstab) { |
| 211 | auto partition = GetReadonlyPartitionName(entry); |
| 212 | if (!partition) { |
| 213 | continue; |
| 214 | } |
| 215 | |
| 216 | // system is mounted to "/"; |
| 217 | const char* mount_point = (entry.mount_point == "/system") |
| 218 | ? "/" : entry.mount_point.c_str(); |
| 219 | |
| 220 | struct statvfs vst; |
| 221 | if (statvfs(mount_point, &vst) == -1) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | auto partition_proto = FindPartition(proto, *partition); |
| 226 | if (partition_proto == nullptr) { |
| 227 | partition_proto = proto->add_partitions(); |
| 228 | partition_proto->set_name(*partition); |
| 229 | partition_proto->set_is_dynamic(false); |
| 230 | } |
| 231 | partition_proto->set_fs_size((uint64_t)vst.f_blocks * vst.f_frsize); |
| 232 | if (vst.f_bavail <= vst.f_blocks) { |
| 233 | partition_proto->set_fs_used((uint64_t)(vst.f_blocks - vst.f_bavail) * vst.f_frsize); |
| 234 | } |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | // Print output in JSON format. |
| 241 | // If successful, this function must write a valid JSON string to "cout" and return 0. |
| 242 | static int PrintJson(const LpMetadata* metadata, std::ostream& cout, |
| 243 | std::ostream& cerr) { |
| 244 | DynamicPartitionsDeviceInfoProto proto; |
| 245 | |
| 246 | if (base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { |
| 247 | proto.set_enabled(true); |
| 248 | } |
| 249 | |
| 250 | if (base::GetBoolProperty("ro.boot.dynamic_partitions_retrofit", false)) { |
| 251 | proto.set_retrofit(true); |
| 252 | } |
| 253 | |
| 254 | if (!MergeMetadata(metadata, &proto)) { |
| 255 | cerr << "Warning: Failed to read metadata.\n"; |
| 256 | } |
| 257 | #ifdef __ANDROID__ |
| 258 | if (!MergeFsUsage(&proto, cerr)) { |
| 259 | cerr << "Warning: Failed to read filesystem size and usage.\n"; |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | auto error_or_json = jsonpb::MessageToJsonString(proto); |
| 264 | if (!error_or_json.ok()) { |
| 265 | cerr << error_or_json.error() << "\n"; |
| 266 | return EX_SOFTWARE; |
| 267 | } |
| 268 | cout << *error_or_json; |
| 269 | return EX_OK; |
| 270 | } |
| 271 | |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 272 | static int DumpMetadataSize(const LpMetadata& metadata, std::ostream& cout) { |
| 273 | auto super_device = GetMetadataSuperBlockDevice(metadata); |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 274 | uint64_t metadata_size = super_device->first_logical_sector * LP_SECTOR_SIZE; |
| 275 | cout << metadata_size << std::endl; |
| 276 | return EX_OK; |
| 277 | } |
| 278 | |
David Anderson | 8a403a8 | 2018-11-13 11:39:19 -0800 | [diff] [blame] | 279 | class FileOrBlockDeviceOpener final : public PartitionOpener { |
| 280 | public: |
| 281 | android::base::unique_fd Open(const std::string& path, int flags) const override { |
| 282 | // Try a local file first. |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 283 | android::base::unique_fd fd; |
| 284 | |
| 285 | #ifdef __ANDROID__ |
| 286 | fd.reset(android_get_control_file(path.c_str())); |
| 287 | if (fd >= 0) return fd; |
| 288 | #endif |
| 289 | fd.reset(open(path.c_str(), flags)); |
| 290 | if (fd >= 0) return fd; |
| 291 | |
David Anderson | 8a403a8 | 2018-11-13 11:39:19 -0800 | [diff] [blame] | 292 | return PartitionOpener::Open(path, flags); |
| 293 | } |
| 294 | }; |
| 295 | |
Yifan Hong | 6ba2d0c | 2019-09-23 18:01:20 -0700 | [diff] [blame] | 296 | std::optional<std::tuple<std::string, uint64_t>> |
| 297 | ParseLinearExtentData(const LpMetadata& pt, const LpMetadataExtent& extent) { |
| 298 | if (extent.target_type != LP_TARGET_TYPE_LINEAR) { |
| 299 | return std::nullopt; |
| 300 | } |
| 301 | const auto& block_device = pt.block_devices[extent.target_source]; |
| 302 | std::string device_name = GetBlockDevicePartitionName(block_device); |
| 303 | return std::make_tuple(std::move(device_name), extent.target_data); |
| 304 | } |
| 305 | |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 306 | static void PrintMetadata(const LpMetadata& pt, std::ostream& cout) { |
| 307 | cout << "Metadata version: " << pt.header.major_version << "." << pt.header.minor_version |
| 308 | << "\n"; |
| 309 | cout << "Metadata size: " << (pt.header.header_size + pt.header.tables_size) << " bytes\n"; |
| 310 | cout << "Metadata max size: " << pt.geometry.metadata_max_size << " bytes\n"; |
| 311 | cout << "Metadata slot count: " << pt.geometry.metadata_slot_count << "\n"; |
David Anderson | ab1b095 | 2019-12-12 17:12:27 -0800 | [diff] [blame] | 312 | cout << "Header flags: " << BuildHeaderFlagString(pt.header.flags) << "\n"; |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 313 | cout << "Partition table:\n"; |
| 314 | cout << "------------------------\n"; |
| 315 | |
Yifan Hong | 6ba2d0c | 2019-09-23 18:01:20 -0700 | [diff] [blame] | 316 | std::vector<std::tuple<std::string, const LpMetadataExtent*>> extents; |
| 317 | |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 318 | for (const auto& partition : pt.partitions) { |
| 319 | std::string name = GetPartitionName(partition); |
| 320 | std::string group_name = GetPartitionGroupName(pt.groups[partition.group_index]); |
| 321 | cout << " Name: " << name << "\n"; |
| 322 | cout << " Group: " << group_name << "\n"; |
| 323 | cout << " Attributes: " << BuildAttributeString(partition.attributes) << "\n"; |
| 324 | cout << " Extents:\n"; |
| 325 | uint64_t first_sector = 0; |
| 326 | for (size_t i = 0; i < partition.num_extents; i++) { |
| 327 | const LpMetadataExtent& extent = pt.extents[partition.first_extent_index + i]; |
| 328 | cout << " " << first_sector << " .. " << (first_sector + extent.num_sectors - 1) |
| 329 | << " "; |
| 330 | first_sector += extent.num_sectors; |
| 331 | if (extent.target_type == LP_TARGET_TYPE_LINEAR) { |
| 332 | const auto& block_device = pt.block_devices[extent.target_source]; |
| 333 | std::string device_name = GetBlockDevicePartitionName(block_device); |
| 334 | cout << "linear " << device_name.c_str() << " " << extent.target_data; |
| 335 | } else if (extent.target_type == LP_TARGET_TYPE_ZERO) { |
| 336 | cout << "zero"; |
| 337 | } |
Yifan Hong | 6ba2d0c | 2019-09-23 18:01:20 -0700 | [diff] [blame] | 338 | extents.push_back(std::make_tuple(name, &extent)); |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 339 | cout << "\n"; |
| 340 | } |
| 341 | cout << "------------------------\n"; |
| 342 | } |
| 343 | |
Yifan Hong | 6ba2d0c | 2019-09-23 18:01:20 -0700 | [diff] [blame] | 344 | std::sort(extents.begin(), extents.end(), [&](const auto& x, const auto& y) { |
| 345 | auto x_data = ParseLinearExtentData(pt, *std::get<1>(x)); |
| 346 | auto y_data = ParseLinearExtentData(pt, *std::get<1>(y)); |
| 347 | return x_data < y_data; |
| 348 | }); |
| 349 | |
| 350 | cout << "Super partition layout:\n"; |
| 351 | cout << "------------------------\n"; |
| 352 | for (auto&& [name, extent] : extents) { |
| 353 | auto data = ParseLinearExtentData(pt, *extent); |
| 354 | if (!data) continue; |
| 355 | auto&& [block_device, offset] = *data; |
| 356 | cout << block_device << ": " << offset << " .. " << (offset + extent->num_sectors) |
| 357 | << ": " << name << " (" << extent->num_sectors << " sectors)\n"; |
| 358 | } |
| 359 | cout << "------------------------\n"; |
| 360 | |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 361 | cout << "Block device table:\n"; |
| 362 | cout << "------------------------\n"; |
| 363 | for (const auto& block_device : pt.block_devices) { |
| 364 | std::string partition_name = GetBlockDevicePartitionName(block_device); |
| 365 | cout << " Partition name: " << partition_name << "\n"; |
| 366 | cout << " First sector: " << block_device.first_logical_sector << "\n"; |
| 367 | cout << " Size: " << block_device.size << " bytes\n"; |
| 368 | cout << " Flags: " << BuildBlockDeviceFlagString(block_device.flags) << "\n"; |
| 369 | cout << "------------------------\n"; |
| 370 | } |
| 371 | |
| 372 | cout << "Group table:\n"; |
| 373 | cout << "------------------------\n"; |
| 374 | for (const auto& group : pt.groups) { |
| 375 | std::string group_name = GetPartitionGroupName(group); |
| 376 | cout << " Name: " << group_name << "\n"; |
| 377 | cout << " Maximum size: " << group.maximum_size << " bytes\n"; |
| 378 | cout << " Flags: " << BuildGroupFlagString(group.flags) << "\n"; |
| 379 | cout << "------------------------\n"; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | static std::unique_ptr<LpMetadata> ReadDeviceOrFile(const std::string& path, uint32_t slot) { |
| 384 | if (IsEmptySuperImage(path)) { |
| 385 | return ReadFromImageFile(path); |
| 386 | } |
| 387 | return ReadMetadata(path, slot); |
| 388 | } |
| 389 | |
Yifan Hong | b48d79f | 2019-03-19 16:18:11 -0700 | [diff] [blame] | 390 | int LpdumpMain(int argc, char* argv[], std::ostream& cout, std::ostream& cerr) { |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 391 | // clang-format off |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 392 | struct option options[] = { |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 393 | { "all", no_argument, nullptr, 'a' }, |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 394 | { "slot", required_argument, nullptr, 's' }, |
| 395 | { "help", no_argument, nullptr, 'h' }, |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 396 | { "json", no_argument, nullptr, 'j' }, |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 397 | { "dump-metadata-size", no_argument, nullptr, 'd' }, |
| 398 | { "is-super-empty", no_argument, nullptr, 'e' }, |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 399 | { nullptr, 0, nullptr, 0 }, |
| 400 | }; |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 401 | // clang-format on |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 402 | |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 403 | // Allow this function to be invoked by lpdumpd multiple times. |
| 404 | optind = 1; |
| 405 | |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 406 | int rv; |
| 407 | int index; |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 408 | bool json = false; |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 409 | bool dump_metadata_size = false; |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 410 | bool dump_all = false; |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 411 | std::optional<uint32_t> slot; |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 412 | while ((rv = getopt_long_only(argc, argv, "s:jhde", options, &index)) != -1) { |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 413 | switch (rv) { |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 414 | case 'a': |
| 415 | dump_all = true; |
| 416 | break; |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 417 | case 'h': |
David Anderson | 58871e4 | 2019-11-19 17:07:14 -0800 | [diff] [blame] | 418 | usage(argc, argv, cout); |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 419 | return EX_OK; |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 420 | case 's': { |
| 421 | uint32_t slot_arg; |
| 422 | if (android::base::ParseUint(optarg, &slot_arg)) { |
| 423 | slot = slot_arg; |
| 424 | } else { |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 425 | slot = SlotNumberForSlotSuffix(optarg); |
| 426 | } |
| 427 | break; |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 428 | } |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 429 | case 'e': |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 430 | // This is ignored, we now derive whether it's empty automatically. |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 431 | break; |
| 432 | case 'd': |
| 433 | dump_metadata_size = true; |
| 434 | break; |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 435 | case 'j': |
| 436 | json = true; |
| 437 | break; |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 438 | case '?': |
| 439 | case ':': |
| 440 | return usage(argc, argv, cerr); |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 444 | if (dump_all) { |
| 445 | if (slot.has_value()) { |
| 446 | cerr << "Cannot specify both --all and --slot.\n"; |
| 447 | return usage(argc, argv, cerr); |
| 448 | } |
| 449 | if (json) { |
| 450 | cerr << "Cannot specify both --all and --json.\n"; |
| 451 | return usage(argc, argv, cerr); |
| 452 | } |
| 453 | |
| 454 | // When dumping everything always start from the first slot. |
| 455 | slot = 0; |
| 456 | } |
| 457 | |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 458 | #ifdef __ANDROID__ |
| 459 | // Use the current slot as a default for A/B devices. |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 460 | auto current_slot_suffix = GetSlotSuffix(); |
| 461 | if (!slot.has_value() && !current_slot_suffix.empty()) { |
| 462 | slot = SlotNumberForSlotSuffix(current_slot_suffix); |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 463 | } |
| 464 | #endif |
| 465 | |
| 466 | // If we still haven't determined a slot yet, use the first one. |
| 467 | if (!slot.has_value()) { |
| 468 | slot = 0; |
| 469 | } |
| 470 | |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 471 | // Determine the path to the super partition (or image). If an explicit |
| 472 | // path is given, we use it for everything. Otherwise, we will infer it |
| 473 | // at the time we need to read metadata. |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 474 | std::string super_path; |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 475 | bool override_super_name = (optind < argc); |
| 476 | if (override_super_name) { |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 477 | super_path = argv[optind++]; |
David Anderson | 299abc7 | 2018-12-12 13:36:43 -0800 | [diff] [blame] | 478 | } else { |
| 479 | #ifdef __ANDROID__ |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 480 | super_path = GetSuperPartitionName(slot); |
David Anderson | 299abc7 | 2018-12-12 13:36:43 -0800 | [diff] [blame] | 481 | #else |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 482 | cerr << "Must specify a super partition image.\n"; |
Yifan Hong | b48d79f | 2019-03-19 16:18:11 -0700 | [diff] [blame] | 483 | return usage(argc, argv, cerr); |
David Anderson | 299abc7 | 2018-12-12 13:36:43 -0800 | [diff] [blame] | 484 | #endif |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 485 | } |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 486 | |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 487 | auto pt = ReadDeviceOrFile(super_path, slot.value()); |
| 488 | |
Yifan Hong | 7ea1a93 | 2019-03-15 16:06:50 -0700 | [diff] [blame] | 489 | // --json option doesn't require metadata to be present. |
| 490 | if (json) { |
| 491 | return PrintJson(pt.get(), cout, cerr); |
| 492 | } |
| 493 | |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 494 | if (!pt) { |
Yifan Hong | b48d79f | 2019-03-19 16:18:11 -0700 | [diff] [blame] | 495 | cerr << "Failed to read metadata.\n"; |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 496 | return EX_NOINPUT; |
| 497 | } |
| 498 | |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 499 | if (dump_metadata_size) { |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 500 | return DumpMetadataSize(*pt.get(), cout); |
David Anderson | 289570e | 2019-05-22 14:31:06 -0700 | [diff] [blame] | 501 | } |
| 502 | |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 503 | // When running on the device, we can check the slot count. Otherwise we |
| 504 | // use the # of metadata slots. (There is an extra slot we don't want to |
| 505 | // dump because it is currently unused.) |
| 506 | #ifdef __ANDROID__ |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 507 | uint32_t num_slots = current_slot_suffix.empty() ? 1 : 2; |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 508 | if (dump_all && num_slots > 1) { |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 509 | cout << "Current slot: " << current_slot_suffix << "\n"; |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 510 | } |
| 511 | #else |
| 512 | uint32_t num_slots = pt->geometry.metadata_slot_count; |
| 513 | #endif |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 514 | // Empty images only have one slot. |
| 515 | if (IsEmptySuperImage(super_path)) { |
| 516 | num_slots = 1; |
| 517 | } |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 518 | |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 519 | if (num_slots > 1) { |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 520 | cout << "Slot " << slot.value() << ":\n"; |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 521 | } |
David Anderson | 674ae9e | 2019-09-05 17:57:20 -0700 | [diff] [blame] | 522 | PrintMetadata(*pt.get(), cout); |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 523 | |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 524 | if (dump_all) { |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 525 | for (uint32_t i = 1; i < num_slots; i++) { |
David Anderson | 998892b | 2019-09-05 18:53:21 -0700 | [diff] [blame] | 526 | if (!override_super_name) { |
| 527 | super_path = GetSuperPartitionName(i); |
| 528 | } |
| 529 | |
David Anderson | cd5227d | 2019-09-05 18:31:12 -0700 | [diff] [blame] | 530 | pt = ReadDeviceOrFile(super_path, i); |
| 531 | if (!pt) { |
| 532 | continue; |
| 533 | } |
| 534 | |
| 535 | cout << "\nSlot " << i << ":\n"; |
| 536 | PrintMetadata(*pt.get(), cout); |
| 537 | } |
| 538 | } |
David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 539 | return EX_OK; |
| 540 | } |
Yifan Hong | b48d79f | 2019-03-19 16:18:11 -0700 | [diff] [blame] | 541 | |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 542 | int LpdumpMain(int argc, char* argv[]) { |
Yifan Hong | b48d79f | 2019-03-19 16:18:11 -0700 | [diff] [blame] | 543 | return LpdumpMain(argc, argv, std::cout, std::cerr); |
| 544 | } |