1 /* $NetBSD: landisk.c,v 1.6 2013/10/19 17:08:15 christos 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.6 2013/10/19 17:08:15 christos 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
;
72 assert(params
!= NULL
);
73 assert(params
->fsfd
!= -1);
74 assert(params
->filesystem
!= NULL
);
75 assert(params
->s1fd
!= -1);
76 assert(params
->stage1
!= NULL
);
82 * There is only 8k of space in a FFSv1 partition (and ustarfs)
83 * so ensure we don't splat over anything important.
85 if (params
->s1stat
.st_size
> 8192) {
86 warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
92 * Read in the existing MBR.
94 rv
= pread(params
->fsfd
, &mbr
, sizeof(mbr
), MBR_BBSECTOR
);
96 warn("Reading `%s'", params
->filesystem
);
98 } else if (rv
!= sizeof(mbr
)) {
99 warnx("Reading `%s': short read", params
->filesystem
);
102 if (mbr
.mbr_magic
!= le16toh(MBR_MAGIC
)) {
103 if (params
->flags
& IB_VERBOSE
) {
105 "Ignoring MBR with invalid magic in sector 0 of `%s'\n",
108 memset(&mbr
, 0, sizeof(mbr
));
112 * Allocate a buffer, with space to round up the input file
113 * to the next block size boundary, and with space for the boot
116 bootstrapsize
= roundup(params
->s1stat
.st_size
, 512);
118 bootstrapbuf
= malloc(bootstrapsize
);
119 if (bootstrapbuf
== NULL
) {
120 warn("Allocating %zu bytes", bootstrapsize
);
123 memset(bootstrapbuf
, 0, bootstrapsize
);
126 * Read the file into the buffer.
128 rv
= pread(params
->s1fd
, bootstrapbuf
, params
->s1stat
.st_size
, 0);
130 warn("Reading `%s'", params
->stage1
);
132 } else if (rv
!= params
->s1stat
.st_size
) {
133 warnx("Reading `%s': short read", params
->stage1
);
137 magic
= *(uint32_t *)(bootstrapbuf
+ 512 * 2 + 4);
138 if (magic
!= htole32(LANDISK_BOOT_MAGIC_1
)) {
139 warnx("Invalid magic in stage1 boostrap %x != %x",
140 magic
, htole32(LANDISK_BOOT_MAGIC_1
));
145 * Determine size of BIOS Parameter Block (BPB) to copy from
146 * original MBR to the temporary buffer by examining the first
147 * few instruction in the new bootblock. Supported values:
148 * 2b a0 11 jmp ENDOF(mbr_bpbFAT32)+1, nop
149 * (anything else) ; don't preserve
153 if (bootstrapbuf
[1] == 0xa0 && bootstrapbuf
[2] == 0x11 &&
154 (bootstrapbuf
[0] == 0x2b /*|| bootstrapbuf[0] == 0x1d*/)) {
155 bpbsize
= bootstrapbuf
[0] + 2 - MBR_BPB_OFFSET
;
160 * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
161 * the partition table.
163 for (i
= 0; i
< (int)sizeof(mbr
.mbr_parts
); i
++) {
164 if (*(uint8_t *)(bootstrapbuf
+ MBR_PART_OFFSET
+ i
) != 0) {
166 "Partition table has non-zero byte at offset %d in `%s'",
167 MBR_PART_OFFSET
+ i
, params
->stage1
);
174 * Copy the BPB and the partition table from the original MBR to the
175 * temporary buffer so that they're written back to the fs.
178 if (params
->flags
& IB_VERBOSE
)
179 printf("Preserving %d (%#x) bytes of the BPB\n",
181 (void)memcpy(bootstrapbuf
+ MBR_BPB_OFFSET
, &mbr
.mbr_bpb
,
185 memcpy(bootstrapbuf
+ MBR_PART_OFFSET
, &mbr
.mbr_parts
,
186 sizeof(mbr
.mbr_parts
));
189 * Fill in any user-specified options into the
190 * struct landisk_boot_params
191 * that's 8 bytes in from the start of the third sector.
192 * See sys/arch/landisk/stand/bootxx/bootxx.S for more information.
194 bpp
= (void *)(bootstrapbuf
+ 512 * 2 + 8);
195 bplen
= le32toh(bpp
->bp_length
);
196 if (bplen
> sizeof bp
)
197 /* Ignore pad space in bootxx */
199 /* Take (and update) local copy so we handle size mismatches */
200 memset(&bp
, 0, sizeof bp
);
201 memcpy(&bp
, bpp
, bplen
);
202 if (params
->flags
& IB_TIMEOUT
)
203 bp
.bp_timeout
= htole32(params
->timeout
);
204 /* Check we aren't trying to set anything we can't save */
205 if (bplen
< sizeof bp
&& memcmp((char *)&bp
+ bplen
,
206 (char *)&bp
+ bplen
+ 1,
207 sizeof bp
- bplen
- 1) != 0) {
208 warnx("Patch area in stage1 bootstrap is too small");
211 memcpy(bpp
, &bp
, bplen
);
213 if (params
->flags
& IB_NOWRITE
) {
219 * Write MBR code to sector zero.
221 rv
= pwrite(params
->fsfd
, bootstrapbuf
, 512, 0);
223 warn("Writing `%s'", params
->filesystem
);
225 } else if (rv
!= 512) {
226 warnx("Writing `%s': short write", params
->filesystem
);
231 * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
233 rv
= pwrite(params
->fsfd
, bootstrapbuf
+ 512 * 2,
234 bootstrapsize
- 512 * 2, 512 * 2);
236 warn("Writing `%s'", params
->filesystem
);
238 } else if ((size_t)rv
!= bootstrapsize
- 512 * 2) {
239 warnx("Writing `%s': short write", params
->filesystem
);