2 * Copyright (c) 1999 Minoura Makoto
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Minoura Makoto.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Create the disk mark for x68k SCSI IPL.
33 * It used to be a shell/awk script, but is rewritten in order to be fit with
36 * Usage: /usr/mdec/newdisk [-vnfc] [-m /usr/mdec/mboot] /dev/rsd?c
45 #include <sys/param.h>
46 #include <sys/disklabel.h>
54 const char copyright
[] = "NetBSD/x68k SCSI Primary Boot. ";
56 int verbose
= 0, dry_run
= 0, force
= 0, check_only
= 0, mark_only
= 0;
58 void usage(void) __attribute__((__noreturn__
));
59 int main(int, char *[]);
65 "Usage: %s [-v] [-n] [-f] [-c] [-m /usr/mdec/mboot] "
66 "/dev/rsdXc\n", prog
);
72 main(int argc
, char *argv
[])
77 struct disklabel label
;
80 while ((ch
= getopt(argc
, argv
, "vnfcm:p")) != -1) {
110 fd
= opendisk(argv
[0], O_RDONLY
, dev
, MAXPATHLEN
, 0);
112 err(1, "opening %s", dev
);
113 if (access(mboot
, R_OK
) < 0)
114 err(1, "checking %s", mboot
);
116 if (read(fd
, buf
, 512) < 0)
117 err(1, "reading %s", dev
);
118 if (strncmp(buf
, "X68SCSI1", 8) == 0 &&
120 errx(1, "%s is already marked. Use -f to overwrite the existing mark.");
125 fprintf(stderr
, "Inspecting %s... ", dev
);
127 if (ioctl(fd
, DIOCGDINFO
, &label
) < 0)
128 err(1, "inspecting %s", dev
);
130 if (label
.d_secsize
!= 512)
131 errx(1, "This type of disk is not supported by NetBSD.");
134 fprintf(stderr
, "total number of sector is %d.\n", label
.d_secperunit
);
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",
142 (n
/16777216)%256, (n
/65536)%256, (n
/256)%256, n
%256,
146 fprintf(stderr
, "done.\n");
149 fprintf(stderr
, "Merging %s... ", mboot
);
150 fd
= open(mboot
, O_RDONLY
);
152 err(1, "opening %s", mboot
);
153 if (read(fd
, buf
+1024, 1024) < 0)
154 err(1, "reading %s", mboot
);
157 fprintf(stderr
, "done.\n");
161 fprintf(stderr
, "Creating an empty partition table... ");
162 #define n (label.d_secperunit/2)
164 "X68K%c%c%c%c%c%c%c%c%c%c%c%c",
166 (n
/16777215)%256, (n
/65536)%256, (n
/256)%256, n
%256,
167 (n
/16777215)%256, (n
/65536)%256, (n
/256)%256, n
%256);
170 fprintf(stderr
, "done.\n");
174 char filename
[MAXPATHLEN
] = "/tmp/diskmarkXXXXX";
175 fd
= mkstemp(filename
);
177 err(1, "opening %s", filename
);
178 if (write(fd
, buf
, 4096) < 0)
179 err(1, "writing %s", filename
);
181 fprintf(stderr
, "Disk mark is kept in %s.\n", filename
);
186 fprintf(stderr
, "Writing... ");
187 fd
= open(dev
, O_WRONLY
);
189 err(1, "opening %s", dev
);
190 if (ioctl(fd
, DIOCWLABEL
, (char *)&mode
) < 0)
191 err(1, "DIOCWLABEL %s", dev
);
192 if (write(fd
, buf
, 4096) != 4096) {
194 ioctl(fd
, DIOCWLABEL
, (char *)&mode
);
195 err(1, "DIOCWLABEL %s", dev
);
197 ioctl(fd
, DIOCWLABEL
, (char *)&mode
);
199 fprintf(stderr
, "done.\n");