sqfs_filesystem.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020 Bootlin
  4. *
  5. * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
  6. */
  7. #ifndef SQFS_FILESYSTEM_H
  8. #define SQFS_FILESYSTEM_H
  9. #include <asm/unaligned.h>
  10. #include <stdint.h>
  11. #include <fs.h>
  12. #define SQFS_UNCOMPRESSED_DATA 0x0002
  13. #define SQFS_MAGIC_NUMBER 0x73717368
  14. /* The three first members of squashfs_dir_index make a total of 12 bytes */
  15. #define SQFS_DIR_INDEX_BASE_LENGTH 12
  16. /* size of metadata (inode and directory) blocks */
  17. #define SQFS_METADATA_BLOCK_SIZE 8192
  18. /* Max. number of fragment entries in a metadata block is 512 */
  19. #define SQFS_MAX_ENTRIES 512
  20. /* Metadata blocks start by a 2-byte length header */
  21. #define SQFS_HEADER_SIZE 2
  22. #define SQFS_LREG_INODE_MIN_SIZE 56
  23. #define SQFS_DIR_HEADER_SIZE 12
  24. #define SQFS_MISC_ENTRY_TYPE -1
  25. #define SQFS_EMPTY_FILE_SIZE 3
  26. #define SQFS_STOP_READDIR 1
  27. #define SQFS_EMPTY_DIR -1
  28. /*
  29. * A directory entry object has a fixed length of 8 bytes, corresponding to its
  30. * first four members, plus the size of the entry name, which is equal to
  31. * 'entry_name' + 1 bytes.
  32. */
  33. #define SQFS_ENTRY_BASE_LENGTH 8
  34. /* Inode types */
  35. #define SQFS_DIR_TYPE 1
  36. #define SQFS_REG_TYPE 2
  37. #define SQFS_SYMLINK_TYPE 3
  38. #define SQFS_BLKDEV_TYPE 4
  39. #define SQFS_CHRDEV_TYPE 5
  40. #define SQFS_FIFO_TYPE 6
  41. #define SQFS_SOCKET_TYPE 7
  42. #define SQFS_LDIR_TYPE 8
  43. #define SQFS_LREG_TYPE 9
  44. #define SQFS_LSYMLINK_TYPE 10
  45. #define SQFS_LBLKDEV_TYPE 11
  46. #define SQFS_LCHRDEV_TYPE 12
  47. #define SQFS_LFIFO_TYPE 13
  48. #define SQFS_LSOCKET_TYPE 14
  49. struct squashfs_super_block {
  50. __le32 s_magic;
  51. __le32 inodes;
  52. __le32 mkfs_time;
  53. __le32 block_size;
  54. __le32 fragments;
  55. __le16 compression;
  56. __le16 block_log;
  57. __le16 flags;
  58. __le16 no_ids;
  59. __le16 s_major;
  60. __le16 s_minor;
  61. __le64 root_inode;
  62. __le64 bytes_used;
  63. __le64 id_table_start;
  64. __le64 xattr_id_table_start;
  65. __le64 inode_table_start;
  66. __le64 directory_table_start;
  67. __le64 fragment_table_start;
  68. __le64 export_table_start;
  69. };
  70. struct squashfs_directory_index {
  71. u32 index;
  72. u32 start;
  73. u32 size;
  74. char name[0];
  75. };
  76. struct squashfs_base_inode {
  77. __le16 inode_type;
  78. __le16 mode;
  79. __le16 uid;
  80. __le16 guid;
  81. __le32 mtime;
  82. __le32 inode_number;
  83. };
  84. struct squashfs_ipc_inode {
  85. __le16 inode_type;
  86. __le16 mode;
  87. __le16 uid;
  88. __le16 guid;
  89. __le32 mtime;
  90. __le32 inode_number;
  91. __le32 nlink;
  92. };
  93. struct squashfs_lipc_inode {
  94. __le16 inode_type;
  95. __le16 mode;
  96. __le16 uid;
  97. __le16 guid;
  98. __le32 mtime;
  99. __le32 inode_number;
  100. __le32 nlink;
  101. __le32 xattr;
  102. };
  103. struct squashfs_dev_inode {
  104. __le16 inode_type;
  105. __le16 mode;
  106. __le16 uid;
  107. __le16 guid;
  108. __le32 mtime;
  109. __le32 inode_number;
  110. __le32 nlink;
  111. __le32 rdev;
  112. };
  113. struct squashfs_ldev_inode {
  114. __le16 inode_type;
  115. __le16 mode;
  116. __le16 uid;
  117. __le16 guid;
  118. __le32 mtime;
  119. __le32 inode_number;
  120. __le32 nlink;
  121. __le32 rdev;
  122. __le32 xattr;
  123. };
  124. struct squashfs_symlink_inode {
  125. __le16 inode_type;
  126. __le16 mode;
  127. __le16 uid;
  128. __le16 guid;
  129. __le32 mtime;
  130. __le32 inode_number;
  131. __le32 nlink;
  132. __le32 symlink_size;
  133. char symlink[0];
  134. };
  135. struct squashfs_reg_inode {
  136. __le16 inode_type;
  137. __le16 mode;
  138. __le16 uid;
  139. __le16 guid;
  140. __le32 mtime;
  141. __le32 inode_number;
  142. __le32 start_block;
  143. __le32 fragment;
  144. __le32 offset;
  145. __le32 file_size;
  146. __le32 block_list[0];
  147. };
  148. struct squashfs_lreg_inode {
  149. __le16 inode_type;
  150. __le16 mode;
  151. __le16 uid;
  152. __le16 guid;
  153. __le32 mtime;
  154. __le32 inode_number;
  155. __le64 start_block;
  156. __le64 file_size;
  157. __le64 sparse;
  158. __le32 nlink;
  159. __le32 fragment;
  160. __le32 offset;
  161. __le32 xattr;
  162. __le32 block_list[0];
  163. };
  164. struct squashfs_dir_inode {
  165. __le16 inode_type;
  166. __le16 mode;
  167. __le16 uid;
  168. __le16 guid;
  169. __le32 mtime;
  170. __le32 inode_number;
  171. __le32 start_block;
  172. __le32 nlink;
  173. __le16 file_size;
  174. __le16 offset;
  175. __le32 parent_inode;
  176. };
  177. struct squashfs_ldir_inode {
  178. __le16 inode_type;
  179. __le16 mode;
  180. __le16 uid;
  181. __le16 guid;
  182. __le32 mtime;
  183. __le32 inode_number;
  184. __le32 nlink;
  185. __le32 file_size;
  186. __le32 start_block;
  187. __le32 parent_inode;
  188. __le16 i_count;
  189. __le16 offset;
  190. __le32 xattr;
  191. struct squashfs_directory_index index[0];
  192. };
  193. union squashfs_inode {
  194. struct squashfs_base_inode *base;
  195. struct squashfs_dev_inode *dev;
  196. struct squashfs_ldev_inode *ldev;
  197. struct squashfs_symlink_inode *symlink;
  198. struct squashfs_reg_inode *reg;
  199. struct squashfs_lreg_inode *lreg;
  200. struct squashfs_dir_inode *dir;
  201. struct squashfs_ldir_inode *ldir;
  202. struct squashfs_ipc_inode *ipc;
  203. struct squashfs_lipc_inode *lipc;
  204. };
  205. struct squashfs_directory_entry {
  206. u16 offset;
  207. u16 inode_offset;
  208. u16 type;
  209. u16 name_size;
  210. char name[0];
  211. };
  212. struct squashfs_directory_header {
  213. u32 count;
  214. u32 start;
  215. u32 inode_number;
  216. };
  217. struct squashfs_fragment_block_entry {
  218. u64 start;
  219. u32 size;
  220. u32 _unused;
  221. };
  222. struct squashfs_dir_stream {
  223. struct fs_dir_stream fs_dirs;
  224. struct fs_dirent dentp;
  225. /*
  226. * 'size' is the uncompressed size of the entire listing, including
  227. * headers. 'entry_count' is the number of entries following a
  228. * specific header. Both variables are decremented in sqfs_readdir() so
  229. * the function knows when the end of the directory is reached.
  230. */
  231. size_t size;
  232. int entry_count;
  233. /* SquashFS structures */
  234. struct squashfs_directory_header *dir_header;
  235. struct squashfs_directory_entry *entry;
  236. /*
  237. * 'table' points to a position into the directory table. Both 'table'
  238. * and 'inode' are defined for the first time in sqfs_opendir().
  239. * 'table's value changes in sqfs_readdir().
  240. */
  241. unsigned char *table;
  242. union squashfs_inode i;
  243. struct squashfs_dir_inode i_dir;
  244. struct squashfs_ldir_inode i_ldir;
  245. /*
  246. * References to the tables' beginnings. They are assigned in
  247. * sqfs_opendir() and freed in sqfs_closedir().
  248. */
  249. unsigned char *inode_table;
  250. unsigned char *dir_table;
  251. };
  252. struct squashfs_file_info {
  253. /* File size in bytes (uncompressed) */
  254. size_t size;
  255. /* Reference to list of data blocks's sizes */
  256. u32 *blk_sizes;
  257. /* Offset into the fragment block */
  258. u32 offset;
  259. /* Offset in which the data blocks begin */
  260. u64 start;
  261. /* Is file fragmented? */
  262. bool frag;
  263. /* Compressed fragment */
  264. bool comp;
  265. };
  266. void *sqfs_find_inode(void *inode_table, int inode_number, __le32 inode_count,
  267. __le32 block_size);
  268. int sqfs_dir_offset(void *dir_i, u32 *m_list, int m_count);
  269. int sqfs_read_metablock(unsigned char *file_mapping, int offset,
  270. bool *compressed, u32 *data_size);
  271. bool sqfs_is_empty_dir(void *dir_i);
  272. bool sqfs_is_dir(u16 type);
  273. #endif /* SQFS_FILESYSTEM_H */