blob: 1ec3540eafd3fd965d05552a09648af9f56779e5 [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 Zhang83bb5d52023-11-03 09:57:15 -070042DEFINE_string(vabc_compression_param,
43 "",
44 "Compression parameter for VABC. Default is use what's specified "
45 "in OTA package");
Kelvin Zhangb93055f2021-02-03 14:22:35 -050046
47namespace chromeos_update_engine {
48
David Anderson8144b252023-05-09 22:16:40 -070049bool ProcessPartition(
50 const chromeos_update_engine::DeltaArchiveManifest& manifest,
51 const chromeos_update_engine::PartitionUpdate& partition,
52 const char* image_dir) {
Kelvin Zhangb93055f2021-02-03 14:22:35 -050053 base::FilePath img_dir{image_dir};
54 auto target_img = img_dir.Append(partition.partition_name() + ".img");
55 auto output_cow = img_dir.Append(partition.partition_name() + ".cow");
56 FileDescriptorPtr target_img_fd = std::make_shared<EintrSafeFileDescriptor>();
57 if (!target_img_fd->Open(target_img.value().c_str(), O_RDONLY)) {
58 PLOG(ERROR) << "Failed to open " << target_img.value();
59 return false;
60 }
61 android::base::unique_fd output_fd{
62 open(output_cow.value().c_str(), O_RDWR | O_CREAT, 0744)};
63 if (output_fd < 0) {
64 PLOG(ERROR) << "Failed to open " << output_cow.value();
65 return false;
66 }
67
David Anderson8144b252023-05-09 22:16:40 -070068 const auto& dap = manifest.dynamic_partition_metadata();
69
70 android::snapshot::CowOptions options{
71 .block_size = static_cast<uint32_t>(manifest.block_size()),
72 .compression = dap.vabc_compression_param()};
Kelvin Zhang83bb5d52023-11-03 09:57:15 -070073 if (!FLAGS_vabc_compression_param.empty()) {
74 options.compression = FLAGS_vabc_compression_param;
75 }
David Anderson8144b252023-05-09 22:16:40 -070076 auto cow_writer = android::snapshot::CreateCowWriter(
77 dap.cow_version(), options, std::move(output_fd));
78 TEST_AND_RETURN_FALSE(cow_writer);
Kelvin Zhange36e4a32021-08-27 13:48:35 -070079 TEST_AND_RETURN_FALSE(CowDryRun(nullptr,
80 target_img_fd,
Kelvin Zhangb93055f2021-02-03 14:22:35 -050081 partition.operations(),
82 partition.merge_operations(),
David Anderson8144b252023-05-09 22:16:40 -070083 manifest.block_size(),
84 cow_writer.get(),
Kelvin Zhange36e4a32021-08-27 13:48:35 -070085 partition.new_partition_info().size(),
86 false));
David Anderson8144b252023-05-09 22:16:40 -070087 TEST_AND_RETURN_FALSE(cow_writer->Finalize());
Kelvin Zhangb93055f2021-02-03 14:22:35 -050088 return true;
89}
90
91} // namespace chromeos_update_engine
92
93using chromeos_update_engine::MetadataParseResult;
94using chromeos_update_engine::PayloadMetadata;
95
Kelvin Zhang901c7d52022-06-21 09:35:45 -070096int main(int argc, char* argv[]) {
97 gflags::SetUsageMessage(
98 "A tool to extract device images from Android OTA packages");
99 gflags::ParseCommandLineFlags(&argc, &argv, true);
100 auto tokens = android::base::Tokenize(FLAGS_partitions, ",");
101 const std::set<std::string> partitions(
102 std::make_move_iterator(tokens.begin()),
103 std::make_move_iterator(tokens.end()));
104
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500105 if (argc != 3) {
106 printf("Usage: %s <payload.bin> <extracted target_file>\n", argv[0]);
107 return -1;
108 }
109 const char* payload_path = argv[1];
110 const char* images_dir = argv[2];
111 int payload_fd = open(payload_path, O_RDONLY);
112 if (payload_fd < 0) {
113 PLOG(ERROR) << "Failed to open payload file:";
114 return 1;
115 }
116 chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
117 auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
118 if (payload_size <= 0) {
119 PLOG(ERROR)
120 << "Couldn't determine size of payload file, or payload file is empty";
121 return 2;
122 }
123
124 PayloadMetadata payload_metadata;
125 auto payload = static_cast<unsigned char*>(
126 mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
127
128 // C++ dark magic to ensure that |payload| is properly deallocated once the
129 // program exits.
130 auto munmap_deleter = [payload_size](auto payload) {
131 munmap(payload, payload_size);
132 };
133 std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
134 payload, munmap_deleter};
135
136 if (payload == nullptr) {
137 PLOG(ERROR) << "Failed to mmap() payload file";
138 return 3;
139 }
140 if (payload_metadata.ParsePayloadHeader(payload, payload_size, nullptr) !=
141 chromeos_update_engine::MetadataParseResult::kSuccess) {
142 LOG(ERROR) << "Payload header parse failed!";
143 return 4;
144 }
145 chromeos_update_engine::DeltaArchiveManifest manifest;
146 if (!payload_metadata.GetManifest(payload, payload_size, &manifest)) {
147 LOG(ERROR) << "Failed to parse manifest!";
148 return 5;
149 }
150
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700151 size_t estimated_total_cow_size = 0;
152 size_t actual_total_cow_size = 0;
153
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500154 for (const auto& partition : manifest.partitions()) {
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700155 if (partition.estimate_cow_size() == 0) {
156 continue;
157 }
Kelvin Zhang901c7d52022-06-21 09:35:45 -0700158 if (!partitions.empty() &&
159 partitions.count(partition.partition_name()) == 0) {
160 continue;
161 }
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500162 LOG(INFO) << partition.partition_name();
David Anderson8144b252023-05-09 22:16:40 -0700163 if (!ProcessPartition(manifest, partition, images_dir)) {
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500164 return 6;
165 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700166 base::FilePath img_dir{images_dir};
167 const auto output_cow =
168 img_dir.Append(partition.partition_name() + ".cow").value();
169 const auto actual_cow_size =
170 chromeos_update_engine::utils::FileSize(output_cow);
171 LOG(INFO) << partition.partition_name()
172 << ": estimated COW size is: " << partition.estimate_cow_size()
173 << ", actual COW size is: " << actual_cow_size
174 << ", estimated COW size is "
175 << (actual_cow_size - partition.estimate_cow_size()) * 100.0f /
176 actual_cow_size
177 << "% smaller";
178 estimated_total_cow_size += partition.estimate_cow_size();
179 actual_total_cow_size += actual_cow_size;
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500180 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700181
182 LOG(INFO) << "Total estimated COW size is: " << estimated_total_cow_size
183 << ", Total actual COW size is: " << actual_total_cow_size
184 << ", estimated COW size is "
185 << (actual_total_cow_size - estimated_total_cow_size) * 100.0f /
186 actual_total_cow_size
187 << "% smaller";
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500188 return 0;
189}