1/*
2 * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
3 *
4 * IMPORTANT:  This Apple software is supplied to you by Apple Inc. ("Apple") in
5 * consideration of your agreement to the following terms, and your use, installation,
6 * modification or redistribution of this Apple software constitutes acceptance of these
7 * terms.  If you do not agree with these terms, please do not use, install, modify or
8 * redistribute this Apple software.
9 *
10 * In consideration of your agreement to abide by the following terms, and subject to these
11 * terms, Apple grants you a personal, non exclusive license, under Apple�s copyrights in this
12 * original Apple software (the �Apple Software�), to use, reproduce, modify and redistribute
13 * the Apple Software, with or without modifications, in source and/or binary forms; provided
14 * that if you redistribute the Apple Software in its entirety and without modifications, you
15 * must retain this notice and the following text and disclaimers in all such redistributions
16 * of the Apple Software.  Neither the name, trademarks, service marks or logos of Apple
17 * Computer, Inc. may be used to endorse or promote products derived from the Apple Software
18 * without specific prior written permission from Apple. Except as expressly stated in this
19 * notice, no other rights or licenses, express or implied, are granted by Apple herein,
20 * including but not limited to any patent rights that may be infringed by your derivative
21 * works or by other works in which the Apple Software may be incorporated.
22 *
23 * The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
24 * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-
25 * INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE
26 * SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
27 *
28 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
31 * REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
32 * WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
33 * OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36
37//-----------------------------------------------------------------------------
38//	Includes
39//-----------------------------------------------------------------------------
40
41#include <stdio.h>
42#include <stdbool.h>
43#include <unistd.h>
44#include <mach/mach_error.h>
45#include <CoreFoundation/CoreFoundation.h>
46#include "Probing.h"
47
48
49//-----------------------------------------------------------------------------
50//	Macros
51//-----------------------------------------------------------------------------
52
53#define DEBUG 0
54
55#define DEBUG_ASSERT_COMPONENT_NAME_STRING "SCSITargetProberUnixTool"
56
57#if DEBUG
58#define DEBUG_ASSERT_MESSAGE(componentNameString,	\
59							 assertionString,		\
60							 exceptionLabelString,	\
61							 errorString,			\
62							 fileName,				\
63							 lineNumber,			\
64							 errorCode)				\
65DebugAssert(componentNameString,					\
66					   assertionString,				\
67					   exceptionLabelString,		\
68					   errorString,					\
69					   fileName,					\
70					   lineNumber,					\
71					   errorCode)					\
72
73static void
74DebugAssert ( const char *	componentNameString,
75			  const char *	assertionString,
76			  const char *	exceptionLabelString,
77			  const char *	errorString,
78			  const char *	fileName,
79			  long			lineNumber,
80			  int			errorCode )
81{
82
83	if ( ( assertionString != NULL ) && ( *assertionString != '\0' ) )
84		printf ( "Assertion failed: %s: %s\n", componentNameString, assertionString );
85	else
86		printf ( "Check failed: %s:\n", componentNameString );
87	if ( exceptionLabelString != NULL )
88		printf ( "	 %s\n", exceptionLabelString );
89	if ( errorString != NULL )
90		printf ( "	 %s\n", errorString );
91	if ( fileName != NULL )
92		printf ( "	 file: %s\n", fileName );
93	if ( lineNumber != 0 )
94		printf ( "	 line: %ld\n", lineNumber );
95	if ( errorCode != 0 )
96		printf ( "	 error: %d\n", errorCode );
97
98}
99
100#endif	/* DEBUG */
101
102#include <AssertMacros.h>
103
104
105//-----------------------------------------------------------------------------
106//	Prototypes
107//-----------------------------------------------------------------------------
108
109static IOReturn
110ParseArguments ( int argc, const char * argv[],
111				 UInt64 * domainID, SCSITargetIdentifier * targetID );
112
113static void
114PrintUsage ( void );
115
116
117//-----------------------------------------------------------------------------
118//	main - Our main entry point
119//-----------------------------------------------------------------------------
120
121int
122main ( int argc, const char * argv[] )
123{
124
125	int						returnCode	= 0;
126	IOReturn				result		= kIOReturnSuccess;
127	UInt64					domainID	= 0;
128	SCSITargetIdentifier	targetID	= 0;
129
130	result = ParseArguments ( argc, argv, &domainID, &targetID );
131	require_action ( ( result == 0 ), ErrorExit, PrintUsage ( ); returnCode = 1 );
132
133	printf ( "SCSITargetProber: Probing device for domain = %lld, targetID = %lld\n", domainID, targetID );
134
135	result = ReprobeDomainTarget ( domainID, targetID );
136	require_action ( ( result == 0 ), ErrorExit, printf ( "Error = %s (0x%08x) reprobing device\n", mach_error_string ( result ), result ); returnCode = 2 );
137
138	return 0;
139
140
141ErrorExit:
142
143
144	return returnCode;
145
146}
147
148
149//-----------------------------------------------------------------------------
150//	ParseArguments - Parses argument list
151//-----------------------------------------------------------------------------
152
153static IOReturn
154ParseArguments ( int argc, const char * argv[],
155				 UInt64 * domainID, SCSITargetIdentifier * targetID )
156{
157
158	IOReturn	result	= kIOReturnSuccess;
159	int			ch;
160
161	while ( ( ch = getopt ( argc, ( char * const * ) argv, "d:t:" ) ) != -1 )
162	{
163
164		switch ( ch )
165		{
166
167			case 'd':
168				*domainID = strtoull ( optarg, ( char ** ) NULL, 10 );
169				break;
170
171			case 't':
172				*targetID = strtoull ( optarg, ( char ** ) NULL, 10 );
173				break;
174
175			default:
176				result = kIOReturnBadArgument;
177				break;
178
179		}
180
181	}
182
183	return result;
184
185}
186
187
188//-----------------------------------------------------------------------------
189//	PrintUsage - Prints out usage
190//-----------------------------------------------------------------------------
191
192void
193PrintUsage ( void )
194{
195
196	printf ( "\n" );
197	printf ( "Usage: stp -d domainID -t targetID\n" );
198	printf ( "\t\t" );
199	printf ( "-d This option specifies which SCSI Domain on which to find the target for probing\n" );
200	printf ( "\t\t" );
201	printf ( "-t This option specifices which SCSI Target Identifier should be probed\n" );
202	printf ( "\n" );
203
204}