blob: 6fab44d2cc960133acd54e2ff5a8f31c039ba270 [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,
52 const uint8_t* payload,
53 size_t size,
54 std::string_view output_dir) {
55 InstallOperationExecutor executor(manifest.block_size());
56 const uint8_t* data_begin = payload + metadata.GetMetadataSize() +
57 metadata.GetMetadataSignatureSize();
58 const base::FilePath path(
59 base::StringPiece(output_dir.data(), output_dir.size()));
60 for (const auto& partition : manifest.partitions()) {
61 LOG(INFO) << "Extracting partition " << partition.partition_name()
62 << " size: " << partition.new_partition_info().size();
63 const auto output_path =
64 path.Append(partition.partition_name() + ".img").value();
65 auto fd =
66 std::make_shared<chromeos_update_engine::EintrSafeFileDescriptor>();
67 TEST_AND_RETURN_FALSE_ERRNO(
68 fd->Open(output_path.c_str(), O_RDWR | O_CREAT, 0644));
69 for (const auto& op : partition.operations()) {
70 auto direct_writer = std::make_unique<DirectExtentWriter>(fd);
71 const auto op_data = data_begin + op.data_offset();
72 TEST_AND_RETURN_FALSE(executor.ExecuteReplaceOperation(
73 op, std::move(direct_writer), op_data, op.data_length()));
74 }
75 int err =
76 truncate64(output_path.c_str(), partition.new_partition_info().size());
77 if (err) {
78 PLOG(ERROR) << "Failed to truncate " << output_path << " to "
79 << partition.new_partition_info().size();
80 }
81 brillo::Blob actual_hash;
82 TEST_AND_RETURN_FALSE(
83 HashCalculator::RawHashOfFile(output_path, &actual_hash));
84 CHECK_EQ(HexEncode(ToStringView(actual_hash)),
85 HexEncode(partition.new_partition_info().hash()));
86 }
87 return true;
88}
89
90} // namespace chromeos_update_engine
91
92int main(int argc, char* argv[]) {
93 gflags::ParseCommandLineFlags(&argc, &argv, true);
94 xz_crc32_init();
95 int payload_fd = open(FLAGS_payload.c_str(), O_RDONLY | O_CLOEXEC);
96 if (payload_fd < 0) {
97 PLOG(ERROR) << "Failed to open payload file:";
98 return 1;
99 }
100 chromeos_update_engine::ScopedFdCloser closer{&payload_fd};
101 auto payload_size = chromeos_update_engine::utils::FileSize(payload_fd);
102 if (payload_size <= 0) {
103 PLOG(ERROR)
104 << "Couldn't determine size of payload file, or payload file is empty";
105 return 1;
106 }
107
108 PayloadMetadata payload_metadata;
109 auto payload = static_cast<unsigned char*>(
110 mmap(nullptr, payload_size, PROT_READ, MAP_PRIVATE, payload_fd, 0));
111
112 if (payload == MAP_FAILED) {
113 PLOG(ERROR) << "Failed to mmap() payload file";
114 return 1;
115 }
116
117 auto munmap_deleter = [payload_size](auto payload) {
118 munmap(payload, payload_size);
119 };
120 std::unique_ptr<unsigned char, decltype(munmap_deleter)> munmapper{
121 payload, munmap_deleter};
122 if (payload_metadata.ParsePayloadHeader(payload + FLAGS_payload_offset,
123 payload_size - FLAGS_payload_offset,
124 nullptr) !=
125 chromeos_update_engine::MetadataParseResult::kSuccess) {
126 LOG(ERROR) << "Payload header parse failed!";
127 return 1;
128 }
129 DeltaArchiveManifest manifest;
130 if (!payload_metadata.GetManifest(payload + FLAGS_payload_offset,
131 payload_size - FLAGS_payload_offset,
132 &manifest)) {
133 LOG(ERROR) << "Failed to parse manifest!";
134 return 1;
135 }
136 return !ExtractImagesFromOTA(manifest,
137 payload_metadata,
138 payload + FLAGS_payload_offset,
139 payload_size - FLAGS_payload_offset,
140 FLAGS_output_dir);
141}