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