2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * cramfsck - check a cramfs file system
6 * Copyright (C) 2000-2002 Transmeta Corporation
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.
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.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * 1999/12/03: Linus Torvalds (cramfs tester and unarchive program)
24 * 2000/06/03: Daniel Quinlan (CRC and length checking program)
25 * 2000/06/04: Daniel Quinlan (merged programs, added options, support
26 * for special files, preserve permissions and
27 * ownership, cramfs superblock v2, bogus mode
28 * test, pathname length test, etc.)
29 * 2000/06/06: Daniel Quinlan (support for holes, pretty-printing,
31 * 2000/07/11: Daniel Quinlan (file length tests, start at offset 0 or 512,
32 * fsck-compatible exit codes)
33 * 2000/07/15: Daniel Quinlan (initial support for block devices)
34 * 2002/01/10: Daniel Quinlan (additional checks, test more return codes,
35 * use read if mmap fails, standardize messages)
49 /* We don't use our include/crc32.h, but crc32 from zlib!
51 * The zlib implementation performs pre/post-conditioning. The util-linux
52 * imlemenation requires post-conditioning (xor) in the applications.
57 #include <sys/types.h>
65 #include "exitcodes.h"
67 #include "closestream.h"
69 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
72 static int fd
; /* ROM image file descriptor */
73 static char *filename
; /* ROM image filename */
74 static struct cramfs_super super
; /* just find the cramfs superblock once */
75 static int cramfs_is_big_endian
= 0; /* source is big endian */
76 static int opt_verbose
= 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */
77 static int opt_extract
= 0; /* extract cramfs (-x) */
78 static char *extract_dir
= ""; /* optional extraction directory (-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 2 * blksize at a time */
92 #define CRAMFS_ROMBUFFER_BITS 13
93 #define CRAMFS_ROMBUFFERSIZE (1 << CRAMFS_ROMBUFFER_BITS)
94 #define CRAMFS_ROMBUFFERMASK (CRAMFS_ROMBUFFERSIZE - 1)
96 /* Defaults, updated in main() according to block size */
97 static size_t rombufbits
= CRAMFS_ROMBUFFER_BITS
;
98 static size_t rombufsize
= CRAMFS_ROMBUFFERSIZE
;
99 static size_t rombufmask
= CRAMFS_ROMBUFFERMASK
;
101 static char *read_buffer
;
102 static unsigned long read_buffer_block
= ~0UL;
104 static z_stream stream
;
107 static void expand_fs(char *, struct cramfs_inode
*);
109 static char *outbuffer
;
111 static size_t blksize
= 0;
113 static void __attribute__((__noreturn__
)) usage(void)
117 fputs(USAGE_HEADER
, out
);
119 _(" %s [options] <file>\n"), program_invocation_short_name
);
121 fputs(USAGE_SEPARATOR
, out
);
122 fputs(_("Check and repair a compressed ROM filesystem.\n"), out
);
124 fputs(USAGE_OPTIONS
, out
);
125 fputs(_(" -a for compatibility only, ignored\n"), out
);
126 fputs(_(" -v, --verbose be more verbose\n"), out
);
127 fputs(_(" -y for compatibility only, ignored\n"), out
);
128 fputs(_(" -b, --blocksize <size> use this blocksize, defaults to page size\n"), out
);
129 fputs(_(" --extract[=<dir>] test uncompression, optionally extract into <dir>\n"), out
);
130 fputs(USAGE_SEPARATOR
, out
);
131 fprintf(out
, USAGE_HELP_OPTIONS(26));
133 fprintf(out
, USAGE_MAN_TAIL("fsck.cramfs(8)"));
137 static int get_superblock_endianness(uint32_t magic
)
139 if (magic
== CRAMFS_MAGIC
) {
140 cramfs_is_big_endian
= HOST_IS_BIG_ENDIAN
;
145 u32_toggle_endianness(!HOST_IS_BIG_ENDIAN
, CRAMFS_MAGIC
)) {
146 cramfs_is_big_endian
= !HOST_IS_BIG_ENDIAN
;
153 static void test_super(int *start
)
156 unsigned long long length
;
158 fd
= open(filename
, O_RDONLY
);
160 err(FSCK_EX_ERROR
, _("cannot open %s"), filename
);
162 /* find the physical size of the file or block device */
163 if (fstat(fd
, &st
) < 0)
164 err(FSCK_EX_ERROR
, _("stat of %s failed"), filename
);
166 if (S_ISBLK(st
.st_mode
)) {
167 if (blkdev_get_size(fd
, &length
))
169 _("ioctl failed: unable to determine device size: %s"),
171 } else if (S_ISREG(st
.st_mode
))
174 errx(FSCK_EX_ERROR
, _("not a block device or file: %s"), filename
);
176 if (length
< sizeof(struct cramfs_super
))
177 errx(FSCK_EX_UNCORRECTED
, _("file length too short"));
179 /* find superblock */
180 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
181 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
182 if (get_superblock_endianness(super
.magic
) != -1)
184 else if (length
>= (PAD_SIZE
+ sizeof(super
))) {
185 if (lseek(fd
, PAD_SIZE
, SEEK_SET
) == (off_t
) -1)
186 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
187 if (read(fd
, &super
, sizeof(super
)) != sizeof(super
))
188 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
189 if (get_superblock_endianness(super
.magic
) != -1)
192 errx(FSCK_EX_UNCORRECTED
, _("superblock magic not found"));
194 errx(FSCK_EX_UNCORRECTED
, _("superblock magic not found"));
197 printf(_("cramfs endianness is %s\n"),
198 cramfs_is_big_endian
? _("big") : _("little"));
200 super_toggle_endianness(cramfs_is_big_endian
, &super
);
201 if (super
.flags
& ~CRAMFS_SUPPORTED_FLAGS
)
202 errx(FSCK_EX_ERROR
, _("unsupported filesystem features"));
204 /* What are valid superblock sizes? */
205 if (super
.size
< *start
+ sizeof(struct cramfs_super
))
206 errx(FSCK_EX_UNCORRECTED
, _("superblock size (%d) too small"),
209 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
) {
210 if (super
.fsid
.files
== 0)
211 errx(FSCK_EX_UNCORRECTED
, _("zero file count"));
212 if (length
< super
.size
)
213 errx(FSCK_EX_UNCORRECTED
, _("file length too short"));
214 else if (length
> super
.size
)
215 warnx(_("file extends past end of filesystem"));
217 warnx(_("old cramfs format"));
220 static void test_crc(int start
)
225 if (!(super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
)) {
226 warnx(_("unable to test CRC: old cramfs format"));
230 crc
= crc32(0L, NULL
, 0);
233 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
234 if (buf
== MAP_FAILED
) {
236 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
,
237 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
238 if (buf
!= MAP_FAILED
) {
240 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
241 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
242 tmp
= read(fd
, buf
, super
.size
);
244 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
245 if (tmp
!= (ssize_t
) super
.size
)
246 errx(FSCK_EX_ERROR
, _("failed to read %"PRIu32
" bytes from file %s"),
247 super
.size
, filename
);
250 if (buf
!= MAP_FAILED
) {
251 ((struct cramfs_super
*)((unsigned char *) buf
+ start
))->fsid
.crc
=
253 crc
= crc32(crc
, (unsigned char *) buf
+ start
, super
.size
- start
);
254 munmap(buf
, super
.size
);
260 if (lseek(fd
, start
, SEEK_SET
) == (off_t
) -1)
261 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
263 retval
= read(fd
, buf
, 4096);
265 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
266 else if (retval
== 0)
269 ((struct cramfs_super
*)buf
)->fsid
.crc
=
272 if (length
> (super
.size
- start
)) {
273 crc
= crc32(crc
, buf
,
275 (super
.size
- start
)));
278 crc
= crc32(crc
, buf
, retval
);
283 if (crc
!= super
.fsid
.crc
)
284 errx(FSCK_EX_UNCORRECTED
, _("crc error"));
287 static void print_node(char type
, struct cramfs_inode
*i
, char *name
)
291 if (S_ISCHR(i
->mode
) || (S_ISBLK(i
->mode
)))
292 /* major/minor numbers can be as high as 2^12 or 4096 */
293 snprintf(info
, 10, "%4d,%4d", major(i
->size
), minor(i
->size
));
295 /* size be as high as 2^24 or 16777216 */
296 snprintf(info
, 10, "%9d", i
->size
);
298 printf("%c %04o %s %5d:%-3d %s\n",
299 type
, i
->mode
& ~S_IFMT
, info
, i
->uid
, i
->gid
,
300 !*name
&& type
== 'd' ? "/" : name
);
304 * Create a fake "blocked" access
306 static void *romfs_read(unsigned long offset
)
308 unsigned int block
= offset
>> rombufbits
;
309 if (block
!= read_buffer_block
) {
312 read_buffer_block
= block
;
313 if (lseek(fd
, block
<< rombufbits
, SEEK_SET
) == (off_t
) -1)
314 warn(_("seek failed"));
316 x
= read(fd
, read_buffer
, rombufsize
* 2);
318 warn(_("read romfs failed"));
320 return read_buffer
+ (offset
& rombufmask
);
323 static struct cramfs_inode
*cramfs_iget(struct cramfs_inode
*i
)
325 struct cramfs_inode
*inode
= xmalloc(sizeof(struct cramfs_inode
));
327 inode_to_host(cramfs_is_big_endian
, i
, inode
);
331 static struct cramfs_inode
*iget(unsigned int ino
)
333 return cramfs_iget(romfs_read(ino
));
336 static void iput(struct cramfs_inode
*inode
)
342 * Return the offset of the root directory
344 static struct cramfs_inode
*read_super(void)
346 struct cramfs_inode
*root
= cramfs_iget(&super
.root
);
347 unsigned long offset
= root
->offset
<< 2;
349 if (!S_ISDIR(root
->mode
))
350 errx(FSCK_EX_UNCORRECTED
, _("root inode is not directory"));
351 if (!(super
.flags
& CRAMFS_FLAG_SHIFTED_ROOT_OFFSET
) &&
352 ((offset
!= sizeof(struct cramfs_super
)) &&
353 (offset
!= PAD_SIZE
+ sizeof(struct cramfs_super
)))) {
354 errx(FSCK_EX_UNCORRECTED
, _("bad root offset (%lu)"), offset
);
359 static int uncompress_block(void *src
, size_t len
)
363 stream
.next_in
= src
;
364 stream
.avail_in
= len
;
366 stream
.next_out
= (unsigned char *)outbuffer
;
367 stream
.avail_out
= blksize
* 2;
369 inflateReset(&stream
);
371 if (len
> blksize
* 2)
372 errx(FSCK_EX_UNCORRECTED
, _("data block too large"));
374 err
= inflate(&stream
, Z_FINISH
);
375 if (err
!= Z_STREAM_END
)
376 errx(FSCK_EX_UNCORRECTED
, _("decompression error: %s"),
378 return stream
.total_out
;
385 static void do_uncompress(char *path
, int outfd
, unsigned long offset
,
388 unsigned long curr
= offset
+ 4 * ((size
+ blksize
- 1) / blksize
);
391 unsigned long out
= blksize
;
392 unsigned long next
= u32_toggle_endianness(cramfs_is_big_endian
,
402 printf(_(" hole at %lu (%zu)\n"), curr
,
406 memset(outbuffer
, 0x00, out
);
409 printf(_(" uncompressing block at %lu to %lu (%lu)\n"),
410 curr
, next
, next
- curr
);
411 out
= uncompress_block(romfs_read(curr
), next
- curr
);
413 if (size
>= blksize
) {
415 errx(FSCK_EX_UNCORRECTED
,
416 _("non-block (%ld) bytes"), out
);
419 errx(FSCK_EX_UNCORRECTED
,
420 _("non-size (%ld vs %ld) bytes"), out
,
424 if (*extract_dir
!= '\0' && write(outfd
, outbuffer
, out
) < 0)
425 err(FSCK_EX_ERROR
, _("write failed: %s"), path
);
429 static void change_file_status(char *path
, struct cramfs_inode
*i
)
431 const struct timeval epoch
[] = { {0,0}, {0,0} };
434 if (lchown(path
, i
->uid
, i
->gid
) < 0)
435 err(FSCK_EX_ERROR
, _("lchown failed: %s"), path
);
436 if (S_ISLNK(i
->mode
))
438 if (((S_ISUID
| S_ISGID
) & i
->mode
) && chmod(path
, i
->mode
) < 0)
439 err(FSCK_EX_ERROR
, _("chmod failed: %s"), path
);
441 if (S_ISLNK(i
->mode
))
443 if (utimes(path
, epoch
) < 0)
444 err(FSCK_EX_ERROR
, _("utimes failed: %s"), path
);
447 static int is_dangerous_filename(char *name
, int len
)
449 return (len
== 1 && name
[0] == '.') ||
450 (len
== 2 && name
[0] == '.' && name
[1] == '.');
453 static void __attribute__((__noreturn__
))
454 errx_path(const char *mesg
, const char *name
, size_t namelen
)
456 char buf
[PATH_MAX
] = { 0 };
458 namelen
= min(namelen
, sizeof(buf
) - 1);
459 memcpy(buf
, name
, namelen
);
461 errx(FSCK_EX_UNCORRECTED
, "%s: %s", mesg
, buf
);
464 static void do_directory(char *path
, struct cramfs_inode
*i
)
466 int pathlen
= strlen(path
);
468 unsigned long offset
= i
->offset
<< 2;
469 char *newpath
= xmalloc(pathlen
+ 256);
471 if (offset
== 0 && count
!= 0)
472 errx(FSCK_EX_UNCORRECTED
,
473 _("directory inode has zero offset and non-zero size: %s"),
476 if (offset
!= 0 && offset
< start_dir
)
479 /* TODO: Do we need to check end_dir for empty case? */
480 memcpy(newpath
, path
, pathlen
);
481 newpath
[pathlen
] = '/';
484 print_node('d', i
, path
);
486 if (*extract_dir
!= '\0') {
487 if (mkdir(path
, i
->mode
) < 0)
488 err(FSCK_EX_ERROR
, _("mkdir failed: %s"), path
);
489 change_file_status(path
, i
);
492 struct cramfs_inode
*child
= iget(offset
);
495 int newlen
= child
->namelen
<< 2;
497 size
= sizeof(struct cramfs_inode
) + newlen
;
500 offset
+= sizeof(struct cramfs_inode
);
501 name
= romfs_read(offset
);
503 if (memchr(name
, '/', newlen
) != NULL
)
504 errx_path(_("illegal filename"), name
, newlen
);
505 if (*extract_dir
!= '\0' && is_dangerous_filename(name
, newlen
))
506 errx_path(_("dangerous filename"), name
, newlen
);
507 memcpy(newpath
+ pathlen
, name
, newlen
);
508 newpath
[pathlen
+ newlen
] = 0;
510 errx(FSCK_EX_UNCORRECTED
, _("filename length is zero"));
511 if ((pathlen
+ newlen
) - strlen(newpath
) > 3)
512 errx(FSCK_EX_UNCORRECTED
, _("bad filename length"));
513 expand_fs(newpath
, child
);
517 if (offset
<= start_dir
)
518 errx(FSCK_EX_UNCORRECTED
, _("bad inode offset"));
519 if (offset
> end_dir
)
521 iput(child
); /* free(child) */
526 static void do_file(char *path
, struct cramfs_inode
*i
)
528 unsigned long offset
= i
->offset
<< 2;
531 if (offset
== 0 && i
->size
!= 0)
532 errx(FSCK_EX_UNCORRECTED
,
533 _("file inode has zero offset and non-zero size"));
534 if (i
->size
== 0 && offset
!= 0)
535 errx(FSCK_EX_UNCORRECTED
,
536 _("file inode has zero size and non-zero offset"));
537 if (offset
!= 0 && offset
< start_data
)
540 print_node('f', i
, path
);
541 if (*extract_dir
!= '\0') {
542 outfd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, i
->mode
);
544 err(FSCK_EX_ERROR
, _("cannot open %s"), path
);
547 do_uncompress(path
, outfd
, offset
, i
->size
);
548 if ( *extract_dir
!= '\0') {
549 if (close_fd(outfd
) != 0)
550 err(FSCK_EX_ERROR
, _("write failed: %s"), path
);
551 change_file_status(path
, i
);
555 static void do_symlink(char *path
, struct cramfs_inode
*i
)
557 unsigned long offset
= i
->offset
<< 2;
558 unsigned long curr
= offset
+ 4;
560 u32_toggle_endianness(cramfs_is_big_endian
,
561 *(uint32_t *) romfs_read(offset
));
565 errx(FSCK_EX_UNCORRECTED
, _("symbolic link has zero offset"));
567 errx(FSCK_EX_UNCORRECTED
, _("symbolic link has zero size"));
569 if (offset
< start_data
)
574 size
= uncompress_block(romfs_read(curr
), next
- curr
);
576 errx(FSCK_EX_UNCORRECTED
, _("size error in symlink: %s"), path
);
581 xasprintf(&str
, "%s -> %s", path
, outbuffer
);
582 print_node('l', i
, str
);
584 printf(_(" uncompressing block at %lu to %lu (%lu)\n"),
585 curr
, next
, next
- curr
);
588 if (*extract_dir
!= '\0') {
589 if (symlink(outbuffer
, path
) < 0)
590 err(FSCK_EX_ERROR
, _("symlink failed: %s"), path
);
591 change_file_status(path
, i
);
595 static void do_special_inode(char *path
, struct cramfs_inode
*i
)
601 /* no need to shift offset */
602 errx(FSCK_EX_UNCORRECTED
,
603 _("special file has non-zero offset: %s"), path
);
605 if (S_ISCHR(i
->mode
)) {
608 } else if (S_ISBLK(i
->mode
)) {
611 } else if (S_ISFIFO(i
->mode
)) {
613 errx(FSCK_EX_UNCORRECTED
, _("fifo has non-zero size: %s"),
616 } else if (S_ISSOCK(i
->mode
)) {
618 errx(FSCK_EX_UNCORRECTED
,
619 _("socket has non-zero size: %s"), path
);
622 errx(FSCK_EX_UNCORRECTED
, _("bogus mode: %s (%o)"), path
, i
->mode
);
623 return; /* not reached */
627 print_node(type
, i
, path
);
629 if (*extract_dir
!= '\0') {
630 if (mknod(path
, i
->mode
, devtype
) < 0)
631 err(FSCK_EX_ERROR
, _("mknod failed: %s"), path
);
632 change_file_status(path
, i
);
636 static void expand_fs(char *path
, struct cramfs_inode
*inode
)
638 if (S_ISDIR(inode
->mode
))
639 do_directory(path
, inode
);
640 else if (S_ISREG(inode
->mode
))
641 do_file(path
, inode
);
642 else if (S_ISLNK(inode
->mode
))
643 do_symlink(path
, inode
);
645 do_special_inode(path
, inode
);
648 static void test_fs(int start
)
650 struct cramfs_inode
*root
;
655 stream
.next_in
= NULL
;
657 inflateInit(&stream
);
658 expand_fs(extract_dir
, root
);
660 if (start_data
!= ~0UL) {
661 if (start_data
< (sizeof(struct cramfs_super
) + start
))
662 errx(FSCK_EX_UNCORRECTED
,
663 _("directory data start (%lu) < sizeof(struct cramfs_super) + start (%zu)"),
664 start_data
, sizeof(struct cramfs_super
) + start
);
665 if (end_dir
!= start_data
)
666 errx(FSCK_EX_UNCORRECTED
,
667 _("directory data end (%lu) != file data start (%lu)"),
668 end_dir
, start_data
);
670 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
&& end_data
> super
.size
)
671 errx(FSCK_EX_UNCORRECTED
, _("invalid file data offset"));
673 iput(root
); /* free(root) */
676 int main(int argc
, char **argv
)
678 int c
; /* for getopt */
681 static const struct option longopts
[] = {
682 {"verbose", no_argument
, NULL
, 'v'},
683 {"version", no_argument
, NULL
, 'V'},
684 {"help", no_argument
, NULL
, 'h'},
685 {"blocksize", required_argument
, NULL
, 'b'},
686 {"extract", optional_argument
, NULL
, 'x'},
690 setlocale(LC_MESSAGES
, "");
691 setlocale(LC_CTYPE
, "");
692 bindtextdomain(PACKAGE
, LOCALEDIR
);
694 close_stdout_atexit();
696 strutils_set_exitcode(FSCK_EX_USAGE
);
698 /* command line options */
699 while ((c
= getopt_long(argc
, argv
, "ayvVhb:", longopts
, NULL
)) != EOF
)
701 case 'a': /* ignore */
708 print_version(FSCK_EX_OK
);
712 extract_dir
= optarg
;
718 blksize
= strtou32_or_err(optarg
, _("invalid blocksize argument"));
721 errtryhelp(FSCK_EX_USAGE
);
724 if ((argc
- optind
) != 1){
725 warnx(_("bad usage"));
726 errtryhelp(FSCK_EX_USAGE
);
728 filename
= argv
[optind
];
737 blksize
= getpagesize();
739 /* re-calculate according to blksize */
740 bufsize
= rombufsize
= blksize
* 2;
742 while (bufsize
>>= 1)
744 rombufmask
= rombufsize
- 1;
746 outbuffer
= xmalloc(blksize
* 2);
747 read_buffer
= xmalloc(rombufsize
* 2);
752 printf(_("%s: OK\n"), filename
);