]> git.ipfire.org Git - thirdparty/util-linux.git/blob - fdisk/sfdisk.c
Imported from util-linux-2.12h tarball.
[thirdparty/util-linux.git] / fdisk / sfdisk.c
1 /*
2 * sfdisk version 3.0 - aeb - 950813
3 *
4 * Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
5 *
6 * This program is free software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation: either Version 1
9 * or (at your option) any later version.
10 *
11 * A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994,
12 * patched by various people (faith@cs.unc.edu, martin@cs.unc.edu,
13 * leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl)
14 * 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e.
15 * This program had (head,sector,cylinder) as basic unit, and was
16 * (therefore) broken in several ways for the use on larger disks -
17 * for example, my last patch (from 2.0d to 2.0e) was required
18 * to allow a partition to cross cylinder 8064, and to write an
19 * extended partition past the 4GB mark.
20 *
21 * The current program is a rewrite from scratch, and I started a
22 * version numbering at 3.0.
23 * Andries Brouwer, aeb@cwi.nl, 950813
24 *
25 * Well, a good user interface is still lacking. On the other hand,
26 * many configurations cannot be handled by any other fdisk.
27 * I changed the name to sfdisk to prevent confusion. - aeb, 970501
28 *
29 * Changes:
30 * 19990319 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> - i18n
31 * 20040824 - David A. Wheeler <dwheeler@dwheeler.com> - warnings to stderr
32 */
33
34 #define PROGNAME "sfdisk"
35 #define VERSION "3.08"
36 #define DATE "040824"
37
38 #include <stdio.h>
39 #include <stdlib.h> /* atoi, free */
40 #include <stdarg.h> /* varargs */
41 #include <unistd.h> /* read, write */
42 #include <fcntl.h> /* O_RDWR */
43 #include <errno.h> /* ERANGE */
44 #include <string.h> /* index() */
45 #include <ctype.h>
46 #include <getopt.h>
47 #include <sys/ioctl.h>
48 #include <sys/stat.h>
49 #include <sys/utsname.h>
50 #include <linux/unistd.h> /* _syscall */
51 #include "nls.h"
52 #include "common.h"
53
54 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
55
56 /*
57 * Table of contents:
58 * A. About seeking
59 * B. About sectors
60 * C. About heads, sectors and cylinders
61 * D. About system Ids
62 * E. About partitions
63 * F. The standard input
64 * G. The command line
65 * H. Listing the current situation
66 * I. Writing the new situation
67 */
68 int exit_status = 0;
69
70 int force = 0; /* 1: do what I say, even if it is stupid ... */
71 int quiet = 0; /* 1: suppress all warnings */
72 /* IA-64 gcc spec file currently does -DLinux... */
73 #undef Linux
74 int Linux = 0; /* 1: suppress warnings irrelevant for Linux */
75 int DOS = 0; /* 1: shift extended partitions by #sectors, not 1 */
76 int DOS_extended = 0; /* 1: use starting cylinder boundary of extd partn */
77 int dump = 0; /* 1: list in a format suitable for later input */
78 int verify = 0; /* 1: check that listed partition is reasonable */
79 int no_write = 0; /* 1: do not actually write to disk */
80 int no_reread = 0; /* 1: skip the BLKRRPART ioctl test at startup */
81 int leave_last = 0; /* 1: don't allocate the last cylinder */
82 int opt_list = 0;
83 char *save_sector_file = NULL;
84 char *restore_sector_file = NULL;
85
86 static void
87 do_warn(char *s, ...) {
88 va_list p;
89
90 va_start(p, s);
91 fflush(stdout);
92 vfprintf(stderr, s, p);
93 fflush(stderr);
94 va_end(p);
95 }
96
97 static void
98 warn(char *s, ...) {
99 va_list p;
100
101 va_start(p, s);
102 if (!quiet)
103 do_warn(s, p);
104 va_end(p);
105 }
106
107 static void
108 error(char *s, ...) {
109 va_list p;
110
111 va_start(p, s);
112 fflush(stdout);
113 fprintf(stderr, "\n" PROGNAME ": ");
114 vfprintf(stderr, s, p);
115 fflush(stderr);
116 va_end(p);
117 }
118
119 static void
120 fatal(char *s, ...) {
121 va_list p;
122
123 va_start(p, s);
124 fflush(stdout);
125 fprintf(stderr, "\n" PROGNAME ": ");
126 vfprintf(stderr, s, p);
127 fflush(stderr);
128 va_end(p);
129 exit(1);
130 }
131
132 /*
133 * GCC nonsense - needed for GCC 3.4.x with -O2
134 */
135 #if defined(__GNUC__PREREQ) && __GNUC_PREREQ(3,4)
136 #define __attribute__used __attribute__ ((used))
137 #else
138 #define __attribute__used
139 #endif
140
141 /* Or test with #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4) */
142
143
144 /*
145 * A. About seeking
146 */
147
148 /*
149 * sseek: seek to specified sector - return 0 on failure
150 *
151 * For >4GB disks lseek needs a > 32bit arg, and we have to use llseek.
152 * On the other hand, a 32 bit sector number is OK until 2TB.
153 * The routines _llseek and sseek below are the only ones that
154 * know about the loff_t type.
155 *
156 * Note: we use 512-byte sectors here, irrespective of the hardware ss.
157 */
158 #undef use_lseek
159 #if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (__s390x__)
160 #define use_lseek
161 #endif
162
163 #ifndef use_lseek
164 static __attribute__used
165 _syscall5(int, _llseek, unsigned int, fd, ulong, hi, ulong, lo,
166 loff_t *, res, unsigned int, wh);
167 #endif
168
169 static int
170 sseek(char *dev, unsigned int fd, unsigned long s) {
171 loff_t in, out;
172 in = ((loff_t) s << 9);
173 out = 1;
174
175 #ifndef use_lseek
176 if (_llseek (fd, in>>32, in & 0xffffffff, &out, SEEK_SET) != 0) {
177 #else
178 if ((out = lseek(fd, in, SEEK_SET)) != in) {
179 #endif
180 perror("llseek");
181 error(_("seek error on %s - cannot seek to %lu\n"), dev, s);
182 return 0;
183 }
184
185 if (in != out) {
186 error(_("seek error: wanted 0x%08x%08x, got 0x%08x%08x\n"),
187 (unsigned int)(in>>32), (unsigned int)(in & 0xffffffff),
188 (unsigned int)(out>>32), (unsigned int)(out & 0xffffffff));
189 return 0;
190 }
191 return 1;
192 }
193
194 /*
195 * B. About sectors
196 */
197
198 /*
199 * We preserve all sectors read in a chain - some of these will
200 * have to be modified and written back.
201 */
202 struct sector {
203 struct sector *next;
204 unsigned long sectornumber;
205 int to_be_written;
206 char data[512];
207 } *sectorhead;
208
209 static void
210 free_sectors(void) {
211 struct sector *s;
212
213 while (sectorhead) {
214 s = sectorhead;
215 sectorhead = s->next;
216 free(s);
217 }
218 }
219
220 static struct sector *
221 get_sector(char *dev, int fd, unsigned long sno) {
222 struct sector *s;
223
224 for(s = sectorhead; s; s = s->next)
225 if (s->sectornumber == sno)
226 return s;
227
228 if (!sseek(dev, fd, sno))
229 return 0;
230
231 if (!(s = (struct sector *) malloc(sizeof(struct sector))))
232 fatal(_("out of memory - giving up\n"));
233
234 if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
235 if (errno) /* 0 in case we read past end-of-disk */
236 perror("read");
237 error(_("read error on %s - cannot read sector %lu\n"), dev, sno);
238 free(s);
239 return 0;
240 }
241
242 s->next = sectorhead;
243 sectorhead = s;
244 s->sectornumber = sno;
245 s->to_be_written = 0;
246
247 return s;
248 }
249
250 static int
251 msdos_signature (struct sector *s) {
252 unsigned char *data = s->data;
253 if (data[510] == 0x55 && data[511] == 0xaa)
254 return 1;
255 error(_("ERROR: sector %lu does not have an msdos signature\n"),
256 s->sectornumber);
257 return 0;
258 }
259
260 static int
261 write_sectors(char *dev, int fd) {
262 struct sector *s;
263
264 for (s = sectorhead; s; s = s->next)
265 if (s->to_be_written) {
266 if (!sseek(dev, fd, s->sectornumber))
267 return 0;
268 if (write(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
269 perror("write");
270 error(_("write error on %s - cannot write sector %lu\n"),
271 dev, s->sectornumber);
272 return 0;
273 }
274 s->to_be_written = 0;
275 }
276 return 1;
277 }
278
279 static void
280 ulong_to_chars(unsigned long u, char *uu) {
281 int i;
282
283 for(i=0; i<4; i++) {
284 uu[i] = (u & 0xff);
285 u >>= 8;
286 }
287 }
288
289 static unsigned long
290 chars_to_ulong(unsigned char *uu) {
291 int i;
292 unsigned long u = 0;
293
294 for(i=3; i>=0; i--)
295 u = (u << 8) | uu[i];
296 return u;
297 }
298
299 static int
300 save_sectors(char *dev, int fdin) {
301 struct sector *s;
302 char ss[516];
303 int fdout;
304
305 fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444);
306 if (fdout < 0) {
307 perror(save_sector_file);
308 error(_("cannot open partition sector save file (%s)\n"),
309 save_sector_file);
310 return 0;
311 }
312
313 for (s = sectorhead; s; s = s->next)
314 if (s->to_be_written) {
315 ulong_to_chars(s->sectornumber, ss);
316 if (!sseek(dev, fdin, s->sectornumber))
317 return 0;
318 if (read(fdin, ss+4, 512) != 512) {
319 perror("read");
320 error(_("read error on %s - cannot read sector %lu\n"),
321 dev, s->sectornumber);
322 return 0;
323 }
324 if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) {
325 perror("write");
326 error(_("write error on %s\n"), save_sector_file);
327 return 0;
328 }
329 }
330 return 1;
331 }
332
333 static void reread_disk_partition(char *dev, int fd);
334
335 static int
336 restore_sectors(char *dev) {
337 int fdin, fdout, ct;
338 struct stat statbuf;
339 char *ss0, *ss;
340 unsigned long sno;
341
342 if (stat(restore_sector_file, &statbuf) < 0) {
343 perror(restore_sector_file);
344 error(_("cannot stat partition restore file (%s)\n"),
345 restore_sector_file);
346 return 0;
347 }
348 if (statbuf.st_size % 516) {
349 error(_("partition restore file has wrong size - not restoring\n"));
350 return 0;
351 }
352 if (!(ss = (char *) malloc(statbuf.st_size))) {
353 error(_("out of memory?\n"));
354 return 0;
355 }
356 fdin = open(restore_sector_file, O_RDONLY);
357 if (fdin < 0) {
358 perror(restore_sector_file);
359 error(_("cannot open partition restore file (%s)\n"),
360 restore_sector_file);
361 return 0;
362 }
363 if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) {
364 perror("read");
365 error(_("error reading %s\n"), restore_sector_file);
366 return 0;
367 }
368
369 fdout = open(dev, O_WRONLY);
370 if (fdout < 0) {
371 perror(dev);
372 error(_("cannot open device %s for writing\n"), dev);
373 return 0;
374 }
375
376 ss0 = ss;
377 ct = statbuf.st_size/516;
378 while(ct--) {
379 sno = chars_to_ulong(ss);
380 if (!sseek(dev, fdout, sno))
381 return 0;
382 if (write(fdout, ss+4, 512) != 512) {
383 perror(dev);
384 error(_("error writing sector %lu on %s\n"), sno, dev);
385 return 0;
386 }
387 ss += 516;
388 }
389 free(ss0);
390
391 reread_disk_partition(dev, fdout);
392
393 return 1;
394 }
395
396 /*
397 * C. About heads, sectors and cylinders
398 */
399
400 /*
401 * <linux/hdreg.h> defines HDIO_GETGEO and
402 * struct hd_geometry {
403 * unsigned char heads;
404 * unsigned char sectors;
405 * unsigned short cylinders;
406 * unsigned long start;
407 * };
408 *
409 * For large disks g.cylinders is truncated, so we use BLKGETSIZE.
410 */
411
412 /*
413 * We consider several geometries for a disk:
414 * B - the BIOS geometry, gotten from the kernel via HDIO_GETGEO
415 * F - the fdisk geometry
416 * U - the user-specified geometry
417 *
418 * 0 means unspecified / unknown
419 */
420 struct geometry {
421 unsigned long cylindersize;
422 unsigned long heads, sectors, cylinders;
423 unsigned long start;
424 } B, F, U;
425
426 static struct geometry
427 get_geometry(char *dev, int fd, int silent) {
428 struct hd_geometry g;
429 unsigned long cyls;
430 unsigned long long sectors;
431 struct geometry R;
432
433 if (ioctl(fd, HDIO_GETGEO, &g)) {
434 g.heads = g.sectors = g.cylinders = g.start = 0;
435 if (!silent)
436 do_warn(_("Disk %s: cannot get geometry\n"), dev);
437 }
438
439 R.start = g.start;
440 R.heads = g.heads;
441 R.sectors = g.sectors;
442 R.cylindersize = R.heads * R.sectors;
443 R.cylinders = 0;
444
445 if (disksize(fd, &sectors)) {
446 if (!silent)
447 do_warn(_("Disk %s: cannot get size\n"), dev);
448 } else if (R.cylindersize) {
449 sectors /= R.cylindersize;
450 cyls = sectors;
451 if (cyls != sectors)
452 cyls = ~0;
453 R.cylinders = cyls;
454 }
455 return R;
456 }
457
458 static void
459 get_cylindersize(char *dev, int fd, int silent) {
460 struct geometry R;
461
462 R = get_geometry(dev, fd, silent);
463
464 B.heads = (U.heads ? U.heads : R.heads);
465 B.sectors = (U.sectors ? U.sectors : R.sectors);
466 B.cylinders = (U.cylinders ? U.cylinders : R.cylinders);
467
468 B.cylindersize = B.heads * B.sectors;
469
470 if (R.start && !force) {
471 warn(
472 _("Warning: start=%lu - this looks like a partition rather than\n"
473 "the entire disk. Using fdisk on it is probably meaningless.\n"
474 "[Use the --force option if you really want this]\n"), R.start);
475 exit(1);
476 }
477
478 if (R.heads && B.heads != R.heads)
479 warn(_("Warning: HDIO_GETGEO says that there are %lu heads\n"),
480 R.heads);
481 if (R.sectors && B.sectors != R.sectors)
482 warn(_("Warning: HDIO_GETGEO says that there are %lu sectors\n"),
483 R.sectors);
484 if (R.cylinders && B.cylinders != R.cylinders
485 && B.cylinders < 65536 && R.cylinders < 65536)
486 warn(_("Warning: BLKGETSIZE/HDIO_GETGEO says that there are %lu cylinders\n"),
487 R.cylinders);
488
489 if (B.sectors > 63)
490 warn(_("Warning: unlikely number of sectors (%lu) - usually at most 63\n"
491 "This will give problems with all software that uses C/H/S addressing.\n"),
492 B.sectors);
493 if (!silent)
494 printf(_("\nDisk %s: %lu cylinders, %lu heads, %lu sectors/track\n"),
495 dev, B.cylinders, B.heads, B.sectors);
496 }
497
498 typedef struct { unsigned char h,s,c; } chs; /* has some c bits in s */
499 chs zero_chs = { 0,0,0 };
500
501 typedef struct { unsigned long h,s,c; } longchs;
502 longchs zero_longchs;
503
504 static chs
505 longchs_to_chs (longchs aa, struct geometry G) {
506 chs a;
507
508 if (aa.h < 256 && aa.s < 64 && aa.c < 1024) {
509 a.h = aa.h;
510 a.s = aa.s | ((aa.c >> 2) & 0xc0);
511 a.c = (aa.c & 0xff);
512 } else if (G.heads && G.sectors) {
513 a.h = G.heads - 1;
514 a.s = G.sectors | 0xc0;
515 a.c = 0xff;
516 } else
517 a = zero_chs;
518 return a;
519 }
520
521 static longchs
522 chs_to_longchs (chs a) {
523 longchs aa;
524
525 aa.h = a.h;
526 aa.s = (a.s & 0x3f);
527 aa.c = (a.s & 0xc0);
528 aa.c = (aa.c << 2) + a.c;
529 return aa;
530 }
531
532 static longchs
533 ulong_to_longchs (unsigned long sno, struct geometry G) {
534 longchs aa;
535
536 if (G.heads && G.sectors && G.cylindersize) {
537 aa.s = 1 + sno % G.sectors;
538 aa.h = (sno / G.sectors) % G.heads;
539 aa.c = sno / G.cylindersize;
540 return aa;
541 } else {
542 return zero_longchs;
543 }
544 }
545
546 static chs
547 ulong_to_chs (unsigned long sno, struct geometry G) {
548 return longchs_to_chs(ulong_to_longchs(sno, G), G);
549 }
550
551 #if 0
552 static unsigned long
553 longchs_to_ulong (longchs aa, struct geometry G) {
554 return (aa.c*G.cylindersize + aa.h*G.sectors + aa.s - 1);
555 }
556
557 static unsigned long
558 chs_to_ulong (chs a, struct geometry G) {
559 return longchs_to_ulong(chs_to_longchs(a), G);
560 }
561 #endif
562
563 static int
564 is_equal_chs (chs a, chs b) {
565 return (a.h == b.h && a.s == b.s && a.c == b.c);
566 }
567
568 static int
569 chs_ok (chs a, char *v, char *w) {
570 longchs aa = chs_to_longchs(a);
571 int ret = 1;
572
573 if (is_equal_chs(a, zero_chs))
574 return 1;
575 if (B.heads && aa.h >= B.heads) {
576 warn(_("%s of partition %s has impossible value for head: "
577 "%lu (should be in 0-%lu)\n"), w, v, aa.h, B.heads-1);
578 ret = 0;
579 }
580 if (B.sectors && (aa.s == 0 || aa.s > B.sectors)) {
581 warn(_("%s of partition %s has impossible value for sector: "
582 "%lu (should be in 1-%lu)\n"), w, v, aa.s, B.sectors);
583 ret = 0;
584 }
585 if (B.cylinders && aa.c >= B.cylinders) {
586 warn(_("%s of partition %s has impossible value for cylinders: "
587 "%lu (should be in 0-%lu)\n"), w, v, aa.c, B.cylinders-1);
588 ret = 0;
589 }
590 return ret;
591 }
592
593 /*
594 * D. About system Ids
595 */
596
597 #define EMPTY_PARTITION 0
598 #define EXTENDED_PARTITION 5
599 #define WIN98_EXTENDED 0x0f
600 #define DM6_AUX1PARTITION 0x51
601 #define DM6_AUX3PARTITION 0x53
602 #define DM6_PARTITION 0x54
603 #define EZD_PARTITION 0x55
604 #define LINUX_SWAP 0x82
605 #define LINUX_NATIVE 0x83
606 #define LINUX_EXTENDED 0x85
607 #define BSD_PARTITION 0xa5
608 #define NETBSD_PARTITION 0xa9
609
610 /* List of partition types now in i386_sys_types.c */
611
612 static const char *
613 sysname(unsigned char type) {
614 struct systypes *s;
615
616 for (s = i386_sys_types; s->name; s++)
617 if (s->type == type)
618 return _(s->name);
619 return _("Unknown");
620 }
621
622 static void
623 list_types(void) {
624 struct systypes *s;
625
626 printf(_("Id Name\n\n"));
627 for (s = i386_sys_types; s->name; s++)
628 printf("%2x %s\n", s->type, _(s->name));
629 }
630
631 static int
632 is_extended(unsigned char type) {
633 return (type == EXTENDED_PARTITION
634 || type == LINUX_EXTENDED
635 || type == WIN98_EXTENDED);
636 }
637
638 static int
639 is_bsd(unsigned char type) {
640 return (type == BSD_PARTITION || type == NETBSD_PARTITION);
641 }
642
643 /*
644 * E. About partitions
645 */
646
647 /* MS/DOS partition */
648
649 struct partition {
650 unsigned char bootable; /* 0 or 0x80 */
651 chs begin_chs;
652 unsigned char sys_type;
653 chs end_chs;
654 unsigned int start_sect; /* starting sector counting from 0 */
655 unsigned int nr_sects; /* nr of sectors in partition */
656 };
657
658 /* Unfortunately, partitions are not aligned, and non-Intel machines
659 are unhappy with non-aligned integers. So, we need a copy by hand. */
660 static int
661 copy_to_int(unsigned char *cp) {
662 unsigned int m;
663
664 m = *cp++;
665 m += (*cp++ << 8);
666 m += (*cp++ << 16);
667 m += (*cp++ << 24);
668 return m;
669 }
670
671 static void
672 copy_from_int(int m, char *cp) {
673 *cp++ = (m & 0xff); m >>= 8;
674 *cp++ = (m & 0xff); m >>= 8;
675 *cp++ = (m & 0xff); m >>= 8;
676 *cp++ = (m & 0xff);
677 }
678
679 static void
680 copy_to_part(char *cp, struct partition *p) {
681 p->bootable = *cp++;
682 p->begin_chs.h = *cp++;
683 p->begin_chs.s = *cp++;
684 p->begin_chs.c = *cp++;
685 p->sys_type = *cp++;
686 p->end_chs.h = *cp++;
687 p->end_chs.s = *cp++;
688 p->end_chs.c = *cp++;
689 p->start_sect = copy_to_int(cp);
690 p->nr_sects = copy_to_int(cp+4);
691 }
692
693 static void
694 copy_from_part(struct partition *p, char *cp) {
695 *cp++ = p->bootable;
696 *cp++ = p->begin_chs.h;
697 *cp++ = p->begin_chs.s;
698 *cp++ = p->begin_chs.c;
699 *cp++ = p->sys_type;
700 *cp++ = p->end_chs.h;
701 *cp++ = p->end_chs.s;
702 *cp++ = p->end_chs.c;
703 copy_from_int(p->start_sect, cp);
704 copy_from_int(p->nr_sects, cp+4);
705 }
706
707 /* Roughly speaking, Linux doesn't use any of the above fields except
708 for partition type, start sector and number of sectors. (However,
709 see also linux/drivers/scsi/fdomain.c.)
710 The only way partition type is used (in the kernel) is the comparison
711 for equality with EXTENDED_PARTITION (and these Disk Manager types). */
712
713 struct part_desc {
714 unsigned long start;
715 unsigned long size;
716 unsigned long sector, offset; /* disk location of this info */
717 struct partition p;
718 struct part_desc *ep; /* extended partition containing this one */
719 int ptype;
720 #define DOS_TYPE 0
721 #define BSD_TYPE 1
722 } zero_part_desc;
723
724 static struct part_desc *
725 outer_extended_partition(struct part_desc *p) {
726 while (p->ep)
727 p = p->ep;
728 return p;
729 }
730
731 static int
732 is_parent(struct part_desc *pp, struct part_desc *p) {
733 while (p) {
734 if (pp == p)
735 return 1;
736 p = p->ep;
737 }
738 return 0;
739 }
740
741 struct disk_desc {
742 struct part_desc partitions[512];
743 int partno;
744 } oldp, newp;
745
746 /* determine where on the disk this information goes */
747 static void
748 add_sector_and_offset(struct disk_desc *z) {
749 int pno;
750 struct part_desc *p;
751
752 for (pno = 0; pno < z->partno; pno++) {
753 p = &(z->partitions[pno]);
754 p->offset = 0x1be + (pno%4)*sizeof(struct partition);
755 p->sector = (p->ep ? p->ep->start : 0);
756 }
757 }
758
759 /* tell the kernel to reread the partition tables */
760 static int
761 reread_ioctl(int fd) {
762 if (ioctl(fd, BLKRRPART)) {
763 perror("BLKRRPART");
764 return -1;
765 }
766 return 0;
767 }
768
769 static int
770 is_blockdev(int fd) {
771 struct stat statbuf;
772
773 return(fstat(fd, &statbuf) == 0 && S_ISBLK(statbuf.st_mode));
774 }
775
776 /* reread after writing */
777 static void
778 reread_disk_partition(char *dev, int fd) {
779 printf(_("Re-reading the partition table ...\n"));
780 fflush(stdout);
781 sync();
782 sleep(3); /* superfluous since 1.3.20 */
783
784 if (reread_ioctl(fd) && is_blockdev(fd))
785 do_warn(_("The command to re-read the partition table failed\n"
786 "Reboot your system now, before using mkfs\n"));
787
788 if (close(fd)) {
789 perror(dev);
790 do_warn(_("Error closing %s\n"), dev);
791 }
792 printf("\n");
793 }
794
795 /* find Linux name of this partition, assuming that it will have a name */
796 static int
797 index_to_linux(int pno, struct disk_desc *z) {
798 int i, ct = 1;
799 struct part_desc *p = &(z->partitions[0]);
800 for (i=0; i<pno; i++,p++)
801 if (i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
802 ct++;
803 return ct;
804 }
805
806 static int
807 linux_to_index(int lpno, struct disk_desc *z) {
808 int i, ct = 0;
809 struct part_desc *p = &(z->partitions[0]);
810 for (i=0; i<z->partno && ct < lpno; i++,p++)
811 if ((i < 4 || (p->size > 0 && !is_extended(p->p.sys_type)))
812 && ++ct == lpno)
813 return i;
814 return -1;
815 }
816
817 static int
818 asc_to_index(char *pnam, struct disk_desc *z) {
819 int pnum, pno;
820
821 if (*pnam == '#') {
822 pno = atoi(pnam+1);
823 } else {
824 pnum = atoi(pnam);
825 pno = linux_to_index(pnum, z);
826 }
827 if (!(pno >= 0 && pno < z->partno))
828 fatal(_("%s: no such partition\n"), pnam);
829 return pno;
830 }
831
832 /*
833 * List partitions - in terms of sectors, blocks or cylinders
834 */
835 #define F_SECTOR 1
836 #define F_BLOCK 2
837 #define F_CYLINDER 3
838 #define F_MEGABYTE 4
839
840 int default_format = F_MEGABYTE;
841 int specified_format = 0;
842 int show_extended = 0;
843 int one_only = 0;
844 int one_only_pno;
845 int increment = 0;
846
847 static void
848 set_format(char c) {
849 switch(c) {
850 default:
851 do_warn(_("unrecognized format - using sectors\n"));
852 case 'S': specified_format = F_SECTOR; break;
853 case 'B': specified_format = F_BLOCK; break;
854 case 'C': specified_format = F_CYLINDER; break;
855 case 'M': specified_format = F_MEGABYTE; break;
856 }
857 }
858
859 static unsigned long
860 unitsize(int format) {
861 default_format = (B.cylindersize ? F_CYLINDER : F_MEGABYTE);
862 if (!format && !(format = specified_format))
863 format = default_format;
864
865 switch(format) {
866 default:
867 case F_CYLINDER:
868 if (B.cylindersize)
869 return B.cylindersize;
870 case F_SECTOR:
871 return 1;
872 case F_BLOCK:
873 return 2;
874 case F_MEGABYTE:
875 return 2048;
876 }
877 }
878
879 static unsigned long
880 get_disksize(int format) {
881 unsigned long cs = B.cylinders;
882 if (cs && leave_last)
883 cs--;
884 return (cs * B.cylindersize) / unitsize(format);
885 }
886
887 static void
888 out_partition_header(char *dev, int format, struct geometry G) {
889 if (dump) {
890 printf(_("# partition table of %s\n"), dev);
891 printf("unit: sectors\n\n");
892 return;
893 }
894
895 default_format = (G.cylindersize ? F_CYLINDER : F_MEGABYTE);
896 if (!format && !(format = specified_format))
897 format = default_format;
898
899 switch(format) {
900 default:
901 do_warn(_("unimplemented format - using %s\n"),
902 G.cylindersize ? _("cylinders") : _("sectors"));
903 case F_CYLINDER:
904 if (G.cylindersize) {
905 printf(_("Units = cylinders of %lu bytes, blocks of 1024 bytes"
906 ", counting from %d\n\n"),
907 G.cylindersize<<9, increment);
908 printf(_(" Device Boot Start End #cyls #blocks Id System\n"));
909 break;
910 }
911 /* fall through */
912 case F_SECTOR:
913 printf(_("Units = sectors of 512 bytes, counting from %d\n\n"),
914 increment);
915 printf(_(" Device Boot Start End #sectors Id System\n"));
916 break;
917 case F_BLOCK:
918 printf(_("Units = blocks of 1024 bytes, counting from %d\n\n"),
919 increment);
920 printf(_(" Device Boot Start End #blocks Id System\n"));
921 break;
922 case F_MEGABYTE:
923 printf(_("Units = mebibytes of 1048576 bytes, blocks of 1024 bytes"
924 ", counting from %d\n\n"), increment);
925 printf(_(" Device Boot Start End MiB #blocks Id System\n"));
926 break;
927 }
928 }
929
930 static void
931 out_rounddown(int width, unsigned long n, unsigned long unit, int inc) {
932 printf("%*lu", width, inc + n/unit);
933 if (unit != 1)
934 putchar((n % unit) ? '+' : ' ');
935 putchar(' ');
936 }
937
938 static void
939 out_roundup(int width, unsigned long n, unsigned long unit, int inc) {
940 if (n == (unsigned long)(-1))
941 printf("%*s", width, "-");
942 else
943 printf("%*lu", width, inc + n/unit);
944 if (unit != 1)
945 putchar(((n+1) % unit) ? '-' : ' ');
946 putchar(' ');
947 }
948
949 static void
950 out_roundup_size(int width, unsigned long n, unsigned long unit) {
951 printf("%*lu", width, (n+unit-1)/unit);
952 if (unit != 1)
953 putchar((n % unit) ? '-' : ' ');
954 putchar(' ');
955 }
956
957 static struct geometry
958 get_fdisk_geometry_one(struct part_desc *p) {
959 struct geometry G;
960
961 chs b = p->p.end_chs;
962 longchs bb = chs_to_longchs(b);
963 G.heads = bb.h+1;
964 G.sectors = bb.s;
965 G.cylindersize = G.heads*G.sectors;
966 G.cylinders = G.start = 0;
967 return G;
968 }
969
970 static int
971 get_fdisk_geometry(struct disk_desc *z) {
972 struct part_desc *p;
973 int pno, agree;
974 struct geometry G0, G;
975
976 agree = 0;
977 G0.heads = G0.sectors = 0;
978 for (pno=0; pno < z->partno; pno++) {
979 p = &(z->partitions[pno]);
980 if (p->size != 0 && p->p.sys_type != 0) {
981 G = get_fdisk_geometry_one(p);
982 if (!G0.heads) {
983 G0 = G;
984 agree = 1;
985 } else if (G.heads != G0.heads || G.sectors != G0.sectors) {
986 agree = 0;
987 break;
988 }
989 }
990 }
991 F = (agree ? G0 : B);
992 return (F.sectors != B.sectors || F.heads != B.heads);
993 }
994
995 static void
996 out_partition(char *dev, int format, struct part_desc *p,
997 struct disk_desc *z, struct geometry G) {
998 unsigned long start, end, size;
999 int pno, lpno;
1000
1001 if (!format && !(format = specified_format))
1002 format = default_format;
1003
1004 pno = p - &(z->partitions[0]); /* our index */
1005 lpno = index_to_linux(pno, z); /* name of next one that has a name */
1006 if (pno == linux_to_index(lpno, z)) /* was that us? */
1007 printf("%s", partname(dev, lpno, 10)); /* yes */
1008 else if (show_extended)
1009 printf(" - ");
1010 else
1011 return;
1012 putchar(dump ? ':' : ' ');
1013
1014 start = p->start;
1015 end = p->start + p->size - 1;
1016 size = p->size;
1017
1018 if (dump) {
1019 printf(" start=%9lu", start);
1020 printf(", size=%9lu", size);
1021 if (p->ptype == DOS_TYPE) {
1022 printf(", Id=%2x", p->p.sys_type);
1023 if (p->p.bootable == 0x80)
1024 printf(", bootable");
1025 }
1026 printf("\n");
1027 return;
1028 }
1029
1030 if (p->ptype != DOS_TYPE || p->p.bootable == 0)
1031 printf(" ");
1032 else if (p->p.bootable == 0x80)
1033 printf(" * ");
1034 else
1035 printf(" ? "); /* garbage */
1036
1037 switch(format) {
1038 case F_CYLINDER:
1039 if (G.cylindersize) {
1040 out_rounddown(6, start, G.cylindersize, increment);
1041 out_roundup(6, end, G.cylindersize, increment);
1042 out_roundup_size(6, size, G.cylindersize);
1043 out_rounddown(9, size, 2, 0);
1044 break;
1045 }
1046 /* fall through */
1047 default:
1048 case F_SECTOR:
1049 out_rounddown(9, start, 1, increment);
1050 out_roundup(9, end, 1, increment);
1051 out_rounddown(10, size, 1, 0);
1052 break;
1053 case F_BLOCK:
1054 #if 0
1055 printf("%8lu,%3lu ",
1056 p->sector/2, ((p->sector & 1) ? 512 : 0) + p->offset);
1057 #endif
1058 out_rounddown(8, start, 2, increment);
1059 out_roundup(8, end, 2, increment);
1060 out_rounddown(9, size, 2, 0);
1061 break;
1062 case F_MEGABYTE:
1063 out_rounddown(5, start, 2048, increment);
1064 out_roundup(5, end, 2048, increment);
1065 out_roundup_size(5, size, 2048);
1066 out_rounddown(9, size, 2, 0);
1067 break;
1068 }
1069 if (p->ptype == DOS_TYPE) {
1070 printf(" %2x %s\n",
1071 p->p.sys_type, sysname(p->p.sys_type));
1072 } else {
1073 printf("\n");
1074 }
1075
1076 /* Is chs as we expect? */
1077 if (!quiet && p->ptype == DOS_TYPE) {
1078 chs a, b;
1079 longchs aa, bb;
1080 a = (size ? ulong_to_chs(start,G) : zero_chs);
1081 b = p->p.begin_chs;
1082 aa = chs_to_longchs(a);
1083 bb = chs_to_longchs(b);
1084 if (a.s && !is_equal_chs(a, b))
1085 do_warn(_("\t\tstart: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1086 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1087 a = (size ? ulong_to_chs(end,G) : zero_chs);
1088 b = p->p.end_chs;
1089 aa = chs_to_longchs(a);
1090 bb = chs_to_longchs(b);
1091 if (a.s && !is_equal_chs(a, b))
1092 do_warn(_("\t\tend: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1093 aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1094 if (G.cylinders && G.cylinders < 1024 && bb.c > G.cylinders)
1095 do_warn(_("partition ends on cylinder %ld, beyond the end of the disk\n"),
1096 bb.c);
1097 }
1098 }
1099
1100 static void
1101 out_partitions(char *dev, struct disk_desc *z) {
1102 int pno, format = 0;
1103
1104 if (z->partno == 0)
1105 do_warn(_("No partitions found\n"));
1106 else {
1107 if (get_fdisk_geometry(z) && !dump) {
1108 do_warn(
1109 _("Warning: The partition table looks like it was made\n"
1110 " for C/H/S=*/%ld/%ld (instead of %ld/%ld/%ld).\n"
1111 "For this listing I'll assume that geometry.\n"),
1112 F.heads, F.sectors, B.cylinders, B.heads, B.sectors);
1113 }
1114
1115 out_partition_header(dev, format, F);
1116 for(pno=0; pno < z->partno; pno++) {
1117 out_partition(dev, format, &(z->partitions[pno]), z, F);
1118 if (show_extended && pno%4==3)
1119 printf("\n");
1120 }
1121 }
1122 }
1123
1124 static int
1125 disj(struct part_desc *p, struct part_desc *q) {
1126 return
1127 ((p->start + p->size <= q->start)
1128 || (is_extended(p->p.sys_type)
1129 && q->start + q->size <= p->start + p->size));
1130 }
1131
1132 static char *
1133 pnumber(struct part_desc *p, struct disk_desc *z) {
1134 static char buf[20];
1135 int this, next;
1136 struct part_desc *p0 = &(z->partitions[0]);
1137
1138 this = index_to_linux(p-p0, z);
1139 next = index_to_linux(p-p0+1, z);
1140
1141 if (next > this)
1142 sprintf(buf, "%d", this);
1143 else
1144 sprintf(buf, "[%d]", this);
1145 return buf;
1146 }
1147
1148 static int
1149 partitions_ok(struct disk_desc *z) {
1150 struct part_desc *partitions = &(z->partitions[0]), *p, *q;
1151 int partno = z->partno;
1152
1153 #define PNO(p) pnumber(p, z)
1154
1155 /* Have at least 4 partitions been defined? */
1156 if (partno < 4) {
1157 if (!partno)
1158 fatal(_("no partition table present.\n"));
1159 else
1160 fatal(_("strange, only %d partitions defined.\n"), partno);
1161 return 0;
1162 }
1163
1164 /* Are the partitions of size 0 marked empty?
1165 And do they have start = 0? And bootable = 0? */
1166 for (p = partitions; p - partitions < partno; p++)
1167 if (p->size == 0) {
1168 if (p->p.sys_type != EMPTY_PARTITION)
1169 warn(_("Warning: partition %s has size 0 but is not marked Empty\n"),
1170 PNO(p));
1171 else if (p->p.bootable != 0)
1172 warn(_("Warning: partition %s has size 0 and is bootable\n"),
1173 PNO(p));
1174 else if (p->p.start_sect != 0)
1175 warn(_("Warning: partition %s has size 0 and nonzero start\n"),
1176 PNO(p));
1177 /* all this is probably harmless, no error return */
1178 }
1179
1180 /* Are the logical partitions contained in their extended partitions? */
1181 for (p = partitions+4; p < partitions+partno; p++)
1182 if (p->ptype == DOS_TYPE)
1183 if (p->size && !is_extended(p->p.sys_type)) {
1184 q = p->ep;
1185 if (p->start < q->start || p->start + p->size > q->start + q->size) {
1186 warn(_("Warning: partition %s "), PNO(p));
1187 warn(_("is not contained in partition %s\n"), PNO(q));
1188 return 0;
1189 }
1190 }
1191
1192 /* Are the data partitions mutually disjoint? */
1193 for (p = partitions; p < partitions+partno; p++)
1194 if (p->size && !is_extended(p->p.sys_type))
1195 for (q = p+1; q < partitions+partno; q++)
1196 if (q->size && !is_extended(q->p.sys_type))
1197 if (!((p->start > q-> start) ? disj(q,p) : disj(p,q))) {
1198 warn(_("Warning: partitions %s "), PNO(p));
1199 warn(_("and %s overlap\n"), PNO(q));
1200 return 0;
1201 }
1202
1203 /* Are the data partitions and the extended partition
1204 table sectors disjoint? */
1205 for (p = partitions; p < partitions+partno; p++)
1206 if (p->size && !is_extended(p->p.sys_type))
1207 for (q = partitions; q < partitions+partno; q++)
1208 if (is_extended(q->p.sys_type))
1209 if (p->start <= q->start && p->start + p->size > q->start) {
1210 warn(_("Warning: partition %s contains part of "
1211 "the partition table (sector %lu),\n"
1212 "and will destroy it when filled\n"),
1213 PNO(p), q->start);
1214 return 0;
1215 }
1216
1217 /* Do they start past zero and end before end-of-disk? */
1218 { unsigned long ds = get_disksize(F_SECTOR);
1219 for (p = partitions; p < partitions+partno; p++)
1220 if (p->size) {
1221 if (p->start == 0) {
1222 warn(_("Warning: partition %s starts at sector 0\n"), PNO(p));
1223 return 0;
1224 }
1225 if (p->size && p->start + p->size > ds) {
1226 warn(_("Warning: partition %s extends past end of disk\n"),
1227 PNO(p));
1228 return 0;
1229 }
1230 }
1231 }
1232
1233 /* At most one chain of DOS extended partitions ? */
1234 /* It seems that the OS/2 fdisk has the additional requirement
1235 that the extended partition must be the fourth one */
1236 { int ect = 0;
1237 for (p = partitions; p < partitions+4; p++)
1238 if (p->p.sys_type == EXTENDED_PARTITION)
1239 ect++;
1240 if (ect > 1 && !Linux) {
1241 warn(_("Among the primary partitions, at most one can be extended\n"
1242 " (although this is not a problem under Linux)\n"));
1243 return 0;
1244 }
1245 }
1246
1247 /*
1248 * Do all partitions start at a cylinder boundary ?
1249 * (this is not required for Linux)
1250 * The first partition starts after MBR.
1251 * Logical partitions start slightly after the containing extended partn.
1252 */
1253 if (B.cylindersize) {
1254 for(p = partitions; p < partitions+partno; p++)
1255 if (p->size) {
1256 if (p->start % B.cylindersize != 0
1257 && (!p->ep || p->start / B.cylindersize != p->ep->start / B.cylindersize)
1258 && (p->p.start_sect >= B.cylindersize)) {
1259 warn(_("Warning: partition %s does not start "
1260 "at a cylinder boundary\n"), PNO(p));
1261 if (!Linux)
1262 return 0;
1263 }
1264 if ((p->start + p->size) % B.cylindersize) {
1265 warn(_("Warning: partition %s does not end "
1266 "at a cylinder boundary\n"), PNO(p));
1267 if (!Linux)
1268 return 0;
1269 }
1270 }
1271 }
1272
1273 /* Usually, one can boot only from primary partitions. */
1274 /* In fact, from a unique one only. */
1275 /* do not warn about bootable extended partitions -
1276 often LILO is there */
1277 { int pno = -1;
1278 for(p = partitions; p < partitions+partno; p++)
1279 if (p->p.bootable) {
1280 if (pno == -1)
1281 pno = p - partitions;
1282 else if (p - partitions < 4) {
1283 warn(_("Warning: more than one primary partition is marked "
1284 "bootable (active)\n"
1285 "This does not matter for LILO, but the DOS MBR will "
1286 "not boot this disk.\n"));
1287 break;
1288 }
1289 if (p - partitions >= 4) {
1290 warn(_("Warning: usually one can boot from primary partitions "
1291 "only\nLILO disregards the `bootable' flag.\n"));
1292 break;
1293 }
1294 }
1295 if (pno == -1 || pno >= 4)
1296 warn(_("Warning: no primary partition is marked bootable (active)\n"
1297 "This does not matter for LILO, but the DOS MBR will "
1298 "not boot this disk.\n"));
1299 }
1300
1301 /* Is chs as we expect? */
1302 for(p = partitions; p < partitions+partno; p++)
1303 if (p->ptype == DOS_TYPE) {
1304 chs a, b;
1305 longchs aa, bb;
1306 a = p->size ? ulong_to_chs(p->start,B) : zero_chs;
1307 b = p->p.begin_chs;
1308 aa = chs_to_longchs(a);
1309 bb = chs_to_longchs(b);
1310 if (!chs_ok(b, PNO(p), _("start")))
1311 return 0;
1312 if (a.s && !is_equal_chs(a, b))
1313 warn(_("partition %s: start: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1314 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1315 a = p->size ? ulong_to_chs(p->start + p->size - 1, B) : zero_chs;
1316 b = p->p.end_chs;
1317 aa = chs_to_longchs(a);
1318 bb = chs_to_longchs(b);
1319 if (!chs_ok(b, PNO(p), _("end")))
1320 return 0;
1321 if (a.s && !is_equal_chs(a, b))
1322 warn(_("partition %s: end: (c,h,s) expected (%ld,%ld,%ld) found (%ld,%ld,%ld)\n"),
1323 PNO(p), aa.c, aa.h, aa.s, bb.c, bb.h, bb.s);
1324 if (B.cylinders && B.cylinders < 1024 && bb.c > B.cylinders)
1325 warn(_("partition %s ends on cylinder %ld, beyond the end of the disk\n"),
1326 PNO(p), bb.c);
1327 }
1328
1329 return 1;
1330
1331 #undef PNO
1332 }
1333
1334 static void
1335 extended_partition(char *dev, int fd, struct part_desc *ep, struct disk_desc *z) {
1336 char *cp;
1337 struct sector *s;
1338 unsigned long start, here, next;
1339 int i, moretodo = 1;
1340 struct partition p;
1341 struct part_desc *partitions = &(z->partitions[0]);
1342 int pno = z->partno;
1343
1344 here = start = ep->start;
1345
1346 if (B.cylindersize && start % B.cylindersize) {
1347 /* This is BAD */
1348 if (DOS_extended) {
1349 here = start -= (start % B.cylindersize);
1350 do_warn(_("Warning: shifted start of the extd partition "
1351 "from %ld to %ld\n"
1352 "(For listing purposes only. "
1353 "Do not change its contents.)\n"),
1354 ep->start, start);
1355 } else {
1356 do_warn(_("Warning: extended partition does not start at a "
1357 "cylinder boundary.\n"
1358 "DOS and Linux will interpret the contents differently.\n"));
1359 }
1360 }
1361
1362 while (moretodo) {
1363 moretodo = 0;
1364
1365 if (!(s = get_sector(dev, fd, here)))
1366 break;
1367
1368 if (!msdos_signature(s))
1369 break;
1370
1371 cp = s->data + 0x1be;
1372
1373 if (pno+4 >= SIZE(z->partitions)) {
1374 do_warn(_("too many partitions - ignoring those past nr (%d)\n"),
1375 pno-1);
1376 break;
1377 }
1378
1379 next = 0;
1380
1381 for (i=0; i<4; i++,cp += sizeof(struct partition)) {
1382 partitions[pno].sector = here;
1383 partitions[pno].offset = cp - s->data;
1384 partitions[pno].ep = ep;
1385 copy_to_part(cp,&p);
1386 if (is_extended(p.sys_type)) {
1387 partitions[pno].start = start + p.start_sect;
1388 if (next)
1389 do_warn(_("tree of partitions?\n"));
1390 else
1391 next = partitions[pno].start; /* follow `upper' branch */
1392 moretodo = 1;
1393 } else {
1394 partitions[pno].start = here + p.start_sect;
1395 }
1396 partitions[pno].size = p.nr_sects;
1397 partitions[pno].ptype = DOS_TYPE;
1398 partitions[pno].p = p;
1399 pno++;
1400 }
1401 here = next;
1402 }
1403
1404 z->partno = pno;
1405 }
1406
1407 #define BSD_DISKMAGIC (0x82564557UL)
1408 #define BSD_MAXPARTITIONS 16
1409 #define BSD_FS_UNUSED 0
1410 typedef unsigned char u8;
1411 typedef unsigned short u16;
1412 typedef unsigned int u32;
1413 struct bsd_disklabel {
1414 u32 d_magic;
1415 char d_junk1[4];
1416 char d_typename[16];
1417 char d_packname[16];
1418 char d_junk2[92];
1419 u32 d_magic2;
1420 char d_junk3[2];
1421 u16 d_npartitions; /* number of partitions in following */
1422 char d_junk4[8];
1423 struct bsd_partition { /* the partition table */
1424 u32 p_size; /* number of sectors in partition */
1425 u32 p_offset; /* starting sector */
1426 u32 p_fsize; /* filesystem basic fragment size */
1427 u8 p_fstype; /* filesystem type, see below */
1428 u8 p_frag; /* filesystem fragments per block */
1429 u16 p_cpg; /* filesystem cylinders per group */
1430 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
1431 };
1432
1433 static void
1434 bsd_partition(char *dev, int fd, struct part_desc *ep, struct disk_desc *z) {
1435 struct bsd_disklabel *l;
1436 struct bsd_partition *bp, *bp0;
1437 unsigned long start = ep->start;
1438 struct sector *s;
1439 struct part_desc *partitions = &(z->partitions[0]);
1440 int pno = z->partno;
1441
1442 if (!(s = get_sector(dev,fd,start+1)))
1443 return;
1444 l = (struct bsd_disklabel *) (s->data);
1445 if (l->d_magic != BSD_DISKMAGIC || l->d_magic2 != BSD_DISKMAGIC)
1446 return;
1447
1448 bp = bp0 = &l->d_partitions[0];
1449 while (bp - bp0 < BSD_MAXPARTITIONS && bp - bp0 < l->d_npartitions) {
1450 if (pno+1 >= SIZE(z->partitions)) {
1451 do_warn(_("too many partitions - ignoring those "
1452 "past nr (%d)\n"), pno-1);
1453 break;
1454 }
1455 if (bp->p_fstype != BSD_FS_UNUSED) {
1456 partitions[pno].start = bp->p_offset;
1457 partitions[pno].size = bp->p_size;
1458 partitions[pno].sector = start+1;
1459 partitions[pno].offset = (char *)bp - (char *)bp0;
1460 partitions[pno].ep = 0;
1461 partitions[pno].ptype = BSD_TYPE;
1462 pno++;
1463 }
1464 bp++;
1465 }
1466 z->partno = pno;
1467 }
1468
1469 #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
1470
1471 static int
1472 linux_version_code(void) {
1473 struct utsname my_utsname;
1474 int p, q, r;
1475
1476 if (uname(&my_utsname) == 0) {
1477 p = atoi(strtok(my_utsname.release, "."));
1478 q = atoi(strtok(NULL, "."));
1479 r = atoi(strtok(NULL, "."));
1480 return MAKE_VERSION(p,q,r);
1481 }
1482 return 0;
1483 }
1484
1485 static int
1486 msdos_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1487 int i;
1488 char *cp;
1489 struct partition pt;
1490 struct sector *s;
1491 struct part_desc *partitions = &(z->partitions[0]);
1492 int pno = z->partno;
1493 int bsd_later = (linux_version_code() >= MAKE_VERSION(2,3,40));
1494
1495 if (!(s = get_sector(dev, fd, start)))
1496 return 0;
1497
1498 if (!msdos_signature(s))
1499 return 0;
1500
1501 cp = s->data + 0x1be;
1502 copy_to_part(cp,&pt);
1503
1504 /* If I am not mistaken, recent kernels will hide this from us,
1505 so we will never actually see traces of a Disk Manager */
1506 if (pt.sys_type == DM6_PARTITION
1507 || pt.sys_type == EZD_PARTITION
1508 || pt.sys_type == DM6_AUX1PARTITION
1509 || pt.sys_type == DM6_AUX3PARTITION) {
1510 do_warn(_("detected Disk Manager - unable to handle that\n"));
1511 return 0;
1512 }
1513 { unsigned int sig = *(unsigned short *)(s->data + 2);
1514 if (sig <= 0x1ae
1515 && *(unsigned short *)(s->data + sig) == 0x55aa
1516 && (1 & *(unsigned char *)(s->data + sig + 2))) {
1517 do_warn(_("DM6 signature found - giving up\n"));
1518 return 0;
1519 }
1520 }
1521
1522 for (pno=0; pno<4; pno++,cp += sizeof(struct partition)) {
1523 partitions[pno].sector = start;
1524 partitions[pno].offset = cp - s->data;
1525 copy_to_part(cp,&pt);
1526 partitions[pno].start = start + pt.start_sect;
1527 partitions[pno].size = pt.nr_sects;
1528 partitions[pno].ep = 0;
1529 partitions[pno].p = pt;
1530 }
1531
1532 z->partno = pno;
1533
1534 for (i=0; i<4; i++) {
1535 if (is_extended(partitions[i].p.sys_type)) {
1536 if (!partitions[i].size) {
1537 do_warn(_("strange..., an extended partition of size 0?\n"));
1538 continue;
1539 }
1540 extended_partition(dev, fd, &partitions[i], z);
1541 }
1542 if (!bsd_later && is_bsd(partitions[i].p.sys_type)) {
1543 if (!partitions[i].size) {
1544 do_warn(_("strange..., a BSD partition of size 0?\n"));
1545 continue;
1546 }
1547 bsd_partition(dev, fd, &partitions[i], z);
1548 }
1549 }
1550
1551 if (bsd_later) {
1552 for (i=0; i<4; i++) {
1553 if (is_bsd(partitions[i].p.sys_type)) {
1554 if (!partitions[i].size) {
1555 do_warn(_("strange..., a BSD partition of size 0?\n"));
1556 continue;
1557 }
1558 bsd_partition(dev, fd, &partitions[i], z);
1559 }
1560 }
1561 }
1562
1563 return 1;
1564 }
1565
1566 static int
1567 osf_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1568 return 0;
1569 }
1570
1571 static int
1572 sun_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1573 return 0;
1574 }
1575
1576 static int
1577 amiga_partition(char *dev, int fd, unsigned long start, struct disk_desc *z) {
1578 return 0;
1579 }
1580
1581 static void
1582 get_partitions(char *dev, int fd, struct disk_desc *z) {
1583 z->partno = 0;
1584
1585 if (!msdos_partition(dev, fd, 0, z)
1586 && !osf_partition(dev, fd, 0, z)
1587 && !sun_partition(dev, fd, 0, z)
1588 && !amiga_partition(dev, fd, 0, z)) {
1589 do_warn(_(" %s: unrecognized partition table type\n"), dev);
1590 return;
1591 }
1592 }
1593
1594 static int
1595 write_partitions(char *dev, int fd, struct disk_desc *z) {
1596 struct sector *s;
1597 struct part_desc *partitions = &(z->partitions[0]), *p;
1598 int pno = z->partno;
1599
1600 if (no_write) {
1601 do_warn(_("-n flag was given: Nothing changed\n"));
1602 exit(0);
1603 }
1604
1605 for (p = partitions; p < partitions+pno; p++) {
1606 s = get_sector(dev, fd, p->sector);
1607 if (!s) return 0;
1608 s->to_be_written = 1;
1609 if (p->ptype == DOS_TYPE) {
1610 copy_from_part(&(p->p), s->data + p->offset);
1611 s->data[510] = 0x55;
1612 s->data[511] = 0xaa;
1613 }
1614 }
1615 if (save_sector_file) {
1616 if (!save_sectors(dev, fd)) {
1617 fatal(_("Failed saving the old sectors - aborting\n"));
1618 return 0;
1619 }
1620 }
1621 if (!write_sectors(dev, fd)) {
1622 error(_("Failed writing the partition on %s\n"), dev);
1623 return 0;
1624 }
1625 return 1;
1626 }
1627
1628 /*
1629 * F. The standard input
1630 */
1631
1632 /*
1633 * Input format:
1634 * <start> <size> <type> <bootable> <c,h,s> <c,h,s>
1635 * Fields are separated by whitespace or comma or semicolon possibly
1636 * followed by whitespace; initial and trailing whitespace is ignored.
1637 * Numbers can be octal, decimal or hexadecimal, decimal is default
1638 * The <c,h,s> parts can (and probably should) be omitted.
1639 * Bootable is specified as [*|-], with as default not-bootable.
1640 * Type is given in hex, without the 0x prefix, or is [E|S|L|X], where
1641 * L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), and E
1642 * is EXTENDED_PARTITION (5), X is LINUX_EXTENDED (85).
1643 * The default value of start is the first nonassigned sector/cylinder/...
1644 * The default value of size is as much as possible (until next
1645 * partition or end-of-disk).
1646 * .: end of chain of extended partitions.
1647 *
1648 * On interactive input an empty line means: all defaults.
1649 * Otherwise empty lines are ignored.
1650 */
1651
1652 int eof, eob;
1653
1654 struct dumpfld {
1655 int fldno;
1656 char *fldname;
1657 int is_bool;
1658 } dumpflds[] = {
1659 { 0, "start", 0 },
1660 { 1, "size", 0 },
1661 { 2, "Id", 0 },
1662 { 3, "bootable", 1 },
1663 { 4, "bh", 0 },
1664 { 5, "bs", 0 },
1665 { 6, "bc", 0 },
1666 { 7, "eh", 0 },
1667 { 8, "es", 0 },
1668 { 9, "ec", 0 }
1669 };
1670
1671 /*
1672 * Read a line, split it into fields
1673 *
1674 * (some primitive handwork, but a more elaborate parser seems
1675 * unnecessary)
1676 */
1677 #define RD_EOF (-1)
1678 #define RD_CMD (-2)
1679
1680 static int
1681 read_stdin(unsigned char **fields, unsigned char *line, int fieldssize, int linesize) {
1682 unsigned char *lp, *ip;
1683 int c, fno;
1684
1685 /* boolean true and empty string at start */
1686 line[0] = '*';
1687 line[1] = 0;
1688 for (fno=0; fno < fieldssize; fno++)
1689 fields[fno] = line + 1;
1690 fno = 0;
1691
1692 /* read a line from stdin */
1693 lp = fgets(line+2, linesize, stdin);
1694 if (lp == NULL) {
1695 eof = 1;
1696 return RD_EOF;
1697 }
1698 if (!(lp = index(lp, '\n')))
1699 fatal(_("long or incomplete input line - quitting\n"));
1700 *lp = 0;
1701
1702 /* remove comments, if any */
1703 if ((lp = index(line+2, '#')) != 0)
1704 *lp = 0;
1705
1706 /* recognize a few commands - to be expanded */
1707 if (!strcmp(line+2, "unit: sectors")) {
1708 specified_format = F_SECTOR;
1709 return RD_CMD;
1710 }
1711
1712 /* dump style? - then bad input is fatal */
1713 if ((ip = index(line+2, ':')) != 0) {
1714 struct dumpfld *d;
1715
1716 nxtfld:
1717 ip++;
1718 while(isspace(*ip))
1719 ip++;
1720 if (*ip == 0)
1721 return fno;
1722 for(d = dumpflds; d-dumpflds < SIZE(dumpflds); d++) {
1723 if (!strncmp(ip, d->fldname, strlen(d->fldname))) {
1724 ip += strlen(d->fldname);
1725 while(isspace(*ip))
1726 ip++;
1727 if (d->is_bool)
1728 fields[d->fldno] = line;
1729 else if (*ip == '=') {
1730 while(isspace(*++ip)) ;
1731 fields[d->fldno] = ip;
1732 while(isalnum(*ip)) /* 0x07FF */
1733 ip++;
1734 } else
1735 fatal(_("input error: `=' expected after %s field\n"),
1736 d->fldname);
1737 if (fno <= d->fldno)
1738 fno = d->fldno + 1;
1739 if (*ip == 0)
1740 return fno;
1741 if (*ip != ',' && *ip != ';')
1742 fatal(_("input error: unexpected character %c after %s field\n"),
1743 *ip, d->fldname);
1744 *ip = 0;
1745 goto nxtfld;
1746 }
1747 }
1748 fatal(_("unrecognized input: %s\n"), ip);
1749 }
1750
1751 /* split line into fields */
1752 lp = ip = line+2;
1753 fields[fno++] = lp;
1754 while((c = *ip++) != 0) {
1755 if (!lp[-1] && (c == '\t' || c == ' '))
1756 ;
1757 else if (c == '\t' || c == ' ' || c == ',' || c == ';') {
1758 *lp++ = 0;
1759 if (fno < fieldssize)
1760 fields[fno++] = lp;
1761 continue;
1762 } else
1763 *lp++ = c;
1764 }
1765
1766 if (lp == fields[fno-1])
1767 fno--;
1768 return fno;
1769 }
1770
1771 /* read a number, use default if absent */
1772 /* a sign gives an offset from the default */
1773 static int
1774 get_ul(char *u, unsigned long *up, unsigned long def, int base) {
1775 char *nu;
1776 int sign = 0;
1777 unsigned long val;
1778
1779 if (*u == '+') {
1780 sign = 1;
1781 u++;
1782 } else if (*u == '-') {
1783 sign = -1;
1784 u++;
1785 }
1786 if (*u) {
1787 errno = 0;
1788 val = strtoul(u, &nu, base);
1789 if (errno == ERANGE) {
1790 do_warn(_("number too big\n"));
1791 return -1;
1792 }
1793 if (*nu) {
1794 do_warn(_("trailing junk after number\n"));
1795 return -1;
1796 }
1797 if (sign == 1)
1798 val = def + val;
1799 else if (sign == -1)
1800 val = def - val;
1801 *up = val;
1802 } else
1803 *up = def;
1804 return 0;
1805 }
1806
1807 /* There are two common ways to structure extended partitions:
1808 as nested boxes, and as a chain. Sometimes the partitions
1809 must be given in order. Sometimes all logical partitions
1810 must lie inside the outermost extended partition.
1811 NESTED: every partition is contained in the surrounding partitions
1812 and is disjoint from all others.
1813 CHAINED: every data partition is contained in the surrounding partitions
1814 and disjoint from all others, but extended partitions may lie outside
1815 (insofar as allowed by all_logicals_inside_outermost_extended).
1816 ONESECTOR: all data partitions are mutually disjoint; extended partitions
1817 each use one sector only (except perhaps for the outermost one).
1818 */
1819 int partitions_in_order = 0;
1820 int all_logicals_inside_outermost_extended = 1;
1821 enum { NESTED, CHAINED, ONESECTOR } boxes = NESTED;
1822
1823 /* find the default value for <start> - assuming entire units */
1824 static unsigned long
1825 first_free(int pno, int is_extended, struct part_desc *ep, int format,
1826 unsigned long mid, struct disk_desc *z) {
1827 unsigned long ff, fff;
1828 unsigned long unit = unitsize(format);
1829 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1830
1831 /* if containing ep undefined, look at its container */
1832 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1833 ep = ep->ep;
1834
1835 if (ep) {
1836 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1837 pp = ep;
1838 else if (all_logicals_inside_outermost_extended)
1839 pp = outer_extended_partition(ep);
1840 }
1841 #if 0
1842 ff = pp ? (pp->start + unit - 1) / unit : 0;
1843 #else
1844 /* rounding up wastes almost an entire cylinder - round down
1845 and leave it to compute_start_sect() to fix the difference */
1846 ff = pp ? pp->start / unit : 0;
1847 #endif
1848 /* MBR and 1st sector of an extended partition are never free */
1849 if (unit == 1)
1850 ff++;
1851
1852 again:
1853 for(pp = partitions; pp < partitions+pno; pp++) {
1854 if (!is_parent(pp, ep) && pp->size > 0) {
1855 if ((partitions_in_order || pp->start / unit <= ff
1856 || (mid && pp->start / unit <= mid))
1857 && (fff = (pp->start + pp->size + unit - 1) / unit) > ff) {
1858 ff = fff;
1859 goto again;
1860 }
1861 }
1862 }
1863
1864 return ff;
1865 }
1866
1867 /* find the default value for <size> - assuming entire units */
1868 static unsigned long
1869 max_length(int pno, int is_extended, struct part_desc *ep, int format,
1870 unsigned long start, struct disk_desc *z) {
1871 unsigned long fu;
1872 unsigned long unit = unitsize(format);
1873 struct part_desc *partitions = &(z->partitions[0]), *pp = 0;
1874
1875 /* if containing ep undefined, look at its container */
1876 if (ep && ep->p.sys_type == EMPTY_PARTITION)
1877 ep = ep->ep;
1878
1879 if (ep) {
1880 if (boxes == NESTED || (boxes == CHAINED && !is_extended))
1881 pp = ep;
1882 else if (all_logicals_inside_outermost_extended)
1883 pp = outer_extended_partition(ep);
1884 }
1885 fu = pp ? (pp->start + pp->size) / unit : get_disksize(format);
1886
1887 for(pp = partitions; pp < partitions+pno; pp++)
1888 if (!is_parent(pp, ep) && pp->size > 0
1889 && pp->start / unit >= start && pp->start / unit < fu)
1890 fu = pp->start / unit;
1891
1892 return (fu > start) ? fu - start : 0;
1893 }
1894
1895 /* compute starting sector of a partition inside an extended one */
1896 /* return 0 on failure */
1897 /* ep is 0 or points to surrounding extended partition */
1898 static int
1899 compute_start_sect(struct part_desc *p, struct part_desc *ep) {
1900 unsigned long base;
1901 int inc = (DOS && B.sectors) ? B.sectors : 1;
1902 int delta;
1903
1904 if (ep && p->start + p->size >= ep->start + 1)
1905 delta = p->start - ep->start - inc;
1906 else if (p->start == 0 && p->size > 0)
1907 delta = -inc;
1908 else
1909 delta = 0;
1910
1911 if (delta < 0) {
1912 p->start -= delta;
1913 p->size += delta;
1914 if (is_extended(p->p.sys_type) && boxes == ONESECTOR)
1915 p->size = inc;
1916 else if ((int)(p->size) <= 0) {
1917 warn(_("no room for partition descriptor\n"));
1918 return 0;
1919 }
1920 }
1921 base = (!ep ? 0
1922 : (is_extended(p->p.sys_type) ?
1923 outer_extended_partition(ep) : ep)->start);
1924 p->ep = ep;
1925 if (p->p.sys_type == EMPTY_PARTITION && p->size == 0) {
1926 p->p.start_sect = 0;
1927 p->p.begin_chs = zero_chs;
1928 p->p.end_chs = zero_chs;
1929 } else {
1930 p->p.start_sect = p->start - base;
1931 p->p.begin_chs = ulong_to_chs(p->start,B);
1932 p->p.end_chs = ulong_to_chs(p->start + p->size - 1,B);
1933 }
1934 p->p.nr_sects = p->size;
1935 return 1;
1936 }
1937
1938 /* build the extended partition surrounding a given logical partition */
1939 static int
1940 build_surrounding_extended(struct part_desc *p, struct part_desc *ep,
1941 struct disk_desc *z) {
1942 int inc = (DOS && B.sectors) ? B.sectors : 1;
1943 int format = F_SECTOR;
1944 struct part_desc *p0 = &(z->partitions[0]), *eep = ep->ep;
1945
1946 if (boxes == NESTED) {
1947 ep->start = first_free(ep-p0, 1, eep, format, p->start, z);
1948 ep->size = max_length(ep-p0, 1, eep, format, ep->start, z);
1949 if (ep->start > p->start || ep->start + ep->size < p->start + p->size) {
1950 warn(_("cannot build surrounding extended partition\n"));
1951 return 0;
1952 }
1953 } else {
1954 ep->start = p->start;
1955 if (boxes == CHAINED)
1956 ep->size = p->size;
1957 else
1958 ep->size = inc;
1959 }
1960
1961 ep->p.nr_sects = ep->size;
1962 ep->p.bootable = 0;
1963 ep->p.sys_type = EXTENDED_PARTITION;
1964 if (!compute_start_sect(ep, eep) || !compute_start_sect(p, ep)) {
1965 ep->p.sys_type = EMPTY_PARTITION;
1966 ep->size = 0;
1967 return 0;
1968 }
1969
1970 return 1;
1971 }
1972
1973 static int
1974 read_line(int pno, struct part_desc *ep, char *dev, int interactive,
1975 struct disk_desc *z) {
1976 unsigned char line[1000];
1977 unsigned char *fields[11];
1978 int fno, pct = pno%4;
1979 struct part_desc p, *orig;
1980 unsigned long ff, ff1, ul, ml, ml1, def;
1981 int format, lpno, is_extd;
1982
1983 if (eof || eob)
1984 return -1;
1985
1986 lpno = index_to_linux(pno, z);
1987
1988 if (interactive) {
1989 if (pct == 0 && (show_extended || pno == 0))
1990 warn("\n");
1991 warn("%s:", partname(dev, lpno, 10));
1992 }
1993
1994 /* read input line - skip blank lines when reading from a file */
1995 do {
1996 fno = read_stdin(fields, line, SIZE(fields), SIZE(line));
1997 } while(fno == RD_CMD || (fno == 0 && !interactive));
1998 if (fno == RD_EOF) {
1999 return -1;
2000 } else if (fno > 10 && *(fields[10]) != 0) {
2001 do_warn(_("too many input fields\n"));
2002 return 0;
2003 }
2004
2005 if (fno == 1 && !strcmp(fields[0], ".")) {
2006 eob = 1;
2007 return -1;
2008 }
2009
2010 /* use specified format, but round to cylinders if F_MEGABYTE specified */
2011 format = 0;
2012 if (B.cylindersize && specified_format == F_MEGABYTE)
2013 format = F_CYLINDER;
2014
2015 orig = (one_only ? &(oldp.partitions[pno]) : 0);
2016
2017 p = zero_part_desc;
2018 p.ep = ep;
2019
2020 /* first read the type - we need to know whether it is extended */
2021 /* stop reading when input blank (defaults) and all is full */
2022 is_extd = 0;
2023 if (fno == 0) { /* empty line */
2024 if (orig && is_extended(orig->p.sys_type))
2025 is_extd = 1;
2026 ff = first_free(pno, is_extd, ep, format, 0, z);
2027 ml = max_length(pno, is_extd, ep, format, ff, z);
2028 if (ml == 0 && is_extd == 0) {
2029 is_extd = 1;
2030 ff = first_free(pno, is_extd, ep, format, 0, z);
2031 ml = max_length(pno, is_extd, ep, format, ff, z);
2032 }
2033 if (ml == 0 && pno >= 4) {
2034 /* no free blocks left - don't read any further */
2035 warn(_("No room for more\n"));
2036 return -1;
2037 }
2038 }
2039 if (fno < 3 || !*(fields[2]))
2040 ul = orig ? orig->p.sys_type :
2041 (is_extd || (pno > 3 && pct == 1 && show_extended))
2042 ? EXTENDED_PARTITION : LINUX_NATIVE;
2043 else if (!strcmp(fields[2], "L"))
2044 ul = LINUX_NATIVE;
2045 else if (!strcmp(fields[2], "S"))
2046 ul = LINUX_SWAP;
2047 else if (!strcmp(fields[2], "E"))
2048 ul = EXTENDED_PARTITION;
2049 else if (!strcmp(fields[2], "X"))
2050 ul = LINUX_EXTENDED;
2051 else if (get_ul(fields[2], &ul, LINUX_NATIVE, 16))
2052 return 0;
2053 if (ul > 255) {
2054 warn(_("Illegal type\n"));
2055 return 0;
2056 }
2057 p.p.sys_type = ul;
2058 is_extd = is_extended(ul);
2059
2060 /* find start */
2061 ff = first_free(pno, is_extd, ep, format, 0, z);
2062 ff1 = ff * unitsize(format);
2063 def = orig ? orig->start : (pno > 4 && pct > 1) ? 0 : ff1;
2064 if (fno < 1 || !*(fields[0]))
2065 p.start = def;
2066 else {
2067 if (get_ul(fields[0], &ul, def / unitsize(0), 0))
2068 return 0;
2069 p.start = ul * unitsize(0);
2070 p.start -= (p.start % unitsize(format));
2071 }
2072
2073 /* find length */
2074 ml = max_length(pno, is_extd, ep, format, p.start / unitsize(format), z);
2075 ml1 = ml * unitsize(format);
2076 def = orig ? orig->size : (pno > 4 && pct > 1) ? 0 : ml1;
2077 if (fno < 2 || !*(fields[1]))
2078 p.size = def;
2079 else {
2080 if (get_ul(fields[1], &ul, def / unitsize(0), 0))
2081 return 0;
2082 p.size = ul * unitsize(0) + unitsize(format) - 1;
2083 p.size -= (p.size % unitsize(format));
2084 }
2085 if (p.size > ml1) {
2086 warn(_("Warning: given size (%lu) exceeds max allowable size (%lu)\n"),
2087 (p.size + unitsize(0) - 1) / unitsize(0), ml1 / unitsize(0));
2088 if (!force)
2089 return 0;
2090 }
2091 if (p.size == 0 && pno >= 4 && (fno < 2 || !*(fields[1]))) {
2092 warn(_("Warning: empty partition\n"));
2093 if (!force)
2094 return 0;
2095 }
2096 p.p.nr_sects = p.size;
2097
2098 if (p.size == 0 && !orig) {
2099 if (fno < 1 || !*(fields[0]))
2100 p.start = 0;
2101 if (fno < 3 || !*(fields[2]))
2102 p.p.sys_type = EMPTY_PARTITION;
2103 }
2104
2105 if (p.start < ff1 && p.size > 0) {
2106 warn(_("Warning: bad partition start (earliest %lu)\n"),
2107 (ff1 + unitsize(0) - 1) / unitsize(0));
2108 if (!force)
2109 return 0;
2110 }
2111
2112 if (fno < 4 || !*(fields[3]))
2113 ul = (orig ? orig->p.bootable : 0);
2114 else if (!strcmp(fields[3], "-"))
2115 ul = 0;
2116 else if (!strcmp(fields[3], "*") || !strcmp(fields[3], "+"))
2117 ul = 0x80;
2118 else {
2119 warn(_("unrecognized bootable flag - choose - or *\n"));
2120 return 0;
2121 }
2122 p.p.bootable = ul;
2123
2124 if (ep && ep->p.sys_type == EMPTY_PARTITION) {
2125 if (!build_surrounding_extended(&p, ep, z))
2126 return 0;
2127 } else
2128 if (!compute_start_sect(&p, ep))
2129 return 0;
2130
2131 { longchs aa = chs_to_longchs(p.p.begin_chs), bb;
2132
2133 if (fno < 5) {
2134 bb = aa;
2135 } else if (fno < 7) {
2136 warn(_("partial c,h,s specification?\n"));
2137 return 0;
2138 } else if (get_ul(fields[4], &bb.c, aa.c, 0) ||
2139 get_ul(fields[5], &bb.h, aa.h, 0) ||
2140 get_ul(fields[6], &bb.s, aa.s, 0))
2141 return 0;
2142 p.p.begin_chs = longchs_to_chs(bb,B);
2143 }
2144 { longchs aa = chs_to_longchs(p.p.end_chs), bb;
2145
2146 if (fno < 8) {
2147 bb = aa;
2148 } else if (fno < 10) {
2149 warn(_("partial c,h,s specification?\n"));
2150 return 0;
2151 } else if (get_ul(fields[7], &bb.c, aa.c, 0) ||
2152 get_ul(fields[8], &bb.h, aa.h, 0) ||
2153 get_ul(fields[9], &bb.s, aa.s, 0))
2154 return 0;
2155 p.p.end_chs = longchs_to_chs(bb, B);
2156 }
2157
2158 if (pno > 3 && p.size && show_extended && p.p.sys_type != EMPTY_PARTITION
2159 && (is_extended(p.p.sys_type) != (pct == 1))) {
2160 warn(_("Extended partition not where expected\n"));
2161 if (!force)
2162 return 0;
2163 }
2164
2165 z->partitions[pno] = p;
2166 if (pno >= z->partno)
2167 z->partno += 4; /* reqd for out_partition() */
2168
2169 if (interactive)
2170 out_partition(dev, 0, &(z->partitions[pno]), z, B);
2171
2172 return 1;
2173 }
2174
2175 /* ep either points to the extended partition to contain this one,
2176 or to the empty partition that may become extended or is 0 */
2177 static int
2178 read_partition(char *dev, int interactive, int pno, struct part_desc *ep,
2179 struct disk_desc *z) {
2180 struct part_desc *p = &(z->partitions[pno]);
2181 int i;
2182
2183 if (one_only) {
2184 *p = oldp.partitions[pno];
2185 if (one_only_pno != pno)
2186 goto ret;
2187 } else if (!show_extended && pno > 4 && pno%4)
2188 goto ret;
2189
2190 while (!(i = read_line(pno, ep, dev, interactive, z)))
2191 if (!interactive)
2192 fatal(_("bad input\n"));
2193 if (i < 0) {
2194 p->ep = ep;
2195 return 0;
2196 }
2197
2198 ret:
2199 p->ep = ep;
2200 if (pno >= z->partno)
2201 z->partno += 4;
2202 return 1;
2203 }
2204
2205 static void
2206 read_partition_chain(char *dev, int interactive, struct part_desc *ep,
2207 struct disk_desc *z) {
2208 int i, base;
2209
2210 eob = 0;
2211 while (1) {
2212 base = z->partno;
2213 if (base+4 > SIZE(z->partitions)) {
2214 do_warn(_("too many partitions\n"));
2215 break;
2216 }
2217 for (i=0; i<4; i++)
2218 if (!read_partition(dev, interactive, base+i, ep, z))
2219 return;
2220 for (i=0; i<4; i++) {
2221 ep = &(z->partitions[base+i]);
2222 if (is_extended(ep->p.sys_type) && ep->size)
2223 break;
2224 }
2225 if (i == 4) {
2226 /* nothing found - maybe an empty partition is going
2227 to be extended */
2228 if (one_only || show_extended)
2229 break;
2230 ep = &(z->partitions[base+1]);
2231 if (ep->size || ep->p.sys_type != EMPTY_PARTITION)
2232 break;
2233 }
2234 }
2235 }
2236
2237 static void
2238 read_input(char *dev, int interactive, struct disk_desc *z) {
2239 int i;
2240 struct part_desc *partitions = &(z->partitions[0]), *ep;
2241
2242 for (i=0; i < SIZE(z->partitions); i++)
2243 partitions[i] = zero_part_desc;
2244 z->partno = 0;
2245
2246 if (interactive)
2247 warn(_("Input in the following format; absent fields get a default value.\n"
2248 "<start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s>\n"
2249 "Usually you only need to specify <start> and <size> (and perhaps <type>).\n"));
2250 eof = 0;
2251
2252 for (i=0; i<4; i++)
2253 read_partition(dev, interactive, i, 0, z);
2254 for (i=0; i<4; i++) {
2255 ep = partitions+i;
2256 if (is_extended(ep->p.sys_type) && ep->size)
2257 read_partition_chain(dev, interactive, ep, z);
2258 }
2259 add_sector_and_offset(z);
2260 }
2261
2262 /*
2263 * G. The command line
2264 */
2265
2266 static void version(void) {
2267 printf("%s %s %s (aeb@cwi.nl, %s)\n", PROGNAME, _("version"), VERSION, DATE);
2268 }
2269
2270 static void
2271 usage(void) {
2272 version();
2273 printf(_("Usage: %s [options] device ...\n"), PROGNAME);
2274 puts (_("device: something like /dev/hda or /dev/sda"));
2275 puts (_("useful options:"));
2276 puts (_(" -s [or --show-size]: list size of a partition"));
2277 puts (_(" -c [or --id]: print or change partition Id"));
2278 puts (_(" -l [or --list]: list partitions of each device"));
2279 puts (_(" -d [or --dump]: idem, but in a format suitable for later input"));
2280 puts (_(" -i [or --increment]: number cylinders etc. from 1 instead of from 0"));
2281 puts (_(" -uS, -uB, -uC, -uM: accept/report in units of sectors/blocks/cylinders/MB"));
2282 puts (_(" -T [or --list-types]:list the known partition types"));
2283 puts (_(" -D [or --DOS]: for DOS-compatibility: waste a little space"));
2284 puts (_(" -R [or --re-read]: make kernel reread partition table"));
2285 puts (_(" -N# : change only the partition with number #"));
2286 puts (_(" -n : do not actually write to disk"));
2287 puts (_(" -O file : save the sectors that will be overwritten to file"));
2288 puts (_(" -I file : restore these sectors again"));
2289 puts (_(" -v [or --version]: print version"));
2290 puts (_(" -? [or --help]: print this message"));
2291 puts (_("dangerous options:"));
2292 puts (_(" -g [or --show-geometry]: print the kernel's idea of the geometry"));
2293 puts (_(" -x [or --show-extended]: also list extended partitions on output\n"
2294 " or expect descriptors for them on input"));
2295 puts (_(" -L [or --Linux]: do not complain about things irrelevant for Linux"));
2296 puts (_(" -q [or --quiet]: suppress warning messages"));
2297 puts (_(" You can override the detected geometry using:"));
2298 puts (_(" -C# [or --cylinders #]:set the number of cylinders to use"));
2299 puts (_(" -H# [or --heads #]: set the number of heads to use"));
2300 puts (_(" -S# [or --sectors #]: set the number of sectors to use"));
2301 puts (_("You can disable all consistency checking with:"));
2302 puts (_(" -f [or --force]: do what I say, even if it is stupid"));
2303 exit(1);
2304 }
2305
2306 static void
2307 activate_usage(char *progn) {
2308 puts (_("Usage:"));
2309 printf(_("%s device list active partitions on device\n"), progn);
2310 printf(_("%s device n1 n2 ... activate partitions n1 ..., inactivate the rest\n"), progn);
2311 printf(_("%s -An device activate partition n, inactivate the other ones\n"), PROGNAME);
2312 exit(1);
2313 }
2314
2315 static void
2316 unhide_usage(char *progn) {
2317 exit(1);
2318 }
2319
2320 static char short_opts[] = "cdfgilnqsu:vx?1A::C:DH:I:LN:O:RS:TU::V";
2321
2322 #define PRINT_ID 0400
2323 #define CHANGE_ID 01000
2324
2325 static const struct option long_opts[] = {
2326 { "change-id", no_argument, NULL, 'c' + CHANGE_ID },
2327 { "print-id", no_argument, NULL, 'c' + PRINT_ID },
2328 { "id", no_argument, NULL, 'c' },
2329 { "dump", no_argument, NULL, 'd' },
2330 { "force", no_argument, NULL, 'f' },
2331 { "show-geometry", no_argument, NULL, 'g' },
2332 { "increment", no_argument, NULL, 'i' },
2333 { "list", no_argument, NULL, 'l' },
2334 { "quiet", no_argument, NULL, 'q' },
2335 { "show-size", no_argument, NULL, 's' },
2336 { "unit", required_argument, NULL, 'u' },
2337 { "version", no_argument, NULL, 'v' },
2338 { "show-extended", no_argument, NULL, 'x' },
2339 { "help", no_argument, NULL, '?' },
2340 { "one-only", no_argument, NULL, '1' },
2341 { "cylinders", required_argument, NULL, 'C' },
2342 { "heads", required_argument, NULL, 'H' },
2343 { "sectors", required_argument, NULL, 'S' },
2344 { "activate", optional_argument, NULL, 'A' },
2345 { "DOS", no_argument, NULL, 'D' },
2346 { "DOS-extended", no_argument, NULL, 'E' },
2347 { "Linux", no_argument, NULL, 'L' },
2348 { "re-read", no_argument, NULL, 'R' },
2349 { "list-types", no_argument, NULL, 'T' },
2350 { "unhide", optional_argument, NULL, 'U' },
2351 { "no-reread", no_argument, NULL, 160 },
2352 { "IBM", no_argument, NULL, 161 },
2353 { "leave-last", no_argument, NULL, 161 },
2354 /* undocumented flags - not all completely implemented */
2355 { "in-order", no_argument, NULL, 128 },
2356 { "not-in-order", no_argument, NULL, 129 },
2357 { "inside-outer", no_argument, NULL, 130 },
2358 { "not-inside-outer", no_argument, NULL, 131 },
2359 { "nested", no_argument, NULL, 132 },
2360 { "chained", no_argument, NULL, 133 },
2361 { "onesector", no_argument, NULL, 134 },
2362 { NULL, 0, NULL, 0 }
2363 };
2364
2365 static int
2366 is_ide_cdrom_or_tape(char *device) {
2367 FILE *procf;
2368 char buf[100];
2369 struct stat statbuf;
2370 int is_ide = 0;
2371
2372 /* No device was given explicitly, and we are trying some
2373 likely things. But opening /dev/hdc may produce errors like
2374 "hdc: tray open or drive not ready"
2375 if it happens to be a CD-ROM drive. It even happens that
2376 the process hangs on the attempt to read a music CD.
2377 So try to be careful. This only works since 2.1.73. */
2378
2379 if (strncmp("/dev/hd", device, 7))
2380 return 0;
2381
2382 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2383 procf = fopen(buf, "r");
2384 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2385 is_ide = (!strncmp(buf, "cdrom", 5) ||
2386 !strncmp(buf, "tape", 4));
2387 else
2388 /* Now when this proc file does not exist, skip the
2389 device when it is read-only. */
2390 if (stat(device, &statbuf) == 0)
2391 is_ide = ((statbuf.st_mode & 0222) == 0);
2392
2393 if (procf)
2394 fclose(procf);
2395 return is_ide;
2396 }
2397
2398 static int
2399 is_probably_full_disk(char *name) {
2400 struct hd_geometry geometry;
2401 int fd, i = 0;
2402
2403 fd = open(name, O_RDONLY);
2404 if (fd >= 0) {
2405 i = ioctl(fd, HDIO_GETGEO, &geometry);
2406 close(fd);
2407 }
2408 return (fd >= 0 && i == 0 && geometry.start == 0);
2409 }
2410
2411 #define PROC_PARTITIONS "/proc/partitions"
2412 static FILE *procf = NULL;
2413
2414 static void
2415 openproc(void) {
2416 procf = fopen(PROC_PARTITIONS, "r");
2417 if (procf == NULL)
2418 fprintf(stderr, _("cannot open %s\n"), PROC_PARTITIONS);
2419 }
2420
2421 static char *
2422 nextproc(void) {
2423 static char devname[120];
2424 char line[100], ptname[100];
2425 int ma, mi, sz;
2426
2427 if (procf == NULL)
2428 return NULL;
2429 while (fgets(line, sizeof(line), procf) != NULL) {
2430 if (sscanf (line, " %d %d %d %[^\n ]",
2431 &ma, &mi, &sz, ptname) != 4)
2432 continue;
2433 snprintf(devname, sizeof(devname), "/dev/%s", ptname);
2434 if (!is_probably_full_disk(devname))
2435 continue;
2436 return devname;
2437 }
2438
2439 fclose(procf);
2440 procf = NULL;
2441 return NULL;
2442 }
2443
2444 static void do_list(char *dev, int silent);
2445 static void do_size(char *dev, int silent);
2446 static void do_geom(char *dev, int silent);
2447 static void do_fdisk(char *dev);
2448 static void do_reread(char *dev);
2449 static void do_change_id(char *dev, char *part, char *id);
2450 static void do_unhide(char **av, int ac, char *arg);
2451 static void do_activate(char **av, int ac, char *arg);
2452
2453 unsigned long long total_size;
2454
2455 int
2456 main(int argc, char **argv) {
2457 char *progn;
2458 int c;
2459 char *dev;
2460 int opt_size = 0;
2461 int opt_out_geom = 0;
2462 int opt_reread = 0;
2463 int activate = 0;
2464 int do_id = 0;
2465 int unhide = 0;
2466 int fdisk = 0;
2467 char *activatearg = 0;
2468 char *unhidearg = 0;
2469
2470 setlocale(LC_ALL, "");
2471 bindtextdomain(PACKAGE, LOCALEDIR);
2472 textdomain(PACKAGE);
2473
2474 if (argc < 1)
2475 fatal(_("no command?\n"));
2476 if ((progn = rindex(argv[0], '/')) == NULL)
2477 progn = argv[0];
2478 else
2479 progn++;
2480 if (!strcmp(progn, "activate"))
2481 activate = 1; /* equivalent to `sfdisk -A' */
2482 #if 0 /* not important enough to deserve a name */
2483 else if (!strcmp(progn, "unhide"))
2484 unhide = 1; /* equivalent to `sfdisk -U' */
2485 #endif
2486 else
2487 fdisk = 1;
2488
2489 while ((c = getopt_long (argc, argv, short_opts, long_opts, NULL)) != -1) {
2490 switch (c) {
2491 case 'f':
2492 force = 1; break; /* does not imply quiet */
2493 case 'g':
2494 opt_out_geom = 1; break;
2495 case 'i':
2496 increment = 1; break;
2497 case 'c':
2498 case 'c' + PRINT_ID:
2499 case 'c' + CHANGE_ID:
2500 do_id = c; break;
2501 case 'd':
2502 dump = 1; /* fall through */
2503 case 'l':
2504 opt_list = 1; break;
2505 case 'n':
2506 no_write = 1; break;
2507 case 'q':
2508 quiet = 1; break;
2509 case 's':
2510 opt_size = 1; break;
2511 case 'u':
2512 set_format(*optarg); break;
2513 case 'v':
2514 version();
2515 exit(0);
2516 case 'x':
2517 show_extended = 1; break;
2518 case 'A':
2519 activatearg = optarg;
2520 activate = 1; break;
2521 case 'C':
2522 U.cylinders = atoi(optarg); break;
2523 case 'D':
2524 DOS = 1; break;
2525 case 'E':
2526 DOS_extended = 1; break;
2527 case 'H':
2528 U.heads = atoi(optarg); break;
2529 case 'L':
2530 Linux = 1; break;
2531 case 'N':
2532 one_only = atoi(optarg); break;
2533 case 'I':
2534 restore_sector_file = optarg; break;
2535 case 'O':
2536 save_sector_file = optarg; break;
2537 case 'R':
2538 opt_reread = 1; break;
2539 case 'S':
2540 U.sectors = atoi(optarg); break;
2541 case 'T':
2542 list_types();
2543 exit(0);
2544 case 'U':
2545 unhidearg = optarg;
2546 unhide = 1; break;
2547 case 'V':
2548 verify = 1; break;
2549 case '?':
2550 default:
2551 usage(); break;
2552
2553 /* undocumented flags */
2554 case 128:
2555 partitions_in_order = 1; break;
2556 case 129:
2557 partitions_in_order = 0; break;
2558 case 130:
2559 all_logicals_inside_outermost_extended = 1; break;
2560 case 131:
2561 all_logicals_inside_outermost_extended = 0; break;
2562 case 132:
2563 boxes = NESTED; break;
2564 case 133:
2565 boxes = CHAINED; break;
2566 case 134:
2567 boxes = ONESECTOR; break;
2568
2569 /* more flags */
2570 case 160:
2571 no_reread = 1; break;
2572 case 161:
2573 leave_last = 1; break;
2574 }
2575 }
2576
2577 if (optind == argc && (opt_list || opt_out_geom || opt_size || verify)) {
2578 /* try all known devices */
2579 total_size = 0;
2580 openproc();
2581 while ((dev = nextproc()) != NULL) {
2582 if (is_ide_cdrom_or_tape(dev))
2583 continue;
2584 if (opt_out_geom)
2585 do_geom(dev, 1);
2586 if (opt_size)
2587 do_size(dev, 1);
2588 if (opt_list || verify)
2589 do_list(dev, 1);
2590 }
2591
2592 if (opt_size)
2593 printf(_("total: %llu blocks\n"), total_size);
2594
2595 exit(exit_status);
2596 }
2597
2598 if (optind == argc) {
2599 if (activate)
2600 activate_usage(fdisk ? "sfdisk -A" : progn);
2601 else if (unhide)
2602 unhide_usage(fdisk ? "sfdisk -U" : progn);
2603 else
2604 usage();
2605 }
2606
2607 if (opt_list || opt_out_geom || opt_size || verify) {
2608 while (optind < argc) {
2609 if (opt_out_geom)
2610 do_geom(argv[optind], 0);
2611 if (opt_size)
2612 do_size(argv[optind], 0);
2613 if (opt_list || verify)
2614 do_list(argv[optind], 0);
2615 optind++;
2616 }
2617 exit(exit_status);
2618 }
2619
2620 if (activate) {
2621 do_activate(argv+optind, argc-optind, activatearg);
2622 exit(exit_status);
2623 }
2624 if (unhide) {
2625 do_unhide(argv+optind, argc-optind, unhidearg);
2626 exit(exit_status);
2627 }
2628 if (do_id) {
2629 if ((do_id & PRINT_ID) != 0 && optind != argc-2)
2630 fatal(_("usage: sfdisk --print-id device partition-number\n"));
2631 else if ((do_id & CHANGE_ID) != 0 && optind != argc-3)
2632 fatal(_("usage: sfdisk --change-id device partition-number Id\n"));
2633 else if (optind != argc-3 && optind != argc-2)
2634 fatal(_("usage: sfdisk --id device partition-number [Id]\n"));
2635 do_change_id(argv[optind], argv[optind+1],
2636 (optind == argc-2) ? 0 : argv[optind+2]);
2637 exit(exit_status);
2638 }
2639
2640 if (optind != argc-1)
2641 fatal(_("can specify only one device (except with -l or -s)\n"));
2642 dev = argv[optind];
2643
2644 if (opt_reread)
2645 do_reread(dev);
2646 else if (restore_sector_file)
2647 restore_sectors(dev);
2648 else
2649 do_fdisk(dev);
2650
2651 return 0;
2652 }
2653
2654 /*
2655 * H. Listing the current situation
2656 */
2657
2658 static int
2659 my_open (char *dev, int rw, int silent) {
2660 int fd, mode;
2661
2662 mode = (rw ? O_RDWR : O_RDONLY);
2663 fd = open(dev, mode);
2664 if (fd < 0 && !silent) {
2665 perror(dev);
2666 if (rw)
2667 fatal(_("cannot open %s read-write\n"), dev);
2668 else
2669 fatal(_("cannot open %s for reading\n"), dev);
2670 }
2671 return fd;
2672 }
2673
2674 static void
2675 do_list (char *dev, int silent) {
2676 int fd;
2677 struct disk_desc *z;
2678
2679 fd = my_open(dev, 0, silent);
2680 if (fd < 0)
2681 return;
2682
2683 z = &oldp;
2684
2685 free_sectors();
2686 get_cylindersize(dev, fd, dump ? 1 : opt_list ? 0 : 1);
2687 get_partitions(dev, fd, z);
2688
2689 if (opt_list)
2690 out_partitions(dev, z);
2691
2692 if (verify) {
2693 if (partitions_ok(z))
2694 warn(_("%s: OK\n"), dev);
2695 else
2696 exit_status = 1;
2697 }
2698 }
2699
2700 static void
2701 do_geom (char *dev, int silent) {
2702 int fd;
2703 struct geometry R;
2704
2705 fd = my_open(dev, 0, silent);
2706 if (fd < 0)
2707 return;
2708
2709 R = get_geometry(dev, fd, silent);
2710 if (R.cylinders)
2711 printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"),
2712 dev, R.cylinders, R.heads, R.sectors);
2713 }
2714
2715 /* for compatibility with earlier fdisk: provide option -s */
2716 static void
2717 do_size (char *dev, int silent) {
2718 int fd;
2719 unsigned long long size;
2720
2721 fd = my_open(dev, 0, silent);
2722 if (fd < 0)
2723 return;
2724
2725 if (disksize(fd, &size)) {
2726 if (!silent) {
2727 perror(dev);
2728 fatal(_("Cannot get size of %s\n"), dev);
2729 }
2730 return;
2731 }
2732
2733 size /= 2; /* convert sectors to blocks */
2734
2735 /* a CDROM drive without mounted CD yields MAXINT */
2736 if (silent && size == ((1<<30)-1))
2737 return;
2738
2739 if (silent)
2740 printf("%s: %9llu\n", dev, size);
2741 else
2742 printf("%llu\n", size);
2743
2744 total_size += size;
2745 }
2746
2747 /*
2748 * Activate: usually one wants to have a single primary partition
2749 * to be active. OS/2 fdisk makes non-bootable logical partitions
2750 * active - I don't know what that means to OS/2 Boot Manager.
2751 *
2752 * Call: activate /dev/hda 2 5 7 make these partitions active
2753 * and the remaining ones inactive
2754 * Or: sfdisk -A /dev/hda 2 5 7
2755 *
2756 * If only a single partition must be active, one may also use the form
2757 * sfdisk -A2 /dev/hda
2758 *
2759 * With "activate /dev/hda" or "sfdisk -A /dev/hda" the active partitions
2760 * are listed but not changed. To get zero active partitions, use
2761 * "activate /dev/hda none" or "sfdisk -A /dev/hda none".
2762 * Use something like `echo ",,,*" | sfdisk -N2 /dev/hda' to only make
2763 * /dev/hda2 active, without changing other partitions.
2764 *
2765 * A warning will be given if after the change not precisely one primary
2766 * partition is active.
2767 *
2768 * The present syntax was chosen to be (somewhat) compatible with the
2769 * activate from the LILO package.
2770 */
2771 static void
2772 set_active (struct disk_desc *z, char *pnam) {
2773 int pno;
2774
2775 pno = asc_to_index(pnam, z);
2776 if (z->partitions[pno].ptype == DOS_TYPE)
2777 z->partitions[pno].p.bootable = 0x80;
2778 }
2779
2780 static void
2781 do_activate (char **av, int ac, char *arg) {
2782 char *dev = av[0];
2783 int fd;
2784 int rw, i, pno, lpno;
2785 struct disk_desc *z;
2786
2787 z = &oldp;
2788
2789 rw = (!no_write && (arg || ac > 1));
2790 fd = my_open(dev, rw, 0);
2791
2792 free_sectors();
2793 get_cylindersize(dev, fd, 1);
2794 get_partitions(dev, fd, z);
2795
2796 if (!arg && ac == 1) {
2797 /* list active partitions */
2798 for (pno=0; pno < z->partno; pno++) {
2799 if (z->partitions[pno].p.bootable) {
2800 lpno = index_to_linux(pno, z);
2801 if (pno == linux_to_index(lpno, z))
2802 printf("%s\n", partname(dev, lpno, 0));
2803 else
2804 printf("%s#%d\n", dev, pno);
2805 if (z->partitions[pno].p.bootable != 0x80)
2806 warn(_("bad active byte: 0x%x instead of 0x80\n"),
2807 z->partitions[pno].p.bootable);
2808 }
2809 }
2810 } else {
2811 /* clear `active byte' everywhere */
2812 for (pno=0; pno < z->partno; pno++)
2813 if (z->partitions[pno].ptype == DOS_TYPE)
2814 z->partitions[pno].p.bootable = 0;
2815
2816 /* then set where desired */
2817 if (ac == 1)
2818 set_active(z, arg);
2819 else for(i=1; i<ac; i++)
2820 set_active(z, av[i]);
2821
2822 /* then write to disk */
2823 if (write_partitions(dev, fd, z))
2824 warn(_("Done\n\n"));
2825 else
2826 exit_status = 1;
2827 }
2828 i = 0;
2829 for (pno=0; pno < z->partno && pno < 4; pno++)
2830 if (z->partitions[pno].p.bootable)
2831 i++;
2832 if (i != 1)
2833 warn(_("You have %d active primary partitions. This does not matter for LILO,\n"
2834 "but the DOS MBR will only boot a disk with 1 active partition.\n"), i);
2835 }
2836
2837 static void
2838 set_unhidden (struct disk_desc *z, char *pnam) {
2839 int pno;
2840 unsigned char id;
2841
2842 pno = asc_to_index(pnam, z);
2843 id = z->partitions[pno].p.sys_type;
2844 if (id == 0x11 || id == 0x14 || id == 0x16 || id == 0x17)
2845 id -= 0x10;
2846 else
2847 fatal(_("partition %s has id %x and is not hidden\n"), pnam, id);
2848 z->partitions[pno].p.sys_type = id;
2849 }
2850
2851 /*
2852 * maybe remove and make part of --change-id
2853 */
2854 static void
2855 do_unhide (char **av, int ac, char *arg) {
2856 char *dev = av[0];
2857 int fd, rw, i;
2858 struct disk_desc *z;
2859
2860 z = &oldp;
2861
2862 rw = !no_write;
2863 fd = my_open(dev, rw, 0);
2864
2865 free_sectors();
2866 get_cylindersize(dev, fd, 1);
2867 get_partitions(dev, fd, z);
2868
2869 /* unhide where desired */
2870 if (ac == 1)
2871 set_unhidden(z, arg);
2872 else for(i=1; i<ac; i++)
2873 set_unhidden(z, av[i]);
2874
2875 /* then write to disk */
2876 if (write_partitions(dev, fd, z))
2877 warn(_("Done\n\n"));
2878 else
2879 exit_status = 1;
2880 }
2881
2882 static void
2883 do_change_id(char *dev, char *pnam, char *id) {
2884 int fd, rw, pno;
2885 struct disk_desc *z;
2886 unsigned long i;
2887
2888 z = &oldp;
2889
2890 rw = !no_write;
2891 fd = my_open(dev, rw, 0);
2892
2893 free_sectors();
2894 get_cylindersize(dev, fd, 1);
2895 get_partitions(dev, fd, z);
2896
2897 pno = asc_to_index(pnam, z);
2898 if (id == 0) {
2899 printf("%x\n", z->partitions[pno].p.sys_type);
2900 return;
2901 }
2902 i = strtoul(id, NULL, 16);
2903 if (i > 255)
2904 fatal(_("Bad Id %lx\n"), i);
2905 z->partitions[pno].p.sys_type = i;
2906
2907 if (write_partitions(dev, fd, z))
2908 warn(_("Done\n\n"));
2909 else
2910 exit_status = 1;
2911 }
2912
2913 static void
2914 do_reread(char *dev) {
2915 int fd;
2916
2917 fd = my_open(dev, 0, 0);
2918 if (reread_ioctl(fd))
2919 do_warn(_("This disk is currently in use.\n"));
2920 }
2921
2922 /*
2923 * I. Writing the new situation
2924 */
2925
2926 static void
2927 do_fdisk(char *dev){
2928 int fd;
2929 int c, answer;
2930 struct stat statbuf;
2931 int interactive = isatty(0);
2932 struct disk_desc *z;
2933
2934 if (stat(dev, &statbuf) < 0) {
2935 perror(dev);
2936 fatal(_("Fatal error: cannot find %s\n"), dev);
2937 }
2938 if (!S_ISBLK(statbuf.st_mode)) {
2939 warn(_("Warning: %s is not a block device\n"), dev);
2940 no_reread = 1;
2941 }
2942 fd = my_open(dev, !no_write, 0);
2943
2944 if (!no_write && !no_reread) {
2945 warn(_("Checking that no-one is using this disk right now ...\n"));
2946 if (reread_ioctl(fd)) {
2947 do_warn(_("\nThis disk is currently in use - repartitioning is probably a bad idea.\n"
2948 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
2949 "Use the --no-reread flag to suppress this check.\n"));
2950 if (!force) {
2951 do_warn(_("Use the --force flag to overrule all checks.\n"));
2952 exit(1);
2953 }
2954 } else
2955 warn(_("OK\n"));
2956 }
2957
2958 z = &oldp;
2959
2960 free_sectors();
2961 get_cylindersize(dev, fd, 0);
2962 get_partitions(dev, fd, z);
2963
2964 printf(_("Old situation:\n"));
2965 out_partitions(dev, z);
2966
2967 if (one_only && (one_only_pno = linux_to_index(one_only, z)) < 0)
2968 fatal(_("Partition %d does not exist, cannot change it\n"), one_only);
2969
2970 z = &newp;
2971
2972 while(1) {
2973
2974 read_input(dev, interactive, z);
2975
2976 printf(_("New situation:\n"));
2977 out_partitions(dev, z);
2978
2979 if (!partitions_ok(z) && !force) {
2980 if (!interactive)
2981 fatal(_("I don't like these partitions - nothing changed.\n"
2982 "(If you really want this, use the --force option.)\n"));
2983 else
2984 do_warn(_("I don't like this - probably you should answer No\n"));
2985 }
2986 ask:
2987 if (interactive) {
2988 if (no_write)
2989 printf(_("Are you satisfied with this? [ynq] "));
2990 else
2991 printf(_("Do you want to write this to disk? [ynq] "));
2992 answer = c = getchar();
2993 while (c != '\n' && c != EOF)
2994 c = getchar();
2995 if (c == EOF)
2996 printf(_("\nsfdisk: premature end of input\n"));
2997 if (c == EOF || answer == 'q' || answer == 'Q') {
2998 fatal(_("Quitting - nothing changed\n"));
2999 } else if (answer == 'n' || answer == 'N') {
3000 continue;
3001 } else if (answer == 'y' || answer == 'Y') {
3002 break;
3003 } else {
3004 printf(_("Please answer one of y,n,q\n"));
3005 goto ask;
3006 }
3007 } else
3008 break;
3009 }
3010
3011 if (write_partitions(dev, fd, z))
3012 printf(_("Successfully wrote the new partition table\n\n"));
3013 else
3014 exit_status = 1;
3015
3016 reread_disk_partition(dev, fd);
3017
3018 warn(_("If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)\n"
3019 "to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1\n"
3020 "(See fdisk(8).)\n"));
3021
3022 sync(); /* superstition */
3023 sleep(3);
3024 exit(exit_status);
3025 }