blob: 047b5ee02fdda77d5585ec77f81326c4a29b95fb [file] [log] [blame]
David Anderson41241232018-06-13 16:50:11 -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 <getopt.h>
18#include <inttypes.h>
Yifan Hong7ea1a932019-03-15 16:06:50 -070019#include <sys/mount.h>
David Anderson41241232018-06-13 16:50:11 -070020#include <sys/stat.h>
Yifan Hong7ea1a932019-03-15 16:06:50 -070021#include <sys/statvfs.h>
David Anderson41241232018-06-13 16:50:11 -070022#include <sys/types.h>
23#include <sysexits.h>
24#include <unistd.h>
25
Yifan Hong6ba2d0c2019-09-23 18:01:20 -070026#include <algorithm>
Yifan Hongb48d79f2019-03-19 16:18:11 -070027#include <iostream>
Yifan Hong7ea1a932019-03-15 16:06:50 -070028#include <optional>
29#include <regex>
David Anderson41241232018-06-13 16:50:11 -070030#include <string>
David Anderson1c5907a2018-11-07 18:27:14 -080031#include <vector>
David Anderson41241232018-06-13 16:50:11 -070032
33#include <android-base/parseint.h>
Yifan Hong7ea1a932019-03-15 16:06:50 -070034#include <android-base/properties.h>
35#include <android-base/strings.h>
David Anderson299abc72018-12-12 13:36:43 -080036#ifdef __ANDROID__
Yifan Hongb84fb572019-03-15 14:19:08 -070037#include <cutils/android_get_control_file.h>
David Anderson299abc72018-12-12 13:36:43 -080038#include <fs_mgr.h>
39#endif
Yifan Hong7ea1a932019-03-15 16:06:50 -070040#include <jsonpb/jsonpb.h>
41#include <liblp/builder.h>
David Anderson0640c672018-07-12 13:10:57 -070042#include <liblp/liblp.h>
David Anderson41241232018-06-13 16:50:11 -070043
Yifan Hong7ea1a932019-03-15 16:06:50 -070044#include "dynamic_partitions_device_info.pb.h"
David Anderson41241232018-06-13 16:50:11 -070045using namespace android;
46using namespace android::fs_mgr;
47
Yifan Hongb48d79f2019-03-19 16:18:11 -070048static 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 Anderson41241232018-06-13 16:50:11 -070051 "\n"
52 "Usage:\n"
Yifan Hongb48d79f2019-03-19 16:18:11 -070053 " "
54 << argv[0]
Yifan Hong7ea1a932019-03-15 16:06:50 -070055 << " [-s <SLOT#>|--slot=<SLOT#>] [-j|--json] [FILE|DEVICE]\n"
David Anderson41241232018-06-13 16:50:11 -070056 "\n"
57 "Options:\n"
Yifan Hong7ea1a932019-03-15 16:06:50 -070058 " -s, --slot=N Slot number or suffix.\n"
David Anderson289570e2019-05-22 14:31:06 -070059 " -j, --json Print in JSON format.\n"
David Anderson289570e2019-05-22 14:31:06 -070060 " -d, --dump-metadata-size\n"
61 " Print the space reserved for metadata to stdout\n"
David Andersoncd5227d2019-09-05 18:31:12 -070062 " in bytes.\n"
63 " -a, --all Dump all slots (not available in JSON mode).\n";
David Anderson41241232018-06-13 16:50:11 -070064 return EX_USAGE;
65}
66
David Andersonbdcadc72018-11-16 21:38:40 -080067static std::string BuildFlagString(const std::vector<std::string>& strings) {
68 return strings.empty() ? "none" : android::base::Join(strings, ",");
69}
70
David Andersonab1b0952019-12-12 17:12:27 -080071static std::string BuildHeaderFlagString(uint32_t flags) {
72 std::vector<std::string> strings;
73
David Anderson7b14a8d2019-12-13 15:34:54 -080074 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 Andersonab1b0952019-12-12 17:12:27 -080079 for (uint32_t i = 0; i < sizeof(flags) * 8; i++) {
Greg Kaiser669932f2019-12-17 05:43:47 -080080 if (!(flags & (1U << i))) {
David Andersonab1b0952019-12-12 17:12:27 -080081 continue;
82 }
83 strings.emplace_back("unknown_flag_bit_" + std::to_string(i));
84 }
85 return BuildFlagString(strings);
86}
87
David Anderson41241232018-06-13 16:50:11 -070088static std::string BuildAttributeString(uint32_t attrs) {
David Anderson1c5907a2018-11-07 18:27:14 -080089 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 Anderson817cab02019-08-27 13:44:48 -070092 if (attrs & LP_PARTITION_ATTR_UPDATED) strings.emplace_back("updated");
David Anderson8ac2c112019-12-18 15:11:45 -080093 if (attrs & LP_PARTITION_ATTR_DISABLED) strings.emplace_back("disabled");
David Andersonbdcadc72018-11-16 21:38:40 -080094 return BuildFlagString(strings);
95}
96
97static 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
103static 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 Anderson41241232018-06-13 16:50:11 -0700107}
108
Yifan Hongb84fb572019-03-15 14:19:08 -0700109// Reimplementation of fs_mgr_get_slot_suffix() without reading
110// kernel commandline.
111static 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 Anderson998892b2019-09-05 18:53:21 -0700117static std::string GetSuperPartitionName(const std::optional<uint32_t>& slot = {}) {
Yifan Hongb84fb572019-03-15 14:19:08 -0700118 std::string super_partition = base::GetProperty("ro.boot.super_partition", "");
119 if (super_partition.empty()) {
120 return LP_METADATA_DEFAULT_PARTITION_NAME;
121 }
David Anderson998892b2019-09-05 18:53:21 -0700122 if (slot.has_value()) {
123 return super_partition + SlotSuffixForSlotNumber(slot.value());
124 }
Yifan Hongb84fb572019-03-15 14:19:08 -0700125 return super_partition + GetSlotSuffix();
126}
127
Yifan Hong7ea1a932019-03-15 16:06:50 -0700128static 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.
136static 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__
180static 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
190static 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
201static 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.
242static 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 Anderson674ae9e2019-09-05 17:57:20 -0700272static int DumpMetadataSize(const LpMetadata& metadata, std::ostream& cout) {
273 auto super_device = GetMetadataSuperBlockDevice(metadata);
David Anderson289570e2019-05-22 14:31:06 -0700274 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 Anderson8a403a82018-11-13 11:39:19 -0800279class FileOrBlockDeviceOpener final : public PartitionOpener {
280public:
281 android::base::unique_fd Open(const std::string& path, int flags) const override {
282 // Try a local file first.
Yifan Hongb84fb572019-03-15 14:19:08 -0700283 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 Anderson8a403a82018-11-13 11:39:19 -0800292 return PartitionOpener::Open(path, flags);
293 }
294};
295
Yifan Hong6ba2d0c2019-09-23 18:01:20 -0700296std::optional<std::tuple<std::string, uint64_t>>
297ParseLinearExtentData(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 Anderson674ae9e2019-09-05 17:57:20 -0700306static 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 Andersonab1b0952019-12-12 17:12:27 -0800312 cout << "Header flags: " << BuildHeaderFlagString(pt.header.flags) << "\n";
David Anderson674ae9e2019-09-05 17:57:20 -0700313 cout << "Partition table:\n";
314 cout << "------------------------\n";
315
Yifan Hong6ba2d0c2019-09-23 18:01:20 -0700316 std::vector<std::tuple<std::string, const LpMetadataExtent*>> extents;
317
David Anderson674ae9e2019-09-05 17:57:20 -0700318 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 Hong6ba2d0c2019-09-23 18:01:20 -0700338 extents.push_back(std::make_tuple(name, &extent));
David Anderson674ae9e2019-09-05 17:57:20 -0700339 cout << "\n";
340 }
341 cout << "------------------------\n";
342 }
343
Yifan Hong6ba2d0c2019-09-23 18:01:20 -0700344 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 Anderson674ae9e2019-09-05 17:57:20 -0700361 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
383static 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 Hongb48d79f2019-03-19 16:18:11 -0700390int LpdumpMain(int argc, char* argv[], std::ostream& cout, std::ostream& cerr) {
Yifan Hong7ea1a932019-03-15 16:06:50 -0700391 // clang-format off
David Anderson41241232018-06-13 16:50:11 -0700392 struct option options[] = {
David Andersoncd5227d2019-09-05 18:31:12 -0700393 { "all", no_argument, nullptr, 'a' },
David Anderson41241232018-06-13 16:50:11 -0700394 { "slot", required_argument, nullptr, 's' },
395 { "help", no_argument, nullptr, 'h' },
Yifan Hong7ea1a932019-03-15 16:06:50 -0700396 { "json", no_argument, nullptr, 'j' },
David Anderson289570e2019-05-22 14:31:06 -0700397 { "dump-metadata-size", no_argument, nullptr, 'd' },
398 { "is-super-empty", no_argument, nullptr, 'e' },
David Anderson41241232018-06-13 16:50:11 -0700399 { nullptr, 0, nullptr, 0 },
400 };
Yifan Hong7ea1a932019-03-15 16:06:50 -0700401 // clang-format on
David Anderson41241232018-06-13 16:50:11 -0700402
Yifan Hongb84fb572019-03-15 14:19:08 -0700403 // Allow this function to be invoked by lpdumpd multiple times.
404 optind = 1;
405
David Anderson41241232018-06-13 16:50:11 -0700406 int rv;
407 int index;
Yifan Hong7ea1a932019-03-15 16:06:50 -0700408 bool json = false;
David Anderson289570e2019-05-22 14:31:06 -0700409 bool dump_metadata_size = false;
David Andersoncd5227d2019-09-05 18:31:12 -0700410 bool dump_all = false;
David Anderson674ae9e2019-09-05 17:57:20 -0700411 std::optional<uint32_t> slot;
David Anderson289570e2019-05-22 14:31:06 -0700412 while ((rv = getopt_long_only(argc, argv, "s:jhde", options, &index)) != -1) {
David Anderson41241232018-06-13 16:50:11 -0700413 switch (rv) {
David Andersoncd5227d2019-09-05 18:31:12 -0700414 case 'a':
415 dump_all = true;
416 break;
David Anderson41241232018-06-13 16:50:11 -0700417 case 'h':
David Anderson58871e42019-11-19 17:07:14 -0800418 usage(argc, argv, cout);
David Anderson289570e2019-05-22 14:31:06 -0700419 return EX_OK;
David Anderson674ae9e2019-09-05 17:57:20 -0700420 case 's': {
421 uint32_t slot_arg;
422 if (android::base::ParseUint(optarg, &slot_arg)) {
423 slot = slot_arg;
424 } else {
David Anderson41241232018-06-13 16:50:11 -0700425 slot = SlotNumberForSlotSuffix(optarg);
426 }
427 break;
David Anderson674ae9e2019-09-05 17:57:20 -0700428 }
David Anderson289570e2019-05-22 14:31:06 -0700429 case 'e':
David Anderson674ae9e2019-09-05 17:57:20 -0700430 // This is ignored, we now derive whether it's empty automatically.
David Anderson289570e2019-05-22 14:31:06 -0700431 break;
432 case 'd':
433 dump_metadata_size = true;
434 break;
Yifan Hong7ea1a932019-03-15 16:06:50 -0700435 case 'j':
436 json = true;
437 break;
David Anderson289570e2019-05-22 14:31:06 -0700438 case '?':
439 case ':':
440 return usage(argc, argv, cerr);
David Anderson41241232018-06-13 16:50:11 -0700441 }
442 }
443
David Andersoncd5227d2019-09-05 18:31:12 -0700444 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 Anderson674ae9e2019-09-05 17:57:20 -0700458#ifdef __ANDROID__
459 // Use the current slot as a default for A/B devices.
David Anderson998892b2019-09-05 18:53:21 -0700460 auto current_slot_suffix = GetSlotSuffix();
461 if (!slot.has_value() && !current_slot_suffix.empty()) {
462 slot = SlotNumberForSlotSuffix(current_slot_suffix);
David Anderson674ae9e2019-09-05 17:57:20 -0700463 }
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 Anderson998892b2019-09-05 18:53:21 -0700471 // 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 Anderson674ae9e2019-09-05 17:57:20 -0700474 std::string super_path;
David Anderson998892b2019-09-05 18:53:21 -0700475 bool override_super_name = (optind < argc);
476 if (override_super_name) {
David Anderson674ae9e2019-09-05 17:57:20 -0700477 super_path = argv[optind++];
David Anderson299abc72018-12-12 13:36:43 -0800478 } else {
479#ifdef __ANDROID__
David Anderson998892b2019-09-05 18:53:21 -0700480 super_path = GetSuperPartitionName(slot);
David Anderson299abc72018-12-12 13:36:43 -0800481#else
David Anderson674ae9e2019-09-05 17:57:20 -0700482 cerr << "Must specify a super partition image.\n";
Yifan Hongb48d79f2019-03-19 16:18:11 -0700483 return usage(argc, argv, cerr);
David Anderson299abc72018-12-12 13:36:43 -0800484#endif
David Anderson41241232018-06-13 16:50:11 -0700485 }
Yifan Hong7ea1a932019-03-15 16:06:50 -0700486
David Anderson674ae9e2019-09-05 17:57:20 -0700487 auto pt = ReadDeviceOrFile(super_path, slot.value());
488
Yifan Hong7ea1a932019-03-15 16:06:50 -0700489 // --json option doesn't require metadata to be present.
490 if (json) {
491 return PrintJson(pt.get(), cout, cerr);
492 }
493
David Anderson41241232018-06-13 16:50:11 -0700494 if (!pt) {
Yifan Hongb48d79f2019-03-19 16:18:11 -0700495 cerr << "Failed to read metadata.\n";
David Anderson41241232018-06-13 16:50:11 -0700496 return EX_NOINPUT;
497 }
498
David Anderson289570e2019-05-22 14:31:06 -0700499 if (dump_metadata_size) {
David Anderson674ae9e2019-09-05 17:57:20 -0700500 return DumpMetadataSize(*pt.get(), cout);
David Anderson289570e2019-05-22 14:31:06 -0700501 }
502
David Andersoncd5227d2019-09-05 18:31:12 -0700503 // 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 Anderson998892b2019-09-05 18:53:21 -0700507 uint32_t num_slots = current_slot_suffix.empty() ? 1 : 2;
David Andersoncd5227d2019-09-05 18:31:12 -0700508 if (dump_all && num_slots > 1) {
David Anderson998892b2019-09-05 18:53:21 -0700509 cout << "Current slot: " << current_slot_suffix << "\n";
David Andersoncd5227d2019-09-05 18:31:12 -0700510 }
511#else
512 uint32_t num_slots = pt->geometry.metadata_slot_count;
513#endif
David Anderson998892b2019-09-05 18:53:21 -0700514 // Empty images only have one slot.
515 if (IsEmptySuperImage(super_path)) {
516 num_slots = 1;
517 }
David Andersoncd5227d2019-09-05 18:31:12 -0700518
David Anderson998892b2019-09-05 18:53:21 -0700519 if (num_slots > 1) {
David Anderson674ae9e2019-09-05 17:57:20 -0700520 cout << "Slot " << slot.value() << ":\n";
David Anderson41241232018-06-13 16:50:11 -0700521 }
David Anderson674ae9e2019-09-05 17:57:20 -0700522 PrintMetadata(*pt.get(), cout);
David Andersoncd5227d2019-09-05 18:31:12 -0700523
David Anderson998892b2019-09-05 18:53:21 -0700524 if (dump_all) {
David Andersoncd5227d2019-09-05 18:31:12 -0700525 for (uint32_t i = 1; i < num_slots; i++) {
David Anderson998892b2019-09-05 18:53:21 -0700526 if (!override_super_name) {
527 super_path = GetSuperPartitionName(i);
528 }
529
David Andersoncd5227d2019-09-05 18:31:12 -0700530 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 Anderson41241232018-06-13 16:50:11 -0700539 return EX_OK;
540}
Yifan Hongb48d79f2019-03-19 16:18:11 -0700541
Yifan Hongb84fb572019-03-15 14:19:08 -0700542int LpdumpMain(int argc, char* argv[]) {
Yifan Hongb48d79f2019-03-19 16:18:11 -0700543 return LpdumpMain(argc, argv, std::cout, std::cerr);
544}