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 // This class implements a FilesystemInterface, which lets the caller obtain 18 // basic information about what files are in the filesystem and where are they 19 // located in the disk, but not full access to the uncompressed contents of 20 // these files. This class uses the definitions found in 21 // fs/squashfs/squashfs_fs.h in the kernel header tree. This class supports 22 // squashfs version 4 in little-endian format only. 23 24 #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_SQUASHFS_FILESYSTEM_H_ 25 #define UPDATE_ENGINE_PAYLOAD_GENERATOR_SQUASHFS_FILESYSTEM_H_ 26 27 #include <memory> 28 #include <string> 29 #include <vector> 30 31 #include <brillo/secure_blob.h> 32 33 #include "update_engine/payload_generator/filesystem_interface.h" 34 35 namespace chromeos_update_engine { 36 37 class SquashfsFilesystem : public FilesystemInterface { 38 public: 39 // From an squashfs image we need: (offset, bytes) 40 // - magic: (0, 4) 41 // * Acceptable value is: 0x73717368 42 // - block size: (12, 4) 43 // - compression type: (20, 2) 44 // * 1 is for zlib, gzip 45 // - major number: (28, 2) 46 // * We only support version 4 for now. 47 struct SquashfsHeader { 48 uint32_t magic; 49 uint32_t block_size; 50 uint16_t compression_type; 51 uint16_t major_version; 52 }; 53 54 ~SquashfsFilesystem() override = default; 55 56 // Creates the file system from the Squashfs file itself. If 57 // |extract_deflates| is true, it will process files to find location of all 58 // deflate streams. 59 static std::unique_ptr<SquashfsFilesystem> CreateFromFile( 60 const std::string& sqfs_path, bool extract_deflates); 61 62 // Creates the file system from a file map |filemap| which is a multi-line 63 // string with each line with the following format: 64 // 65 // file_path start_byte compressed_size_1 ... compressed_size_2 66 // 67 // file_path: The name of the file inside the Squashfs File. 68 // start_byte: The byte address of the start of the file. 69 // compressed_size_i: The compressed size of the ith block of the file. 70 // 71 // The 25th bit of compressed_size_i is set if the block is uncompressed. 72 // |size| is the size of the Squashfs image. 73 static std::unique_ptr<SquashfsFilesystem> CreateFromFileMap( 74 const std::string& filemap, size_t size, const SquashfsHeader& header); 75 76 // FilesystemInterface overrides. 77 size_t GetBlockSize() const override; 78 size_t GetBlockCount() const override; 79 80 // Returns one FilesystemInterface::File for every file (that is not added to 81 // fragments) in the squashfs image. 82 // 83 // It also returns the following metadata files: 84 // <fragment-i>: The ith fragment in the Sqauashfs file. 85 // <metadata-i>: The part of the file system that does not belong to any 86 // file. Normally, there is only two: one for superblock and 87 // one for the metadata at the end. 88 bool GetFiles(std::vector<File>* files) const override; 89 90 // Returns true if the first few bytes of a file indicates a valid Squashfs 91 // image. The size of the |blob| should be at least 92 // sizeof(SquashfsHeader) or for now 96 bytes. 93 static bool IsSquashfsImage(const brillo::Blob& blob); 94 95 private: 96 SquashfsFilesystem() = default; 97 98 // Initialize and populates the files in the file system. 99 bool Init(const std::string& map, 100 const std::string& sqfs_path, 101 size_t size, 102 const SquashfsHeader& header, 103 bool extract_deflates); 104 105 // The size of the image in bytes. 106 size_t size_{}; 107 108 // All the files in the filesystem. 109 std::vector<File> files_; 110 111 DISALLOW_COPY_AND_ASSIGN(SquashfsFilesystem); 112 }; 113 114 } // namespace chromeos_update_engine 115 #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_SQUASHFS_FILESYSTEM_H_ 116