1/*
2 * Copyright (c) 1998-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <libkern/c++/OSUnserialize.h>
30#include <libkern/c++/OSKext.h>
31#include <libkern/version.h>
32#include <IOKit/IORegistryEntry.h>
33#include <IOKit/IODeviceTreeSupport.h>
34#include <IOKit/IOCatalogue.h>
35#include <IOKit/IOUserClient.h>
36#include <IOKit/IOMemoryDescriptor.h>
37#include <IOKit/IOPlatformExpert.h>
38#include <IOKit/IOLib.h>
39#include <IOKit/IOKitKeys.h>
40#include <IOKit/IOKitDebug.h>
41#include <IOKit/pwr_mgt/RootDomain.h>
42#include <IOKit/pwr_mgt/IOPMinformeeList.h>
43#include <IOKit/IOStatisticsPrivate.h>
44#include <IOKit/IOKitKeysPrivate.h>
45
46#include <IOKit/assert.h>
47
48#include "IOKitKernelInternal.h"
49
50extern "C" {
51
52extern void OSlibkernInit (void);
53
54void iokit_post_constructor_init(void) __attribute__((section("__TEXT, initcode")));
55
56#include <kern/clock.h>
57#include <sys/time.h>
58
59void IOKitInitializeTime( void )
60{
61	mach_timespec_t		t;
62
63	t.tv_sec = 2;
64	t.tv_nsec = 0;
65
66	IOService::waitForService(IOService::resourceMatching("IORTC"), &t );
67#if defined(__i386__) || defined(__x86_64__)
68	IOService::waitForService(IOService::resourceMatching("IONVRAM"), &t );
69#endif
70
71    clock_initialize_calendar();
72}
73
74void IOKitResetTime( void )
75{
76    clock_sec_t		secs;
77	clock_usec_t	microsecs;
78
79    clock_initialize_calendar();
80
81    clock_get_calendar_microtime(&secs, &microsecs);
82    gIOLastWakeTime.tv_sec  = secs;
83    gIOLastWakeTime.tv_usec = microsecs;
84
85    IOService::updateConsoleUsers(NULL, kIOMessageSystemHasPoweredOn);
86}
87
88void iokit_post_constructor_init(void)
89{
90    IORegistryEntry *		root;
91    OSObject *			obj;
92
93    root = IORegistryEntry::initialize();
94    assert( root );
95    IOService::initialize();
96    IOCatalogue::initialize();
97    IOStatistics::initialize();
98    OSKext::initialize();
99    IOUserClient::initialize();
100    IOMemoryDescriptor::initialize();
101    IORootParent::initialize();
102
103    // Initializes IOPMinformeeList class-wide shared lock
104    IOPMinformeeList::getSharedRecursiveLock();
105
106    obj = OSString::withCString( version );
107    assert( obj );
108    if( obj ) {
109        root->setProperty( kIOKitBuildVersionKey, obj );
110	obj->release();
111    }
112    obj = IOKitDiagnostics::diagnostics();
113    if( obj ) {
114        root->setProperty( kIOKitDiagnosticsKey, obj );
115	obj->release();
116    }
117}
118
119// From <osfmk/kern/debug.c>
120extern int debug_mode;
121
122/*****
123 * Pointer into bootstrap KLD segment for functions never used past startup.
124 */
125void (*record_startup_extensions_function)(void) = 0;
126
127void StartIOKit( void * p1, void * p2, void * p3, void * p4 )
128{
129    IOPlatformExpertDevice *	rootNub;
130    int				debugFlags;
131
132    if( PE_parse_boot_argn( "io", &debugFlags, sizeof (debugFlags) ))
133		gIOKitDebug = debugFlags;
134
135    if( PE_parse_boot_argn( "iotrace", &debugFlags, sizeof (debugFlags) ))
136		gIOKitTrace = debugFlags;
137
138
139	// Compat for boot-args
140	gIOKitTrace |= (gIOKitDebug & kIOTraceCompatBootArgs);
141
142    // Check for the log synchronous bit set in io
143    if (gIOKitDebug & kIOLogSynchronous)
144        debug_mode = true;
145
146    //
147    // Have to start IOKit environment before we attempt to start
148    // the C++ runtime environment.  At some stage we have to clean up
149    // the initialisation path so that OS C++ can initialise independantly
150    // of iokit basic service initialisation, or better we have IOLib stuff
151    // initialise as basic OS services.
152    //
153    IOLibInit();
154    OSlibkernInit();
155
156    rootNub = new IOPlatformExpertDevice;
157
158    if( rootNub && rootNub->initWithArgs( p1, p2, p3, p4)) {
159        rootNub->attach( 0 );
160
161       /* If the bootstrap segment set up a function to record startup
162        * extensions, call it now.
163        */
164        if (record_startup_extensions_function) {
165            record_startup_extensions_function();
166        }
167
168        rootNub->registerService();
169
170#if !NO_KEXTD
171       /* Add a busy count to keep the registry busy until kextd has
172        * completely finished launching. This is decremented when kextd
173        * messages the kernel after the in-kernel linker has been
174        * removed and personalities have been sent.
175        */
176        IOService::getServiceRoot()->adjustBusy(1);
177#endif
178    }
179}
180
181void
182IORegistrySetOSBuildVersion(char * build_version)
183{
184    IORegistryEntry * root = IORegistryEntry::getRegistryRoot();
185
186    if (root) {
187        if (build_version) {
188            root->setProperty(kOSBuildVersionKey, build_version);
189        } else {
190            root->removeProperty(kOSBuildVersionKey);
191        }
192    }
193
194    return;
195}
196
197}; /* extern "C" */
198