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