1 /* $NetBSD: newdisk.c,v 1.11 2014/03/26 08:17:59 christos 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * Create the disk mark for x68k SCSI IPL.
30 * It used to be a shell/awk script, but is rewritten in order to be fit with
31 * the install kernel.
32 *
33 * Usage: /usr/mdec/newdisk [-vnfc] [-m /usr/mdec/mboot] /dev/rsd?c
34 */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <util.h>
43 #include <sys/param.h>
44 #include <sys/disklabel.h>
45 #include <sys/dkio.h>
46 #include <sys/ioctl.h>
47
48 const char *mboot = MBOOT;
49 char dev[MAXPATHLEN];
50 char buf[4096 + 1];
51
52 const char copyright[] = "NetBSD/x68k SCSI Primary Boot. ";
53
54 int verbose = 0, dry_run = 0, force = 0, check_only = 0, mark_only = 0;
55
56 void usage(void) __attribute__((__noreturn__));
57 int main(int, char *[]);
58
59 void
usage(void)60 usage(void)
61 {
62 fprintf(stderr,
63 "Usage: %s [-v] [-n] [-f] [-c] [-m /usr/mdec/mboot] "
64 "/dev/rsdXc\n", getprogname());
65 exit(1);
66 /* NOTREACHED */
67 }
68
69 int
main(int argc,char * argv[])70 main(int argc, char *argv[])
71 {
72 int ch;
73 int fd;
74 struct disklabel label;
75
76 while ((ch = getopt(argc, argv, "vnfcm:p")) != -1) {
77 switch (ch) {
78 case 'v':
79 verbose = 1;
80 break;
81 case 'n':
82 dry_run = 1;
83 break;
84 case 'f':
85 force = 1;
86 break;
87 case 'c':
88 check_only = 1;
89 break;
90 case 'm':
91 mboot = optarg;
92 break;
93 case 'p':
94 mark_only = 1;
95 break;
96 default:
97 usage();
98 }
99 }
100 argc -= optind;
101 argv += optind;
102
103 if (argc != 1)
104 usage();
105
106 fd = opendisk(argv[0], O_RDONLY, dev, MAXPATHLEN, 0);
107 if (fd < 0)
108 err(1, "opening %s", dev);
109 if (access(mboot, R_OK) < 0)
110 err(1, "checking %s", mboot);
111
112 if (read(fd, buf, 512) < 0)
113 err(1, "reading %s", dev);
114 if (strncmp(buf, "X68SCSI1", 8) == 0 && !force)
115 errx(1, "%s is already marked. "
116 "Use -f to overwrite the existing mark.", dev);
117 if (check_only)
118 return 0;
119
120 if (verbose)
121 fprintf(stderr, "Inspecting %s... ", dev);
122
123 if (ioctl(fd, DIOCGDINFO, &label) < 0)
124 err(1, "inspecting %s", dev);
125 close(fd);
126 if (label.d_secsize != 512)
127 errx(1, "This type of disk is not supported by NetBSD.");
128
129 if (verbose)
130 fprintf(stderr, "total number of sector is %d.\n",
131 label.d_secperunit);
132
133 if (verbose)
134 fprintf(stderr, "Building disk mark... ");
135 memset(buf, 0, 3072);
136 #define n label.d_secperunit
137 snprintf(buf, sizeof(buf), "X68SCSI1%c%c%c%c%c%c%c%c%s",
138 2, 0,
139 (n/16777216)%256, (n/65536)%256, (n/256)%256, n%256,
140 1, 0, copyright);
141 #undef n
142 if (verbose)
143 fprintf(stderr, "done.\n");
144
145 if (verbose)
146 fprintf(stderr, "Merging %s... ", mboot);
147 fd = open(mboot, O_RDONLY);
148 if (fd < 0)
149 err(1, "opening %s", mboot);
150 if (read(fd, buf+1024, 1024) < 0)
151 err(1, "reading %s", mboot);
152 close(fd);
153 if (verbose)
154 fprintf(stderr, "done.\n");
155
156 if (!mark_only) {
157 if (verbose)
158 fprintf(stderr,
159 "Creating an empty partition table... ");
160 #define n (label.d_secperunit/2)
161 snprintf(buf + 2048, sizeof(buf) - 2048,
162 "X68K%c%c%c%c%c%c%c%c%c%c%c%c",
163 0, 0, 0, 32,
164 (n/16777215)%256, (n/65536)%256, (n/256)%256, n%256,
165 (n/16777215)%256, (n/65536)%256, (n/256)%256, n%256);
166 #undef n
167 if (verbose)
168 fprintf(stderr, "done.\n");
169 }
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