blob: aa0d8230b5920c768dce06e312373fdfad1b1634 [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 Zhang596a3202022-03-07 14:13:42 -080089 TEST_AND_RETURN_FALSE(executor.ExecuteReplaceOperation(
Kelvin Zhangb9368922022-03-17 21:11:32 -070090 op, std::move(direct_writer), blob.data(), blob.size()));
Kelvin Zhang596a3202022-03-07 14:13:42 -080091 }
92 int err =
93 truncate64(output_path.c_str(), partition.new_partition_info().size());
94 if (err) {
95 PLOG(ERROR) << "Failed to truncate " << output_path << " to "
96 << partition.new_partition_info().size();
97 }
98 brillo::Blob actual_hash;
99 TEST_AND_RETURN_FALSE(
100 HashCalculator::RawHashOfFile(output_path, &actual_hash));
101 CHECK_EQ(HexEncode(ToStringView(actual_hash)),
102 HexEncode(partition.new_partition_info().hash()));
103 }
104 return true;
105}
106
107} // namespace chromeos_update_engine
108
109int main(int argc, char* argv[]) {
110 gflags::ParseCommandLineFlags(&argc, &argv, true);
111 xz_crc32_init();
Kelvin Zhangc7515d42022-03-23 10:44:16 -0700112 auto tokens = android::base::Tokenize(FLAGS_partitions, ",");
113 const std::set<std::string> partitions(
114 std::make_move_iterator(tokens.begin()),
115 std::make_move_iterator(tokens.end()));
116 if (FLAGS_payload.empty()) {
117 LOG(ERROR) << "--payload <payload path> is required";
118 return 1;
119 }
120 if (!partitions.empty()) {
121 LOG(INFO) << "Extracting " << android::base::Join(partitions, ", ");
122 }
Kelvin Zhang596a3202022-03-07 14:13:42 -0800123 int payload_fd = open(FLAGS_payload.c_str(), O_RDONLY | O_CLOEXEC);
124 if (payload_fd < 0) {
Kelvin Zhangc7515d42022-03-23 10:44:16 -0700125 PLOG(ERROR) << "Failed to open payload file";
Kelvin Zhang596a3202022-03-07 14:13:42 -0800126 return 1;
127 }
128 chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
129 auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
130 if (payload_size <= 0) {
131 PLOG(ERROR)
132 << "Couldn't determine size of payload file, or payload file is empty";
133 return 1;
134 }
135
136 PayloadMetadata payload_metadata;
137 auto payload = static_cast<unsigned char*>(
138 mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
139
140 if (payload == MAP_FAILED) {
141 PLOG(ERROR) << "Failed to mmap() payload file";
142 return 1;
143 }
144
145 auto munmap_deleter = [payload_size](auto payload) {
146 munmap(payload, payload_size);
147 };
148 std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
149 payload, munmap_deleter};
150 if (payload_metadata.ParsePayloadHeader(payload + FLAGS_payload_offset,
151 payload_size - FLAGS_payload_offset,
152 nullptr) !=
153 chromeos_update_engine::MetadataParseResult::kSuccess) {
154 LOG(ERROR) << "Payload header parse failed!";
155 return 1;
156 }
157 DeltaArchiveManifest manifest;
158 if (!payload_metadata.GetManifest(payload + FLAGS_payload_offset,
159 payload_size - FLAGS_payload_offset,
160 &manifest)) {
161 LOG(ERROR) << "Failed to parse manifest!";
162 return 1;
163 }
164 return !ExtractImagesFromOTA(manifest,
165 payload_metadata,
Kelvin Zhangb9368922022-03-17 21:11:32 -0700166 payload_fd,
167 FLAGS_payload_offset,
Kelvin Zhangc7515d42022-03-23 10:44:16 -0700168 FLAGS_output_dir,
169 partitions);
Kelvin Zhang596a3202022-03-07 14:13:42 -0800170}