Deleted Added
full compact
newfs_msdos.c (185345) newfs_msdos.c (185587)
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

--- 13 unchanged lines hidden (view full) ---

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
29static const char rcsid[] =
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

--- 13 unchanged lines hidden (view full) ---

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
29static const char rcsid[] =
30 "$FreeBSD: head/sbin/newfs_msdos/newfs_msdos.c 185345 2008-11-26 21:05:03Z luigi $";
30 "$FreeBSD: head/sbin/newfs_msdos/newfs_msdos.c 185587 2008-12-03 18:22:36Z luigi $";
31#endif /* not lint */
32
33#include <sys/param.h>
34#include <sys/fdcio.h>
35#include <sys/disk.h>
36#include <sys/disklabel.h>
37#include <sys/mount.h>
38#include <sys/stat.h>

--- 175 unchanged lines hidden (view full) ---

214
215static void check_mounted(const char *, mode_t);
216static void getstdfmt(const char *, struct bpb *);
217static void getdiskinfo(int, const char *, const char *, int,
218 struct bpb *);
219static void print_bpb(struct bpb *);
220static u_int ckgeom(const char *, u_int, const char *);
221static u_int argtou(const char *, u_int, u_int, const char *);
31#endif /* not lint */
32
33#include <sys/param.h>
34#include <sys/fdcio.h>
35#include <sys/disk.h>
36#include <sys/disklabel.h>
37#include <sys/mount.h>
38#include <sys/stat.h>

--- 175 unchanged lines hidden (view full) ---

