1 /* $NetBSD: i386.c,v 1.37 2011/08/14 17:50:17 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 * 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. 30 */ 31 32 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 #if !defined(__lint) 38 __RCSID("$NetBSD: i386.c,v 1.37 2011/08/14 17:50:17 christos Exp $"); 39 #endif /* !__lint */ 40 41 #include <sys/param.h> 42 #ifndef HAVE_NBTOOL_CONFIG_H 43 #include <sys/ioctl.h> 44 #include <sys/dkio.h> 45 #endif 46 47 #include <assert.h> 48 #include <errno.h> 49 #include <err.h> 50 #include <md5.h> 51 #include <stddef.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include "installboot.h" 58 59 static const struct console_name { 60 const char *name; /* Name of console selection */ 61 const int dev; /* value matching CONSDEV_* from sys/arch/i386/stand/lib/libi386.h */ 62 } consoles[] = { 63 { "pc", 0 /* CONSDEV_PC */ }, 64 { "com0", 1 /* CONSDEV_COM0 */ }, 65 { "com1", 2 /* CONSDEV_COM1 */ }, 66 { "com2", 3 /* CONSDEV_COM2 */ }, 67 { "com3", 4 /* CONSDEV_COM3 */ }, 68 { "com0kbd", 5 /* CONSDEV_COM0KBD */ }, 69 { "com1kbd", 6 /* CONSDEV_COM1KBD */ }, 70 { "com2kbd", 7 /* CONSDEV_COM2KBD */ }, 71 { "com3kbd", 8 /* CONSDEV_COM3KBD */ }, 72 { "auto", -1 /* CONSDEV_AUTO */ }, 73 }; 74 75 static int i386_setboot(ib_params *); 76 static int i386_editboot(ib_params *); 77 78 struct ib_mach ib_mach_i386 = 79 { "i386", i386_setboot, no_clearboot, i386_editboot, 80 IB_RESETVIDEO | IB_CONSOLE | IB_CONSPEED | IB_CONSADDR | 81 IB_KEYMAP | IB_PASSWORD | IB_TIMEOUT | 82 IB_MODULES | IB_BOOTCONF }; 83 84 struct ib_mach ib_mach_amd64 = 85 { "amd64", i386_setboot, no_clearboot, i386_editboot, 86 IB_RESETVIDEO | IB_CONSOLE | IB_CONSPEED | IB_CONSADDR | 87 IB_KEYMAP | IB_PASSWORD | IB_TIMEOUT | 88 IB_MODULES | IB_BOOTCONF }; 89 90 /* 91 * Attempting to write the 'labelsector' (or a sector near it - within 8k?) 92 * using the non-raw disk device fails silently. This can be detected (today) 93 * by doing a fsync() and a read back. 94 * This is very likely to affect installboot, indeed the code may need to 95 * be written into the 'labelsector' itself - especially on non-512 byte media. 96 * We do all writes with a read verify. 97 * If EROFS is returned we also try to enable writes to the label sector. 98 * (Maybe these functions should be in the generic part of installboot.) 99 */ 100 static int 101 pwrite_validate(int fd, const void *buf, size_t n_bytes, off_t offset) 102 { 103 void *r_buf; 104 ssize_t rv; 105 106 r_buf = malloc(n_bytes); 107 if (r_buf == NULL) 108 return -1; 109 rv = pwrite(fd, buf, n_bytes, offset); 110 if (rv == -1) { 111 free(r_buf); 112 return -1; 113 } 114 fsync(fd); 115 if (pread(fd, r_buf, rv, offset) == rv && memcmp(r_buf, buf, rv) == 0) { 116 free(r_buf); 117 return rv; 118 } 119 free(r_buf); 120 errno = EROFS; 121 return -1; 122 } 123 124 static int 125 write_boot_area(ib_params *params, uint8_t *buf, size_t len) 126 { 127 int rv, i; 128 129 /* 130 * Writing the 'label' sector (likely to be bytes 512-1023) could 131 * fail, so we try to avoid writing that area. 132 * Unfortunately, if we are accessing the raw disk, and the sector 133 * size is larger than 512 bytes that is also doomed. 134 * See how we get on.... 135 * 136 * NB: Even if the physical sector size is not 512, the space for 137 * the label is 512 bytes from the start of the disk. 138 * So all the '512' constants in these functions are correct. 139 */ 140 141 /* Write out first 512 bytes - the pbr code */ 142 rv = pwrite_validate(params->fsfd, buf, 512, 0); 143 if (rv == 512) { 144 /* That worked, do the rest */ 145 if (len == 512) 146 return 1; 147 len -= 512 * 2; 148 rv = pwrite_validate(params->fsfd, buf + 512 * 2, len, 512 * 2); 149 if (rv != (ssize_t)len) 150 goto bad_write; 151 return 1; 152 } 153 if (rv != -1 || (errno != EINVAL && errno != EROFS)) 154 goto bad_write; 155 156 if (errno == EINVAL) { 157 /* Assume the failure was due to to the sector size > 512 */ 158 rv = pwrite_validate(params->fsfd, buf, len, 0); 159 if (rv == (ssize_t)len) 160 return 1; 161 if (rv != -1 || (errno != EROFS)) 162 goto bad_write; 163 } 164 165 #ifdef DIOCWLABEL 166 /* Pesky label is protected, try to unprotect it */ 167 i = 1; 168 rv = ioctl(params->fsfd, DIOCWLABEL, &i); 169 if (rv != 0) { 170 warn("Cannot enable writes to the label sector"); 171 return 0; 172 } 173 /* Try again with label write-enabled */ 174 rv = pwrite_validate(params->fsfd, buf, len, 0); 175 176 /* Reset write-protext */ 177 i = 0; 178 ioctl(params->fsfd, DIOCWLABEL, &i); 179 if (rv == (ssize_t)len) 180 return 1; 181 #endif 182 183 bad_write: 184 if (rv == -1) 185 warn("Writing `%s'", params->filesystem); 186 else 187 warnx("Writing `%s': short write, %u bytes", 188 params->filesystem, rv); 189 return 0; 190 } 191 192 static void 193 show_i386_boot_params(struct x86_boot_params *bpp) 194 { 195 size_t i; 196 197 printf("Boot options: "); 198 printf("timeout %d, ", le32toh(bpp->bp_timeout)); 199 printf("flags %x, ", le32toh(bpp->bp_flags)); 200 printf("speed %d, ", le32toh(bpp->bp_conspeed)); 201 printf("ioaddr %x, ", le32toh(bpp->bp_consaddr)); 202 for (i = 0; i < __arraycount(consoles); i++) { 203 if (consoles[i].dev == (int)le32toh(bpp->bp_consdev)) 204 break; 205 } 206 if (i == __arraycount(consoles)) 207 printf("console %d\n", le32toh(bpp->bp_consdev)); 208 else 209 printf("console %s\n", consoles[i].name); 210 if (bpp->bp_keymap[0]) 211 printf(" keymap %s\n", bpp->bp_keymap); 212 } 213 214 static int 215 is_zero(const uint8_t *p, unsigned int len) 216 { 217 return len == 0 || (p[0] == 0 && memcmp(p, p + 1, len - 1) == 0); 218 } 219 220 static int 221 update_i386_boot_params(ib_params *params, struct x86_boot_params *bpp) 222 { 223 struct x86_boot_params bp; 224 uint32_t bplen; 225 size_t i; 226 227 bplen = le32toh(bpp->bp_length); 228 if (bplen > sizeof bp) 229 /* Ignore pad space in bootxx */ 230 bplen = sizeof bp; 231 232 /* Take (and update) local copy so we handle size mismatches */ 233 memset(&bp, 0, sizeof bp); 234 memcpy(&bp, bpp, bplen); 235 236 if (params->flags & IB_TIMEOUT) 237 bp.bp_timeout = htole32(params->timeout); 238 if (params->flags & IB_RESETVIDEO) 239 bp.bp_flags ^= htole32(X86_BP_FLAGS_RESET_VIDEO); 240 if (params->flags & IB_CONSPEED) 241 bp.bp_conspeed = htole32(params->conspeed); 242 if (params->flags & IB_CONSADDR) 243 bp.bp_consaddr = htole32(params->consaddr); 244 if (params->flags & IB_CONSOLE) { 245 for (i = 0; i < __arraycount(consoles); i++) 246 if (strcmp(consoles[i].name, params->console) == 0) 247 break; 248 249 if (i == __arraycount(consoles)) { 250 warnx("invalid console name, valid names are:"); 251 (void)fprintf(stderr, "\t%s", consoles[0].name); 252 for (i = 1; consoles[i].name != NULL; i++) 253 (void)fprintf(stderr, ", %s", consoles[i].name); 254 (void)fprintf(stderr, "\n"); 255 return 1; 256 } 257 bp.bp_consdev = htole32(consoles[i].dev); 258 } 259 if (params->flags & IB_PASSWORD) { 260 if (params->password[0]) { 261 MD5_CTX md5ctx; 262 MD5Init(&md5ctx); 263 MD5Update(&md5ctx, params->password, 264 strlen(params->password)); 265 MD5Final(bp.bp_password, &md5ctx); 266 bp.bp_flags |= htole32(X86_BP_FLAGS_PASSWORD); 267 } else { 268 memset(&bp.bp_password, 0, sizeof bp.bp_password); 269 bp.bp_flags &= ~htole32(X86_BP_FLAGS_PASSWORD); 270 } 271 } 272 if (params->flags & IB_KEYMAP) 273 strlcpy(bp.bp_keymap, params->keymap, sizeof bp.bp_keymap); 274 if (params->flags & IB_MODULES) 275 bp.bp_flags ^= htole32(X86_BP_FLAGS_NOMODULES); 276 if (params->flags & IB_BOOTCONF) 277 bp.bp_flags ^= htole32(X86_BP_FLAGS_NOBOOTCONF); 278 279 if (params->flags & (IB_NOWRITE | IB_VERBOSE)) 280 show_i386_boot_params(&bp); 281 282 /* Check we aren't trying to set anything we can't save */ 283 if (!is_zero((char *)&bp + bplen, sizeof bp - bplen)) { 284 warnx("Patch area in stage1 bootstrap is too small"); 285 return 1; 286 } 287 memcpy(bpp, &bp, bplen); 288 return 0; 289 } 290 291 static int 292 i386_setboot(ib_params *params) 293 { 294 unsigned int u; 295 ssize_t rv; 296 uint32_t *magic, expected_magic; 297 union { 298 struct mbr_sector mbr; 299 uint8_t b[8192]; 300 } disk_buf, bootstrap; 301 302 assert(params != NULL); 303 assert(params->fsfd != -1); 304 assert(params->filesystem != NULL); 305 assert(params->s1fd != -1); 306 assert(params->stage1 != NULL); 307 308 /* 309 * There is only 8k of space in a FFSv1 partition (and ustarfs) 310 * so ensure we don't splat over anything important. 311 */ 312 if (params->s1stat.st_size > (off_t)(sizeof bootstrap)) { 313 warnx("stage1 bootstrap `%s' (%u bytes) is larger than 8192 bytes", 314 params->stage1, (unsigned int)params->s1stat.st_size); 315 return 0; 316 } 317 if (params->s1stat.st_size < 3 * 512 && params->s1stat.st_size != 512) { 318 warnx("stage1 bootstrap `%s' (%u bytes) is too small", 319 params->stage1, (unsigned int)params->s1stat.st_size); 320 return 0; 321 } 322 323 /* Read in the existing disk header and boot code */ 324 rv = pread(params->fsfd, &disk_buf, sizeof (disk_buf), 0); 325 if (rv != sizeof(disk_buf)) { 326 if (rv == -1) 327 warn("Reading `%s'", params->filesystem); 328 else 329 warnx("Reading `%s': short read, %ld bytes" 330 " (should be %ld)", params->filesystem, (long)rv, 331 (long)sizeof(disk_buf)); 332 return 0; 333 } 334 335 if (disk_buf.mbr.mbr_magic != le16toh(MBR_MAGIC)) { 336 if (params->flags & IB_VERBOSE) { 337 printf( 338 "Ignoring PBR with invalid magic in sector 0 of `%s'\n", 339 params->filesystem); 340 } 341 memset(&disk_buf, 0, 512); 342 } 343 344 /* Read the new bootstrap code. */ 345 rv = pread(params->s1fd, &bootstrap, params->s1stat.st_size, 0); 346 if (rv != params->s1stat.st_size) { 347 if (rv == -1) 348 warn("Reading `%s'", params->stage1); 349 else 350 warnx("Reading `%s': short read, %ld bytes" 351 " (should be %ld)", params->stage1, (long)rv, 352 (long)params->s1stat.st_size); 353 return 0; 354 } 355 356 /* 357 * The bootstrap code is either 512 bytes for booting FAT16, or best 358 * part of 8k (with bytes 512-1023 all zeros). 359 */ 360 if (params->s1stat.st_size == 512) { 361 /* Magic number is at end of pbr code */ 362 magic = (void *)(bootstrap.b + 512 - 16 + 4); 363 expected_magic = htole32(X86_BOOT_MAGIC_FAT); 364 } else { 365 /* Magic number is at start of sector following label */ 366 magic = (void *)(bootstrap.b + 512 * 2 + 4); 367 expected_magic = htole32(X86_BOOT_MAGIC_1); 368 /* 369 * For a variety of reasons we restrict our 'normal' partition 370 * boot code to a size which enable it to be used as mbr code. 371 * IMHO this is bugus (dsl). 372 */ 373 if (!is_zero(bootstrap.b + 512-2-64, 64)) { 374 warnx("Data in mbr partition table of new bootstrap"); 375 return 0; 376 } 377 if (!is_zero(bootstrap.b + 512, 512)) { 378 warnx("Data in label part of new bootstrap"); 379 return 0; 380 } 381 /* Copy mbr table and label from existing disk buffer */ 382 memcpy(bootstrap.b + 512-2-64, disk_buf.b + 512-2-64, 64); 383 memcpy(bootstrap.b + 512, disk_buf.b + 512, 512); 384 } 385 386 /* Validate the 'magic number' that marks the parameter block */ 387 if (*magic != expected_magic) { 388 warnx("Invalid magic in stage1 bootstrap %x != %x", 389 *magic, expected_magic); 390 return 0; 391 } 392 393 /* 394 * If the partition has a FAT (or NTFS) filesystem, then we must 395 * preserve the BIOS Parameter Block (BPB). 396 * It is also very likely that there isn't 8k of space available 397 * for (say) bootxx_msdos, and that blindly installing it will trash 398 * the FAT filesystem. 399 * To avoid this we check the number of 'reserved' sectors to ensure 400 * there there is enough space. 401 * Unfortunately newfs(8) doesn't (yet) splat the BPB (which is 402 * effectively the FAT superblock) when a filesystem is initailised 403 * so this code tends to complain rather too often, 404 * Specifying 'installboot -f' will delete the old BPB info. 405 */ 406 if (!(params->flags & IB_FORCE)) { 407 #define USE_F ", use -f (may invalidate filesystem)" 408 /* 409 * For FAT compatibility, the pbr code starts 'jmp xx; nop' 410 * followed by the BIOS Parameter Block (BPB). 411 * The 2nd byte (jump offset) is the size of the nop + BPB. 412 */ 413 if (bootstrap.b[0] != 0xeb || bootstrap.b[2] != 0x90) { 414 warnx("No BPB in new bootstrap %02x:%02x:%02x" USE_F, 415 bootstrap.b[0], bootstrap.b[1], bootstrap.b[2]); 416 return 0; 417 } 418 419 /* Find size of old BPB, and copy into new bootcode */ 420 if (!is_zero(disk_buf.b + 3 + 8, disk_buf.b[1] - 1 - 8)) { 421 struct mbr_bpbFAT16 *bpb = (void *)(disk_buf.b + 3 + 8); 422 /* Check enough space before the FAT for the bootcode */ 423 u = le16toh(bpb->bpbBytesPerSec) 424 * le16toh(bpb->bpbResSectors); 425 if (u != 0 && u < params->s1stat.st_size) { 426 warnx("Insufficient reserved space before FAT " 427 "(%u bytes available)" USE_F, u); 428 return 0; 429 } 430 /* Check we have enough space for the old bpb */ 431 if (disk_buf.b[1] > bootstrap.b[1]) { 432 /* old BPB is larger, allow if extra zeros */ 433 if (!is_zero(disk_buf.b + 2 + bootstrap.b[1], 434 disk_buf.b[1] - bootstrap.b[1])) { 435 warnx("Old BPB too big" USE_F); 436 return 0; 437 } 438 u = bootstrap.b[1]; 439 } else { 440 /* Old BPB is shorter, leave zero filled */ 441 u = disk_buf.b[1]; 442 } 443 memcpy(bootstrap.b + 2, disk_buf.b + 2, u); 444 } 445 #undef USE_F 446 } 447 448 /* 449 * Fill in any user-specified options into the 450 * struct x86_boot_params 451 * that follows the magic number. 452 * See sys/arch/i386/stand/bootxx/bootxx.S for more information. 453 */ 454 if (update_i386_boot_params(params, (void *)(magic + 1))) 455 return 0; 456 457 if (params->flags & IB_NOWRITE) { 458 return 1; 459 } 460 461 /* Copy new bootstrap data into disk buffer, ignoring label area */ 462 memcpy(&disk_buf, &bootstrap, 512); 463 if (params->s1stat.st_size > 512 * 2) { 464 memcpy(disk_buf.b + 2 * 512, bootstrap.b + 2 * 512, 465 params->s1stat.st_size - 2 * 512); 466 /* Zero pad to 512 byte sector boundary */ 467 memset(disk_buf.b + params->s1stat.st_size, 0, 468 (8192 - params->s1stat.st_size) & 511); 469 } 470 471 return write_boot_area(params, disk_buf.b, sizeof disk_buf.b); 472 } 473 474 static int 475 i386_editboot(ib_params *params) 476 { 477 int retval; 478 uint8_t buf[512]; 479 ssize_t rv; 480 uint32_t magic; 481 uint32_t offset; 482 struct x86_boot_params *bpp; 483 484 assert(params != NULL); 485 assert(params->fsfd != -1); 486 assert(params->filesystem != NULL); 487 488 retval = 0; 489 490 /* 491 * Read in the existing bootstrap. 492 * Look in any of the first 4 sectors. 493 */ 494 495 bpp = NULL; 496 for (offset = 0; offset < 4 * 512; offset += 512) { 497 rv = pread(params->fsfd, &buf, sizeof buf, offset); 498 if (rv == -1) { 499 warn("Reading `%s'", params->filesystem); 500 goto done; 501 } else if (rv != sizeof buf) { 502 warnx("Reading `%s': short read", params->filesystem); 503 goto done; 504 } 505 506 /* Magic number is 4 bytes in (to allow for a jmps) */ 507 /* Also allow any of the magic numbers. */ 508 magic = le32toh(*(uint32_t *)(buf + 4)) | 0xf; 509 if (magic != (X86_BOOT_MAGIC_1 | 0xf)) 510 continue; 511 512 /* The parameters are just after the magic number */ 513 bpp = (void *)(buf + 8); 514 break; 515 } 516 if (bpp == NULL) { 517 warnx("Invalid magic in existing bootstrap"); 518 goto done; 519 } 520 521 /* 522 * Fill in any user-specified options into the 523 * struct x86_boot_params 524 * that's 8 bytes in from the start of the third sector. 525 * See sys/arch/i386/stand/bootxx/bootxx.S for more information. 526 */ 527 if (update_i386_boot_params(params, bpp)) 528 goto done; 529 530 if (params->flags & IB_NOWRITE) { 531 retval = 1; 532 goto done; 533 } 534 535 /* 536 * Write boot code back 537 */ 538 rv = pwrite(params->fsfd, buf, sizeof buf, offset); 539 if (rv == -1) { 540 warn("Writing `%s'", params->filesystem); 541 goto done; 542 } else if (rv != sizeof buf) { 543 warnx("Writing `%s': short write, %zd bytes (should be %zu)", 544 params->filesystem, rv, sizeof(buf)); 545 goto done; 546 } 547 548 retval = 1; 549 550 done: 551 return retval; 552 } 553