Chameleon

Chameleon Svn Source Tree

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

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

Archive Download this file

Revision: 1317