1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /*␊ |
25 | * Copyright 1993 NeXT, Inc.␊ |
26 | * All rights reserved.␊ |
27 | */␊ |
28 | ␊ |
29 | #include "libsaio.h"␊ |
30 | #include "bootstruct.h"␊ |
31 | #include "platform.h"␊ |
32 | ␊ |
33 | boot_args_common *bootArgs = NULL;␊ |
34 | ␊ |
35 | ␊ |
36 | /*==========================================================================␊ |
37 | * structure of parameters passed to␊ |
38 | * the kernel by the booter.␊ |
39 | */␊ |
40 | boot_args_Legacy *bootArgsLegacy = NULL;␊ |
41 | boot_args_107 *bootArgs107 = NULL;␊ |
42 | boot_args_108 *bootArgs108 = NULL;␊ |
43 | /* ... */␊ |
44 | ␊ |
45 | PrivateBootInfo_t *bootInfo = NULL;␊ |
46 | //Node *gMemoryMapNode = NULL;␊ |
47 | ␊ |
48 | static char platformName[64];␊ |
49 | static MemoryRange memoryMap[kMemoryMapCountMax];␊ |
50 | ␊ |
51 | void initKernBootStruct( void )␊ |
52 | { ␊ |
53 | static int init_done = 0;␊ |
54 | ␊ |
55 | if ( !init_done )␊ |
56 | {␉␉␉␊ |
57 | ␉␉int convmem; // conventional memory␊ |
58 | ␉␉int extmem; // extended memory␊ |
59 | ␉␉␊ |
60 | ␉␉unsigned long memoryMapCount = 0;␊ |
61 | ␉␉␊ |
62 | bootArgs = (boot_args_common *)malloc(sizeof(boot_args_common));␊ |
63 | bootInfo = (PrivateBootInfo_t *)malloc(sizeof(PrivateBootInfo_t));␊ |
64 | if (bootArgs == NULL || bootInfo == NULL)␊ |
65 | stop("Couldn't allocate boot info\n");␊ |
66 | else␊ |
67 | {␊ |
68 | bzero(bootArgs, sizeof(boot_args_common));␊ |
69 | bzero(bootInfo, sizeof(PrivateBootInfo_t));␊ |
70 | ␊ |
71 | // Get system memory map. Also update the size of the␊ |
72 | // conventional/extended memory for backwards compatibility.␊ |
73 | ␊ |
74 | ␊ |
75 | memoryMapCount =␊ |
76 | getMemoryMap( memoryMap, kMemoryMapCountMax,␊ |
77 | (unsigned long *) &convmem,␊ |
78 | (unsigned long *) &extmem );␊ |
79 | ␊ |
80 | ␊ |
81 | ␊ |
82 | if ( memoryMapCount == 0 )␊ |
83 | {␊ |
84 | // BIOS did not provide a memory map, systems with␊ |
85 | // discontiguous memory or unusual memory hole locations␊ |
86 | // may have problems.␊ |
87 | ␊ |
88 | convmem = getConventionalMemorySize();␊ |
89 | extmem = getExtendedMemorySize();␉␉␉␊ |
90 | ␊ |
91 | }␊ |
92 | ␉␉␉␊ |
93 | bootArgs->Video.v_display = VGA_TEXT_MODE;␊ |
94 | ␊ |
95 | DT__Initialize();␊ |
96 | ␊ |
97 | {␊ |
98 | Node *node;␊ |
99 | node = DT__FindNode("/", true);␊ |
100 | if (node == 0) {␊ |
101 | stop("Couldn't create root node");␊ |
102 | }␊ |
103 | getPlatformName(platformName);␊ |
104 | ␊ |
105 | {␊ |
106 | int nameLen;␊ |
107 | nameLen = strlen(platformName) + 1;␊ |
108 | DT__AddProperty(node, "compatible", nameLen, platformName);␊ |
109 | DT__AddProperty(node, "model", nameLen, platformName);␊ |
110 | } ␊ |
111 | }␉␉ ␊ |
112 | ␊ |
113 | Node *gMemoryMapNode = DT__FindNode("/chosen/memory-map", true);␊ |
114 | ␊ |
115 | set_env(envConvMem, convmem);␊ |
116 | set_env(envExtMem, extmem);␊ |
117 | set_env(envMemoryMap, (uint32_t)memoryMap);␊ |
118 | set_env(envMemoryMapCnt, memoryMapCount);␊ |
119 | ␉␉␉set_env(envMemoryMapNode, (uint32_t)gMemoryMapNode);␊ |
120 | ␊ |
121 | ␊ |
122 | init_done = 1;␊ |
123 | }␊ |
124 | ␊ |
125 | }␊ |
126 | ␊ |
127 | }␊ |
128 | ␊ |
129 | /* boot args getters/setters. */␊ |
130 | ␊ |
131 | void setBootArgsVideoMode(int mode)␊ |
132 | {␊ |
133 | bootArgs->Video.v_display = mode;␊ |
134 | }␊ |
135 | //==========================================================================␊ |
136 | // Return the current video mode.␊ |
137 | uint32_t getVideoMode(void)␊ |
138 | {␊ |
139 | return bootArgs->Video.v_display;␊ |
140 | }␊ |
141 | void setBootArgsVideoStruct(Boot_Video␉*Video)␊ |
142 | {␊ |
143 | bootArgs->Video.v_display = Video->v_display;␊ |
144 | bootArgs->Video.v_width = Video->v_width;␊ |
145 | bootArgs->Video.v_height = Video->v_height;␊ |
146 | bootArgs->Video.v_depth = Video->v_depth;␊ |
147 | bootArgs->Video.v_rowBytes = Video->v_rowBytes;␊ |
148 | bootArgs->Video.v_baseAddr = Video->v_baseAddr;␊ |
149 | return;␊ |
150 | }␊ |
151 | boot_args_common * getBootArgs(void)␊ |
152 | {␊ |
153 | return bootArgs;␊ |
154 | }␊ |
155 | ␊ |
156 | #define AllocateKernelMemoryForBootArgs(Ver) \␊ |
157 | { \␊ |
158 | bootArgs##Ver = (boot_args_##Ver *)AllocateKernelMemory(sizeof(boot_args_##Ver));\␊ |
159 | }␊ |
160 | ␊ |
161 | #define CopyCommonBootArgsHeader(Ver) \␊ |
162 | { \␊ |
163 | bootArgs##Ver->Revision = bootArgs->Header.Revision ;\␊ |
164 | bootArgs##Ver->Version = bootArgs->Header.Version ;\␊ |
165 | }␊ |
166 | ␊ |
167 | // For 10.6, 10.5 and 10.4 please use :Legacy:, for 10.7 use :107:, for 10.8 use :108:␊ |
168 | #define CopyCommonBootArgs(Ver) \␊ |
169 | { \␊ |
170 | bcopy(bootArgs->CommandLine, bootArgs##Ver->CommandLine, BOOT_LINE_LENGTH);\␊ |
171 | bootArgs##Ver->MemoryMap = bootArgs->MemoryMap ;\␊ |
172 | bootArgs##Ver->MemoryMapSize = bootArgs->MemoryMapSize ;\␊ |
173 | bootArgs##Ver->MemoryMapDescriptorSize = bootArgs->MemoryMapDescriptorSize ;\␊ |
174 | bootArgs##Ver->MemoryMapDescriptorVersion = bootArgs->MemoryMapDescriptorVersion ;\␊ |
175 | bootArgs##Ver->Video = bootArgs->Video ;\␊ |
176 | bootArgs##Ver->deviceTreeP = bootArgs->deviceTreeP ;\␊ |
177 | bootArgs##Ver->deviceTreeLength = bootArgs->deviceTreeLength ;\␊ |
178 | bootArgs##Ver->kaddr = bootArgs->kaddr ;\␊ |
179 | bootArgs##Ver->ksize = bootArgs->ksize ;\␊ |
180 | bootArgs##Ver->efiRuntimeServicesPageStart = bootArgs->efiRuntimeServicesPageStart ;\␊ |
181 | bootArgs##Ver->efiRuntimeServicesPageCount = bootArgs->efiRuntimeServicesPageCount ;\␊ |
182 | bootArgs##Ver->efiSystemTable = bootArgs->efiSystemTable ;\␊ |
183 | bootArgs##Ver->efiMode = bootArgs->efiMode ;\␊ |
184 | bootArgs##Ver->performanceDataStart = bootArgs->performanceDataStart ;\␊ |
185 | bootArgs##Ver->performanceDataSize = bootArgs->performanceDataSize ;\␊ |
186 | bootArgs##Ver->efiRuntimeServicesVirtualPageStart = bootArgs->efiRuntimeServicesVirtualPageStart ;\␊ |
187 | }␊ |
188 | ␊ |
189 | /* ␊ |
190 | * Darwin 10.7+ specific boot arguments ␊ |
191 | *␊ |
192 | * for 10.7 use :107:, for 10.8 use :108:␊ |
193 | */␊ |
194 | #define Copy107plusBootArgs(Ver) \␊ |
195 | { \␊ |
196 | bootArgs##Ver->keyStoreDataStart = bootArgs->keyStoreDataStart ;\␊ |
197 | bootArgs##Ver->keyStoreDataSize = bootArgs->keyStoreDataSize ;\␊ |
198 | bootArgs##Ver->bootMemStart = bootArgs->bootMemStart ;\␊ |
199 | bootArgs##Ver->bootMemSize = bootArgs->bootMemSize ;\␊ |
200 | bootArgs##Ver->PhysicalMemorySize = bootArgs->PhysicalMemorySize ;\␊ |
201 | bootArgs##Ver->FSBFrequency = bootArgs->FSBFrequency ;\␊ |
202 | bootArgs##Ver->debugMode = bootArgs->debugMode ;\␊ |
203 | }␊ |
204 | ␊ |
205 | #define init_boot_args(Ver) \␊ |
206 | { \␊ |
207 | AllocateKernelMemoryForBootArgs(Ver);\␊ |
208 | CopyCommonBootArgsHeader(Ver);\␊ |
209 | CopyCommonBootArgs(Ver);\␊ |
210 | }␊ |
211 | ␊ |
212 | /* Copy boot args after kernel and record address. */␊ |
213 | ␊ |
214 | void␊ |
215 | reserveKern107BootStruct(void)␊ |
216 | {␉␊ |
217 | ␉init_boot_args(107);␊ |
218 | ␉Copy107plusBootArgs(107);␊ |
219 | }␊ |
220 | ␊ |
221 | void␊ |
222 | reserveKern108BootStruct(void)␊ |
223 | {␉␊ |
224 | ␉init_boot_args(108);␉␊ |
225 | ␉Copy107plusBootArgs(108);␊ |
226 | ␉␊ |
227 | ␉/* Darwin 10.8 specific boot arguments */␉␉␊ |
228 | }␊ |
229 | ␊ |
230 | void␊ |
231 | reserveKernLegacyBootStruct(void)␊ |
232 | {␊ |
233 | ␉init_boot_args(Legacy);␊ |
234 | }␊ |
235 | ␊ |
236 | void␊ |
237 | finalizeBootStruct(void)␊ |
238 | { ␊ |
239 | ␉{␊ |
240 | ␉␉int i;␊ |
241 | ␉␉EfiMemoryRange *kMemoryMap = NULL;␊ |
242 | ␉␉MemoryRange *range = NULL;␊ |
243 | ␉␉␊ |
244 | ␉␉uint64_t␉sane_size = 0; /* Memory size to use for defaults calculations */␊ |
245 | ␉␉␊ |
246 | ␉␉int memoryMapCount = (int)get_env(envMemoryMapCnt);␊ |
247 | ␉␉␊ |
248 | ␉␉if (memoryMapCount == 0) {␊ |
249 | ␉␉␉␊ |
250 | ␉␉␉// XXX could make a two-part map here␊ |
251 | ␉␉␉stop("No memory map found\n");␊ |
252 | ␉␉}␊ |
253 | ␉␉␊ |
254 | ␉␉␊ |
255 | ␉␉␊ |
256 | ␉␉// convert memory map to boot_args memory map␊ |
257 | ␉␉kMemoryMap = (EfiMemoryRange *)AllocateKernelMemory(sizeof(EfiMemoryRange) * memoryMapCount);␊ |
258 | ␉␉if (kMemoryMap == NULL) {␊ |
259 | ␉␉␉␊ |
260 | ␉␉␉stop("Unable to allocate kernel space for the memory map\n");␊ |
261 | return;␊ |
262 | ␉␉}␊ |
263 | ␉␉bootArgs->MemoryMap = (uint32_t)kMemoryMap;␊ |
264 | ␉␉bootArgs->MemoryMapSize = sizeof(EfiMemoryRange) * memoryMapCount;␊ |
265 | ␉␉bootArgs->MemoryMapDescriptorSize = sizeof(EfiMemoryRange);␊ |
266 | ␉␉bootArgs->MemoryMapDescriptorVersion = 0;␊ |
267 | ␉␉␊ |
268 | ␉␉for (i=0; i<memoryMapCount; i++, kMemoryMap++) {␊ |
269 | ␉␉␉range = &memoryMap[i];␊ |
270 | if (!range || !kMemoryMap) {␊ |
271 | stop("Error while computing kernel memory map\n");␊ |
272 | return;␊ |
273 | }␊ |
274 | ␉␉␉switch(range->type) {␊ |
275 | ␉␉␉␉case kMemoryRangeACPI:␊ |
276 | ␉␉␉␉␉kMemoryMap->Type = kEfiACPIReclaimMemory;␊ |
277 | ␉␉␉␉␉break;␊ |
278 | ␉␉␉␉case kMemoryRangeNVS:␊ |
279 | ␉␉␉␉␉kMemoryMap->Type = kEfiACPIMemoryNVS;␊ |
280 | ␉␉␉␉␉break;␊ |
281 | ␉␉␉␉case kMemoryRangeUsable:␊ |
282 | ␉␉␉␉␉kMemoryMap->Type = kEfiConventionalMemory;␊ |
283 | ␉␉␉␉␉break;␊ |
284 | ␉␉␉␉case kMemoryRangeReserved:␊ |
285 | ␉␉␉␉default:␊ |
286 | ␉␉␉␉␉kMemoryMap->Type = kEfiReservedMemoryType;␊ |
287 | ␉␉␉␉␉break;␊ |
288 | ␉␉␉}␊ |
289 | ␉␉␉kMemoryMap->PhysicalStart = range->base;␊ |
290 | ␉␉␉kMemoryMap->VirtualStart = range->base;␊ |
291 | ␉␉␉kMemoryMap->NumberOfPages = range->length >> I386_PGSHIFT;␊ |
292 | ␉␉␉kMemoryMap->Attribute = 0;␊ |
293 | ␉␉␉␊ |
294 | ␉␉␉switch (kMemoryMap->Type) {␊ |
295 | ␉␉␉␉case kEfiLoaderCode:␊ |
296 | ␉␉␉␉case kEfiLoaderData:␊ |
297 | ␉␉␉␉case kEfiBootServicesCode:␊ |
298 | ␉␉␉␉case kEfiBootServicesData:␊ |
299 | ␉␉␉␉case kEfiConventionalMemory:␊ |
300 | ␉␉␉␉␉/*␊ |
301 | ␉␉␉␉␉ * Consolidate usable memory types into one.␊ |
302 | ␉␉␉␉␉ */␊ |
303 | ␉␉␉␉␉sane_size += (uint64_t)(kMemoryMap->NumberOfPages << I386_PGSHIFT);␊ |
304 | ␉␉␉␉␉break;␊ |
305 | ␉␉␉␉␉␊ |
306 | ␉␉␉␉case kEfiRuntimeServicesCode:␊ |
307 | ␉␉␉␉case kEfiRuntimeServicesData:␊ |
308 | ␉␉␉␉case kEfiACPIReclaimMemory:␊ |
309 | ␉␉␉␉case kEfiACPIMemoryNVS:␊ |
310 | ␉␉␉␉case kEfiPalCode:␊ |
311 | ␉␉␉␉␉/*␊ |
312 | ␉␉␉␉␉ * sane_size should reflect the total amount of physical ram␊ |
313 | ␉␉␉␉␉ * in the system, not just the amount that is available for␊ |
314 | ␉␉␉␉␉ * the OS to use␊ |
315 | ␉␉␉␉␉ */␊ |
316 | ␉␉␉␉␉sane_size += (uint64_t)(kMemoryMap->NumberOfPages << I386_PGSHIFT);␊ |
317 | ␉␉␉␉␉break;␊ |
318 | ␉␉␉␉default:␊ |
319 | ␉␉␉␉␉break;␊ |
320 | ␉␉␉␉␉␊ |
321 | ␉␉␉}␊ |
322 | ␉␉}␊ |
323 | ␉␉␊ |
324 | ␉␉if (sane_size == 0) {␊ |
325 | ␉␉␉␊ |
326 | ␉␉␉// I Guess that if sane_size == 0 we've got a big problem here, ␊ |
327 | ␉␉␉// and it means that the memory map was not converted properly␊ |
328 | ␉␉␉stop("Unable to convert memory map into proper format\n");␊ |
329 | ␉␉}␊ |
330 | ␉␉␊ |
331 | #define MEG␉␉(1024*1024)␊ |
332 | ␉␉␊ |
333 | ␉␉/*␊ |
334 | ␉␉ * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory␊ |
335 | ␉␉ * not reported by EFI.␊ |
336 | ␉␉ */␊ |
337 | ␉␉␊ |
338 | ␉␉sane_size = (sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1));␊ |
339 | ␉␉bootArgs->PhysicalMemorySize = sane_size;␊ |
340 | ␉␉bootArgs->FSBFrequency = get_env(envFSBFreq);␊ |
341 | ␊ |
342 | ␉}␉␊ |
343 | ␊ |
344 | ␊ |
345 | ␉{␊ |
346 | ␉␉uint32_t size;␊ |
347 | ␉␉void *addr;␊ |
348 | ␉␉// Flatten device tree␊ |
349 | ␉␉DT__FlattenDeviceTree(0, &size);␊ |
350 | ␉␉addr = (void *)AllocateKernelMemory(size);␊ |
351 | ␉␉if (addr == 0) {␊ |
352 | ␉␉␉stop("Couldn't allocate device tree\n");␊ |
353 | ␉␉}␊ |
354 | ␉␉␊ |
355 | ␉␉DT__FlattenDeviceTree((void **)&addr, &size);␊ |
356 | if (!size) {␊ |
357 | stop("Couldn't get flatten device tree\n");␊ |
358 | }␊ |
359 | ␉␉bootArgs->deviceTreeP = (uint32_t)addr;␊ |
360 | ␉␉bootArgs->deviceTreeLength = size;␊ |
361 | ␉} ␊ |
362 | ␊ |
363 | }␊ |
364 | |