xref: /freebsd/sbin/newfs_msdos/newfs_msdos.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 Robert Nordier
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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
15  *    distribution.
16  *
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.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <paths.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "mkfs_msdos.h"
41 
42 #define	argto1(arg, lo, msg)  argtou(arg, lo, 0xff, msg)
43 #define	argto2(arg, lo, msg)  argtou(arg, lo, 0xffff, msg)
44 #define	argto4(arg, lo, msg)  argtou(arg, lo, 0xffffffff, msg)
45 #define	argtox(arg, lo, msg)  argtou(arg, lo, UINT_MAX, msg)
46 
47 static u_int argtou(const char *, u_int, u_int, const char *);
48 static off_t argtooff(const char *, const char *);
49 static void usage(void) __dead2;
50 
51 static time_t
52 get_tstamp(const char *b)
53 {
54     struct stat st;
55     char *eb;
56     long long l;
57 
58     if (stat(b, &st) != -1)
59         return (time_t)st.st_mtime;
60 
61     errno = 0;
62     l = strtoll(b, &eb, 0);
63     if (b == eb || *eb || errno)
64         errx(EXIT_FAILURE, "Can't parse timestamp '%s'", b);
65     return (time_t)l;
66 }
67 
68 /*
69  * Construct a FAT12, FAT16, or FAT32 file system.
70  */
71 int
72 main(int argc, char *argv[])
73 {
74     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:";
75     struct msdos_options o;
76     const char *fname, *dtype;
77     char buf[MAXPATHLEN];
78     int ch;
79 
80     memset(&o, 0, sizeof(o));
81 
82     while ((ch = getopt(argc, argv, opts)) != -1)
83 	switch (ch) {
84 	case '@':
85 	    o.offset = argtooff(optarg, "offset");
86 	    break;
87 	case 'N':
88 	    o.no_create = 1;
89 	    break;
90 	case 'A':
91 	    o.align = true;
92 	    break;
93 	case 'B':
94 	    o.bootstrap = optarg;
95 	    break;
96 	case 'C':
97 	    o.create_size = argtooff(optarg, "create size");
98 	    break;
99 	case 'F':
100 	    if (strcmp(optarg, "12") &&
101 		strcmp(optarg, "16") &&
102 		strcmp(optarg, "32"))
103 		errx(1, "%s: bad FAT type", optarg);
104 	    o.fat_type = atoi(optarg);
105 	    break;
106 	case 'I':
107 	    o.volume_id = argto4(optarg, 0, "volume ID");
108 	    o.volume_id_set = 1;
109 	    break;
110 	case 'L':
111 	    o.volume_label = optarg;
112 	    break;
113 	case 'O':
114 	    o.OEM_string = optarg;
115 	    break;
116 	case 'S':
117 	    o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
118 	    break;
119 	case 'a':
120 	    o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
121 	    break;
122 	case 'b':
123 	    o.block_size = argtox(optarg, 1, "block size");
124 	    o.sectors_per_cluster = 0;
125 	    break;
126 	case 'c':
127 	    o.sectors_per_cluster = argto1(optarg, 1, "sectors/cluster");
128 	    o.block_size = 0;
129 	    break;
130 	case 'e':
131 	    o.directory_entries = argto2(optarg, 1, "directory entries");
132 	    break;
133 	case 'f':
134 	    o.floppy = optarg;
135 	    break;
136 	case 'h':
137 	    o.drive_heads = argto2(optarg, 1, "drive heads");
138 	    break;
139 	case 'i':
140 	    o.info_sector = argto2(optarg, 1, "info sector");
141 	    break;
142 	case 'k':
143 	    o.backup_sector = argto2(optarg, 1, "backup sector");
144 	    break;
145 	case 'm':
146 	    o.media_descriptor = argto1(optarg, 0, "media descriptor");
147 	    o.media_descriptor_set = 1;
148 	    break;
149 	case 'n':
150 	    o.num_FAT = argto1(optarg, 1, "number of FATs");
151 	    break;
152 	case 'o':
153 	    o.hidden_sectors = argto4(optarg, 0, "hidden sectors");
154 	    o.hidden_sectors_set = 1;
155 	    break;
156 	case 'r':
157 	    o.reserved_sectors = argto2(optarg, 1, "reserved sectors");
158 	    break;
159 	case 's':
160 	    o.size = argto4(optarg, 1, "file system size");
161 	    break;
162 	case 'T':
163 	    o.timestamp_set = 1;
164 	    o.timestamp = get_tstamp(optarg);
165 	    break;
166 	case 'u':
167 	    o.sectors_per_track = argto2(optarg, 1, "sectors/track");
168 	    break;
169 	default:
170 	    usage();
171 	}
172     argc -= optind;
173     argv += optind;
174     if (argc < 1 || argc > 2)
175 	usage();
176     if (o.align) {
177 	if (o.reserved_sectors)
178 	    errx(1, "align (-A) is incompatible with -r");
179     }
180     fname = *argv++;
181     if (!o.create_size && !strchr(fname, '/')) {
182 	snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
183 	fname = buf;
184     }
185     dtype = *argv;
186     exit(!!mkfs_msdos(fname, dtype, &o));
187 }
188 
189 /*
190  * Convert and check a numeric option argument.
191  */
192 static u_int
193 argtou(const char *arg, u_int lo, u_int hi, const char *msg)
194 {
195     char *s;
196     u_long x;
197 
198     errno = 0;
199     x = strtoul(arg, &s, 0);
200     if (errno || !*arg || *s || x < lo || x > hi)
201 	errx(1, "%s: bad %s", arg, msg);
202     return x;
203 }
204 
205 /*
206  * Same for off_t, with optional skmgpP suffix
207  */
208 static off_t
209 argtooff(const char *arg, const char *msg)
210 {
211     char *s;
212     off_t x;
213 
214     errno = 0;
215     x = strtoll(arg, &s, 0);
216     /* allow at most one extra char */
217     if (errno || x < 0 || (s[0] && s[1]) )
218 	errx(1, "%s: bad %s", arg, msg);
219     if (*s) {	/* the extra char is the multiplier */
220 	switch (*s) {
221 	default:
222 	    errx(1, "%s: bad %s", arg, msg);
223 	    /* notreached */
224 
225 	case 's':		/* sector */
226 	case 'S':
227 	    x <<= 9;		/* times 512 */
228 	    break;
229 
230 	case 'k':		/* kilobyte */
231 	case 'K':
232 	    x <<= 10;		/* times 1024 */
233 	    break;
234 
235 	case 'm':		/* megabyte */
236 	case 'M':
237 	    x <<= 20;		/* times 1024*1024 */
238 	    break;
239 
240 	case 'g':		/* gigabyte */
241 	case 'G':
242 	    x <<= 30;		/* times 1024*1024*1024 */
243 	    break;
244 
245 	case 'p':		/* partition start */
246 	case 'P':
247 	case 'l':		/* partition length */
248 	case 'L':
249 	    errx(1, "%s: not supported yet %s", arg, msg);
250 	    /* notreached */
251 	}
252     }
253     return x;
254 }
255 
256 /*
257  * Print usage message.
258  */
259 static void
260 usage(void)
261 {
262     fprintf(stderr,
263 	    "usage: %s [ -options ] special [disktype]\n", getprogname());
264     fprintf(stderr, "where the options are:\n");
265 static struct {
266     char o;
267     const char *h;
268 } opts[] = {
269 #define AOPT(_opt, _type, _name, _min, _desc) { _opt, _desc },
270 ALLOPTS
271 #undef AOPT
272     };
273     for (size_t i = 0; i < nitems(opts); i++)
274 	fprintf(stderr, "\t-%c %s\n", opts[i].o, opts[i].h);
275     exit(1);
276 }
277