newdisk.c revision 1.5.4.1 1 /* $NetBSD: newdisk.c,v 1.5.4.1 2011/05/31 03:04:22 rmind 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 <err.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <util.h>
48 #include <sys/param.h>
49 #include <sys/disklabel.h>
50 #include <sys/dkio.h>
51 #include <sys/ioctl.h>
52
53 const char *mboot = MBOOT;
54 char dev[MAXPATHLEN];
55 char buf[4096 + 1];
56
57 const char copyright[] = "NetBSD/x68k SCSI Primary Boot. ";
58
59 int verbose = 0, dry_run = 0, force = 0, check_only = 0, mark_only = 0;
60
61 void usage(void) __attribute__((__noreturn__));
62 int main(int, char *[]);
63
64 void
65 usage(void)
66 {
67 fprintf(stderr,
68 "Usage: %s [-v] [-n] [-f] [-c] [-m /usr/mdec/mboot] "
69 "/dev/rsdXc\n", getprogname());
70 exit(1);
71 /* NOTREACHED */
72 }
73
74 int
75 main(int argc, char *argv[])
76 {
77 int ch;
78 int fd;
79 struct disklabel label;
80
81 while ((ch = getopt(argc, argv, "vnfcm:p")) != -1) {
82 switch (ch) {
83 case 'v':
84 verbose = 1;
85 break;
86 case 'n':
87 dry_run = 1;
88 break;
89 case 'f':
90 force = 1;
91 break;
92 case 'c':
93 check_only = 1;
94 break;
95 case 'm':
96 mboot = optarg;
97 break;
98 case 'p':
99 mark_only = 1;
100 break;
101 default:
102 usage();
103 }
104 }
105 argc -= optind;
106 argv += optind;
107
108 if (argc != 1)
109 usage();
110
111 fd = opendisk(argv[0], O_RDONLY, dev, MAXPATHLEN, 0);
112 if (fd < 0)
113 err(1, "opening %s", dev);
114 if (access(mboot, R_OK) < 0)
115 err(1, "checking %s", mboot);
116
117 if (read(fd, buf, 512) < 0)
118 err(1, "reading %s", dev);
119 if (strncmp(buf, "X68SCSI1", 8) == 0 && !force)
120 errx(1, "%s is already marked. "
121 "Use -f to overwrite the existing mark.", dev);
122 if (check_only)
123 return 0;
124
125 if (verbose)
126 fprintf(stderr, "Inspecting %s... ", dev);
127
128 if (ioctl(fd, DIOCGDINFO, &label) < 0)
129 err(1, "inspecting %s", dev);
130 close(fd);
131 if (label.d_secsize != 512)
132 errx(1, "This type of disk is not supported by NetBSD.");
133
134 if (verbose)
135 fprintf(stderr, "total number of sector is %d.\n",
136 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,
164 "Creating an empty partition table... ");
165 #define n (label.d_secperunit/2)
166 sprintf(buf+2048,
167 "X68K%c%c%c%c%c%c%c%c%c%c%c%c",
168 0, 0, 0, 32,
169 (n/16777215)%256, (n/65536)%256, (n/256)%256, n%256,
170 (n/16777215)%256, (n/65536)%256, (n/256)%256, n%256);
171 #undef n
172 if (verbose)
173 fprintf(stderr, "done.\n");
174 }
175
176 if (dry_run) {
177 char filename[MAXPATHLEN] = "/tmp/diskmarkXXXXX";
178 fd = mkstemp(filename);
179 if (fd < 0)
180 err(1, "opening %s", filename);
181 if (write(fd, buf, 4096) < 0)
182 err(1, "writing %s", filename);
183 close(fd);
184 fprintf(stderr, "Disk mark is kept in %s.\n", filename);
185 } else {
186 int mode = 1;
187
188 if (verbose)
189 fprintf(stderr, "Writing... ");
190 fd = open(dev, O_WRONLY);
191 if (fd < 0)
192 err(1, "opening %s", dev);
193 if (ioctl(fd, DIOCWLABEL, (char *)&mode) < 0)
194 err(1, "DIOCWLABEL %s", dev);
195 if (write(fd, buf, 4096) != 4096) {
196 mode = 0;
197 ioctl(fd, DIOCWLABEL, (char *)&mode);
198 err(1, "DIOCWLABEL %s", dev);
199 }
200 ioctl(fd, DIOCWLABEL, (char *)&mode);
201 if (verbose)
202 fprintf(stderr, "done.\n");
203 close(fd);
204 }
205
206 return 0;
207 }
208