blob: 32aa12f27eb7efabc646bce0bb3b215ee6362dc3 [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 Zhange7ca3692023-12-20 13:54:27 -080042DEFINE_int32(cow_version,
43 0,
44 "VABC Cow version to use. Default is to use what's specified in "
45 "the OTA manifest");
Kelvin Zhang83bb5d52023-11-03 09:57:15 -070046DEFINE_string(vabc_compression_param,
47 "",
48 "Compression parameter for VABC. Default is use what's specified "
49 "in OTA package");
Kelvin Zhangb93055f2021-02-03 14:22:35 -050050
51namespace chromeos_update_engine {
52
David Anderson8144b252023-05-09 22:16:40 -070053bool ProcessPartition(
54 const chromeos_update_engine::DeltaArchiveManifest& manifest,
55 const chromeos_update_engine::PartitionUpdate& partition,
56 const char* image_dir) {
Kelvin Zhangb93055f2021-02-03 14:22:35 -050057 base::FilePath img_dir{image_dir};
58 auto target_img = img_dir.Append(partition.partition_name() + ".img");
59 auto output_cow = img_dir.Append(partition.partition_name() + ".cow");
60 FileDescriptorPtr target_img_fd = std::make_shared<EintrSafeFileDescriptor>();
61 if (!target_img_fd->Open(target_img.value().c_str(), O_RDONLY)) {
62 PLOG(ERROR) << "Failed to open " << target_img.value();
63 return false;
64 }
65 android::base::unique_fd output_fd{
66 open(output_cow.value().c_str(), O_RDWR | O_CREAT, 0744)};
67 if (output_fd < 0) {
68 PLOG(ERROR) << "Failed to open " << output_cow.value();
69 return false;
70 }
71
David Anderson8144b252023-05-09 22:16:40 -070072 const auto& dap = manifest.dynamic_partition_metadata();
73
74 android::snapshot::CowOptions options{
75 .block_size = static_cast<uint32_t>(manifest.block_size()),
Kelvin Zhange7ca3692023-12-20 13:54:27 -080076 .compression = dap.vabc_compression_param(),
77 .batch_write = true,
78 .op_count_max = static_cast<uint32_t>(
79 partition.new_partition_info().size() / manifest.block_size())};
Kelvin Zhang83bb5d52023-11-03 09:57:15 -070080 if (!FLAGS_vabc_compression_param.empty()) {
81 options.compression = FLAGS_vabc_compression_param;
82 }
Kelvin Zhange7ca3692023-12-20 13:54:27 -080083 auto cow_version = dap.cow_version();
84 if (FLAGS_cow_version > 0) {
85 cow_version = FLAGS_cow_version;
86 LOG(INFO) << "Using user specified COW version " << cow_version;
87 }
David Anderson8144b252023-05-09 22:16:40 -070088 auto cow_writer = android::snapshot::CreateCowWriter(
Kelvin Zhange7ca3692023-12-20 13:54:27 -080089 cow_version, options, std::move(output_fd));
David Anderson8144b252023-05-09 22:16:40 -070090 TEST_AND_RETURN_FALSE(cow_writer);
Kelvin Zhange36e4a32021-08-27 13:48:35 -070091 TEST_AND_RETURN_FALSE(CowDryRun(nullptr,
92 target_img_fd,
Kelvin Zhangb93055f2021-02-03 14:22:35 -050093 partition.operations(),
94 partition.merge_operations(),
David Anderson8144b252023-05-09 22:16:40 -070095 manifest.block_size(),
96 cow_writer.get(),
Kelvin Zhange36e4a32021-08-27 13:48:35 -070097 partition.new_partition_info().size(),
Daniel Zheng1ab64152024-04-03 14:25:40 -070098 partition.old_partition_info().size(),
Kelvin Zhange36e4a32021-08-27 13:48:35 -070099 false));
David Anderson8144b252023-05-09 22:16:40 -0700100 TEST_AND_RETURN_FALSE(cow_writer->Finalize());
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500101 return true;
102}
103
104} // namespace chromeos_update_engine
105
106using chromeos_update_engine::MetadataParseResult;
107using chromeos_update_engine::PayloadMetadata;
108
Kelvin Zhang901c7d52022-06-21 09:35:45 -0700109int main(int argc, char* argv[]) {
110 gflags::SetUsageMessage(
111 "A tool to extract device images from Android OTA packages");
112 gflags::ParseCommandLineFlags(&argc, &argv, true);
113 auto tokens = android::base::Tokenize(FLAGS_partitions, ",");
114 const std::set<std::string> partitions(
115 std::make_move_iterator(tokens.begin()),
116 std::make_move_iterator(tokens.end()));
117
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500118 if (argc != 3) {
119 printf("Usage: %s <payload.bin> <extracted target_file>\n", argv[0]);
120 return -1;
121 }
122 const char* payload_path = argv[1];
123 const char* images_dir = argv[2];
124 int payload_fd = open(payload_path, O_RDONLY);
125 if (payload_fd < 0) {
126 PLOG(ERROR) << "Failed to open payload file:";
127 return 1;
128 }
129 chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
130 auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
131 if (payload_size <= 0) {
132 PLOG(ERROR)
133 << "Couldn't determine size of payload file, or payload file is empty";
134 return 2;
135 }
136
137 PayloadMetadata payload_metadata;
138 auto payload = static_cast<unsigned char*>(
139 mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
140
141 // C++ dark magic to ensure that |payload| is properly deallocated once the
142 // program exits.
143 auto munmap_deleter = [payload_size](auto payload) {
144 munmap(payload, payload_size);
145 };
146 std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
147 payload, munmap_deleter};
148
149 if (payload == nullptr) {
150 PLOG(ERROR) << "Failed to mmap() payload file";
151 return 3;
152 }
153 if (payload_metadata.ParsePayloadHeader(payload, payload_size, nullptr) !=
154 chromeos_update_engine::MetadataParseResult::kSuccess) {
155 LOG(ERROR) << "Payload header parse failed!";
156 return 4;
157 }
158 chromeos_update_engine::DeltaArchiveManifest manifest;
159 if (!payload_metadata.GetManifest(payload, payload_size, &manifest)) {
160 LOG(ERROR) << "Failed to parse manifest!";
161 return 5;
162 }
163
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700164 size_t estimated_total_cow_size = 0;
165 size_t actual_total_cow_size = 0;
166
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500167 for (const auto& partition : manifest.partitions()) {
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700168 if (partition.estimate_cow_size() == 0) {
169 continue;
170 }
Kelvin Zhang901c7d52022-06-21 09:35:45 -0700171 if (!partitions.empty() &&
172 partitions.count(partition.partition_name()) == 0) {
173 continue;
174 }
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500175 LOG(INFO) << partition.partition_name();
David Anderson8144b252023-05-09 22:16:40 -0700176 if (!ProcessPartition(manifest, partition, images_dir)) {
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500177 return 6;
178 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700179 base::FilePath img_dir{images_dir};
180 const auto output_cow =
181 img_dir.Append(partition.partition_name() + ".cow").value();
182 const auto actual_cow_size =
183 chromeos_update_engine::utils::FileSize(output_cow);
184 LOG(INFO) << partition.partition_name()
185 << ": estimated COW size is: " << partition.estimate_cow_size()
186 << ", actual COW size is: " << actual_cow_size
187 << ", estimated COW size is "
188 << (actual_cow_size - partition.estimate_cow_size()) * 100.0f /
189 actual_cow_size
190 << "% smaller";
191 estimated_total_cow_size += partition.estimate_cow_size();
192 actual_total_cow_size += actual_cow_size;
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500193 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700194
195 LOG(INFO) << "Total estimated COW size is: " << estimated_total_cow_size
196 << ", Total actual COW size is: " << actual_total_cow_size
197 << ", estimated COW size is "
198 << (actual_total_cow_size - estimated_total_cow_size) * 100.0f /
199 actual_total_cow_size
200 << "% smaller";
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500201 return 0;
202}