blob: 80bb74bf2413ed0a3fe62f55e0cb6312a50b7e58 [file] [log] [blame]
Kelvin Zhang596a3202022-03-07 14:13:42 -08001//
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 Zhangc7515d42022-03-23 10:44:16 -070021#include <iterator>
Kelvin Zhang596a3202022-03-07 14:13:42 -080022#include <memory>
23
24#include <sys/mman.h>
25#include <sys/stat.h>
26
Kelvin Zhangc7515d42022-03-23 10:44:16 -070027#include <android-base/strings.h>
Kelvin Zhang596a3202022-03-07 14:13:42 -080028#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
40DEFINE_string(payload, "", "Path to payload.bin");
41DEFINE_string(output_dir, "", "Directory to put output images");
42DEFINE_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 Zhangc7515d42022-03-23 10:44:16 -070046DEFINE_string(partitions,
47 "",
48 "Comma separated list of partitions to extract, leave empty for "
49 "extracting all partitions");
Kelvin Zhang596a3202022-03-07 14:13:42 -080050
51using chromeos_update_engine::DeltaArchiveManifest;
52using chromeos_update_engine::PayloadMetadata;
53
54namespace chromeos_update_engine {
55
56bool ExtractImagesFromOTA(const DeltaArchiveManifest& manifest,
57 const PayloadMetadata& metadata,
Kelvin Zhangb9368922022-03-17 21:11:32 -070058 int payload_fd,
59 size_t payload_offset,
Kelvin Zhangc7515d42022-03-23 10:44:16 -070060 std::string_view output_dir,
61 const std::set<std::string>& partitions) {
Kelvin Zhang596a3202022-03-07 14:13:42 -080062 InstallOperationExecutor executor(manifest.block_size());
Kelvin Zhangb9368922022-03-17 21:11:32 -070063 const size_t data_begin = metadata.GetMetadataSize() +
64 metadata.GetMetadataSignatureSize() +
65 payload_offset;
Kelvin Zhang596a3202022-03-07 14:13:42 -080066 const base::FilePath path(
67 base::StringPiece(output_dir.data(), output_dir.size()));
Kelvin Zhangb9368922022-03-17 21:11:32 -070068 std::vector<unsigned char> blob;
Kelvin Zhang596a3202022-03-07 14:13:42 -080069 for (const auto& partition : manifest.partitions()) {
Kelvin Zhangc7515d42022-03-23 10:44:16 -070070 if (!partitions.empty() &&
71 partitions.count(partition.partition_name()) == 0) {
72 continue;
73 }
Kelvin Zhang596a3202022-03-07 14:13:42 -080074 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 Zhangb9368922022-03-17 21:11:32 -070083 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 Zhang596a3202022-03-07 14:13:42 -080088 auto direct_writer = std::make_unique<DirectExtentWriter>(fd);
Kelvin Zhangcf323872022-03-25 14:00:39 -070089 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 Zhang596a3202022-03-07 14:13:42 -0800102 }
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
120int main(int argc, char* argv[]) {
121 gflags::ParseCommandLineFlags(&argc, &argv, true);
122 xz_crc32_init();
Kelvin Zhangc7515d42022-03-23 10:44:16 -0700123 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 Zhang596a3202022-03-07 14:13:42 -0800134 int payload_fd = open(FLAGS_payload.c_str(), O_RDONLY | O_CLOEXEC);
135 if (payload_fd < 0) {
Kelvin Zhangc7515d42022-03-23 10:44:16 -0700136 PLOG(ERROR) << "Failed to open payload file";
Kelvin Zhang596a3202022-03-07 14:13:42 -0800137 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 Zhangb9368922022-03-17 21:11:32 -0700177 payload_fd,
178 FLAGS_payload_offset,
Kelvin Zhangc7515d42022-03-23 10:44:16 -0700179 FLAGS_output_dir,
180 partitions);
Kelvin Zhang596a3202022-03-07 14:13:42 -0800181}