2015-04-23 16:27:14 +03:00
|
|
|
/* $NetBSD: newfs_msdos.c,v 1.43 2015/04/23 13:27:14 abs Exp $ */
|
1997-03-01 22:42:54 +03:00
|
|
|
|
|
|
|
/*
|
2001-09-10 22:27:41 +04:00
|
|
|
* Copyright (c) 1998 Robert Nordier
|
1997-03-01 22:42:54 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
2001-09-10 22:27:41 +04:00
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
1997-03-01 22:42:54 +03:00
|
|
|
*
|
2001-09-10 22:27:41 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
|
|
|
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
|
|
|
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1997-03-01 22:42:54 +03:00
|
|
|
*/
|
1997-09-15 10:31:54 +04:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
1997-03-01 22:42:54 +03:00
|
|
|
#ifndef lint
|
2001-09-10 22:27:41 +04:00
|
|
|
#if 0
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.15 2000/10/10 01:49:37 wollman Exp $";
|
|
|
|
#else
|
2015-04-23 16:27:14 +03:00
|
|
|
__RCSID("$NetBSD: newfs_msdos.c,v 1.43 2015/04/23 13:27:14 abs Exp $");
|
2001-09-10 22:27:41 +04:00
|
|
|
#endif
|
1997-03-01 22:42:54 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <stdio.h>
|
2001-09-10 22:27:41 +04:00
|
|
|
#include <string.h>
|
2013-01-22 00:28:38 +04:00
|
|
|
#include <err.h>
|
|
|
|
#include <stdlib.h>
|
1997-03-01 22:42:54 +03:00
|
|
|
#include <unistd.h>
|
2013-01-22 00:28:38 +04:00
|
|
|
#include <paths.h>
|
|
|
|
#include <errno.h>
|
2001-09-10 22:27:41 +04:00
|
|
|
|
2013-01-22 00:28:38 +04:00
|
|
|
#include "mkfs_msdos.h"
|
2001-09-10 22:27:41 +04:00
|
|
|
|
|
|
|
#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
|
|
|
|
#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
|
|
|
|
#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
|
|
|
|
#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
|
|
|
|
|
2013-01-22 00:28:38 +04:00
|
|
|
__dead static void usage(void);
|
2001-09-10 22:27:41 +04:00
|
|
|
static u_int argtou(const char *, u_int, u_int, const char *);
|
2009-03-26 11:39:24 +03:00
|
|
|
static off_t argtooff(const char *, const char *);
|
1997-03-01 22:42:54 +03:00
|
|
|
|
|
|
|
/*
|
2001-09-10 22:27:41 +04:00
|
|
|
* Construct a FAT12, FAT16, or FAT32 file system.
|
1997-03-01 22:42:54 +03:00
|
|
|
*/
|
2001-09-10 22:27:41 +04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1997-03-01 22:42:54 +03:00
|
|
|
{
|
2012-07-04 22:35:58 +04:00
|
|
|
static const char opts[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
|
2013-01-22 00:28:38 +04:00
|
|
|
struct msdos_options o;
|
|
|
|
char *fname, *dtype;
|
2001-09-10 22:27:41 +04:00
|
|
|
char buf[MAXPATHLEN];
|
2013-01-22 00:28:38 +04:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
memset(&o, 0, sizeof(o));
|
2001-09-10 22:27:41 +04:00
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, opts)) != -1)
|
|
|
|
switch (ch) {
|
2009-03-26 11:39:24 +03:00
|
|
|
case '@':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.offset = argtooff(optarg, "offset");
|
2009-03-26 11:39:24 +03:00
|
|
|
break;
|
2001-09-10 22:27:41 +04:00
|
|
|
case 'N':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.no_create = 1;
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'B':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.bootstrap = optarg;
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
2009-03-26 11:39:24 +03:00
|
|
|
case 'C':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.create_size = argtooff(optarg, "create size");
|
2009-03-26 11:39:24 +03:00
|
|
|
break;
|
2001-09-10 22:27:41 +04:00
|
|
|
case 'F':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.fat_type = atoi(optarg);
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'I':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.volume_id = argto4(optarg, 0, "volume ID");
|
|
|
|
o.volume_id_set = 1;
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'L':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.volume_label = optarg;
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'O':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.OEM_string = optarg;
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'S':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'a':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'b':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.block_size = argtox(optarg, 1, "block size");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'c':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.sectors_per_cluster = argto1(optarg, 1, "sectors/cluster");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'e':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.directory_entries = argto2(optarg, 1, "directory entries");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'f':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.floppy = optarg;
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'h':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.drive_heads = argto2(optarg, 1, "drive heads");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'i':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.info_sector = argto2(optarg, 1, "info sector");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'k':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.backup_sector = argto2(optarg, 1, "backup sector");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'm':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.media_descriptor = argto1(optarg, 0, "media descriptor");
|
|
|
|
o.media_descriptor_set = 1;
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'n':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.num_FAT = argto1(optarg, 1, "number of FATs");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'o':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.hidden_sectors = argto4(optarg, 0, "hidden sectors");
|
|
|
|
o.hidden_sectors_set = 1;
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'r':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.reserved_sectors = argto2(optarg, 1, "reserved sectors");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 's':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.size = argto4(optarg, 1, "file system size");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
case 'u':
|
2013-01-22 00:28:38 +04:00
|
|
|
o.sectors_per_track = argto2(optarg, 1, "sectors/track");
|
2001-09-10 22:27:41 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc < 1 || argc > 2)
|
|
|
|
usage();
|
|
|
|
fname = *argv++;
|
2013-01-22 00:28:38 +04:00
|
|
|
if (!strchr(fname, '/') && !o.create_size) {
|
2001-09-10 23:25:35 +04:00
|
|
|
snprintf(buf, sizeof(buf), "%sr%s", _PATH_DEV, fname);
|
2001-09-10 22:27:41 +04:00
|
|
|
if (!(fname = strdup(buf)))
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
dtype = *argv;
|
2013-01-22 00:28:38 +04:00
|
|
|
return mkfs_msdos(fname, dtype, &o);
|
2001-09-10 22:27:41 +04:00
|
|
|
}
|
1997-03-01 22:42:54 +03:00
|
|
|
|
2001-09-10 22:27:41 +04:00
|
|
|
/*
|
|
|
|
* Convert and check a numeric option argument.
|
|
|
|
*/
|
|
|
|
static u_int
|
|
|
|
argtou(const char *arg, u_int lo, u_int hi, const char *msg)
|
|
|
|
{
|
2015-04-23 16:27:14 +03:00
|
|
|
off_t x;
|
2001-09-10 22:27:41 +04:00
|
|
|
|
|
|
|
errno = 0;
|
2015-04-23 16:27:14 +03:00
|
|
|
x = argtooff(arg, msg);
|
|
|
|
if (x < lo || x > hi)
|
2001-09-10 22:27:41 +04:00
|
|
|
errx(1, "%s: bad %s", arg, msg);
|
2015-04-23 16:27:14 +03:00
|
|
|
return (u_int)x;
|
2001-09-10 22:27:41 +04:00
|
|
|
}
|
1997-03-01 22:42:54 +03:00
|
|
|
|
2009-03-26 11:39:24 +03:00
|
|
|
/*
|
|
|
|
* Same for off_t, with optional skmgpP suffix
|
|
|
|
*/
|
|
|
|
static off_t
|
|
|
|
argtooff(const char *arg, const char *msg)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
off_t x;
|
|
|
|
|
2011-03-05 16:47:43 +03:00
|
|
|
errno = 0;
|
2009-03-26 11:39:24 +03:00
|
|
|
x = strtoll(arg, &s, 0);
|
|
|
|
/* allow at most one extra char */
|
|
|
|
if (errno || x < 0 || (s[0] && s[1]) )
|
|
|
|
errx(1, "%s: bad %s", arg, msg);
|
|
|
|
if (*s) { /* the extra char is the multiplier */
|
|
|
|
switch (*s) {
|
|
|
|
default:
|
|
|
|
errx(1, "%s: bad %s", arg, msg);
|
|
|
|
/* notreached */
|
|
|
|
|
|
|
|
case 's': /* sector */
|
|
|
|
case 'S':
|
|
|
|
x <<= 9; /* times 512 */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k': /* kilobyte */
|
|
|
|
case 'K':
|
|
|
|
x <<= 10; /* times 1024 */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm': /* megabyte */
|
|
|
|
case 'M':
|
|
|
|
x <<= 20; /* times 1024*1024 */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g': /* gigabyte */
|
|
|
|
case 'G':
|
|
|
|
x <<= 30; /* times 1024*1024*1024 */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p': /* partition start */
|
|
|
|
case 'P': /* partition start */
|
|
|
|
case 'l': /* partition length */
|
|
|
|
case 'L': /* partition length */
|
|
|
|
errx(1, "%s: not supported yet %s", arg, msg);
|
2013-01-22 00:28:38 +04:00
|
|
|
return -1;
|
2009-03-26 11:39:24 +03:00
|
|
|
/* notreached */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2001-09-10 22:27:41 +04:00
|
|
|
/*
|
|
|
|
* Print usage message.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2004-01-06 02:23:32 +03:00
|
|
|
"usage: %s [ -options ] special [disktype]\n", getprogname());
|
2001-09-10 22:27:41 +04:00
|
|
|
fprintf(stderr, "where the options are:\n");
|
2013-01-22 00:28:38 +04:00
|
|
|
static struct {
|
|
|
|
char o;
|
|
|
|
const char *h;
|
|
|
|
} opts[] = {
|
2013-01-23 19:29:15 +04:00
|
|
|
#define AOPT(_opt, _type, _name, _min, _desc) { _opt, _desc },
|
2013-01-22 00:28:38 +04:00
|
|
|
ALLOPTS
|
|
|
|
#undef AOPT
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < __arraycount(opts); i++)
|
|
|
|
fprintf(stderr, "\t-%c %s\n", opts[i].o, opts[i].h);
|
2001-09-10 22:27:41 +04:00
|
|
|
exit(1);
|
1997-03-01 22:42:54 +03:00
|
|
|
}
|