Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/newfs_msdos Split the make fs portion into a function; ...



details:   https://anonhg.NetBSD.org/src/rev/0ab9fe8d785e
branches:  trunk
changeset: 784201:0ab9fe8d785e
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Jan 21 20:28:38 2013 +0000

description:
Split the make fs portion into a function; do argument tests inside the
function.

diffstat:

 sbin/newfs_msdos/Makefile      |    4 +-
 sbin/newfs_msdos/mkfs_msdos.c  |  940 +++++++++++++++++++++++++++++++++++++++++
 sbin/newfs_msdos/mkfs_msdos.h  |   71 +++
 sbin/newfs_msdos/newfs_msdos.c |  889 ++------------------------------------
 4 files changed, 1065 insertions(+), 839 deletions(-)

diffs (truncated from 2043 to 300 lines):

diff -r 29b727adfb6e -r 0ab9fe8d785e sbin/newfs_msdos/Makefile
--- a/sbin/newfs_msdos/Makefile Mon Jan 21 20:20:32 2013 +0000
+++ b/sbin/newfs_msdos/Makefile Mon Jan 21 20:28:38 2013 +0000
@@ -1,11 +1,11 @@
-# $NetBSD: Makefile,v 1.5 2009/06/05 21:52:31 haad Exp $
+# $NetBSD: Makefile,v 1.6 2013/01/21 20:28:38 christos Exp $
 # From: $FreeBSD: src/sbin/newfs_msdos/Makefile,v 1.5 2001/03/26 14:33:18 ru Exp $
 
 .include <bsd.own.mk>
 
 PROG=  newfs_msdos
 MAN=   newfs_msdos.8
-SRCS=  newfs_msdos.c partutil.c
+SRCS=  newfs_msdos.c partutil.c mkfs_msdos.c
 
 LDADD+= -lutil
 DPADD+= ${LIBUTIL}
