]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <IOKit/IOBSD.h>
23 #include <IOKit/IOLib.h>
24 #include <IOKit/IOService.h>
25 #include <IOKit/IODeviceTreeSupport.h>
26 #include <IOKit/IOKitKeys.h>
27 #include <IOKit/IOPlatformExpert.h>
29 #include <sys/disklabel.h>
33 #include <pexpert/pexpert.h>
34 #include <kern/clock.h>
36 // how long to wait for matching root device, secs
37 #define ROOTDEVICETIMEOUT 60
39 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
40 extern dev_t
mdevlookup(int devid
);
45 IOService::publishResource("IOBSD");
47 return( kIOReturnSuccess
);
50 OSDictionary
* IOBSDNameMatching( const char * name
)
53 const OSSymbol
* str
= 0;
57 dict
= IOService::serviceMatching( gIOServiceKey
);
60 str
= OSSymbol::withCString( name
);
63 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
78 OSDictionary
* IOCDMatching( void )
83 dict
= IOService::serviceMatching( "IOMedia" );
85 IOLog("Unable to find IOMedia\n");
89 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
95 dict
->setObject( "Content Hint", (OSObject
*)str
);
100 OSDictionary
* IONetworkMatching( const char * path
,
101 char * buf
, int maxLen
)
103 OSDictionary
* matching
= 0;
112 len
= strlen( kIODeviceTreePlane
":" );
117 strcpy( buf
, kIODeviceTreePlane
":" );
120 // remove parameters following ':' from the path
121 skip
= strchr( path
, ':');
129 strncpy( comp
, path
, len
);
132 matching
= IOService::serviceMatching( "IONetworkInterface" );
135 dict
= IOService::addLocation( matching
);
139 str
= OSString::withCString( buf
);
142 dict
->setObject( kIOPathMatchKey
, str
);
155 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
157 OSDictionary
* matching
;
158 OSDictionary
* propDict
= 0;
159 const OSSymbol
* str
= 0;
162 matching
= IOService::serviceMatching( "IONetworkInterface" );
166 propDict
= OSDictionary::withCapacity(1);
170 str
= OSSymbol::withCString( prefix
);
174 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
178 if ( matching
->setObject( gIOPropertyMatchKey
,
179 (OSObject
*) propDict
) != true )
189 if ( matching
) matching
->release();
190 if ( propDict
) propDict
->release();
191 if ( str
) str
->release();
196 static bool IORegisterNetworkInterface( IOService
* netif
)
198 // A network interface is typically named and registered
199 // with BSD after receiving a request from a user space
200 // "namer". However, for cases when the system needs to
201 // root from the network, this registration task must be
202 // done inside the kernel and completed before the root
203 // device is handed to BSD.
208 OSDictionary
* dict
= 0;
211 enum { kMaxPathLen
= 512 };
214 stack
= IOService::waitForService(
215 IOService::serviceMatching("IONetworkStack") );
216 if ( stack
== 0 ) break;
218 dict
= OSDictionary::withCapacity(3);
219 if ( dict
== 0 ) break;
221 zero
= OSNumber::withNumber((UInt64
) 0, 32);
222 if ( zero
== 0 ) break;
224 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
225 if ( pathBuf
== 0 ) break;
228 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
231 path
= OSString::withCStringNoCopy( pathBuf
);
232 if ( path
== 0 ) break;
234 dict
->setObject( "IOInterfaceUnit", zero
);
235 dict
->setObject( kIOPathMatchKey
, path
);
237 stack
->setProperties( dict
);
241 if ( zero
) zero
->release();
242 if ( path
) path
->release();
243 if ( dict
) dict
->release();
244 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
246 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
249 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
258 const char * partitionSep
= NULL
;
260 // scan the tail of the path for "@unit:partition"
262 // Have to get the full path to the controller - an alias may
263 // tell us next to nothing, like "hd:8"
264 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
266 look
= path
+ strlen( path
);
268 while( look
!= path
) {
269 if( *(--look
) == c
) {
271 partition
= strtol( look
+ 1, 0, 0 );
274 } else if( c
== '@') {
277 unit
= strtol( look
+ 1, 0, 16 );
279 diff
= (int)partitionSep
- (int)look
;
282 for ( ; diff
> 0; diff
-- )
284 if( look
[diff
] == ',' )
286 lun
= strtol ( &look
[diff
+ 1], 0, 16 );
293 } else if( c
== '/') {
299 if( alias
&& (look
== path
)) {
301 look
= path
+ strlen( path
);
305 if( c
|| unit
== -1 || partition
== -1)
308 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
309 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
310 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
313 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
314 comp
= buf
+ strlen( buf
);
317 strcpy( comp
, alias
);
318 comp
+= strlen( alias
);
321 if ( (look
- path
)) {
322 strncpy( comp
, path
, look
- path
);
328 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
332 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
337 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
344 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
346 /* need to look up path, get device type,
347 call matching help based on device type */
349 return( IODiskMatching( path
, buf
, maxLen
));
353 IOService
* IOFindMatchingChild( IOService
* service
)
355 // find a matching child service
356 IOService
* child
= 0;
357 OSIterator
* iter
= service
->getClientIterator();
359 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
360 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
365 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
371 dict
->setObject( "Content", (OSObject
*)str
);
373 if ( child
->compareProperty( dict
, "Content" ) ) {
378 IOService
* subchild
= IOFindMatchingChild( child
);
389 static int didRam
= 0;
391 kern_return_t
IOFindBSDRoot( char * rootName
,
392 dev_t
* root
, u_int32_t
* oflags
)
396 IORegistryEntry
* regEntry
;
397 OSDictionary
* matching
= 0;
401 UInt32
*ramdParms
= 0;
405 bool findHFSChild
= false;
407 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
409 const char * look
= 0;
411 bool forceNet
= false;
412 bool debugInfoPrintedOnce
= false;
414 static int mountAttempts
= 0;
422 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
424 return( kIOReturnNoMemory
);
425 rdBootVar
= str
+ kMaxPathBuf
;
427 if (!PE_parse_boot_arg("rd", rdBootVar
)
428 && !PE_parse_boot_arg("rootdev", rdBootVar
))
432 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
433 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
437 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
438 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
445 look
= (const char *) data
->getBytesNoCopy();
447 if( rdBootVar
[0] == '*') {
448 look
= rdBootVar
+ 1;
451 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
452 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
460 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
461 // It will become /dev/mdx, where x is 0-f.
464 if(!didRam
) { /* Have we already build this ram disk? */
465 didRam
= 1; /* Remember we did this */
466 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
467 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
468 if(data
) { /* We found one */
470 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
471 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
473 regEntry
->release(); /* Toss the entry */
478 // Now check if we are trying to root on a memory device
481 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
482 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
483 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
485 xchar
= xchar
& ~' '; /* Fold to upper case */
486 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
487 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
488 dchar
= dchar
| ' '; /* Fold to lower case */
490 else xchar
= -1; /* Show bogus */
492 if(xchar
>= 0) { /* Do we have a valid memory device name? */
493 *root
= mdevlookup(xchar
); /* Find the device number */
494 if(*root
>= 0) { /* Did we find one? */
496 rootName
[0] = 'm'; /* Build root name */
497 rootName
[1] = 'd'; /* Build root name */
498 rootName
[2] = dchar
; /* Build root name */
499 rootName
[3] = 0; /* Build root name */
500 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
501 *oflags
= 0; /* Show that this is not network */
502 goto iofrootx
; /* Join common exit... */
504 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
509 // from OpenFirmware path
510 IOLog("From path: \"%s\", ", look
);
512 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
513 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
515 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
519 if( (!matching
) && rdBootVar
[0] ) {
525 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
526 matching
= IONetworkNamePrefixMatching( "en" );
527 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
528 matching
= IOCDMatching();
531 matching
= IOBSDNameMatching( look
);
538 matching
= IOService::serviceMatching( "IOMedia" );
539 astring
= OSString::withCStringNoCopy("Apple_HFS");
541 matching
->setObject("Content", astring
);
546 if( true && matching
) {
547 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
549 if( matching
->serialize( s
)) {
550 IOLog( "Waiting on %s\n", s
->text() );
556 t
.tv_sec
= ROOTDEVICETIMEOUT
;
559 service
= IOService::waitForService( matching
, &t
);
560 if( (!service
) || (mountAttempts
== 10)) {
561 PE_display_icon( 0, "noroot");
562 IOLog( "Still waiting for root device\n" );
564 if( !debugInfoPrintedOnce
) {
565 debugInfoPrintedOnce
= true;
566 if( gIOKitDebug
& kIOLogDTree
) {
567 IOLog("\nDT plane:\n");
568 IOPrintPlane( gIODTPlane
);
570 if( gIOKitDebug
& kIOLogServiceTree
) {
571 IOLog("\nService plane:\n");
572 IOPrintPlane( gIOServicePlane
);
574 if( gIOKitDebug
& kIOLogMemory
)
581 if ( service
&& findHFSChild
) {
583 // wait for children services to finish registering
585 t
.tv_sec
= ROOTDEVICETIMEOUT
;
587 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
590 IOLog( "Waiting for child registration\n" );
593 // look for a subservice with an Apple_HFS child
594 IOService
* subservice
= IOFindMatchingChild( service
);
595 if ( subservice
) service
= subservice
;
601 // If the IOService we matched to is a subclass of IONetworkInterface,
602 // then make sure it has been registered with BSD and has a BSD name
606 && service
->metaCast( "IONetworkInterface" )
607 && !IORegisterNetworkInterface( service
) )
615 service
->getPath( str
, &len
, gIOServicePlane
);
616 IOLog( "Got boot device = %s\n", str
);
618 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
620 strcpy( rootName
, iostr
->getCStringNoCopy() );
621 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
623 major
= off
->unsigned32BitValue();
624 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
626 minor
= off
->unsigned32BitValue();
628 if( service
->metaCast( "IONetworkInterface" ))
633 IOLog( "Wait for root failed\n" );
634 strcpy( rootName
, "en0");
638 IOLog( "BSD root: %s", rootName
);
640 IOLog(", major %d, minor %d\n", major
, minor
);
644 *root
= makedev( major
, minor
);
647 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
650 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
652 IOService::getPlatform()->waitQuiet();
653 if( gIOKitDebug
& kIOLogDTree
) {
654 IOLog("\nDT plane:\n");
655 IOPrintPlane( gIODTPlane
);
657 if( gIOKitDebug
& kIOLogServiceTree
) {
658 IOLog("\nService plane:\n");
659 IOPrintPlane( gIOServicePlane
);
661 if( gIOKitDebug
& kIOLogMemory
)
665 return( kIOReturnSuccess
);
669 IOBSDRegistryEntryForDeviceTree(char * path
)
671 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
675 IOBSDRegistryEntryRelease(void * entry
)
677 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
685 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
689 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
691 data
= (OSData
*) regEntry
->getProperty(property_name
);
693 *packet_length
= data
->getLength();
694 return (data
->getBytesNoCopy());