blob: b9ec00a609b7aa016553bd58cef63877c6f5a22c [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
38using android::snapshot::CowWriter;
Kelvin Zhang901c7d52022-06-21 09:35:45 -070039DEFINE_string(partitions,
40 "",
41 "Comma separated list of partitions to extract, leave empty for "
42 "extracting all partitions");
Kelvin Zhangb93055f2021-02-03 14:22:35 -050043
44namespace chromeos_update_engine {
45
46bool ProcessPartition(const chromeos_update_engine::PartitionUpdate& partition,
47 const char* image_dir,
48 size_t block_size) {
49 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
64 android::snapshot::CowWriter cow_writer{
65 {.block_size = static_cast<uint32_t>(block_size), .compression = "gz"}};
66 TEST_AND_RETURN_FALSE(cow_writer.Initialize(output_fd));
Kelvin Zhange36e4a32021-08-27 13:48:35 -070067 TEST_AND_RETURN_FALSE(CowDryRun(nullptr,
68 target_img_fd,
Kelvin Zhangb93055f2021-02-03 14:22:35 -050069 partition.operations(),
70 partition.merge_operations(),
71 block_size,
Kelvin Zhange36e4a32021-08-27 13:48:35 -070072 &cow_writer,
73 partition.new_partition_info().size(),
74 false));
Kelvin Zhangb93055f2021-02-03 14:22:35 -050075 TEST_AND_RETURN_FALSE(cow_writer.Finalize());
76 return true;
77}
78
79} // namespace chromeos_update_engine
80
81using chromeos_update_engine::MetadataParseResult;
82using chromeos_update_engine::PayloadMetadata;
83
Kelvin Zhang901c7d52022-06-21 09:35:45 -070084int main(int argc, char* argv[]) {
85 gflags::SetUsageMessage(
86 "A tool to extract device images from Android OTA packages");
87 gflags::ParseCommandLineFlags(&argc, &argv, true);
88 auto tokens = android::base::Tokenize(FLAGS_partitions, ",");
89 const std::set<std::string> partitions(
90 std::make_move_iterator(tokens.begin()),
91 std::make_move_iterator(tokens.end()));
92
Kelvin Zhangb93055f2021-02-03 14:22:35 -050093 if (argc != 3) {
94 printf("Usage: %s <payload.bin> <extracted target_file>\n", argv[0]);
95 return -1;
96 }
97 const char* payload_path = argv[1];
98 const char* images_dir = argv[2];
99 int payload_fd = open(payload_path, O_RDONLY);
100 if (payload_fd < 0) {
101 PLOG(ERROR) << "Failed to open payload file:";
102 return 1;
103 }
104 chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
105 auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
106 if (payload_size <= 0) {
107 PLOG(ERROR)
108 << "Couldn't determine size of payload file, or payload file is empty";
109 return 2;
110 }
111
112 PayloadMetadata payload_metadata;
113 auto payload = static_cast<unsigned char*>(
114 mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
115
116 // C++ dark magic to ensure that |payload| is properly deallocated once the
117 // program exits.
118 auto munmap_deleter = [payload_size](auto payload) {
119 munmap(payload, payload_size);
120 };
121 std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
122 payload, munmap_deleter};
123
124 if (payload == nullptr) {
125 PLOG(ERROR) << "Failed to mmap() payload file";
126 return 3;
127 }
128 if (payload_metadata.ParsePayloadHeader(payload, payload_size, nullptr) !=
129 chromeos_update_engine::MetadataParseResult::kSuccess) {
130 LOG(ERROR) << "Payload header parse failed!";
131 return 4;
132 }
133 chromeos_update_engine::DeltaArchiveManifest manifest;
134 if (!payload_metadata.GetManifest(payload, payload_size, &manifest)) {
135 LOG(ERROR) << "Failed to parse manifest!";
136 return 5;
137 }
138
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700139 size_t estimated_total_cow_size = 0;
140 size_t actual_total_cow_size = 0;
141
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500142 for (const auto& partition : manifest.partitions()) {
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700143 if (partition.estimate_cow_size() == 0) {
144 continue;
145 }
Kelvin Zhang901c7d52022-06-21 09:35:45 -0700146 if (!partitions.empty() &&
147 partitions.count(partition.partition_name()) == 0) {
148 continue;
149 }
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500150 LOG(INFO) << partition.partition_name();
151 if (!ProcessPartition(partition, images_dir, manifest.block_size())) {
152 return 6;
153 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700154 base::FilePath img_dir{images_dir};
155 const auto output_cow =
156 img_dir.Append(partition.partition_name() + ".cow").value();
157 const auto actual_cow_size =
158 chromeos_update_engine::utils::FileSize(output_cow);
159 LOG(INFO) << partition.partition_name()
160 << ": estimated COW size is: " << partition.estimate_cow_size()
161 << ", actual COW size is: " << actual_cow_size
162 << ", estimated COW size is "
163 << (actual_cow_size - partition.estimate_cow_size()) * 100.0f /
164 actual_cow_size
165 << "% smaller";
166 estimated_total_cow_size += partition.estimate_cow_size();
167 actual_total_cow_size += actual_cow_size;
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500168 }
Kelvin Zhange4f70e82021-10-13 14:56:59 -0700169
170 LOG(INFO) << "Total estimated COW size is: " << estimated_total_cow_size
171 << ", Total actual COW size is: " << actual_total_cow_size
172 << ", estimated COW size is "
173 << (actual_total_cow_size - estimated_total_cow_size) * 100.0f /
174 actual_total_cow_size
175 << "% smaller";
Kelvin Zhangb93055f2021-02-03 14:22:35 -0500176 return 0;
177}