fdisk.c revision 1.33 1 /* $NetBSD: fdisk.c,v 1.33 1999/02/09 19:11:46 perry 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.33 1999/02/09 19:11:46 perry 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 #define LBUF 100
54 static char lbuf[LBUF];
55
56 /*
57 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
58 * Copyright (c) 1989 Robert. V. Baron
59 * Created.
60 */
61
62 char *disk = "/dev/rwd0d";
63
64 struct disklabel disklabel; /* disk parameters */
65
66 int cylinders, sectors, heads, cylindersectors, disksectors;
67
68 struct mboot {
69 u_int8_t padding[2]; /* force the longs to be long alligned */
70 u_int8_t bootinst[MBR_PARTOFF];
71 struct mbr_partition parts[NMBRPART];
72 u_int16_t signature;
73 };
74 struct mboot mboot;
75
76 #ifdef __i386__
77 #define DEFAULT_BOOTCODE "/usr/mdec/mbr"
78 #endif
79
80 #define ACTIVE 0x80
81
82 int dos_cylinders;
83 int dos_heads;
84 int dos_sectors;
85 int dos_cylindersectors;
86
87 #define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0))
88 #define DOSCYL(c) ((c) & 0xff)
89
90 #define MAXCYL 1024
91 int partition = -1;
92
93 int a_flag; /* set active partition */
94 int i_flag; /* init bootcode */
95 int u_flag; /* update partition data */
96 int sh_flag; /* Output data as shell defines */
97 int f_flag; /* force --not interactive */
98 int s_flag; /* set id,offset,size */
99 int b_flag; /* Set cyl, heads, secs (as c/h/s) */
100 int b_cyl, b_head, b_sec; /* b_flag values. */
101
102 unsigned char bootcode[8192]; /* maximum size of bootcode */
103 int bootsize; /* actual size of bootcode */
104
105
106 static char reserved[] = "reserved";
108
109 struct part_type {
110 int type;
111 char *name;
112 } part_types[] = {
113 {0x00, "unused"},
114 {0x01, "Primary DOS with 12 bit FAT"},
115 {0x02, "XENIX / filesystem"},
116 {0x03, "XENIX /usr filesystem"},
117 {0x04, "Primary DOS with 16 bit FAT <32M"},
118 {0x05, "Extended partition"},
119 {0x06, "Primary 'big' DOS, 16-bit FAT (> 32MB)"},
120 {0x07, "OS/2 HPFS or NTFS or QNX2 or Advanced UNIX"},
121 {0x08, "AIX filesystem or OS/2 (thru v1.3) or DELL multiple drives"
122 "or Commodore DOS or SplitDrive"},
123 {0x09, "AIX boot partition or Coherent"},
124 {0x0A, "OS/2 Boot Manager or Coherent swap or OPUS"},
125 {0x0b, "Primary DOS with 32 bit FAT"},
126 {0x0c, "Primary DOS with 32 bit FAT - LBA"},
127 {0x0d, "Type 7??? - LBA"},
128 {0x0E, "DOS (16-bit FAT) - LBA"},
129 {0x0F, "Ext. partition - LBA"},
130 {0x10, "OPUS"},
131 {0x11, "OS/2 BM: hidden DOS 12-bit FAT"},
132 {0x12, "Compaq diagnostics"},
133 {0x14, "OS/2 BM: hidden DOS 16-bit FAT <32M or Novell DOS 7.0 bug"},
134 {0x16, "OS/2 BM: hidden DOS 16-bit FAT >=32M"},
135 {0x17, "OS/2 BM: hidden IFS"},
136 {0x18, "AST Windows swapfile"},
137 {0x19, "Willowtech Photon coS"},
138 {0x1e, "hidden FAT95"},
139 {0x20, "Willowsoft OFS1"},
140 {0x21, reserved},
141 {0x23, reserved},
142 {0x24, "NEC DOS"},
143 {0x26, reserved},
144 {0x31, reserved},
145 {0x33, reserved},
146 {0x34, reserved},
147 {0x36, reserved},
148 {0x38, "Theos"},
149 {0x3C, "PartitionMagic recovery"},
150 {0x40, "VENIX 286 or LynxOS"},
151 {0x41, "Linux/MINIX (sharing disk with DRDOS) or Personal RISC boot"},
152 {0x42, "SFS or Linux swap (sharing disk with DRDOS)"},
153 {0x43, "Linux native (sharing disk with DRDOS)"},
154 {0x50, "DM (disk manager)"},
155 {0x51, "DM6 Aux1 (or Novell)"},
156 {0x52, "CP/M or Microport SysV/AT"},
157 {0x53, "DM6 Aux3"},
158 {0x54, "DM6 DDO"},
159 {0x55, "EZ-Drive (disk manager)"},
160 {0x56, "Golden Bow (disk manager)"},
161 {0x5C, "Priam Edisk (disk manager)"},
162 {0x61, "SpeedStor"},
163 {0x63, "GNU HURD or Mach or Sys V/386 (such as ISC UNIX) or MtXinu"},
164 {0x64, "Novell Netware 2.xx or Speedstore"},
165 {0x65, "Novell Netware 3.xx"},
166 {0x66, "Novell 386 Netware"},
167 {0x67, "Novell"},
168 {0x68, "Novell"},
169 {0x69, "Novell"},
170 {0x70, "DiskSecure Multi-Boot"},
171 {0x71, reserved},
172 {0x73, reserved},
173 {0x74, reserved},
174 {0x75, "PC/IX"},
175 {0x76, reserved},
176 {0x77, "QNX4.x"},
177 {0x78, "QNX4.x 2nd part"},
178 {0x79, "QNX4.x 3rd part"},
179 {0x80, "MINIX until 1.4a"},
180 {0x81, "MINIX since 1.4b, early Linux, Mitac dmgr"},
181 {0x82, "Linux swap or Prime or Solaris"},
182 {0x83, "Linux native"},
183 {0x84, "OS/2 hidden C: drive"},
184 {0x85, "Linux extended"},
185 {0x86, "NT FAT volume set"},
186 {0x87, "NTFS volume set or HPFS mirrored"},
187 {0x93, "Amoeba filesystem"},
188 {0x94, "Amoeba bad block table"},
189 {0x99, "Mylex EISA SCSI"},
190 {0x9f, "BSDI?"},
191 {0xA0, "IBM Thinkpad hibernation"},
192 {0xa1, reserved},
193 {0xa3, reserved},
194 {0xa4, reserved},
195 {0xA5, "FreeBSD or 386BSD or old NetBSD"},
196 {0xA6, "OpenBSD"},
197 {0xA7, "NeXTSTEP 486"},
198 {0xa9, "NetBSD"},
199 {0xb1, reserved},
200 {0xb3, reserved},
201 {0xb4, reserved},
202 {0xb6, reserved},
203 {0xB7, "BSDI BSD/386 filesystem"},
204 {0xB8, "BSDI BSD/386 swap"},
205 {0xc0, "CTOS"},
206 {0xC1, "DRDOS/sec (FAT-12)"},
207 {0xC4, "DRDOS/sec (FAT-16, < 32M)"},
208 {0xC6, "DRDOS/sec (FAT-16, >= 32M)"},
209 {0xC7, "Syrinx (Cyrnix?) or HPFS disabled"},
210 {0xd8, "CP/M 86"},
211 {0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"},
212 {0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"},
213 {0xE3, "DOS R/O or SpeedStor or Storage Dimensions"},
214 {0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."},
215 {0xe5, reserved},
216 {0xe6, reserved},
217 {0xeb, "BeOS"},
218 {0xF1, "SpeedStor or Storage Dimensions"},
219 {0xF2, "DOS 3.3+ Secondary"},
220 {0xf3, reserved},
221 {0xF4, "SpeedStor large partition or Storage Dimensions"},
222 {0xf6, reserved},
223 {0xFE, "SpeedStor >1024 cyl. or LANstep or IBM PS/2 IML"},
224 {0xFF, "Xenix Bad Block Table"},
225 };
226
227 void usage __P((void));
228 void print_s0 __P((int));
229 void print_part __P((int));
230 void read_boot __P((char *));
231 void init_sector0 __P((int, int));
232 void intuit_translated_geometry __P((void));
233 int try_heads __P((quad_t, quad_t, quad_t, quad_t, quad_t, quad_t, quad_t,
234 quad_t));
235 int try_sectors __P((quad_t, quad_t, quad_t, quad_t, quad_t));
236 void change_part __P((int, int, int, int));
237 void print_params __P((void));
238 void change_active __P((int));
239 void get_params_to_use __P((void));
240 void dos __P((int, unsigned char *, unsigned char *, unsigned char *));
241 int open_disk __P((int));
242 int read_disk __P((int, void *));
243 int write_disk __P((int, void *));
244 int get_params __P((void));
245 int read_s0 __P((void));
246 int write_s0 __P((void));
247 int yesno __P((char *));
248 void decimal __P((char *, int *));
249 int type_match __P((const void *, const void *));
250 char *get_type __P((int));
251 int get_mapping __P((int, int *, int *, int *, long *));
252
253 static inline unsigned short getshort __P((void *));
254 static inline void putshort __P((void *p, unsigned short));
255 static inline unsigned long getlong __P((void *));
256 static inline void putlong __P((void *, unsigned long));
257
258
259 int main __P((int, char **));
260
261 int
262 main(argc, argv)
263 int argc;
264 char *argv[];
265 {
266 int ch;
267 int part;
268
269 int csysid, cstart, csize; /* For the b_flag. */
270
271 a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
272 csysid = cstart = csize = 0;
273 while ((ch = getopt(argc, argv, "0123Safius:b:c:")) != -1)
274 switch (ch) {
275 case '0':
276 partition = 0;
277 break;
278 case '1':
279 partition = 1;
280 break;
281 case '2':
282 partition = 2;
283 break;
284 case '3':
285 partition = 3;
286 break;
287 case 'S':
288 sh_flag = 1;
289 break;
290 case 'a':
291 a_flag = 1;
292 break;
293 case 'f':
294 f_flag = 1;
295 break;
296 case 'i':
297 i_flag = 1;
298 break;
299 case 'u':
300 u_flag = 1;
301 break;
302 case 's':
303 s_flag = 1;
304 if (sscanf (optarg, "%d/%d/%d",
305 &csysid, &cstart, &csize) != 3) {
306 (void)fprintf (stderr, "%s: Bad argument "
307 "to the -s flag.\n",
308 argv[0]);
309 exit (1);
310 }
311 break;
312 case 'b':
313 b_flag = 1;
314 if (sscanf (optarg, "%d/%d/%d",
315 &b_cyl, &b_head, &b_sec) != 3) {
316 (void)fprintf (stderr, "%s: Bad argument "
317 "to the -b flag.\n",
318 argv[0]);
319 exit (1);
320 }
321 if (b_cyl > MAXCYL)
322 b_cyl = MAXCYL;
323 break;
324 case 'c':
325 read_boot(optarg);
326 break;
327 default:
328 usage();
329 }
330 argc -= optind;
331 argv += optind;
332
333 if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
334 usage();
335
336 if (partition == -1 && s_flag) {
337 (void) fprintf (stderr,
338 "-s flag requires a partition selected.\n");
339 usage();
340 }
341
342 if (argc > 0)
343 disk = argv[0];
344
345 if (open_disk(a_flag || i_flag || u_flag) < 0)
346 exit(1);
347
348 if (read_s0())
349 init_sector0(sectors > 63 ? 63 : sectors, 1);
350
351 intuit_translated_geometry();
352
353 if (!sh_flag && !f_flag)
354 printf("******* Working on device %s *******\n", disk);
355
356
357 if ((i_flag || u_flag) && (!f_flag || b_flag))
358 get_params_to_use();
359
360 if (i_flag)
361 init_sector0(dos_sectors > 63 ? 63 : dos_sectors, 0);
362
363 if (!sh_flag && !f_flag)
364 printf("Warning: BIOS sector numbering starts with sector 1\n");
365
366 /* Do the update stuff! */
367 if (u_flag) {
368 if (!f_flag)
369 printf("Information from DOS bootblock is:\n");
370 if (partition == -1)
371 for (part = 0; part < NMBRPART; part++)
372 change_part(part,-1, -1, -1);
373 else
374 change_part(partition, csysid, cstart, csize);
375 } else
376 if (!i_flag)
377 print_s0(partition);
378
379 if (a_flag)
380 change_active(partition);
381
382 if (u_flag || a_flag || i_flag) {
383 if (!f_flag) {
384 printf("\nWe haven't changed the partition table "
385 "yet. This is your last chance.\n");
386 print_s0(-1);
387 if (yesno("Should we write new partition table?"))
388 write_s0();
389 } else
390 write_s0();
391 }
392
393 exit(0);
394 }
395
396 void
397 usage()
398 {
399 (void)fprintf(stderr, "usage: fdisk [-aiufSc] [-0|-1|-2|-3] "
400 "[device]\n");
401 exit(1);
402 }
403
404 void
405 print_s0(which)
406 int which;
407 {
408 int part;
409
410 print_params();
411 if (!sh_flag)
412 printf("Information from DOS bootblock is:\n");
413 if (which == -1) {
414 for (part = 0; part < NMBRPART; part++) {
415 if (!sh_flag)
416 printf("%d: ", part);
417 print_part(part);
418 }
419 } else
420 print_part(which);
421 }
422
423 static struct mbr_partition mtpart = { 0 };
424
425 static inline unsigned short
426 getshort(p)
427 void *p;
428 {
429 unsigned char *cp = p;
430
431 return cp[0] | (cp[1] << 8);
432 }
433
434 static inline void
435 putshort(p, l)
436 void *p;
437 unsigned short l;
438 {
439 unsigned char *cp = p;
440
441 *cp++ = l;
442 *cp++ = l >> 8;
443 }
444
445 static inline unsigned long
446 getlong(p)
447 void *p;
448 {
449 unsigned char *cp = p;
450
451 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
452 }
453
454 static inline void
455 putlong(p, l)
456 void *p;
457 unsigned long l;
458 {
459 unsigned char *cp = p;
460
461 *cp++ = l;
462 *cp++ = l >> 8;
463 *cp++ = l >> 16;
464 *cp++ = l >> 24;
465 }
466
467 void
468 print_part(part)
469 int part;
470 {
471 struct mbr_partition *partp;
472 int empty;
473
474 partp = &mboot.parts[part];
475 empty = !memcmp(partp, &mtpart, sizeof(struct mbr_partition));
476
477 if (sh_flag) {
478 if (empty) {
479 printf("PART%dSIZE=0\n", part);
480 return;
481 }
482
483 printf("PART%dID=%d\n", part, partp->mbrp_typ);
484 printf("PART%dSIZE=%ld\n", part, getlong(&partp->mbrp_size));
485 printf("PART%dSTART=%ld\n", part, getlong(&partp->mbrp_start));
486 printf("PART%dFLAG=0x%x\n", part, partp->mbrp_flag);
487 printf("PART%dBCYL=%d\n", part, MBR_PCYL(partp->mbrp_scyl,
488 partp->mbrp_ssect));
489 printf("PART%dBHEAD=%d\n", part, partp->mbrp_shd);
490 printf("PART%dBSEC=%d\n", part, MBR_PSECT(partp->mbrp_ssect));
491 printf("PART%dECYL=%d\n", part, MBR_PCYL(partp->mbrp_ecyl,
492 partp->mbrp_esect));
493 printf("PART%dEHEAD=%d\n", part, partp->mbrp_ehd);
494 printf("PART%dESEC=%d\n", part, MBR_PSECT(partp->mbrp_esect));
495 return;
496 }
497
498 /* Not sh_flag. */
499 if (empty) {
500 printf("<UNUSED>\n");
501 return;
502 }
503 printf("sysid %d (%s)\n", partp->mbrp_typ, get_type(partp->mbrp_typ));
504 printf(" start %ld, size %ld (%ld MB), flag 0x%x\n",
505 getlong(&partp->mbrp_start), getlong(&partp->mbrp_size),
506 getlong(&partp->mbrp_size) * 512 / (1024 * 1024), partp->mbrp_flag);
507 printf("\tbeg: cylinder %4d, head %3d, sector %2d\n",
508 MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect),
509 partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect));
510 printf("\tend: cylinder %4d, head %3d, sector %2d\n",
511 MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect),
512 partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect));
513 }
514
515 void
516 read_boot(name)
517 char *name;
518 {
519 int fd;
520 struct stat st;
521
522 if ((fd = open(name, O_RDONLY)) < 0)
523 err(1, "%s", name);
524 if (fstat(fd, &st) == -1)
525 err(1, "%s", name);
526 if (st.st_size > sizeof(bootcode))
527 errx(1, "%s: bootcode too large", name);
528 bootsize = st.st_size;
529 if (bootsize < 0x200)
530 errx(1, "%s: bootcode too small", name);
531 if (read(fd, bootcode, bootsize) != bootsize)
532 err(1, "%s", name);
533 close(fd);
534
535 /*
536 * Do some sanity checking here
537 */
538 if (getshort(bootcode + MBR_MAGICOFF) != MBR_MAGIC)
539 errx(1, "%s: invalid magic", name);
540 bootsize = (bootsize + 0x1ff) / 0x200;
541 bootsize *= 0x200;
542 }
543
544 void
545 init_sector0(start, dopart)
546 int start, dopart;
547 {
548 int i;
549
550 #ifdef DEFAULT_BOOTCODE
551 if (!bootsize)
552 read_boot(DEFAULT_BOOTCODE);
553 #endif
554
555 memcpy(mboot.bootinst, bootcode, sizeof(mboot.bootinst));
556 putshort(&mboot.signature, MBR_MAGIC);
557
558 if (dopart)
559 for (i=0; i<3; i++)
560 memset (&mboot.parts[i], 0, sizeof(struct mbr_partition));
561
562 }
563
564 /* Prerequisite: the disklabel parameters and master boot record must
565 * have been read (i.e. dos_* and mboot are meaningful).
566 * Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
567 * dos_cylindersectors to be consistent with what the
568 * partition table is using, if we can find a geometry
569 * which is consistent with all partition table entries.
570 * We may get the number of cylinders slightly wrong (in
571 * the conservative direction). The idea is to be able
572 * to create a NetBSD partition on a disk we don't know
573 * the translated geometry of.
574 * This whole routine should be replaced with a kernel interface to get
575 * the BIOS geometry (which in turn requires modifications to the i386
576 * boot loader to pass in the BIOS geometry for each disk). */
577 void
578 intuit_translated_geometry()
579 {
580
581 int cylinders = -1, heads = -1, sectors = -1, i, j;
582 int c1, h1, s1, c2, h2, s2;
583 long a1, a2;
584 quad_t num, denom;
585
586 /* Try to deduce the number of heads from two different mappings. */
587 for (i = 0; i < NMBRPART * 2; i++) {
588 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
589 continue;
590 for (j = 0; j < 8; j++) {
591 if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
592 continue;
593 num = (quad_t)h1*(a2-s2) - (quad_t)h2*(a1-s1);
594 denom = (quad_t)c2*(a1-s1) - (quad_t)c1*(a2-s2);
595 if (denom != 0 && num % denom == 0) {
596 heads = num / denom;
597 break;
598 }
599 }
600 if (heads != -1)
601 break;
602 }
603
604 if (heads == -1)
605 return;
606
607 /* Now figure out the number of sectors from a single mapping. */
608 for (i = 0; i < NMBRPART * 2; i++) {
609 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
610 continue;
611 num = a1 - s1;
612 denom = c1 * heads + h1;
613 if (denom != 0 && num % denom == 0) {
614 sectors = num / denom;
615 break;
616 }
617 }
618
619 if (sectors == -1)
620 return;
621
622 /* Estimate the number of cylinders. */
623 cylinders = disklabel.d_secperunit / heads / sectors;
624
625 /* Now verify consistency with each of the partition table entries.
626 * Be willing to shove cylinders up a little bit to make things work,
627 * but translation mismatches are fatal. */
628 for (i = 0; i < NMBRPART * 2; i++) {
629 if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
630 continue;
631 if (sectors * (c1 * heads + h1) + s1 != a1)
632 return;
633 if (c1 >= cylinders)
634 cylinders = c1 + 1;
635 }
636
637 /* Everything checks out. Reset the geometry to use for further
638 * calculations. */
639 dos_cylinders = cylinders;
640 dos_heads = heads;
641 dos_sectors = sectors;
642 dos_cylindersectors = heads * sectors;
643 }
644
645 /* For the purposes of intuit_translated_geometry(), treat the partition
646 * table as a list of eight mapping between (cylinder, head, sector)
647 * triplets and absolute sectors. Get the relevant geometry triplet and
648 * absolute sectors for a given entry, or return -1 if it isn't present.
649 * Note: for simplicity, the returned sector is 0-based. */
650 int
651 get_mapping(i, cylinder, head, sector, absolute)
652 int i, *cylinder, *head, *sector;
653 long *absolute;
654 {
655 struct mbr_partition *part = &mboot.parts[i / 2];
656
657 if (part->mbrp_typ == 0)
658 return -1;
659 if (i % 2 == 0) {
660 *cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
661 *head = part->mbrp_shd;
662 *sector = MBR_PSECT(part->mbrp_ssect) - 1;
663 *absolute = getlong(&part->mbrp_start);
664 } else {
665 *cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
666 *head = part->mbrp_ehd;
667 *sector = MBR_PSECT(part->mbrp_esect) - 1;
668 *absolute = getlong(&part->mbrp_start)
669 + getlong(&part->mbrp_size) - 1;
670 }
671 return 0;
672 }
673
674 void
675 change_part(part, csysid, cstart, csize)
676 int part, csysid, cstart, csize;
677 {
678 struct mbr_partition *partp;
679
680 partp = &mboot.parts[part];
681
682 if (s_flag) {
683 if (csysid == 0 && cstart == 0 && csize == 0)
684 memset(partp, 0, sizeof *partp);
685 else {
686 partp->mbrp_typ = csysid;
687 #if 0
688 checkcyl(cstart / dos_cylindersectors);
689 #endif
690 putlong(&partp->mbrp_start, cstart);
691 putlong(&partp->mbrp_size, csize);
692 dos(getlong(&partp->mbrp_start),
693 &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
694 dos(getlong(&partp->mbrp_start)
695 + getlong(&partp->mbrp_size) - 1,
696 &partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
697 }
698 if (f_flag)
699 return;
700 }
701
702 printf("The data for partition %d is:\n", part);
703 print_part(part);
704 if (!u_flag || !yesno("Do you want to change it?"))
705 return;
706
707 do {
708 {
709 int sysid, start, size;
710
711 sysid = partp->mbrp_typ,
712 start = getlong(&partp->mbrp_start),
713 size = getlong(&partp->mbrp_size);
714 decimal("sysid", &sysid);
715 decimal("start", &start);
716 decimal("size", &size);
717 partp->mbrp_typ = sysid;
718 putlong(&partp->mbrp_start, start);
719 putlong(&partp->mbrp_size, size);
720 }
721
722 if (yesno("Explicitly specify beg/end address?")) {
723 int tsector, tcylinder, thead;
724
725 tcylinder = MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect);
726 thead = partp->mbrp_shd;
727 tsector = MBR_PSECT(partp->mbrp_ssect);
728 decimal("beginning cylinder", &tcylinder);
729 #if 0
730 checkcyl(tcylinder);
731 #endif
732 decimal("beginning head", &thead);
733 decimal("beginning sector", &tsector);
734 partp->mbrp_scyl = DOSCYL(tcylinder);
735 partp->mbrp_shd = thead;
736 partp->mbrp_ssect = DOSSECT(tsector, tcylinder);
737
738 tcylinder = MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect);
739 thead = partp->mbrp_ehd;
740 tsector = MBR_PSECT(partp->mbrp_esect);
741 decimal("ending cylinder", &tcylinder);
742 decimal("ending head", &thead);
743 decimal("ending sector", &tsector);
744 partp->mbrp_ecyl = DOSCYL(tcylinder);
745 partp->mbrp_ehd = thead;
746 partp->mbrp_esect = DOSSECT(tsector, tcylinder);
747 } else {
748
749 if (partp->mbrp_typ == 0
750 && getlong(&partp->mbrp_start) == 0
751 && getlong(&partp->mbrp_size) == 0)
752 memset(partp, 0, sizeof *partp);
753 else {
754 #if 0
755 checkcyl(getlong(&partp->mbrp_start)
756 / dos_cylindersectors);
757 #endif
758 dos(getlong(&partp->mbrp_start), &partp->mbrp_scyl,
759 &partp->mbrp_shd, &partp->mbrp_ssect);
760 dos(getlong(&partp->mbrp_start)
761 + getlong(&partp->mbrp_size) - 1,
762 &partp->mbrp_ecyl, &partp->mbrp_ehd,
763 &partp->mbrp_esect);
764 }
765 }
766
767 print_part(part);
768 } while (!yesno("Is this entry okay?"));
769 }
770
771 void
772 print_params()
773 {
774
775 if (sh_flag) {
776 printf ("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%d\n",
777 cylinders, heads, sectors, disksectors);
778 printf ("BCYL=%d\nBHEAD=%d\nBSEC=%d\n",
779 dos_cylinders, dos_heads, dos_sectors);
780 return;
781 }
782
783 /* Not sh_flag */
784 printf("parameters extracted from in-core disklabel are:\n");
785 printf("cylinders=%d heads=%d sectors/track=%d (%d sectors/cylinder)\n\n",
786 cylinders, heads, sectors, cylindersectors);
787 if (dos_sectors > 63 || dos_heads > 255)
788 printf("Figures below won't work with BIOS for partitions not in cylinder 1\n");
789 printf("parameters to be used for BIOS calculations are:\n");
790 printf("cylinders=%d heads=%d sectors/track=%d (%d sectors/cylinder)\n\n",
791 dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors);
792 }
793
794 void
795 change_active(which)
796 int which;
797 {
798 struct mbr_partition *partp;
799 int part;
800 int active = 4;
801
802 partp = &mboot.parts[0];
803
804 if (a_flag && which != -1)
805 active = which;
806 else {
807 for (part = 0; part < NMBRPART; part++)
808 if (partp[part].mbrp_flag & ACTIVE)
809 active = part;
810 }
811 if (!f_flag) {
812 if (yesno("Do you want to change the active partition?")) {
813 printf ("Choosing 4 will make no partition active.\n");
814 do {
815 decimal("active partition", &active);
816 } while (!yesno("Are you happy with this choice?"));
817 } else
818 return;
819 } else
820 if (active != 4)
821 printf ("Making partition %d active.\n", active);
822
823 for (part = 0; part < NMBRPART; part++)
824 partp[part].mbrp_flag &= ~ACTIVE;
825 if (active < 4)
826 partp[active].mbrp_flag |= ACTIVE;
827 }
828
829 void
830 get_params_to_use()
831 {
832 if (b_flag) {
833 dos_cylinders = b_cyl;
834 dos_heads = b_head;
835 dos_sectors = b_sec;
836 dos_cylindersectors = dos_heads * dos_sectors;
837 return;
838 }
839
840 print_params();
841 if (yesno("Do you want to change our idea of what BIOS thinks?")) {
842 do {
843 decimal("BIOS's idea of #cylinders", &dos_cylinders);
844 decimal("BIOS's idea of #heads", &dos_heads);
845 decimal("BIOS's idea of #sectors", &dos_sectors);
846 dos_cylindersectors = dos_heads * dos_sectors;
847 print_params();
848 } while (!yesno("Are you happy with this choice?"));
849 }
850 }
851
852 /***********************************************\
853 * Change real numbers into strange dos numbers *
854 \***********************************************/
855 void
856 dos(sector, cylinderp, headp, sectorp)
857 int sector;
858 unsigned char *cylinderp, *headp, *sectorp;
859 {
860 int cylinder, head;
861
862 cylinder = sector / dos_cylindersectors;
863
864 sector -= cylinder * dos_cylindersectors;
865
866 head = sector / dos_sectors;
867 sector -= head * dos_sectors;
868
869 if (cylinder >= MAXCYL)
870 cylinder = MAXCYL - 1;
871 *cylinderp = DOSCYL(cylinder);
872 *headp = head;
873 *sectorp = DOSSECT(sector + 1, cylinder);
874 }
875
876 #if 0
877 void
878 checkcyl(cyl)
879 int cyl;
880 {
881 if (cyl >= MAXCYL)
882 warnx("partition start beyond BIOS limit");
883 }
884 #endif
885
886 int fd;
887
888 int
889 open_disk(u_flag)
890 int u_flag;
891 {
892 static char namebuf[MAXPATHLEN + 1];
893 struct stat st;
894
895 fd = opendisk(disk, u_flag ? O_RDWR : O_RDONLY, namebuf,
896 sizeof(namebuf), 0);
897 if (fd < 0) {
898 warn("%s", namebuf);
899 return (-1);
900 }
901 disk = namebuf;
902 if (fstat(fd, &st) == -1) {
903 close(fd);
904 warn("%s", disk);
905 return (-1);
906 }
907 if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode)) {
908 close(fd);
909 warnx("%s is not a character device or regular file", disk);
910 return (-1);
911 }
912 if (get_params() == -1) {
913 close(fd);
914 return (-1);
915 }
916 return (0);
917 }
918
919 int
920 read_disk(sector, buf)
921 int sector;
922 void *buf;
923 {
924
925 if (lseek(fd, (off_t)(sector * 512), 0) == -1)
926 return (-1);
927 return (read(fd, buf, 512));
928 }
929
930 int
931 write_disk(sector, buf)
932 int sector;
933 void *buf;
934 {
935
936 if (lseek(fd, (off_t)(sector * 512), 0) == -1)
937 return (-1);
938 return (write(fd, buf, 512));
939 }
940
941 int
942 get_params()
943 {
944
945 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
946 warn("DIOCGDINFO");
947 return (-1);
948 }
949
950 dos_cylinders = cylinders = disklabel.d_ncylinders;
951 dos_heads = heads = disklabel.d_ntracks;
952 dos_sectors = sectors = disklabel.d_nsectors;
953 dos_cylindersectors = cylindersectors = heads * sectors;
954 disksectors = disklabel.d_secperunit;
955
956 return (0);
957 }
958
959 int
960 read_s0()
961 {
962
963 if (read_disk(0, mboot.bootinst) == -1) {
964 warn("can't read fdisk partition table");
965 return (-1);
966 }
967 if (getshort(&mboot.signature) != MBR_MAGIC) {
968 warnx("invalid fdisk partition table found");
969 return (-1);
970 }
971 return (0);
972 }
973
974 int
975 write_s0()
976 {
977 int flag, i;
978
979 /*
980 * write enable label sector before write (if necessary),
981 * disable after writing.
982 * needed if the disklabel protected area also protects
983 * sector 0. (e.g. empty disk)
984 */
985 flag = 1;
986 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
987 warn("DIOCWLABEL");
988 if (write_disk(0, mboot.bootinst) == -1) {
989 warn("can't write fdisk partition table");
990 return -1;
991 }
992 for (i = bootsize; (i -= 0x200) > 0;)
993 if (write_disk(i / 0x200, bootcode + i) == -1) {
994 warn("can't write bootcode");
995 return -1;
996 }
997 flag = 0;
998 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
999 warn("DIOCWLABEL");
1000 return 0;
1001 }
1002
1003 int
1004 yesno(str)
1005 char *str;
1006 {
1007 int ch, first;
1008
1009 printf("%s [n] ", str);
1010
1011 first = ch = getchar();
1012 while (ch != '\n' && ch != EOF)
1013 ch = getchar();
1014 return (first == 'y' || first == 'Y');
1015 }
1016
1017 void
1018 decimal(str, num)
1019 char *str;
1020 int *num;
1021 {
1022 int acc = 0;
1023 char *cp;
1024
1025 for (;; printf("%s is not a valid decimal number.\n", lbuf)) {
1026 printf("Supply a decimal value for \"%s\" [%d] ", str, *num);
1027
1028 fgets(lbuf, LBUF, stdin);
1029 lbuf[strlen(lbuf)-1] = '\0';
1030 cp = lbuf;
1031
1032 cp += strspn(cp, " \t");
1033 if (*cp == '\0')
1034 return;
1035
1036 if (!isdigit(*cp))
1037 continue;
1038 acc = strtol(lbuf, &cp, 10);
1039
1040 cp += strspn(cp, " \t");
1041 if (*cp != '\0')
1042 continue;
1043
1044 *num = acc;
1045 return;
1046 }
1047
1048 }
1049
1050 int
1051 type_match(key, item)
1052 const void *key, *item;
1053 {
1054 const int *typep = key;
1055 const struct part_type *ptr = item;
1056
1057 if (*typep < ptr->type)
1058 return (-1);
1059 if (*typep > ptr->type)
1060 return (1);
1061 return (0);
1062 }
1063
1064 char *
1065 get_type(type)
1066 int type;
1067 {
1068 struct part_type *ptr;
1069
1070 ptr = bsearch(&type, part_types,
1071 sizeof(part_types) / sizeof(struct part_type),
1072 sizeof(struct part_type), type_match);
1073 if (ptr == 0)
1074 return ("unknown");
1075 return (ptr->name);
1076 }
1077