2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
1998-07-06 20:01:34 +00:00
|
|
|
* Copyright (c) 1998 Robert Nordier
|
|
|
|
* 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
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1998-07-06 20:01:34 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2017-05-16 17:04:50 +00:00
|
|
|
#include <sys/stat.h>
|
1998-07-06 20:01:34 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-15 19:00:33 +00:00
|
|
|
#include "mkfs_msdos.h"
|
1998-07-06 20:01:34 +00:00
|
|
|
|
2014-04-30 21:19:46 +00: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)
|
1998-07-06 20:01:34 +00:00
|
|
|
|
|
|
|
static u_int argtou(const char *, u_int, u_int, const char *);
|
2008-12-03 18:22:36 +00:00
|
|
|
static off_t argtooff(const char *, const char *);
|
1998-07-06 20:01:34 +00:00
|
|
|
static void usage(void);
|
|
|
|
|
2017-05-16 17:04:50 +00:00
|
|
|
static time_t
|
|
|
|
get_tstamp(const char *b)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *eb;
|
|
|
|
long long l;
|
|
|
|
|
|
|
|
if (stat(b, &st) != -1)
|
|
|
|
return (time_t)st.st_mtime;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
l = strtoll(b, &eb, 0);
|
|
|
|
if (b == eb || *eb || errno)
|
|
|
|
errx(EXIT_FAILURE, "Can't parse timestamp '%s'", b);
|
|
|
|
return (time_t)l;
|
|
|
|
}
|
|
|
|
|
1998-07-06 20:01:34 +00:00
|
|
|
/*
|
2002-08-21 18:11:48 +00:00
|
|
|
* Construct a FAT12, FAT16, or FAT32 file system.
|
1998-07-06 20:01:34 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2018-06-15 06:03:40 +00:00
|
|
|
static const char opts[] = "@:NAB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:T:u:";
|
2015-10-15 16:19:00 +00:00
|
|
|
struct msdos_options o;
|
|
|
|
const char *fname, *dtype;
|
1998-07-06 20:01:34 +00:00
|
|
|
char buf[MAXPATHLEN];
|
2015-10-15 16:19:00 +00:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
memset(&o, 0, sizeof(o));
|
1998-07-06 20:01:34 +00:00
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, opts)) != -1)
|
|
|
|
switch (ch) {
|
2008-12-03 18:22:36 +00:00
|
|
|
case '@':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.offset = argtooff(optarg, "offset");
|
2008-12-03 18:22:36 +00:00
|
|
|
break;
|
1998-07-06 20:01:34 +00:00
|
|
|
case 'N':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.no_create = 1;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
2018-06-15 06:03:40 +00:00
|
|
|
case 'A':
|
|
|
|
o.align = true;
|
|
|
|
break;
|
1998-07-06 20:01:34 +00:00
|
|
|
case 'B':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.bootstrap = optarg;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
2008-12-03 18:22:36 +00:00
|
|
|
case 'C':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.create_size = argtooff(optarg, "create size");
|
2008-12-03 18:22:36 +00:00
|
|
|
break;
|
1998-07-06 20:01:34 +00:00
|
|
|
case 'F':
|
|
|
|
if (strcmp(optarg, "12") &&
|
|
|
|
strcmp(optarg, "16") &&
|
|
|
|
strcmp(optarg, "32"))
|
|
|
|
errx(1, "%s: bad FAT type", optarg);
|
2015-10-15 16:19:00 +00:00
|
|
|
o.fat_type = atoi(optarg);
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.volume_id = argto4(optarg, 0, "volume ID");
|
|
|
|
o.volume_id_set = 1;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'L':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.volume_label = optarg;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'O':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.OEM_string = optarg;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.block_size = argtox(optarg, 1, "block size");
|
|
|
|
o.sectors_per_cluster = 0;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.sectors_per_cluster = argto1(optarg, 1, "sectors/cluster");
|
|
|
|
o.block_size = 0;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.directory_entries = argto2(optarg, 1, "directory entries");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.floppy = optarg;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.drive_heads = argto2(optarg, 1, "drive heads");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.info_sector = argto2(optarg, 1, "info sector");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'k':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.backup_sector = argto2(optarg, 1, "backup sector");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.media_descriptor = argto1(optarg, 0, "media descriptor");
|
|
|
|
o.media_descriptor_set = 1;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.num_FAT = argto1(optarg, 1, "number of FATs");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'o':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.hidden_sectors = argto4(optarg, 0, "hidden sectors");
|
|
|
|
o.hidden_sectors_set = 1;
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.reserved_sectors = argto2(optarg, 1, "reserved sectors");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.size = argto4(optarg, 1, "file system size");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
2017-05-16 17:04:50 +00:00
|
|
|
case 'T':
|
|
|
|
o.timestamp_set = 1;
|
|
|
|
o.timestamp = get_tstamp(optarg);
|
|
|
|
break;
|
1998-07-06 20:01:34 +00:00
|
|
|
case 'u':
|
2015-10-15 16:19:00 +00:00
|
|
|
o.sectors_per_track = argto2(optarg, 1, "sectors/track");
|
1998-07-06 20:01:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc < 1 || argc > 2)
|
|
|
|
usage();
|
2018-06-15 06:03:40 +00:00
|
|
|
if (o.align) {
|
|
|
|
if (o.hidden_sectors_set)
|
|
|
|
errx(1, "align (-A) is incompatible with -r");
|
|
|
|
}
|
1998-07-06 20:01:34 +00:00
|
|
|
fname = *argv++;
|
2015-10-15 16:19:00 +00:00
|
|
|
if (!o.create_size && !strchr(fname, '/')) {
|
2000-07-26 20:12:46 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
|
1998-07-06 20:01:34 +00:00
|
|
|
if (!(fname = strdup(buf)))
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
dtype = *argv;
|
2015-11-23 18:58:00 +00:00
|
|
|
return !!mkfs_msdos(fname, dtype, &o);
|
2015-10-15 16:19:00 +00:00
|
|
|
}
|
|
|
|
|
1998-07-06 20:01:34 +00:00
|
|
|
/*
|
|
|
|
* Convert and check a numeric option argument.
|
|
|
|
*/
|
|
|
|
static u_int
|
|
|
|
argtou(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)
|
|
|
|
errx(1, "%s: bad %s", arg, msg);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2008-12-03 18:22:36 +00: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-12-20 15:50:54 +00:00
|
|
|
errno = 0;
|
2008-12-03 18:22:36 +00: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 */
|
2015-10-15 19:00:33 +00:00
|
|
|
|
2011-12-20 15:50:54 +00:00
|
|
|
case 's': /* sector */
|
2008-12-03 18:22:36 +00:00
|
|
|
case 'S':
|
2011-12-20 15:50:54 +00:00
|
|
|
x <<= 9; /* times 512 */
|
2008-12-03 18:22:36 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-20 15:50:54 +00:00
|
|
|
case 'k': /* kilobyte */
|
2008-12-03 18:22:36 +00:00
|
|
|
case 'K':
|
2011-12-20 15:50:54 +00:00
|
|
|
x <<= 10; /* times 1024 */
|
2008-12-03 18:22:36 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-20 15:50:54 +00:00
|
|
|
case 'm': /* megabyte */
|
2008-12-03 18:22:36 +00:00
|
|
|
case 'M':
|
2011-12-20 15:50:54 +00:00
|
|
|
x <<= 20; /* times 1024*1024 */
|
2008-12-03 18:22:36 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-20 15:50:54 +00:00
|
|
|
case 'g': /* gigabyte */
|
2008-12-03 18:22:36 +00:00
|
|
|
case 'G':
|
2011-12-20 15:50:54 +00:00
|
|
|
x <<= 30; /* times 1024*1024*1024 */
|
2008-12-03 18:22:36 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-20 15:50:54 +00:00
|
|
|
case 'p': /* partition start */
|
|
|
|
case 'P':
|
|
|
|
case 'l': /* partition length */
|
|
|
|
case 'L':
|
2008-12-03 18:22:36 +00:00
|
|
|
errx(1, "%s: not supported yet %s", arg, msg);
|
|
|
|
/* notreached */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
1998-07-06 20:01:34 +00:00
|
|
|
/*
|
|
|
|
* Print usage message.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-11-27 14:40:21 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [ -options ] special [disktype]\n", getprogname());
|
|
|
|
fprintf(stderr, "where the options are:\n");
|
|
|
|
static struct {
|
|
|
|
char o;
|
|
|
|
const char *h;
|
|
|
|
} opts[] = {
|
|
|
|
#define AOPT(_opt, _type, _name, _min, _desc) { _opt, _desc },
|
|
|
|
ALLOPTS
|
|
|
|
#undef AOPT
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < nitems(opts); i++)
|
|
|
|
fprintf(stderr, "\t-%c %s\n", opts[i].o, opts[i].h);
|
|
|
|
exit(1);
|
1998-07-06 20:01:34 +00:00
|
|
|
}
|