1 /* $NetBSD: newfs_msdos.c,v 1.45 2017/02/16 22:42:25 christos Exp $ */
4 * Copyright (c) 1998 Robert Nordier
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
33 static const char rcsid
[] =
34 "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.15 2000/10/10 01:49:37 wollman Exp $";
36 __RCSID("$NetBSD: newfs_msdos.c,v 1.45 2017/02/16 22:42:25 christos Exp $");
40 #include <sys/param.h>
51 #include "mkfs_msdos.h"
53 #define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
54 #define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
55 #define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
56 #define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
58 __dead
static void usage(void);
59 static u_int
argtou(const char *, u_int
, u_int
, const char *);
60 static off_t
argtooff(const char *, const char *);
63 get_tstamp(const char *b
)
68 #ifndef HAVE_NBTOOL_CONFIG_H
72 if (stat(b
, &st
) != -1)
73 return (time_t)st
.st_mtime
;
75 #ifndef HAVE_NBTOOL_CONFIG_H
77 if ((when
= parsedate(b
, NULL
, NULL
)) != -1 || errno
== 0)
81 l
= strtoll(b
, &eb
, 0);
82 if (b
== eb
|| *eb
|| errno
)
83 errx(EXIT_FAILURE
, "Can't parse timestamp `%s'", b
);
88 * Construct a FAT12, FAT16, or FAT32 file system.
91 main(int argc
, char *argv
[])
93 static const char opts
[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:T:u:";
94 struct msdos_options o
;
99 memset(&o
, 0, sizeof(o
));
101 while ((ch
= getopt(argc
, argv
, opts
)) != -1)
104 o
.offset
= argtooff(optarg
, "offset");
110 o
.bootstrap
= optarg
;
113 o
.create_size
= argtooff(optarg
, "create size");
116 o
.fat_type
= atoi(optarg
);
119 o
.volume_id
= argto4(optarg
, 0, "volume ID");
123 o
.volume_label
= optarg
;
126 o
.OEM_string
= optarg
;
129 o
.bytes_per_sector
= argto2(optarg
, 1, "bytes/sector");
132 o
.sectors_per_fat
= argto4(optarg
, 1, "sectors/FAT");
135 o
.block_size
= argtox(optarg
, 1, "block size");
138 o
.sectors_per_cluster
= argto1(optarg
, 1, "sectors/cluster");
141 o
.directory_entries
= argto2(optarg
, 1, "directory entries");
147 o
.drive_heads
= argto2(optarg
, 1, "drive heads");
150 o
.info_sector
= argto2(optarg
, 1, "info sector");
153 o
.backup_sector
= argto2(optarg
, 1, "backup sector");
156 o
.media_descriptor
= argto1(optarg
, 0, "media descriptor");
157 o
.media_descriptor_set
= 1;
160 o
.num_FAT
= argto1(optarg
, 1, "number of FATs");
163 o
.hidden_sectors
= argto4(optarg
, 0, "hidden sectors");
164 o
.hidden_sectors_set
= 1;
167 o
.reserved_sectors
= argto2(optarg
, 1, "reserved sectors");
170 o
.size
= argto4(optarg
, 1, "file system size");
174 o
.timestamp
= get_tstamp(optarg
);
177 o
.sectors_per_track
= argto2(optarg
, 1, "sectors/track");
184 if (argc
< 1 || argc
> 2)
187 if (!strchr(fname
, '/') && !o
.create_size
) {
188 snprintf(buf
, sizeof(buf
), "%sr%s", _PATH_DEV
, fname
);
189 if (!(fname
= strdup(buf
)))
193 return mkfs_msdos(fname
, dtype
, &o
);
197 * Convert and check a numeric option argument.
200 argtou(const char *arg
, u_int lo
, u_int hi
, const char *msg
)
205 x
= argtooff(arg
, msg
);
206 if (x
< lo
|| x
> hi
)
207 errx(1, "%s: bad %s", arg
, msg
);
212 * Same for off_t, with optional skmgpP suffix
215 argtooff(const char *arg
, const char *msg
)
221 x
= strtoll(arg
, &s
, 0);
222 /* allow at most one extra char */
223 if (errno
|| x
< 0 || (s
[0] && s
[1]) )
224 errx(1, "%s: bad %s", arg
, msg
);
225 if (*s
) { /* the extra char is the multiplier */
228 errx(1, "%s: bad %s", arg
, msg
);
231 case 's': /* sector */
233 x
<<= 9; /* times 512 */
236 case 'k': /* kilobyte */
238 x
<<= 10; /* times 1024 */
241 case 'm': /* megabyte */
243 x
<<= 20; /* times 1024*1024 */
246 case 'g': /* gigabyte */
248 x
<<= 30; /* times 1024*1024*1024 */
251 case 'p': /* partition start */
252 case 'P': /* partition start */
253 case 'l': /* partition length */
254 case 'L': /* partition length */
255 errx(1, "%s: not supported yet %s", arg
, msg
);
264 * Print usage message.
270 "usage: %s [ -options ] special [disktype]\n", getprogname());
271 fprintf(stderr
, "where the options are:\n");
276 #define AOPT(_opt, _type, _name, _min, _desc) { _opt, _desc },
280 for (size_t i
= 0; i
< __arraycount(opts
); i
++)
281 fprintf(stderr
, "\t-%c %s\n", opts
[i
].o
, opts
[i
].h
);