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