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
20 * along with this program. If not, see <https://gnu.org/licenses/>.
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,
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)
48 /* We don't use our include/crc32.h, but crc32 from zlib!
50 * The zlib implementation performs pre/post-conditioning. The util-linux
51 * imlemenation requires post-conditioning (xor) in the applications.
56 #include <sys/types.h>
64 #include "exitcodes.h"
66 #include "closestream.h"
68 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
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) */
81 static uid_t euid
; /* effective UID */
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 */
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)
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
;
100 static char *read_buffer
;
101 static unsigned long read_buffer_block
= ~0UL;
103 static z_stream stream
;
106 static void expand_fs(char *, struct cramfs_inode
*);
108 static char *outbuffer
;
110 static size_t blksize
= 0;
112 static void __attribute__((__noreturn__
)) usage(void)
116 fputs(USAGE_HEADER
, out
);
118 _(" %s [options] <file>\n"), program_invocation_short_name
);
120 fputs(USAGE_SEPARATOR
, out
);
121 fputs(_("Check and repair a compressed ROM filesystem.\n"), out
);
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));
132 fprintf(out
, USAGE_MAN_TAIL("fsck.cramfs(8)"));
136 static int get_superblock_endianness(uint32_t magic
)
138 if (magic
== CRAMFS_MAGIC
) {
139 cramfs_is_big_endian
= HOST_IS_BIG_ENDIAN
;
144 u32_toggle_endianness(!HOST_IS_BIG_ENDIAN
, CRAMFS_MAGIC
)) {
145 cramfs_is_big_endian
= !HOST_IS_BIG_ENDIAN
;
152 static void test_super(int *start
)
155 unsigned long long length
;
157 fd
= open(filename
, O_RDONLY
);
159 err(FSCK_EX_ERROR
, _("cannot open %s"), filename
);
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
);
165 if (S_ISBLK(st
.st_mode
)) {
166 if (blkdev_get_size(fd
, &length
))
168 _("ioctl failed: unable to determine device size: %s"),
170 } else if (S_ISREG(st
.st_mode
))
173 errx(FSCK_EX_ERROR
, _("not a block device or file: %s"), filename
);
175 if (length
< sizeof(struct cramfs_super
))
176 errx(FSCK_EX_UNCORRECTED
, _("file length too short"));
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)
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)
191 errx(FSCK_EX_UNCORRECTED
, _("superblock magic not found"));
193 errx(FSCK_EX_UNCORRECTED
, _("superblock magic not found"));
196 printf(_("cramfs endianness is %s\n"),
197 cramfs_is_big_endian
? _("big") : _("little"));
199 super_toggle_endianness(cramfs_is_big_endian
, &super
);
200 if (super
.flags
& ~CRAMFS_SUPPORTED_FLAGS
)
201 errx(FSCK_EX_ERROR
, _("unsupported filesystem features"));
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"),
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"));
216 warnx(_("old cramfs format"));
219 static void test_crc(int start
)
224 if (!(super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
)) {
225 warnx(_("unable to test CRC: old cramfs format"));
229 crc
= crc32(0L, NULL
, 0);
232 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
233 if (buf
== MAP_FAILED
) {
235 mmap(NULL
, super
.size
, PROT_READ
| PROT_WRITE
,
236 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
237 if (buf
!= MAP_FAILED
) {
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
);
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
);
249 if (buf
!= MAP_FAILED
) {
250 ((struct cramfs_super
*)((unsigned char *) buf
+ start
))->fsid
.crc
=
252 crc
= crc32(crc
, (unsigned char *) buf
+ start
, super
.size
- start
);
253 munmap(buf
, super
.size
);
259 if (lseek(fd
, start
, SEEK_SET
) == (off_t
) -1)
260 err(FSCK_EX_ERROR
, _("seek on %s failed"), filename
);
262 retval
= read(fd
, buf
, 4096);
264 err(FSCK_EX_ERROR
, _("cannot read %s"), filename
);
265 else if (retval
== 0)
268 ((struct cramfs_super
*)buf
)->fsid
.crc
=
271 if (length
> (super
.size
- start
)) {
272 crc
= crc32(crc
, buf
,
274 (super
.size
- start
)));
277 crc
= crc32(crc
, buf
, retval
);
282 if (crc
!= super
.fsid
.crc
)
283 errx(FSCK_EX_UNCORRECTED
, _("crc error"));
286 static void print_node(char type
, struct cramfs_inode
*i
, char *name
)
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
));
294 /* size be as high as 2^24 or 16777216 */
295 snprintf(info
, 10, "%9d", i
->size
);
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
);
303 * Create a fake "blocked" access
305 static void *romfs_read(unsigned long offset
)
307 unsigned int block
= offset
>> rombufbits
;
308 if (block
!= read_buffer_block
) {
311 read_buffer_block
= block
;
312 if (lseek(fd
, block
<< rombufbits
, SEEK_SET
) == (off_t
) -1)
313 warn(_("seek failed"));
315 x
= read(fd
, read_buffer
, rombufsize
* 2);
317 warn(_("read romfs failed"));
319 return read_buffer
+ (offset
& rombufmask
);
322 static struct cramfs_inode
*cramfs_iget(struct cramfs_inode
*i
)
324 struct cramfs_inode
*inode
= xmalloc(sizeof(struct cramfs_inode
));
326 inode_to_host(cramfs_is_big_endian
, i
, inode
);
330 static struct cramfs_inode
*iget(unsigned int ino
)
332 return cramfs_iget(romfs_read(ino
));
335 static void iput(struct cramfs_inode
*inode
)
341 * Return the offset of the root directory
343 static struct cramfs_inode
*read_super(void)
345 struct cramfs_inode
*root
= cramfs_iget(&super
.root
);
346 unsigned long offset
= root
->offset
<< 2;
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
);
358 static int uncompress_block(void *src
, size_t len
)
362 stream
.next_in
= src
;
363 stream
.avail_in
= len
;
365 stream
.next_out
= (unsigned char *)outbuffer
;
366 stream
.avail_out
= blksize
* 2;
368 inflateReset(&stream
);
370 if (len
> blksize
* 2)
371 errx(FSCK_EX_UNCORRECTED
, _("data block too large"));
373 err
= inflate(&stream
, Z_FINISH
);
374 if (err
!= Z_STREAM_END
)
375 errx(FSCK_EX_UNCORRECTED
, _("decompression error: %s"),
377 return stream
.total_out
;
384 static void do_uncompress(char *path
, int outfd
, unsigned long offset
,
387 unsigned long curr
= offset
+ 4 * ((size
+ blksize
- 1) / blksize
);
390 unsigned long out
= blksize
;
391 unsigned long next
= u32_toggle_endianness(cramfs_is_big_endian
,
401 printf(_(" hole at %lu (%zu)\n"), curr
,
405 memset(outbuffer
, 0x00, out
);
408 printf(_(" uncompressing block at %lu to %lu (%lu)\n"),
409 curr
, next
, next
- curr
);
410 out
= uncompress_block(romfs_read(curr
), next
- curr
);
412 if (size
>= blksize
) {
414 errx(FSCK_EX_UNCORRECTED
,
415 _("non-block (%ld) bytes"), out
);
418 errx(FSCK_EX_UNCORRECTED
,
419 _("non-size (%ld vs %ld) bytes"), out
,
423 if (*extract_dir
!= '\0' && write(outfd
, outbuffer
, out
) < 0)
424 err(FSCK_EX_ERROR
, _("write failed: %s"), path
);
428 static void change_file_status(char *path
, struct cramfs_inode
*i
)
430 const struct timeval epoch
[] = { {0,0}, {0,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
))
437 if (((S_ISUID
| S_ISGID
) & i
->mode
) && chmod(path
, i
->mode
) < 0)
438 err(FSCK_EX_ERROR
, _("chmod failed: %s"), path
);
440 if (S_ISLNK(i
->mode
))
442 if (utimes(path
, epoch
) < 0)
443 err(FSCK_EX_ERROR
, _("utimes failed: %s"), path
);
446 static int is_dangerous_filename(char *name
, int len
)
448 return (len
== 1 && name
[0] == '.') ||
449 (len
== 2 && name
[0] == '.' && name
[1] == '.');
452 static void __attribute__((__noreturn__
))
453 errx_path(const char *mesg
, const char *name
, size_t namelen
)
455 char buf
[PATH_MAX
] = { 0 };
457 namelen
= min(namelen
, sizeof(buf
) - 1);
458 memcpy(buf
, name
, namelen
);
460 errx(FSCK_EX_UNCORRECTED
, "%s: %s", mesg
, buf
);
463 static void do_directory(char *path
, struct cramfs_inode
*i
)
465 int pathlen
= strlen(path
);
467 unsigned long offset
= i
->offset
<< 2;
468 char *newpath
= xmalloc(pathlen
+ 256);
470 if (offset
== 0 && count
!= 0)
471 errx(FSCK_EX_UNCORRECTED
,
472 _("directory inode has zero offset and non-zero size: %s"),
475 if (offset
!= 0 && offset
< start_dir
)
478 /* TODO: Do we need to check end_dir for empty case? */
479 memcpy(newpath
, path
, pathlen
);
480 newpath
[pathlen
] = '/';
483 print_node('d', i
, path
);
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
);
491 struct cramfs_inode
*child
= iget(offset
);
494 int newlen
= child
->namelen
<< 2;
496 size
= sizeof(struct cramfs_inode
) + newlen
;
499 offset
+= sizeof(struct cramfs_inode
);
500 name
= romfs_read(offset
);
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;
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
);
516 if (offset
<= start_dir
)
517 errx(FSCK_EX_UNCORRECTED
, _("bad inode offset"));
518 if (offset
> end_dir
)
520 iput(child
); /* free(child) */
525 static void do_file(char *path
, struct cramfs_inode
*i
)
527 unsigned long offset
= i
->offset
<< 2;
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
)
539 print_node('f', i
, path
);
540 if (*extract_dir
!= '\0') {
541 outfd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, i
->mode
);
543 err(FSCK_EX_ERROR
, _("cannot open %s"), path
);
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
);
554 static void do_symlink(char *path
, struct cramfs_inode
*i
)
556 unsigned long offset
= i
->offset
<< 2;
557 unsigned long curr
= offset
+ 4;
559 u32_toggle_endianness(cramfs_is_big_endian
,
560 *(uint32_t *) romfs_read(offset
));
564 errx(FSCK_EX_UNCORRECTED
, _("symbolic link has zero offset"));
566 errx(FSCK_EX_UNCORRECTED
, _("symbolic link has zero size"));
568 if (offset
< start_data
)
573 size
= uncompress_block(romfs_read(curr
), next
- curr
);
575 errx(FSCK_EX_UNCORRECTED
, _("size error in symlink: %s"), path
);
580 xasprintf(&str
, "%s -> %s", path
, outbuffer
);
581 print_node('l', i
, str
);
583 printf(_(" uncompressing block at %lu to %lu (%lu)\n"),
584 curr
, next
, next
- curr
);
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
);
594 static void do_special_inode(char *path
, struct cramfs_inode
*i
)
600 /* no need to shift offset */
601 errx(FSCK_EX_UNCORRECTED
,
602 _("special file has non-zero offset: %s"), path
);
604 if (S_ISCHR(i
->mode
)) {
607 } else if (S_ISBLK(i
->mode
)) {
610 } else if (S_ISFIFO(i
->mode
)) {
612 errx(FSCK_EX_UNCORRECTED
, _("fifo has non-zero size: %s"),
615 } else if (S_ISSOCK(i
->mode
)) {
617 errx(FSCK_EX_UNCORRECTED
,
618 _("socket has non-zero size: %s"), path
);
621 errx(FSCK_EX_UNCORRECTED
, _("bogus mode: %s (%o)"), path
, i
->mode
);
622 return; /* not reached */
626 print_node(type
, i
, path
);
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
);
635 static void expand_fs(char *path
, struct cramfs_inode
*inode
)
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
);
644 do_special_inode(path
, inode
);
647 static void test_fs(int start
)
649 struct cramfs_inode
*root
;
654 stream
.next_in
= NULL
;
656 inflateInit(&stream
);
657 expand_fs(extract_dir
, root
);
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
);
669 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
&& end_data
> super
.size
)
670 errx(FSCK_EX_UNCORRECTED
, _("invalid file data offset"));
672 iput(root
); /* free(root) */
675 int main(int argc
, char **argv
)
677 int c
; /* for getopt */
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'},
689 setlocale(LC_MESSAGES
, "");
690 setlocale(LC_CTYPE
, "");
691 bindtextdomain(PACKAGE
, LOCALEDIR
);
693 close_stdout_atexit();
695 strutils_set_exitcode(FSCK_EX_USAGE
);
697 /* command line options */
698 while ((c
= getopt_long(argc
, argv
, "ayvVhb:", longopts
, NULL
)) != EOF
)
700 case 'a': /* ignore */
707 print_version(FSCK_EX_OK
);
711 extract_dir
= optarg
;
717 blksize
= strtou32_or_err(optarg
, _("invalid blocksize argument"));
720 errtryhelp(FSCK_EX_USAGE
);
723 if ((argc
- optind
) != 1){
724 warnx(_("bad usage"));
725 errtryhelp(FSCK_EX_USAGE
);
727 filename
= argv
[optind
];
736 blksize
= getpagesize();
738 /* re-calculate according to blksize */
739 bufsize
= rombufsize
= blksize
* 2;
741 while (bufsize
>>= 1)
743 rombufmask
= rombufsize
- 1;
745 outbuffer
= xmalloc(blksize
* 2);
746 read_buffer
= xmalloc(rombufsize
* 2);
751 printf(_("%s: OK\n"), filename
);