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