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)
36 /* compile-time options */
37 //#define INCLUDE_FS_TESTS /* include cramfs checking and extraction */
52 #include <sys/types.h>
55 #include <sys/sysmacros.h> /* for major, minor */
61 #include "exitcodes.h"
63 #define XALLOC_EXIT_CODE FSCK_ERROR
66 static const char *progname
= "cramfsck";
68 static int fd
; /* ROM image file descriptor */
69 static char *filename
; /* ROM image filename */
70 struct cramfs_super super
; /* just find the cramfs superblock once */
71 static int cramfs_is_big_endian
= 0; /* source is big endian */
72 static int opt_verbose
= 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */
74 char *extract_dir
= ""; /* extraction directory (-x) */
78 #ifdef INCLUDE_FS_TESTS
80 static int opt_extract
= 0; /* extract cramfs (-x) */
82 static uid_t euid
; /* effective UID */
84 /* (cramfs_super + start) <= start_dir < end_dir <= start_data <= end_data */
85 static unsigned long start_dir
= ~0UL; /* start of first non-root inode */
86 static unsigned long end_dir
= 0; /* end of the directory structure */
87 static unsigned long start_data
= ~0UL; /* start of the data (256 MB = max) */
88 static unsigned long end_data
= 0; /* end of the data */
91 /* Guarantee access to at least 8kB at a time */
92 #define ROMBUFFER_BITS 13
93 #define ROMBUFFERSIZE (1 << ROMBUFFER_BITS)
94 #define ROMBUFFERMASK (ROMBUFFERSIZE - 1)
95 static char read_buffer
[ROMBUFFERSIZE
* 2];
96 static unsigned long read_buffer_block
= ~0UL;
98 static z_stream stream
;
101 static void expand_fs(char *, struct cramfs_inode
*);
102 #endif /* INCLUDE_FS_TESTS */
104 static char *outbuffer
;
106 static size_t page_size
;
108 /* Input status of 0 to print help and exit without an error. */
109 static void __attribute__((__noreturn__
)) usage(int status
)
111 FILE *stream
= status
? stderr
: stdout
;
113 fprintf(stream
, _("usage: %s [-hv] [-x dir] file\n"
114 " -h print this help\n"
115 " -x dir extract into dir\n"
116 " -v be more verbose\n"
117 " file file to test\n"), progname
);
122 int get_superblock_endianness(uint32_t magic
)
124 if (magic
== CRAMFS_MAGIC
) {
125 cramfs_is_big_endian
= HOST_IS_BIG_ENDIAN
;
128 u32_toggle_endianness(!HOST_IS_BIG_ENDIAN
, CRAMFS_MAGIC
)) {
129 cramfs_is_big_endian
= !HOST_IS_BIG_ENDIAN
;
135 static void test_super(int *start
, size_t * length
)
139 /* find the physical size of the file or block device */
140 if (stat(filename
, &st
) < 0)
141 err(FSCK_ERROR
, _("stat failed: %s"), filename
);
143 fd
= open(filename
, O_RDONLY
);
145 err(FSCK_ERROR
, _("open failed: %s"), filename
);
147 if (S_ISBLK(st
.st_mode
)) {
148 unsigned long long bytes
;
149 if (blkdev_get_size(fd
, &bytes
))
151 _("ioctl failed: unable to determine device size: %s"),
154 } else if (S_ISREG(st
.st_mode
))
155 *length
= st
.st_size
;
157 errx(FSCK_ERROR
, _("not a block device or file: %s"), filename
);
159 if (*length
< sizeof(struct cramfs_super
))
160 errx(FSCK_UNCORRECTED
, _("file length too short"));
162 /* find superblock */
163 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
164 err(FSCK_ERROR
, _("read failed: %s"), filename
);
165 if (get_superblock_endianness(super
.magic
) != -1)
167 else if (*length
>= (PAD_SIZE
+ sizeof(super
))) {
168 lseek(fd
, PAD_SIZE
, SEEK_SET
);
169 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
170 err(FSCK_ERROR
, _("read failed: %s"), filename
);
171 if (get_superblock_endianness(super
.magic
) != -1)
174 errx(FSCK_UNCORRECTED
, _("superblock magic not found"));
176 errx(FSCK_UNCORRECTED
, _("superblock magic not found"));
179 printf(_("cramfs endianness is %s\n"),
180 cramfs_is_big_endian
? _("big") : _("little"));
182 super_toggle_endianness(cramfs_is_big_endian
, &super
);
183 if (super
.flags
& ~CRAMFS_SUPPORTED_FLAGS
)
184 errx(FSCK_ERROR
, _("unsupported filesystem features"));
186 if (super
.size
< page_size
)
187 errx(FSCK_UNCORRECTED
, _("superblock size (%d) too small"),
190 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
) {
191 if (super
.fsid
.files
== 0)
192 errx(FSCK_UNCORRECTED
, _("zero file count"));
193 if (*length
< super
.size
)
194 errx(FSCK_UNCORRECTED
, _("file length too short"));
195 else if (*length
> super
.size
)
197 _("warning: file extends past end of filesystem\n"));
199 fprintf(stderr
, _("warning: old cramfs format\n"));
202 static void test_crc(int start
)
207 if (!(super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
)) {
208 #ifdef INCLUDE_FS_TESTS
211 errx(FSCK_USAGE
, _("unable to test CRC: old cramfs format"));
215 crc
= crc32(0L, Z_NULL
, 0);
218 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
219 if (buf
== MAP_FAILED
) {
221 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
,
222 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
223 if (buf
!= MAP_FAILED
) {
224 lseek(fd
, 0, SEEK_SET
);
225 if (read(fd
, buf
, super
.size
) < 0)
226 err(FSCK_ERROR
, _("read failed: %s"), filename
);
229 if (buf
!= MAP_FAILED
) {
230 ((struct cramfs_super
*)(buf
+ start
))->fsid
.crc
=
231 crc32(0L, Z_NULL
, 0);
232 crc
= crc32(crc
, buf
+ start
, super
.size
- start
);
233 munmap(buf
, super
.size
);
239 lseek(fd
, start
, SEEK_SET
);
241 retval
= read(fd
, buf
, 4096);
243 err(FSCK_ERROR
, _("read failed: %s"), filename
);
244 else if (retval
== 0)
247 ((struct cramfs_super
*)buf
)->fsid
.crc
=
248 crc32(0L, Z_NULL
, 0);
250 if (length
> (super
.size
- start
)) {
251 crc
= crc32(crc
, buf
,
253 (super
.size
- start
)));
256 crc
= crc32(crc
, buf
, retval
);
261 if (crc
!= super
.fsid
.crc
)
262 errx(FSCK_UNCORRECTED
, _("crc error"));
265 #ifdef INCLUDE_FS_TESTS
266 static void print_node(char type
, struct cramfs_inode
*i
, char *name
)
270 if (S_ISCHR(i
->mode
) || (S_ISBLK(i
->mode
)))
271 /* major/minor numbers can be as high as 2^12 or 4096 */
272 snprintf(info
, 10, "%4d,%4d", major(i
->size
), minor(i
->size
));
274 /* size be as high as 2^24 or 16777216 */
275 snprintf(info
, 10, "%9d", i
->size
);
277 printf("%c %04o %s %5d:%-3d %s\n",
278 type
, i
->mode
& ~S_IFMT
, info
, i
->uid
, i
->gid
,
279 !*name
&& type
== 'd' ? "/" : name
);
283 * Create a fake "blocked" access
285 static void *romfs_read(unsigned long offset
)
287 unsigned int block
= offset
>> ROMBUFFER_BITS
;
288 if (block
!= read_buffer_block
) {
289 read_buffer_block
= block
;
290 lseek(fd
, block
<< ROMBUFFER_BITS
, SEEK_SET
);
291 read(fd
, read_buffer
, ROMBUFFERSIZE
* 2);
293 return read_buffer
+ (offset
& ROMBUFFERMASK
);
296 static struct cramfs_inode
*cramfs_iget(struct cramfs_inode
*i
)
298 struct cramfs_inode
*inode
= xmalloc(sizeof(struct cramfs_inode
));
300 inode_to_host(cramfs_is_big_endian
, i
, inode
);
304 static struct cramfs_inode
*iget(unsigned int ino
)
306 return cramfs_iget(romfs_read(ino
));
309 static void iput(struct cramfs_inode
*inode
)
315 * Return the offset of the root directory
317 static struct cramfs_inode
*read_super(void)
319 struct cramfs_inode
*root
= cramfs_iget(&super
.root
);
320 unsigned long offset
= root
->offset
<< 2;
322 if (!S_ISDIR(root
->mode
))
323 errx(FSCK_UNCORRECTED
, _("root inode is not directory"));
324 if (!(super
.flags
& CRAMFS_FLAG_SHIFTED_ROOT_OFFSET
) &&
325 ((offset
!= sizeof(struct cramfs_super
)) &&
326 (offset
!= PAD_SIZE
+ sizeof(struct cramfs_super
)))) {
327 errx(FSCK_UNCORRECTED
, _("bad root offset (%lu)"), offset
);
332 static int uncompress_block(void *src
, int len
)
336 stream
.next_in
= src
;
337 stream
.avail_in
= len
;
339 stream
.next_out
= (unsigned char *)outbuffer
;
340 stream
.avail_out
= page_size
* 2;
342 inflateReset(&stream
);
344 if (len
> page_size
* 2)
345 errx(FSCK_UNCORRECTED
, _("data block too large"));
347 err
= inflate(&stream
, Z_FINISH
);
348 if (err
!= Z_STREAM_END
)
349 errx(FSCK_UNCORRECTED
, _("decompression error %p(%d): %s"),
350 zError(err
), src
, len
);
351 return stream
.total_out
;
358 static void do_uncompress(char *path
, int fd
, unsigned long offset
,
361 unsigned long curr
= offset
+ 4 * ((size
+ page_size
- 1) / page_size
);
364 unsigned long out
= page_size
;
365 unsigned long next
= u32_toggle_endianness(cramfs_is_big_endian
,
375 printf(_(" hole at %ld (%zd)\n"), curr
,
377 if (size
< page_size
)
379 memset(outbuffer
, 0x00, out
);
382 printf(_(" uncompressing block at %ld to %ld (%ld)\n"),
383 curr
, next
, next
- curr
);
384 out
= uncompress_block(romfs_read(curr
), next
- curr
);
386 if (size
>= page_size
) {
387 if (out
!= page_size
)
388 errx(FSCK_UNCORRECTED
,
389 _("non-block (%ld) bytes"), out
);
392 errx(FSCK_UNCORRECTED
,
393 _("non-size (%ld vs %ld) bytes"), out
,
398 if (write(fd
, outbuffer
, out
) < 0)
399 err(FSCK_ERROR
, _("write failed: %s"),
405 static void change_file_status(char *path
, struct cramfs_inode
*i
)
407 struct utimbuf epoch
= { 0, 0 };
410 if (lchown(path
, i
->uid
, i
->gid
) < 0)
411 err(FSCK_ERROR
, _("lchown failed: %s"), path
);
412 if (S_ISLNK(i
->mode
))
414 if (((S_ISUID
| S_ISGID
) & i
->mode
) && chmod(path
, i
->mode
) < 0)
415 err(FSCK_ERROR
, _("chown failed: %s"), path
);
417 if (S_ISLNK(i
->mode
))
419 if (utime(path
, &epoch
) < 0)
420 err(FSCK_ERROR
, _("utime failed: %s"), path
);
423 static void do_directory(char *path
, struct cramfs_inode
*i
)
425 int pathlen
= strlen(path
);
427 unsigned long offset
= i
->offset
<< 2;
428 char *newpath
= xmalloc(pathlen
+ 256);
430 if (offset
== 0 && count
!= 0)
431 errx(FSCK_UNCORRECTED
,
432 _("directory inode has zero offset and non-zero size: %s"),
435 if (offset
!= 0 && offset
< start_dir
)
438 /* TODO: Do we need to check end_dir for empty case? */
439 memcpy(newpath
, path
, pathlen
);
440 newpath
[pathlen
] = '/';
443 print_node('d', i
, path
);
446 if (mkdir(path
, i
->mode
) < 0)
447 err(FSCK_ERROR
, _("mkdir failed: %s"), path
);
448 change_file_status(path
, i
);
451 struct cramfs_inode
*child
= iget(offset
);
453 int newlen
= child
->namelen
<< 2;
455 size
= sizeof(struct cramfs_inode
) + newlen
;
458 offset
+= sizeof(struct cramfs_inode
);
460 memcpy(newpath
+ pathlen
, romfs_read(offset
), newlen
);
461 newpath
[pathlen
+ newlen
] = 0;
463 errx(FSCK_UNCORRECTED
, _("filename length is zero"));
464 if ((pathlen
+ newlen
) - strlen(newpath
) > 3)
465 errx(FSCK_UNCORRECTED
, _("bad filename length"));
466 expand_fs(newpath
, child
);
470 if (offset
<= start_dir
)
471 errx(FSCK_UNCORRECTED
, _("bad inode offset"));
472 if (offset
> end_dir
)
474 iput(child
); /* free(child) */
479 static void do_file(char *path
, struct cramfs_inode
*i
)
481 unsigned long offset
= i
->offset
<< 2;
484 if (offset
== 0 && i
->size
!= 0)
485 errx(FSCK_UNCORRECTED
,
486 _("file inode has zero offset and non-zero size"));
487 if (i
->size
== 0 && offset
!= 0)
488 errx(FSCK_UNCORRECTED
,
489 _("file inode has zero size and non-zero offset"));
490 if (offset
!= 0 && offset
< start_data
)
493 print_node('f', i
, path
);
495 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, i
->mode
);
497 err(FSCK_ERROR
, _("open failed: %s"), path
);
500 do_uncompress(path
, fd
, offset
, i
->size
);
503 change_file_status(path
, i
);
507 static void do_symlink(char *path
, struct cramfs_inode
*i
)
509 unsigned long offset
= i
->offset
<< 2;
510 unsigned long curr
= offset
+ 4;
512 u32_toggle_endianness(cramfs_is_big_endian
,
513 *(uint32_t *) romfs_read(offset
));
517 errx(FSCK_UNCORRECTED
, _("symbolic link has zero offset"));
519 errx(FSCK_UNCORRECTED
, _("symbolic link has zero size"));
521 if (offset
< start_data
)
526 size
= uncompress_block(romfs_read(curr
), next
- curr
);
528 errx(FSCK_UNCORRECTED
, _("size error in symlink: %s"), path
);
533 asprintf(&str
, "%s -> %s", path
, outbuffer
);
534 print_node('l', i
, str
);
536 printf(_(" uncompressing block at %ld to %ld (%ld)\n"),
537 curr
, next
, next
- curr
);
541 if (symlink(outbuffer
, path
) < 0)
542 err(FSCK_ERROR
, _("symlink failed: %s"), path
);
543 change_file_status(path
, i
);
547 static void do_special_inode(char *path
, struct cramfs_inode
*i
)
553 /* no need to shift offset */
554 errx(FSCK_UNCORRECTED
,
555 _("special file has non-zero offset: %s"), path
);
557 if (S_ISCHR(i
->mode
)) {
560 } else if (S_ISBLK(i
->mode
)) {
563 } else if (S_ISFIFO(i
->mode
)) {
565 errx(FSCK_UNCORRECTED
, _("fifo has non-zero size: %s"),
568 } else if (S_ISSOCK(i
->mode
)) {
570 errx(FSCK_UNCORRECTED
,
571 _("socket has non-zero size: %s"), path
);
574 errx(FSCK_UNCORRECTED
, _("bogus mode: %s (%o)"), path
, i
->mode
);
575 return; /* not reached */
579 print_node(type
, i
, path
);
582 if (mknod(path
, i
->mode
, devtype
) < 0)
583 err(FSCK_ERROR
, _("mknod failed: %s"), path
);
584 change_file_status(path
, i
);
588 static void expand_fs(char *path
, struct cramfs_inode
*inode
)
590 if (S_ISDIR(inode
->mode
))
591 do_directory(path
, inode
);
592 else if (S_ISREG(inode
->mode
))
593 do_file(path
, inode
);
594 else if (S_ISLNK(inode
->mode
))
595 do_symlink(path
, inode
);
597 do_special_inode(path
, inode
);
600 static void test_fs(int start
)
602 struct cramfs_inode
*root
;
607 stream
.next_in
= NULL
;
609 inflateInit(&stream
);
610 expand_fs(extract_dir
, root
);
612 if (start_data
!= ~0UL) {
613 if (start_data
< (sizeof(struct cramfs_super
) + start
))
614 errx(FSCK_UNCORRECTED
,
615 _("directory data start (%ld) < sizeof(struct cramfs_super) + start (%ld)"),
616 start_data
, sizeof(struct cramfs_super
) + start
);
617 if (end_dir
!= start_data
)
618 errx(FSCK_UNCORRECTED
,
619 _("directory data end (%ld) != file data start (%ld)"),
620 end_dir
, start_data
);
622 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
)
623 if (end_data
> super
.size
)
624 errx(FSCK_UNCORRECTED
, _("invalid file data offset"));
626 iput(root
); /* free(root) */
628 #endif /* INCLUDE_FS_TESTS */
630 int main(int argc
, char **argv
)
632 int c
; /* for getopt */
636 setlocale(LC_MESSAGES
, "");
637 bindtextdomain(PACKAGE
, LOCALEDIR
);
640 page_size
= getpagesize();
645 outbuffer
= xmalloc(page_size
* 2);
647 /* command line options */
648 while ((c
= getopt(argc
, argv
, "hx:v")) != EOF
)
654 #ifdef INCLUDE_FS_TESTS
656 extract_dir
= optarg
;
659 errx(FSCK_USAGE
, _("compiled without -x support"));
666 if ((argc
- optind
) != 1)
668 filename
= argv
[optind
];
670 test_super(&start
, &length
);
672 #ifdef INCLUDE_FS_TESTS
677 printf(_("%s: OK\n"), filename
);