xref: /aosp_15_r20/system/update_engine/aosp/ota_extractor.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2022 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #include <array>
18*5a923131SAndroid Build Coastguard Worker #include <cstdint>
19*5a923131SAndroid Build Coastguard Worker #include <cstdio>
20*5a923131SAndroid Build Coastguard Worker #include <future>
21*5a923131SAndroid Build Coastguard Worker #include <iterator>
22*5a923131SAndroid Build Coastguard Worker #include <memory>
23*5a923131SAndroid Build Coastguard Worker 
24*5a923131SAndroid Build Coastguard Worker #include <fcntl.h>
25*5a923131SAndroid Build Coastguard Worker #include <sys/mman.h>
26*5a923131SAndroid Build Coastguard Worker #include <sys/stat.h>
27*5a923131SAndroid Build Coastguard Worker 
28*5a923131SAndroid Build Coastguard Worker #include <android-base/strings.h>
29*5a923131SAndroid Build Coastguard Worker #include <base/files/file_path.h>
30*5a923131SAndroid Build Coastguard Worker #include <gflags/gflags.h>
31*5a923131SAndroid Build Coastguard Worker #include <unistd.h>
32*5a923131SAndroid Build Coastguard Worker #include <xz.h>
33*5a923131SAndroid Build Coastguard Worker 
34*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/utils.h"
35*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/hash_calculator.h"
36*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/file_descriptor.h"
37*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/file_descriptor_utils.h"
38*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/install_operation_executor.h"
39*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/payload_metadata.h"
40*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/verity_writer_android.h"
41*5a923131SAndroid Build Coastguard Worker #include "update_engine/update_metadata.pb.h"
42*5a923131SAndroid Build Coastguard Worker 
43*5a923131SAndroid Build Coastguard Worker DEFINE_string(payload, "", "Path to payload.bin");
44*5a923131SAndroid Build Coastguard Worker DEFINE_string(
45*5a923131SAndroid Build Coastguard Worker     input_dir,
46*5a923131SAndroid Build Coastguard Worker     "",
47*5a923131SAndroid Build Coastguard Worker     "Directory to read input images. Only required for incremental OTAs");
48*5a923131SAndroid Build Coastguard Worker DEFINE_string(output_dir, "", "Directory to put output images");
49*5a923131SAndroid Build Coastguard Worker DEFINE_int64(payload_offset,
50*5a923131SAndroid Build Coastguard Worker              0,
51*5a923131SAndroid Build Coastguard Worker              "Offset to start of payload.bin. Useful if payload path actually "
52*5a923131SAndroid Build Coastguard Worker              "points to a .zip file containing payload.bin");
53*5a923131SAndroid Build Coastguard Worker DEFINE_string(partitions,
54*5a923131SAndroid Build Coastguard Worker               "",
55*5a923131SAndroid Build Coastguard Worker               "Comma separated list of partitions to extract, leave empty for "
56*5a923131SAndroid Build Coastguard Worker               "extracting all partitions");
57*5a923131SAndroid Build Coastguard Worker DEFINE_bool(single_thread, false, "Limit extraction to a single thread");
58*5a923131SAndroid Build Coastguard Worker 
59*5a923131SAndroid Build Coastguard Worker using chromeos_update_engine::DeltaArchiveManifest;
60*5a923131SAndroid Build Coastguard Worker using chromeos_update_engine::PayloadMetadata;
61*5a923131SAndroid Build Coastguard Worker 
62*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
63*5a923131SAndroid Build Coastguard Worker 
WriteVerity(const PartitionUpdate & partition,FileDescriptorPtr fd,const size_t block_size)64*5a923131SAndroid Build Coastguard Worker void WriteVerity(const PartitionUpdate& partition,
65*5a923131SAndroid Build Coastguard Worker                  FileDescriptorPtr fd,
66*5a923131SAndroid Build Coastguard Worker                  const size_t block_size) {
67*5a923131SAndroid Build Coastguard Worker   // 512KB buffer, arbitrary value. Larger buffers may improve performance.
68*5a923131SAndroid Build Coastguard Worker   static constexpr size_t BUFFER_SIZE = 1024 * 512;
69*5a923131SAndroid Build Coastguard Worker   if (partition.hash_tree_extent().num_blocks() == 0 &&
70*5a923131SAndroid Build Coastguard Worker       partition.fec_extent().num_blocks() == 0) {
71*5a923131SAndroid Build Coastguard Worker     return;
72*5a923131SAndroid Build Coastguard Worker   }
73*5a923131SAndroid Build Coastguard Worker   InstallPlan::Partition install_part;
74*5a923131SAndroid Build Coastguard Worker   install_part.block_size = block_size;
75*5a923131SAndroid Build Coastguard Worker   CHECK(install_part.ParseVerityConfig(partition));
76*5a923131SAndroid Build Coastguard Worker   VerityWriterAndroid writer;
77*5a923131SAndroid Build Coastguard Worker   CHECK(writer.Init(install_part));
78*5a923131SAndroid Build Coastguard Worker   std::array<uint8_t, BUFFER_SIZE> buffer;
79*5a923131SAndroid Build Coastguard Worker   const auto data_size =
80*5a923131SAndroid Build Coastguard Worker       install_part.hash_tree_data_offset + install_part.hash_tree_data_size;
81*5a923131SAndroid Build Coastguard Worker   size_t offset = 0;
82*5a923131SAndroid Build Coastguard Worker   while (offset < data_size) {
83*5a923131SAndroid Build Coastguard Worker     const auto bytes_to_read =
84*5a923131SAndroid Build Coastguard Worker         static_cast<ssize_t>(std::min(BUFFER_SIZE, data_size - offset));
85*5a923131SAndroid Build Coastguard Worker     ssize_t bytes_read;
86*5a923131SAndroid Build Coastguard Worker     CHECK(
87*5a923131SAndroid Build Coastguard Worker         utils::ReadAll(fd, buffer.data(), bytes_to_read, offset, &bytes_read));
88*5a923131SAndroid Build Coastguard Worker     CHECK_EQ(bytes_read, bytes_to_read)
89*5a923131SAndroid Build Coastguard Worker         << " Failed to read at offset " << offset << " "
90*5a923131SAndroid Build Coastguard Worker         << android::base::ErrnoNumberAsString(errno);
91*5a923131SAndroid Build Coastguard Worker     writer.Update(offset, buffer.data(), bytes_read);
92*5a923131SAndroid Build Coastguard Worker     offset += bytes_read;
93*5a923131SAndroid Build Coastguard Worker   }
94*5a923131SAndroid Build Coastguard Worker   CHECK(writer.Finalize(fd.get(), fd.get()));
95*5a923131SAndroid Build Coastguard Worker   return;
96*5a923131SAndroid Build Coastguard Worker }
97*5a923131SAndroid Build Coastguard Worker 
ExtractImageFromPartition(const DeltaArchiveManifest & manifest,const PartitionUpdate & partition,const size_t data_begin,int payload_fd,std::string_view input_dir,std::string_view output_dir)98*5a923131SAndroid Build Coastguard Worker bool ExtractImageFromPartition(const DeltaArchiveManifest& manifest,
99*5a923131SAndroid Build Coastguard Worker                                const PartitionUpdate& partition,
100*5a923131SAndroid Build Coastguard Worker                                const size_t data_begin,
101*5a923131SAndroid Build Coastguard Worker                                int payload_fd,
102*5a923131SAndroid Build Coastguard Worker                                std::string_view input_dir,
103*5a923131SAndroid Build Coastguard Worker                                std::string_view output_dir) {
104*5a923131SAndroid Build Coastguard Worker   InstallOperationExecutor executor(manifest.block_size());
105*5a923131SAndroid Build Coastguard Worker   const base::FilePath output_dir_path(
106*5a923131SAndroid Build Coastguard Worker       base::StringPiece(output_dir.data(), output_dir.size()));
107*5a923131SAndroid Build Coastguard Worker   const base::FilePath input_dir_path(
108*5a923131SAndroid Build Coastguard Worker       base::StringPiece(input_dir.data(), input_dir.size()));
109*5a923131SAndroid Build Coastguard Worker   std::vector<unsigned char> blob;
110*5a923131SAndroid Build Coastguard Worker 
111*5a923131SAndroid Build Coastguard Worker   LOG(INFO) << "Extracting partition " << partition.partition_name()
112*5a923131SAndroid Build Coastguard Worker             << " size: " << partition.new_partition_info().size();
113*5a923131SAndroid Build Coastguard Worker   const auto output_path =
114*5a923131SAndroid Build Coastguard Worker       output_dir_path.Append(partition.partition_name() + ".img").value();
115*5a923131SAndroid Build Coastguard Worker   auto out_fd =
116*5a923131SAndroid Build Coastguard Worker       std::make_shared<chromeos_update_engine::EintrSafeFileDescriptor>();
117*5a923131SAndroid Build Coastguard Worker   TEST_AND_RETURN_FALSE_ERRNO(
118*5a923131SAndroid Build Coastguard Worker       out_fd->Open(output_path.c_str(), O_RDWR | O_CREAT, 0644));
119*5a923131SAndroid Build Coastguard Worker   auto in_fd =
120*5a923131SAndroid Build Coastguard Worker       std::make_shared<chromeos_update_engine::EintrSafeFileDescriptor>();
121*5a923131SAndroid Build Coastguard Worker   if (partition.has_old_partition_info()) {
122*5a923131SAndroid Build Coastguard Worker     const auto input_path =
123*5a923131SAndroid Build Coastguard Worker         input_dir_path.Append(partition.partition_name() + ".img").value();
124*5a923131SAndroid Build Coastguard Worker     LOG(INFO) << "Incremental OTA detected for partition "
125*5a923131SAndroid Build Coastguard Worker               << partition.partition_name() << " opening source image "
126*5a923131SAndroid Build Coastguard Worker               << input_path;
127*5a923131SAndroid Build Coastguard Worker     CHECK(in_fd->Open(input_path.c_str(), O_RDONLY))
128*5a923131SAndroid Build Coastguard Worker         << " failed to open " << input_path;
129*5a923131SAndroid Build Coastguard Worker   }
130*5a923131SAndroid Build Coastguard Worker 
131*5a923131SAndroid Build Coastguard Worker   for (const auto& op : partition.operations()) {
132*5a923131SAndroid Build Coastguard Worker     if (op.has_src_sha256_hash()) {
133*5a923131SAndroid Build Coastguard Worker       brillo::Blob actual_hash;
134*5a923131SAndroid Build Coastguard Worker       TEST_AND_RETURN_FALSE(fd_utils::ReadAndHashExtents(
135*5a923131SAndroid Build Coastguard Worker           in_fd, op.src_extents(), manifest.block_size(), &actual_hash));
136*5a923131SAndroid Build Coastguard Worker       CHECK_EQ(HexEncode(ToStringView(actual_hash)),
137*5a923131SAndroid Build Coastguard Worker                HexEncode(op.src_sha256_hash()))
138*5a923131SAndroid Build Coastguard Worker           << ", failed partition: " << partition.partition_name();
139*5a923131SAndroid Build Coastguard Worker     }
140*5a923131SAndroid Build Coastguard Worker 
141*5a923131SAndroid Build Coastguard Worker     blob.resize(op.data_length());
142*5a923131SAndroid Build Coastguard Worker     const auto op_data_offset = data_begin + op.data_offset();
143*5a923131SAndroid Build Coastguard Worker     ssize_t bytes_read = 0;
144*5a923131SAndroid Build Coastguard Worker     TEST_AND_RETURN_FALSE(utils::PReadAll(
145*5a923131SAndroid Build Coastguard Worker         payload_fd, blob.data(), blob.size(), op_data_offset, &bytes_read));
146*5a923131SAndroid Build Coastguard Worker     if (op.has_data_sha256_hash()) {
147*5a923131SAndroid Build Coastguard Worker       brillo::Blob actual_hash;
148*5a923131SAndroid Build Coastguard Worker       TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfData(blob, &actual_hash));
149*5a923131SAndroid Build Coastguard Worker       CHECK_EQ(HexEncode(ToStringView(actual_hash)),
150*5a923131SAndroid Build Coastguard Worker                HexEncode(op.data_sha256_hash()))
151*5a923131SAndroid Build Coastguard Worker           << ", failed partition: " << partition.partition_name();
152*5a923131SAndroid Build Coastguard Worker     }
153*5a923131SAndroid Build Coastguard Worker     auto direct_writer = std::make_unique<DirectExtentWriter>(out_fd);
154*5a923131SAndroid Build Coastguard Worker     if (op.type() == InstallOperation::ZERO) {
155*5a923131SAndroid Build Coastguard Worker       TEST_AND_RETURN_FALSE(
156*5a923131SAndroid Build Coastguard Worker           executor.ExecuteZeroOrDiscardOperation(op, std::move(direct_writer)));
157*5a923131SAndroid Build Coastguard Worker     } else if (op.type() == InstallOperation::REPLACE ||
158*5a923131SAndroid Build Coastguard Worker                op.type() == InstallOperation::REPLACE_BZ ||
159*5a923131SAndroid Build Coastguard Worker                op.type() == InstallOperation::REPLACE_XZ) {
160*5a923131SAndroid Build Coastguard Worker       TEST_AND_RETURN_FALSE(executor.ExecuteReplaceOperation(
161*5a923131SAndroid Build Coastguard Worker           op, std::move(direct_writer), blob.data()));
162*5a923131SAndroid Build Coastguard Worker     } else if (op.type() == InstallOperation::SOURCE_COPY) {
163*5a923131SAndroid Build Coastguard Worker       CHECK(in_fd->IsOpen())
164*5a923131SAndroid Build Coastguard Worker           << ", failed partition: " << partition.partition_name();
165*5a923131SAndroid Build Coastguard Worker       TEST_AND_RETURN_FALSE(executor.ExecuteSourceCopyOperation(
166*5a923131SAndroid Build Coastguard Worker           op, std::move(direct_writer), in_fd));
167*5a923131SAndroid Build Coastguard Worker     } else {
168*5a923131SAndroid Build Coastguard Worker       CHECK(in_fd->IsOpen())
169*5a923131SAndroid Build Coastguard Worker           << ", failed partition: " << partition.partition_name();
170*5a923131SAndroid Build Coastguard Worker       TEST_AND_RETURN_FALSE(executor.ExecuteDiffOperation(
171*5a923131SAndroid Build Coastguard Worker           op, std::move(direct_writer), in_fd, blob.data(), blob.size()));
172*5a923131SAndroid Build Coastguard Worker     }
173*5a923131SAndroid Build Coastguard Worker   }
174*5a923131SAndroid Build Coastguard Worker   WriteVerity(partition, out_fd, manifest.block_size());
175*5a923131SAndroid Build Coastguard Worker   int err =
176*5a923131SAndroid Build Coastguard Worker       truncate64(output_path.c_str(), partition.new_partition_info().size());
177*5a923131SAndroid Build Coastguard Worker   if (err) {
178*5a923131SAndroid Build Coastguard Worker     PLOG(ERROR) << "Failed to truncate " << output_path << " to "
179*5a923131SAndroid Build Coastguard Worker                 << partition.new_partition_info().size();
180*5a923131SAndroid Build Coastguard Worker   }
181*5a923131SAndroid Build Coastguard Worker   brillo::Blob actual_hash;
182*5a923131SAndroid Build Coastguard Worker   TEST_AND_RETURN_FALSE(
183*5a923131SAndroid Build Coastguard Worker       HashCalculator::RawHashOfFile(output_path, &actual_hash));
184*5a923131SAndroid Build Coastguard Worker   CHECK_EQ(HexEncode(ToStringView(actual_hash)),
185*5a923131SAndroid Build Coastguard Worker            HexEncode(partition.new_partition_info().hash()))
186*5a923131SAndroid Build Coastguard Worker       << " Partition " << partition.partition_name()
187*5a923131SAndroid Build Coastguard Worker       << " hash mismatches. Either the source image or OTA package is "
188*5a923131SAndroid Build Coastguard Worker          "corrupted.";
189*5a923131SAndroid Build Coastguard Worker 
190*5a923131SAndroid Build Coastguard Worker   LOG(INFO) << "Extracted partition " << partition.partition_name();
191*5a923131SAndroid Build Coastguard Worker 
192*5a923131SAndroid Build Coastguard Worker   return true;
193*5a923131SAndroid Build Coastguard Worker }
194*5a923131SAndroid Build Coastguard Worker 
ExtractImagesFromOTA(const DeltaArchiveManifest & manifest,const PayloadMetadata & metadata,int payload_fd,size_t payload_offset,std::string_view input_dir,std::string_view output_dir,const std::set<std::string> & partitions)195*5a923131SAndroid Build Coastguard Worker bool ExtractImagesFromOTA(const DeltaArchiveManifest& manifest,
196*5a923131SAndroid Build Coastguard Worker                           const PayloadMetadata& metadata,
197*5a923131SAndroid Build Coastguard Worker                           int payload_fd,
198*5a923131SAndroid Build Coastguard Worker                           size_t payload_offset,
199*5a923131SAndroid Build Coastguard Worker                           std::string_view input_dir,
200*5a923131SAndroid Build Coastguard Worker                           std::string_view output_dir,
201*5a923131SAndroid Build Coastguard Worker                           const std::set<std::string>& partitions) {
202*5a923131SAndroid Build Coastguard Worker   const size_t data_begin = metadata.GetMetadataSize() +
203*5a923131SAndroid Build Coastguard Worker                             metadata.GetMetadataSignatureSize() +
204*5a923131SAndroid Build Coastguard Worker                             payload_offset;
205*5a923131SAndroid Build Coastguard Worker   bool ret = true;
206*5a923131SAndroid Build Coastguard Worker 
207*5a923131SAndroid Build Coastguard Worker   if (FLAGS_single_thread) {
208*5a923131SAndroid Build Coastguard Worker     for (const auto& partition : manifest.partitions()) {
209*5a923131SAndroid Build Coastguard Worker       if (!partitions.empty() &&
210*5a923131SAndroid Build Coastguard Worker           partitions.count(partition.partition_name()) == 0) {
211*5a923131SAndroid Build Coastguard Worker         continue;
212*5a923131SAndroid Build Coastguard Worker       }
213*5a923131SAndroid Build Coastguard Worker       if (!ExtractImageFromPartition(manifest,
214*5a923131SAndroid Build Coastguard Worker                                      partition,
215*5a923131SAndroid Build Coastguard Worker                                      data_begin,
216*5a923131SAndroid Build Coastguard Worker                                      payload_fd,
217*5a923131SAndroid Build Coastguard Worker                                      input_dir,
218*5a923131SAndroid Build Coastguard Worker                                      output_dir)) {
219*5a923131SAndroid Build Coastguard Worker         ret = false;
220*5a923131SAndroid Build Coastguard Worker         LOG(ERROR) << "Extraction of partition " << partition.partition_name()
221*5a923131SAndroid Build Coastguard Worker                    << " failed";
222*5a923131SAndroid Build Coastguard Worker         break;
223*5a923131SAndroid Build Coastguard Worker       }
224*5a923131SAndroid Build Coastguard Worker     }
225*5a923131SAndroid Build Coastguard Worker   } else {
226*5a923131SAndroid Build Coastguard Worker     std::vector<std::pair<std::future<bool>, std::string>> futures;
227*5a923131SAndroid Build Coastguard Worker     for (const auto& partition : manifest.partitions()) {
228*5a923131SAndroid Build Coastguard Worker       if (!partitions.empty() &&
229*5a923131SAndroid Build Coastguard Worker           partitions.count(partition.partition_name()) == 0) {
230*5a923131SAndroid Build Coastguard Worker         continue;
231*5a923131SAndroid Build Coastguard Worker       }
232*5a923131SAndroid Build Coastguard Worker       futures.push_back(std::make_pair(std::async(std::launch::async,
233*5a923131SAndroid Build Coastguard Worker                                                   ExtractImageFromPartition,
234*5a923131SAndroid Build Coastguard Worker                                                   manifest,
235*5a923131SAndroid Build Coastguard Worker                                                   partition,
236*5a923131SAndroid Build Coastguard Worker                                                   data_begin,
237*5a923131SAndroid Build Coastguard Worker                                                   payload_fd,
238*5a923131SAndroid Build Coastguard Worker                                                   input_dir,
239*5a923131SAndroid Build Coastguard Worker                                                   output_dir),
240*5a923131SAndroid Build Coastguard Worker                                        partition.partition_name()));
241*5a923131SAndroid Build Coastguard Worker     }
242*5a923131SAndroid Build Coastguard Worker     for (auto& future : futures) {
243*5a923131SAndroid Build Coastguard Worker       if (!future.first.get()) {
244*5a923131SAndroid Build Coastguard Worker         ret = false;
245*5a923131SAndroid Build Coastguard Worker         LOG(ERROR) << "Extraction of partition " << future.second << " failed";
246*5a923131SAndroid Build Coastguard Worker       }
247*5a923131SAndroid Build Coastguard Worker     }
248*5a923131SAndroid Build Coastguard Worker   }
249*5a923131SAndroid Build Coastguard Worker   return ret;
250*5a923131SAndroid Build Coastguard Worker }
251*5a923131SAndroid Build Coastguard Worker 
252*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
253*5a923131SAndroid Build Coastguard Worker 
254*5a923131SAndroid Build Coastguard Worker namespace {
255*5a923131SAndroid Build Coastguard Worker 
IsIncrementalOTA(const DeltaArchiveManifest & manifest)256*5a923131SAndroid Build Coastguard Worker bool IsIncrementalOTA(const DeltaArchiveManifest& manifest) {
257*5a923131SAndroid Build Coastguard Worker   for (const auto& part : manifest.partitions()) {
258*5a923131SAndroid Build Coastguard Worker     if (part.has_old_partition_info()) {
259*5a923131SAndroid Build Coastguard Worker       return true;
260*5a923131SAndroid Build Coastguard Worker     }
261*5a923131SAndroid Build Coastguard Worker   }
262*5a923131SAndroid Build Coastguard Worker   return false;
263*5a923131SAndroid Build Coastguard Worker }
264*5a923131SAndroid Build Coastguard Worker 
265*5a923131SAndroid Build Coastguard Worker }  // namespace
266*5a923131SAndroid Build Coastguard Worker 
main(int argc,char * argv[])267*5a923131SAndroid Build Coastguard Worker int main(int argc, char* argv[]) {
268*5a923131SAndroid Build Coastguard Worker   gflags::SetUsageMessage(
269*5a923131SAndroid Build Coastguard Worker       "A tool to extract device images from Android OTA packages");
270*5a923131SAndroid Build Coastguard Worker   gflags::ParseCommandLineFlags(&argc, &argv, true);
271*5a923131SAndroid Build Coastguard Worker   xz_crc32_init();
272*5a923131SAndroid Build Coastguard Worker   auto tokens = android::base::Tokenize(FLAGS_partitions, ",");
273*5a923131SAndroid Build Coastguard Worker   const std::set<std::string> partitions(
274*5a923131SAndroid Build Coastguard Worker       std::make_move_iterator(tokens.begin()),
275*5a923131SAndroid Build Coastguard Worker       std::make_move_iterator(tokens.end()));
276*5a923131SAndroid Build Coastguard Worker   if (FLAGS_payload.empty()) {
277*5a923131SAndroid Build Coastguard Worker     LOG(ERROR) << "--payload <payload path> is required";
278*5a923131SAndroid Build Coastguard Worker     return 1;
279*5a923131SAndroid Build Coastguard Worker   }
280*5a923131SAndroid Build Coastguard Worker   if (!partitions.empty()) {
281*5a923131SAndroid Build Coastguard Worker     LOG(INFO) << "Extracting " << android::base::Join(partitions, ", ");
282*5a923131SAndroid Build Coastguard Worker   }
283*5a923131SAndroid Build Coastguard Worker   int payload_fd = open(FLAGS_payload.c_str(), O_RDONLY | O_CLOEXEC);
284*5a923131SAndroid Build Coastguard Worker   if (payload_fd < 0) {
285*5a923131SAndroid Build Coastguard Worker     PLOG(ERROR) << "Failed to open payload file";
286*5a923131SAndroid Build Coastguard Worker     return 1;
287*5a923131SAndroid Build Coastguard Worker   }
288*5a923131SAndroid Build Coastguard Worker   chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
289*5a923131SAndroid Build Coastguard Worker   auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
290*5a923131SAndroid Build Coastguard Worker   if (payload_size <= 0) {
291*5a923131SAndroid Build Coastguard Worker     PLOG(ERROR)
292*5a923131SAndroid Build Coastguard Worker         << "Couldn't determine size of payload file, or payload file is empty";
293*5a923131SAndroid Build Coastguard Worker     return 1;
294*5a923131SAndroid Build Coastguard Worker   }
295*5a923131SAndroid Build Coastguard Worker 
296*5a923131SAndroid Build Coastguard Worker   PayloadMetadata payload_metadata;
297*5a923131SAndroid Build Coastguard Worker   auto payload = static_cast<unsigned char*>(
298*5a923131SAndroid Build Coastguard Worker       mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
299*5a923131SAndroid Build Coastguard Worker 
300*5a923131SAndroid Build Coastguard Worker   if (payload == MAP_FAILED) {
301*5a923131SAndroid Build Coastguard Worker     PLOG(ERROR) << "Failed to mmap() payload file";
302*5a923131SAndroid Build Coastguard Worker     return 1;
303*5a923131SAndroid Build Coastguard Worker   }
304*5a923131SAndroid Build Coastguard Worker 
305*5a923131SAndroid Build Coastguard Worker   auto munmap_deleter = [payload_size](auto payload) {
306*5a923131SAndroid Build Coastguard Worker     munmap(payload, payload_size);
307*5a923131SAndroid Build Coastguard Worker   };
308*5a923131SAndroid Build Coastguard Worker   std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
309*5a923131SAndroid Build Coastguard Worker       payload, munmap_deleter};
310*5a923131SAndroid Build Coastguard Worker   if (payload_metadata.ParsePayloadHeader(payload + FLAGS_payload_offset,
311*5a923131SAndroid Build Coastguard Worker                                           payload_size - FLAGS_payload_offset,
312*5a923131SAndroid Build Coastguard Worker                                           nullptr) !=
313*5a923131SAndroid Build Coastguard Worker       chromeos_update_engine::MetadataParseResult::kSuccess) {
314*5a923131SAndroid Build Coastguard Worker     LOG(ERROR) << "Payload header parse failed!";
315*5a923131SAndroid Build Coastguard Worker     return 1;
316*5a923131SAndroid Build Coastguard Worker   }
317*5a923131SAndroid Build Coastguard Worker   DeltaArchiveManifest manifest;
318*5a923131SAndroid Build Coastguard Worker   if (!payload_metadata.GetManifest(payload + FLAGS_payload_offset,
319*5a923131SAndroid Build Coastguard Worker                                     payload_size - FLAGS_payload_offset,
320*5a923131SAndroid Build Coastguard Worker                                     &manifest)) {
321*5a923131SAndroid Build Coastguard Worker     LOG(ERROR) << "Failed to parse manifest!";
322*5a923131SAndroid Build Coastguard Worker     return 1;
323*5a923131SAndroid Build Coastguard Worker   }
324*5a923131SAndroid Build Coastguard Worker   if (IsIncrementalOTA(manifest) && FLAGS_input_dir.empty()) {
325*5a923131SAndroid Build Coastguard Worker     LOG(ERROR) << FLAGS_payload
326*5a923131SAndroid Build Coastguard Worker                << " is an incremental OTA, --input_dir parameter is required.";
327*5a923131SAndroid Build Coastguard Worker     return 1;
328*5a923131SAndroid Build Coastguard Worker   }
329*5a923131SAndroid Build Coastguard Worker   return !ExtractImagesFromOTA(manifest,
330*5a923131SAndroid Build Coastguard Worker                                payload_metadata,
331*5a923131SAndroid Build Coastguard Worker                                payload_fd,
332*5a923131SAndroid Build Coastguard Worker                                FLAGS_payload_offset,
333*5a923131SAndroid Build Coastguard Worker                                FLAGS_input_dir,
334*5a923131SAndroid Build Coastguard Worker                                FLAGS_output_dir,
335*5a923131SAndroid Build Coastguard Worker                                partitions);
336*5a923131SAndroid Build Coastguard Worker }
337