diff -r 29b727adfb6e -r 0ab9fe8d785e sbin/newfs_msdos/mkfs_msdos.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sbin/newfs_msdos/mkfs_msdos.c     Mon Jan 21 20:28:38 2013 +0000
@@ -0,0 +1,940 @@
+/*     $NetBSD: mkfs_msdos.c,v 1.1 2013/01/21 20:28:38 christos Exp $  */
+
+/*
+ * Copyright (c) 1998 Robert Nordier
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+#if 0
+static const char rcsid[] =
+  "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.15 2000/10/10 01:49:37 wollman Exp $";
+#else
+__RCSID("$NetBSD: mkfs_msdos.c,v 1.1 2013/01/21 20:28:38 christos Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/disk.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <signal.h>
+
+#include <util.h>
+#include <disktab.h>
+
+#include "partutil.h"
+#include "mkfs_msdos.h"
+
+#define MAXU16   0xffff        /* maximum unsigned 16-bit quantity */
+#define BPN      4             /* bits per nibble */
+#define NPB      2             /* nibbles per byte */
+
+#define DOSMAGIC  0xaa55       /* DOS magic number */
+#define MINBPS   512           /* minimum bytes per sector */
+#define MAXSPC   128           /* maximum sectors per cluster */
+#define MAXNFT   16            /* maximum number of FATs */
+#define DEFBLK   4096          /* default block size */
+#define DEFBLK16  2048         /* default block size FAT16 */
+#define DEFRDE   512           /* default root directory entries */
+#define RESFTE   2             /* reserved FAT entries */
+#define MINCLS12  1            /* minimum FAT12 clusters */
+#define MINCLS16  0xff5                /* minimum FAT16 clusters */
+#define MINCLS32  0xfff5       /* minimum FAT32 clusters */
+#define MAXCLS12  0xff4        /* maximum FAT12 clusters */
+#define MAXCLS16  0xfff4       /* maximum FAT16 clusters */
+#define MAXCLS32  0xffffff4    /* maximum FAT32 clusters */
+
+#define mincls(fat_type)  ((fat_type) == 12 ? MINCLS12 :       \
+                     (fat_type) == 16 ? MINCLS16 :     \
+                                   MINCLS32)
+
+#define maxcls(fat_type)  ((fat_type) == 12 ? MAXCLS12 :       \
+                     (fat_type) == 16 ? MAXCLS16 :     \
+                                   MAXCLS32)
+
+#define mk1(p, x)                              \
+    (p) = (u_int8_t)(x)
+
+#define mk2(p, x)                              \
+    (p)[0] = (u_int8_t)(x),                    \
+    (p)[1] = (u_int8_t)((x) >> 010)
+
+#define mk4(p, x)                              \
+    (p)[0] = (u_int8_t)(x),                    \
+    (p)[1] = (u_int8_t)((x) >> 010),           \
+    (p)[2] = (u_int8_t)((x) >> 020),           \
+    (p)[3] = (u_int8_t)((x) >> 030)
+
+struct bs {
+    u_int8_t jmp[3];           /* bootstrap entry point */
+    u_int8_t oem[8];           /* OEM name and version */
+};
+
+struct bsbpb {
+    u_int8_t bps[2];           /* bytes per sector */
+    u_int8_t spc;              /* sectors per cluster */
+    u_int8_t res[2];           /* reserved sectors */
+    u_int8_t nft;              /* number of FATs */
+    u_int8_t rde[2];           /* root directory entries */
+    u_int8_t sec[2];           /* total sectors */
+    u_int8_t mid;              /* media descriptor */
+    u_int8_t spf[2];           /* sectors per FAT */
+    u_int8_t spt[2];           /* sectors per track */
+    u_int8_t hds[2];           /* drive heads */
+    u_int8_t hid[4];           /* hidden sectors */
+    u_int8_t bsec[4];          /* big total sectors */
+};
+
+struct bsxbpb {
+    u_int8_t bspf[4];          /* big sectors per FAT */
+    u_int8_t xflg[2];          /* FAT control flags */
+    u_int8_t vers[2];          /* file system version */
+    u_int8_t rdcl[4];          /* root directory start cluster */
+    u_int8_t infs[2];          /* file system info sector */
+    u_int8_t bkbs[2];          /* backup boot sector */
+    u_int8_t rsvd[12];         /* reserved */
+};
+
+struct bsx {
+    u_int8_t drv;              /* drive number */
+    u_int8_t rsvd;             /* reserved */
+    u_int8_t sig;              /* extended boot signature */
+    u_int8_t volid[4];         /* volume ID number */
+    u_int8_t label[11];        /* volume label */
+    u_int8_t type[8];          /* file system type */
+};
+
+struct de {
+    u_int8_t namext[11];       /* name and extension */
+    u_int8_t attr;             /* attributes */
+    u_int8_t rsvd[10];         /* reserved */
+    u_int8_t time[2];          /* creation time */
+    u_int8_t date[2];          /* creation date */
+    u_int8_t clus[2];          /* starting cluster */
+    u_int8_t size[4];          /* size */
+};
+
+struct bpb {
+    u_int bps;                 /* bytes per sector */
+    u_int spc;                 /* sectors per cluster */
+    u_int res;                 /* reserved sectors */
+    u_int nft;                 /* number of FATs */
+    u_int rde;                 /* root directory entries */
+    u_int sec;                 /* total sectors */
+    u_int mid;                 /* media descriptor */
+    u_int spf;                 /* sectors per FAT */
+    u_int spt;                 /* sectors per track */
+    u_int hds;                 /* drive heads */
+    u_int hid;                 /* hidden sectors */
+    u_int bsec;                /* big total sectors */
+    u_int bspf;                /* big sectors per FAT */
+    u_int rdcl;                /* root directory start cluster */
+    u_int infs;                /* file system info sector */
+    u_int bkbs;                /* backup boot sector */
+};
+
+#define INIT(a, b, c, d, e, f, g, h, i, j) \
+    { .bps = a, .spc = b, .res = c, .nft = d, .rde = e, \
+      .sec = f, .mid = g, .spf = h, .spt = i, .hds = j, }
+static struct {
+    const char *name;
+    struct bpb bpb;
+} stdfmt[] = {
+    {"160",  INIT(512, 1, 1, 2,  64,  320, 0xfe, 1,  8, 1)},
+    {"180",  INIT(512, 1, 1, 2,  64,  360, 0xfc, 2,  9, 1)},
+    {"320",  INIT(512, 2, 1, 2, 112,  640, 0xff, 1,  8, 2)},
+    {"360",  INIT(512, 2, 1, 2, 112,  720, 0xfd, 2,  9, 2)},
+    {"640",  INIT(512, 2, 1, 2, 112, 1280, 0xfb, 2,  8, 2)},    
+    {"720",  INIT(512, 2, 1, 2, 112, 1440, 0xf9, 3,  9, 2)},
+    {"1200", INIT(512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2)},
+    {"1232", INIT(1024,1, 1, 2, 192, 1232, 0xfe, 2,  8, 2)},    
+    {"1440", INIT(512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2)},
+    {"2880", INIT(512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2)}
+};
+
+static u_int8_t bootcode[] = {
+    0xfa,                      /* cli              */
+    0x31, 0xc0,                /* xor     ax,ax    */
+    0x8e, 0xd0,                /* mov     ss,ax    */
+    0xbc, 0x00, 0x7c,          /* mov     sp,7c00h */
+    0xfb,                      /* sti              */
+    0x8e, 0xd8,                /* mov     ds,ax    */
+    0xe8, 0x00, 0x00,          /* call    $ + 3    */
+    0x5e,                      /* pop     si       */
+    0x83, 0xc6, 0x19,          /* add     si,+19h  */
+    0xbb, 0x07, 0x00,          /* mov     bx,0007h */
+    0xfc,                      /* cld              */
+    0xac,                      /* lodsb            */
+    0x84, 0xc0,                /* test    al,al    */
+    0x74, 0x06,                /* jz      $ + 8    */
+    0xb4, 0x0e,                /* mov     ah,0eh   */
+    0xcd, 0x10,                /* int     10h      */
+    0xeb, 0xf5,                /* jmp     $ - 9    */
+    0x30, 0xe4,                /* xor     ah,ah    */
+    0xcd, 0x16,                /* int     16h      */
+    0xcd, 0x19,                /* int     19h      */
+    0x0d, 0x0a,
+    'N', 'o', 'n', '-', 's', 'y', 's', 't',
+    'e', 'm', ' ', 'd', 'i', 's', 'k',
+    0x0d, 0x0a,
+    'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
+    'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
+    ' ', 'r', 'e', 'b', 'o', 'o', 't',
+    0x0d, 0x0a,
+    0
+};
+
+static int got_siginfo = 0; /* received a SIGINFO */
+
+static int check_mounted(const char *, mode_t);
+static int getstdfmt(const char *, struct bpb *);
+static int getbpbinfo(int, const char *, const char *, int, struct bpb *, int);
+static void print_bpb(struct bpb *);
+static int ckgeom(const char *, u_int, const char *);
+static int oklabel(const char *);
+static void mklabel(u_int8_t *, const char *);
+static void setstr(u_int8_t *, const char *, size_t);
+static void infohandler(int sig);
+
+int
+mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
+{
+    char buf[MAXPATHLEN];
+    struct stat sb;
+    struct timeval tv;
+    struct bpb bpb;
+    struct tm *tm;
+    struct bs *bs;
+    struct bsbpb *bsbpb;
+    struct bsxbpb *bsxbpb;
+    struct bsx *bsx;
+    struct de *de;
+    u_int8_t *img;
+    const char *bname;
+    ssize_t n;
+    time_t now;
+    u_int bss, rds, cls, dir, lsn, x, x1, x2;
+    int ch, fd, fd1;
+    struct msdos_options o = *op;
+
+    if (o.block_size && o.sectors_per_cluster) {
+       warnx("Cannot specify both block size and sectors per cluster");
+       return -1;
+    }
+    if (strlen(o.OEM_string) > 8) {
+       warnx("%s: bad OEM string", o.OEM_string);
+       return -1;
+    }
+    if (o.create_size) {
+       off_t pos;
+       if (o.no_create) {
+           warnx("create (-C) is incompatible with -N");
+           return -1;
+       }
+       fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
+       if (fd == -1) {
+           warnx("failed to create %s", fname);
+           return -1;
+       }
+       pos = lseek(fd, o.create_size - 1, SEEK_SET);
+       if (write(fd, "\0", 1) != 1) {
+           warn("failed to set file size");
+           return -1;



Home | Main Index | Thread Index | Old Index