2 * cramfsck - check a cramfs file system
4 * Copyright (C) 2000-2002 Transmeta Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * 1999/12/03: Linus Torvalds (cramfs tester and unarchive program)
22 * 2000/06/03: Daniel Quinlan (CRC and length checking program)
23 * 2000/06/04: Daniel Quinlan (merged programs, added options, support
24 * for special files, preserve permissions and
25 * ownership, cramfs superblock v2, bogus mode
26 * test, pathname length test, etc.)
27 * 2000/06/06: Daniel Quinlan (support for holes, pretty-printing,
29 * 2000/07/11: Daniel Quinlan (file length tests, start at offset 0 or 512,
30 * fsck-compatible exit codes)
31 * 2000/07/15: Daniel Quinlan (initial support for block devices)
32 * 2002/01/10: Daniel Quinlan (additional checks, test more return codes,
33 * use read if mmap fails, standardize messages)
47 /* We don't use our include/crc32.h, but crc32 from zlib!
49 * The zlib implementation performs pre/post-conditioning. The util-linux
50 * imlemenation requires post-conditioning (xor) in the applications.
55 #include <sys/types.h>
63 #include "exitcodes.h"
65 #include "closestream.h"
67 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
70 static int fd
; /* ROM image file descriptor */
71 static char *filename
; /* ROM image filename */
72 static struct cramfs_super super
; /* just find the cramfs superblock once */
73 static int cramfs_is_big_endian
= 0; /* source is big endian */
74 static int opt_verbose
= 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */
75 static int opt_extract
= 0; /* extract cramfs (-x) */
76 static char *extract_dir
= ""; /* optional extraction directory (-x) */
80 static uid_t euid
; /* effective UID */
82 /* (cramfs_super + start) <= start_dir < end_dir <= start_data <= end_data */
83 static unsigned long start_dir
= ~0UL; /* start of first non-root inode */
84 static unsigned long end_dir
= 0; /* end of the directory structure */
85 static unsigned long start_data
= ~0UL; /* start of the data (256 MB = max) */
86 static unsigned long end_data
= 0; /* end of the data */
89 /* Guarantee access to at least 8kB at a time */
90 #define ROMBUFFER_BITS 13
91 #define ROMBUFFERSIZE (1 << ROMBUFFER_BITS)
92 #define ROMBUFFERMASK (ROMBUFFERSIZE - 1)
93 static char read_buffer
[ROMBUFFERSIZE
* 2];
94 static unsigned long read_buffer_block
= ~0UL;
96 static z_stream stream
;
99 static void expand_fs(char *, struct cramfs_inode
*);
101 static char *outbuffer
;
103 static size_t blksize
= 0;
105 static void __attribute__((__noreturn__
)) usage(void)
109 fputs(USAGE_HEADER
, out
);
111 _(" %s [options] <file>\n"), program_invocation_short_name
);
113 fputs(USAGE_SEPARATOR
, out
);
114 fputs(_("Check and repair a compressed ROM filesystem.\n"), out
);
116 fputs(USAGE_OPTIONS
, out
);
117 fputs(_(" -a for compatibility only, ignored\n"), out
);
118 fputs(_(" -v, --verbose be more verbose\n"), out
);
119 fputs(_(" -y for compatibility only, ignored\n"), out
);
120 fputs(_(" -b, --blocksize <size> use this blocksize, defaults to page size\n"), out
);
121 fputs(_(" --extract[=<dir>] test uncompression, optionally extract into <dir>\n"), out
);
122 fputs(USAGE_SEPARATOR
, out
);
123 printf(USAGE_HELP_OPTIONS(26));
125 printf(USAGE_MAN_TAIL("fsck.cramfs(8)"));
129 static int get_superblock_endianness(uint32_t magic
)
131 if (magic
== CRAMFS_MAGIC
) {
132 cramfs_is_big_endian
= HOST_IS_BIG_ENDIAN
;
135 u32_toggle_endianness(!HOST_IS_BIG_ENDIAN
, CRAMFS_MAGIC
)) {
136 cramfs_is_big_endian
= !HOST_IS_BIG_ENDIAN
;
142 static void test_super(int *start
, size_t * length
)
146 /* find the physical size of the file or block device */
147 if (stat(filename
, &st
) < 0)
148 err(FSCK_EX_ERROR
, _("stat of %s failed"), filename
);
150 fd
= open(filename
, O_RDONLY
);
152 err(FSCK_EX_ERROR
, _("cannot open %s"), filename
);
154 if (S_ISBLK(st
.st_mode
)) {
155 unsigned long long bytes
;
156 if (blkdev_get_size(fd
, &bytes
))
158 _("ioctl failed: unable to determine device size: %s"),
161 } else if (S_ISREG(st
.st_mode
))
162 *length
= st
.st_size
;
164 errx(FSCK_EX_ERROR
, _("not a block device or file: %s"), filename
);
166 if (*length
< sizeof(struct cramfs_super
))
167 errx(FSCK_EX_UNCORRECTED
, _("file length too short"));
169 /* find superblock */
170 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
171 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
172 if (get_superblock_endianness(super
.magic
) != -1)
174 else if (*length
>= (PAD_SIZE
+ sizeof(super
))) {
175 if (lseek(fd
, PAD_SIZE
, SEEK_SET
) == (off_t
) -1)
176 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
177 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
178 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
179 if (get_superblock_endianness(super
.magic
) != -1)
182 errx(FSCK_EX_UNCORRECTED
, _("superblock magic not found"));
184 errx(FSCK_EX_UNCORRECTED
, _("superblock magic not found"));
187 printf(_("cramfs endianness is %s\n"),
188 cramfs_is_big_endian
? _("big") : _("little"));
190 super_toggle_endianness(cramfs_is_big_endian
, &super
);
191 if (super
.flags
& ~CRAMFS_SUPPORTED_FLAGS
)
192 errx(FSCK_EX_ERROR
, _("unsupported filesystem features"));
194 /* What are valid superblock sizes? */
195 if (super
.size
< *start
+ sizeof(struct cramfs_super
))
196 errx(FSCK_EX_UNCORRECTED
, _("superblock size (%d) too small"),
199 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
) {
200 if (super
.fsid
.files
== 0)
201 errx(FSCK_EX_UNCORRECTED
, _("zero file count"));
202 if (*length
< super
.size
)
203 errx(FSCK_EX_UNCORRECTED
, _("file length too short"));
204 else if (*length
> super
.size
)
205 warnx(_("file extends past end of filesystem"));
207 warnx(_("old cramfs format"));
210 static void test_crc(int start
)
215 if (!(super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
)) {
216 warnx(_("unable to test CRC: old cramfs format"));
220 crc
= crc32(0L, NULL
, 0);
223 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
224 if (buf
== MAP_FAILED
) {
226 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
,
227 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
228 if (buf
!= MAP_FAILED
) {
230 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
231 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
232 tmp
= read(fd
, buf
, super
.size
);
234 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
235 if (tmp
!= (ssize_t
) super
.size
)
236 errx(FSCK_EX_ERROR
, _("failed to read %"PRIu32
" bytes from file %s"),
237 super
.size
, filename
);
240 if (buf
!= MAP_FAILED
) {
241 ((struct cramfs_super
*)((unsigned char *) buf
+ start
))->fsid
.crc
=
243 crc
= crc32(crc
, (unsigned char *) buf
+ start
, super
.size
- start
);
244 munmap(buf
, super
.size
);
250 if (lseek(fd
, start
, SEEK_SET
) == (off_t
) -1)
251 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
253 retval
= read(fd
, buf
, 4096);
255 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
256 else if (retval
== 0)
259 ((struct cramfs_super
*)buf
)->fsid
.crc
=
262 if (length
> (super
.size
- start
)) {
263 crc
= crc32(crc
, buf
,
265 (super
.size
- start
)));
268 crc
= crc32(crc
, buf
, retval
);
273 if (crc
!= super
.fsid
.crc
)
274 errx(FSCK_EX_UNCORRECTED
, _("crc error"));
277 static void print_node(char type
, struct cramfs_inode
*i
, char *name
)
281 if (S_ISCHR(i
->mode
) || (S_ISBLK(i
->mode
)))
282 /* major/minor numbers can be as high as 2^12 or 4096 */
283 snprintf(info
, 10, "%4d,%4d", major(i
->size
), minor(i
->size
));
285 /* size be as high as 2^24 or 16777216 */
286 snprintf(info
, 10, "%9d", i
->size
);
288 printf("%c %04o %s %5d:%-3d %s\n",
289 type
, i
->mode
& ~S_IFMT
, info
, i
->uid
, i
->gid
,
290 !*name
&& type
== 'd' ? "/" : name
);
294 * Create a fake "blocked" access
296 static void *romfs_read(unsigned long offset
)
298 unsigned int block
= offset
>> ROMBUFFER_BITS
;
299 if (block
!= read_buffer_block
) {
302 read_buffer_block
= block
;
303 if (lseek(fd
, block
<< ROMBUFFER_BITS
, SEEK_SET
) == (off_t
) -1)
304 warn(_("seek failed"));
306 x
= read(fd
, read_buffer
, ROMBUFFERSIZE
* 2);
308 warn(_("read romfs failed"));
310 return read_buffer
+ (offset
& ROMBUFFERMASK
);
313 static struct cramfs_inode
*cramfs_iget(struct cramfs_inode
*i
)
315 struct cramfs_inode
*inode
= xmalloc(sizeof(struct cramfs_inode
));
317 inode_to_host(cramfs_is_big_endian
, i
, inode
);
321 static struct cramfs_inode
*iget(unsigned int ino
)
323 return cramfs_iget(romfs_read(ino
));
326 static void iput(struct cramfs_inode
*inode
)
332 * Return the offset of the root directory
334 static struct cramfs_inode
*read_super(void)
336 struct cramfs_inode
*root
= cramfs_iget(&super
.root
);
337 unsigned long offset
= root
->offset
<< 2;
339 if (!S_ISDIR(root
->mode
))
340 errx(FSCK_EX_UNCORRECTED
, _("root inode is not directory"));
341 if (!(super
.flags
& CRAMFS_FLAG_SHIFTED_ROOT_OFFSET
) &&
342 ((offset
!= sizeof(struct cramfs_super
)) &&
343 (offset
!= PAD_SIZE
+ sizeof(struct cramfs_super
)))) {
344 errx(FSCK_EX_UNCORRECTED
, _("bad root offset (%lu)"), offset
);
349 static int uncompress_block(void *src
, size_t len
)
353 stream
.next_in
= src
;
354 stream
.avail_in
= len
;
356 stream
.next_out
= (unsigned char *)outbuffer
;
357 stream
.avail_out
= blksize
* 2;
359 inflateReset(&stream
);
361 if (len
> blksize
* 2)
362 errx(FSCK_EX_UNCORRECTED
, _("data block too large"));
364 err
= inflate(&stream
, Z_FINISH
);
365 if (err
!= Z_STREAM_END
)
366 errx(FSCK_EX_UNCORRECTED
, _("decompression error: %s"),
368 return stream
.total_out
;
375 static void do_uncompress(char *path
, int outfd
, unsigned long offset
,
378 unsigned long curr
= offset
+ 4 * ((size
+ blksize
- 1) / blksize
);
381 unsigned long out
= blksize
;
382 unsigned long next
= u32_toggle_endianness(cramfs_is_big_endian
,
392 printf(_(" hole at %lu (%zu)\n"), curr
,
396 memset(outbuffer
, 0x00, out
);
399 printf(_(" uncompressing block at %lu to %lu (%lu)\n"),
400 curr
, next
, next
- curr
);
401 out
= uncompress_block(romfs_read(curr
), next
- curr
);
403 if (size
>= blksize
) {
405 errx(FSCK_EX_UNCORRECTED
,
406 _("non-block (%ld) bytes"), out
);
409 errx(FSCK_EX_UNCORRECTED
,
410 _("non-size (%ld vs %ld) bytes"), out
,
414 if (*extract_dir
!= '\0' && write(outfd
, outbuffer
, out
) < 0)
415 err(FSCK_EX_ERROR
, _("write failed: %s"), path
);
419 static void change_file_status(char *path
, struct cramfs_inode
*i
)
421 const struct timeval epoch
[] = { {0,0}, {0,0} };
424 if (lchown(path
, i
->uid
, i
->gid
) < 0)
425 err(FSCK_EX_ERROR
, _("lchown failed: %s"), path
);
426 if (S_ISLNK(i
->mode
))
428 if (((S_ISUID
| S_ISGID
) & i
->mode
) && chmod(path
, i
->mode
) < 0)
429 err(FSCK_EX_ERROR
, _("chown failed: %s"), path
);
431 if (S_ISLNK(i
->mode
))
433 if (utimes(path
, epoch
) < 0)
434 err(FSCK_EX_ERROR
, _("utimes failed: %s"), path
);
437 static void do_directory(char *path
, struct cramfs_inode
*i
)
439 int pathlen
= strlen(path
);
441 unsigned long offset
= i
->offset
<< 2;
442 char *newpath
= xmalloc(pathlen
+ 256);
444 if (offset
== 0 && count
!= 0)
445 errx(FSCK_EX_UNCORRECTED
,
446 _("directory inode has zero offset and non-zero size: %s"),
449 if (offset
!= 0 && offset
< start_dir
)
452 /* TODO: Do we need to check end_dir for empty case? */
453 memcpy(newpath
, path
, pathlen
);
454 newpath
[pathlen
] = '/';
457 print_node('d', i
, path
);
459 if (*extract_dir
!= '\0') {
460 if (mkdir(path
, i
->mode
) < 0)
461 err(FSCK_EX_ERROR
, _("mkdir failed: %s"), path
);
462 change_file_status(path
, i
);
465 struct cramfs_inode
*child
= iget(offset
);
467 int newlen
= child
->namelen
<< 2;
469 size
= sizeof(struct cramfs_inode
) + newlen
;
472 offset
+= sizeof(struct cramfs_inode
);
474 memcpy(newpath
+ pathlen
, romfs_read(offset
), newlen
);
475 newpath
[pathlen
+ newlen
] = 0;
477 errx(FSCK_EX_UNCORRECTED
, _("filename length is zero"));
478 if ((pathlen
+ newlen
) - strlen(newpath
) > 3)
479 errx(FSCK_EX_UNCORRECTED
, _("bad filename length"));
480 expand_fs(newpath
, child
);
484 if (offset
<= start_dir
)
485 errx(FSCK_EX_UNCORRECTED
, _("bad inode offset"));
486 if (offset
> end_dir
)
488 iput(child
); /* free(child) */
493 static void do_file(char *path
, struct cramfs_inode
*i
)
495 unsigned long offset
= i
->offset
<< 2;
498 if (offset
== 0 && i
->size
!= 0)
499 errx(FSCK_EX_UNCORRECTED
,
500 _("file inode has zero offset and non-zero size"));
501 if (i
->size
== 0 && offset
!= 0)
502 errx(FSCK_EX_UNCORRECTED
,
503 _("file inode has zero size and non-zero offset"));
504 if (offset
!= 0 && offset
< start_data
)
507 print_node('f', i
, path
);
508 if (*extract_dir
!= '\0') {
509 outfd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, i
->mode
);
511 err(FSCK_EX_ERROR
, _("cannot open %s"), path
);
514 do_uncompress(path
, outfd
, offset
, i
->size
);
515 if ( *extract_dir
!= '\0') {
516 if (close_fd(outfd
) != 0)
517 err(FSCK_EX_ERROR
, _("write failed: %s"), path
);
518 change_file_status(path
, i
);
522 static void do_symlink(char *path
, struct cramfs_inode
*i
)
524 unsigned long offset
= i
->offset
<< 2;
525 unsigned long curr
= offset
+ 4;
527 u32_toggle_endianness(cramfs_is_big_endian
,
528 *(uint32_t *) romfs_read(offset
));
532 errx(FSCK_EX_UNCORRECTED
, _("symbolic link has zero offset"));
534 errx(FSCK_EX_UNCORRECTED
, _("symbolic link has zero size"));
536 if (offset
< start_data
)
541 size
= uncompress_block(romfs_read(curr
), next
- curr
);
543 errx(FSCK_EX_UNCORRECTED
, _("size error in symlink: %s"), path
);
548 xasprintf(&str
, "%s -> %s", path
, outbuffer
);
549 print_node('l', i
, str
);
551 printf(_(" uncompressing block at %lu to %lu (%lu)\n"),
552 curr
, next
, next
- curr
);
555 if (*extract_dir
!= '\0') {
556 if (symlink(outbuffer
, path
) < 0)
557 err(FSCK_EX_ERROR
, _("symlink failed: %s"), path
);
558 change_file_status(path
, i
);
562 static void do_special_inode(char *path
, struct cramfs_inode
*i
)
568 /* no need to shift offset */
569 errx(FSCK_EX_UNCORRECTED
,
570 _("special file has non-zero offset: %s"), path
);
572 if (S_ISCHR(i
->mode
)) {
575 } else if (S_ISBLK(i
->mode
)) {
578 } else if (S_ISFIFO(i
->mode
)) {
580 errx(FSCK_EX_UNCORRECTED
, _("fifo has non-zero size: %s"),
583 } else if (S_ISSOCK(i
->mode
)) {
585 errx(FSCK_EX_UNCORRECTED
,
586 _("socket has non-zero size: %s"), path
);
589 errx(FSCK_EX_UNCORRECTED
, _("bogus mode: %s (%o)"), path
, i
->mode
);
590 return; /* not reached */
594 print_node(type
, i
, path
);
596 if (*extract_dir
!= '\0') {
597 if (mknod(path
, i
->mode
, devtype
) < 0)
598 err(FSCK_EX_ERROR
, _("mknod failed: %s"), path
);
599 change_file_status(path
, i
);
603 static void expand_fs(char *path
, struct cramfs_inode
*inode
)
605 if (S_ISDIR(inode
->mode
))
606 do_directory(path
, inode
);
607 else if (S_ISREG(inode
->mode
))
608 do_file(path
, inode
);
609 else if (S_ISLNK(inode
->mode
))
610 do_symlink(path
, inode
);
612 do_special_inode(path
, inode
);
615 static void test_fs(int start
)
617 struct cramfs_inode
*root
;
622 stream
.next_in
= NULL
;
624 inflateInit(&stream
);
625 expand_fs(extract_dir
, root
);
627 if (start_data
!= ~0UL) {
628 if (start_data
< (sizeof(struct cramfs_super
) + start
))
629 errx(FSCK_EX_UNCORRECTED
,
630 _("directory data start (%lu) < sizeof(struct cramfs_super) + start (%zu)"),
631 start_data
, sizeof(struct cramfs_super
) + start
);
632 if (end_dir
!= start_data
)
633 errx(FSCK_EX_UNCORRECTED
,
634 _("directory data end (%lu) != file data start (%lu)"),
635 end_dir
, start_data
);
637 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
&& end_data
> super
.size
)
638 errx(FSCK_EX_UNCORRECTED
, _("invalid file data offset"));
640 iput(root
); /* free(root) */
643 int main(int argc
, char **argv
)
645 int c
; /* for getopt */
649 static const struct option longopts
[] = {
650 {"verbose", no_argument
, NULL
, 'v'},
651 {"version", no_argument
, NULL
, 'V'},
652 {"help", no_argument
, NULL
, 'h'},
653 {"blocksize", required_argument
, NULL
, 'b'},
654 {"extract", optional_argument
, NULL
, 'x'},
658 setlocale(LC_MESSAGES
, "");
659 setlocale(LC_CTYPE
, "");
660 bindtextdomain(PACKAGE
, LOCALEDIR
);
662 close_stdout_atexit();
664 strutils_set_exitcode(FSCK_EX_USAGE
);
666 /* command line options */
667 while ((c
= getopt_long(argc
, argv
, "ayvVhb:", longopts
, NULL
)) != EOF
)
669 case 'a': /* ignore */
676 print_version(FSCK_EX_OK
);
680 extract_dir
= optarg
;
686 blksize
= strtou32_or_err(optarg
, _("invalid blocksize argument"));
689 errtryhelp(FSCK_EX_USAGE
);
692 if ((argc
- optind
) != 1){
693 warnx(_("bad usage"));
694 errtryhelp(FSCK_EX_USAGE
);
696 filename
= argv
[optind
];
698 test_super(&start
, &length
);
703 blksize
= getpagesize();
704 outbuffer
= xmalloc(blksize
* 2);
709 printf(_("%s: OK\n"), filename
);