1 /* $NetBSD: landisk.c,v 1.4 2009/04/05 11:55:39 lukem Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: landisk.c,v 1.4 2009/04/05 11:55:39 lukem Exp $");
41 #include <sys/param.h>
52 #include "installboot.h"
54 static int landisk_setboot(ib_params
*);
56 struct ib_mach ib_mach_landisk
=
57 { "landisk", landisk_setboot
, no_clearboot
, no_editboot
,
61 landisk_setboot(ib_params
*params
)
63 struct mbr_sector mbr
;
64 struct landisk_boot_params bp
, *bpp
;
65 uint8_t *bootstrapbuf
;
73 assert(params
!= NULL
);
74 assert(params
->fsfd
!= -1);
75 assert(params
->filesystem
!= NULL
);
76 assert(params
->s1fd
!= -1);
77 assert(params
->stage1
!= NULL
);
83 * There is only 8k of space in a FFSv1 partition (and ustarfs)
84 * so ensure we don't splat over anything important.
86 if (params
->s1stat
.st_size
> 8192) {
87 warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
93 * Read in the existing MBR.
95 rv
= pread(params
->fsfd
, &mbr
, sizeof(mbr
), MBR_BBSECTOR
);
97 warn("Reading `%s'", params
->filesystem
);
99 } else if (rv
!= sizeof(mbr
)) {
100 warnx("Reading `%s': short read", params
->filesystem
);
103 if (mbr
.mbr_magic
!= le16toh(MBR_MAGIC
)) {
104 if (params
->flags
& IB_VERBOSE
) {
106 "Ignoring MBR with invalid magic in sector 0 of `%s'\n",
109 memset(&mbr
, 0, sizeof(mbr
));
113 * Allocate a buffer, with space to round up the input file
114 * to the next block size boundary, and with space for the boot
117 bootstrapsize
= roundup(params
->s1stat
.st_size
, 512);
119 bootstrapbuf
= malloc(bootstrapsize
);
120 if (bootstrapbuf
== NULL
) {
121 warn("Allocating %zu bytes", bootstrapsize
);
124 memset(bootstrapbuf
, 0, bootstrapsize
);
127 * Read the file into the buffer.
129 rv
= pread(params
->s1fd
, bootstrapbuf
, params
->s1stat
.st_size
, 0);
131 warn("Reading `%s'", params
->stage1
);
133 } else if (rv
!= params
->s1stat
.st_size
) {
134 warnx("Reading `%s': short read", params
->stage1
);
138 magic
= *(uint32_t *)(bootstrapbuf
+ 512 * 2 + 4);
139 if (magic
!= htole32(LANDISK_BOOT_MAGIC_1
)) {
140 warnx("Invalid magic in stage1 boostrap %x != %x",
141 magic
, htole32(LANDISK_BOOT_MAGIC_1
));
146 * Determine size of BIOS Parameter Block (BPB) to copy from
147 * original MBR to the temporary buffer by examining the first
148 * few instruction in the new bootblock. Supported values:
149 * 2b a0 11 jmp ENDOF(mbr_bpbFAT32)+1, nop
150 * (anything else) ; don't preserve
154 if (bootstrapbuf
[1] == 0xa0 && bootstrapbuf
[2] == 0x11 &&
155 (bootstrapbuf
[0] == 0x2b /*|| bootstrapbuf[0] == 0x1d*/)) {
156 bpbsize
= bootstrapbuf
[0] + 2 - MBR_BPB_OFFSET
;
161 * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
162 * the partition table.
164 for (i
= 0; i
< (int)sizeof(mbr
.mbr_parts
); i
++) {
165 if (*(uint8_t *)(bootstrapbuf
+ MBR_PART_OFFSET
+ i
) != 0) {
167 "Partition table has non-zero byte at offset %d in `%s'",
168 MBR_PART_OFFSET
+ i
, params
->stage1
);
175 * Copy the BPB and the partition table from the original MBR to the
176 * temporary buffer so that they're written back to the fs.
179 if (params
->flags
& IB_VERBOSE
)
180 printf("Preserving %d (%#x) bytes of the BPB\n",
182 (void)memcpy(bootstrapbuf
+ MBR_BPB_OFFSET
, &mbr
.mbr_bpb
,
186 memcpy(bootstrapbuf
+ MBR_PART_OFFSET
, &mbr
.mbr_parts
,
187 sizeof(mbr
.mbr_parts
));
190 * Fill in any user-specified options into the
191 * struct landisk_boot_params
192 * that's 8 bytes in from the start of the third sector.
193 * See sys/arch/landisk/stand/bootxx/bootxx.S for more information.
195 bpp
= (void *)(bootstrapbuf
+ 512 * 2 + 8);
196 bplen
= le32toh(bpp
->bp_length
);
197 if (bplen
> sizeof bp
)
198 /* Ignore pad space in bootxx */
200 /* Take (and update) local copy so we handle size mismatches */
201 memset(&bp
, 0, sizeof bp
);
202 memcpy(&bp
, bpp
, bplen
);
203 if (params
->flags
& IB_TIMEOUT
)
204 bp
.bp_timeout
= htole32(params
->timeout
);
205 /* Check we aren't trying to set anything we can't save */
206 if (bplen
< sizeof bp
&& memcmp((char *)&bp
+ bplen
,
207 (char *)&bp
+ bplen
+ 1,
208 sizeof bp
- bplen
- 1) != 0) {
209 warnx("Patch area in stage1 bootstrap is too small");
212 memcpy(bpp
, &bp
, bplen
);
214 if (params
->flags
& IB_NOWRITE
) {
220 * Write MBR code to sector zero.
222 rv
= pwrite(params
->fsfd
, bootstrapbuf
, 512, 0);
224 warn("Writing `%s'", params
->filesystem
);
226 } else if (rv
!= 512) {
227 warnx("Writing `%s': short write", params
->filesystem
);
232 * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
234 rv
= pwrite(params
->fsfd
, bootstrapbuf
+ 512 * 2,
235 bootstrapsize
- 512 * 2, 512 * 2);
237 warn("Writing `%s'", params
->filesystem
);
239 } else if ((size_t)rv
!= bootstrapsize
- 512 * 2) {
240 warnx("Writing `%s': short write", params
->filesystem
);