]>
git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
895f27b986fe2c1107991ca9540746ea680e6991
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <IOKit/IOBSD.h>
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IOService.h>
31 #include <IOKit/IODeviceTreeSupport.h>
32 #include <IOKit/IOKitKeys.h>
33 #include <IOKit/IOPlatformExpert.h>
37 #include <pexpert/pexpert.h>
38 #include <kern/clock.h>
39 #include <uuid/uuid.h>
41 // how long to wait for matching root device, secs
42 #define ROOTDEVICETIMEOUT 60
44 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
45 extern dev_t
mdevlookup(int devid
);
46 extern void mdevremoveall(void);
51 IOService::publishResource("IOBSD");
53 return( kIOReturnSuccess
);
56 OSDictionary
* IOBSDNameMatching( const char * name
)
59 const OSSymbol
* str
= 0;
63 dict
= IOService::serviceMatching( gIOServiceKey
);
66 str
= OSSymbol::withCString( name
);
69 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
84 OSDictionary
* IOUUIDMatching( void )
86 return IOService::resourceMatching( "boot-uuid-media" );
90 OSDictionary
* IOCDMatching( void )
95 dict
= IOService::serviceMatching( "IOMedia" );
97 IOLog("Unable to find IOMedia\n");
101 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
107 dict
->setObject( "Content Hint", (OSObject
*)str
);
112 OSDictionary
* IONetworkMatching( const char * path
,
113 char * buf
, int maxLen
)
115 OSDictionary
* matching
= 0;
124 len
= strlen( kIODeviceTreePlane
":" );
129 strcpy( buf
, kIODeviceTreePlane
":" );
132 // remove parameters following ':' from the path
133 skip
= strchr( path
, ':');
141 strncpy( comp
, path
, len
);
144 matching
= IOService::serviceMatching( "IONetworkInterface" );
147 dict
= IOService::addLocation( matching
);
151 str
= OSString::withCString( buf
);
154 dict
->setObject( kIOPathMatchKey
, str
);
167 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
169 OSDictionary
* matching
;
170 OSDictionary
* propDict
= 0;
171 const OSSymbol
* str
= 0;
172 char networkType
[128];
175 matching
= IOService::serviceMatching( "IONetworkInterface" );
179 propDict
= OSDictionary::withCapacity(1);
183 str
= OSSymbol::withCString( prefix
);
187 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
191 // see if we're contrained to netroot off of specific network type
192 if(PE_parse_boot_argn( "network-type", networkType
, 128 ))
194 str
= OSSymbol::withCString( networkType
);
197 propDict
->setObject( "IONetworkRootType", str
);
203 if ( matching
->setObject( gIOPropertyMatchKey
,
204 (OSObject
*) propDict
) != true )
214 if ( matching
) matching
->release();
215 if ( propDict
) propDict
->release();
216 if ( str
) str
->release();
221 static bool IORegisterNetworkInterface( IOService
* netif
)
223 // A network interface is typically named and registered
224 // with BSD after receiving a request from a user space
225 // "namer". However, for cases when the system needs to
226 // root from the network, this registration task must be
227 // done inside the kernel and completed before the root
228 // device is handed to BSD.
233 OSDictionary
* dict
= 0;
236 enum { kMaxPathLen
= 512 };
239 stack
= IOService::waitForService(
240 IOService::serviceMatching("IONetworkStack") );
241 if ( stack
== 0 ) break;
243 dict
= OSDictionary::withCapacity(3);
244 if ( dict
== 0 ) break;
246 zero
= OSNumber::withNumber((UInt64
) 0, 32);
247 if ( zero
== 0 ) break;
249 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
250 if ( pathBuf
== 0 ) break;
253 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
256 path
= OSString::withCStringNoCopy( pathBuf
);
257 if ( path
== 0 ) break;
259 dict
->setObject( "IOInterfaceUnit", zero
);
260 dict
->setObject( kIOPathMatchKey
, path
);
262 stack
->setProperties( dict
);
266 if ( zero
) zero
->release();
267 if ( path
) path
->release();
268 if ( dict
) dict
->release();
269 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
271 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
274 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
284 // scan the tail of the path for "@unit:partition"
286 // Have to get the full path to the controller - an alias may
287 // tell us next to nothing, like "hd:8"
288 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
290 look
= path
+ strlen( path
);
292 while( look
!= path
) {
293 if( *(--look
) == c
) {
295 partition
= strtol( look
+ 1, 0, 0 );
297 } else if( c
== '@') {
298 unit
= strtol( look
+ 1, &comp
, 16 );
301 lun
= strtol( comp
+ 1, 0, 16 );
305 } else if( c
== '/') {
311 if( alias
&& (look
== path
)) {
313 look
= path
+ strlen( path
);
317 if( c
|| unit
== -1 || partition
== -1)
320 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
321 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
322 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
325 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
326 comp
= buf
+ strlen( buf
);
329 strcpy( comp
, alias
);
330 comp
+= strlen( alias
);
333 if ( (look
- path
)) {
334 strncpy( comp
, path
, look
- path
);
340 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
344 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
349 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
356 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
358 OSDictionary
* matching
;
363 /* need to look up path, get device type,
364 call matching help based on device type */
366 matching
= IODiskMatching( path
, buf
, maxLen
);
372 len
= strlen( kIODeviceTreePlane
":" );
377 strcpy( buf
, kIODeviceTreePlane
":" );
380 len
= strlen( path
);
384 strncpy( comp
, path
, len
);
387 matching
= OSDictionary::withCapacity( 1 );
391 str
= OSString::withCString( buf
);
394 matching
->setObject( kIOPathMatchKey
, str
);
407 IOService
* IOFindMatchingChild( IOService
* service
)
409 // find a matching child service
410 IOService
* child
= 0;
411 OSIterator
* iter
= service
->getClientIterator();
413 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
414 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
419 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
425 dict
->setObject( "Content", (OSObject
*)str
);
427 if ( child
->compareProperty( dict
, "Content" ) ) {
432 IOService
* subchild
= IOFindMatchingChild( child
);
443 static int didRam
= 0;
445 kern_return_t
IOFindBSDRoot( char * rootName
,
446 dev_t
* root
, u_int32_t
* oflags
)
450 IORegistryEntry
* regEntry
;
451 OSDictionary
* matching
= 0;
455 UInt32
*ramdParms
= 0;
459 bool findHFSChild
= false;
460 char * mediaProperty
= 0;
462 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
464 const char * look
= 0;
466 bool forceNet
= false;
467 bool debugInfoPrintedOnce
= false;
468 const char * uuidStr
= NULL
;
470 static int mountAttempts
= 0;
478 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
480 return( kIOReturnNoMemory
);
481 rdBootVar
= str
+ kMaxPathBuf
;
483 if (!PE_parse_boot_argn("rd", rdBootVar
, kMaxBootVar
)
484 && !PE_parse_boot_argn("rootdev", rdBootVar
, kMaxBootVar
))
488 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
489 data
= OSDynamicCast(OSData
, regEntry
->getProperty( "root-matching" ));
491 matching
= OSDynamicCast(OSDictionary
, OSUnserializeXML((char *)data
->getBytesNoCopy()));
497 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
499 uuidStr
= (const char*)data
->getBytesNoCopy();
500 OSString
*uuidString
= OSString::withCString( uuidStr
);
502 // match the boot-args boot-uuid processing below
504 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
505 IOService::publishResource( "boot-uuid", uuidString
);
506 uuidString
->release();
507 matching
= IOUUIDMatching();
508 mediaProperty
= "boot-uuid-media";
516 // else try for an OF Path
517 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
521 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
522 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
528 if( data
&& !uuidStr
)
529 look
= (const char *) data
->getBytesNoCopy();
531 if( rdBootVar
[0] == '*') {
532 look
= rdBootVar
+ 1;
535 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
536 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
544 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
545 // It will become /dev/mdx, where x is 0-f.
548 if(!didRam
) { /* Have we already build this ram disk? */
549 didRam
= 1; /* Remember we did this */
550 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
551 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
552 if(data
) { /* We found one */
554 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
555 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
557 regEntry
->release(); /* Toss the entry */
562 // Now check if we are trying to root on a memory device
565 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
566 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
567 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
569 xchar
= xchar
& ~' '; /* Fold to upper case */
570 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
571 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
572 dchar
= dchar
| ' '; /* Fold to lower case */
574 else xchar
= -1; /* Show bogus */
576 if(xchar
>= 0) { /* Do we have a valid memory device name? */
577 *root
= mdevlookup(xchar
); /* Find the device number */
578 if(*root
>= 0) { /* Did we find one? */
580 rootName
[0] = 'm'; /* Build root name */
581 rootName
[1] = 'd'; /* Build root name */
582 rootName
[2] = dchar
; /* Build root name */
583 rootName
[3] = 0; /* Build root name */
584 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
585 *oflags
= 0; /* Show that this is not network */
586 goto iofrootx
; /* Join common exit... */
588 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
593 // from OpenFirmware path
594 IOLog("From path: \"%s\", ", look
);
597 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
598 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
600 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
605 if( (!matching
) && rdBootVar
[0] ) {
611 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
612 matching
= IONetworkNamePrefixMatching( "en" );
613 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
614 matching
= IOCDMatching();
616 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
618 OSString
*uuidString
;
620 uuid
= (char *)IOMalloc( kMaxBootVar
);
623 if (!PE_parse_boot_argn( "boot-uuid", uuid
, kMaxBootVar
)) {
624 panic( "rd=uuid but no boot-uuid=<value> specified" );
626 uuidString
= OSString::withCString( uuid
);
628 IOService::publishResource( "boot-uuid", uuidString
);
629 uuidString
->release();
630 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
631 matching
= IOUUIDMatching();
632 mediaProperty
= "boot-uuid-media";
634 IOFree( uuid
, kMaxBootVar
);
637 matching
= IOBSDNameMatching( look
);
643 // Match any HFS media
645 matching
= IOService::serviceMatching( "IOMedia" );
646 astring
= OSString::withCStringNoCopy("Apple_HFS");
648 matching
->setObject("Content", astring
);
653 if( true && matching
) {
654 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
656 if( matching
->serialize( s
)) {
657 IOLog( "Waiting on %s\n", s
->text() );
663 t
.tv_sec
= ROOTDEVICETIMEOUT
;
666 service
= IOService::waitForService( matching
, &t
);
667 if( (!service
) || (mountAttempts
== 10)) {
668 PE_display_icon( 0, "noroot");
669 IOLog( "Still waiting for root device\n" );
671 if( !debugInfoPrintedOnce
) {
672 debugInfoPrintedOnce
= true;
673 if( gIOKitDebug
& kIOLogDTree
) {
674 IOLog("\nDT plane:\n");
675 IOPrintPlane( gIODTPlane
);
677 if( gIOKitDebug
& kIOLogServiceTree
) {
678 IOLog("\nService plane:\n");
679 IOPrintPlane( gIOServicePlane
);
681 if( gIOKitDebug
& kIOLogMemory
)
688 if ( service
&& findHFSChild
) {
690 // wait for children services to finish registering
692 t
.tv_sec
= ROOTDEVICETIMEOUT
;
694 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
697 IOLog( "Waiting for child registration\n" );
700 // look for a subservice with an Apple_HFS child
701 IOService
* subservice
= IOFindMatchingChild( service
);
702 if ( subservice
) service
= subservice
;
703 } else if ( service
&& mediaProperty
) {
704 service
= (IOService
*)service
->getProperty(mediaProperty
);
710 // If the IOService we matched to is a subclass of IONetworkInterface,
711 // then make sure it has been registered with BSD and has a BSD name
715 && service
->metaCast( "IONetworkInterface" )
716 && !IORegisterNetworkInterface( service
) )
724 service
->getPath( str
, &len
, gIOServicePlane
);
725 IOLog( "Got boot device = %s\n", str
);
727 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
729 strcpy( rootName
, iostr
->getCStringNoCopy() );
730 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
732 mjr
= off
->unsigned32BitValue();
733 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
735 mnr
= off
->unsigned32BitValue();
737 if( service
->metaCast( "IONetworkInterface" ))
742 IOLog( "Wait for root failed\n" );
743 strcpy( rootName
, "en0");
747 IOLog( "BSD root: %s", rootName
);
749 IOLog(", major %d, minor %d\n", mjr
, mnr
);
753 *root
= makedev( mjr
, mnr
);
756 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
759 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
761 IOService::getPlatform()->waitQuiet();
762 if( gIOKitDebug
& kIOLogDTree
) {
763 IOLog("\nDT plane:\n");
764 IOPrintPlane( gIODTPlane
);
766 if( gIOKitDebug
& kIOLogServiceTree
) {
767 IOLog("\nService plane:\n");
768 IOPrintPlane( gIOServicePlane
);
770 if( gIOKitDebug
& kIOLogMemory
)
774 return( kIOReturnSuccess
);
777 void IOSecureBSDRoot(const char * rootName
)
781 IOPlatformExpert
*pe
;
782 const OSSymbol
*functionName
= OSSymbol::withCStringNoCopy("SecureRootName");
784 while ((pe
= IOService::getPlatform()) == 0) IOSleep(1 * 1000);
786 // Returns kIOReturnNotPrivileged is the root device is not secure.
787 // Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
788 result
= pe
->callPlatformFunction(functionName
, false, (void *)rootName
, (void *)0, (void *)0, (void *)0);
790 functionName
->release();
792 if (result
== kIOReturnNotPrivileged
) mdevremoveall();
797 IOBSDRegistryEntryForDeviceTree(char * path
)
799 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
803 IOBSDRegistryEntryRelease(void * entry
)
805 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
813 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
817 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
819 data
= (OSData
*) regEntry
->getProperty(property_name
);
821 *packet_length
= data
->getLength();
822 return (data
->getBytesNoCopy());
827 kern_return_t
IOBSDGetPlatformUUID( uuid_t uuid
, mach_timespec_t timeout
)
829 IOService
* resources
;
832 resources
= IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey
), &timeout
);
833 if ( resources
== 0 ) return KERN_OPERATION_TIMED_OUT
;
835 string
= ( OSString
* ) IOService::getPlatform( )->getProvider( )->getProperty( kIOPlatformUUIDKey
);
836 if ( string
== 0 ) return KERN_NOT_SUPPORTED
;
838 uuid_parse( string
->getCStringNoCopy( ), uuid
);