]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fsck.cramfs.c
docs: corrections to FSF license files, and postal address
[thirdparty/util-linux.git] / disk-utils / fsck.cramfs.c
1 /*
2 * cramfsck - check a cramfs file system
3 *
4 * Copyright (C) 2000-2002 Transmeta Corporation
5 * 2005 Adrian Bunk
6 *
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.
11 *
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.
16 *
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.
20 *
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,
28 * symlink size test)
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)
34 */
35
36 /* compile-time options */
37 //#define INCLUDE_FS_TESTS /* include cramfs checking and extraction */
38
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <stdint.h>
42 #include <unistd.h>
43 #include <dirent.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <string.h>
47 #include <getopt.h>
48 #include <utime.h>
49 #include <fcntl.h>
50 #include <zlib.h>
51
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <sys/mman.h>
55 #include <sys/sysmacros.h> /* for major, minor */
56
57 #include "cramfs.h"
58 #include "nls.h"
59 #include "blkdev.h"
60 #include "c.h"
61 #include "exitcodes.h"
62
63 #define XALLOC_EXIT_CODE FSCK_ERROR
64 #include "xalloc.h"
65
66 static const char *progname = "cramfsck";
67
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) */
73
74 char *extract_dir = ""; /* extraction directory (-x) */
75
76 #define PAD_SIZE 512
77
78 #ifdef INCLUDE_FS_TESTS
79
80 static int opt_extract = 0; /* extract cramfs (-x) */
81
82 static uid_t euid; /* effective UID */
83
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 */
89
90
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;
97
98 static z_stream stream;
99
100 /* Prototypes */
101 static void expand_fs(char *, struct cramfs_inode *);
102 #endif /* INCLUDE_FS_TESTS */
103
104 static char *outbuffer;
105
106 static size_t page_size;
107
108 /* Input status of 0 to print help and exit without an error. */
109 static void __attribute__((__noreturn__)) usage(int status)
110 {
111 FILE *stream = status ? stderr : stdout;
112
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);
118
119 exit(status);
120 }
121
122 int get_superblock_endianness(uint32_t magic)
123 {
124 if (magic == CRAMFS_MAGIC) {
125 cramfs_is_big_endian = HOST_IS_BIG_ENDIAN;
126 return 0;
127 } else if (magic ==
128 u32_toggle_endianness(!HOST_IS_BIG_ENDIAN, CRAMFS_MAGIC)) {
129 cramfs_is_big_endian = !HOST_IS_BIG_ENDIAN;
130 return 0;
131 } else
132 return -1;
133 }
134
135 static void test_super(int *start, size_t * length)
136 {
137 struct stat st;
138
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);
142
143 fd = open(filename, O_RDONLY);
144 if (fd < 0)
145 err(FSCK_ERROR, _("open failed: %s"), filename);
146
147 if (S_ISBLK(st.st_mode)) {
148 unsigned long long bytes;
149 if (blkdev_get_size(fd, &bytes))
150 err(FSCK_ERROR,
151 _("ioctl failed: unable to determine device size: %s"),
152 filename);
153 *length = bytes;
154 } else if (S_ISREG(st.st_mode))
155 *length = st.st_size;
156 else
157 errx(FSCK_ERROR, _("not a block device or file: %s"), filename);
158
159 if (*length < sizeof(struct cramfs_super))
160 errx(FSCK_UNCORRECTED, _("file length too short"));
161
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)
166 *start = 0;
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)
172 *start = PAD_SIZE;
173 else
174 errx(FSCK_UNCORRECTED, _("superblock magic not found"));
175 } else
176 errx(FSCK_UNCORRECTED, _("superblock magic not found"));
177
178 if (opt_verbose)
179 printf(_("cramfs endianness is %s\n"),
180 cramfs_is_big_endian ? _("big") : _("little"));
181
182 super_toggle_endianness(cramfs_is_big_endian, &super);
183 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS)
184 errx(FSCK_ERROR, _("unsupported filesystem features"));
185
186 if (super.size < page_size)
187 errx(FSCK_UNCORRECTED, _("superblock size (%d) too small"),
188 super.size);
189
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)
196 fprintf(stderr,
197 _("warning: file extends past end of filesystem\n"));
198 } else
199 fprintf(stderr, _("warning: old cramfs format\n"));
200 }
201
202 static void test_crc(int start)
203 {
204 void *buf;
205 uint32_t crc;
206
207 if (!(super.flags & CRAMFS_FLAG_FSID_VERSION_2)) {
208 #ifdef INCLUDE_FS_TESTS
209 return;
210 #else
211 errx(FSCK_USAGE, _("unable to test CRC: old cramfs format"));
212 #endif
213 }
214
215 crc = crc32(0L, Z_NULL, 0);
216
217 buf =
218 mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
219 if (buf == MAP_FAILED) {
220 buf =
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);
227 }
228 }
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);
234 } else {
235 int retval;
236 size_t length = 0;
237
238 buf = xmalloc(4096);
239 lseek(fd, start, SEEK_SET);
240 for (;;) {
241 retval = read(fd, buf, 4096);
242 if (retval < 0)
243 err(FSCK_ERROR, _("read failed: %s"), filename);
244 else if (retval == 0)
245 break;
246 if (length == 0)
247 ((struct cramfs_super *)buf)->fsid.crc =
248 crc32(0L, Z_NULL, 0);
249 length += retval;
250 if (length > (super.size - start)) {
251 crc = crc32(crc, buf,
252 retval - (length -
253 (super.size - start)));
254 break;
255 }
256 crc = crc32(crc, buf, retval);
257 }
258 free(buf);
259 }
260
261 if (crc != super.fsid.crc)
262 errx(FSCK_UNCORRECTED, _("crc error"));
263 }
264
265 #ifdef INCLUDE_FS_TESTS
266 static void print_node(char type, struct cramfs_inode *i, char *name)
267 {
268 char info[10];
269
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));
273 else
274 /* size be as high as 2^24 or 16777216 */
275 snprintf(info, 10, "%9d", i->size);
276
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);
280 }
281
282 /*
283 * Create a fake "blocked" access
284 */
285 static void *romfs_read(unsigned long offset)
286 {
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);
292 }
293 return read_buffer + (offset & ROMBUFFERMASK);
294 }
295
296 static struct cramfs_inode *cramfs_iget(struct cramfs_inode *i)
297 {
298 struct cramfs_inode *inode = xmalloc(sizeof(struct cramfs_inode));
299
300 inode_to_host(cramfs_is_big_endian, i, inode);
301 return inode;
302 }
303
304 static struct cramfs_inode *iget(unsigned int ino)
305 {
306 return cramfs_iget(romfs_read(ino));
307 }
308
309 static void iput(struct cramfs_inode *inode)
310 {
311 free(inode);
312 }
313
314 /*
315 * Return the offset of the root directory
316 */
317 static struct cramfs_inode *read_super(void)
318 {
319 struct cramfs_inode *root = cramfs_iget(&super.root);
320 unsigned long offset = root->offset << 2;
321
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);
328 }
329 return root;
330 }
331
332 static int uncompress_block(void *src, int len)
333 {
334 int err;
335
336 stream.next_in = src;
337 stream.avail_in = len;
338
339 stream.next_out = (unsigned char *)outbuffer;
340 stream.avail_out = page_size * 2;
341
342 inflateReset(&stream);
343
344 if (len > page_size * 2)
345 errx(FSCK_UNCORRECTED, _("data block too large"));
346
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;
352 }
353
354 #if !HAVE_LCHOWN
355 #define lchown chown
356 #endif
357
358 static void do_uncompress(char *path, int fd, unsigned long offset,
359 unsigned long size)
360 {
361 unsigned long curr = offset + 4 * ((size + page_size - 1) / page_size);
362
363 do {
364 unsigned long out = page_size;
365 unsigned long next = u32_toggle_endianness(cramfs_is_big_endian,
366 *(uint32_t *)
367 romfs_read(offset));
368
369 if (next > end_data)
370 end_data = next;
371
372 offset += 4;
373 if (curr == next) {
374 if (opt_verbose > 1)
375 printf(_(" hole at %ld (%zd)\n"), curr,
376 page_size);
377 if (size < page_size)
378 out = size;
379 memset(outbuffer, 0x00, out);
380 } else {
381 if (opt_verbose > 1)
382 printf(_(" uncompressing block at %ld to %ld (%ld)\n"),
383 curr, next, next - curr);
384 out = uncompress_block(romfs_read(curr), next - curr);
385 }
386 if (size >= page_size) {
387 if (out != page_size)
388 errx(FSCK_UNCORRECTED,
389 _("non-block (%ld) bytes"), out);
390 } else {
391 if (out != size)
392 errx(FSCK_UNCORRECTED,
393 _("non-size (%ld vs %ld) bytes"), out,
394 size);
395 }
396 size -= out;
397 if (opt_extract)
398 if (write(fd, outbuffer, out) < 0)
399 err(FSCK_ERROR, _("write failed: %s"),
400 path);
401 curr = next;
402 } while (size);
403 }
404
405 static void change_file_status(char *path, struct cramfs_inode *i)
406 {
407 struct utimbuf epoch = { 0, 0 };
408
409 if (euid == 0) {
410 if (lchown(path, i->uid, i->gid) < 0)
411 err(FSCK_ERROR, _("lchown failed: %s"), path);
412 if (S_ISLNK(i->mode))
413 return;
414 if (((S_ISUID | S_ISGID) & i->mode) && chmod(path, i->mode) < 0)
415 err(FSCK_ERROR, _("chown failed: %s"), path);
416 }
417 if (S_ISLNK(i->mode))
418 return;
419 if (utime(path, &epoch) < 0)
420 err(FSCK_ERROR, _("utime failed: %s"), path);
421 }
422
423 static void do_directory(char *path, struct cramfs_inode *i)
424 {
425 int pathlen = strlen(path);
426 int count = i->size;
427 unsigned long offset = i->offset << 2;
428 char *newpath = xmalloc(pathlen + 256);
429
430 if (offset == 0 && count != 0)
431 errx(FSCK_UNCORRECTED,
432 _("directory inode has zero offset and non-zero size: %s"),
433 path);
434
435 if (offset != 0 && offset < start_dir)
436 start_dir = offset;
437
438 /* TODO: Do we need to check end_dir for empty case? */
439 memcpy(newpath, path, pathlen);
440 newpath[pathlen] = '/';
441 pathlen++;
442 if (opt_verbose)
443 print_node('d', i, path);
444
445 if (opt_extract) {
446 if (mkdir(path, i->mode) < 0)
447 err(FSCK_ERROR, _("mkdir failed: %s"), path);
448 change_file_status(path, i);
449 }
450 while (count > 0) {
451 struct cramfs_inode *child = iget(offset);
452 int size;
453 int newlen = child->namelen << 2;
454
455 size = sizeof(struct cramfs_inode) + newlen;
456 count -= size;
457
458 offset += sizeof(struct cramfs_inode);
459
460 memcpy(newpath + pathlen, romfs_read(offset), newlen);
461 newpath[pathlen + newlen] = 0;
462 if (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);
467
468 offset += newlen;
469
470 if (offset <= start_dir)
471 errx(FSCK_UNCORRECTED, _("bad inode offset"));
472 if (offset > end_dir)
473 end_dir = offset;
474 iput(child); /* free(child) */
475 }
476 free(newpath);
477 }
478
479 static void do_file(char *path, struct cramfs_inode *i)
480 {
481 unsigned long offset = i->offset << 2;
482 int fd = 0;
483
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)
491 start_data = offset;
492 if (opt_verbose)
493 print_node('f', i, path);
494 if (opt_extract) {
495 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, i->mode);
496 if (fd < 0)
497 err(FSCK_ERROR, _("open failed: %s"), path);
498 }
499 if (i->size)
500 do_uncompress(path, fd, offset, i->size);
501 if (opt_extract) {
502 close(fd);
503 change_file_status(path, i);
504 }
505 }
506
507 static void do_symlink(char *path, struct cramfs_inode *i)
508 {
509 unsigned long offset = i->offset << 2;
510 unsigned long curr = offset + 4;
511 unsigned long next =
512 u32_toggle_endianness(cramfs_is_big_endian,
513 *(uint32_t *) romfs_read(offset));
514 unsigned long size;
515
516 if (offset == 0)
517 errx(FSCK_UNCORRECTED, _("symbolic link has zero offset"));
518 if (i->size == 0)
519 errx(FSCK_UNCORRECTED, _("symbolic link has zero size"));
520
521 if (offset < start_data)
522 start_data = offset;
523 if (next > end_data)
524 end_data = next;
525
526 size = uncompress_block(romfs_read(curr), next - curr);
527 if (size != i->size)
528 errx(FSCK_UNCORRECTED, _("size error in symlink: %s"), path);
529 outbuffer[size] = 0;
530 if (opt_verbose) {
531 char *str;
532
533 asprintf(&str, "%s -> %s", path, outbuffer);
534 print_node('l', i, str);
535 if (opt_verbose > 1)
536 printf(_(" uncompressing block at %ld to %ld (%ld)\n"),
537 curr, next, next - curr);
538 free(str);
539 }
540 if (opt_extract) {
541 if (symlink(outbuffer, path) < 0)
542 err(FSCK_ERROR, _("symlink failed: %s"), path);
543 change_file_status(path, i);
544 }
545 }
546
547 static void do_special_inode(char *path, struct cramfs_inode *i)
548 {
549 dev_t devtype = 0;
550 char type;
551
552 if (i->offset)
553 /* no need to shift offset */
554 errx(FSCK_UNCORRECTED,
555 _("special file has non-zero offset: %s"), path);
556
557 if (S_ISCHR(i->mode)) {
558 devtype = i->size;
559 type = 'c';
560 } else if (S_ISBLK(i->mode)) {
561 devtype = i->size;
562 type = 'b';
563 } else if (S_ISFIFO(i->mode)) {
564 if (i->size != 0)
565 errx(FSCK_UNCORRECTED, _("fifo has non-zero size: %s"),
566 path);
567 type = 'p';
568 } else if (S_ISSOCK(i->mode)) {
569 if (i->size != 0)
570 errx(FSCK_UNCORRECTED,
571 _("socket has non-zero size: %s"), path);
572 type = 's';
573 } else {
574 errx(FSCK_UNCORRECTED, _("bogus mode: %s (%o)"), path, i->mode);
575 return; /* not reached */
576 }
577
578 if (opt_verbose)
579 print_node(type, i, path);
580
581 if (opt_extract) {
582 if (mknod(path, i->mode, devtype) < 0)
583 err(FSCK_ERROR, _("mknod failed: %s"), path);
584 change_file_status(path, i);
585 }
586 }
587
588 static void expand_fs(char *path, struct cramfs_inode *inode)
589 {
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);
596 else
597 do_special_inode(path, inode);
598 }
599
600 static void test_fs(int start)
601 {
602 struct cramfs_inode *root;
603
604 root = read_super();
605 umask(0);
606 euid = geteuid();
607 stream.next_in = NULL;
608 stream.avail_in = 0;
609 inflateInit(&stream);
610 expand_fs(extract_dir, root);
611 inflateEnd(&stream);
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);
621 }
622 if (super.flags & CRAMFS_FLAG_FSID_VERSION_2)
623 if (end_data > super.size)
624 errx(FSCK_UNCORRECTED, _("invalid file data offset"));
625
626 iput(root); /* free(root) */
627 }
628 #endif /* INCLUDE_FS_TESTS */
629
630 int main(int argc, char **argv)
631 {
632 int c; /* for getopt */
633 int start = 0;
634 size_t length = 0;
635
636 setlocale(LC_MESSAGES, "");
637 bindtextdomain(PACKAGE, LOCALEDIR);
638 textdomain(PACKAGE);
639
640 page_size = getpagesize();
641
642 if (argc)
643 progname = argv[0];
644
645 outbuffer = xmalloc(page_size * 2);
646
647 /* command line options */
648 while ((c = getopt(argc, argv, "hx:v")) != EOF)
649 switch (c) {
650 case 'h':
651 usage(FSCK_OK);
652 break;
653 case 'x':
654 #ifdef INCLUDE_FS_TESTS
655 opt_extract = 1;
656 extract_dir = optarg;
657 break;
658 #else
659 errx(FSCK_USAGE, _("compiled without -x support"));
660 #endif
661 case 'v':
662 opt_verbose++;
663 break;
664 }
665
666 if ((argc - optind) != 1)
667 usage(FSCK_USAGE);
668 filename = argv[optind];
669
670 test_super(&start, &length);
671 test_crc(start);
672 #ifdef INCLUDE_FS_TESTS
673 test_fs(start);
674 #endif
675
676 if (opt_verbose)
677 printf(_("%s: OK\n"), filename);
678
679 exit(FSCK_OK);
680 }