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