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