installboot.c revision 1.1 1 /* $NetBSD: installboot.c,v 1.1 2001/11/24 16:26:56 minoura Exp $ */
2
3 /*
4 * Copyright (c) 2001 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 AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <paths.h>
35 #include <err.h>
36 #include <sys/param.h>
37 #include <sys/disklabel.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
42
43 #include "dkcksum.h"
44
45 #define MAXBBSIZE BBSIZE
46 #define LABELBYTEOFFSET (LABELSECTOR*512+LABELOFFSET)
47
48 int nflag = 0, vflag = 0, fflag = 0, merging = 0;
49 int floppy = 0;
50 char rawname[PATH_MAX];
51 off_t bboffset = 0;
52 size_t bootprogsize;
53 size_t blocksize;
54 const char *progname;
55 const char *bootprog;
56 char *target;
57 char template[] = _PATH_TMP "/installbootXXXXXX";
58 u_int8_t bootblock[MAXBBSIZE];
59 struct disklabel label;
60
61 void usage(void) __attribute((__noreturn__));
62 int checkbootprog(const char *);
63 int checktargetdev(const char *);
64 int checkparttype(const char *, int);
65
66 void
67 usage(void)
68 {
69 fprintf(stderr, "usage: %s [-nvf] /usr/mdec/xxboot_ufs /dev/rxx?a\n",
70 progname);
71 exit(1);
72 /* NOTREACHED */
73 }
74
75 int
76 checkbootprog(const char *name)
77 {
78 #if 0
79 struct stat st;
80
81 if (access(name, R_OK) < 0)
82 err(1, "%s", name);
83 if (stat(name, &st) < 0)
84 err(1, "%s", name);
85 bootprogsize = st.st_size;
86
87 return 0;
88 #else
89 int fd;
90
91 if (access(name, R_OK) < 0)
92 err(1, "%s", name);
93 fd = open(name, O_RDONLY);
94 if (fd < 0)
95 err(1, "%s", name);
96 bootprogsize = lseek(fd, 0, SEEK_END);
97 close(fd);
98
99 return 0;
100 #endif
101 }
102
103 int
104 checktargetdev(const char *name)
105 {
106 struct stat st;
107
108 if (access(name, W_OK) < 0)
109 err(1, "%s", name);
110 if (stat(name, &st) < 0)
111 err(1, "%s", name);
112 if ((st.st_mode & S_IFCHR) == 0)
113 errx(1, "%s: not a character special device", name);
114 if (DISKPART(st.st_rdev) > MAXPARTITIONS)
115 errx(1, "%s: invalid device", name);
116 strcpy(rawname, name);
117 if (strncmp(name + strlen(name) - 4, "fd", 2) == 1) {
118 printf("floppy\n"); /* XXX */
119 floppy = 1;
120 } else {
121 printf("not floppy\n");
122 rawname[strlen(name) - 1] = RAW_PART+'a';
123 }
124 if (!floppy && DISKPART(st.st_rdev) == RAW_PART)
125 errx(1, "%s is the raw device", name);
126
127 return 0;
128 }
129
130 int
131 checkparttype(const char *name, int force)
132 {
133 struct stat st;
134 int fd, part;
135
136 fd = open(rawname, O_RDONLY | O_EXLOCK);
137 if (fd < 0)
138 err(1, "opening %s", name);
139 if (stat(name, &st) < 0)
140 err(1, "%s", name);
141 if ((st.st_mode & S_IFCHR) == 0)
142 errx(1, "%s: not a character special device", name);
143 part = DISKPART(st.st_rdev);
144 if (ioctl(fd, DIOCGDINFO, &label) < 0)
145 err(1, "%s: reading disklabel", name);
146 if (part >= label.d_npartitions)
147 errx(1, "%s: invalid partition", name);
148 blocksize = label.d_secsize;
149 if (blocksize < 512)
150 blocksize = 512;
151 if (blocksize > MAXBBSIZE)
152 errx(1, "%s: blocksize too large", name);
153 if (read(fd, bootblock, blocksize) != blocksize)
154 errx(1, "%s: reading the mark", name);
155 close(fd);
156 if (strncmp(bootblock, "X68SCSI1", 8) != 0)
157 floppy = 1; /* XXX: or unformated */
158
159 if (!force && !floppy) {
160 if (label.d_partitions[part].p_fstype != FS_BSDFFS
161 && label.d_partitions[part].p_fstype != FS_BSDLFS)
162 errx(1, "%s: invalid partition type", name);
163 if ((label.d_partitions[part].p_offset * blocksize < 32768) &&
164 label.d_partitions[part].p_offset != 0)
165 errx(1, "%s: cannot make the partition bootable",
166 name);
167 }
168 if (floppy)
169 merging = 1;
170 else if (label.d_partitions[part].p_offset == 0) {
171 merging = 1;
172 bboffset = 1024; /* adjusted below */
173 }
174 if (merging) {
175 struct disklabel *lp;
176
177 lp = (struct disklabel *) &bootblock[LABELBYTEOFFSET];
178 memcpy(&label, lp, sizeof(struct disklabel));
179 if (dkcksum(lp) != 0)
180 /* there is no valid label */
181 memset(&label, 0, sizeof(struct disklabel));
182 }
183
184 return 0;
185 }
186
187 int
188 main(int argc, char *argv[])
189 {
190 int c;
191 int fd;
192
193 progname = argv[0];
194
195 while ((c = getopt(argc, argv, "nvf")) != -1) {
196 switch (c) {
197 case 'n':
198 nflag = 1;
199 break;
200 case 'v':
201 vflag = 1;
202 break;
203 case 'f':
204 fflag = 1;
205 break;
206 default:
207 usage();
208 /* NOTREACHED */
209 }
210 }
211 argc -= optind;
212 argv += optind;
213
214 if (argc != 2)
215 usage();
216 bootprog = argv[0];
217 target = argv[1];
218
219 if (checkbootprog(bootprog) < 0)
220 errx(1, "aborting");
221 if (checktargetdev(target) < 0)
222 errx(1, "aborting");
223
224 if (checkparttype(target, fflag))
225 errx(1, "aborting");
226 if (merging && blocksize > bboffset)
227 bboffset = blocksize;
228 if (bootprogsize > MAXBBSIZE - bboffset)
229 errx(1, "%s: boot block too big", bootprog);
230
231 /* Read the boot program */
232 fd = open(bootprog, O_RDONLY);
233 if (fd < 0)
234 err(1, "opening %s", bootprog);
235 if (read(fd, bootblock + bboffset, bootprogsize) != bootprogsize)
236 err(1, "reading %s", bootprog);
237 close(fd);
238 if (merging)
239 memcpy(bootblock + LABELBYTEOFFSET, &label, sizeof(label));
240
241 /* Write the boot block (+ disklabel if necessary) */
242 if (nflag) {
243 target = template;
244
245 fd = mkstemp(target);
246 if (fd < 0)
247 err(1, "opening the output file");
248 } else {
249 int writable = 1;
250
251 fd = open(target, O_WRONLY);
252 if (fd < 0)
253 err(1, "opening the disk");
254 if (merging && ioctl(fd, DIOCWLABEL, &writable) < 0)
255 err(1, "opening the disk");
256 }
257 bootprogsize = howmany(bootprogsize+bboffset, blocksize) * blocksize;
258 if (write(fd, bootblock, bootprogsize) != bootprogsize) {
259 warn("writing the label");
260 if (!nflag && merging) {
261 int writable = 0;
262 ioctl(fd, DIOCWLABEL, &writable);
263 }
264 exit(1);
265 }
266
267 if (!nflag && merging) {
268 int writable = 0;
269 ioctl(fd, DIOCWLABEL, &writable);
270 }
271 close(fd);
272
273 if (nflag)
274 fprintf(stderr, "The bootblock is kept in %s\n", target);
275 else
276 fprintf(stderr, "Do not forget to copy /usr/mdec/boot"
277 " to the root directory of %s.\n", target);
278
279 return 0;
280 }
281