]> git.pld-linux.org Git - packages/kernel.git/blob - linux-2.4.17-cramfs.patch
- obsolete
[packages/kernel.git] / linux-2.4.17-cramfs.patch
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
4 @@ -19,9 +19,7 @@
5  File sizes are limited to less than 16MB.
6  
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.)
12  
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
15 @@ -48,18 +46,28 @@
16  
17  
18  For /usr/share/magic
19 -------------------
20 +--------------------
21  
22 -0      long    0x28cd3d45      Linux cramfs
23 ->4     long    x               size %d
24 ->8     long    x               flags 0x%x
25 ->12    long    x               future 0x%x
26 +0      ulelong 0x28cd3d45      Linux cramfs offset 0
27 +>4     ulelong x               size %d
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"
50  
51  
52  Hacker Notes
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
56 @@ -29,6 +29,10 @@
57  lines); put another way, the same order as `find -type d -exec
58  ls -AU1 {} \;'.
59  
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.
63 +
64  <data>:
65         One <file_data> for each file that's either a symlink or a
66          regular file of non-zero st_size.
67 @@ -63,17 +67,22 @@
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
73 -them.
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.
76  
77  
78  Tools
79  -----
80  
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.
86 +
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.
92  
93  
94  Future Development
95 @@ -103,8 +112,8 @@
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.
100 -
101 +PAGE_CACHE_SIZE is subject to change between kernel versions
102 +(currently possible with arm and ia64).
103  
104  The remaining options try to make cramfs more sharable.
105  
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
109 @@ -24,6 +24,12 @@
110  #define CRAMFS_OFFSET_WIDTH 26
111  
112  /*
113 + * Since inode.namelen is a unsigned 6-bit number, the maximum cramfs
114 + * path length is 63 << 2 = 252.
115 + */
116 +#define CRAMFS_MAXPATHLEN (((1 << CRAMFS_NAMELEN_WIDTH) - 1) << 2)
117 +
118 +/*
119   * Reasonably terse representation of the inode data.
120   */
121  struct cramfs_inode {
122 @@ -52,14 +58,14 @@
123   * Superblock information at the beginning of the FS.
124   */
125  struct cramfs_super {
126 -       u32 magic;              /* 0x28cd3d45 - random number */
127 -       u32 size;               /* length in bytes */
128 -       u32 flags;              /* 0 */
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 */
141  };
142  
143  /*
144 @@ -79,7 +85,10 @@
145   * if (flags & ~CRAMFS_SUPPORTED_FLAGS).  Maybe that should be
146   * changed to test super.future instead.
147   */
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 )
153  
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
159 @@ -1,7 +1,7 @@
160  /*
161   * cramfsck - check a cramfs file system
162   *
163 - * Copyright (C) 2000-2001 Transmeta Corporation
164 + * Copyright (C) 2000-2002 Transmeta Corporation
165   *
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
168 @@ -28,11 +28,14 @@
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)
174   */
175  
176  /* compile-time options */
177  #define INCLUDE_FS_TESTS       /* include cramfs checking and extraction */
178  
179 +#define _GNU_SOURCE
180  #include <sys/types.h>
181  #include <stdio.h>
182  #include <sys/stat.h>
183 @@ -43,7 +46,6 @@
184  #include <stdlib.h>
185  #include <errno.h>
186  #include <string.h>
187 -#include <assert.h>
188  #include <getopt.h>
189  #include <sys/sysmacros.h>
190  #include <utime.h>
191 @@ -53,25 +55,34 @@
192  #include <linux/cramfs_fs.h>
193  #include <zlib.h>
194  
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 */
203 +
204 +#define PAD_SIZE 512
205 +#define PAGE_CACHE_SIZE (4096)
206 +
207  static const char *progname = "cramfsck";
208  
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) */
217 -
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 */
226  
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 */
234  
235  /* Guarantee access to at least 8kB at a time */
236  #define ROMBUFFER_BITS 13
237 @@ -82,8 +93,10 @@
238  
239  /* Uncompressing data structures... */
240  static char outbuffer[PAGE_CACHE_SIZE*2];
241 -z_stream stream;
242 +static z_stream stream;
243  
244 +/* Prototypes */
245 +static void expand_fs(char *, struct cramfs_inode *);
246  #endif /* INCLUDE_FS_TESTS */
247  
248  /* Input status of 0 to print help and exit without an error. */
249 @@ -100,8 +113,159 @@
250         exit(status);
251  }
252  
253 +static void die(int status, int syserr, const char *fmt, ...)
254 +{
255 +       va_list arg_ptr;
256 +       int save = errno;
257 +
258 +       fflush(0);
259 +       va_start(arg_ptr, fmt);
260 +       fprintf(stderr, "%s: ", progname);
261 +       vfprintf(stderr, fmt, arg_ptr);
262 +       if (syserr) {
263 +               fprintf(stderr, ": %s", strerror(save));
264 +       }
265 +       fprintf(stderr, "\n");
266 +       va_end(arg_ptr);
267 +       exit(status);
268 +}
269 +
270 +static void test_super(int *start, size_t *length) {
271 +       struct stat st;
272 +
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);
276 +       }
277 +       fd = open(filename, O_RDONLY);
278 +       if (fd < 0) {
279 +               die(FSCK_ERROR, 1, "open failed: %s", filename);
280 +       }
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);
284 +               }
285 +               *length = *length * 512;
286 +       }
287 +       else if (S_ISREG(st.st_mode)) {
288 +               *length = st.st_size;
289 +       }
290 +       else {
291 +               die(FSCK_ERROR, 0, "not a block device or file: %s", filename);
292 +       }
293 +
294 +       if (*length < sizeof(struct cramfs_super)) {
295 +               die(FSCK_UNCORRECTED, 0, "file length too short");
296 +       }
297 +
298 +       /* find superblock */
299 +       if (read(fd, &super, sizeof(super)) != sizeof(super)) {
300 +               die(FSCK_ERROR, 1, "read failed: %s", filename);
301 +       }
302 +       if (super.magic == CRAMFS_MAGIC) {
303 +               *start = 0;
304 +       }
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);
309 +               }
310 +               if (super.magic == CRAMFS_MAGIC) {
311 +                       *start = PAD_SIZE;
312 +               }
313 +       }
314 +
315 +       /* superblock tests */
316 +       if (super.magic != CRAMFS_MAGIC) {
317 +               die(FSCK_UNCORRECTED, 0, "superblock magic not found");
318 +       }
319 +       if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
320 +               die(FSCK_ERROR, 0, "unsupported filesystem features");
321 +       }
322 +       if (super.size < PAGE_CACHE_SIZE) {
323 +               die(FSCK_UNCORRECTED, 0, "superblock size (%d) too small", super.size);
324 +       }
325 +       if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
326 +               if (super.fsid.files == 0) {
327 +                       die(FSCK_UNCORRECTED, 0, "zero file count");
328 +               }
329 +               if (*length < super.size) {
330 +                       die(FSCK_UNCORRECTED, 0, "file length too short");
331 +               }
332 +               else if (*length > super.size) {
333 +                       fprintf(stderr, "warning: file extends past end of filesystem\n");
334 +               }
335 +       }
336 +       else {
337 +               fprintf(stderr, "warning: old cramfs format\n");
338 +       }
339 +}
340 +
341 +static void test_crc(int start)
342 +{
343 +       void *buf;
344 +       u32 crc;
345 +
346 +       if (!(super.flags & CRAMFS_FLAG_FSID_VERSION_2)) {
347 +#ifdef INCLUDE_FS_TESTS
348 +               return;
349 +#else /* not INCLUDE_FS_TESTS */
350 +               die(FSCK_USAGE, 0, "unable to test CRC: old cramfs format");
351 +#endif /* not INCLUDE_FS_TESTS */
352 +       }
353 +
354 +       crc = crc32(0L, Z_NULL, 0);
355 +
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);
362 +               }
363 +       }
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);
368 +       }
369 +       else {
370 +               int retval;
371 +               size_t length = 0;
372 +
373 +               buf = malloc(4096);
374 +               if (!buf) {
375 +                       die(FSCK_ERROR, 1, "malloc failed");
376 +               }
377 +               lseek(fd, start, SEEK_SET);
378 +               for (;;) {
379 +                       retval = read(fd, buf, 4096);
380 +                       if (retval < 0) {
381 +                               die(FSCK_ERROR, 1, "read failed: %s", filename);
382 +                       }
383 +                       else if (retval == 0) {
384 +                               break;
385 +                       }
386 +                       if (length == 0) {
387 +                               ((struct cramfs_super *) buf)->fsid.crc = crc32(0L, Z_NULL, 0);
388 +                       }
389 +                       length += retval;
390 +                       if (length > (super.size-start)) {
391 +                               crc = crc32(crc, buf, retval - (length - (super.size-start)));
392 +                               break;
393 +                       }
394 +                       crc = crc32(crc, buf, retval);
395 +               }
396 +               free(buf);
397 +       }
398 +
399 +       if (crc != super.fsid.crc) {
400 +               die(FSCK_UNCORRECTED, 0, "crc error");
401 +       }
402 +}
403 +
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)
407  {
408         char info[10];
409  
410 @@ -135,6 +299,10 @@
411  static struct cramfs_inode *cramfs_iget(struct cramfs_inode * i)
412  {
413         struct cramfs_inode *inode = malloc(sizeof(struct cramfs_inode));
414 +
415 +       if (!inode) {
416 +               die(FSCK_ERROR, 1, "malloc failed");
417 +       }
418         *inode = *i;
419         return inode;
420  }
421 @@ -144,27 +312,27 @@
422         return cramfs_iget(romfs_read(ino));
423  }
424  
425 -void iput(struct cramfs_inode *inode)
426 +static void iput(struct cramfs_inode *inode)
427  {
428         free(inode);
429  }
430  
431  /*
432 - * Return the offset of the root directory,
433 - * or 0 if none.
434 + * Return the offset of the root directory
435   */
436  static struct cramfs_inode *read_super(void)
437  {
438 -       unsigned long offset;
439 +       unsigned long offset = super.root.offset << 2;
440  
441 -       offset = super->root.offset << 2;
442 -       if (super->magic != CRAMFS_MAGIC)
443 -               return NULL;
444 -       if (memcmp(super->signature, CRAMFS_SIGNATURE, sizeof(super->signature)) != 0)
445 -               return NULL;
446 -       if (offset < sizeof(super))
447 -               return NULL;
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))))
454 +       {
455 +               die(FSCK_UNCORRECTED, 0, "bad root offset (%lu)", offset);
456 +       }
457 +       return cramfs_iget(&super.root);
458  }
459  
460  static int uncompress_block(void *src, int len)
461 @@ -179,38 +347,177 @@
462  
463         inflateReset(&stream);
464  
465 +       if (len > PAGE_CACHE_SIZE*2) {
466 +               die(FSCK_UNCORRECTED, 0, "data block too large");
467 +       }
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);
472 -               exit(4);
473 +               die(FSCK_UNCORRECTED, 0, "decompression error %p(%d): %s",
474 +                   zError(err), src, len);
475         }
476         return stream.total_out;
477  }
478  
479 +static void do_uncompress(char *path, int fd, unsigned long offset, unsigned long size)
480 +{
481 +       unsigned long curr = offset + 4 * ((size + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE);
482 +
483 +       do {
484 +               unsigned long out = PAGE_CACHE_SIZE;
485 +               unsigned long next = *(u32 *) romfs_read(offset);
486 +
487 +               if (next > end_data) {
488 +                       end_data = next;
489 +               }
490 +
491 +               offset += 4;
492 +               if (curr == next) {
493 +                       if (opt_verbose > 1) {
494 +                               printf("  hole at %ld (%d)\n", curr, PAGE_CACHE_SIZE);
495 +                       }
496 +                       if (size < PAGE_CACHE_SIZE)
497 +                               out = size;
498 +                       memset(outbuffer, 0x00, out);
499 +               }
500 +               else {
501 +                       if (opt_verbose > 1) {
502 +                               printf("  uncompressing block at %ld to %ld (%ld)\n", curr, next, next - curr);
503 +                       }
504 +                       out = uncompress_block(romfs_read(curr), next - curr);
505 +               }
506 +               if (size >= PAGE_CACHE_SIZE) {
507 +                       if (out != PAGE_CACHE_SIZE) {
508 +                               die(FSCK_UNCORRECTED, 0, "non-block (%ld) bytes", out);
509 +                       }
510 +               } else {
511 +                       if (out != size) {
512 +                               die(FSCK_UNCORRECTED, 0, "non-size (%ld vs %ld) bytes", out, size);
513 +                       }
514 +               }
515 +               size -= out;
516 +               if (opt_extract) {
517 +                       if (write(fd, outbuffer, out) < 0) {
518 +                               die(FSCK_ERROR, 1, "write failed: %s", path);
519 +                       }
520 +               }
521 +               curr = next;
522 +       } while (size);
523 +}
524 +
525  static void change_file_status(char *path, struct cramfs_inode *i)
526  {
527         struct utimbuf epoch = { 0, 0 };
528  
529         if (euid == 0) {
530                 if (lchown(path, i->uid, i->gid) < 0) {
531 -                       perror(path);
532 -                       exit(8);
533 +                       die(FSCK_ERROR, 1, "lchown failed: %s", path);
534                 }
535                 if (S_ISLNK(i->mode))
536                         return;
537                 if ((S_ISUID | S_ISGID) & i->mode) {
538                         if (chmod(path, i->mode) < 0) {
539 -                               perror(path);
540 -                               exit(8);
541 +                               die(FSCK_ERROR, 1, "chown failed: %s", path);
542                         }
543                 }
544         }
545         if (S_ISLNK(i->mode))
546                 return;
547         if (utime(path, &epoch) < 0) {
548 -               perror(path);
549 -               exit(8);
550 +               die(FSCK_ERROR, 1, "utime failed: %s", path);
551 +       }
552 +}
553 +
554 +static void do_directory(char *path, struct cramfs_inode *i)
555 +{
556 +       int pathlen = strlen(path);
557 +       int count = i->size;
558 +       unsigned long offset = i->offset << 2;
559 +       char *newpath = malloc(pathlen + 256);
560 +
561 +       if (!newpath) {
562 +               die(FSCK_ERROR, 1, "malloc failed");
563 +       }
564 +       if (offset == 0 && count != 0) {
565 +               die(FSCK_UNCORRECTED, 0, "directory inode has zero offset and non-zero size: %s", path);
566 +       }
567 +       if (offset != 0 && offset < start_dir) {
568 +               start_dir = offset;
569 +       }
570 +       /* TODO: Do we need to check end_dir for empty case? */
571 +       memcpy(newpath, path, pathlen);
572 +       newpath[pathlen] = '/';
573 +       pathlen++;
574 +       if (opt_verbose) {
575 +               print_node('d', i, path);
576 +       }
577 +       if (opt_extract) {
578 +               if (mkdir(path, i->mode) < 0) {
579 +                       die(FSCK_ERROR, 1, "mkdir failed: %s", path);
580 +               }
581 +               change_file_status(path, i);
582 +       }
583 +       while (count > 0) {
584 +               struct cramfs_inode *child = iget(offset);
585 +               int size;
586 +               int newlen = child->namelen << 2;
587 +
588 +               size = sizeof(struct cramfs_inode) + newlen;
589 +               count -= size;
590 +
591 +               offset += sizeof(struct cramfs_inode);
592 +
593 +               memcpy(newpath + pathlen, romfs_read(offset), newlen);
594 +               newpath[pathlen + newlen] = 0;
595 +               if (newlen == 0) {
596 +                       die(FSCK_UNCORRECTED, 0, "filename length is zero");
597 +               }
598 +               if ((pathlen + newlen) - strlen(newpath) > 3) {
599 +                       die(FSCK_UNCORRECTED, 0, "bad filename length");
600 +               }
601 +               expand_fs(newpath, child);
602 +
603 +               offset += newlen;
604 +
605 +               if (offset <= start_dir) {
606 +                       die(FSCK_UNCORRECTED, 0, "bad inode offset");
607 +               }
608 +               if (offset > end_dir) {
609 +                       end_dir = offset;
610 +               }
611 +               iput(child); /* free(child) */
612 +       }
613 +       free(newpath);
614 +}
615 +
616 +static void do_file(char *path, struct cramfs_inode *i)
617 +{
618 +       unsigned long offset = i->offset << 2;
619 +       int fd = 0;
620 +
621 +       if (offset == 0 && i->size != 0) {
622 +               die(FSCK_UNCORRECTED, 0, "file inode has zero offset and non-zero size");
623 +       }
624 +       if (i->size == 0 && offset != 0) {
625 +               die(FSCK_UNCORRECTED, 0, "file inode has zero size and non-zero offset");
626 +       }
627 +       if (offset != 0 && offset < start_data) {
628 +               start_data = offset;
629 +       }
630 +       if (opt_verbose) {
631 +               print_node('f', i, path);
632 +       }
633 +       if (opt_extract) {
634 +               fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, i->mode);
635 +               if (fd < 0) {
636 +                       die(FSCK_ERROR, 1, "open failed: %s", path);
637 +               }
638 +       }
639 +       if (i->size) {
640 +               do_uncompress(path, fd, offset, i->size);
641 +       }
642 +       if (opt_extract) {
643 +               close(fd);
644 +               change_file_status(path, i);
645         }
646  }
647  
648 @@ -221,32 +528,39 @@
649         unsigned long next = *(u32 *) romfs_read(offset);
650         unsigned long size;
651  
652 +       if (offset == 0) {
653 +               die(FSCK_UNCORRECTED, 0, "symbolic link has zero offset");
654 +       }
655 +       if (i->size == 0) {
656 +               die(FSCK_UNCORRECTED, 0, "symbolic link has zero size");
657 +       }
658 +
659 +       if (offset < start_data) {
660 +               start_data = offset;
661 +       }
662         if (next > end_data) {
663                 end_data = next;
664         }
665  
666         size = uncompress_block(romfs_read(curr), next - curr);
667         if (size != i->size) {
668 -               fprintf(stderr, "%s: size error in symlink `%s'\n",
669 -                       filename, path);
670 -               exit(4);
671 +               die(FSCK_UNCORRECTED, 0, "size error in symlink: %s", path);
672         }
673         outbuffer[size] = 0;
674         if (opt_verbose) {
675                 char *str;
676  
677 -               str = malloc(strlen(outbuffer) + strlen(path) + 5);
678 -               strcpy(str, path);
679 -               strncat(str, " -> ", 4);
680 -               strncat(str, outbuffer, size);
681 -
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);
686                 }
687 +               free(str);
688         }
689         if (opt_extract) {
690 -               symlink(outbuffer, path);
691 +               if (symlink(outbuffer, path) < 0) {
692 +                       die(FSCK_ERROR, 1, "symlink failed: %s", path);
693 +               }
694                 change_file_status(path, i);
695         }
696  }
697 @@ -256,6 +570,9 @@
698         dev_t devtype = 0;
699         char type;
700  
701 +       if (i->offset) {        /* no need to shift offset */
702 +               die(FSCK_UNCORRECTED, 0, "special file has non-zero offset: %s", path);
703 +       }
704         if (S_ISCHR(i->mode)) {
705                 devtype = i->size;
706                 type = 'c';
707 @@ -264,13 +581,21 @@
708                 devtype = i->size;
709                 type = 'b';
710         }
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);
715 +               }
716                 type = 'p';
717 -       else if (S_ISSOCK(i->mode))
718 +       }
719 +       else if (S_ISSOCK(i->mode)) {
720 +               if (i->size != 0) {
721 +                       die(FSCK_UNCORRECTED, 0, "socket has non-zero size: %s", path);
722 +               }
723                 type = 's';
724 +       }
725         else {
726 -               fprintf(stderr, "%s: bogus mode on `%s' (%o)\n", filename, path, i->mode);
727 -               exit(4);
728 +               die(FSCK_UNCORRECTED, 0, "bogus mode: %s (%o)", path, i->mode);
729 +               return;         /* not reached */
730         }
731  
732         if (opt_verbose) {
733 @@ -279,155 +604,62 @@
734  
735         if (opt_extract) {
736                 if (mknod(path, i->mode, devtype) < 0) {
737 -                       perror(path);
738 -                       exit(8);
739 +                       die(FSCK_ERROR, 1, "mknod failed: %s", path);
740                 }
741                 change_file_status(path, i);
742         }
743  }
744  
745 -static void do_uncompress(int fd, unsigned long offset, unsigned long size)
746 +static void expand_fs(char *path, struct cramfs_inode *inode)
747  {
748 -       unsigned long curr = offset + 4 * ((size + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE);
749 -
750 -       do {
751 -               unsigned long out = PAGE_CACHE_SIZE;
752 -               unsigned long next = *(u32 *) romfs_read(offset);
753 -
754 -               if (next > end_data) {
755 -                       end_data = next;
756 -               }
757 -
758 -               offset += 4;
759 -               if (curr == next) {
760 -                       if (opt_verbose > 1) {
761 -                               printf("  hole at %ld (%d)\n", curr, PAGE_CACHE_SIZE);
762 -                       }
763 -                       if (size < PAGE_CACHE_SIZE)
764 -                               out = size;
765 -                       memset(outbuffer, 0x00, out);
766 -               }
767 -               else {
768 -                       if (opt_verbose > 1) {
769 -                               printf("  uncompressing block at %ld to %ld (%ld)\n", curr, next, next - curr);
770 -                       }
771 -                       out = uncompress_block(romfs_read(curr), next - curr);
772 -               }
773 -               if (size >= PAGE_CACHE_SIZE) {
774 -                       if (out != PAGE_CACHE_SIZE) {
775 -                               fprintf(stderr, "%s: Non-block (%ld) bytes\n", filename, out);
776 -                               exit(4);
777 -                       }
778 -               } else {
779 -                       if (out != size) {
780 -                               fprintf(stderr, "%s: Non-size (%ld vs %ld) bytes\n", filename, out, size);
781 -                               exit(4);
782 -                       }
783 -               }
784 -               size -= out;
785 -               if (opt_extract) {
786 -                       write(fd, outbuffer, out);
787 -               }
788 -               curr = next;
789 -       } while (size);
790 +       if (S_ISDIR(inode->mode)) {
791 +               do_directory(path, inode);
792 +       }
793 +       else if (S_ISREG(inode->mode)) {
794 +               do_file(path, inode);
795 +       }
796 +       else if (S_ISLNK(inode->mode)) {
797 +               do_symlink(path, inode);
798 +       }
799 +       else {
800 +               do_special_inode(path, inode);
801 +       }
802  }
803  
804 -static void expand_fs(int pathlen, char *path, struct cramfs_inode *inode)
805 +static void test_fs(int start)
806  {
807 -       if (S_ISDIR(inode->mode)) {
808 -               int count = inode->size;
809 -               unsigned long offset = inode->offset << 2;
810 -               char *newpath = malloc(pathlen + 256);
811 -
812 -               if (count > 0 && offset < start_inode) {
813 -                       start_inode = offset;
814 -               }
815 -               /* XXX - need to check end_inode for empty case? */
816 -               memcpy(newpath, path, pathlen);
817 -               newpath[pathlen] = '/';
818 -               pathlen++;
819 -               if (opt_verbose) {
820 -                       print_node('d', inode, path);
821 -               }
822 -               if (opt_extract) {
823 -                       mkdir(path, inode->mode);
824 -                       change_file_status(path, inode);
825 -               }
826 -               while (count > 0) {
827 -                       struct cramfs_inode *child = iget(offset);
828 -                       int size;
829 -                       int newlen = child->namelen << 2;
830 -
831 -                       size = sizeof(struct cramfs_inode) + newlen;
832 -                       count -= size;
833 -
834 -                       offset += sizeof(struct cramfs_inode);
835 -
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);
840 -                               exit(4);
841 -                       }
842 -                       expand_fs(strlen(newpath), newpath, child);
843 -
844 -                       offset += newlen;
845 -
846 -                       if (offset > end_inode) {
847 -                               end_inode = offset;
848 -                       }
849 -               }
850 -               return;
851 -       }
852 -       if (S_ISREG(inode->mode)) {
853 -               int fd = 0;
854 -               unsigned long offset = inode->offset << 2;
855 +       struct cramfs_inode *root;
856  
857 -               if (offset > 0 && offset < start_data) {
858 -                       start_data = offset;
859 -               }
860 -               if (opt_verbose) {
861 -                       print_node('f', inode, path);
862 -               }
863 -               if (opt_extract) {
864 -                       fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, inode->mode);
865 -               }
866 -               if (inode->size) {
867 -                       do_uncompress(fd, offset, inode->size);
868 +       root = read_super();
869 +       umask(0);
870 +       euid = geteuid();
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);
879                 }
880 -               if (opt_extract) {
881 -                       close(fd);
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);
885                 }
886 -               return;
887         }
888 -       if (S_ISLNK(inode->mode)) {
889 -               unsigned long offset = inode->offset << 2;
890 -
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");
896                 }
897 -               do_symlink(path, inode);
898 -               return;
899 -       }
900 -       else {
901 -               do_special_inode(path, inode);
902 -               return;
903         }
904 +       iput(root);             /* free(root) */
905  }
906  #endif /* INCLUDE_FS_TESTS */
907  
908  int main(int argc, char **argv)
909  {
910 -       void *buf;
911 -       size_t length;
912 -       struct stat st;
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 */
918         int start = 0;
919 +       size_t length;
920  
921         if (argc)
922                 progname = argv[0];
923 @@ -436,17 +668,14 @@
924         while ((c = getopt(argc, argv, "hx:v")) != EOF) {
925                 switch (c) {
926                 case 'h':
927 -                       usage(0);
928 +                       usage(FSCK_OK);
929                 case 'x':
930  #ifdef INCLUDE_FS_TESTS
931                         opt_extract = 1;
932 -                       extract_dir = malloc(strlen(optarg) + 1);
933 -                       strcpy(extract_dir, optarg);
934 +                       extract_dir = optarg;
935                         break;
936 -#else /*  not INCLUDE_FS_TESTS */
937 -                       fprintf(stderr, "%s: compiled without -x support\n",
938 -                               progname);
939 -                       exit(16);
940 +#else /* not INCLUDE_FS_TESTS */
941 +                       die(FSCK_USAGE, 0, "compiled without -x support");
942  #endif /* not INCLUDE_FS_TESTS */
943                 case 'v':
944                         opt_verbose++;
945 @@ -455,134 +684,24 @@
946         }
947  
948         if ((argc - optind) != 1)
949 -               usage(16);
950 +               usage(FSCK_USAGE);
951         filename = argv[optind];
952  
953 -       /* find the physical size of the file or block device */
954 -       if (lstat(filename, &st) < 0) {
955 -               perror(filename);
956 -               exit(8);
957 -       }
958 -       fd = open(filename, O_RDONLY);
959 -       if (fd < 0) {
960 -               perror(filename);
961 -               exit(8);
962 -       }
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);
966 -                       exit(4);
967 -               }
968 -               length = length * 512;
969 -       }
970 -       else if (S_ISREG(st.st_mode)) {
971 -               length = st.st_size;
972 -       }
973 -       else {
974 -               fprintf(stderr, "%s is not a block device or file\n", filename);
975 -               exit(8);
976 -       }
977 -
978 -       if (length < sizeof(struct cramfs_super)) {
979 -               fprintf(stderr, "%s: invalid cramfs--file length too short\n", filename);
980 -               exit(4);
981 -       }
982 -
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);
987 -       }
988 -       else {
989 -               /* nice and easy */
990 -               buf = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
991 -       }
992 -
993 -       /* XXX - this could be cleaner... */
994 -       if (((struct cramfs_super *) buf)->magic == CRAMFS_MAGIC) {
995 -               start = 0;
996 -               super = (struct cramfs_super *) buf;
997 -       }
998 -       else if (length >= (PAD_SIZE + sizeof(struct cramfs_super)) &&
999 -                ((((struct cramfs_super *) (buf + PAD_SIZE))->magic == CRAMFS_MAGIC)))
1000 -       {
1001 -               start = PAD_SIZE;
1002 -               super = (struct cramfs_super *) (buf + PAD_SIZE);
1003 -       }
1004 -       else {
1005 -               fprintf(stderr, "%s: invalid cramfs--wrong magic\n", filename);
1006 -               exit(4);
1007 -       }
1008 -
1009 -       if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
1010 -               /* length test */
1011 -               if (length < super->size) {
1012 -                       fprintf(stderr, "%s: invalid cramfs--file length too short\n", filename);
1013 -                       exit(4);
1014 -               }
1015 -               else if (length > super->size) {
1016 -                       fprintf(stderr, "%s: warning--file length too long, padded image?\n", filename);
1017 -               }
1018 -
1019 -               /* CRC test */
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);
1026 -                       exit(4);
1027 -               }
1028 -       }
1029 -       else {
1030 -               fprintf(stderr, "%s: warning--old cramfs image, no CRC\n",
1031 -                       filename);
1032 -       }
1033 -
1034 +       test_super(&start, &length);
1035 +       test_crc(start);
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));
1040 -       }
1041 -       else if (length >= (PAD_SIZE + sizeof(struct cramfs_super)) &&
1042 -                ((((struct cramfs_super *) (buf + PAD_SIZE))->magic == CRAMFS_MAGIC)))
1043 -       {
1044 -               memcpy(super, (buf + PAD_SIZE), sizeof(struct cramfs_super));
1045 -       }
1046 -
1047 -       munmap(buf, length);
1048 -
1049 -       /* file format test, uses fake "blocked" accesses */
1050 -       root = read_super();
1051 -       umask(0);
1052 -       euid = geteuid();
1053 -       if (!root) {
1054 -               fprintf(stderr, "%s: invalid cramfs--bad superblock\n",
1055 -                       filename);
1056 -               exit(4);
1057 -       }
1058 -       stream.next_in = NULL;
1059 -       stream.avail_in = 0;
1060 -       inflateInit(&stream);
1061 -
1062 -       if (!extract_dir) {
1063 -               extract_dir = "root";
1064 -       }
1065 -
1066 -       expand_fs(strlen(extract_dir), extract_dir, root);
1067 -       inflateEnd(&stream);
1068 +       test_fs(start);
1069 +#endif /* INCLUDE_FS_TESTS */
1070  
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);
1073 -               exit(4);
1074 -       }
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);
1078 -                       exit(4);
1079 -               }
1080 +       if (opt_verbose) {
1081 +               printf("%s: OK\n", filename);
1082         }
1083 -#endif /* INCLUDE_FS_TESTS */
1084  
1085 -       exit(0);
1086 +       exit(FSCK_OK);
1087  }
1088 +
1089 +/*
1090 + * Local variables:
1091 + * c-file-style: "linux"
1092 + * End:
1093 + */
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
1097 @@ -1,7 +1,7 @@
1098  /*
1099   * mkcramfs - make a cramfs file system
1100   *
1101 - * Copyright (C) 1999-2001 Transmeta Corporation
1102 + * Copyright (C) 1999-2002 Transmeta Corporation
1103   *
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
1106 @@ -18,6 +18,10 @@
1107   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1108   */
1109  
1110 +/*
1111 + * If you change the disk format of cramfs, please update fs/cramfs/README.
1112 + */
1113 +
1114  #include <sys/types.h>
1115  #include <stdio.h>
1116  #include <sys/stat.h>
1117 @@ -28,39 +32,47 @@
1118  #include <stdlib.h>
1119  #include <errno.h>
1120  #include <string.h>
1121 -#include <assert.h>
1122  #include <getopt.h>
1123 +#include <stdarg.h>
1124  #include <linux/cramfs_fs.h>
1125  #include <zlib.h>
1126  
1127 -#define PAD_SIZE 512           /* only 0 and 512 supported by kernel */
1128 -
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 */
1134  
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
1138  
1139 -/* Input status of 0 to print help and exit without an error. */
1140 -static void usage(int status)
1141 -{
1142 -       FILE *stream = status ? stderr : stdout;
1143 +/* The kernel assumes PAGE_CACHE_SIZE as block size. */
1144 +#define PAGE_CACHE_SIZE (4096)
1145  
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);
1157 +/*
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.
1163 + *
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.
1167 + */
1168 +#define MAX_INPUT_NAMELEN 255
1169  
1170 -       exit(status);
1171 -}
1172 +/*
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.)
1175 + *
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.
1178 + */
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 */ )
1182  
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;
1189 @@ -72,31 +84,30 @@
1190   *
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.
1193 + *
1194 + * If opt_verbose is 1, be verbose.  If it is higher, be even more verbose.
1195   */
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;
1204  
1205  static int warn_dev, warn_gid, warn_namelen, warn_skip, warn_size, warn_uid;
1206  
1207 -#ifndef MIN
1208 -# define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
1209 -#endif
1210 -
1211  /* In-core version of inode / directory entry. */
1212  struct entry {
1213         /* stats */
1214 -       char *name;
1215 +       unsigned char *name;
1216         unsigned int mode, size, uid, gid;
1217  
1218         /* FS data */
1219         void *uncompressed;
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? */
1227  
1228         /* organization */
1229 @@ -104,34 +115,67 @@
1230         struct entry *next;
1231  };
1232  
1233 -/*
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.
1239 - */
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)
1243 +{
1244 +       FILE *stream = status ? stderr : stdout;
1245 +
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);
1258 +
1259 +       exit(status);
1260 +}
1261 +
1262 +static void die(int status, int syserr, const char *fmt, ...)
1263 +{
1264 +       va_list arg_ptr;
1265 +       int save = errno;
1266 +
1267 +       fflush(0);
1268 +       va_start(arg_ptr, fmt);
1269 +       fprintf(stderr, "%s: ", progname);
1270 +       vfprintf(stderr, fmt, arg_ptr);
1271 +       if (syserr) {
1272 +               fprintf(stderr, ": %s", strerror(save));
1273 +       }
1274 +       fprintf(stderr, "\n");
1275 +       va_end(arg_ptr);
1276 +       exit(status);
1277 +}
1278  
1279  static int find_identical_file(struct entry *orig,struct entry *newfile)
1280  {
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;
1285 -                return 1;
1286 -        }
1287 -        return find_identical_file(orig->child,newfile) ||
1288 -                   find_identical_file(orig->next,newfile);
1289 +       if (orig == newfile)
1290 +               return 1;
1291 +       if (!orig)
1292 +               return 0;
1293 +       if (orig->size == newfile->size && orig->uncompressed &&
1294 +           !memcmp(orig->uncompressed, newfile->uncompressed, orig->size))
1295 +       {
1296 +               newfile->same = orig;
1297 +               return 1;
1298 +       }
1299 +       return (find_identical_file(orig->child,newfile) ||
1300 +               find_identical_file(orig->next,newfile));
1301  }
1302  
1303  static void eliminate_doubles(struct entry *root,struct entry *orig) {
1304 -        if(orig) {
1305 -                if(orig->size && orig->uncompressed)
1306 +       if (orig) {
1307 +               if (orig->size && orig->uncompressed)
1308                         find_identical_file(root,orig);
1309 -                eliminate_doubles(root,orig->child);
1310 -                eliminate_doubles(root,orig->next);
1311 -        }
1312 +               eliminate_doubles(root,orig->child);
1313 +               eliminate_doubles(root,orig->next);
1314 +       }
1315  }
1316  
1317  /*
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);
1321         if (!path) {
1322 -               perror(NULL);
1323 -               exit(8);
1324 +               die(MKFS_ERROR, 1, "malloc failed");
1325         }
1326         memcpy(path, name, len);
1327         endpath = path + len;
1328         *endpath = '/';
1329         endpath++;
1330  
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);
1335  
1336         if (dircount < 0) {
1337 -               perror(name);
1338 -               exit(8);
1339 +               die(MKFS_ERROR, 1, "scandir failed: %s", name);
1340         }
1341  
1342         /* process directory */
1343 @@ -192,36 +234,38 @@
1344                 }
1345                 namelen = strlen(dirent->d_name);
1346                 if (namelen > MAX_INPUT_NAMELEN) {
1347 -                       fprintf(stderr,
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);
1354 -                       exit(8);
1355                 }
1356                 memcpy(endpath, dirent->d_name, namelen + 1);
1357  
1358                 if (lstat(path, &st) < 0) {
1359 -                       perror(endpath);
1360                         warn_skip = 1;
1361                         continue;
1362                 }
1363                 entry = calloc(1, sizeof(struct entry));
1364                 if (!entry) {
1365 -                       perror(NULL);
1366 -                       exit(8);
1367 +                       die(MKFS_ERROR, 1, "calloc failed");
1368                 }
1369                 entry->name = strdup(dirent->d_name);
1370                 if (!entry->name) {
1371 -                       perror(NULL);
1372 -                       exit(8);
1373 +                       die(MKFS_ERROR, 1, "strdup failed");
1374                 }
1375 -               if (namelen > 255) {
1376 -                       /* Can't happen when reading from ext2fs. */
1377 -
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;
1384                         warn_namelen = 1;
1385 +                       /* the first lost byte must not be a trail byte */
1386 +                       while ((entry->name[namelen] & 0xc0) == 0x80) {
1387 +                               namelen--;
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");
1391 +                               }
1392 +                       }
1393 +                       entry->name[namelen] = '\0';
1394                 }
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. */
1409                         warn_gid = 1;
1410                 size = sizeof(struct cramfs_inode) + ((namelen + 3) & ~3);
1411                 *fslen_ub += size;
1412 @@ -252,7 +296,6 @@
1413                            (which would just save a little memory). */
1414                         int fd = open(path, O_RDONLY);
1415                         if (fd < 0) {
1416 -                               perror(path);
1417                                 warn_skip = 1;
1418                                 continue;
1419                         }
1420 @@ -263,39 +306,38 @@
1421                                 }
1422  
1423                                 entry->uncompressed = mmap(NULL, entry->size, PROT_READ, MAP_PRIVATE, fd, 0);
1424 -                               if (-1 == (int) (long) entry->uncompressed) {
1425 -                                       perror("mmap");
1426 -                                       exit(8);
1427 +                               if (entry->uncompressed == MAP_FAILED) {
1428 +                                       die(MKFS_ERROR, 1, "mmap failed");
1429                                 }
1430                         }
1431                         close(fd);
1432                 } else if (S_ISLNK(st.st_mode)) {
1433                         entry->uncompressed = malloc(entry->size);
1434                         if (!entry->uncompressed) {
1435 -                               perror(NULL);
1436 -                               exit(8);
1437 +                               die(MKFS_ERROR, 1, "malloc failed");
1438                         }
1439                         if (readlink(path, entry->uncompressed, entry->size) < 0) {
1440 -                               perror(path);
1441                                 warn_skip = 1;
1442                                 continue;
1443                         }
1444                 } else if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
1445                         /* maybe we should skip sockets */
1446                         entry->size = 0;
1447 -               } else {
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))
1451                                 warn_dev = 1;
1452 +               } else {
1453 +                       die(MKFS_ERROR, 0, "bogus file type: %s", entry->name);
1454                 }
1455  
1456                 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
1457                         int blocks = ((entry->size - 1) / blksize + 1);
1458  
1459                         /* block pointers & data expansion allowance + data */
1460 -                       if(entry->size)
1461 +                       if (entry->size)
1462                                 *fslen_ub += (4+26)*blocks + entry->size + 3;
1463 -                }
1464 +               }
1465  
1466                 /* Link it into the list */
1467                 *prev = entry;
1468 @@ -313,9 +355,7 @@
1469         struct cramfs_super *super = (struct cramfs_super *) base;
1470         unsigned int offset = sizeof(struct cramfs_super) + image_length;
1471  
1472 -       if (opt_pad) {
1473 -               offset += opt_pad;
1474 -       }
1475 +       offset += opt_pad;      /* 0 if no padding */
1476  
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)
1481  {
1482         struct cramfs_inode *inode = (struct cramfs_inode *) (base + entry->dir_offset);
1483 -#ifdef DEBUG
1484 -       assert ((offset & 3) == 0);
1485 -#endif /* DEBUG */
1486 +
1487 +       if ((offset & 3) != 0) {
1488 +               die(MKFS_ERROR, 0, "illegal offset of %lu bytes", offset);
1489 +       }
1490         if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH))) {
1491 -               fprintf(stderr, "filesystem too big.  Exiting.\n");
1492 -               exit(8);
1493 +               die(MKFS_ERROR, 0, "filesystem too big");
1494         }
1495         inode->offset = (offset >> 2);
1496  }
1497  
1498 +/*
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.
1502 + */
1503 +static void print_node(struct entry *e)
1504 +{
1505 +       char info[10];
1506 +       char type = '?';
1507 +
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';
1515 +
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));
1519 +       }
1520 +       else {
1521 +               /* size be as high as 2^24 or 16777216 */
1522 +               snprintf(info, 10, "%9d", e->size);
1523 +       }
1524 +
1525 +       printf("%c %04o %s %5d:%-3d %s\n",
1526 +              type, e->mode & ~S_IFMT, info, e->uid, e->gid, e->name);
1527 +}
1528  
1529  /*
1530   * We do a width-first printout of the directory
1531   * entries, using a stack to remember the directories
1532   * we've seen.
1533   */
1534 -#define MAXENTRIES (100)
1535  static unsigned int write_directory_structure(struct entry *entry, char *base, unsigned int offset)
1536  {
1537         int stack_entries = 0;
1538 -       struct entry *entry_stack[MAXENTRIES];
1539 +       int stack_size = 64;
1540 +       struct entry **entry_stack;
1541 +
1542 +       entry_stack = malloc(stack_size * sizeof(struct entry *));
1543 +       if (!entry_stack) {
1544 +               die(MKFS_ERROR, 1, "malloc failed");
1545 +       }
1546 +
1547 +       if (opt_verbose) {
1548 +               printf("root:\n");
1549 +       }
1550  
1551         for (;;) {
1552                 int dir_start = stack_entries;
1553 @@ -398,15 +477,16 @@
1554                         inode->namelen = len >> 2;
1555                         offset += len;
1556  
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);
1562 +                       if (opt_verbose)
1563 +                               print_node(entry);
1564 +
1565                         if (entry->child) {
1566 -                               if (stack_entries >= MAXENTRIES) {
1567 -                                       fprintf(stderr, "Exceeded MAXENTRIES.  Raise this value in mkcramfs.c and recompile.  Exiting.\n");
1568 -                                       exit(8);
1569 +                               if (stack_entries >= stack_size) {
1570 +                                       stack_size *= 2;
1571 +                                       entry_stack = realloc(entry_stack, stack_size * sizeof(struct entry *));
1572 +                                       if (!entry_stack) {
1573 +                                               die(MKFS_ERROR, 1, "realloc failed");
1574 +                                       }
1575                                 }
1576                                 entry_stack[stack_entries] = entry;
1577                                 stack_entries++;
1578 @@ -416,9 +496,9 @@
1579  
1580                 /*
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
1584 -                 * `ls -UR' order.
1585 +                * this directory, for a small optimization of disk
1586 +                * access in the created fs.  This change makes things
1587 +                * `ls -UR' order.
1588                  */
1589                 {
1590                         struct entry **lo = entry_stack + dir_start;
1591 @@ -439,29 +519,27 @@
1592                 entry = entry_stack[stack_entries];
1593  
1594                 set_data_offset(entry, base, offset);
1595 -               printf("'%s':\n", entry->name);
1596 +               if (opt_verbose) {
1597 +                       printf("%s:\n", entry->name);
1598 +               }
1599                 entry = entry->child;
1600         }
1601 +       free(entry_stack);
1602         return offset;
1603  }
1604  
1605  static int is_zero(char const *begin, unsigned len)
1606  {
1607 -       if (opt_holes)
1608 -               /* Returns non-zero iff the first LEN bytes from BEGIN are
1609 -                  all NULs. */
1610 -               return (len-- == 0 ||
1611 -                       (begin[0] == '\0' &&
1612 -                        (len-- == 0 ||
1613 -                         (begin[1] == '\0' &&
1614 -                          (len-- == 0 ||
1615 -                           (begin[2] == '\0' &&
1616 -                            (len-- == 0 ||
1617 -                             (begin[3] == '\0' &&
1618 -                              memcmp(begin, begin + 4, len) == 0))))))));
1619 -       else
1620 -               /* Never create holes. */
1621 -               return 0;
1622 +       /* Returns non-zero iff the first LEN bytes from BEGIN are all NULs. */
1623 +       return (len-- == 0 ||
1624 +               (begin[0] == '\0' &&
1625 +                (len-- == 0 ||
1626 +                 (begin[1] == '\0' &&
1627 +                  (len-- == 0 ||
1628 +                   (begin[2] == '\0' &&
1629 +                    (len-- == 0 ||
1630 +                     (begin[3] == '\0' &&
1631 +                      memcmp(begin, begin + 4, len) == 0))))))));
1632  }
1633  
1634  /*
1635 @@ -488,19 +566,23 @@
1636         do {
1637                 unsigned long len = 2 * blksize;
1638                 unsigned int input = size;
1639 +               int err;
1640 +
1641                 if (input > blksize)
1642                         input = blksize;
1643                 size -= input;
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));
1650 +                       }
1651                         curr += len;
1652                 }
1653                 uncompressed += input;
1654  
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);
1658 -                       exit(8);
1659 +                       die(MKFS_ERROR, 0, "AIEEE: block \"compressed\" to > 2*blocklength (%ld)", len);
1660                 }
1661  
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);
1672 +       }
1673  
1674         return curr;
1675  }
1676 @@ -529,18 +613,19 @@
1677  {
1678         do {
1679                 if (entry->uncompressed) {
1680 -                        if(entry->same) {
1681 -                                set_data_offset(entry, base, entry->same->offset);
1682 -                                entry->offset=entry->same->offset;
1683 -                        } else {
1684 -                                set_data_offset(entry, base, offset);
1685 -                                entry->offset=offset;
1686 -                                offset = do_compress(base, offset, entry->name, entry->uncompressed, entry->size);
1687 -                        }
1688 +                       if (entry->same) {
1689 +                               set_data_offset(entry, base, entry->same->offset);
1690 +                               entry->offset = entry->same->offset;
1691 +                       }
1692 +                       else {
1693 +                               set_data_offset(entry, base, offset);
1694 +                               entry->offset = offset;
1695 +                               offset = do_compress(base, offset, entry->name, entry->uncompressed, entry->size);
1696 +                       }
1697                 }
1698                 else if (entry->child)
1699                         offset = write_data(entry->child, base, offset);
1700 -                entry=entry->next;
1701 +               entry=entry->next;
1702         } while (entry);
1703         return offset;
1704  }
1705 @@ -552,10 +637,12 @@
1706  
1707         fd = open(file, O_RDONLY);
1708         if (fd < 0) {
1709 -               perror(file);
1710 -               exit(8);
1711 +               die(MKFS_ERROR, 1, "open failed: %s", file);
1712         }
1713         buf = mmap(NULL, image_length, PROT_READ, MAP_PRIVATE, fd, 0);
1714 +       if (buf == MAP_FAILED) {
1715 +               die(MKFS_ERROR, 1, "mmap failed");
1716 +       }
1717         memcpy(base + offset, buf, image_length);
1718         munmap(buf, image_length);
1719         close (fd);
1720 @@ -567,27 +654,6 @@
1721         return (offset + image_length);
1722  }
1723  
1724 -/*
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.)
1727 - *
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?).
1730 - */
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 */ )
1734 -
1735 -
1736 -/*
1737 - * Usage:
1738 - *
1739 - *      mkcramfs directory-name outfile
1740 - *
1741 - * where "directory-name" is simply the root of the directory
1742 - * tree that we want to generate a compressed filesystem out
1743 - * of.
1744 - */
1745  int main(int argc, char **argv)
1746  {
1747         struct stat st;         /* used twice... */
1748 @@ -598,8 +664,9 @@
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);
1753 +       u32 crc;
1754         int c;                  /* for getopt */
1755 +       char *ep;               /* for strtoul */
1756  
1757         total_blocks = 0;
1758  
1759 @@ -607,21 +674,23 @@
1760                 progname = argv[0];
1761  
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) {
1765                 switch (c) {
1766                 case 'h':
1767 -                       usage(0);
1768 +                       usage(MKFS_OK);
1769                 case 'E':
1770                         opt_errors = 1;
1771                         break;
1772                 case 'e':
1773 -                       opt_edition = atoi(optarg);
1774 +                       errno = 0;
1775 +                       opt_edition = strtoul(optarg, &ep, 10);
1776 +                       if (errno || optarg[0] == '\0' || *ep != '\0')
1777 +                               usage(MKFS_USAGE);
1778                         break;
1779                 case 'i':
1780                         opt_image = optarg;
1781                         if (lstat(opt_image, &st) < 0) {
1782 -                               perror(opt_image);
1783 -                               exit(16);
1784 +                               die(MKFS_ERROR, 1, "lstat failed: %s", opt_image);
1785                         }
1786                         image_length = st.st_size; /* may be padded later */
1787                         fslen_ub += (image_length + 3); /* 3 is for padding */
1788 @@ -636,6 +705,9 @@
1789                 case 's':
1790                         /* old option, ignored */
1791                         break;
1792 +               case 'v':
1793 +                       opt_verbose++;
1794 +                       break;
1795                 case 'z':
1796                         opt_holes = 1;
1797                         break;
1798 @@ -643,20 +715,21 @@
1799         }
1800  
1801         if ((argc - optind) != 2)
1802 -               usage(16);
1803 +               usage(MKFS_USAGE);
1804         dirname = argv[optind];
1805         outfile = argv[optind + 1];
1806  
1807         if (stat(dirname, &st) < 0) {
1808 -               perror(dirname);
1809 -               exit(16);
1810 +               die(MKFS_USAGE, 1, "stat failed: %s", dirname);
1811         }
1812         fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1813 +       if (fd < 0) {
1814 +               die(MKFS_USAGE, 1, "open failed: %s", outfile);
1815 +       }
1816  
1817         root_entry = calloc(1, sizeof(struct entry));
1818         if (!root_entry) {
1819 -               perror(NULL);
1820 -               exit(8);
1821 +               die(MKFS_ERROR, 1, "calloc failed");
1822         }
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);
1827  
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;
1832  
1833         if (fslen_ub > MAXFSLEN) {
1834                 fprintf(stderr,
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",
1837                         fslen_ub >> 20,
1838                         MAXFSLEN >> 20);
1839                 fslen_ub = MAXFSLEN;
1840         }
1841  
1842 -        /* find duplicate files. TODO: uses the most inefficient algorithm
1843 -           possible. */
1844 -        eliminate_doubles(root_entry,root_entry);
1845 +       /* find duplicate files. TODO: uses the most inefficient algorithm
1846 +          possible. */
1847 +       eliminate_doubles(root_entry,root_entry);
1848  
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);
1867  
1868 -       if (-1 == (int) (long) rom_image) {
1869 -               perror("ROM image map");
1870 -               exit(8);
1871 +       if (rom_image == MAP_FAILED) {
1872 +               die(MKFS_ERROR, 1, "mmap failed");
1873         }
1874  
1875         /* Skip the first opt_pad bytes for boot loader code */
1876 @@ -715,7 +787,7 @@
1877         offset = write_data(root_entry, rom_image, offset);
1878  
1879         /* We always write a multiple of blksize bytes, so that
1880 -           losetup works. */
1881 +          losetup works. */
1882         offset = ((offset - 1) | (blksize - 1)) + 1;
1883         printf("Everything: %d kilobytes\n", offset >> 10);
1884  
1885 @@ -724,53 +796,58 @@
1886         printf("Super block: %d bytes\n", sizeof(struct cramfs_super));
1887  
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);
1893  
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);
1898 -               exit(8);
1899 +               die(MKFS_ERROR, 0, "not enough space allocated for ROM image (%Ld allocated, %d used)", fslen_ub, offset);
1900         }
1901  
1902         written = write(fd, rom_image, offset);
1903         if (written < 0) {
1904 -               perror("ROM image");
1905 -               exit(8);
1906 +               die(MKFS_ERROR, 1, "write failed");
1907         }
1908         if (offset != written) {
1909 -               fprintf(stderr, "ROM image write failed (%d %d)\n", written, offset);
1910 -               exit(8);
1911 +               die(MKFS_ERROR, 0, "ROM image write failed (wrote %d of %d bytes)", written, offset);
1912         }
1913  
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.) */
1920 +       if (warn_namelen)
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);
1924         if (warn_skip)
1925 -               fprintf(stderr, "warning: files were skipped due to errors.\n");
1926 +               fprintf(stderr, "warning: files were skipped due to errors\n");
1927         if (warn_size)
1928                 fprintf(stderr,
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) */
1933                 fprintf(stderr,
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",
1936                         CRAMFS_UID_WIDTH);
1937         if (warn_gid)
1938                 fprintf(stderr,
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",
1941                         CRAMFS_GID_WIDTH);
1942         if (warn_dev)
1943                 fprintf(stderr,
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);
1949         if (opt_errors &&
1950             (warn_namelen||warn_skip||warn_size||warn_uid||warn_gid||warn_dev))
1951 -               exit(8);
1952 -       return 0;
1953 +               exit(MKFS_ERROR);
1954 +
1955 +       exit(MKFS_OK);
1956  }
1957 +
1958 +/*
1959 + * Local variables:
1960 + * c-file-style: "linux"
1961 + * End:
1962 + */
1963
This page took 0.255542 seconds and 3 git commands to generate.