]> git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
c921715044a3914761a9b51c656cd50bdb5bb322
[apple/xnu.git] / iokit / bsddev / IOKitBSDInit.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 #include <IOKit/IOBSD.h>
31 #include <IOKit/IOLib.h>
32 #include <IOKit/IOService.h>
33 #include <IOKit/IODeviceTreeSupport.h>
34 #include <IOKit/IOKitKeys.h>
35 #include <IOKit/IOPlatformExpert.h>
36
37 extern "C" {
38
39 #include <pexpert/pexpert.h>
40 #include <kern/clock.h>
41
42 // how long to wait for matching root device, secs
43 #define ROOTDEVICETIMEOUT 60
44
45 extern dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys);
46 extern dev_t mdevlookup(int devid);
47
48 kern_return_t
49 IOKitBSDInit( void )
50 {
51 IOService::publishResource("IOBSD");
52
53 return( kIOReturnSuccess );
54 }
55
56 OSDictionary * IOBSDNameMatching( const char * name )
57 {
58 OSDictionary * dict;
59 const OSSymbol * str = 0;
60
61 do {
62
63 dict = IOService::serviceMatching( gIOServiceKey );
64 if( !dict)
65 continue;
66 str = OSSymbol::withCString( name );
67 if( !str)
68 continue;
69 dict->setObject( kIOBSDNameKey, (OSObject *) str );
70 str->release();
71
72 return( dict );
73
74 } while( false );
75
76 if( dict)
77 dict->release();
78 if( str)
79 str->release();
80
81 return( 0 );
82 }
83
84 OSDictionary * IOUUIDMatching( void )
85 {
86 return IOService::resourceMatching( "boot-uuid-media" );
87 }
88
89
90 OSDictionary * IOCDMatching( void )
91 {
92 OSDictionary * dict;
93 const OSSymbol * str;
94
95 dict = IOService::serviceMatching( "IOMedia" );
96 if( dict == 0 ) {
97 IOLog("Unable to find IOMedia\n");
98 return 0;
99 }
100
101 str = OSSymbol::withCString( "CD_ROM_Mode_1" );
102 if( str == 0 ) {
103 dict->release();
104 return 0;
105 }
106
107 dict->setObject( "Content Hint", (OSObject *)str );
108 str->release();
109 return( dict );
110 }
111
112 OSDictionary * IONetworkMatching( const char * path,
113 char * buf, int maxLen )
114 {
115 OSDictionary * matching = 0;
116 OSDictionary * dict;
117 OSString * str;
118 char * comp;
119 const char * skip;
120 int len;
121
122 do {
123
124 len = strlen( kIODeviceTreePlane ":" );
125 maxLen -= len;
126 if( maxLen < 0)
127 continue;
128
129 strcpy( buf, kIODeviceTreePlane ":" );
130 comp = buf + len;
131
132 // remove parameters following ':' from the path
133 skip = strchr( path, ':');
134 if( !skip)
135 continue;
136
137 len = skip - path;
138 maxLen -= len;
139 if( maxLen < 0)
140 continue;
141 strncpy( comp, path, len );
142 comp[ len ] = 0;
143
144 matching = IOService::serviceMatching( "IONetworkInterface" );
145 if( !matching)
146 continue;
147 dict = IOService::addLocation( matching );
148 if( !dict)
149 continue;
150
151 str = OSString::withCString( buf );
152 if( !str)
153 continue;
154 dict->setObject( kIOPathMatchKey, str );
155 str->release();
156
157 return( matching );
158
159 } while( false );
160
161 if( matching)
162 matching->release();
163
164 return( 0 );
165 }
166
167 OSDictionary * IONetworkNamePrefixMatching( const char * prefix )
168 {
169 OSDictionary * matching;
170 OSDictionary * propDict = 0;
171 const OSSymbol * str = 0;
172
173 do {
174 matching = IOService::serviceMatching( "IONetworkInterface" );
175 if ( matching == 0 )
176 continue;
177
178 propDict = OSDictionary::withCapacity(1);
179 if ( propDict == 0 )
180 continue;
181
182 str = OSSymbol::withCString( prefix );
183 if ( str == 0 )
184 continue;
185
186 propDict->setObject( "IOInterfaceNamePrefix", (OSObject *) str );
187 str->release();
188 str = 0;
189
190 if ( matching->setObject( gIOPropertyMatchKey,
191 (OSObject *) propDict ) != true )
192 continue;
193
194 propDict->release();
195 propDict = 0;
196
197 return( matching );
198
199 } while ( false );
200
201 if ( matching ) matching->release();
202 if ( propDict ) propDict->release();
203 if ( str ) str->release();
204
205 return( 0 );
206 }
207
208 static bool IORegisterNetworkInterface( IOService * netif )
209 {
210 // A network interface is typically named and registered
211 // with BSD after receiving a request from a user space
212 // "namer". However, for cases when the system needs to
213 // root from the network, this registration task must be
214 // done inside the kernel and completed before the root
215 // device is handed to BSD.
216
217 IOService * stack;
218 OSNumber * zero = 0;
219 OSString * path = 0;
220 OSDictionary * dict = 0;
221 char * pathBuf = 0;
222 int len;
223 enum { kMaxPathLen = 512 };
224
225 do {
226 stack = IOService::waitForService(
227 IOService::serviceMatching("IONetworkStack") );
228 if ( stack == 0 ) break;
229
230 dict = OSDictionary::withCapacity(3);
231 if ( dict == 0 ) break;
232
233 zero = OSNumber::withNumber((UInt64) 0, 32);
234 if ( zero == 0 ) break;
235
236 pathBuf = (char *) IOMalloc( kMaxPathLen );
237 if ( pathBuf == 0 ) break;
238
239 len = kMaxPathLen;
240 if ( netif->getPath( pathBuf, &len, gIOServicePlane )
241 == false ) break;
242
243 path = OSString::withCStringNoCopy( pathBuf );
244 if ( path == 0 ) break;
245
246 dict->setObject( "IOInterfaceUnit", zero );
247 dict->setObject( kIOPathMatchKey, path );
248
249 stack->setProperties( dict );
250 }
251 while ( false );
252
253 if ( zero ) zero->release();
254 if ( path ) path->release();
255 if ( dict ) dict->release();
256 if ( pathBuf ) IOFree(pathBuf, kMaxPathLen);
257
258 return ( netif->getProperty( kIOBSDNameKey ) != 0 );
259 }
260
261 OSDictionary * IODiskMatching( const char * path, char * buf, int maxLen )
262 {
263 const char * look;
264 const char * alias;
265 char * comp;
266 long unit = -1;
267 long partition = -1;
268 long lun = -1;
269 char c;
270
271 // scan the tail of the path for "@unit:partition"
272 do {
273 // Have to get the full path to the controller - an alias may
274 // tell us next to nothing, like "hd:8"
275 alias = IORegistryEntry::dealiasPath( &path, gIODTPlane );
276
277 look = path + strlen( path);
278 c = ':';
279 while( look != path) {
280 if( *(--look) == c) {
281 if( c == ':') {
282 partition = strtol( look + 1, 0, 0 );
283 c = '@';
284 } else if( c == '@') {
285 unit = strtol( look + 1, &comp, 16 );
286
287 if( *comp == ',') {
288 lun = strtol( comp + 1, 0, 16 );
289 }
290
291 c = '/';
292 } else if( c == '/') {
293 c = 0;
294 break;
295 }
296 }
297
298 if( alias && (look == path)) {
299 path = alias;
300 look = path + strlen( path);
301 alias = 0;
302 }
303 }
304 if( c || unit == -1 || partition == -1)
305 continue;
306
307 maxLen -= strlen( "{" kIOPathMatchKey "='" kIODeviceTreePlane ":" );
308 maxLen -= ( alias ? strlen( alias ) : 0 ) + (look - path);
309 maxLen -= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
310
311 if( maxLen > 0) {
312 sprintf( buf, "{" kIOPathMatchKey "='" kIODeviceTreePlane ":" );
313 comp = buf + strlen( buf );
314
315 if( alias) {
316 strcpy( comp, alias );
317 comp += strlen( alias );
318 }
319
320 if ( (look - path)) {
321 strncpy( comp, path, look - path);
322 comp += look - path;
323 }
324
325 if ( lun != -1 )
326 {
327 sprintf ( comp, "/@%lx,%lx:%ld';}", unit, lun, partition );
328 }
329 else
330 {
331 sprintf( comp, "/@%lx:%ld';}", unit, partition );
332 }
333 } else
334 continue;
335
336 return( OSDynamicCast(OSDictionary, OSUnserialize( buf, 0 )) );
337
338 } while( false );
339
340 return( 0 );
341 }
342
343 OSDictionary * IOOFPathMatching( const char * path, char * buf, int maxLen )
344 {
345 OSDictionary * matching;
346 OSString * str;
347 char * comp;
348 int len;
349
350 /* need to look up path, get device type,
351 call matching help based on device type */
352
353 matching = IODiskMatching( path, buf, maxLen );
354 if( matching)
355 return( matching );
356
357 do {
358
359 len = strlen( kIODeviceTreePlane ":" );
360 maxLen -= len;
361 if( maxLen < 0)
362 continue;
363
364 strcpy( buf, kIODeviceTreePlane ":" );
365 comp = buf + len;
366
367 len = strlen( path );
368 maxLen -= len;
369 if( maxLen < 0)
370 continue;
371 strncpy( comp, path, len );
372 comp[ len ] = 0;
373
374 matching = OSDictionary::withCapacity( 1 );
375 if( !matching)
376 continue;
377
378 str = OSString::withCString( buf );
379 if( !str)
380 continue;
381 matching->setObject( kIOPathMatchKey, str );
382 str->release();
383
384 return( matching );
385
386 } while( false );
387
388 if( matching)
389 matching->release();
390
391 return( 0 );
392 }
393
394 IOService * IOFindMatchingChild( IOService * service )
395 {
396 // find a matching child service
397 IOService * child = 0;
398 OSIterator * iter = service->getClientIterator();
399 if ( iter ) {
400 while( ( child = (IOService *) iter->getNextObject() ) ) {
401 OSDictionary * dict = OSDictionary::withCapacity( 1 );
402 if( dict == 0 ) {
403 iter->release();
404 return 0;
405 }
406 const OSSymbol * str = OSSymbol::withCString( "Apple_HFS" );
407 if( str == 0 ) {
408 dict->release();
409 iter->release();
410 return 0;
411 }
412 dict->setObject( "Content", (OSObject *)str );
413 str->release();
414 if ( child->compareProperty( dict, "Content" ) ) {
415 dict->release();
416 break;
417 }
418 dict->release();
419 IOService * subchild = IOFindMatchingChild( child );
420 if ( subchild ) {
421 child = subchild;
422 break;
423 }
424 }
425 iter->release();
426 }
427 return child;
428 }
429
430 static int didRam = 0;
431
432 kern_return_t IOFindBSDRoot( char * rootName,
433 dev_t * root, u_int32_t * oflags )
434 {
435 mach_timespec_t t;
436 IOService * service;
437 IORegistryEntry * regEntry;
438 OSDictionary * matching = 0;
439 OSString * iostr;
440 OSNumber * off;
441 OSData * data = 0;
442 UInt32 *ramdParms = 0;
443
444 UInt32 flags = 0;
445 int minor, major;
446 bool findHFSChild = false;
447 char * mediaProperty = 0;
448 char * rdBootVar;
449 enum { kMaxPathBuf = 512, kMaxBootVar = 128 };
450 char * str;
451 const char * look = 0;
452 int len;
453 bool forceNet = false;
454 bool debugInfoPrintedOnce = false;
455 const char * uuidStr = NULL;
456
457 static int mountAttempts = 0;
458
459 int xchar, dchar;
460
461
462 if( mountAttempts++)
463 IOSleep( 5 * 1000 );
464
465 str = (char *) IOMalloc( kMaxPathBuf + kMaxBootVar );
466 if( !str)
467 return( kIOReturnNoMemory );
468 rdBootVar = str + kMaxPathBuf;
469
470 if (!PE_parse_boot_arg("rd", rdBootVar )
471 && !PE_parse_boot_arg("rootdev", rdBootVar ))
472 rdBootVar[0] = 0;
473
474 do {
475 if( (regEntry = IORegistryEntry::fromPath( "/chosen", gIODTPlane ))) {
476 data = (OSData *) regEntry->getProperty( "boot-uuid" );
477 if( data) {
478 uuidStr = (const char*)data->getBytesNoCopy();
479 OSString *uuidString = OSString::withCString( uuidStr );
480
481 // match the boot-args boot-uuid processing below
482 if( uuidString) {
483 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr);
484 IOService::publishResource( "boot-uuid", uuidString );
485 uuidString->release();
486 matching = IOUUIDMatching();
487 mediaProperty = "boot-uuid-media";
488 regEntry->release();
489 continue;
490 } else {
491 uuidStr = NULL;
492 }
493 }
494
495 // else try for an OF Path
496 data = (OSData *) regEntry->getProperty( "rootpath" );
497 regEntry->release();
498 if( data) continue;
499 }
500 if( (regEntry = IORegistryEntry::fromPath( "/options", gIODTPlane ))) {
501 data = (OSData *) regEntry->getProperty( "boot-file" );
502 regEntry->release();
503 if( data) continue;
504 }
505 } while( false );
506
507 if( data && !uuidStr)
508 look = (const char *) data->getBytesNoCopy();
509
510 if( rdBootVar[0] == '*') {
511 look = rdBootVar + 1;
512 forceNet = false;
513 } else {
514 if( (regEntry = IORegistryEntry::fromPath( "/", gIODTPlane ))) {
515 forceNet = (0 != regEntry->getProperty( "net-boot" ));
516 regEntry->release();
517 }
518 }
519
520
521
522 //
523 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
524 // It will become /dev/mdx, where x is 0-f.
525 //
526
527 if(!didRam) { /* Have we already build this ram disk? */
528 didRam = 1; /* Remember we did this */
529 if((regEntry = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane ))) { /* Find the map node */
530 data = (OSData *)regEntry->getProperty("RAMDisk"); /* Find the ram disk, if there */
531 if(data) { /* We found one */
532
533 ramdParms = (UInt32 *)data->getBytesNoCopy(); /* Point to the ram disk base and size */
534 (void)mdevadd(-1, ramdParms[0] >> 12, ramdParms[1] >> 12, 0); /* Initialize it and pass back the device number */
535 }
536 regEntry->release(); /* Toss the entry */
537 }
538 }
539
540 //
541 // Now check if we are trying to root on a memory device
542 //
543
544 if((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
545 dchar = xchar = rdBootVar[2]; /* Get the actual device */
546 if((xchar >= '0') && (xchar <= '9')) xchar = xchar - '0'; /* If digit, convert */
547 else {
548 xchar = xchar & ~' '; /* Fold to upper case */
549 if((xchar >= 'A') && (xchar <= 'F')) { /* Is this a valid digit? */
550 xchar = (xchar & 0xF) + 9; /* Convert the hex digit */
551 dchar = dchar | ' '; /* Fold to lower case */
552 }
553 else xchar = -1; /* Show bogus */
554 }
555 if(xchar >= 0) { /* Do we have a valid memory device name? */
556 *root = mdevlookup(xchar); /* Find the device number */
557 if(*root >= 0) { /* Did we find one? */
558
559 rootName[0] = 'm'; /* Build root name */
560 rootName[1] = 'd'; /* Build root name */
561 rootName[2] = dchar; /* Build root name */
562 rootName[3] = 0; /* Build root name */
563 IOLog("BSD root: %s, major %d, minor %d\n", rootName, major(*root), minor(*root));
564 *oflags = 0; /* Show that this is not network */
565 goto iofrootx; /* Join common exit... */
566 }
567 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar); /* Not there */
568 }
569 }
570
571 if( look) {
572 // from OpenFirmware path
573 IOLog("From path: \"%s\", ", look);
574
575 if( forceNet || (0 == strncmp( look, "enet", strlen( "enet" ))) ) {
576 matching = IONetworkMatching( look, str, kMaxPathBuf );
577 } else {
578 matching = IODiskMatching( look, str, kMaxPathBuf );
579 }
580 }
581
582 if( (!matching) && rdBootVar[0] ) {
583 // by BSD name
584 look = rdBootVar;
585 if( look[0] == '*')
586 look++;
587
588 if ( strncmp( look, "en", strlen( "en" )) == 0 ) {
589 matching = IONetworkNamePrefixMatching( "en" );
590 } else if ( strncmp( look, "cdrom", strlen( "cdrom" )) == 0 ) {
591 matching = IOCDMatching();
592 findHFSChild = true;
593 } else if ( strncmp( look, "uuid", strlen( "uuid" )) == 0 ) {
594 char *uuid;
595 OSString *uuidString;
596
597 uuid = (char *)IOMalloc( kMaxBootVar );
598
599 if ( uuid ) {
600 if (!PE_parse_boot_arg( "boot-uuid", uuid )) {
601 panic( "rd=uuid but no boot-uuid=<value> specified" );
602 }
603 uuidString = OSString::withCString( uuid );
604 if ( uuidString ) {
605 IOService::publishResource( "boot-uuid", uuidString );
606 uuidString->release();
607 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid );
608 matching = IOUUIDMatching();
609 mediaProperty = "boot-uuid-media";
610 }
611 IOFree( uuid, kMaxBootVar );
612 }
613 } else {
614 matching = IOBSDNameMatching( look );
615 }
616 }
617
618 if( !matching) {
619 OSString * astring;
620 // any HFS
621 matching = IOService::serviceMatching( "IOMedia" );
622 astring = OSString::withCStringNoCopy("Apple_HFS");
623 if ( astring ) {
624 matching->setObject("Content", astring);
625 astring->release();
626 }
627 }
628
629 if( true && matching) {
630 OSSerialize * s = OSSerialize::withCapacity( 5 );
631
632 if( matching->serialize( s )) {
633 IOLog( "Waiting on %s\n", s->text() );
634 s->release();
635 }
636 }
637
638 do {
639 t.tv_sec = ROOTDEVICETIMEOUT;
640 t.tv_nsec = 0;
641 matching->retain();
642 service = IOService::waitForService( matching, &t );
643 if( (!service) || (mountAttempts == 10)) {
644 PE_display_icon( 0, "noroot");
645 IOLog( "Still waiting for root device\n" );
646
647 if( !debugInfoPrintedOnce) {
648 debugInfoPrintedOnce = true;
649 if( gIOKitDebug & kIOLogDTree) {
650 IOLog("\nDT plane:\n");
651 IOPrintPlane( gIODTPlane );
652 }
653 if( gIOKitDebug & kIOLogServiceTree) {
654 IOLog("\nService plane:\n");
655 IOPrintPlane( gIOServicePlane );
656 }
657 if( gIOKitDebug & kIOLogMemory)
658 IOPrintMemory();
659 }
660 }
661 } while( !service);
662 matching->release();
663
664 if ( service && findHFSChild ) {
665 bool waiting = true;
666 // wait for children services to finish registering
667 while ( waiting ) {
668 t.tv_sec = ROOTDEVICETIMEOUT;
669 t.tv_nsec = 0;
670 if ( service->waitQuiet( &t ) == kIOReturnSuccess ) {
671 waiting = false;
672 } else {
673 IOLog( "Waiting for child registration\n" );
674 }
675 }
676 // look for a subservice with an Apple_HFS child
677 IOService * subservice = IOFindMatchingChild( service );
678 if ( subservice ) service = subservice;
679 } else if ( service && mediaProperty ) {
680 service = service->getProperty(mediaProperty);
681 }
682
683 major = 0;
684 minor = 0;
685
686 // If the IOService we matched to is a subclass of IONetworkInterface,
687 // then make sure it has been registered with BSD and has a BSD name
688 // assigned.
689
690 if ( service
691 && service->metaCast( "IONetworkInterface" )
692 && !IORegisterNetworkInterface( service ) )
693 {
694 service = 0;
695 }
696
697 if( service) {
698
699 len = kMaxPathBuf;
700 service->getPath( str, &len, gIOServicePlane );
701 IOLog( "Got boot device = %s\n", str );
702
703 iostr = (OSString *) service->getProperty( kIOBSDNameKey );
704 if( iostr)
705 strcpy( rootName, iostr->getCStringNoCopy() );
706 off = (OSNumber *) service->getProperty( kIOBSDMajorKey );
707 if( off)
708 major = off->unsigned32BitValue();
709 off = (OSNumber *) service->getProperty( kIOBSDMinorKey );
710 if( off)
711 minor = off->unsigned32BitValue();
712
713 if( service->metaCast( "IONetworkInterface" ))
714 flags |= 1;
715
716 } else {
717
718 IOLog( "Wait for root failed\n" );
719 strcpy( rootName, "en0");
720 flags |= 1;
721 }
722
723 IOLog( "BSD root: %s", rootName );
724 if( major)
725 IOLog(", major %d, minor %d\n", major, minor );
726 else
727 IOLog("\n");
728
729 *root = makedev( major, minor );
730 *oflags = flags;
731
732 IOFree( str, kMaxPathBuf + kMaxBootVar );
733
734 iofrootx:
735 if( (gIOKitDebug & (kIOLogDTree | kIOLogServiceTree | kIOLogMemory)) && !debugInfoPrintedOnce) {
736
737 IOService::getPlatform()->waitQuiet();
738 if( gIOKitDebug & kIOLogDTree) {
739 IOLog("\nDT plane:\n");
740 IOPrintPlane( gIODTPlane );
741 }
742 if( gIOKitDebug & kIOLogServiceTree) {
743 IOLog("\nService plane:\n");
744 IOPrintPlane( gIOServicePlane );
745 }
746 if( gIOKitDebug & kIOLogMemory)
747 IOPrintMemory();
748 }
749
750 return( kIOReturnSuccess );
751 }
752
753 void *
754 IOBSDRegistryEntryForDeviceTree(char * path)
755 {
756 return (IORegistryEntry::fromPath(path, gIODTPlane));
757 }
758
759 void
760 IOBSDRegistryEntryRelease(void * entry)
761 {
762 IORegistryEntry * regEntry = (IORegistryEntry *)entry;
763
764 if (regEntry)
765 regEntry->release();
766 return;
767 }
768
769 const void *
770 IOBSDRegistryEntryGetData(void * entry, char * property_name,
771 int * packet_length)
772 {
773 OSData * data;
774 IORegistryEntry * regEntry = (IORegistryEntry *)entry;
775
776 data = (OSData *) regEntry->getProperty(property_name);
777 if (data) {
778 *packet_length = data->getLength();
779 return (data->getBytesNoCopy());
780 }
781 return (NULL);
782 }
783
784 } /* extern "C" */