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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 */
51 #include <sys/types.h>
54 #include <sys/ioctl.h>
55 #include <sys/sysmacros.h> /* for major, minor */
58 #include "cramfs_common.h"
62 static const char *progname = "cramfsck";
64 static int fd; /* ROM image file descriptor */
65 static char *filename; /* ROM image filename */
66 struct cramfs_super super; /* just find the cramfs superblock once */
67 static int cramfs_is_big_endian = 0; /* source is big endian */
68 static int opt_verbose = 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */
70 char *extract_dir = ""; /* extraction directory (-x) */
72 /* Exit codes used by fsck-type programs */
73 #define FSCK_OK 0 /* No errors */
74 #define FSCK_NONDESTRUCT 1 /* File system errors corrected */
75 #define FSCK_REBOOT 2 /* System should be rebooted */
76 #define FSCK_UNCORRECTED 4 /* File system errors left uncorrected */
77 #define FSCK_ERROR 8 /* Operational error */
78 #define FSCK_USAGE 16 /* Usage or syntax error */
79 #define FSCK_LIBRARY 128 /* Shared library error */
83 #ifdef INCLUDE_FS_TESTS
85 static int opt_extract = 0; /* extract cramfs (-x) */
87 static uid_t euid; /* effective UID */
89 /* (cramfs_super + start) <= start_dir < end_dir <= start_data <= end_data */
90 static unsigned long start_dir = ~0UL; /* start of first non-root inode */
91 static unsigned long end_dir = 0; /* end of the directory structure */
92 static unsigned long start_data = ~0UL; /* start of the data (256 MB = max) */
93 static unsigned long end_data = 0; /* end of the data */
96 /* Guarantee access to at least 8kB at a time */
97 #define ROMBUFFER_BITS 13
98 #define ROMBUFFERSIZE (1 << ROMBUFFER_BITS)
99 #define ROMBUFFERMASK (ROMBUFFERSIZE-1)
100 static char read_buffer[ROMBUFFERSIZE * 2];
101 static unsigned long read_buffer_block = ~0UL;
103 static z_stream stream;
106 static void expand_fs(char *, struct cramfs_inode *);
107 #endif /* INCLUDE_FS_TESTS */
109 static char *outbuffer;
111 static size_t page_size;
113 /* Input status of 0 to print help and exit without an error. */
114 static void usage(int status)
116 FILE *stream = status ? stderr : stdout;
118 fprintf(stream, _("usage: %s [-hv] [-x dir] file\n"
119 " -h print this help\n"
120 " -x dir extract into dir\n"
121 " -v be more verbose\n"
122 " file file to test\n"), progname);
127 static void die(int status, int syserr, const char *fmt, ...)
133 va_start(arg_ptr, fmt);
134 fprintf(stderr, "%s: ", progname);
135 vfprintf(stderr, fmt, arg_ptr);
137 fprintf(stderr, ": %s", strerror(save));
139 fprintf(stderr, "\n");
144 int get_superblock_endianness(u32 magic)
146 if (magic == CRAMFS_MAGIC) {
147 cramfs_is_big_endian = HOST_IS_BIG_ENDIAN;
150 else if (magic == u32_toggle_endianness(!HOST_IS_BIG_ENDIAN, CRAMFS_MAGIC)) {
151 cramfs_is_big_endian = !HOST_IS_BIG_ENDIAN;
159 static void test_super(int *start, size_t *length) {
162 /* find the physical size of the file or block device */
163 if (stat(filename, &st) < 0) {
164 die(FSCK_ERROR, 1, _("stat failed: %s"), filename);
166 fd = open(filename, O_RDONLY);
168 die(FSCK_ERROR, 1, _("open failed: %s"), filename);
170 if (S_ISBLK(st.st_mode)) {
171 unsigned long long bytes;
172 if (blkdev_get_size(fd, &bytes)) {
173 die(FSCK_ERROR, 1, _("ioctl failed: unable to determine device size: %s"), filename);
177 else if (S_ISREG(st.st_mode)) {
178 *length = st.st_size;
181 die(FSCK_ERROR, 0, _("not a block device or file: %s"), filename);
184 if (*length < sizeof(struct cramfs_super)) {
185 die(FSCK_UNCORRECTED, 0, _("file length too short"));
188 /* find superblock */
189 if (read(fd, &super, sizeof(super)) != sizeof(super)) {
190 die(FSCK_ERROR, 1, _("read failed: %s"), filename);
192 if (get_superblock_endianness(super.magic) != -1) {
195 else if (*length >= (PAD_SIZE + sizeof(super))) {
196 lseek(fd, PAD_SIZE, SEEK_SET);
197 if (read(fd, &super, sizeof(super)) != sizeof(super)) {
198 die(FSCK_ERROR, 1, _("read failed: %s"), filename);
200 if (get_superblock_endianness(super.magic) != -1) {
204 die(FSCK_UNCORRECTED, 0, "superblock magic not found");
208 die(FSCK_UNCORRECTED, 0, _("superblock magic not found"));
212 printf("cramfs endianness is %s\n", cramfs_is_big_endian ? "big" : "little");
215 super_toggle_endianness(cramfs_is_big_endian, &super);
216 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
217 die(FSCK_ERROR, 0, _("unsupported filesystem features"));
219 if (super.size < page_size) {
220 die(FSCK_UNCORRECTED, 0, _("superblock size (%d) too small"), super.size);
222 if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
223 if (super.fsid.files == 0) {
224 die(FSCK_UNCORRECTED, 0, _("zero file count"));
226 if (*length < super.size) {
227 die(FSCK_UNCORRECTED, 0, _("file length too short"));
229 else if (*length > super.size) {
230 fprintf(stderr, _("warning: file extends past end of filesystem\n"));
234 fprintf(stderr, _("warning: old cramfs format\n"));
238 static void test_crc(int start)
243 if (!(super.flags & CRAMFS_FLAG_FSID_VERSION_2)) {
244 #ifdef INCLUDE_FS_TESTS
246 #else /* not INCLUDE_FS_TESTS */
247 die(FSCK_USAGE, 0, _("unable to test CRC: old cramfs format"));
248 #endif /* not INCLUDE_FS_TESTS */
251 crc = crc32(0L, Z_NULL, 0);
253 buf = mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
254 if (buf == MAP_FAILED) {
255 buf = mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
256 if (buf != MAP_FAILED) {
257 lseek(fd, 0, SEEK_SET);
258 if (read(fd, buf, super.size) < 0)
259 die(FSCK_ERROR, 1, _("read failed: %s"), filename);
262 if (buf != MAP_FAILED) {
263 ((struct cramfs_super *) (buf+start))->fsid.crc = crc32(0L, Z_NULL, 0);
264 crc = crc32(crc, buf+start, super.size-start);
265 munmap(buf, super.size);
273 die(FSCK_ERROR, 1, _("malloc failed"));
275 lseek(fd, start, SEEK_SET);
277 retval = read(fd, buf, 4096);
279 die(FSCK_ERROR, 1, _("read failed: %s"), filename);
281 else if (retval == 0) {
285 ((struct cramfs_super *) buf)->fsid.crc = crc32(0L, Z_NULL, 0);
288 if (length > (super.size-start)) {
289 crc = crc32(crc, buf, retval - (length - (super.size-start)));
292 crc = crc32(crc, buf, retval);
297 if (crc != super.fsid.crc) {
298 die(FSCK_UNCORRECTED, 0, _("crc error"));
302 #ifdef INCLUDE_FS_TESTS
303 static void print_node(char type, struct cramfs_inode *i, char *name)
307 if (S_ISCHR(i->mode) || (S_ISBLK(i->mode))) {
308 /* major/minor numbers can be as high as 2^12 or 4096 */
309 snprintf(info, 10, "%4d,%4d", major(i->size), minor(i->size));
312 /* size be as high as 2^24 or 16777216 */
313 snprintf(info, 10, "%9d", i->size);
316 printf("%c %04o %s %5d:%-3d %s\n",
317 type, i->mode & ~S_IFMT, info, i->uid, i->gid,
318 !*name && type == 'd' ? "/" : name);
322 * Create a fake "blocked" access
324 static void *romfs_read(unsigned long offset)
326 unsigned int block = offset >> ROMBUFFER_BITS;
327 if (block != read_buffer_block) {
328 read_buffer_block = block;
329 lseek(fd, block << ROMBUFFER_BITS, SEEK_SET);
330 read(fd, read_buffer, ROMBUFFERSIZE * 2);
332 return read_buffer + (offset & ROMBUFFERMASK);
335 static struct cramfs_inode *cramfs_iget(struct cramfs_inode * i)
337 struct cramfs_inode *inode = malloc(sizeof(struct cramfs_inode));
340 die(FSCK_ERROR, 1, _("malloc failed"));
342 inode_to_host(cramfs_is_big_endian, i, inode);
346 static struct cramfs_inode *iget(unsigned int ino)
348 return cramfs_iget(romfs_read(ino));
351 static void iput(struct cramfs_inode *inode)
357 * Return the offset of the root directory
359 static struct cramfs_inode *read_super(void)
361 struct cramfs_inode * root = cramfs_iget(&super.root);
362 unsigned long offset = root->offset << 2;
364 if (!S_ISDIR(root->mode))
365 die(FSCK_UNCORRECTED, 0, _("root inode is not directory"));
366 if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
367 ((offset != sizeof(struct cramfs_super)) &&
368 (offset != PAD_SIZE + sizeof(struct cramfs_super))))
370 die(FSCK_UNCORRECTED, 0, _("bad root offset (%lu)"), offset);
375 static int uncompress_block(void *src, int len)
379 stream.next_in = src;
380 stream.avail_in = len;
382 stream.next_out = (unsigned char *) outbuffer;
383 stream.avail_out = page_size*2;
385 inflateReset(&stream);
387 if (len > page_size*2) {
388 die(FSCK_UNCORRECTED, 0, _("data block too large"));
390 err = inflate(&stream, Z_FINISH);
391 if (err != Z_STREAM_END) {
392 die(FSCK_UNCORRECTED, 0, _("decompression error %p(%d): %s"),
393 zError(err), src, len);
395 return stream.total_out;
401 static void do_uncompress(char *path, int fd, unsigned long offset, unsigned long size)
403 unsigned long curr = offset + 4 * ((size + page_size - 1) / page_size);
406 unsigned long out = page_size;
407 unsigned long next = u32_toggle_endianness(cramfs_is_big_endian, *(u32 *) romfs_read(offset));
409 if (next > end_data) {
415 if (opt_verbose > 1) {
416 printf(_(" hole at %ld (%zd)\n"), curr, page_size);
418 if (size < page_size)
420 memset(outbuffer, 0x00, out);
423 if (opt_verbose > 1) {
424 printf(_(" uncompressing block at %ld to %ld (%ld)\n"), curr, next, next - curr);
426 out = uncompress_block(romfs_read(curr), next - curr);
428 if (size >= page_size) {
429 if (out != page_size) {
430 die(FSCK_UNCORRECTED, 0, _("non-block (%ld) bytes"), out);
434 die(FSCK_UNCORRECTED, 0, _("non-size (%ld vs %ld) bytes"), out, size);
439 if (write(fd, outbuffer, out) < 0) {
440 die(FSCK_ERROR, 1, _("write failed: %s"), path);
447 static void change_file_status(char *path, struct cramfs_inode *i)
449 struct utimbuf epoch = { 0, 0 };
452 if (lchown(path, i->uid, i->gid) < 0) {
453 die(FSCK_ERROR, 1, _("lchown failed: %s"), path);
455 if (S_ISLNK(i->mode))
457 if ((S_ISUID | S_ISGID) & i->mode) {
458 if (chmod(path, i->mode) < 0) {
459 die(FSCK_ERROR, 1, _("chown failed: %s"), path);
463 if (S_ISLNK(i->mode))
465 if (utime(path, &epoch) < 0) {
466 die(FSCK_ERROR, 1, _("utime failed: %s"), path);
470 static void do_directory(char *path, struct cramfs_inode *i)
472 int pathlen = strlen(path);
474 unsigned long offset = i->offset << 2;
475 char *newpath = malloc(pathlen + 256);
478 die(FSCK_ERROR, 1, _("malloc failed"));
480 if (offset == 0 && count != 0) {
481 die(FSCK_UNCORRECTED, 0, _("directory inode has zero offset and non-zero size: %s"), path);
483 if (offset != 0 && offset < start_dir) {
486 /* TODO: Do we need to check end_dir for empty case? */
487 memcpy(newpath, path, pathlen);
488 newpath[pathlen] = '/';
491 print_node('d', i, path);
494 if (mkdir(path, i->mode) < 0) {
495 die(FSCK_ERROR, 1, _("mkdir failed: %s"), path);
497 change_file_status(path, i);
500 struct cramfs_inode *child = iget(offset);
502 int newlen = child->namelen << 2;
504 size = sizeof(struct cramfs_inode) + newlen;
507 offset += sizeof(struct cramfs_inode);
509 memcpy(newpath + pathlen, romfs_read(offset), newlen);
510 newpath[pathlen + newlen] = 0;
512 die(FSCK_UNCORRECTED, 0, _("filename length is zero"));
514 if ((pathlen + newlen) - strlen(newpath) > 3) {
515 die(FSCK_UNCORRECTED, 0, _("bad filename length"));
517 expand_fs(newpath, child);
521 if (offset <= start_dir) {
522 die(FSCK_UNCORRECTED, 0, _("bad inode offset"));
524 if (offset > end_dir) {
527 iput(child); /* free(child) */
532 static void do_file(char *path, struct cramfs_inode *i)
534 unsigned long offset = i->offset << 2;
537 if (offset == 0 && i->size != 0) {
538 die(FSCK_UNCORRECTED, 0, _("file inode has zero offset and non-zero size"));
540 if (i->size == 0 && offset != 0) {
541 die(FSCK_UNCORRECTED, 0, _("file inode has zero size and non-zero offset"));
543 if (offset != 0 && offset < start_data) {
547 print_node('f', i, path);
550 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, i->mode);
552 die(FSCK_ERROR, 1, _("open failed: %s"), path);
556 do_uncompress(path, fd, offset, i->size);
560 change_file_status(path, i);
564 static void do_symlink(char *path, struct cramfs_inode *i)
566 unsigned long offset = i->offset << 2;
567 unsigned long curr = offset + 4;
568 unsigned long next = u32_toggle_endianness(cramfs_is_big_endian, *(u32 *) romfs_read(offset));
572 die(FSCK_UNCORRECTED, 0, _("symbolic link has zero offset"));
575 die(FSCK_UNCORRECTED, 0, _("symbolic link has zero size"));
578 if (offset < start_data) {
581 if (next > end_data) {
585 size = uncompress_block(romfs_read(curr), next - curr);
586 if (size != i->size) {
587 die(FSCK_UNCORRECTED, 0, _("size error in symlink: %s"), path);
593 asprintf(&str, "%s -> %s", path, outbuffer);
594 print_node('l', i, str);
595 if (opt_verbose > 1) {
596 printf(_(" uncompressing block at %ld to %ld (%ld)\n"), curr, next, next - curr);
601 if (symlink(outbuffer, path) < 0) {
602 die(FSCK_ERROR, 1, _("symlink failed: %s"), path);
604 change_file_status(path, i);
608 static void do_special_inode(char *path, struct cramfs_inode *i)
613 if (i->offset) { /* no need to shift offset */
614 die(FSCK_UNCORRECTED, 0, _("special file has non-zero offset: %s"), path);
616 if (S_ISCHR(i->mode)) {
620 else if (S_ISBLK(i->mode)) {
624 else if (S_ISFIFO(i->mode)) {
626 die(FSCK_UNCORRECTED, 0, _("fifo has non-zero size: %s"), path);
630 else if (S_ISSOCK(i->mode)) {
632 die(FSCK_UNCORRECTED, 0, _("socket has non-zero size: %s"), path);
637 die(FSCK_UNCORRECTED, 0, _("bogus mode: %s (%o)"), path, i->mode);
638 return; /* not reached */
642 print_node(type, i, path);
646 if (mknod(path, i->mode, devtype) < 0) {
647 die(FSCK_ERROR, 1, _("mknod failed: %s"), path);
649 change_file_status(path, i);
653 static void expand_fs(char *path, struct cramfs_inode *inode)
655 if (S_ISDIR(inode->mode)) {
656 do_directory(path, inode);
658 else if (S_ISREG(inode->mode)) {
659 do_file(path, inode);
661 else if (S_ISLNK(inode->mode)) {
662 do_symlink(path, inode);
665 do_special_inode(path, inode);
669 static void test_fs(int start)
671 struct cramfs_inode *root;
676 stream.next_in = NULL;
678 inflateInit(&stream);
679 expand_fs(extract_dir, root);
681 if (start_data != ~0UL) {
682 if (start_data < (sizeof(struct cramfs_super) + start)) {
683 die(FSCK_UNCORRECTED, 0, _("directory data start (%ld) < sizeof(struct cramfs_super) + start (%ld)"), start_data, sizeof(struct cramfs_super) + start);
685 if (end_dir != start_data) {
686 die(FSCK_UNCORRECTED, 0, _("directory data end (%ld) != file data start (%ld)"), end_dir, start_data);
689 if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
690 if (end_data > super.size) {
691 die(FSCK_UNCORRECTED, 0, _("invalid file data offset"));
694 iput(root); /* free(root) */
696 #endif /* INCLUDE_FS_TESTS */
698 int main(int argc, char **argv)
700 int c; /* for getopt */
704 setlocale(LC_MESSAGES, "");
705 bindtextdomain(PACKAGE, LOCALEDIR);
708 page_size = getpagesize();
713 outbuffer = malloc(page_size * 2);
715 die(FSCK_ERROR, 1, _("failed to allocate outbuffer"));
717 /* command line options */
718 while ((c = getopt(argc, argv, "hx:v")) != EOF) {
723 #ifdef INCLUDE_FS_TESTS
725 extract_dir = optarg;
727 #else /* not INCLUDE_FS_TESTS */
728 die(FSCK_USAGE, 0, _("compiled without -x support"));
729 #endif /* not INCLUDE_FS_TESTS */
736 if ((argc - optind) != 1)
738 filename = argv[optind];
740 test_super(&start, &length);
742 #ifdef INCLUDE_FS_TESTS
744 #endif /* INCLUDE_FS_TESTS */
747 printf("%s: OK\n", filename);