newdisk.c revision 1.1 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 "(C) 1999 by The NetBSD Foundation, Inc.";
56
57 int verbose = 0, dry_run = 0, force = 0, check_only = 0;
58
59 volatile void usage __P((void));
60 int main __P((int, char *[]));
61
62 volatile void
63 usage(void)
64 {
65 fprintf(stderr,
66 "Usage: %s [-v] [-n] [-f] [-c] [-m /usr/mdec/mboot] "
67 "/dev/rsdXc\n", prog);
68 exit(1);
69 /* NOTREACHED */
70 }
71
72 int
73 main(argc, argv)
74 int argc;
75 char *argv[];
76 {
77 extern int optind;
78 int ch;
79 int fd;
80 struct disklabel label;
81
82 prog = argv[0];
83 while ((ch = getopt(argc, argv, "vnfcm:")) != -1) {
84 switch (ch) {
85 case 'v':
86 verbose = 1;
87 break;
88 case 'n':
89 dry_run = 1;
90 break;
91 case 'f':
92 force = 1;
93 break;
94 case 'c':
95 check_only = 1;
96 break;
97 case 'm':
98 mboot = optarg;
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 (verbose)
160 fprintf(stderr, "Creating an empty partition table... ");
161 #define n (label.d_secperunit/2)
162 sprintf(buf+2048,
163 "X68K%c%c%c%c%c%c%c%c%c%c%c%c",
164 0, 0, 0, 32,
165 (n/16777215)%256, (n/65536)%256, (n/256)%256, n%256,
166 (n/16777215)%256, (n/65536)%256, (n/256)%256, n%256);
167 #undef n
168 if (verbose)
169 fprintf(stderr, "done.\n");
170
171 if (dry_run) {
172 char filename[MAXPATHLEN] = "/tmp/diskmarkXXXXX";
173 fd = mkstemp(filename);
174 if (fd < 0)
175 err(1, "opening %s", filename);
176 if (write(fd, buf, 4096) < 0)
177 err(1, "writing %s", filename);
178 close(fd);
179 fprintf(stderr, "Disk mark is kept in %s.\n", filename);
180 } else {
181 int mode = 1;
182
183 if (verbose)
184 fprintf(stderr, "Writing... ");
185 fd = open(dev, O_WRONLY);
186 if (fd < 0)
187 err(1, "opening %s", dev);
188 if (ioctl(fd, DIOCWLABEL, (char *)&mode) < 0)
189 err(1, "DIOCWLABEL %s", dev);
190 if (write(fd, buf, 4096) != 4096) {
191 mode = 0;
192 ioctl(fd, DIOCWLABEL, (char *)&mode);
193 err(1, "DIOCWLABEL %s", dev);
194 }
195 ioctl(fd, DIOCWLABEL, (char *)&mode);
196 if (verbose)
197 fprintf(stderr, "done.\n");
198 close(fd);
199 }
200
201 return 0;
202 }
203