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