1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef SQUASHFS_FS
3 #define SQUASHFS_FS
4 /*
5  * Squashfs
6  *
7  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
8  * Phillip Lougher <phillip@squashfs.org.uk>
9  *
10  * squashfs_fs.h
11  */
12 
13 #define SQUASHFS_CACHED_FRAGMENTS	CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE
14 #define SQUASHFS_MAJOR			4
15 #define SQUASHFS_MINOR			0
16 #define SQUASHFS_START			0
17 
18 /* size of metadata (inode and directory) blocks */
19 #define SQUASHFS_METADATA_SIZE		8192
20 #define SQUASHFS_BLOCK_OFFSET		2
21 
22 /* default size of block device I/O */
23 #ifdef CONFIG_SQUASHFS_4K_DEVBLK_SIZE
24 #define SQUASHFS_DEVBLK_SIZE 4096
25 #else
26 #define SQUASHFS_DEVBLK_SIZE 1024
27 #endif
28 
29 #define SQUASHFS_FILE_MAX_SIZE		1048576
30 #define SQUASHFS_FILE_MAX_LOG		20
31 
32 /* Max length of filename (not 255) */
33 #define SQUASHFS_NAME_LEN		256
34 
35 /* Max value for directory header count*/
36 #define SQUASHFS_DIR_COUNT		256
37 
38 #define SQUASHFS_INVALID_FRAG		(0xffffffffU)
39 #define SQUASHFS_INVALID_XATTR		(0xffffffffU)
40 #define SQUASHFS_INVALID_BLK		(-1LL)
41 
42 /* Filesystem flags */
43 #define SQUASHFS_NOI			0
44 #define SQUASHFS_NOD			1
45 #define SQUASHFS_NOF			3
46 #define SQUASHFS_NO_FRAG		4
47 #define SQUASHFS_ALWAYS_FRAG		5
48 #define SQUASHFS_DUPLICATE		6
49 #define SQUASHFS_EXPORT			7
50 #define SQUASHFS_COMP_OPT		10
51 
52 #define SQUASHFS_BIT(flag, bit)		((flag >> bit) & 1)
53 
54 #define SQUASHFS_UNCOMPRESSED_INODES(flags)	SQUASHFS_BIT(flags, \
55 						SQUASHFS_NOI)
56 
57 #define SQUASHFS_UNCOMPRESSED_DATA(flags)	SQUASHFS_BIT(flags, \
58 						SQUASHFS_NOD)
59 
60 #define SQUASHFS_UNCOMPRESSED_FRAGMENTS(flags)	SQUASHFS_BIT(flags, \
61 						SQUASHFS_NOF)
62 
63 #define SQUASHFS_NO_FRAGMENTS(flags)		SQUASHFS_BIT(flags, \
64 						SQUASHFS_NO_FRAG)
65 
66 #define SQUASHFS_ALWAYS_FRAGMENTS(flags)	SQUASHFS_BIT(flags, \
67 						SQUASHFS_ALWAYS_FRAG)
68 
69 #define SQUASHFS_DUPLICATES(flags)		SQUASHFS_BIT(flags, \
70 						SQUASHFS_DUPLICATE)
71 
72 #define SQUASHFS_EXPORTABLE(flags)		SQUASHFS_BIT(flags, \
73 						SQUASHFS_EXPORT)
74 
75 #define SQUASHFS_COMP_OPTS(flags)		SQUASHFS_BIT(flags, \
76 						SQUASHFS_COMP_OPT)
77 
78 /* Inode types including extended types */
79 #define SQUASHFS_DIR_TYPE		1
80 #define SQUASHFS_REG_TYPE		2
81 #define SQUASHFS_SYMLINK_TYPE		3
82 #define SQUASHFS_BLKDEV_TYPE		4
83 #define SQUASHFS_CHRDEV_TYPE		5
84 #define SQUASHFS_FIFO_TYPE		6
85 #define SQUASHFS_SOCKET_TYPE		7
86 #define SQUASHFS_LDIR_TYPE		8
87 #define SQUASHFS_LREG_TYPE		9
88 #define SQUASHFS_LSYMLINK_TYPE		10
89 #define SQUASHFS_LBLKDEV_TYPE		11
90 #define SQUASHFS_LCHRDEV_TYPE		12
91 #define SQUASHFS_LFIFO_TYPE		13
92 #define SQUASHFS_LSOCKET_TYPE		14
93 
94 /* Max type value stored in directory entry */
95 #define SQUASHFS_MAX_DIR_TYPE		7
96 
97 /* Xattr types */
98 #define SQUASHFS_XATTR_USER             0
99 #define SQUASHFS_XATTR_TRUSTED          1
100 #define SQUASHFS_XATTR_SECURITY         2
101 #define SQUASHFS_XATTR_VALUE_OOL        256
102 #define SQUASHFS_XATTR_PREFIX_MASK      0xff
103 
104 /* Flag whether block is compressed or uncompressed, bit is set if block is
105  * uncompressed */
106 #define SQUASHFS_COMPRESSED_BIT		(1 << 15)
107 
108 #define SQUASHFS_COMPRESSED_SIZE(B)	(((B) & ~SQUASHFS_COMPRESSED_BIT) ? \
109 		(B) & ~SQUASHFS_COMPRESSED_BIT :  SQUASHFS_COMPRESSED_BIT)
110 
111 #define SQUASHFS_COMPRESSED(B)		(!((B) & SQUASHFS_COMPRESSED_BIT))
112 
113 #define SQUASHFS_COMPRESSED_BIT_BLOCK	(1 << 24)
114 
115 #define SQUASHFS_COMPRESSED_SIZE_BLOCK(B)	((B) & \
116 						~SQUASHFS_COMPRESSED_BIT_BLOCK)
117 
118 #define SQUASHFS_COMPRESSED_BLOCK(B)	(!((B) & SQUASHFS_COMPRESSED_BIT_BLOCK))
119 
squashfs_block_size(__le32 raw)120 static inline int squashfs_block_size(__le32 raw)
121 {
122 	u32 size = le32_to_cpu(raw);
123 	return (size >> 25) ? -EIO : size;
124 }
125 
126 /*
127  * Inode number ops.  Inodes consist of a compressed block number, and an
128  * uncompressed offset within that block
129  */
130 #define SQUASHFS_INODE_BLK(A)		((unsigned int) ((A) >> 16))
131 
132 #define SQUASHFS_INODE_OFFSET(A)	((unsigned int) ((A) & 0xffff))
133 
134 #define SQUASHFS_MKINODE(A, B)		((long long)(((long long) (A)\
135 					<< 16) + (B)))
136 
137 /* fragment and fragment table defines */
138 #define SQUASHFS_FRAGMENT_BYTES(A)	\
139 				((A) * sizeof(struct squashfs_fragment_entry))
140 
141 #define SQUASHFS_FRAGMENT_INDEX(A)	(SQUASHFS_FRAGMENT_BYTES(A) / \
142 					SQUASHFS_METADATA_SIZE)
143 
144 #define SQUASHFS_FRAGMENT_INDEX_OFFSET(A)	(SQUASHFS_FRAGMENT_BYTES(A) % \
145 						SQUASHFS_METADATA_SIZE)
146 
147 #define SQUASHFS_FRAGMENT_INDEXES(A)	((SQUASHFS_FRAGMENT_BYTES(A) + \
148 					SQUASHFS_METADATA_SIZE - 1) / \
149 					SQUASHFS_METADATA_SIZE)
150 
151 #define SQUASHFS_FRAGMENT_INDEX_BYTES(A)	(SQUASHFS_FRAGMENT_INDEXES(A) *\
152 						sizeof(u64))
153 
154 /* inode lookup table defines */
155 #define SQUASHFS_LOOKUP_BYTES(A)	((A) * sizeof(u64))
156 
157 #define SQUASHFS_LOOKUP_BLOCK(A)	(SQUASHFS_LOOKUP_BYTES(A) / \
158 					SQUASHFS_METADATA_SIZE)
159 
160 #define SQUASHFS_LOOKUP_BLOCK_OFFSET(A)	(SQUASHFS_LOOKUP_BYTES(A) % \
161 					SQUASHFS_METADATA_SIZE)
162 
163 #define SQUASHFS_LOOKUP_BLOCKS(A)	((SQUASHFS_LOOKUP_BYTES(A) + \
164 					SQUASHFS_METADATA_SIZE - 1) / \
165 					SQUASHFS_METADATA_SIZE)
166 
167 #define SQUASHFS_LOOKUP_BLOCK_BYTES(A)	(SQUASHFS_LOOKUP_BLOCKS(A) *\
168 					sizeof(u64))
169 
170 /* uid/gid lookup table defines */
171 #define SQUASHFS_ID_BYTES(A)		((A) * sizeof(unsigned int))
172 
173 #define SQUASHFS_ID_BLOCK(A)		(SQUASHFS_ID_BYTES(A) / \
174 					SQUASHFS_METADATA_SIZE)
175 
176 #define SQUASHFS_ID_BLOCK_OFFSET(A)	(SQUASHFS_ID_BYTES(A) % \
177 					SQUASHFS_METADATA_SIZE)
178 
179 #define SQUASHFS_ID_BLOCKS(A)		((SQUASHFS_ID_BYTES(A) + \
180 					SQUASHFS_METADATA_SIZE - 1) / \
181 					SQUASHFS_METADATA_SIZE)
182 
183 #define SQUASHFS_ID_BLOCK_BYTES(A)	(SQUASHFS_ID_BLOCKS(A) *\
184 					sizeof(u64))
185 /* xattr id lookup table defines */
186 #define SQUASHFS_XATTR_BYTES(A)		((A) * sizeof(struct squashfs_xattr_id))
187 
188 #define SQUASHFS_XATTR_BLOCK(A)		(SQUASHFS_XATTR_BYTES(A) / \
189 					SQUASHFS_METADATA_SIZE)
190 
191 #define SQUASHFS_XATTR_BLOCK_OFFSET(A)	(SQUASHFS_XATTR_BYTES(A) % \
192 					SQUASHFS_METADATA_SIZE)
193 
194 #define SQUASHFS_XATTR_BLOCKS(A)	((SQUASHFS_XATTR_BYTES(A) + \
195 					SQUASHFS_METADATA_SIZE - 1) / \
196 					SQUASHFS_METADATA_SIZE)
197 
198 #define SQUASHFS_XATTR_BLOCK_BYTES(A)	(SQUASHFS_XATTR_BLOCKS(A) *\
199 					sizeof(u64))
200 #define SQUASHFS_XATTR_BLK(A)		((unsigned int) ((A) >> 16))
201 
202 #define SQUASHFS_XATTR_OFFSET(A)	((unsigned int) ((A) & 0xffff))
203 
204 /* cached data constants for filesystem */
205 #define SQUASHFS_CACHED_BLKS		8
206 
207 /* meta index cache */
208 #define SQUASHFS_META_INDEXES	(SQUASHFS_METADATA_SIZE / sizeof(unsigned int))
209 #define SQUASHFS_META_ENTRIES	127
210 #define SQUASHFS_META_SLOTS	8
211 
212 struct meta_entry {
213 	u64			data_block;
214 	unsigned int		index_block;
215 	unsigned short		offset;
216 	unsigned short		pad;
217 };
218 
219 struct meta_index {
220 	unsigned int		inode_number;
221 	unsigned int		offset;
222 	unsigned short		entries;
223 	unsigned short		skip;
224 	unsigned short		locked;
225 	unsigned short		pad;
226 	struct meta_entry	meta_entry[SQUASHFS_META_ENTRIES];
227 };
228 
229 
230 /*
231  * definitions for structures on disk
232  */
233 #define ZLIB_COMPRESSION	1
234 #define LZMA_COMPRESSION	2
235 #define LZO_COMPRESSION		3
236 #define XZ_COMPRESSION		4
237 #define LZ4_COMPRESSION		5
238 #define ZSTD_COMPRESSION	6
239 
240 struct squashfs_super_block {
241 	__le32			s_magic;
242 	__le32			inodes;
243 	__le32			mkfs_time;
244 	__le32			block_size;
245 	__le32			fragments;
246 	__le16			compression;
247 	__le16			block_log;
248 	__le16			flags;
249 	__le16			no_ids;
250 	__le16			s_major;
251 	__le16			s_minor;
252 	__le64			root_inode;
253 	__le64			bytes_used;
254 	__le64			id_table_start;
255 	__le64			xattr_id_table_start;
256 	__le64			inode_table_start;
257 	__le64			directory_table_start;
258 	__le64			fragment_table_start;
259 	__le64			lookup_table_start;
260 };
261 
262 struct squashfs_dir_index {
263 	__le32			index;
264 	__le32			start_block;
265 	__le32			size;
266 	unsigned char		name[];
267 };
268 
269 struct squashfs_base_inode {
270 	__le16			inode_type;
271 	__le16			mode;
272 	__le16			uid;
273 	__le16			guid;
274 	__le32			mtime;
275 	__le32			inode_number;
276 };
277 
278 struct squashfs_ipc_inode {
279 	__le16			inode_type;
280 	__le16			mode;
281 	__le16			uid;
282 	__le16			guid;
283 	__le32			mtime;
284 	__le32			inode_number;
285 	__le32			nlink;
286 };
287 
288 struct squashfs_lipc_inode {
289 	__le16			inode_type;
290 	__le16			mode;
291 	__le16			uid;
292 	__le16			guid;
293 	__le32			mtime;
294 	__le32			inode_number;
295 	__le32			nlink;
296 	__le32			xattr;
297 };
298 
299 struct squashfs_dev_inode {
300 	__le16			inode_type;
301 	__le16			mode;
302 	__le16			uid;
303 	__le16			guid;
304 	__le32			mtime;
305 	__le32			inode_number;
306 	__le32			nlink;
307 	__le32			rdev;
308 };
309 
310 struct squashfs_ldev_inode {
311 	__le16			inode_type;
312 	__le16			mode;
313 	__le16			uid;
314 	__le16			guid;
315 	__le32			mtime;
316 	__le32			inode_number;
317 	__le32			nlink;
318 	__le32			rdev;
319 	__le32			xattr;
320 };
321 
322 struct squashfs_symlink_inode {
323 	__le16			inode_type;
324 	__le16			mode;
325 	__le16			uid;
326 	__le16			guid;
327 	__le32			mtime;
328 	__le32			inode_number;
329 	__le32			nlink;
330 	__le32			symlink_size;
331 	char			symlink[];
332 };
333 
334 struct squashfs_reg_inode {
335 	__le16			inode_type;
336 	__le16			mode;
337 	__le16			uid;
338 	__le16			guid;
339 	__le32			mtime;
340 	__le32			inode_number;
341 	__le32			start_block;
342 	__le32			fragment;
343 	__le32			offset;
344 	__le32			file_size;
345 	__le16			block_list[];
346 };
347 
348 struct squashfs_lreg_inode {
349 	__le16			inode_type;
350 	__le16			mode;
351 	__le16			uid;
352 	__le16			guid;
353 	__le32			mtime;
354 	__le32			inode_number;
355 	__le64			start_block;
356 	__le64			file_size;
357 	__le64			sparse;
358 	__le32			nlink;
359 	__le32			fragment;
360 	__le32			offset;
361 	__le32			xattr;
362 	__le16			block_list[];
363 };
364 
365 struct squashfs_dir_inode {
366 	__le16			inode_type;
367 	__le16			mode;
368 	__le16			uid;
369 	__le16			guid;
370 	__le32			mtime;
371 	__le32			inode_number;
372 	__le32			start_block;
373 	__le32			nlink;
374 	__le16			file_size;
375 	__le16			offset;
376 	__le32			parent_inode;
377 };
378 
379 struct squashfs_ldir_inode {
380 	__le16			inode_type;
381 	__le16			mode;
382 	__le16			uid;
383 	__le16			guid;
384 	__le32			mtime;
385 	__le32			inode_number;
386 	__le32			nlink;
387 	__le32			file_size;
388 	__le32			start_block;
389 	__le32			parent_inode;
390 	__le16			i_count;
391 	__le16			offset;
392 	__le32			xattr;
393 	struct squashfs_dir_index	index[];
394 };
395 
396 union squashfs_inode {
397 	struct squashfs_base_inode		base;
398 	struct squashfs_dev_inode		dev;
399 	struct squashfs_ldev_inode		ldev;
400 	struct squashfs_symlink_inode		symlink;
401 	struct squashfs_reg_inode		reg;
402 	struct squashfs_lreg_inode		lreg;
403 	struct squashfs_dir_inode		dir;
404 	struct squashfs_ldir_inode		ldir;
405 	struct squashfs_ipc_inode		ipc;
406 	struct squashfs_lipc_inode		lipc;
407 };
408 
409 struct squashfs_dir_entry {
410 	__le16			offset;
411 	__le16			inode_number;
412 	__le16			type;
413 	__le16			size;
414 	char			name[];
415 };
416 
417 struct squashfs_dir_header {
418 	__le32			count;
419 	__le32			start_block;
420 	__le32			inode_number;
421 };
422 
423 struct squashfs_fragment_entry {
424 	__le64			start_block;
425 	__le32			size;
426 	unsigned int		unused;
427 };
428 
429 struct squashfs_xattr_entry {
430 	__le16			type;
431 	__le16			size;
432 	char			data[];
433 };
434 
435 struct squashfs_xattr_val {
436 	__le32			vsize;
437 	char			value[];
438 };
439 
440 struct squashfs_xattr_id {
441 	__le64			xattr;
442 	__le32			count;
443 	__le32			size;
444 };
445 
446 struct squashfs_xattr_id_table {
447 	__le64			xattr_table_start;
448 	__le32			xattr_ids;
449 	__le32			unused;
450 };
451 
452 #endif
453