1 /* Copyright 1997-2003,2005-2007,2009 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
19 #define DONT_NEED_WAIT
21 #include "sysincludes.h"
28 #include "nameclash.h"
31 #include "partition.h"
34 #include "linux/hdreg.h"
36 #define _LINUX_STRING_H_
39 #undef _LINUX_STRING_H_
44 (sector(x)-1+(head(x)+cyl(x)*used_dev->heads)*used_dev->sectors)
47 static __inline__ void print_hsc(hsc *h)
49 printf(" h=%d s=%d c=%d\n",
50 head(*h), sector(*h), cyl(*h));
53 static void set_offset(hsc *h, int offset, int heads, int sectors)
55 int head, sector, cyl;
57 if(! heads || !sectors)
58 head = sector = cyl = 0; /* linear mode */
60 sector = offset % sectors;
61 offset = offset / sectors;
63 head = offset % heads;
65 if(cyl > 1023) cyl = 1023;
69 h->sector = ((sector+1) & 0x3f) | ((cyl & 0x300)>>2);
73 void setBeginEnd(struct partition *partTable,
74 unsigned int begin, unsigned int end,
75 unsigned int heads, unsigned int sectors,
76 int activate, int type, int fat_bits)
78 set_offset(&partTable->start, begin, heads, sectors);
79 set_offset(&partTable->end, end-1, heads, sectors);
80 set_dword(partTable->start_sect, begin);
81 set_dword(partTable->nr_sects, end-begin);
83 partTable->boot_ind = 0x80;
85 partTable->boot_ind = 0;
89 * Fat bits unknown / not specified. We look
90 * at size to get a rough estimate what FAT
91 * bits are used. Note: this is only an
92 * estimate, the precise calculation would
93 * involve the number of clusters, which is
94 * not necessarily known here.
96 /* cc977219 would have a cutoff number of 32680,
97 * corresponding to a FAT12 partition with 4K
98 * clusters, however other information hints that
99 * only partitions with less than 4096 sectors are
107 /* Description of various partition types in
108 * https://en.wikipedia.org/wiki/Partition_type#List_of_partition_IDs
110 * https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-2000-server/cc977219(v=technet.10)
113 /* FAT 32 partition. For now, we disregard the
114 * possibility of FAT 32 CHS partitions */
115 type = 0x0C; /* Win95 FAT32, LBA */
116 else if (end < 65536) {
117 /* FAT 12 or FAT 16 partitions which fit entirely below
119 /* The 32M restriction doesn't apply to logical
120 partitions within an extended partition, but for the
121 moment mpartition only makes primary partitions */
123 /* FAT 12 partition */
124 type = 0x01; /* DOS FAT12, CHS */
125 else if (fat_bits == 16)
126 /* FAT 16 partition */
127 type = 0x04; /* DOS FAT16, CHS */
128 } else if (end < sectors * heads * 1024)
129 /* FAT 12 or FAT16 partition above the 32M
130 * mark but below the 1024 cylinder mark.
131 * Indeed, there can be no CHS partition
132 * beyond 1024 cylinders */
133 type = 0x06; /* DOS BIG FAT16 or FAT12, CHS */
135 type = 0x0E; /* Win95 BIG FAT16, LBA */
137 partTable->sys_ind = type;
140 int consistencyCheck(struct partition *partTable, int doprint, int verbose,
141 int *has_activated, unsigned int *last_end,
143 struct device *used_dev, int target_partition)
146 unsigned int inconsistency;
151 /* quick consistency check */
155 if(!partTable[i].sys_ind)
157 if(partTable[i].boot_ind)
160 (used_dev->heads != head(partTable[i].end)+1 ||
161 used_dev->sectors != sector(partTable[i].end))) ||
162 sector(partTable[i].start) != 1){
164 "Partition %d is not aligned\n",
170 *last_end > BEGIN(partTable[i])) {
172 "Partitions %d and %d badly ordered or overlapping\n",
177 *last_end = END(partTable[i]);
181 cyl(partTable[i].start) != 1023 &&
182 tolinear(partTable[i].start) != BEGIN(partTable[i])) {
184 "Start position mismatch for partition %d\n",
189 cyl(partTable[i].end) != 1023 &&
190 tolinear(partTable[i].end)+1 != END(partTable[i])) {
192 "End position mismatch for partition %d\n",
197 if(doprint && verbose) {
198 if(i==target_partition)
202 printf("Partition %d\n",i);
204 printf(" active=%x\n", partTable[i].boot_ind);
206 print_hsc(&partTable[i].start);
207 printf(" type=0x%x\n", partTable[i].sys_ind);
209 print_hsc(&partTable[i].end);
210 printf(" start=%d\n", BEGIN(partTable[i]));
211 printf(" nr=%d\n", _DWORD(partTable[i].nr_sects));
215 return inconsistency;
218 /* setsize function. Determines scsicam mapping if this cannot be inferred from
219 * any existing partitions. Shamelessly snarfed from the Linux kernel ;-) */
222 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
223 * unsigned int *hds, unsigned int *secs);
225 * Purpose : to determine a near-optimal int 0x13 mapping for a
226 * SCSI disk in terms of lost space of size capacity, storing
227 * the results in *cyls, *hds, and *secs.
229 * Returns : -1 on failure, 0 on success.
239 * Information technology -
240 * SCSI-2 Common access method
241 * transport and SCSI interface module
245 * setsize() converts a read capacity value to int 13h
246 * head-cylinder-sector requirements. It minimizes the value for
247 * number of heads and maximizes the number of cylinders. This
248 * will support rather large disks before the number of heads
249 * will not fit in 4 bits (or 6 bits). This algorithm also
250 * minimizes the number of sectors that will be unused at the end
251 * of the disk while allowing for very large disks to be
252 * accommodated. This algorithm does not use physical geometry.
255 static int setsize(unsigned long capacity,unsigned int *cyls,unsigned int *hds,
256 unsigned int *secs) {
258 unsigned long heads, sectors, cylinders, temp;
260 cylinders = 1024L; /* Set number of cylinders to max */
261 sectors = 62L; /* Maximize sectors per track */
263 temp = cylinders * sectors; /* Compute divisor for heads */
264 heads = capacity / temp; /* Compute value for number of heads */
265 if (capacity % temp) { /* If no remainder, done! */
266 heads++; /* Else, increment number of heads */
267 temp = cylinders * heads; /* Compute divisor for sectors */
268 sectors = capacity / temp; /* Compute value for sectors per
270 if (capacity % temp) { /* If no remainder, done! */
271 sectors++; /* Else, increment number of sectors */
272 temp = heads * sectors; /* Compute divisor for cylinders */
273 cylinders = capacity / temp;/* Compute number of cylinders */
276 if (cylinders == 0) rv=(unsigned)-1;/* Give error if 0 cylinders */
278 *cyls = (unsigned int) cylinders; /* Stuff return values */
279 *secs = (unsigned int) sectors;
280 *hds = (unsigned int) heads;
284 static void setsize0(unsigned long capacity,unsigned int *cyls,
285 unsigned int *hds, unsigned int *secs)
289 /* 1. First try "Megabyte" sizes */
290 if(capacity < 1024 * 2048 && !(capacity % 1024)) {
291 *cyls = capacity >> 11;
297 /* then try scsicam's size */
298 r = setsize(capacity,cyls,hds,secs);
299 if(r || *hds > 255 || *secs > 63) {
300 /* scsicam failed. Do megabytes anyways */
301 *cyls = capacity >> 11;
309 static void usage(int ret) NORETURN;
310 static void usage(int ret)
313 "Mtools version %s, dated %s\n", mversion, mdate);
315 "Usage: %s [-pradcv] [-I] [-B bootsect-template] [-s sectors] "
317 "[-h heads] [-T type] [-b begin] [-l length] "
318 "drive\n", progname);
322 void mpartition(int argc, char **argv, int dummy)
334 unsigned int length = 0;
337 unsigned int tot_sectors=0;
342 unsigned int last_end = 0;
344 int has_activated = 0;
346 unsigned int begin=0;
350 int open2flags = NO_OFFSET;
353 struct device used_dev;
354 int argtracks, argheads, argsectors;
356 char drive, name[EXPAND_BUF];
357 unsigned char buf[512];
358 struct partition *partTable=(struct partition *)(buf+ 0x1ae);
367 /* get command line options */
368 if(helpFlag(argc, argv))
370 while ((c = getopt(argc, argv, "i:adprcIT:t:h:s:fvpb:l:S:B:")) != EOF) {
373 set_cmd_line_image(optarg);
379 /* no privs, as it could be abused to
380 * make other partitions unbootable, or
381 * to boot a rogue kernel from this one */
382 open2flags |= NO_PRIV;
398 /* could be abused to nuke all other
400 open2flags |= NO_PRIV;
410 /* could be abused to "manually" create
411 * extended partitions */
412 open2flags |= NO_PRIV;
413 type = strtoul(optarg,0,0);
417 argtracks = atoi(optarg);
420 argheads = atoi(optarg);
423 argsectors = atoi(optarg);
427 /* could be abused by creating overlapping
428 * partitions and other such Snafu */
429 open2flags |= NO_PRIV;
438 /* could be abused to create partitions
439 * extending beyond the actual size of the
441 open2flags |= NO_PRIV;
442 tot_sectors = strtoul(optarg,0,0);
447 begin = strtoul(optarg, NULL, 10);
451 length = strtoul(optarg, NULL, 10);
459 if (argc - optind != 1 ||
460 !argv[optind][0] || argv[optind][1] != ':')
463 drive = toupper(argv[optind][0]);
465 /* check out a drive whose letter and parameters match */
466 sprintf(errmsg, "Drive '%c:' not supported", drive);
468 for(dev=devices;dev->drive;dev++) {
473 if (dev->drive != drive)
475 if (dev->partition < 1 || dev->partition > 4) {
477 "Drive '%c:' is not a partition",
483 SET_INT(used_dev.tracks, argtracks);
484 SET_INT(used_dev.heads, argheads);
485 SET_INT(used_dev.sectors, argsectors);
487 expand(dev->name, name);
489 mode = dirty ? O_RDWR : O_RDONLY;
493 #ifdef USING_NEW_VOLD
494 strcpy(name, getVoldName(dev, name));
496 Stream = SimpleFileOpen(&used_dev, dev, name, mode,
497 errmsg, open2flags, 1, 0);
501 snprintf(errmsg,199,"init: open: %s", strerror(errno));
503 sprintf(errmsg,"init: open: %s", strerror(errno));
509 /* try to find out the size */
513 unsigned char cmd[10];
514 unsigned char data[10];
515 cmd[0] = SCSI_READ_CAPACITY;
516 memset ((void *) &cmd[2], 0, 8);
517 memset ((void *) &data[0], 137, 10);
518 scsi_cmd(get_fd(Stream), cmd, 10, SCSI_IO_READ,
519 data, 10, get_extra_data(Stream));
527 printf("%d sectors in total\n", tot_sectors);
531 if (tot_sectors == 0) {
532 ioctl(get_fd(Stream), BLKGETSIZE, &tot_sectors);
536 /* read the partition table */
537 if (READS(Stream, (char *) buf, 0, 512) != 512 && !initialize){
539 snprintf(errmsg, 199,
540 "Error reading from '%s', wrong parameters?",
544 "Error reading from '%s', wrong parameters?",
550 print_sector("Read sector", buf, 512);
554 /* print error msg if needed */
555 if ( dev->drive == 0 ){
557 fprintf(stderr,"%s: %s\n", argv[0],errmsg);
561 if((used_dev.sectors || used_dev.heads) &&
562 (!used_dev.sectors || !used_dev.heads)) {
563 fprintf(stderr,"You should either indicate both the number of sectors and the number of heads,\n");
564 fprintf(stderr," or none of them\n");
571 fd = open(bootSector, O_RDONLY | O_BINARY | O_LARGEFILE);
576 if(read(fd, (char *) buf, 512) < 512) {
581 memset((char *)(partTable+1), 0, 4*sizeof(*partTable));
582 set_word(((unsigned char*)buf)+510, 0xaa55);
585 /* check for boot signature, and place it if needed */
586 if((buf[510] != 0x55) || (buf[511] != 0xaa)) {
587 fprintf(stderr,"Boot signature not set\n");
589 "Use the -I flag to initialize the partition table, and set the boot signature\n");
594 if(!partTable[dev->partition].sys_ind)
596 "Partition for drive %c: does not exist\n",
598 if((partTable[dev->partition].sys_ind & 0x3f) == 5) {
600 "Partition for drive %c: may be an extended partition\n",
603 "Use the -f flag to remove it anyways\n");
606 memset(&partTable[dev->partition], 0, sizeof(*partTable));
609 if(create && partTable[dev->partition].sys_ind) {
611 "Partition for drive %c: already exists\n", drive);
613 "Use the -r flag to remove it before attempting to recreate it\n");
617 /* find out number of heads and sectors, and whether there is
618 * any activated partition */
621 if(!partTable[i].sys_ind)
624 if(partTable[i].boot_ind)
627 /* set geometry from entry */
629 used_dev.heads = head(partTable[i].end)+1;
630 if(!used_dev.sectors)
631 used_dev.sectors = sector(partTable[i].end);
632 if(i<dev->partition && !begin_set)
633 begin = END(partTable[i]);
634 if(i>dev->partition && !end_set && !size_set) {
635 end = BEGIN(partTable[i]);
641 if(!used_dev.sectors && !used_dev.heads) {
643 struct hd_geometry geom;
644 if(ioctl(get_fd(Stream), HDIO_GETGEO, &geom) == 0) {
645 used_dev.heads = geom.heads;
646 used_dev.sectors = geom.sectors;
652 if(!used_dev.sectors && !used_dev.heads) {
654 setsize0(tot_sectors,&dummy2,&used_dev.heads,
658 used_dev.sectors = 32;
663 fprintf(stderr,"sectors: %d heads: %d %d\n",
664 used_dev.sectors, used_dev.heads, tot_sectors);
666 sec_per_cyl = used_dev.sectors * used_dev.heads;
668 if(!end_set && tot_sectors) {
669 end = tot_sectors - tot_sectors % sec_per_cyl;
673 /* if the partition starts right at the beginning of
674 * the disk, keep one track unused to allow place for
675 * the master boot record */
676 if(!begin && !begin_set)
677 begin = used_dev.sectors;
678 if(!size_set && used_dev.tracks) {
680 length = sec_per_cyl * used_dev.tracks;
682 /* round the size in order to take
683 * into account any "hidden" sectors */
685 /* do we anchor this at the beginning ?*/
686 if(begin_set || dev->partition <= 2 || !end_set)
687 length -= begin % sec_per_cyl;
688 else if(end - length < begin)
689 /* truncate any overlap */
690 length = end - begin;
693 if(!begin_set && dev->partition >2 && end_set)
694 begin = end - length;
696 end = begin + length;
697 } else if(!end_set) {
698 fprintf(stderr,"Unknown size\n");
702 setBeginEnd(&partTable[dev->partition], begin, end,
703 used_dev.heads, used_dev.sectors,
704 !has_activated, type,
709 if(!partTable[dev->partition].sys_ind) {
711 "Partition for drive %c: does not exist\n",
716 partTable[dev->partition].boot_ind=0x80;
719 partTable[dev->partition].boot_ind=0x00;
726 inconsistency |= consistencyCheck(partTable, doprint, verbose,
727 &has_activated, &last_end, &j,
728 &used_dev, dev->partition);
730 if(doprint && !inconsistency && partTable[dev->partition].sys_ind) {
731 printf("The following command will recreate the partition for drive %c:\n",
734 (_DWORD(partTable[dev->partition].nr_sects) +
735 (BEGIN(partTable[dev->partition]) % sec_per_cyl)) /
737 printf("mpartition -c -t %d -h %d -s %d -b %u %c:\n",
738 used_dev.tracks, used_dev.heads, used_dev.sectors,
739 BEGIN(partTable[dev->partition]), drive);
742 if(tot_sectors && last_end >tot_sectors) {
744 "Partition %d exceeds beyond end of disk\n",
750 switch(has_activated) {
753 "Warning: no active (bootable) partition present\n");
759 "Warning: %d active (bootable) partitions present\n",
762 "Usually, a disk should have exactly one active partition\n");
766 if(inconsistency && !force) {
768 "inconsistency detected!\n" );
771 "Retry with the -f switch to go ahead anyways\n");
776 /* write data back to the disk */
778 print_sector("Writing sector", buf, 512);
779 if (WRITES(Stream, (char *) buf, 0, 512) != 512) {
780 fprintf(stderr,"Error writing partition table");
784 print_sector("Sector written", buf, 512);