Kelvin Zhang | 83b594b | 2023-05-25 13:43:06 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2023 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 | |
Kelvin Zhang | 8bd5c4c | 2023-06-08 11:21:27 -0700 | [diff] [blame^] | 17 | #include <array> |
| 18 | |
| 19 | #include <android-base/stringprintf.h> |
| 20 | #include <android-base/unique_fd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sparse/sparse.h> |
| 23 | |
| 24 | #include "android-base/file.h" |
Kelvin Zhang | 83b594b | 2023-05-25 13:43:06 -0700 | [diff] [blame] | 25 | #include "common/utils.h" |
| 26 | #include "update_engine/payload_generator/ext2_filesystem.h" |
| 27 | #include "update_engine/payload_generator/erofs_filesystem.h" |
| 28 | #include "update_engine/payload_generator/filesystem_interface.h" |
| 29 | |
| 30 | namespace chromeos_update_engine { |
| 31 | |
| 32 | int WriteBlockMap(const char* img, |
| 33 | const FilesystemInterface* fs, |
| 34 | const char* output_file) { |
| 35 | std::vector<FilesystemInterface::File> files; |
| 36 | if (!fs->GetFiles(&files)) { |
| 37 | LOG(ERROR) << "Failed to parse file info in " << img; |
| 38 | return -2; |
| 39 | } |
| 40 | android::base::unique_fd fd( |
| 41 | open(output_file, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644)); |
| 42 | if (fd < 0) { |
| 43 | PLOG(ERROR) << "Failed to open " << output_file; |
| 44 | return -errno; |
| 45 | } |
| 46 | for (const auto& file : files) { |
| 47 | if (file.extents.empty()) { |
| 48 | continue; |
| 49 | } |
| 50 | std::string output_line; |
| 51 | output_line.append(file.name); |
| 52 | for (const auto& extent : file.extents) { |
| 53 | if (extent.num_blocks() <= 0) { |
| 54 | continue; |
| 55 | } |
| 56 | output_line.append(" "); |
| 57 | if (extent.num_blocks() == 1) { |
| 58 | output_line.append(std::to_string(extent.start_block())); |
| 59 | continue; |
| 60 | } |
| 61 | const auto extent_str = android::base::StringPrintf( |
| 62 | "%lu-%lu", |
| 63 | extent.start_block(), |
| 64 | extent.start_block() + extent.num_blocks() - 1); |
| 65 | output_line.append(extent_str); |
| 66 | } |
| 67 | output_line.append("\n"); |
| 68 | if (!utils::WriteAll(fd.get(), output_line.data(), output_line.size())) { |
| 69 | PLOG(ERROR) << "Failed to write to " << output_file; |
| 70 | return -errno; |
| 71 | } |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Kelvin Zhang | 8bd5c4c | 2023-06-08 11:21:27 -0700 | [diff] [blame^] | 76 | bool IsSparseImage(int fd) { |
| 77 | static constexpr std::string_view kSparseMagic = "\x3A\xFF\x26\xED"; |
| 78 | |
| 79 | std::array<char, kSparseMagic.size()> buf{}; |
| 80 | if (pread(fd, buf.data(), kSparseMagic.size(), 0) != 4) { |
| 81 | return false; |
| 82 | } |
| 83 | return memcmp(buf.data(), kSparseMagic.data(), kSparseMagic.size()) == 0; |
| 84 | } |
| 85 | |
Kelvin Zhang | 83b594b | 2023-05-25 13:43:06 -0700 | [diff] [blame] | 86 | int Main(int argc, const char* argv[]) { |
| 87 | const char* img = argv[1]; |
| 88 | const char* output_file = argv[2]; |
Kelvin Zhang | 8bd5c4c | 2023-06-08 11:21:27 -0700 | [diff] [blame^] | 89 | android::base::unique_fd fd(open(img, O_RDONLY | O_CLOEXEC)); |
| 90 | if (!fd.ok()) { |
| 91 | PLOG(ERROR) << "Failed to open " << img; |
| 92 | return -errno; |
| 93 | } |
| 94 | TemporaryFile tmpfile; |
| 95 | if (IsSparseImage(fd.get())) { |
| 96 | LOG(INFO) << "Detected sparse image " << img << ", unsparsing..."; |
| 97 | struct sparse_file* s = sparse_file_import(fd, true, false); |
| 98 | if (s == nullptr) { |
| 99 | LOG(ERROR) << "Failed to unsparse " << img; |
| 100 | return -2; |
| 101 | } |
| 102 | if (sparse_file_write(s, tmpfile.fd, false, false, false) < 0) { |
| 103 | LOG(ERROR) << "Failed to write unsparsed output to " << tmpfile.path; |
| 104 | return -1; |
| 105 | } |
| 106 | img = tmpfile.path; |
| 107 | } |
Kelvin Zhang | 83b594b | 2023-05-25 13:43:06 -0700 | [diff] [blame] | 108 | std::unique_ptr<FilesystemInterface> fs; |
| 109 | fs = ErofsFilesystem::CreateFromFile(img); |
| 110 | if (fs != nullptr) { |
| 111 | return WriteBlockMap(img, fs.get(), output_file); |
| 112 | } |
| 113 | fs = Ext2Filesystem::CreateFromFile(img); |
| 114 | if (fs != nullptr) { |
| 115 | return WriteBlockMap(img, fs.get(), output_file); |
| 116 | } |
| 117 | LOG(ERROR) << "Failed to parse " << img; |
| 118 | return -1; |
| 119 | } |
| 120 | } // namespace chromeos_update_engine |
| 121 | |
| 122 | int main(int argc, const char* argv[]) { |
| 123 | return chromeos_update_engine::Main(argc, argv); |
| 124 | } |