xref: /freebsd/sbin/newfs_msdos/newfs_msdos.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*
2  * Copyright (c) 1998 Robert Nordier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef lint
29 static const char rcsid[] =
30   "$FreeBSD$";
31 #endif /* not lint */
32 
33 #include <sys/param.h>
34 
35 #include <err.h>
36 #include <errno.h>
37 #include <paths.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "mkfs_msdos.h"
44 
45 #define	argto1(arg, lo, msg)  argtou(arg, lo, 0xff, msg)
46 #define	argto2(arg, lo, msg)  argtou(arg, lo, 0xffff, msg)
47 #define	argto4(arg, lo, msg)  argtou(arg, lo, 0xffffffff, msg)
48 #define	argtox(arg, lo, msg)  argtou(arg, lo, UINT_MAX, msg)
49 
50 static u_int argtou(const char *, u_int, u_int, const char *);
51 static off_t argtooff(const char *, const char *);
52 static void usage(void);
53 
54 /*
55  * Construct a FAT12, FAT16, or FAT32 file system.
56  */
57 int
58 main(int argc, char *argv[])
59 {
60     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:";
61     struct msdos_options o;
62     const char *fname, *dtype;
63     char buf[MAXPATHLEN];
64     int ch;
65 
66     memset(&o, 0, sizeof(o));
67 
68     while ((ch = getopt(argc, argv, opts)) != -1)
69 	switch (ch) {
70 	case '@':
71 	    o.offset = argtooff(optarg, "offset");
72 	    break;
73 	case 'N':
74 	    o.no_create = 1;
75 	    break;
76 	case 'B':
77 	    o.bootstrap = optarg;
78 	    break;
79 	case 'C':
80 	    o.create_size = argtooff(optarg, "create size");
81 	    break;
82 	case 'F':
83 	    if (strcmp(optarg, "12") &&
84 		strcmp(optarg, "16") &&
85 		strcmp(optarg, "32"))
86 		errx(1, "%s: bad FAT type", optarg);
87 	    o.fat_type = atoi(optarg);
88 	    break;
89 	case 'I':
90 	    o.volume_id = argto4(optarg, 0, "volume ID");
91 	    o.volume_id_set = 1;
92 	    break;
93 	case 'L':
94 	    o.volume_label = optarg;
95 	    break;
96 	case 'O':
97 	    o.OEM_string = optarg;
98 	    break;
99 	case 'S':
100 	    o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
101 	    break;
102 	case 'a':
103 	    o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
104 	    break;
105 	case 'b':
106 	    o.block_size = argtox(optarg, 1, "block size");
107 	    o.sectors_per_cluster = 0;
108 	    break;
109 	case 'c':
110 	    o.sectors_per_cluster = argto1(optarg, 1, "sectors/cluster");
111 	    o.block_size = 0;
112 	    break;
113 	case 'e':
114 	    o.directory_entries = argto2(optarg, 1, "directory entries");
115 	    break;
116 	case 'f':
117 	    o.floppy = optarg;
118 	    break;
119 	case 'h':
120 	    o.drive_heads = argto2(optarg, 1, "drive heads");
121 	    break;
122 	case 'i':
123 	    o.info_sector = argto2(optarg, 1, "info sector");
124 	    break;
125 	case 'k':
126 	    o.backup_sector = argto2(optarg, 1, "backup sector");
127 	    break;
128 	case 'm':
129 	    o.media_descriptor = argto1(optarg, 0, "media descriptor");
130 	    o.media_descriptor_set = 1;
131 	    break;
132 	case 'n':
133 	    o.num_FAT = argto1(optarg, 1, "number of FATs");
134 	    break;
135 	case 'o':
136 	    o.hidden_sectors = argto4(optarg, 0, "hidden sectors");
137 	    o.hidden_sectors_set = 1;
138 	    break;
139 	case 'r':
140 	    o.reserved_sectors = argto2(optarg, 1, "reserved sectors");
141 	    break;
142 	case 's':
143 	    o.size = argto4(optarg, 1, "file system size");
144 	    break;
145 	case 'u':
146 	    o.sectors_per_track = argto2(optarg, 1, "sectors/track");
147 	    break;
148 	default:
149 	    usage();
150 	}
151     argc -= optind;
152     argv += optind;
153     if (argc < 1 || argc > 2)
154 	usage();
155     fname = *argv++;
156     if (!o.create_size && !strchr(fname, '/')) {
157 	snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
158 	if (!(fname = strdup(buf)))
159 	    err(1, NULL);
160     }
161     dtype = *argv;
162     return !!mkfs_msdos(fname, dtype, &o);
163 }
164 
165 /*
166  * Convert and check a numeric option argument.
167  */
168 static u_int
169 argtou(const char *arg, u_int lo, u_int hi, const char *msg)
170 {
171     char *s;
172     u_long x;
173 
174     errno = 0;
175     x = strtoul(arg, &s, 0);
176     if (errno || !*arg || *s || x < lo || x > hi)
177 	errx(1, "%s: bad %s", arg, msg);
178     return x;
179 }
180 
181 /*
182  * Same for off_t, with optional skmgpP suffix
183  */
184 static off_t
185 argtooff(const char *arg, const char *msg)
186 {
187     char *s;
188     off_t x;
189 
190     errno = 0;
191     x = strtoll(arg, &s, 0);
192     /* allow at most one extra char */
193     if (errno || x < 0 || (s[0] && s[1]) )
194 	errx(1, "%s: bad %s", arg, msg);
195     if (*s) {	/* the extra char is the multiplier */
196 	switch (*s) {
197 	default:
198 	    errx(1, "%s: bad %s", arg, msg);
199 	    /* notreached */
200 
201 	case 's':		/* sector */
202 	case 'S':
203 	    x <<= 9;		/* times 512 */
204 	    break;
205 
206 	case 'k':		/* kilobyte */
207 	case 'K':
208 	    x <<= 10;		/* times 1024 */
209 	    break;
210 
211 	case 'm':		/* megabyte */
212 	case 'M':
213 	    x <<= 20;		/* times 1024*1024 */
214 	    break;
215 
216 	case 'g':		/* gigabyte */
217 	case 'G':
218 	    x <<= 30;		/* times 1024*1024*1024 */
219 	    break;
220 
221 	case 'p':		/* partition start */
222 	case 'P':
223 	case 'l':		/* partition length */
224 	case 'L':
225 	    errx(1, "%s: not supported yet %s", arg, msg);
226 	    /* notreached */
227 	}
228     }
229     return x;
230 }
231 
232 /*
233  * Print usage message.
234  */
235 static void
236 usage(void)
237 {
238     fprintf(stderr,
239 	    "usage: %s [ -options ] special [disktype]\n", getprogname());
240     fprintf(stderr, "where the options are:\n");
241 static struct {
242     char o;
243     const char *h;
244 } opts[] = {
245 #define AOPT(_opt, _type, _name, _min, _desc) { _opt, _desc },
246 ALLOPTS
247 #undef AOPT
248     };
249     for (size_t i = 0; i < nitems(opts); i++)
250 	fprintf(stderr, "\t-%c %s\n", opts[i].o, opts[i].h);
251     exit(1);
252 }
253