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