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 volatile void usage __P((void)); 59 int main __P((int, char *[])); 60 61 volatile 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(argc, argv) 73 int argc; 74 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 && 121 !force) 122 errx(1, "%s is already marked. 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", label.d_secperunit); 137 138 if (verbose) 139 fprintf(stderr, "Building disk mark... "); 140 memset(buf, 0, 3072); 141 #define n label.d_secperunit 142 sprintf(buf, "X68SCSI1%c%c%c%c%c%c%c%c%s", 143 2, 0, 144 (n/16777216)%256, (n/65536)%256, (n/256)%256, n%256, 145 1, 0, copyright); 146 #undef n 147 if (verbose) 148 fprintf(stderr, "done.\n"); 149 150 if (verbose) 151 fprintf(stderr, "Merging %s... ", mboot); 152 fd = open(mboot, O_RDONLY); 153 if (fd < 0) 154 err(1, "opening %s", mboot); 155 if (read(fd, buf+1024, 1024) < 0) 156 err(1, "reading %s", mboot); 157 close(fd); 158 if (verbose) 159 fprintf(stderr, "done.\n"); 160 161 if (!mark_only) { 162 if (verbose) 163 fprintf(stderr, "Creating an empty partition table... "); 164 #define n (label.d_secperunit/2) 165 sprintf(buf+2048, 166 "X68K%c%c%c%c%c%c%c%c%c%c%c%c", 167 0, 0, 0, 32, 168 (n/16777215)%256, (n/65536)%256, (n/256)%256, n%256, 169 (n/16777215)%256, (n/65536)%256, (n/256)%256, n%256); 170 #undef n 171 if (verbose) 172 fprintf(stderr, "done.\n"); 173 } 174 175 if (dry_run) { 176 char filename[MAXPATHLEN] = "/tmp/diskmarkXXXXX"; 177 fd = mkstemp(filename); 178 if (fd < 0) 179 err(1, "opening %s", filename); 180 if (write(fd, buf, 4096) < 0) 181 err(1, "writing %s", filename); 182 close(fd); 183 fprintf(stderr, "Disk mark is kept in %s.\n", filename); 184 } else { 185 int mode = 1; 186 187 if (verbose) 188 fprintf(stderr, "Writing... "); 189 fd = open(dev, O_WRONLY); 190 if (fd < 0) 191 err(1, "opening %s", dev); 192 if (ioctl(fd, DIOCWLABEL, (char *)&mode) < 0) 193 err(1, "DIOCWLABEL %s", dev); 194 if (write(fd, buf, 4096) != 4096) { 195 mode = 0; 196 ioctl(fd, DIOCWLABEL, (char *)&mode); 197 err(1, "DIOCWLABEL %s", dev); 198 } 199 ioctl(fd, DIOCWLABEL, (char *)&mode); 200 if (verbose) 201 fprintf(stderr, "done.\n"); 202 close(fd); 203 } 204 205 return 0; 206 } 207