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 2 * blksize at a time */
90 #define CRAMFS_ROMBUFFER_BITS 13
91 #define CRAMFS_ROMBUFFERSIZE (1 << CRAMFS_ROMBUFFER_BITS)
92 #define CRAMFS_ROMBUFFERMASK (CRAMFS_ROMBUFFERSIZE - 1)
94 /* Defaults, updated in main() according to block size */
95 static size_t rombufbits
= CRAMFS_ROMBUFFER_BITS
;
96 static size_t rombufsize
= CRAMFS_ROMBUFFERSIZE
;
97 static size_t rombufmask
= CRAMFS_ROMBUFFERMASK
;
99 static char *read_buffer
;
100 static unsigned long read_buffer_block
= ~0UL;
102 static z_stream stream
;
105 static void expand_fs(char *, struct cramfs_inode
*);
107 static char *outbuffer
;
109 static size_t blksize
= 0;
111 static void __attribute__((__noreturn__
)) usage(void)
115 fputs(USAGE_HEADER
, out
);
117 _(" %s [options] <file>\n"), program_invocation_short_name
);
119 fputs(USAGE_SEPARATOR
, out
);
120 fputs(_("Check and repair a compressed ROM filesystem.\n"), out
);
122 fputs(USAGE_OPTIONS
, out
);
123 fputs(_(" -a for compatibility only, ignored\n"), out
);
124 fputs(_(" -v, --verbose be more verbose\n"), out
);
125 fputs(_(" -y for compatibility only, ignored\n"), out
);
126 fputs(_(" -b, --blocksize <size> use this blocksize, defaults to page size\n"), out
);
127 fputs(_(" --extract[=<dir>] test uncompression, optionally extract into <dir>\n"), out
);
128 fputs(USAGE_SEPARATOR
, out
);
129 fprintf(out
, USAGE_HELP_OPTIONS(26));
131 fprintf(out
, USAGE_MAN_TAIL("fsck.cramfs(8)"));
135 static int get_superblock_endianness(uint32_t magic
)
137 if (magic
== CRAMFS_MAGIC
) {
138 cramfs_is_big_endian
= HOST_IS_BIG_ENDIAN
;
143 u32_toggle_endianness(!HOST_IS_BIG_ENDIAN
, CRAMFS_MAGIC
)) {
144 cramfs_is_big_endian
= !HOST_IS_BIG_ENDIAN
;
151 static void test_super(int *start
)
154 unsigned long long length
;
156 fd
= open(filename
, O_RDONLY
);
158 err(FSCK_EX_ERROR
, _("cannot open %s"), filename
);
160 /* find the physical size of the file or block device */
161 if (fstat(fd
, &st
) < 0)
162 err(FSCK_EX_ERROR
, _("stat of %s failed"), filename
);
164 if (S_ISBLK(st
.st_mode
)) {
165 if (blkdev_get_size(fd
, &length
))
167 _("ioctl failed: unable to determine device size: %s"),
169 } else if (S_ISREG(st
.st_mode
))
172 errx(FSCK_EX_ERROR
, _("not a block device or file: %s"), filename
);
174 if (length
< sizeof(struct cramfs_super
))
175 errx(FSCK_EX_UNCORRECTED
, _("file length too short"));
177 /* find superblock */
178 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
179 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
180 if (get_superblock_endianness(super
.magic
) != -1)
182 else if (length
>= (PAD_SIZE
+ sizeof(super
))) {
183 if (lseek(fd
, PAD_SIZE
, SEEK_SET
) == (off_t
) -1)
184 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
185 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
186 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
187 if (get_superblock_endianness(super
.magic
) != -1)
190 errx(FSCK_EX_UNCORRECTED
, _("superblock magic not found"));
192 errx(FSCK_EX_UNCORRECTED
, _("superblock magic not found"));
195 printf(_("cramfs endianness is %s\n"),
196 cramfs_is_big_endian
? _("big") : _("little"));
198 super_toggle_endianness(cramfs_is_big_endian
, &super
);
199 if (super
.flags
& ~CRAMFS_SUPPORTED_FLAGS
)
200 errx(FSCK_EX_ERROR
, _("unsupported filesystem features"));
202 /* What are valid superblock sizes? */
203 if (super
.size
< *start
+ sizeof(struct cramfs_super
))
204 errx(FSCK_EX_UNCORRECTED
, _("superblock size (%d) too small"),
207 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
) {
208 if (super
.fsid
.files
== 0)
209 errx(FSCK_EX_UNCORRECTED
, _("zero file count"));
210 if (length
< super
.size
)
211 errx(FSCK_EX_UNCORRECTED
, _("file length too short"));
212 else if (length
> super
.size
)
213 warnx(_("file extends past end of filesystem"));
215 warnx(_("old cramfs format"));
218 static void test_crc(int start
)
223 if (!(super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
)) {
224 warnx(_("unable to test CRC: old cramfs format"));
228 crc
= crc32(0L, NULL
, 0);
231 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
232 if (buf
== MAP_FAILED
) {
234 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
,
235 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
236 if (buf
!= MAP_FAILED
) {
238 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
239 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
240 tmp
= read(fd
, buf
, super
.size
);
242 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
243 if (tmp
!= (ssize_t
) super
.size
)
244 errx(FSCK_EX_ERROR
, _("failed to read %"PRIu32
" bytes from file %s"),
245 super
.size
, filename
);
248 if (buf
!= MAP_FAILED
) {
249 ((struct cramfs_super
*)((unsigned char *) buf
+ start
))->fsid
.crc
=
251 crc
= crc32(crc
, (unsigned char *) buf
+ start
, super
.size
- start
);
252 munmap(buf
, super
.size
);
258 if (lseek(fd
, start
, SEEK_SET
) == (off_t
) -1)
259 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
261 retval
= read(fd
, buf
, 4096);
263 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
264 else if (retval
== 0)
267 ((struct cramfs_super
*)buf
)->fsid
.crc
=
270 if (length
> (super
.size
- start
)) {
271 crc
= crc32(crc
, buf
,
273 (super
.size
- start
)));
276 crc
= crc32(crc
, buf
, retval
);
281 if (crc
!= super
.fsid
.crc
)
282 errx(FSCK_EX_UNCORRECTED
, _("crc error"));
285 static void print_node(char type
, struct cramfs_inode
*i
, char *name
)
289 if (S_ISCHR(i
->mode
) || (S_ISBLK(i
->mode
)))
290 /* major/minor numbers can be as high as 2^12 or 4096 */
291 snprintf(info
, 10, "%4d,%4d", major(i
->size
), minor(i
->size
));
293 /* size be as high as 2^24 or 16777216 */
294 snprintf(info
, 10, "%9d", i
->size
);
296 printf("%c %04o %s %5d:%-3d %s\n",
297 type
, i
->mode
& ~S_IFMT
, info
, i
->uid
, i
->gid
,
298 !*name
&& type
== 'd' ? "/" : name
);
302 * Create a fake "blocked" access
304 static void *romfs_read(unsigned long offset
)
306 unsigned int block
= offset
>> rombufbits
;
307 if (block
!= read_buffer_block
) {
310 read_buffer_block
= block
;
311 if (lseek(fd
, block
<< rombufbits
, SEEK_SET
) == (off_t
) -1)
312 warn(_("seek failed"));
314 x
= read(fd
, read_buffer
, rombufsize
* 2);
316 warn(_("read romfs failed"));
318 return read_buffer
+ (offset
& rombufmask
);
321 static struct cramfs_inode
*cramfs_iget(struct cramfs_inode
*i
)
323 struct cramfs_inode
*inode
= xmalloc(sizeof(struct cramfs_inode
));
325 inode_to_host(cramfs_is_big_endian
, i
, inode
);
329 static struct cramfs_inode
*iget(unsigned int ino
)
331 return cramfs_iget(romfs_read(ino
));
334 static void iput(struct cramfs_inode
*inode
)
340 * Return the offset of the root directory
342 static struct cramfs_inode
*read_super(void)
344 struct cramfs_inode
*root
= cramfs_iget(&super
.root
);
345 unsigned long offset
= root
->offset
<< 2;
347 if (!S_ISDIR(root
->mode
))
348 errx(FSCK_EX_UNCORRECTED
, _("root inode is not directory"));
349 if (!(super
.flags
& CRAMFS_FLAG_SHIFTED_ROOT_OFFSET
) &&
350 ((offset
!= sizeof(struct cramfs_super
)) &&
351 (offset
!= PAD_SIZE
+ sizeof(struct cramfs_super
)))) {
352 errx(FSCK_EX_UNCORRECTED
, _("bad root offset (%lu)"), offset
);
357 static int uncompress_block(void *src
, size_t len
)
361 stream
.next_in
= src
;
362 stream
.avail_in
= len
;
364 stream
.next_out
= (unsigned char *)outbuffer
;
365 stream
.avail_out
= blksize
* 2;
367 inflateReset(&stream
);
369 if (len
> blksize
* 2)
370 errx(FSCK_EX_UNCORRECTED
, _("data block too large"));
372 err
= inflate(&stream
, Z_FINISH
);
373 if (err
!= Z_STREAM_END
)
374 errx(FSCK_EX_UNCORRECTED
, _("decompression error: %s"),
376 return stream
.total_out
;
383 static void do_uncompress(char *path
, int outfd
, unsigned long offset
,
386 unsigned long curr
= offset
+ 4 * ((size
+ blksize
- 1) / blksize
);
389 unsigned long out
= blksize
;
390 unsigned long next
= u32_toggle_endianness(cramfs_is_big_endian
,
400 printf(_(" hole at %lu (%zu)\n"), curr
,
404 memset(outbuffer
, 0x00, out
);
407 printf(_(" uncompressing block at %lu to %lu (%lu)\n"),
408 curr
, next
, next
- curr
);
409 out
= uncompress_block(romfs_read(curr
), next
- curr
);
411 if (size
>= blksize
) {
413 errx(FSCK_EX_UNCORRECTED
,
414 _("non-block (%ld) bytes"), out
);
417 errx(FSCK_EX_UNCORRECTED
,
418 _("non-size (%ld vs %ld) bytes"), out
,
422 if (*extract_dir
!= '\0' && write(outfd
, outbuffer
, out
) < 0)
423 err(FSCK_EX_ERROR
, _("write failed: %s"), path
);
427 static void change_file_status(char *path
, struct cramfs_inode
*i
)
429 const struct timeval epoch
[] = { {0,0}, {0,0} };
432 if (lchown(path
, i
->uid
, i
->gid
) < 0)
433 err(FSCK_EX_ERROR
, _("lchown failed: %s"), path
);
434 if (S_ISLNK(i
->mode
))
436 if (((S_ISUID
| S_ISGID
) & i
->mode
) && chmod(path
, i
->mode
) < 0)
437 err(FSCK_EX_ERROR
, _("chmod failed: %s"), path
);
439 if (S_ISLNK(i
->mode
))
441 if (utimes(path
, epoch
) < 0)
442 err(FSCK_EX_ERROR
, _("utimes failed: %s"), path
);
445 static int is_dangerous_filename(char *name
, int len
)
447 return (len
== 1 && name
[0] == '.') ||
448 (len
== 2 && name
[0] == '.' && name
[1] == '.');
451 static void __attribute__((__noreturn__
))
452 errx_path(const char *mesg
, const char *name
, size_t namelen
)
454 char buf
[PATH_MAX
] = { 0 };
456 namelen
= min(namelen
, sizeof(buf
) - 1);
457 memcpy(buf
, name
, namelen
);
459 errx(FSCK_EX_UNCORRECTED
, "%s: %s", mesg
, buf
);
462 static void do_directory(char *path
, struct cramfs_inode
*i
)
464 int pathlen
= strlen(path
);
466 unsigned long offset
= i
->offset
<< 2;
467 char *newpath
= xmalloc(pathlen
+ 256);
469 if (offset
== 0 && count
!= 0)
470 errx(FSCK_EX_UNCORRECTED
,
471 _("directory inode has zero offset and non-zero size: %s"),
474 if (offset
!= 0 && offset
< start_dir
)
477 /* TODO: Do we need to check end_dir for empty case? */
478 memcpy(newpath
, path
, pathlen
);
479 newpath
[pathlen
] = '/';
482 print_node('d', i
, path
);
484 if (*extract_dir
!= '\0') {
485 if (mkdir(path
, i
->mode
) < 0)
486 err(FSCK_EX_ERROR
, _("mkdir failed: %s"), path
);
487 change_file_status(path
, i
);
490 struct cramfs_inode
*child
= iget(offset
);
493 int newlen
= child
->namelen
<< 2;
495 size
= sizeof(struct cramfs_inode
) + newlen
;
498 offset
+= sizeof(struct cramfs_inode
);
499 name
= romfs_read(offset
);
501 if (memchr(name
, '/', newlen
) != NULL
)
502 errx_path(_("illegal filename"), name
, newlen
);
503 if (*extract_dir
!= '\0' && is_dangerous_filename(name
, newlen
))
504 errx_path(_("dangerous filename"), name
, newlen
);
505 memcpy(newpath
+ pathlen
, name
, newlen
);
506 newpath
[pathlen
+ newlen
] = 0;
508 errx(FSCK_EX_UNCORRECTED
, _("filename length is zero"));
509 if ((pathlen
+ newlen
) - strlen(newpath
) > 3)
510 errx(FSCK_EX_UNCORRECTED
, _("bad filename length"));
511 expand_fs(newpath
, child
);
515 if (offset
<= start_dir
)
516 errx(FSCK_EX_UNCORRECTED
, _("bad inode offset"));
517 if (offset
> end_dir
)
519 iput(child
); /* free(child) */
524 static void do_file(char *path
, struct cramfs_inode
*i
)
526 unsigned long offset
= i
->offset
<< 2;
529 if (offset
== 0 && i
->size
!= 0)
530 errx(FSCK_EX_UNCORRECTED
,
531 _("file inode has zero offset and non-zero size"));
532 if (i
->size
== 0 && offset
!= 0)
533 errx(FSCK_EX_UNCORRECTED
,
534 _("file inode has zero size and non-zero offset"));
535 if (offset
!= 0 && offset
< start_data
)
538 print_node('f', i
, path
);
539 if (*extract_dir
!= '\0') {
540 outfd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, i
->mode
);
542 err(FSCK_EX_ERROR
, _("cannot open %s"), path
);
545 do_uncompress(path
, outfd
, offset
, i
->size
);
546 if ( *extract_dir
!= '\0') {
547 if (close_fd(outfd
) != 0)
548 err(FSCK_EX_ERROR
, _("write failed: %s"), path
);
549 change_file_status(path
, i
);
553 static void do_symlink(char *path
, struct cramfs_inode
*i
)
555 unsigned long offset
= i
->offset
<< 2;
556 unsigned long curr
= offset
+ 4;
558 u32_toggle_endianness(cramfs_is_big_endian
,
559 *(uint32_t *) romfs_read(offset
));
563 errx(FSCK_EX_UNCORRECTED
, _("symbolic link has zero offset"));
565 errx(FSCK_EX_UNCORRECTED
, _("symbolic link has zero size"));
567 if (offset
< start_data
)
572 size
= uncompress_block(romfs_read(curr
), next
- curr
);
574 errx(FSCK_EX_UNCORRECTED
, _("size error in symlink: %s"), path
);
579 xasprintf(&str
, "%s -> %s", path
, outbuffer
);
580 print_node('l', i
, str
);
582 printf(_(" uncompressing block at %lu to %lu (%lu)\n"),
583 curr
, next
, next
- curr
);
586 if (*extract_dir
!= '\0') {
587 if (symlink(outbuffer
, path
) < 0)
588 err(FSCK_EX_ERROR
, _("symlink failed: %s"), path
);
589 change_file_status(path
, i
);
593 static void do_special_inode(char *path
, struct cramfs_inode
*i
)
599 /* no need to shift offset */
600 errx(FSCK_EX_UNCORRECTED
,
601 _("special file has non-zero offset: %s"), path
);
603 if (S_ISCHR(i
->mode
)) {
606 } else if (S_ISBLK(i
->mode
)) {
609 } else if (S_ISFIFO(i
->mode
)) {
611 errx(FSCK_EX_UNCORRECTED
, _("fifo has non-zero size: %s"),
614 } else if (S_ISSOCK(i
->mode
)) {
616 errx(FSCK_EX_UNCORRECTED
,
617 _("socket has non-zero size: %s"), path
);
620 errx(FSCK_EX_UNCORRECTED
, _("bogus mode: %s (%o)"), path
, i
->mode
);
621 return; /* not reached */
625 print_node(type
, i
, path
);
627 if (*extract_dir
!= '\0') {
628 if (mknod(path
, i
->mode
, devtype
) < 0)
629 err(FSCK_EX_ERROR
, _("mknod failed: %s"), path
);
630 change_file_status(path
, i
);
634 static void expand_fs(char *path
, struct cramfs_inode
*inode
)
636 if (S_ISDIR(inode
->mode
))
637 do_directory(path
, inode
);
638 else if (S_ISREG(inode
->mode
))
639 do_file(path
, inode
);
640 else if (S_ISLNK(inode
->mode
))
641 do_symlink(path
, inode
);
643 do_special_inode(path
, inode
);
646 static void test_fs(int start
)
648 struct cramfs_inode
*root
;
653 stream
.next_in
= NULL
;
655 inflateInit(&stream
);
656 expand_fs(extract_dir
, root
);
658 if (start_data
!= ~0UL) {
659 if (start_data
< (sizeof(struct cramfs_super
) + start
))
660 errx(FSCK_EX_UNCORRECTED
,
661 _("directory data start (%lu) < sizeof(struct cramfs_super) + start (%zu)"),
662 start_data
, sizeof(struct cramfs_super
) + start
);
663 if (end_dir
!= start_data
)
664 errx(FSCK_EX_UNCORRECTED
,
665 _("directory data end (%lu) != file data start (%lu)"),
666 end_dir
, start_data
);
668 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
&& end_data
> super
.size
)
669 errx(FSCK_EX_UNCORRECTED
, _("invalid file data offset"));
671 iput(root
); /* free(root) */
674 int main(int argc
, char **argv
)
676 int c
; /* for getopt */
679 static const struct option longopts
[] = {
680 {"verbose", no_argument
, NULL
, 'v'},
681 {"version", no_argument
, NULL
, 'V'},
682 {"help", no_argument
, NULL
, 'h'},
683 {"blocksize", required_argument
, NULL
, 'b'},
684 {"extract", optional_argument
, NULL
, 'x'},
688 setlocale(LC_MESSAGES
, "");
689 setlocale(LC_CTYPE
, "");
690 bindtextdomain(PACKAGE
, LOCALEDIR
);
692 close_stdout_atexit();
694 strutils_set_exitcode(FSCK_EX_USAGE
);
696 /* command line options */
697 while ((c
= getopt_long(argc
, argv
, "ayvVhb:", longopts
, NULL
)) != EOF
)
699 case 'a': /* ignore */
706 print_version(FSCK_EX_OK
);
710 extract_dir
= optarg
;
716 blksize
= strtou32_or_err(optarg
, _("invalid blocksize argument"));
719 errtryhelp(FSCK_EX_USAGE
);
722 if ((argc
- optind
) != 1){
723 warnx(_("bad usage"));
724 errtryhelp(FSCK_EX_USAGE
);
726 filename
= argv
[optind
];
735 blksize
= getpagesize();
737 /* re-calculate according to blksize */
738 bufsize
= rombufsize
= blksize
* 2;
740 while (bufsize
>>= 1)
742 rombufmask
= rombufsize
- 1;
744 outbuffer
= xmalloc(blksize
* 2);
745 read_buffer
= xmalloc(rombufsize
* 2);
750 printf(_("%s: OK\n"), filename
);