2 * Read a squashfs filesystem. This is a highly compressed read only filesystem.
4 * Copyright (c) 2002, 2003, 2004 Phillip Lougher <plougher@users.sourceforge.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 extern void read_bytes(int, unsigned int, int, char *);
24 extern int add_file(int, int, unsigned int *, int, unsigned int, int, int);
29 #include <sys/types.h>
39 #include <squashfs_fs.h>
44 #define TRACE(s, args...) printf("mksquashfs: "s, ## args)
46 #define TRACE(s, args...)
49 #define ERROR(s, args...) fprintf(stderr, s, ## args)
53 int read_block(int fd, int start, int *next, unsigned char *block, squashfs_super_block *sBlk)
55 unsigned short c_byte;
59 read_bytes(fd, start, 2, block);
60 ((unsigned char *) &c_byte)[1] = block[0];
61 ((unsigned char *) &c_byte)[0] = block[1];
63 read_bytes(fd, start, 2, (char *)&c_byte);
65 if(SQUASHFS_CHECK_DATA(sBlk->flags))
67 if(SQUASHFS_COMPRESSED(c_byte)) {
68 unsigned char buffer[SQUASHFS_METADATA_SIZE];
70 long bytes = SQUASHFS_METADATA_SIZE;
72 c_byte = SQUASHFS_COMPRESSED_SIZE(c_byte);
73 read_bytes(fd, start + offset, c_byte, buffer);
75 if((res = uncompress(block, &bytes, (const char *) buffer, c_byte)) != Z_OK) {
76 if(res == Z_MEM_ERROR)
77 ERROR("zlib::uncompress failed, not enough memory\n");
78 else if(res == Z_BUF_ERROR)
79 ERROR("zlib::uncompress failed, not enough room in output buffer\n");
81 ERROR("zlib::uncompress failed, unknown error %d\n", res);
85 *next = start + offset + c_byte;
88 c_byte = SQUASHFS_COMPRESSED_SIZE(c_byte);
89 read_bytes(fd, start + offset, c_byte, block);
91 *next = start + offset + c_byte;
97 int scan_inode_table(int fd, int start, int end, int root_inode_start, squashfs_super_block *sBlk,
98 squashfs_dir_inode_header *dir_inode, unsigned char **inode_table, unsigned int *root_inode_block,
99 unsigned int *uncompressed_file, unsigned int *uncompressed_directory, int *file_count, int *sym_count,
100 int *dev_count, int *dir_count, int *fifo_count, int *sock_count)
102 unsigned char *cur_ptr;
103 int bytes = 0, size = 0, files = 0;
104 squashfs_reg_inode_header inode;
105 unsigned int directory_start_block;
107 TRACE("scan_inode_table: start 0x%x, end 0x%x, root_inode_start 0x%x\n", start, end, root_inode_start);
109 if(start == root_inode_start) {
110 TRACE("scan_inode_table: read compressed block 0x%x containing root inode\n", start);
111 *root_inode_block = bytes;
113 if((size - bytes < SQUASHFS_METADATA_SIZE) &&
114 ((*inode_table = realloc(*inode_table, size += SQUASHFS_METADATA_SIZE)) == NULL))
116 TRACE("scan_inode_table: reading block 0x%x\n", start);
117 if((bytes += read_block(fd, start, &start, *inode_table + bytes, sBlk)) == 0) {
124 * Read last inode entry which is the root directory inode, and obtain the last
125 * directory start block index. This is used when calculating the total uncompressed
126 * directory size. The directory bytes in the last block will be counted as normal.
128 * The root inode is ignored in the inode scan. This ensures there is
129 * always enough bytes left to read a regular file inode entry
131 bytes -= sizeof(squashfs_dir_inode_header);
133 squashfs_dir_inode_header sinode;
134 memcpy((void *)&sinode, (void *)(*inode_table + bytes), sizeof(*dir_inode));
135 SQUASHFS_SWAP_DIR_INODE_HEADER(dir_inode, &sinode);
137 memcpy((void *)dir_inode, (void *)(*inode_table + bytes), sizeof(*dir_inode));
138 directory_start_block = dir_inode->start_block;
140 for(cur_ptr = *inode_table; cur_ptr < *inode_table + bytes; files ++) {
142 squashfs_reg_inode_header sinode;
143 memcpy((void *)&sinode, (void *)cur_ptr, sizeof(inode));
144 SQUASHFS_SWAP_REG_INODE_HEADER(&inode, &sinode);
146 memcpy((void *)&inode, (void *)cur_ptr, sizeof(inode));
148 TRACE("scan_inode_table: processing inode @ byte position 0x%x, type 0x%x\n", cur_ptr - *inode_table,
150 switch(inode.inode_type) {
151 case SQUASHFS_FILE_TYPE: {
152 int frag_bytes = inode.fragment == SQUASHFS_INVALID_BLK ? 0 : inode.file_size % sBlk->block_size;
153 int blocks = inode.fragment == SQUASHFS_INVALID_BLK ? (inode.file_size
154 + sBlk->block_size - 1) >> sBlk->block_log : inode.file_size >>
156 int file_bytes = 0, i, start = inode.start_block;
157 unsigned int block_list[blocks];
159 TRACE("scan_inode_table: regular file, file_size %d, blocks %d\n", inode.file_size, blocks);
161 cur_ptr += sizeof(inode);
163 unsigned int sblock_list[blocks];
164 memcpy((void *)sblock_list, (void *)cur_ptr, blocks * sizeof(unsigned int));
165 SQUASHFS_SWAP_INTS(block_list, sblock_list, blocks);
167 memcpy((void *)block_list, (void *)cur_ptr, blocks * sizeof(unsigned int));
169 *uncompressed_file += inode.file_size;
172 for(i = 0; i < blocks; i++)
173 file_bytes += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]);
175 add_file(start, file_bytes, block_list, blocks, inode.fragment, inode.offset, frag_bytes);
176 cur_ptr += blocks * sizeof(unsigned int);
179 case SQUASHFS_SYMLINK_TYPE: {
180 squashfs_symlink_inode_header inodep;
183 squashfs_symlink_inode_header sinodep;
184 memcpy((void *)&sinodep, (void *)cur_ptr, sizeof(sinodep));
185 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(&inodep, &sinodep);
187 memcpy((void *)&inodep, (void *)cur_ptr, sizeof(inodep));
189 cur_ptr += sizeof(inodep) + inodep.symlink_size;
192 case SQUASHFS_DIR_TYPE: {
193 squashfs_dir_inode_header dir_inode;
196 squashfs_dir_inode_header sinode;
197 memcpy((void *)&sinode, (void *)cur_ptr, sizeof(dir_inode));
198 SQUASHFS_SWAP_DIR_INODE_HEADER(&dir_inode, &sinode);
200 memcpy((void *)&dir_inode, (void *)cur_ptr, sizeof(dir_inode));
201 if(dir_inode.start_block < directory_start_block)
202 *uncompressed_directory += dir_inode.file_size;
204 cur_ptr += sizeof(squashfs_dir_inode_header);
207 case SQUASHFS_BLKDEV_TYPE:
208 case SQUASHFS_CHRDEV_TYPE:
210 cur_ptr += sizeof(squashfs_dev_inode_header);
213 case SQUASHFS_FIFO_TYPE:
215 cur_ptr += sizeof(squashfs_ipc_inode_header);
217 case SQUASHFS_SOCKET_TYPE:
219 cur_ptr += sizeof(squashfs_ipc_inode_header);
222 ERROR("Unknown inode type %d in scan_inode_table!\n", inode.inode_type);
232 int read_super(int fd, squashfs_super_block *sBlk, int *be, char *source)
234 read_bytes(fd, SQUASHFS_START, sizeof(squashfs_super_block), (char *) sBlk);
236 /* Check it is a SQUASHFS superblock */
238 if(sBlk->s_magic != SQUASHFS_MAGIC) {
239 if(sBlk->s_magic == SQUASHFS_MAGIC_SWAP) {
240 squashfs_super_block sblk;
241 ERROR("Reading a different endian SQUASHFS filesystem on %s - ignoring -le/-be options\n", source);
242 SQUASHFS_SWAP_SUPER_BLOCK(&sblk, sBlk);
243 memcpy(sBlk, &sblk, sizeof(squashfs_super_block));
246 ERROR("Can't find a SQUASHFS superblock on %s\n", source);
251 /* Check the MAJOR & MINOR versions */
252 if(sBlk->s_major != SQUASHFS_MAJOR || sBlk->s_minor > SQUASHFS_MINOR) {
253 if(sBlk->s_major == 1)
254 ERROR("Filesystem on %s is a SQUASHFS 1.x filesystem. Appending\nto SQUASHFS 1.x filesystems is not supported. Please convert it to a SQUASHFS 2.0 filesystem...n", source);
256 ERROR("Major/Minor mismatch, filesystem on %s is (%d:%d), I support (%d: <= %d)\n",
257 source, sBlk->s_major, sBlk->s_minor, SQUASHFS_MAJOR, SQUASHFS_MINOR);
261 #if __BYTE_ORDER == __BIG_ENDIAN
267 printf("Found a valid SQUASHFS superblock on %s.\n", source);
268 printf("\tInodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(sBlk->flags) ? "un" : "");
269 printf("\tData is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(sBlk->flags) ? "un" : "");
270 printf("\tFragments are %scompressed\n", SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk->flags) ? "un" : "");
271 printf("\tCheck data is %s present in the filesystem\n", SQUASHFS_CHECK_DATA(sBlk->flags) ? "" : "not");
272 printf("\tFragments are %s present in the filesystem\n", SQUASHFS_NO_FRAGMENTS(sBlk->flags) ? "not" : "");
273 printf("\tAlways_use_fragments option is %s specified\n", SQUASHFS_ALWAYS_FRAGMENTS(sBlk->flags) ? "" : "not");
274 printf("\tDuplicates are %s removed\n", SQUASHFS_DUPLICATES(sBlk->flags) ? "" : "not");
275 printf("\tFilesystem size %d bytes\n", sBlk->bytes_used);
276 printf("\tBlock size %d\n", sBlk->block_size);
277 printf("\tNumber of fragments %d\n", sBlk->fragments);
278 printf("\tNumber of inodes %d\n", sBlk->inodes);
279 printf("\tNumber of uids %d\n", sBlk->no_uids);
280 printf("\tNumber of gids %d\n", sBlk->no_guids);
281 TRACE("sBlk->inode_table_start %x\n", sBlk->inode_table_start);
282 TRACE("sBlk->directory_table_start %x\n", sBlk->directory_table_start);
283 TRACE("sBlk->uid_start %x\n", sBlk->uid_start);
284 TRACE("sBlk->fragment_table_start %x\n", sBlk->fragment_table_start);
294 unsigned char *squashfs_readdir(int fd, int root_entries, int start, int offset, int size, squashfs_super_block *sBlk,
295 void (push_directory_entry)(char *, squashfs_inode, int))
297 squashfs_dir_header dirh;
298 char buffer[sizeof(squashfs_dir_entry) + SQUASHFS_NAME_LEN + 1];
299 squashfs_dir_entry *dire = (squashfs_dir_entry *) buffer;
300 unsigned char *directory_table = NULL;
301 int bytes = 0, dir_count;
304 if((directory_table = malloc((size + SQUASHFS_METADATA_SIZE * 2 - 1) & ~(SQUASHFS_METADATA_SIZE - 1))) == NULL)
306 while(bytes < size) {
307 TRACE("squashfs_readdir: reading block 0x%x, bytes read so far %d\n", start, bytes);
308 if((bytes += read_block(fd, start, &start, directory_table + bytes, sBlk)) == 0) {
309 free(directory_table);
318 while(bytes < size) {
320 squashfs_dir_header sdirh;
321 memcpy((void *)&sdirh, directory_table + bytes, sizeof(sdirh));
322 SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
324 memcpy((void *)&dirh, directory_table + bytes, sizeof(dirh));
326 dir_count = dirh.count + 1;
327 TRACE("squashfs_readdir: Read directory header @ byte position 0x%x, 0x%x directory entries\n", bytes, dir_count);
328 bytes += sizeof(dirh);
332 squashfs_dir_entry sdire;
333 memcpy((void *)&sdire, directory_table + bytes, sizeof(sdire));
334 SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
336 memcpy((void *)dire, directory_table + bytes, sizeof(dire));
337 bytes += sizeof(*dire);
339 memcpy((void *)dire->name, directory_table + bytes, dire->size + 1);
340 dire->name[dire->size + 1] = '\0';
341 TRACE("squashfs_readdir: pushing directory entry %s, inode %x:%x, type 0x%x\n", dire->name, dirh.start_block, dire->offset, dire->type);
342 push_directory_entry(dire->name, SQUASHFS_MKINODE(dirh.start_block, dire->offset), dire->type);
343 bytes += dire->size + 1;
348 return directory_table;
352 int read_fragment_table(int fd, squashfs_super_block *sBlk, squashfs_fragment_entry **fragment_table)
354 int i, indexes = SQUASHFS_FRAGMENT_INDEXES(sBlk->fragments);
355 squashfs_fragment_index fragment_table_index[indexes];
357 TRACE("read_fragment_table: %d fragments, reading %d fragment indexes from 0x%x\n", sBlk->fragments, indexes, sBlk->fragment_table_start);
358 if(sBlk->fragments == 0)
361 if((*fragment_table = (squashfs_fragment_entry *) malloc(sBlk->fragments * sizeof(squashfs_fragment_entry))) == NULL) {
362 ERROR("Failed to allocate fragment table\n");
367 squashfs_fragment_index sfragment_table_index[indexes];
369 read_bytes(fd, sBlk->fragment_table_start, SQUASHFS_FRAGMENT_INDEX_BYTES(sBlk->fragments), (char *) sfragment_table_index);
370 SQUASHFS_SWAP_FRAGMENT_INDEXES(fragment_table_index, sfragment_table_index, indexes);
372 read_bytes(fd, sBlk->fragment_table_start, SQUASHFS_FRAGMENT_INDEX_BYTES(sBlk->fragments), (char *) fragment_table_index);
374 for(i = 0; i < indexes; i++) {
375 int length = read_block(fd, fragment_table_index[i], NULL, ((char *) *fragment_table) + (i * SQUASHFS_METADATA_SIZE), sBlk);
376 TRACE("Read fragment table block %d, from 0x%x, length %d\n", i, fragment_table_index[i], length);
381 squashfs_fragment_entry sfragment;
382 for(i = 0; i < sBlk->fragments; i++) {
383 SQUASHFS_SWAP_FRAGMENT_ENTRY((&sfragment), (&(*fragment_table)[i]));
384 memcpy((char *) &(*fragment_table)[i], (char *) &sfragment, sizeof(squashfs_fragment_entry));
392 int read_filesystem(char *root_name, int fd, squashfs_super_block *sBlk, char **cinode_table, int *inode_bytes,
393 char **data_cache, int *cache_bytes, int *cache_size,
394 char **cdirectory_table, int *directory_bytes,
395 char **directory_data_cache, int *directory_cache_bytes, int *directory_cache_size,
396 int *file_count, int *sym_count, int *dev_count, int *dir_count, int *fifo_count, int *sock_count,
397 squashfs_uid *uids, unsigned short *uid_count, squashfs_uid *guids, unsigned short *guid_count,
398 unsigned int *uncompressed_file, unsigned int *uncompressed_inode, unsigned int *uncompressed_directory,
399 void (push_directory_entry)(char *, squashfs_inode, int), squashfs_fragment_entry **fragment_table)
401 unsigned char *inode_table = NULL, *directory_table;
402 unsigned int start = sBlk->inode_table_start, end = sBlk->directory_table_start,
403 root_inode_start = start + SQUASHFS_INODE_BLK(sBlk->root_inode),
404 root_inode_offset = SQUASHFS_INODE_OFFSET(sBlk->root_inode), root_directory_offset;
405 squashfs_dir_inode_header inode;
406 unsigned int files, root_inode_block;
408 printf("Scanning existing filesystem...\n");
410 if(read_fragment_table(fd, sBlk, fragment_table) == 0)
413 if((files = scan_inode_table(fd, start, end, root_inode_start, sBlk, &inode, &inode_table, &root_inode_block,
414 uncompressed_file, uncompressed_directory, file_count, sym_count, dev_count, dir_count, fifo_count, sock_count)) == 0) {
415 ERROR("read_filesystem: inode table read failed\n");
419 *uncompressed_inode = root_inode_block;
421 printf("Read existing filesystem, %d inodes scanned\n", files);
423 if(inode.inode_type == SQUASHFS_DIR_TYPE) {
424 if((directory_table = squashfs_readdir(fd, !root_name, sBlk->directory_table_start + inode.start_block,
425 inode.offset, inode.file_size, sBlk, push_directory_entry)) == NULL) {
426 ERROR("read_filesystem: Could not read root directory\n");
430 if((*cinode_table = (char *) malloc(root_inode_start - start)) == NULL) {
431 ERROR("read_filesystem: failed to alloc space for existing filesystem inode table\n");
435 read_bytes(fd, start, root_inode_start - start, *cinode_table);
437 if((*cdirectory_table = (char *) malloc(inode.start_block)) == NULL) {
438 ERROR("read_filesystem: failed to alloc space for existing filesystem inode table\n");
442 read_bytes(fd, sBlk->directory_table_start, inode.start_block, *cdirectory_table);
444 *inode_bytes = root_inode_start - start;
445 *directory_bytes = inode.start_block;
447 root_inode_offset += sizeof(inode);
448 root_directory_offset = inode.offset + inode.file_size;
451 if(((*data_cache = (char *) malloc(root_inode_offset)) == NULL) ||
452 ((*directory_data_cache = (char *) malloc(root_directory_offset)) == NULL)) {
453 ERROR("read_filesystem: failed to alloc inode/directory caches\n");
457 memcpy(*data_cache, inode_table + root_inode_block, root_inode_offset);
458 memcpy(*directory_data_cache, directory_table, root_directory_offset);
459 *cache_size = root_inode_offset;
460 *directory_cache_size = root_directory_offset;
463 push_directory_entry(root_name, sBlk->root_inode, SQUASHFS_DIR_TYPE);
464 *cache_bytes = root_inode_offset;
465 *directory_cache_bytes = root_directory_offset;
467 *cache_bytes = root_inode_offset - sizeof(inode);
468 *directory_cache_bytes = inode.offset;
472 read_bytes(fd, sBlk->uid_start, sBlk->no_uids * sizeof(squashfs_uid), (char *) uids);
474 squashfs_uid uids_copy[sBlk->no_uids];
476 read_bytes(fd, sBlk->uid_start, sBlk->no_uids * sizeof(squashfs_uid), (char *) uids_copy);
477 SQUASHFS_SWAP_DATA(uids, uids_copy, sBlk->no_uids, sizeof(squashfs_uid) * 8);
481 read_bytes(fd, sBlk->guid_start, sBlk->no_guids * sizeof(squashfs_uid), (char *) guids);
483 squashfs_uid guids_copy[sBlk->no_guids];
485 read_bytes(fd, sBlk->guid_start, sBlk->no_guids * sizeof(squashfs_uid), (char *) guids_copy);
486 SQUASHFS_SWAP_DATA(guids, guids_copy, sBlk->no_guids, sizeof(squashfs_uid) * 8);
488 *uid_count = sBlk->no_uids;
489 *guid_count = sBlk->no_guids;
492 free(directory_table);
493 return sBlk->inode_table_start;