]>
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_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #include <IOKit/IOBSD.h>
26 #include <IOKit/IOLib.h>
27 #include <IOKit/IOService.h>
28 #include <IOKit/IODeviceTreeSupport.h>
29 #include <IOKit/IOKitKeys.h>
30 #include <IOKit/IOPlatformExpert.h>
32 #include <sys/disklabel.h>
36 #include <pexpert/pexpert.h>
37 #include <kern/clock.h>
39 // how long to wait for matching root device, secs
40 #define ROOTDEVICETIMEOUT 60
42 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
43 extern dev_t
mdevlookup(int devid
);
48 IOService::publishResource("IOBSD");
50 return( kIOReturnSuccess
);
53 OSDictionary
* IOBSDNameMatching( const char * name
)
56 const OSSymbol
* str
= 0;
60 dict
= IOService::serviceMatching( gIOServiceKey
);
63 str
= OSSymbol::withCString( name
);
66 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
81 OSDictionary
* IOCDMatching( void )
86 dict
= IOService::serviceMatching( "IOMedia" );
88 IOLog("Unable to find IOMedia\n");
92 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
98 dict
->setObject( "Content Hint", (OSObject
*)str
);
103 OSDictionary
* IONetworkMatching( const char * path
,
104 char * buf
, int maxLen
)
106 OSDictionary
* matching
= 0;
115 len
= strlen( kIODeviceTreePlane
":" );
120 strcpy( buf
, kIODeviceTreePlane
":" );
123 // remove parameters following ':' from the path
124 skip
= strchr( path
, ':');
132 strncpy( comp
, path
, len
);
135 matching
= IOService::serviceMatching( "IONetworkInterface" );
138 dict
= IOService::addLocation( matching
);
142 str
= OSString::withCString( buf
);
145 dict
->setObject( kIOPathMatchKey
, str
);
158 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
160 OSDictionary
* matching
;
161 OSDictionary
* propDict
= 0;
162 const OSSymbol
* str
= 0;
165 matching
= IOService::serviceMatching( "IONetworkInterface" );
169 propDict
= OSDictionary::withCapacity(1);
173 str
= OSSymbol::withCString( prefix
);
177 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
181 if ( matching
->setObject( gIOPropertyMatchKey
,
182 (OSObject
*) propDict
) != true )
192 if ( matching
) matching
->release();
193 if ( propDict
) propDict
->release();
194 if ( str
) str
->release();
199 static bool IORegisterNetworkInterface( IOService
* netif
)
201 // A network interface is typically named and registered
202 // with BSD after receiving a request from a user space
203 // "namer". However, for cases when the system needs to
204 // root from the network, this registration task must be
205 // done inside the kernel and completed before the root
206 // device is handed to BSD.
211 OSDictionary
* dict
= 0;
214 enum { kMaxPathLen
= 512 };
217 stack
= IOService::waitForService(
218 IOService::serviceMatching("IONetworkStack") );
219 if ( stack
== 0 ) break;
221 dict
= OSDictionary::withCapacity(3);
222 if ( dict
== 0 ) break;
224 zero
= OSNumber::withNumber((UInt64
) 0, 32);
225 if ( zero
== 0 ) break;
227 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
228 if ( pathBuf
== 0 ) break;
231 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
234 path
= OSString::withCStringNoCopy( pathBuf
);
235 if ( path
== 0 ) break;
237 dict
->setObject( "IOInterfaceUnit", zero
);
238 dict
->setObject( kIOPathMatchKey
, path
);
240 stack
->setProperties( dict
);
244 if ( zero
) zero
->release();
245 if ( path
) path
->release();
246 if ( dict
) dict
->release();
247 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
249 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
252 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
261 const char * partitionSep
= NULL
;
263 // scan the tail of the path for "@unit:partition"
265 // Have to get the full path to the controller - an alias may
266 // tell us next to nothing, like "hd:8"
267 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
269 look
= path
+ strlen( path
);
271 while( look
!= path
) {
272 if( *(--look
) == c
) {
274 partition
= strtol( look
+ 1, 0, 0 );
277 } else if( c
== '@') {
280 unit
= strtol( look
+ 1, 0, 16 );
282 diff
= (int)partitionSep
- (int)look
;
285 for ( ; diff
> 0; diff
-- )
287 if( look
[diff
] == ',' )
289 lun
= strtol ( &look
[diff
+ 1], 0, 16 );
296 } else if( c
== '/') {
302 if( alias
&& (look
== path
)) {
304 look
= path
+ strlen( path
);
308 if( c
|| unit
== -1 || partition
== -1)
311 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
312 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
313 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
316 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
317 comp
= buf
+ strlen( buf
);
320 strcpy( comp
, alias
);
321 comp
+= strlen( alias
);
324 if ( (look
- path
)) {
325 strncpy( comp
, path
, look
- path
);
331 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
335 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
340 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
347 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
349 /* need to look up path, get device type,
350 call matching help based on device type */
352 return( IODiskMatching( path
, buf
, maxLen
));
356 IOService
* IOFindMatchingChild( IOService
* service
)
358 // find a matching child service
359 IOService
* child
= 0;
360 OSIterator
* iter
= service
->getClientIterator();
362 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
363 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
368 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
374 dict
->setObject( "Content", (OSObject
*)str
);
376 if ( child
->compareProperty( dict
, "Content" ) ) {
381 IOService
* subchild
= IOFindMatchingChild( child
);
392 static int didRam
= 0;
394 kern_return_t
IOFindBSDRoot( char * rootName
,
395 dev_t
* root
, u_int32_t
* oflags
)
399 IORegistryEntry
* regEntry
;
400 OSDictionary
* matching
= 0;
404 UInt32
*ramdParms
= 0;
408 bool findHFSChild
= false;
410 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
412 const char * look
= 0;
414 bool forceNet
= false;
415 bool debugInfoPrintedOnce
= false;
417 static int mountAttempts
= 0;
425 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
427 return( kIOReturnNoMemory
);
428 rdBootVar
= str
+ kMaxPathBuf
;
430 if (!PE_parse_boot_arg("rd", rdBootVar
)
431 && !PE_parse_boot_arg("rootdev", rdBootVar
))
435 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
436 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
440 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
441 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
448 look
= (const char *) data
->getBytesNoCopy();
450 if( rdBootVar
[0] == '*') {
451 look
= rdBootVar
+ 1;
454 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
455 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
463 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
464 // It will become /dev/mdx, where x is 0-f.
467 if(!didRam
) { /* Have we already build this ram disk? */
468 didRam
= 1; /* Remember we did this */
469 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
470 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
471 if(data
) { /* We found one */
473 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
474 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
476 regEntry
->release(); /* Toss the entry */
481 // Now check if we are trying to root on a memory device
484 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
485 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
486 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
488 xchar
= xchar
& ~' '; /* Fold to upper case */
489 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
490 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
491 dchar
= dchar
| ' '; /* Fold to lower case */
493 else xchar
= -1; /* Show bogus */
495 if(xchar
>= 0) { /* Do we have a valid memory device name? */
496 *root
= mdevlookup(xchar
); /* Find the device number */
497 if(*root
>= 0) { /* Did we find one? */
499 rootName
[0] = 'm'; /* Build root name */
500 rootName
[1] = 'd'; /* Build root name */
501 rootName
[2] = dchar
; /* Build root name */
502 rootName
[3] = 0; /* Build root name */
503 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
504 *oflags
= 0; /* Show that this is not network */
505 goto iofrootx
; /* Join common exit... */
507 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
512 // from OpenFirmware path
513 IOLog("From path: \"%s\", ", look
);
515 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
516 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
518 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
522 if( (!matching
) && rdBootVar
[0] ) {
528 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
529 matching
= IONetworkNamePrefixMatching( "en" );
530 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
531 matching
= IOCDMatching();
534 matching
= IOBSDNameMatching( look
);
541 matching
= IOService::serviceMatching( "IOMedia" );
542 astring
= OSString::withCStringNoCopy("Apple_HFS");
544 matching
->setObject("Content", astring
);
549 if( true && matching
) {
550 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
552 if( matching
->serialize( s
)) {
553 IOLog( "Waiting on %s\n", s
->text() );
559 t
.tv_sec
= ROOTDEVICETIMEOUT
;
562 service
= IOService::waitForService( matching
, &t
);
563 if( (!service
) || (mountAttempts
== 10)) {
564 PE_display_icon( 0, "noroot");
565 IOLog( "Still waiting for root device\n" );
567 if( !debugInfoPrintedOnce
) {
568 debugInfoPrintedOnce
= true;
569 if( gIOKitDebug
& kIOLogDTree
) {
570 IOLog("\nDT plane:\n");
571 IOPrintPlane( gIODTPlane
);
573 if( gIOKitDebug
& kIOLogServiceTree
) {
574 IOLog("\nService plane:\n");
575 IOPrintPlane( gIOServicePlane
);
577 if( gIOKitDebug
& kIOLogMemory
)
584 if ( service
&& findHFSChild
) {
586 // wait for children services to finish registering
588 t
.tv_sec
= ROOTDEVICETIMEOUT
;
590 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
593 IOLog( "Waiting for child registration\n" );
596 // look for a subservice with an Apple_HFS child
597 IOService
* subservice
= IOFindMatchingChild( service
);
598 if ( subservice
) service
= subservice
;
604 // If the IOService we matched to is a subclass of IONetworkInterface,
605 // then make sure it has been registered with BSD and has a BSD name
609 && service
->metaCast( "IONetworkInterface" )
610 && !IORegisterNetworkInterface( service
) )
618 service
->getPath( str
, &len
, gIOServicePlane
);
619 IOLog( "Got boot device = %s\n", str
);
621 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
623 strcpy( rootName
, iostr
->getCStringNoCopy() );
624 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
626 major
= off
->unsigned32BitValue();
627 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
629 minor
= off
->unsigned32BitValue();
631 if( service
->metaCast( "IONetworkInterface" ))
636 IOLog( "Wait for root failed\n" );
637 strcpy( rootName
, "en0");
641 IOLog( "BSD root: %s", rootName
);
643 IOLog(", major %d, minor %d\n", major
, minor
);
647 *root
= makedev( major
, minor
);
650 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
653 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
655 IOService::getPlatform()->waitQuiet();
656 if( gIOKitDebug
& kIOLogDTree
) {
657 IOLog("\nDT plane:\n");
658 IOPrintPlane( gIODTPlane
);
660 if( gIOKitDebug
& kIOLogServiceTree
) {
661 IOLog("\nService plane:\n");
662 IOPrintPlane( gIOServicePlane
);
664 if( gIOKitDebug
& kIOLogMemory
)
668 return( kIOReturnSuccess
);
672 IOBSDRegistryEntryForDeviceTree(char * path
)
674 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
678 IOBSDRegistryEntryRelease(void * entry
)
680 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
688 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
692 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
694 data
= (OSData
*) regEntry
->getProperty(property_name
);
696 *packet_length
= data
->getLength();
697 return (data
->getBytesNoCopy());