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