Chameleon

Chameleon Svn Source Tree

Root/branches/xZenu/src/arch/ppc/boot2/main.c

Source at commit 1406 created 12 years 10 months ago.
By meklort, Revert drivers.c so that kexts are only loaded when OSBundleRequired is set and that value is not safe mode. Added some comments about it too.
1/*
2 * Copyright (c) 2000-2007 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/*
30 * main.c - Main functions for BootX.
31 *
32 * Copyright (c) 1998-2004 Apple Computer, Inc.
33 *
34 * DRI: Josh de Cesare
35 */
36
37
38#include <sl.h>
39#include <stdio.h>
40#include <string.h>
41
42extern voidmalloc_init(char * start, int size, int nodes, void (*malloc_error)(char *, size_t, const char *, int));
43extern void init_module_system();
44extern intexecute_hook(const char* name, void*, void*, void*, void*);
45
46static void Start(void *unused1, void *unused2, ClientInterfacePtr ciPtr);
47static void Main(ClientInterfacePtr ciPtr);
48static long InitEverything(ClientInterfacePtr ciPtr);
49static long InitMemoryMap(void);
50static long GetOFVersion(void);
51static void malloc_error(char *addr, size_t size, const char *file, int line);
52
53const unsigned long StartTVector[2] = {(unsigned long)Start, 0};
54
55
56char gStackBaseAddr[0x8000];
57
58long gOFVersion = 0;
59
60char *gKeyMap;
61
62long gRootAddrCells;
63long gRootSizeCells;
64
65CICell gChosenPH;
66CICell gMemoryMapPH;
67CICell gStdOutPH;
68
69CICell gMMUIH;
70CICell gMemoryIH;
71CICell gStdOutIH;
72CICell gKeyboardIH;
73
74
75// Private Functions
76
77static void malloc_error(char *addr, size_t size, const char *file, int line)
78{
79printf("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
80while(1);
81}
82
83
84static void Start(void *unused1, void *unused2, ClientInterfacePtr ciPtr)
85{
86long newSP;
87
88// Move the Stack to a chunk of the BSS
89newSP = (long)gStackBaseAddr + sizeof(gStackBaseAddr) - 0x100;
90__asm__ volatile("mr r1, %0" : : "r" (newSP));
91
92Main(ciPtr);
93}
94
95static void Main(ClientInterfacePtr ciPtr)
96{
97int loopCount = 0;
98long ret;
99
100ret = InitEverything(ciPtr);
101if (ret != 0) Exit();
102
103// Intialize module system
104init_module_system();
105
106execute_hook("Initialize", (void*)loopCount++, (void*)ciPtr, 0, 0);// Main work loop
107
108while(1)
109 {
110 execute_hook("WorkLoop", (void*)loopCount++, (void*)ciPtr, 0, 0);// Main work loop
111 }
112
113}
114static long InitEverything(ClientInterfacePtr ciPtr)
115{
116long ret, size;
117CICell keyboardPH;
118char name[32];
119
120// Init the OF Client Interface.
121ret = InitCI(ciPtr);
122if (ret != 0) return -1;
123
124// Get the OF Version
125gOFVersion = GetOFVersion();
126if (gOFVersion == 0) return -1;
127
128// Get the address and size cells for the root.
129GetProp(Peer(0), "#address-cells", (char *)&gRootAddrCells, 4);
130GetProp(Peer(0), "#size-cells", (char *)&gRootSizeCells, 4);
131if ((gRootAddrCells > 2) || (gRootAddrCells > 2)) return -1;
132
133// Init the SL Words package.
134ret = InitSLWords();
135if (ret != 0) return -1;
136
137
138// Get the phandle for /chosen
139gChosenPH = FindDevice("/chosen");
140if (gChosenPH == -1) return -1;
141
142// Init the Memory Map.
143ret = InitMemoryMap();
144if (ret != 0) return -1;
145
146// Get IHandles for the MMU and Memory
147size = GetProp(gChosenPH, "mmu", (char *)&gMMUIH, 4);
148if (size != 4) {
149printf("Failed to get the IH for the MMU.\n");
150return -1;
151}
152size = GetProp(gChosenPH, "memory", (char *)&gMemoryIH, 4);
153if (size != 4) {
154printf("Failed to get the IH for the Memory.\n");
155return -1;
156}
157
158// Get stdout's IH, so that the boot display can be found.
159ret = GetProp(gChosenPH, "stdout", (char *)&gStdOutIH, 4);
160if (ret == 4) gStdOutPH = InstanceToPackage(gStdOutIH);
161else gStdOutPH = gStdOutIH = 0;
162
163// Try to find the keyboard using chosen
164ret = GetProp(gChosenPH, "stdin", (char *)&gKeyboardIH, 4);
165if (ret != 4) gKeyboardIH = 0;
166else {
167keyboardPH = InstanceToPackage(gKeyboardIH);
168ret = GetProp(keyboardPH, "name", name, 31);
169if (ret != -1) {
170name[ret] = '\0';
171if (strcmp(name, "keyboard") && strcmp(name, "kbd")) gKeyboardIH = 0;
172} else gKeyboardIH = 0;
173}
174
175// Try to the find the keyboard using open if chosen did not work.
176if (gKeyboardIH == 0) gKeyboardIH = Open("keyboard");
177if (gKeyboardIH == 0) gKeyboardIH = Open("kbd");
178
179// Get the key map set up, and make it up to date.
180gKeyMap = InitKeyMap(gKeyboardIH);
181if (gKeyMap == NULL) return -1;
182UpdateKeyMap();
183
184SetOutputLevel(kOutputLevelFull);
185
186// printf now works.
187printf("\n\nMac OS X Loader\n");
188
189// Claim memory for malloc.
190if (Claim(kMallocAddr, kMallocSize, 0) == 0) {
191printf("Claim for malloc failed.\n");
192return -1;
193}
194malloc_init((char *)kMallocAddr, kMallocSize, 768, malloc_error);
195
196
197return 0;
198}
199
200static long InitMemoryMap(void)
201{
202long result;
203
204result = Interpret(0, 1,
205 " dev /chosen"
206 " new-device"
207 " \" memory-map\" device-name"
208 " active-package"
209 " device-end"
210 , &gMemoryMapPH);
211
212return result;
213}
214
215
216static long GetOFVersion(void)
217{
218CICell ph;
219char versStr[256], *tmpStr;
220long vers, size;
221
222// Get the openprom package
223ph = FindDevice("/openprom");
224if (ph == -1) return 0;
225
226// Get it's model property
227size = GetProp(ph, "model", versStr, 255);
228if (size == -1) return -1;
229versStr[size] = '\0';
230
231// Find the start of the number.
232tmpStr = NULL;
233if (!strncmp(versStr, "Open Firmware, ", 15)) {
234tmpStr = versStr + 15;
235} else if (!strncmp(versStr, "OpenFirmware ", 13)) {
236tmpStr = versStr + 13;
237} else return -1;
238
239// Clasify by each instance as needed...
240switch (*tmpStr) {
241case '1' :
242vers = kOFVersion1x;
243break;
244
245case '2' :
246vers = kOFVersion2x;
247break;
248
249case '3' :
250vers = kOFVersion3x;
251break;
252
253case '4' :
254vers = kOFVersion4x;
255break;
256
257default :
258vers = 0;
259break;
260}
261
262return vers;
263}

Archive Download this file

Revision: 1406