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