blob: e46460562d2fbc35b6e6e647c24ccc98bed00b1a [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
32#include "update_engine/common/cow_operation_convert.h"
33#include "update_engine/common/utils.h"
34#include "update_engine/payload_consumer/file_descriptor.h"
35#include "update_engine/payload_consumer/payload_metadata.h"
36#include "update_engine/payload_generator/cow_size_estimator.h"
37#include "update_engine/update_metadata.pb.h"
38
39using android::snapshot::CowWriter;
Kelvin Zhang901c7d52022-06-21 09:35:45 -070040DEFINE_string(partitions,
41 "",
42 "Comma separated list of partitions to extract, leave empty for "
43 "extracting all partitions");
Kelvin Zhangb93055f2021-02-03 14:22:35 -050044
45namespace chromeos_update_engine {
46
47bool ProcessPartition(const chromeos_update_engine::PartitionUpdate& partition,
48 const char* image_dir,
49 size_t block_size) {
50 base::FilePath img_dir{image_dir};
51 auto target_img = img_dir.Append(partition.partition_name() + ".img");
52 auto output_cow = img_dir.Append(partition.partition_name() + ".cow");
53 FileDescriptorPtr target_img_fd = std::make_shared<EintrSafeFileDescriptor>();
54 if (!target_img_fd->Open(target_img.value().c_str(), O_RDONLY)) {
55 PLOG(ERROR) << "Failed to open " << target_img.value();
56 return false;
57 }
58 android::base::unique_fd output_fd{
59 open(output_cow.value().c_str(), O_RDWR | O_CREAT, 0744)};
60 if (output_fd < 0) {
61 PLOG(ERROR) << "Failed to open " << output_cow.value();
62 return false;
63 }
64
65 android::snapshot::CowWriter cow_writer{
66 {.block_size = static_cast<uint32_t>(block_size), .compression = "gz"}};
67 TEST_AND_RETURN_FALSE(cow_writer.Initialize(output_fd));
Kelvin Zhange36e4a32021-08-27 13:48:35 -070068 TEST_AND_RETURN_FALSE(CowDryRun(nullptr,
69 target_img_fd,
Kelvin Zhangb93055f2021-02-03 14:22:35 -050070 partition.operations(),
71 partition.merge_operations(),
72 block_size,
Kelvin Zhange36e4a32021-08-27 13:48:35 -070073 &cow_writer,
74 partition.new_partition_info().size(),
75 false));
Kelvin Zhangb93055f2021-02-03 14:22:35 -050076 TEST_AND_RETURN_FALSE(cow_writer.Finalize());
77 return true;
78}
79
80} // namespace chromeos_update_engine
81
82using chromeos_update_engine::MetadataParseResult;
83using chromeos_update_engine::PayloadMetadata;
84
Kelvin Zhang901c7d52022-06-21 09:35:45 -070085int main(int argc, char* argv[]) {
86 gflags::SetUsageMessage(
87 "A tool to extract device images from Android OTA packages");
88 gflags::ParseCommandLineFlags(&argc, &argv, true);
89 auto tokens = android::base::Tokenize(FLAGS_partitions, ",");
90 const std::set<std::string> partitions(
91 std::make_move_iterator(tokens.begin()),
92 std::make_move_iterator(tokens.end()));
93
Kelvin Zhangb93055f2021-02-03 14:22:35 -050094 if (argc != 3) {
95 printf("Usage: %s <payload.bin> <extracted target_file>\n", argv[0]);
96 return -1;
97 }
98 const char* payload_path = argv[1];
99 const char* images_dir = argv[2];
100 int payload_fd = open(payload_path, O_RDONLY);
101 if (payload_fd < 0) {
102 PLOG(ERROR) << "Failed to open payload file:";
103 return 1;
104 }
105 chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
106 auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
107 if (payload_size <= 0) {
108 PLOG(ERROR)
109 << "Couldn't determine size of payload file, or payload file is empty";
110 return 2;
111 }
112
113 PayloadMetadata payload_metadata;
114 auto payload = static_cast<unsigned char*>(
115 mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
116
117 // C++ dark magic to ensure that |payload| is properly deallocated once the
118 // program exits.
119 auto munmap_deleter = [payload_size](auto payload) {
120 munmap(payload, payload_size);
121 };
122 std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
123 payload, munmap_deleter};
124
125 if (payload == nullptr) {
126 PLOG(ERROR) << "Failed to mmap() payload file";
127 return 3;
128 }
129 if (payload_metadata.ParsePayloadHeader(payload, payload_size, nullptr) !=
130 chromeos_update_engine::MetadataParseResult::kSuccess) {
131 LOG(ERROR) << "Payload header parse failed!";
132 return 4;
133 }
134 chromeos_update_engine::DeltaArchiveManifest manifest;
135 if (!payload_metadata.GetManifest(payload, payload_size, &manifest)) {
136 LOG(ERROR) << "Failed to parse manifest!";
137 return 5;
138 }
139
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700140 size_t estimated_total_cow_size = 0;
141 size_t actual_total_cow_size = 0;
142
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500143 for (const auto& partition : manifest.partitions()) {
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700144 if (partition.estimate_cow_size() == 0) {
145 continue;
146 }
Kelvin Zhang901c7d52022-06-21 09:35:45 -0700147 if (!partitions.empty() &&
148 partitions.count(partition.partition_name()) == 0) {
149 continue;
150 }
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500151 LOG(INFO) << partition.partition_name();
152 if (!ProcessPartition(partition, images_dir, manifest.block_size())) {
153 return 6;
154 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700155 base::FilePath img_dir{images_dir};
156 const auto output_cow =
157 img_dir.Append(partition.partition_name() + ".cow").value();
158 const auto actual_cow_size =
159 chromeos_update_engine::utils::FileSize(output_cow);
160 LOG(INFO) << partition.partition_name()
161 << ": estimated COW size is: " << partition.estimate_cow_size()
162 << ", actual COW size is: " << actual_cow_size
163 << ", estimated COW size is "
164 << (actual_cow_size - partition.estimate_cow_size()) * 100.0f /
165 actual_cow_size
166 << "% smaller";
167 estimated_total_cow_size += partition.estimate_cow_size();
168 actual_total_cow_size += actual_cow_size;
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500169 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700170
171 LOG(INFO) << "Total estimated COW size is: " << estimated_total_cow_size
172 << ", Total actual COW size is: " << actual_total_cow_size
173 << ", estimated COW size is "
174 << (actual_total_cow_size - estimated_total_cow_size) * 100.0f /
175 actual_total_cow_size
176 << "% smaller";
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500177 return 0;
178}