mkfs_msdos.c revision 1.1.2.2 1 1.1.2.2 yamt /* $NetBSD: mkfs_msdos.c,v 1.1.2.2 2013/01/23 00:05:32 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*
4 1.1.2.2 yamt * Copyright (c) 1998 Robert Nordier
5 1.1.2.2 yamt * All rights reserved.
6 1.1.2.2 yamt *
7 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 yamt * modification, are permitted provided that the following conditions
9 1.1.2.2 yamt * are met:
10 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in
14 1.1.2.2 yamt * the documentation and/or other materials provided with the
15 1.1.2.2 yamt * distribution.
16 1.1.2.2 yamt *
17 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
18 1.1.2.2 yamt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.1.2.2 yamt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1.2.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
21 1.1.2.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1.2.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 1.1.2.2 yamt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 1.1.2.2 yamt * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.1.2.2 yamt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 1.1.2.2 yamt * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1.2.2 yamt */
29 1.1.2.2 yamt
30 1.1.2.2 yamt #include <sys/cdefs.h>
31 1.1.2.2 yamt #ifndef lint
32 1.1.2.2 yamt #if 0
33 1.1.2.2 yamt static const char rcsid[] =
34 1.1.2.2 yamt "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.15 2000/10/10 01:49:37 wollman Exp $";
35 1.1.2.2 yamt #else
36 1.1.2.2 yamt __RCSID("$NetBSD: mkfs_msdos.c,v 1.1.2.2 2013/01/23 00:05:32 yamt Exp $");
37 1.1.2.2 yamt #endif
38 1.1.2.2 yamt #endif /* not lint */
39 1.1.2.2 yamt
40 1.1.2.2 yamt #include <sys/types.h>
41 1.1.2.2 yamt #include <sys/param.h>
42 1.1.2.2 yamt #include <sys/mount.h>
43 1.1.2.2 yamt #include <sys/stat.h>
44 1.1.2.2 yamt #include <sys/time.h>
45 1.1.2.2 yamt #include <sys/disk.h>
46 1.1.2.2 yamt
47 1.1.2.2 yamt #include <ctype.h>
48 1.1.2.2 yamt #include <err.h>
49 1.1.2.2 yamt #include <errno.h>
50 1.1.2.2 yamt #include <fcntl.h>
51 1.1.2.2 yamt #include <paths.h>
52 1.1.2.2 yamt #include <stdio.h>
53 1.1.2.2 yamt #include <stdlib.h>
54 1.1.2.2 yamt #include <string.h>
55 1.1.2.2 yamt #include <time.h>
56 1.1.2.2 yamt #include <unistd.h>
57 1.1.2.2 yamt #include <signal.h>
58 1.1.2.2 yamt
59 1.1.2.2 yamt #include <util.h>
60 1.1.2.2 yamt #include <disktab.h>
61 1.1.2.2 yamt
62 1.1.2.2 yamt #include "partutil.h"
63 1.1.2.2 yamt #include "mkfs_msdos.h"
64 1.1.2.2 yamt
65 1.1.2.2 yamt #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
66 1.1.2.2 yamt #define BPN 4 /* bits per nibble */
67 1.1.2.2 yamt #define NPB 2 /* nibbles per byte */
68 1.1.2.2 yamt
69 1.1.2.2 yamt #define DOSMAGIC 0xaa55 /* DOS magic number */
70 1.1.2.2 yamt #define MINBPS 512 /* minimum bytes per sector */
71 1.1.2.2 yamt #define MAXSPC 128 /* maximum sectors per cluster */
72 1.1.2.2 yamt #define MAXNFT 16 /* maximum number of FATs */
73 1.1.2.2 yamt #define DEFBLK 4096 /* default block size */
74 1.1.2.2 yamt #define DEFBLK16 2048 /* default block size FAT16 */
75 1.1.2.2 yamt #define DEFRDE 512 /* default root directory entries */
76 1.1.2.2 yamt #define RESFTE 2 /* reserved FAT entries */
77 1.1.2.2 yamt #define MINCLS12 1 /* minimum FAT12 clusters */
78 1.1.2.2 yamt #define MINCLS16 0xff5 /* minimum FAT16 clusters */
79 1.1.2.2 yamt #define MINCLS32 0xfff5 /* minimum FAT32 clusters */
80 1.1.2.2 yamt #define MAXCLS12 0xff4 /* maximum FAT12 clusters */
81 1.1.2.2 yamt #define MAXCLS16 0xfff4 /* maximum FAT16 clusters */
82 1.1.2.2 yamt #define MAXCLS32 0xffffff4 /* maximum FAT32 clusters */
83 1.1.2.2 yamt
84 1.1.2.2 yamt #define mincls(fat_type) ((fat_type) == 12 ? MINCLS12 : \
85 1.1.2.2 yamt (fat_type) == 16 ? MINCLS16 : \
86 1.1.2.2 yamt MINCLS32)
87 1.1.2.2 yamt
88 1.1.2.2 yamt #define maxcls(fat_type) ((fat_type) == 12 ? MAXCLS12 : \
89 1.1.2.2 yamt (fat_type) == 16 ? MAXCLS16 : \
90 1.1.2.2 yamt MAXCLS32)
91 1.1.2.2 yamt
92 1.1.2.2 yamt #define mk1(p, x) \
93 1.1.2.2 yamt (p) = (u_int8_t)(x)
94 1.1.2.2 yamt
95 1.1.2.2 yamt #define mk2(p, x) \
96 1.1.2.2 yamt (p)[0] = (u_int8_t)(x), \
97 1.1.2.2 yamt (p)[1] = (u_int8_t)((x) >> 010)
98 1.1.2.2 yamt
99 1.1.2.2 yamt #define mk4(p, x) \
100 1.1.2.2 yamt (p)[0] = (u_int8_t)(x), \
101 1.1.2.2 yamt (p)[1] = (u_int8_t)((x) >> 010), \
102 1.1.2.2 yamt (p)[2] = (u_int8_t)((x) >> 020), \
103 1.1.2.2 yamt (p)[3] = (u_int8_t)((x) >> 030)
104 1.1.2.2 yamt
105 1.1.2.2 yamt struct bs {
106 1.1.2.2 yamt u_int8_t jmp[3]; /* bootstrap entry point */
107 1.1.2.2 yamt u_int8_t oem[8]; /* OEM name and version */
108 1.1.2.2 yamt };
109 1.1.2.2 yamt
110 1.1.2.2 yamt struct bsbpb {
111 1.1.2.2 yamt u_int8_t bps[2]; /* bytes per sector */
112 1.1.2.2 yamt u_int8_t spc; /* sectors per cluster */
113 1.1.2.2 yamt u_int8_t res[2]; /* reserved sectors */
114 1.1.2.2 yamt u_int8_t nft; /* number of FATs */
115 1.1.2.2 yamt u_int8_t rde[2]; /* root directory entries */
116 1.1.2.2 yamt u_int8_t sec[2]; /* total sectors */
117 1.1.2.2 yamt u_int8_t mid; /* media descriptor */
118 1.1.2.2 yamt u_int8_t spf[2]; /* sectors per FAT */
119 1.1.2.2 yamt u_int8_t spt[2]; /* sectors per track */
120 1.1.2.2 yamt u_int8_t hds[2]; /* drive heads */
121 1.1.2.2 yamt u_int8_t hid[4]; /* hidden sectors */
122 1.1.2.2 yamt u_int8_t bsec[4]; /* big total sectors */
123 1.1.2.2 yamt };
124 1.1.2.2 yamt
125 1.1.2.2 yamt struct bsxbpb {
126 1.1.2.2 yamt u_int8_t bspf[4]; /* big sectors per FAT */
127 1.1.2.2 yamt u_int8_t xflg[2]; /* FAT control flags */
128 1.1.2.2 yamt u_int8_t vers[2]; /* file system version */
129 1.1.2.2 yamt u_int8_t rdcl[4]; /* root directory start cluster */
130 1.1.2.2 yamt u_int8_t infs[2]; /* file system info sector */
131 1.1.2.2 yamt u_int8_t bkbs[2]; /* backup boot sector */
132 1.1.2.2 yamt u_int8_t rsvd[12]; /* reserved */
133 1.1.2.2 yamt };
134 1.1.2.2 yamt
135 1.1.2.2 yamt struct bsx {
136 1.1.2.2 yamt u_int8_t drv; /* drive number */
137 1.1.2.2 yamt u_int8_t rsvd; /* reserved */
138 1.1.2.2 yamt u_int8_t sig; /* extended boot signature */
139 1.1.2.2 yamt u_int8_t volid[4]; /* volume ID number */
140 1.1.2.2 yamt u_int8_t label[11]; /* volume label */
141 1.1.2.2 yamt u_int8_t type[8]; /* file system type */
142 1.1.2.2 yamt };
143 1.1.2.2 yamt
144 1.1.2.2 yamt struct de {
145 1.1.2.2 yamt u_int8_t namext[11]; /* name and extension */
146 1.1.2.2 yamt u_int8_t attr; /* attributes */
147 1.1.2.2 yamt u_int8_t rsvd[10]; /* reserved */
148 1.1.2.2 yamt u_int8_t time[2]; /* creation time */
149 1.1.2.2 yamt u_int8_t date[2]; /* creation date */
150 1.1.2.2 yamt u_int8_t clus[2]; /* starting cluster */
151 1.1.2.2 yamt u_int8_t size[4]; /* size */
152 1.1.2.2 yamt };
153 1.1.2.2 yamt
154 1.1.2.2 yamt struct bpb {
155 1.1.2.2 yamt u_int bps; /* bytes per sector */
156 1.1.2.2 yamt u_int spc; /* sectors per cluster */
157 1.1.2.2 yamt u_int res; /* reserved sectors */
158 1.1.2.2 yamt u_int nft; /* number of FATs */
159 1.1.2.2 yamt u_int rde; /* root directory entries */
160 1.1.2.2 yamt u_int sec; /* total sectors */
161 1.1.2.2 yamt u_int mid; /* media descriptor */
162 1.1.2.2 yamt u_int spf; /* sectors per FAT */
163 1.1.2.2 yamt u_int spt; /* sectors per track */
164 1.1.2.2 yamt u_int hds; /* drive heads */
165 1.1.2.2 yamt u_int hid; /* hidden sectors */
166 1.1.2.2 yamt u_int bsec; /* big total sectors */
167 1.1.2.2 yamt u_int bspf; /* big sectors per FAT */
168 1.1.2.2 yamt u_int rdcl; /* root directory start cluster */
169 1.1.2.2 yamt u_int infs; /* file system info sector */
170 1.1.2.2 yamt u_int bkbs; /* backup boot sector */
171 1.1.2.2 yamt };
172 1.1.2.2 yamt
173 1.1.2.2 yamt #define INIT(a, b, c, d, e, f, g, h, i, j) \
174 1.1.2.2 yamt { .bps = a, .spc = b, .res = c, .nft = d, .rde = e, \
175 1.1.2.2 yamt .sec = f, .mid = g, .spf = h, .spt = i, .hds = j, }
176 1.1.2.2 yamt static struct {
177 1.1.2.2 yamt const char *name;
178 1.1.2.2 yamt struct bpb bpb;
179 1.1.2.2 yamt } stdfmt[] = {
180 1.1.2.2 yamt {"160", INIT(512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1)},
181 1.1.2.2 yamt {"180", INIT(512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1)},
182 1.1.2.2 yamt {"320", INIT(512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2)},
183 1.1.2.2 yamt {"360", INIT(512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2)},
184 1.1.2.2 yamt {"640", INIT(512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2)},
185 1.1.2.2 yamt {"720", INIT(512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2)},
186 1.1.2.2 yamt {"1200", INIT(512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2)},
187 1.1.2.2 yamt {"1232", INIT(1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2)},
188 1.1.2.2 yamt {"1440", INIT(512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2)},
189 1.1.2.2 yamt {"2880", INIT(512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2)}
190 1.1.2.2 yamt };
191 1.1.2.2 yamt
192 1.1.2.2 yamt static u_int8_t bootcode[] = {
193 1.1.2.2 yamt 0xfa, /* cli */
194 1.1.2.2 yamt 0x31, 0xc0, /* xor ax,ax */
195 1.1.2.2 yamt 0x8e, 0xd0, /* mov ss,ax */
196 1.1.2.2 yamt 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
197 1.1.2.2 yamt 0xfb, /* sti */
198 1.1.2.2 yamt 0x8e, 0xd8, /* mov ds,ax */
199 1.1.2.2 yamt 0xe8, 0x00, 0x00, /* call $ + 3 */
200 1.1.2.2 yamt 0x5e, /* pop si */
201 1.1.2.2 yamt 0x83, 0xc6, 0x19, /* add si,+19h */
202 1.1.2.2 yamt 0xbb, 0x07, 0x00, /* mov bx,0007h */
203 1.1.2.2 yamt 0xfc, /* cld */
204 1.1.2.2 yamt 0xac, /* lodsb */
205 1.1.2.2 yamt 0x84, 0xc0, /* test al,al */
206 1.1.2.2 yamt 0x74, 0x06, /* jz $ + 8 */
207 1.1.2.2 yamt 0xb4, 0x0e, /* mov ah,0eh */
208 1.1.2.2 yamt 0xcd, 0x10, /* int 10h */
209 1.1.2.2 yamt 0xeb, 0xf5, /* jmp $ - 9 */
210 1.1.2.2 yamt 0x30, 0xe4, /* xor ah,ah */
211 1.1.2.2 yamt 0xcd, 0x16, /* int 16h */
212 1.1.2.2 yamt 0xcd, 0x19, /* int 19h */
213 1.1.2.2 yamt 0x0d, 0x0a,
214 1.1.2.2 yamt 'N', 'o', 'n', '-', 's', 'y', 's', 't',
215 1.1.2.2 yamt 'e', 'm', ' ', 'd', 'i', 's', 'k',
216 1.1.2.2 yamt 0x0d, 0x0a,
217 1.1.2.2 yamt 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
218 1.1.2.2 yamt 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
219 1.1.2.2 yamt ' ', 'r', 'e', 'b', 'o', 'o', 't',
220 1.1.2.2 yamt 0x0d, 0x0a,
221 1.1.2.2 yamt 0
222 1.1.2.2 yamt };
223 1.1.2.2 yamt
224 1.1.2.2 yamt static int got_siginfo = 0; /* received a SIGINFO */
225 1.1.2.2 yamt
226 1.1.2.2 yamt static int check_mounted(const char *, mode_t);
227 1.1.2.2 yamt static int getstdfmt(const char *, struct bpb *);
228 1.1.2.2 yamt static int getbpbinfo(int, const char *, const char *, int, struct bpb *, int);
229 1.1.2.2 yamt static void print_bpb(struct bpb *);
230 1.1.2.2 yamt static int ckgeom(const char *, u_int, const char *);
231 1.1.2.2 yamt static int oklabel(const char *);
232 1.1.2.2 yamt static void mklabel(u_int8_t *, const char *);
233 1.1.2.2 yamt static void setstr(u_int8_t *, const char *, size_t);
234 1.1.2.2 yamt static void infohandler(int sig);
235 1.1.2.2 yamt
236 1.1.2.2 yamt int
237 1.1.2.2 yamt mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
238 1.1.2.2 yamt {
239 1.1.2.2 yamt char buf[MAXPATHLEN];
240 1.1.2.2 yamt struct stat sb;
241 1.1.2.2 yamt struct timeval tv;
242 1.1.2.2 yamt struct bpb bpb;
243 1.1.2.2 yamt struct tm *tm;
244 1.1.2.2 yamt struct bs *bs;
245 1.1.2.2 yamt struct bsbpb *bsbpb;
246 1.1.2.2 yamt struct bsxbpb *bsxbpb;
247 1.1.2.2 yamt struct bsx *bsx;
248 1.1.2.2 yamt struct de *de;
249 1.1.2.2 yamt u_int8_t *img;
250 1.1.2.2 yamt const char *bname;
251 1.1.2.2 yamt ssize_t n;
252 1.1.2.2 yamt time_t now;
253 1.1.2.2 yamt u_int bss, rds, cls, dir, lsn, x, x1, x2;
254 1.1.2.2 yamt int ch, fd, fd1;
255 1.1.2.2 yamt struct msdos_options o = *op;
256 1.1.2.2 yamt
257 1.1.2.2 yamt if (o.block_size && o.sectors_per_cluster) {
258 1.1.2.2 yamt warnx("Cannot specify both block size and sectors per cluster");
259 1.1.2.2 yamt return -1;
260 1.1.2.2 yamt }
261 1.1.2.2 yamt if (strlen(o.OEM_string) > 8) {
262 1.1.2.2 yamt warnx("%s: bad OEM string", o.OEM_string);
263 1.1.2.2 yamt return -1;
264 1.1.2.2 yamt }
265 1.1.2.2 yamt if (o.create_size) {
266 1.1.2.2 yamt off_t pos;
267 1.1.2.2 yamt if (o.no_create) {
268 1.1.2.2 yamt warnx("create (-C) is incompatible with -N");
269 1.1.2.2 yamt return -1;
270 1.1.2.2 yamt }
271 1.1.2.2 yamt fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
272 1.1.2.2 yamt if (fd == -1) {
273 1.1.2.2 yamt warnx("failed to create %s", fname);
274 1.1.2.2 yamt return -1;
275 1.1.2.2 yamt }
276 1.1.2.2 yamt pos = lseek(fd, o.create_size - 1, SEEK_SET);
277 1.1.2.2 yamt if (write(fd, "\0", 1) != 1) {
278 1.1.2.2 yamt warn("failed to set file size");
279 1.1.2.2 yamt return -1;
280 1.1.2.2 yamt }
281 1.1.2.2 yamt pos = lseek(fd, 0, SEEK_SET);
282 1.1.2.2 yamt } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1 ||
283 1.1.2.2 yamt fstat(fd, &sb))
284 1.1.2.2 yamt warn("%s", fname);
285 1.1.2.2 yamt return -1;
286 1.1.2.2 yamt if (!o.no_create)
287 1.1.2.2 yamt if (check_mounted(fname, sb.st_mode) == -1)
288 1.1.2.2 yamt return -1;
289 1.1.2.2 yamt if (!S_ISCHR(sb.st_mode) && !o.create_size) {
290 1.1.2.2 yamt warnx("warning, %s is not a character device", fname);
291 1.1.2.2 yamt return -1;
292 1.1.2.2 yamt }
293 1.1.2.2 yamt if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
294 1.1.2.2 yamt warnx("cannot seek to %jd", (intmax_t)o.offset);
295 1.1.2.2 yamt return -1;
296 1.1.2.2 yamt }
297 1.1.2.2 yamt memset(&bpb, 0, sizeof(bpb));
298 1.1.2.2 yamt if (o.floppy) {
299 1.1.2.2 yamt if (getstdfmt(o.floppy, &bpb) == -1)
300 1.1.2.2 yamt return -1;
301 1.1.2.2 yamt bpb.bsec = bpb.sec;
302 1.1.2.2 yamt bpb.sec = 0;
303 1.1.2.2 yamt bpb.bspf = bpb.spf;
304 1.1.2.2 yamt bpb.spf = 0;
305 1.1.2.2 yamt }
306 1.1.2.2 yamt if (o.drive_heads)
307 1.1.2.2 yamt bpb.hds = o.drive_heads;
308 1.1.2.2 yamt if (o.sectors_per_track)
309 1.1.2.2 yamt bpb.spt = o.sectors_per_track;
310 1.1.2.2 yamt if (o.bytes_per_sector)
311 1.1.2.2 yamt bpb.bps = o.bytes_per_sector;
312 1.1.2.2 yamt if (o.size)
313 1.1.2.2 yamt bpb.bsec = o.size;
314 1.1.2.2 yamt if (o.hidden_sectors_set)
315 1.1.2.2 yamt bpb.hid = o.hidden_sectors;
316 1.1.2.2 yamt if (!(o.floppy || (o.drive_heads && o.sectors_per_track &&
317 1.1.2.2 yamt o.bytes_per_sector && o.size && o.hidden_sectors_set))) {
318 1.1.2.2 yamt if (getbpbinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb,
319 1.1.2.2 yamt o.create_size != 0) == -1)
320 1.1.2.2 yamt return -1;
321 1.1.2.2 yamt bpb.bsec -= (o.offset / bpb.bps);
322 1.1.2.2 yamt if (bpb.spc == 0) { /* set defaults */
323 1.1.2.2 yamt if (bpb.bsec <= 6000) /* about 3MB -> 512 bytes */
324 1.1.2.2 yamt bpb.spc = 1;
325 1.1.2.2 yamt else if (bpb.bsec <= (1<<17)) /* 64M -> 4k */
326 1.1.2.2 yamt bpb.spc = 8;
327 1.1.2.2 yamt else if (bpb.bsec <= (1<<19)) /* 256M -> 8k */
328 1.1.2.2 yamt bpb.spc = 16;
329 1.1.2.2 yamt else if (bpb.bsec <= (1<<21)) /* 1G -> 16k */
330 1.1.2.2 yamt bpb.spc = 32;
331 1.1.2.2 yamt else
332 1.1.2.2 yamt bpb.spc = 64; /* otherwise 32k */
333 1.1.2.2 yamt }
334 1.1.2.2 yamt }
335 1.1.2.2 yamt
336 1.1.2.2 yamt if (!oklabel(o.volume_label)) {
337 1.1.2.2 yamt warnx("%s: bad volume label", o.volume_label);
338 1.1.2.2 yamt return -1;
339 1.1.2.2 yamt }
340 1.1.2.2 yamt
341 1.1.2.2 yamt switch (o.fat_type) {
342 1.1.2.2 yamt case 0:
343 1.1.2.2 yamt if (o.floppy)
344 1.1.2.2 yamt o.fat_type = 12;
345 1.1.2.2 yamt else if (!o.directory_entries && (o.info_sector || o.backup_sector))
346 1.1.2.2 yamt o.fat_type = 32;
347 1.1.2.2 yamt break;
348 1.1.2.2 yamt case 12:
349 1.1.2.2 yamt case 16:
350 1.1.2.2 yamt if (o.info_sector) {
351 1.1.2.2 yamt warnx("Cannot specify info sector with FAT%u", o.fat_type);
352 1.1.2.2 yamt return -1;
353 1.1.2.2 yamt }
354 1.1.2.2 yamt if (o.backup_sector) {
355 1.1.2.2 yamt warnx("Cannot specify backup sector with FAT%u", o.fat_type);
356 1.1.2.2 yamt return -1;
357 1.1.2.2 yamt }
358 1.1.2.2 yamt break;
359 1.1.2.2 yamt case 32:
360 1.1.2.2 yamt if (o.directory_entries) {
361 1.1.2.2 yamt warnx("Cannot specify directory entries with FAT32");
362 1.1.2.2 yamt return -1;
363 1.1.2.2 yamt }
364 1.1.2.2 yamt break;
365 1.1.2.2 yamt default:
366 1.1.2.2 yamt warnx("%d: bad FAT type", o.fat_type);
367 1.1.2.2 yamt return -1;
368 1.1.2.2 yamt }
369 1.1.2.2 yamt if (!powerof2(bpb.bps)) {
370 1.1.2.2 yamt warnx("bytes/sector (%u) is not a power of 2", bpb.bps);
371 1.1.2.2 yamt return -1;
372 1.1.2.2 yamt }
373 1.1.2.2 yamt if (bpb.bps < MINBPS) {
374 1.1.2.2 yamt warnx("bytes/sector (%u) is too small; minimum is %u",
375 1.1.2.2 yamt bpb.bps, MINBPS);
376 1.1.2.2 yamt return -1;
377 1.1.2.2 yamt }
378 1.1.2.2 yamt
379 1.1.2.2 yamt if (o.floppy && o.fat_type == 32)
380 1.1.2.2 yamt bpb.rde = 0;
381 1.1.2.2 yamt if (o.block_size) {
382 1.1.2.2 yamt if (!powerof2(o.block_size)) {
383 1.1.2.2 yamt warnx("block size (%u) is not a power of 2", o.block_size);
384 1.1.2.2 yamt return -1;
385 1.1.2.2 yamt }
386 1.1.2.2 yamt if (o.block_size < bpb.bps) {
387 1.1.2.2 yamt warnx("block size (%u) is too small; minimum is %u",
388 1.1.2.2 yamt o.block_size, bpb.bps);
389 1.1.2.2 yamt return -1;
390 1.1.2.2 yamt }
391 1.1.2.2 yamt if (o.block_size > bpb.bps * MAXSPC) {
392 1.1.2.2 yamt warnx("block size (%u) is too large; maximum is %u",
393 1.1.2.2 yamt o.block_size, bpb.bps * MAXSPC);
394 1.1.2.2 yamt return -1;
395 1.1.2.2 yamt }
396 1.1.2.2 yamt bpb.spc = o.block_size / bpb.bps;
397 1.1.2.2 yamt }
398 1.1.2.2 yamt if (o.sectors_per_cluster) {
399 1.1.2.2 yamt if (!powerof2(o.sectors_per_cluster)) {
400 1.1.2.2 yamt warnx("sectors/cluster (%u) is not a power of 2",
401 1.1.2.2 yamt o.sectors_per_cluster);
402 1.1.2.2 yamt return -1;
403 1.1.2.2 yamt }
404 1.1.2.2 yamt bpb.spc = o.sectors_per_cluster;
405 1.1.2.2 yamt }
406 1.1.2.2 yamt if (o.reserved_sectors)
407 1.1.2.2 yamt bpb.res = o.reserved_sectors;
408 1.1.2.2 yamt if (o.num_FAT) {
409 1.1.2.2 yamt if (o.num_FAT > MAXNFT) {
410 1.1.2.2 yamt warnx("number of FATs (%u) is too large; maximum is %u",
411 1.1.2.2 yamt o.num_FAT, MAXNFT);
412 1.1.2.2 yamt return -1;
413 1.1.2.2 yamt }
414 1.1.2.2 yamt bpb.nft = o.num_FAT;
415 1.1.2.2 yamt }
416 1.1.2.2 yamt if (o.directory_entries)
417 1.1.2.2 yamt bpb.rde = o.directory_entries;
418 1.1.2.2 yamt if (o.media_descriptor_set) {
419 1.1.2.2 yamt if (o.media_descriptor < 0xf0) {
420 1.1.2.2 yamt warnx("illegal media descriptor (%#x)", o.media_descriptor);
421 1.1.2.2 yamt return -1;
422 1.1.2.2 yamt }
423 1.1.2.2 yamt bpb.mid = o.media_descriptor;
424 1.1.2.2 yamt }
425 1.1.2.2 yamt if (o.sectors_per_fat)
426 1.1.2.2 yamt bpb.bspf = o.sectors_per_fat;
427 1.1.2.2 yamt if (o.info_sector)
428 1.1.2.2 yamt bpb.infs = o.info_sector;
429 1.1.2.2 yamt if (o.backup_sector)
430 1.1.2.2 yamt bpb.bkbs = o.backup_sector;
431 1.1.2.2 yamt bss = 1;
432 1.1.2.2 yamt bname = NULL;
433 1.1.2.2 yamt fd1 = -1;
434 1.1.2.2 yamt if (o.bootstrap) {
435 1.1.2.2 yamt bname = o.bootstrap;
436 1.1.2.2 yamt if (!strchr(bname, '/')) {
437 1.1.2.2 yamt snprintf(buf, sizeof(buf), "/boot/%s", bname);
438 1.1.2.2 yamt if (!(bname = strdup(buf)))
439 1.1.2.2 yamt err(1, NULL);
440 1.1.2.2 yamt }
441 1.1.2.2 yamt if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) {
442 1.1.2.2 yamt warn("%s", bname);
443 1.1.2.2 yamt return -1;
444 1.1.2.2 yamt }
445 1.1.2.2 yamt if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
446 1.1.2.2 yamt sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16) {
447 1.1.2.2 yamt warnx("%s: inappropriate file type or format", bname);
448 1.1.2.2 yamt return -1;
449 1.1.2.2 yamt }
450 1.1.2.2 yamt bss = sb.st_size / bpb.bps;
451 1.1.2.2 yamt }
452 1.1.2.2 yamt if (!bpb.nft)
453 1.1.2.2 yamt bpb.nft = 2;
454 1.1.2.2 yamt if (!o.fat_type) {
455 1.1.2.2 yamt if (bpb.bsec < (bpb.res ? bpb.res : bss) +
456 1.1.2.2 yamt howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
457 1.1.2.2 yamt ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
458 1.1.2.2 yamt bpb.nft +
459 1.1.2.2 yamt howmany(bpb.rde ? bpb.rde : DEFRDE,
460 1.1.2.2 yamt bpb.bps / sizeof(struct de)) +
461 1.1.2.2 yamt (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
462 1.1.2.2 yamt (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
463 1.1.2.2 yamt o.fat_type = 12;
464 1.1.2.2 yamt else if (bpb.rde || bpb.bsec <
465 1.1.2.2 yamt (bpb.res ? bpb.res : bss) +
466 1.1.2.2 yamt howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
467 1.1.2.2 yamt howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
468 1.1.2.2 yamt (MAXCLS16 + 1) *
469 1.1.2.2 yamt (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
470 1.1.2.2 yamt o.fat_type = 16;
471 1.1.2.2 yamt else
472 1.1.2.2 yamt o.fat_type = 32;
473 1.1.2.2 yamt }
474 1.1.2.2 yamt x = bss;
475 1.1.2.2 yamt if (o.fat_type == 32) {
476 1.1.2.2 yamt if (!bpb.infs) {
477 1.1.2.2 yamt if (x == MAXU16 || x == bpb.bkbs) {
478 1.1.2.2 yamt warnx("no room for info sector");
479 1.1.2.2 yamt return -1;
480 1.1.2.2 yamt }
481 1.1.2.2 yamt bpb.infs = x;
482 1.1.2.2 yamt }
483 1.1.2.2 yamt if (bpb.infs != MAXU16 && x <= bpb.infs)
484 1.1.2.2 yamt x = bpb.infs + 1;
485 1.1.2.2 yamt if (!bpb.bkbs) {
486 1.1.2.2 yamt if (x == MAXU16) {
487 1.1.2.2 yamt warnx("no room for backup sector");
488 1.1.2.2 yamt return -1;
489 1.1.2.2 yamt }
490 1.1.2.2 yamt bpb.bkbs = x;
491 1.1.2.2 yamt } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs) {
492 1.1.2.2 yamt warnx("backup sector would overwrite info sector");
493 1.1.2.2 yamt return -1;
494 1.1.2.2 yamt }
495 1.1.2.2 yamt if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
496 1.1.2.2 yamt x = bpb.bkbs + 1;
497 1.1.2.2 yamt }
498 1.1.2.2 yamt if (!bpb.res)
499 1.1.2.2 yamt bpb.res = o.fat_type == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
500 1.1.2.2 yamt else if (bpb.res < x) {
501 1.1.2.2 yamt warnx("too few reserved sectors (need %d have %d)", x, bpb.res);
502 1.1.2.2 yamt return -1;
503 1.1.2.2 yamt }
504 1.1.2.2 yamt if (o.fat_type != 32 && !bpb.rde)
505 1.1.2.2 yamt bpb.rde = DEFRDE;
506 1.1.2.2 yamt rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
507 1.1.2.2 yamt if (!bpb.spc)
508 1.1.2.2 yamt for (bpb.spc = howmany(o.fat_type == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
509 1.1.2.2 yamt bpb.spc < MAXSPC &&
510 1.1.2.2 yamt bpb.res +
511 1.1.2.2 yamt howmany((RESFTE + maxcls(o.fat_type)) * (o.fat_type / BPN),
512 1.1.2.2 yamt bpb.bps * NPB) * bpb.nft +
513 1.1.2.2 yamt rds +
514 1.1.2.2 yamt (u_int64_t)(maxcls(o.fat_type) + 1) * bpb.spc <= bpb.bsec;
515 1.1.2.2 yamt bpb.spc <<= 1);
516 1.1.2.2 yamt if (o.fat_type != 32 && bpb.bspf > MAXU16) {
517 1.1.2.2 yamt warnx("too many sectors/FAT for FAT12/16");
518 1.1.2.2 yamt return -1;
519 1.1.2.2 yamt }
520 1.1.2.2 yamt x1 = bpb.res + rds;
521 1.1.2.2 yamt x = bpb.bspf ? bpb.bspf : 1;
522 1.1.2.2 yamt if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec) {
523 1.1.2.2 yamt warnx("meta data exceeds file system size");
524 1.1.2.2 yamt return -1;
525 1.1.2.2 yamt }
526 1.1.2.2 yamt x1 += x * bpb.nft;
527 1.1.2.2 yamt x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
528 1.1.2.2 yamt (bpb.spc * bpb.bps * NPB + o.fat_type / BPN * bpb.nft);
529 1.1.2.2 yamt x2 = howmany((RESFTE + MIN(x, maxcls(o.fat_type))) * (o.fat_type / BPN),
530 1.1.2.2 yamt bpb.bps * NPB);
531 1.1.2.2 yamt if (!bpb.bspf) {
532 1.1.2.2 yamt bpb.bspf = x2;
533 1.1.2.2 yamt x1 += (bpb.bspf - 1) * bpb.nft;
534 1.1.2.2 yamt }
535 1.1.2.2 yamt cls = (bpb.bsec - x1) / bpb.spc;
536 1.1.2.2 yamt x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (o.fat_type / BPN) - RESFTE;
537 1.1.2.2 yamt if (cls > x)
538 1.1.2.2 yamt cls = x;
539 1.1.2.2 yamt if (bpb.bspf < x2) {
540 1.1.2.2 yamt warnx("warning: sectors/FAT limits file system to %u clusters",
541 1.1.2.2 yamt cls);
542 1.1.2.2 yamt return -1;
543 1.1.2.2 yamt }
544 1.1.2.2 yamt if (cls < mincls(o.fat_type)) {
545 1.1.2.2 yamt warnx("%u clusters too few clusters for FAT%u, need %u", cls,
546 1.1.2.2 yamt o.fat_type, mincls(o.fat_type));
547 1.1.2.2 yamt return -1;
548 1.1.2.2 yamt }
549 1.1.2.2 yamt if (cls > maxcls(o.fat_type)) {
550 1.1.2.2 yamt cls = maxcls(o.fat_type);
551 1.1.2.2 yamt bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
552 1.1.2.2 yamt warnx("warning: FAT type limits file system to %u sectors",
553 1.1.2.2 yamt bpb.bsec);
554 1.1.2.2 yamt return -1;
555 1.1.2.2 yamt }
556 1.1.2.2 yamt printf("%s: %u sector%s in %u FAT%u cluster%s "
557 1.1.2.2 yamt "(%u bytes/cluster)\n", fname, cls * bpb.spc,
558 1.1.2.2 yamt cls * bpb.spc == 1 ? "" : "s", cls, o.fat_type,
559 1.1.2.2 yamt cls == 1 ? "" : "s", bpb.bps * bpb.spc);
560 1.1.2.2 yamt if (!bpb.mid)
561 1.1.2.2 yamt bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
562 1.1.2.2 yamt if (o.fat_type == 32)
563 1.1.2.2 yamt bpb.rdcl = RESFTE;
564 1.1.2.2 yamt if (bpb.hid + bpb.bsec <= MAXU16) {
565 1.1.2.2 yamt bpb.sec = bpb.bsec;
566 1.1.2.2 yamt bpb.bsec = 0;
567 1.1.2.2 yamt }
568 1.1.2.2 yamt if (o.fat_type != 32) {
569 1.1.2.2 yamt bpb.spf = bpb.bspf;
570 1.1.2.2 yamt bpb.bspf = 0;
571 1.1.2.2 yamt }
572 1.1.2.2 yamt ch = 0;
573 1.1.2.2 yamt if (o.fat_type == 12)
574 1.1.2.2 yamt ch = 1; /* 001 Primary DOS with 12 bit FAT */
575 1.1.2.2 yamt else if (o.fat_type == 16) {
576 1.1.2.2 yamt if (bpb.bsec == 0)
577 1.1.2.2 yamt ch = 4; /* 004 Primary DOS with 16 bit FAT <32M */
578 1.1.2.2 yamt else
579 1.1.2.2 yamt ch = 6; /* 006 Primary 'big' DOS, 16-bit FAT (> 32MB) */
580 1.1.2.2 yamt /*
581 1.1.2.2 yamt * XXX: what about:
582 1.1.2.2 yamt * 014 DOS (16-bit FAT) - LBA
583 1.1.2.2 yamt * ?
584 1.1.2.2 yamt */
585 1.1.2.2 yamt } else if (o.fat_type == 32) {
586 1.1.2.2 yamt ch = 11; /* 011 Primary DOS with 32 bit FAT */
587 1.1.2.2 yamt /*
588 1.1.2.2 yamt * XXX: what about:
589 1.1.2.2 yamt * 012 Primary DOS with 32 bit FAT - LBA
590 1.1.2.2 yamt * ?
591 1.1.2.2 yamt */
592 1.1.2.2 yamt }
593 1.1.2.2 yamt if (ch != 0)
594 1.1.2.2 yamt printf("MBR type: %d\n", ch);
595 1.1.2.2 yamt print_bpb(&bpb);
596 1.1.2.2 yamt if (!o.no_create) {
597 1.1.2.2 yamt gettimeofday(&tv, NULL);
598 1.1.2.2 yamt now = tv.tv_sec;
599 1.1.2.2 yamt tm = localtime(&now);
600 1.1.2.2 yamt if (!(img = malloc(bpb.bps)))
601 1.1.2.2 yamt err(1, NULL);
602 1.1.2.2 yamt dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
603 1.1.2.2 yamt signal(SIGINFO, infohandler);
604 1.1.2.2 yamt for (lsn = 0; lsn < dir + (o.fat_type == 32 ? bpb.spc : rds); lsn++) {
605 1.1.2.2 yamt if (got_siginfo) {
606 1.1.2.2 yamt fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
607 1.1.2.2 yamt fname,lsn,(dir + (o.fat_type == 32 ? bpb.spc : rds)),
608 1.1.2.2 yamt (lsn*100)/(dir + (o.fat_type == 32 ? bpb.spc : rds)));
609 1.1.2.2 yamt got_siginfo = 0;
610 1.1.2.2 yamt }
611 1.1.2.2 yamt x = lsn;
612 1.1.2.2 yamt if (o.bootstrap &&
613 1.1.2.2 yamt o.fat_type == 32 && bpb.bkbs != MAXU16 &&
614 1.1.2.2 yamt bss <= bpb.bkbs && x >= bpb.bkbs) {
615 1.1.2.2 yamt x -= bpb.bkbs;
616 1.1.2.2 yamt if (!x && lseek(fd1, o.offset, SEEK_SET)) {
617 1.1.2.2 yamt warn("%s", bname);
618 1.1.2.2 yamt return -1;
619 1.1.2.2 yamt }
620 1.1.2.2 yamt }
621 1.1.2.2 yamt if (o.bootstrap && x < bss) {
622 1.1.2.2 yamt if ((n = read(fd1, img, bpb.bps)) == -1) {
623 1.1.2.2 yamt warn("%s", bname);
624 1.1.2.2 yamt return -1;
625 1.1.2.2 yamt }
626 1.1.2.2 yamt if ((size_t)n != bpb.bps) {
627 1.1.2.2 yamt warnx("%s: can't read sector %u", bname, x);
628 1.1.2.2 yamt return -1;
629 1.1.2.2 yamt }
630 1.1.2.2 yamt } else
631 1.1.2.2 yamt memset(img, 0, bpb.bps);
632 1.1.2.2 yamt if (!lsn ||
633 1.1.2.2 yamt (o.fat_type == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
634 1.1.2.2 yamt x1 = sizeof(struct bs);
635 1.1.2.2 yamt bsbpb = (struct bsbpb *)(img + x1);
636 1.1.2.2 yamt mk2(bsbpb->bps, bpb.bps);
637 1.1.2.2 yamt mk1(bsbpb->spc, bpb.spc);
638 1.1.2.2 yamt mk2(bsbpb->res, bpb.res);
639 1.1.2.2 yamt mk1(bsbpb->nft, bpb.nft);
640 1.1.2.2 yamt mk2(bsbpb->rde, bpb.rde);
641 1.1.2.2 yamt mk2(bsbpb->sec, bpb.sec);
642 1.1.2.2 yamt mk1(bsbpb->mid, bpb.mid);
643 1.1.2.2 yamt mk2(bsbpb->spf, bpb.spf);
644 1.1.2.2 yamt mk2(bsbpb->spt, bpb.spt);
645 1.1.2.2 yamt mk2(bsbpb->hds, bpb.hds);
646 1.1.2.2 yamt mk4(bsbpb->hid, bpb.hid);
647 1.1.2.2 yamt mk4(bsbpb->bsec, bpb.bsec);
648 1.1.2.2 yamt x1 += sizeof(struct bsbpb);
649 1.1.2.2 yamt if (o.fat_type == 32) {
650 1.1.2.2 yamt bsxbpb = (struct bsxbpb *)(img + x1);
651 1.1.2.2 yamt mk4(bsxbpb->bspf, bpb.bspf);
652 1.1.2.2 yamt mk2(bsxbpb->xflg, 0);
653 1.1.2.2 yamt mk2(bsxbpb->vers, 0);
654 1.1.2.2 yamt mk4(bsxbpb->rdcl, bpb.rdcl);
655 1.1.2.2 yamt mk2(bsxbpb->infs, bpb.infs);
656 1.1.2.2 yamt mk2(bsxbpb->bkbs, bpb.bkbs);
657 1.1.2.2 yamt x1 += sizeof(struct bsxbpb);
658 1.1.2.2 yamt }
659 1.1.2.2 yamt bsx = (struct bsx *)(img + x1);
660 1.1.2.2 yamt mk1(bsx->sig, 0x29);
661 1.1.2.2 yamt if (o.volume_id_set)
662 1.1.2.2 yamt x = o.volume_id;
663 1.1.2.2 yamt else
664 1.1.2.2 yamt x = (((u_int)(1 + tm->tm_mon) << 8 |
665 1.1.2.2 yamt (u_int)tm->tm_mday) +
666 1.1.2.2 yamt ((u_int)tm->tm_sec << 8 |
667 1.1.2.2 yamt (u_int)(tv.tv_usec / 10))) << 16 |
668 1.1.2.2 yamt ((u_int)(1900 + tm->tm_year) +
669 1.1.2.2 yamt ((u_int)tm->tm_hour << 8 |
670 1.1.2.2 yamt (u_int)tm->tm_min));
671 1.1.2.2 yamt mk4(bsx->volid, x);
672 1.1.2.2 yamt mklabel(bsx->label, o.volume_label ? o.volume_label : "NO NAME");
673 1.1.2.2 yamt snprintf(buf, sizeof(buf), "FAT%u", o.fat_type);
674 1.1.2.2 yamt setstr(bsx->type, buf, sizeof(bsx->type));
675 1.1.2.2 yamt if (!o.bootstrap) {
676 1.1.2.2 yamt x1 += sizeof(struct bsx);
677 1.1.2.2 yamt bs = (struct bs *)img;
678 1.1.2.2 yamt mk1(bs->jmp[0], 0xeb);
679 1.1.2.2 yamt mk1(bs->jmp[1], x1 - 2);
680 1.1.2.2 yamt mk1(bs->jmp[2], 0x90);
681 1.1.2.2 yamt setstr(bs->oem, o.OEM_string ? o.OEM_string : "NetBSD",
682 1.1.2.2 yamt sizeof(bs->oem));
683 1.1.2.2 yamt memcpy(img + x1, bootcode, sizeof(bootcode));
684 1.1.2.2 yamt mk2(img + MINBPS - 2, DOSMAGIC);
685 1.1.2.2 yamt }
686 1.1.2.2 yamt } else if (o.fat_type == 32 && bpb.infs != MAXU16 &&
687 1.1.2.2 yamt (lsn == bpb.infs ||
688 1.1.2.2 yamt (bpb.bkbs != MAXU16 &&
689 1.1.2.2 yamt lsn == bpb.bkbs + bpb.infs))) {
690 1.1.2.2 yamt mk4(img, 0x41615252);
691 1.1.2.2 yamt mk4(img + MINBPS - 28, 0x61417272);
692 1.1.2.2 yamt mk4(img + MINBPS - 24, 0xffffffff);
693 1.1.2.2 yamt mk4(img + MINBPS - 20, bpb.rdcl);
694 1.1.2.2 yamt mk2(img + MINBPS - 2, DOSMAGIC);
695 1.1.2.2 yamt } else if (lsn >= bpb.res && lsn < dir &&
696 1.1.2.2 yamt !((lsn - bpb.res) %
697 1.1.2.2 yamt (bpb.spf ? bpb.spf : bpb.bspf))) {
698 1.1.2.2 yamt mk1(img[0], bpb.mid);
699 1.1.2.2 yamt for (x = 1; x < o.fat_type * (o.fat_type == 32 ? 3U : 2U) / 8U; x++)
700 1.1.2.2 yamt mk1(img[x], o.fat_type == 32 && x % 4 == 3 ? 0x0f : 0xff);
701 1.1.2.2 yamt } else if (lsn == dir && o.volume_label) {
702 1.1.2.2 yamt de = (struct de *)img;
703 1.1.2.2 yamt mklabel(de->namext, o.volume_label);
704 1.1.2.2 yamt mk1(de->attr, 050);
705 1.1.2.2 yamt x = (u_int)tm->tm_hour << 11 |
706 1.1.2.2 yamt (u_int)tm->tm_min << 5 |
707 1.1.2.2 yamt (u_int)tm->tm_sec >> 1;
708 1.1.2.2 yamt mk2(de->time, x);
709 1.1.2.2 yamt x = (u_int)(tm->tm_year - 80) << 9 |
710 1.1.2.2 yamt (u_int)(tm->tm_mon + 1) << 5 |
711 1.1.2.2 yamt (u_int)tm->tm_mday;
712 1.1.2.2 yamt mk2(de->date, x);
713 1.1.2.2 yamt }
714 1.1.2.2 yamt if ((n = write(fd, img, bpb.bps)) == -1) {
715 1.1.2.2 yamt warn("%s", fname);
716 1.1.2.2 yamt return -1;
717 1.1.2.2 yamt }
718 1.1.2.2 yamt if ((size_t)n != bpb.bps) {
719 1.1.2.2 yamt warnx("%s: can't write sector %u", fname, lsn);
720 1.1.2.2 yamt return -1;
721 1.1.2.2 yamt }
722 1.1.2.2 yamt }
723 1.1.2.2 yamt }
724 1.1.2.2 yamt return 0;
725 1.1.2.2 yamt }
726 1.1.2.2 yamt
727 1.1.2.2 yamt /*
728 1.1.2.2 yamt * return -1 with error if file system is mounted.
729 1.1.2.2 yamt */
730 1.1.2.2 yamt static int
731 1.1.2.2 yamt check_mounted(const char *fname, mode_t mode)
732 1.1.2.2 yamt {
733 1.1.2.2 yamt struct statvfs *mp;
734 1.1.2.2 yamt const char *s1, *s2;
735 1.1.2.2 yamt size_t len;
736 1.1.2.2 yamt int n, r;
737 1.1.2.2 yamt
738 1.1.2.2 yamt if (!(n = getmntinfo(&mp, MNT_NOWAIT))) {
739 1.1.2.2 yamt warn("getmntinfo");
740 1.1.2.2 yamt return -1;
741 1.1.2.2 yamt }
742 1.1.2.2 yamt len = strlen(_PATH_DEV);
743 1.1.2.2 yamt s1 = fname;
744 1.1.2.2 yamt if (!strncmp(s1, _PATH_DEV, len))
745 1.1.2.2 yamt s1 += len;
746 1.1.2.2 yamt r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
747 1.1.2.2 yamt for (; n--; mp++) {
748 1.1.2.2 yamt s2 = mp->f_mntfromname;
749 1.1.2.2 yamt if (!strncmp(s2, _PATH_DEV, len))
750 1.1.2.2 yamt s2 += len;
751 1.1.2.2 yamt if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
752 1.1.2.2 yamt !strcmp(s1, s2)) {
753 1.1.2.2 yamt warnx("%s is mounted on %s", fname, mp->f_mntonname);
754 1.1.2.2 yamt return -1;
755 1.1.2.2 yamt }
756 1.1.2.2 yamt }
757 1.1.2.2 yamt return 0;
758 1.1.2.2 yamt }
759 1.1.2.2 yamt
760 1.1.2.2 yamt /*
761 1.1.2.2 yamt * Get a standard format.
762 1.1.2.2 yamt */
763 1.1.2.2 yamt static int
764 1.1.2.2 yamt getstdfmt(const char *fmt, struct bpb *bpb)
765 1.1.2.2 yamt {
766 1.1.2.2 yamt u_int x, i;
767 1.1.2.2 yamt
768 1.1.2.2 yamt x = sizeof(stdfmt) / sizeof(stdfmt[0]);
769 1.1.2.2 yamt for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
770 1.1.2.2 yamt if (i == x) {
771 1.1.2.2 yamt warnx("%s: unknown standard format", fmt);
772 1.1.2.2 yamt return -1;
773 1.1.2.2 yamt }
774 1.1.2.2 yamt *bpb = stdfmt[i].bpb;
775 1.1.2.2 yamt return 0;
776 1.1.2.2 yamt }
777 1.1.2.2 yamt
778 1.1.2.2 yamt /*
779 1.1.2.2 yamt * Get disk slice, partition, and geometry information.
780 1.1.2.2 yamt */
781 1.1.2.2 yamt static int
782 1.1.2.2 yamt getbpbinfo(int fd, const char *fname, const char *dtype, int iflag,
783 1.1.2.2 yamt struct bpb *bpb, int create)
784 1.1.2.2 yamt {
785 1.1.2.2 yamt struct disk_geom geo;
786 1.1.2.2 yamt struct dkwedge_info dkw;
787 1.1.2.2 yamt const char *s1, *s2;
788 1.1.2.2 yamt int part;
789 1.1.2.2 yamt int maxpartitions;
790 1.1.2.2 yamt
791 1.1.2.2 yamt part = -1;
792 1.1.2.2 yamt s1 = fname;
793 1.1.2.2 yamt if ((s2 = strrchr(s1, '/')))
794 1.1.2.2 yamt s1 = s2 + 1;
795 1.1.2.2 yamt for (s2 = s1; *s2 && !isdigit((unsigned char)*s2); s2++);
796 1.1.2.2 yamt if (!*s2 || s2 == s1)
797 1.1.2.2 yamt s2 = NULL;
798 1.1.2.2 yamt else
799 1.1.2.2 yamt while (isdigit((unsigned char)*++s2));
800 1.1.2.2 yamt s1 = s2;
801 1.1.2.2 yamt
802 1.1.2.2 yamt maxpartitions = getmaxpartitions();
803 1.1.2.2 yamt
804 1.1.2.2 yamt if (s2 && *s2 >= 'a' && *s2 <= 'a' + maxpartitions - 1) {
805 1.1.2.2 yamt part = *s2++ - 'a';
806 1.1.2.2 yamt }
807 1.1.2.2 yamt if (((part != -1) && ((!iflag && part != -1) || !bpb->bsec)) ||
808 1.1.2.2 yamt !bpb->bps || !bpb->spt || !bpb->hds) {
809 1.1.2.2 yamt if (create || getdiskinfo(fname, fd, NULL, &geo, &dkw) == -1) {
810 1.1.2.2 yamt struct stat st;
811 1.1.2.2 yamt
812 1.1.2.2 yamt if (fstat(fd, &st) == -1) {
813 1.1.2.2 yamt warnx("Can't get disk size for `%s'", fname);
814 1.1.2.2 yamt return -1;
815 1.1.2.2 yamt }
816 1.1.2.2 yamt /* create a fake geometry for a file image */
817 1.1.2.2 yamt geo.dg_secsize = 512;
818 1.1.2.2 yamt geo.dg_nsectors = 63;
819 1.1.2.2 yamt geo.dg_ntracks = 255;
820 1.1.2.2 yamt dkw.dkw_size = st.st_size / geo.dg_secsize;
821 1.1.2.2 yamt }
822 1.1.2.2 yamt if (!bpb->bps) {
823 1.1.2.2 yamt if (ckgeom(fname, geo.dg_secsize, "bytes/sector") == -1)
824 1.1.2.2 yamt return -1;
825 1.1.2.2 yamt bpb->bps = geo.dg_secsize;
826 1.1.2.2 yamt }
827 1.1.2.2 yamt
828 1.1.2.2 yamt if (geo.dg_nsectors > 63) {
829 1.1.2.2 yamt /*
830 1.1.2.2 yamt * The kernel doesn't accept BPB with spt > 63.
831 1.1.2.2 yamt * (see sys/fs/msdosfs/msdosfs_vfsops.c:msdosfs_mountfs())
832 1.1.2.2 yamt * If values taken from disklabel don't match these
833 1.1.2.2 yamt * restrictions, use popular BIOS default values instead.
834 1.1.2.2 yamt */
835 1.1.2.2 yamt geo.dg_nsectors = 63;
836 1.1.2.2 yamt }
837 1.1.2.2 yamt if (!bpb->spt) {
838 1.1.2.2 yamt if (ckgeom(fname, geo.dg_nsectors, "sectors/track") == -1)
839 1.1.2.2 yamt return -1;
840 1.1.2.2 yamt bpb->spt = geo.dg_nsectors;
841 1.1.2.2 yamt }
842 1.1.2.2 yamt if (!bpb->hds)
843 1.1.2.2 yamt if (ckgeom(fname, geo.dg_ntracks, "drive heads") == -1)
844 1.1.2.2 yamt return -1;
845 1.1.2.2 yamt bpb->hds = geo.dg_ntracks;
846 1.1.2.2 yamt if (!bpb->bsec)
847 1.1.2.2 yamt bpb->bsec = dkw.dkw_size;
848 1.1.2.2 yamt }
849 1.1.2.2 yamt return 0;
850 1.1.2.2 yamt }
851 1.1.2.2 yamt
852 1.1.2.2 yamt /*
853 1.1.2.2 yamt * Print out BPB values.
854 1.1.2.2 yamt */
855 1.1.2.2 yamt static void
856 1.1.2.2 yamt print_bpb(struct bpb *bpb)
857 1.1.2.2 yamt {
858 1.1.2.2 yamt printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
859 1.1.2.2 yamt bpb->nft);
860 1.1.2.2 yamt if (bpb->rde)
861 1.1.2.2 yamt printf(" rde=%u", bpb->rde);
862 1.1.2.2 yamt if (bpb->sec)
863 1.1.2.2 yamt printf(" sec=%u", bpb->sec);
864 1.1.2.2 yamt printf(" mid=%#x", bpb->mid);
865 1.1.2.2 yamt if (bpb->spf)
866 1.1.2.2 yamt printf(" spf=%u", bpb->spf);
867 1.1.2.2 yamt printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
868 1.1.2.2 yamt if (bpb->bsec)
869 1.1.2.2 yamt printf(" bsec=%u", bpb->bsec);
870 1.1.2.2 yamt if (!bpb->spf) {
871 1.1.2.2 yamt printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
872 1.1.2.2 yamt printf(" infs=");
873 1.1.2.2 yamt printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
874 1.1.2.2 yamt printf(" bkbs=");
875 1.1.2.2 yamt printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
876 1.1.2.2 yamt }
877 1.1.2.2 yamt printf("\n");
878 1.1.2.2 yamt }
879 1.1.2.2 yamt
880 1.1.2.2 yamt /*
881 1.1.2.2 yamt * Check a disk geometry value.
882 1.1.2.2 yamt */
883 1.1.2.2 yamt static int
884 1.1.2.2 yamt ckgeom(const char *fname, u_int val, const char *msg)
885 1.1.2.2 yamt {
886 1.1.2.2 yamt if (!val) {
887 1.1.2.2 yamt warnx("%s: no default %s", fname, msg);
888 1.1.2.2 yamt return -1;
889 1.1.2.2 yamt }
890 1.1.2.2 yamt if (val > MAXU16) {
891 1.1.2.2 yamt warnx("%s: illegal %s", fname, msg);
892 1.1.2.2 yamt return -1;
893 1.1.2.2 yamt }
894 1.1.2.2 yamt return 0;
895 1.1.2.2 yamt }
896 1.1.2.2 yamt /*
897 1.1.2.2 yamt * Check a volume label.
898 1.1.2.2 yamt */
899 1.1.2.2 yamt static int
900 1.1.2.2 yamt oklabel(const char *src)
901 1.1.2.2 yamt {
902 1.1.2.2 yamt int c, i;
903 1.1.2.2 yamt
904 1.1.2.2 yamt for (i = 0; i <= 11; i++) {
905 1.1.2.2 yamt c = (u_char)*src++;
906 1.1.2.2 yamt if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
907 1.1.2.2 yamt break;
908 1.1.2.2 yamt }
909 1.1.2.2 yamt return i && !c;
910 1.1.2.2 yamt }
911 1.1.2.2 yamt
912 1.1.2.2 yamt /*
913 1.1.2.2 yamt * Make a volume label.
914 1.1.2.2 yamt */
915 1.1.2.2 yamt static void
916 1.1.2.2 yamt mklabel(u_int8_t *dest, const char *src)
917 1.1.2.2 yamt {
918 1.1.2.2 yamt int c, i;
919 1.1.2.2 yamt
920 1.1.2.2 yamt for (i = 0; i < 11; i++) {
921 1.1.2.2 yamt c = *src ? toupper((unsigned char)*src++) : ' ';
922 1.1.2.2 yamt *dest++ = !i && c == '\xe5' ? 5 : c;
923 1.1.2.2 yamt }
924 1.1.2.2 yamt }
925 1.1.2.2 yamt
926 1.1.2.2 yamt /*
927 1.1.2.2 yamt * Copy string, padding with spaces.
928 1.1.2.2 yamt */
929 1.1.2.2 yamt static void
930 1.1.2.2 yamt setstr(u_int8_t *dest, const char *src, size_t len)
931 1.1.2.2 yamt {
932 1.1.2.2 yamt while (len--)
933 1.1.2.2 yamt *dest++ = *src ? *src++ : ' ';
934 1.1.2.2 yamt }
935 1.1.2.2 yamt
936 1.1.2.2 yamt static void
937 1.1.2.2 yamt infohandler(int sig)
938 1.1.2.2 yamt {
939 1.1.2.2 yamt got_siginfo = 1;
940 1.1.2.2 yamt }
941