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"
30 #include "partition.h"
31 #include "open_image.h"
35 #include "linux/hdreg.h"
39 static void set_offset(hsc *h, unsigned long offset,
40 uint16_t heads, uint16_t sectors)
42 uint16_t head, sector;
45 if(! heads || !sectors)
46 head = sector = cyl = 0; /* linear mode */
48 sector = offset % sectors;
49 offset = offset / sectors;
51 head = offset % heads;
52 offset = offset / heads;
56 cyl = (uint16_t) offset;
58 if(head > UINT8_MAX) {
59 /* sector or head out of range => linear mode */
60 head = sector = cyl = 0;
62 h->head = (uint8_t) head;
63 h->sector = ((sector+1) & 0x3f) | ((cyl & 0x300)>>2);
67 void setBeginEnd(struct partition *partTable,
68 uint32_t begin, uint32_t end,
69 uint16_t iheads, uint16_t isectors,
70 int activate, uint8_t type, unsigned int fat_bits)
72 uint8_t heads, sectors;
74 if(iheads > UINT8_MAX) {
76 "Too many heads for partition: %d\n",
80 heads=(uint8_t) iheads;
81 if(isectors > UINT8_MAX) {
83 "Too many sectors for partition: %d\n",
87 sectors=(uint8_t) isectors;
89 set_offset(&partTable->start, begin, heads, sectors);
90 set_offset(&partTable->end, end-1, heads, sectors);
91 set_dword(partTable->start_sect, begin);
92 set_dword(partTable->nr_sects, end-begin);
94 partTable->boot_ind = 0x80;
96 partTable->boot_ind = 0;
100 * Fat bits unknown / not specified. We look
101 * at size to get a rough estimate what FAT
102 * bits are used. Note: this is only an
103 * estimate, the precise calculation would
104 * involve the number of clusters, which is
105 * not necessarily known here.
107 /* cc977219 would have a cutoff number of 32680,
108 * corresponding to a FAT12 partition with 4K
109 * clusters, however other information hints that
110 * only partitions with less than 4096 sectors are
118 /* Description of various partition types in
119 * https://en.wikipedia.org/wiki/Partition_type#List_of_partition_IDs
121 * https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-2000-server/cc977219(v=technet.10)
124 /* FAT 32 partition. For now, we disregard the
125 * possibility of FAT 32 CHS partitions */
126 type = 0x0C; /* Win95 FAT32, LBA */
127 else if (end < 65536) {
128 /* FAT 12 or FAT 16 partitions which fit entirely below
130 /* The 32M restriction doesn't apply to logical
131 partitions within an extended partition, but for the
132 moment mpartition only makes primary partitions */
134 /* FAT 12 partition */
135 type = 0x01; /* DOS FAT12, CHS */
136 else if (fat_bits == 16)
137 /* FAT 16 partition */
138 type = 0x04; /* DOS FAT16, CHS */
139 } else if (end < sectors * heads * 1024u)
140 /* FAT 12 or FAT16 partition above the 32M
141 * mark but below the 1024 cylinder mark.
142 * Indeed, there can be no CHS partition
143 * beyond 1024 cylinders */
144 type = 0x06; /* DOS BIG FAT16 or FAT12, CHS */
146 type = 0x0E; /* Win95 BIG FAT16, LBA */
148 partTable->sys_ind = type;
152 /* setsize function. Determines scsicam mapping if this cannot be inferred from
153 * any existing partitions. Shamelessly snarfed from the Linux kernel ;-) */
156 * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
157 * unsigned int *hds, unsigned int *secs);
159 * Purpose : to determine a near-optimal int 0x13 mapping for a
160 * SCSI disk in terms of lost space of size capacity, storing
161 * the results in *cyls, *hds, and *secs.
163 * Returns : -1 on failure, 0 on success.
173 * Information technology -
174 * SCSI-2 Common access method
175 * transport and SCSI interface module
179 * setsize() converts a read capacity value to int 13h
180 * head-cylinder-sector requirements. It minimizes the value for
181 * number of heads and maximizes the number of cylinders. This
182 * will support rather large disks before the number of heads
183 * will not fit in 4 bits (or 6 bits). This algorithm also
184 * minimizes the number of sectors that will be unused at the end
185 * of the disk while allowing for very large disks to be
186 * accommodated. This algorithm does not use physical geometry.
189 static int setsize(unsigned long capacity,unsigned int *cyls,
190 uint16_t *hds, uint16_t *secs) {
192 unsigned long heads, sectors, cylinders, temp;
194 cylinders = 1024L; /* Set number of cylinders to max */
195 sectors = 62L; /* Maximize sectors per track */
197 temp = cylinders * sectors; /* Compute divisor for heads */
198 heads = capacity / temp; /* Compute value for number of heads */
199 if (capacity % temp) { /* If no remainder, done! */
200 heads++; /* Else, increment number of heads */
201 temp = cylinders * heads; /* Compute divisor for sectors */
202 sectors = capacity / temp; /* Compute value for sectors per
204 if (capacity % temp) { /* If no remainder, done! */
205 sectors++; /* Else, increment number of sectors */
206 temp = heads * sectors; /* Compute divisor for cylinders */
207 cylinders = capacity / temp;/* Compute number of cylinders */
210 if (cylinders == 0) rv=-1;/* Give error if 0 cylinders */
212 *cyls = (unsigned int) cylinders; /* Stuff return values */
213 *secs = (uint16_t) sectors;
214 *hds = (uint16_t) heads;
218 static void setsize0(uint32_t capacity,unsigned int *cyls,
219 uint16_t *hds, uint16_t *secs)
223 /* 1. First try "Megabyte" sizes */
224 if(capacity < 1024 * 2048 && !(capacity % 1024)) {
225 *cyls = capacity >> 11;
231 /* then try scsicam's size */
232 r = setsize(capacity,cyls,hds,secs);
233 if(r || *hds > 255 || *secs > 63) {
234 /* scsicam failed. Do megabytes anyways */
235 *cyls = capacity >> 11;
243 static void usage(int ret) NORETURN;
244 static void usage(int ret)
247 "Mtools version %s, dated %s\n", mversion, mdate);
249 "Usage: %s [-pradcv] [-I] [-B bootsect-template] [-s sectors] "
251 "[-h heads] [-T type] [-b begin] [-l length] "
252 "drive\n", progname);
256 void mpartition(int argc, char **argv, int dummy UNUSEDP) NORETURN;
257 void mpartition(int argc, char **argv, int dummy UNUSEDP)
264 uint16_t sec_per_cyl;
269 unsigned int length = 0;
273 uint32_t tot_sectors=0;
274 /* Needs to be long due to BLKGETSIZE ioctl */
281 int has_activated = 0;
283 unsigned int begin=0;
289 struct device used_dev;
290 unsigned int argtracks;
291 uint16_t argheads, argsectors;
293 char drive, name[EXPAND_BUF];
294 unsigned char buf[512];
295 struct partition *partTable=(struct partition *)(buf+ 0x1ae);
299 struct partition *tpartition;
305 /* get command line options */
306 if(helpFlag(argc, argv))
308 while ((c = getopt(argc, argv, "i:adprcIT:t:h:s:fvpb:l:S:B:")) != EOF) {
313 set_cmd_line_image(optarg);
319 /* no privs, as it could be abused to
320 * make other partitions unbootable, or
321 * to boot a rogue kernel from this one */
322 open2flags |= NO_PRIV;
338 /* could be abused to nuke all other
340 open2flags |= NO_PRIV;
350 /* could be abused to "manually" create
351 * extended partitions */
352 open2flags |= NO_PRIV;
353 type = strtou8(optarg, &endptr, 0);
357 argtracks = atoui(optarg);
360 argheads = atou16(optarg);
363 argsectors = atou16(optarg);
367 /* could be abused by creating overlapping
368 * partitions and other such Snafu */
369 open2flags |= NO_PRIV;
378 begin = strtoui(optarg, &endptr, 0);
382 length = parseSize(optarg);
388 check_number_parse_errno((char)c, optarg, endptr);
391 if (argc - optind != 1 ||
392 !argv[optind][0] || argv[optind][1] != ':')
395 drive = ch_toupper(argv[optind][0]);
397 /* check out a drive whose letter and parameters match */
398 sprintf(errmsg, "Drive '%c:' not supported", drive);
400 for(dev=devices;dev->drive;dev++) {
405 if (dev->drive != drive)
407 if (dev->partition < 1 || dev->partition > 4) {
409 "Drive '%c:' is not a partition",
415 SET_INT(used_dev.tracks, argtracks);
416 SET_INT(used_dev.heads, argheads);
417 SET_INT(used_dev.sectors, argsectors);
419 expand(dev->name, name);
421 mode = dirty ? O_RDWR : O_RDONLY;
425 #ifdef USING_NEW_VOLD
426 strcpy(name, getVoldName(dev, name));
428 Stream = OpenImage(&used_dev, dev, name, mode, errmsg,
429 open2flags | SKIP_PARTITION | ALWAYS_GET_GEOMETRY,
430 mode, NULL, NULL, NULL);
434 snprintf(errmsg,sizeof(errmsg)-1,
435 "init: open: %s", strerror(errno));
437 sprintf(errmsg,"init: open: %s", strerror(errno));
442 tot_sectors = used_dev.tot_sectors;
444 /* read the partition table */
445 if (PREADS(Stream, (char *) buf, 0, 512) != 512 && !initialize){
447 snprintf(errmsg, sizeof(errmsg)-1,
448 "Error reading from '%s', wrong parameters?",
452 "Error reading from '%s', wrong parameters?",
458 print_sector("Read sector", buf, 512);
462 /* print error msg if needed */
463 if ( dev->drive == 0 ){
465 fprintf(stderr,"%s: %s\n", argv[0],errmsg);
469 if((used_dev.sectors || used_dev.heads) &&
470 (!used_dev.sectors || !used_dev.heads)) {
471 fprintf(stderr,"You should either indicate both the number of sectors and the number of heads,\n");
472 fprintf(stderr," or none of them\n");
479 fd = open(bootSector, O_RDONLY | O_BINARY | O_LARGEFILE);
484 if(read(fd, (char *) buf, 512) < 512) {
489 memset((char *)(partTable+1), 0, 4*sizeof(*partTable));
490 set_word(((unsigned char*)buf)+510, 0xaa55);
493 /* check for boot signature, and place it if needed */
494 if((buf[510] != 0x55) || (buf[511] != 0xaa)) {
495 fprintf(stderr,"Boot signature not set\n");
497 "Use the -I flag to initialize the partition table, and set the boot signature\n");
501 tpartition=&partTable[dev->partition];
503 if(!tpartition->sys_ind)
505 "Partition for drive %c: does not exist\n",
507 if((tpartition->sys_ind & 0x3f) == 5) {
509 "Partition for drive %c: may be an extended partition\n",
512 "Use the -f flag to remove it anyways\n");
515 memset(tpartition, 0, sizeof(*tpartition));
518 if(create && tpartition->sys_ind) {
520 "Partition for drive %c: already exists\n", drive);
522 "Use the -r flag to remove it before attempting to recreate it\n");
525 /* if number of heads and sectors not known yet, set "reasonable"
527 compute_lba_geom_from_tot_sectors(&used_dev);
529 /* find out whether there is any activated partition. Moreover
530 * if no offset of a partition to be created have been
531 * specificed, find out whether it may be placed between the
532 * preceding and following partition already existing */
535 struct partition *partition=&partTable[i];
536 if(!partition->sys_ind)
539 if(partition->boot_ind)
542 if(i<dev->partition && !begin_set)
543 begin = END(partition);
544 if(i>dev->partition && !end_set && !size_set) {
545 end = BEGIN(partition);
550 if(!used_dev.sectors && !used_dev.heads) {
552 setsize0((uint32_t)tot_sectors,&dummy2,&used_dev.heads,
556 used_dev.sectors = 32;
561 fprintf(stderr,"sectors: %d heads: %d %u\n",
562 used_dev.sectors, used_dev.heads, tot_sectors);
564 sec_per_cyl = used_dev.sectors * used_dev.heads;
566 unsigned int overlap;
567 if(!end_set && !size_set && tot_sectors) {
568 end = tot_sectors - tot_sectors % sec_per_cyl;
572 /* if the partition starts right at the beginning of
573 * the disk, keep one track unused to allow place for
574 * the master boot record */
575 if(!begin && !begin_set)
576 begin = used_dev.sectors ? used_dev.sectors : 2048;
578 /* Do not try to align partitions (other than first) on track
579 * boundaries here: apparently this was a thing of the past */
582 end = begin + length;
583 } else if(!end_set) {
584 fprintf(stderr,"Unknown size\n");
588 /* Make sure partition boundaries are correctly ordered
591 fprintf(stderr, "Begin larger than end\n");
595 /* Check whether new partition doesn't overlap with
596 * any of those already in place */
597 if((overlap=findOverlap(partTable, 4, begin, end))) {
599 "Partition would overlap with partition %d\n",
604 setBeginEnd(tpartition, begin, end,
605 used_dev.heads, used_dev.sectors,
606 !has_activated, type,
611 if(!tpartition->sys_ind) {
613 "Partition for drive %c: does not exist\n",
618 tpartition->boot_ind=0x80;
621 tpartition->boot_ind=0x00;
627 inconsistency |= consistencyCheck(partTable, doprint, verbose,
628 &has_activated, tot_sectors,
629 &used_dev, dev->partition);
631 switch(has_activated) {
634 "Warning: no active (bootable) partition present\n");
640 "Warning: %d active (bootable) partitions present\n",
643 "Usually, a disk should have exactly one active partition\n");
647 if(inconsistency && !force) {
649 "inconsistency detected!\n" );
652 "Retry with the -f switch to go ahead anyways\n");
657 if(doprint && tpartition->sys_ind) {
658 printf("The following command will recreate the partition for drive %c:\n",
661 (_DWORD(tpartition->nr_sects) +
662 (BEGIN(tpartition) % sec_per_cyl)) /
664 printf("mpartition -c -b %d -l %d -t %d -h %d -s %d -b %u %c:\n",
665 BEGIN(tpartition), PART_SIZE(tpartition),
666 used_dev.tracks, used_dev.heads, used_dev.sectors,
667 BEGIN(tpartition), drive);
671 /* write data back to the disk */
673 print_sector("Writing sector", buf, 512);
674 if (PWRITES(Stream, (char *) buf, 0, 512) != 512) {
675 fprintf(stderr,"Error writing partition table");
679 print_sector("Sector written", buf, 512);