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