-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqsq-image.c
293 lines (251 loc) · 6.57 KB
/
sqsq-image.c
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
/*
* Copyright (c) 2021 Westermo Network Technologies AB
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <byteswap.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <linux/magic.h>
#define SQFS_HDR_BYTES_USED 40 /* Position of bytes used in sqfs hdr */
#define SQFS_ALIGN 4096 /* Sqfs default alignment */
#define MAX_SQFS_NUM 256 /* Arbitrary recursion guard */
enum {
__CMD_ERROR__,
CMD_LIST, /* default */
CMD_OFFSET,
CMD_SIZE,
CMD_USED,
CMD_COUNT,
};
int block = 0;
static uint32_t hdr_read_magic(int fd)
{
uint32_t magic;
ssize_t n;
n = read(fd, &magic, sizeof(magic));
if (n == -1) {
perror("Error, unable to read magic");
return 0;
}
if (!n)
return 0;
if (n != sizeof(magic)) {
fprintf(stderr, "Error, short read when reading magic\n");
return 0;
}
#if __BYTE_ORDER == __BIG_ENDIAN
magic = __bswap_32(magic);
#endif
return magic;
}
static long long hdr_read_bytes_used(int fd)
{
long long used;
ssize_t n;
n = read(fd, &used, sizeof(used));
if (n == -1) {
perror("Error, unable to read bytes used");
return -1;
}
if (!n)
return 0;
if (n != sizeof(used)) {
fprintf(stderr, "Error, short read when reading bytes used");
return -1;
}
#if __BYTE_ORDER == __BIG_ENDIAN
used = __bswap_64(used);
#endif
return used;
}
static unsigned int calc_sqfs_pad(long long used)
{
unsigned int tail;
tail = used % SQFS_ALIGN;
return SQFS_ALIGN - tail;
}
static void print_sqfs_info(long long used, size_t offset, int num)
{
int fs;
fs = (num % 2) ? num / 2 : num / 2 - 1;
if (num % 2 != 0)
printf("%d: Filesystem%02d ", num, fs + 1);
else
printf("%d: Filesystem%02d-meta", num, fs + 1);
if (block) {
printf(" (%llu blocks @ block %zu)\n",
used / SQFS_ALIGN ? : 1, offset / SQFS_ALIGN);
} else {
printf(" (%llu bytes @ %zu)\n", used, offset);
}
}
/* num has to start at 1 */
static int foreach_sqfs(int fd, off_t offset, int num, int cmd, int filter)
{
unsigned int pad;
long long used;
uint32_t magic;
/* Set fd at sqfs start */
if (lseek(fd, offset, SEEK_SET) == -1) {
perror("Error, seeking in binary file");
return -1;
}
/* Recursion guard */
if (num > MAX_SQFS_NUM) {
fprintf(stderr, "Error, to many squash filesystems found\n");
return -1;
}
magic = hdr_read_magic(fd);
/* We expect this to happen at the end of the file */
if (!magic || magic != SQUASHFS_MAGIC)
return num - 1;
if (lseek(fd, SQFS_HDR_BYTES_USED - sizeof(magic), SEEK_CUR) == -1) {
perror("Error, seeking in binary file");
return 1;
}
used = hdr_read_bytes_used(fd);
if (used < 0) {
fprintf(stderr, "Error, reading bytes used\n");
return 1;
}
pad = calc_sqfs_pad(used);
if (cmd == CMD_COUNT) {
if (filter) {
fprintf(stderr, "Error, can't use filter with count\n");
return 1;
}
goto next;
}
if (!filter || filter == num) {
switch (cmd) {
case CMD_LIST:
print_sqfs_info(used, offset, num);
break;
case CMD_OFFSET:
printf("%jd\n", block ? (intmax_t)offset / SQFS_ALIGN :
(intmax_t)offset);
break;
case CMD_SIZE:
printf("%llu\n", block ? (used + pad) / SQFS_ALIGN :
used + pad);
break;
case CMD_USED:
printf("%llu\n", block ? used / SQFS_ALIGN : used);
break;
}
}
next:
return foreach_sqfs(fd, offset + used + pad, ++num, cmd, filter);
}
static int usage(char *argv0)
{
fprintf(stderr, "Usage: %s [OPTIONS] [COMMAND] SQSQ-IMAGE\n", argv0);
fprintf(stderr, "\nCommands:\n");
fprintf(stderr, " list - List filesystems (default)\n");
fprintf(stderr, " offset - Get start address of filesystem\n");
fprintf(stderr, " size - Get padded size of filesystem\n");
fprintf(stderr, " used - Get squashfs BYTES_USED of filesystem\n");
fprintf(stderr, " count - Count the number of filesystems\n");
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, " -n NUMBER - Only look at filesystem NUMBER\n");
fprintf(stderr, " -b - Count in sqfs blocks (4096 bytes)\n");
fprintf(stderr, " -h - Help (this text)\n");
return 1;
}
static int parse_cmd(const char *word)
{
if (strcmp("list", word) == 0)
return CMD_LIST;
if (strcmp("offset", word) == 0)
return CMD_OFFSET;
if (strcmp("size", word) == 0)
return CMD_SIZE;
if (strcmp("used", word) == 0)
return CMD_USED;
if (strcmp("count", word) == 0)
return CMD_COUNT;
return __CMD_ERROR__;
}
int main(int argc, char *argv[])
{
const char *filename;
int filter;
int cmd;
int num;
int opt;
int fd;
filter = 0;
cmd = CMD_LIST;
while ((opt = getopt(argc, argv, "hbn:")) != -1) {
switch (opt) {
case 'n':
filter = atoi(optarg);
break;
case 'b':
block = 1;
break;
case 'h':
return usage(argv[0]);
}
}
if (argc - optind > 1) {
filename = argv[optind + 1];
cmd = parse_cmd(argv[optind]);
if (!cmd) {
fprintf(stderr, "Error, invalid operation \"%s\"\n",
argv[optind]);
return 1;
}
} else if (argc - optind == 1) {
filename = argv[optind];
} else {
return usage(argv[0]);
}
fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("Error, opening file");
return 1;
}
num = foreach_sqfs(fd, 0, 1, cmd, filter);
if (num < 0) {
fprintf(stderr, "Error, searching for sqfs in image\n");
goto out;
}
if (cmd == CMD_COUNT)
printf("%d\n", num);
else if (cmd == CMD_LIST && !filter)
fprintf(stderr, "\nFound %d filesystems in image\n", num);
out:
close(fd);
return (num >= 2) ? 0 : 1;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/