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