]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * SPDX-License-Identifier: GPL-2.0-or-later | |
3 | * | |
4 | * cramfsck - check a cramfs file system | |
5 | * | |
6 | * Copyright (C) 2000-2002 Transmeta Corporation | |
7 | * 2005 Adrian Bunk | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program. If not, see <https://gnu.org/licenses/>. | |
21 | * | |
22 | * 1999/12/03: Linus Torvalds (cramfs tester and unarchive program) | |
23 | * 2000/06/03: Daniel Quinlan (CRC and length checking program) | |
24 | * 2000/06/04: Daniel Quinlan (merged programs, added options, support | |
25 | * for special files, preserve permissions and | |
26 | * ownership, cramfs superblock v2, bogus mode | |
27 | * test, pathname length test, etc.) | |
28 | * 2000/06/06: Daniel Quinlan (support for holes, pretty-printing, | |
29 | * symlink size test) | |
30 | * 2000/07/11: Daniel Quinlan (file length tests, start at offset 0 or 512, | |
31 | * fsck-compatible exit codes) | |
32 | * 2000/07/15: Daniel Quinlan (initial support for block devices) | |
33 | * 2002/01/10: Daniel Quinlan (additional checks, test more return codes, | |
34 | * use read if mmap fails, standardize messages) | |
35 | */ | |
36 | ||
37 | #include <stdio.h> | |
38 | #include <stdarg.h> | |
39 | #include <stdint.h> | |
40 | #include <unistd.h> | |
41 | #include <dirent.h> | |
42 | #include <stdlib.h> | |
43 | #include <errno.h> | |
44 | #include <string.h> | |
45 | #include <getopt.h> | |
46 | #include <fcntl.h> | |
47 | ||
48 | /* We don't use our include/crc32.h, but crc32 from zlib! | |
49 | * | |
50 | * The zlib implementation performs pre/post-conditioning. The util-linux | |
51 | * imlemenation requires post-conditioning (xor) in the applications. | |
52 | */ | |
53 | #include <zlib.h> | |
54 | ||
55 | #include <sys/time.h> | |
56 | #include <sys/types.h> | |
57 | #include <sys/stat.h> | |
58 | #include <sys/mman.h> | |
59 | ||
60 | #include "c.h" | |
61 | #include "cramfs.h" | |
62 | #include "nls.h" | |
63 | #include "blkdev.h" | |
64 | #include "exitcodes.h" | |
65 | #include "strutils.h" | |
66 | #include "closestream.h" | |
67 | ||
68 | #define XALLOC_EXIT_CODE FSCK_EX_ERROR | |
69 | #include "xalloc.h" | |
70 | ||
71 | static int fd; /* ROM image file descriptor */ | |
72 | static char *filename; /* ROM image filename */ | |
73 | static struct cramfs_super super; /* just find the cramfs superblock once */ | |
74 | static int cramfs_is_big_endian = 0; /* source is big endian */ | |
75 | static int opt_verbose = 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */ | |
76 | static int opt_extract = 0; /* extract cramfs (-x) */ | |
77 | static char *extract_dir = ""; /* optional extraction directory (-x) */ | |
78 | ||
79 | #define PAD_SIZE 512 | |
80 | ||
81 | static uid_t euid; /* effective UID */ | |
82 | ||
83 | /* (cramfs_super + start) <= start_dir < end_dir <= start_data <= end_data */ | |
84 | static unsigned long start_dir = ~0UL; /* start of first non-root inode */ | |
85 | static unsigned long end_dir = 0; /* end of the directory structure */ | |
86 | static unsigned long start_data = ~0UL; /* start of the data (256 MB = max) */ | |
87 | static unsigned long end_data = 0; /* end of the data */ | |
88 | ||
89 | ||
90 | /* Guarantee access to at least 2 * blksize at a time */ | |
91 | #define CRAMFS_ROMBUFFER_BITS 13 | |
92 | #define CRAMFS_ROMBUFFERSIZE (1 << CRAMFS_ROMBUFFER_BITS) | |
93 | #define CRAMFS_ROMBUFFERMASK (CRAMFS_ROMBUFFERSIZE - 1) | |
94 | ||
95 | /* Defaults, updated in main() according to block size */ | |
96 | static size_t rombufbits = CRAMFS_ROMBUFFER_BITS; | |
97 | static size_t rombufsize = CRAMFS_ROMBUFFERSIZE; | |
98 | static size_t rombufmask = CRAMFS_ROMBUFFERMASK; | |
99 | ||
100 | static char *read_buffer; | |
101 | static unsigned long read_buffer_block = ~0UL; | |
102 | ||
103 | static z_stream stream; | |
104 | ||
105 | /* Prototypes */ | |
106 | static void expand_fs(char *, struct cramfs_inode *); | |
107 | ||
108 | static char *outbuffer; | |
109 | ||
110 | static size_t blksize = 0; | |
111 | ||
112 | static void __attribute__((__noreturn__)) usage(void) | |
113 | { | |
114 | FILE *out = stdout; | |
115 | ||
116 | fputs(USAGE_HEADER, out); | |
117 | fprintf(out, | |
118 | _(" %s [options] <file>\n"), program_invocation_short_name); | |
119 | ||
120 | fputs(USAGE_SEPARATOR, out); | |
121 | fputs(_("Check and repair a compressed ROM filesystem.\n"), out); | |
122 | ||
123 | fputs(USAGE_OPTIONS, out); | |
124 | fputs(_(" -a for compatibility only, ignored\n"), out); | |
125 | fputs(_(" -v, --verbose be more verbose\n"), out); | |
126 | fputs(_(" -y for compatibility only, ignored\n"), out); | |
127 | fputs(_(" -b, --blocksize <size> use this blocksize, defaults to page size\n"), out); | |
128 | fputs(_(" --extract[=<dir>] test uncompression, optionally extract into <dir>\n"), out); | |
129 | fputs(USAGE_SEPARATOR, out); | |
130 | fprintf(out, USAGE_HELP_OPTIONS(26)); | |
131 | ||
132 | fprintf(out, USAGE_MAN_TAIL("fsck.cramfs(8)")); | |
133 | exit(FSCK_EX_OK); | |
134 | } | |
135 | ||
136 | static int get_superblock_endianness(uint32_t magic) | |
137 | { | |
138 | if (magic == CRAMFS_MAGIC) { | |
139 | cramfs_is_big_endian = HOST_IS_BIG_ENDIAN; | |
140 | return 0; | |
141 | } | |
142 | ||
143 | if (magic == | |
144 | u32_toggle_endianness(!HOST_IS_BIG_ENDIAN, CRAMFS_MAGIC)) { | |
145 | cramfs_is_big_endian = !HOST_IS_BIG_ENDIAN; | |
146 | return 0; | |
147 | } | |
148 | ||
149 | return -1; | |
150 | } | |
151 | ||
152 | static void test_super(int *start) | |
153 | { | |
154 | struct stat st; | |
155 | unsigned long long length; | |
156 | ||
157 | fd = open(filename, O_RDONLY); | |
158 | if (fd < 0) | |
159 | err(FSCK_EX_ERROR, _("cannot open %s"), filename); | |
160 | ||
161 | /* find the physical size of the file or block device */ | |
162 | if (fstat(fd, &st) < 0) | |
163 | err(FSCK_EX_ERROR, _("stat of %s failed"), filename); | |
164 | ||
165 | if (S_ISBLK(st.st_mode)) { | |
166 | if (blkdev_get_size(fd, &length)) | |
167 | err(FSCK_EX_ERROR, | |
168 | _("ioctl failed: unable to determine device size: %s"), | |
169 | filename); | |
170 | } else if (S_ISREG(st.st_mode)) | |
171 | length = st.st_size; | |
172 | else | |
173 | errx(FSCK_EX_ERROR, _("not a block device or file: %s"), filename); | |
174 | ||
175 | if (length < sizeof(struct cramfs_super)) | |
176 | errx(FSCK_EX_UNCORRECTED, _("file length too short")); | |
177 | ||
178 | /* find superblock */ | |
179 | if (read(fd, &super, sizeof(super)) != sizeof(super)) | |
180 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); | |
181 | if (get_superblock_endianness(super.magic) != -1) | |
182 | *start = 0; | |
183 | else if (length >= (PAD_SIZE + sizeof(super))) { | |
184 | if (lseek(fd, PAD_SIZE, SEEK_SET) == (off_t) -1) | |
185 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); | |
186 | if (read(fd, &super, sizeof(super)) != sizeof(super)) | |
187 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); | |
188 | if (get_superblock_endianness(super.magic) != -1) | |
189 | *start = PAD_SIZE; | |
190 | else | |
191 | errx(FSCK_EX_UNCORRECTED, _("superblock magic not found")); | |
192 | } else | |
193 | errx(FSCK_EX_UNCORRECTED, _("superblock magic not found")); | |
194 | ||
195 | if (opt_verbose) | |
196 | printf(_("cramfs endianness is %s\n"), | |
197 | cramfs_is_big_endian ? _("big") : _("little")); | |
198 | ||
199 | super_toggle_endianness(cramfs_is_big_endian, &super); | |
200 | if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) | |
201 | errx(FSCK_EX_ERROR, _("unsupported filesystem features")); | |
202 | ||
203 | /* What are valid superblock sizes? */ | |
204 | if (super.size < *start + sizeof(struct cramfs_super)) | |
205 | errx(FSCK_EX_UNCORRECTED, _("superblock size (%d) too small"), | |
206 | super.size); | |
207 | ||
208 | if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) { | |
209 | if (super.fsid.files == 0) | |
210 | errx(FSCK_EX_UNCORRECTED, _("zero file count")); | |
211 | if (length < super.size) | |
212 | errx(FSCK_EX_UNCORRECTED, _("file length too short")); | |
213 | else if (length > super.size) | |
214 | warnx(_("file extends past end of filesystem")); | |
215 | } else | |
216 | warnx(_("old cramfs format")); | |
217 | } | |
218 | ||
219 | static void test_crc(int start) | |
220 | { | |
221 | void *buf; | |
222 | uint32_t crc; | |
223 | ||
224 | if (!(super.flags & CRAMFS_FLAG_FSID_VERSION_2)) { | |
225 | warnx(_("unable to test CRC: old cramfs format")); | |
226 | return; | |
227 | } | |
228 | ||
229 | crc = crc32(0L, NULL, 0); | |
230 | ||
231 | buf = | |
232 | mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
233 | if (buf == MAP_FAILED) { | |
234 | buf = | |
235 | mmap(NULL, super.size, PROT_READ | PROT_WRITE, | |
236 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
237 | if (buf != MAP_FAILED) { | |
238 | ssize_t tmp; | |
239 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) | |
240 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); | |
241 | tmp = read(fd, buf, super.size); | |
242 | if (tmp < 0) | |
243 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); | |
244 | if (tmp != (ssize_t) super.size) | |
245 | errx(FSCK_EX_ERROR, _("failed to read %"PRIu32" bytes from file %s"), | |
246 | super.size, filename); | |
247 | } | |
248 | } | |
249 | if (buf != MAP_FAILED) { | |
250 | ((struct cramfs_super *)((unsigned char *) buf + start))->fsid.crc = | |
251 | crc32(0L, NULL, 0); | |
252 | crc = crc32(crc, (unsigned char *) buf + start, super.size - start); | |
253 | munmap(buf, super.size); | |
254 | } else { | |
255 | int retval; | |
256 | size_t length = 0; | |
257 | ||
258 | buf = xmalloc(4096); | |
259 | if (lseek(fd, start, SEEK_SET) == (off_t) -1) | |
260 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); | |
261 | for (;;) { | |
262 | retval = read(fd, buf, 4096); | |
263 | if (retval < 0) | |
264 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); | |
265 | else if (retval == 0) | |
266 | break; | |
267 | if (length == 0) | |
268 | ((struct cramfs_super *)buf)->fsid.crc = | |
269 | crc32(0L, NULL, 0); | |
270 | length += retval; | |
271 | if (length > (super.size - start)) { | |
272 | crc = crc32(crc, buf, | |
273 | retval - (length - | |
274 | (super.size - start))); | |
275 | break; | |
276 | } | |
277 | crc = crc32(crc, buf, retval); | |
278 | } | |
279 | free(buf); | |
280 | } | |
281 | ||
282 | if (crc != super.fsid.crc) | |
283 | errx(FSCK_EX_UNCORRECTED, _("crc error")); | |
284 | } | |
285 | ||
286 | static void print_node(char type, struct cramfs_inode *i, char *name) | |
287 | { | |
288 | char info[10]; | |
289 | ||
290 | if (S_ISCHR(i->mode) || (S_ISBLK(i->mode))) | |
291 | /* major/minor numbers can be as high as 2^12 or 4096 */ | |
292 | snprintf(info, 10, "%4d,%4d", major(i->size), minor(i->size)); | |
293 | else | |
294 | /* size be as high as 2^24 or 16777216 */ | |
295 | snprintf(info, 10, "%9d", i->size); | |
296 | ||
297 | printf("%c %04o %s %5d:%-3d %s\n", | |
298 | type, i->mode & ~S_IFMT, info, i->uid, i->gid, | |
299 | !*name && type == 'd' ? "/" : name); | |
300 | } | |
301 | ||
302 | /* | |
303 | * Create a fake "blocked" access | |
304 | */ | |
305 | static void *romfs_read(unsigned long offset) | |
306 | { | |
307 | unsigned int block = offset >> rombufbits; | |
308 | if (block != read_buffer_block) { | |
309 | ssize_t x; | |
310 | ||
311 | read_buffer_block = block; | |
312 | if (lseek(fd, block << rombufbits, SEEK_SET) == (off_t) -1) | |
313 | warn(_("seek failed")); | |
314 | ||
315 | x = read(fd, read_buffer, rombufsize * 2); | |
316 | if (x < 0) | |
317 | warn(_("read romfs failed")); | |
318 | } | |
319 | return read_buffer + (offset & rombufmask); | |
320 | } | |
321 | ||
322 | static struct cramfs_inode *cramfs_iget(struct cramfs_inode *i) | |
323 | { | |
324 | struct cramfs_inode *inode = xmalloc(sizeof(struct cramfs_inode)); | |
325 | ||
326 | inode_to_host(cramfs_is_big_endian, i, inode); | |
327 | return inode; | |
328 | } | |
329 | ||
330 | static struct cramfs_inode *iget(unsigned int ino) | |
331 | { | |
332 | return cramfs_iget(romfs_read(ino)); | |
333 | } | |
334 | ||
335 | static void iput(struct cramfs_inode *inode) | |
336 | { | |
337 | free(inode); | |
338 | } | |
339 | ||
340 | /* | |
341 | * Return the offset of the root directory | |
342 | */ | |
343 | static struct cramfs_inode *read_super(void) | |
344 | { | |
345 | struct cramfs_inode *root = cramfs_iget(&super.root); | |
346 | unsigned long offset = root->offset << 2; | |
347 | ||
348 | if (!S_ISDIR(root->mode)) | |
349 | errx(FSCK_EX_UNCORRECTED, _("root inode is not directory")); | |
350 | if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) && | |
351 | ((offset != sizeof(struct cramfs_super)) && | |
352 | (offset != PAD_SIZE + sizeof(struct cramfs_super)))) { | |
353 | errx(FSCK_EX_UNCORRECTED, _("bad root offset (%lu)"), offset); | |
354 | } | |
355 | return root; | |
356 | } | |
357 | ||
358 | static int uncompress_block(void *src, size_t len) | |
359 | { | |
360 | int err; | |
361 | ||
362 | stream.next_in = src; | |
363 | stream.avail_in = len; | |
364 | ||
365 | stream.next_out = (unsigned char *)outbuffer; | |
366 | stream.avail_out = blksize * 2; | |
367 | ||
368 | inflateReset(&stream); | |
369 | ||
370 | if (len > blksize * 2) | |
371 | errx(FSCK_EX_UNCORRECTED, _("data block too large")); | |
372 | ||
373 | err = inflate(&stream, Z_FINISH); | |
374 | if (err != Z_STREAM_END) | |
375 | errx(FSCK_EX_UNCORRECTED, _("decompression error: %s"), | |
376 | zError(err)); | |
377 | return stream.total_out; | |
378 | } | |
379 | ||
380 | #ifndef HAVE_LCHOWN | |
381 | #define lchown chown | |
382 | #endif | |
383 | ||
384 | static void do_uncompress(char *path, int outfd, unsigned long offset, | |
385 | unsigned long size) | |
386 | { | |
387 | unsigned long curr = offset + 4 * ((size + blksize - 1) / blksize); | |
388 | ||
389 | do { | |
390 | unsigned long out = blksize; | |
391 | unsigned long next = u32_toggle_endianness(cramfs_is_big_endian, | |
392 | *(uint32_t *) | |
393 | romfs_read(offset)); | |
394 | ||
395 | if (next > end_data) | |
396 | end_data = next; | |
397 | ||
398 | offset += 4; | |
399 | if (curr == next) { | |
400 | if (opt_verbose > 1) | |
401 | printf(_(" hole at %lu (%zu)\n"), curr, | |
402 | blksize); | |
403 | if (size < blksize) | |
404 | out = size; | |
405 | memset(outbuffer, 0x00, out); | |
406 | } else { | |
407 | if (opt_verbose > 1) | |
408 | printf(_(" uncompressing block at %lu to %lu (%lu)\n"), | |
409 | curr, next, next - curr); | |
410 | out = uncompress_block(romfs_read(curr), next - curr); | |
411 | } | |
412 | if (size >= blksize) { | |
413 | if (out != blksize) | |
414 | errx(FSCK_EX_UNCORRECTED, | |
415 | _("non-block (%ld) bytes"), out); | |
416 | } else { | |
417 | if (out != size) | |
418 | errx(FSCK_EX_UNCORRECTED, | |
419 | _("non-size (%ld vs %ld) bytes"), out, | |
420 | size); | |
421 | } | |
422 | size -= out; | |
423 | if (*extract_dir != '\0' && write(outfd, outbuffer, out) < 0) | |
424 | err(FSCK_EX_ERROR, _("write failed: %s"), path); | |
425 | curr = next; | |
426 | } while (size); | |
427 | } | |
428 | static void change_file_status(char *path, struct cramfs_inode *i) | |
429 | { | |
430 | const struct timeval epoch[] = { {0,0}, {0,0} }; | |
431 | ||
432 | if (euid == 0) { | |
433 | if (lchown(path, i->uid, i->gid) < 0) | |
434 | err(FSCK_EX_ERROR, _("lchown failed: %s"), path); | |
435 | if (S_ISLNK(i->mode)) | |
436 | return; | |
437 | if (((S_ISUID | S_ISGID) & i->mode) && chmod(path, i->mode) < 0) | |
438 | err(FSCK_EX_ERROR, _("chmod failed: %s"), path); | |
439 | } | |
440 | if (S_ISLNK(i->mode)) | |
441 | return; | |
442 | if (utimes(path, epoch) < 0) | |
443 | err(FSCK_EX_ERROR, _("utimes failed: %s"), path); | |
444 | } | |
445 | ||
446 | static int is_dangerous_filename(char *name, int len) | |
447 | { | |
448 | return (len == 1 && name[0] == '.') || | |
449 | (len == 2 && name[0] == '.' && name[1] == '.'); | |
450 | } | |
451 | ||
452 | static void __attribute__((__noreturn__)) | |
453 | errx_path(const char *mesg, const char *name, size_t namelen) | |
454 | { | |
455 | char buf[PATH_MAX] = { 0 }; | |
456 | ||
457 | namelen = min(namelen, sizeof(buf) - 1); | |
458 | memcpy(buf, name, namelen); | |
459 | ||
460 | errx(FSCK_EX_UNCORRECTED, "%s: %s", mesg, buf); | |
461 | } | |
462 | ||
463 | static void do_directory(char *path, struct cramfs_inode *i) | |
464 | { | |
465 | int pathlen = strlen(path); | |
466 | int count = i->size; | |
467 | unsigned long offset = i->offset << 2; | |
468 | char *newpath = xmalloc(pathlen + 256); | |
469 | ||
470 | if (offset == 0 && count != 0) | |
471 | errx(FSCK_EX_UNCORRECTED, | |
472 | _("directory inode has zero offset and non-zero size: %s"), | |
473 | path); | |
474 | ||
475 | if (offset != 0 && offset < start_dir) | |
476 | start_dir = offset; | |
477 | ||
478 | /* TODO: Do we need to check end_dir for empty case? */ | |
479 | memcpy(newpath, path, pathlen); | |
480 | newpath[pathlen] = '/'; | |
481 | pathlen++; | |
482 | if (opt_verbose) | |
483 | print_node('d', i, path); | |
484 | ||
485 | if (*extract_dir != '\0') { | |
486 | if (mkdir(path, i->mode) < 0) | |
487 | err(FSCK_EX_ERROR, _("mkdir failed: %s"), path); | |
488 | change_file_status(path, i); | |
489 | } | |
490 | while (count > 0) { | |
491 | struct cramfs_inode *child = iget(offset); | |
492 | char *name; | |
493 | int size; | |
494 | int newlen = child->namelen << 2; | |
495 | ||
496 | size = sizeof(struct cramfs_inode) + newlen; | |
497 | count -= size; | |
498 | ||
499 | offset += sizeof(struct cramfs_inode); | |
500 | name = romfs_read(offset); | |
501 | ||
502 | if (memchr(name, '/', newlen) != NULL) | |
503 | errx_path(_("illegal filename"), name, newlen); | |
504 | if (*extract_dir != '\0' && is_dangerous_filename(name, newlen)) | |
505 | errx_path(_("dangerous filename"), name, newlen); | |
506 | memcpy(newpath + pathlen, name, newlen); | |
507 | newpath[pathlen + newlen] = 0; | |
508 | if (newlen == 0) | |
509 | errx(FSCK_EX_UNCORRECTED, _("filename length is zero")); | |
510 | if ((pathlen + newlen) - strlen(newpath) > 3) | |
511 | errx(FSCK_EX_UNCORRECTED, _("bad filename length")); | |
512 | expand_fs(newpath, child); | |
513 | ||
514 | offset += newlen; | |
515 | ||
516 | if (offset <= start_dir) | |
517 | errx(FSCK_EX_UNCORRECTED, _("bad inode offset")); | |
518 | if (offset > end_dir) | |
519 | end_dir = offset; | |
520 | iput(child); /* free(child) */ | |
521 | } | |
522 | free(newpath); | |
523 | } | |
524 | ||
525 | static void do_file(char *path, struct cramfs_inode *i) | |
526 | { | |
527 | unsigned long offset = i->offset << 2; | |
528 | int outfd = 0; | |
529 | ||
530 | if (offset == 0 && i->size != 0) | |
531 | errx(FSCK_EX_UNCORRECTED, | |
532 | _("file inode has zero offset and non-zero size")); | |
533 | if (i->size == 0 && offset != 0) | |
534 | errx(FSCK_EX_UNCORRECTED, | |
535 | _("file inode has zero size and non-zero offset")); | |
536 | if (offset != 0 && offset < start_data) | |
537 | start_data = offset; | |
538 | if (opt_verbose) | |
539 | print_node('f', i, path); | |
540 | if (*extract_dir != '\0') { | |
541 | outfd = open(path, O_WRONLY | O_CREAT | O_TRUNC, i->mode); | |
542 | if (outfd < 0) | |
543 | err(FSCK_EX_ERROR, _("cannot open %s"), path); | |
544 | } | |
545 | if (i->size) | |
546 | do_uncompress(path, outfd, offset, i->size); | |
547 | if ( *extract_dir != '\0') { | |
548 | if (close_fd(outfd) != 0) | |
549 | err(FSCK_EX_ERROR, _("write failed: %s"), path); | |
550 | change_file_status(path, i); | |
551 | } | |
552 | } | |
553 | ||
554 | static void do_symlink(char *path, struct cramfs_inode *i) | |
555 | { | |
556 | unsigned long offset = i->offset << 2; | |
557 | unsigned long curr = offset + 4; | |
558 | unsigned long next = | |
559 | u32_toggle_endianness(cramfs_is_big_endian, | |
560 | *(uint32_t *) romfs_read(offset)); | |
561 | unsigned long size; | |
562 | ||
563 | if (offset == 0) | |
564 | errx(FSCK_EX_UNCORRECTED, _("symbolic link has zero offset")); | |
565 | if (i->size == 0) | |
566 | errx(FSCK_EX_UNCORRECTED, _("symbolic link has zero size")); | |
567 | ||
568 | if (offset < start_data) | |
569 | start_data = offset; | |
570 | if (next > end_data) | |
571 | end_data = next; | |
572 | ||
573 | size = uncompress_block(romfs_read(curr), next - curr); | |
574 | if (size != i->size) | |
575 | errx(FSCK_EX_UNCORRECTED, _("size error in symlink: %s"), path); | |
576 | outbuffer[size] = 0; | |
577 | if (opt_verbose) { | |
578 | char *str; | |
579 | ||
580 | xasprintf(&str, "%s -> %s", path, outbuffer); | |
581 | print_node('l', i, str); | |
582 | if (opt_verbose > 1) | |
583 | printf(_(" uncompressing block at %lu to %lu (%lu)\n"), | |
584 | curr, next, next - curr); | |
585 | free(str); | |
586 | } | |
587 | if (*extract_dir != '\0') { | |
588 | if (symlink(outbuffer, path) < 0) | |
589 | err(FSCK_EX_ERROR, _("symlink failed: %s"), path); | |
590 | change_file_status(path, i); | |
591 | } | |
592 | } | |
593 | ||
594 | static void do_special_inode(char *path, struct cramfs_inode *i) | |
595 | { | |
596 | dev_t devtype = 0; | |
597 | char type; | |
598 | ||
599 | if (i->offset) | |
600 | /* no need to shift offset */ | |
601 | errx(FSCK_EX_UNCORRECTED, | |
602 | _("special file has non-zero offset: %s"), path); | |
603 | ||
604 | if (S_ISCHR(i->mode)) { | |
605 | devtype = i->size; | |
606 | type = 'c'; | |
607 | } else if (S_ISBLK(i->mode)) { | |
608 | devtype = i->size; | |
609 | type = 'b'; | |
610 | } else if (S_ISFIFO(i->mode)) { | |
611 | if (i->size != 0) | |
612 | errx(FSCK_EX_UNCORRECTED, _("fifo has non-zero size: %s"), | |
613 | path); | |
614 | type = 'p'; | |
615 | } else if (S_ISSOCK(i->mode)) { | |
616 | if (i->size != 0) | |
617 | errx(FSCK_EX_UNCORRECTED, | |
618 | _("socket has non-zero size: %s"), path); | |
619 | type = 's'; | |
620 | } else { | |
621 | errx(FSCK_EX_UNCORRECTED, _("bogus mode: %s (%o)"), path, i->mode); | |
622 | return; /* not reached */ | |
623 | } | |
624 | ||
625 | if (opt_verbose) | |
626 | print_node(type, i, path); | |
627 | ||
628 | if (*extract_dir != '\0') { | |
629 | if (mknod(path, i->mode, devtype) < 0) | |
630 | err(FSCK_EX_ERROR, _("mknod failed: %s"), path); | |
631 | change_file_status(path, i); | |
632 | } | |
633 | } | |
634 | ||
635 | static void expand_fs(char *path, struct cramfs_inode *inode) | |
636 | { | |
637 | if (S_ISDIR(inode->mode)) | |
638 | do_directory(path, inode); | |
639 | else if (S_ISREG(inode->mode)) | |
640 | do_file(path, inode); | |
641 | else if (S_ISLNK(inode->mode)) | |
642 | do_symlink(path, inode); | |
643 | else | |
644 | do_special_inode(path, inode); | |
645 | } | |
646 | ||
647 | static void test_fs(int start) | |
648 | { | |
649 | struct cramfs_inode *root; | |
650 | ||
651 | root = read_super(); | |
652 | umask(0); | |
653 | euid = geteuid(); | |
654 | stream.next_in = NULL; | |
655 | stream.avail_in = 0; | |
656 | inflateInit(&stream); | |
657 | expand_fs(extract_dir, root); | |
658 | inflateEnd(&stream); | |
659 | if (start_data != ~0UL) { | |
660 | if (start_data < (sizeof(struct cramfs_super) + start)) | |
661 | errx(FSCK_EX_UNCORRECTED, | |
662 | _("directory data start (%lu) < sizeof(struct cramfs_super) + start (%zu)"), | |
663 | start_data, sizeof(struct cramfs_super) + start); | |
664 | if (end_dir != start_data) | |
665 | errx(FSCK_EX_UNCORRECTED, | |
666 | _("directory data end (%lu) != file data start (%lu)"), | |
667 | end_dir, start_data); | |
668 | } | |
669 | if (super.flags & CRAMFS_FLAG_FSID_VERSION_2 && end_data > super.size) | |
670 | errx(FSCK_EX_UNCORRECTED, _("invalid file data offset")); | |
671 | ||
672 | iput(root); /* free(root) */ | |
673 | } | |
674 | ||
675 | int main(int argc, char **argv) | |
676 | { | |
677 | int c; /* for getopt */ | |
678 | int start = 0; | |
679 | ||
680 | static const struct option longopts[] = { | |
681 | {"verbose", no_argument, NULL, 'v'}, | |
682 | {"version", no_argument, NULL, 'V'}, | |
683 | {"help", no_argument, NULL, 'h'}, | |
684 | {"blocksize", required_argument, NULL, 'b'}, | |
685 | {"extract", optional_argument, NULL, 'x'}, | |
686 | {NULL, 0, NULL, 0}, | |
687 | }; | |
688 | ||
689 | setlocale(LC_MESSAGES, ""); | |
690 | setlocale(LC_CTYPE, ""); | |
691 | bindtextdomain(PACKAGE, LOCALEDIR); | |
692 | textdomain(PACKAGE); | |
693 | close_stdout_atexit(); | |
694 | ||
695 | strutils_set_exitcode(FSCK_EX_USAGE); | |
696 | ||
697 | /* command line options */ | |
698 | while ((c = getopt_long(argc, argv, "ayvVhb:", longopts, NULL)) != EOF) | |
699 | switch (c) { | |
700 | case 'a': /* ignore */ | |
701 | case 'y': | |
702 | break; | |
703 | case 'h': | |
704 | usage(); | |
705 | break; | |
706 | case 'V': | |
707 | print_version(FSCK_EX_OK); | |
708 | case 'x': | |
709 | opt_extract = 1; | |
710 | if(optarg) | |
711 | extract_dir = optarg; | |
712 | break; | |
713 | case 'v': | |
714 | opt_verbose++; | |
715 | break; | |
716 | case 'b': | |
717 | blksize = strtou32_or_err(optarg, _("invalid blocksize argument")); | |
718 | break; | |
719 | default: | |
720 | errtryhelp(FSCK_EX_USAGE); | |
721 | } | |
722 | ||
723 | if ((argc - optind) != 1){ | |
724 | warnx(_("bad usage")); | |
725 | errtryhelp(FSCK_EX_USAGE); | |
726 | } | |
727 | filename = argv[optind]; | |
728 | ||
729 | test_super(&start); | |
730 | test_crc(start); | |
731 | ||
732 | if (opt_extract) { | |
733 | size_t bufsize = 0; | |
734 | ||
735 | if (blksize == 0) | |
736 | blksize = getpagesize(); | |
737 | ||
738 | /* re-calculate according to blksize */ | |
739 | bufsize = rombufsize = blksize * 2; | |
740 | rombufbits = 0; | |
741 | while (bufsize >>= 1) | |
742 | rombufbits++; | |
743 | rombufmask = rombufsize - 1; | |
744 | ||
745 | outbuffer = xmalloc(blksize * 2); | |
746 | read_buffer = xmalloc(rombufsize * 2); | |
747 | test_fs(start); | |
748 | } | |
749 | ||
750 | if (opt_verbose) | |
751 | printf(_("%s: OK\n"), filename); | |
752 | ||
753 | exit(FSCK_EX_OK); | |
754 | } |