Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2022 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 <fcntl.h> |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <cstdio> |
Kelvin Zhang | c7515d4 | 2022-03-23 10:44:16 -0700 | [diff] [blame] | 21 | #include <iterator> |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 22 | #include <memory> |
| 23 | |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/stat.h> |
| 26 | |
Kelvin Zhang | c7515d4 | 2022-03-23 10:44:16 -0700 | [diff] [blame] | 27 | #include <android-base/strings.h> |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 28 | #include <base/files/file_path.h> |
| 29 | #include <gflags/gflags.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include "update_engine/common/utils.h" |
| 33 | #include "update_engine/common/hash_calculator.h" |
| 34 | #include "update_engine/payload_consumer/file_descriptor.h" |
| 35 | #include "update_engine/payload_consumer/install_operation_executor.h" |
| 36 | #include "update_engine/payload_consumer/payload_metadata.h" |
| 37 | #include "update_engine/update_metadata.pb.h" |
| 38 | #include "xz.h" |
| 39 | |
| 40 | DEFINE_string(payload, "", "Path to payload.bin"); |
| 41 | DEFINE_string(output_dir, "", "Directory to put output images"); |
| 42 | DEFINE_int64(payload_offset, |
| 43 | 0, |
| 44 | "Offset to start of payload.bin. Useful if payload path actually " |
| 45 | "points to a .zip file containing payload.bin"); |
Kelvin Zhang | c7515d4 | 2022-03-23 10:44:16 -0700 | [diff] [blame] | 46 | DEFINE_string(partitions, |
| 47 | "", |
| 48 | "Comma separated list of partitions to extract, leave empty for " |
| 49 | "extracting all partitions"); |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 50 | |
| 51 | using chromeos_update_engine::DeltaArchiveManifest; |
| 52 | using chromeos_update_engine::PayloadMetadata; |
| 53 | |
| 54 | namespace chromeos_update_engine { |
| 55 | |
| 56 | bool ExtractImagesFromOTA(const DeltaArchiveManifest& manifest, |
| 57 | const PayloadMetadata& metadata, |
Kelvin Zhang | b936892 | 2022-03-17 21:11:32 -0700 | [diff] [blame] | 58 | int payload_fd, |
| 59 | size_t payload_offset, |
Kelvin Zhang | c7515d4 | 2022-03-23 10:44:16 -0700 | [diff] [blame] | 60 | std::string_view output_dir, |
| 61 | const std::set<std::string>& partitions) { |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 62 | InstallOperationExecutor executor(manifest.block_size()); |
Kelvin Zhang | b936892 | 2022-03-17 21:11:32 -0700 | [diff] [blame] | 63 | const size_t data_begin = metadata.GetMetadataSize() + |
| 64 | metadata.GetMetadataSignatureSize() + |
| 65 | payload_offset; |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 66 | const base::FilePath path( |
| 67 | base::StringPiece(output_dir.data(), output_dir.size())); |
Kelvin Zhang | b936892 | 2022-03-17 21:11:32 -0700 | [diff] [blame] | 68 | std::vector<unsigned char> blob; |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 69 | for (const auto& partition : manifest.partitions()) { |
Kelvin Zhang | c7515d4 | 2022-03-23 10:44:16 -0700 | [diff] [blame] | 70 | if (!partitions.empty() && |
| 71 | partitions.count(partition.partition_name()) == 0) { |
| 72 | continue; |
| 73 | } |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 74 | LOG(INFO) << "Extracting partition " << partition.partition_name() |
| 75 | << " size: " << partition.new_partition_info().size(); |
| 76 | const auto output_path = |
| 77 | path.Append(partition.partition_name() + ".img").value(); |
| 78 | auto fd = |
| 79 | std::make_shared<chromeos_update_engine::EintrSafeFileDescriptor>(); |
| 80 | TEST_AND_RETURN_FALSE_ERRNO( |
| 81 | fd->Open(output_path.c_str(), O_RDWR | O_CREAT, 0644)); |
| 82 | for (const auto& op : partition.operations()) { |
Kelvin Zhang | b936892 | 2022-03-17 21:11:32 -0700 | [diff] [blame] | 83 | blob.resize(op.data_length()); |
| 84 | const auto op_data_offset = data_begin + op.data_offset(); |
| 85 | ssize_t bytes_read = 0; |
| 86 | TEST_AND_RETURN_FALSE(utils::PReadAll( |
| 87 | payload_fd, blob.data(), blob.size(), op_data_offset, &bytes_read)); |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 88 | auto direct_writer = std::make_unique<DirectExtentWriter>(fd); |
Kelvin Zhang | cf32387 | 2022-03-25 14:00:39 -0700 | [diff] [blame^] | 89 | if (op.type() == InstallOperation::ZERO) { |
| 90 | TEST_AND_RETURN_FALSE(executor.ExecuteZeroOrDiscardOperation( |
| 91 | op, std::move(direct_writer))); |
| 92 | } else if (op.type() == InstallOperation::REPLACE || |
| 93 | op.type() == InstallOperation::REPLACE_BZ || |
| 94 | op.type() == InstallOperation::REPLACE_XZ) { |
| 95 | TEST_AND_RETURN_FALSE(executor.ExecuteReplaceOperation( |
| 96 | op, std::move(direct_writer), blob.data(), blob.size())); |
| 97 | } else { |
| 98 | LOG(ERROR) << "Unsupported operation type: " << op.type() << ", " |
| 99 | << InstallOperation::Type_Name(op.type()); |
| 100 | return false; |
| 101 | } |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 102 | } |
| 103 | int err = |
| 104 | truncate64(output_path.c_str(), partition.new_partition_info().size()); |
| 105 | if (err) { |
| 106 | PLOG(ERROR) << "Failed to truncate " << output_path << " to " |
| 107 | << partition.new_partition_info().size(); |
| 108 | } |
| 109 | brillo::Blob actual_hash; |
| 110 | TEST_AND_RETURN_FALSE( |
| 111 | HashCalculator::RawHashOfFile(output_path, &actual_hash)); |
| 112 | CHECK_EQ(HexEncode(ToStringView(actual_hash)), |
| 113 | HexEncode(partition.new_partition_info().hash())); |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | } // namespace chromeos_update_engine |
| 119 | |
| 120 | int main(int argc, char* argv[]) { |
| 121 | gflags::ParseCommandLineFlags(&argc, &argv, true); |
| 122 | xz_crc32_init(); |
Kelvin Zhang | c7515d4 | 2022-03-23 10:44:16 -0700 | [diff] [blame] | 123 | auto tokens = android::base::Tokenize(FLAGS_partitions, ","); |
| 124 | const std::set<std::string> partitions( |
| 125 | std::make_move_iterator(tokens.begin()), |
| 126 | std::make_move_iterator(tokens.end())); |
| 127 | if (FLAGS_payload.empty()) { |
| 128 | LOG(ERROR) << "--payload <payload path> is required"; |
| 129 | return 1; |
| 130 | } |
| 131 | if (!partitions.empty()) { |
| 132 | LOG(INFO) << "Extracting " << android::base::Join(partitions, ", "); |
| 133 | } |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 134 | int payload_fd = open(FLAGS_payload.c_str(), O_RDONLY | O_CLOEXEC); |
| 135 | if (payload_fd < 0) { |
Kelvin Zhang | c7515d4 | 2022-03-23 10:44:16 -0700 | [diff] [blame] | 136 | PLOG(ERROR) << "Failed to open payload file"; |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 137 | return 1; |
| 138 | } |
| 139 | chromeos_update_engine::ScopedFdCloser closer{&payload_fd}; |
| 140 | auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd); |
| 141 | if (payload_size <= 0) { |
| 142 | PLOG(ERROR) |
| 143 | << "Couldn't determine size of payload file, or payload file is empty"; |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | PayloadMetadata payload_metadata; |
| 148 | auto payload = static_cast<unsigned char*>( |
| 149 | mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0)); |
| 150 | |
| 151 | if (payload == MAP_FAILED) { |
| 152 | PLOG(ERROR) << "Failed to mmap() payload file"; |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | auto munmap_deleter = [payload_size](auto payload) { |
| 157 | munmap(payload, payload_size); |
| 158 | }; |
| 159 | std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{ |
| 160 | payload, munmap_deleter}; |
| 161 | if (payload_metadata.ParsePayloadHeader(payload + FLAGS_payload_offset, |
| 162 | payload_size - FLAGS_payload_offset, |
| 163 | nullptr) != |
| 164 | chromeos_update_engine::MetadataParseResult::kSuccess) { |
| 165 | LOG(ERROR) << "Payload header parse failed!"; |
| 166 | return 1; |
| 167 | } |
| 168 | DeltaArchiveManifest manifest; |
| 169 | if (!payload_metadata.GetManifest(payload + FLAGS_payload_offset, |
| 170 | payload_size - FLAGS_payload_offset, |
| 171 | &manifest)) { |
| 172 | LOG(ERROR) << "Failed to parse manifest!"; |
| 173 | return 1; |
| 174 | } |
| 175 | return !ExtractImagesFromOTA(manifest, |
| 176 | payload_metadata, |
Kelvin Zhang | b936892 | 2022-03-17 21:11:32 -0700 | [diff] [blame] | 177 | payload_fd, |
| 178 | FLAGS_payload_offset, |
Kelvin Zhang | c7515d4 | 2022-03-23 10:44:16 -0700 | [diff] [blame] | 179 | FLAGS_output_dir, |
| 180 | partitions); |
Kelvin Zhang | 596a320 | 2022-03-07 14:13:42 -0800 | [diff] [blame] | 181 | } |