Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1998 Robert Nordier |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS |
| 16 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY |
| 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 23 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 25 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef lint |
| 29 | static const char rcsid[] = |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 30 | "$FreeBSD$"; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 31 | #endif /* not lint */ |
| 32 | |
| 33 | #include <sys/param.h> |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 34 | #ifdef MAKEFS |
| 35 | /* In the makefs case we only want struct disklabel */ |
| 36 | #include <sys/disk/bsd.h> |
| 37 | #elif defined(__linux__) |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 38 | #include <linux/fs.h> |
| 39 | #include <linux/hdreg.h> |
| 40 | #include <sys/ioctl.h> |
| 41 | #elif defined(__APPLE__) |
| 42 | /* Nothing. */ |
| 43 | #else |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 44 | #include <sys/fdcio.h> |
| 45 | #include <sys/disk.h> |
| 46 | #include <sys/disklabel.h> |
| 47 | #include <sys/mount.h> |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 48 | #endif |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 49 | #include <sys/stat.h> |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 50 | #if __has_include(<sys/sysctl.h>) |
| 51 | #include <sys/sysctl.h> |
| 52 | #endif |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 53 | #include <sys/time.h> |
| 54 | |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 55 | #include <assert.h> |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 56 | #include <ctype.h> |
| 57 | #include <err.h> |
| 58 | #include <errno.h> |
| 59 | #include <fcntl.h> |
| 60 | #include <inttypes.h> |
| 61 | #include <paths.h> |
| 62 | #include <signal.h> |
| 63 | #include <stdio.h> |
| 64 | #include <stdlib.h> |
| 65 | #include <string.h> |
| 66 | #include <time.h> |
| 67 | #include <unistd.h> |
| 68 | |
| 69 | #include "mkfs_msdos.h" |
| 70 | |
| 71 | #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */ |
| 72 | #define BPN 4 /* bits per nibble */ |
| 73 | #define NPB 2 /* nibbles per byte */ |
| 74 | |
| 75 | #define DOSMAGIC 0xaa55 /* DOS magic number */ |
| 76 | #define MINBPS 512 /* minimum bytes per sector */ |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 77 | #define MAXBPS 4096 /* maximum bytes per sector */ |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 78 | #define MAXSPC 128 /* maximum sectors per cluster */ |
| 79 | #define MAXNFT 16 /* maximum number of FATs */ |
| 80 | #define DEFBLK 4096 /* default block size */ |
| 81 | #define DEFBLK16 2048 /* default block size FAT16 */ |
| 82 | #define DEFRDE 512 /* default root directory entries */ |
| 83 | #define RESFTE 2 /* reserved FAT entries */ |
| 84 | #define MINCLS12 1U /* minimum FAT12 clusters */ |
| 85 | #define MINCLS16 0xff5U /* minimum FAT16 clusters */ |
| 86 | #define MINCLS32 0xfff5U /* minimum FAT32 clusters */ |
| 87 | #define MAXCLS12 0xff4U /* maximum FAT12 clusters */ |
| 88 | #define MAXCLS16 0xfff4U /* maximum FAT16 clusters */ |
| 89 | #define MAXCLS32 0xffffff4U /* maximum FAT32 clusters */ |
| 90 | |
| 91 | #define mincls(fat) ((fat) == 12 ? MINCLS12 : \ |
| 92 | (fat) == 16 ? MINCLS16 : \ |
| 93 | MINCLS32) |
| 94 | |
| 95 | #define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \ |
| 96 | (fat) == 16 ? MAXCLS16 : \ |
| 97 | MAXCLS32) |
| 98 | |
| 99 | #define mk1(p, x) \ |
| 100 | (p) = (u_int8_t)(x) |
| 101 | |
| 102 | #define mk2(p, x) \ |
| 103 | (p)[0] = (u_int8_t)(x), \ |
| 104 | (p)[1] = (u_int8_t)((x) >> 010) |
| 105 | |
| 106 | #define mk4(p, x) \ |
| 107 | (p)[0] = (u_int8_t)(x), \ |
| 108 | (p)[1] = (u_int8_t)((x) >> 010), \ |
| 109 | (p)[2] = (u_int8_t)((x) >> 020), \ |
| 110 | (p)[3] = (u_int8_t)((x) >> 030) |
| 111 | |
| 112 | struct bs { |
| 113 | u_int8_t bsJump[3]; /* bootstrap entry point */ |
| 114 | u_int8_t bsOemName[8]; /* OEM name and version */ |
| 115 | } __packed; |
| 116 | |
| 117 | struct bsbpb { |
| 118 | u_int8_t bpbBytesPerSec[2]; /* bytes per sector */ |
| 119 | u_int8_t bpbSecPerClust; /* sectors per cluster */ |
| 120 | u_int8_t bpbResSectors[2]; /* reserved sectors */ |
| 121 | u_int8_t bpbFATs; /* number of FATs */ |
| 122 | u_int8_t bpbRootDirEnts[2]; /* root directory entries */ |
| 123 | u_int8_t bpbSectors[2]; /* total sectors */ |
| 124 | u_int8_t bpbMedia; /* media descriptor */ |
| 125 | u_int8_t bpbFATsecs[2]; /* sectors per FAT */ |
| 126 | u_int8_t bpbSecPerTrack[2]; /* sectors per track */ |
| 127 | u_int8_t bpbHeads[2]; /* drive heads */ |
| 128 | u_int8_t bpbHiddenSecs[4]; /* hidden sectors */ |
| 129 | u_int8_t bpbHugeSectors[4]; /* big total sectors */ |
| 130 | } __packed; |
| 131 | |
| 132 | struct bsxbpb { |
| 133 | u_int8_t bpbBigFATsecs[4]; /* big sectors per FAT */ |
| 134 | u_int8_t bpbExtFlags[2]; /* FAT control flags */ |
| 135 | u_int8_t bpbFSVers[2]; /* file system version */ |
| 136 | u_int8_t bpbRootClust[4]; /* root directory start cluster */ |
| 137 | u_int8_t bpbFSInfo[2]; /* file system info sector */ |
| 138 | u_int8_t bpbBackup[2]; /* backup boot sector */ |
| 139 | u_int8_t bpbReserved[12]; /* reserved */ |
| 140 | } __packed; |
| 141 | |
| 142 | struct bsx { |
| 143 | u_int8_t exDriveNumber; /* drive number */ |
| 144 | u_int8_t exReserved1; /* reserved */ |
| 145 | u_int8_t exBootSignature; /* extended boot signature */ |
| 146 | u_int8_t exVolumeID[4]; /* volume ID number */ |
| 147 | u_int8_t exVolumeLabel[11]; /* volume label */ |
| 148 | u_int8_t exFileSysType[8]; /* file system type */ |
| 149 | } __packed; |
| 150 | |
| 151 | struct de { |
| 152 | u_int8_t deName[11]; /* name and extension */ |
| 153 | u_int8_t deAttributes; /* attributes */ |
| 154 | u_int8_t rsvd[10]; /* reserved */ |
| 155 | u_int8_t deMTime[2]; /* last-modified time */ |
| 156 | u_int8_t deMDate[2]; /* last-modified date */ |
| 157 | u_int8_t deStartCluster[2]; /* starting cluster */ |
| 158 | u_int8_t deFileSize[4]; /* size */ |
| 159 | } __packed; |
| 160 | |
| 161 | struct bpb { |
| 162 | u_int bpbBytesPerSec; /* bytes per sector */ |
| 163 | u_int bpbSecPerClust; /* sectors per cluster */ |
| 164 | u_int bpbResSectors; /* reserved sectors */ |
| 165 | u_int bpbFATs; /* number of FATs */ |
| 166 | u_int bpbRootDirEnts; /* root directory entries */ |
| 167 | u_int bpbSectors; /* total sectors */ |
| 168 | u_int bpbMedia; /* media descriptor */ |
| 169 | u_int bpbFATsecs; /* sectors per FAT */ |
| 170 | u_int bpbSecPerTrack; /* sectors per track */ |
| 171 | u_int bpbHeads; /* drive heads */ |
| 172 | u_int bpbHiddenSecs; /* hidden sectors */ |
| 173 | u_int bpbHugeSectors; /* big total sectors */ |
| 174 | u_int bpbBigFATsecs; /* big sectors per FAT */ |
| 175 | u_int bpbRootClust; /* root directory start cluster */ |
| 176 | u_int bpbFSInfo; /* file system info sector */ |
| 177 | u_int bpbBackup; /* backup boot sector */ |
| 178 | }; |
| 179 | |
| 180 | #define BPBGAP 0, 0, 0, 0, 0, 0 |
| 181 | |
| 182 | static struct { |
| 183 | const char *name; |
| 184 | struct bpb bpb; |
| 185 | } const stdfmt[] = { |
| 186 | {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1, BPBGAP}}, |
| 187 | {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}}, |
| 188 | {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}}, |
| 189 | {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}}, |
| 190 | {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}}, |
| 191 | {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}}, |
| 192 | {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}}, |
| 193 | {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}}, |
| 194 | {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}}, |
| 195 | {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}} |
| 196 | }; |
| 197 | |
| 198 | static const u_int8_t bootcode[] = { |
| 199 | 0xfa, /* cli */ |
| 200 | 0x31, 0xc0, /* xor ax,ax */ |
| 201 | 0x8e, 0xd0, /* mov ss,ax */ |
| 202 | 0xbc, 0x00, 0x7c, /* mov sp,7c00h */ |
| 203 | 0xfb, /* sti */ |
| 204 | 0x8e, 0xd8, /* mov ds,ax */ |
| 205 | 0xe8, 0x00, 0x00, /* call $ + 3 */ |
| 206 | 0x5e, /* pop si */ |
| 207 | 0x83, 0xc6, 0x19, /* add si,+19h */ |
| 208 | 0xbb, 0x07, 0x00, /* mov bx,0007h */ |
| 209 | 0xfc, /* cld */ |
| 210 | 0xac, /* lodsb */ |
| 211 | 0x84, 0xc0, /* test al,al */ |
| 212 | 0x74, 0x06, /* jz $ + 8 */ |
| 213 | 0xb4, 0x0e, /* mov ah,0eh */ |
| 214 | 0xcd, 0x10, /* int 10h */ |
| 215 | 0xeb, 0xf5, /* jmp $ - 9 */ |
| 216 | 0x30, 0xe4, /* xor ah,ah */ |
| 217 | 0xcd, 0x16, /* int 16h */ |
| 218 | 0xcd, 0x19, /* int 19h */ |
| 219 | 0x0d, 0x0a, |
| 220 | 'N', 'o', 'n', '-', 's', 'y', 's', 't', |
| 221 | 'e', 'm', ' ', 'd', 'i', 's', 'k', |
| 222 | 0x0d, 0x0a, |
| 223 | 'P', 'r', 'e', 's', 's', ' ', 'a', 'n', |
| 224 | 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o', |
| 225 | ' ', 'r', 'e', 'b', 'o', 'o', 't', |
| 226 | 0x0d, 0x0a, |
| 227 | 0 |
| 228 | }; |
| 229 | |
| 230 | static volatile sig_atomic_t got_siginfo; |
| 231 | static void infohandler(int); |
| 232 | |
| 233 | static int check_mounted(const char *, mode_t); |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 234 | static ssize_t getchunksize(void); |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 235 | static int getstdfmt(const char *, struct bpb *); |
| 236 | static int getdiskinfo(int, const char *, const char *, int, struct bpb *); |
| 237 | static void print_bpb(struct bpb *); |
| 238 | static int ckgeom(const char *, u_int, const char *); |
| 239 | static void mklabel(u_int8_t *, const char *); |
| 240 | static int oklabel(const char *); |
| 241 | static void setstr(u_int8_t *, const char *, size_t); |
| 242 | |
| 243 | int |
| 244 | mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op) |
| 245 | { |
| 246 | char buf[MAXPATHLEN]; |
| 247 | struct sigaction si_sa; |
| 248 | struct stat sb; |
| 249 | struct timeval tv; |
| 250 | struct bpb bpb; |
| 251 | struct tm *tm; |
| 252 | struct bs *bs; |
| 253 | struct bsbpb *bsbpb; |
| 254 | struct bsxbpb *bsxbpb; |
| 255 | struct bsx *bsx; |
| 256 | struct de *de; |
| 257 | u_int8_t *img; |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 258 | u_int8_t *physbuf, *physbuf_end; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 259 | const char *bname; |
| 260 | ssize_t n; |
| 261 | time_t now; |
| 262 | u_int fat, bss, rds, cls, dir, lsn, x, x1, x2; |
Xin Li | 4d60a22 | 2018-06-18 17:30:37 -0700 | [diff] [blame] | 263 | u_int extra_res, alignment, saved_x, attempts=0; |
| 264 | bool set_res, set_spf, set_spc; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 265 | int fd, fd1, rv; |
| 266 | struct msdos_options o = *op; |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 267 | ssize_t chunksize; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 268 | |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 269 | physbuf = NULL; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 270 | rv = -1; |
| 271 | fd = fd1 = -1; |
| 272 | |
| 273 | if (o.block_size && o.sectors_per_cluster) { |
| 274 | warnx("Cannot specify both block size and sectors per cluster"); |
| 275 | goto done; |
| 276 | } |
| 277 | if (o.OEM_string && strlen(o.OEM_string) > 8) { |
| 278 | warnx("%s: bad OEM string", o.OEM_string); |
| 279 | goto done; |
| 280 | } |
| 281 | if (o.create_size) { |
| 282 | if (o.no_create) { |
| 283 | warnx("create (-C) is incompatible with -N"); |
| 284 | goto done; |
| 285 | } |
| 286 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644); |
| 287 | if (fd == -1) { |
| 288 | warnx("failed to create %s", fname); |
| 289 | goto done; |
| 290 | } |
| 291 | if (ftruncate(fd, o.create_size)) { |
| 292 | warnx("failed to initialize %jd bytes", (intmax_t)o.create_size); |
| 293 | goto done; |
| 294 | } |
| 295 | } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1) { |
| 296 | warn("%s", fname); |
| 297 | goto done; |
| 298 | } |
| 299 | if (fstat(fd, &sb)) { |
| 300 | warn("%s", fname); |
| 301 | goto done; |
| 302 | } |
| 303 | if (o.create_size) { |
| 304 | if (!S_ISREG(sb.st_mode)) |
| 305 | warnx("warning, %s is not a regular file", fname); |
| 306 | } else { |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 307 | #ifdef MAKEFS |
| 308 | errx(1, "o.create_size must be set!"); |
| 309 | #else |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 310 | if (!S_ISCHR(sb.st_mode)) |
| 311 | warnx("warning, %s is not a character device", fname); |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 312 | #endif |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 313 | } |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 314 | #ifndef MAKEFS |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 315 | if (!o.no_create) |
| 316 | if (check_mounted(fname, sb.st_mode) == -1) |
| 317 | goto done; |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 318 | #endif |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 319 | if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) { |
| 320 | warnx("cannot seek to %jd", (intmax_t)o.offset); |
| 321 | goto done; |
| 322 | } |
| 323 | memset(&bpb, 0, sizeof(bpb)); |
| 324 | if (o.floppy) { |
| 325 | if (getstdfmt(o.floppy, &bpb) == -1) |
| 326 | goto done; |
| 327 | bpb.bpbHugeSectors = bpb.bpbSectors; |
| 328 | bpb.bpbSectors = 0; |
| 329 | bpb.bpbBigFATsecs = bpb.bpbFATsecs; |
| 330 | bpb.bpbFATsecs = 0; |
| 331 | } |
| 332 | if (o.drive_heads) |
| 333 | bpb.bpbHeads = o.drive_heads; |
| 334 | if (o.sectors_per_track) |
| 335 | bpb.bpbSecPerTrack = o.sectors_per_track; |
| 336 | if (o.bytes_per_sector) |
| 337 | bpb.bpbBytesPerSec = o.bytes_per_sector; |
| 338 | if (o.size) |
| 339 | bpb.bpbHugeSectors = o.size; |
| 340 | if (o.hidden_sectors_set) |
| 341 | bpb.bpbHiddenSecs = o.hidden_sectors; |
| 342 | if (!(o.floppy || (o.drive_heads && o.sectors_per_track && |
| 343 | o.bytes_per_sector && o.size && o.hidden_sectors_set))) { |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 344 | if (getdiskinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb) == -1) |
| 345 | goto done; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 346 | bpb.bpbHugeSectors -= (o.offset / bpb.bpbBytesPerSec); |
| 347 | if (bpb.bpbSecPerClust == 0) { /* set defaults */ |
| 348 | if (bpb.bpbHugeSectors <= 6000) /* about 3MB -> 512 bytes */ |
| 349 | bpb.bpbSecPerClust = 1; |
| 350 | else if (bpb.bpbHugeSectors <= (1<<17)) /* 64M -> 4k */ |
| 351 | bpb.bpbSecPerClust = 8; |
| 352 | else if (bpb.bpbHugeSectors <= (1<<19)) /* 256M -> 8k */ |
| 353 | bpb.bpbSecPerClust = 16; |
| 354 | else if (bpb.bpbHugeSectors <= (1<<21)) /* 1G -> 16k */ |
| 355 | bpb.bpbSecPerClust = 32; |
| 356 | else |
| 357 | bpb.bpbSecPerClust = 64; /* otherwise 32k */ |
| 358 | } |
| 359 | } |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 360 | if (bpb.bpbBytesPerSec < MINBPS || |
| 361 | bpb.bpbBytesPerSec > MAXBPS || |
| 362 | !powerof2(bpb.bpbBytesPerSec)) { |
| 363 | warnx("Invalid bytes/sector (%u): must be 512, 1024, 2048 or 4096", |
| 364 | bpb.bpbBytesPerSec); |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 365 | goto done; |
| 366 | } |
| 367 | |
| 368 | if (o.volume_label && !oklabel(o.volume_label)) { |
| 369 | warnx("%s: bad volume label", o.volume_label); |
| 370 | goto done; |
| 371 | } |
| 372 | if (!(fat = o.fat_type)) { |
| 373 | if (o.floppy) |
| 374 | fat = 12; |
| 375 | else if (!o.directory_entries && (o.info_sector || o.backup_sector)) |
| 376 | fat = 32; |
| 377 | } |
| 378 | if ((fat == 32 && o.directory_entries) || (fat != 32 && (o.info_sector || o.backup_sector))) { |
| 379 | warnx("-%c is not a legal FAT%s option", |
| 380 | fat == 32 ? 'e' : o.info_sector ? 'i' : 'k', |
| 381 | fat == 32 ? "32" : "12/16"); |
| 382 | goto done; |
| 383 | } |
| 384 | if (o.floppy && fat == 32) |
| 385 | bpb.bpbRootDirEnts = 0; |
| 386 | if (fat != 0 && fat != 12 && fat != 16 && fat != 32) { |
| 387 | warnx("%d: bad FAT type", fat); |
| 388 | goto done; |
| 389 | } |
| 390 | |
| 391 | if (o.block_size) { |
| 392 | if (!powerof2(o.block_size)) { |
| 393 | warnx("block size (%u) is not a power of 2", o.block_size); |
| 394 | goto done; |
| 395 | } |
| 396 | if (o.block_size < bpb.bpbBytesPerSec) { |
| 397 | warnx("block size (%u) is too small; minimum is %u", |
| 398 | o.block_size, bpb.bpbBytesPerSec); |
| 399 | goto done; |
| 400 | } |
| 401 | if (o.block_size > bpb.bpbBytesPerSec * MAXSPC) { |
| 402 | warnx("block size (%u) is too large; maximum is %u", |
| 403 | o.block_size, bpb.bpbBytesPerSec * MAXSPC); |
| 404 | goto done; |
| 405 | } |
| 406 | bpb.bpbSecPerClust = o.block_size / bpb.bpbBytesPerSec; |
| 407 | } |
| 408 | if (o.sectors_per_cluster) { |
| 409 | if (!powerof2(o.sectors_per_cluster)) { |
| 410 | warnx("sectors/cluster (%u) is not a power of 2", |
| 411 | o.sectors_per_cluster); |
| 412 | goto done; |
| 413 | } |
| 414 | bpb.bpbSecPerClust = o.sectors_per_cluster; |
| 415 | } |
| 416 | if (o.reserved_sectors) |
| 417 | bpb.bpbResSectors = o.reserved_sectors; |
| 418 | if (o.num_FAT) { |
| 419 | if (o.num_FAT > MAXNFT) { |
| 420 | warnx("number of FATs (%u) is too large; maximum is %u", |
| 421 | o.num_FAT, MAXNFT); |
| 422 | goto done; |
| 423 | } |
| 424 | bpb.bpbFATs = o.num_FAT; |
| 425 | } |
| 426 | if (o.directory_entries) |
| 427 | bpb.bpbRootDirEnts = o.directory_entries; |
| 428 | if (o.media_descriptor_set) { |
| 429 | if (o.media_descriptor < 0xf0) { |
| 430 | warnx("illegal media descriptor (%#x)", o.media_descriptor); |
| 431 | goto done; |
| 432 | } |
| 433 | bpb.bpbMedia = o.media_descriptor; |
| 434 | } |
| 435 | if (o.sectors_per_fat) |
| 436 | bpb.bpbBigFATsecs = o.sectors_per_fat; |
| 437 | if (o.info_sector) |
| 438 | bpb.bpbFSInfo = o.info_sector; |
| 439 | if (o.backup_sector) |
| 440 | bpb.bpbBackup = o.backup_sector; |
| 441 | bss = 1; |
| 442 | bname = NULL; |
| 443 | fd1 = -1; |
| 444 | if (o.bootstrap) { |
| 445 | bname = o.bootstrap; |
| 446 | if (!strchr(bname, '/')) { |
| 447 | snprintf(buf, sizeof(buf), "/boot/%s", bname); |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 448 | bname = buf; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 449 | } |
| 450 | if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) { |
| 451 | warn("%s", bname); |
| 452 | goto done; |
| 453 | } |
| 454 | if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec || |
| 455 | sb.st_size < bpb.bpbBytesPerSec || |
| 456 | sb.st_size > bpb.bpbBytesPerSec * MAXU16) { |
| 457 | warnx("%s: inappropriate file type or format", bname); |
| 458 | goto done; |
| 459 | } |
| 460 | bss = sb.st_size / bpb.bpbBytesPerSec; |
| 461 | } |
| 462 | if (!bpb.bpbFATs) |
| 463 | bpb.bpbFATs = 2; |
| 464 | if (!fat) { |
| 465 | if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) + |
| 466 | howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) * |
| 467 | (bpb.bpbSecPerClust ? 16 : 12) / BPN, |
| 468 | bpb.bpbBytesPerSec * NPB) * |
| 469 | bpb.bpbFATs + |
| 470 | howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE, |
| 471 | bpb.bpbBytesPerSec / sizeof(struct de)) + |
| 472 | (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) * |
| 473 | (bpb.bpbSecPerClust ? bpb.bpbSecPerClust : |
| 474 | howmany(DEFBLK, bpb.bpbBytesPerSec))) |
| 475 | fat = 12; |
| 476 | else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors < |
| 477 | (bpb.bpbResSectors ? bpb.bpbResSectors : bss) + |
| 478 | howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) * |
| 479 | bpb.bpbFATs + |
| 480 | howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) + |
| 481 | (MAXCLS16 + 1) * |
| 482 | (bpb.bpbSecPerClust ? bpb.bpbSecPerClust : |
| 483 | howmany(8192, bpb.bpbBytesPerSec))) |
| 484 | fat = 16; |
| 485 | else |
| 486 | fat = 32; |
| 487 | } |
| 488 | x = bss; |
| 489 | if (fat == 32) { |
| 490 | if (!bpb.bpbFSInfo) { |
| 491 | if (x == MAXU16 || x == bpb.bpbBackup) { |
| 492 | warnx("no room for info sector"); |
| 493 | goto done; |
| 494 | } |
| 495 | bpb.bpbFSInfo = x; |
| 496 | } |
| 497 | if (bpb.bpbFSInfo != MAXU16 && x <= bpb.bpbFSInfo) |
| 498 | x = bpb.bpbFSInfo + 1; |
| 499 | if (!bpb.bpbBackup) { |
| 500 | if (x == MAXU16) { |
| 501 | warnx("no room for backup sector"); |
| 502 | goto done; |
| 503 | } |
| 504 | bpb.bpbBackup = x; |
| 505 | } else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) { |
| 506 | warnx("backup sector would overwrite info sector"); |
| 507 | goto done; |
| 508 | } |
| 509 | if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup) |
| 510 | x = bpb.bpbBackup + 1; |
| 511 | } |
Xin Li | 4d60a22 | 2018-06-18 17:30:37 -0700 | [diff] [blame] | 512 | |
| 513 | extra_res = 0; |
| 514 | alignment = 0; |
| 515 | set_res = (bpb.bpbResSectors == 0); |
| 516 | set_spf = (bpb.bpbBigFATsecs == 0); |
| 517 | set_spc = (bpb.bpbSecPerClust == 0); |
| 518 | saved_x = x; |
| 519 | |
| 520 | /* |
| 521 | * Attempt to align the root directory to cluster if o.align is set. |
| 522 | * This is done by padding with reserved blocks. Note that this can |
| 523 | * cause other factors to change, which can in turn change the alignment. |
| 524 | * This should take at most 2 iterations, as increasing the reserved |
| 525 | * amount may cause the FAT size to decrease by 1, requiring another |
| 526 | * bpbFATs reserved blocks. If bpbSecPerClust changes, it will |
| 527 | * be half of its previous size, and thus will not throw off alignment. |
| 528 | */ |
| 529 | do { |
| 530 | x = saved_x; |
| 531 | if (set_res) |
| 532 | bpb.bpbResSectors = ((fat == 32) ? |
| 533 | MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x) + extra_res; |
| 534 | else if (bpb.bpbResSectors < x) { |
| 535 | warnx("too few reserved sectors (need %d have %d)", x, |
| 536 | bpb.bpbResSectors); |
| 537 | goto done; |
| 538 | } |
| 539 | if (fat != 32 && !bpb.bpbRootDirEnts) |
| 540 | bpb.bpbRootDirEnts = DEFRDE; |
| 541 | rds = howmany(bpb.bpbRootDirEnts, |
| 542 | bpb.bpbBytesPerSec / sizeof(struct de)); |
| 543 | if (set_spc) { |
| 544 | for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 : |
| 545 | DEFBLK, bpb.bpbBytesPerSec); |
| 546 | bpb.bpbSecPerClust < MAXSPC && (bpb.bpbResSectors + |
| 547 | howmany((RESFTE + maxcls(fat)) * (fat / BPN), |
| 548 | bpb.bpbBytesPerSec * NPB) * bpb.bpbFATs + |
| 549 | rds + |
| 550 | (u_int64_t) (maxcls(fat) + 1) * bpb.bpbSecPerClust) <= |
| 551 | bpb.bpbHugeSectors; |
| 552 | bpb.bpbSecPerClust <<= 1) |
| 553 | continue; |
| 554 | |
| 555 | } |
| 556 | if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) { |
| 557 | warnx("too many sectors/FAT for FAT12/16"); |
| 558 | goto done; |
| 559 | } |
| 560 | x1 = bpb.bpbResSectors + rds; |
| 561 | x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1; |
| 562 | if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) { |
| 563 | warnx("meta data exceeds file system size"); |
| 564 | goto done; |
| 565 | } |
| 566 | x1 += x * bpb.bpbFATs; |
| 567 | x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB / |
| 568 | (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB + |
| 569 | fat / BPN * bpb.bpbFATs); |
| 570 | x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN), |
| 571 | bpb.bpbBytesPerSec * NPB); |
| 572 | if (set_spf) { |
| 573 | if (bpb.bpbBigFATsecs == 0) |
| 574 | bpb.bpbBigFATsecs = x2; |
| 575 | x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs; |
| 576 | } |
| 577 | if (set_res) { |
| 578 | /* attempt to align root directory */ |
| 579 | alignment = (bpb.bpbResSectors + bpb.bpbBigFATsecs * bpb.bpbFATs) % |
| 580 | bpb.bpbSecPerClust; |
| 581 | if (o.align) |
| 582 | extra_res += bpb.bpbSecPerClust - alignment; |
| 583 | } |
| 584 | attempts++; |
| 585 | } while (o.align && alignment != 0 && attempts < 2); |
| 586 | if (o.align && alignment != 0) |
| 587 | warnx("warning: Alignment failed."); |
| 588 | |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 589 | cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust; |
| 590 | x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) - |
| 591 | RESFTE; |
| 592 | if (cls > x) |
| 593 | cls = x; |
| 594 | if (bpb.bpbBigFATsecs < x2) |
| 595 | warnx("warning: sectors/FAT limits file system to %u clusters", |
| 596 | cls); |
| 597 | if (cls < mincls(fat)) { |
| 598 | warnx("%u clusters too few clusters for FAT%u, need %u", cls, fat, |
| 599 | mincls(fat)); |
| 600 | goto done; |
| 601 | } |
| 602 | if (cls > maxcls(fat)) { |
| 603 | cls = maxcls(fat); |
| 604 | bpb.bpbHugeSectors = x1 + (cls + 1) * bpb.bpbSecPerClust - 1; |
| 605 | warnx("warning: FAT type limits file system to %u sectors", |
| 606 | bpb.bpbHugeSectors); |
| 607 | } |
| 608 | printf("%s: %u sector%s in %u FAT%u cluster%s " |
| 609 | "(%u bytes/cluster)\n", fname, cls * bpb.bpbSecPerClust, |
| 610 | cls * bpb.bpbSecPerClust == 1 ? "" : "s", cls, fat, |
| 611 | cls == 1 ? "" : "s", bpb.bpbBytesPerSec * bpb.bpbSecPerClust); |
| 612 | if (!bpb.bpbMedia) |
| 613 | bpb.bpbMedia = !bpb.bpbHiddenSecs ? 0xf0 : 0xf8; |
| 614 | if (fat == 32) |
| 615 | bpb.bpbRootClust = RESFTE; |
| 616 | if (bpb.bpbHugeSectors <= MAXU16) { |
| 617 | bpb.bpbSectors = bpb.bpbHugeSectors; |
| 618 | bpb.bpbHugeSectors = 0; |
| 619 | } |
| 620 | if (fat != 32) { |
| 621 | bpb.bpbFATsecs = bpb.bpbBigFATsecs; |
| 622 | bpb.bpbBigFATsecs = 0; |
| 623 | } |
| 624 | print_bpb(&bpb); |
| 625 | if (!o.no_create) { |
| 626 | if (o.timestamp_set) { |
| 627 | tv.tv_sec = now = o.timestamp; |
| 628 | tv.tv_usec = 0; |
| 629 | tm = gmtime(&now); |
| 630 | } else { |
| 631 | gettimeofday(&tv, NULL); |
| 632 | now = tv.tv_sec; |
| 633 | tm = localtime(&now); |
| 634 | } |
| 635 | |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 636 | chunksize = getchunksize(); |
| 637 | physbuf = malloc(chunksize); |
| 638 | if (physbuf == NULL) { |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 639 | warn(NULL); |
| 640 | goto done; |
| 641 | } |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 642 | physbuf_end = physbuf + chunksize; |
| 643 | img = physbuf; |
| 644 | |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 645 | dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs : |
| 646 | bpb.bpbBigFATsecs) * bpb.bpbFATs; |
| 647 | memset(&si_sa, 0, sizeof(si_sa)); |
| 648 | si_sa.sa_handler = infohandler; |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 649 | #ifdef SIGINFO |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 650 | if (sigaction(SIGINFO, &si_sa, NULL) == -1) { |
| 651 | warn("sigaction SIGINFO"); |
| 652 | goto done; |
| 653 | } |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 654 | #endif |
hyeongseok.kim | 41c6a50 | 2020-03-30 16:22:42 +0900 | [diff] [blame] | 655 | |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 656 | for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) { |
| 657 | if (got_siginfo) { |
| 658 | fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n", |
| 659 | fname, lsn, |
| 660 | (dir + (fat == 32 ? bpb.bpbSecPerClust: rds)), |
| 661 | (lsn * 100) / (dir + |
| 662 | (fat == 32 ? bpb.bpbSecPerClust: rds))); |
| 663 | got_siginfo = 0; |
| 664 | } |
| 665 | x = lsn; |
| 666 | if (o.bootstrap && |
| 667 | fat == 32 && bpb.bpbBackup != MAXU16 && |
| 668 | bss <= bpb.bpbBackup && x >= bpb.bpbBackup) { |
| 669 | x -= bpb.bpbBackup; |
| 670 | if (!x && lseek(fd1, o.offset, SEEK_SET)) { |
| 671 | warn("%s", bname); |
| 672 | goto done; |
| 673 | } |
| 674 | } |
| 675 | if (o.bootstrap && x < bss) { |
| 676 | if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) { |
| 677 | warn("%s", bname); |
| 678 | goto done; |
| 679 | } |
| 680 | if ((unsigned)n != bpb.bpbBytesPerSec) { |
| 681 | warnx("%s: can't read sector %u", bname, x); |
| 682 | goto done; |
| 683 | } |
| 684 | } else |
| 685 | memset(img, 0, bpb.bpbBytesPerSec); |
| 686 | if (!lsn || |
| 687 | (fat == 32 && bpb.bpbBackup != MAXU16 && |
| 688 | lsn == bpb.bpbBackup)) { |
| 689 | x1 = sizeof(struct bs); |
| 690 | bsbpb = (struct bsbpb *)(img + x1); |
| 691 | mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec); |
| 692 | mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust); |
| 693 | mk2(bsbpb->bpbResSectors, bpb.bpbResSectors); |
| 694 | mk1(bsbpb->bpbFATs, bpb.bpbFATs); |
| 695 | mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts); |
| 696 | mk2(bsbpb->bpbSectors, bpb.bpbSectors); |
| 697 | mk1(bsbpb->bpbMedia, bpb.bpbMedia); |
| 698 | mk2(bsbpb->bpbFATsecs, bpb.bpbFATsecs); |
| 699 | mk2(bsbpb->bpbSecPerTrack, bpb.bpbSecPerTrack); |
| 700 | mk2(bsbpb->bpbHeads, bpb.bpbHeads); |
| 701 | mk4(bsbpb->bpbHiddenSecs, bpb.bpbHiddenSecs); |
| 702 | mk4(bsbpb->bpbHugeSectors, bpb.bpbHugeSectors); |
| 703 | x1 += sizeof(struct bsbpb); |
| 704 | if (fat == 32) { |
| 705 | bsxbpb = (struct bsxbpb *)(img + x1); |
| 706 | mk4(bsxbpb->bpbBigFATsecs, bpb.bpbBigFATsecs); |
| 707 | mk2(bsxbpb->bpbExtFlags, 0); |
| 708 | mk2(bsxbpb->bpbFSVers, 0); |
| 709 | mk4(bsxbpb->bpbRootClust, bpb.bpbRootClust); |
| 710 | mk2(bsxbpb->bpbFSInfo, bpb.bpbFSInfo); |
| 711 | mk2(bsxbpb->bpbBackup, bpb.bpbBackup); |
| 712 | x1 += sizeof(struct bsxbpb); |
| 713 | } |
| 714 | bsx = (struct bsx *)(img + x1); |
| 715 | mk1(bsx->exBootSignature, 0x29); |
| 716 | if (o.volume_id_set) |
| 717 | x = o.volume_id; |
| 718 | else |
| 719 | x = (((u_int)(1 + tm->tm_mon) << 8 | |
| 720 | (u_int)tm->tm_mday) + |
| 721 | ((u_int)tm->tm_sec << 8 | |
| 722 | (u_int)(tv.tv_usec / 10))) << 16 | |
| 723 | ((u_int)(1900 + tm->tm_year) + |
| 724 | ((u_int)tm->tm_hour << 8 | |
| 725 | (u_int)tm->tm_min)); |
| 726 | mk4(bsx->exVolumeID, x); |
| 727 | mklabel(bsx->exVolumeLabel, o.volume_label ? o.volume_label : "NO NAME"); |
| 728 | snprintf(buf, sizeof(buf), "FAT%u", fat); |
| 729 | setstr(bsx->exFileSysType, buf, sizeof(bsx->exFileSysType)); |
| 730 | if (!o.bootstrap) { |
| 731 | x1 += sizeof(struct bsx); |
| 732 | bs = (struct bs *)img; |
| 733 | mk1(bs->bsJump[0], 0xeb); |
| 734 | mk1(bs->bsJump[1], x1 - 2); |
| 735 | mk1(bs->bsJump[2], 0x90); |
| 736 | setstr(bs->bsOemName, o.OEM_string ? o.OEM_string : "BSD4.4 ", |
| 737 | sizeof(bs->bsOemName)); |
| 738 | memcpy(img + x1, bootcode, sizeof(bootcode)); |
| 739 | mk2(img + MINBPS - 2, DOSMAGIC); |
| 740 | } |
| 741 | } else if (fat == 32 && bpb.bpbFSInfo != MAXU16 && |
| 742 | (lsn == bpb.bpbFSInfo || |
| 743 | (bpb.bpbBackup != MAXU16 && |
| 744 | lsn == bpb.bpbBackup + bpb.bpbFSInfo))) { |
| 745 | mk4(img, 0x41615252); |
| 746 | mk4(img + MINBPS - 28, 0x61417272); |
| 747 | mk4(img + MINBPS - 24, 0xffffffff); |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 748 | mk4(img + MINBPS - 20, 0xffffffff); |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 749 | mk2(img + MINBPS - 2, DOSMAGIC); |
| 750 | } else if (lsn >= bpb.bpbResSectors && lsn < dir && |
| 751 | !((lsn - bpb.bpbResSectors) % |
| 752 | (bpb.bpbFATsecs ? bpb.bpbFATsecs : |
| 753 | bpb.bpbBigFATsecs))) { |
| 754 | mk1(img[0], bpb.bpbMedia); |
| 755 | for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++) |
| 756 | mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff); |
| 757 | } else if (lsn == dir && o.volume_label) { |
| 758 | de = (struct de *)img; |
| 759 | mklabel(de->deName, o.volume_label); |
| 760 | mk1(de->deAttributes, 050); |
| 761 | x = (u_int)tm->tm_hour << 11 | |
| 762 | (u_int)tm->tm_min << 5 | |
| 763 | (u_int)tm->tm_sec >> 1; |
| 764 | mk2(de->deMTime, x); |
| 765 | x = (u_int)(tm->tm_year - 80) << 9 | |
| 766 | (u_int)(tm->tm_mon + 1) << 5 | |
| 767 | (u_int)tm->tm_mday; |
| 768 | mk2(de->deMDate, x); |
| 769 | } |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 770 | /* |
| 771 | * Issue a write of chunksize once we have collected |
| 772 | * enough sectors. |
| 773 | */ |
| 774 | img += bpb.bpbBytesPerSec; |
| 775 | if (img >= physbuf_end) { |
| 776 | n = write(fd, physbuf, chunksize); |
| 777 | if (n != chunksize) { |
| 778 | warnx("%s: can't write sector %u", fname, lsn); |
| 779 | goto done; |
| 780 | } |
| 781 | img = physbuf; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 782 | } |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 783 | } |
| 784 | /* |
| 785 | * Write remaining sectors, if the last write didn't end |
| 786 | * up filling a whole chunk. |
| 787 | */ |
| 788 | if (img != physbuf) { |
| 789 | ssize_t tailsize = img - physbuf; |
| 790 | |
| 791 | n = write(fd, physbuf, tailsize); |
| 792 | if (n != tailsize) { |
| 793 | warnx("%s: can't write sector %u", fname, lsn); |
| 794 | goto done; |
| 795 | } |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | rv = 0; |
| 799 | done: |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 800 | free(physbuf); |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 801 | if (fd != -1) |
| 802 | close(fd); |
| 803 | if (fd1 != -1) |
| 804 | close(fd1); |
| 805 | |
| 806 | return rv; |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | * return -1 with error if file system is mounted. |
| 811 | */ |
| 812 | static int |
| 813 | check_mounted(const char *fname, mode_t mode) |
| 814 | { |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 815 | /* |
| 816 | * If getmntinfo() is not available (e.g. Linux) don't check. This should |
| 817 | * not be a problem since we will only be using makefs to create images. |
| 818 | */ |
| 819 | #if 0 && !defined(MAKEFS) |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 820 | struct statfs *mp; |
| 821 | const char *s1, *s2; |
| 822 | size_t len; |
| 823 | int n, r; |
| 824 | |
| 825 | if (!(n = getmntinfo(&mp, MNT_NOWAIT))) { |
| 826 | warn("getmntinfo"); |
| 827 | return -1; |
| 828 | } |
| 829 | len = strlen(_PATH_DEV); |
| 830 | s1 = fname; |
| 831 | if (!strncmp(s1, _PATH_DEV, len)) |
| 832 | s1 += len; |
| 833 | r = S_ISCHR(mode) && s1 != fname && *s1 == 'r'; |
| 834 | for (; n--; mp++) { |
| 835 | s2 = mp->f_mntfromname; |
| 836 | if (!strncmp(s2, _PATH_DEV, len)) |
| 837 | s2 += len; |
| 838 | if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) || |
| 839 | !strcmp(s1, s2)) { |
| 840 | warnx("%s is mounted on %s", fname, mp->f_mntonname); |
| 841 | return -1; |
| 842 | } |
| 843 | } |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 844 | #endif |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | /* |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 849 | * Get optimal I/O size |
| 850 | */ |
| 851 | static ssize_t |
| 852 | getchunksize(void) |
| 853 | { |
| 854 | static int chunksize; |
| 855 | |
| 856 | if (chunksize != 0) |
| 857 | return ((ssize_t)chunksize); |
| 858 | |
| 859 | #ifdef KERN_MAXPHYS |
| 860 | int mib[2]; |
| 861 | size_t len; |
| 862 | |
| 863 | mib[0] = CTL_KERN; |
| 864 | mib[1] = KERN_MAXPHYS; |
| 865 | len = sizeof(chunksize); |
| 866 | |
| 867 | if (sysctl(mib, 2, &chunksize, &len, NULL, 0) == -1) { |
| 868 | warn("sysctl: KERN_MAXPHYS, using %zu", (size_t)MAXPHYS); |
| 869 | chunksize = 0; |
| 870 | } |
| 871 | #endif |
| 872 | if (chunksize == 0) |
| 873 | chunksize = MAXPHYS; |
| 874 | |
| 875 | /* |
| 876 | * For better performance, we want to write larger chunks instead of |
| 877 | * individual sectors (the size can only be 512, 1024, 2048 or 4096 |
| 878 | * bytes). Assert that chunksize can always hold an integer number of |
| 879 | * sectors by asserting that both are power of two numbers and the |
| 880 | * chunksize is greater than MAXBPS. |
| 881 | */ |
| 882 | static_assert(powerof2(MAXBPS), "MAXBPS is not power of 2"); |
| 883 | assert(powerof2(chunksize)); |
| 884 | assert(chunksize > MAXBPS); |
| 885 | |
| 886 | return ((ssize_t)chunksize); |
| 887 | } |
| 888 | |
| 889 | /* |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 890 | * Get a standard format. |
| 891 | */ |
| 892 | static int |
| 893 | getstdfmt(const char *fmt, struct bpb *bpb) |
| 894 | { |
| 895 | u_int x, i; |
| 896 | |
| 897 | x = nitems(stdfmt); |
| 898 | for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++); |
| 899 | if (i == x) { |
| 900 | warnx("%s: unknown standard format", fmt); |
| 901 | return -1; |
| 902 | } |
| 903 | *bpb = stdfmt[i].bpb; |
| 904 | return 0; |
| 905 | } |
| 906 | |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 907 | #if 0 |
| 908 | static void |
| 909 | compute_geometry_from_file(int fd, const char *fname, struct disklabel *lp) |
| 910 | { |
| 911 | struct stat st; |
| 912 | off_t ms; |
| 913 | |
| 914 | if (fstat(fd, &st)) |
| 915 | err(1, "cannot get disk size"); |
| 916 | if (!S_ISREG(st.st_mode)) |
| 917 | errx(1, "%s is not a regular file", fname); |
| 918 | ms = st.st_size; |
| 919 | lp->d_secsize = 512; |
| 920 | lp->d_nsectors = 63; |
| 921 | lp->d_ntracks = 255; |
| 922 | lp->d_secperunit = ms / lp->d_secsize; |
| 923 | } |
| 924 | #endif |
| 925 | |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 926 | /* |
| 927 | * Get disk slice, partition, and geometry information. |
| 928 | */ |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 929 | #if defined(__linux__) |
| 930 | static int getdiskinfo(int fd, const char *fname, const char *dtype, |
| 931 | __unused int oflag, struct bpb *bpb) |
| 932 | { |
| 933 | if (ioctl(fd, BLKSSZGET, &bpb->bpbBytesPerSec)) { |
| 934 | err(1, "ioctl(BLKSSZGET) for bytes/sector failed"); |
| 935 | } |
| 936 | |
| 937 | if (ckgeom(fname, bpb->bpbBytesPerSec, "bytes/sector") == -1) return -1; |
| 938 | |
Alessio Balsini | ee18a6b | 2021-02-15 14:47:16 +0000 | [diff] [blame] | 939 | u_int64_t device_size; |
Ed Tsai | 7dd1b1b | 2021-01-19 17:36:57 +0800 | [diff] [blame] | 940 | if (ioctl(fd, BLKGETSIZE64, &device_size)) { |
| 941 | err(1, "ioctl(BLKGETSIZE64) failed"); |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Alessio Balsini | ee18a6b | 2021-02-15 14:47:16 +0000 | [diff] [blame] | 944 | u_int64_t sectors = device_size/bpb->bpbBytesPerSec; |
| 945 | if (sectors > UINT_MAX) { |
| 946 | errx(1, "too many sectors: %"PRIu64" (%"PRIu64" byte device, %u bytes/sector)", |
Ed Tsai | 7dd1b1b | 2021-01-19 17:36:57 +0800 | [diff] [blame] | 947 | sectors, device_size, bpb->bpbBytesPerSec); |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 948 | } |
Ed Tsai | 7dd1b1b | 2021-01-19 17:36:57 +0800 | [diff] [blame] | 949 | bpb->bpbHugeSectors = sectors; |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 950 | |
Elliott Hughes | 188c86e | 2018-06-20 13:33:22 -0700 | [diff] [blame] | 951 | bpb->bpbSecPerTrack = 63; |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 952 | if (ckgeom(fname, bpb->bpbSecPerTrack, "sectors/track") == -1) return -1; |
| 953 | |
Elliott Hughes | 188c86e | 2018-06-20 13:33:22 -0700 | [diff] [blame] | 954 | bpb->bpbHeads = 64; |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 955 | if (ckgeom(fname, bpb->bpbHeads, "drive heads") == -1) return -1; |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | #elif defined(__APPLE__) |
| 960 | static int |
| 961 | getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag, |
| 962 | struct bpb *bpb) { |
| 963 | return 0; |
| 964 | } |
| 965 | #else |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 966 | static int |
| 967 | getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag, |
| 968 | struct bpb *bpb) |
| 969 | { |
| 970 | struct disklabel *lp, dlp; |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 971 | off_t hs = 0; |
| 972 | #ifndef MAKEFS |
| 973 | off_t ms; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 974 | struct fd_type type; |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 975 | |
| 976 | lp = NULL; |
| 977 | |
| 978 | /* If the user specified a disk type, try to use that */ |
| 979 | if (dtype != NULL) { |
| 980 | lp = getdiskbyname(dtype); |
| 981 | } |
| 982 | |
| 983 | /* Maybe it's a floppy drive */ |
| 984 | if (lp == NULL) { |
| 985 | if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) { |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 986 | /* create a fake geometry for a file image */ |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 987 | compute_geometry_from_file(fd, fname, &dlp); |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 988 | lp = &dlp; |
| 989 | } else if (ioctl(fd, FD_GTYPE, &type) != -1) { |
| 990 | dlp.d_secsize = 128 << type.secsize; |
| 991 | dlp.d_nsectors = type.sectrac; |
| 992 | dlp.d_ntracks = type.heads; |
| 993 | dlp.d_secperunit = ms / dlp.d_secsize; |
| 994 | lp = &dlp; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | /* Maybe it's a fixed drive */ |
| 999 | if (lp == NULL) { |
| 1000 | if (bpb->bpbBytesPerSec) |
| 1001 | dlp.d_secsize = bpb->bpbBytesPerSec; |
| 1002 | if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE, |
| 1003 | &dlp.d_secsize) == -1) |
| 1004 | err(1, "cannot get sector size"); |
| 1005 | |
| 1006 | dlp.d_secperunit = ms / dlp.d_secsize; |
| 1007 | |
| 1008 | if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS, |
| 1009 | &dlp.d_nsectors) == -1) { |
| 1010 | warn("cannot get number of sectors per track"); |
| 1011 | dlp.d_nsectors = 63; |
| 1012 | } |
| 1013 | if (bpb->bpbHeads == 0 && |
| 1014 | ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) { |
| 1015 | warn("cannot get number of heads"); |
| 1016 | if (dlp.d_secperunit <= 63*1*1024) |
| 1017 | dlp.d_ntracks = 1; |
| 1018 | else if (dlp.d_secperunit <= 63*16*1024) |
| 1019 | dlp.d_ntracks = 16; |
| 1020 | else |
| 1021 | dlp.d_ntracks = 255; |
| 1022 | } |
| 1023 | |
| 1024 | hs = (ms / dlp.d_secsize) - dlp.d_secperunit; |
| 1025 | lp = &dlp; |
| 1026 | } |
Elliott Hughes | 8c4b3f0 | 2021-01-20 17:03:21 -0800 | [diff] [blame] | 1027 | #else |
| 1028 | /* In the makefs case we only support image files: */ |
| 1029 | compute_geometry_from_file(fd, fname, &dlp); |
| 1030 | lp = &dlp; |
| 1031 | #endif |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 1032 | |
| 1033 | if (bpb->bpbBytesPerSec == 0) { |
| 1034 | if (ckgeom(fname, lp->d_secsize, "bytes/sector") == -1) |
| 1035 | return -1; |
| 1036 | bpb->bpbBytesPerSec = lp->d_secsize; |
| 1037 | } |
| 1038 | if (bpb->bpbSecPerTrack == 0) { |
| 1039 | if (ckgeom(fname, lp->d_nsectors, "sectors/track") == -1) |
| 1040 | return -1; |
| 1041 | bpb->bpbSecPerTrack = lp->d_nsectors; |
| 1042 | } |
| 1043 | if (bpb->bpbHeads == 0) { |
| 1044 | if (ckgeom(fname, lp->d_ntracks, "drive heads") == -1) |
| 1045 | return -1; |
| 1046 | bpb->bpbHeads = lp->d_ntracks; |
| 1047 | } |
| 1048 | if (bpb->bpbHugeSectors == 0) |
| 1049 | bpb->bpbHugeSectors = lp->d_secperunit; |
| 1050 | if (bpb->bpbHiddenSecs == 0) |
| 1051 | bpb->bpbHiddenSecs = hs; |
| 1052 | return 0; |
| 1053 | } |
Elliott Hughes | 5946c62 | 2018-06-13 12:52:58 -0700 | [diff] [blame] | 1054 | #endif |
Elliott Hughes | 6d2f393 | 2018-06-13 10:39:17 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * Print out BPB values. |
| 1058 | */ |
| 1059 | static void |
| 1060 | print_bpb(struct bpb *bpb) |
| 1061 | { |
| 1062 | printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u", |
| 1063 | bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors, |
| 1064 | bpb->bpbFATs); |
| 1065 | if (bpb->bpbRootDirEnts) |
| 1066 | printf(" RootDirEnts=%u", bpb->bpbRootDirEnts); |
| 1067 | if (bpb->bpbSectors) |
| 1068 | printf(" Sectors=%u", bpb->bpbSectors); |
| 1069 | printf(" Media=%#x", bpb->bpbMedia); |
| 1070 | if (bpb->bpbFATsecs) |
| 1071 | printf(" FATsecs=%u", bpb->bpbFATsecs); |
| 1072 | printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack, |
| 1073 | bpb->bpbHeads, bpb->bpbHiddenSecs); |
| 1074 | if (bpb->bpbHugeSectors) |
| 1075 | printf(" HugeSectors=%u", bpb->bpbHugeSectors); |
| 1076 | if (!bpb->bpbFATsecs) { |
| 1077 | printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs, |
| 1078 | bpb->bpbRootClust); |
| 1079 | printf(" FSInfo="); |
| 1080 | printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo); |
| 1081 | printf(" Backup="); |
| 1082 | printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup); |
| 1083 | } |
| 1084 | printf("\n"); |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Check a disk geometry value. |
| 1089 | */ |
| 1090 | static int |
| 1091 | ckgeom(const char *fname, u_int val, const char *msg) |
| 1092 | { |
| 1093 | if (!val) { |
| 1094 | warnx("%s: no default %s", fname, msg); |
| 1095 | return -1; |
| 1096 | } |
| 1097 | if (val > MAXU16) { |
| 1098 | warnx("%s: illegal %s %d", fname, msg, val); |
| 1099 | return -1; |
| 1100 | } |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * Check a volume label. |
| 1106 | */ |
| 1107 | static int |
| 1108 | oklabel(const char *src) |
| 1109 | { |
| 1110 | int c, i; |
| 1111 | |
| 1112 | for (i = 0; i <= 11; i++) { |
| 1113 | c = (u_char)*src++; |
| 1114 | if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c)) |
| 1115 | break; |
| 1116 | } |
| 1117 | return i && !c; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | * Make a volume label. |
| 1122 | */ |
| 1123 | static void |
| 1124 | mklabel(u_int8_t *dest, const char *src) |
| 1125 | { |
| 1126 | int c, i; |
| 1127 | |
| 1128 | for (i = 0; i < 11; i++) { |
| 1129 | c = *src ? toupper(*src++) : ' '; |
| 1130 | *dest++ = !i && c == '\xe5' ? 5 : c; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * Copy string, padding with spaces. |
| 1136 | */ |
| 1137 | static void |
| 1138 | setstr(u_int8_t *dest, const char *src, size_t len) |
| 1139 | { |
| 1140 | while (len--) |
| 1141 | *dest++ = *src ? *src++ : ' '; |
| 1142 | } |
| 1143 | |
| 1144 | static void |
| 1145 | infohandler(int sig __unused) |
| 1146 | { |
| 1147 | |
| 1148 | got_siginfo = 1; |
| 1149 | } |