summaryrefslogtreecommitdiff
path: root/fs/squashfs/sqfs_filesystem.h
blob: ff2b0b1d34eccb8a8536f695c22778fcd0ccde01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (C) 2020 Bootlin
 *
 * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
 */

#ifndef SQFS_FILESYSTEM_H
#define SQFS_FILESYSTEM_H

#include <asm/unaligned.h>
#include <fs.h>
#include <part.h>
#include <stdint.h>

#define SQFS_UNCOMPRESSED_DATA 0x0002
#define SQFS_MAGIC_NUMBER 0x73717368
/* The three first members of squashfs_dir_index make a total of 12 bytes */
#define SQFS_DIR_INDEX_BASE_LENGTH 12
/* size of metadata (inode and directory) blocks */
#define SQFS_METADATA_BLOCK_SIZE 8192
/* Max. number of fragment entries in a metadata block is 512 */
#define SQFS_MAX_ENTRIES 512
/* Metadata blocks start by a 2-byte length header */
#define SQFS_HEADER_SIZE 2
#define SQFS_LREG_INODE_MIN_SIZE 56
#define SQFS_DIR_HEADER_SIZE 12
#define SQFS_MISC_ENTRY_TYPE -1
#define SQFS_EMPTY_FILE_SIZE 3
#define SQFS_STOP_READDIR 1
#define SQFS_EMPTY_DIR -1
/*
 * A directory entry object has a fixed length of 8 bytes, corresponding to its
 * first four members, plus the size of the entry name, which is equal to
 * 'entry_name' + 1 bytes.
 */
#define SQFS_ENTRY_BASE_LENGTH 8
/* Inode types */
#define SQFS_DIR_TYPE 1
#define SQFS_REG_TYPE 2
#define SQFS_SYMLINK_TYPE 3
#define SQFS_BLKDEV_TYPE 4
#define SQFS_CHRDEV_TYPE 5
#define SQFS_FIFO_TYPE 6
#define SQFS_SOCKET_TYPE 7
#define SQFS_LDIR_TYPE 8
#define SQFS_LREG_TYPE 9
#define SQFS_LSYMLINK_TYPE 10
#define SQFS_LBLKDEV_TYPE 11
#define SQFS_LCHRDEV_TYPE 12
#define SQFS_LFIFO_TYPE 13
#define SQFS_LSOCKET_TYPE 14

struct squashfs_super_block {
	__le32 s_magic;
	__le32 inodes;
	__le32 mkfs_time;
	__le32 block_size;
	__le32 fragments;
	__le16 compression;
	__le16 block_log;
	__le16 flags;
	__le16 no_ids;
	__le16 s_major;
	__le16 s_minor;
	__le64 root_inode;
	__le64 bytes_used;
	__le64 id_table_start;
	__le64 xattr_id_table_start;
	__le64 inode_table_start;
	__le64 directory_table_start;
	__le64 fragment_table_start;
	__le64 export_table_start;
};

struct squashfs_ctxt {
	struct disk_partition cur_part_info;
	struct blk_desc *cur_dev;
	struct squashfs_super_block *sblk;
};

struct squashfs_directory_index {
	u32 index;
	u32 start;
	u32 size;
	char name[0];
};

struct squashfs_base_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
};

struct squashfs_ipc_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le32 nlink;
};

struct squashfs_lipc_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le32 nlink;
	__le32 xattr;
};

struct squashfs_dev_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le32 nlink;
	__le32 rdev;
};

struct squashfs_ldev_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le32 nlink;
	__le32 rdev;
	__le32 xattr;
};

struct squashfs_symlink_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le32 nlink;
	__le32 symlink_size;
	char symlink[0];
};

struct squashfs_reg_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le32 start_block;
	__le32 fragment;
	__le32 offset;
	__le32 file_size;
	__le32 block_list[0];
};

struct squashfs_lreg_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le64 start_block;
	__le64 file_size;
	__le64 sparse;
	__le32 nlink;
	__le32 fragment;
	__le32 offset;
	__le32 xattr;
	__le32 block_list[0];
};

struct squashfs_dir_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le32 start_block;
	__le32 nlink;
	__le16 file_size;
	__le16 offset;
	__le32 parent_inode;
};

struct squashfs_ldir_inode {
	__le16 inode_type;
	__le16 mode;
	__le16 uid;
	__le16 guid;
	__le32 mtime;
	__le32 inode_number;
	__le32 nlink;
	__le32 file_size;
	__le32 start_block;
	__le32 parent_inode;
	__le16 i_count;
	__le16 offset;
	__le32 xattr;
	struct squashfs_directory_index index[0];
};

union squashfs_inode {
	struct squashfs_base_inode *base;
	struct squashfs_dev_inode *dev;
	struct squashfs_ldev_inode *ldev;
	struct squashfs_symlink_inode *symlink;
	struct squashfs_reg_inode *reg;
	struct squashfs_lreg_inode *lreg;
	struct squashfs_dir_inode *dir;
	struct squashfs_ldir_inode *ldir;
	struct squashfs_ipc_inode *ipc;
	struct squashfs_lipc_inode *lipc;
};

struct squashfs_directory_entry {
	u16 offset;
	u16 inode_offset;
	u16 type;
	u16 name_size;
	char name[0];
};

struct squashfs_directory_header {
	u32 count;
	u32 start;
	u32 inode_number;
};

struct squashfs_fragment_block_entry {
	u64 start;
	u32 size;
	u32 _unused;
};

struct squashfs_dir_stream {
	struct fs_dir_stream fs_dirs;
	struct fs_dirent dentp;
	/*
	 * 'size' is the uncompressed size of the entire listing, including
	 * headers. 'entry_count' is the number of entries following a
	 * specific header. Both variables are decremented in sqfs_readdir() so
	 * the function knows when the end of the directory is reached.
	 */
	size_t size;
	int entry_count;
	/* SquashFS structures */
	struct squashfs_directory_header *dir_header;
	struct squashfs_directory_entry *entry;
	/*
	 * 'table' points to a position into the directory table. Both 'table'
	 * and 'inode' are defined for the first time in sqfs_opendir().
	 * 'table's value changes in sqfs_readdir().
	 */
	unsigned char *table;
	union squashfs_inode i;
	struct squashfs_dir_inode i_dir;
	struct squashfs_ldir_inode i_ldir;
	/*
	 * References to the tables' beginnings. They are assigned in
	 * sqfs_opendir() and freed in sqfs_closedir().
	 */
	unsigned char *inode_table;
	unsigned char *dir_table;
};

struct squashfs_file_info {
	/* File size in bytes (uncompressed) */
	size_t size;
	/* Reference to list of data blocks's sizes */
	u32 *blk_sizes;
	/* Offset into the fragment block */
	u32 offset;
	/* Offset in which the data blocks begin */
	u64 start;
	/* Is file fragmented? */
	bool frag;
	/* Compressed fragment */
	bool comp;
};

void *sqfs_find_inode(void *inode_table, int inode_number, __le32 inode_count,
		      __le32 block_size);

int sqfs_dir_offset(void *dir_i, u32 *m_list, int m_count);

int sqfs_read_metablock(unsigned char *file_mapping, int offset,
			bool *compressed, u32 *data_size);

bool sqfs_is_empty_dir(void *dir_i);

bool sqfs_is_dir(u16 type);

#endif /* SQFS_FILESYSTEM_H */