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)); |
| 63 | TEST_AND_RETURN_FALSE(CowDryRun(target_img_fd, |
| 64 | partition.operations(), |
| 65 | partition.merge_operations(), |
| 66 | block_size, |
| 67 | &cow_writer)); |
| 68 | TEST_AND_RETURN_FALSE(cow_writer.Finalize()); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | } // namespace chromeos_update_engine |
| 73 | |
| 74 | using chromeos_update_engine::MetadataParseResult; |
| 75 | using chromeos_update_engine::PayloadMetadata; |
| 76 | |
| 77 | int main(int argc, const char* argv[]) { |
| 78 | if (argc != 3) { |
| 79 | printf("Usage: %s <payload.bin> <extracted target_file>\n", argv[0]); |
| 80 | return -1; |
| 81 | } |
| 82 | const char* payload_path = argv[1]; |
| 83 | const char* images_dir = argv[2]; |
| 84 | int payload_fd = open(payload_path, O_RDONLY); |
| 85 | if (payload_fd < 0) { |
| 86 | PLOG(ERROR) << "Failed to open payload file:"; |
| 87 | return 1; |
| 88 | } |
| 89 | chromeos_update_engine::ScopedFdCloser closer{&payload_fd}; |
| 90 | auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd); |
| 91 | if (payload_size <= 0) { |
| 92 | PLOG(ERROR) |
| 93 | << "Couldn't determine size of payload file, or payload file is empty"; |
| 94 | return 2; |
| 95 | } |
| 96 | |
| 97 | PayloadMetadata payload_metadata; |
| 98 | auto payload = static_cast<unsigned char*>( |
| 99 | mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0)); |
| 100 | |
| 101 | // C++ dark magic to ensure that |payload| is properly deallocated once the |
| 102 | // program exits. |
| 103 | auto munmap_deleter = [payload_size](auto payload) { |
| 104 | munmap(payload, payload_size); |
| 105 | }; |
| 106 | std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{ |
| 107 | payload, munmap_deleter}; |
| 108 | |
| 109 | if (payload == nullptr) { |
| 110 | PLOG(ERROR) << "Failed to mmap() payload file"; |
| 111 | return 3; |
| 112 | } |
| 113 | if (payload_metadata.ParsePayloadHeader(payload, payload_size, nullptr) != |
| 114 | chromeos_update_engine::MetadataParseResult::kSuccess) { |
| 115 | LOG(ERROR) << "Payload header parse failed!"; |
| 116 | return 4; |
| 117 | } |
| 118 | chromeos_update_engine::DeltaArchiveManifest manifest; |
| 119 | if (!payload_metadata.GetManifest(payload, payload_size, &manifest)) { |
| 120 | LOG(ERROR) << "Failed to parse manifest!"; |
| 121 | return 5; |
| 122 | } |
| 123 | |
| 124 | for (const auto& partition : manifest.partitions()) { |
| 125 | LOG(INFO) << partition.partition_name(); |
| 126 | if (!ProcessPartition(partition, images_dir, manifest.block_size())) { |
| 127 | return 6; |
| 128 | } |
| 129 | } |
| 130 | return 0; |
| 131 | } |