xref: /aosp_15_r20/system/update_engine/aosp/cow_converter.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1 //
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 
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 #include <cstdint>
22 #include <cstdio>
23 #include <memory>
24 
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 
28 #include <base/files/file_path.h>
29 #include <libsnapshot/cow_writer.h>
30 #include <gflags/gflags.h>
31 
32 #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 
38 DEFINE_string(partitions,
39               "",
40               "Comma separated list of partitions to extract, leave empty for "
41               "extracting all partitions");
42 DEFINE_int32(cow_version,
43              0,
44              "VABC Cow version to use. Default is to use what's specified in "
45              "the OTA manifest");
46 DEFINE_string(vabc_compression_param,
47               "",
48               "Compression parameter for VABC. Default is use what's specified "
49               "in OTA package");
50 
51 namespace chromeos_update_engine {
52 
ProcessPartition(const chromeos_update_engine::DeltaArchiveManifest & manifest,const chromeos_update_engine::PartitionUpdate & partition,const char * image_dir)53 bool ProcessPartition(
54     const chromeos_update_engine::DeltaArchiveManifest& manifest,
55     const chromeos_update_engine::PartitionUpdate& partition,
56     const char* image_dir) {
57   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 
72   const auto& dap = manifest.dynamic_partition_metadata();
73 
74   android::snapshot::CowOptions options{
75       .block_size = static_cast<uint32_t>(manifest.block_size()),
76       .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())};
80   if (!FLAGS_vabc_compression_param.empty()) {
81     options.compression = FLAGS_vabc_compression_param;
82   }
83   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   }
88   auto cow_writer = android::snapshot::CreateCowWriter(
89       cow_version, options, std::move(output_fd));
90   TEST_AND_RETURN_FALSE(cow_writer);
91   TEST_AND_RETURN_FALSE(CowDryRun(nullptr,
92                                   target_img_fd,
93                                   partition.operations(),
94                                   partition.merge_operations(),
95                                   manifest.block_size(),
96                                   cow_writer.get(),
97                                   partition.new_partition_info().size(),
98                                   partition.old_partition_info().size(),
99                                   false));
100   TEST_AND_RETURN_FALSE(cow_writer->Finalize());
101   return true;
102 }
103 
104 }  // namespace chromeos_update_engine
105 
106 using chromeos_update_engine::MetadataParseResult;
107 using chromeos_update_engine::PayloadMetadata;
108 
main(int argc,char * argv[])109 int 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 
118   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 
164   size_t estimated_total_cow_size = 0;
165   size_t actual_total_cow_size = 0;
166 
167   for (const auto& partition : manifest.partitions()) {
168     if (partition.estimate_cow_size() == 0) {
169       continue;
170     }
171     if (!partitions.empty() &&
172         partitions.count(partition.partition_name()) == 0) {
173       continue;
174     }
175     LOG(INFO) << partition.partition_name();
176     if (!ProcessPartition(manifest, partition, images_dir)) {
177       return 6;
178     }
179     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;
193   }
194 
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";
201   return 0;
202 }
203