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