]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/mkfs.cramfs.c
disk-utils: add SPDX and Copyright notices
[thirdparty/util-linux.git] / disk-utils / mkfs.cramfs.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * mkcramfs - make a cramfs file system
5 *
6 * Copyright (C) 1999-2002 Transmeta Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 /*
24 * Old version would die on largish filesystems. Change to mmap the
25 * files one by one instead of all simultaneously. - aeb, 2002-11-01
26 */
27
28 #include <sys/types.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <sys/mman.h>
33 #include <fcntl.h>
34 #include <dirent.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <getopt.h>
40 #include <zconf.h>
41
42 /* We don't use our include/crc32.h, but crc32 from zlib!
43 *
44 * The zlib implementation performs pre/post-conditioning. The util-linux
45 * imlemenation requires post-conditioning (xor) in the applications.
46 */
47 #include <zlib.h>
48
49 #include "blkdev.h"
50 #include "c.h"
51 #include "cramfs.h"
52 #include "md5.h"
53 #include "nls.h"
54 #include "exitcodes.h"
55 #include "strutils.h"
56
57 #define CLOSE_EXIT_CODE MKFS_EX_ERROR
58 #include "closestream.h"
59
60 #define XALLOC_EXIT_CODE MKFS_EX_ERROR
61 #include "xalloc.h"
62
63 /* The kernel only supports PAD_SIZE of 0 and 512. */
64 #define PAD_SIZE 512
65
66 static int verbose = 0;
67
68 static unsigned int blksize = 0; /* settable via -b option, default page size */
69 static long total_blocks = 0, total_nodes = 1; /* pre-count the root node */
70 static int image_length = 0;
71 static int cramfs_is_big_endian = 0; /* target is big endian */
72
73 /*
74 * If opt_holes is set, then mkcramfs can create explicit holes in the
75 * data, which saves 26 bytes per hole (which is a lot smaller a
76 * saving than for most filesystems).
77 *
78 * Note that kernels up to at least 2.3.39 don't support cramfs holes,
79 * which is why this is turned off by default.
80 */
81 static unsigned int opt_edition = 0;
82 static int opt_errors = 0;
83 static int opt_holes = 0;
84 static int opt_pad = 0;
85 static char *opt_image = NULL;
86 static char *opt_name = NULL;
87
88 static int warn_dev = 0;
89 static int warn_gid = 0;
90 static int warn_namelen = 0;
91 static int warn_skip = 0;
92 static int warn_size = 0;
93 static int warn_uid = 0;
94
95 /* entry.flags */
96 #define CRAMFS_EFLAG_MD5 1
97 #define CRAMFS_EFLAG_INVALID 2
98
99 /* In-core version of inode / directory entry. */
100 struct entry {
101 /* stats */
102 unsigned char *name;
103 unsigned int mode, size, uid, gid;
104 unsigned char md5sum[UL_MD5LENGTH];
105 unsigned char flags; /* CRAMFS_EFLAG_* */
106
107 /* FS data */
108 char *path;
109 int fd; /* temporarily open files while mmapped */
110 struct entry *same; /* points to other identical file */
111 unsigned int offset; /* pointer to compressed data in archive */
112 unsigned int dir_offset; /* offset of directory entry in archive */
113
114 /* organization */
115 struct entry *child; /* NULL for non-directory and empty dir */
116 struct entry *next;
117 };
118
119 /*
120 * Width of various bitfields in struct cramfs_inode.
121 * Used only to generate warnings.
122 */
123 #define CRAMFS_SIZE_WIDTH 24
124 #define CRAMFS_UID_WIDTH 16
125 #define CRAMFS_GID_WIDTH 8
126 #define CRAMFS_OFFSET_WIDTH 26
127
128 static void __attribute__((__noreturn__)) usage(void)
129 {
130 fputs(USAGE_HEADER, stdout);
131 fprintf(stdout, _(" %s [-h] [-v] [-b blksize] [-e edition] [-N endian] [-i file] [-n name] dirname outfile\n"),
132 program_invocation_short_name);
133 fputs(USAGE_SEPARATOR, stdout);
134 fputs(_("Make compressed ROM file system."), stdout);
135 fputs(USAGE_OPTIONS, stdout);
136 fputs(_( " -v be verbose"), stdout);
137 fputs(_( " -E make all warnings errors (non-zero exit status)"), stdout);
138 fputs(_( " -b blksize use this blocksize, must equal page size"), stdout);
139 fputs(_( " -e edition set edition number (part of fsid)"), stdout);
140 fprintf(stdout, _(" -N endian set cramfs endianness (%s|%s|%s), default %s\n"), "big", "little", "host", "host");
141 fputs(_( " -i file insert a file image into the filesystem"), stdout);
142 fputs(_( " -n name set name of cramfs filesystem"), stdout);
143 fprintf(stdout, _(" -p pad by %d bytes for boot code\n"), PAD_SIZE);
144 fputs(_( " -s sort directory entries (old option, ignored)"), stdout);
145 fputs(_( " -z make explicit holes"), stdout);
146 fputs(_( " -l[=<mode>] use exclusive device lock (yes, no or nonblock)"), stdout);
147 fputs(_( " dirname root of the filesystem to be compressed"), stdout);
148 fputs(_( " outfile output file"), stdout);
149 fputs(USAGE_SEPARATOR, stdout);
150 fprintf(stdout, USAGE_HELP_OPTIONS(16));
151 fprintf(stdout, USAGE_MAN_TAIL("mkfs.cramfs(8)"));
152 exit(MKFS_EX_OK);
153 }
154
155 static char *
156 do_mmap(char *path, unsigned int size, unsigned int mode){
157 int fd;
158 char *start = NULL;
159
160 if (!size)
161 return NULL;
162
163 if (S_ISLNK(mode)) {
164 /* The link buffer is unnecessary to terminate by null as it's
165 * always used as buffer rather than a string */
166 start = xmalloc(size);
167 if (readlink(path, start, size) < 0) {
168 warn(_("readlink failed: %s"), path);
169 warn_skip = 1;
170 goto err;
171 }
172 return start;
173 }
174
175 fd = open(path, O_RDONLY);
176 if (fd < 0) {
177 warn(_("cannot open %s"), path);
178 warn_skip = 1;
179 goto err;
180 }
181
182 start = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
183 close(fd);
184 if (start == MAP_FAILED)
185 err(MKFS_EX_ERROR, "mmap");
186 return start;
187 err:
188 free(start);
189 return NULL;
190 }
191
192 static void
193 do_munmap(char *start, unsigned int size, unsigned int mode){
194 if (S_ISLNK(mode))
195 free(start);
196 else
197 munmap(start, size);
198 }
199
200 /* compute md5sums, so that we do not have to compare every pair of files */
201 static void
202 mdfile(struct entry *e) {
203 char *start;
204
205 start = do_mmap(e->path, e->size, e->mode);
206 if (start == NULL) {
207 e->flags |= CRAMFS_EFLAG_INVALID;
208 } else {
209 UL_MD5_CTX ctx;
210
211 ul_MD5Init(&ctx);
212 ul_MD5Update(&ctx, (unsigned char *) start, e->size);
213 ul_MD5Final(e->md5sum, &ctx);
214
215 do_munmap(start, e->size, e->mode);
216
217 e->flags |= CRAMFS_EFLAG_MD5;
218 }
219 }
220
221 /* md5 digests are equal; files are almost certainly the same,
222 but just to be sure, do the comparison */
223 static int
224 identical_file(struct entry *e1, struct entry *e2){
225 char *start1, *start2;
226 int equal;
227
228 start1 = do_mmap(e1->path, e1->size, e1->mode);
229 if (!start1)
230 return 0;
231 start2 = do_mmap(e2->path, e2->size, e2->mode);
232 if (!start2) {
233 do_munmap(start1, e1->size, e1->mode);
234 return 0;
235 }
236 equal = !memcmp(start1, start2, e1->size);
237 do_munmap(start1, e1->size, e1->mode);
238 do_munmap(start2, e2->size, e2->mode);
239 return equal;
240 }
241
242 /*
243 * The longest file name component to allow for in the input directory tree.
244 * Ext2fs (and many others) allow up to 255 bytes. A couple of filesystems
245 * allow longer (e.g. smbfs 1024), but there isn't much use in supporting
246 * >255-byte names in the input directory tree given that such names get
247 * truncated to 255 bytes when written to cramfs.
248 */
249 #define MAX_INPUT_NAMELEN 255
250
251 static int find_identical_file(struct entry *orig, struct entry *new, loff_t *fslen_ub)
252 {
253 if (orig == new)
254 return 1;
255 if (!orig)
256 return 0;
257 if (orig->size == new->size && orig->path) {
258 if (!orig->flags)
259 mdfile(orig);
260 if (!new->flags)
261 mdfile(new);
262
263 if ((orig->flags & CRAMFS_EFLAG_MD5) &&
264 (new->flags & CRAMFS_EFLAG_MD5) &&
265 !memcmp(orig->md5sum, new->md5sum, UL_MD5LENGTH) &&
266 identical_file(orig, new)) {
267 new->same = orig;
268 *fslen_ub -= new->size;
269 return 1;
270 }
271 }
272 return find_identical_file(orig->child, new, fslen_ub) ||
273 find_identical_file(orig->next, new, fslen_ub);
274 }
275
276 static void eliminate_doubles(struct entry *root, struct entry *orig, loff_t *fslen_ub) {
277 if (orig) {
278 if (orig->size && orig->path)
279 find_identical_file(root,orig, fslen_ub);
280 eliminate_doubles(root,orig->child, fslen_ub);
281 eliminate_doubles(root,orig->next, fslen_ub);
282 }
283 }
284
285 /*
286 * We define our own sorting function instead of using alphasort which
287 * uses strcoll and changes ordering based on locale information.
288 */
289 static int cramsort (const struct dirent **a, const struct dirent **b)
290 {
291 return strcmp((*a)->d_name, (*b)->d_name);
292 }
293
294 static unsigned int parse_directory(struct entry *root_entry, const char *name, struct entry **prev, loff_t *fslen_ub)
295 {
296 struct dirent **dirlist;
297 int totalsize = 0, dircount, dirindex;
298 char *path, *endpath;
299 size_t len = strlen(name);
300
301 /* Set up the path. */
302 /* TODO: Reuse the parent's buffer to save memcpy'ing and duplication. */
303 path = xmalloc(len + 1 + MAX_INPUT_NAMELEN + 1);
304 memcpy(path, name, len);
305 endpath = path + len;
306 *endpath = '/';
307 endpath++;
308
309 /* read in the directory and sort */
310 dircount = scandir(name, &dirlist, NULL, cramsort);
311
312 if (dircount < 0)
313 err(MKFS_EX_ERROR, _("could not read directory %s"), name);
314
315 /* process directory */
316 for (dirindex = 0; dirindex < dircount; dirindex++) {
317 struct dirent *dirent;
318 struct entry *entry;
319 struct stat st;
320 int size;
321 size_t namelen;
322
323 dirent = dirlist[dirindex];
324
325 /* Ignore "." and ".." - we won't be adding them
326 to the archive */
327 if (dirent->d_name[0] == '.') {
328 if (dirent->d_name[1] == '\0')
329 continue;
330 if (dirent->d_name[1] == '.' &&
331 dirent->d_name[2] == '\0')
332 continue;
333 }
334 namelen = strlen(dirent->d_name);
335 if (namelen > MAX_INPUT_NAMELEN) {
336 namelen = MAX_INPUT_NAMELEN;
337 warn_namelen = 1;
338 }
339
340 memcpy(endpath, dirent->d_name, namelen + 1);
341
342 if (lstat(path, &st) < 0) {
343 warn(_("stat of %s failed"), endpath);
344 warn_skip = 1;
345 continue;
346 }
347 entry = xcalloc(1, sizeof(struct entry));
348 entry->name = (unsigned char *)xstrndup(dirent->d_name, namelen);
349 entry->mode = st.st_mode;
350 entry->size = st.st_size;
351 entry->uid = st.st_uid;
352 if (entry->uid >= 1 << CRAMFS_UID_WIDTH)
353 warn_uid = 1;
354 entry->gid = st.st_gid;
355 if (entry->gid >= 1 << CRAMFS_GID_WIDTH)
356 /* TODO: We ought to replace with a default
357 gid instead of truncating; otherwise there
358 are security problems. Maybe mode should
359 be &= ~070. Same goes for uid once Linux
360 supports >16-bit uids. */
361 warn_gid = 1;
362 size = sizeof(struct cramfs_inode) + ((namelen + 3) & ~3);
363 *fslen_ub += size;
364 if (S_ISDIR(st.st_mode)) {
365 entry->size = parse_directory(root_entry, path, &entry->child, fslen_ub);
366 } else if (S_ISREG(st.st_mode)) {
367 entry->path = xstrdup(path);
368 if (entry->size >= (1 << CRAMFS_SIZE_WIDTH)) {
369 warn_size = 1;
370 entry->size = (1 << CRAMFS_SIZE_WIDTH) - 1;
371 }
372 } else if (S_ISLNK(st.st_mode)) {
373 entry->path = xstrdup(path);
374 } else if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
375 /* maybe we should skip sockets */
376 entry->size = 0;
377 } else {
378 entry->size = st.st_rdev;
379 if (entry->size & -(1<<CRAMFS_SIZE_WIDTH))
380 warn_dev = 1;
381 }
382
383 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
384 int blocks = ((entry->size - 1) / blksize + 1);
385
386 /* block pointers & data expansion allowance + data */
387 if (entry->size)
388 *fslen_ub += (4+26)*blocks + entry->size + 3;
389 }
390
391 /* Link it into the list */
392 *prev = entry;
393 prev = &entry->next;
394 totalsize += size;
395 }
396 free(path);
397 free(dirlist); /* allocated by scandir() with malloc() */
398 return totalsize;
399 }
400
401 /* Returns sizeof(struct cramfs_super), which includes the root inode. */
402 static unsigned int write_superblock(struct entry *root, char *base, int size)
403 {
404 struct cramfs_super *super = (struct cramfs_super *) base;
405 unsigned int offset = sizeof(struct cramfs_super) + image_length;
406
407 if (opt_pad) {
408 offset += opt_pad;
409 }
410
411 super->magic = CRAMFS_MAGIC;
412 super->flags = CRAMFS_FLAG_FSID_VERSION_2 | CRAMFS_FLAG_SORTED_DIRS;
413 if (opt_holes)
414 super->flags |= CRAMFS_FLAG_HOLES;
415 if (image_length > 0)
416 super->flags |= CRAMFS_FLAG_SHIFTED_ROOT_OFFSET;
417 super->size = size;
418 memcpy(super->signature, CRAMFS_SIGNATURE, sizeof(super->signature));
419
420 super->fsid.crc = crc32(0L, NULL, 0);
421 super->fsid.edition = opt_edition;
422 super->fsid.blocks = total_blocks;
423 super->fsid.files = total_nodes;
424
425 memset(super->name, 0x00, sizeof(super->name));
426 if (opt_name)
427 str2memcpy((char *)super->name, opt_name, sizeof(super->name));
428 else
429 str2memcpy((char *)super->name, "Compressed", sizeof(super->name));
430
431 super->root.mode = root->mode;
432 super->root.uid = root->uid;
433 super->root.gid = root->gid;
434 super->root.size = root->size;
435 super->root.offset = offset >> 2;
436
437 super_toggle_endianness(cramfs_is_big_endian, super);
438 inode_from_host(cramfs_is_big_endian, &super->root, &super->root);
439
440 return offset;
441 }
442
443 static void set_data_offset(struct entry *entry, char *base, unsigned long offset)
444 {
445 struct cramfs_inode *inode = (struct cramfs_inode *) (base + entry->dir_offset);
446 inode_to_host(cramfs_is_big_endian, inode, inode);
447 if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH)))
448 errx(MKFS_EX_ERROR, _("filesystem too big. Exiting."));
449 inode->offset = (offset >> 2);
450 inode_from_host(cramfs_is_big_endian, inode, inode);
451 }
452
453
454 /*
455 * We do a width-first printout of the directory
456 * entries, using a stack to remember the directories
457 * we've seen.
458 */
459 static unsigned int write_directory_structure(struct entry *entry, char *base, unsigned int offset)
460 {
461 int stack_entries = 0;
462 int stack_size = 64;
463 struct entry **entry_stack;
464
465 entry_stack = xmalloc(stack_size * sizeof(struct entry *));
466
467 for (;;) {
468 int dir_start = stack_entries;
469 while (entry) {
470 struct cramfs_inode *inode =
471 (struct cramfs_inode *) (base + offset);
472 size_t len = strlen((const char *)entry->name);
473
474 entry->dir_offset = offset;
475
476 inode->mode = entry->mode;
477 inode->uid = entry->uid;
478 inode->gid = entry->gid;
479 inode->size = entry->size;
480 inode->offset = 0;
481 /* Non-empty directories, regfiles and symlinks will
482 write over inode->offset later. */
483
484 offset += sizeof(struct cramfs_inode);
485 total_nodes++; /* another node */
486 memcpy(base + offset, entry->name, len);
487 /* Pad up the name to a 4-byte boundary */
488 while (len & 3) {
489 *(base + offset + len) = '\0';
490 len++;
491 }
492 inode->namelen = len >> 2;
493 offset += len;
494
495 if (verbose)
496 printf(" %s\n", entry->name);
497 if (entry->child) {
498 if (stack_entries >= stack_size) {
499 stack_size *= 2;
500 entry_stack = xreallocarray(entry_stack, stack_size, sizeof(struct entry *));
501 }
502 entry_stack[stack_entries] = entry;
503 stack_entries++;
504 }
505 inode_from_host(cramfs_is_big_endian, inode, inode);
506 entry = entry->next;
507 }
508
509 /*
510 * Reverse the order the stack entries pushed during
511 * this directory, for a small optimization of disk
512 * access in the created fs. This change makes things
513 * `ls -UR' order.
514 */
515 {
516 struct entry **lo = entry_stack + dir_start;
517 struct entry **hi = entry_stack + stack_entries;
518 struct entry *tmp;
519
520 while (lo < --hi) {
521 tmp = *lo;
522 *lo++ = *hi;
523 *hi = tmp;
524 }
525 }
526
527 /* Pop a subdirectory entry from the stack, and recurse. */
528 if (!stack_entries)
529 break;
530 stack_entries--;
531 entry = entry_stack[stack_entries];
532
533 set_data_offset(entry, base, offset);
534 if (verbose)
535 printf("'%s':\n", entry->name);
536 entry = entry->child;
537 }
538 free(entry_stack);
539 return offset;
540 }
541
542 static int is_zero(unsigned char const *begin, unsigned len)
543 {
544 if (opt_holes)
545 /* Returns non-zero iff the first LEN bytes from BEGIN are
546 all NULs. */
547 return (len-- == 0 ||
548 (begin[0] == '\0' &&
549 (len-- == 0 ||
550 (begin[1] == '\0' &&
551 (len-- == 0 ||
552 (begin[2] == '\0' &&
553 (len-- == 0 ||
554 (begin[3] == '\0' &&
555 memcmp(begin, begin + 4, len) == 0))))))));
556
557 /* Never create holes. */
558 return 0;
559 }
560
561 /*
562 * One 4-byte pointer per block and then the actual blocked
563 * output. The first block does not need an offset pointer,
564 * as it will start immediately after the pointer block;
565 * so the i'th pointer points to the end of the i'th block
566 * (i.e. the start of the (i+1)'th block or past EOF).
567 *
568 * Note that size > 0, as a zero-sized file wouldn't ever
569 * have gotten here in the first place.
570 */
571 static unsigned int
572 do_compress(char *base, unsigned int offset, unsigned char const *name,
573 char *path, unsigned int size, unsigned int mode)
574 {
575 unsigned long original_size, original_offset, new_size, blocks, curr;
576 long change;
577 char *start;
578 Bytef *p;
579
580 /* get uncompressed data */
581 start = do_mmap(path, size, mode);
582 if (start == NULL)
583 return offset;
584 p = (Bytef *) start;
585
586 original_size = size;
587 original_offset = offset;
588 blocks = (size - 1) / blksize + 1;
589 curr = offset + 4 * blocks;
590
591 total_blocks += blocks;
592
593 do {
594 uLongf len = 2 * blksize;
595 uLongf input = size;
596 if (input > blksize)
597 input = blksize;
598 size -= input;
599 if (!is_zero (p, input)) {
600 compress((Bytef *)(base + curr), &len, p, input);
601 curr += len;
602 }
603 p += input;
604
605 if (len > blksize*2) {
606 /* (I don't think this can happen with zlib.) */
607 printf(_("AIEEE: block \"compressed\" to > "
608 "2*blocklength (%ld)\n"),
609 len);
610 exit(MKFS_EX_ERROR);
611 }
612
613 *(uint32_t *) (base + offset) = u32_toggle_endianness(cramfs_is_big_endian, curr);
614 offset += 4;
615 } while (size);
616
617 do_munmap(start, original_size, mode);
618
619 curr = (curr + 3) & ~3;
620 new_size = curr - original_offset;
621 /* TODO: Arguably, original_size in these 2 lines should be
622 st_blocks * 512. But if you say that, then perhaps
623 administrative data should also be included in both. */
624 change = new_size - original_size;
625 if (verbose)
626 printf(_("%6.2f%% (%+ld bytes)\t%s\n"),
627 (change * 100) / (double) original_size, change, name);
628
629 return curr;
630 }
631
632
633 /*
634 * Traverse the entry tree, writing data for every item that has
635 * non-null entry->path (i.e. every symlink and non-empty
636 * regfile).
637 */
638 static unsigned int
639 write_data(struct entry *entry, char *base, unsigned int offset) {
640 struct entry *e;
641
642 for (e = entry; e; e = e->next) {
643 if (e->path) {
644 if (e->same) {
645 set_data_offset(e, base, e->same->offset);
646 e->offset = e->same->offset;
647 } else if (e->size) {
648 set_data_offset(e, base, offset);
649 e->offset = offset;
650 offset = do_compress(base, offset, e->name,
651 e->path, e->size,e->mode);
652 }
653 } else if (e->child)
654 offset = write_data(e->child, base, offset);
655 }
656 return offset;
657 }
658
659 static unsigned int write_file(char *file, char *base, unsigned int offset)
660 {
661 int fd;
662 char *buf;
663
664 fd = open(file, O_RDONLY);
665 if (fd < 0)
666 err(MKFS_EX_ERROR, _("cannot open %s"), file);
667 buf = mmap(NULL, image_length, PROT_READ, MAP_PRIVATE, fd, 0);
668 memcpy(base + offset, buf, image_length);
669 munmap(buf, image_length);
670 if (close (fd) < 0)
671 err(MKFS_EX_ERROR, _("cannot close file %s"), file);
672 /* Pad up the image_length to a 4-byte boundary */
673 while (image_length & 3) {
674 *(base + offset + image_length) = '\0';
675 image_length++;
676 }
677 return (offset + image_length);
678 }
679
680 /*
681 * Maximum size fs you can create is roughly 256MB. (The last file's
682 * data must begin within 256MB boundary but can extend beyond that.)
683 *
684 * Note that if you want it to fit in a ROM then you're limited to what the
685 * hardware and kernel can support (64MB?).
686 */
687 static unsigned int
688 maxfslen(void) {
689 return (((1 << CRAMFS_OFFSET_WIDTH) - 1) << 2) /* offset */
690 + (1 << CRAMFS_SIZE_WIDTH) - 1 /* filesize */
691 + (1 << CRAMFS_SIZE_WIDTH) * 4 / blksize; /* block pointers */
692 }
693
694 /*
695 * Usage:
696 *
697 * mkcramfs directory-name outfile
698 *
699 * where "directory-name" is simply the root of the directory
700 * tree that we want to generate a compressed filesystem out
701 * of.
702 */
703 int main(int argc, char **argv)
704 {
705 struct stat st; /* used twice... */
706 struct entry *root_entry;
707 char *rom_image;
708 ssize_t offset, written;
709 int fd;
710 /* initial guess (upper-bound) of required filesystem size */
711 loff_t fslen_ub = sizeof(struct cramfs_super);
712 unsigned int fslen_max;
713 char const *dirname, *outfile;
714 char *lockmode = 0;
715 uint32_t crc = crc32(0L, NULL, 0);
716 int c;
717 cramfs_is_big_endian = HOST_IS_BIG_ENDIAN; /* default is to use host order */
718
719 total_blocks = 0;
720
721 setlocale(LC_ALL, "");
722 bindtextdomain(PACKAGE, LOCALEDIR);
723 textdomain(PACKAGE);
724 close_stdout_atexit();
725
726 if (argc > 1) {
727 /* first arg may be one of our standard longopts */
728 if (!strcmp(argv[1], "--help"))
729 usage();
730 if (!strcmp(argv[1], "--version")) {
731 print_version(EXIT_SUCCESS);
732 exit(MKFS_EX_OK);
733 }
734 }
735 strutils_set_exitcode(MKFS_EX_USAGE);
736
737 /* command line options */
738 while ((c = getopt(argc, argv, "hb:Ee:i:n:N:l::psVvz")) != EOF) {
739 switch (c) {
740 case 'h':
741 usage();
742 case 'b':
743 blksize = strtou32_or_err(optarg, _("invalid blocksize argument"));
744 break;
745 case 'E':
746 opt_errors = 1;
747 break;
748 case 'e':
749 opt_edition = strtou32_or_err(optarg, _("invalid edition number argument"));
750 break;
751 case 'N':
752 if (strcmp(optarg, "big") == 0)
753 cramfs_is_big_endian = 1;
754 else if (strcmp(optarg, "little") == 0)
755 cramfs_is_big_endian = 0;
756 else if (strcmp(optarg, "host") == 0)
757 /* default */ ;
758 else
759 errx(MKFS_EX_USAGE, _("invalid endianness given;"
760 " must be 'big', 'little', or 'host'"));
761 break;
762 case 'i':
763 opt_image = optarg;
764 if (lstat(opt_image, &st) < 0)
765 err(MKFS_EX_USAGE, _("stat of %s failed"), opt_image);
766 image_length = st.st_size; /* may be padded later */
767 fslen_ub += (image_length + 3); /* 3 is for padding */
768 break;
769 case 'l':
770 lockmode = "1";
771 if (optarg) {
772 if (*optarg == '=')
773 optarg++;
774 lockmode = optarg;
775 }
776 break;
777 case 'n':
778 opt_name = optarg;
779 break;
780 case 'p':
781 opt_pad = PAD_SIZE;
782 fslen_ub += PAD_SIZE;
783 break;
784 case 's':
785 /* old option, ignored */
786 break;
787 case 'V':
788 print_version(MKFS_EX_OK);
789 case 'v':
790 verbose = 1;
791 break;
792 case 'z':
793 opt_holes = 1;
794 break;
795 default:
796 errtryhelp(MKFS_EX_USAGE);
797 }
798 }
799
800 if ((argc - optind) != 2) {
801 warnx(_("bad usage"));
802 errtryhelp(MKFS_EX_USAGE);
803 }
804 dirname = argv[optind];
805 outfile = argv[optind + 1];
806
807 if (blksize == 0)
808 blksize = getpagesize();
809
810 if (stat(dirname, &st) < 0)
811 err(MKFS_EX_USAGE, _("stat of %s failed"), dirname);
812 fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
813 if (fd < 0)
814 err(MKFS_EX_USAGE, _("cannot open %s"), outfile);
815
816 if (blkdev_lock(fd, outfile, lockmode) != 0)
817 exit(MKFS_EX_ERROR);
818
819 root_entry = xcalloc(1, sizeof(struct entry));
820 root_entry->mode = st.st_mode;
821 root_entry->uid = st.st_uid;
822 root_entry->gid = st.st_gid;
823
824 root_entry->size = parse_directory(root_entry, dirname, &root_entry->child, &fslen_ub);
825
826 /* find duplicate files */
827 eliminate_doubles(root_entry,root_entry, &fslen_ub);
828
829 /* always allocate a multiple of blksize bytes because that's
830 what we're going to write later on */
831 fslen_ub = ((fslen_ub - 1) | (blksize - 1)) + 1;
832 fslen_max = maxfslen();
833
834 if (fslen_ub > fslen_max) {
835 warnx( _("warning: guestimate of required size (upper bound) "
836 "is %lldMB, but maximum image size is %uMB. "
837 "We might die prematurely."),
838 (long long)fslen_ub >> 20,
839 fslen_max >> 20);
840 fslen_ub = fslen_max;
841 }
842
843 /* TODO: Why do we use a private/anonymous mapping here
844 followed by a write below, instead of just a shared mapping
845 and a couple of ftruncate calls? Is it just to save us
846 having to deal with removing the file afterwards? If we
847 really need this huge anonymous mapping, we ought to mmap
848 in smaller chunks, so that the user doesn't need nn MB of
849 RAM free. If the reason is to be able to write to
850 un-mmappable block devices, then we could try shared mmap
851 and revert to anonymous mmap if the shared mmap fails. */
852 rom_image = mmap(NULL,
853 fslen_ub?fslen_ub:1,
854 PROT_READ | PROT_WRITE,
855 MAP_PRIVATE | MAP_ANONYMOUS,
856 -1, 0);
857
858 if (-1 == (int) (long) rom_image)
859 err(MKFS_EX_ERROR, _("ROM image map"));
860
861 /* Skip the first opt_pad bytes for boot loader code */
862 offset = opt_pad;
863 memset(rom_image, 0x00, opt_pad);
864
865 /* Skip the superblock and come back to write it later. */
866 offset += sizeof(struct cramfs_super);
867
868 /* Insert a file image. */
869 if (opt_image) {
870 if (verbose)
871 printf(_("Including: %s\n"), opt_image);
872 offset = write_file(opt_image, rom_image, offset);
873 }
874
875 offset = write_directory_structure(root_entry->child, rom_image, offset);
876 if (verbose)
877 printf(_("Directory data: %zd bytes\n"), offset);
878
879 offset = write_data(root_entry, rom_image, offset);
880
881 /* We always write a multiple of blksize bytes, so that
882 losetup works. */
883 offset = ((offset - 1) | (blksize - 1)) + 1;
884 if (verbose)
885 printf(_("Everything: %zd kilobytes\n"), offset >> 10);
886
887 /* Write the superblock now that we can fill in all of the fields. */
888 write_superblock(root_entry, rom_image+opt_pad, offset);
889 if (verbose)
890 printf(_("Super block: %zd bytes\n"),
891 sizeof(struct cramfs_super));
892
893 /* Put the checksum in. */
894 crc = crc32(crc, (unsigned char *) (rom_image+opt_pad), (offset-opt_pad));
895 ((struct cramfs_super *) (rom_image+opt_pad))->fsid.crc = u32_toggle_endianness(cramfs_is_big_endian, crc);
896 if (verbose)
897 printf(_("CRC: %x\n"), crc);
898
899 /* Check to make sure we allocated enough space. */
900 if (fslen_ub < offset)
901 errx(MKFS_EX_ERROR,
902 _("not enough space allocated for ROM image "
903 "(%lld allocated, %zu used)"),
904 (long long) fslen_ub, offset);
905
906 written = write(fd, rom_image, offset);
907 if (offset != written)
908 errx(MKFS_EX_ERROR, _("ROM image write failed (%zd %zd)"),
909 written, offset);
910 if (close_fd(fd) != 0)
911 err(MKFS_EX_ERROR, _("ROM image"));
912
913 /*
914 * (These warnings used to come at the start, but they scroll off
915 * the screen too quickly.)
916 */
917 if (warn_namelen)
918 /* Can't happen when reading from ext2fs. */
919 /* Bytes, not chars: think UTF8. */
920 warnx(_("warning: filenames truncated to %u bytes."), MAX_INPUT_NAMELEN);
921 if (warn_skip)
922 warnx(_("warning: files were skipped due to errors."));
923 if (warn_size)
924 warnx(_("warning: file sizes truncated to %luMB "
925 "(minus 1 byte)."), 1L << (CRAMFS_SIZE_WIDTH - 20));
926 if (warn_uid)
927 /* (not possible with current Linux versions) */
928 warnx(_("warning: uids truncated to %u bits. "
929 "(This may be a security concern.)"), CRAMFS_UID_WIDTH);
930 if (warn_gid)
931 warnx(_("warning: gids truncated to %u bits. "
932 "(This may be a security concern.)"), CRAMFS_GID_WIDTH);
933 if (warn_dev)
934 warnx(_("WARNING: device numbers truncated to %u bits. "
935 "This almost certainly means\n"
936 "that some device files will be wrong."),
937 CRAMFS_OFFSET_WIDTH);
938 if (opt_errors &&
939 (warn_namelen|warn_skip|warn_size|warn_uid|warn_gid|warn_dev))
940 exit(MKFS_EX_ERROR);
941
942 return MKFS_EX_OK;
943 }