int uncramfs(char const *dirname, char const *imagefile) { struct stat st; int fd; size_t fslen_ub; u8 const *rom_image; struct cramfs_super const *sb; // Check the directory if (access(dirname, W_OK) == -1) { if (errno != ENOENT) { perror(dirname); exit(1); } } // Check the image file if (stat(imagefile, &st) == -1) { perror(imagefile); exit(1); } // Map the cramfs image fd = open(imagefile, O_RDONLY); fslen_ub = st.st_size; rom_image = mmap(0, fslen_ub, PROT_READ, MAP_SHARED, fd, 0); if (rom_image == MAP_FAILED) { perror("Mapping cramfs file"); exit(1); } sb = (struct cramfs_super const *)(rom_image); // Check cramfs magic number and signature if (CRAMFS_MAGIC != sb->magic || 0 != memcmp(sb->signature, CRAMFS_SIGNATURE, sizeof(sb->signature))) { fprintf(stderr, "The image file doesn't have cramfs signatures\n"); exit(1); } // Set umask to 0 to let the image modes shine through umask(0); clearstats(); // Start doing... do_file_entry(rom_image, dirname, "", "", 0, &sb->root); do_dir_entry(rom_image, dirname, "", "", 0, &sb->root); return 0; }
/* * Usage: * * uncramfs directory-name outfile * * where "directory-name" is simply the root of the directory * tree that we want to expand the compressed filesystem into. */ int main(int argc, char **argv) { char const* dirname; char const* imagefile; struct stat st; int fd; size_t fslen_ub; u8 const* rom_image; struct cramfs_super const* sb; int i; // Check the program usage if (argc) progname = argv[0]; if (argc != 3 && argc != 5 && argc != 7) usage(); if (argc == 3) { dirname=argv[1]; imagefile=argv[2]; } else if (argc == 5) { if (strcmp(argv[1],"-d")==0) { opt_devfile = argv[2]; } else if (strcmp(argv[1],"-m")==0) { opt_idsfile = argv[2]; } else usage(); dirname=argv[3]; imagefile=argv[4]; } else { if (strcmp(argv[1],"-d")==0) { opt_devfile = argv[2]; } else if (strcmp(argv[1],"-m")==0) { opt_idsfile = argv[2]; } else usage(); if (strcmp(argv[3],"-d")==0 && !opt_devfile) { opt_devfile = argv[4]; } else if (strcmp(argv[3],"-m")==0 && !opt_idsfile) { opt_idsfile = argv[4]; } else usage(); dirname=argv[5]; imagefile=argv[6]; } // Check the directory if (access(dirname, W_OK) == -1) { if (errno != ENOENT ) { perror(dirname); exit(1); } } // Check the image file if (stat(imagefile, &st) == -1) { perror(imagefile); exit(1); } // Map the cramfs image fd = open(imagefile, O_RDONLY); fslen_ub = st.st_size; rom_image = mmap(0, fslen_ub, PROT_READ, MAP_SHARED, fd, 0); if (rom_image == MAP_FAILED) { perror("Mapping cramfs file"); exit(1); } sb=(struct cramfs_super const*)(rom_image); // Check cramfs magic number and signature if (CRAMFS_MAGIC != sb->magic || 0 != memcmp(sb->signature, CRAMFS_SIGNATURE, sizeof(sb->signature))) { fprintf(stderr,"The image file doesn't have cramfs signatures\n"); exit(1); } // Set umask to 0 to let the image modes shine through umask(0); // Print some reassuring blurb printf("[Volume size: 0x%x]\n", fslen_ub); printf("[Volume serial: "); for (i=0; i< sizeof(sb->fsid); ++i) printf("%02x", sb->fsid[i]); printf("]\n"); printf("[Volume name: %s]\n", sb->name); printf("\n"); clearstats(); // Start doing... printf("do file entry \n"); do_file_entry(rom_image, dirname, "", "", 0, &sb->root); printf("do dir entry\n"); do_dir_entry(rom_image, dirname, "", "", 0, &sb->root); //process_directory(rom_image, dirname, sb->root.offset<<2, sb->root.size, "."); printstats(); return 0; }