1 diff -ur linux-orig/Documentation/filesystems/cramfs.txt linux/Documentation/filesystems/cramfs.txt
2 --- linux-orig/Documentation/filesystems/cramfs.txt Thu Jul 19 16:14:53 2001
3 +++ linux/Documentation/filesystems/cramfs.txt Tue Jan 8 00:27:30 2002
5 File sizes are limited to less than 16MB.
7 Maximum filesystem size is a little over 256MB. (The last file on the
8 -filesystem is allowed to extend past 256MB.) (Comments in mkcramfs.c
9 -suggest that ROM sizes may be limited to 64MB, though that's not a
10 -limitation in cramfs code.)
11 +filesystem is allowed to extend past 256MB.)
13 Only the low 8 bits of gid are stored. The current version of
14 mkcramfs simply truncates to 8 bits, which is a potential security
22 -0 long 0x28cd3d45 Linux cramfs
25 ->12 long x future 0x%x
26 +0 ulelong 0x28cd3d45 Linux cramfs offset 0
28 +>8 ulelong x flags 0x%x
29 +>12 ulelong x future 0x%x
30 >16 string >\0 signature "%.16s"
31 ->32 long x fsid.crc 0x%x
32 ->36 long x fsid.edition %d
33 ->40 long x fsid.blocks %d
34 ->44 long x fsid.files %d
35 +>32 ulelong x fsid.crc 0x%x
36 +>36 ulelong x fsid.edition %d
37 +>40 ulelong x fsid.blocks %d
38 +>44 ulelong x fsid.files %d
39 >48 string >\0 name "%.16s"
40 +512 ulelong 0x28cd3d45 Linux cramfs offset 512
41 +>516 ulelong x size %d
42 +>520 ulelong x flags 0x%x
43 +>524 ulelong x future 0x%x
44 +>528 string >\0 signature "%.16s"
45 +>544 ulelong x fsid.crc 0x%x
46 +>548 ulelong x fsid.edition %d
47 +>552 ulelong x fsid.blocks %d
48 +>556 ulelong x fsid.files %d
49 +>560 string >\0 name "%.16s"
53 diff -ur linux-orig/fs/cramfs/README linux/fs/cramfs/README
54 --- linux-orig/fs/cramfs/README Thu Jul 19 16:14:53 2001
55 +++ linux/fs/cramfs/README Sat Jan 12 22:15:55 2002
57 lines); put another way, the same order as `find -type d -exec
60 +Beginning in 2.4.7, directory entries are sorted. This optimization
61 +allows cramfs_lookup to return more quickly when a filename does not
62 +exist, speeds up user-space directory sorts, etc.
65 One <file_data> for each file that's either a symlink or a
66 regular file of non-zero st_size.
68 This kernel supports cramfs holes (i.e. [efficient representation of]
69 blocks in uncompressed data consisting entirely of NUL bytes), but by
70 default mkcramfs doesn't test for & create holes, since cramfs in
71 -kernels up to at least 2.3.39 didn't support holes. Compile mkcramfs
72 -with -DDO_HOLES if you want it to create files that can have holes in
74 +kernels up to at least 2.3.39 didn't support holes. Run mkcramfs
75 +with -z if you want it to create files that can have holes in them.
81 +The cramfsck program included in the scripts/cramfs directory can (and
82 +perhaps even should) be used to test cramfs filesystems, although it
83 +does not try to fix errors since cramfs is a read-only filesystem and
84 +you can just re-run mkcramfs after fixing any bugs. cramfsck can also
85 +extract cramfs filesystems without mounting them.
87 If you're hacking on cramfs, you might find useful some tools for
88 -testing cramfs at <http://cvs.bofh.asn.au/cramfs/>, including a
89 -rudimentary fsck for cramfs.
90 +testing cramfs at <http://cvs.bofh.asn.au/cramfs/>. Note: the
91 +cramfsck located there is superseded by the one in scripts/cramfs.
96 PAGE_CACHE_SIZE (4096)' to `#include <asm/page.h>'. The disadvantage
97 is that the generated cramfs cannot always be shared between different
98 kernels, not even necessarily kernels of the same architecture if
99 -PAGE_CACHE_SIZE is subject to change between kernel versions.
101 +PAGE_CACHE_SIZE is subject to change between kernel versions
102 +(currently possible with arm and ia64).
104 The remaining options try to make cramfs more sharable.
106 diff -ur linux-orig/include/linux/cramfs_fs.h linux/include/linux/cramfs_fs.h
107 --- linux-orig/include/linux/cramfs_fs.h Thu Jul 19 16:14:53 2001
108 +++ linux/include/linux/cramfs_fs.h Tue Jan 8 00:29:38 2002
110 #define CRAMFS_OFFSET_WIDTH 26
113 + * Since inode.namelen is a unsigned 6-bit number, the maximum cramfs
114 + * path length is 63 << 2 = 252.
116 +#define CRAMFS_MAXPATHLEN (((1 << CRAMFS_NAMELEN_WIDTH) - 1) << 2)
119 * Reasonably terse representation of the inode data.
121 struct cramfs_inode {
123 * Superblock information at the beginning of the FS.
125 struct cramfs_super {
126 - u32 magic; /* 0x28cd3d45 - random number */
127 - u32 size; /* length in bytes */
129 - u32 future; /* 0 */
130 - u8 signature[16]; /* "Compressed ROMFS" */
131 + u32 magic; /* 0x28cd3d45 - random number */
132 + u32 size; /* length in bytes */
133 + u32 flags; /* feature flags */
134 + u32 future; /* reserved for future use */
135 + u8 signature[16]; /* "Compressed ROMFS" */
136 struct cramfs_info fsid; /* unique filesystem info */
137 - u8 name[16]; /* user-defined name */
138 - struct cramfs_inode root; /* Root inode data */
139 + u8 name[16]; /* user-defined name */
140 + struct cramfs_inode root; /* root inode data */
145 * if (flags & ~CRAMFS_SUPPORTED_FLAGS). Maybe that should be
146 * changed to test super.future instead.
148 -#define CRAMFS_SUPPORTED_FLAGS (0x7ff)
149 +#define CRAMFS_SUPPORTED_FLAGS ( 0x000000ff \
150 + | CRAMFS_FLAG_HOLES \
151 + | CRAMFS_FLAG_WRONG_SIGNATURE \
152 + | CRAMFS_FLAG_SHIFTED_ROOT_OFFSET )
154 /* Uncompression interfaces to the underlying zlib */
155 int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen);
156 diff -ur linux-orig/scripts/cramfs/cramfsck.c linux/scripts/cramfs/cramfsck.c
157 --- linux-orig/scripts/cramfs/cramfsck.c Thu Jul 19 16:14:53 2001
158 +++ linux/scripts/cramfs/cramfsck.c Sat Jan 12 22:19:44 2002
161 * cramfsck - check a cramfs file system
163 - * Copyright (C) 2000-2001 Transmeta Corporation
164 + * Copyright (C) 2000-2002 Transmeta Corporation
166 * This program is free software; you can redistribute it and/or modify
167 * it under the terms of the GNU General Public License as published by
169 * 2000/07/11: Daniel Quinlan (file length tests, start at offset 0 or 512,
170 * fsck-compatible exit codes)
171 * 2000/07/15: Daniel Quinlan (initial support for block devices)
172 + * 2002/01/10: Daniel Quinlan (additional checks, test more return codes,
173 + * use read if mmap fails, standardize messages)
176 /* compile-time options */
177 #define INCLUDE_FS_TESTS /* include cramfs checking and extraction */
180 #include <sys/types.h>
182 #include <sys/stat.h>
189 #include <sys/sysmacros.h>
192 #include <linux/cramfs_fs.h>
195 +/* Exit codes used by fsck-type programs */
196 +#define FSCK_OK 0 /* No errors */
197 +#define FSCK_NONDESTRUCT 1 /* File system errors corrected */
198 +#define FSCK_REBOOT 2 /* System should be rebooted */
199 +#define FSCK_UNCORRECTED 4 /* File system errors left uncorrected */
200 +#define FSCK_ERROR 8 /* Operational error */
201 +#define FSCK_USAGE 16 /* Usage or syntax error */
202 +#define FSCK_LIBRARY 128 /* Shared library error */
204 +#define PAD_SIZE 512
205 +#define PAGE_CACHE_SIZE (4096)
207 static const char *progname = "cramfsck";
209 static int fd; /* ROM image file descriptor */
210 static char *filename; /* ROM image filename */
211 -struct cramfs_super *super; /* just find the cramfs superblock once */
212 +struct cramfs_super super; /* just find the cramfs superblock once */
213 static int opt_verbose = 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */
214 #ifdef INCLUDE_FS_TESTS
215 -static int opt_extract = 0; /* extract cramfs (-x) */
216 -char *extract_dir = NULL; /* extraction directory (-x) */
218 -unsigned long start_inode = 1 << 28; /* start of first non-root inode */
219 -unsigned long end_inode = 0; /* end of the directory structure */
220 -unsigned long start_data = 1 << 28; /* start of the data (256 MB = max) */
221 -unsigned long end_data = 0; /* end of the data */
222 -/* true? cramfs_super < start_inode < end_inode <= start_data <= end_data */
223 +static int opt_extract = 0; /* extract cramfs (-x) */
224 +static char *extract_dir = "root"; /* extraction directory (-x) */
225 static uid_t euid; /* effective UID */
227 -#define PAD_SIZE 512
228 -#define PAGE_CACHE_SIZE (4096)
229 +/* (cramfs_super + start) <= start_dir < end_dir <= start_data <= end_data */
230 +static unsigned long start_dir = ~0UL; /* start of first non-root inode */
231 +static unsigned long end_dir = 0; /* end of the directory structure */
232 +static unsigned long start_data = ~0UL; /* start of the data (256 MB = max) */
233 +static unsigned long end_data = 0; /* end of the data */
235 /* Guarantee access to at least 8kB at a time */
236 #define ROMBUFFER_BITS 13
239 /* Uncompressing data structures... */
240 static char outbuffer[PAGE_CACHE_SIZE*2];
242 +static z_stream stream;
245 +static void expand_fs(char *, struct cramfs_inode *);
246 #endif /* INCLUDE_FS_TESTS */
248 /* Input status of 0 to print help and exit without an error. */
249 @@ -100,8 +113,159 @@
253 +static void die(int status, int syserr, const char *fmt, ...)
259 + va_start(arg_ptr, fmt);
260 + fprintf(stderr, "%s: ", progname);
261 + vfprintf(stderr, fmt, arg_ptr);
263 + fprintf(stderr, ": %s", strerror(save));
265 + fprintf(stderr, "\n");
270 +static void test_super(int *start, size_t *length) {
273 + /* find the physical size of the file or block device */
274 + if (stat(filename, &st) < 0) {
275 + die(FSCK_ERROR, 1, "stat failed: %s", filename);
277 + fd = open(filename, O_RDONLY);
279 + die(FSCK_ERROR, 1, "open failed: %s", filename);
281 + if (S_ISBLK(st.st_mode)) {
282 + if (ioctl(fd, BLKGETSIZE, length) < 0) {
283 + die(FSCK_ERROR, 1, "ioctl failed: unable to determine device size: %s", filename);
285 + *length = *length * 512;
287 + else if (S_ISREG(st.st_mode)) {
288 + *length = st.st_size;
291 + die(FSCK_ERROR, 0, "not a block device or file: %s", filename);
294 + if (*length < sizeof(struct cramfs_super)) {
295 + die(FSCK_UNCORRECTED, 0, "file length too short");
298 + /* find superblock */
299 + if (read(fd, &super, sizeof(super)) != sizeof(super)) {
300 + die(FSCK_ERROR, 1, "read failed: %s", filename);
302 + if (super.magic == CRAMFS_MAGIC) {
305 + else if (*length >= (PAD_SIZE + sizeof(super))) {
306 + lseek(fd, PAD_SIZE, SEEK_SET);
307 + if (read(fd, &super, sizeof(super)) != sizeof(super)) {
308 + die(FSCK_ERROR, 1, "read failed: %s", filename);
310 + if (super.magic == CRAMFS_MAGIC) {
315 + /* superblock tests */
316 + if (super.magic != CRAMFS_MAGIC) {
317 + die(FSCK_UNCORRECTED, 0, "superblock magic not found");
319 + if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
320 + die(FSCK_ERROR, 0, "unsupported filesystem features");
322 + if (super.size < PAGE_CACHE_SIZE) {
323 + die(FSCK_UNCORRECTED, 0, "superblock size (%d) too small", super.size);
325 + if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
326 + if (super.fsid.files == 0) {
327 + die(FSCK_UNCORRECTED, 0, "zero file count");
329 + if (*length < super.size) {
330 + die(FSCK_UNCORRECTED, 0, "file length too short");
332 + else if (*length > super.size) {
333 + fprintf(stderr, "warning: file extends past end of filesystem\n");
337 + fprintf(stderr, "warning: old cramfs format\n");
341 +static void test_crc(int start)
346 + if (!(super.flags & CRAMFS_FLAG_FSID_VERSION_2)) {
347 +#ifdef INCLUDE_FS_TESTS
349 +#else /* not INCLUDE_FS_TESTS */
350 + die(FSCK_USAGE, 0, "unable to test CRC: old cramfs format");
351 +#endif /* not INCLUDE_FS_TESTS */
354 + crc = crc32(0L, Z_NULL, 0);
356 + buf = mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
357 + if (buf == MAP_FAILED) {
358 + buf = mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
359 + if (buf != MAP_FAILED) {
360 + lseek(fd, 0, SEEK_SET);
361 + read(fd, buf, super.size);
364 + if (buf != MAP_FAILED) {
365 + ((struct cramfs_super *) (buf+start))->fsid.crc = crc32(0L, Z_NULL, 0);
366 + crc = crc32(crc, buf+start, super.size-start);
367 + munmap(buf, super.size);
373 + buf = malloc(4096);
375 + die(FSCK_ERROR, 1, "malloc failed");
377 + lseek(fd, start, SEEK_SET);
379 + retval = read(fd, buf, 4096);
381 + die(FSCK_ERROR, 1, "read failed: %s", filename);
383 + else if (retval == 0) {
387 + ((struct cramfs_super *) buf)->fsid.crc = crc32(0L, Z_NULL, 0);
390 + if (length > (super.size-start)) {
391 + crc = crc32(crc, buf, retval - (length - (super.size-start)));
394 + crc = crc32(crc, buf, retval);
399 + if (crc != super.fsid.crc) {
400 + die(FSCK_UNCORRECTED, 0, "crc error");
404 #ifdef INCLUDE_FS_TESTS
405 -void print_node(char type, struct cramfs_inode *i, char *name)
406 +static void print_node(char type, struct cramfs_inode *i, char *name)
411 static struct cramfs_inode *cramfs_iget(struct cramfs_inode * i)
413 struct cramfs_inode *inode = malloc(sizeof(struct cramfs_inode));
416 + die(FSCK_ERROR, 1, "malloc failed");
421 @@ -144,27 +312,27 @@
422 return cramfs_iget(romfs_read(ino));
425 -void iput(struct cramfs_inode *inode)
426 +static void iput(struct cramfs_inode *inode)
432 - * Return the offset of the root directory,
434 + * Return the offset of the root directory
436 static struct cramfs_inode *read_super(void)
438 - unsigned long offset;
439 + unsigned long offset = super.root.offset << 2;
441 - offset = super->root.offset << 2;
442 - if (super->magic != CRAMFS_MAGIC)
444 - if (memcmp(super->signature, CRAMFS_SIGNATURE, sizeof(super->signature)) != 0)
446 - if (offset < sizeof(super))
448 - return cramfs_iget(&super->root);
449 + if (!S_ISDIR(super.root.mode))
450 + die(FSCK_UNCORRECTED, 0, "root inode is not directory");
451 + if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
452 + ((offset != sizeof(struct cramfs_super)) &&
453 + (offset != PAD_SIZE + sizeof(struct cramfs_super))))
455 + die(FSCK_UNCORRECTED, 0, "bad root offset (%lu)", offset);
457 + return cramfs_iget(&super.root);
460 static int uncompress_block(void *src, int len)
461 @@ -179,38 +347,177 @@
463 inflateReset(&stream);
465 + if (len > PAGE_CACHE_SIZE*2) {
466 + die(FSCK_UNCORRECTED, 0, "data block too large");
468 err = inflate(&stream, Z_FINISH);
469 if (err != Z_STREAM_END) {
470 - fprintf(stderr, "%s: error %d while decompressing! %p(%d)\n",
471 - filename, err, src, len);
473 + die(FSCK_UNCORRECTED, 0, "decompression error %p(%d): %s",
474 + zError(err), src, len);
476 return stream.total_out;
479 +static void do_uncompress(char *path, int fd, unsigned long offset, unsigned long size)
481 + unsigned long curr = offset + 4 * ((size + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE);
484 + unsigned long out = PAGE_CACHE_SIZE;
485 + unsigned long next = *(u32 *) romfs_read(offset);
487 + if (next > end_data) {
492 + if (curr == next) {
493 + if (opt_verbose > 1) {
494 + printf(" hole at %ld (%d)\n", curr, PAGE_CACHE_SIZE);
496 + if (size < PAGE_CACHE_SIZE)
498 + memset(outbuffer, 0x00, out);
501 + if (opt_verbose > 1) {
502 + printf(" uncompressing block at %ld to %ld (%ld)\n", curr, next, next - curr);
504 + out = uncompress_block(romfs_read(curr), next - curr);
506 + if (size >= PAGE_CACHE_SIZE) {
507 + if (out != PAGE_CACHE_SIZE) {
508 + die(FSCK_UNCORRECTED, 0, "non-block (%ld) bytes", out);
512 + die(FSCK_UNCORRECTED, 0, "non-size (%ld vs %ld) bytes", out, size);
517 + if (write(fd, outbuffer, out) < 0) {
518 + die(FSCK_ERROR, 1, "write failed: %s", path);
525 static void change_file_status(char *path, struct cramfs_inode *i)
527 struct utimbuf epoch = { 0, 0 };
530 if (lchown(path, i->uid, i->gid) < 0) {
533 + die(FSCK_ERROR, 1, "lchown failed: %s", path);
535 if (S_ISLNK(i->mode))
537 if ((S_ISUID | S_ISGID) & i->mode) {
538 if (chmod(path, i->mode) < 0) {
541 + die(FSCK_ERROR, 1, "chown failed: %s", path);
545 if (S_ISLNK(i->mode))
547 if (utime(path, &epoch) < 0) {
550 + die(FSCK_ERROR, 1, "utime failed: %s", path);
554 +static void do_directory(char *path, struct cramfs_inode *i)
556 + int pathlen = strlen(path);
557 + int count = i->size;
558 + unsigned long offset = i->offset << 2;
559 + char *newpath = malloc(pathlen + 256);
562 + die(FSCK_ERROR, 1, "malloc failed");
564 + if (offset == 0 && count != 0) {
565 + die(FSCK_UNCORRECTED, 0, "directory inode has zero offset and non-zero size: %s", path);
567 + if (offset != 0 && offset < start_dir) {
568 + start_dir = offset;
570 + /* TODO: Do we need to check end_dir for empty case? */
571 + memcpy(newpath, path, pathlen);
572 + newpath[pathlen] = '/';
575 + print_node('d', i, path);
578 + if (mkdir(path, i->mode) < 0) {
579 + die(FSCK_ERROR, 1, "mkdir failed: %s", path);
581 + change_file_status(path, i);
583 + while (count > 0) {
584 + struct cramfs_inode *child = iget(offset);
586 + int newlen = child->namelen << 2;
588 + size = sizeof(struct cramfs_inode) + newlen;
591 + offset += sizeof(struct cramfs_inode);
593 + memcpy(newpath + pathlen, romfs_read(offset), newlen);
594 + newpath[pathlen + newlen] = 0;
596 + die(FSCK_UNCORRECTED, 0, "filename length is zero");
598 + if ((pathlen + newlen) - strlen(newpath) > 3) {
599 + die(FSCK_UNCORRECTED, 0, "bad filename length");
601 + expand_fs(newpath, child);
605 + if (offset <= start_dir) {
606 + die(FSCK_UNCORRECTED, 0, "bad inode offset");
608 + if (offset > end_dir) {
611 + iput(child); /* free(child) */
616 +static void do_file(char *path, struct cramfs_inode *i)
618 + unsigned long offset = i->offset << 2;
621 + if (offset == 0 && i->size != 0) {
622 + die(FSCK_UNCORRECTED, 0, "file inode has zero offset and non-zero size");
624 + if (i->size == 0 && offset != 0) {
625 + die(FSCK_UNCORRECTED, 0, "file inode has zero size and non-zero offset");
627 + if (offset != 0 && offset < start_data) {
628 + start_data = offset;
631 + print_node('f', i, path);
634 + fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, i->mode);
636 + die(FSCK_ERROR, 1, "open failed: %s", path);
640 + do_uncompress(path, fd, offset, i->size);
644 + change_file_status(path, i);
648 @@ -221,32 +528,39 @@
649 unsigned long next = *(u32 *) romfs_read(offset);
653 + die(FSCK_UNCORRECTED, 0, "symbolic link has zero offset");
655 + if (i->size == 0) {
656 + die(FSCK_UNCORRECTED, 0, "symbolic link has zero size");
659 + if (offset < start_data) {
660 + start_data = offset;
662 if (next > end_data) {
666 size = uncompress_block(romfs_read(curr), next - curr);
667 if (size != i->size) {
668 - fprintf(stderr, "%s: size error in symlink `%s'\n",
671 + die(FSCK_UNCORRECTED, 0, "size error in symlink: %s", path);
677 - str = malloc(strlen(outbuffer) + strlen(path) + 5);
679 - strncat(str, " -> ", 4);
680 - strncat(str, outbuffer, size);
682 + asprintf(&str, "%s -> %s", path, outbuffer);
683 print_node('l', i, str);
684 if (opt_verbose > 1) {
685 printf(" uncompressing block at %ld to %ld (%ld)\n", curr, next, next - curr);
690 - symlink(outbuffer, path);
691 + if (symlink(outbuffer, path) < 0) {
692 + die(FSCK_ERROR, 1, "symlink failed: %s", path);
694 change_file_status(path, i);
701 + if (i->offset) { /* no need to shift offset */
702 + die(FSCK_UNCORRECTED, 0, "special file has non-zero offset: %s", path);
704 if (S_ISCHR(i->mode)) {
707 @@ -264,13 +581,21 @@
711 - else if (S_ISFIFO(i->mode))
712 + else if (S_ISFIFO(i->mode)) {
713 + if (i->size != 0) {
714 + die(FSCK_UNCORRECTED, 0, "fifo has non-zero size: %s", path);
717 - else if (S_ISSOCK(i->mode))
719 + else if (S_ISSOCK(i->mode)) {
720 + if (i->size != 0) {
721 + die(FSCK_UNCORRECTED, 0, "socket has non-zero size: %s", path);
726 - fprintf(stderr, "%s: bogus mode on `%s' (%o)\n", filename, path, i->mode);
728 + die(FSCK_UNCORRECTED, 0, "bogus mode: %s (%o)", path, i->mode);
729 + return; /* not reached */
733 @@ -279,155 +604,62 @@
736 if (mknod(path, i->mode, devtype) < 0) {
739 + die(FSCK_ERROR, 1, "mknod failed: %s", path);
741 change_file_status(path, i);
745 -static void do_uncompress(int fd, unsigned long offset, unsigned long size)
746 +static void expand_fs(char *path, struct cramfs_inode *inode)
748 - unsigned long curr = offset + 4 * ((size + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE);
751 - unsigned long out = PAGE_CACHE_SIZE;
752 - unsigned long next = *(u32 *) romfs_read(offset);
754 - if (next > end_data) {
759 - if (curr == next) {
760 - if (opt_verbose > 1) {
761 - printf(" hole at %ld (%d)\n", curr, PAGE_CACHE_SIZE);
763 - if (size < PAGE_CACHE_SIZE)
765 - memset(outbuffer, 0x00, out);
768 - if (opt_verbose > 1) {
769 - printf(" uncompressing block at %ld to %ld (%ld)\n", curr, next, next - curr);
771 - out = uncompress_block(romfs_read(curr), next - curr);
773 - if (size >= PAGE_CACHE_SIZE) {
774 - if (out != PAGE_CACHE_SIZE) {
775 - fprintf(stderr, "%s: Non-block (%ld) bytes\n", filename, out);
780 - fprintf(stderr, "%s: Non-size (%ld vs %ld) bytes\n", filename, out, size);
786 - write(fd, outbuffer, out);
790 + if (S_ISDIR(inode->mode)) {
791 + do_directory(path, inode);
793 + else if (S_ISREG(inode->mode)) {
794 + do_file(path, inode);
796 + else if (S_ISLNK(inode->mode)) {
797 + do_symlink(path, inode);
800 + do_special_inode(path, inode);
804 -static void expand_fs(int pathlen, char *path, struct cramfs_inode *inode)
805 +static void test_fs(int start)
807 - if (S_ISDIR(inode->mode)) {
808 - int count = inode->size;
809 - unsigned long offset = inode->offset << 2;
810 - char *newpath = malloc(pathlen + 256);
812 - if (count > 0 && offset < start_inode) {
813 - start_inode = offset;
815 - /* XXX - need to check end_inode for empty case? */
816 - memcpy(newpath, path, pathlen);
817 - newpath[pathlen] = '/';
820 - print_node('d', inode, path);
823 - mkdir(path, inode->mode);
824 - change_file_status(path, inode);
826 - while (count > 0) {
827 - struct cramfs_inode *child = iget(offset);
829 - int newlen = child->namelen << 2;
831 - size = sizeof(struct cramfs_inode) + newlen;
834 - offset += sizeof(struct cramfs_inode);
836 - memcpy(newpath + pathlen, romfs_read(offset), newlen);
837 - newpath[pathlen + newlen] = 0;
838 - if ((pathlen + newlen) - strlen(newpath) > 3) {
839 - fprintf(stderr, "%s: invalid cramfs--bad path length\n", filename);
842 - expand_fs(strlen(newpath), newpath, child);
846 - if (offset > end_inode) {
847 - end_inode = offset;
852 - if (S_ISREG(inode->mode)) {
854 - unsigned long offset = inode->offset << 2;
855 + struct cramfs_inode *root;
857 - if (offset > 0 && offset < start_data) {
858 - start_data = offset;
861 - print_node('f', inode, path);
864 - fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, inode->mode);
867 - do_uncompress(fd, offset, inode->size);
868 + root = read_super();
871 + stream.next_in = NULL;
872 + stream.avail_in = 0;
873 + inflateInit(&stream);
874 + expand_fs(extract_dir, root);
875 + inflateEnd(&stream);
876 + if (start_data != ~0UL) {
877 + if (start_data < (sizeof(struct cramfs_super) + start)) {
878 + die(FSCK_UNCORRECTED, 0, "directory data start (%ld) < sizeof(struct cramfs_super) + start (%ld)", start_data, sizeof(struct cramfs_super) + start);
882 - change_file_status(path, inode);
883 + if (end_dir != start_data) {
884 + die(FSCK_UNCORRECTED, 0, "directory data end (%ld) != file data start (%ld)", end_dir, start_data);
888 - if (S_ISLNK(inode->mode)) {
889 - unsigned long offset = inode->offset << 2;
891 - if (offset < start_data) {
892 - start_data = offset;
893 + if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
894 + if (end_data > super.size) {
895 + die(FSCK_UNCORRECTED, 0, "invalid file data offset");
897 - do_symlink(path, inode);
901 - do_special_inode(path, inode);
904 + iput(root); /* free(root) */
906 #endif /* INCLUDE_FS_TESTS */
908 int main(int argc, char **argv)
913 - u32 crc_old, crc_new;
914 -#ifdef INCLUDE_FS_TESTS
915 - struct cramfs_inode *root;
916 -#endif /* INCLUDE_FS_TESTS */
917 int c; /* for getopt */
923 @@ -436,17 +668,14 @@
924 while ((c = getopt(argc, argv, "hx:v")) != EOF) {
930 #ifdef INCLUDE_FS_TESTS
932 - extract_dir = malloc(strlen(optarg) + 1);
933 - strcpy(extract_dir, optarg);
934 + extract_dir = optarg;
936 -#else /* not INCLUDE_FS_TESTS */
937 - fprintf(stderr, "%s: compiled without -x support\n",
940 +#else /* not INCLUDE_FS_TESTS */
941 + die(FSCK_USAGE, 0, "compiled without -x support");
942 #endif /* not INCLUDE_FS_TESTS */
945 @@ -455,134 +684,24 @@
948 if ((argc - optind) != 1)
951 filename = argv[optind];
953 - /* find the physical size of the file or block device */
954 - if (lstat(filename, &st) < 0) {
958 - fd = open(filename, O_RDONLY);
963 - if (S_ISBLK(st.st_mode)) {
964 - if (ioctl(fd, BLKGETSIZE, &length) < 0) {
965 - fprintf(stderr, "%s: warning--unable to determine filesystem size \n", filename);
968 - length = length * 512;
970 - else if (S_ISREG(st.st_mode)) {
971 - length = st.st_size;
974 - fprintf(stderr, "%s is not a block device or file\n", filename);
978 - if (length < sizeof(struct cramfs_super)) {
979 - fprintf(stderr, "%s: invalid cramfs--file length too short\n", filename);
983 - if (S_ISBLK(st.st_mode)) {
984 - /* nasty because mmap of block devices fails */
985 - buf = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
986 - read(fd, buf, length);
989 - /* nice and easy */
990 - buf = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
993 - /* XXX - this could be cleaner... */
994 - if (((struct cramfs_super *) buf)->magic == CRAMFS_MAGIC) {
996 - super = (struct cramfs_super *) buf;
998 - else if (length >= (PAD_SIZE + sizeof(struct cramfs_super)) &&
999 - ((((struct cramfs_super *) (buf + PAD_SIZE))->magic == CRAMFS_MAGIC)))
1002 - super = (struct cramfs_super *) (buf + PAD_SIZE);
1005 - fprintf(stderr, "%s: invalid cramfs--wrong magic\n", filename);
1009 - if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
1011 - if (length < super->size) {
1012 - fprintf(stderr, "%s: invalid cramfs--file length too short\n", filename);
1015 - else if (length > super->size) {
1016 - fprintf(stderr, "%s: warning--file length too long, padded image?\n", filename);
1020 - crc_old = super->fsid.crc;
1021 - super->fsid.crc = crc32(0L, Z_NULL, 0);
1022 - crc_new = crc32(0L, Z_NULL, 0);
1023 - crc_new = crc32(crc_new, (unsigned char *) buf+start, super->size - start);
1024 - if (crc_new != crc_old) {
1025 - fprintf(stderr, "%s: invalid cramfs--crc error\n", filename);
1030 - fprintf(stderr, "%s: warning--old cramfs image, no CRC\n",
1034 + test_super(&start, &length);
1036 #ifdef INCLUDE_FS_TESTS
1037 - super = (struct cramfs_super *) malloc(sizeof(struct cramfs_super));
1038 - if (((struct cramfs_super *) buf)->magic == CRAMFS_MAGIC) {
1039 - memcpy(super, buf, sizeof(struct cramfs_super));
1041 - else if (length >= (PAD_SIZE + sizeof(struct cramfs_super)) &&
1042 - ((((struct cramfs_super *) (buf + PAD_SIZE))->magic == CRAMFS_MAGIC)))
1044 - memcpy(super, (buf + PAD_SIZE), sizeof(struct cramfs_super));
1047 - munmap(buf, length);
1049 - /* file format test, uses fake "blocked" accesses */
1050 - root = read_super();
1054 - fprintf(stderr, "%s: invalid cramfs--bad superblock\n",
1058 - stream.next_in = NULL;
1059 - stream.avail_in = 0;
1060 - inflateInit(&stream);
1062 - if (!extract_dir) {
1063 - extract_dir = "root";
1066 - expand_fs(strlen(extract_dir), extract_dir, root);
1067 - inflateEnd(&stream);
1069 +#endif /* INCLUDE_FS_TESTS */
1071 - if (start_data != 1 << 28 && end_inode != start_data) {
1072 - fprintf(stderr, "%s: invalid cramfs--directory data end (%ld) != file data start (%ld)\n", filename, end_inode, start_data);
1075 - if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
1076 - if (end_data > super->size) {
1077 - fprintf(stderr, "%s: invalid cramfs--invalid file data offset\n", filename);
1080 + if (opt_verbose) {
1081 + printf("%s: OK\n", filename);
1083 -#endif /* INCLUDE_FS_TESTS */
1090 + * Local variables:
1091 + * c-file-style: "linux"
1094 diff -ur linux-orig/scripts/cramfs/mkcramfs.c linux/scripts/cramfs/mkcramfs.c
1095 --- linux-orig/scripts/cramfs/mkcramfs.c Thu Jul 19 16:14:53 2001
1096 +++ linux/scripts/cramfs/mkcramfs.c Sun Jan 13 21:40:58 2002
1099 * mkcramfs - make a cramfs file system
1101 - * Copyright (C) 1999-2001 Transmeta Corporation
1102 + * Copyright (C) 1999-2002 Transmeta Corporation
1104 * This program is free software; you can redistribute it and/or modify
1105 * it under the terms of the GNU General Public License as published by
1107 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1111 + * If you change the disk format of cramfs, please update fs/cramfs/README.
1114 #include <sys/types.h>
1116 #include <sys/stat.h>
1121 -#include <assert.h>
1123 +#include <stdarg.h>
1124 #include <linux/cramfs_fs.h>
1127 -#define PAD_SIZE 512 /* only 0 and 512 supported by kernel */
1129 -static const char *progname = "mkcramfs";
1130 +/* Exit codes used by mkfs-type programs */
1131 +#define MKFS_OK 0 /* No errors */
1132 +#define MKFS_ERROR 8 /* Operational error */
1133 +#define MKFS_USAGE 16 /* Usage or syntax error */
1135 -/* N.B. If you change the disk format of cramfs, please update fs/cramfs/README. */
1136 +/* The kernel only supports PAD_SIZE of 0 and 512. */
1137 +#define PAD_SIZE 512
1139 -/* Input status of 0 to print help and exit without an error. */
1140 -static void usage(int status)
1142 - FILE *stream = status ? stderr : stdout;
1143 +/* The kernel assumes PAGE_CACHE_SIZE as block size. */
1144 +#define PAGE_CACHE_SIZE (4096)
1146 - fprintf(stream, "usage: %s [-h] [-e edition] [-i file] [-n name] dirname outfile\n"
1147 - " -h print this help\n"
1148 - " -E make all warnings errors (non-zero exit status)\n"
1149 - " -e edition set edition number (part of fsid)\n"
1150 - " -i file insert a file image into the filesystem (requires >= 2.4.0)\n"
1151 - " -n name set name of cramfs filesystem\n"
1152 - " -p pad by %d bytes for boot code\n"
1153 - " -s sort directory entries (old option, ignored)\n"
1154 - " -z make explicit holes (requires >= 2.3.39)\n"
1155 - " dirname root of the filesystem to be compressed\n"
1156 - " outfile output file\n", progname, PAD_SIZE);
1158 + * The longest filename component to allow for in the input directory tree.
1159 + * ext2fs (and many others) allow up to 255 bytes. A couple of filesystems
1160 + * allow longer (e.g. smbfs 1024), but there isn't much use in supporting
1161 + * >255-byte names in the input directory tree given that such names get
1162 + * truncated to CRAMFS_MAXPATHLEN (252 bytes) when written to cramfs.
1164 + * Old versions of mkcramfs generated corrupted filesystems if any input
1165 + * filenames exceeded CRAMFS_MAXPATHLEN (252 bytes), however old
1166 + * versions of cramfsck seem to have been able to detect the corruption.
1168 +#define MAX_INPUT_NAMELEN 255
1173 + * Maximum size fs you can create is roughly 256MB. (The last file's
1174 + * data must begin within 256MB boundary but can extend beyond that.)
1176 + * Note that if you want it to fit in a ROM then you're limited to what the
1177 + * hardware and kernel can support.
1179 +#define MAXFSLEN ((((1 << CRAMFS_OFFSET_WIDTH) - 1) << 2) /* offset */ \
1180 + + (1 << CRAMFS_SIZE_WIDTH) - 1 /* filesize */ \
1181 + + (1 << CRAMFS_SIZE_WIDTH) * 4 / PAGE_CACHE_SIZE /* block pointers */ )
1183 -#define PAGE_CACHE_SIZE (4096)
1184 -/* The kernel assumes PAGE_CACHE_SIZE as block size. */
1185 +static const char *progname = "mkcramfs";
1186 static unsigned int blksize = PAGE_CACHE_SIZE;
1187 static long total_blocks = 0, total_nodes = 1; /* pre-count the root node */
1188 static int image_length = 0;
1191 * Note that kernels up to at least 2.3.39 don't support cramfs holes,
1192 * which is why this is turned off by default.
1194 + * If opt_verbose is 1, be verbose. If it is higher, be even more verbose.
1196 -static int opt_edition = 0;
1197 +static u32 opt_edition = 0;
1198 static int opt_errors = 0;
1199 static int opt_holes = 0;
1200 static int opt_pad = 0;
1201 +static int opt_verbose = 0;
1202 static char *opt_image = NULL;
1203 static char *opt_name = NULL;
1205 static int warn_dev, warn_gid, warn_namelen, warn_skip, warn_size, warn_uid;
1208 -# define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
1211 /* In-core version of inode / directory entry. */
1215 + unsigned char *name;
1216 unsigned int mode, size, uid, gid;
1220 - /* points to other identical file */
1221 - struct entry *same;
1222 - unsigned int offset; /* pointer to compressed data in archive */
1223 + /* points to other identical file */
1224 + struct entry *same;
1225 + unsigned int offset; /* pointer to compressed data in archive */
1226 unsigned int dir_offset; /* Where in the archive is the directory entry? */
1229 @@ -104,34 +115,67 @@
1234 - * The longest file name component to allow for in the input directory tree.
1235 - * Ext2fs (and many others) allow up to 255 bytes. A couple of filesystems
1236 - * allow longer (e.g. smbfs 1024), but there isn't much use in supporting
1237 - * >255-byte names in the input directory tree given that such names get
1238 - * truncated to 255 bytes when written to cramfs.
1240 -#define MAX_INPUT_NAMELEN 255
1241 +/* Input status of 0 to print help and exit without an error. */
1242 +static void usage(int status)
1244 + FILE *stream = status ? stderr : stdout;
1246 + fprintf(stream, "usage: %s [-h] [-e edition] [-i file] [-n name] dirname outfile\n"
1247 + " -h print this help\n"
1248 + " -E make all warnings errors (non-zero exit status)\n"
1249 + " -e edition set edition number (part of fsid)\n"
1250 + " -i file insert a file image into the filesystem (requires >= 2.4.0)\n"
1251 + " -n name set name of cramfs filesystem\n"
1252 + " -p pad by %d bytes for boot code\n"
1253 + " -s sort directory entries (old option, ignored)\n"
1254 + " -v be more verbose\n"
1255 + " -z make explicit holes (requires >= 2.3.39)\n"
1256 + " dirname root of the directory tree to be compressed\n"
1257 + " outfile output file\n", progname, PAD_SIZE);
1262 +static void die(int status, int syserr, const char *fmt, ...)
1268 + va_start(arg_ptr, fmt);
1269 + fprintf(stderr, "%s: ", progname);
1270 + vfprintf(stderr, fmt, arg_ptr);
1272 + fprintf(stderr, ": %s", strerror(save));
1274 + fprintf(stderr, "\n");
1279 static int find_identical_file(struct entry *orig,struct entry *newfile)
1281 - if(orig==newfile) return 1;
1282 - if(!orig) return 0;
1283 - if(orig->size==newfile->size && orig->uncompressed && !memcmp(orig->uncompressed,newfile->uncompressed,orig->size)) {
1284 - newfile->same=orig;
1287 - return find_identical_file(orig->child,newfile) ||
1288 - find_identical_file(orig->next,newfile);
1289 + if (orig == newfile)
1293 + if (orig->size == newfile->size && orig->uncompressed &&
1294 + !memcmp(orig->uncompressed, newfile->uncompressed, orig->size))
1296 + newfile->same = orig;
1299 + return (find_identical_file(orig->child,newfile) ||
1300 + find_identical_file(orig->next,newfile));
1303 static void eliminate_doubles(struct entry *root,struct entry *orig) {
1305 - if(orig->size && orig->uncompressed)
1307 + if (orig->size && orig->uncompressed)
1308 find_identical_file(root,orig);
1309 - eliminate_doubles(root,orig->child);
1310 - eliminate_doubles(root,orig->next);
1312 + eliminate_doubles(root,orig->child);
1313 + eliminate_doubles(root,orig->next);
1318 @@ -155,20 +199,18 @@
1319 /* TODO: Reuse the parent's buffer to save memcpy'ing and duplication. */
1320 path = malloc(len + 1 + MAX_INPUT_NAMELEN + 1);
1324 + die(MKFS_ERROR, 1, "malloc failed");
1326 memcpy(path, name, len);
1327 endpath = path + len;
1331 - /* read in the directory and sort */
1332 - dircount = scandir(name, &dirlist, 0, cramsort);
1333 + /* read in the directory and sort */
1334 + dircount = scandir(name, &dirlist, 0, cramsort);
1339 + die(MKFS_ERROR, 1, "scandir failed: %s", name);
1342 /* process directory */
1343 @@ -192,36 +234,38 @@
1345 namelen = strlen(dirent->d_name);
1346 if (namelen > MAX_INPUT_NAMELEN) {
1348 - "Very long (%u bytes) filename `%s' found.\n"
1349 - " Please increase MAX_INPUT_NAMELEN in mkcramfs.c and recompile. Exiting.\n",
1350 + die(MKFS_ERROR, 0,
1351 + "very long (%u bytes) filename found: %s\n"
1352 + "please increase MAX_INPUT_NAMELEN in mkcramfs.c and recompile",
1353 namelen, dirent->d_name);
1356 memcpy(endpath, dirent->d_name, namelen + 1);
1358 if (lstat(path, &st) < 0) {
1363 entry = calloc(1, sizeof(struct entry));
1367 + die(MKFS_ERROR, 1, "calloc failed");
1369 entry->name = strdup(dirent->d_name);
1373 + die(MKFS_ERROR, 1, "strdup failed");
1375 - if (namelen > 255) {
1376 - /* Can't happen when reading from ext2fs. */
1378 - /* TODO: we ought to avoid chopping in half
1379 - multi-byte UTF8 characters. */
1380 - entry->name[namelen = 255] = '\0';
1381 + /* truncate multi-byte UTF-8 filenames on character boundary */
1382 + if (namelen > CRAMFS_MAXPATHLEN) {
1383 + namelen = CRAMFS_MAXPATHLEN;
1385 + /* the first lost byte must not be a trail byte */
1386 + while ((entry->name[namelen] & 0xc0) == 0x80) {
1388 + /* are we reasonably certain it was UTF-8 ? */
1389 + if (entry->name[namelen] < 0x80 || !namelen) {
1390 + die(MKFS_ERROR, 0, "cannot truncate filenames not encoded in UTF-8");
1393 + entry->name[namelen] = '\0';
1395 entry->mode = st.st_mode;
1396 entry->size = st.st_size;
1397 @@ -231,10 +275,10 @@
1398 entry->gid = st.st_gid;
1399 if (entry->gid >= 1 << CRAMFS_GID_WIDTH)
1400 /* TODO: We ought to replace with a default
1401 - gid instead of truncating; otherwise there
1402 - are security problems. Maybe mode should
1403 - be &= ~070. Same goes for uid once Linux
1404 - supports >16-bit uids. */
1405 + gid instead of truncating; otherwise there
1406 + are security problems. Maybe mode should
1407 + be &= ~070. Same goes for uid once Linux
1408 + supports >16-bit uids. */
1410 size = sizeof(struct cramfs_inode) + ((namelen + 3) & ~3);
1413 (which would just save a little memory). */
1414 int fd = open(path, O_RDONLY);
1420 @@ -263,39 +306,38 @@
1423 entry->uncompressed = mmap(NULL, entry->size, PROT_READ, MAP_PRIVATE, fd, 0);
1424 - if (-1 == (int) (long) entry->uncompressed) {
1427 + if (entry->uncompressed == MAP_FAILED) {
1428 + die(MKFS_ERROR, 1, "mmap failed");
1432 } else if (S_ISLNK(st.st_mode)) {
1433 entry->uncompressed = malloc(entry->size);
1434 if (!entry->uncompressed) {
1437 + die(MKFS_ERROR, 1, "malloc failed");
1439 if (readlink(path, entry->uncompressed, entry->size) < 0) {
1444 } else if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
1445 /* maybe we should skip sockets */
1448 + } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1449 entry->size = st.st_rdev;
1450 if (entry->size & -(1<<CRAMFS_SIZE_WIDTH))
1453 + die(MKFS_ERROR, 0, "bogus file type: %s", entry->name);
1456 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
1457 int blocks = ((entry->size - 1) / blksize + 1);
1459 /* block pointers & data expansion allowance + data */
1462 *fslen_ub += (4+26)*blocks + entry->size + 3;
1466 /* Link it into the list */
1469 struct cramfs_super *super = (struct cramfs_super *) base;
1470 unsigned int offset = sizeof(struct cramfs_super) + image_length;
1473 - offset += opt_pad;
1475 + offset += opt_pad; /* 0 if no padding */
1477 super->magic = CRAMFS_MAGIC;
1478 super->flags = CRAMFS_FLAG_FSID_VERSION_2 | CRAMFS_FLAG_SORTED_DIRS;
1479 @@ -349,27 +389,66 @@
1480 static void set_data_offset(struct entry *entry, char *base, unsigned long offset)
1482 struct cramfs_inode *inode = (struct cramfs_inode *) (base + entry->dir_offset);
1484 - assert ((offset & 3) == 0);
1487 + if ((offset & 3) != 0) {
1488 + die(MKFS_ERROR, 0, "illegal offset of %lu bytes", offset);
1490 if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH))) {
1491 - fprintf(stderr, "filesystem too big. Exiting.\n");
1493 + die(MKFS_ERROR, 0, "filesystem too big");
1495 inode->offset = (offset >> 2);
1499 + * TODO: Does this work for chars >= 0x80? Most filesystems use UTF-8
1500 + * encoding for filenames, whereas the console is a single-byte
1501 + * character set like iso-latin-1.
1503 +static void print_node(struct entry *e)
1508 + if (S_ISREG(e->mode)) type = 'f';
1509 + else if (S_ISDIR(e->mode)) type = 'd';
1510 + else if (S_ISLNK(e->mode)) type = 'l';
1511 + else if (S_ISCHR(e->mode)) type = 'c';
1512 + else if (S_ISBLK(e->mode)) type = 'b';
1513 + else if (S_ISFIFO(e->mode)) type = 'p';
1514 + else if (S_ISSOCK(e->mode)) type = 's';
1516 + if (S_ISCHR(e->mode) || (S_ISBLK(e->mode))) {
1517 + /* major/minor numbers can be as high as 2^12 or 4096 */
1518 + snprintf(info, 10, "%4d,%4d", major(e->size), minor(e->size));
1521 + /* size be as high as 2^24 or 16777216 */
1522 + snprintf(info, 10, "%9d", e->size);
1525 + printf("%c %04o %s %5d:%-3d %s\n",
1526 + type, e->mode & ~S_IFMT, info, e->uid, e->gid, e->name);
1530 * We do a width-first printout of the directory
1531 * entries, using a stack to remember the directories
1534 -#define MAXENTRIES (100)
1535 static unsigned int write_directory_structure(struct entry *entry, char *base, unsigned int offset)
1537 int stack_entries = 0;
1538 - struct entry *entry_stack[MAXENTRIES];
1539 + int stack_size = 64;
1540 + struct entry **entry_stack;
1542 + entry_stack = malloc(stack_size * sizeof(struct entry *));
1543 + if (!entry_stack) {
1544 + die(MKFS_ERROR, 1, "malloc failed");
1547 + if (opt_verbose) {
1548 + printf("root:\n");
1552 int dir_start = stack_entries;
1553 @@ -398,15 +477,16 @@
1554 inode->namelen = len >> 2;
1557 - /* TODO: this may get it wrong for chars >= 0x80.
1558 - Most filesystems use UTF8 encoding for filenames,
1559 - whereas the console is a single-byte character
1560 - set like iso-latin-1. */
1561 - printf(" %s\n", entry->name);
1563 + print_node(entry);
1566 - if (stack_entries >= MAXENTRIES) {
1567 - fprintf(stderr, "Exceeded MAXENTRIES. Raise this value in mkcramfs.c and recompile. Exiting.\n");
1569 + if (stack_entries >= stack_size) {
1571 + entry_stack = realloc(entry_stack, stack_size * sizeof(struct entry *));
1572 + if (!entry_stack) {
1573 + die(MKFS_ERROR, 1, "realloc failed");
1576 entry_stack[stack_entries] = entry;
1581 * Reverse the order the stack entries pushed during
1582 - * this directory, for a small optimization of disk
1583 - * access in the created fs. This change makes things
1585 + * this directory, for a small optimization of disk
1586 + * access in the created fs. This change makes things
1590 struct entry **lo = entry_stack + dir_start;
1591 @@ -439,29 +519,27 @@
1592 entry = entry_stack[stack_entries];
1594 set_data_offset(entry, base, offset);
1595 - printf("'%s':\n", entry->name);
1596 + if (opt_verbose) {
1597 + printf("%s:\n", entry->name);
1599 entry = entry->child;
1601 + free(entry_stack);
1605 static int is_zero(char const *begin, unsigned len)
1608 - /* Returns non-zero iff the first LEN bytes from BEGIN are
1610 - return (len-- == 0 ||
1611 - (begin[0] == '\0' &&
1613 - (begin[1] == '\0' &&
1615 - (begin[2] == '\0' &&
1617 - (begin[3] == '\0' &&
1618 - memcmp(begin, begin + 4, len) == 0))))))));
1620 - /* Never create holes. */
1622 + /* Returns non-zero iff the first LEN bytes from BEGIN are all NULs. */
1623 + return (len-- == 0 ||
1624 + (begin[0] == '\0' &&
1626 + (begin[1] == '\0' &&
1628 + (begin[2] == '\0' &&
1630 + (begin[3] == '\0' &&
1631 + memcmp(begin, begin + 4, len) == 0))))))));
1635 @@ -488,19 +566,23 @@
1637 unsigned long len = 2 * blksize;
1638 unsigned int input = size;
1641 if (input > blksize)
1644 - if (!is_zero (uncompressed, input)) {
1645 - compress(base + curr, &len, uncompressed, input);
1646 + if (!(opt_holes && is_zero (uncompressed, input))) {
1647 + err = compress2(base + curr, &len, uncompressed, input, Z_BEST_COMPRESSION);
1648 + if (err != Z_OK) {
1649 + die(MKFS_ERROR, 0, "compression error: %s", zError(err));
1653 uncompressed += input;
1655 if (len > blksize*2) {
1656 /* (I don't think this can happen with zlib.) */
1657 - printf("AIEEE: block \"compressed\" to > 2*blocklength (%ld)\n", len);
1659 + die(MKFS_ERROR, 0, "AIEEE: block \"compressed\" to > 2*blocklength (%ld)", len);
1662 *(u32 *) (base + offset) = curr;
1663 @@ -513,8 +595,10 @@
1664 st_blocks * 512. But if you say that then perhaps
1665 administrative data should also be included in both. */
1666 change = new_size - original_size;
1667 - printf("%6.2f%% (%+d bytes)\t%s\n",
1668 - (change * 100) / (double) original_size, change, name);
1669 + if (opt_verbose > 1) {
1670 + printf("%6.2f%% (%+d bytes)\t%s\n",
1671 + (change * 100) / (double) original_size, change, name);
1676 @@ -529,18 +613,19 @@
1679 if (entry->uncompressed) {
1681 - set_data_offset(entry, base, entry->same->offset);
1682 - entry->offset=entry->same->offset;
1684 - set_data_offset(entry, base, offset);
1685 - entry->offset=offset;
1686 - offset = do_compress(base, offset, entry->name, entry->uncompressed, entry->size);
1688 + if (entry->same) {
1689 + set_data_offset(entry, base, entry->same->offset);
1690 + entry->offset = entry->same->offset;
1693 + set_data_offset(entry, base, offset);
1694 + entry->offset = offset;
1695 + offset = do_compress(base, offset, entry->name, entry->uncompressed, entry->size);
1698 else if (entry->child)
1699 offset = write_data(entry->child, base, offset);
1700 - entry=entry->next;
1701 + entry=entry->next;
1705 @@ -552,10 +637,12 @@
1707 fd = open(file, O_RDONLY);
1711 + die(MKFS_ERROR, 1, "open failed: %s", file);
1713 buf = mmap(NULL, image_length, PROT_READ, MAP_PRIVATE, fd, 0);
1714 + if (buf == MAP_FAILED) {
1715 + die(MKFS_ERROR, 1, "mmap failed");
1717 memcpy(base + offset, buf, image_length);
1718 munmap(buf, image_length);
1720 @@ -567,27 +654,6 @@
1721 return (offset + image_length);
1725 - * Maximum size fs you can create is roughly 256MB. (The last file's
1726 - * data must begin within 256MB boundary but can extend beyond that.)
1728 - * Note that if you want it to fit in a ROM then you're limited to what the
1729 - * hardware and kernel can support (64MB?).
1731 -#define MAXFSLEN ((((1 << CRAMFS_OFFSET_WIDTH) - 1) << 2) /* offset */ \
1732 - + (1 << CRAMFS_SIZE_WIDTH) - 1 /* filesize */ \
1733 - + (1 << CRAMFS_SIZE_WIDTH) * 4 / PAGE_CACHE_SIZE /* block pointers */ )
1739 - * mkcramfs directory-name outfile
1741 - * where "directory-name" is simply the root of the directory
1742 - * tree that we want to generate a compressed filesystem out
1745 int main(int argc, char **argv)
1747 struct stat st; /* used twice... */
1749 /* initial guess (upper-bound) of required filesystem size */
1750 loff_t fslen_ub = sizeof(struct cramfs_super);
1751 char const *dirname, *outfile;
1752 - u32 crc = crc32(0L, Z_NULL, 0);
1754 int c; /* for getopt */
1755 + char *ep; /* for strtoul */
1759 @@ -607,21 +674,23 @@
1762 /* command line options */
1763 - while ((c = getopt(argc, argv, "hEe:i:n:psz")) != EOF) {
1764 + while ((c = getopt(argc, argv, "hEe:i:n:psvz")) != EOF) {
1773 - opt_edition = atoi(optarg);
1775 + opt_edition = strtoul(optarg, &ep, 10);
1776 + if (errno || optarg[0] == '\0' || *ep != '\0')
1777 + usage(MKFS_USAGE);
1781 if (lstat(opt_image, &st) < 0) {
1782 - perror(opt_image);
1784 + die(MKFS_ERROR, 1, "lstat failed: %s", opt_image);
1786 image_length = st.st_size; /* may be padded later */
1787 fslen_ub += (image_length + 3); /* 3 is for padding */
1790 /* old option, ignored */
1798 @@ -643,20 +715,21 @@
1801 if ((argc - optind) != 2)
1803 + usage(MKFS_USAGE);
1804 dirname = argv[optind];
1805 outfile = argv[optind + 1];
1807 if (stat(dirname, &st) < 0) {
1810 + die(MKFS_USAGE, 1, "stat failed: %s", dirname);
1812 fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1814 + die(MKFS_USAGE, 1, "open failed: %s", outfile);
1817 root_entry = calloc(1, sizeof(struct entry));
1821 + die(MKFS_ERROR, 1, "calloc failed");
1823 root_entry->mode = st.st_mode;
1824 root_entry->uid = st.st_uid;
1825 @@ -665,35 +738,34 @@
1826 root_entry->size = parse_directory(root_entry, dirname, &root_entry->child, &fslen_ub);
1828 /* always allocate a multiple of blksize bytes because that's
1829 - what we're going to write later on */
1830 + what we're going to write later on */
1831 fslen_ub = ((fslen_ub - 1) | (blksize - 1)) + 1;
1833 if (fslen_ub > MAXFSLEN) {
1835 - "warning: guestimate of required size (upper bound) is %LdMB, but maximum image size is %uMB. We might die prematurely.\n",
1836 + "warning: estimate of required size (upper bound) is %LdMB, but maximum image size is %uMB, we might die prematurely\n",
1839 fslen_ub = MAXFSLEN;
1842 - /* find duplicate files. TODO: uses the most inefficient algorithm
1844 - eliminate_doubles(root_entry,root_entry);
1845 + /* find duplicate files. TODO: uses the most inefficient algorithm
1847 + eliminate_doubles(root_entry,root_entry);
1849 /* TODO: Why do we use a private/anonymous mapping here
1850 - followed by a write below, instead of just a shared mapping
1851 - and a couple of ftruncate calls? Is it just to save us
1852 - having to deal with removing the file afterwards? If we
1853 - really need this huge anonymous mapping, we ought to mmap
1854 - in smaller chunks, so that the user doesn't need nn MB of
1855 - RAM free. If the reason is to be able to write to
1856 - un-mmappable block devices, then we could try shared mmap
1857 - and revert to anonymous mmap if the shared mmap fails. */
1858 + followed by a write below, instead of just a shared mapping
1859 + and a couple of ftruncate calls? Is it just to save us
1860 + having to deal with removing the file afterwards? If we
1861 + really need this huge anonymous mapping, we ought to mmap
1862 + in smaller chunks, so that the user doesn't need nn MB of
1863 + RAM free. If the reason is to be able to write to
1864 + un-mmappable block devices, then we could try shared mmap
1865 + and revert to anonymous mmap if the shared mmap fails. */
1866 rom_image = mmap(NULL, fslen_ub?fslen_ub:1, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1868 - if (-1 == (int) (long) rom_image) {
1869 - perror("ROM image map");
1871 + if (rom_image == MAP_FAILED) {
1872 + die(MKFS_ERROR, 1, "mmap failed");
1875 /* Skip the first opt_pad bytes for boot loader code */
1877 offset = write_data(root_entry, rom_image, offset);
1879 /* We always write a multiple of blksize bytes, so that
1882 offset = ((offset - 1) | (blksize - 1)) + 1;
1883 printf("Everything: %d kilobytes\n", offset >> 10);
1885 @@ -724,53 +796,58 @@
1886 printf("Super block: %d bytes\n", sizeof(struct cramfs_super));
1888 /* Put the checksum in. */
1889 + crc = crc32(0L, Z_NULL, 0);
1890 crc = crc32(crc, (rom_image+opt_pad), (offset-opt_pad));
1891 ((struct cramfs_super *) (rom_image+opt_pad))->fsid.crc = crc;
1892 printf("CRC: %x\n", crc);
1894 /* Check to make sure we allocated enough space. */
1895 if (fslen_ub < offset) {
1896 - fprintf(stderr, "not enough space allocated for ROM image (%Ld allocated, %d used)\n",
1897 - fslen_ub, offset);
1899 + die(MKFS_ERROR, 0, "not enough space allocated for ROM image (%Ld allocated, %d used)", fslen_ub, offset);
1902 written = write(fd, rom_image, offset);
1904 - perror("ROM image");
1906 + die(MKFS_ERROR, 1, "write failed");
1908 if (offset != written) {
1909 - fprintf(stderr, "ROM image write failed (%d %d)\n", written, offset);
1911 + die(MKFS_ERROR, 0, "ROM image write failed (wrote %d of %d bytes)", written, offset);
1914 /* (These warnings used to come at the start, but they scroll off the
1915 - screen too quickly.) */
1916 - if (warn_namelen) /* (can't happen when reading from ext2fs) */
1917 - fprintf(stderr, /* bytes, not chars: think UTF8. */
1918 - "warning: filenames truncated to 255 bytes.\n");
1919 + screen too quickly.) */
1921 + fprintf(stderr, /* bytes, not chars: think UTF-8. */
1922 + "warning: filenames truncated to %d bytes (possibly less if multi-byte UTF-8)\n",
1923 + CRAMFS_MAXPATHLEN);
1925 - fprintf(stderr, "warning: files were skipped due to errors.\n");
1926 + fprintf(stderr, "warning: files were skipped due to errors\n");
1929 - "warning: file sizes truncated to %luMB (minus 1 byte).\n",
1930 + "warning: file sizes truncated to %luMB (minus 1 byte)\n",
1931 1L << (CRAMFS_SIZE_WIDTH - 20));
1932 if (warn_uid) /* (not possible with current Linux versions) */
1934 - "warning: uids truncated to %u bits. (This may be a security concern.)\n",
1935 + "warning: uids truncated to %u bits (this may be a security concern)\n",
1939 - "warning: gids truncated to %u bits. (This may be a security concern.)\n",
1940 + "warning: gids truncated to %u bits (this may be a security concern)\n",
1944 - "WARNING: device numbers truncated to %u bits. This almost certainly means\n"
1945 - "that some device files will be wrong.\n",
1946 + "WARNING: device numbers truncated to %u bits (this almost certainly means\n"
1947 + "that some device files will be wrong)\n",
1948 CRAMFS_OFFSET_WIDTH);
1950 (warn_namelen||warn_skip||warn_size||warn_uid||warn_gid||warn_dev))
1959 + * Local variables:
1960 + * c-file-style: "linux"