| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -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/squashfs_filesystem.h" | 
|  | 18 |  | 
|  | 19 | #include <fcntl.h> | 
|  | 20 |  | 
|  | 21 | #include <algorithm> | 
|  | 22 | #include <string> | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 23 | #include <utility> | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 24 |  | 
|  | 25 | #include <base/files/file_util.h> | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 26 | #include <base/files/scoped_temp_dir.h> | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 27 | #include <base/logging.h> | 
|  | 28 | #include <base/strings/string_number_conversions.h> | 
|  | 29 | #include <base/strings/string_split.h> | 
|  | 30 | #include <brillo/streams/file_stream.h> | 
|  | 31 |  | 
|  | 32 | #include "update_engine/common/subprocess.h" | 
|  | 33 | #include "update_engine/common/utils.h" | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 34 | #include "update_engine/payload_generator/deflate_utils.h" | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 35 | #include "update_engine/payload_generator/delta_diff_generator.h" | 
|  | 36 | #include "update_engine/payload_generator/extent_ranges.h" | 
|  | 37 | #include "update_engine/payload_generator/extent_utils.h" | 
|  | 38 | #include "update_engine/update_metadata.pb.h" | 
|  | 39 |  | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 40 | using base::FilePath; | 
|  | 41 | using base::ScopedTempDir; | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 42 | using std::string; | 
|  | 43 | using std::unique_ptr; | 
|  | 44 | using std::vector; | 
|  | 45 |  | 
|  | 46 | namespace chromeos_update_engine { | 
|  | 47 |  | 
|  | 48 | namespace { | 
|  | 49 |  | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 50 | // The size of the squashfs super block. | 
|  | 51 | constexpr size_t kSquashfsSuperBlockSize = 96; | 
|  | 52 | constexpr uint64_t kSquashfsCompressedBit = 1 << 24; | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 53 | constexpr uint32_t kSquashfsZlibCompression = 1; | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 54 |  | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 55 | constexpr char kUpdateEngineConf[] = "etc/update_engine.conf"; | 
|  | 56 |  | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 57 | bool ReadSquashfsHeader(const brillo::Blob blob, | 
|  | 58 | SquashfsFilesystem::SquashfsHeader* header) { | 
|  | 59 | if (blob.size() < kSquashfsSuperBlockSize) { | 
|  | 60 | return false; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | memcpy(&header->magic, blob.data(), 4); | 
|  | 64 | memcpy(&header->block_size, blob.data() + 12, 4); | 
|  | 65 | memcpy(&header->compression_type, blob.data() + 20, 2); | 
|  | 66 | memcpy(&header->major_version, blob.data() + 28, 2); | 
|  | 67 | return true; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | bool CheckHeader(const SquashfsFilesystem::SquashfsHeader& header) { | 
|  | 71 | return header.magic == 0x73717368 && header.major_version == 4; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | bool GetFileMapContent(const string& sqfs_path, string* map) { | 
|  | 75 | // Create a tmp file | 
|  | 76 | string map_file; | 
|  | 77 | TEST_AND_RETURN_FALSE( | 
|  | 78 | utils::MakeTempFile("squashfs_file_map.XXXXXX", &map_file, nullptr)); | 
|  | 79 | ScopedPathUnlinker map_unlinker(map_file); | 
|  | 80 |  | 
|  | 81 | // Run unsquashfs to get the system file map. | 
|  | 82 | // unsquashfs -m <map-file> <squashfs-file> | 
|  | 83 | vector<string> cmd = {"unsquashfs", "-m", map_file, sqfs_path}; | 
| Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 84 | string stdout, stderr; | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 85 | int exit_code; | 
| Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 86 | if (!Subprocess::SynchronousExec(cmd, &exit_code, &stdout, &stderr) || | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 87 | exit_code != 0) { | 
| Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 88 | LOG(ERROR) << "Failed to run `unsquashfs -m` with stdout content: " | 
|  | 89 | << stdout << " and stderr content: " << stderr; | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 90 | return false; | 
|  | 91 | } | 
|  | 92 | TEST_AND_RETURN_FALSE(utils::ReadFile(map_file, map)); | 
|  | 93 | return true; | 
|  | 94 | } | 
|  | 95 |  | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 96 | bool GetUpdateEngineConfig(const std::string& sqfs_path, string* config) { | 
|  | 97 | ScopedTempDir unsquash_dir; | 
|  | 98 | if (!unsquash_dir.CreateUniqueTempDir()) { | 
|  | 99 | PLOG(ERROR) << "Failed to create a temporary directory."; | 
|  | 100 | return false; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | // Run unsquashfs to extract update_engine.conf | 
|  | 104 | // -f: To force overriding if the target directory exists. | 
|  | 105 | // -d: The directory to unsquash the files. | 
|  | 106 | vector<string> cmd = {"unsquashfs", | 
|  | 107 | "-f", | 
|  | 108 | "-d", | 
|  | 109 | unsquash_dir.GetPath().value(), | 
|  | 110 | sqfs_path, | 
|  | 111 | kUpdateEngineConf}; | 
| Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 112 | string stdout, stderr; | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 113 | int exit_code; | 
| Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 114 | if (!Subprocess::SynchronousExec(cmd, &exit_code, &stdout, &stderr) || | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 115 | exit_code != 0) { | 
| Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 116 | PLOG(ERROR) << "Failed to unsquashfs etc/update_engine.conf with stdout: " | 
|  | 117 | << stdout << " and stderr: " << stderr; | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 118 | return false; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | auto config_path = unsquash_dir.GetPath().Append(kUpdateEngineConf); | 
|  | 122 | string config_content; | 
|  | 123 | if (!utils::ReadFile(config_path.value(), &config_content)) { | 
|  | 124 | PLOG(ERROR) << "Failed to read " << config_path.value(); | 
|  | 125 | return false; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | if (config_content.empty()) { | 
|  | 129 | LOG(ERROR) << "update_engine config file was empty!!"; | 
|  | 130 | return false; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | *config = std::move(config_content); | 
|  | 134 | return true; | 
|  | 135 | } | 
|  | 136 |  | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 137 | }  // namespace | 
|  | 138 |  | 
|  | 139 | bool SquashfsFilesystem::Init(const string& map, | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 140 | const string& sqfs_path, | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 141 | size_t size, | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 142 | const SquashfsHeader& header, | 
|  | 143 | bool extract_deflates) { | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 144 | size_ = size; | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 145 |  | 
|  | 146 | bool is_zlib = header.compression_type == kSquashfsZlibCompression; | 
|  | 147 | if (!is_zlib) { | 
|  | 148 | LOG(WARNING) << "Filesystem is not Gzipped. Not filling deflates!"; | 
|  | 149 | } | 
|  | 150 | vector<puffin::ByteExtent> zlib_blks; | 
|  | 151 |  | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 152 | // Reading files map. For the format of the file map look at the comments for | 
|  | 153 | // |CreateFromFileMap()|. | 
|  | 154 | auto lines = base::SplitStringPiece(map, | 
|  | 155 | "\n", | 
|  | 156 | base::WhitespaceHandling::KEEP_WHITESPACE, | 
|  | 157 | base::SplitResult::SPLIT_WANT_NONEMPTY); | 
|  | 158 | for (const auto& line : lines) { | 
|  | 159 | auto splits = | 
|  | 160 | base::SplitStringPiece(line, | 
|  | 161 | " \t", | 
|  | 162 | base::WhitespaceHandling::TRIM_WHITESPACE, | 
|  | 163 | base::SplitResult::SPLIT_WANT_NONEMPTY); | 
|  | 164 | // Only filename is invalid. | 
|  | 165 | TEST_AND_RETURN_FALSE(splits.size() > 1); | 
|  | 166 | uint64_t start; | 
|  | 167 | TEST_AND_RETURN_FALSE(base::StringToUint64(splits[1], &start)); | 
|  | 168 | uint64_t cur_offset = start; | 
| Amin Hassani | 1a200c1 | 2020-02-26 14:47:23 -0800 | [diff] [blame] | 169 | bool is_compressed = false; | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 170 | for (size_t i = 2; i < splits.size(); ++i) { | 
|  | 171 | uint64_t blk_size; | 
|  | 172 | TEST_AND_RETURN_FALSE(base::StringToUint64(splits[i], &blk_size)); | 
|  | 173 | // TODO(ahassani): For puffin push it into a proper list if uncompressed. | 
|  | 174 | auto new_blk_size = blk_size & ~kSquashfsCompressedBit; | 
|  | 175 | TEST_AND_RETURN_FALSE(new_blk_size <= header.block_size); | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 176 | if (new_blk_size > 0 && !(blk_size & kSquashfsCompressedBit)) { | 
| Amin Hassani | 1a200c1 | 2020-02-26 14:47:23 -0800 | [diff] [blame] | 177 | // It is a compressed block. | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 178 | if (is_zlib && extract_deflates) { | 
|  | 179 | zlib_blks.emplace_back(cur_offset, new_blk_size); | 
|  | 180 | } | 
| Amin Hassani | 1a200c1 | 2020-02-26 14:47:23 -0800 | [diff] [blame] | 181 | is_compressed = true; | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 182 | } | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 183 | cur_offset += new_blk_size; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | // If size is zero do not add the file. | 
|  | 187 | if (cur_offset - start > 0) { | 
|  | 188 | File file; | 
|  | 189 | file.name = splits[0].as_string(); | 
|  | 190 | file.extents = {ExtentForBytes(kBlockSize, start, cur_offset - start)}; | 
| Amin Hassani | 1a200c1 | 2020-02-26 14:47:23 -0800 | [diff] [blame] | 191 | file.is_compressed = is_compressed; | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 192 | files_.emplace_back(file); | 
|  | 193 | } | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | // Sort all files by their offset in the squashfs. | 
|  | 197 | std::sort(files_.begin(), files_.end(), [](const File& a, const File& b) { | 
|  | 198 | return a.extents[0].start_block() < b.extents[0].start_block(); | 
|  | 199 | }); | 
|  | 200 | // If there is any overlap between two consecutive extents, remove them. Here | 
|  | 201 | // we are assuming all files have exactly one extent. If this assumption | 
|  | 202 | // changes then this implementation needs to change too. | 
| Jae Hoon Kim | 3f894a8 | 2020-05-20 19:26:19 -0700 | [diff] [blame] | 203 | for (auto first = files_.begin(), | 
|  | 204 | second = first + (first == files_.end() ? 0 : 1); | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 205 | first != files_.end() && second != files_.end(); | 
|  | 206 | second = first + 1) { | 
|  | 207 | auto first_begin = first->extents[0].start_block(); | 
|  | 208 | auto first_end = first_begin + first->extents[0].num_blocks(); | 
|  | 209 | auto second_begin = second->extents[0].start_block(); | 
|  | 210 | auto second_end = second_begin + second->extents[0].num_blocks(); | 
|  | 211 | // Remove the first file if the size is zero. | 
|  | 212 | if (first_end == first_begin) { | 
|  | 213 | first = files_.erase(first); | 
|  | 214 | } else if (first_end > second_begin) {  // We found a collision. | 
|  | 215 | if (second_end <= first_end) { | 
|  | 216 | // Second file is inside the first file, remove the second file. | 
|  | 217 | second = files_.erase(second); | 
|  | 218 | } else if (first_begin == second_begin) { | 
|  | 219 | // First file is inside the second file, remove the first file. | 
|  | 220 | first = files_.erase(first); | 
|  | 221 | } else { | 
|  | 222 | // Remove overlapping extents from the first file. | 
|  | 223 | first->extents[0].set_num_blocks(second_begin - first_begin); | 
|  | 224 | ++first; | 
|  | 225 | } | 
|  | 226 | } else { | 
|  | 227 | ++first; | 
|  | 228 | } | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | // Find all the metadata including superblock and add them to the list of | 
|  | 232 | // files. | 
|  | 233 | ExtentRanges file_extents; | 
|  | 234 | for (const auto& file : files_) { | 
|  | 235 | file_extents.AddExtents(file.extents); | 
|  | 236 | } | 
| Sen Jiang | 0a582fb | 2018-06-26 19:27:21 -0700 | [diff] [blame] | 237 | vector<Extent> full = {ExtentForBytes(kBlockSize, 0, size_)}; | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 238 | auto metadata_extents = FilterExtentRanges(full, file_extents); | 
|  | 239 | // For now there should be at most two extents. One for superblock and one for | 
|  | 240 | // metadata at the end. Just create appropriate files with <metadata-i> name. | 
|  | 241 | // We can add all these extents as one metadata too, but that violates the | 
|  | 242 | // contiguous write optimization. | 
|  | 243 | for (size_t i = 0; i < metadata_extents.size(); i++) { | 
|  | 244 | File file; | 
|  | 245 | file.name = "<metadata-" + std::to_string(i) + ">"; | 
|  | 246 | file.extents = {metadata_extents[i]}; | 
|  | 247 | files_.emplace_back(file); | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | // Do one last sort before returning. | 
|  | 251 | std::sort(files_.begin(), files_.end(), [](const File& a, const File& b) { | 
|  | 252 | return a.extents[0].start_block() < b.extents[0].start_block(); | 
|  | 253 | }); | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 254 |  | 
|  | 255 | if (is_zlib && extract_deflates) { | 
|  | 256 | // If it is infact gzipped, then the sqfs_path should be valid to read its | 
|  | 257 | // content. | 
|  | 258 | TEST_AND_RETURN_FALSE(!sqfs_path.empty()); | 
|  | 259 | if (zlib_blks.empty()) { | 
|  | 260 | return true; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | // Sort zlib blocks. | 
|  | 264 | std::sort(zlib_blks.begin(), | 
|  | 265 | zlib_blks.end(), | 
|  | 266 | [](const puffin::ByteExtent& a, const puffin::ByteExtent& b) { | 
|  | 267 | return a.offset < b.offset; | 
|  | 268 | }); | 
|  | 269 |  | 
| Amin Hassani | 5d18505 | 2019-04-23 07:28:30 -0700 | [diff] [blame] | 270 | // Sometimes a squashfs can have a two files that are hard linked. In this | 
|  | 271 | // case both files will have the same starting offset in the image and hence | 
|  | 272 | // the same zlib blocks. So we need to remove these duplicates to eliminate | 
|  | 273 | // further potential probems. As a matter of fact the next statement will | 
|  | 274 | // fail if there are duplicates (there will be overlap between two blocks). | 
|  | 275 | auto last = std::unique(zlib_blks.begin(), zlib_blks.end()); | 
|  | 276 | zlib_blks.erase(last, zlib_blks.end()); | 
|  | 277 |  | 
| Tianjie | e283ce4 | 2020-07-29 11:37:51 -0700 | [diff] [blame] | 278 | // Make sure zlib blocks are not overlapping. | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 279 | auto result = std::adjacent_find( | 
|  | 280 | zlib_blks.begin(), | 
|  | 281 | zlib_blks.end(), | 
|  | 282 | [](const puffin::ByteExtent& a, const puffin::ByteExtent& b) { | 
|  | 283 | return (a.offset + a.length) > b.offset; | 
|  | 284 | }); | 
|  | 285 | TEST_AND_RETURN_FALSE(result == zlib_blks.end()); | 
|  | 286 |  | 
|  | 287 | vector<puffin::BitExtent> deflates; | 
|  | 288 | TEST_AND_RETURN_FALSE( | 
|  | 289 | puffin::LocateDeflatesInZlibBlocks(sqfs_path, zlib_blks, &deflates)); | 
|  | 290 |  | 
|  | 291 | // Add deflates for each file. | 
|  | 292 | for (auto& file : files_) { | 
|  | 293 | file.deflates = deflate_utils::FindDeflates(file.extents, deflates); | 
|  | 294 | } | 
|  | 295 | } | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 296 | return true; | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | unique_ptr<SquashfsFilesystem> SquashfsFilesystem::CreateFromFile( | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 300 | const string& sqfs_path, bool extract_deflates, bool load_settings) { | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 301 | if (sqfs_path.empty()) | 
|  | 302 | return nullptr; | 
|  | 303 |  | 
|  | 304 | brillo::StreamPtr sqfs_file = | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 305 | brillo::FileStream::Open(FilePath(sqfs_path), | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 306 | brillo::Stream::AccessMode::READ, | 
|  | 307 | brillo::FileStream::Disposition::OPEN_EXISTING, | 
|  | 308 | nullptr); | 
|  | 309 | if (!sqfs_file) { | 
|  | 310 | LOG(ERROR) << "Unable to open " << sqfs_path << " for reading."; | 
|  | 311 | return nullptr; | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | SquashfsHeader header; | 
|  | 315 | brillo::Blob blob(kSquashfsSuperBlockSize); | 
|  | 316 | if (!sqfs_file->ReadAllBlocking(blob.data(), blob.size(), nullptr)) { | 
|  | 317 | LOG(ERROR) << "Unable to read from file: " << sqfs_path; | 
|  | 318 | return nullptr; | 
|  | 319 | } | 
|  | 320 | if (!ReadSquashfsHeader(blob, &header) || !CheckHeader(header)) { | 
|  | 321 | // This is not necessary an error. | 
|  | 322 | return nullptr; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | // Read the map file. | 
|  | 326 | string filemap; | 
|  | 327 | if (!GetFileMapContent(sqfs_path, &filemap)) { | 
|  | 328 | LOG(ERROR) << "Failed to produce squashfs map file: " << sqfs_path; | 
|  | 329 | return nullptr; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | unique_ptr<SquashfsFilesystem> sqfs(new SquashfsFilesystem()); | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 333 | if (!sqfs->Init( | 
|  | 334 | filemap, sqfs_path, sqfs_file->GetSize(), header, extract_deflates)) { | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 335 | LOG(ERROR) << "Failed to initialized the Squashfs file system"; | 
|  | 336 | return nullptr; | 
|  | 337 | } | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 338 |  | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 339 | if (load_settings) { | 
|  | 340 | if (!GetUpdateEngineConfig(sqfs_path, &sqfs->update_engine_config_)) { | 
|  | 341 | return nullptr; | 
|  | 342 | } | 
|  | 343 | } | 
|  | 344 |  | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 345 | return sqfs; | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | unique_ptr<SquashfsFilesystem> SquashfsFilesystem::CreateFromFileMap( | 
|  | 349 | const string& filemap, size_t size, const SquashfsHeader& header) { | 
|  | 350 | if (!CheckHeader(header)) { | 
|  | 351 | LOG(ERROR) << "Invalid Squashfs super block!"; | 
|  | 352 | return nullptr; | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | unique_ptr<SquashfsFilesystem> sqfs(new SquashfsFilesystem()); | 
| Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 356 | if (!sqfs->Init(filemap, "", size, header, false)) { | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 357 | LOG(ERROR) << "Failed to initialize the Squashfs file system using filemap"; | 
|  | 358 | return nullptr; | 
|  | 359 | } | 
|  | 360 | // TODO(ahassani): Add a function that initializes the puffin related extents. | 
|  | 361 | return sqfs; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | size_t SquashfsFilesystem::GetBlockSize() const { | 
|  | 365 | return kBlockSize; | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | size_t SquashfsFilesystem::GetBlockCount() const { | 
|  | 369 | return size_ / kBlockSize; | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | bool SquashfsFilesystem::GetFiles(vector<File>* files) const { | 
|  | 373 | files->insert(files->end(), files_.begin(), files_.end()); | 
|  | 374 | return true; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | bool SquashfsFilesystem::LoadSettings(brillo::KeyValueStore* store) const { | 
| Amin Hassani | 77c25fc | 2019-01-29 10:24:19 -0800 | [diff] [blame] | 378 | if (!store->LoadFromString(update_engine_config_)) { | 
|  | 379 | LOG(ERROR) << "Failed to load the settings with config: " | 
|  | 380 | << update_engine_config_; | 
|  | 381 | return false; | 
|  | 382 | } | 
|  | 383 | return true; | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 384 | } | 
|  | 385 |  | 
|  | 386 | bool SquashfsFilesystem::IsSquashfsImage(const brillo::Blob& blob) { | 
|  | 387 | SquashfsHeader header; | 
|  | 388 | return ReadSquashfsHeader(blob, &header) && CheckHeader(header); | 
|  | 389 | } | 
| Amin Hassani | d7da8f4 | 2017-08-23 14:29:40 -0700 | [diff] [blame] | 390 | }  // namespace chromeos_update_engine |