| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 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 |  | 
| Colin Cross | 238d904 | 2021-12-21 13:09:30 -0800 | [diff] [blame] | 17 | #include <fcntl.h> | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 18 | #include <stdio.h> | 
 | 19 | #include <string.h> | 
 | 20 |  | 
 | 21 | #include <cstdint> | 
 | 22 | #include <cstdio> | 
 | 23 | #include <memory> | 
 | 24 |  | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 25 | #include <sys/mman.h> | 
 | 26 | #include <sys/stat.h> | 
 | 27 |  | 
 | 28 | #include <base/files/file_path.h> | 
 | 29 | #include <libsnapshot/cow_writer.h> | 
| Kelvin Zhang | 901c7d5 | 2022-06-21 09:35:45 -0700 | [diff] [blame] | 30 | #include <gflags/gflags.h> | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 31 |  | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 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 |  | 
| Kelvin Zhang | 901c7d5 | 2022-06-21 09:35:45 -0700 | [diff] [blame] | 38 | DEFINE_string(partitions, | 
 | 39 |               "", | 
 | 40 |               "Comma separated list of partitions to extract, leave empty for " | 
 | 41 |               "extracting all partitions"); | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 42 |  | 
 | 43 | namespace chromeos_update_engine { | 
 | 44 |  | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 45 | bool ProcessPartition( | 
 | 46 |     const chromeos_update_engine::DeltaArchiveManifest& manifest, | 
 | 47 |     const chromeos_update_engine::PartitionUpdate& partition, | 
 | 48 |     const char* image_dir) { | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 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 |  | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 64 |   const auto& dap = manifest.dynamic_partition_metadata(); | 
 | 65 |  | 
 | 66 |   android::snapshot::CowOptions options{ | 
 | 67 |       .block_size = static_cast<uint32_t>(manifest.block_size()), | 
 | 68 |       .compression = dap.vabc_compression_param()}; | 
 | 69 |   auto cow_writer = android::snapshot::CreateCowWriter( | 
 | 70 |       dap.cow_version(), options, std::move(output_fd)); | 
 | 71 |   TEST_AND_RETURN_FALSE(cow_writer); | 
| Kelvin Zhang | e36e4a3 | 2021-08-27 13:48:35 -0700 | [diff] [blame] | 72 |   TEST_AND_RETURN_FALSE(CowDryRun(nullptr, | 
 | 73 |                                   target_img_fd, | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 74 |                                   partition.operations(), | 
 | 75 |                                   partition.merge_operations(), | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 76 |                                   manifest.block_size(), | 
 | 77 |                                   cow_writer.get(), | 
| Kelvin Zhang | e36e4a3 | 2021-08-27 13:48:35 -0700 | [diff] [blame] | 78 |                                   partition.new_partition_info().size(), | 
 | 79 |                                   false)); | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 80 |   TEST_AND_RETURN_FALSE(cow_writer->Finalize()); | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 81 |   return true; | 
 | 82 | } | 
 | 83 |  | 
 | 84 | }  // namespace chromeos_update_engine | 
 | 85 |  | 
 | 86 | using chromeos_update_engine::MetadataParseResult; | 
 | 87 | using chromeos_update_engine::PayloadMetadata; | 
 | 88 |  | 
