Home | History | Annotate | Line # | Download | only in newdisk
newdisk.c revision 1.3.76.1
      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.3.76.1      jym void usage(void) __attribute__((__noreturn__));
     59  1.3.76.1      jym 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.3.76.1      jym main(int argc, char *argv[])
     73       1.1  minoura {
     74       1.1  minoura     extern int optind;
     75       1.1  minoura     int ch;
     76       1.1  minoura     int fd;
     77       1.1  minoura     struct disklabel label;
     78       1.1  minoura 
     79       1.1  minoura     prog = argv[0];
     80       1.2  minoura     while ((ch = getopt(argc, argv, "vnfcm:p")) != -1) {
     81       1.1  minoura 	switch (ch) {
     82       1.1  minoura 	case 'v':
     83       1.1  minoura 	    verbose = 1;
     84       1.1  minoura 	    break;
     85       1.1  minoura 	case 'n':
     86       1.1  minoura 	    dry_run = 1;
     87       1.1  minoura 	    break;
     88       1.1  minoura 	case 'f':
     89       1.1  minoura 	    force = 1;
     90       1.1  minoura 	    break;
     91       1.1  minoura 	case 'c':
     92       1.1  minoura 	    check_only = 1;
     93       1.1  minoura 	    break;
     94       1.1  minoura 	case 'm':
     95       1.1  minoura 	    mboot = optarg;
     96       1.1  minoura 	    break;
     97       1.2  minoura 	case 'p':
     98       1.2  minoura 	    mark_only = 1;
     99       1.2  minoura 	    break;
    100       1.1  minoura 	default:
    101       1.1  minoura 	    usage();
    102       1.1  minoura 	}
    103       1.1  minoura     }
    104       1.1  minoura     argc -= optind;
    105       1.1  minoura     argv += optind;
    106       1.1  minoura 
    107       1.1  minoura     if (argc != 1)
    108       1.1  minoura 	usage();
    109       1.1  minoura 
    110       1.1  minoura     fd = opendisk(argv[0], O_RDONLY, dev, MAXPATHLEN, 0);
    111       1.1  minoura     if (fd < 0)
    112       1.1  minoura 	err(1, "opening %s", dev);
    113       1.1  minoura     if (access(mboot, R_OK) < 0)
    114       1.1  minoura 	err(1, "checking %s", mboot);
    115       1.1  minoura 
    116       1.1  minoura     if (read(fd, buf, 512) < 0)
    117       1.1  minoura 	err(1, "reading %s", dev);
    118       1.1  minoura     if (strncmp(buf, "X68SCSI1", 8) == 0 &&
    119       1.1  minoura 	!force)
    120       1.1  minoura 	errx(1, "%s is already marked.  Use -f to overwrite the existing mark.");
    121       1.1  minoura     if (check_only)
    122       1.1  minoura 	return 0;
    123       1.1  minoura 
    124       1.1  minoura     if (verbose)
    125       1.1  minoura 	fprintf(stderr, "Inspecting %s... ", dev);
    126       1.1  minoura 
    127       1.1  minoura     if (ioctl(fd, DIOCGDINFO, &label) < 0)
    128       1.1  minoura 	err(1, "inspecting %s", dev);
    129       1.1  minoura     close(fd);
    130       1.1  minoura     if (label.d_secsize != 512)
    131       1.1  minoura 	errx(1, "This type of disk is not supported by NetBSD.");
    132       1.1  minoura 
    133       1.1  minoura     if (verbose)
    134       1.1  minoura 	fprintf(stderr, "total number of sector is %d.\n", label.d_secperunit);
    135       1.1  minoura 
    136       1.1  minoura     if (verbose)
    137       1.1  minoura 	fprintf(stderr, "Building disk mark... ");
    138       1.1  minoura     memset(buf, 0, 3072);
    139       1.1  minoura #define n label.d_secperunit
    140       1.1  minoura     sprintf(buf, "X68SCSI1%c%c%c%c%c%c%c%c%s",
    141       1.1  minoura 	    2, 0,
    142       1.1  minoura 	    (n/16777216)%256, (n/65536)%256, (n/256)%256, n%256,
    143       1.1  minoura 	    1, 0, copyright);
    144       1.1  minoura #undef n
    145       1.1  minoura     if (verbose)
    146       1.1  minoura 	fprintf(stderr, "done.\n");
    147       1.1  minoura 
    148       1.1  minoura     if (verbose)
    149       1.1  minoura 	fprintf(stderr, "Merging %s... ", mboot);
    150       1.1  minoura     fd = open(mboot, O_RDONLY);
    151       1.1  minoura     if (fd < 0)
    152       1.1  minoura 	err(1, "opening %s", mboot);
    153       1.1  minoura     if (read(fd, buf+1024, 1024) < 0)
    154       1.1  minoura 	err(1, "reading %s", mboot);
    155       1.1  minoura     close(fd);
    156       1.1  minoura     if (verbose)
    157       1.1  minoura 	fprintf(stderr, "done.\n");
    158       1.1  minoura 
    159       1.2  minoura     if (!mark_only) {
    160       1.2  minoura 	if (verbose)
    161       1.2  minoura 	    fprintf(stderr, "Creating an empty partition table... ");
    162       1.1  minoura #define n (label.d_secperunit/2)
    163       1.2  minoura 	sprintf(buf+2048,
    164       1.2  minoura 		"X68K%c%c%c%c%c%c%c%c%c%c%c%c",
    165       1.2  minoura 		0, 0, 0, 32,
    166       1.2  minoura 		(n/16777215)%256, (n/65536)%256, (n/256)%256, n%256,
    167       1.2  minoura 		(n/16777215)%256, (n/65536)%256, (n/256)%256, n%256);
    168       1.1  minoura #undef n
    169       1.2  minoura 	if (verbose)
    170       1.2  minoura 	    fprintf(stderr, "done.\n");
    171       1.2  minoura     }
    172       1.1  minoura 
    173       1.1  minoura     if (dry_run) {
    174       1.1  minoura 	char filename[MAXPATHLEN] = "/tmp/diskmarkXXXXX";
    175       1.1  minoura 	fd = mkstemp(filename);
    176       1.1  minoura 	if (fd < 0)
    177       1.1  minoura 	    err(1, "opening %s", filename);
    178       1.1  minoura 	if (write(fd, buf, 4096) < 0)
    179       1.1  minoura 	    err(1, "writing %s", filename);
    180       1.1  minoura 	close(fd);
    181       1.1  minoura 	fprintf(stderr, "Disk mark is kept in %s.\n", filename);
    182       1.1  minoura     } else {
    183       1.1  minoura 	int mode = 1;
    184       1.1  minoura 
    185       1.1  minoura 	if (verbose)
    186       1.1  minoura 	    fprintf(stderr, "Writing... ");
    187       1.1  minoura 	fd = open(dev, O_WRONLY);
    188       1.1  minoura 	if (fd < 0)
    189       1.1  minoura 	    err(1, "opening %s", dev);
    190       1.1  minoura 	if (ioctl(fd, DIOCWLABEL, (char *)&mode) < 0)
    191       1.1  minoura 	    err(1, "DIOCWLABEL %s", dev);
    192       1.1  minoura 	if (write(fd, buf, 4096) != 4096) {
    193       1.1  minoura 	    mode = 0;
    194       1.1  minoura 	    ioctl(fd, DIOCWLABEL, (char *)&mode);
    195       1.1  minoura 	    err(1, "DIOCWLABEL %s", dev);
    196       1.1  minoura 	}
    197       1.1  minoura 	ioctl(fd, DIOCWLABEL, (char *)&mode);
    198       1.1  minoura 	if (verbose)
    199       1.1  minoura 	    fprintf(stderr, "done.\n");
    200       1.1  minoura 	close(fd);
    201       1.1  minoura     }
    202       1.1  minoura 
    203       1.1  minoura     return 0;
    204       1.1  minoura }
    205