1 /* $NetBSD: fdisk.c,v 1.48 2001/11/07 14:50:32 lukem Exp $ */ 2 3 /* 4 * Mach Operating System 5 * Copyright (c) 1992 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie Mellon 26 * the rights to redistribute these changes. 27 */ 28 29 #include <sys/cdefs.h> 30 31 #ifndef lint 32 __RCSID("$NetBSD: fdisk.c,v 1.48 2001/11/07 14:50:32 lukem Exp $"); 33 #endif /* not lint */ 34 35 #include <sys/types.h> 36 #include <sys/disklabel.h> 37 #include <sys/disklabel_mbr.h> 38 #include <sys/ioctl.h> 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 #include <sys/sysctl.h> 42 43 #include <ctype.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <paths.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include <util.h> 53 54 #ifdef __i386__ 55 #include <machine/cpu.h> 56 #endif 57 58 #define LBUF 100 59 static char lbuf[LBUF]; 60 61 /* 62 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University 63 * Copyright (c) 1989 Robert. V. Baron 64 * Created. 65 */ 66 67 #ifndef _PATH_DEFDISK 68 #define _PATH_DEFDISK "/dev/rwd0d" 69 #endif 70 71 const char *disk = _PATH_DEFDISK; 72 73 struct disklabel disklabel; /* disk parameters */ 74 75 int cylinders, sectors, heads, cylindersectors, disksectors; 76 77 struct mboot { 78 u_int8_t padding[2]; /* force the longs to be long alligned */ 79 u_int8_t bootinst[MBR_PARTOFF]; 80 struct mbr_partition parts[NMBRPART]; 81 u_int16_t signature; 82 }; 83 struct mboot mboot; 84 85 #ifdef __i386__ 86 87 #define PARTNAMESIZE 8 /* From mbr_bootsel.S */ 88 89 struct mbr_bootsel { 90 u_int8_t defkey; 91 u_int8_t flags; 92 u_int16_t timeo; 93 char nametab[4][PARTNAMESIZE + 1]; 94 u_int16_t magic; 95 } __attribute__((packed)); 96 97 #define BFL_SELACTIVE 0x01 98 #define BFL_EXTINT13 0x02 99 100 #define SCAN_ENTER 0x1c 101 #define SCAN_F1 0x3b 102 103 #define MBR_BOOTSELOFF (MBR_PARTOFF - sizeof (struct mbr_bootsel)) 104 105 #define DEFAULT_BOOTCODE "/usr/mdec/mbr" 106 #define DEFAULT_BOOTSELCODE "/usr/mdec/mbr_bootsel" 107 #define OPTIONS "0123BSafius:b:c:" 108 #else 109 #define OPTIONS "0123Safius:b:c:" 110 #endif 111 112 #define ACTIVE 0x80 113 114 int dos_cylinders; 115 int dos_heads; 116 int dos_sectors; 117 int dos_cylindersectors; 118 119 #define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0)) 120 #define DOSCYL(c) ((c) & 0xff) 121 122 #define MAXCYL 1024 123 int partition = -1; 124 125 int a_flag; /* set active partition */ 126 int i_flag; /* init bootcode */ 127 int u_flag; /* update partition data */ 128 int sh_flag; /* Output data as shell defines */ 129 int f_flag; /* force --not interactive */ 130 int s_flag; /* set id,offset,size */ 131 int b_flag; /* Set cyl, heads, secs (as c/h/s) */ 132 int B_flag; /* Edit/install bootselect code */ 133 int b_cyl, b_head, b_sec; /* b_flag values. */ 134 int bootsel_modified; 135 136 unsigned char bootcode[8192]; /* maximum size of bootcode */ 137 unsigned char tempcode[8192]; 138 int bootsize; /* actual size of bootcode */ 139 140 141 static char reserved[] = "reserved"; 142 143 struct part_type { 144 int type; 145 const char *name; 146 } part_types[] = { 147 {0x00, "unused"}, 148 {0x01, "Primary DOS with 12 bit FAT"}, 149 {0x02, "XENIX / filesystem"}, 150 {0x03, "XENIX /usr filesystem"}, 151 {0x04, "Primary DOS with 16 bit FAT <32M"}, 152 {0x05, "Extended partition"}, 153 {0x06, "Primary 'big' DOS, 16-bit FAT (> 32MB)"}, 154 {0x07, "OS/2 HPFS or NTFS or QNX2 or Advanced UNIX"}, 155 {0x08, "AIX filesystem or OS/2 (thru v1.3) or DELL multiple drives" 156 "or Commodore DOS or SplitDrive"}, 157 {0x09, "AIX boot partition or Coherent"}, 158 {0x0A, "OS/2 Boot Manager or Coherent swap or OPUS"}, 159 {0x0b, "Primary DOS with 32 bit FAT"}, 160 {0x0c, "Primary DOS with 32 bit FAT - LBA"}, 161 {0x0d, "Type 7??? - LBA"}, 162 {0x0E, "DOS (16-bit FAT) - LBA"}, 163 {0x0F, "Ext. partition - LBA"}, 164 {0x10, "OPUS"}, 165 {0x11, "OS/2 BM: hidden DOS 12-bit FAT"}, 166 {0x12, "Compaq diagnostics"}, 167 {0x14, "OS/2 BM: hidden DOS 16-bit FAT <32M or Novell DOS 7.0 bug"}, 168 {0x16, "OS/2 BM: hidden DOS 16-bit FAT >=32M"}, 169 {0x17, "OS/2 BM: hidden IFS"}, 170 {0x18, "AST Windows swapfile"}, 171 {0x19, "Willowtech Photon coS"}, 172 {0x1e, "hidden FAT95"}, 173 {0x20, "Willowsoft OFS1"}, 174 {0x21, reserved}, 175 {0x23, reserved}, 176 {0x24, "NEC DOS"}, 177 {0x26, reserved}, 178 {0x31, reserved}, 179 {0x33, reserved}, 180 {0x34, reserved}, 181 {0x36, reserved}, 182 {0x38, "Theos"}, 183 {0x3C, "PartitionMagic recovery"}, 184 {0x40, "VENIX 286 or LynxOS"}, 185 {0x41, "Linux/MINIX (sharing disk with DRDOS) or Personal RISC boot"}, 186 {0x42, "SFS or Linux swap (sharing disk with DRDOS)"}, 187 {0x43, "Linux native (sharing disk with DRDOS)"}, 188 {0x4D, "QNX4.x"}, 189 {0x4E, "QNX4.x 2nd part"}, 190 {0x4F, "QNX4.x 3rd part"}, 191 {0x50, "DM (disk manager)"}, 192 {0x51, "DM6 Aux1 (or Novell)"}, 193 {0x52, "CP/M or Microport SysV/AT"}, 194 {0x53, "DM6 Aux3"}, 195 {0x54, "DM6 DDO"}, 196 {0x55, "EZ-Drive (disk manager)"}, 197 {0x56, "Golden Bow (disk manager)"}, 198 {0x5C, "Priam Edisk (disk manager)"}, 199 {0x61, "SpeedStor"}, 200 {0x63, "GNU HURD or Mach or Sys V/386 (such as ISC UNIX) or MtXinu"}, 201 {0x64, "Novell Netware 2.xx or Speedstore"}, 202 {0x65, "Novell Netware 3.xx"}, 203 {0x66, "Novell 386 Netware"}, 204 {0x67, "Novell"}, 205 {0x68, "Novell"}, 206 {0x69, "Novell"}, 207 {0x70, "DiskSecure Multi-Boot"}, 208 {0x71, reserved}, 209 {0x73, reserved}, 210 {0x74, reserved}, 211 {0x75, "PC/IX"}, 212 {0x76, reserved}, 213 {0x80, "MINIX until 1.4a"}, 214 {0x81, "MINIX since 1.4b, early Linux, Mitac dmgr"}, 215 {0x82, "Linux swap or Prime or Solaris"}, 216 {0x83, "Linux native"}, 217 {0x84, "OS/2 hidden C: drive"}, 218 {0x85, "Linux extended"}, 219 {0x86, "NT FAT volume set"}, 220 {0x87, "NTFS volume set or HPFS mirrored"}, 221 {0x93, "Amoeba filesystem"}, 222 {0x94, "Amoeba bad block table"}, 223 {0x99, "Mylex EISA SCSI"}, 224 {0x9f, "BSDI?"}, 225 {0xA0, "IBM Thinkpad hibernation"}, 226 {0xa1, reserved}, 227 {0xa3, reserved}, 228 {0xa4, reserved}, 229 {0xA5, "FreeBSD or 386BSD or old NetBSD"}, 230 {0xA6, "OpenBSD"}, 231 {0xA7, "NeXTSTEP 486"}, 232 {0xa9, "NetBSD"}, 233 {0xb1, reserved}, 234 {0xb3, reserved}, 235 {0xb4, reserved}, 236 {0xb6, reserved}, 237 {0xB7, "BSDI BSD/386 filesystem"}, 238 {0xB8, "BSDI BSD/386 swap"}, 239 {0xc0, "CTOS"}, 240 {0xC1, "DRDOS/sec (FAT-12)"}, 241 {0xC4, "DRDOS/sec (FAT-16, < 32M)"}, 242 {0xC6, "DRDOS/sec (FAT-16, >= 32M)"}, 243 {0xC7, "Syrinx (Cyrnix?) or HPFS disabled"}, 244 {0xd8, "CP/M 86"}, 245 {0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"}, 246 {0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"}, 247 {0xE3, "DOS R/O or SpeedStor or Storage Dimensions"}, 248 {0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."}, 249 {0xe5, reserved}, 250 {0xe6, reserved}, 251 {0xeb, "BeOS"}, 252 {0xF1, "SpeedStor or Storage Dimensions"}, 253 {0xF2, "DOS 3.3+ Secondary"}, 254 {0xf3, reserved}, 255 {0xF4, "SpeedStor large partition or Storage Dimensions"}, 256 {0xf6, reserved}, 257 {0xFE, "SpeedStor >1024 cyl. or LANstep or IBM PS/2 IML"}, 258 {0xFF, "Xenix Bad Block Table"}, 259 }; 260 261 void usage(void); 262 void print_s0(int); 263 void print_part(int); 264 void print_mbr_partition(struct mbr_partition *, off_t, off_t, int); 265 int read_boot(const char *, void *, size_t); 266 void init_sector0(int, int); 267 void intuit_translated_geometry(void); 268 void get_geometry(void); 269 void get_diskname(const char *, char *, size_t); 270 int try_heads(quad_t, quad_t, quad_t, quad_t, quad_t, quad_t, quad_t, 271 quad_t); 272 int try_sectors(quad_t, quad_t, quad_t, quad_t, quad_t); 273 void change_part(int, int, int, int); 274 void print_params(void); 275 void change_active(int); 276 void get_params_to_use(void); 277 void dos(int, unsigned char *, unsigned char *, unsigned char *); 278 int open_disk(int); 279 int read_disk(off_t, void *); 280 int write_disk(off_t, void *); 281 int get_params(void); 282 int read_s0(off_t, struct mboot *); 283 int write_s0(void); 284 int yesno(const char *); 285 void decimal(const char *, int *); 286 int type_match(const void *, const void *); 287 const char *get_type(int); 288 int get_mapping(int, int *, int *, int *, unsigned long *); 289 #ifdef __i386__ 290 void configure_bootsel(void); 291 #endif 292 293 static unsigned short getshort(void *); 294 static void putshort(void *p, unsigned short); 295 static unsigned long getlong(void *); 296 static void putlong(void *, unsigned long); 297 298 299 int main(int, char *[]); 300 301 int 302 main(int argc, char *argv[]) 303 { 304 int ch, part, mib[2], len; 305 char *root_device; 306 307 int csysid, cstart, csize; /* For the b_flag. */ 308 309 mib[0] = CTL_KERN; 310 mib[1] = KERN_ROOT_DEVICE; 311 if (sysctl(mib, 2, NULL, &len, NULL, 0) != -1 && 312 (root_device = malloc(len)) != NULL && 313 sysctl(mib, 2, root_device, &len, NULL, 0) != -1) 314 disk = root_device; 315 316 a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0; 317 csysid = cstart = csize = 0; 318 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 319 switch (ch) { 320 case '0': 321 partition = 0; 322 break; 323 case '1': 324 partition = 1; 325 break; 326 case '2': 327 partition = 2; 328 break; 329 case '3': 330 partition = 3; 331 break; 332 #ifdef __i386__ 333 case 'B': 334 B_flag = 1; 335 break; 336 #endif 337 case 'S': 338 sh_flag = 1; 339 break; 340 case 'a': 341 a_flag = 1; 342 break; 343 case 'f': 344 f_flag = 1; 345 break; 346 case 'i': 347 i_flag = 1; 348 break; 349 case 'u': 350 u_flag = 1; 351 break; 352 case 's': 353 s_flag = 1; 354 if (sscanf (optarg, "%d/%d/%d", 355 &csysid, &cstart, &csize) != 3) { 356 (void)fprintf (stderr, "%s: Bad argument " 357 "to the -s flag.\n", 358 argv[0]); 359 exit (1); 360 } 361 break; 362 case 'b': 363 b_flag = 1; 364 if (sscanf (optarg, "%d/%d/%d", 365 &b_cyl, &b_head, &b_sec) != 3) { 366 (void)fprintf (stderr, "%s: Bad argument " 367 "to the -b flag.\n", 368 argv[0]); 369 exit (1); 370 } 371 if (b_cyl > MAXCYL) 372 b_cyl = MAXCYL; 373 break; 374 case 'c': 375 bootsize = read_boot(optarg, bootcode, sizeof bootcode); 376 break; 377 default: 378 usage(); 379 } 380 argc -= optind; 381 argv += optind; 382 383 if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag)) 384 usage(); 385 386 if (B_flag && (a_flag || i_flag || u_flag || f_flag || s_flag)) 387 usage(); 388 389 if (partition == -1 && s_flag) { 390 (void) fprintf (stderr, 391 "-s flag requires a partition selected.\n"); 392 usage(); 393 } 394 395 if (argc > 0) 396 disk = argv[0]; 397 398 if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0) 399 exit(1); 400 401 if (read_s0(0, &mboot)) 402 init_sector0(sectors > 63 ? 63 : sectors, 1); 403 404 #ifdef __i386__ 405 get_geometry(); 406 #else 407 intuit_translated_geometry(); 408 #endif 409 410 411 if ((i_flag || u_flag) && (!f_flag || b_flag)) 412 get_params_to_use(); 413 414 if (i_flag) 415 init_sector0(dos_sectors > 63 ? 63 : dos_sectors, 0); 416 417 /* Do the update stuff! */ 418 if (u_flag) { 419 if (!f_flag) 420 printf("Partition table:\n"); 421 if (partition == -1) 422 for (part = 0; part < NMBRPART; part++) 423 change_part(part,-1, -1, -1); 424 else 425 change_part(partition, csysid, cstart, csize); 426 } else 427 if (!i_flag) 428 print_s0(partition); 429 430 if (a_flag) 431 change_active(partition); 432 433 #ifdef __i386__ 434 if (B_flag) { 435 configure_bootsel(); 436 if (B_flag && bootsel_modified) 437 write_s0(); 438 } 439 #endif 440 441 if (u_flag || a_flag || i_flag) { 442 if (!f_flag) { 443 printf("\nWe haven't written the MBR back to disk " 444 "yet. This is your last chance.\n"); 445 print_s0(-1); 446 if (yesno("Should we write new partition table?")) 447 write_s0(); 448 } else 449 write_s0(); 450 } 451 452 exit(0); 453 } 454 455 void 456 usage(void) 457 { 458 459 (void)fprintf(stderr, "usage: fdisk [-aiufBS] [-0|-1|-2|-3] " 460 "[-b cylinders/heads/sectors]\n" 461 " [-s id/start/size] [-c bootcode] " 462 "[device]\n"); 463 exit(1); 464 } 465 466 void 467 print_s0(int which) 468 { 469 int part; 470 471 print_params(); 472 if (!sh_flag) 473 printf("Partition table:\n"); 474 if (which == -1) { 475 for (part = 0; part < NMBRPART; part++) { 476 if (!sh_flag) 477 printf("%d: ", part); 478 print_part(part); 479 } 480 } else 481 print_part(which); 482 } 483 484 static unsigned short 485 getshort(void *p) 486 { 487 unsigned char *cp = p; 488 489 return cp[0] | (cp[1] << 8); 490 } 491 492 static void 493 putshort(void *p, unsigned short l) 494 { 495 unsigned char *cp = p; 496 497 *cp++ = l; 498 *cp++ = l >> 8; 499 } 500 501 static unsigned long 502 getlong(void *p) 503 { 504 unsigned char *cp = p; 505 506 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24); 507 } 508 509 static void 510 putlong(void *p, unsigned long l) 511 { 512 unsigned char *cp = p; 513 514 *cp++ = l; 515 *cp++ = l >> 8; 516 *cp++ = l >> 16; 517 *cp++ = l >> 24; 518 } 519 520 void 521 print_part(int part) 522 { 523 struct mbr_partition *partp; 524 int empty; 525 526 partp = &mboot.parts[part]; 527 empty = (partp->mbrp_typ == 0); 528 529 if (sh_flag) { 530 if (empty) { 531 printf("PART%dSIZE=0\n", part); 532 return; 533 } 534 535 printf("PART%dID=%d\n", part, partp->mbrp_typ); 536 printf("PART%dSIZE=%ld\n", part, getlong(&partp->mbrp_size)); 537 printf("PART%dSTART=%ld\n", part, getlong(&partp->mbrp_start)); 538 printf("PART%dFLAG=0x%x\n", part, partp->mbrp_flag); 539 printf("PART%dBCYL=%d\n", part, MBR_PCYL(partp->mbrp_scyl, 540 partp->mbrp_ssect)); 541 printf("PART%dBHEAD=%d\n", part, partp->mbrp_shd); 542 printf("PART%dBSEC=%d\n", part, MBR_PSECT(partp->mbrp_ssect)); 543 printf("PART%dECYL=%d\n", part, MBR_PCYL(partp->mbrp_ecyl, 544 partp->mbrp_esect)); 545 printf("PART%dEHEAD=%d\n", part, partp->mbrp_ehd); 546 printf("PART%dESEC=%d\n", part, MBR_PSECT(partp->mbrp_esect)); 547 return; 548 } 549 print_mbr_partition(partp, 0, 0, 0); 550 } 551 552 void 553 print_mbr_partition(struct mbr_partition *partp, 554 off_t offset, off_t exoffset, int indent) 555 { 556 int empty; 557 off_t start; 558 559 empty = (partp->mbrp_typ == 0); 560 if (MBR_IS_EXTENDED(partp->mbrp_typ)) 561 start = (off_t)getlong(&partp->mbrp_start) + exoffset; 562 else 563 start = (off_t)getlong(&partp->mbrp_start) + offset; 564 if (empty) { 565 printf("<UNUSED>\n"); 566 return; 567 } 568 printf("sysid %d (%s)\n", 569 partp->mbrp_typ, get_type(partp->mbrp_typ)); 570 printf("%*s start %lld, size %ld (%ld MB), flag 0x%x\n", 571 indent, "", 572 start, getlong(&partp->mbrp_size), 573 getlong(&partp->mbrp_size) * 512 / (1024 * 1024), partp->mbrp_flag); 574 printf("%*s beg: cylinder %4d, head %3d, sector %2d\n", 575 indent, "", 576 MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect), 577 partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect)); 578 printf("%*s end: cylinder %4d, head %3d, sector %2d\n", 579 indent, "", 580 MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect), 581 partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect)); 582 583 if (MBR_IS_EXTENDED(partp->mbrp_typ)) { 584 struct mboot eboot; 585 int part; 586 587 printf("%*s Extended partition table:\n", indent, ""); 588 if (read_s0(start, &eboot) == -1) 589 return; 590 indent += 8; 591 if (exoffset == 0) 592 exoffset = start; 593 for (part = 0; part < NMBRPART; part++) { 594 printf("%*s%d: ", indent, "", part); 595 print_mbr_partition(&eboot.parts[part], 596 start, exoffset, indent); 597 } 598 } 599 } 600 601 int 602 read_boot(const char *name, void *buf, size_t len) 603 { 604 int bfd, ret; 605 struct stat st; 606 607 if ((bfd = open(name, O_RDONLY)) < 0) 608 err(1, "%s", name); 609 if (fstat(bfd, &st) == -1) 610 err(1, "%s", name); 611 if (st.st_size > len) 612 errx(1, "%s: bootcode too large", name); 613 ret = st.st_size; 614 if (ret < 0x200) 615 errx(1, "%s: bootcode too small", name); 616 if (read(bfd, buf, len) != ret) 617 err(1, "%s", name); 618 close(bfd); 619 620 /* 621 * Do some sanity checking here 622 */ 623 if (getshort(bootcode + MBR_MAGICOFF) != MBR_MAGIC) 624 errx(1, "%s: invalid magic", name); 625 ret = (ret + 0x1ff) / 0x200; 626 ret *= 0x200; 627 return ret; 628 } 629 630 void 631 init_sector0(int start, int dopart) 632 { 633 int i; 634 635 #ifdef DEFAULT_BOOTCODE 636 if (!bootsize) 637 bootsize = read_boot(DEFAULT_BOOTCODE, bootcode, 638 sizeof bootcode); 639 #endif 640 641 memcpy(mboot.bootinst, bootcode, sizeof(mboot.bootinst)); 642 putshort(&mboot.signature, MBR_MAGIC); 643 644 if (dopart) 645 for (i=0; i<4; i++) 646 memset(&mboot.parts[i], 0, sizeof(struct mbr_partition)); 647 648 } 649 650 #ifdef __i386__ 651 652 void 653 get_diskname(const char *fullname, char *diskname, size_t size) 654 { 655 const char *p, *p2; 656 size_t len; 657 658 p = strrchr(fullname, '/'); 659 if (p == NULL) 660 p = fullname; 661 else 662 p++; 663 664 if (*p == 0) { 665 strncpy(diskname, fullname, size - 1); 666 diskname[size - 1] = '\0'; 667 return; 668 } 669 670 if (*p == 'r') 671 p++; 672 673 for (p2 = p; *p2 != 0; p2++) 674 if (isdigit(*p2)) 675 break; 676 if (*p2 == 0) { 677 /* XXX invalid diskname? */ 678 strncpy(diskname, fullname, size - 1); 679 diskname[size - 1] = '\0'; 680 return; 681 } 682 while (isdigit(*p2)) 683 p2++; 684 685 len = p2 - p; 686 if (len > size) { 687 /* XXX */ 688 strncpy(diskname, fullname, size - 1); 689 diskname[size - 1] = '\0'; 690 return; 691 } 692 693 strncpy(diskname, p, len); 694 diskname[len] = 0; 695 } 696 697 void 698 get_geometry(void) 699 { 700 int mib[2], i; 701 size_t len; 702 struct disklist *dl; 703 struct biosdisk_info *bip; 704 struct nativedisk_info *nip; 705 char diskname[8]; 706 707 mib[0] = CTL_MACHDEP; 708 mib[1] = CPU_DISKINFO; 709 if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) { 710 intuit_translated_geometry(); 711 return; 712 } 713 dl = (struct disklist *) malloc(len); 714 sysctl(mib, 2, dl, &len, NULL, 0); 715 716 get_diskname(disk, diskname, sizeof diskname); 717 718 for (i = 0; i < dl->dl_nnativedisks; i++) { 719 nip = &dl->dl_nativedisks[i]; 720 if (strcmp(diskname, nip->ni_devname)) 721 continue; 722 /* 723 * XXX listing possible matches is better. This is ok 724 * for now because the user has a chance to change 725 * it later. 726 */ 727 if (nip->ni_nmatches != 0) { 728 bip = &dl->dl_biosdisks[nip->ni_biosmatches[0]]; 729 dos_cylinders = bip->bi_cyl; 730 dos_heads = bip->bi_head; 731 dos_sectors = bip->bi_sec; 732 dos_cylindersectors = bip->bi_head * bip->bi_sec; 733 return; 734 } 735 } 736 /* Allright, allright, make a stupid guess.. */ 737 intuit_translated_geometry(); 738 } 739 740 void 741 configure_bootsel(void) 742 { 743 struct mbr_bootsel *mbs = 744 (struct mbr_bootsel *)&mboot.bootinst[MBR_BOOTSELOFF]; 745 int i, nused, firstpart = -1, item; 746 char desc[PARTNAMESIZE + 2], *p; 747 int timo, entry_changed = 0; 748 749 for (i = nused = 0; i < NMBRPART; ++i) { 750 if (mboot.parts[i].mbrp_typ != 0) { 751 if (firstpart == -1) 752 firstpart = i; 753 nused++; 754 } 755 } 756 757 if (nused == 0) { 758 printf("No used partitions found. Partition the disk first.\n"); 759 return; 760 } 761 762 if (mbs->magic != MBR_MAGIC) { 763 if (!yesno("Bootselector not yet installed. Install it now?")) { 764 printf("Bootselector not installed.\n"); 765 return; 766 } 767 bootsize = read_boot(DEFAULT_BOOTSELCODE, bootcode, 768 sizeof bootcode); 769 memcpy(mboot.bootinst, bootcode, sizeof(mboot.bootinst)); 770 bootsel_modified = 1; 771 mbs->flags |= BFL_SELACTIVE; 772 } else { 773 if (mbs->flags & BFL_SELACTIVE) { 774 printf("The bootselector is installed and active.\n"); 775 if (!yesno("Do you want to change its settings?")) { 776 if (yesno("Do you want to deactivate it?")) { 777 mbs->flags &= ~BFL_SELACTIVE; 778 bootsel_modified = 1; 779 goto done; 780 } 781 return; 782 } 783 } else { 784 printf("The bootselector is installed but not active.\n"); 785 if (yesno("Do you want to activate it?")) { 786 mbs->flags |= BFL_SELACTIVE; 787 bootsel_modified = 1; 788 } 789 if (!yesno("Do you want to change its settings?")) 790 goto done; 791 } 792 } 793 794 printf("\n\nPartition table:\n"); 795 for (i = 0; i < NMBRPART; i++) { 796 printf("%d: ", i); 797 print_part(i); 798 } 799 800 printf("\n\nCurrent boot selection menu option names:\n"); 801 for (i = 0; i < NMBRPART; i++) { 802 if (mbs->nametab[i][0] != 0) 803 printf("%d: %s\n", i, &mbs->nametab[i][0]); 804 else 805 printf("%d: <UNUSED>\n", i); 806 } 807 printf("\n"); 808 809 item = firstpart; 810 811 editentries: 812 while (1) { 813 decimal("Change which entry (-1 quits)?", &item); 814 if (item == -1) 815 break; 816 if (item < 0 || item >= NMBRPART) { 817 printf("Invalid entry number\n"); 818 item = -1; 819 continue; 820 } 821 if (mboot.parts[item].mbrp_typ == 0) { 822 printf("The partition entry is unused\n"); 823 item = -1; 824 continue; 825 } 826 827 printf("Enter descriptions (max. 8 characters): "); 828 rewind(stdin); 829 fgets(desc, PARTNAMESIZE + 1, stdin); 830 fpurge(stdin); 831 p = strchr(desc, '\n'); 832 if (p != NULL) 833 *p = 0; 834 strcpy(&mbs->nametab[item][0], desc); 835 entry_changed = bootsel_modified = 1; 836 837 item++; 838 } 839 840 if (entry_changed) 841 printf("Boot selection menu option names are now:\n"); 842 843 firstpart = -1; 844 for (i = 0; i < NMBRPART; i++) { 845 if (mbs->nametab[i][0] != 0) { 846 firstpart = i; 847 if (entry_changed) 848 printf("%d: %s\n", i, &mbs->nametab[i][0]); 849 } else { 850 if (entry_changed) 851 printf("%d: <UNUSED>\n", i); 852 } 853 } 854 if (entry_changed) 855 printf("\n"); 856 857 if (firstpart == -1) { 858 printf("All menu entries are now inactive.\n"); 859 if (!yesno("Are you sure about this?")) 860 goto editentries; 861 } else { 862 if (!(mbs->flags & BFL_SELACTIVE)) { 863 printf("The bootselector is not yet active.\n"); 864 if (yesno("Activate it now?")) 865 mbs->flags |= BFL_SELACTIVE; 866 } 867 } 868 869 /* bootsel is dirty from here on out. */ 870 bootsel_modified = 1; 871 872 /* The timeout value is in ticks, 18.2 Hz. Avoid using floats. */ 873 timo = ((1000 * mbs->timeo) / 18200); 874 do { 875 decimal("Timeout value", &timo); 876 } while (timo < 0 || timo > 3600); 877 mbs->timeo = (u_int16_t)((timo * 18200) / 1000); 878 879 printf("Select the default boot option. Options are:\n\n"); 880 for (i = 0; i < NMBRPART; i++) { 881 if (mbs->nametab[i][0] != 0) 882 printf("%d: %s\n", i, &mbs->nametab[i][0]); 883 } 884 for (i = 4; i < 10; i++) 885 printf("%d: Harddisk %d\n", i, i - 4); 886 printf("10: The first active partition\n"); 887 888 if (mbs->defkey == SCAN_ENTER) 889 item = 10; 890 else 891 item = mbs->defkey - SCAN_F1; 892 893 if (item < 0 || item > 10 || mbs->nametab[item][0] == 0) 894 item = 10; 895 896 do { 897 decimal("Default boot option", &item); 898 } while (item < 0 || item > 10 || 899 (item <= 3 && mbs->nametab[item][0] == 0)); 900 901 if (item == 10) 902 mbs->defkey = SCAN_ENTER; 903 else 904 mbs->defkey = SCAN_F1 + item; 905 906 done: 907 for (i = 0; i < NMBRPART; i++) { 908 if (mboot.parts[i].mbrp_typ != 0 && 909 mboot.parts[i].mbrp_start >= 910 (dos_cylinders * dos_heads * dos_sectors)) { 911 mbs->flags |= BFL_EXTINT13; 912 break; 913 } 914 } 915 916 if (bootsel_modified != 0 && !yesno("Update the bootselector?")) 917 bootsel_modified = 0; 918 } 919 #endif 920 921 922 /* Prerequisite: the disklabel parameters and master boot record must 923 * have been read (i.e. dos_* and mboot are meaningful). 924 * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and 925 * dos_cylindersectors to be consistent with what the 926 * partition table is using, if we can find a geometry 927 * which is consistent with all partition table entries. 928 * We may get the number of cylinders slightly wrong (in 929 * the conservative direction). The idea is to be able 930 * to create a NetBSD partition on a disk we don't know 931 * the translated geometry of. 932 * This whole routine should be replaced with a kernel interface to get 933 * the BIOS geometry (which in turn requires modifications to the i386 934 * boot loader to pass in the BIOS geometry for each disk). */ 935 void 936 intuit_translated_geometry(void) 937 { 938 939 int xcylinders = -1, xheads = -1, xsectors = -1, i, j; 940 int c1, h1, s1, c2, h2, s2; 941 long a1, a2; 942 quad_t num, denom; 943 944 /* Try to deduce the number of heads from two different mappings. */ 945 for (i = 0; i < NMBRPART * 2; i++) { 946 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0) 947 continue; 948 for (j = 0; j < 8; j++) { 949 if (get_mapping(j, &c2, &h2, &s2, &a2) < 0) 950 continue; 951 num = (quad_t)h1*(a2-s2) - (quad_t)h2*(a1-s1); 952 denom = (quad_t)c2*(a1-s1) - (quad_t)c1*(a2-s2); 953 if (denom != 0 && num % denom == 0) { 954 xheads = num / denom; 955 break; 956 } 957 } 958 if (xheads != -1) 959 break; 960 } 961 962 if (xheads == -1) 963 return; 964 965 /* Now figure out the number of sectors from a single mapping. */ 966 for (i = 0; i < NMBRPART * 2; i++) { 967 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0) 968 continue; 969 num = a1 - s1; 970 denom = c1 * xheads + h1; 971 if (denom != 0 && num % denom == 0) { 972 xsectors = num / denom; 973 break; 974 } 975 } 976 977 if (xsectors == -1) 978 return; 979 980 /* Estimate the number of cylinders. */ 981 xcylinders = disklabel.d_secperunit / xheads / xsectors; 982 983 /* Now verify consistency with each of the partition table entries. 984 * Be willing to shove cylinders up a little bit to make things work, 985 * but translation mismatches are fatal. */ 986 for (i = 0; i < NMBRPART * 2; i++) { 987 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0) 988 continue; 989 if (xsectors * (c1 * xheads + h1) + s1 != a1) 990 return; 991 if (c1 >= xcylinders) 992 xcylinders = c1 + 1; 993 } 994 995 /* Everything checks out. Reset the geometry to use for further 996 * calculations. */ 997 dos_cylinders = xcylinders; 998 dos_heads = xheads; 999 dos_sectors = xsectors; 1000 dos_cylindersectors = xheads * xsectors; 1001 } 1002 1003 /* For the purposes of intuit_translated_geometry(), treat the partition 1004 * table as a list of eight mapping between (cylinder, head, sector) 1005 * triplets and absolute sectors. Get the relevant geometry triplet and 1006 * absolute sectors for a given entry, or return -1 if it isn't present. 1007 * Note: for simplicity, the returned sector is 0-based. */ 1008 int 1009 get_mapping(int i, int *cylinder, int *head, int *sector, 1010 unsigned long *absolute) 1011 { 1012 struct mbr_partition *part = &mboot.parts[i / 2]; 1013 1014 if (part->mbrp_typ == 0) 1015 return -1; 1016 if (i % 2 == 0) { 1017 *cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect); 1018 *head = part->mbrp_shd; 1019 *sector = MBR_PSECT(part->mbrp_ssect) - 1; 1020 *absolute = getlong(&part->mbrp_start); 1021 } else { 1022 *cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect); 1023 *head = part->mbrp_ehd; 1024 *sector = MBR_PSECT(part->mbrp_esect) - 1; 1025 *absolute = getlong(&part->mbrp_start) 1026 + getlong(&part->mbrp_size) - 1; 1027 } 1028 return 0; 1029 } 1030 1031 void 1032 change_part(int part, int csysid, int cstart, int csize) 1033 { 1034 struct mbr_partition *partp; 1035 1036 partp = &mboot.parts[part]; 1037 1038 if (s_flag) { 1039 if (csysid == 0 && cstart == 0 && csize == 0) 1040 memset(partp, 0, sizeof *partp); 1041 else { 1042 partp->mbrp_typ = csysid; 1043 #if 0 1044 checkcyl(cstart / dos_cylindersectors); 1045 #endif 1046 putlong(&partp->mbrp_start, cstart); 1047 putlong(&partp->mbrp_size, csize); 1048 dos(getlong(&partp->mbrp_start), 1049 &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect); 1050 dos(getlong(&partp->mbrp_start) 1051 + getlong(&partp->mbrp_size) - 1, 1052 &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect); 1053 } 1054 if (f_flag) 1055 return; 1056 } 1057 1058 printf("The data for partition %d is:\n", part); 1059 print_part(part); 1060 if (!u_flag || !yesno("Do you want to change it?")) 1061 return; 1062 1063 do { 1064 { 1065 int sysid, start, size; 1066 1067 sysid = partp->mbrp_typ, 1068 start = getlong(&partp->mbrp_start), 1069 size = getlong(&partp->mbrp_size); 1070 decimal("sysid", &sysid); 1071 decimal("start", &start); 1072 decimal("size", &size); 1073 partp->mbrp_typ = sysid; 1074 putlong(&partp->mbrp_start, start); 1075 putlong(&partp->mbrp_size, size); 1076 } 1077 1078 if (yesno("Explicitly specify beg/end address?")) { 1079 int tsector, tcylinder, thead; 1080 1081 tcylinder = MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect); 1082 thead = partp->mbrp_shd; 1083 tsector = MBR_PSECT(partp->mbrp_ssect); 1084 decimal("beginning cylinder", &tcylinder); 1085 #if 0 1086 checkcyl(tcylinder); 1087 #endif 1088 decimal("beginning head", &thead); 1089 decimal("beginning sector", &tsector); 1090 partp->mbrp_scyl = DOSCYL(tcylinder); 1091 partp->mbrp_shd = thead; 1092 partp->mbrp_ssect = DOSSECT(tsector, tcylinder); 1093 1094 tcylinder = MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect); 1095 thead = partp->mbrp_ehd; 1096 tsector = MBR_PSECT(partp->mbrp_esect); 1097 decimal("ending cylinder", &tcylinder); 1098 decimal("ending head", &thead); 1099 decimal("ending sector", &tsector); 1100 partp->mbrp_ecyl = DOSCYL(tcylinder); 1101 partp->mbrp_ehd = thead; 1102 partp->mbrp_esect = DOSSECT(tsector, tcylinder); 1103 } else { 1104 1105 if (partp->mbrp_typ == 0 1106 && getlong(&partp->mbrp_start) == 0 1107 && getlong(&partp->mbrp_size) == 0) 1108 memset(partp, 0, sizeof *partp); 1109 else { 1110 #if 0 1111 checkcyl(getlong(&partp->mbrp_start) 1112 / dos_cylindersectors); 1113 #endif 1114 dos(getlong(&partp->mbrp_start), &partp->mbrp_scyl, 1115 &partp->mbrp_shd, &partp->mbrp_ssect); 1116 dos(getlong(&partp->mbrp_start) 1117 + getlong(&partp->mbrp_size) - 1, 1118 &partp->mbrp_ecyl, &partp->mbrp_ehd, 1119 &partp->mbrp_esect); 1120 } 1121 } 1122 1123 print_part(part); 1124 } while (!yesno("Is this entry okay?")); 1125 } 1126 1127 void 1128 print_params(void) 1129 { 1130 1131 if (sh_flag) { 1132 printf ("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%d\n", 1133 cylinders, heads, sectors, disksectors); 1134 printf ("BCYL=%d\nBHEAD=%d\nBSEC=%d\n", 1135 dos_cylinders, dos_heads, dos_sectors); 1136 return; 1137 } 1138 1139 /* Not sh_flag */ 1140 printf("Disk: %s\n", disk); 1141 printf("NetBSD disklabel disk geometry:\n"); 1142 printf("cylinders: %d heads: %d sectors/track: %d (%d sectors/cylinder)\n\n", 1143 cylinders, heads, sectors, cylindersectors); 1144 printf("BIOS disk geometry:\n"); 1145 printf("cylinders: %d heads: %d sectors/track: %d (%d sectors/cylinder)\n\n", 1146 dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors); 1147 } 1148 1149 void 1150 change_active(int which) 1151 { 1152 struct mbr_partition *partp; 1153 int part; 1154 int active = 4; 1155 1156 partp = &mboot.parts[0]; 1157 1158 if (a_flag && which != -1) 1159 active = which; 1160 else { 1161 for (part = 0; part < NMBRPART; part++) 1162 if (partp[part].mbrp_flag & ACTIVE) 1163 active = part; 1164 } 1165 if (!f_flag) { 1166 if (yesno("Do you want to change the active partition?")) { 1167 printf ("Choosing 4 will make no partition active.\n"); 1168 do { 1169 decimal("active partition", &active); 1170 } while (!yesno("Are you happy with this choice?")); 1171 } else 1172 return; 1173 } else 1174 if (active != 4) 1175 printf ("Making partition %d active.\n", active); 1176 1177 for (part = 0; part < NMBRPART; part++) 1178 partp[part].mbrp_flag &= ~ACTIVE; 1179 if (active < 4) 1180 partp[active].mbrp_flag |= ACTIVE; 1181 } 1182 1183 void 1184 get_params_to_use(void) 1185 { 1186 1187 if (b_flag) { 1188 dos_cylinders = b_cyl; 1189 dos_heads = b_head; 1190 dos_sectors = b_sec; 1191 dos_cylindersectors = dos_heads * dos_sectors; 1192 return; 1193 } 1194 1195 print_params(); 1196 if (yesno("Do you want to change our idea of what BIOS thinks?")) { 1197 do { 1198 decimal("BIOS's idea of #cylinders", &dos_cylinders); 1199 decimal("BIOS's idea of #heads", &dos_heads); 1200 decimal("BIOS's idea of #sectors", &dos_sectors); 1201 dos_cylindersectors = dos_heads * dos_sectors; 1202 print_params(); 1203 } while (!yesno("Are you happy with this choice?")); 1204 } 1205 } 1206 1207 /***********************************************\ 1208 * Change real numbers into strange dos numbers * 1209 \***********************************************/ 1210 void 1211 dos(int sector, unsigned char *cylinderp, unsigned char *headp, 1212 unsigned char *sectorp) 1213 { 1214 int cylinder, head; 1215 int biosmaxsec; 1216 1217 biosmaxsec = dos_cylinders * dos_heads * dos_sectors - 1; 1218 if (sector > biosmaxsec) 1219 sector = biosmaxsec; 1220 1221 cylinder = sector / dos_cylindersectors; 1222 1223 sector -= cylinder * dos_cylindersectors; 1224 1225 head = sector / dos_sectors; 1226 sector -= head * dos_sectors; 1227 1228 *cylinderp = DOSCYL(cylinder); 1229 *headp = head; 1230 *sectorp = DOSSECT(sector + 1, cylinder); 1231 } 1232 1233 #if 0 1234 void 1235 checkcyl(int cyl) 1236 { 1237 1238 if (cyl >= MAXCYL) 1239 warnx("partition start beyond BIOS limit"); 1240 } 1241 #endif 1242 1243 int fd = -1; 1244 1245 int 1246 open_disk(int update) 1247 { 1248 static char namebuf[MAXPATHLEN + 1]; 1249 1250 fd = opendisk(disk, update ? O_RDWR : O_RDONLY, namebuf, 1251 sizeof(namebuf), 0); 1252 if (fd < 0) { 1253 if (errno == ENODEV) 1254 warnx("%s is not a character device", namebuf); 1255 else 1256 warn("%s", namebuf); 1257 return (-1); 1258 } 1259 disk = namebuf; 1260 if (get_params() == -1) { 1261 close(fd); 1262 return (-1); 1263 } 1264 return (0); 1265 } 1266 1267 int 1268 read_disk(off_t sector, void *buf) 1269 { 1270 1271 if (fd == -1) 1272 errx(1, "read_disk(); fd == -1"); 1273 if (lseek(fd, sector * 512, 0) == -1) 1274 return (-1); 1275 return (read(fd, buf, 512)); 1276 } 1277 1278 int 1279 write_disk(off_t sector, void *buf) 1280 { 1281 1282 if (fd == -1) 1283 errx(1, "write_disk(); fd == -1"); 1284 if (lseek(fd, sector * 512, 0) == -1) 1285 return (-1); 1286 return (write(fd, buf, 512)); 1287 } 1288 1289 int 1290 get_params(void) 1291 { 1292 1293 if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) { 1294 warn("DIOCGDEFLABEL"); 1295 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) { 1296 warn("DIOCGDINFO"); 1297 return (-1); 1298 } 1299 } 1300 1301 dos_cylinders = cylinders = disklabel.d_ncylinders; 1302 dos_heads = heads = disklabel.d_ntracks; 1303 dos_sectors = sectors = disklabel.d_nsectors; 1304 dos_cylindersectors = cylindersectors = heads * sectors; 1305 disksectors = disklabel.d_secperunit; 1306 1307 return (0); 1308 } 1309 1310 int 1311 read_s0(off_t offset, struct mboot *boot) 1312 { 1313 1314 if (read_disk(offset, boot->bootinst) == -1) { 1315 warn("can't read %s partition table", 1316 offset ? "extended" : "fdisk"); 1317 return (-1); 1318 } 1319 if (getshort(&boot->signature) != MBR_MAGIC) { 1320 warnx("invalid %s partition table found", 1321 offset ? "extended" : "fdisk"); 1322 return (-1); 1323 } 1324 return (0); 1325 } 1326 1327 int 1328 write_s0(void) 1329 { 1330 int flag, i; 1331 1332 /* 1333 * write enable label sector before write (if necessary), 1334 * disable after writing. 1335 * needed if the disklabel protected area also protects 1336 * sector 0. (e.g. empty disk) 1337 */ 1338 flag = 1; 1339 if (ioctl(fd, DIOCWLABEL, &flag) < 0) 1340 warn("DIOCWLABEL"); 1341 if (write_disk(0, mboot.bootinst) == -1) { 1342 warn("can't write fdisk partition table"); 1343 return -1; 1344 } 1345 for (i = bootsize; (i -= 0x200) > 0;) 1346 if (write_disk(i / 0x200, bootcode + i) == -1) { 1347 warn("can't write bootcode"); 1348 return -1; 1349 } 1350 flag = 0; 1351 if (ioctl(fd, DIOCWLABEL, &flag) < 0) 1352 warn("DIOCWLABEL"); 1353 return 0; 1354 } 1355 1356 int 1357 yesno(const char *str) 1358 { 1359 int ch, first; 1360 1361 printf("%s [n] ", str); 1362 1363 first = ch = getchar(); 1364 while (ch != '\n' && ch != EOF) 1365 ch = getchar(); 1366 return (first == 'y' || first == 'Y'); 1367 } 1368 1369 void 1370 decimal(const char *str, int *num) 1371 { 1372 int acc = 0; 1373 char *cp; 1374 1375 for (;; printf("%s is not a valid decimal number.\n", lbuf)) { 1376 printf("%s: [%d] ", str, *num); 1377 1378 fgets(lbuf, LBUF, stdin); 1379 lbuf[strlen(lbuf)-1] = '\0'; 1380 cp = lbuf; 1381 1382 cp += strspn(cp, " \t"); 1383 if (*cp == '\0') 1384 return; 1385 1386 if (!isdigit(*cp) && *cp != '-') 1387 continue; 1388 acc = strtol(lbuf, &cp, 10); 1389 1390 cp += strspn(cp, " \t"); 1391 if (*cp != '\0') 1392 continue; 1393 1394 *num = acc; 1395 return; 1396 } 1397 1398 } 1399 1400 int 1401 type_match(const void *key, const void *item) 1402 { 1403 const int *typep = key; 1404 const struct part_type *ptr = item; 1405 1406 if (*typep < ptr->type) 1407 return (-1); 1408 if (*typep > ptr->type) 1409 return (1); 1410 return (0); 1411 } 1412 1413 const char * 1414 get_type(int type) 1415 { 1416 struct part_type *ptr; 1417 1418 ptr = bsearch(&type, part_types, 1419 sizeof(part_types) / sizeof(struct part_type), 1420 sizeof(struct part_type), type_match); 1421 if (ptr == 0) 1422 return ("unknown"); 1423 return (ptr->name); 1424 } 1425