]> git.ipfire.org Git - thirdparty/util-linux.git/blob - fdisk/fdisk.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / fdisk / fdisk.c
1 /* fdisk.c -- Partition table manipulator for Linux.
2 *
3 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
4 *
5 * This program is free software. You can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation: either version 1 or
8 * (at your option) any later version.
9 *
10 * For detailed old history, see older versions.
11 * Contributions before 2000 by faith@cs.unc.edu, Michael Bischoff,
12 * LeBlanc@mcc.ac.uk, martin@cs.unc.edu, leisner@sdsp.mc.xerox.com,
13 * esr@snark.thyrsus.com, aeb@cwi.nl, quinlan@yggdrasil.com,
14 * fasten@cs.bonn.edu, orschaer@cip.informatik.uni-erlangen.de,
15 * jj@sunsite.mff.cuni.cz, fasten@shw.com, ANeuper@GUUG.de,
16 * kgw@suse.de.
17 *
18 * Modified, Sun Feb 20 2000, kalium@gmx.de
19 * Added fix operation allowing to reorder primary/extended partition
20 * entries within the partition table. Some programs or OSes have
21 * problems using a partition table with entries not ordered
22 * according to their positions on disk.
23 * Munged this patch to also make it work for logical partitions.
24 * aeb, 2000-02-20.
25 *
26 * Wed Mar 1 14:34:53 EST 2000 David Huggins-Daines <dhuggins@linuxcare.com>
27 * Better support for OSF/1 disklabels on Alpha.
28 *
29 * 2000-04-06, Michal Jaegermann (michal@ellpspace.math.ualberta.ca)
30 * fixed and added some alpha stuff.
31 */
32
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <ctype.h>
40 #include <setjmp.h>
41 #include <errno.h>
42 #include <getopt.h>
43 #include <sys/stat.h>
44
45 #include <linux/hdreg.h> /* for HDIO_GETGEO */
46
47 #include "nls.h"
48 #include "common.h"
49 #include "fdisk.h"
50
51 #include "fdisksunlabel.h"
52 #include "fdisksgilabel.h"
53 #include "fdiskaixlabel.h"
54
55 #include "../defines.h"
56 #ifdef HAVE_blkpg_h
57 #include <linux/blkpg.h>
58 #endif
59
60 static void delete_partition(int i);
61
62 #define hex_val(c) ({ \
63 char _c = (c); \
64 isdigit(_c) ? _c - '0' : \
65 tolower(_c) + 10 - 'a'; \
66 })
67
68
69 #define LINE_LENGTH 80
70 #define pt_offset(b, n) ((struct partition *)((b) + 0x1be + \
71 (n) * sizeof(struct partition)))
72 #define sector(s) ((s) & 0x3f)
73 #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
74
75 #define hsc2sector(h,s,c) (sector(s) - 1 + sectors * \
76 ((h) + heads * cylinder(s,c)))
77 #define set_hsc(h,s,c,sector) { \
78 s = sector % sectors + 1; \
79 sector /= sectors; \
80 h = sector % heads; \
81 sector /= heads; \
82 c = sector & 0xff; \
83 s |= (sector >> 2) & 0xc0; \
84 }
85
86 /* A valid partition table sector ends in 0x55 0xaa */
87 static unsigned int
88 part_table_flag(char *b) {
89 return ((uint) b[510]) + (((uint) b[511]) << 8);
90 }
91
92 int
93 valid_part_table_flag(unsigned char *b) {
94 return (b[510] == 0x55 && b[511] == 0xaa);
95 }
96
97 static void
98 write_part_table_flag(char *b) {
99 b[510] = 0x55;
100 b[511] = 0xaa;
101 }
102
103 /* start_sect and nr_sects are stored little endian on all machines */
104 /* moreover, they are not aligned correctly */
105 static void
106 store4_little_endian(unsigned char *cp, unsigned int val) {
107 cp[0] = (val & 0xff);
108 cp[1] = ((val >> 8) & 0xff);
109 cp[2] = ((val >> 16) & 0xff);
110 cp[3] = ((val >> 24) & 0xff);
111 }
112
113 static unsigned int
114 read4_little_endian(unsigned char *cp) {
115 return (uint)(cp[0]) + ((uint)(cp[1]) << 8)
116 + ((uint)(cp[2]) << 16) + ((uint)(cp[3]) << 24);
117 }
118
119 static void
120 set_start_sect(struct partition *p, unsigned int start_sect) {
121 store4_little_endian(p->start4, start_sect);
122 }
123
124 unsigned int
125 get_start_sect(struct partition *p) {
126 return read4_little_endian(p->start4);
127 }
128
129 static void
130 set_nr_sects(struct partition *p, unsigned int nr_sects) {
131 store4_little_endian(p->size4, nr_sects);
132 }
133
134 unsigned int
135 get_nr_sects(struct partition *p) {
136 return read4_little_endian(p->size4);
137 }
138
139 /* normally O_RDWR, -l option gives O_RDONLY */
140 static int type_open = O_RDWR;
141
142 /*
143 * Raw disk label. For DOS-type partition tables the MBR,
144 * with descriptions of the primary partitions.
145 */
146 char MBRbuffer[MAX_SECTOR_SIZE];
147
148 /*
149 * per partition table entry data
150 *
151 * The four primary partitions have the same sectorbuffer (MBRbuffer)
152 * and have NULL ext_pointer.
153 * Each logical partition table entry has two pointers, one for the
154 * partition and one link to the next one.
155 */
156 struct pte {
157 struct partition *part_table; /* points into sectorbuffer */
158 struct partition *ext_pointer; /* points into sectorbuffer */
159 char changed; /* boolean */
160 uint offset; /* disk sector number */
161 char *sectorbuffer; /* disk sector contents */
162 } ptes[MAXIMUM_PARTS];
163
164 char *disk_device, /* must be specified */
165 *line_ptr, /* interactive input */
166 line_buffer[LINE_LENGTH];
167
168 int fd, /* the disk */
169 ext_index, /* the prime extended partition */
170 listing = 0, /* no aborts for fdisk -l */
171 nowarn = 0, /* no warnings for fdisk -l/-s */
172 dos_compatible_flag = ~0,
173 partitions = 4; /* maximum partition + 1 */
174
175 uint user_cylinders, user_heads, user_sectors;
176
177 uint heads,
178 sectors,
179 cylinders,
180 sector_size = DEFAULT_SECTOR_SIZE,
181 user_set_sector_size = 0,
182 sector_offset = 1,
183 units_per_sector = 1,
184 display_in_cyl_units = 1,
185 extended_offset = 0; /* offset of link pointers */
186
187 #define dos_label (!sun_label && !sgi_label && !aix_label && !osf_label)
188 int sun_label = 0; /* looking at sun disklabel */
189 int sgi_label = 0; /* looking at sgi disklabel */
190 int aix_label = 0; /* looking at aix disklabel */
191 int osf_label = 0; /* looking at osf disklabel */
192 jmp_buf listingbuf;
193
194 void fatal(enum failure why) {
195 char error[LINE_LENGTH],
196 *message = error;
197
198 if (listing) {
199 close(fd);
200 longjmp(listingbuf, 1);
201 }
202
203 switch (why) {
204 case usage: message = _(
205 "Usage: fdisk [-b SSZ] [-u] DISK Change partition table\n"
206 " fdisk -l [-b SSZ] [-u] DISK List partition table(s)\n"
207 " fdisk -s PARTITION Give partition size(s) in blocks\n"
208 " fdisk -v Give fdisk version\n"
209 "Here DISK is something like /dev/hdb or /dev/sda\n"
210 "and PARTITION is something like /dev/hda7\n"
211 "-u: give Start and End in sector (instead of cylinder) units\n"
212 "-b 2048: (for certain MO drives) use 2048-byte sectors\n");
213 break;
214 case usage2:
215 /* msg in cases where fdisk used to probe */
216 message = _(
217 "Usage: fdisk [-l] [-b SSZ] [-u] device\n"
218 "E.g.: fdisk /dev/hda (for the first IDE disk)\n"
219 " or: fdisk /dev/sdc (for the third SCSI disk)\n"
220 " or: fdisk /dev/eda (for the first PS/2 ESDI drive)\n"
221 " or: fdisk /dev/rd/c0d0 or: fdisk /dev/ida/c0d0 (for RAID devices)\n"
222 " ...\n");
223 break;
224 case unable_to_open:
225 sprintf(error, _("Unable to open %s\n"), disk_device);
226 break;
227 case unable_to_read:
228 sprintf(error, _("Unable to read %s\n"), disk_device);
229 break;
230 case unable_to_seek:
231 sprintf(error, _("Unable to seek on %s\n"),disk_device);
232 break;
233 case unable_to_write:
234 sprintf(error, _("Unable to write %s\n"), disk_device);
235 break;
236 case ioctl_error:
237 sprintf(error, _("BLKGETSIZE ioctl failed on %s\n"),
238 disk_device);
239 break;
240 case out_of_memory:
241 message = _("Unable to allocate any more memory\n");
242 break;
243 default: message = _("Fatal error\n");
244 }
245
246 fputc('\n', stderr);
247 fputs(message, stderr);
248 exit(1);
249 }
250
251 static void
252 seek_sector(int fd, uint secno) {
253 ext2_loff_t offset = (ext2_loff_t) secno * sector_size;
254 if (ext2_llseek(fd, offset, SEEK_SET) == (ext2_loff_t) -1)
255 fatal(unable_to_seek);
256 }
257
258 static void
259 read_sector(int fd, uint secno, char *buf) {
260 seek_sector(fd, secno);
261 if (read(fd, buf, sector_size) != sector_size)
262 fatal(unable_to_read);
263 }
264
265 static void
266 write_sector(int fd, uint secno, char *buf) {
267 seek_sector(fd, secno);
268 if (write(fd, buf, sector_size) != sector_size)
269 fatal(unable_to_write);
270 }
271
272 /* Allocate a buffer and read a partition table sector */
273 static void
274 read_pte(int fd, int pno, uint offset) {
275 struct pte *pe = &ptes[pno];
276
277 pe->offset = offset;
278 pe->sectorbuffer = (char *) malloc(sector_size);
279 if (!pe->sectorbuffer)
280 fatal(out_of_memory);
281 read_sector(fd, offset, pe->sectorbuffer);
282 pe->changed = 0;
283 pe->part_table = pe->ext_pointer = NULL;
284 }
285
286 static unsigned int
287 get_partition_start(struct pte *pe) {
288 return pe->offset + get_start_sect(pe->part_table);
289 }
290
291 struct partition *
292 get_part_table(int i) {
293 return ptes[i].part_table;
294 }
295
296 void
297 set_all_unchanged(void) {
298 int i;
299
300 for (i = 0; i < MAXIMUM_PARTS; i++)
301 ptes[i].changed = 0;
302 }
303
304 void
305 set_changed(int i) {
306 ptes[i].changed = 1;
307 }
308
309 static void
310 menu(void) {
311 if (sun_label) {
312 puts(_("Command action"));
313 puts(_(" a toggle a read only flag")); /* sun */
314 puts(_(" b edit bsd disklabel"));
315 puts(_(" c toggle the mountable flag")); /* sun */
316 puts(_(" d delete a partition"));
317 puts(_(" l list known partition types"));
318 puts(_(" m print this menu"));
319 puts(_(" n add a new partition"));
320 puts(_(" o create a new empty DOS partition table"));
321 puts(_(" p print the partition table"));
322 puts(_(" q quit without saving changes"));
323 puts(_(" s create a new empty Sun disklabel")); /* sun */
324 puts(_(" t change a partition's system id"));
325 puts(_(" u change display/entry units"));
326 puts(_(" v verify the partition table"));
327 puts(_(" w write table to disk and exit"));
328 puts(_(" x extra functionality (experts only)"));
329 }
330 else if(sgi_label) {
331 puts(_("Command action"));
332 puts(_(" a select bootable partition")); /* sgi flavour */
333 puts(_(" b edit bootfile entry")); /* sgi */
334 puts(_(" c select sgi swap partition")); /* sgi flavour */
335 puts(_(" d delete a partition"));
336 puts(_(" l list known partition types"));
337 puts(_(" m print this menu"));
338 puts(_(" n add a new partition"));
339 puts(_(" o create a new empty DOS partition table"));
340 puts(_(" p print the partition table"));
341 puts(_(" q quit without saving changes"));
342 puts(_(" s create a new empty Sun disklabel")); /* sun */
343 puts(_(" t change a partition's system id"));
344 puts(_(" u change display/entry units"));
345 puts(_(" v verify the partition table"));
346 puts(_(" w write table to disk and exit"));
347 }
348 else if(aix_label) {
349 puts(_("Command action"));
350 puts(_(" m print this menu"));
351 puts(_(" o create a new empty DOS partition table"));
352 puts(_(" q quit without saving changes"));
353 puts(_(" s create a new empty Sun disklabel")); /* sun */
354 }
355 else {
356 puts(_("Command action"));
357 puts(_(" a toggle a bootable flag"));
358 puts(_(" b edit bsd disklabel"));
359 puts(_(" c toggle the dos compatibility flag"));
360 puts(_(" d delete a partition"));
361 puts(_(" l list known partition types"));
362 puts(_(" m print this menu"));
363 puts(_(" n add a new partition"));
364 puts(_(" o create a new empty DOS partition table"));
365 puts(_(" p print the partition table"));
366 puts(_(" q quit without saving changes"));
367 puts(_(" s create a new empty Sun disklabel")); /* sun */
368 puts(_(" t change a partition's system id"));
369 puts(_(" u change display/entry units"));
370 puts(_(" v verify the partition table"));
371 puts(_(" w write table to disk and exit"));
372 puts(_(" x extra functionality (experts only)"));
373 }
374 }
375
376 static void
377 xmenu(void) {
378 if (sun_label) {
379 puts(_("Command action"));
380 puts(_(" a change number of alternate cylinders")); /*sun*/
381 puts(_(" c change number of cylinders"));
382 puts(_(" d print the raw data in the partition table"));
383 puts(_(" e change number of extra sectors per cylinder"));/*sun*/
384 puts(_(" h change number of heads"));
385 puts(_(" i change interleave factor")); /*sun*/
386 puts(_(" o change rotation speed (rpm)")); /*sun*/
387 puts(_(" m print this menu"));
388 puts(_(" p print the partition table"));
389 puts(_(" q quit without saving changes"));
390 puts(_(" r return to main menu"));
391 puts(_(" s change number of sectors/track"));
392 puts(_(" v verify the partition table"));
393 puts(_(" w write table to disk and exit"));
394 puts(_(" y change number of physical cylinders")); /*sun*/
395 }
396 else if(sgi_label) {
397 puts(_("Command action"));
398 puts(_(" b move beginning of data in a partition")); /* !sun */
399 puts(_(" c change number of cylinders"));
400 puts(_(" d print the raw data in the partition table"));
401 puts(_(" e list extended partitions")); /* !sun */
402 puts(_(" g create an IRIX partition table")); /* sgi */
403 puts(_(" h change number of heads"));
404 puts(_(" m print this menu"));
405 puts(_(" p print the partition table"));
406 puts(_(" q quit without saving changes"));
407 puts(_(" r return to main menu"));
408 puts(_(" s change number of sectors/track"));
409 puts(_(" v verify the partition table"));
410 puts(_(" w write table to disk and exit"));
411 }
412 else if(aix_label) {
413 puts(_("Command action"));
414 puts(_(" b move beginning of data in a partition")); /* !sun */
415 puts(_(" c change number of cylinders"));
416 puts(_(" d print the raw data in the partition table"));
417 puts(_(" e list extended partitions")); /* !sun */
418 puts(_(" g create an IRIX partition table")); /* sgi */
419 puts(_(" h change number of heads"));
420 puts(_(" m print this menu"));
421 puts(_(" p print the partition table"));
422 puts(_(" q quit without saving changes"));
423 puts(_(" r return to main menu"));
424 puts(_(" s change number of sectors/track"));
425 puts(_(" v verify the partition table"));
426 puts(_(" w write table to disk and exit"));
427 }
428 else {
429 puts(_("Command action"));
430 puts(_(" b move beginning of data in a partition")); /* !sun */
431 puts(_(" c change number of cylinders"));
432 puts(_(" d print the raw data in the partition table"));
433 puts(_(" e list extended partitions")); /* !sun */
434 puts(_(" f fix partition order")); /* !sun, !aix, !sgi */
435 puts(_(" g create an IRIX partition table")); /* sgi */
436 puts(_(" h change number of heads"));
437 puts(_(" m print this menu"));
438 puts(_(" p print the partition table"));
439 puts(_(" q quit without saving changes"));
440 puts(_(" r return to main menu"));
441 puts(_(" s change number of sectors/track"));
442 puts(_(" v verify the partition table"));
443 puts(_(" w write table to disk and exit"));
444 }
445 }
446
447 static int
448 get_sysid(int i) {
449 return (
450 sun_label ? sunlabel->infos[i].id :
451 sgi_label ? sgi_get_sysid(i) : ptes[i].part_table->sys_ind);
452 }
453
454 static struct systypes *
455 get_sys_types(void) {
456 return (
457 sun_label ? sun_sys_types :
458 sgi_label ? sgi_sys_types : i386_sys_types);
459 }
460
461 char *partition_type(unsigned char type)
462 {
463 int i;
464 struct systypes *types = get_sys_types();
465
466 for (i=0; types[i].name; i++)
467 if (types[i].type == type)
468 return _(types[i].name);
469
470 return NULL;
471 }
472
473 void list_types(struct systypes *sys)
474 {
475 uint last[4], done = 0, next = 0, size;
476 int i;
477
478 for (i = 0; sys[i].name; i++);
479 size = i;
480
481 for (i = 3; i >= 0; i--)
482 last[3 - i] = done += (size + i - done) / (i + 1);
483 i = done = 0;
484
485 do {
486 printf("%c%2x %-15.15s", i ? ' ' : '\n',
487 sys[next].type, _(sys[next].name));
488 next = last[i++] + done;
489 if (i > 3 || next >= last[i]) {
490 i = 0;
491 next = ++done;
492 }
493 } while (done < last[0]);
494 putchar('\n');
495 }
496
497 static void
498 clear_partition(struct partition *p) {
499 if (!p)
500 return;
501 p->boot_ind = 0;
502 p->head = 0;
503 p->sector = 0;
504 p->cyl = 0;
505 p->sys_ind = 0;
506 p->end_head = 0;
507 p->end_sector = 0;
508 p->end_cyl = 0;
509 set_start_sect(p,0);
510 set_nr_sects(p,0);
511 }
512
513 static void
514 set_partition(int i, struct partition *p, uint start, uint stop,
515 int sysid, uint offset) {
516 p->boot_ind = 0;
517 p->sys_ind = sysid;
518 set_start_sect(p, start - offset);
519 set_nr_sects(p, stop - start + 1);
520 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
521 start = heads*sectors*1024 - 1;
522 set_hsc(p->head, p->sector, p->cyl, start);
523 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
524 stop = heads*sectors*1024 - 1;
525 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
526 ptes[i].changed = 1;
527 }
528
529 static int
530 test_c(char **m, char *mesg) {
531 int val = 0;
532 if (!*m)
533 fprintf(stderr, _("You must set"));
534 else {
535 fprintf(stderr, " %s", *m);
536 val = 1;
537 }
538 *m = mesg;
539 return val;
540 }
541
542 static int
543 warn_geometry(void) {
544 char *m = NULL;
545 int prev = 0;
546 if (!heads)
547 prev = test_c(&m, _("heads"));
548 if (!sectors)
549 prev = test_c(&m, _("sectors"));
550 if (!cylinders)
551 prev = test_c(&m, _("cylinders"));
552 if (!m)
553 return 0;
554 fprintf(stderr,
555 _("%s%s.\nYou can do this from the extra functions menu.\n"),
556 prev ? _(" and ") : " ", m);
557 return 1;
558 }
559
560 void update_units(void)
561 {
562 int cyl_units = heads * sectors;
563
564 if (display_in_cyl_units && cyl_units)
565 units_per_sector = cyl_units;
566 else
567 units_per_sector = 1; /* in sectors */
568 }
569
570 static void
571 warn_cylinders(void) {
572 if (dos_label && cylinders > 1024 && !nowarn)
573 fprintf(stderr, _("\n"
574 "The number of cylinders for this disk is set to %d.\n"
575 "There is nothing wrong with that, but this is larger than 1024,\n"
576 "and could in certain setups cause problems with:\n"
577 "1) software that runs at boot time (e.g., old versions of LILO)\n"
578 "2) booting and partitioning software from other OSs\n"
579 " (e.g., DOS FDISK, OS/2 FDISK)\n"),
580 cylinders);
581 }
582
583 static void
584 read_extended(int ext) {
585 int i;
586 struct pte *pex;
587 struct partition *p, *q;
588
589 ext_index = ext;
590 pex = &ptes[ext];
591 pex->ext_pointer = pex->part_table;
592
593 p = pex->part_table;
594 if (!get_start_sect(p)) {
595 fprintf(stderr,
596 _("Bad offset in primary extended partition\n"));
597 return;
598 }
599
600 while (IS_EXTENDED (p->sys_ind)) {
601 struct pte *pe = &ptes[partitions];
602
603 if (partitions >= MAXIMUM_PARTS) {
604 /* This is not a Linux restriction, but
605 this program uses arrays of size MAXIMUM_PARTS.
606 Do not try to `improve' this test. */
607 struct pte *pre = &ptes[partitions-1];
608
609 fprintf(stderr,
610 _("Warning: deleting partitions after %d\n"),
611 partitions);
612 clear_partition(pre->ext_pointer);
613 pre->changed = 1;
614 return;
615 }
616
617 read_pte(fd, partitions, extended_offset + get_start_sect(p));
618
619 if (!extended_offset)
620 extended_offset = get_start_sect(p);
621
622 q = p = pt_offset(pe->sectorbuffer, 0);
623 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
624 if (IS_EXTENDED (p->sys_ind)) {
625 if (pe->ext_pointer)
626 fprintf(stderr, _("Warning: extra link"
627 " pointer in partition table "
628 "%d\n"), partitions + 1);
629 else
630 pe->ext_pointer = p;
631 } else if (p->sys_ind) {
632 if (pe->part_table)
633 fprintf(stderr,
634 _("Warning: ignoring extra data "
635 "in partition table %d\n"),
636 partitions + 1);
637 else
638 pe->part_table = p;
639 }
640 }
641
642 /* very strange code here... */
643 if (!pe->part_table) {
644 if (q != pe->ext_pointer)
645 pe->part_table = q;
646 else
647 pe->part_table = q + 1;
648 }
649 if (!pe->ext_pointer) {
650 if (q != pe->part_table)
651 pe->ext_pointer = q;
652 else
653 pe->ext_pointer = q + 1;
654 }
655
656 p = pe->ext_pointer;
657 partitions++;
658 }
659
660 /* remove empty links */
661 remove:
662 for (i = 4; i < partitions; i++) {
663 struct pte *pe = &ptes[i];
664
665 if (!get_nr_sects(pe->part_table)) {
666 printf("omitting empty partition (%d)\n", i+1);
667 delete_partition(i);
668 goto remove; /* numbering changed */
669 }
670 }
671 }
672
673 static void
674 create_doslabel(void) {
675 int i;
676
677 fprintf(stderr,
678 _("Building a new DOS disklabel. Changes will remain in memory only,\n"
679 "until you decide to write them. After that, of course, the previous\n"
680 "content won't be recoverable.\n\n"));
681
682 sun_nolabel(); /* otherwise always recognised as sun */
683 sgi_nolabel(); /* otherwise always recognised as sgi */
684
685 write_part_table_flag(MBRbuffer);
686 for (i = 0; i < 4; i++)
687 if (ptes[i].part_table)
688 clear_partition(ptes[i].part_table);
689 set_all_unchanged();
690 set_changed(0);
691 get_boot(create_empty);
692 }
693
694 #include <sys/utsname.h>
695 #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
696
697 static int
698 linux_version_code(void) {
699 static int kernel_version = 0;
700 struct utsname my_utsname;
701 int p, q, r;
702
703 if (!kernel_version && uname(&my_utsname) == 0) {
704 p = atoi(strtok(my_utsname.release, "."));
705 q = atoi(strtok(NULL, "."));
706 r = atoi(strtok(NULL, "."));
707 kernel_version = MAKE_VERSION(p,q,r);
708 }
709 return kernel_version;
710 }
711
712 static void
713 get_sectorsize(int fd) {
714 #if defined(BLKSSZGET)
715 if (!user_set_sector_size &&
716 linux_version_code() >= MAKE_VERSION(2,3,3)) {
717 int arg;
718 if (ioctl(fd, BLKSSZGET, &arg) == 0)
719 sector_size = arg;
720 if (sector_size != DEFAULT_SECTOR_SIZE)
721 printf(_("Note: sector size is %d (not %d)\n"),
722 sector_size, DEFAULT_SECTOR_SIZE);
723 }
724 #else
725 /* maybe the user specified it; and otherwise we still
726 have the DEFAULT_SECTOR_SIZE default */
727 #endif
728 }
729
730 /*
731 * Ask kernel about geometry. Invent something reasonable
732 * in case the kernel has no opinion.
733 */
734 void
735 get_geometry(int fd, struct geom *g) {
736 int sec_fac;
737 long longsectors;
738 struct hd_geometry geometry;
739 int res1, res2;
740
741 get_sectorsize(fd);
742 sec_fac = sector_size / 512;
743
744 guess_device_type(fd);
745
746 res1 = ioctl(fd, BLKGETSIZE, &longsectors);
747 #ifdef HDIO_REQ
748 res2 = ioctl(fd, HDIO_REQ, &geometry);
749 #else
750 res2 = ioctl(fd, HDIO_GETGEO, &geometry);
751 #endif
752
753 /* never use geometry.cylinders - it is truncated */
754 heads = cylinders = sectors = 0;
755 sector_offset = 1;
756 if (res2 == 0) {
757 heads = geometry.heads;
758 sectors = geometry.sectors;
759 if (heads * sectors == 0)
760 res2 = -1;
761 else if (dos_compatible_flag)
762 sector_offset = sectors;
763 }
764 if (res1 == 0 && res2 == 0) { /* normal case */
765 cylinders = longsectors / (heads * sectors);
766 cylinders /= sec_fac; /* do not round up */
767 } else if (res1 == 0) { /* size but no geometry */
768 heads = cylinders = 1;
769 sectors = longsectors / sec_fac;
770 }
771
772 if (!sectors)
773 sectors = user_sectors;
774 if (!heads)
775 heads = user_heads;
776 if (!cylinders)
777 cylinders = user_cylinders;
778
779 if (g) {
780 g->heads = heads;
781 g->sectors = sectors;
782 g->cylinders = cylinders;
783 }
784 }
785
786 /*
787 * Read MBR. Returns:
788 * -1: no 0xaa55 flag present (possibly entire disk BSD)
789 * 0: found or created label
790 */
791 int
792 get_boot(enum action what) {
793 int i;
794
795 partitions = 4;
796
797 if (what == create_empty)
798 goto got_table; /* skip reading disk */
799
800 if ((fd = open(disk_device, type_open)) < 0) {
801 if ((fd = open(disk_device, O_RDONLY)) < 0)
802 fatal(unable_to_open);
803 else
804 printf(_("You will not be able to write the partition table.\n"));
805 }
806
807 get_geometry(fd, NULL);
808
809 update_units();
810
811 if (sector_size != read(fd, MBRbuffer, sector_size))
812 fatal(unable_to_read);
813
814 got_table:
815
816 if (check_sun_label())
817 return 0;
818
819 if (check_sgi_label())
820 return 0;
821
822 if (check_aix_label())
823 return 0;
824
825 if (check_osf_label())
826 return 0;
827
828 if (!valid_part_table_flag(MBRbuffer)) {
829 switch(what) {
830 case fdisk:
831 fprintf(stderr,
832 _("Device contains neither a valid DOS partition"
833 " table, nor Sun, SGI or OSF disklabel\n"));
834 #ifdef __sparc__
835 create_sunlabel();
836 #else
837 create_doslabel();
838 #endif
839 return 0;
840 case require:
841 return -1;
842 case try_only:
843 return -1;
844 case create_empty:
845 break;
846 }
847
848 fprintf(stderr, _("Internal error\n"));
849 exit(1);
850 }
851
852 warn_cylinders();
853 warn_geometry();
854
855 for (i = 0; i < 4; i++) {
856 struct pte *pe = &ptes[i];
857
858 pe->part_table = pt_offset(MBRbuffer, i);
859 pe->ext_pointer = NULL;
860 pe->offset = 0;
861 pe->sectorbuffer = MBRbuffer;
862 pe->changed = (what == create_empty);
863 }
864
865 for (i = 0; i < 4; i++) {
866 struct pte *pe = &ptes[i];
867
868 if (IS_EXTENDED (pe->part_table->sys_ind)) {
869 if (partitions != 4)
870 fprintf(stderr, _("Ignoring extra extended "
871 "partition %d\n"), i + 1);
872 else
873 read_extended(i);
874 }
875 }
876
877 for (i = 3; i < partitions; i++) {
878 struct pte *pe = &ptes[i];
879
880 if (!valid_part_table_flag(pe->sectorbuffer)) {
881 fprintf(stderr,
882 _("Warning: invalid flag 0x%04x of partition "
883 "table %d will be corrected by w(rite)\n"),
884 part_table_flag(pe->sectorbuffer), i + 1);
885 pe->changed = 1;
886 }
887 }
888
889 return 0;
890 }
891
892 /* read line; return 0 or first char */
893 int
894 read_line(void)
895 {
896 static int got_eof = 0;
897
898 line_ptr = line_buffer;
899 if (!fgets(line_buffer, LINE_LENGTH, stdin)) {
900 if (feof(stdin))
901 got_eof++; /* user typed ^D ? */
902 if (got_eof >= 3) {
903 fflush(stdout);
904 fprintf(stderr, _("\ngot EOF thrice - exiting..\n"));
905 exit(1);
906 }
907 return 0;
908 }
909 while (*line_ptr && !isgraph(*line_ptr))
910 line_ptr++;
911 return *line_ptr;
912 }
913
914 char
915 read_char(char *mesg)
916 {
917 do {
918 fputs(mesg, stdout);
919 fflush (stdout); /* requested by niles@scyld.com */
920 } while (!read_line());
921 return *line_ptr;
922 }
923
924 char
925 read_chars(char *mesg)
926 {
927 fputs(mesg, stdout);
928 fflush (stdout); /* niles@scyld.com */
929 if (!read_line()) {
930 *line_ptr = '\n';
931 line_ptr[1] = 0;
932 }
933 return *line_ptr;
934 }
935
936 int
937 read_hex(struct systypes *sys)
938 {
939 int hex;
940
941 while (1)
942 {
943 read_char(_("Hex code (type L to list codes): "));
944 if (tolower(*line_ptr) == 'l')
945 list_types(sys);
946 else if (isxdigit (*line_ptr))
947 {
948 hex = 0;
949 do
950 hex = hex << 4 | hex_val(*line_ptr++);
951 while (isxdigit(*line_ptr));
952 return hex;
953 }
954 }
955 }
956
957 /*
958 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
959 * If the user hits Enter, DFLT is returned.
960 * Answers like +10 are interpreted as offsets from BASE.
961 *
962 * There is no default if DFLT is not between LOW and HIGH.
963 */
964 uint
965 read_int(uint low, uint dflt, uint high, uint base, char *mesg)
966 {
967 uint i;
968 int default_ok = 1;
969 static char *ms = NULL;
970 static int mslen = 0;
971
972 if (!ms || strlen(mesg)+50 > mslen) {
973 mslen = strlen(mesg)+100;
974 if (!(ms = realloc(ms,mslen)))
975 fatal(out_of_memory);
976 }
977
978 if (dflt < low || dflt > high)
979 default_ok = 0;
980
981 if (default_ok)
982 sprintf(ms, _("%s (%d-%d, default %d): "), mesg, low, high, dflt);
983 else
984 sprintf(ms, "%s (%d-%d): ", mesg, low, high);
985
986 while (1) {
987 int use_default = default_ok;
988
989 /* ask question and read answer */
990 while (read_chars(ms) != '\n' && !isdigit(*line_ptr)
991 && *line_ptr != '-' && *line_ptr != '+')
992 continue;
993
994 if (*line_ptr == '+' || *line_ptr == '-') {
995 i = atoi(line_ptr+1);
996 if (*line_ptr == '-')
997 i = -i;
998 while (isdigit(*++line_ptr))
999 use_default = 0;
1000 switch (*line_ptr) {
1001 case 'c':
1002 case 'C':
1003 if (!display_in_cyl_units)
1004 i *= heads * sectors;
1005 break;
1006 case 'k':
1007 case 'K':
1008 i *= 2;
1009 i /= (sector_size / 512);
1010 i /= units_per_sector;
1011 break;
1012 case 'm':
1013 case 'M':
1014 i *= 2048;
1015 i /= (sector_size / 512);
1016 i /= units_per_sector;
1017 break;
1018 case 'g':
1019 case 'G':
1020 i *= 2048000;
1021 i /= (sector_size / 512);
1022 i /= units_per_sector;
1023 break;
1024 default:
1025 break;
1026 }
1027 i += base;
1028 } else {
1029 i = atoi(line_ptr);
1030 while (isdigit(*line_ptr)) {
1031 line_ptr++;
1032 use_default = 0;
1033 }
1034 }
1035 if (use_default)
1036 printf(_("Using default value %d\n"), i = dflt);
1037 if (i >= low && i <= high)
1038 break;
1039 else
1040 printf(_("Value out of range.\n"));
1041 }
1042 return i;
1043 }
1044
1045 int
1046 get_partition(int warn, int max) {
1047 struct pte *pe;
1048 int i;
1049
1050 i = read_int(1, 0, max, 0, _("Partition number")) - 1;
1051 pe = &ptes[i];
1052
1053 if (warn && (
1054 (!sun_label && !sgi_label && !pe->part_table->sys_ind)
1055 || (sun_label &&
1056 (!sunlabel->partitions[i].num_sectors ||
1057 !sunlabel->infos[i].id))
1058 || (sgi_label && (!sgi_get_num_sectors(i)))
1059 )) fprintf(stderr, _("Warning: partition %d has empty type\n"), i+1);
1060 return i;
1061 }
1062
1063 char * const
1064 str_units(int n) { /* n==1: use singular */
1065 if (n == 1)
1066 return display_in_cyl_units ? _("cylinder") : _("sector");
1067 else
1068 return display_in_cyl_units ? _("cylinders") : _("sectors");
1069 }
1070
1071 void change_units(void)
1072 {
1073 display_in_cyl_units = !display_in_cyl_units;
1074 update_units();
1075 printf(_("Changing display/entry units to %s\n"),
1076 str_units(PLURAL));
1077 }
1078
1079 static void
1080 toggle_active(int i) {
1081 struct pte *pe = &ptes[i];
1082 struct partition *p = pe->part_table;
1083
1084 if (IS_EXTENDED (p->sys_ind) && !p->boot_ind)
1085 fprintf(stderr,
1086 _("WARNING: Partition %d is an extended partition\n"),
1087 i + 1);
1088 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1089 pe->changed = 1;
1090 }
1091
1092 static void
1093 toggle_dos_compatibility_flag(void) {
1094 dos_compatible_flag = ~dos_compatible_flag;
1095 if (dos_compatible_flag) {
1096 sector_offset = sectors;
1097 printf(_("DOS Compatibility flag is set\n"));
1098 }
1099 else {
1100 sector_offset = 1;
1101 printf(_("DOS Compatibility flag is not set\n"));
1102 }
1103 }
1104
1105 static void
1106 delete_partition(int i) {
1107 struct pte *pe = &ptes[i];
1108 struct partition *p = pe->part_table;
1109 struct partition *q = pe->ext_pointer;
1110
1111 /* Note that for the fifth partition (i == 4) we don't actually
1112 * decrement partitions.
1113 */
1114
1115 if (warn_geometry())
1116 return;
1117 pe->changed = 1;
1118
1119 if (sun_label) {
1120 sun_delete_partition(i);
1121 return;
1122 }
1123
1124 if (sgi_label) {
1125 sgi_delete_partition(i);
1126 return;
1127 }
1128
1129 if (i < 4) {
1130 if (IS_EXTENDED (p->sys_ind) && i == ext_index) {
1131 partitions = 4;
1132 ptes[ext_index].ext_pointer = NULL;
1133 extended_offset = 0;
1134 }
1135 clear_partition(p);
1136 return;
1137 }
1138
1139 if (!q->sys_ind && i > 4) {
1140 --partitions;
1141 --i;
1142 clear_partition(ptes[i].ext_pointer);
1143 ptes[i].changed = 1;
1144 } else {
1145 if (i > 4) {
1146 p = ptes[i-1].ext_pointer;
1147 p->boot_ind = 0;
1148 p->head = q->head;
1149 p->sector = q->sector;
1150 p->cyl = q->cyl;
1151 p->sys_ind = EXTENDED;
1152 p->end_head = q->end_head;
1153 p->end_sector = q->end_sector;
1154 p->end_cyl = q->end_cyl;
1155 set_start_sect(p, get_start_sect(q));
1156 set_nr_sects(p, get_nr_sects(q));
1157 ptes[i-1].changed = 1;
1158 } else if (partitions > 5) { /* 5 will be moved to 4 */
1159 struct pte *pe = &ptes[5];
1160
1161 if(pe->part_table) /* prevent SEGFAULT */
1162 set_start_sect(pe->part_table,
1163 get_partition_start(pe) -
1164 extended_offset);
1165 pe->offset = extended_offset;
1166 pe->changed = 1;
1167 }
1168
1169 if (partitions > 5) {
1170 partitions--;
1171 while (i < partitions) {
1172 ptes[i] = ptes[i+1];
1173 i++;
1174 }
1175 } else
1176 clear_partition(ptes[i].part_table);
1177 }
1178 }
1179
1180 static void
1181 change_sysid(void) {
1182 char *temp;
1183 int i = get_partition(0, partitions), sys, origsys;
1184 struct partition *p = ptes[i].part_table;
1185
1186 origsys = sys = get_sysid(i);
1187
1188 if (!sys && !sgi_label)
1189 printf(_("Partition %d does not exist yet!\n"), i + 1);
1190 else while (1) {
1191 sys = read_hex (get_sys_types());
1192
1193 if (!sys && !sgi_label) {
1194 printf(_("Type 0 means free space to many systems\n"
1195 "(but not to Linux). Having partitions of\n"
1196 "type 0 is probably unwise. You can delete\n"
1197 "a partition using the `d' command.\n"));
1198 /* break; */
1199 }
1200
1201 if (!sun_label && !sgi_label) {
1202 if (IS_EXTENDED (sys) != IS_EXTENDED (p->sys_ind)) {
1203 printf(_("You cannot change a partition into"
1204 " an extended one or vice versa\n"
1205 "Delete it first.\n"));
1206 break;
1207 }
1208 }
1209
1210 if (sys < 256) {
1211 if (sun_label && i == 2 && sys != WHOLE_DISK)
1212 printf(_("Consider leaving partition 3 "
1213 "as Whole disk (5),\n"
1214 "as SunOS/Solaris expects it and "
1215 "even Linux likes it.\n\n"));
1216 if (sgi_label && ((i == 10 && sys != ENTIRE_DISK)
1217 || (i == 8 && sys != 0)))
1218 printf(_("Consider leaving partition 9 "
1219 "as volume header (0),\nand "
1220 "partition 11 as entire volume (6)"
1221 "as IRIX expects it.\n\n"));
1222 if (sys == origsys)
1223 break;
1224
1225 if (sun_label) {
1226 sun_change_sysid(i, sys);
1227 } else
1228 if (sgi_label) {
1229 sgi_change_sysid(i, sys);
1230 } else
1231 p->sys_ind = sys;
1232 printf (_("Changed system type of partition %d "
1233 "to %x (%s)\n"), i + 1, sys,
1234 (temp = partition_type(sys)) ? temp :
1235 _("Unknown"));
1236 ptes[i].changed = 1;
1237 break;
1238 }
1239 }
1240 }
1241
1242 /* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993,
1243 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1244 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1245 * Lubkin Oct. 1991). */
1246
1247 static void long2chs(ulong ls, uint *c, uint *h, uint *s) {
1248 int spc = heads * sectors;
1249
1250 *c = ls / spc;
1251 ls = ls % spc;
1252 *h = ls / sectors;
1253 *s = ls % sectors + 1; /* sectors count from 1 */
1254 }
1255
1256 static void check_consistency(struct partition *p, int partition) {
1257 uint pbc, pbh, pbs; /* physical beginning c, h, s */
1258 uint pec, peh, pes; /* physical ending c, h, s */
1259 uint lbc, lbh, lbs; /* logical beginning c, h, s */
1260 uint lec, leh, les; /* logical ending c, h, s */
1261
1262 if (!heads || !sectors || (partition >= 4))
1263 return; /* do not check extended partitions */
1264
1265 /* physical beginning c, h, s */
1266 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1267 pbh = p->head;
1268 pbs = p->sector & 0x3f;
1269
1270 /* physical ending c, h, s */
1271 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1272 peh = p->end_head;
1273 pes = p->end_sector & 0x3f;
1274
1275 /* compute logical beginning (c, h, s) */
1276 long2chs(get_start_sect(p), &lbc, &lbh, &lbs);
1277
1278 /* compute logical ending (c, h, s) */
1279 long2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
1280
1281 /* Same physical / logical beginning? */
1282 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1283 printf(_("Partition %d has different physical/logical "
1284 "beginnings (non-Linux?):\n"), partition + 1);
1285 printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
1286 printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
1287 }
1288
1289 /* Same physical / logical ending? */
1290 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1291 printf(_("Partition %d has different physical/logical "
1292 "endings:\n"), partition + 1);
1293 printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
1294 printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
1295 }
1296
1297 #if 0
1298 /* Beginning on cylinder boundary? */
1299 if (pbh != !pbc || pbs != 1) {
1300 printf(_("Partition %i does not start on cylinder "
1301 "boundary:\n"), partition + 1);
1302 printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
1303 printf(_("should be (%d, %d, 1)\n"), pbc, !pbc);
1304 }
1305 #endif
1306
1307 /* Ending on cylinder boundary? */
1308 if (peh != (heads - 1) || pes != sectors) {
1309 printf(_("Partition %i does not end on cylinder boundary:\n"),
1310 partition + 1);
1311 printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
1312 printf(_("should be (%d, %d, %d)\n"),
1313 pec, heads - 1, sectors);
1314 }
1315 }
1316
1317 static void
1318 list_disk_geometry(void) {
1319 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\nUnits = "
1320 "%s of %d * %d bytes\n\n"), disk_device, heads, sectors,
1321 cylinders, str_units(PLURAL), units_per_sector, sector_size);
1322 }
1323
1324 /*
1325 * Check whether partition entries are ordered by their starting positions.
1326 * Return 0 if OK. Return i if partition i should have been earlier.
1327 * Two separate checks: primary and logical partitions.
1328 */
1329 static int
1330 wrong_p_order(int *prev) {
1331 struct pte *pe;
1332 struct partition *p;
1333 uint last_p_start_pos = 0, p_start_pos;
1334 int i, last_i = 0;
1335
1336 for (i = 0 ; i < partitions; i++) {
1337 if (i == 4) {
1338 last_i = 4;
1339 last_p_start_pos = 0;
1340 }
1341 pe = &ptes[i];
1342 if ((p = pe->part_table)->sys_ind) {
1343 p_start_pos = get_partition_start(pe);
1344
1345 if (last_p_start_pos > p_start_pos) {
1346 if (prev)
1347 *prev = last_i;
1348 return i;
1349 }
1350
1351 last_p_start_pos = p_start_pos;
1352 last_i = i;
1353 }
1354 }
1355 return 0;
1356 }
1357
1358 static void
1359 fix_partition_table_order(void) {
1360 struct pte *pei, *pek, ptebuf;
1361 int i,k;
1362
1363 if(!wrong_p_order(NULL)) {
1364 printf(_("Nothing to do. Ordering is correct already.\n\n"));
1365 return;
1366 }
1367
1368 while ((i = wrong_p_order(&k)) != 0) {
1369 /* partition i should have come earlier, move it */
1370 pei = &ptes[i];
1371 pek = &ptes[k];
1372
1373 if (i < 4) {
1374 /* We have to move data in the MBR */
1375 struct partition *pi, *pk, *pe, pbuf;
1376
1377 pe = pei->ext_pointer;
1378 pei->ext_pointer = pek->ext_pointer;
1379 pek->ext_pointer = pe;
1380
1381 pi = pei->part_table;
1382 pk = pek->part_table;
1383
1384 memmove(&pbuf, pi, sizeof(struct partition));
1385 memmove(pi, pk, sizeof(struct partition));
1386 memmove(pk, &pbuf, sizeof(struct partition));
1387 } else {
1388 /* Only change is change in numbering */
1389 ptebuf = *pei;
1390 *pei = *pek;
1391 *pek = ptebuf;
1392 }
1393 pei->changed = pek->changed = 1;
1394
1395 }
1396 }
1397
1398 static void
1399 list_table(int xtra) {
1400 struct partition *p;
1401 char *type;
1402 int i, w;
1403
1404 if (sun_label) {
1405 sun_list_table(xtra);
1406 return;
1407 }
1408
1409 if (sgi_label) {
1410 sgi_list_table(xtra);
1411 return;
1412 }
1413
1414 list_disk_geometry();
1415
1416 if (osf_label) {
1417 xbsd_print_disklabel(xtra);
1418 return;
1419 }
1420
1421 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
1422 but if the device name ends in a digit, say /dev/foo1,
1423 then the partition is called /dev/foo1p3. */
1424 w = strlen(disk_device);
1425 if (w && isdigit(disk_device[w-1]))
1426 w++;
1427 if (w < 5)
1428 w = 5;
1429
1430 printf(_("%*s Boot Start End Blocks Id System\n"),
1431 w+1, _("Device"));
1432
1433 for (i = 0 ; i < partitions; i++) {
1434 struct pte *pe = &ptes[i];
1435
1436 p = pe->part_table;
1437 if (p && p->sys_ind) {
1438 unsigned int psects = get_nr_sects(p);
1439 unsigned int pblocks = psects;
1440 unsigned int podd = 0;
1441
1442 if (sector_size < 1024) {
1443 pblocks /= (1024 / sector_size);
1444 podd = psects % (1024 / sector_size);
1445 }
1446 if (sector_size > 1024)
1447 pblocks *= (sector_size / 1024);
1448 printf(
1449 "%s %c %9ld %9ld %9ld%c %2x %s\n",
1450 partname(disk_device, i+1, w+2),
1451 /* boot flag */ !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG
1452 ? '*' : '?',
1453 /* start */ (long) cround(get_partition_start(pe)),
1454 /* end */ (long) cround(get_partition_start(pe) + psects
1455 - (psects ? 1 : 0)),
1456 /* odd flag on end */ (long) pblocks, podd ? '+' : ' ',
1457 /* type id */ p->sys_ind,
1458 /* type name */ (type = partition_type(p->sys_ind)) ?
1459 type : _("Unknown"));
1460 check_consistency(p, i);
1461 }
1462 }
1463
1464 /* Is partition table in disk order? It need not be, but... */
1465 /* partition table entries are not checked for correct order if this
1466 is a sgi, sun or aix labeled disk... */
1467 if (dos_label && wrong_p_order(NULL)) {
1468 printf(_("\nPartition table entries are not in disk order\n"));
1469 }
1470 }
1471
1472 static void
1473 x_list_table(int extend) {
1474 struct pte *pe;
1475 struct partition *p;
1476 int i;
1477
1478 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
1479 disk_device, heads, sectors, cylinders);
1480 printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
1481 for (i = 0 ; i < partitions; i++) {
1482 pe = &ptes[i];
1483 p = (extend ? pe->ext_pointer : pe->part_table);
1484 if (p != NULL) {
1485 printf("%2d %02x%4d%4d%5d%4d%4d%5d%9d%9d %02x\n",
1486 i + 1, p->boot_ind, p->head,
1487 sector(p->sector),
1488 cylinder(p->sector, p->cyl), p->end_head,
1489 sector(p->end_sector),
1490 cylinder(p->end_sector, p->end_cyl),
1491 get_start_sect(p), get_nr_sects(p), p->sys_ind);
1492 if (p->sys_ind)
1493 check_consistency(p, i);
1494 }
1495 }
1496 }
1497
1498 static void
1499 fill_bounds(uint *first, uint *last) {
1500 int i;
1501 struct pte *pe = &ptes[0];
1502 struct partition *p;
1503
1504 for (i = 0; i < partitions; pe++,i++) {
1505 p = pe->part_table;
1506 if (!p->sys_ind || IS_EXTENDED (p->sys_ind)) {
1507 first[i] = 0xffffffff;
1508 last[i] = 0;
1509 } else {
1510 first[i] = get_partition_start(pe);
1511 last[i] = first[i] + get_nr_sects(p) - 1;
1512 }
1513 }
1514 }
1515
1516 static void
1517 check(int n, uint h, uint s, uint c, uint start) {
1518 uint total, real_s, real_c;
1519
1520 real_s = sector(s) - 1;
1521 real_c = cylinder(s, c);
1522 total = (real_c * sectors + real_s) * heads + h;
1523 if (!total)
1524 fprintf(stderr, _("Warning: partition %d contains sector 0\n"), n);
1525 if (h >= heads)
1526 fprintf(stderr,
1527 _("Partition %d: head %d greater than maximum %d\n"),
1528 n, h + 1, heads);
1529 if (real_s >= sectors)
1530 fprintf(stderr, _("Partition %d: sector %d greater than "
1531 "maximum %d\n"), n, s, sectors);
1532 if (real_c >= cylinders)
1533 fprintf(stderr, _("Partitions %d: cylinder %d greater than "
1534 "maximum %d\n"), n, real_c + 1, cylinders);
1535 if (cylinders <= 1024 && start != total)
1536 fprintf(stderr,
1537 _("Partition %d: previous sectors %d disagrees with "
1538 "total %d\n"), n, start, total);
1539 }
1540
1541 static void
1542 verify(void) {
1543 int i, j;
1544 uint total = 1;
1545 uint first[partitions], last[partitions];
1546 struct partition *p = part_table[0];
1547
1548 if (warn_geometry())
1549 return;
1550
1551 if (sun_label) {
1552 verify_sun();
1553 return;
1554 }
1555
1556 if (sgi_label) {
1557 verify_sgi(1);
1558 return;
1559 }
1560
1561 fill_bounds(first, last);
1562 for (i = 0; i < partitions; i++) {
1563 struct pte *pe = &ptes[i];
1564
1565 p = pe->part_table;
1566 if (p->sys_ind && !IS_EXTENDED (p->sys_ind)) {
1567 check_consistency(p, i);
1568 if (get_partition_start(pe) < first[i])
1569 printf(_("Warning: bad start-of-data in "
1570 "partition %d\n"), i + 1);
1571 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
1572 last[i]);
1573 total += last[i] + 1 - first[i];
1574 for (j = 0; j < i; j++)
1575 if ((first[i] >= first[j] && first[i] <= last[j])
1576 || ((last[i] <= last[j] && last[i] >= first[j]))) {
1577 printf(_("Warning: partition %d overlaps "
1578 "partition %d.\n"), j + 1, i + 1);
1579 total += first[i] >= first[j] ?
1580 first[i] : first[j];
1581 total -= last[i] <= last[j] ?
1582 last[i] : last[j];
1583 }
1584 }
1585 }
1586
1587 if (extended_offset) {
1588 struct pte *pex = &ptes[ext_index];
1589 uint e_last = get_start_sect(pex->part_table) +
1590 get_nr_sects(pex->part_table) - 1;
1591
1592 for (i = 4; i < partitions; i++) {
1593 total++;
1594 p = ptes[i].part_table;
1595 if (!p->sys_ind) {
1596 if (i != 4 || i + 1 < partitions)
1597 printf(_("Warning: partition %d "
1598 "is empty\n"), i + 1);
1599 }
1600 else if (first[i] < extended_offset ||
1601 last[i] > e_last)
1602 printf(_("Logical partition %d not entirely in "
1603 "partition %d\n"), i + 1, ext_index + 1);
1604 }
1605 }
1606
1607 if (total > heads * sectors * cylinders)
1608 printf(_("Total allocated sectors %d greater than the maximum "
1609 "%d\n"), total, heads * sectors * cylinders);
1610 else if ((total = heads * sectors * cylinders - total) != 0)
1611 printf(_("%d unallocated sectors\n"), total);
1612 }
1613
1614 static void
1615 add_partition(int n, int sys) {
1616 char mesg[256]; /* 48 does not suffice in Japanese */
1617 int i, read = 0;
1618 struct partition *p = ptes[n].part_table;
1619 struct partition *q = ptes[ext_index].part_table;
1620 uint start, stop = 0, limit, temp,
1621 first[partitions], last[partitions];
1622
1623 if (p && p->sys_ind) {
1624 printf(_("Partition %d is already defined. Delete "
1625 "it before re-adding it.\n"), n + 1);
1626 return;
1627 }
1628 fill_bounds(first, last);
1629 if (n < 4) {
1630 start = sector_offset;
1631 limit = heads * sectors * cylinders - 1;
1632 if (extended_offset) {
1633 first[ext_index] = extended_offset;
1634 last[ext_index] = get_start_sect(q) +
1635 get_nr_sects(q) - 1;
1636 }
1637 } else {
1638 start = extended_offset + sector_offset;
1639 limit = get_start_sect(q) + get_nr_sects(q) - 1;
1640 }
1641 if (display_in_cyl_units)
1642 for (i = 0; i < partitions; i++)
1643 first[i] = (cround(first[i]) - 1) * units_per_sector;
1644
1645 sprintf(mesg, _("First %s"), str_units(SINGULAR));
1646 do {
1647 temp = start;
1648 for (i = 0; i < partitions; i++) {
1649 int lastplusoff;
1650
1651 if (start == ptes[i].offset)
1652 start += sector_offset;
1653 lastplusoff = last[i] + ((n<4) ? 0 : sector_offset);
1654 if (start >= first[i] && start <= lastplusoff)
1655 start = lastplusoff + 1;
1656 }
1657 if (start > limit)
1658 break;
1659 if (start >= temp+units_per_sector && read) {
1660 printf(_("Sector %d is already allocated\n"), temp);
1661 temp = start;
1662 read = 0;
1663 }
1664 if (!read && start == temp) {
1665 uint i;
1666 i = start;
1667 start = read_int(cround(i), cround(i), cround(limit),
1668 0, mesg);
1669 if (display_in_cyl_units) {
1670 start = (start - 1) * units_per_sector;
1671 if (start < i) start = i;
1672 }
1673 read = 1;
1674 }
1675 } while (start != temp || !read);
1676 if (n > 4) { /* NOT for fifth partition */
1677 struct pte *pe = &ptes[n];
1678
1679 pe->offset = start - sector_offset;
1680 if (pe->offset == extended_offset) { /* must be corrected */
1681 pe->offset++;
1682 if (sector_offset == 1)
1683 start++;
1684 }
1685 }
1686
1687 for (i = 0; i < partitions; i++) {
1688 struct pte *pe = &ptes[i];
1689
1690 if (start < pe->offset && limit >= pe->offset)
1691 limit = pe->offset - 1;
1692 if (start < first[i] && limit >= first[i])
1693 limit = first[i] - 1;
1694 }
1695 if (start > limit) {
1696 printf(_("No free sectors available\n"));
1697 if (n > 4)
1698 partitions--;
1699 return;
1700 }
1701 if (cround(start) == cround(limit)) {
1702 stop = limit;
1703 } else {
1704 sprintf(mesg, _("Last %s or +size or +sizeM or +sizeK"),
1705 str_units(SINGULAR));
1706 stop = read_int(cround(start), cround(limit), cround(limit),
1707 cround(start), mesg);
1708 if (display_in_cyl_units) {
1709 stop = stop * units_per_sector - 1;
1710 if (stop >limit)
1711 stop = limit;
1712 }
1713 }
1714
1715 set_partition(n, p, start, stop, sys, ptes[n].offset);
1716
1717 if (IS_EXTENDED (sys)) {
1718 struct pte *pe4 = &ptes[4];
1719 struct pte *pen = &ptes[n];
1720
1721 ext_index = n;
1722 pen->ext_pointer = p;
1723 pe4->offset = extended_offset = start;
1724 if (!(pe4->sectorbuffer = calloc(1, sector_size)))
1725 fatal(out_of_memory);
1726 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
1727 pe4->ext_pointer = pe4->part_table + 1;
1728 pe4->changed = 1;
1729 partitions = 5;
1730 } else {
1731 if (n > 4)
1732 set_partition(n - 1, ptes[n-1].ext_pointer,
1733 ptes[n].offset, stop, EXTENDED,
1734 extended_offset);
1735 #if 0
1736 if ((limit = get_nr_sects(p)) & 1)
1737 printf(_("Warning: partition %d has an odd "
1738 "number of sectors.\n"), n + 1);
1739 #endif
1740 }
1741 }
1742
1743 static void
1744 add_logical(void) {
1745 if (partitions > 5 || ptes[4].part_table->sys_ind) {
1746 struct pte *pe = &ptes[partitions];
1747
1748 if (!(pe->sectorbuffer = calloc(1, sector_size)))
1749 fatal(out_of_memory);
1750 pe->part_table = pt_offset(pe->sectorbuffer, 0);
1751 pe->ext_pointer = pe->part_table + 1;
1752 pe->offset = 0;
1753 pe->changed = 1;
1754 partitions++;
1755 }
1756 add_partition(partitions - 1, LINUX_NATIVE);
1757 }
1758
1759 static void
1760 new_partition(void) {
1761 int i, free_primary = 0;
1762
1763 if (warn_geometry())
1764 return;
1765
1766 if (sun_label) {
1767 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
1768 return;
1769 }
1770
1771 if (sgi_label) {
1772 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
1773 return;
1774 }
1775
1776 if (partitions >= MAXIMUM_PARTS) {
1777 printf(_("The maximum number of partitions has been created\n"));
1778 return;
1779 }
1780
1781 for (i = 0; i < 4; i++)
1782 free_primary += !ptes[i].part_table->sys_ind;
1783 if (!free_primary) {
1784 if (extended_offset)
1785 add_logical();
1786 else
1787 printf(_("You must delete some partition and add "
1788 "an extended partition first\n"));
1789 } else {
1790 char c, line[LINE_LENGTH];
1791 sprintf(line, _("Command action\n %s\n p primary "
1792 "partition (1-4)\n"), extended_offset ?
1793 _("l logical (5 or over)") : _("e extended"));
1794 while (1)
1795 if ((c = tolower(read_char(line))) == 'p') {
1796 add_partition(get_partition(0, 4),
1797 LINUX_NATIVE);
1798 return;
1799 }
1800 else if (c == 'l' && extended_offset) {
1801 add_logical();
1802 return;
1803 }
1804 else if (c == 'e' && !extended_offset) {
1805 add_partition(get_partition(0, 4),
1806 EXTENDED);
1807 return;
1808 }
1809 else
1810 printf(_("Invalid partition number "
1811 "for type `%c'\n"), c);
1812
1813 }
1814 }
1815
1816 static void
1817 write_table(void) {
1818 int i;
1819
1820 if (dos_label) {
1821 for (i=0; i<3; i++)
1822 if(ptes[i].changed)
1823 ptes[3].changed = 1;
1824 for (i = 3; i < partitions; i++) {
1825 struct pte *pe = &ptes[i];
1826
1827 if (pe->changed) {
1828 write_part_table_flag(pe->sectorbuffer);
1829 write_sector(fd, pe->offset, pe->sectorbuffer);
1830 }
1831 }
1832 } else if (sgi_label) {
1833 /* no test on change? the printf below might be mistaken */
1834 sgi_write_table();
1835 } else if (sun_label) {
1836 int needw = 0;
1837
1838 for (i=0; i<8; i++)
1839 if(ptes[i].changed)
1840 needw = 1;
1841 if (needw)
1842 sun_write_table();
1843 }
1844
1845 printf(_("The partition table has been altered!\n\n"));
1846 reread_partition_table(1);
1847 }
1848
1849 void
1850 reread_partition_table(int leave) {
1851 int error = 0;
1852 int i;
1853
1854 printf(_("Calling ioctl() to re-read partition table.\n"));
1855 sync();
1856 sleep(2);
1857 if ((i = ioctl(fd, BLKRRPART)) != 0) {
1858 error = errno;
1859 } else {
1860 /* some kernel versions (1.2.x) seem to have trouble
1861 rereading the partition table, but if asked to do it
1862 twice, the second time works. - biro@yggdrasil.com */
1863 sync();
1864 sleep(2);
1865 if((i = ioctl(fd, BLKRRPART)) != 0)
1866 error = errno;
1867 }
1868
1869 if (i < 0)
1870 printf(_("Re-read table failed with error %d: %s.\nReboot your "
1871 "system to ensure the partition table is updated.\n"),
1872 error, strerror(error));
1873
1874 if (!sun_label && !sgi_label)
1875 printf(
1876 _("\nWARNING: If you have created or modified any DOS 6.x\n"
1877 "partitions, please see the fdisk manual page for additional\n"
1878 "information.\n"));
1879
1880 if (leave) {
1881 close(fd);
1882
1883 printf(_("Syncing disks.\n"));
1884 sync();
1885 sleep(4); /* for sync() */
1886 exit(!!i);
1887 }
1888 }
1889
1890 #define MAX_PER_LINE 16
1891 static void
1892 print_buffer(char pbuffer[]) {
1893 int i,
1894 l;
1895
1896 for (i = 0, l = 0; i < sector_size; i++, l++) {
1897 if (l == 0)
1898 printf("0x%03X:", i);
1899 printf(" %02X", (unsigned char) pbuffer[i]);
1900 if (l == MAX_PER_LINE - 1) {
1901 printf("\n");
1902 l = -1;
1903 }
1904 }
1905 if (l > 0)
1906 printf("\n");
1907 printf("\n");
1908 }
1909
1910 static void
1911 print_raw(void) {
1912 int i;
1913
1914 printf(_("Device: %s\n"), disk_device);
1915 if (sun_label || sgi_label)
1916 print_buffer(MBRbuffer);
1917 else for (i = 3; i < partitions; i++)
1918 print_buffer(ptes[i].sectorbuffer);
1919 }
1920
1921 static void
1922 move_begin(int i) {
1923 struct pte *pe = &ptes[i];
1924 struct partition *p = pe->part_table;
1925 uint new, first;
1926
1927 if (warn_geometry())
1928 return;
1929 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED (p->sys_ind)) {
1930 printf(_("Partition %d has no data area\n"), i + 1);
1931 return;
1932 }
1933 first = get_partition_start(pe);
1934 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
1935 _("New beginning of data")) - pe->offset;
1936
1937 if (new != get_nr_sects(p)) {
1938 first = get_nr_sects(p) + get_start_sect(p) - new;
1939 set_nr_sects(p, first);
1940 set_start_sect(p, new);
1941 pe->changed = 1;
1942 }
1943 }
1944
1945 static void
1946 xselect(void) {
1947 char c;
1948
1949 while(1) {
1950 putchar('\n');
1951 c = tolower(read_char(_("Expert command (m for help): ")));
1952 switch (c) {
1953 case 'a':
1954 if (sun_label)
1955 sun_set_alt_cyl();
1956 break;
1957 case 'b':
1958 if (dos_label)
1959 move_begin(get_partition(0, partitions));
1960 break;
1961 case 'c':
1962 user_cylinders = cylinders =
1963 read_int(1, cylinders, 131071, 0,
1964 _("Number of cylinders"));
1965 if (sun_label)
1966 sun_set_ncyl(cylinders);
1967 if (dos_label)
1968 warn_cylinders();
1969 break;
1970 case 'd':
1971 print_raw();
1972 break;
1973 case 'e':
1974 if (sgi_label)
1975 sgi_set_xcyl();
1976 else if (sun_label)
1977 sun_set_xcyl();
1978 else if (dos_label)
1979 x_list_table(1);
1980 break;
1981 case 'f':
1982 if(dos_label)
1983 fix_partition_table_order();
1984 break;
1985 case 'g':
1986 create_sgilabel();
1987 break;
1988 case 'h':
1989 user_heads = heads = read_int(1, heads, 256, 0,
1990 _("Number of heads"));
1991 update_units();
1992 break;
1993 case 'i':
1994 if (sun_label)
1995 sun_set_ilfact();
1996 break;
1997 case 'o':
1998 if (sun_label)
1999 sun_set_rspeed();
2000 break;
2001 case 'p':
2002 if (sun_label)
2003 list_table(1);
2004 else
2005 x_list_table(0);
2006 break;
2007 case 'q':
2008 close(fd);
2009 printf("\n");
2010 exit(0);
2011 case 'r':
2012 return;
2013 case 's':
2014 user_sectors = sectors = read_int(1, sectors, 63, 0,
2015 _("Number of sectors"));
2016 if (dos_compatible_flag) {
2017 sector_offset = sectors;
2018 fprintf(stderr, _("Warning: setting "
2019 "sector offset for DOS "
2020 "compatiblity\n"));
2021 }
2022 update_units();
2023 break;
2024 case 'v':
2025 verify();
2026 break;
2027 case 'w':
2028 write_table(); /* does not return */
2029 break;
2030 case 'y':
2031 if (sun_label)
2032 sun_set_pcylcount();
2033 break;
2034 default:
2035 xmenu();
2036 }
2037 }
2038 }
2039
2040 static int
2041 is_ide_cdrom(char *device) {
2042 /* No device was given explicitly, and we are trying some
2043 likely things. But opening /dev/hdc may produce errors like
2044 "hdc: tray open or drive not ready"
2045 if it happens to be a CD-ROM drive. It even happens that
2046 the process hangs on the attempt to read a music CD.
2047 So try to be careful. This only works since 2.1.73. */
2048
2049 FILE *procf;
2050 char buf[100];
2051 struct stat statbuf;
2052
2053 if (strncmp("/dev/hd", device, 7))
2054 return 0;
2055 sprintf(buf, "/proc/ide/%s/media", device+5);
2056 procf = fopen(buf, "r");
2057 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2058 return !strncmp(buf, "cdrom", 5);
2059
2060 /* Now when this proc file does not exist, skip the
2061 device when it is read-only. */
2062 if (stat(device, &statbuf) == 0)
2063 return (statbuf.st_mode & 0222) == 0;
2064
2065 return 0;
2066 }
2067
2068 static void
2069 try(char *device, int user_specified) {
2070 disk_device = device;
2071 if (!setjmp(listingbuf)) {
2072 if (!user_specified)
2073 if (is_ide_cdrom(device))
2074 return;
2075 if ((fd = open(disk_device, type_open)) >= 0) {
2076 if (get_boot(try_only) < 0) {
2077 list_disk_geometry();
2078 if (aix_label)
2079 return;
2080 if (btrydev(device) < 0)
2081 fprintf(stderr,
2082 _("Disk %s doesn't contain a valid "
2083 "partition table\n"), device);
2084 close(fd);
2085 } else {
2086 close(fd);
2087 list_table(0);
2088 if (!sun_label && partitions > 4)
2089 delete_partition(ext_index);
2090 }
2091 } else {
2092 /* Ignore other errors, since we try IDE
2093 and SCSI hard disks which may not be
2094 installed on the system. */
2095 if(errno == EACCES) {
2096 fprintf(stderr, _("Cannot open %s\n"), device);
2097 return;
2098 }
2099 }
2100 }
2101 }
2102
2103 /* for fdisk -l: try all things in /proc/partitions
2104 that look like a partition name (do not end in a digit) */
2105 static void
2106 tryprocpt(void) {
2107 FILE *procpt;
2108 char line[100], ptname[100], devname[120], *s;
2109 int ma, mi, sz;
2110
2111 procpt = fopen(PROC_PARTITIONS, "r");
2112 if (procpt == NULL) {
2113 fprintf(stderr, _("cannot open %s\n"), PROC_PARTITIONS);
2114 return;
2115 }
2116
2117 while (fgets(line, sizeof(line), procpt)) {
2118 if (sscanf (line, " %d %d %d %[^\n]\n",
2119 &ma, &mi, &sz, ptname) != 4)
2120 continue;
2121 for(s = ptname; *s; s++);
2122 if (isdigit(s[-1]))
2123 continue;
2124 sprintf(devname, "/dev/%s", ptname);
2125 try(devname, 0);
2126 }
2127 }
2128
2129 static void
2130 dummy(int *kk) {}
2131
2132 static void
2133 unknown_command(int c) {
2134 printf(_("%c: unknown command\n"), c);
2135 }
2136
2137 int
2138 main(int argc, char **argv) {
2139 int j, c;
2140 int optl = 0, opts = 0;
2141
2142 setlocale(LC_ALL, "");
2143 bindtextdomain(PACKAGE, LOCALEDIR);
2144 textdomain(PACKAGE);
2145
2146 /*
2147 * Calls:
2148 * fdisk -v
2149 * fdisk -l [-b sectorsize] [-u] device ...
2150 * fdisk -s [partition] ...
2151 * fdisk [-b sectorsize] [-u] device
2152 */
2153 while ((c = getopt(argc, argv, "b:lsuvV")) != EOF) {
2154 switch (c) {
2155 case 'b':
2156 /* ugly: this sector size is really per device,
2157 so cannot be combined with multiple disks */
2158 sector_size = atoi(optarg);
2159 if (sector_size != 512 && sector_size != 1024 &&
2160 sector_size != 2048)
2161 fatal(usage);
2162 sector_offset = 2;
2163 user_set_sector_size = 1;
2164 break;
2165 case 'l':
2166 optl = 1;
2167 break;
2168 case 's':
2169 opts = 1;
2170 break;
2171 case 'u':
2172 display_in_cyl_units = 0;
2173 break;
2174 case 'V':
2175 case 'v':
2176 printf("fdisk v" UTIL_LINUX_VERSION "\n");
2177 exit(0);
2178 default:
2179 fatal(usage);
2180 }
2181 }
2182
2183 #if 0
2184 printf(_("This kernel finds the sector size itself - -b option ignored\n"));
2185 #else
2186 if (user_set_sector_size && argc-optind != 1)
2187 printf(_("Warning: the -b (set sector size) option should"
2188 " be used with one specified device\n"));
2189 #endif
2190
2191 if (optl) {
2192 nowarn = 1;
2193 type_open = O_RDONLY;
2194 if (argc > optind) {
2195 int k;
2196 /* avoid gcc warning:
2197 variable `k' might be clobbered by `longjmp' */
2198 dummy(&k);
2199 listing = 1;
2200 for(k=optind; k<argc; k++)
2201 try(argv[k], 1);
2202 } else {
2203 /* we no longer have default device names */
2204 /* but, we can use /proc/partitions instead */
2205 tryprocpt();
2206 }
2207 exit(0);
2208 }
2209
2210 if (opts) {
2211 long size;
2212
2213 nowarn = 1;
2214 type_open = O_RDONLY;
2215
2216 opts = argc - optind;
2217 if (opts <= 0)
2218 fatal(usage);
2219
2220 for (j = optind; j < argc; j++) {
2221 disk_device = argv[j];
2222 if ((fd = open(disk_device, type_open)) < 0)
2223 fatal(unable_to_open);
2224 if (ioctl(fd, BLKGETSIZE, &size))
2225 fatal(ioctl_error);
2226 close(fd);
2227 if (opts == 1)
2228 printf("%ld\n", size/2);
2229 else
2230 printf("%s: %ld\n", argv[j], size/2);
2231 }
2232 exit(0);
2233 }
2234
2235 if (argc-optind == 1)
2236 disk_device = argv[optind];
2237 else if (argc-optind != 0)
2238 fatal(usage);
2239 else
2240 fatal(usage2);
2241
2242 get_boot(fdisk);
2243 if (aix_label)
2244 exit(0);
2245
2246 #ifdef __alpha__
2247 /* On alpha, if we detect a disklabel, go directly to
2248 disklabel mode (typically you'll be switching from DOS
2249 partition tables to disklabels, not the other way around)
2250 - dhuggins@linuxcare.com */
2251 if (osf_label) {
2252 printf(_("Detected an OSF/1 disklabel on %s, entering disklabel mode.\n"
2253 "To return to DOS partition table mode, use the 'r' command.\n"),
2254 disk_device);
2255 bselect();
2256 }
2257 #endif
2258
2259 while (1) {
2260 putchar('\n');
2261 c = tolower(read_char(_("Command (m for help): ")));
2262 switch (c) {
2263 case 'a':
2264 if (dos_label)
2265 toggle_active(get_partition(1, partitions));
2266 else if (sun_label)
2267 toggle_sunflags(get_partition(1, partitions),
2268 0x01);
2269 else if (sgi_label)
2270 sgi_set_bootpartition(
2271 get_partition(1, partitions));
2272 else
2273 unknown_command(c);
2274 break;
2275 case 'b':
2276 if (sgi_label) {
2277 printf(_("\nThe current boot file is: %s\n"),
2278 sgi_get_bootfile());
2279 if (read_chars(_("Please enter the name of the "
2280 "new boot file: ")) == '\n')
2281 printf(_("Boot file unchanged\n"));
2282 else
2283 sgi_set_bootfile(line_ptr);
2284 } else
2285 bselect();
2286 break;
2287 case 'c':
2288 if (dos_label)
2289 toggle_dos_compatibility_flag();
2290 else if (sun_label)
2291 toggle_sunflags(get_partition(1, partitions),
2292 0x10);
2293 else if (sgi_label)
2294 sgi_set_swappartition(
2295 get_partition(1, partitions));
2296 else
2297 unknown_command(c);
2298 break;
2299 case 'd':
2300 delete_partition(
2301 get_partition(1, partitions));
2302 break;
2303 case 'i':
2304 if (sgi_label)
2305 create_sgiinfo();
2306 else
2307 unknown_command(c);
2308 case 'l':
2309 list_types(get_sys_types());
2310 break;
2311 case 'm':
2312 menu();
2313 break;
2314 case 'n':
2315 new_partition();
2316 break;
2317 case 'o':
2318 create_doslabel();
2319 break;
2320 case 'p':
2321 list_table(0);
2322 break;
2323 case 'q':
2324 close(fd);
2325 printf("\n");
2326 exit(0);
2327 case 's':
2328 create_sunlabel();
2329 break;
2330 case 't':
2331 change_sysid();
2332 break;
2333 case 'u':
2334 change_units();
2335 break;
2336 case 'v':
2337 verify();
2338 break;
2339 case 'w':
2340 write_table(); /* does not return */
2341 break;
2342 case 'x':
2343 if(sgi_label) {
2344 fprintf(stderr,
2345 _("\n\tSorry, no experts menu for SGI "
2346 "partition tables available.\n\n"));
2347 } else
2348 xselect();
2349 break;
2350 default:
2351 unknown_command(c);
2352 menu();
2353 }
2354 }
2355 return 0;
2356 }