fdisk.c revision 1.60 1 /* $NetBSD: fdisk.c,v 1.60 2003/04/01 19:39:13 fvdl 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 (at) 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.60 2003/04/01 19:39:13 fvdl 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 #if defined(__i386__) || defined(__x86_64__)
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 aligned */
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 #if defined(__i386__) || defined(__x86_64__)
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 "0123BSafilus:b:c:"
108 #else
109 #define OPTIONS "0123Safilus: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 {0xa8, "Apple UFS"},
233 {0xa9, "NetBSD"},
234 {0xab, "Apple Boot"},
235 {0xaf, "Apple HFS"},
236 {0xb1, reserved},
237 {0xb3, reserved},
238 {0xb4, reserved},
239 {0xb6, reserved},
240 {0xB7, "BSDI BSD/386 filesystem"},
241 {0xB8, "BSDI BSD/386 swap"},
242 {0xc0, "CTOS"},
243 {0xC1, "DRDOS/sec (FAT-12)"},
244 {0xC4, "DRDOS/sec (FAT-16, < 32M)"},
245 {0xC6, "DRDOS/sec (FAT-16, >= 32M)"},
246 {0xC7, "Syrinx (Cyrnix?) or HPFS disabled"},
247 {0xd8, "CP/M 86"},
248 {0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"},
249 {0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"},
250 {0xE3, "DOS R/O or SpeedStor or Storage Dimensions"},
251 {0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."},
252 {0xe5, reserved},
253 {0xe6, reserved},
254 {0xeb, "BeOS"},
255 {0xF1, "SpeedStor or Storage Dimensions"},
256 {0xF2, "DOS 3.3+ Secondary"},
257 {0xf3, reserved},
258 {0xF4, "SpeedStor large partition or Storage Dimensions"},
259 {0xf6, reserved},
260 {0xFE, "SpeedStor >1024 cyl. or LANstep or IBM PS/2 IML"},
261 {0xFF, "Xenix Bad Block Table"},
262 };
263
264 #define KNOWN_SYSIDS (sizeof(part_types)/sizeof(part_types[0]))
265
266 void usage(void);
267 void print_s0(int);
268 void print_part(int);
269 void print_mbr_partition(struct mbr_partition *, off_t, off_t, int);
270 int read_boot(const char *, void *, size_t);
271 void init_sector0(int, int);
272 void intuit_translated_geometry(void);
273 void get_geometry(void);
274 void get_diskname(const char *, char *, size_t);
275 int try_heads(quad_t, quad_t, quad_t, quad_t, quad_t, quad_t, quad_t,
276 quad_t);
277 int try_sectors(quad_t, quad_t, quad_t, quad_t, quad_t);
278 void change_part(int, int, int, int);
279 void print_params(void);
280 void change_active(int);
281 void get_params_to_use(void);
282 void dos(int, unsigned char *, unsigned char *, unsigned char *);
283 int open_disk(int);
284 int read_disk(off_t, void *);
285 int write_disk(off_t, void *);
286 int get_params(void);
287 int read_s0(off_t, struct mboot *);
288 int write_s0(void);
289 int yesno(const char *);
290 void decimal(const char *, int *);
291 int type_match(const void *, const void *);
292 const char *get_type(int);
293 int get_mapping(int, int *, int *, int *, unsigned long *);
294 #if defined(__i386__) || defined(__x86_64__)
295 void configure_bootsel(void);
296 #endif
297
298 static unsigned short getshort(void *);
299 static void putshort(void *p, unsigned short);
300 static unsigned long getlong(void *);
301 static void putlong(void *, unsigned long);
302
303
304 int main(int, char *[]);
305
306 int
307 main(int argc, char *argv[])
308 {
309 int ch, part, mib[2];
310 size_t len;
311 char *root_device;
312
313 int csysid, cstart, csize; /* For the b_flag. */
314
315 mib[0] = CTL_KERN;
316 mib[1] = KERN_ROOT_DEVICE;
317 if (sysctl(mib, 2, NULL, &len, NULL, 0) != -1 &&
318 (root_device = malloc(len)) != NULL &&
319 sysctl(mib, 2, root_device, &len, NULL, 0) != -1)
320 disk = root_device;
321
322 a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
323 csysid = cstart = csize = 0;
324 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
325 switch (ch) {
326 case '0':
327 partition = 0;
328 break;
329 case '1':
330 partition = 1;
331 break;
332 case '2':
333 partition = 2;
334 break;
335 case '3':
336 partition = 3;
337 break;
338 #if defined(__i386__) || defined(__x86_64__)
339 case 'B':
340 B_flag = 1;
341 break;
342 #endif
343 case 'S':
344 sh_flag = 1;
345 break;
346 case 'a':
347 a_flag = 1;
348 break;
349 case 'f':
350 f_flag = 1;
351 break;
352 case 'i':
353 i_flag = 1;
354 break;
355 case 'l':
356 for (len = 0; len < KNOWN_SYSIDS; len++)
357 printf("%03d %s\n", part_types[len].type,
358 part_types[len].name);
359 return 0;
360 case 'u':
361 u_flag = 1;
362 break;
363 case 's':
364 s_flag = 1;
365 if (sscanf(optarg, "%d/%d/%d", &csysid, &cstart,
366 &csize) != 3)
367 errx(1, "Bad argument to the -s flag.");
368 break;
369 case 'b':
370 b_flag = 1;
371 if (sscanf(optarg, "%d/%d/%d", &b_cyl, &b_head,
372 &b_sec) != 3)
373 errx(1, "Bad argument to the -b flag.");
374 if (b_cyl > MAXCYL)
375 b_cyl = MAXCYL;
376 break;
377 case 'c':
378 bootsize = read_boot(optarg, bootcode, sizeof bootcode);
379 break;
380 default:
381 usage();
382 }
383 argc -= optind;
384 argv += optind;
385
386 if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
387 usage();
388
389 if (B_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
390 usage();
391
392 if (partition == -1 && s_flag) {
393 warnx("-s flag requires a partition selected.");
394 usage();
395 }
396
397 if (argc > 0)
398 disk = argv[0];
399
400 if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0)
401 exit(1);
402
403 if (read_s0(0, &mboot))
404 init_sector0(sectors > 63 ? 63 : sectors, 1);
405
406 #if defined(__i386__) || defined(__x86_64__)
407 get_geometry();
408 #else
409 intuit_translated_geometry();
410 #endif
411
412
413 if ((i_flag || u_flag) && (!f_flag || b_flag))
414 get_params_to_use();
415
416 if (i_flag)
417 init_sector0(dos_sectors > 63 ? 63 : dos_sectors, 0);
418
419 /* Do the update stuff! */
420 if (u_flag) {
421 if (!f_flag)
422 printf("Partition table:\n");
423 if (partition == -1)
424 for (part = 0; part < NMBRPART; part++)
425 change_part(part,-1, -1, -1);
426 else
427 change_part(partition, csysid, cstart, csize);
428 } else
429 if (!i_flag)
430 print_s0(partition);
431
432 if (a_flag)
433 change_active(partition);
434
435 #if defined(__i386__) || defined(__x86_64__)
436 if (B_flag) {
437 configure_bootsel();
438 if (B_flag && bootsel_modified)
439 write_s0();
440 }
441 #endif
442
443 if (u_flag || a_flag || i_flag) {
444 if (!f_flag) {
445 printf("\nWe haven't written the MBR back to disk "
446 "yet. This is your last chance.\n");
447 print_s0(-1);
448 if (yesno("Should we write new partition table?"))
449 write_s0();
450 } else
451 write_s0();
452 }
453
454 exit(0);
455 }
456
457 void
458 usage(void)
459 {
460
461 (void)fprintf(stderr, "Usage: %s [-aiufBS] [-0|-1|-2|-3] "
462 "[-b cylinders/heads/sectors]\n"
463 "\t%s [-s id/start/size] [-c bootcode] [device]\n",
464 getprogname(), getprogname());
465 exit(1);
466 }
467
468 void
469 print_s0(int which)
470 {
471 int part;
472
473 print_params();
474 if (!sh_flag)
475 printf("Partition table:\n");
476 if (which == -1) {
477 for (part = 0; part < NMBRPART; part++) {
478 if (!sh_flag)
479 printf("%d: ", part);
480 print_part(part);
481 }
482 } else
483 print_part(which);
484 }
485
486 static unsigned short
487 getshort(void *p)
488 {
489 unsigned char *cp = p;
490
491 return cp[0] | (cp[1] << 8);
492 }
493
494 static void
495 putshort(void *p, unsigned short l)
496 {
497 unsigned char *cp = p;
498
499 *cp++ = l;
500 *cp++ = l >> 8;
501 }
502
503 static unsigned long
504 getlong(void *p)
505 {
506 unsigned char *cp = p;
507
508 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
509 }
510
511 static void
512 putlong(void *p, unsigned long l)
513 {
514 unsigned char *cp = p;
515
516 *cp++ = l;
517 *cp++ = l >> 8;
518 *cp++ = l >> 16;
519 *cp++ = l >> 24;
520 }
521
522 void
523 print_part(int part)
524 {
525 struct mbr_partition *partp;
526 int empty;
527
528 partp = &mboot.parts[part];
529 empty = (partp->mbrp_typ == 0);
530
531 if (sh_flag) {
532 if (empty) {
533 printf("PART%dSIZE=0\n", part);
534 return;
535 }
536
537 printf("PART%dID=%d\n", part, partp->mbrp_typ);
538 printf("PART%dSIZE=%ld\n", part, getlong(&partp->mbrp_size));
539 printf("PART%dSTART=%ld\n", part, getlong(&partp->mbrp_start));
540 printf("PART%dFLAG=0x%x\n", part, partp->mbrp_flag);
541 printf("PART%dBCYL=%d\n", part, MBR_PCYL(partp->mbrp_scyl,
542 partp->mbrp_ssect));
543 printf("PART%dBHEAD=%d\n", part, partp->mbrp_shd);
544 printf("PART%dBSEC=%d\n", part, MBR_PSECT(partp->mbrp_ssect));
545 printf("PART%dECYL=%d\n", part, MBR_PCYL(partp->mbrp_ecyl,
546 partp->mbrp_esect));
547 printf("PART%dEHEAD=%d\n", part, partp->mbrp_ehd);
548 printf("PART%dESEC=%d\n", part, MBR_PSECT(partp->mbrp_esect));
549 return;
550 }
551 print_mbr_partition(partp, 0, 0, 0);
552 }
553
554 void
555 print_mbr_partition(struct mbr_partition *partp,
556 off_t offset, off_t exoffset, int indent)
557 {
558 int empty;
559 off_t start;
560
561 empty = (partp->mbrp_typ == 0);
562 if (MBR_IS_EXTENDED(partp->mbrp_typ))
563 start = (off_t)getlong(&partp->mbrp_start) + exoffset;
564 else
565 start = (off_t)getlong(&partp->mbrp_start) + offset;
566 if (empty) {
567 printf("<UNUSED>\n");
568 return;
569 }
570 printf("sysid %d (%s)\n",
571 partp->mbrp_typ, get_type(partp->mbrp_typ));
572 printf("%*s start %lld, size %ld (%ld MB), flag 0x%x\n",
573 indent, "",
574 (long long)start, getlong(&partp->mbrp_size),
575 getlong(&partp->mbrp_size) * 512 / (1024 * 1024), partp->mbrp_flag);
576 printf("%*s beg: cylinder %4d, head %3d, sector %2d\n",
577 indent, "",
578 MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect),
579 partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect));
580 printf("%*s end: cylinder %4d, head %3d, sector %2d\n",
581 indent, "",
582 MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect),
583 partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect));
584
585 if (MBR_IS_EXTENDED(partp->mbrp_typ)) {
586 struct mboot eboot;
587 int part;
588
589 printf("%*s Extended partition table:\n", indent, "");
590 if (read_s0(start, &eboot) == -1)
591 return;
592 indent += 8;
593 if (exoffset == 0)
594 exoffset = start;
595 for (part = 0; part < NMBRPART; part++) {
596 printf("%*s%d: ", indent, "", part);
597 print_mbr_partition(&eboot.parts[part],
598 start, exoffset, indent);
599 }
600 }
601 }
602
603 int
604 read_boot(const char *name, void *buf, size_t len)
605 {
606 int bfd, ret;
607 struct stat st;
608
609 if ((bfd = open(name, O_RDONLY)) < 0)
610 err(1, "%s", name);
611 if (fstat(bfd, &st) == -1)
612 err(1, "%s", name);
613 if (st.st_size > (off_t)len)
614 errx(1, "%s: bootcode too large", name);
615 ret = st.st_size;
616 if (ret < 0x200)
617 errx(1, "%s: bootcode too small", name);
618 if (read(bfd, buf, len) != ret)
619 err(1, "%s", name);
620 close(bfd);
621
622 /*
623 * Do some sanity checking here
624 */
625 if (getshort(bootcode + MBR_MAGICOFF) != MBR_MAGIC)
626 errx(1, "%s: invalid magic", name);
627 ret = (ret + 0x1ff) / 0x200;
628 ret *= 0x200;
629 return ret;
630 }
631
632 void
633 init_sector0(int start, int dopart)
634 {
635 int i;
636
637 #ifdef DEFAULT_BOOTCODE
638 if (!bootsize)
639 bootsize = read_boot(DEFAULT_BOOTCODE, bootcode,
640 sizeof bootcode);
641 #endif
642
643 memcpy(mboot.bootinst, bootcode, sizeof(mboot.bootinst));
644 putshort(&mboot.signature, MBR_MAGIC);
645
646 if (dopart)
647 for (i = 0; i < 4; i++)
648 memset(&mboot.parts[i], 0, sizeof(mboot.parts[i]));
649
650 }
651
652 #if defined(__i386__) || defined(__x86_64__)
653
654 void
655 get_diskname(const char *fullname, char *diskname, size_t size)
656 {
657 const char *p, *p2;
658 size_t len;
659
660 p = strrchr(fullname, '/');
661 if (p == NULL)
662 p = fullname;
663 else
664 p++;
665
666 if (*p == 0) {
667 strncpy(diskname, fullname, size - 1);
668 diskname[size - 1] = '\0';
669 return;
670 }
671
672 if (*p == 'r')
673 p++;
674
675 for (p2 = p; *p2 != 0; p2++)
676 if (isdigit(*p2))
677 break;
678 if (*p2 == 0) {
679 /* XXX invalid diskname? */
680 strncpy(diskname, fullname, size - 1);
681 diskname[size - 1] = '\0';
682 return;
683 }
684 while (isdigit(*p2))
685 p2++;
686
687 len = p2 - p;
688 if (len > size) {
689 /* XXX */
690 strncpy(diskname, fullname, size - 1);
691 diskname[size - 1] = '\0';
692 return;
693 }
694
695 strncpy(diskname, p, len);
696 diskname[len] = 0;
697 }
698
699 void
700 get_geometry(void)
701 {
702 int mib[2], i;
703 size_t len;
704 struct disklist *dl;
705 struct biosdisk_info *bip;
706 struct nativedisk_info *nip;
707 char diskname[8];
708
709 mib[0] = CTL_MACHDEP;
710 mib[1] = CPU_DISKINFO;
711 if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
712 intuit_translated_geometry();
713 return;
714 }
715 dl = (struct disklist *) malloc(len);
716 sysctl(mib, 2, dl, &len, NULL, 0);
717
718 get_diskname(disk, diskname, sizeof diskname);
719
720 for (i = 0; i < dl->dl_nnativedisks; i++) {
721 nip = &dl->dl_nativedisks[i];
722 if (strcmp(diskname, nip->ni_devname))
723 continue;
724 /*
725 * XXX listing possible matches is better. This is ok
726 * for now because the user has a chance to change
727 * it later.
728 */
729 if (nip->ni_nmatches != 0) {
730 bip = &dl->dl_biosdisks[nip->ni_biosmatches[0]];
731 dos_cylinders = bip->bi_cyl;
732 dos_heads = bip->bi_head;
733 dos_sectors = bip->bi_sec;
734 dos_cylindersectors = bip->bi_head * bip->bi_sec;
735 return;
736 }
737 }
738 /* Allright, allright, make a stupid guess.. */
739 intuit_translated_geometry();
740 }
741
742 void
743 configure_bootsel(void)
744 {
745 struct mbr_bootsel *mbs =
746 (struct mbr_bootsel *)&mboot.bootinst[MBR_BOOTSELOFF];
747 int i, nused, firstpart = -1, item;
748 char desc[PARTNAMESIZE + 2], *p;
749 int timo, entry_changed = 0;
750
751 for (i = nused = 0; i < NMBRPART; ++i) {
752 if (mboot.parts[i].mbrp_typ != 0) {
753 if (firstpart == -1)
754 firstpart = i;
755 nused++;
756 }
757 }
758
759 if (nused == 0) {
760 warnx("No used partitions found. Partition the disk first.");
761 return;
762 }
763
764 if (mbs->magic != MBR_MAGIC) {
765 if (!yesno("Bootselector not yet installed. Install it now?")) {
766 warnx("Bootselector not installed.");
767 return;
768 }
769 bootsize = read_boot(DEFAULT_BOOTSELCODE, bootcode,
770 sizeof bootcode);
771 memcpy(mboot.bootinst, bootcode, sizeof(mboot.bootinst));
772 bootsel_modified = 1;
773 mbs->flags |= BFL_SELACTIVE;
774 } else {
775 if (mbs->flags & BFL_SELACTIVE) {
776 printf("The bootselector is installed and active.\n");
777 if (!yesno("Do you want to change its settings?")) {
778 if (yesno("Do you want to deactivate it?")) {
779 mbs->flags &= ~BFL_SELACTIVE;
780 bootsel_modified = 1;
781 goto done;
782 }
783 return;
784 }
785 } else {
786 printf("The bootselector is installed but not active.\n");
787 if (yesno("Do you want to activate it?")) {
788 mbs->flags |= BFL_SELACTIVE;
789 bootsel_modified = 1;
790 }
791 if (!yesno("Do you want to change its settings?"))
792 goto done;
793 }
794 }
795
796 printf("\n\nPartition table:\n");
797 for (i = 0; i < NMBRPART; i++) {
798 printf("%d: ", i);
799 print_part(i);
800 }
801
802 printf("\n\nCurrent boot selection menu option names:\n");
803 for (i = 0; i < NMBRPART; i++) {
804 if (mbs->nametab[i][0] != 0)
805 printf("%d: %s\n", i, &mbs->nametab[i][0]);
806 else
807 printf("%d: <UNUSED>\n", i);
808 }
809 printf("\n");
810
811 item = firstpart;
812
813 editentries:
814 while (1) {
815 decimal("Change which entry (-1 quits)?", &item);
816 if (item == -1)
817 break;
818 if (item < 0 || item >= NMBRPART) {
819 printf("Invalid entry number\n");
820 item = -1;
821 continue;
822 }
823 if (mboot.parts[item].mbrp_typ == 0) {
824 printf("The partition entry is unused\n");
825 item = -1;
826 continue;
827 }
828
829 printf("Enter descriptions (max. 8 characters): ");
830 rewind(stdin);
831 if (!fgets(desc, PARTNAMESIZE + 1, stdin))
832 errx(1, "EOF");
833 fpurge(stdin);
834 p = strchr(desc, '\n');
835 if (p != NULL)
836 *p = 0;
837 strcpy(&mbs->nametab[item][0], desc);
838 entry_changed = bootsel_modified = 1;
839
840 if (item < NMBRPART -1)
841 item++;
842 else
843 item = -1;
844 }
845
846 if (entry_changed)
847 printf("Boot selection menu option names are now:\n");
848
849 firstpart = -1;
850 for (i = 0; i < NMBRPART; i++) {
851 if (mbs->nametab[i][0] != 0) {
852 firstpart = i;
853 if (entry_changed)
854 printf("%d: %s\n", i, &mbs->nametab[i][0]);
855 } else {
856 if (entry_changed)
857 printf("%d: <UNUSED>\n", i);
858 }
859 }
860 if (entry_changed)
861 printf("\n");
862
863 if (firstpart == -1) {
864 printf("All menu entries are now inactive.\n");
865 if (!yesno("Are you sure about this?"))
866 goto editentries;
867 } else {
868 if (!(mbs->flags & BFL_SELACTIVE)) {
869 printf("The bootselector is not yet active.\n");
870 if (yesno("Activate it now?"))
871 mbs->flags |= BFL_SELACTIVE;
872 }
873 }
874
875 /* bootsel is dirty from here on out. */
876 bootsel_modified = 1;
877
878 /* The timeout value is in ticks, 18.2 Hz. Avoid using floats. */
879 timo = ((1000 * mbs->timeo) / 18200);
880 do {
881 decimal("Timeout value", &timo);
882 } while (timo < 0 || timo > 3600);
883 mbs->timeo = (u_int16_t)((timo * 18200) / 1000);
884
885 printf("Select the default boot option. Options are:\n\n");
886 for (i = 0; i < NMBRPART; i++) {
887 if (mbs->nametab[i][0] != 0)
888 printf("%d: %s\n", i, &mbs->nametab[i][0]);
889 }
890 for (i = 4; i < 10; i++)
891 printf("%d: Harddisk %d\n", i, i - 4);
892 printf("10: The first active partition\n");
893
894 if (mbs->defkey == SCAN_ENTER)
895 item = 10;
896 else
897 item = mbs->defkey - SCAN_F1;
898
899 if (item < 0 || item > 10 ||
900 (item < NMBRPART && mbs->nametab[item][0] == 0))
901 item = 10;
902
903 do {
904 decimal("Default boot option", &item);
905 } while (item < 0 || item > 10 ||
906 (item < NMBRPART && mbs->nametab[item][0] == 0));
907
908 if (item == 10)
909 mbs->defkey = SCAN_ENTER;
910 else
911 mbs->defkey = SCAN_F1 + item;
912
913 done:
914 for (i = 0; i < NMBRPART; i++) {
915 if (mboot.parts[i].mbrp_typ != 0 &&
916 mboot.parts[i].mbrp_start >=
917 (unsigned)(dos_cylinders * dos_heads * dos_sectors)) {
918 mbs->flags |= BFL_EXTINT13;
919 break;
920 }
921 }
922
923 if (bootsel_modified != 0 && !yesno("Update the bootselector?"))
924 bootsel_modified = 0;
925 }
926 #endif
927
928
929 /* Prerequisite: the disklabel parameters and master boot record must
930 * have been read (i.e. dos_* and mboot are meaningful).
931 * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
932 * dos_cylindersectors to be consistent with what the
933 * partition table is using, if we can find a geometry
934 * which is consistent with all partition table entries.
935 * We may get the number of cylinders slightly wrong (in
936 * the conservative direction). The idea is to be able
937 * to create a NetBSD partition on a disk we don't know
938 * the translated geometry of.
939 * This whole routine should be replaced with a kernel interface to get
940 * the BIOS geometry (which in turn requires modifications to the i386
941 * boot loader to pass in the BIOS geometry for each disk). */
942 void
943 intuit_translated_geometry(void)
944 {
945
946 int xcylinders = -1, xheads = -1, xsectors = -1, i, j;
947 int c1, h1, s1, c2, h2, s2;
948 long a1, a2;
949 quad_t num, denom;
950
951 /* Try to deduce the number of heads from two different mappings. */
952 for (i = 0; i < NMBRPART * 2; i++) {
953 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
954 continue;
955 for (j = 0; j < 8; j++) {
956 if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
957 continue;
958 num = (quad_t)h1*(a2-s2) - (quad_t)h2*(a1-s1);
959 denom = (quad_t)c2*(a1-s1) - (quad_t)c1*(a2-s2);
960 if (denom != 0 && num % denom == 0) {
961 xheads = num / denom;
962 break;
963 }
964 }
965 if (xheads != -1)
966 break;
967 }
968
969 if (xheads == -1)
970 return;
971
972 /* Now figure out the number of sectors from a single mapping. */
973 for (i = 0; i < NMBRPART * 2; i++) {
974 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
975 continue;
976 num = a1 - s1;
977 denom = c1 * xheads + h1;
978 if (denom != 0 && num % denom == 0) {
979 xsectors = num / denom;
980 break;
981 }
982 }
983
984 if (xsectors == -1)
985 return;
986
987 /* Estimate the number of cylinders. */
988 xcylinders = disklabel.d_secperunit / xheads / xsectors;
989
990 /* Now verify consistency with each of the partition table entries.
991 * Be willing to shove cylinders up a little bit to make things work,
992 * but translation mismatches are fatal. */
993 for (i = 0; i < NMBRPART * 2; i++) {
994 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
995 continue;
996 if (xsectors * (c1 * xheads + h1) + s1 != a1)
997 return;
998 if (c1 >= xcylinders)
999 xcylinders = c1 + 1;
1000 }
1001
1002 /* Everything checks out. Reset the geometry to use for further
1003 * calculations. */
1004 dos_cylinders = xcylinders;
1005 dos_heads = xheads;
1006 dos_sectors = xsectors;
1007 dos_cylindersectors = xheads * xsectors;
1008 }
1009
1010 /* For the purposes of intuit_translated_geometry(), treat the partition
1011 * table as a list of eight mapping between (cylinder, head, sector)
1012 * triplets and absolute sectors. Get the relevant geometry triplet and
1013 * absolute sectors for a given entry, or return -1 if it isn't present.
1014 * Note: for simplicity, the returned sector is 0-based. */
1015 int
1016 get_mapping(int i, int *cylinder, int *head, int *sector,
1017 unsigned long *absolute)
1018 {
1019 struct mbr_partition *part = &mboot.parts[i / 2];
1020
1021 if (part->mbrp_typ == 0)
1022 return -1;
1023 if (i % 2 == 0) {
1024 *cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
1025 *head = part->mbrp_shd;
1026 *sector = MBR_PSECT(part->mbrp_ssect) - 1;
1027 *absolute = getlong(&part->mbrp_start);
1028 } else {
1029 *cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
1030 *head = part->mbrp_ehd;
1031 *sector = MBR_PSECT(part->mbrp_esect) - 1;
1032 *absolute = getlong(&part->mbrp_start)
1033 + getlong(&part->mbrp_size) - 1;
1034 }
1035 return 0;
1036 }
1037
1038 void
1039 change_part(int part, int csysid, int cstart, int csize)
1040 {
1041 struct mbr_partition *partp;
1042
1043 partp = &mboot.parts[part];
1044
1045 if (s_flag) {
1046 if (csysid == 0 && cstart == 0 && csize == 0)
1047 memset(partp, 0, sizeof *partp);
1048 else {
1049 partp->mbrp_typ = csysid;
1050 #if 0
1051 checkcyl(cstart / dos_cylindersectors);
1052 #endif
1053 putlong(&partp->mbrp_start, cstart);
1054 putlong(&partp->mbrp_size, csize);
1055 dos(getlong(&partp->mbrp_start),
1056 &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
1057 dos(getlong(&partp->mbrp_start)
1058 + getlong(&partp->mbrp_size) - 1,
1059 &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
1060 }
1061 if (f_flag)
1062 return;
1063 }
1064
1065 printf("The data for partition %d is:\n", part);
1066 print_part(part);
1067 if (!u_flag || !yesno("Do you want to change it?"))
1068 return;
1069
1070 do {
1071 {
1072 int sysid, start, size;
1073
1074 sysid = partp->mbrp_typ,
1075 start = getlong(&partp->mbrp_start),
1076 size = getlong(&partp->mbrp_size);
1077 decimal("sysid", &sysid);
1078 decimal("start", &start);
1079 decimal("size", &size);
1080 partp->mbrp_typ = sysid;
1081 putlong(&partp->mbrp_start, start);
1082 putlong(&partp->mbrp_size, size);
1083 }
1084
1085 if (yesno("Explicitly specify beg/end address?")) {
1086 int tsector, tcylinder, thead;
1087
1088 tcylinder = MBR_PCYL(partp->mbrp_scyl,
1089 partp->mbrp_ssect);
1090 thead = partp->mbrp_shd;
1091 tsector = MBR_PSECT(partp->mbrp_ssect);
1092 decimal("beginning cylinder", &tcylinder);
1093 #if 0
1094 checkcyl(tcylinder);
1095 #endif
1096 decimal("beginning head", &thead);
1097 decimal("beginning sector", &tsector);
1098 partp->mbrp_scyl = DOSCYL(tcylinder);
1099 partp->mbrp_shd = thead;
1100 partp->mbrp_ssect = DOSSECT(tsector, tcylinder);
1101
1102 tcylinder = MBR_PCYL(partp->mbrp_ecyl,
1103 partp->mbrp_esect);
1104 thead = partp->mbrp_ehd;
1105 tsector = MBR_PSECT(partp->mbrp_esect);
1106 decimal("ending cylinder", &tcylinder);
1107 decimal("ending head", &thead);
1108 decimal("ending sector", &tsector);
1109 partp->mbrp_ecyl = DOSCYL(tcylinder);
1110 partp->mbrp_ehd = thead;
1111 partp->mbrp_esect = DOSSECT(tsector, tcylinder);
1112 } else {
1113
1114 if (partp->mbrp_typ == 0
1115 && getlong(&partp->mbrp_start) == 0
1116 && getlong(&partp->mbrp_size) == 0)
1117 memset(partp, 0, sizeof *partp);
1118 else {
1119 #if 0
1120 checkcyl(getlong(&partp->mbrp_start)
1121 / dos_cylindersectors);
1122 #endif
1123 dos(getlong(&partp->mbrp_start),
1124 &partp->mbrp_scyl,
1125 &partp->mbrp_shd, &partp->mbrp_ssect);
1126 dos(getlong(&partp->mbrp_start)
1127 + getlong(&partp->mbrp_size) - 1,
1128 &partp->mbrp_ecyl, &partp->mbrp_ehd,
1129 &partp->mbrp_esect);
1130 }
1131 }
1132
1133 print_part(part);
1134 } while (!yesno("Is this entry okay?"));
1135 }
1136
1137 void
1138 print_params(void)
1139 {
1140
1141 if (sh_flag) {
1142 printf ("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%d\n",
1143 cylinders, heads, sectors, disksectors);
1144 printf ("BCYL=%d\nBHEAD=%d\nBSEC=%d\n",
1145 dos_cylinders, dos_heads, dos_sectors);
1146 return;
1147 }
1148
1149 /* Not sh_flag */
1150 printf("Disk: %s\n", disk);
1151 printf("NetBSD disklabel disk geometry:\n");
1152 printf("cylinders: %d heads: %d sectors/track: %d (%d sectors/cylinder)\n\n",
1153 cylinders, heads, sectors, cylindersectors);
1154 printf("BIOS disk geometry:\n");
1155 printf("cylinders: %d heads: %d sectors/track: %d (%d sectors/cylinder)\n\n",
1156 dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors);
1157 }
1158
1159 void
1160 change_active(int which)
1161 {
1162 struct mbr_partition *partp;
1163 int part;
1164 int active = 4;
1165
1166 partp = &mboot.parts[0];
1167
1168 if (a_flag && which != -1)
1169 active = which;
1170 else {
1171 for (part = 0; part < NMBRPART; part++)
1172 if (partp[part].mbrp_flag & ACTIVE)
1173 active = part;
1174 }
1175 if (!f_flag) {
1176 if (yesno("Do you want to change the active partition?")) {
1177 printf ("Choosing 4 will make no partition active.\n");
1178 do {
1179 decimal("active partition", &active);
1180 } while (!yesno("Are you happy with this choice?"));
1181 } else
1182 return;
1183 } else
1184 if (active != 4)
1185 printf ("Making partition %d active.\n", active);
1186
1187 for (part = 0; part < NMBRPART; part++)
1188 partp[part].mbrp_flag &= ~ACTIVE;
1189 if (active < 4)
1190 partp[active].mbrp_flag |= ACTIVE;
1191 }
1192
1193 void
1194 get_params_to_use(void)
1195 {
1196
1197 if (b_flag) {
1198 dos_cylinders = b_cyl;
1199 dos_heads = b_head;
1200 dos_sectors = b_sec;
1201 dos_cylindersectors = dos_heads * dos_sectors;
1202 return;
1203 }
1204
1205 print_params();
1206 if (yesno("Do you want to change our idea of what BIOS thinks?")) {
1207 do {
1208 decimal("BIOS's idea of #cylinders", &dos_cylinders);
1209 decimal("BIOS's idea of #heads", &dos_heads);
1210 decimal("BIOS's idea of #sectors", &dos_sectors);
1211 dos_cylindersectors = dos_heads * dos_sectors;
1212 print_params();
1213 } while (!yesno("Are you happy with this choice?"));
1214 }
1215 }
1216
1217 /***********************************************\
1218 * Change real numbers into strange dos numbers *
1219 \***********************************************/
1220 void
1221 dos(int sector, unsigned char *cylinderp, unsigned char *headp,
1222 unsigned char *sectorp)
1223 {
1224 int cylinder, head;
1225 int biosmaxsec;
1226
1227 biosmaxsec = dos_cylinders * dos_heads * dos_sectors - 1;
1228 if (sector > biosmaxsec)
1229 sector = biosmaxsec;
1230
1231 cylinder = sector / dos_cylindersectors;
1232
1233 sector -= cylinder * dos_cylindersectors;
1234
1235 head = sector / dos_sectors;
1236 sector -= head * dos_sectors;
1237
1238 *cylinderp = DOSCYL(cylinder);
1239 *headp = head;
1240 *sectorp = DOSSECT(sector + 1, cylinder);
1241 }
1242
1243 #if 0
1244 void
1245 checkcyl(int cyl)
1246 {
1247
1248 if (cyl >= MAXCYL)
1249 warnx("partition start beyond BIOS limit");
1250 }
1251 #endif
1252
1253 int fd = -1;
1254
1255 int
1256 open_disk(int update)
1257 {
1258 static char namebuf[MAXPATHLEN + 1];
1259
1260 fd = opendisk(disk, update ? O_RDWR : O_RDONLY, namebuf,
1261 sizeof(namebuf), 0);
1262 if (fd < 0) {
1263 if (errno == ENODEV)
1264 warnx("%s is not a character device", namebuf);
1265 else
1266 warn("%s", namebuf);
1267 return (-1);
1268 }
1269 disk = namebuf;
1270 if (get_params() == -1) {
1271 close(fd);
1272 return (-1);
1273 }
1274 return (0);
1275 }
1276
1277 int
1278 read_disk(off_t sector, void *buf)
1279 {
1280
1281 if (fd == -1)
1282 errx(1, "read_disk(); fd == -1");
1283 if (lseek(fd, sector * 512, 0) == -1)
1284 return (-1);
1285 return (read(fd, buf, 512));
1286 }
1287
1288 int
1289 write_disk(off_t sector, void *buf)
1290 {
1291
1292 if (fd == -1)
1293 errx(1, "write_disk(); fd == -1");
1294 if (lseek(fd, sector * 512, 0) == -1)
1295 return (-1);
1296 return (write(fd, buf, 512));
1297 }
1298
1299 int
1300 get_params(void)
1301 {
1302
1303 if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) {
1304 warn("DIOCGDEFLABEL");
1305 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
1306 warn("DIOCGDINFO");
1307 return (-1);
1308 }
1309 }
1310
1311 dos_cylinders = cylinders = disklabel.d_ncylinders;
1312 dos_heads = heads = disklabel.d_ntracks;
1313 dos_sectors = sectors = disklabel.d_nsectors;
1314 dos_cylindersectors = cylindersectors = heads * sectors;
1315 disksectors = disklabel.d_secperunit;
1316
1317 return (0);
1318 }
1319
1320 int
1321 read_s0(off_t offset, struct mboot *boot)
1322 {
1323
1324 if (read_disk(offset, boot->bootinst) == -1) {
1325 warn("can't read %s partition table",
1326 offset ? "extended" : "fdisk");
1327 return (-1);
1328 }
1329 if (getshort(&boot->signature) != MBR_MAGIC) {
1330 warnx("invalid %s partition table found",
1331 offset ? "extended" : "fdisk");
1332 return (-1);
1333 }
1334 return (0);
1335 }
1336
1337 int
1338 write_s0(void)
1339 {
1340 int flag, i;
1341
1342 /*
1343 * write enable label sector before write (if necessary),
1344 * disable after writing.
1345 * needed if the disklabel protected area also protects
1346 * sector 0. (e.g. empty disk)
1347 */
1348 flag = 1;
1349 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
1350 warn("DIOCWLABEL");
1351 if (write_disk(0, mboot.bootinst) == -1) {
1352 warn("can't write fdisk partition table");
1353 return -1;
1354 }
1355 for (i = bootsize; (i -= 0x200) > 0;)
1356 if (write_disk(i / 0x200, bootcode + i) == -1) {
1357 warn("can't write bootcode");
1358 return -1;
1359 }
1360 flag = 0;
1361 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
1362 warn("DIOCWLABEL");
1363 return 0;
1364 }
1365
1366 int
1367 yesno(const char *str)
1368 {
1369 int ch, first;
1370
1371 printf("%s [n] ", str);
1372
1373 first = ch = getchar();
1374 while (ch != '\n' && ch != EOF)
1375 ch = getchar();
1376 if (ch == EOF)
1377 errx(1, "EOF");
1378 return (first == 'y' || first == 'Y');
1379 }
1380
1381 void
1382 decimal(const char *str, int *num)
1383 {
1384 int acc = 0;
1385 char *cp;
1386
1387 for (;; printf("%s is not a valid decimal number.\n", lbuf)) {
1388 printf("%s: [%d] ", str, *num);
1389
1390 if (!fgets(lbuf, LBUF, stdin))
1391 errx(1, "EOF");
1392 lbuf[strlen(lbuf)-1] = '\0';
1393 cp = lbuf;
1394
1395 cp += strspn(cp, " \t");
1396 if (*cp == '\0')
1397 return;
1398
1399 if (!isdigit(*cp) && *cp != '-')
1400 continue;
1401 acc = strtol(lbuf, &cp, 10);
1402
1403 cp += strspn(cp, " \t");
1404 if (*cp != '\0')
1405 continue;
1406
1407 *num = acc;
1408 return;
1409 }
1410
1411 }
1412
1413 int
1414 type_match(const void *key, const void *item)
1415 {
1416 const int *typep = key;
1417 const struct part_type *ptr = item;
1418
1419 if (*typep < ptr->type)
1420 return (-1);
1421 if (*typep > ptr->type)
1422 return (1);
1423 return (0);
1424 }
1425
1426 const char *
1427 get_type(int type)
1428 {
1429 struct part_type *ptr;
1430
1431 ptr = bsearch(&type, part_types,
1432 sizeof(part_types) / sizeof(struct part_type),
1433 sizeof(struct part_type), type_match);
1434 if (ptr == 0)
1435 return ("unknown");
1436 return (ptr->name);
1437 }
1438