X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=mkfs_minix.c;h=240dfded0349e482f32c380924bc272e187acecb;hb=9c5fcc3408626c46bc5187554e950b981143bb38;hp=4435cb64a82f9a960947ef44bb628923b322f6da;hpb=e49d5ecbbe51718fa925b6890a735e5937cc2aa2;p=oweals%2Fbusybox.git diff --git a/mkfs_minix.c b/mkfs_minix.c index 4435cb64a..240dfded0 100644 --- a/mkfs_minix.c +++ b/mkfs_minix.c @@ -57,6 +57,9 @@ * * The device may be a block device or a image of one, but this isn't * enforced (but it's not much fun on a character device :-). + * + * Modified for BusyBox by Erik Andersen -- + * removed getopt based parser and added a hand rolled one. */ #include "internal.h" @@ -71,20 +74,107 @@ #include #include #include +#include #include -#include -#include -#include -#ifdef MINIX2_SUPER_MAGIC2 -#define HAVE_MINIX2 1 -#endif +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; + + +#define MINIX_ROOT_INO 1 +#define MINIX_LINK_MAX 250 +#define MINIX2_LINK_MAX 65530 + +#define MINIX_I_MAP_SLOTS 8 +#define MINIX_Z_MAP_SLOTS 64 +#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */ +#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */ +#define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */ +#define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */ +#define MINIX_VALID_FS 0x0001 /* Clean fs. */ +#define MINIX_ERROR_FS 0x0002 /* fs has errors. */ + +#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode))) +#define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode))) + +#define MINIX_V1 0x0001 /* original minix fs */ +#define MINIX_V2 0x0002 /* minix V2 fs */ + +#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version + +/* + * This is the original minix inode layout on disk. + * Note the 8-bit gid and atime and ctime. + */ +struct minix_inode { + u16 i_mode; + u16 i_uid; + u32 i_size; + u32 i_time; + u8 i_gid; + u8 i_nlinks; + u16 i_zone[9]; +}; + +/* + * The new minix inode has all the time entries, as well as + * long block numbers and a third indirect block (7+1+1+1 + * instead of 7+1+1). Also, some previously 8-bit values are + * now 16-bit. The inode is now 64 bytes instead of 32. + */ +struct minix2_inode { + u16 i_mode; + u16 i_nlinks; + u16 i_uid; + u16 i_gid; + u32 i_size; + u32 i_atime; + u32 i_mtime; + u32 i_ctime; + u32 i_zone[10]; +}; -#ifndef __GNUC__ -#error "needs gcc for the bitop-__asm__'s" +/* + * minix super-block data on disk + */ +struct minix_super_block { + u16 s_ninodes; + u16 s_nzones; + u16 s_imap_blocks; + u16 s_zmap_blocks; + u16 s_firstdatazone; + u16 s_log_zone_size; + u32 s_max_size; + u16 s_magic; + u16 s_state; + u32 s_zones; +}; + +struct minix_dir_entry { + u16 inode; + char name[0]; +}; + +#define BLOCK_SIZE_BITS 10 +#define BLOCK_SIZE (1<i_zone[0], root_block); } -#ifdef HAVE_MINIX2 +#ifdef BB_FEATURE_MINIX2 static void make_root_inode2(void) { struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; @@ -498,7 +573,7 @@ static void setup_tables(void) else inodes = req_nr_inodes; /* Round up inode count to fill block size */ -#ifdef HAVE_MINIX2 +#ifdef BB_FEATURE_MINIX2 if (version2) inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & ~(MINIX2_INODES_PER_BLOCK - 1)); @@ -530,19 +605,15 @@ static void setup_tables(void) die("unable to allocate buffers for maps"); } FIRSTZONE = NORM_FIRSTZONE; - inode_map = malloc(IMAPS * BLOCK_SIZE); - zone_map = malloc(ZMAPS * BLOCK_SIZE); - if (!inode_map || !zone_map) - die("unable to allocate buffers for maps"); + inode_map = xmalloc(IMAPS * BLOCK_SIZE); + zone_map = xmalloc(ZMAPS * BLOCK_SIZE); memset(inode_map, 0xff, IMAPS * BLOCK_SIZE); memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE); for (i = FIRSTZONE; i < ZONES; i++) unmark_zone(i); for (i = MINIX_ROOT_INO; i <= INODES; i++) unmark_inode(i); - inode_buffer = malloc(INODE_BUFFER_SIZE); - if (!inode_buffer) - die("unable to allocate buffer for inodes"); + inode_buffer = xmalloc(INODE_BUFFER_SIZE); memset(inode_buffer, 0, INODE_BUFFER_SIZE); printf("%ld inodes\n", INODES); printf("%ld blocks\n", ZONES); @@ -646,77 +717,112 @@ char *filename; extern int mkfs_minix_main(int argc, char **argv) { - int i; + int i=1; char *tmp; struct stat statbuf; char *listfile = NULL; + int stopIt=FALSE; - if (argc && *argv) - program_name = *argv; if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) die("bad inode size"); -#ifdef HAVE_MINIX2 +#ifdef BB_FEATURE_MINIX2 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) die("bad inode size"); #endif - opterr = 0; - while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF) - switch (i) { - case 'c': - check = 1; - break; - case 'i': - req_nr_inodes = (unsigned long) atol(optarg); - break; - case 'l': - listfile = optarg; - break; - case 'n': - i = strtoul(optarg, &tmp, 0); - if (*tmp) - show_usage(); - if (i == 14) - magic = MINIX_SUPER_MAGIC; - else if (i == 30) - magic = MINIX_SUPER_MAGIC2; - else - show_usage(); - namelen = i; - dirsize = i + 2; - break; - case 'v': -#ifdef HAVE_MINIX2 - version2 = 1; + + /* Parse options */ + argv++; + while (--argc >= 0 && *argv && **argv) { + if (**argv == '-') { + stopIt=FALSE; + while (i > 0 && *++(*argv) && stopIt==FALSE) { + switch (**argv) { + case 'c': + check = 1; + break; + case 'i': + { + char *cp=NULL; + if (*(*argv+1) != 0) { + cp = ++(*argv); + } else { + if (--argc == 0) { + goto goodbye; + } + cp = *(++argv); + } + req_nr_inodes = strtoul(cp, &tmp, 0); + if (*tmp) + show_usage(); + stopIt=TRUE; + break; + } + case 'l': + if (--argc == 0) { + goto goodbye; + } + listfile = *(++argv); + break; + case 'n': + { + char *cp=NULL; + + if (*(*argv+1) != 0) { + cp = ++(*argv); + } else { + if (--argc == 0) { + goto goodbye; + } + cp = *(++argv); + } + i = strtoul(cp, &tmp, 0); + if (*tmp) + show_usage(); + if (i == 14) + magic = MINIX_SUPER_MAGIC; + else if (i == 30) + magic = MINIX_SUPER_MAGIC2; + else + show_usage(); + namelen = i; + dirsize = i + 2; + stopIt=TRUE; + break; + } + case 'v': +#ifdef BB_FEATURE_MINIX2 + version2 = 1; #else - fprintf(stderr, "%s: not compiled with minix v2 support\n", - program_name, device_name); - exit(-1); + errorMsg("%s: not compiled with minix v2 support\n", + device_name); + exit(-1); #endif - break; - default: - show_usage(); + break; + case '-': + case 'h': + default: +goodbye: + show_usage(); + } + } + } else { + if (device_name == NULL) + device_name = *argv; + else if (BLOCKS == 0) + BLOCKS = strtol(*argv, &tmp, 0); + else { + goto goodbye; + } } - argc -= optind; - argv += optind; - if (argc > 0 && !device_name) { - device_name = argv[0]; - argc--; argv++; } - if (argc > 0) { - BLOCKS = strtol(argv[0], &tmp, 0); - if (*tmp) { - printf("strtol error: number of blocks not specified"); - show_usage(); - } - } if (device_name && !BLOCKS) BLOCKS = get_size(device_name) / 1024; if (!device_name || BLOCKS < 10) { show_usage(); } -#ifdef HAVE_MINIX2 +#ifdef BB_FEATURE_MINIX2 if (version2) { if (namelen == 14) magic = MINIX2_SUPER_MAGIC; @@ -750,7 +856,7 @@ extern int mkfs_minix_main(int argc, char **argv) check_blocks(); else if (listfile) get_list_blocks(listfile); -#ifdef HAVE_MINIX2 +#ifdef BB_FEATURE_MINIX2 if (version2) { make_root_inode2(); make_bad_inode2(); @@ -762,5 +868,6 @@ extern int mkfs_minix_main(int argc, char **argv) } mark_good_blocks(); write_tables(); - return 0; + return( 0); + }