| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2017 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 "update_engine/payload_generator/deflate_utils.h" | 
|  | 18 |  | 
|  | 19 | #include <algorithm> | 
|  | 20 | #include <string> | 
|  | 21 | #include <utility> | 
|  | 22 |  | 
|  | 23 | #include <base/files/file_util.h> | 
|  | 24 | #include <base/logging.h> | 
|  | 25 | #include <base/strings/string_util.h> | 
|  | 26 |  | 
|  | 27 | #include "update_engine/common/utils.h" | 
|  | 28 | #include "update_engine/payload_generator/delta_diff_generator.h" | 
|  | 29 | #include "update_engine/payload_generator/extent_ranges.h" | 
|  | 30 | #include "update_engine/payload_generator/extent_utils.h" | 
|  | 31 | #include "update_engine/payload_generator/squashfs_filesystem.h" | 
|  | 32 | #include "update_engine/update_metadata.pb.h" | 
|  | 33 |  | 
|  | 34 | using std::string; | 
|  | 35 | using std::vector; | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 36 | using puffin::BitExtent; | 
|  | 37 | using puffin::ByteExtent; | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | namespace chromeos_update_engine { | 
|  | 40 | namespace deflate_utils { | 
|  | 41 | namespace { | 
|  | 42 |  | 
|  | 43 | // The minimum size for a squashfs image to be processed. | 
|  | 44 | const uint64_t kMinimumSquashfsImageSize = 1 * 1024 * 1024;  // bytes | 
|  | 45 |  | 
|  | 46 | // TODO(*): Optimize this so we don't have to read all extents into memory in | 
|  | 47 | // case it is large. | 
|  | 48 | bool CopyExtentsToFile(const string& in_path, | 
|  | 49 | const vector<Extent> extents, | 
|  | 50 | const string& out_path, | 
|  | 51 | size_t block_size) { | 
| Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 52 | brillo::Blob data(utils::BlocksInExtents(extents) * block_size); | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 53 | TEST_AND_RETURN_FALSE( | 
|  | 54 | utils::ReadExtents(in_path, extents, &data, data.size(), block_size)); | 
|  | 55 | TEST_AND_RETURN_FALSE( | 
|  | 56 | utils::WriteFile(out_path.c_str(), data.data(), data.size())); | 
|  | 57 | return true; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | bool IsSquashfsImage(const string& part_path, | 
|  | 61 | const FilesystemInterface::File& file) { | 
|  | 62 | // Only check for files with img postfix. | 
|  | 63 | if (base::EndsWith(file.name, ".img", base::CompareCase::SENSITIVE) && | 
| Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 64 | utils::BlocksInExtents(file.extents) >= | 
|  | 65 | kMinimumSquashfsImageSize / kBlockSize) { | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 66 | brillo::Blob super_block; | 
|  | 67 | TEST_AND_RETURN_FALSE( | 
|  | 68 | utils::ReadFileChunk(part_path, | 
|  | 69 | file.extents[0].start_block() * kBlockSize, | 
|  | 70 | 100, | 
|  | 71 | &super_block)); | 
|  | 72 | return SquashfsFilesystem::IsSquashfsImage(super_block); | 
|  | 73 | } | 
|  | 74 | return false; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | // Realigns subfiles |files| of a splitted file |file| into its correct | 
|  | 78 | // positions. This can be used for squashfs, zip, apk, etc. | 
|  | 79 | bool RealignSplittedFiles(const FilesystemInterface::File& file, | 
|  | 80 | vector<FilesystemInterface::File>* files) { | 
|  | 81 | // We have to shift all the Extents in |files|, based on the Extents of the | 
|  | 82 | // |file| itself. | 
|  | 83 | size_t num_blocks = 0; | 
|  | 84 | for (auto& in_file : *files) {  // We need to modify so no constant. | 
|  | 85 | TEST_AND_RETURN_FALSE( | 
|  | 86 | ShiftExtentsOverExtents(file.extents, &in_file.extents)); | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 87 | TEST_AND_RETURN_FALSE( | 
|  | 88 | ShiftBitExtentsOverExtents(file.extents, &in_file.deflates)); | 
|  | 89 |  | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 90 | in_file.name = file.name + "/" + in_file.name; | 
| Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 91 | num_blocks += utils::BlocksInExtents(in_file.extents); | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | // Check that all files in |in_files| cover the entire image. | 
| Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 95 | TEST_AND_RETURN_FALSE(utils::BlocksInExtents(file.extents) == num_blocks); | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 96 | return true; | 
|  | 97 | } | 
|  | 98 |  | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 99 | bool IsBitExtentInExtent(const Extent& extent, const BitExtent& bit_extent) { | 
|  | 100 | return (bit_extent.offset / 8) >= (extent.start_block() * kBlockSize) && | 
|  | 101 | ((bit_extent.offset + bit_extent.length + 7) / 8) <= | 
|  | 102 | ((extent.start_block() + extent.num_blocks()) * kBlockSize); | 
|  | 103 | } | 
|  | 104 |  | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 105 | }  // namespace | 
|  | 106 |  | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 107 | ByteExtent ExpandToByteExtent(const BitExtent& extent) { | 
|  | 108 | uint64_t offset = extent.offset / 8; | 
|  | 109 | uint64_t length = ((extent.offset + extent.length + 7) / 8) - offset; | 
|  | 110 | return {offset, length}; | 
|  | 111 | } | 
|  | 112 |  | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 113 | bool ShiftExtentsOverExtents(const vector<Extent>& base_extents, | 
|  | 114 | vector<Extent>* over_extents) { | 
| Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 115 | if (utils::BlocksInExtents(base_extents) < | 
|  | 116 | utils::BlocksInExtents(*over_extents)) { | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 117 | LOG(ERROR) << "over_extents have more blocks than base_extents! Invalid!"; | 
|  | 118 | return false; | 
|  | 119 | } | 
|  | 120 | for (size_t idx = 0; idx < over_extents->size(); idx++) { | 
|  | 121 | auto over_ext = &over_extents->at(idx); | 
|  | 122 | auto gap_blocks = base_extents[0].start_block(); | 
|  | 123 | auto last_end_block = base_extents[0].start_block(); | 
|  | 124 | for (auto base_ext : base_extents) {  // We need to modify |base_ext|, so we | 
|  | 125 | // use copy. | 
|  | 126 | gap_blocks += base_ext.start_block() - last_end_block; | 
|  | 127 | last_end_block = base_ext.start_block() + base_ext.num_blocks(); | 
|  | 128 | base_ext.set_start_block(base_ext.start_block() - gap_blocks); | 
|  | 129 | if (over_ext->start_block() >= base_ext.start_block() && | 
|  | 130 | over_ext->start_block() < | 
|  | 131 | base_ext.start_block() + base_ext.num_blocks()) { | 
|  | 132 | if (over_ext->start_block() + over_ext->num_blocks() <= | 
|  | 133 | base_ext.start_block() + base_ext.num_blocks()) { | 
|  | 134 | // |over_ext| is inside |base_ext|, increase its start block. | 
|  | 135 | over_ext->set_start_block(over_ext->start_block() + gap_blocks); | 
|  | 136 | } else { | 
|  | 137 | // |over_ext| spills over this |base_ext|, split it into two. | 
|  | 138 | auto new_blocks = base_ext.start_block() + base_ext.num_blocks() - | 
|  | 139 | over_ext->start_block(); | 
|  | 140 | vector<Extent> new_extents = { | 
|  | 141 | ExtentForRange(gap_blocks + over_ext->start_block(), new_blocks), | 
|  | 142 | ExtentForRange(over_ext->start_block() + new_blocks, | 
|  | 143 | over_ext->num_blocks() - new_blocks)}; | 
|  | 144 | *over_ext = new_extents[0]; | 
|  | 145 | over_extents->insert(std::next(over_extents->begin(), idx + 1), | 
|  | 146 | new_extents[1]); | 
|  | 147 | } | 
|  | 148 | break;  // We processed |over_ext|, so break the loop; | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 | } | 
|  | 152 | return true; | 
|  | 153 | } | 
|  | 154 |  | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 155 | bool ShiftBitExtentsOverExtents(const vector<Extent>& base_extents, | 
|  | 156 | vector<BitExtent>* over_extents) { | 
|  | 157 | if (over_extents->empty()) { | 
|  | 158 | return true; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | // This check is needed to make sure the number of bytes in |over_extents| | 
|  | 162 | // does not exceed |base_extents|. | 
|  | 163 | auto last_extent = ExpandToByteExtent(over_extents->back()); | 
|  | 164 | TEST_AND_RETURN_FALSE(last_extent.offset + last_extent.length <= | 
| Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 165 | utils::BlocksInExtents(base_extents) * kBlockSize); | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 166 |  | 
|  | 167 | for (auto o_ext = over_extents->begin(); o_ext != over_extents->end();) { | 
|  | 168 | size_t gap_blocks = base_extents[0].start_block(); | 
|  | 169 | size_t last_end_block = base_extents[0].start_block(); | 
|  | 170 | bool o_ext_processed = false; | 
|  | 171 | for (auto b_ext : base_extents) {  // We need to modify |b_ext|, so we copy. | 
|  | 172 | gap_blocks += b_ext.start_block() - last_end_block; | 
|  | 173 | last_end_block = b_ext.start_block() + b_ext.num_blocks(); | 
|  | 174 | b_ext.set_start_block(b_ext.start_block() - gap_blocks); | 
|  | 175 | auto byte_o_ext = ExpandToByteExtent(*o_ext); | 
|  | 176 | if (byte_o_ext.offset >= b_ext.start_block() * kBlockSize && | 
|  | 177 | byte_o_ext.offset < | 
|  | 178 | (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) { | 
|  | 179 | if ((byte_o_ext.offset + byte_o_ext.length) <= | 
|  | 180 | (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) { | 
|  | 181 | // |o_ext| is inside |b_ext|, increase its start block. | 
|  | 182 | o_ext->offset += gap_blocks * kBlockSize * 8; | 
|  | 183 | ++o_ext; | 
|  | 184 | } else { | 
|  | 185 | // |o_ext| spills over this |b_ext|, remove it. | 
|  | 186 | o_ext = over_extents->erase(o_ext); | 
|  | 187 | } | 
|  | 188 | o_ext_processed = true; | 
|  | 189 | break;  // We processed o_ext, so break the loop; | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 | TEST_AND_RETURN_FALSE(o_ext_processed); | 
|  | 193 | } | 
|  | 194 | return true; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | vector<BitExtent> FindDeflates(const vector<Extent>& extents, | 
|  | 198 | const vector<BitExtent>& in_deflates) { | 
|  | 199 | vector<BitExtent> result; | 
|  | 200 | // TODO(ahassani): Replace this with binary_search style search. | 
|  | 201 | for (const auto& deflate : in_deflates) { | 
|  | 202 | for (const auto& extent : extents) { | 
|  | 203 | if (IsBitExtentInExtent(extent, deflate)) { | 
|  | 204 | result.push_back(deflate); | 
|  | 205 | break; | 
|  | 206 | } | 
|  | 207 | } | 
|  | 208 | } | 
|  | 209 | return result; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | bool CompactDeflates(const vector<Extent>& extents, | 
|  | 213 | const vector<BitExtent>& in_deflates, | 
|  | 214 | vector<BitExtent>* out_deflates) { | 
|  | 215 | size_t bytes_passed = 0; | 
|  | 216 | out_deflates->reserve(in_deflates.size()); | 
|  | 217 | for (const auto& extent : extents) { | 
|  | 218 | size_t gap_bytes = extent.start_block() * kBlockSize - bytes_passed; | 
|  | 219 | for (const auto& deflate : in_deflates) { | 
|  | 220 | if (IsBitExtentInExtent(extent, deflate)) { | 
|  | 221 | out_deflates->emplace_back(deflate.offset - (gap_bytes * 8), | 
|  | 222 | deflate.length); | 
|  | 223 | } | 
|  | 224 | } | 
|  | 225 | bytes_passed += extent.num_blocks() * kBlockSize; | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | // All given |in_deflates| items should've been inside one of the extents in | 
|  | 229 | // |extents|. | 
|  | 230 | TEST_AND_RETURN_FALSE(in_deflates.size() == out_deflates->size()); | 
|  | 231 |  | 
|  | 232 | // Make sure all outgoing deflates are ordered and non-overlapping. | 
|  | 233 | auto result = std::adjacent_find(out_deflates->begin(), | 
|  | 234 | out_deflates->end(), | 
|  | 235 | [](const BitExtent& a, const BitExtent& b) { | 
|  | 236 | return (a.offset + a.length) > b.offset; | 
|  | 237 | }); | 
|  | 238 | TEST_AND_RETURN_FALSE(result == out_deflates->end()); | 
|  | 239 | return true; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | bool FindAndCompactDeflates(const vector<Extent>& extents, | 
|  | 243 | const vector<BitExtent>& in_deflates, | 
|  | 244 | vector<BitExtent>* out_deflates) { | 
|  | 245 | auto found_deflates = FindDeflates(extents, in_deflates); | 
|  | 246 | TEST_AND_RETURN_FALSE(CompactDeflates(extents, found_deflates, out_deflates)); | 
|  | 247 | return true; | 
|  | 248 | } | 
|  | 249 |  | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 250 | bool PreprocessParitionFiles(const PartitionConfig& part, | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 251 | vector<FilesystemInterface::File>* result_files, | 
|  | 252 | bool extract_deflates) { | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 253 | // Get the file system files. | 
|  | 254 | vector<FilesystemInterface::File> tmp_files; | 
|  | 255 | part.fs_interface->GetFiles(&tmp_files); | 
|  | 256 | result_files->reserve(tmp_files.size()); | 
|  | 257 |  | 
| Tianjie Xu | 1a7bb2c | 2018-01-22 17:56:57 -0800 | [diff] [blame] | 258 | for (auto& file : tmp_files) { | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 259 | if (IsSquashfsImage(part.path, file)) { | 
|  | 260 | // Read the image into a file. | 
|  | 261 | base::FilePath path; | 
|  | 262 | TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&path)); | 
|  | 263 | ScopedPathUnlinker old_unlinker(path.value()); | 
|  | 264 | TEST_AND_RETURN_FALSE( | 
|  | 265 | CopyExtentsToFile(part.path, file.extents, path.value(), kBlockSize)); | 
|  | 266 | // Test if it is actually a Squashfs file. | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 267 | auto sqfs = | 
|  | 268 | SquashfsFilesystem::CreateFromFile(path.value(), extract_deflates); | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 269 | if (sqfs) { | 
|  | 270 | // It is an squashfs file. Get its files to replace with itself. | 
|  | 271 | vector<FilesystemInterface::File> files; | 
|  | 272 | sqfs->GetFiles(&files); | 
|  | 273 |  | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 274 | // Replace squashfs file with its files only if |files| has at least two | 
|  | 275 | // files or if it has some deflates (since it is better to replace it to | 
|  | 276 | // take advantage of the deflates.) | 
|  | 277 | if (files.size() > 1 || | 
|  | 278 | (files.size() == 1 && !files[0].deflates.empty())) { | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 279 | TEST_AND_RETURN_FALSE(RealignSplittedFiles(file, &files)); | 
|  | 280 | result_files->insert(result_files->end(), files.begin(), files.end()); | 
|  | 281 | continue; | 
|  | 282 | } | 
|  | 283 | } else { | 
|  | 284 | LOG(WARNING) << "We thought file: " << file.name | 
|  | 285 | << " was a Squashfs file, but it was not."; | 
|  | 286 | } | 
|  | 287 | } | 
| Tianjie Xu | 1a7bb2c | 2018-01-22 17:56:57 -0800 | [diff] [blame] | 288 |  | 
|  | 289 | // Search for deflates if the file is in zip format. | 
| Amin Hassani | afaaa03 | 2018-03-29 11:39:48 -0700 | [diff] [blame] | 290 | // .zvoice files may eventually move out of rootfs. If that happens, remove | 
|  | 291 | // ".zvoice" (crbug.com/782918). | 
|  | 292 | const string zip_file_extensions[] = {".apk", ".zip", ".jar", ".zvoice"}; | 
| Tianjie Xu | 1a7bb2c | 2018-01-22 17:56:57 -0800 | [diff] [blame] | 293 | bool is_zip = | 
| Amin Hassani | afaaa03 | 2018-03-29 11:39:48 -0700 | [diff] [blame] | 294 | any_of(zip_file_extensions, | 
|  | 295 | std::end(zip_file_extensions), | 
|  | 296 | [&file](const string& ext) { | 
|  | 297 | return base::EndsWith( | 
|  | 298 | file.name, ext, base::CompareCase::INSENSITIVE_ASCII); | 
|  | 299 | }); | 
| Tianjie Xu | 1a7bb2c | 2018-01-22 17:56:57 -0800 | [diff] [blame] | 300 |  | 
| Tianjie Xu | 3959067 | 2018-02-05 17:45:02 -0800 | [diff] [blame] | 301 | if (is_zip && extract_deflates) { | 
| Tianjie Xu | 1a7bb2c | 2018-01-22 17:56:57 -0800 | [diff] [blame] | 302 | brillo::Blob data; | 
|  | 303 | TEST_AND_RETURN_FALSE( | 
|  | 304 | utils::ReadExtents(part.path, | 
|  | 305 | file.extents, | 
|  | 306 | &data, | 
|  | 307 | kBlockSize * utils::BlocksInExtents(file.extents), | 
|  | 308 | kBlockSize)); | 
|  | 309 | std::vector<puffin::BitExtent> deflates_sub_blocks; | 
|  | 310 | TEST_AND_RETURN_FALSE(puffin::LocateDeflateSubBlocksInZipArchive( | 
|  | 311 | data, &deflates_sub_blocks)); | 
|  | 312 | // Shift the deflate's extent to the offset starting from the beginning | 
|  | 313 | // of the current partition; and the delta processor will align the | 
|  | 314 | // extents in a continuous buffer later. | 
|  | 315 | TEST_AND_RETURN_FALSE( | 
|  | 316 | ShiftBitExtentsOverExtents(file.extents, &deflates_sub_blocks)); | 
|  | 317 | file.deflates = std::move(deflates_sub_blocks); | 
|  | 318 | } | 
|  | 319 |  | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 320 | result_files->push_back(file); | 
|  | 321 | } | 
| Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 322 | return true; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | }  // namespace deflate_utils | 
|  | 326 | }  // namespace chromeos_update_engine |