214
215static void check_mounted(const char *, mode_t);
216static void getstdfmt(const char *, struct bpb *);
217static void getdiskinfo(int, const char *, const char *, int,
218 struct bpb *);
219static void print_bpb(struct bpb *);
220static u_int ckgeom(const char *, u_int, const char *);
221static u_int argtou(const char *, u_int, u_int, const char *);
222static off_t argtooff(const char *, const char *);
222static int oklabel(const char *);
223static void mklabel(u_int8_t *, const char *);
224static void setstr(u_int8_t *, const char *, size_t);
225static void usage(void);
226
227/*
228 * Construct a FAT12, FAT16, or FAT32 file system.
229 */
230int
231main(int argc, char *argv[])
232{
223static int oklabel(const char *);
224static void mklabel(u_int8_t *, const char *);
225static void setstr(u_int8_t *, const char *, size_t);
226static void usage(void);
227
228/*
229 * Construct a FAT12, FAT16, or FAT32 file system.
230 */
231int
232main(int argc, char *argv[])
233{
233 static char opts[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
234 static char opts[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
234 static const char *opt_B, *opt_L, *opt_O, *opt_f;
235 static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
236 static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
237 static u_int opt_s, opt_u;
238 static int opt_N;
239 static int Iflag, mflag, oflag;
240 char buf[MAXPATHLEN];
241 struct stat sb;

--- 6 unchanged lines hidden (view full) ---

248 struct bsx *bsx;
249 struct de *de;
250 u_int8_t *img;
251 const char *fname, *dtype, *bname;
252 ssize_t n;
253 time_t now;
254 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
255 int ch, fd, fd1;
235 static const char *opt_B, *opt_L, *opt_O, *opt_f;
236 static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
237 static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
238 static u_int opt_s, opt_u;
239 static int opt_N;
240 static int Iflag, mflag, oflag;
241 char buf[MAXPATHLEN];
242 struct stat sb;

--- 6 unchanged lines hidden (view full) ---

249 struct bsx *bsx;
250 struct de *de;
251 u_int8_t *img;
252 const char *fname, *dtype, *bname;
253 ssize_t n;
254 time_t now;
255 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
256 int ch, fd, fd1;
257 static off_t opt_create=0, opt_ofs=0;
256
257 while ((ch = getopt(argc, argv, opts)) != -1)
258 switch (ch) {
258
259 while ((ch = getopt(argc, argv, opts)) != -1)
260 switch (ch) {
261 case '@':
262 opt_ofs = argtooff(optarg, "offset");
263 break;
259 case 'N':
260 opt_N = 1;
261 break;
262 case 'B':
263 opt_B = optarg;
264 break;
264 case 'N':
265 opt_N = 1;
266 break;
267 case 'B':
268 opt_B = optarg;
269 break;
270 case 'C':
271 opt_create = argtooff(optarg, "create size");
272 break;
265 case 'F':
266 if (strcmp(optarg, "12") &&
267 strcmp(optarg, "16") &&
268 strcmp(optarg, "32"))
269 errx(1, "%s: bad FAT type", optarg);
270 opt_F = atoi(optarg);
271 break;
272 case 'I':

--- 68 unchanged lines hidden (view full) ---

341 usage();
342 fname = *argv++;
343 if (!strchr(fname, '/')) {
344 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
345 if (!(fname = strdup(buf)))
346 err(1, NULL);
347 }
348 dtype = *argv;
273 case 'F':
274 if (strcmp(optarg, "12") &&
275 strcmp(optarg, "16") &&
276 strcmp(optarg, "32"))
277 errx(1, "%s: bad FAT type", optarg);
278 opt_F = atoi(optarg);
279 break;
280 case 'I':

--- 68 unchanged lines hidden (view full) ---

349 usage();
350 fname = *argv++;
351 if (!strchr(fname, '/')) {
352 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
353 if (!(fname = strdup(buf)))
354 err(1, NULL);
355 }
356 dtype = *argv;
349 if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1 ||
357 if (opt_create) {
358 off_t pos;
359
360 if (opt_N)
361 errx(1, "create (-C) is incompatible with -N");
362 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
363 if (fd == -1)
364 errx(1, "failed to create %s", fname);
365 pos = lseek(fd, opt_create - 1, SEEK_SET);
366 if (write(fd, "\0", 1) != 1)
367 errx(1, "failed to initialize %lld bytes", opt_create);
368 pos = lseek(fd, 0, SEEK_SET);
369 } else if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1 ||
350 fstat(fd, &sb))
351 err(1, "%s", fname);
352 if (!opt_N)
353 check_mounted(fname, sb.st_mode);
354 if (!S_ISCHR(sb.st_mode))
370 fstat(fd, &sb))
371 err(1, "%s", fname);
372 if (!opt_N)
373 check_mounted(fname, sb.st_mode);
374 if (!S_ISCHR(sb.st_mode))
355 warnx("warning: %s is not a character device", fname);
375 warnx("warning, %s is not a character device", fname);
376 if (opt_ofs && opt_ofs != lseek(fd, opt_ofs, SEEK_SET))
377 errx(1, "cannot seek to %lld", opt_ofs);
356 memset(&bpb, 0, sizeof(bpb));
357 if (opt_f) {
358 getstdfmt(opt_f, &bpb);
359 bpb.bsec = bpb.sec;
360 bpb.sec = 0;
361 bpb.bspf = bpb.spf;
362 bpb.spf = 0;
363 }
364 if (opt_h)
365 bpb.hds = opt_h;
366 if (opt_u)
367 bpb.spt = opt_u;
368 if (opt_S)
369 bpb.bps = opt_S;
370 if (opt_s)
371 bpb.bsec = opt_s;
372 if (oflag)
373 bpb.hid = opt_o;
378 memset(&bpb, 0, sizeof(bpb));
379 if (opt_f) {
380 getstdfmt(opt_f, &bpb);
381 bpb.bsec = bpb.sec;
382 bpb.sec = 0;
383 bpb.bspf = bpb.spf;
384 bpb.spf = 0;
385 }
386 if (opt_h)
387 bpb.hds = opt_h;
388 if (opt_u)
389 bpb.spt = opt_u;
390 if (opt_S)
391 bpb.bps = opt_S;
392 if (opt_s)
393 bpb.bsec = opt_s;
394 if (oflag)
395 bpb.hid = opt_o;
374 if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag)))
396 if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag))) {
397 off_t delta;
375 getdiskinfo(fd, fname, dtype, oflag, &bpb);
398 getdiskinfo(fd, fname, dtype, oflag, &bpb);
399 bpb.bsec -= (opt_ofs / bpb.bps);
400 delta = bpb.bsec % bpb.spt;
401 if (delta != 0) {
402 warnx("trim %d sectors to adjust to a multiple of %d",
403 (int)delta, bpb.spt);
404 bpb.bsec -= delta;
405 }
406 if (bpb.spc == 0) { /* set defaults */
407 if (bpb.bsec <= 6000) /* about 3MB -> 512 bytes */
408 bpb.spc = 1;
409 else if (bpb.bsec <= (1<<17)) /* 64M -> 4k */
410 bpb.spc = 8;
411 else if (bpb.bsec <= (1<<19)) /* 256M -> 8k */
412 bpb.spc = 16;
413 else if (bpb.bsec <= (1<<21)) /* 1G -> 16k */
414 bpb.spc = 32;
415 else
416 bpb.spc = 64; /* otherwise 32k */
417 }
418 }
376 if (!powerof2(bpb.bps))
377 errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
378 if (bpb.bps < MINBPS)
379 errx(1, "bytes/sector (%u) is too small; minimum is %u",
380 bpb.bps, MINBPS);
381 if (!(fat = opt_F)) {
382 if (opt_f)
383 fat = 12;

--- 172 unchanged lines hidden (view full) ---

556 err(1, NULL);
557 dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
558 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
559 x = lsn;
560 if (opt_B &&
561 fat == 32 && bpb.bkbs != MAXU16 &&
562 bss <= bpb.bkbs && x >= bpb.bkbs) {
563 x -= bpb.bkbs;
419 if (!powerof2(bpb.bps))
420 errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
421 if (bpb.bps < MINBPS)
422 errx(1, "bytes/sector (%u) is too small; minimum is %u",
423 bpb.bps, MINBPS);
424 if (!(fat = opt_F)) {
425 if (opt_f)
426 fat = 12;

--- 172 unchanged lines hidden (view full) ---

599 err(1, NULL);
600 dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
601 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
602 x = lsn;
603 if (opt_B &&
604 fat == 32 && bpb.bkbs != MAXU16 &&
605 bss <= bpb.bkbs && x >= bpb.bkbs) {
606 x -= bpb.bkbs;
564 if (!x && lseek(fd1, 0, SEEK_SET))
607 if (!x && lseek(fd1, opt_ofs, SEEK_SET))
565 err(1, "%s", bname);
566 }
567 if (opt_B && x < bss) {
568 if ((n = read(fd1, img, bpb.bps)) == -1)
569 err(1, "%s", bname);
570 if ((unsigned)n != bpb.bps)
571 errx(1, "%s: can't read sector %u", bname, x);
572 } else

--- 150 unchanged lines hidden (view full) ---

723 lp = getdiskbyname(dtype);
724 }
725
726 /* Maybe it's a floppy drive */
727 if (lp == NULL) {
728 if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
729 struct stat st;
730
608 err(1, "%s", bname);
609 }
610 if (opt_B && x < bss) {
611 if ((n = read(fd1, img, bpb.bps)) == -1)
612 err(1, "%s", bname);
613 if ((unsigned)n != bpb.bps)
614 errx(1, "%s: can't read sector %u", bname, x);
615 } else

--- 150 unchanged lines hidden (view full) ---

766 lp = getdiskbyname(dtype);
767 }
768
769 /* Maybe it's a floppy drive */
770 if (lp == NULL) {
771 if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
772 struct stat st;
773
731 bzero(&st, sizeof(st));
732 if (fstat(fd, &st))
733 err(1, "Cannot get disk size");
734 /* create a fake geometry for a file image */
735 ms = st.st_size;
736 dlp.d_secsize = 512;
774 if (fstat(fd, &st))
775 err(1, "Cannot get disk size");
776 /* create a fake geometry for a file image */
777 ms = st.st_size;
778 dlp.d_secsize = 512;
737 dlp.d_nsectors = 64;
738 dlp.d_ntracks = 32;
779 dlp.d_nsectors = 63;
780 dlp.d_ntracks = 255;
739 dlp.d_secperunit = ms / dlp.d_secsize;
740 lp = &dlp;
741 } else if (ioctl(fd, FD_GTYPE, &type) != -1) {
742 dlp.d_secsize = 128 << type.secsize;
743 dlp.d_nsectors = type.sectrac;
744 dlp.d_ntracks = type.heads;
745 dlp.d_secperunit = ms / dlp.d_secsize;
746 lp = &dlp;

--- 81 unchanged lines hidden (view full) ---

828 errno = 0;
829 x = strtoul(arg, &s, 0);
830 if (errno || !*arg || *s || x < lo || x > hi)
831 errx(1, "%s: bad %s", arg, msg);
832 return x;
833}
834
835/*
781 dlp.d_secperunit = ms / dlp.d_secsize;
782 lp = &dlp;
783 } else if (ioctl(fd, FD_GTYPE, &type) != -1) {
784 dlp.d_secsize = 128 << type.secsize;
785 dlp.d_nsectors = type.sectrac;
786 dlp.d_ntracks = type.heads;
787 dlp.d_secperunit = ms / dlp.d_secsize;
788 lp = &dlp;

--- 81 unchanged lines hidden (view full) ---

870 errno = 0;
871 x = strtoul(arg, &s, 0);
872 if (errno || !*arg || *s || x < lo || x > hi)
873 errx(1, "%s: bad %s", arg, msg);
874 return x;
875}
876
877/*
878 * Same for off_t, with optional skmgpP suffix
879 */
880static off_t
881argtooff(const char *arg, const char *msg)
882{
883 char *s;
884 off_t x;
885
886 x = strtoll(arg, &s, 0);
887 /* allow at most one extra char */
888 if (errno || x < 0 || (s[0] && s[1]) )
889 errx(1, "%s: bad %s", arg, msg);
890 if (*s) { /* the extra char is the multiplier */
891 switch (*s) {
892 default:
893 errx(1, "%s: bad %s", arg, msg);
894 /* notreached */
895
896 case 's': /* sector */
897 case 'S':
898 x <<= 9; /* times 512 */
899 break;
900
901 case 'k': /* kilobyte */
902 case 'K':
903 x <<= 10; /* times 1024 */
904 break;
905
906 case 'm': /* megabyte */
907 case 'M':
908 x <<= 20; /* times 1024*1024 */
909 break;
910
911 case 'g': /* gigabyte */
912 case 'G':
913 x <<= 30; /* times 1024*1024*1024 */
914 break;
915
916 case 'p': /* partition start */
917 case 'P': /* partition start */
918 case 'l': /* partition length */
919 case 'L': /* partition length */
920 errx(1, "%s: not supported yet %s", arg, msg);
921 /* notreached */
922 }
923 }
924 return x;
925}
926
927/*
836 * Check a volume label.
837 */
838static int
839oklabel(const char *src)
840{
841 int c, i;
842
843 for (i = 0; i <= 11; i++) {

--- 64 unchanged lines hidden ---
928 * Check a volume label.
929 */
930static int
931oklabel(const char *src)
932{
933 int c, i;
934
935 for (i = 0; i <= 11; i++) {

--- 64 unchanged lines hidden ---