blob: f200fc817d6936ac4a37f9ae400b16d4d3ff6b10 [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>
21#include <memory>
22
23#include <sys/mman.h>
24#include <sys/stat.h>
25
26#include <base/files/file_path.h>
27#include <gflags/gflags.h>
28#include <unistd.h>
29
30#include "update_engine/common/utils.h"
31#include "update_engine/common/hash_calculator.h"
32#include "update_engine/payload_consumer/file_descriptor.h"
33#include "update_engine/payload_consumer/install_operation_executor.h"
34#include "update_engine/payload_consumer/payload_metadata.h"
35#include "update_engine/update_metadata.pb.h"
36#include "xz.h"
37
38DEFINE_string(payload, "", "Path to payload.bin");
39DEFINE_string(output_dir, "", "Directory to put output images");
40DEFINE_int64(payload_offset,
41 0,
42 "Offset to start of payload.bin. Useful if payload path actually "
43 "points to a .zip file containing payload.bin");
44
45using chromeos_update_engine::DeltaArchiveManifest;
46using chromeos_update_engine::PayloadMetadata;
47
48namespace chromeos_update_engine {
49
50bool ExtractImagesFromOTA(const DeltaArchiveManifest& manifest,
51 const PayloadMetadata& metadata,
Kelvin Zhangb9368922022-03-17 21:11:32 -070052 int payload_fd,
53 size_t payload_offset,
Kelvin Zhang596a3202022-03-07 14:13:42 -080054 std::string_view output_dir) {
55 InstallOperationExecutor executor(manifest.block_size());
Kelvin Zhangb9368922022-03-17 21:11:32 -070056 const size_t data_begin = metadata.GetMetadataSize() +
57 metadata.GetMetadataSignatureSize() +
58 payload_offset;
Kelvin Zhang596a3202022-03-07 14:13:42 -080059 const base::FilePath path(
60 base::StringPiece(output_dir.data(), output_dir.size()));
Kelvin Zhangb9368922022-03-17 21:11:32 -070061 std::vector<unsigned char> blob;
Kelvin Zhang596a3202022-03-07 14:13:42 -080062 for (const auto& partition : manifest.partitions()) {
63 LOG(INFO) << "Extracting partition " << partition.partition_name()
64 << " size: " << partition.new_partition_info().size();
65 const auto output_path =
66 path.Append(partition.partition_name() + ".img").value();
67 auto fd =
68 std::make_shared<chromeos_update_engine::EintrSafeFileDescriptor>();
69 TEST_AND_RETURN_FALSE_ERRNO(
70 fd->Open(output_path.c_str(), O_RDWR | O_CREAT, 0644));
71 for (const auto& op : partition.operations()) {
Kelvin Zhangb9368922022-03-17 21:11:32 -070072 blob.resize(op.data_length());
73 const auto op_data_offset = data_begin + op.data_offset();
74 ssize_t bytes_read = 0;
75 TEST_AND_RETURN_FALSE(utils::PReadAll(
76 payload_fd, blob.data(), blob.size(), op_data_offset, &bytes_read));
Kelvin Zhang596a3202022-03-07 14:13:42 -080077 auto direct_writer = std::make_unique<DirectExtentWriter>(fd);
Kelvin Zhang596a3202022-03-07 14:13:42 -080078 TEST_AND_RETURN_FALSE(executor.ExecuteReplaceOperation(
Kelvin Zhangb9368922022-03-17 21:11:32 -070079 op, std::move(direct_writer), blob.data(), blob.size()));
Kelvin Zhang596a3202022-03-07 14:13:42 -080080 }
81 int err =
82 truncate64(output_path.c_str(), partition.new_partition_info().size());
83 if (err) {
84 PLOG(ERROR) << "Failed to truncate " << output_path << " to "
85 << partition.new_partition_info().size();
86 }
87 brillo::Blob actual_hash;
88 TEST_AND_RETURN_FALSE(
89 HashCalculator::RawHashOfFile(output_path, &actual_hash));
90 CHECK_EQ(HexEncode(ToStringView(actual_hash)),
91 HexEncode(partition.new_partition_info().hash()));
92 }
93 return true;
94}
95
96} // namespace chromeos_update_engine
97
98int main(int argc, char* argv[]) {
99 gflags::ParseCommandLineFlags(&argc, &argv, true);
100 xz_crc32_init();
101 int payload_fd = open(FLAGS_payload.c_str(), O_RDONLY | O_CLOEXEC);
102 if (payload_fd < 0) {
103 PLOG(ERROR) << "Failed to open payload file:";
104 return 1;
105 }
106 chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
107 auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
108 if (payload_size <= 0) {
109 PLOG(ERROR)
110 << "Couldn't determine size of payload file, or payload file is empty";
111 return 1;
112 }
113
114 PayloadMetadata payload_metadata;
115 auto payload = static_cast<unsigned char*>(
116 mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
117
118 if (payload == MAP_FAILED) {
119 PLOG(ERROR) << "Failed to mmap() payload file";
120 return 1;
121 }
122
123 auto munmap_deleter = [payload_size](auto payload) {
124 munmap(payload, payload_size);
125 };
126 std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
127 payload, munmap_deleter};
128 if (payload_metadata.ParsePayloadHeader(payload + FLAGS_payload_offset,
129 payload_size - FLAGS_payload_offset,
130 nullptr) !=
131 chromeos_update_engine::MetadataParseResult::kSuccess) {
132 LOG(ERROR) << "Payload header parse failed!";
133 return 1;
134 }
135 DeltaArchiveManifest manifest;
136 if (!payload_metadata.GetManifest(payload + FLAGS_payload_offset,
137 payload_size - FLAGS_payload_offset,
138 &manifest)) {
139 LOG(ERROR) << "Failed to parse manifest!";
140 return 1;
141 }
142 return !ExtractImagesFromOTA(manifest,
143 payload_metadata,
Kelvin Zhangb9368922022-03-17 21:11:32 -0700144 payload_fd,
145 FLAGS_payload_offset,
Kelvin Zhang596a3202022-03-07 14:13:42 -0800146 FLAGS_output_dir);
147}