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