| Kelvin Zhang | 901c7d5 | 2022-06-21 09:35:45 -0700 | [diff] [blame] | 89 | int main(int argc, char* argv[]) { | 
 | 90 |   gflags::SetUsageMessage( | 
 | 91 |       "A tool to extract device images from Android OTA packages"); | 
 | 92 |   gflags::ParseCommandLineFlags(&argc, &argv, true); | 
 | 93 |   auto tokens = android::base::Tokenize(FLAGS_partitions, ","); | 
 | 94 |   const std::set<std::string> partitions( | 
 | 95 |       std::make_move_iterator(tokens.begin()), | 
 | 96 |       std::make_move_iterator(tokens.end())); | 
 | 97 |  | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 98 |   if (argc != 3) { | 
 | 99 |     printf("Usage: %s <payload.bin> <extracted target_file>\n", argv[0]); | 
 | 100 |     return -1; | 
 | 101 |   } | 
 | 102 |   const char* payload_path = argv[1]; | 
 | 103 |   const char* images_dir = argv[2]; | 
 | 104 |   int payload_fd = open(payload_path, O_RDONLY); | 
 | 105 |   if (payload_fd < 0) { | 
 | 106 |     PLOG(ERROR) << "Failed to open payload file:"; | 
 | 107 |     return 1; | 
 | 108 |   } | 
 | 109 |   chromeos_update_engine::ScopedFdCloser closer{&payload_fd}; | 
 | 110 |   auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd); | 
 | 111 |   if (payload_size <= 0) { | 
 | 112 |     PLOG(ERROR) | 
 | 113 |         << "Couldn't determine size of payload file, or payload file is empty"; | 
 | 114 |     return 2; | 
 | 115 |   } | 
 | 116 |  | 
 | 117 |   PayloadMetadata payload_metadata; | 
 | 118 |   auto payload = static_cast<unsigned char*>( | 
 | 119 |       mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0)); | 
 | 120 |  | 
 | 121 |   // C++ dark magic to ensure that |payload| is properly deallocated once the | 
 | 122 |   // program exits. | 
 | 123 |   auto munmap_deleter = [payload_size](auto payload) { | 
 | 124 |     munmap(payload, payload_size); | 
 | 125 |   }; | 
 | 126 |   std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{ | 
 | 127 |       payload, munmap_deleter}; | 
 | 128 |  | 
 | 129 |   if (payload == nullptr) { | 
 | 130 |     PLOG(ERROR) << "Failed to mmap() payload file"; | 
 | 131 |     return 3; | 
 | 132 |   } | 
 | 133 |   if (payload_metadata.ParsePayloadHeader(payload, payload_size, nullptr) != | 
 | 134 |       chromeos_update_engine::MetadataParseResult::kSuccess) { | 
 | 135 |     LOG(ERROR) << "Payload header parse failed!"; | 
 | 136 |     return 4; | 
 | 137 |   } | 
 | 138 |   chromeos_update_engine::DeltaArchiveManifest manifest; | 
 | 139 |   if (!payload_metadata.GetManifest(payload, payload_size, &manifest)) { | 
 | 140 |     LOG(ERROR) << "Failed to parse manifest!"; | 
 | 141 |     return 5; | 
 | 142 |   } | 
 | 143 |  | 
| Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 144 |   size_t estimated_total_cow_size = 0; | 
 | 145 |   size_t actual_total_cow_size = 0; | 
 | 146 |  | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 147 |   for (const auto& partition : manifest.partitions()) { | 
| Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 148 |     if (partition.estimate_cow_size() == 0) { | 
 | 149 |       continue; | 
 | 150 |     } | 
| Kelvin Zhang | 901c7d5 | 2022-06-21 09:35:45 -0700 | [diff] [blame] | 151 |     if (!partitions.empty() && | 
 | 152 |         partitions.count(partition.partition_name()) == 0) { | 
 | 153 |       continue; | 
 | 154 |     } | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 155 |     LOG(INFO) << partition.partition_name(); | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 156 |     if (!ProcessPartition(manifest, partition, images_dir)) { | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 157 |       return 6; | 
 | 158 |     } | 
| Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 159 |     base::FilePath img_dir{images_dir}; | 
 | 160 |     const auto output_cow = | 
 | 161 |         img_dir.Append(partition.partition_name() + ".cow").value(); | 
 | 162 |     const auto actual_cow_size = | 
 | 163 |         chromeos_update_engine::utils::FileSize(output_cow); | 
 | 164 |     LOG(INFO) << partition.partition_name() | 
 | 165 |               << ": estimated COW size is: " << partition.estimate_cow_size() | 
 | 166 |               << ", actual COW size is: " << actual_cow_size | 
 | 167 |               << ", estimated COW size is " | 
 | 168 |               << (actual_cow_size - partition.estimate_cow_size()) * 100.0f / | 
 | 169 |                      actual_cow_size | 
 | 170 |               << "% smaller"; | 
 | 171 |     estimated_total_cow_size += partition.estimate_cow_size(); | 
 | 172 |     actual_total_cow_size += actual_cow_size; | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 173 |   } | 
| Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 174 |  | 
 | 175 |   LOG(INFO) << "Total estimated COW size is: " << estimated_total_cow_size | 
 | 176 |             << ", Total actual COW size is: " << actual_total_cow_size | 
 | 177 |             << ", estimated COW size is " | 
 | 178 |             << (actual_total_cow_size - estimated_total_cow_size) * 100.0f / | 
 | 179 |                    actual_total_cow_size | 
 | 180 |             << "% smaller"; | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 181 |   return 0; | 
 | 182 | } |