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