squashfs_filesystem.h revision d7da8f4118c10d00d02b6274edd2f88971a6cb22
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
37namespace chromeos_update_engine {
38
39class 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
58  // Creates the file system from the Squashfs file itself.
59  static std::unique_ptr<SquashfsFilesystem> CreateFromFile(
60      const std::string& sqfs_path);
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  // Squashfs image does not support this yet.
91  bool LoadSettings(brillo::KeyValueStore* store) const override;
92
93  // Returns true if the first few bytes of a file indicates a valid Squashfs
94  // image. The size of the |blob| should be at least
95  // sizeof(SquashfsHeader) or for now 96 bytes.
96  static bool IsSquashfsImage(const brillo::Blob& blob);
97
98 private:
99  SquashfsFilesystem() = default;
100
101  // Initialize and populates the files in the file system.
102  bool Init(const std::string& map, size_t size, const SquashfsHeader& header);
103
104  // The size of the image in bytes.
105  size_t size_;
106
107  // All the files in the filesystem.
108  std::vector<File> files_;
109
110  DISALLOW_COPY_AND_ASSIGN(SquashfsFilesystem);
111};
112
113}  // namespace chromeos_update_engine
114#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_SQUASHFS_FILESYSTEM_H_
115