1 /* $NetBSD: md.c,v 1.5 2015/01/02 19:43:13 abs Exp $ */ 2 3 /* 4 * Copyright 1997 Piermont Information Systems Inc. 5 * All rights reserved. 6 * 7 * Based on code written by Philip A. Nelson for Piermont Information 8 * Systems Inc. 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. The name of Piermont Information Systems Inc. may not be used to endorse 19 * or promote products derived from this software without specific prior 20 * written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS'' 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* md.c -- i386 machine specific routines - also used by amd64 */ 36 37 #include <sys/param.h> 38 #include <sys/sysctl.h> 39 #include <sys/exec.h> 40 #include <sys/utsname.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <machine/cpu.h> 44 #include <stdio.h> 45 #include <stddef.h> 46 #include <util.h> 47 #include <dirent.h> 48 #include <termios.h> 49 50 #include "defs.h" 51 #include "md.h" 52 #include "endian.h" 53 #include "msg_defs.h" 54 #include "menu_defs.h" 55 56 #ifdef NO_LBA_READS /* for testing */ 57 #undef BIFLAG_EXTINT13 58 #define BIFLAG_EXTINT13 0 59 #endif 60 61 static struct biosdisk_info *biosdisk = NULL; 62 63 /* prototypes */ 64 65 static int get_bios_info(char *); 66 static int mbr_root_above_chs(void); 67 static void md_upgrade_mbrtype(void); 68 static int md_read_bootcode(const char *, struct mbr_sector *); 69 static unsigned int get_bootmodel(void); 70 71 void 72 md_init(void) 73 { 74 } 75 76 void 77 md_init_set_status(int flags) 78 { 79 (void)flags; 80 81 /* Default to install same type of kernel as we are running */ 82 set_kernel_set(get_bootmodel()); 83 } 84 85 int 86 md_get_info(void) 87 { 88 mbr_info_t *ext; 89 struct mbr_partition *p; 90 const char *bootcode; 91 int i; 92 int names, fl, ofl; 93 #define ACTIVE_FOUND 0x0100 94 #define NETBSD_ACTIVE 0x0200 95 #define NETBSD_NAMED 0x0400 96 #define ACTIVE_NAMED 0x0800 97 98 if (pm->no_mbr) 99 return 1; 100 101 if (read_mbr(pm->diskdev, &mbr) < 0) 102 memset(&mbr.mbr, 0, sizeof mbr.mbr - 2); 103 get_bios_info(pm->diskdev); 104 105 edit: 106 if (edit_mbr(&mbr) == 0) 107 return 0; 108 109 root_limit = 0; 110 if (biosdisk != NULL && (biosdisk->bi_flags & BIFLAG_EXTINT13) == 0) { 111 if (mbr_root_above_chs()) { 112 msg_display(MSG_partabovechs); 113 process_menu(MENU_noyes, NULL); 114 if (!yesno) 115 goto edit; 116 /* The user is shooting themselves in the foot here...*/ 117 } else 118 root_limit = bcyl * bhead * bsec; 119 } 120 121 /* 122 * Ensure the install partition (at sector pm->ptstart) and the active 123 * partition are bootable. 124 * Determine whether the bootselect code is needed. 125 * Note that MBR_BS_NEWMBR is always set, so we ignore it! 126 */ 127 fl = 0; 128 names = 0; 129 for (ext = &mbr; ext != NULL; ext = ext->extended) { 130 p = ext->mbr.mbr_parts; 131 for (i = 0; i < MBR_PART_COUNT; p++, i++) { 132 if (p->mbrp_flag == MBR_PFLAG_ACTIVE) { 133 fl |= ACTIVE_FOUND; 134 if (ext->sector + p->mbrp_start == pm->ptstart) 135 fl |= NETBSD_ACTIVE; 136 } 137 if (ext->mbrb.mbrbs_nametab[i][0] == 0) { 138 /* No bootmenu label... */ 139 if (ext->sector == 0) 140 continue; 141 if (ext->sector + p->mbrp_start == pm->ptstart) 142 /* 143 * Have installed into an extended ptn 144 * force name & bootsel... 145 */ 146 names++; 147 continue; 148 } 149 /* Partition has a bootmenu label... */ 150 if (ext->sector != 0) 151 fl |= MBR_BS_EXTLBA; 152 if (ext->sector + p->mbrp_start == pm->ptstart) 153 fl |= NETBSD_NAMED; 154 else if (p->mbrp_flag == MBR_PFLAG_ACTIVE) 155 fl |= ACTIVE_NAMED; 156 else 157 names++; 158 } 159 } 160 if (!(fl & ACTIVE_FOUND)) 161 fl |= NETBSD_ACTIVE; 162 if (fl & NETBSD_NAMED && fl & NETBSD_ACTIVE) 163 fl |= ACTIVE_NAMED; 164 165 if ((names > 0 || !(fl & NETBSD_ACTIVE)) && 166 (!(fl & NETBSD_NAMED) || !(fl & ACTIVE_NAMED))) { 167 /* 168 * There appear to be multiple bootable partitions, but they 169 * don't all have bootmenu texts. 170 */ 171 msg_display(MSG_missing_bootmenu_text); 172 process_menu(MENU_yesno, NULL); 173 if (yesno) 174 goto edit; 175 } 176 177 if ((fl & MBR_BS_EXTLBA) && 178 (biosdisk == NULL || !(biosdisk->bi_flags & BIFLAG_EXTINT13))) { 179 /* Need unsupported LBA reads to read boot sectors */ 180 msg_display(MSG_no_extended_bootmenu); 181 process_menu(MENU_noyes, NULL); 182 if (!yesno) 183 goto edit; 184 } 185 186 /* Sort out the name of the mbr code we need */ 187 if (names > 0 || fl & (NETBSD_NAMED | ACTIVE_NAMED)) { 188 /* Need bootselect code */ 189 fl |= MBR_BS_ACTIVE; 190 bootcode = fl & MBR_BS_EXTLBA ? _PATH_BOOTEXT : _PATH_BOOTSEL; 191 } else 192 bootcode = _PATH_MBR; 193 194 fl &= MBR_BS_ACTIVE | MBR_BS_EXTLBA; 195 196 /* Look at what is installed */ 197 ofl = mbr.mbrb.mbrbs_flags; 198 if (ofl == 0) { 199 /* Check there is some bootcode at all... */ 200 if (mbr.mbr.mbr_magic != htole16(MBR_MAGIC) || 201 mbr.mbr.mbr_jmpboot[0] == 0 || 202 mbr_root_above_chs()) 203 /* Existing won't do, force update */ 204 fl |= MBR_BS_NEWMBR; 205 } 206 ofl = mbr.oflags & (MBR_BS_ACTIVE | MBR_BS_EXTLBA); 207 208 if (fl & ~ofl || (fl == 0 && ofl & MBR_BS_ACTIVE)) { 209 /* Existing boot code isn't the right one... */ 210 if (fl & MBR_BS_ACTIVE) 211 msg_display(MSG_installbootsel); 212 else 213 msg_display(MSG_installmbr); 214 } else 215 /* Existing code would (probably) be ok */ 216 msg_display(MSG_updatembr); 217 218 process_menu(MENU_yesno, NULL); 219 if (!yesno) 220 /* User doesn't want to update mbr code */ 221 return 1; 222 223 if (md_read_bootcode(bootcode, &mbr.mbr) == 0) 224 /* update suceeded - to memory copy */ 225 return 1; 226 227 /* This shouldn't happen since the files are in the floppy fs... */ 228 msg_display("Can't find %s", bootcode); 229 process_menu(MENU_yesno, NULL); 230 231 return 1; 232 } 233 234 /* 235 * md back-end code for menu-driven BSD disklabel editor. 236 */ 237 int 238 md_make_bsd_partitions(void) 239 { 240 return make_bsd_partitions(); 241 } 242 243 /* 244 * any additional partition validation 245 */ 246 int 247 md_check_partitions(void) 248 { 249 int rval; 250 char *bootxx; 251 252 /* check we have boot code for the root partition type */ 253 bootxx = bootxx_name(); 254 rval = access(bootxx, R_OK); 255 free(bootxx); 256 if (rval == 0) 257 return 1; 258 process_menu(MENU_ok, deconst(MSG_No_Bootcode)); 259 return 0; 260 } 261 262 /* 263 * hook called before writing new disklabel. 264 */ 265 int 266 md_pre_disklabel(void) 267 { 268 if (pm->no_mbr) 269 return 0; 270 271 msg_display(MSG_dofdisk); 272 273 /* write edited MBR onto disk. */ 274 if (write_mbr(pm->diskdev, &mbr, 1) != 0) { 275 msg_display(MSG_wmbrfail); 276 process_menu(MENU_ok, NULL); 277 return 1; 278 } 279 return 0; 280 } 281 282 /* 283 * hook called after writing disklabel to new target disk. 284 */ 285 int 286 md_post_disklabel(void) 287 { 288 return 0; 289 } 290 291 /* 292 * hook called after upgrade() or install() has finished setting 293 * up the target disk but immediately before the user is given the 294 * ``disks are now set up'' message. 295 */ 296 int 297 md_post_newfs(void) 298 { 299 int ret; 300 size_t len; 301 char boot_options[1024]; 302 char *bootxx_filename; 303 /* 304 * XXX - this code retains a lot of cruft from when we went 305 * to great pains to exclude installboot from the ramdisk 306 * for size reasons and should be rewritten. 307 */ 308 static const char *consoles[]={ 309 "pc", /* CONSDEV_PC */ 310 "com0", /* CONSDEV_COM0 */ 311 "com1", /* CONSDEV_COM1 */ 312 "com2", /* CONSDEV_COM2 */ 313 "com3", /* CONSDEV_COM3 */ 314 "com0kbd", /* CONSDEV_COM0KBD */ 315 "com1kbd", /* CONSDEV_COM1KBD */ 316 "com2kbd", /* CONSDEV_COM2KBD */ 317 "com3kbd" /* CONSDEV_COM3KBD */ }; 318 static struct x86_boot_params boottype = 319 {sizeof boottype, 0, 5, 0, 9600, { '\0' }, "", 0}; 320 static int conmib[] = {CTL_MACHDEP, CPU_CONSDEV}; 321 struct termios t; 322 dev_t condev; 323 324 /* 325 * Get console device, should either be ttyE0 or tty0n. 326 * Too hard to double check, so just 'know' the device numbers. 327 */ 328 len = sizeof condev; 329 if (sysctl(conmib, nelem(conmib), &condev, &len, NULL, 0) != -1 330 && (condev & ~3) == 0x800) { 331 /* Motherboard serial port */ 332 boottype.bp_consdev = (condev & 3) + 1; 333 /* Defaulting the baud rate to that of stdin should suffice */ 334 if (tcgetattr(0, &t) != -1) 335 boottype.bp_conspeed = t.c_ispeed; 336 } 337 338 process_menu(MENU_getboottype, &boottype); 339 msg_display(MSG_dobootblks, pm->diskdev); 340 if (boottype.bp_consdev == ~0u) 341 /* Use existing bootblocks */ 342 return 0; 343 344 ret = cp_to_target("/usr/mdec/boot", "/boot"); 345 if (ret) 346 return ret; 347 348 bootxx_filename = bootxx_name(); 349 if (bootxx_filename != NULL) { 350 snprintf(boot_options, sizeof boot_options, 351 "console=%s,speed=%u", consoles[boottype.bp_consdev], 352 boottype.bp_conspeed); 353 if (pm->isspecial) { 354 ret = run_program(RUN_DISPLAY | RUN_NO_CLEAR, 355 "/usr/sbin/installboot -o %s /dev/r%s %s", 356 boot_options, pm->diskdev, bootxx_filename); 357 } else { 358 ret = run_program(RUN_DISPLAY | RUN_NO_CLEAR, 359 "/usr/sbin/installboot -o %s /dev/r%s%c %s", 360 boot_options, pm->diskdev, 'a' + pm->rootpart, 361 bootxx_filename); 362 } 363 free(bootxx_filename); 364 } else 365 ret = -1; 366 367 if (ret != 0) 368 process_menu(MENU_ok, 369 deconst("Warning: disk is probably not bootable")); 370 371 return ret; 372 } 373 374 int 375 md_post_extract(void) 376 { 377 return 0; 378 } 379 380 void 381 md_cleanup_install(void) 382 { 383 #ifndef DEBUG 384 enable_rc_conf(); 385 add_rc_conf("wscons=YES\n"); 386 387 # if defined(__i386__) && defined(SET_KERNEL_TINY) 388 /* 389 * For GENERIC_TINY, do not enable any extra screens or wsmux. 390 * Otherwise, run getty on 4 VTs. 391 */ 392 if (get_kernel_set() == SET_KERNEL_TINY) 393 run_program(RUN_CHROOT, 394 "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;" 395 "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf"); 396 else 397 # endif 398 run_program(RUN_CHROOT, 399 "sed -an -e '/^ttyE[1-9]/s/off/on/;" 400 "H;$!d;g;w /etc/ttys' /etc/ttys"); 401 402 #endif 403 } 404 405 int 406 md_pre_update(void) 407 { 408 return 1; 409 } 410 411 /* Upgrade support */ 412 int 413 md_update(void) 414 { 415 md_post_newfs(); 416 md_upgrade_mbrtype(); 417 return 1; 418 } 419 420 int 421 md_check_mbr(mbr_info_t *mbri) 422 { 423 return 2; 424 } 425 426 int 427 md_mbr_use_wholedisk(mbr_info_t *mbri) 428 { 429 return mbr_use_wholedisk(mbri); 430 } 431 432 static int 433 get_bios_info(char *dev) 434 { 435 static struct disklist *disklist = NULL; 436 static int mib[2] = {CTL_MACHDEP, CPU_DISKINFO}; 437 int i; 438 size_t len; 439 struct biosdisk_info *bip; 440 struct nativedisk_info *nip = NULL, *nat; 441 int cyl, head; 442 daddr_t sec; 443 444 if (disklist == NULL) { 445 if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) 446 goto nogeom; 447 disklist = malloc(len); 448 if (disklist == NULL) { 449 fprintf(stderr, "Out of memory\n"); 450 return -1; 451 } 452 sysctl(mib, 2, disklist, &len, NULL, 0); 453 } 454 455 for (i = 0; i < disklist->dl_nnativedisks; i++) { 456 nat = &disklist->dl_nativedisks[i]; 457 if (!strcmp(dev, nat->ni_devname)) { 458 nip = nat; 459 break; 460 } 461 } 462 if (nip == NULL || nip->ni_nmatches == 0) { 463 nogeom: 464 if (nip != NULL) 465 msg_display(MSG_nobiosgeom, pm->dlcyl, pm->dlhead, pm->dlsec); 466 if (guess_biosgeom_from_mbr(&mbr, &cyl, &head, &sec) >= 0 467 && nip != NULL) 468 msg_display_add(MSG_biosguess, cyl, head, sec); 469 biosdisk = NULL; 470 } else { 471 guess_biosgeom_from_mbr(&mbr, &cyl, &head, &sec); 472 if (nip->ni_nmatches == 1) { 473 bip = &disklist->dl_biosdisks[nip->ni_biosmatches[0]]; 474 msg_display(MSG_onebiosmatch); 475 msg_table_add(MSG_onebiosmatch_header); 476 msg_table_add(MSG_onebiosmatch_row, bip->bi_dev, 477 bip->bi_cyl, bip->bi_head, bip->bi_sec, 478 (unsigned)bip->bi_lbasecs, 479 (unsigned)(bip->bi_lbasecs / (1000000000 / 512))); 480 msg_display_add(MSG_biosgeom_advise); 481 biosdisk = bip; 482 process_menu(MENU_biosonematch, &biosdisk); 483 } else { 484 msg_display(MSG_biosmultmatch); 485 msg_table_add(MSG_biosmultmatch_header); 486 for (i = 0; i < nip->ni_nmatches; i++) { 487 bip = &disklist->dl_biosdisks[ 488 nip->ni_biosmatches[i]]; 489 msg_table_add(MSG_biosmultmatch_row, i, 490 bip->bi_dev, bip->bi_cyl, bip->bi_head, 491 bip->bi_sec, (unsigned)bip->bi_lbasecs, 492 (unsigned)bip->bi_lbasecs/(1000000000/512)); 493 } 494 process_menu(MENU_biosmultmatch, &i); 495 if (i == -1) 496 biosdisk = NULL; 497 else 498 biosdisk = &disklist->dl_biosdisks[ 499 nip->ni_biosmatches[i]]; 500 } 501 } 502 if (biosdisk == NULL) { 503 if (nip != NULL) { 504 set_bios_geom(cyl, head, sec); 505 } else { 506 bcyl = cyl; 507 bhead = head; 508 bsec = sec; 509 } 510 } else { 511 bcyl = biosdisk->bi_cyl; 512 bhead = biosdisk->bi_head; 513 bsec = biosdisk->bi_sec; 514 } 515 return 0; 516 } 517 518 static int 519 mbr_root_above_chs(void) 520 { 521 return pm->ptstart + DEFROOTSIZE * (MEG / 512) >= bcyl * bhead * bsec; 522 } 523 524 static void 525 md_upgrade_mbrtype(void) 526 { 527 struct mbr_partition *mbrp; 528 int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0; 529 530 if (pm->no_mbr) 531 return; 532 533 if (read_mbr(pm->diskdev, &mbr) < 0) 534 return; 535 536 mbrp = &mbr.mbr.mbr_parts[0]; 537 538 for (i = 0; i < MBR_PART_COUNT; i++) { 539 if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) { 540 oldbsdpart = i; 541 oldbsdcount++; 542 } else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD) 543 netbsdpart = i; 544 } 545 546 if (netbsdpart == -1 && oldbsdcount == 1) { 547 mbrp[oldbsdpart].mbrp_type = MBR_PTYPE_NETBSD; 548 write_mbr(pm->diskdev, &mbr, 0); 549 } 550 } 551 552 /* 553 * Read MBR code from a file. 554 * The existing partition table and bootselect configuration is kept. 555 */ 556 static int 557 md_read_bootcode(const char *path, struct mbr_sector *mbrs) 558 { 559 int fd; 560 struct stat st; 561 size_t len; 562 struct mbr_sector new_mbr; 563 uint32_t dsn; 564 565 fd = open(path, O_RDONLY); 566 if (fd < 0) 567 return -1; 568 569 if (fstat(fd, &st) < 0 || st.st_size != sizeof *mbrs) { 570 close(fd); 571 return -1; 572 } 573 574 if (read(fd, &new_mbr, sizeof new_mbr) != sizeof new_mbr) { 575 close(fd); 576 return -1; 577 } 578 close(fd); 579 580 if (new_mbr.mbr_bootsel_magic != htole16(MBR_BS_MAGIC)) 581 return -1; 582 583 if (mbrs->mbr_bootsel_magic == htole16(MBR_BS_MAGIC)) { 584 len = offsetof(struct mbr_sector, mbr_bootsel); 585 } else 586 len = offsetof(struct mbr_sector, mbr_parts); 587 588 /* Preserve the 'drive serial number' - especially for Vista */ 589 dsn = mbrs->mbr_dsn; 590 memcpy(mbrs, &new_mbr, len); 591 mbrs->mbr_dsn = dsn; 592 593 /* Keep flags from object file - indicate the properties */ 594 mbrs->mbr_bootsel.mbrbs_flags = new_mbr.mbr_bootsel.mbrbs_flags; 595 mbrs->mbr_magic = htole16(MBR_MAGIC); 596 597 return 0; 598 } 599 600 static unsigned int 601 get_bootmodel(void) 602 { 603 #if defined(__i386__) 604 struct utsname ut; 605 #ifdef DEBUG 606 char *envstr; 607 608 envstr = getenv("BOOTMODEL"); 609 if (envstr != NULL) 610 return atoi(envstr); 611 #endif 612 613 if (uname(&ut) < 0) 614 ut.version[0] = 0; 615 616 #if defined(SET_KERNEL_TINY) 617 if (strstr(ut.version, "TINY") != NULL) 618 return SET_KERNEL_TINY; 619 #endif 620 #if defined(SET_KERNEL_PS2) 621 if (strstr(ut.version, "PS2") != NULL) 622 return SET_KERNEL_PS2; 623 #endif 624 #endif 625 return SET_KERNEL_GENERIC; 626 } 627 628 629 int 630 md_pre_mount() 631 { 632 return 0; 633 } 634