⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mkdosfs.c

📁 Android 一些工具
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (opt_b < bpb.bps)	    fprintf(stderr, "block size (%u) is too small; minimum is %u\n",		 opt_b, bpb.bps);	if (opt_b > bpb.bps * MAXSPC)	    fprintf(stderr, "block size (%u) is too large; maximum is %u\n",		 opt_b, bpb.bps * MAXSPC);	bpb.spc = opt_b / bpb.bps;    }    if (opt_c) {	if (!powerof2(opt_c))	    fprintf(stderr, "sectors/cluster (%u) is not a power of 2\n", opt_c);	bpb.spc = opt_c;    }    if (opt_r)	bpb.res = opt_r;    if (opt_n) {	if (opt_n > MAXNFT)	    fprintf(stderr, "number of FATs (%u) is too large; maximum is %u\n",		 opt_n, MAXNFT);	bpb.nft = opt_n;    }    if (opt_e)	bpb.rde = opt_e;    if (mflag) {	if (opt_m < 0xf0)	    fprintf(stderr, "illegal media descriptor (%#x)\n", opt_m);	bpb.mid = opt_m;    }    if (opt_a)	bpb.bspf = opt_a;    if (opt_i)	bpb.infs = opt_i;    if (opt_k)	bpb.bkbs = opt_k;    bss = 1;    bname = NULL;    fd1 = -1;    if (opt_B) {	bname = opt_B;	if (!strchr(bname, '/')) {	    snprintf(buf, sizeof(buf), "/boot/%s", bname);	    if (!(bname = strdup(buf)))		fprintf(stderr, NULL);	}	if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))	    fprintf(stderr, "%s", bname);	if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||	    sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)	    fprintf(stderr, "%s: inappropriate file type or format\n", bname);	bss = sb.st_size / bpb.bps;    }    if (!bpb.nft)	bpb.nft = 2;    if (!fat) {	if (bpb.bsec < (bpb.res ? bpb.res : bss) +	    howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *		    ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *	    bpb.nft +	    howmany(bpb.rde ? bpb.rde : DEFRDE,		    bpb.bps / DE_SIZE) +	    (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *	    (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))	    fat = 12;	else if (bpb.rde || bpb.bsec <		 (bpb.res ? bpb.res : bss) +		 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +		 howmany(DEFRDE, bpb.bps / DE_SIZE) +		 (MAXCLS16 + 1) *		 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))	    fat = 16;	else	    fat = 32;    }    x = bss;    if (fat == 32) {	if (!bpb.infs) {	    if (x == MAXU16 || x == bpb.bkbs)		fprintf(stderr, "no room for info sector\n");	    bpb.infs = x;	}	if (bpb.infs != MAXU16 && x <= bpb.infs)	    x = bpb.infs + 1;	if (!bpb.bkbs) {	    if (x == MAXU16)		fprintf(stderr, "no room for backup sector\n");	    bpb.bkbs = x;	} else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)	    fprintf(stderr, "backup sector would overwrite info sector\n");	if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)	    x = bpb.bkbs + 1;    }    if (!bpb.res)	bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;    else if (bpb.res < x)	fprintf(stderr, "too few reserved sectors (need %d have %d)\n", x, bpb.res);    if (fat != 32 && !bpb.rde)	bpb.rde = DEFRDE;    rds = howmany(bpb.rde, bpb.bps / DE_SIZE);    if (!bpb.spc)	for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);	     bpb.spc < MAXSPC &&	     bpb.res +	     howmany((RESFTE + maxcls(fat)) * (fat / BPN),		     bpb.bps * NPB) * bpb.nft +	     rds +	     (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;	     bpb.spc <<= 1);    if (fat != 32 && bpb.bspf > MAXU16)	fprintf(stderr, "too many sectors/FAT for FAT12/16\n");    x1 = bpb.res + rds;    x = bpb.bspf ? bpb.bspf : 1;    if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)	fprintf(stderr, "meta data exceeds file system size\n");    x1 += x * bpb.nft;    x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /	(bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);    x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),		 bpb.bps * NPB);    if (!bpb.bspf) {	bpb.bspf = x2;	x1 += (bpb.bspf - 1) * bpb.nft;    }    cls = (bpb.bsec - x1) / bpb.spc;    x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;    if (cls > x)	cls = x;    if (bpb.bspf < x2)	fprintf(stderr, "warning: sectors/FAT limits file system to %u clusters\n",	      cls);    if (cls < mincls(fat))	fprintf(stderr, "%u clusters too few clusters for FAT%u, need %u\n", cls, fat,	    mincls(fat));    if (cls > maxcls(fat)) {	cls = maxcls(fat);	bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;	fprintf(stderr, "warning: FAT type limits file system to %u sectors\n",	      bpb.bsec);    }    printf("%s: %u sector%s in %u FAT%u cluster%s "	   "(%u bytes/cluster)\n", fname, cls * bpb.spc,	   cls * bpb.spc == 1 ? "" : "s", cls, fat,	   cls == 1 ? "" : "s", bpb.bps * bpb.spc);    if (!bpb.mid)	bpb.mid = !bpb.hid ? 0xf0 : 0xf8;    if (fat == 32)	bpb.rdcl = RESFTE;    if (bpb.hid + bpb.bsec <= MAXU16) {	bpb.sec = bpb.bsec;	bpb.bsec = 0;    }    if (fat != 32) {	bpb.spf = bpb.bspf;	bpb.bspf = 0;    }    ch = 0;    if (fat == 12)	ch = 1;			/* 001 Primary DOS with 12 bit FAT */    else if (fat == 16) {	if (bpb.bsec == 0)	    ch = 4;		/* 004 Primary DOS with 16 bit FAT <32M */	else	    ch = 6;		/* 006 Primary 'big' DOS, 16-bit FAT (> 32MB) */				/*				 * XXX: what about:				 * 014 DOS (16-bit FAT) - LBA				 *  ?				 */    } else if (fat == 32) {	ch = 11;		/* 011 Primary DOS with 32 bit FAT */				/*				 * XXX: what about:				 * 012 Primary DOS with 32 bit FAT - LBA				 *  ?				 */    }    if (ch != 0)	printf("MBR type: %d\n", ch);    print_bpb(&bpb);    if (!opt_N) {	gettimeofday(&tv, NULL);	now = tv.tv_sec;	tm = localtime(&now);	if (!(img = malloc(bpb.bps)))	    fprintf(stderr, NULL);	dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;	for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {	    x = lsn;	    if (opt_B &&		fat == 32 && bpb.bkbs != MAXU16 &&		bss <= bpb.bkbs && x >= bpb.bkbs) {		x -= bpb.bkbs;		if (!x && lseek(fd1, 0, SEEK_SET))		    fprintf(stderr, "lseek failed for %s\n", bname);	    }	    if (opt_B && x < bss) {		if ((n = read(fd1, img, bpb.bps)) == -1)		    fprintf(stderr, "%s\n", bname);		if (n != bpb.bps)		    fprintf(stderr, "%s: can't read sector %u\n", bname, x);	    } else		memset(img, 0, bpb.bps);	    if (!lsn ||	      (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {		x1 = BS_SIZE;		bsbpb = (struct bsbpb *)(img + x1);		mk2(bsbpb->bps, bpb.bps);		mk1(bsbpb->spc, bpb.spc);		mk2(bsbpb->res, bpb.res);		mk1(bsbpb->nft, bpb.nft);		mk2(bsbpb->rde, bpb.rde);		mk2(bsbpb->sec, bpb.sec);		mk1(bsbpb->mid, bpb.mid);		mk2(bsbpb->spf, bpb.spf);		mk2(bsbpb->spt, bpb.spt);		mk2(bsbpb->hds, bpb.hds);		mk4(bsbpb->hid, bpb.hid);		mk4(bsbpb->bsec, bpb.bsec);		x1 += BSBPB_SIZE;		if (fat == 32) {		    bsxbpb = (struct bsxbpb *)(img + x1);		    mk4(bsxbpb->bspf, bpb.bspf);		    mk2(bsxbpb->xflg, 0);		    mk2(bsxbpb->vers, 0);		    mk4(bsxbpb->rdcl, bpb.rdcl);		    mk2(bsxbpb->infs, bpb.infs);		    mk2(bsxbpb->bkbs, bpb.bkbs);		    x1 += BSXBPB_SIZE;		}		bsx = (struct bsx *)(img + x1);		mk1(bsx->sig, 0x29);		if (Iflag)		    x = opt_I;		else		    x = (((u_int)(1 + tm->tm_mon) << 8 |			  (u_int)tm->tm_mday) +			 ((u_int)tm->tm_sec << 8 |			  (u_int)(tv.tv_usec / 10))) << 16 |			((u_int)(1900 + tm->tm_year) +			 ((u_int)tm->tm_hour << 8 |			  (u_int)tm->tm_min));		mk4(bsx->volid, x);		mklabel(bsx->label, opt_L ? opt_L : "NO_NAME");		snprintf(buf, sizeof(buf), "FAT%u", fat);		setstr(bsx->type, buf, sizeof(bsx->type));		if (!opt_B) {		    x1 += BSX_SIZE;		    bs = (struct bs *)img;		    mk1(bs->jmp[0], 0xeb);		    mk1(bs->jmp[1], x1 - 2);		    mk1(bs->jmp[2], 0x90);		    setstr(bs->oem, opt_O ? opt_O : "NetBSD",			   sizeof(bs->oem));		    memcpy(img + x1, bootcode, sizeof(bootcode));		    mk2(img + bpb.bps - 2, DOSMAGIC);		}	    } else if (fat == 32 && bpb.infs != MAXU16 &&		       (lsn == bpb.infs ||			(bpb.bkbs != MAXU16 &&			 lsn == bpb.bkbs + bpb.infs))) {		mk4(img, 0x41615252);		mk4(img + bpb.bps - 28, 0x61417272);		mk4(img + bpb.bps - 24, 0xffffffff);		mk4(img + bpb.bps - 20, bpb.rdcl);		mk2(img + bpb.bps - 2, DOSMAGIC);	    } else if (lsn >= bpb.res && lsn < dir &&		       !((lsn - bpb.res) %			 (bpb.spf ? bpb.spf : bpb.bspf))) {		mk1(img[0], bpb.mid);		for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)		    mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);	    } else if (lsn == dir && opt_L) {		de = (struct de *)img;		mklabel(de->namext, opt_L);		mk1(de->attr, 050);		x = (u_int)tm->tm_hour << 11 |		    (u_int)tm->tm_min << 5 |		    (u_int)tm->tm_sec >> 1;		mk2(de->time, x);		x = (u_int)(tm->tm_year - 80) << 9 |		    (u_int)(tm->tm_mon + 1) << 5 |		    (u_int)tm->tm_mday;		mk2(de->date, x);	    }	    if ((n = write(fd, img, bpb.bps)) == -1)		fprintf(stderr, "%s\n", fname);	    if (n != bpb.bps)		fprintf(stderr, "%s: can't write sector %u\n", fname, lsn);	}    }    return 0;}/* * Print out BPB values. */static voidprint_bpb(struct bpb *bpb){    printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,	   bpb->nft);    if (bpb->rde)	printf(" rde=%u", bpb->rde);    if (bpb->sec)	printf(" sec=%u", bpb->sec);    printf(" mid=%#x", bpb->mid);    if (bpb->spf)	printf(" spf=%u", bpb->spf);    printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);    if (bpb->bsec)	printf(" bsec=%u", bpb->bsec);    if (!bpb->spf) {	printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);	printf(" infs=");	printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);	printf(" bkbs=");	printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);    }    printf("\n");}/* * Check a disk geometry value. */static u_intckgeom(const char *fname, u_int val, const char *msg){    if (!val)	fprintf(stderr, "%s: no default %s\n", fname, msg);    if (val > MAXU16)	fprintf(stderr, "%s: illegal %s\n", fname, msg);    return val;}/* * Convert and check a numeric option argument. */static u_intargtou(const char *arg, u_int lo, u_int hi, const char *msg){    char *s;    u_long x;    errno = 0;    x = strtoul(arg, &s, 0);    if (errno || !*arg || *s || x < lo || x > hi)	fprintf(stderr, "%s: bad %s\n", arg, msg);    return x;}/* * Check a volume label. */static intoklabel(const char *src){    int c, i;    for (i = 0; i <= 11; i++) {	c = (u_char)*src++;	if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))	    break;    }    return i && !c;}/* * Make a volume label. */static voidmklabel(u_int8_t *dest, const char *src){    int c, i;    for (i = 0; i < 11; i++) {	c = *src ? toupper((unsigned char)*src++) : ' ';	*dest++ = !i && c == '\xe5' ? 5 : c;    }}/* * Copy string, padding with spaces. */static voidsetstr(u_int8_t *dest, const char *src, size_t len){    while (len--)	*dest++ = *src ? *src++ : ' ';}/* * Print usage message. */static voidusage(char* progname){    fprintf(stderr,	    "usage: %s [ -options ] special [disktype]\n", progname);    fprintf(stderr, "where the options are:\n");    fprintf(stderr, "\t-N don't create file system: "	    "just print out parameters\n");    fprintf(stderr, "\t-B get bootstrap from file\n");    fprintf(stderr, "\t-F FAT type (12, 16, or 32)\n");    fprintf(stderr, "\t-I volume ID\n");    fprintf(stderr, "\t-L volume label\n");    fprintf(stderr, "\t-O OEM string\n");    fprintf(stderr, "\t-S bytes/sector\n");    fprintf(stderr, "\t-a sectors/FAT\n");    fprintf(stderr, "\t-b block size\n");    fprintf(stderr, "\t-c sectors/cluster\n");    fprintf(stderr, "\t-e root directory entries\n");    fprintf(stderr, "\t-h drive heads\n");    fprintf(stderr, "\t-i file system info sector\n");    fprintf(stderr, "\t-k backup boot sector\n");    fprintf(stderr, "\t-m media descriptor\n");    fprintf(stderr, "\t-n number of FATs\n");    fprintf(stderr, "\t-o hidden sectors\n");    fprintf(stderr, "\t-r reserved sectors\n");    fprintf(stderr, "\t-s file system size (sectors)\n");    fprintf(stderr, "\t-u sectors/track\n");    exit(1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -