Home | History | Annotate | Line # | Download | only in newdisk
newdisk.c revision 1.4
      1  1.1  minoura /*-
      2  1.1  minoura  * Copyright (c) 1999 Minoura Makoto
      3  1.1  minoura  * All rights reserved.
      4  1.1  minoura  *
      5  1.1  minoura  * Redistribution and use in source and binary forms, with or without
      6  1.1  minoura  * modification, are permitted provided that the following conditions
      7  1.1  minoura  * are met:
      8  1.1  minoura  * 1. Redistributions of source code must retain the above copyright
      9  1.1  minoura  *    notice, this list of conditions and the following disclaimer.
     10  1.1  minoura  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  minoura  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  minoura  *    documentation and/or other materials provided with the distribution.
     13  1.1  minoura  * 3. All advertising materials mentioning features or use of this software
     14  1.1  minoura  *    must display the following acknowledgement:
     15  1.1  minoura  *	This product includes software developed by Minoura Makoto.
     16  1.1  minoura  * 4. The name of the author may not be used to endorse or promote products
     17  1.1  minoura  *    derived from this software without specific prior written permission.
     18  1.1  minoura  *
     19  1.1  minoura  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  minoura  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  minoura  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  minoura  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  minoura  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  minoura  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  minoura  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  minoura  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  minoura  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  minoura  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  minoura  */
     30  1.1  minoura 
     31  1.1  minoura /*
     32  1.1  minoura  * Create the disk mark for x68k SCSI IPL.
     33  1.1  minoura  * It used to be a shell/awk script, but is rewritten in order to be fit with
     34  1.1  minoura  * the install kernel.
     35  1.1  minoura  *
     36  1.1  minoura  * Usage: /usr/mdec/newdisk [-vnfc] [-m /usr/mdec/mboot] /dev/rsd?c
     37  1.1  minoura  */
     38  1.1  minoura 
     39  1.1  minoura #include <stdio.h>
     40  1.1  minoura #include <stdlib.h>
     41  1.1  minoura #include <string.h>
     42  1.1  minoura #include <fcntl.h>
     43  1.1  minoura #include <unistd.h>
     44  1.1  minoura #include <util.h>
     45  1.1  minoura #include <sys/param.h>
     46  1.1  minoura #include <sys/disklabel.h>
     47  1.1  minoura #include <sys/dkio.h>
     48  1.1  minoura 
     49  1.1  minoura char *prog;
     50  1.1  minoura char *mboot = MBOOT;
     51  1.1  minoura char dev[MAXPATHLEN];
     52  1.1  minoura char buf[4096 + 1];
     53  1.1  minoura 
     54  1.2  minoura const char copyright[] = "NetBSD/x68k SCSI Primary Boot. ";
     55  1.1  minoura 
     56  1.2  minoura int verbose = 0, dry_run = 0, force = 0, check_only = 0, mark_only = 0;
     57  1.1  minoura 
     58  1.4      dsl void usage(void) __attribute__((__noreturn__));
     59  1.4      dsl int main(int, char *[]);
     60  1.1  minoura 
     61  1.3   mhitch void
     62  1.1  minoura usage(void)
     63  1.1  minoura {
     64  1.1  minoura     fprintf(stderr,
     65  1.1  minoura 	    "Usage: %s [-v] [-n] [-f] [-c] [-m /usr/mdec/mboot] "
     66  1.1  minoura 	    "/dev/rsdXc\n", prog);
     67  1.1  minoura     exit(1);
     68  1.1  minoura     /* NOTREACHED */
     69  1.1  minoura }
     70  1.1  minoura 
     71  1.1  minoura int
     72  1.1  minoura main(argc, argv)
     73  1.1  minoura     int argc;
     74  1.1  minoura     char *argv[];
     75  1.1  minoura {
     76  1.1  minoura     extern int optind;
     77  1.1  minoura     int ch;
     78  1.1  minoura     int fd;
     79  1.1  minoura     struct disklabel label;
     80  1.1  minoura 
     81  1.1  minoura     prog = argv[0];
     82  1.2  minoura     while ((ch = getopt(argc, argv, "vnfcm:p")) != -1) {
     83  1.1  minoura 	switch (ch) {
     84  1.1  minoura 	case 'v':
     85  1.1  minoura 	    verbose = 1;
     86  1.1  minoura 	    break;
     87  1.1  minoura 	case 'n':
     88  1.1  minoura 	    dry_run = 1;
     89  1.1  minoura 	    break;
     90  1.1  minoura 	case 'f':
     91  1.1  minoura 	    force = 1;
     92  1.1  minoura 	    break;
     93  1.1  minoura 	case 'c':
     94  1.1  minoura 	    check_only = 1;
     95  1.1  minoura 	    break;
     96  1.1  minoura 	case 'm':
     97  1.1  minoura 	    mboot = optarg;
     98  1.1  minoura 	    break;
     99  1.2  minoura 	case 'p':
    100  1.2  minoura 	    mark_only = 1;
    101  1.2  minoura 	    break;
    102  1.1  minoura 	default:
    103  1.1  minoura 	    usage();
    104  1.1  minoura 	}
    105  1.1  minoura     }
    106  1.1  minoura     argc -= optind;
    107  1.1  minoura     argv += optind;
    108  1.1  minoura 
    109  1.1  minoura     if (argc != 1)
    110  1.1  minoura 	usage();
    111  1.1  minoura 
    112  1.1  minoura     fd = opendisk(argv[0], O_RDONLY, dev, MAXPATHLEN, 0);
    113  1.1  minoura     if (fd < 0)
    114  1.1  minoura 	err(1, "opening %s", dev);
    115  1.1  minoura     if (access(mboot, R_OK) < 0)
    116  1.1  minoura 	err(1, "checking %s", mboot);
    117  1.1  minoura 
    118  1.1  minoura     if (read(fd, buf, 512) < 0)
    119  1.1  minoura 	err(1, "reading %s", dev);
    120  1.1  minoura     if (strncmp(buf, "X68SCSI1", 8) == 0 &&
    121  1.1  minoura 	!force)
    122  1.1  minoura 	errx(1, "%s is already marked.  Use -f to overwrite the existing mark.");
    123  1.1  minoura     if (check_only)
    124  1.1  minoura 	return 0;
    125  1.1  minoura 
    126  1.1  minoura     if (verbose)
    127  1.1  minoura 	fprintf(stderr, "Inspecting %s... ", dev);
    128  1.1  minoura 
    129  1.1  minoura     if (ioctl(fd, DIOCGDINFO, &label) < 0)
    130  1.1  minoura 	err(1, "inspecting %s", dev);
    131  1.1  minoura     close(fd);
    132  1.1  minoura     if (label.d_secsize != 512)
    133  1.1  minoura 	errx(1, "This type of disk is not supported by NetBSD.");
    134  1.1  minoura 
    135  1.1  minoura     if (verbose)
    136  1.1  minoura 	fprintf(stderr, "total number of sector is %d.\n", label.d_secperunit);
    137  1.1  minoura 
    138  1.1  minoura     if (verbose)
    139  1.1  minoura 	fprintf(stderr, "Building disk mark... ");
    140  1.1  minoura     memset(buf, 0, 3072);
    141  1.1  minoura #define n label.d_secperunit
    142  1.1  minoura     sprintf(buf, "X68SCSI1%c%c%c%c%c%c%c%c%s",
    143  1.1  minoura 	    2, 0,
    144  1.1  minoura 	    (n/16777216)%256, (n/65536)%256, (n/256)%256, n%256,
    145  1.1  minoura 	    1, 0, copyright);
    146  1.1  minoura #undef n
    147  1.1  minoura     if (verbose)
    148  1.1  minoura 	fprintf(stderr, "done.\n");
    149  1.1  minoura 
    150  1.1  minoura     if (verbose)
    151  1.1  minoura 	fprintf(stderr, "Merging %s... ", mboot);
    152  1.1  minoura     fd = open(mboot, O_RDONLY);
    153  1.1  minoura     if (fd < 0)
    154  1.1  minoura 	err(1, "opening %s", mboot);
    155  1.1  minoura     if (read(fd, buf+1024, 1024) < 0)
    156  1.1  minoura 	err(1, "reading %s", mboot);
    157  1.1  minoura     close(fd);
    158  1.1  minoura     if (verbose)
    159  1.1  minoura 	fprintf(stderr, "done.\n");
    160  1.1  minoura 
    161  1.2  minoura     if (!mark_only) {
    162  1.2  minoura 	if (verbose)
    163  1.2  minoura 	    fprintf(stderr, "Creating an empty partition table... ");
    164  1.1  minoura #define n (label.d_secperunit/2)
    165  1.2  minoura 	sprintf(buf+2048,
    166  1.2  minoura 		"X68K%c%c%c%c%c%c%c%c%c%c%c%c",
    167  1.2  minoura 		0, 0, 0, 32,
    168  1.2  minoura 		(n/16777215)%256, (n/65536)%256, (n/256)%256, n%256,
    169  1.2  minoura 		(n/16777215)%256, (n/65536)%256, (n/256)%256, n%256);
    170  1.1  minoura #undef n
    171  1.2  minoura 	if (verbose)
    172  1.2  minoura 	    fprintf(stderr, "done.\n");
    173  1.2  minoura     }
    174  1.1  minoura 
    175  1.1  minoura     if (dry_run) {
    176  1.1  minoura 	char filename[MAXPATHLEN] = "/tmp/diskmarkXXXXX";
    177  1.1  minoura 	fd = mkstemp(filename);
    178  1.1  minoura 	if (fd < 0)
    179  1.1  minoura 	    err(1, "opening %s", filename);
    180  1.1  minoura 	if (write(fd, buf, 4096) < 0)
    181  1.1  minoura 	    err(1, "writing %s", filename);
    182  1.1  minoura 	close(fd);
    183  1.1  minoura 	fprintf(stderr, "Disk mark is kept in %s.\n", filename);
    184  1.1  minoura     } else {
    185  1.1  minoura 	int mode = 1;
    186  1.1  minoura 
    187  1.1  minoura 	if (verbose)
    188  1.1  minoura 	    fprintf(stderr, "Writing... ");
    189  1.1  minoura 	fd = open(dev, O_WRONLY);
    190  1.1  minoura 	if (fd < 0)
    191  1.1  minoura 	    err(1, "opening %s", dev);
    192  1.1  minoura 	if (ioctl(fd, DIOCWLABEL, (char *)&mode) < 0)
    193  1.1  minoura 	    err(1, "DIOCWLABEL %s", dev);
    194  1.1  minoura 	if (write(fd, buf, 4096) != 4096) {
    195  1.1  minoura 	    mode = 0;
    196  1.1  minoura 	    ioctl(fd, DIOCWLABEL, (char *)&mode);
    197  1.1  minoura 	    err(1, "DIOCWLABEL %s", dev);
    198  1.1  minoura 	}
    199  1.1  minoura 	ioctl(fd, DIOCWLABEL, (char *)&mode);
    200  1.1  minoura 	if (verbose)
    201  1.1  minoura 	    fprintf(stderr, "done.\n");
    202  1.1  minoura 	close(fd);
    203  1.1  minoura     }
    204  1.1  minoura 
    205  1.1  minoura     return 0;
    206  1.1  minoura }
    207