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