2 * Copyright (c) 1998 Robert Nordier
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
29 static const char rcsid[] =
33 #include <sys/param.h>
43 #include "mkfs_msdos.h"
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)
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);
55 * Construct a FAT12, FAT16, or FAT32 file system.
58 main(int argc, char *argv[])
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;
66 memset(&o, 0, sizeof(o));
68 while ((ch = getopt(argc, argv, opts)) != -1)
71 o.offset = argtooff(optarg, "offset");
80 o.create_size = argtooff(optarg, "create size");
83 if (strcmp(optarg, "12") &&
84 strcmp(optarg, "16") &&
86 errx(1, "%s: bad FAT type", optarg);
87 o.fat_type = atoi(optarg);
90 o.volume_id = argto4(optarg, 0, "volume ID");
94 o.volume_label = optarg;
97 o.OEM_string = optarg;
100 o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
103 o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
106 o.block_size = argtox(optarg, 1, "block size");
107 o.sectors_per_cluster = 0;
110 o.sectors_per_cluster = argto1(optarg, 1, "sectors/cluster");
114 o.directory_entries = argto2(optarg, 1, "directory entries");
120 o.drive_heads = argto2(optarg, 1, "drive heads");
123 o.info_sector = argto2(optarg, 1, "info sector");
126 o.backup_sector = argto2(optarg, 1, "backup sector");
129 o.media_descriptor = argto1(optarg, 0, "media descriptor");
130 o.media_descriptor_set = 1;
133 o.num_FAT = argto1(optarg, 1, "number of FATs");
136 o.hidden_sectors = argto4(optarg, 0, "hidden sectors");
137 o.hidden_sectors_set = 1;
140 o.reserved_sectors = argto2(optarg, 1, "reserved sectors");
143 o.size = argto4(optarg, 1, "file system size");
146 o.sectors_per_track = argto2(optarg, 1, "sectors/track");
153 if (argc < 1 || argc > 2)
156 if (!o.create_size && !strchr(fname, '/')) {
157 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
158 if (!(fname = strdup(buf)))
162 return mkfs_msdos(fname, dtype, &o);
166 * Convert and check a numeric option argument.
169 argtou(const char *arg, u_int lo, u_int hi, const char *msg)
175 x = strtoul(arg, &s, 0);
176 if (errno || !*arg || *s || x < lo || x > hi)
177 errx(1, "%s: bad %s", arg, msg);
182 * Same for off_t, with optional skmgpP suffix
185 argtooff(const char *arg, const char *msg)
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 */
198 errx(1, "%s: bad %s", arg, msg);
201 case 's': /* sector */
203 x <<= 9; /* times 512 */
206 case 'k': /* kilobyte */
208 x <<= 10; /* times 1024 */
211 case 'm': /* megabyte */
213 x <<= 20; /* times 1024*1024 */
216 case 'g': /* gigabyte */
218 x <<= 30; /* times 1024*1024*1024 */
221 case 'p': /* partition start */
223 case 'l': /* partition length */
225 errx(1, "%s: not supported yet %s", arg, msg);
233 * Print usage message.
239 "usage: newfs_msdos [ -options ] special [disktype]\n"
240 "where the options are:\n"
241 "\t-@ create file system at specified offset\n"
242 "\t-B get bootstrap from file\n"
243 "\t-C create image file with specified size\n"
244 "\t-F FAT type (12, 16, or 32)\n"
246 "\t-L volume label\n"
247 "\t-N don't create file system: just print out parameters\n"
249 "\t-S bytes/sector\n"
252 "\t-c sectors/cluster\n"
253 "\t-e root directory entries\n"
254 "\t-f standard format\n"
256 "\t-i file system info sector\n"
257 "\t-k backup boot sector\n"
258 "\t-m media descriptor\n"
259 "\t-n number of FATs\n"
260 "\t-o hidden sectors\n"
261 "\t-r reserved sectors\n"
262 "\t-s file system size (sectors)\n"
263 "\t-u sectors/track\n");