| 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 | e7ca369 | 2023-12-20 13:54:27 -0800 | [diff] [blame] | 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"); | 
| Kelvin Zhang | 83bb5d5 | 2023-11-03 09:57:15 -0700 | [diff] [blame] | 46 | DEFINE_string(vabc_compression_param, | 
 | 47 |               "", | 
 | 48 |               "Compression parameter for VABC. Default is use what's specified " | 
 | 49 |               "in OTA package"); | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 50 |  | 
 | 51 | namespace chromeos_update_engine { | 
 | 52 |  | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 53 | bool ProcessPartition( | 
 | 54 |     const chromeos_update_engine::DeltaArchiveManifest& manifest, | 
 | 55 |     const chromeos_update_engine::PartitionUpdate& partition, | 
 | 56 |     const char* image_dir) { | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 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 |  | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 72 |   const auto& dap = manifest.dynamic_partition_metadata(); | 
 | 73 |  | 
 | 74 |   android::snapshot::CowOptions options{ | 
 | 75 |       .block_size = static_cast<uint32_t>(manifest.block_size()), | 
| Kelvin Zhang | e7ca369 | 2023-12-20 13:54:27 -0800 | [diff] [blame] | 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())}; | 
| Kelvin Zhang | 83bb5d5 | 2023-11-03 09:57:15 -0700 | [diff] [blame] | 80 |   if (!FLAGS_vabc_compression_param.empty()) { | 
 | 81 |     options.compression = FLAGS_vabc_compression_param; | 
 | 82 |   } | 
| Kelvin Zhang | e7ca369 | 2023-12-20 13:54:27 -0800 | [diff] [blame] | 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 |   } | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 88 |   auto cow_writer = android::snapshot::CreateCowWriter( | 
| Kelvin Zhang | e7ca369 | 2023-12-20 13:54:27 -0800 | [diff] [blame] | 89 |       cow_version, options, std::move(output_fd)); | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 90 |   TEST_AND_RETURN_FALSE(cow_writer); | 
| Kelvin Zhang | e36e4a3 | 2021-08-27 13:48:35 -0700 | [diff] [blame] | 91 |   TEST_AND_RETURN_FALSE(CowDryRun(nullptr, | 
 | 92 |                                   target_img_fd, | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 93 |                                   partition.operations(), | 
 | 94 |                                   partition.merge_operations(), | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 95 |                                   manifest.block_size(), | 
 | 96 |                                   cow_writer.get(), | 
| Kelvin Zhang | e36e4a3 | 2021-08-27 13:48:35 -0700 | [diff] [blame] | 97 |                                   partition.new_partition_info().size(), | 
| Daniel Zheng | 1ab6415 | 2024-04-03 14:25:40 -0700 | [diff] [blame] | 98 |                                   partition.old_partition_info().size(), | 
| Kelvin Zhang | e36e4a3 | 2021-08-27 13:48:35 -0700 | [diff] [blame] | 99 |                                   false)); | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 100 |   TEST_AND_RETURN_FALSE(cow_writer->Finalize()); | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 101 |   return true; | 
 | 102 | } | 
 | 103 |  | 
 | 104 | }  // namespace chromeos_update_engine | 
 | 105 |  | 
 | 106 | using chromeos_update_engine::MetadataParseResult; | 
 | 107 | using chromeos_update_engine::PayloadMetadata; | 
 | 108 |  | 
| Kelvin Zhang | 901c7d5 | 2022-06-21 09:35:45 -0700 | [diff] [blame] | 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 |  | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 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 |  | 
| Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 164 |   size_t estimated_total_cow_size = 0; | 
 | 165 |   size_t actual_total_cow_size = 0; | 
 | 166 |  | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 167 |   for (const auto& partition : manifest.partitions()) { | 
| Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 168 |     if (partition.estimate_cow_size() == 0) { | 
 | 169 |       continue; | 
 | 170 |     } | 
| Kelvin Zhang | 901c7d5 | 2022-06-21 09:35:45 -0700 | [diff] [blame] | 171 |     if (!partitions.empty() && | 
 | 172 |         partitions.count(partition.partition_name()) == 0) { | 
 | 173 |       continue; | 
 | 174 |     } | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 175 |     LOG(INFO) << partition.partition_name(); | 
| David Anderson | 8144b25 | 2023-05-09 22:16:40 -0700 | [diff] [blame] | 176 |     if (!ProcessPartition(manifest, partition, images_dir)) { | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 177 |       return 6; | 
 | 178 |     } | 
| Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 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; | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 193 |   } | 
| Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 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"; | 
| Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 201 |   return 0; | 
 | 202 | } |