]>
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>
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 strlcpy( buf
, kIODeviceTreePlane
":", len
+ 1 );
132 // remove parameters following ':' from the path
133 skip
= strchr( path
, ':');
141 strlcpy( comp
, path
, len
+ 1 );
143 matching
= IOService::serviceMatching( "IONetworkInterface" );
146 dict
= IOService::addLocation( matching
);
150 str
= OSString::withCString( buf
);
153 dict
->setObject( kIOPathMatchKey
, str
);
166 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
168 OSDictionary
* matching
;
169 OSDictionary
* propDict
= 0;
170 const OSSymbol
* str
= 0;
171 char networkType
[128];
174 matching
= IOService::serviceMatching( "IONetworkInterface" );
178 propDict
= OSDictionary::withCapacity(1);
182 str
= OSSymbol::withCString( prefix
);
186 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
190 // see if we're contrained to netroot off of specific network type
191 if(PE_parse_boot_argn( "network-type", networkType
, 128 ))
193 str
= OSSymbol::withCString( networkType
);
196 propDict
->setObject( "IONetworkRootType", str
);
202 if ( matching
->setObject( gIOPropertyMatchKey
,
203 (OSObject
*) propDict
) != true )
213 if ( matching
) matching
->release();
214 if ( propDict
) propDict
->release();
215 if ( str
) str
->release();
220 static bool IORegisterNetworkInterface( IOService
* netif
)
222 // A network interface is typically named and registered
223 // with BSD after receiving a request from a user space
224 // "namer". However, for cases when the system needs to
225 // root from the network, this registration task must be
226 // done inside the kernel and completed before the root
227 // device is handed to BSD.
232 OSDictionary
* dict
= 0;
235 enum { kMaxPathLen
= 512 };
238 stack
= IOService::waitForService(
239 IOService::serviceMatching("IONetworkStack") );
240 if ( stack
== 0 ) break;
242 dict
= OSDictionary::withCapacity(3);
243 if ( dict
== 0 ) break;
245 zero
= OSNumber::withNumber((UInt64
) 0, 32);
246 if ( zero
== 0 ) break;
248 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
249 if ( pathBuf
== 0 ) break;
252 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
255 path
= OSString::withCStringNoCopy( pathBuf
);
256 if ( path
== 0 ) break;
258 dict
->setObject( "IOInterfaceUnit", zero
);
259 dict
->setObject( kIOPathMatchKey
, path
);
261 stack
->setProperties( dict
);
265 if ( zero
) zero
->release();
266 if ( path
) path
->release();
267 if ( dict
) dict
->release();
268 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
270 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
273 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 len
= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
325 snprintf( buf
, len
+ 1, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
329 len
= strlen( alias
);
334 strlcpy( comp
, alias
, len
+ 1 );
338 if ( (look
- path
)) {
344 strlcpy( comp
, path
, len
+ 1 );
350 len
= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
355 snprintf( comp
, len
+ 1, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
359 len
= strlen( "/@hhhhhhhh:dddddddddd';}" );
364 snprintf( comp
, len
+ 1, "/@%lx:%ld';}", unit
, partition
);
367 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
374 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
376 OSDictionary
* matching
;
381 /* need to look up path, get device type,
382 call matching help based on device type */
384 matching
= IODiskMatching( path
, buf
, maxLen
);
390 len
= strlen( kIODeviceTreePlane
":" );
395 strlcpy( buf
, kIODeviceTreePlane
":", len
+ 1 );
398 len
= strlen( path
);
402 strlcpy( comp
, path
, len
+ 1 );
404 matching
= OSDictionary::withCapacity( 1 );
408 str
= OSString::withCString( buf
);
411 matching
->setObject( kIOPathMatchKey
, str
);
424 IOService
* IOFindMatchingChild( IOService
* service
)
426 // find a matching child service
427 IOService
* child
= 0;
428 OSIterator
* iter
= service
->getClientIterator();
430 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
431 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
436 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
442 dict
->setObject( "Content", (OSObject
*)str
);
444 if ( child
->compareProperty( dict
, "Content" ) ) {
449 IOService
* subchild
= IOFindMatchingChild( child
);
460 static int didRam
= 0;
462 kern_return_t
IOFindBSDRoot( char * rootName
, unsigned int rootNameSize
,
463 dev_t
* root
, u_int32_t
* oflags
)
467 IORegistryEntry
* regEntry
;
468 OSDictionary
* matching
= 0;
472 UInt32
*ramdParms
= 0;
476 bool findHFSChild
= false;
477 char * mediaProperty
= 0;
479 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
481 const char * look
= 0;
483 bool forceNet
= false;
484 bool debugInfoPrintedOnce
= false;
485 const char * uuidStr
= NULL
;
487 static int mountAttempts
= 0;
495 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
497 return( kIOReturnNoMemory
);
498 rdBootVar
= str
+ kMaxPathBuf
;
500 if (!PE_parse_boot_argn("rd", rdBootVar
, kMaxBootVar
)
501 && !PE_parse_boot_argn("rootdev", rdBootVar
, kMaxBootVar
))
505 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
506 data
= OSDynamicCast(OSData
, regEntry
->getProperty( "root-matching" ));
508 matching
= OSDynamicCast(OSDictionary
, OSUnserializeXML((char *)data
->getBytesNoCopy()));
514 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
516 uuidStr
= (const char*)data
->getBytesNoCopy();
517 OSString
*uuidString
= OSString::withCString( uuidStr
);
519 // match the boot-args boot-uuid processing below
521 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
522 IOService::publishResource( "boot-uuid", uuidString
);
523 uuidString
->release();
524 matching
= IOUUIDMatching();
525 mediaProperty
= "boot-uuid-media";
533 // else try for an OF Path
534 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
538 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
539 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
545 if( data
&& !uuidStr
)
546 look
= (const char *) data
->getBytesNoCopy();
548 if( rdBootVar
[0] == '*') {
549 look
= rdBootVar
+ 1;
552 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
553 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
561 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
562 // It will become /dev/mdx, where x is 0-f.
565 if(!didRam
) { /* Have we already build this ram disk? */
566 didRam
= 1; /* Remember we did this */
567 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
568 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
569 if(data
) { /* We found one */
571 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
572 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
574 regEntry
->release(); /* Toss the entry */
579 // Now check if we are trying to root on a memory device
582 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
583 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
584 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
586 xchar
= xchar
& ~' '; /* Fold to upper case */
587 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
588 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
589 dchar
= dchar
| ' '; /* Fold to lower case */
591 else xchar
= -1; /* Show bogus */
593 if(xchar
>= 0) { /* Do we have a valid memory device name? */
594 *root
= mdevlookup(xchar
); /* Find the device number */
595 if(*root
>= 0) { /* Did we find one? */
597 rootName
[0] = 'm'; /* Build root name */
598 rootName
[1] = 'd'; /* Build root name */
599 rootName
[2] = dchar
; /* Build root name */
600 rootName
[3] = 0; /* Build root name */
601 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
602 *oflags
= 0; /* Show that this is not network */
603 goto iofrootx
; /* Join common exit... */
605 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
610 // from OpenFirmware path
611 IOLog("From path: \"%s\", ", look
);
614 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
615 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
617 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
622 if( (!matching
) && rdBootVar
[0] ) {
628 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
629 matching
= IONetworkNamePrefixMatching( "en" );
630 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
631 matching
= IOCDMatching();
633 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
635 OSString
*uuidString
;
637 uuid
= (char *)IOMalloc( kMaxBootVar
);
640 if (!PE_parse_boot_argn( "boot-uuid", uuid
, kMaxBootVar
)) {
641 panic( "rd=uuid but no boot-uuid=<value> specified" );
643 uuidString
= OSString::withCString( uuid
);
645 IOService::publishResource( "boot-uuid", uuidString
);
646 uuidString
->release();
647 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
648 matching
= IOUUIDMatching();
649 mediaProperty
= "boot-uuid-media";
651 IOFree( uuid
, kMaxBootVar
);
654 matching
= IOBSDNameMatching( look
);
660 // Match any HFS media
662 matching
= IOService::serviceMatching( "IOMedia" );
663 astring
= OSString::withCStringNoCopy("Apple_HFS");
665 matching
->setObject("Content", astring
);
670 if( true && matching
) {
671 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
673 if( matching
->serialize( s
)) {
674 IOLog( "Waiting on %s\n", s
->text() );
680 t
.tv_sec
= ROOTDEVICETIMEOUT
;
683 service
= IOService::waitForService( matching
, &t
);
684 if( (!service
) || (mountAttempts
== 10)) {
685 PE_display_icon( 0, "noroot");
686 IOLog( "Still waiting for root device\n" );
688 if( !debugInfoPrintedOnce
) {
689 debugInfoPrintedOnce
= true;
690 if( gIOKitDebug
& kIOLogDTree
) {
691 IOLog("\nDT plane:\n");
692 IOPrintPlane( gIODTPlane
);
694 if( gIOKitDebug
& kIOLogServiceTree
) {
695 IOLog("\nService plane:\n");
696 IOPrintPlane( gIOServicePlane
);
698 if( gIOKitDebug
& kIOLogMemory
)
705 if ( service
&& findHFSChild
) {
707 // wait for children services to finish registering
709 t
.tv_sec
= ROOTDEVICETIMEOUT
;
711 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
714 IOLog( "Waiting for child registration\n" );
717 // look for a subservice with an Apple_HFS child
718 IOService
* subservice
= IOFindMatchingChild( service
);
719 if ( subservice
) service
= subservice
;
720 } else if ( service
&& mediaProperty
) {
721 service
= (IOService
*)service
->getProperty(mediaProperty
);
727 // If the IOService we matched to is a subclass of IONetworkInterface,
728 // then make sure it has been registered with BSD and has a BSD name
732 && service
->metaCast( "IONetworkInterface" )
733 && !IORegisterNetworkInterface( service
) )
741 service
->getPath( str
, &len
, gIOServicePlane
);
742 IOLog( "Got boot device = %s\n", str
);
744 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
746 strlcpy( rootName
, iostr
->getCStringNoCopy(), rootNameSize
);
747 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
749 mjr
= off
->unsigned32BitValue();
750 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
752 mnr
= off
->unsigned32BitValue();
754 if( service
->metaCast( "IONetworkInterface" ))
759 IOLog( "Wait for root failed\n" );
760 strlcpy( rootName
, "en0", rootNameSize
);
764 IOLog( "BSD root: %s", rootName
);
766 IOLog(", major %d, minor %d\n", mjr
, mnr
);
770 *root
= makedev( mjr
, mnr
);
773 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
776 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
778 IOService::getPlatform()->waitQuiet();
779 if( gIOKitDebug
& kIOLogDTree
) {
780 IOLog("\nDT plane:\n");
781 IOPrintPlane( gIODTPlane
);
783 if( gIOKitDebug
& kIOLogServiceTree
) {
784 IOLog("\nService plane:\n");
785 IOPrintPlane( gIOServicePlane
);
787 if( gIOKitDebug
& kIOLogMemory
)
791 return( kIOReturnSuccess
);
794 void IOSecureBSDRoot(const char * rootName
)
798 IOPlatformExpert
*pe
;
799 const OSSymbol
*functionName
= OSSymbol::withCStringNoCopy("SecureRootName");
801 while ((pe
= IOService::getPlatform()) == 0) IOSleep(1 * 1000);
803 // Returns kIOReturnNotPrivileged is the root device is not secure.
804 // Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
805 result
= pe
->callPlatformFunction(functionName
, false, (void *)rootName
, (void *)0, (void *)0, (void *)0);
807 functionName
->release();
809 if (result
== kIOReturnNotPrivileged
) mdevremoveall();
814 IOBSDRegistryEntryForDeviceTree(char * path
)
816 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
820 IOBSDRegistryEntryRelease(void * entry
)
822 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
830 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
834 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
836 data
= (OSData
*) regEntry
->getProperty(property_name
);
838 *packet_length
= data
->getLength();
839 return (data
->getBytesNoCopy());
844 kern_return_t
IOBSDGetPlatformUUID( uuid_t uuid
, mach_timespec_t timeout
)
846 IOService
* resources
;
849 resources
= IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey
), &timeout
);
850 if ( resources
== 0 ) return KERN_OPERATION_TIMED_OUT
;
852 string
= ( OSString
* ) IOService::getPlatform( )->getProvider( )->getProperty( kIOPlatformUUIDKey
);
853 if ( string
== 0 ) return KERN_NOT_SUPPORTED
;
855 uuid_parse( string
->getCStringNoCopy( ), uuid
);