tools/llvm: Do not build with symbols
[minix3.git] / usr.sbin / installboot / arch / landisk.c
blobd58aa08d96b88cfab95edaca81f0a075ea30baef
1 /* $NetBSD: landisk.c,v 1.6 2013/10/19 17:08:15 christos Exp $ */
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Laight.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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"
34 #endif
36 #include <sys/cdefs.h>
37 #if !defined(__lint)
38 __RCSID("$NetBSD: landisk.c,v 1.6 2013/10/19 17:08:15 christos Exp $");
39 #endif /* !__lint */
41 #include <sys/param.h>
43 #include <assert.h>
44 #include <err.h>
45 #include <md5.h>
46 #include <stddef.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.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,
58 IB_TIMEOUT };
60 static int
61 landisk_setboot(ib_params *params)
63 struct mbr_sector mbr;
64 struct landisk_boot_params bp, *bpp;
65 uint8_t *bootstrapbuf;
66 ssize_t rv;
67 uint32_t magic;
68 size_t bootstrapsize;
69 int retval, i;
70 uint32_t bplen;
72 assert(params != NULL);
73 assert(params->fsfd != -1);
74 assert(params->filesystem != NULL);
75 assert(params->s1fd != -1);
76 assert(params->stage1 != NULL);
78 retval = 0;
79 bootstrapbuf = 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",
87 params->stage1);
88 goto done;
92 * Read in the existing MBR.
94 rv = pread(params->fsfd, &mbr, sizeof(mbr), MBR_BBSECTOR);
95 if (rv == -1) {
96 warn("Reading `%s'", params->filesystem);
97 goto done;
98 } else if (rv != sizeof(mbr)) {
99 warnx("Reading `%s': short read", params->filesystem);
100 goto done;
102 if (mbr.mbr_magic != le16toh(MBR_MAGIC)) {
103 if (params->flags & IB_VERBOSE) {
104 printf(
105 "Ignoring MBR with invalid magic in sector 0 of `%s'\n",
106 params->filesystem);
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
114 * block.
116 bootstrapsize = roundup(params->s1stat.st_size, 512);
118 bootstrapbuf = malloc(bootstrapsize);
119 if (bootstrapbuf == NULL) {
120 warn("Allocating %zu bytes", bootstrapsize);
121 goto done;
123 memset(bootstrapbuf, 0, bootstrapsize);
126 * Read the file into the buffer.
128 rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
129 if (rv == -1) {
130 warn("Reading `%s'", params->stage1);
131 goto done;
132 } else if (rv != params->s1stat.st_size) {
133 warnx("Reading `%s': short read", params->stage1);
134 goto done;
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));
141 goto done;
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
151 #if 0
152 int bpbsize;
153 if (bootstrapbuf[1] == 0xa0 && bootstrapbuf[2] == 0x11 &&
154 (bootstrapbuf[0] == 0x2b /*|| bootstrapbuf[0] == 0x1d*/)) {
155 bpbsize = bootstrapbuf[0] + 2 - MBR_BPB_OFFSET;
157 #endif
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) {
165 warnx(
166 "Partition table has non-zero byte at offset %d in `%s'",
167 MBR_PART_OFFSET + i, params->stage1);
168 goto done;
172 #if 0
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.
177 if (bpbsize != 0) {
178 if (params->flags & IB_VERBOSE)
179 printf("Preserving %d (%#x) bytes of the BPB\n",
180 bpbsize, bpbsize);
181 (void)memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb,
182 bpbsize);
184 #endif
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 */
198 bplen = sizeof bp;
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");
209 goto done;
211 memcpy(bpp, &bp, bplen);
213 if (params->flags & IB_NOWRITE) {
214 retval = 1;
215 goto done;
219 * Write MBR code to sector zero.
221 rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
222 if (rv == -1) {
223 warn("Writing `%s'", params->filesystem);
224 goto done;
225 } else if (rv != 512) {
226 warnx("Writing `%s': short write", params->filesystem);
227 goto done;
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);
235 if (rv == -1) {
236 warn("Writing `%s'", params->filesystem);
237 goto done;
238 } else if ((size_t)rv != bootstrapsize - 512 * 2) {
239 warnx("Writing `%s': short write", params->filesystem);
240 goto done;
243 retval = 1;
245 done:
246 if (bootstrapbuf)
247 free(bootstrapbuf);
248 return retval;