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#include <sys/param.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31
32#include <err.h>
33#include <errno.h>
34#include <paths.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <stdint.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
48static unsigned int argtou(const char*, unsigned int, unsigned int, const char*);
49static off_t argtooff(const char*, const char*);
50static void usage(void);
51
52static time_t
53get_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 */
72int main(int argc, char* argv[]) {
73    static const char opts[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:T:u:";
74    struct msdos_options o;
75    const char* fname;
76    char buf[MAXPATHLEN];
77    int ch;
78
79    memset(&o, 0, sizeof(o));
80
81    while ((ch = getopt(argc, argv, opts)) != -1)
82        switch (ch) {
83        case '@':
84            o.offset = argtooff(optarg, "offset");
85            break;
86        case 'N':
87            o.no_create = 1;
88            break;
89        case 'B':
90            o.bootstrap = optarg;
91            break;
92        case 'C':
93            o.create_size = argtooff(optarg, "create size");
94            break;
95        case 'F':
96            if (strcmp(optarg, "12") && strcmp(optarg, "16") &&
97                strcmp(optarg, "32"))
98                errx(1, "%s: bad FAT type", optarg);
99            o.fat_type = atoi(optarg);
100            break;
101        case 'I':
102            o.volume_id = argto4(optarg, 0, "volume ID");
103            o.volume_id_set = 1;
104            break;
105        case 'L':
106            o.volume_label = optarg;
107            break;
108        case 'O':
109            o.OEM_string = optarg;
110            break;
111        case 'S':
112            o.disk_size = argtooff(optarg, "disk size");
113            break;
114        case 'a':
115            o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
116            break;
117        case 'b':
118            o.block_size = argtox(optarg, 1, "block size");
119            o.sectors_per_cluster = 0;
120            break;
121        case 'c':
122            o.sectors_per_cluster = argto1(optarg, 1, "sectors/cluster");
123            o.block_size = 0;
124            break;
125        case 'e':
126            o.directory_entries = argto2(optarg, 1, "directory entries");
127            break;
128        case 'i':
129            o.info_sector = argto2(optarg, 1, "info sector");
130            break;
131        case 'k':
132            o.backup_sector = argto2(optarg, 1, "backup sector");
133            break;
134        case 'm':
135            o.media_descriptor = argto1(optarg, 0, "media descriptor");
136            o.media_descriptor_set = 1;
137            break;
138        case 'n':
139            o.num_FAT = argto1(optarg, 1, "number of FATs");
140            break;
141        case 'r':
142            o.reserved_sectors = argto2(optarg, 1, "reserved sectors");
143            break;
144        case 'T':
145            o.timestamp_set = 1;
146            o.timestamp = get_tstamp(optarg);
147            break;
148        default:
149            usage();
150        }
151    argc -= optind;
152    argv += optind;
153    if (argc != 1)
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    return !!mkfs_msdos(fname, &o);
162}
163
164/*
165 * Convert and check a numeric option argument.
166 */
167static unsigned int argtou(const char* arg, unsigned int lo, unsigned int hi, const char* msg) {
168    char* s;
169    u_long x;
170
171    errno = 0;
172    x = strtoul(arg, &s, 0);
173    if (errno || !*arg || *s || x < lo || x > hi)
174        errx(1, "%s: bad %s", arg, msg);
175    return x;
176}
177
178/*
179 * Same for off_t, with optional skmgpP suffix
180 */
181static off_t argtooff(const char* arg, const char* msg) {
182    char* s;
183    off_t x;
184
185    errno = 0;
186    x = strtoll(arg, &s, 0);
187    /* allow at most one extra char */
188    if (errno || x < 0 || (s[0] && s[1]))
189        errx(1, "%s: bad %s", arg, msg);
190    if (*s) { /* the extra char is the multiplier */
191        switch (*s) {
192        default:
193            errx(1, "%s: bad %s", arg, msg);
194        /* notreached */
195
196        case 's': /* sector */
197        case 'S':
198            x <<= 9; /* times 512 */
199            break;
200
201        case 'k': /* kilobyte */
202        case 'K':
203            x <<= 10; /* times 1024 */
204            break;
205
206        case 'm': /* megabyte */
207        case 'M':
208            x <<= 20; /* times 1024*1024 */
209            break;
210
211        case 'g': /* gigabyte */
212        case 'G':
213            x <<= 30; /* times 1024*1024*1024 */
214            break;
215
216        case 'p': /* partition start */
217        case 'P':
218        case 'l': /* partition length */
219        case 'L':
220            errx(1, "%s: not supported yet %s", arg, msg);
221            /* notreached */
222        }
223    }
224    return x;
225}
226
227/*
228 * Print usage message.
229 */
230static void usage(void) {
231    fprintf(stderr, "usage: mkfs [ -options ] special [disktype]\n");
232    fprintf(stderr, "where the options are:\n");
233    static struct {
234        char o;
235        const char* h;
236    } opts[] = {
237#define AOPT(_opt, _type, _name, _min, _desc) {_opt, _desc},
238        ALLOPTS
239#undef AOPT
240    };
241    for (size_t i = 0; i < sizeof(opts) / sizeof(opts[0]); i++)
242        fprintf(stderr, "\t-%c %s\n", opts[i].o, opts[i].h);
243    exit(1);
244}
245