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