1 /* $NetBSD: installboot.c,v 1.4 2006/08/04 02:21:19 mhitch Exp $ */
4 * Copyright (c) 2001 Minoura Makoto
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/param.h>
37 #include <sys/disklabel.h>
38 #include <sys/ioctl.h>
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
45 #define MAXBBSIZE BBSIZE
46 #define LABELBYTEOFFSET (LABELSECTOR*512+LABELOFFSET)
48 int nflag
= 0, vflag
= 0, fflag
= 0, merging
= 0;
50 char rawname
[PATH_MAX
];
57 char template[] = _PATH_TMP
"/installbootXXXXXX";
58 u_int8_t bootblock
[MAXBBSIZE
];
59 struct disklabel label
;
61 void usage(void) __attribute((__noreturn__
));
62 int checkbootprog(const char *);
63 int checktargetdev(const char *);
64 int checkparttype(const char *, int);
69 fprintf(stderr
, "usage: %s [-nvf] /usr/mdec/xxboot_ufs /dev/rxx?a\n",
76 checkbootprog(const char *name
)
80 if (access(name
, R_OK
) < 0)
82 if (stat(name
, &st
) < 0)
84 bootprogsize
= st
.st_size
;
90 checktargetdev(const char *name
)
94 if (access(name
, W_OK
) < 0)
96 if (stat(name
, &st
) < 0)
98 if (!S_ISCHR(st
.st_mode
))
99 errx(1, "%s: not a character special device", name
);
100 if (DISKPART(st
.st_rdev
) > MAXPARTITIONS
)
101 errx(1, "%s: invalid device", name
);
102 strcpy(rawname
, name
);
103 if (strncmp(name
+ strlen(name
) - 4, "fd", 2) == 0)
106 rawname
[strlen(name
) - 1] = RAW_PART
+'a';
107 if (!floppy
&& DISKPART(st
.st_rdev
) == RAW_PART
)
108 errx(1, "%s is the raw device", name
);
114 checkparttype(const char *name
, int force
)
119 fd
= open(rawname
, O_RDONLY
| O_EXLOCK
);
121 err(1, "opening %s", name
);
122 if (stat(name
, &st
) < 0)
124 if (!S_ISCHR(st
.st_mode
))
125 errx(1, "%s: not a character special device", name
);
126 part
= DISKPART(st
.st_rdev
);
127 if (ioctl(fd
, DIOCGDINFO
, &label
) < 0)
128 err(1, "%s: reading disklabel", name
);
129 if (part
>= label
.d_npartitions
)
130 errx(1, "%s: invalid partition", name
);
131 blocksize
= label
.d_secsize
;
134 if (blocksize
> MAXBBSIZE
)
135 errx(1, "%s: blocksize too large", name
);
136 if (read(fd
, bootblock
, blocksize
) != blocksize
)
137 errx(1, "%s: reading the mark", name
);
139 if (strncmp((const char *)bootblock
, "X68SCSI1", 8) != 0)
140 floppy
= 1; /* XXX: or unformated */
142 if (!force
&& !floppy
) {
143 if (label
.d_partitions
[part
].p_fstype
!= FS_BSDFFS
144 && label
.d_partitions
[part
].p_fstype
!= FS_BSDLFS
)
145 errx(1, "%s: invalid partition type", name
);
146 if ((label
.d_partitions
[part
].p_offset
* blocksize
< 32768) &&
147 label
.d_partitions
[part
].p_offset
!= 0)
148 errx(1, "%s: cannot make the partition bootable",
153 else if (label
.d_partitions
[part
].p_offset
== 0) {
155 bboffset
= 1024; /* adjusted below */
158 struct disklabel
*lp
;
160 lp
= (struct disklabel
*) &bootblock
[LABELBYTEOFFSET
];
161 memcpy(&label
, lp
, sizeof(struct disklabel
));
162 if (dkcksum(lp
) != 0)
163 /* there is no valid label */
164 memset(&label
, 0, sizeof(struct disklabel
));
171 main(int argc
, char *argv
[])
178 while ((c
= getopt(argc
, argv
, "nvf")) != -1) {
202 if (checkbootprog(bootprog
) < 0)
204 if (checktargetdev(target
) < 0)
207 if (checkparttype(target
, fflag
))
209 if (merging
&& blocksize
> bboffset
&& !floppy
)
210 bboffset
= blocksize
;
211 if (bootprogsize
> MAXBBSIZE
- bboffset
)
212 errx(1, "%s: boot block too big", bootprog
);
214 /* Read the boot program */
215 fd
= open(bootprog
, O_RDONLY
);
217 err(1, "opening %s", bootprog
);
218 if (read(fd
, bootblock
+ bboffset
, bootprogsize
) != bootprogsize
)
219 err(1, "reading %s", bootprog
);
222 memcpy(bootblock
+ LABELBYTEOFFSET
, &label
, sizeof(label
));
224 /* Write the boot block (+ disklabel if necessary) */
228 fd
= mkstemp(target
);
230 err(1, "opening the output file");
234 fd
= open(target
, O_WRONLY
);
236 err(1, "opening the disk");
237 if (merging
&& ioctl(fd
, DIOCWLABEL
, &writable
) < 0)
238 err(1, "opening the disk");
240 bootprogsize
= howmany(bootprogsize
+bboffset
, blocksize
) * blocksize
;
241 if (write(fd
, bootblock
, bootprogsize
) != bootprogsize
) {
242 warn("writing the label");
243 if (!nflag
&& merging
) {
245 ioctl(fd
, DIOCWLABEL
, &writable
);
250 if (!nflag
&& merging
) {
252 ioctl(fd
, DIOCWLABEL
, &writable
);
257 fprintf(stderr
, "The bootblock is kept in %s\n", target
);
259 fprintf(stderr
, "Do not forget to copy /usr/mdec/boot"
260 " to the root directory of %s.\n", target
);