summaryrefslogtreecommitdiff
path: root/lib/sqfs/read_super.c
blob: 094cad525f98da56d4d9d595cbb5ef76d728edb1 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * read_super.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "sqfs/squashfs.h"
#include "util.h"

#include <endian.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>

int sqfs_super_read(sqfs_super_t *super, int fd)
{
	size_t block_size = 0;
	sqfs_super_t temp;
	int i;

	if (read_data_at("reading super block", 0, fd, &temp, sizeof(temp)))
		return -1;

	temp.magic = le32toh(temp.magic);
	temp.inode_count = le32toh(temp.inode_count);
	temp.modification_time = le32toh(temp.modification_time);
	temp.block_size = le32toh(temp.block_size);
	temp.fragment_entry_count = le32toh(temp.fragment_entry_count);
	temp.compression_id = le16toh(temp.compression_id);
	temp.block_log = le16toh(temp.block_log);
	temp.flags = le16toh(temp.flags);
	temp.id_count = le16toh(temp.id_count);
	temp.version_major = le16toh(temp.version_major);
	temp.version_minor = le16toh(temp.version_minor);
	temp.root_inode_ref = le64toh(temp.root_inode_ref);
	temp.bytes_used = le64toh(temp.bytes_used);
	temp.id_table_start = le64toh(temp.id_table_start);
	temp.xattr_id_table_start = le64toh(temp.xattr_id_table_start);
	temp.inode_table_start = le64toh(temp.inode_table_start);
	temp.directory_table_start = le64toh(temp.directory_table_start);
	temp.fragment_table_start = le64toh(temp.fragment_table_start);
	temp.export_table_start = le64toh(temp.export_table_start);

	if (temp.magic != SQFS_MAGIC) {
		fputs("Magic number missing. Not a squashfs image.\n", stderr);
		return -1;
	}

	if ((temp.version_major != SQFS_VERSION_MAJOR) ||
	    (temp.version_minor != SQFS_VERSION_MINOR)) {
		fprintf(stderr,
			"The squashfs image uses squashfs version %d.%d\n"
			"This tool currently only supports version %d.%d.\n",
			temp.version_major, temp.version_minor,
			SQFS_VERSION_MAJOR, SQFS_VERSION_MINOR);
		return -1;
	}

	if ((temp.block_size - 1) & temp.block_size) {
		fputs("Block size in image is not a power of 2!\n", stderr);
		return -1;
	}

	if (temp.block_size < 4096 || temp.block_size >= (1 << 20)) {
		fputs("Block size in iamge not between 4k and 1M\n", stderr);
		return -1;
	}

	if (temp.block_log < 12 || temp.block_log > 20)
		goto fail_block_log;

	block_size = 1;

	for (i = 0; i < temp.block_log; ++i)
		block_size <<= 1;

	if (temp.block_size != block_size)
		goto fail_block_log;

	if (temp.compression_id < SQFS_COMP_MIN ||
	    temp.compression_id > SQFS_COMP_MAX) {
		fputs("Image uses an unsupported compressor\n", stderr);
		return -1;
	}

	if (temp.id_count == 0) {
		fputs("ID table in squashfs image is empty.\n", stderr);
		return -1;
	}

	memcpy(super, &temp, sizeof(temp));
	return 0;
fail_block_log:
	fputs("Mismatch between block size and block log\n", stderr);
	fputs("Filesystem probably currupted.\n", stderr);
	return -1;
}