1 /* $NetBSD: landisk.c,v 1.2 2006/09/27 21:22:52 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by David Laight. 9 * 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. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #if HAVE_NBTOOL_CONFIG_H 40 #include "nbtool_config.h" 41 #endif 42 43 #include <sys/cdefs.h> 44 #if !defined(__lint) 45 __RCSID("$NetBSD: landisk.c,v 1.2 2006/09/27 21:22:52 christos Exp $"); 46 #endif /* !__lint */ 47 48 #include <sys/param.h> 49 50 #include <assert.h> 51 #include <err.h> 52 #include <md5.h> 53 #include <stddef.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #include "installboot.h" 60 61 static int landisk_setboot(ib_params *); 62 63 struct ib_mach ib_mach_landisk = 64 { "landisk", landisk_setboot, no_clearboot, no_editboot, 65 IB_TIMEOUT }; 66 67 static int 68 landisk_setboot(ib_params *params) 69 { 70 struct mbr_sector mbr; 71 struct landisk_boot_params bp, *bpp; 72 uint8_t *bootstrapbuf; 73 ssize_t rv; 74 uint32_t magic; 75 u_int bootstrapsize; 76 int retval, i; 77 int bplen; 78 int bpbsize; 79 80 assert(params != NULL); 81 assert(params->fsfd != -1); 82 assert(params->filesystem != NULL); 83 assert(params->s1fd != -1); 84 assert(params->stage1 != NULL); 85 86 retval = 0; 87 bootstrapbuf = NULL; 88 89 /* 90 * There is only 8k of space in a UFSv1 partition (and ustarfs) 91 * so ensure we don't splat over anything important. 92 */ 93 if (params->s1stat.st_size > 8192) { 94 warnx("stage1 bootstrap `%s' is larger than 8192 bytes", 95 params->stage1); 96 goto done; 97 } 98 99 /* 100 * Read in the existing MBR. 101 */ 102 rv = pread(params->fsfd, &mbr, sizeof(mbr), MBR_BBSECTOR); 103 if (rv == -1) { 104 warn("Reading `%s'", params->filesystem); 105 goto done; 106 } else if (rv != sizeof(mbr)) { 107 warnx("Reading `%s': short read", params->filesystem); 108 goto done; 109 } 110 if (mbr.mbr_magic != le16toh(MBR_MAGIC)) { 111 if (params->flags & IB_VERBOSE) { 112 printf( 113 "Ignoring MBR with invalid magic in sector 0 of `%s'\n", 114 params->filesystem); 115 } 116 memset(&mbr, 0, sizeof(mbr)); 117 } 118 119 /* 120 * Allocate a buffer, with space to round up the input file 121 * to the next block size boundary, and with space for the boot 122 * block. 123 */ 124 bootstrapsize = roundup(params->s1stat.st_size, 512); 125 126 bootstrapbuf = malloc(bootstrapsize); 127 if (bootstrapbuf == NULL) { 128 warn("Allocating %u bytes", bootstrapsize); 129 goto done; 130 } 131 memset(bootstrapbuf, 0, bootstrapsize); 132 133 /* 134 * Read the file into the buffer. 135 */ 136 rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0); 137 if (rv == -1) { 138 warn("Reading `%s'", params->stage1); 139 goto done; 140 } else if (rv != params->s1stat.st_size) { 141 warnx("Reading `%s': short read", params->stage1); 142 goto done; 143 } 144 145 magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4); 146 if (magic != htole32(LANDISK_BOOT_MAGIC_1)) { 147 warnx("Invalid magic in stage1 boostrap %x != %x", 148 magic, htole32(LANDISK_BOOT_MAGIC_1)); 149 goto done; 150 } 151 152 /* 153 * Determine size of BIOS Parameter Block (BPB) to copy from 154 * original MBR to the temporary buffer by examining the first 155 * few instruction in the new bootblock. Supported values: 156 * 2b a0 11 jmp ENDOF(mbr_bpbFAT32)+1, nop 157 * (anything else) ; don't preserve 158 */ 159 bpbsize = 0; 160 #if 0 161 if (bootstrapbuf[1] == 0xa0 && bootstrapbuf[2] == 0x11 && 162 (bootstrapbuf[0] == 0x2b /*|| bootstrapbuf[0] == 0x1d*/)) { 163 bpbsize = bootstrapbuf[0] + 2 - MBR_BPB_OFFSET; 164 } 165 #endif 166 167 /* 168 * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in 169 * the partition table. 170 */ 171 for (i = 0; i < sizeof(mbr.mbr_parts); i++) { 172 if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) { 173 warnx( 174 "Partition table has non-zero byte at offset %d in `%s'", 175 MBR_PART_OFFSET + i, params->stage1); 176 goto done; 177 } 178 } 179 180 #if 0 181 /* 182 * Copy the BPB and the partition table from the original MBR to the 183 * temporary buffer so that they're written back to the fs. 184 */ 185 if (bpbsize != 0) { 186 if (params->flags & IB_VERBOSE) 187 printf("Preserving %d (%#x) bytes of the BPB\n", 188 bpbsize, bpbsize); 189 (void)memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb, 190 bpbsize); 191 } 192 #endif 193 memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts, 194 sizeof(mbr.mbr_parts)); 195 196 /* 197 * Fill in any user-specified options into the 198 * struct landisk_boot_params 199 * that's 8 bytes in from the start of the third sector. 200 * See sys/arch/landisk/stand/bootxx/bootxx.S for more information. 201 */ 202 bpp = (void *)(bootstrapbuf + 512 * 2 + 8); 203 bplen = le32toh(bpp->bp_length); 204 if (bplen > sizeof bp) 205 /* Ignore pad space in bootxx */ 206 bplen = sizeof bp; 207 /* Take (and update) local copy so we handle size mismatches */ 208 memset(&bp, 0, sizeof bp); 209 memcpy(&bp, bpp, bplen); 210 if (params->flags & IB_TIMEOUT) 211 bp.bp_timeout = htole32(params->timeout); 212 /* Check we aren't trying to set anything we can't save */ 213 if (bplen < sizeof bp && memcmp((char *)&bp + bplen, 214 (char *)&bp + bplen + 1, 215 sizeof bp - bplen - 1) != 0) { 216 warnx("Patch area in stage1 bootstrap is too small"); 217 goto done; 218 } 219 memcpy(bpp, &bp, bplen); 220 221 if (params->flags & IB_NOWRITE) { 222 retval = 1; 223 goto done; 224 } 225 226 /* 227 * Write MBR code to sector zero. 228 */ 229 rv = pwrite(params->fsfd, bootstrapbuf, 512, 0); 230 if (rv == -1) { 231 warn("Writing `%s'", params->filesystem); 232 goto done; 233 } else if (rv != 512) { 234 warnx("Writing `%s': short write", params->filesystem); 235 goto done; 236 } 237 238 /* 239 * Skip disklabel in sector 1 and write bootxx to sectors 2..N. 240 */ 241 rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2, 242 bootstrapsize - 512 * 2, 512 * 2); 243 if (rv == -1) { 244 warn("Writing `%s'", params->filesystem); 245 goto done; 246 } else if (rv != bootstrapsize - 512 * 2) { 247 warnx("Writing `%s': short write", params->filesystem); 248 goto done; 249 } 250 251 retval = 1; 252 253 done: 254 if (bootstrapbuf) 255 free(bootstrapbuf); 256 return retval; 257 } 258