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