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