]>
git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
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>
40 // how long to wait for matching root device, secs
41 #define ROOTDEVICETIMEOUT 60
43 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
44 extern dev_t
mdevlookup(int devid
);
49 IOService::publishResource("IOBSD");
51 return( kIOReturnSuccess
);
54 OSDictionary
* IOBSDNameMatching( const char * name
)
57 const OSSymbol
* str
= 0;
61 dict
= IOService::serviceMatching( gIOServiceKey
);
64 str
= OSSymbol::withCString( name
);
67 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
82 OSDictionary
* IOUUIDMatching( void )
84 return IOService::resourceMatching( "boot-uuid-media" );
88 OSDictionary
* IOCDMatching( void )
93 dict
= IOService::serviceMatching( "IOMedia" );
95 IOLog("Unable to find IOMedia\n");
99 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
105 dict
->setObject( "Content Hint", (OSObject
*)str
);
110 OSDictionary
* IONetworkMatching( const char * path
,
111 char * buf
, int maxLen
)
113 OSDictionary
* matching
= 0;
122 len
= strlen( kIODeviceTreePlane
":" );
127 strcpy( buf
, kIODeviceTreePlane
":" );
130 // remove parameters following ':' from the path
131 skip
= strchr( path
, ':');
139 strncpy( comp
, path
, len
);
142 matching
= IOService::serviceMatching( "IONetworkInterface" );
145 dict
= IOService::addLocation( matching
);
149 str
= OSString::withCString( buf
);
152 dict
->setObject( kIOPathMatchKey
, str
);
165 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
167 OSDictionary
* matching
;
168 OSDictionary
* propDict
= 0;
169 const OSSymbol
* str
= 0;
172 matching
= IOService::serviceMatching( "IONetworkInterface" );
176 propDict
= OSDictionary::withCapacity(1);
180 str
= OSSymbol::withCString( prefix
);
184 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
188 if ( matching
->setObject( gIOPropertyMatchKey
,
189 (OSObject
*) propDict
) != true )
199 if ( matching
) matching
->release();
200 if ( propDict
) propDict
->release();
201 if ( str
) str
->release();
206 static bool IORegisterNetworkInterface( IOService
* netif
)
208 // A network interface is typically named and registered
209 // with BSD after receiving a request from a user space
210 // "namer". However, for cases when the system needs to
211 // root from the network, this registration task must be
212 // done inside the kernel and completed before the root
213 // device is handed to BSD.
218 OSDictionary
* dict
= 0;
221 enum { kMaxPathLen
= 512 };
224 stack
= IOService::waitForService(
225 IOService::serviceMatching("IONetworkStack") );
226 if ( stack
== 0 ) break;
228 dict
= OSDictionary::withCapacity(3);
229 if ( dict
== 0 ) break;
231 zero
= OSNumber::withNumber((UInt64
) 0, 32);
232 if ( zero
== 0 ) break;
234 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
235 if ( pathBuf
== 0 ) break;
238 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
241 path
= OSString::withCStringNoCopy( pathBuf
);
242 if ( path
== 0 ) break;
244 dict
->setObject( "IOInterfaceUnit", zero
);
245 dict
->setObject( kIOPathMatchKey
, path
);
247 stack
->setProperties( dict
);
251 if ( zero
) zero
->release();
252 if ( path
) path
->release();
253 if ( dict
) dict
->release();
254 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
256 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
259 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
269 // scan the tail of the path for "@unit:partition"
271 // Have to get the full path to the controller - an alias may
272 // tell us next to nothing, like "hd:8"
273 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
275 look
= path
+ strlen( path
);
277 while( look
!= path
) {
278 if( *(--look
) == c
) {
280 partition
= strtol( look
+ 1, 0, 0 );
282 } else if( c
== '@') {
283 unit
= strtol( look
+ 1, &comp
, 16 );
286 lun
= strtol( comp
+ 1, 0, 16 );
290 } else if( c
== '/') {
296 if( alias
&& (look
== path
)) {
298 look
= path
+ strlen( path
);
302 if( c
|| unit
== -1 || partition
== -1)
305 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
306 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
307 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
310 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
311 comp
= buf
+ strlen( buf
);
314 strcpy( comp
, alias
);
315 comp
+= strlen( alias
);
318 if ( (look
- path
)) {
319 strncpy( comp
, path
, look
- path
);
325 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
329 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
334 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
341 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
343 OSDictionary
* matching
;
348 /* need to look up path, get device type,
349 call matching help based on device type */
351 matching
= IODiskMatching( path
, buf
, maxLen
);
357 len
= strlen( kIODeviceTreePlane
":" );
362 strcpy( buf
, kIODeviceTreePlane
":" );
365 len
= strlen( path
);
369 strncpy( comp
, path
, len
);
372 matching
= OSDictionary::withCapacity( 1 );
376 str
= OSString::withCString( buf
);
379 matching
->setObject( kIOPathMatchKey
, str
);
392 IOService
* IOFindMatchingChild( IOService
* service
)
394 // find a matching child service
395 IOService
* child
= 0;
396 OSIterator
* iter
= service
->getClientIterator();
398 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
399 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
404 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
410 dict
->setObject( "Content", (OSObject
*)str
);
412 if ( child
->compareProperty( dict
, "Content" ) ) {
417 IOService
* subchild
= IOFindMatchingChild( child
);
428 static int didRam
= 0;
430 kern_return_t
IOFindBSDRoot( char * rootName
,
431 dev_t
* root
, u_int32_t
* oflags
)
435 IORegistryEntry
* regEntry
;
436 OSDictionary
* matching
= 0;
440 UInt32
*ramdParms
= 0;
444 bool findHFSChild
= false;
445 char * mediaProperty
= 0;
447 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
449 const char * look
= 0;
451 bool forceNet
= false;
452 bool debugInfoPrintedOnce
= false;
453 const char * uuidStr
= NULL
;
455 static int mountAttempts
= 0;
463 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
465 return( kIOReturnNoMemory
);
466 rdBootVar
= str
+ kMaxPathBuf
;
468 if (!PE_parse_boot_arg("rd", rdBootVar
)
469 && !PE_parse_boot_arg("rootdev", rdBootVar
))
473 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
474 data
= OSDynamicCast(OSData
, regEntry
->getProperty( "root-matching" ));
476 matching
= OSDynamicCast(OSDictionary
, OSUnserializeXML((char *)data
->getBytesNoCopy()));
482 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
484 uuidStr
= (const char*)data
->getBytesNoCopy();
485 OSString
*uuidString
= OSString::withCString( uuidStr
);
487 // match the boot-args boot-uuid processing below
489 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
490 IOService::publishResource( "boot-uuid", uuidString
);
491 uuidString
->release();
492 matching
= IOUUIDMatching();
493 mediaProperty
= "boot-uuid-media";
501 // else try for an OF Path
502 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
506 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
507 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
513 if( data
&& !uuidStr
)
514 look
= (const char *) data
->getBytesNoCopy();
516 if( rdBootVar
[0] == '*') {
517 look
= rdBootVar
+ 1;
520 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
521 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
529 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
530 // It will become /dev/mdx, where x is 0-f.
533 if(!didRam
) { /* Have we already build this ram disk? */
534 didRam
= 1; /* Remember we did this */
535 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
536 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
537 if(data
) { /* We found one */
539 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
540 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
542 regEntry
->release(); /* Toss the entry */
547 // Now check if we are trying to root on a memory device
550 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
551 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
552 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
554 xchar
= xchar
& ~' '; /* Fold to upper case */
555 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
556 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
557 dchar
= dchar
| ' '; /* Fold to lower case */
559 else xchar
= -1; /* Show bogus */
561 if(xchar
>= 0) { /* Do we have a valid memory device name? */
562 *root
= mdevlookup(xchar
); /* Find the device number */
563 if(*root
>= 0) { /* Did we find one? */
565 rootName
[0] = 'm'; /* Build root name */
566 rootName
[1] = 'd'; /* Build root name */
567 rootName
[2] = dchar
; /* Build root name */
568 rootName
[3] = 0; /* Build root name */
569 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
570 *oflags
= 0; /* Show that this is not network */
571 goto iofrootx
; /* Join common exit... */
573 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
578 // from OpenFirmware path
579 IOLog("From path: \"%s\", ", look
);
582 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
583 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
585 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
590 if( (!matching
) && rdBootVar
[0] ) {
596 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
597 matching
= IONetworkNamePrefixMatching( "en" );
598 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
599 matching
= IOCDMatching();
601 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
603 OSString
*uuidString
;
605 uuid
= (char *)IOMalloc( kMaxBootVar
);
608 if (!PE_parse_boot_arg( "boot-uuid", uuid
)) {
609 panic( "rd=uuid but no boot-uuid=<value> specified" );
611 uuidString
= OSString::withCString( uuid
);
613 IOService::publishResource( "boot-uuid", uuidString
);
614 uuidString
->release();
615 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
616 matching
= IOUUIDMatching();
617 mediaProperty
= "boot-uuid-media";
619 IOFree( uuid
, kMaxBootVar
);
622 matching
= IOBSDNameMatching( look
);
629 matching
= IOService::serviceMatching( "IOMedia" );
630 astring
= OSString::withCStringNoCopy("Apple_HFS");
632 matching
->setObject("Content", astring
);
637 if( true && matching
) {
638 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
640 if( matching
->serialize( s
)) {
641 IOLog( "Waiting on %s\n", s
->text() );
647 t
.tv_sec
= ROOTDEVICETIMEOUT
;
650 service
= IOService::waitForService( matching
, &t
);
651 if( (!service
) || (mountAttempts
== 10)) {
652 PE_display_icon( 0, "noroot");
653 IOLog( "Still waiting for root device\n" );
655 if( !debugInfoPrintedOnce
) {
656 debugInfoPrintedOnce
= true;
657 if( gIOKitDebug
& kIOLogDTree
) {
658 IOLog("\nDT plane:\n");
659 IOPrintPlane( gIODTPlane
);
661 if( gIOKitDebug
& kIOLogServiceTree
) {
662 IOLog("\nService plane:\n");
663 IOPrintPlane( gIOServicePlane
);
665 if( gIOKitDebug
& kIOLogMemory
)
672 if ( service
&& findHFSChild
) {
674 // wait for children services to finish registering
676 t
.tv_sec
= ROOTDEVICETIMEOUT
;
678 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
681 IOLog( "Waiting for child registration\n" );
684 // look for a subservice with an Apple_HFS child
685 IOService
* subservice
= IOFindMatchingChild( service
);
686 if ( subservice
) service
= subservice
;
687 } else if ( service
&& mediaProperty
) {
688 service
= (IOService
*)service
->getProperty(mediaProperty
);
694 // If the IOService we matched to is a subclass of IONetworkInterface,
695 // then make sure it has been registered with BSD and has a BSD name
699 && service
->metaCast( "IONetworkInterface" )
700 && !IORegisterNetworkInterface( service
) )
708 service
->getPath( str
, &len
, gIOServicePlane
);
709 IOLog( "Got boot device = %s\n", str
);
711 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
713 strcpy( rootName
, iostr
->getCStringNoCopy() );
714 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
716 major
= off
->unsigned32BitValue();
717 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
719 minor
= off
->unsigned32BitValue();
721 if( service
->metaCast( "IONetworkInterface" ))
726 IOLog( "Wait for root failed\n" );
727 strcpy( rootName
, "en0");
731 IOLog( "BSD root: %s", rootName
);
733 IOLog(", major %d, minor %d\n", major
, minor
);
737 *root
= makedev( major
, minor
);
740 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
743 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
745 IOService::getPlatform()->waitQuiet();
746 if( gIOKitDebug
& kIOLogDTree
) {
747 IOLog("\nDT plane:\n");
748 IOPrintPlane( gIODTPlane
);
750 if( gIOKitDebug
& kIOLogServiceTree
) {
751 IOLog("\nService plane:\n");
752 IOPrintPlane( gIOServicePlane
);
754 if( gIOKitDebug
& kIOLogMemory
)
758 return( kIOReturnSuccess
);
762 IOBSDRegistryEntryForDeviceTree(char * path
)
764 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
768 IOBSDRegistryEntryRelease(void * entry
)
770 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
778 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
782 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
784 data
= (OSData
*) regEntry
->getProperty(property_name
);
786 *packet_length
= data
->getLength();
787 return (data
->getBytesNoCopy());