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 | |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <cstdio> |
| 22 | #include <memory> |
| 23 | |
| 24 | #include <sys/fcntl.h> |
| 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 | |
| 31 | #include "update_engine/common/cow_operation_convert.h" |
| 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 | using android::snapshot::CowWriter; |
| 39 | |
| 40 | namespace chromeos_update_engine { |
| 41 | |
| 42 | bool ProcessPartition(const chromeos_update_engine::PartitionUpdate& partition, |
| 43 | const char* image_dir, |
| 44 | size_t block_size) { |
| 45 | base::FilePath img_dir{image_dir}; |
| 46 | auto target_img = img_dir.Append(partition.partition_name() + ".img"); |
| 47 | auto output_cow = img_dir.Append(partition.partition_name() + ".cow"); |
| 48 | FileDescriptorPtr target_img_fd = std::make_shared<EintrSafeFileDescriptor>(); |
| 49 | if (!target_img_fd->Open(target_img.value().c_str(), O_RDONLY)) { |
| 50 | PLOG(ERROR) << "Failed to open " << target_img.value(); |
| 51 | return false; |
| 52 | } |
| 53 | android::base::unique_fd output_fd{ |
| 54 | open(output_cow.value().c_str(), O_RDWR | O_CREAT, 0744)}; |
| 55 | if (output_fd < 0) { |
| 56 | PLOG(ERROR) << "Failed to open " << output_cow.value(); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | android::snapshot::CowWriter cow_writer{ |
| 61 | {.block_size = static_cast<uint32_t>(block_size), .compression = "gz"}}; |
| 62 | TEST_AND_RETURN_FALSE(cow_writer.Initialize(output_fd)); |
Kelvin Zhang | e36e4a3 | 2021-08-27 13:48:35 -0700 | [diff] [blame] | 63 | TEST_AND_RETURN_FALSE(CowDryRun(nullptr, |
| 64 | target_img_fd, |
Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 65 | partition.operations(), |
| 66 | partition.merge_operations(), |
| 67 | block_size, |
Kelvin Zhang | e36e4a3 | 2021-08-27 13:48:35 -0700 | [diff] [blame] | 68 | &cow_writer, |
| 69 | partition.new_partition_info().size(), |
| 70 | false)); |
Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 71 | TEST_AND_RETURN_FALSE(cow_writer.Finalize()); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | } // namespace chromeos_update_engine |
| 76 | |
| 77 | using chromeos_update_engine::MetadataParseResult; |
| 78 | using chromeos_update_engine::PayloadMetadata; |
| 79 | |
| 80 | int main(int argc, const char* argv[]) { |
| 81 | if (argc != 3) { |
| 82 | printf("Usage: %s <payload.bin> <extracted target_file>\n", argv[0]); |
| 83 | return -1; |
| 84 | } |
| 85 | const char* payload_path = argv[1]; |
| 86 | const char* images_dir = argv[2]; |
| 87 | int payload_fd = open(payload_path, O_RDONLY); |
| 88 | if (payload_fd < 0) { |
| 89 | PLOG(ERROR) << "Failed to open payload file:"; |
| 90 | return 1; |
| 91 | } |
| 92 | chromeos_update_engine::ScopedFdCloser closer{&payload_fd}; |
| 93 | auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd); |
| 94 | if (payload_size <= 0) { |
| 95 | PLOG(ERROR) |
| 96 | << "Couldn't determine size of payload file, or payload file is empty"; |
| 97 | return 2; |
| 98 | } |
| 99 | |
| 100 | PayloadMetadata payload_metadata; |
| 101 | auto payload = static_cast<unsigned char*>( |
| 102 | mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0)); |
| 103 | |
| 104 | // C++ dark magic to ensure that |payload| is properly deallocated once the |
| 105 | // program exits. |
| 106 | auto munmap_deleter = [payload_size](auto payload) { |
| 107 | munmap(payload, payload_size); |
| 108 | }; |
| 109 | std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{ |
| 110 | payload, munmap_deleter}; |
| 111 | |
| 112 | if (payload == nullptr) { |
| 113 | PLOG(ERROR) << "Failed to mmap() payload file"; |
| 114 | return 3; |
| 115 | } |
| 116 | if (payload_metadata.ParsePayloadHeader(payload, payload_size, nullptr) != |
| 117 | chromeos_update_engine::MetadataParseResult::kSuccess) { |
| 118 | LOG(ERROR) << "Payload header parse failed!"; |
| 119 | return 4; |
| 120 | } |
| 121 | chromeos_update_engine::DeltaArchiveManifest manifest; |
| 122 | if (!payload_metadata.GetManifest(payload, payload_size, &manifest)) { |
| 123 | LOG(ERROR) << "Failed to parse manifest!"; |
| 124 | return 5; |
| 125 | } |
| 126 | |
Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 127 | size_t estimated_total_cow_size = 0; |
| 128 | size_t actual_total_cow_size = 0; |
| 129 | |
Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 130 | for (const auto& partition : manifest.partitions()) { |
Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 131 | if (partition.estimate_cow_size() == 0) { |
| 132 | continue; |
| 133 | } |
Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 134 | LOG(INFO) << partition.partition_name(); |
| 135 | if (!ProcessPartition(partition, images_dir, manifest.block_size())) { |
| 136 | return 6; |
| 137 | } |
Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 138 | base::FilePath img_dir{images_dir}; |
| 139 | const auto output_cow = |
| 140 | img_dir.Append(partition.partition_name() + ".cow").value(); |
| 141 | const auto actual_cow_size = |
| 142 | chromeos_update_engine::utils::FileSize(output_cow); |
| 143 | LOG(INFO) << partition.partition_name() |
| 144 | << ": estimated COW size is: " << partition.estimate_cow_size() |
| 145 | << ", actual COW size is: " << actual_cow_size |
| 146 | << ", estimated COW size is " |
| 147 | << (actual_cow_size - partition.estimate_cow_size()) * 100.0f / |
| 148 | actual_cow_size |
| 149 | << "% smaller"; |
| 150 | estimated_total_cow_size += partition.estimate_cow_size(); |
| 151 | actual_total_cow_size += actual_cow_size; |
Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 152 | } |
Kelvin Zhang | e4f70e8 | 2021-10-13 14:56:59 -0700 | [diff] [blame] | 153 | |
| 154 | LOG(INFO) << "Total estimated COW size is: " << estimated_total_cow_size |
| 155 | << ", Total actual COW size is: " << actual_total_cow_size |
| 156 | << ", estimated COW size is " |
| 157 | << (actual_total_cow_size - estimated_total_cow_size) * 100.0f / |
| 158 | actual_total_cow_size |
| 159 | << "% smaller"; |
Kelvin Zhang | b93055f | 2021-02-03 14:22:35 -0500 | [diff] [blame] | 160 | return 0; |
| 161 | } |