]>
git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
b6c40a4b0f3cb87b1aef42a7a5fc29ef9ad2de24
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <IOKit/IOBSD.h>
24 #include <IOKit/IOLib.h>
25 #include <IOKit/IOService.h>
26 #include <IOKit/IODeviceTreeSupport.h>
27 #include <IOKit/IOKitKeys.h>
28 #include <IOKit/IOPlatformExpert.h>
32 #include <pexpert/pexpert.h>
33 #include <kern/clock.h>
35 // how long to wait for matching root device, secs
36 #define ROOTDEVICETIMEOUT 60
38 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
39 extern dev_t
mdevlookup(int devid
);
44 IOService::publishResource("IOBSD");
46 return( kIOReturnSuccess
);
49 OSDictionary
* IOBSDNameMatching( const char * name
)
52 const OSSymbol
* str
= 0;
56 dict
= IOService::serviceMatching( gIOServiceKey
);
59 str
= OSSymbol::withCString( name
);
62 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
77 OSDictionary
* IOUUIDMatching( void )
79 return IOService::resourceMatching( "boot-uuid-media" );
83 OSDictionary
* IOCDMatching( void )
88 dict
= IOService::serviceMatching( "IOMedia" );
90 IOLog("Unable to find IOMedia\n");
94 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
100 dict
->setObject( "Content Hint", (OSObject
*)str
);
105 OSDictionary
* IONetworkMatching( const char * path
,
106 char * buf
, int maxLen
)
108 OSDictionary
* matching
= 0;
117 len
= strlen( kIODeviceTreePlane
":" );
122 strcpy( buf
, kIODeviceTreePlane
":" );
125 // remove parameters following ':' from the path
126 skip
= strchr( path
, ':');
134 strncpy( comp
, path
, len
);
137 matching
= IOService::serviceMatching( "IONetworkInterface" );
140 dict
= IOService::addLocation( matching
);
144 str
= OSString::withCString( buf
);
147 dict
->setObject( kIOPathMatchKey
, str
);
160 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
162 OSDictionary
* matching
;
163 OSDictionary
* propDict
= 0;
164 const OSSymbol
* str
= 0;
167 matching
= IOService::serviceMatching( "IONetworkInterface" );
171 propDict
= OSDictionary::withCapacity(1);
175 str
= OSSymbol::withCString( prefix
);
179 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
183 if ( matching
->setObject( gIOPropertyMatchKey
,
184 (OSObject
*) propDict
) != true )
194 if ( matching
) matching
->release();
195 if ( propDict
) propDict
->release();
196 if ( str
) str
->release();
201 static bool IORegisterNetworkInterface( IOService
* netif
)
203 // A network interface is typically named and registered
204 // with BSD after receiving a request from a user space
205 // "namer". However, for cases when the system needs to
206 // root from the network, this registration task must be
207 // done inside the kernel and completed before the root
208 // device is handed to BSD.
213 OSDictionary
* dict
= 0;
216 enum { kMaxPathLen
= 512 };
219 stack
= IOService::waitForService(
220 IOService::serviceMatching("IONetworkStack") );
221 if ( stack
== 0 ) break;
223 dict
= OSDictionary::withCapacity(3);
224 if ( dict
== 0 ) break;
226 zero
= OSNumber::withNumber((UInt64
) 0, 32);
227 if ( zero
== 0 ) break;
229 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
230 if ( pathBuf
== 0 ) break;
233 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
236 path
= OSString::withCStringNoCopy( pathBuf
);
237 if ( path
== 0 ) break;
239 dict
->setObject( "IOInterfaceUnit", zero
);
240 dict
->setObject( kIOPathMatchKey
, path
);
242 stack
->setProperties( dict
);
246 if ( zero
) zero
->release();
247 if ( path
) path
->release();
248 if ( dict
) dict
->release();
249 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
251 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
254 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
264 // scan the tail of the path for "@unit:partition"
266 // Have to get the full path to the controller - an alias may
267 // tell us next to nothing, like "hd:8"
268 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
270 look
= path
+ strlen( path
);
272 while( look
!= path
) {
273 if( *(--look
) == c
) {
275 partition
= strtol( look
+ 1, 0, 0 );
277 } else if( c
== '@') {
278 unit
= strtol( look
+ 1, &comp
, 16 );
281 lun
= strtol( comp
+ 1, 0, 16 );
285 } else if( c
== '/') {
291 if( alias
&& (look
== path
)) {
293 look
= path
+ strlen( path
);
297 if( c
|| unit
== -1 || partition
== -1)
300 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
301 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
302 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
305 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
306 comp
= buf
+ strlen( buf
);
309 strcpy( comp
, alias
);
310 comp
+= strlen( alias
);
313 if ( (look
- path
)) {
314 strncpy( comp
, path
, look
- path
);
320 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
324 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
329 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
336 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
338 OSDictionary
* matching
;
343 /* need to look up path, get device type,
344 call matching help based on device type */
346 matching
= IODiskMatching( path
, buf
, maxLen
);
352 len
= strlen( kIODeviceTreePlane
":" );
357 strcpy( buf
, kIODeviceTreePlane
":" );
360 len
= strlen( path
);
364 strncpy( comp
, path
, len
);
367 matching
= OSDictionary::withCapacity( 1 );
371 str
= OSString::withCString( buf
);
374 matching
->setObject( kIOPathMatchKey
, str
);
387 IOService
* IOFindMatchingChild( IOService
* service
)
389 // find a matching child service
390 IOService
* child
= 0;
391 OSIterator
* iter
= service
->getClientIterator();
393 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
394 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
399 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
405 dict
->setObject( "Content", (OSObject
*)str
);
407 if ( child
->compareProperty( dict
, "Content" ) ) {
412 IOService
* subchild
= IOFindMatchingChild( child
);
423 static int didRam
= 0;
425 kern_return_t
IOFindBSDRoot( char * rootName
,
426 dev_t
* root
, u_int32_t
* oflags
)
430 IORegistryEntry
* regEntry
;
431 OSDictionary
* matching
= 0;
435 UInt32
*ramdParms
= 0;
439 bool findHFSChild
= false;
440 char * mediaProperty
= 0;
442 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
444 const char * look
= 0;
446 bool forceNet
= false;
447 bool debugInfoPrintedOnce
= false;
448 const char * uuidStr
= NULL
;
450 static int mountAttempts
= 0;
458 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
460 return( kIOReturnNoMemory
);
461 rdBootVar
= str
+ kMaxPathBuf
;
463 if (!PE_parse_boot_arg("rd", rdBootVar
)
464 && !PE_parse_boot_arg("rootdev", rdBootVar
))
468 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
469 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
471 uuidStr
= (const char*)data
->getBytesNoCopy();
472 OSString
*uuidString
= OSString::withCString( uuidStr
);
474 // match the boot-args boot-uuid processing below
476 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
477 IOService::publishResource( "boot-uuid", uuidString
);
478 uuidString
->release();
479 matching
= IOUUIDMatching();
480 mediaProperty
= "boot-uuid-media";
488 // else try for an OF Path
489 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
493 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
494 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
500 if( data
&& !uuidStr
)
501 look
= (const char *) data
->getBytesNoCopy();
503 if( rdBootVar
[0] == '*') {
504 look
= rdBootVar
+ 1;
507 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
508 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
516 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
517 // It will become /dev/mdx, where x is 0-f.
520 if(!didRam
) { /* Have we already build this ram disk? */
521 didRam
= 1; /* Remember we did this */
522 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
523 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
524 if(data
) { /* We found one */
526 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
527 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
529 regEntry
->release(); /* Toss the entry */
534 // Now check if we are trying to root on a memory device
537 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
538 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
539 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
541 xchar
= xchar
& ~' '; /* Fold to upper case */
542 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
543 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
544 dchar
= dchar
| ' '; /* Fold to lower case */
546 else xchar
= -1; /* Show bogus */
548 if(xchar
>= 0) { /* Do we have a valid memory device name? */
549 *root
= mdevlookup(xchar
); /* Find the device number */
550 if(*root
>= 0) { /* Did we find one? */
552 rootName
[0] = 'm'; /* Build root name */
553 rootName
[1] = 'd'; /* Build root name */
554 rootName
[2] = dchar
; /* Build root name */
555 rootName
[3] = 0; /* Build root name */
556 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
557 *oflags
= 0; /* Show that this is not network */
558 goto iofrootx
; /* Join common exit... */
560 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
565 // from OpenFirmware path
566 IOLog("From path: \"%s\", ", look
);
568 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
569 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
571 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
575 if( (!matching
) && rdBootVar
[0] ) {
581 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
582 matching
= IONetworkNamePrefixMatching( "en" );
583 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
584 matching
= IOCDMatching();
586 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
588 OSString
*uuidString
;
590 uuid
= (char *)IOMalloc( kMaxBootVar
);
593 if (!PE_parse_boot_arg( "boot-uuid", uuid
)) {
594 panic( "rd=uuid but no boot-uuid=<value> specified" );
596 uuidString
= OSString::withCString( uuid
);
598 IOService::publishResource( "boot-uuid", uuidString
);
599 uuidString
->release();
600 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
601 matching
= IOUUIDMatching();
602 mediaProperty
= "boot-uuid-media";
604 IOFree( uuid
, kMaxBootVar
);
607 matching
= IOBSDNameMatching( look
);
614 matching
= IOService::serviceMatching( "IOMedia" );
615 astring
= OSString::withCStringNoCopy("Apple_HFS");
617 matching
->setObject("Content", astring
);
622 if( true && matching
) {
623 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
625 if( matching
->serialize( s
)) {
626 IOLog( "Waiting on %s\n", s
->text() );
632 t
.tv_sec
= ROOTDEVICETIMEOUT
;
635 service
= IOService::waitForService( matching
, &t
);
636 if( (!service
) || (mountAttempts
== 10)) {
637 PE_display_icon( 0, "noroot");
638 IOLog( "Still waiting for root device\n" );
640 if( !debugInfoPrintedOnce
) {
641 debugInfoPrintedOnce
= true;
642 if( gIOKitDebug
& kIOLogDTree
) {
643 IOLog("\nDT plane:\n");
644 IOPrintPlane( gIODTPlane
);
646 if( gIOKitDebug
& kIOLogServiceTree
) {
647 IOLog("\nService plane:\n");
648 IOPrintPlane( gIOServicePlane
);
650 if( gIOKitDebug
& kIOLogMemory
)
657 if ( service
&& findHFSChild
) {
659 // wait for children services to finish registering
661 t
.tv_sec
= ROOTDEVICETIMEOUT
;
663 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
666 IOLog( "Waiting for child registration\n" );
669 // look for a subservice with an Apple_HFS child
670 IOService
* subservice
= IOFindMatchingChild( service
);
671 if ( subservice
) service
= subservice
;
672 } else if ( service
&& mediaProperty
) {
673 service
= service
->getProperty(mediaProperty
);
679 // If the IOService we matched to is a subclass of IONetworkInterface,
680 // then make sure it has been registered with BSD and has a BSD name
684 && service
->metaCast( "IONetworkInterface" )
685 && !IORegisterNetworkInterface( service
) )
693 service
->getPath( str
, &len
, gIOServicePlane
);
694 IOLog( "Got boot device = %s\n", str
);
696 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
698 strcpy( rootName
, iostr
->getCStringNoCopy() );
699 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
701 major
= off
->unsigned32BitValue();
702 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
704 minor
= off
->unsigned32BitValue();
706 if( service
->metaCast( "IONetworkInterface" ))
711 IOLog( "Wait for root failed\n" );
712 strcpy( rootName
, "en0");
716 IOLog( "BSD root: %s", rootName
);
718 IOLog(", major %d, minor %d\n", major
, minor
);
722 *root
= makedev( major
, minor
);
725 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
728 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
730 IOService::getPlatform()->waitQuiet();
731 if( gIOKitDebug
& kIOLogDTree
) {
732 IOLog("\nDT plane:\n");
733 IOPrintPlane( gIODTPlane
);
735 if( gIOKitDebug
& kIOLogServiceTree
) {
736 IOLog("\nService plane:\n");
737 IOPrintPlane( gIOServicePlane
);
739 if( gIOKitDebug
& kIOLogMemory
)
743 return( kIOReturnSuccess
);
747 IOBSDRegistryEntryForDeviceTree(char * path
)
749 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
753 IOBSDRegistryEntryRelease(void * entry
)
755 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
763 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
767 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
769 data
= (OSData
*) regEntry
->getProperty(property_name
);
771 *packet_length
= data
->getLength();
772 return (data
->getBytesNoCopy());