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