xref: /onnv-gate/usr/src/cmd/fs.d/pcfs/fsck/pcfs_common.c (revision 7563:84ec90ffc3f7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7563SPrasad.Singamsetty@Sun.COM  * Common Development and Distribution License (the "License").
6*7563SPrasad.Singamsetty@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7563SPrasad.Singamsetty@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7563SPrasad.Singamsetty@Sun.COM  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * fsck_pcfs -- common.c
280Sstevel@tonic-gate  *	All the routines in this file are being swiped directly from
290Sstevel@tonic-gate  *	mkfs_pcfs.  Eventually this file should only exist in one place
300Sstevel@tonic-gate  *	and be part of a library that both mkfs and fsck link against.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <libintl.h>
370Sstevel@tonic-gate #include <sys/isa_defs.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <sys/fcntl.h>
410Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
420Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
430Sstevel@tonic-gate #include <sys/fs/pc_dir.h>
440Sstevel@tonic-gate #include <sys/fs/pc_label.h>
450Sstevel@tonic-gate #include "fsck_pcfs.h"
460Sstevel@tonic-gate #include "pcfs_common.h"
470Sstevel@tonic-gate #include "pcfs_bpb.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  *	The assumption here is that _BIG_ENDIAN implies sparc, and
510Sstevel@tonic-gate  *	so in addition to swapping bytes we also have to construct
520Sstevel@tonic-gate  *	packed structures by hand to avoid bus errors due to improperly
530Sstevel@tonic-gate  *	aligned pointers.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate #ifdef _BIG_ENDIAN
560Sstevel@tonic-gate void swap_pack_grab32bpb(bpb_t *wbpb, struct _boot_sector *bsp);
570Sstevel@tonic-gate void swap_pack_grabbpb(bpb_t *wbpb, struct _boot_sector *bsp);
580Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  *  Global variables related to input questions
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate extern int AlwaysYes;
640Sstevel@tonic-gate extern int AlwaysNo;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate int
is_z_a_power_of_x_le_y(int x,int y,int z)670Sstevel@tonic-gate is_z_a_power_of_x_le_y(int x, int y, int z)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	int ispower = 0;
700Sstevel@tonic-gate 	int pow = 1;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	do {
730Sstevel@tonic-gate 		if (pow == z) {
740Sstevel@tonic-gate 			ispower = 1;
750Sstevel@tonic-gate 			break;
760Sstevel@tonic-gate 		}
770Sstevel@tonic-gate 		pow *= x;
780Sstevel@tonic-gate 	} while (pow <= y);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	return (ispower);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * store_16_bits
850Sstevel@tonic-gate  *	Save the lower 16 bits of a 32 bit value (v) into the provided
860Sstevel@tonic-gate  *	buffer (pointed at by *bp), and increment the buffer pointer
870Sstevel@tonic-gate  *	as well.  This way the routine can be called multiple times in
880Sstevel@tonic-gate  *	succession to fill buffers.  The value is stored in little-endian
890Sstevel@tonic-gate  *	order.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate void
store_16_bits(uchar_t ** bp,uint32_t v)920Sstevel@tonic-gate store_16_bits(uchar_t **bp, uint32_t v)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	uchar_t *l = *bp;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	*l++ = v & 0xff;
970Sstevel@tonic-gate 	*l = (v >> 8) & 0xff;
980Sstevel@tonic-gate 	*bp += 2;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate void
read_16_bits(uchar_t * bp,uint32_t * value)1020Sstevel@tonic-gate read_16_bits(uchar_t *bp, uint32_t *value)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	*value = *bp++;
1050Sstevel@tonic-gate 	*value += *bp << 8;
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * store_32_bits
1100Sstevel@tonic-gate  * 	Save the 32 bit value (v) into the provided buffer (pointed
1110Sstevel@tonic-gate  *	at by *bp), and increment the buffer pointer as well.  This way
1120Sstevel@tonic-gate  *	the routine can be called multiple times in succession to fill
1130Sstevel@tonic-gate  *	buffers.  The value is stored in little-endian order.
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate void
store_32_bits(uchar_t ** bp,uint32_t v)1160Sstevel@tonic-gate store_32_bits(uchar_t **bp, uint32_t v)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	uchar_t *l = *bp;
1190Sstevel@tonic-gate 	int b;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	for (b = 0; b < 4; b++) {
1220Sstevel@tonic-gate 		*l++ = v & 0xff;
1230Sstevel@tonic-gate 		v = v >> 8;
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 	*bp += 4;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate void
read_32_bits(uchar_t * bp,uint32_t * value)1290Sstevel@tonic-gate read_32_bits(uchar_t *bp, uint32_t *value)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	*value = *bp++;
1320Sstevel@tonic-gate 	*value += *bp++ << 8;
1330Sstevel@tonic-gate 	*value += *bp++ << 16;
1340Sstevel@tonic-gate 	*value += *bp++ << 24;
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  *  dump_bytes  -- display bytes as hex numbers.
1390Sstevel@tonic-gate  *		   b is the pointer to the byte buffer
1400Sstevel@tonic-gate  *		   n is the number of bytes in the buffer
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate /* Note: BPL = bytes to display per line */
1430Sstevel@tonic-gate #define	BPL 16
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate void
dump_bytes(uchar_t * buf,int n)1460Sstevel@tonic-gate dump_bytes(uchar_t *buf, int n)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	int printedCount;
1490Sstevel@tonic-gate 	int countdown = n;
1500Sstevel@tonic-gate 	int countup = 0;
1510Sstevel@tonic-gate 	int offset = 0;
1520Sstevel@tonic-gate 	int byte;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/* Display offset, 16 bytes per line, and printable ascii version */
1550Sstevel@tonic-gate 	while (countdown > 0) {
1560Sstevel@tonic-gate 		printedCount = 0;
1570Sstevel@tonic-gate 		(void) fprintf(stderr, "\n%06x: ", offset);
1580Sstevel@tonic-gate 		/*
1590Sstevel@tonic-gate 		 * Print Hex value of characters in columns on left
1600Sstevel@tonic-gate 		 */
1610Sstevel@tonic-gate 		for (byte = 0; byte < BPL; byte++) {
1620Sstevel@tonic-gate 			if (countup + byte < n) {
1630Sstevel@tonic-gate 				(void) fprintf(stderr,
1640Sstevel@tonic-gate 				    "%02x ", (buf[countup + byte] & 0xff));
1650Sstevel@tonic-gate 				printedCount++;
1660Sstevel@tonic-gate 			} else {
1670Sstevel@tonic-gate 				(void) fprintf(stderr, "   ");
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 		/*
1710Sstevel@tonic-gate 		 * Right side has the printable character or '.' for
1720Sstevel@tonic-gate 		 * unprintable for each column of the left.
1730Sstevel@tonic-gate 		 */
1740Sstevel@tonic-gate 		for (byte = 0; byte < BPL; byte++) {
1750Sstevel@tonic-gate 			if ((countup + byte < n) &&
1760Sstevel@tonic-gate 			    ((buf[countup + byte] >= ' ') &&
1770Sstevel@tonic-gate 			    (buf[countup + byte] <= '~'))) {
1780Sstevel@tonic-gate 				(void) fprintf(stderr, "%c",
1790Sstevel@tonic-gate 				    buf[countup + byte]);
1800Sstevel@tonic-gate 			} else {
1810Sstevel@tonic-gate 				(void) fprintf(stderr, ".");
1820Sstevel@tonic-gate 			}
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 		countup += printedCount;
1850Sstevel@tonic-gate 		offset += printedCount;
1860Sstevel@tonic-gate 		countdown -= printedCount;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	(void) fprintf(stderr, "\n\n");
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  *  header_for_dump  --  display simple header over what will be output.
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate void
header_for_dump(void)1950Sstevel@tonic-gate header_for_dump(void)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	int byte;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	(void) fprintf(stderr, "\n        ");
2000Sstevel@tonic-gate 	for (byte = 0; byte < BPL; byte++)
2010Sstevel@tonic-gate 		(void) fprintf(stderr, "%02x ", byte);
2020Sstevel@tonic-gate 	(void) fprintf(stderr, "\n       ");
2030Sstevel@tonic-gate 	byte = 3*BPL;
2040Sstevel@tonic-gate 	while (byte-- > 0)
2050Sstevel@tonic-gate 		(void) fprintf(stderr, "-");
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  *  We are basically (incorrectly) assuming that if you aren't running
2100Sstevel@tonic-gate  *  on x86 the BPB has to be packed by hand AND that the bytes must
2110Sstevel@tonic-gate  *  be swapped.  One or both of these assumptions may one day be invalid.
2120Sstevel@tonic-gate  *  (if they aren't already :-))
2130Sstevel@tonic-gate  */
2140Sstevel@tonic-gate #ifdef _BIG_ENDIAN
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  *  swap_pack_grab{32}bpb
2170Sstevel@tonic-gate  *	If not on an x86 we assume the structures making up the bpb
2180Sstevel@tonic-gate  *	were not packed and that longs and shorts need to be byte swapped
2190Sstevel@tonic-gate  *	(we've kept everything in host order up until now).  A new architecture
2200Sstevel@tonic-gate  *	might not need to swap or might not need to pack, in which case
2210Sstevel@tonic-gate  *	new routines will have to be written.  Of course if an architecture
2220Sstevel@tonic-gate  *	supports both packing and little-endian host order, it can follow the
2230Sstevel@tonic-gate  *	same path as the x86 code.
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate void
swap_pack_grabbpb(bpb_t * wbpb,struct _boot_sector * bsp)2260Sstevel@tonic-gate swap_pack_grabbpb(bpb_t *wbpb, struct _boot_sector *bsp)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	uchar_t *grabp;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	grabp = (uchar_t *)&(bsp->bs_filler[ORIG_BPB_START_INDEX]);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.bytes_per_sector))[1] = *grabp++;
2330Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.bytes_per_sector))[0] = *grabp++;
2340Sstevel@tonic-gate 	wbpb->bpb.sectors_per_cluster = *grabp++;
2350Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.resv_sectors))[1] = *grabp++;
2360Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.resv_sectors))[0] = *grabp++;
2370Sstevel@tonic-gate 	wbpb->bpb.num_fats = *grabp++;
2380Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.num_root_entries))[1] = *grabp++;
2390Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.num_root_entries))[0] = *grabp++;
2400Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_in_volume))[1] = *grabp++;
2410Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_in_volume))[0] = *grabp++;
2420Sstevel@tonic-gate 	wbpb->bpb.media = *grabp++;
2430Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_per_fat))[1] = *grabp++;
2440Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_per_fat))[0] = *grabp++;
2450Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_per_track))[1] = *grabp++;
2460Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_per_track))[0] = *grabp++;
2470Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.heads))[1] = *grabp++;
2480Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.heads))[0] = *grabp++;
2490Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.hidden_sectors))[3] = *grabp++;
2500Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.hidden_sectors))[2] = *grabp++;
2510Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.hidden_sectors))[1] = *grabp++;
2520Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.hidden_sectors))[0] = *grabp++;
2530Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[3] = *grabp++;
2540Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[2] = *grabp++;
2550Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[1] = *grabp++;
2560Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb.sectors_in_logical_volume))[0] = *grabp++;
2570Sstevel@tonic-gate 	wbpb->ebpb.phys_drive_num = *grabp++;
2580Sstevel@tonic-gate 	wbpb->ebpb.reserved = *grabp++;
2590Sstevel@tonic-gate 	wbpb->ebpb.ext_signature = *grabp++;
2600Sstevel@tonic-gate 	((uchar_t *)&(wbpb->ebpb.volume_id))[3] = *grabp++;
2610Sstevel@tonic-gate 	((uchar_t *)&(wbpb->ebpb.volume_id))[2] = *grabp++;
2620Sstevel@tonic-gate 	((uchar_t *)&(wbpb->ebpb.volume_id))[1] = *grabp++;
2630Sstevel@tonic-gate 	((uchar_t *)&(wbpb->ebpb.volume_id))[0] = *grabp++;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	(void) strncpy((char *)wbpb->ebpb.volume_label, (char *)grabp, 11);
2660Sstevel@tonic-gate 	grabp += 11;
2670Sstevel@tonic-gate 	(void) strncpy((char *)wbpb->ebpb.type, (char *)grabp, 8);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate void
swap_pack_grab32bpb(bpb_t * wbpb,struct _boot_sector * bsp)2710Sstevel@tonic-gate swap_pack_grab32bpb(bpb_t *wbpb, struct _boot_sector *bsp)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	uchar_t *grabp;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	grabp = (uchar_t *)&(bsp->bs_filler[BPB_32_START_INDEX]);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[3] = *grabp++;
2780Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[2] = *grabp++;
2790Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[1] = *grabp++;
2800Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.big_sectors_per_fat))[0] = *grabp++;
2810Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.ext_flags))[1] = *grabp++;
2820Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.ext_flags))[0] = *grabp++;
2830Sstevel@tonic-gate 	wbpb->bpb32.fs_vers_lo = *grabp++;
2840Sstevel@tonic-gate 	wbpb->bpb32.fs_vers_hi = *grabp++;
2850Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.root_dir_clust))[3] = *grabp++;
2860Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.root_dir_clust))[2] = *grabp++;
2870Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.root_dir_clust))[1] = *grabp++;
2880Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.root_dir_clust))[0] = *grabp++;
2890Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.fsinfosec))[1] = *grabp++;
2900Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.fsinfosec))[0] = *grabp++;
2910Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.backupboot))[1] = *grabp++;
2920Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.backupboot))[0] = *grabp++;
2930Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[0]))[1] = *grabp++;
2940Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[0]))[0] = *grabp++;
2950Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[1]))[1] = *grabp++;
2960Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[1]))[0] = *grabp++;
2970Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[2]))[1] = *grabp++;
2980Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[2]))[0] = *grabp++;
2990Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[3]))[1] = *grabp++;
3000Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[3]))[0] = *grabp++;
3010Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[4]))[1] = *grabp++;
3020Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[4]))[0] = *grabp++;
3030Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[5]))[1] = *grabp++;
3040Sstevel@tonic-gate 	((uchar_t *)&(wbpb->bpb32.reserved[5]))[0] = *grabp++;
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate #endif	/* _BIG_ENDIAN */
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate int
yes(void)3090Sstevel@tonic-gate yes(void)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	char *affirmative = gettext("yY");
3120Sstevel@tonic-gate 	char *a = affirmative;
3130Sstevel@tonic-gate 	char input[80];
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (AlwaysYes) {
3160Sstevel@tonic-gate 		(void) printf("y\n");
3170Sstevel@tonic-gate 		return (1);
3180Sstevel@tonic-gate 	} else if (AlwaysNo) {
3190Sstevel@tonic-gate 		(void) printf("n\n");
3200Sstevel@tonic-gate 		return (0);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	if (fgets(input, sizeof (input), stdin) == NULL) {
3230Sstevel@tonic-gate 		AlwaysNo = 1;
3240Sstevel@tonic-gate 		(void) printf("n\n");
3250Sstevel@tonic-gate 		return (0);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 	while (*a) {
3280Sstevel@tonic-gate 		if (input[0] == (int)*a)
3290Sstevel@tonic-gate 			break;
3300Sstevel@tonic-gate 		a++;
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	return ((int)*a);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate char *
stat_actual_disk(char * diskname,struct stat * info,char ** suffix)3360Sstevel@tonic-gate stat_actual_disk(char *diskname, struct stat *info, char **suffix)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	char *actualdisk;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	if (stat(diskname, info)) {
3410Sstevel@tonic-gate 		/*
3420Sstevel@tonic-gate 		 *  Device named on command line doesn't exist.  That
3430Sstevel@tonic-gate 		 *  probably means there is a partition-specifying
3440Sstevel@tonic-gate 		 *  suffix attached to the actual disk name.
3450Sstevel@tonic-gate 		 */
3460Sstevel@tonic-gate 		if ((actualdisk = strdup(diskname)) == NULL) {
3470Sstevel@tonic-gate 			(void) fprintf(stderr,
3480Sstevel@tonic-gate 			    gettext("Out of memory for disk name.\n"));
3490Sstevel@tonic-gate 			exit(2);
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 		if ((*suffix = strchr(actualdisk, ':')) != NULL) {
3520Sstevel@tonic-gate 			**suffix = '\0';
3530Sstevel@tonic-gate 			(*suffix)++;
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 		if (stat(actualdisk, info)) {
3570Sstevel@tonic-gate 			perror(actualdisk);
3580Sstevel@tonic-gate 			exit(2);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 	} else {
3610Sstevel@tonic-gate 		if ((actualdisk = strdup(diskname)) == NULL) {
3620Sstevel@tonic-gate 			(void) fprintf(stderr,
3630Sstevel@tonic-gate 			    gettext("Out of memory for disk name.\n"));
3640Sstevel@tonic-gate 			exit(2);
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	return (actualdisk);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate extern void usage(void);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate void
bad_arg(char * option)3740Sstevel@tonic-gate bad_arg(char *option)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate 	(void) fprintf(stderr,
377*7563SPrasad.Singamsetty@Sun.COM 	    gettext("Unrecognized option -o %s.\n"), option);
3780Sstevel@tonic-gate 	usage();
3790Sstevel@tonic-gate 	exit(2);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate void
missing_arg(char * option)3830Sstevel@tonic-gate missing_arg(char *option)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	(void) fprintf(stderr,
386*7563SPrasad.Singamsetty@Sun.COM 	    gettext("Option %s requires a value.\n"), option);
3870Sstevel@tonic-gate 	usage();
3880Sstevel@tonic-gate 	exit(3);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate static int
parse_drvnum(char * pn)3920Sstevel@tonic-gate parse_drvnum(char *pn)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	int drvnum;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	/*
3970Sstevel@tonic-gate 	 * Determine logical drive to seek after.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if ((strlen(pn) == 1) && ((*pn >= 'c') && (*pn <= 'z'))) {
4000Sstevel@tonic-gate 		drvnum = *pn - 'c' + 1;
4010Sstevel@tonic-gate 	} else if ((*pn >= '0') && (*pn <= '9')) {
4020Sstevel@tonic-gate 		char *d;
4030Sstevel@tonic-gate 		int v = 0;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		d = pn;
4060Sstevel@tonic-gate 		while ((*d != '\0') && (*d >= '0') && (*d <= '9')) {
4070Sstevel@tonic-gate 			v *= 10;
4080Sstevel@tonic-gate 			v += *d - '0';
4090Sstevel@tonic-gate 			d++;
4100Sstevel@tonic-gate 		}
4110Sstevel@tonic-gate 		if ((*d != '\0') || (v > 24)) {
4120Sstevel@tonic-gate 			(void) fprintf(stderr,
4130Sstevel@tonic-gate 			    gettext("%s: bogus logical drive specification.\n"),
4140Sstevel@tonic-gate 			    pn);
4150Sstevel@tonic-gate 			return (-1);
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 		drvnum = v;
4180Sstevel@tonic-gate 	} else if (strcmp(pn, "boot") == 0) {
4190Sstevel@tonic-gate 		drvnum = 99;
4200Sstevel@tonic-gate 	} else {
4210Sstevel@tonic-gate 		(void) fprintf(stderr,
4220Sstevel@tonic-gate 		    gettext("%s: bogus logical drive specification.\n"), pn);
4230Sstevel@tonic-gate 		return (-1);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	return (drvnum);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  * isDosDrive()
4310Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
4320Sstevel@tonic-gate  *	and it decides if that's a systid that describes a DOS drive.  We
4330Sstevel@tonic-gate  *	use systid values defined in sys/dktp/fdisk.h.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate static int
isDosDrive(uchar_t checkMe)4360Sstevel@tonic-gate isDosDrive(uchar_t checkMe)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	return ((checkMe == DOSOS12) || (checkMe == DOSOS16) ||
4390Sstevel@tonic-gate 	    (checkMe == DOSHUGE) || (checkMe == FDISK_WINDOWS) ||
4400Sstevel@tonic-gate 	    (checkMe == FDISK_EXT_WIN) || (checkMe == FDISK_FAT95) ||
4410Sstevel@tonic-gate 	    (checkMe == DIAGPART));
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate /*
4450Sstevel@tonic-gate  * isDosExtended()
4460Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
4470Sstevel@tonic-gate  *	and it decides if that's a systid that describes an extended DOS
4480Sstevel@tonic-gate  *	partition.
4490Sstevel@tonic-gate  */
4500Sstevel@tonic-gate static int
isDosExtended(uchar_t checkMe)4510Sstevel@tonic-gate isDosExtended(uchar_t checkMe)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	return ((checkMe == EXTDOS) || (checkMe == FDISK_EXTLBA));
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate  * isBootPart()
4580Sstevel@tonic-gate  *	Boolean function.  Give it the systid field for an fdisk partition
4590Sstevel@tonic-gate  *	and it decides if that's a systid that describes a Solaris boot
4600Sstevel@tonic-gate  *	partition.
4610Sstevel@tonic-gate  */
4620Sstevel@tonic-gate static int
isBootPart(uchar_t checkMe)4630Sstevel@tonic-gate isBootPart(uchar_t checkMe)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	return (checkMe == X86BOOT);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate off64_t
findPartitionOffset(int fd,char * ldrive)4690Sstevel@tonic-gate findPartitionOffset(int fd, char *ldrive)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	struct ipart part[FD_NUMPART];
4720Sstevel@tonic-gate 	struct mboot extmboot;
4730Sstevel@tonic-gate 	struct mboot mb;
474*7563SPrasad.Singamsetty@Sun.COM 	diskaddr_t xstartsect;
4750Sstevel@tonic-gate 	off64_t nextseek = 0;
4760Sstevel@tonic-gate 	off64_t lastseek = 0;
4770Sstevel@tonic-gate 	off64_t found = 0;
4780Sstevel@tonic-gate 	off64_t error = -1;
4790Sstevel@tonic-gate 	int logicalDriveCount = 0;
4800Sstevel@tonic-gate 	int extendedPart = -1;
4810Sstevel@tonic-gate 	int primaryPart = -1;
4820Sstevel@tonic-gate 	int bootPart = -1;
483*7563SPrasad.Singamsetty@Sun.COM 	uint32_t xnumsect = 0;
4840Sstevel@tonic-gate 	int drvnum;
4850Sstevel@tonic-gate 	int driveIndex;
4860Sstevel@tonic-gate 	int i;
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * Count of drives in the current extended partition's
4890Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
4900Sstevel@tonic-gate 	 */
4910Sstevel@tonic-gate 	int extndDrives[FD_NUMPART];
4920Sstevel@tonic-gate 	int numDrives = 0;
4930Sstevel@tonic-gate 	/*
4940Sstevel@tonic-gate 	 * Count of drives (beyond primary) in master boot record's
4950Sstevel@tonic-gate 	 * FDISK table, and indexes of the drives themselves.
4960Sstevel@tonic-gate 	 */
4970Sstevel@tonic-gate 	int extraDrives[FD_NUMPART];
4980Sstevel@tonic-gate 	int numExtraDrives = 0;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	if ((drvnum = parse_drvnum(ldrive)) < 0)
5010Sstevel@tonic-gate 		return (error);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if (read(fd, &mb, sizeof (mb)) != sizeof (mb)) {
5040Sstevel@tonic-gate 		(void) fprintf(stderr,
5050Sstevel@tonic-gate 		    gettext("Couldn't read a Master Boot Record\n"));
5060Sstevel@tonic-gate 		return (error);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	if (ltohs(mb.signature) != BOOTSECSIG) {
5100Sstevel@tonic-gate 		(void) fprintf(stderr,
5110Sstevel@tonic-gate 		    gettext("Bad signature on master boot record (%x)\n"),
5120Sstevel@tonic-gate 		    ltohs(mb.signature));
5130Sstevel@tonic-gate 		return (error);
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/*
5170Sstevel@tonic-gate 	 * Copy partition table into memory
5180Sstevel@tonic-gate 	 */
5190Sstevel@tonic-gate 	(void) memcpy(part, mb.parts, sizeof (part));
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * Get a summary of what is in the Master FDISK table.
5230Sstevel@tonic-gate 	 * Normally we expect to find one partition marked as a DOS drive.
5240Sstevel@tonic-gate 	 * This partition is the one Windows calls the primary dos partition.
5250Sstevel@tonic-gate 	 * If the machine has any logical drives then we also expect
5260Sstevel@tonic-gate 	 * to find a partition marked as an extended DOS partition.
5270Sstevel@tonic-gate 	 *
5280Sstevel@tonic-gate 	 * Sometimes we'll find multiple partitions marked as DOS drives.
5290Sstevel@tonic-gate 	 * The Solaris fdisk program allows these partitions
5300Sstevel@tonic-gate 	 * to be created, but Windows fdisk no longer does.  We still need
5310Sstevel@tonic-gate 	 * to support these, though, since Windows does.  We also need to fix
5320Sstevel@tonic-gate 	 * our fdisk to behave like the Windows version.
5330Sstevel@tonic-gate 	 *
5340Sstevel@tonic-gate 	 * It turns out that some off-the-shelf media have *only* an
5350Sstevel@tonic-gate 	 * Extended partition, so we need to deal with that case as
5360Sstevel@tonic-gate 	 * well.
5370Sstevel@tonic-gate 	 *
5380Sstevel@tonic-gate 	 * Only a single (the first) Extended or Boot Partition will
5390Sstevel@tonic-gate 	 * be recognized.  Any others will be ignored.
5400Sstevel@tonic-gate 	 */
5410Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
5420Sstevel@tonic-gate 		if (isDosDrive(part[i].systid)) {
5430Sstevel@tonic-gate 			if (primaryPart < 0) {
5440Sstevel@tonic-gate 				logicalDriveCount++;
5450Sstevel@tonic-gate 				primaryPart = i;
5460Sstevel@tonic-gate 			} else {
5470Sstevel@tonic-gate 				extraDrives[numExtraDrives++] = i;
5480Sstevel@tonic-gate 			}
5490Sstevel@tonic-gate 			continue;
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 		if ((extendedPart < 0) && isDosExtended(part[i].systid)) {
5520Sstevel@tonic-gate 			extendedPart = i;
5530Sstevel@tonic-gate 			continue;
5540Sstevel@tonic-gate 		}
5550Sstevel@tonic-gate 		if ((bootPart < 0) && isBootPart(part[i].systid)) {
5560Sstevel@tonic-gate 			bootPart = i;
5570Sstevel@tonic-gate 			continue;
5580Sstevel@tonic-gate 		}
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (drvnum == BOOT_PARTITION_DRIVE) {
5620Sstevel@tonic-gate 		if (bootPart < 0) {
5630Sstevel@tonic-gate 			(void) fprintf(stderr,
5640Sstevel@tonic-gate 			    gettext("No boot partition found on drive\n"));
5650Sstevel@tonic-gate 			return (error);
5660Sstevel@tonic-gate 		}
5670Sstevel@tonic-gate 		found = ltohi(part[bootPart].relsect) * BPSEC;
5680Sstevel@tonic-gate 		return (found);
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (drvnum == PRIMARY_DOS_DRIVE && primaryPart >= 0) {
5720Sstevel@tonic-gate 		found = ltohi(part[primaryPart].relsect) * BPSEC;
5730Sstevel@tonic-gate 		return (found);
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * We are not looking for the C: drive (or there was no primary
5780Sstevel@tonic-gate 	 * drive found), so we had better have an extended partition or
5790Sstevel@tonic-gate 	 * extra drives in the Master FDISK table.
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	if ((extendedPart < 0) && (numExtraDrives == 0)) {
5820Sstevel@tonic-gate 		(void) fprintf(stderr,
5830Sstevel@tonic-gate 		    gettext("No such logical drive "
5840Sstevel@tonic-gate 		    "(missing extended partition entry)\n"));
5850Sstevel@tonic-gate 		return (error);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if (extendedPart >= 0) {
5890Sstevel@tonic-gate 		nextseek = xstartsect = ltohi(part[extendedPart].relsect);
5900Sstevel@tonic-gate 		xnumsect = ltohi(part[extendedPart].numsect);
5910Sstevel@tonic-gate 		do {
5920Sstevel@tonic-gate 			/*
5930Sstevel@tonic-gate 			 *  If the seek would not cause us to change
5940Sstevel@tonic-gate 			 *  position on the drive, then we're out of
5950Sstevel@tonic-gate 			 *  extended partitions to examine.
5960Sstevel@tonic-gate 			 */
5970Sstevel@tonic-gate 			if (nextseek == lastseek)
5980Sstevel@tonic-gate 				break;
5990Sstevel@tonic-gate 			logicalDriveCount += numDrives;
6000Sstevel@tonic-gate 			/*
6010Sstevel@tonic-gate 			 *  Seek the next extended partition, and find
6020Sstevel@tonic-gate 			 *  logical drives within it.
6030Sstevel@tonic-gate 			 */
6040Sstevel@tonic-gate 			if (lseek64(fd, nextseek * BPSEC, SEEK_SET) < 0 ||
6050Sstevel@tonic-gate 			    read(fd, &extmboot, sizeof (extmboot)) !=
606*7563SPrasad.Singamsetty@Sun.COM 			    sizeof (extmboot)) {
6070Sstevel@tonic-gate 				perror(gettext("Unable to read extended "
608*7563SPrasad.Singamsetty@Sun.COM 				    "partition record"));
6090Sstevel@tonic-gate 				return (error);
6100Sstevel@tonic-gate 			}
6110Sstevel@tonic-gate 			(void) memcpy(part, extmboot.parts, sizeof (part));
6120Sstevel@tonic-gate 			lastseek = nextseek;
6130Sstevel@tonic-gate 			if (ltohs(extmboot.signature) != MBB_MAGIC) {
6140Sstevel@tonic-gate 				(void) fprintf(stderr,
6150Sstevel@tonic-gate 				    gettext("Bad signature on "
6160Sstevel@tonic-gate 				    "extended partition\n"));
6170Sstevel@tonic-gate 				return (error);
6180Sstevel@tonic-gate 			}
6190Sstevel@tonic-gate 			/*
6200Sstevel@tonic-gate 			 *  Count up drives, and track where the next
6210Sstevel@tonic-gate 			 *  extended partition is in case we need it.  We
6220Sstevel@tonic-gate 			 *  are expecting only one extended partition.  If
6230Sstevel@tonic-gate 			 *  there is more than one we'll only go to the
6240Sstevel@tonic-gate 			 *  first one we see, but warn about ignoring.
6250Sstevel@tonic-gate 			 */
6260Sstevel@tonic-gate 			numDrives = 0;
6270Sstevel@tonic-gate 			for (i = 0; i < FD_NUMPART; i++) {
6280Sstevel@tonic-gate 				if (isDosDrive(part[i].systid)) {
6290Sstevel@tonic-gate 					extndDrives[numDrives++] = i;
6300Sstevel@tonic-gate 					continue;
6310Sstevel@tonic-gate 				} else if (isDosExtended(part[i].systid)) {
6320Sstevel@tonic-gate 					if (nextseek != lastseek) {
6330Sstevel@tonic-gate 						/*
6340Sstevel@tonic-gate 						 * Already found an extended
6350Sstevel@tonic-gate 						 * partition in this table.
6360Sstevel@tonic-gate 						 */
6370Sstevel@tonic-gate 						(void) fprintf(stderr,
6380Sstevel@tonic-gate 						    gettext("WARNING: "
6390Sstevel@tonic-gate 						    "Ignoring unexpected "
6400Sstevel@tonic-gate 						    "additional extended "
6410Sstevel@tonic-gate 						    "partition"));
6420Sstevel@tonic-gate 						continue;
6430Sstevel@tonic-gate 					}
6440Sstevel@tonic-gate 					nextseek = xstartsect +
6450Sstevel@tonic-gate 					    ltohi(part[i].relsect);
6460Sstevel@tonic-gate 					continue;
6470Sstevel@tonic-gate 				}
6480Sstevel@tonic-gate 			}
6490Sstevel@tonic-gate 		} while (drvnum > logicalDriveCount + numDrives);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		if (drvnum <= logicalDriveCount + numDrives) {
6520Sstevel@tonic-gate 			/*
6530Sstevel@tonic-gate 			 * The number of logical drives we've found thus
6540Sstevel@tonic-gate 			 * far is enough to get us to the one we were
6550Sstevel@tonic-gate 			 * searching for.
6560Sstevel@tonic-gate 			 */
6570Sstevel@tonic-gate 			driveIndex = logicalDriveCount + numDrives - drvnum;
6580Sstevel@tonic-gate 			found =
6590Sstevel@tonic-gate 			    ltohi(part[extndDrives[driveIndex]].relsect) +
6600Sstevel@tonic-gate 			    lastseek;
6610Sstevel@tonic-gate 			if (found > (xstartsect + xnumsect)) {
6620Sstevel@tonic-gate 				(void) fprintf(stderr,
6630Sstevel@tonic-gate 				    gettext("Logical drive start sector (%d) "
6640Sstevel@tonic-gate 				    "is not within the partition!\n"), found);
6650Sstevel@tonic-gate 				return (error);
6660Sstevel@tonic-gate 			} else {
6670Sstevel@tonic-gate 				found *= BPSEC;
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 			return (found);
6700Sstevel@tonic-gate 		} else {
6710Sstevel@tonic-gate 			/*
6720Sstevel@tonic-gate 			 * We ran out of extended dos partition
6730Sstevel@tonic-gate 			 * drives.  The only hope now is to go
6740Sstevel@tonic-gate 			 * back to extra drives defined in the master
6750Sstevel@tonic-gate 			 * fdisk table.  But we overwrote that table
6760Sstevel@tonic-gate 			 * already, so we must load it in again.
6770Sstevel@tonic-gate 			 */
6780Sstevel@tonic-gate 			logicalDriveCount += numDrives;
6790Sstevel@tonic-gate 			(void) memcpy(part, mb.parts, sizeof (part));
6800Sstevel@tonic-gate 		}
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 	/*
6830Sstevel@tonic-gate 	 *  Still haven't found the drive, is it an extra
6840Sstevel@tonic-gate 	 *  drive defined in the main FDISK table?
6850Sstevel@tonic-gate 	 */
6860Sstevel@tonic-gate 	if (drvnum <= logicalDriveCount + numExtraDrives) {
6870Sstevel@tonic-gate 		driveIndex = logicalDriveCount + numExtraDrives - drvnum;
6880Sstevel@tonic-gate 		found = ltohi(part[extraDrives[driveIndex]].relsect) * BPSEC;
6890Sstevel@tonic-gate 		return (found);
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 	return (error);
6920Sstevel@tonic-gate }
693