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