blob: 80d161f8d50f8e54e0c01193a1d36427d7018f72 [file] [log] [blame]
Kelvin Zhangb93055f2021-02-03 14:22:35 -05001//
2// Copyright (C) 2021 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
Colin Cross238d9042021-12-21 13:09:30 -080017#include <fcntl.h>
Kelvin Zhangb93055f2021-02-03 14:22:35 -050018#include <stdio.h>
19#include <string.h>
20
21#include <cstdint>
22#include <cstdio>
23#include <memory>
24
Kelvin Zhangb93055f2021-02-03 14:22:35 -050025#include <sys/mman.h>
26#include <sys/stat.h>
27
28#include <base/files/file_path.h>
29#include <libsnapshot/cow_writer.h>
Kelvin Zhang901c7d52022-06-21 09:35:45 -070030#include <gflags/gflags.h>
Kelvin Zhangb93055f2021-02-03 14:22:35 -050031
Kelvin Zhangb93055f2021-02-03 14:22:35 -050032#include "update_engine/common/utils.h"
33#include "update_engine/payload_consumer/file_descriptor.h"
34#include "update_engine/payload_consumer/payload_metadata.h"
35#include "update_engine/payload_generator/cow_size_estimator.h"
36#include "update_engine/update_metadata.pb.h"
37
Kelvin Zhang901c7d52022-06-21 09:35:45 -070038DEFINE_string(partitions,
39 "",
40 "Comma separated list of partitions to extract, leave empty for "
41 "extracting all partitions");
Kelvin Zhangb93055f2021-02-03 14:22:35 -050042
43namespace chromeos_update_engine {
44
David Anderson8144b252023-05-09 22:16:40 -070045bool ProcessPartition(
46 const chromeos_update_engine::DeltaArchiveManifest& manifest,
47 const chromeos_update_engine::PartitionUpdate& partition,
48 const char* image_dir) {
Kelvin Zhangb93055f2021-02-03 14:22:35 -050049 base::FilePath img_dir{image_dir};
50 auto target_img = img_dir.Append(partition.partition_name() + ".img");
51 auto output_cow = img_dir.Append(partition.partition_name() + ".cow");
52 FileDescriptorPtr target_img_fd = std::make_shared<EintrSafeFileDescriptor>();
53 if (!target_img_fd->Open(target_img.value().c_str(), O_RDONLY)) {
54 PLOG(ERROR) << "Failed to open " << target_img.value();
55 return false;
56 }
57 android::base::unique_fd output_fd{
58 open(output_cow.value().c_str(), O_RDWR | O_CREAT, 0744)};
59 if (output_fd < 0) {
60 PLOG(ERROR) << "Failed to open " << output_cow.value();
61 return false;
62 }
63
David Anderson8144b252023-05-09 22:16:40 -070064 const auto& dap = manifest.dynamic_partition_metadata();
65
66 android::snapshot::CowOptions options{
67 .block_size = static_cast<uint32_t>(manifest.block_size()),
68 .compression = dap.vabc_compression_param()};
69 auto cow_writer = android::snapshot::CreateCowWriter(
70 dap.cow_version(), options, std::move(output_fd));
71 TEST_AND_RETURN_FALSE(cow_writer);
Kelvin Zhange36e4a32021-08-27 13:48:35 -070072 TEST_AND_RETURN_FALSE(CowDryRun(nullptr,
73 target_img_fd,
Kelvin Zhangb93055f2021-02-03 14:22:35 -050074 partition.operations(),
75 partition.merge_operations(),
David Anderson8144b252023-05-09 22:16:40 -070076 manifest.block_size(),
77 cow_writer.get(),
Kelvin Zhange36e4a32021-08-27 13:48:35 -070078 partition.new_partition_info().size(),
79 false));
David Anderson8144b252023-05-09 22:16:40 -070080 TEST_AND_RETURN_FALSE(cow_writer->Finalize());
Kelvin Zhangb93055f2021-02-03 14:22:35 -050081 return true;
82}
83
84} // namespace chromeos_update_engine
85
86using chromeos_update_engine::MetadataParseResult;
87using chromeos_update_engine::PayloadMetadata;
88
Kelvin Zhang901c7d52022-06-21 09:35:45 -070089int main(int argc, char* argv[]) {
90 gflags::SetUsageMessage(
91 "A tool to extract device images from Android OTA packages");
92 gflags::ParseCommandLineFlags(&argc, &argv, true);
93 auto tokens = android::base::Tokenize(FLAGS_partitions, ",");
94 const std::set<std::string> partitions(
95 std::make_move_iterator(tokens.begin()),
96 std::make_move_iterator(tokens.end()));
97
Kelvin Zhangb93055f2021-02-03 14:22:35 -050098 if (argc != 3) {
99 printf("Usage: %s <payload.bin> <extracted target_file>\n", argv[0]);
100 return -1;
101 }
102 const char* payload_path = argv[1];
103 const char* images_dir = argv[2];
104 int payload_fd = open(payload_path, O_RDONLY);
105 if (payload_fd < 0) {
106 PLOG(ERROR) << "Failed to open payload file:";
107 return 1;
108 }
109 chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
110 auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
111 if (payload_size <= 0) {
112 PLOG(ERROR)
113 << "Couldn't determine size of payload file, or payload file is empty";
114 return 2;
115 }
116
117 PayloadMetadata payload_metadata;
118 auto payload = static_cast<unsigned char*>(
119 mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
120
121 // C++ dark magic to ensure that |payload| is properly deallocated once the
122 // program exits.
123 auto munmap_deleter = [payload_size](auto payload) {
124 munmap(payload, payload_size);
125 };
126 std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
127 payload, munmap_deleter};
128
129 if (payload == nullptr) {
130 PLOG(ERROR) << "Failed to mmap() payload file";
131 return 3;
132 }
133 if (payload_metadata.ParsePayloadHeader(payload, payload_size, nullptr) !=
134 chromeos_update_engine::MetadataParseResult::kSuccess) {
135 LOG(ERROR) << "Payload header parse failed!";
136 return 4;
137 }
138 chromeos_update_engine::DeltaArchiveManifest manifest;
139 if (!payload_metadata.GetManifest(payload, payload_size, &manifest)) {
140 LOG(ERROR) << "Failed to parse manifest!";
141 return 5;
142 }
143
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700144 size_t estimated_total_cow_size = 0;
145 size_t actual_total_cow_size = 0;
146
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500147 for (const auto& partition : manifest.partitions()) {
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700148 if (partition.estimate_cow_size() == 0) {
149 continue;
150 }
Kelvin Zhang901c7d52022-06-21 09:35:45 -0700151 if (!partitions.empty() &&
152 partitions.count(partition.partition_name()) == 0) {
153 continue;
154 }
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500155 LOG(INFO) << partition.partition_name();
David Anderson8144b252023-05-09 22:16:40 -0700156 if (!ProcessPartition(manifest, partition, images_dir)) {
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500157 return 6;
158 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700159 base::FilePath img_dir{images_dir};
160 const auto output_cow =
161 img_dir.Append(partition.partition_name() + ".cow").value();
162 const auto actual_cow_size =
163 chromeos_update_engine::utils::FileSize(output_cow);
164 LOG(INFO) << partition.partition_name()
165 << ": estimated COW size is: " << partition.estimate_cow_size()
166 << ", actual COW size is: " << actual_cow_size
167 << ", estimated COW size is "
168 << (actual_cow_size - partition.estimate_cow_size()) * 100.0f /
169 actual_cow_size
170 << "% smaller";
171 estimated_total_cow_size += partition.estimate_cow_size();
172 actual_total_cow_size += actual_cow_size;
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500173 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700174
175 LOG(INFO) << "Total estimated COW size is: " << estimated_total_cow_size
176 << ", Total actual COW size is: " << actual_total_cow_size
177 << ", estimated COW size is "
178 << (actual_total_cow_size - estimated_total_cow_size) * 100.0f /
179 actual_total_cow_size
180 << "% smaller";
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500181 return 0;
182}