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 | 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 | ␊ |
120 | ␊ |
121 | init_done = 1;␊ |
122 | }␊ |
123 | ␊ |
124 | }␊ |
125 | ␊ |
126 | }␊ |
127 | ␊ |
128 | /* boot args getters/setters. */␊ |
129 | ␊ |
130 | void setBootArgsVideoMode(int mode)␊ |
131 | {␊ |
132 | bootArgs->Video.v_display = mode;␊ |
133 | }␊ |
134 | //==========================================================================␊ |
135 | // Return the current video mode.␊ |
136 | uint32_t getVideoMode(void)␊ |
137 | {␊ |
138 | return bootArgs->Video.v_display;␊ |
139 | }␊ |
140 | void setBootArgsVideoStruct(Boot_Video␉*Video)␊ |
141 | {␊ |
142 | bootArgs->Video.v_display = Video->v_display;␊ |
143 | bootArgs->Video.v_width = Video->v_width;␊ |
144 | bootArgs->Video.v_height = Video->v_height;␊ |
145 | bootArgs->Video.v_depth = Video->v_depth;␊ |
146 | bootArgs->Video.v_rowBytes = Video->v_rowBytes;␊ |
147 | bootArgs->Video.v_baseAddr = Video->v_baseAddr;␊ |
148 | return;␊ |
149 | }␊ |
150 | boot_args_common * getBootArgs(void)␊ |
151 | {␊ |
152 | return bootArgs;␊ |
153 | }␊ |
154 | ␊ |
155 | #define AllocateKernelMemoryForBootArgs(Ver) \␊ |
156 | { \␊ |
157 | bootArgs##Ver = (boot_args_##Ver *)AllocateKernelMemory(sizeof(boot_args_##Ver));\␊ |
158 | }␊ |
159 | ␊ |
160 | #define CopyCommonBootArgsHeader(Ver) \␊ |
161 | { \␊ |
162 | bootArgs##Ver->Revision = bootArgs->Header.Revision ;\␊ |
163 | bootArgs##Ver->Version = bootArgs->Header.Version ;\␊ |
164 | }␊ |
165 | ␊ |
166 | // For 10.6, 10.5 and 10.4 please use :Legacy:, for 10.7 use :107:, for 10.8 use :108:␊ |
167 | #define CopyCommonBootArgs(Ver) \␊ |
168 | { \␊ |
169 | bcopy(bootArgs->CommandLine, bootArgs##Ver->CommandLine, BOOT_LINE_LENGTH);\␊ |
170 | bootArgs##Ver->MemoryMap = bootArgs->MemoryMap ;\␊ |
171 | bootArgs##Ver->MemoryMapSize = bootArgs->MemoryMapSize ;\␊ |
172 | bootArgs##Ver->MemoryMapDescriptorSize = bootArgs->MemoryMapDescriptorSize ;\␊ |
173 | bootArgs##Ver->MemoryMapDescriptorVersion = bootArgs->MemoryMapDescriptorVersion ;\␊ |
174 | bootArgs##Ver->Video = bootArgs->Video ;\␊ |
175 | bootArgs##Ver->deviceTreeP = bootArgs->deviceTreeP ;\␊ |
176 | bootArgs##Ver->deviceTreeLength = bootArgs->deviceTreeLength ;\␊ |
177 | bootArgs##Ver->kaddr = bootArgs->kaddr ;\␊ |
178 | bootArgs##Ver->ksize = bootArgs->ksize ;\␊ |
179 | bootArgs##Ver->efiRuntimeServicesPageStart = bootArgs->efiRuntimeServicesPageStart ;\␊ |
180 | bootArgs##Ver->efiRuntimeServicesPageCount = bootArgs->efiRuntimeServicesPageCount ;\␊ |
181 | bootArgs##Ver->efiSystemTable = bootArgs->efiSystemTable ;\␊ |
182 | bootArgs##Ver->efiMode = bootArgs->efiMode ;\␊ |
183 | bootArgs##Ver->performanceDataStart = bootArgs->performanceDataStart ;\␊ |
184 | bootArgs##Ver->performanceDataSize = bootArgs->performanceDataSize ;\␊ |
185 | bootArgs##Ver->efiRuntimeServicesVirtualPageStart = bootArgs->efiRuntimeServicesVirtualPageStart ;\␊ |
186 | }␊ |
187 | ␊ |
188 | /* ␊ |
189 | * Darwin 10.7+ specific boot arguments ␊ |
190 | *␊ |
191 | * for 10.7 use :107:, for 10.8 use :108:␊ |
192 | */␊ |
193 | #define Copy107plusBootArgs(Ver) \␊ |
194 | { \␊ |
195 | bootArgs##Ver->keyStoreDataStart = bootArgs->keyStoreDataStart ;\␊ |
196 | bootArgs##Ver->keyStoreDataSize = bootArgs->keyStoreDataSize ;\␊ |
197 | bootArgs##Ver->bootMemStart = bootArgs->bootMemStart ;\␊ |
198 | bootArgs##Ver->bootMemSize = bootArgs->bootMemSize ;\␊ |
199 | bootArgs##Ver->PhysicalMemorySize = bootArgs->PhysicalMemorySize ;\␊ |
200 | bootArgs##Ver->FSBFrequency = bootArgs->FSBFrequency ;\␊ |
201 | bootArgs##Ver->debugMode = bootArgs->debugMode ;\␊ |
202 | }␊ |
203 | ␊ |
204 | #define init_boot_args(Ver) \␊ |
205 | { \␊ |
206 | AllocateKernelMemoryForBootArgs(Ver);\␊ |
207 | CopyCommonBootArgsHeader(Ver);\␊ |
208 | CopyCommonBootArgs(Ver);\␊ |
209 | }␊ |
210 | ␊ |
211 | /* Copy boot args after kernel and record address. */␊ |
212 | ␊ |
213 | void␊ |
214 | reserveKern107BootStruct(void)␊ |
215 | {␉␊ |
216 | ␉init_boot_args(107);␊ |
217 | ␉Copy107plusBootArgs(107);␊ |
218 | }␊ |
219 | ␊ |
220 | void␊ |
221 | reserveKern108BootStruct(void)␊ |
222 | {␉␊ |
223 | ␉init_boot_args(108);␉␊ |
224 | ␉Copy107plusBootArgs(108);␊ |
225 | ␉␊ |
226 | ␉/* Darwin 10.8 specific boot arguments */␉␉␊ |
227 | }␊ |
228 | ␊ |
229 | void␊ |
230 | reserveKernLegacyBootStruct(void)␊ |
231 | {␊ |
232 | ␉init_boot_args(Legacy);␊ |
233 | }␊ |
234 | ␊ |
235 | void␊ |
236 | finalizeBootStruct(void)␊ |
237 | { ␊ |
238 | ␉{␊ |
239 | ␉␉int i;␊ |
240 | ␉␉EfiMemoryRange *kMemoryMap = NULL;␊ |
241 | ␉␉MemoryRange *range = NULL;␊ |
242 | ␉␉␊ |
243 | ␉␉uint64_t␉sane_size = 0; /* Memory size to use for defaults calculations */␊ |
244 | ␉␉␊ |
245 | ␉␉int memoryMapCount = (int)get_env(envMemoryMapCnt);␊ |
246 | ␉␉␊ |
247 | ␉␉if (memoryMapCount == 0) {␊ |
248 | ␉␉␉␊ |
249 | ␉␉␉// XXX could make a two-part map here␊ |
250 | ␉␉␉stop("No memory map found\n");␊ |
251 | ␉␉}␊ |
252 | ␉␉␊ |
253 | ␉␉␊ |
254 | ␉␉␊ |
255 | ␉␉// convert memory map to boot_args memory map␊ |
256 | ␉␉kMemoryMap = (EfiMemoryRange *)AllocateKernelMemory(sizeof(EfiMemoryRange) * memoryMapCount);␊ |
257 | ␉␉if (kMemoryMap == NULL) {␊ |
258 | ␉␉␉␊ |
259 | ␉␉␉stop("Unable to allocate kernel space for the memory map\n");␊ |
260 | return;␊ |
261 | ␉␉}␊ |
262 | ␉␉bootArgs->MemoryMap = (uint32_t)kMemoryMap;␊ |
263 | ␉␉bootArgs->MemoryMapSize = sizeof(EfiMemoryRange) * memoryMapCount;␊ |
264 | ␉␉bootArgs->MemoryMapDescriptorSize = sizeof(EfiMemoryRange);␊ |
265 | ␉␉bootArgs->MemoryMapDescriptorVersion = 0;␊ |
266 | ␉␉␊ |
267 | ␉␉for (i=0; i<memoryMapCount; i++, kMemoryMap++) {␊ |
268 | ␉␉␉range = &memoryMap[i];␊ |
269 | if (!range || !kMemoryMap) {␊ |
270 | stop("Error while computing kernel memory map\n");␊ |
271 | return;␊ |
272 | }␊ |
273 | ␉␉␉switch(range->type) {␊ |
274 | ␉␉␉␉case kMemoryRangeACPI:␊ |
275 | ␉␉␉␉␉kMemoryMap->Type = kEfiACPIReclaimMemory;␊ |
276 | ␉␉␉␉␉break;␊ |
277 | ␉␉␉␉case kMemoryRangeNVS:␊ |
278 | ␉␉␉␉␉kMemoryMap->Type = kEfiACPIMemoryNVS;␊ |
279 | ␉␉␉␉␉break;␊ |
280 | ␉␉␉␉case kMemoryRangeUsable:␊ |
281 | ␉␉␉␉␉kMemoryMap->Type = kEfiConventionalMemory;␊ |
282 | ␉␉␉␉␉break;␊ |
283 | ␉␉␉␉case kMemoryRangeReserved:␊ |
284 | ␉␉␉␉default:␊ |
285 | ␉␉␉␉␉kMemoryMap->Type = kEfiReservedMemoryType;␊ |
286 | ␉␉␉␉␉break;␊ |
287 | ␉␉␉}␊ |
288 | ␉␉␉kMemoryMap->PhysicalStart = range->base;␊ |
289 | ␉␉␉kMemoryMap->VirtualStart = range->base;␊ |
290 | ␉␉␉kMemoryMap->NumberOfPages = range->length >> I386_PGSHIFT;␊ |
291 | ␉␉␉kMemoryMap->Attribute = 0;␊ |
292 | ␉␉␉␊ |
293 | ␉␉␉switch (kMemoryMap->Type) {␊ |
294 | ␉␉␉␉case kEfiLoaderCode:␊ |
295 | ␉␉␉␉case kEfiLoaderData:␊ |
296 | ␉␉␉␉case kEfiBootServicesCode:␊ |
297 | ␉␉␉␉case kEfiBootServicesData:␊ |
298 | ␉␉␉␉case kEfiConventionalMemory:␊ |
299 | ␉␉␉␉␉/*␊ |
300 | ␉␉␉␉␉ * Consolidate usable memory types into one.␊ |
301 | ␉␉␉␉␉ */␊ |
302 | ␉␉␉␉␉sane_size += (uint64_t)(kMemoryMap->NumberOfPages << I386_PGSHIFT);␊ |
303 | ␉␉␉␉␉break;␊ |
304 | ␉␉␉␉␉␊ |
305 | ␉␉␉␉case kEfiRuntimeServicesCode:␊ |
306 | ␉␉␉␉case kEfiRuntimeServicesData:␊ |
307 | ␉␉␉␉case kEfiACPIReclaimMemory:␊ |
308 | ␉␉␉␉case kEfiACPIMemoryNVS:␊ |
309 | ␉␉␉␉case kEfiPalCode:␊ |
310 | ␉␉␉␉␉/*␊ |
311 | ␉␉␉␉␉ * sane_size should reflect the total amount of physical ram␊ |
312 | ␉␉␉␉␉ * in the system, not just the amount that is available for␊ |
313 | ␉␉␉␉␉ * the OS to use␊ |
314 | ␉␉␉␉␉ */␊ |
315 | ␉␉␉␉␉sane_size += (uint64_t)(kMemoryMap->NumberOfPages << I386_PGSHIFT);␊ |
316 | ␉␉␉␉␉break;␊ |
317 | ␉␉␉␉default:␊ |
318 | ␉␉␉␉␉break;␊ |
319 | ␉␉␉␉␉␊ |
320 | ␉␉␉}␊ |
321 | ␉␉}␊ |
322 | ␉␉␊ |
323 | ␉␉if (sane_size == 0) {␊ |
324 | ␉␉␉␊ |
325 | ␉␉␉// I Guess that if sane_size == 0 we've got a big problem here, ␊ |
326 | ␉␉␉// and it means that the memory map was not converted properly␊ |
327 | ␉␉␉stop("Unable to convert memory map into proper format\n");␊ |
328 | ␉␉}␊ |
329 | ␉␉␊ |
330 | #define MEG␉␉(1024*1024)␊ |
331 | ␉␉␊ |
332 | ␉␉/*␊ |
333 | ␉␉ * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory␊ |
334 | ␉␉ * not reported by EFI.␊ |
335 | ␉␉ */␊ |
336 | ␉␉␊ |
337 | ␉␉sane_size = (sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1));␊ |
338 | ␉␉bootArgs->PhysicalMemorySize = sane_size;␊ |
339 | ␉␉bootArgs->FSBFrequency = get_env(envFSBFreq);␊ |
340 | ␊ |
341 | ␉}␉␊ |
342 | ␊ |
343 | ␊ |
344 | ␉{␊ |
345 | ␉␉uint32_t size;␊ |
346 | ␉␉void *addr;␊ |
347 | ␉␉// Flatten device tree␊ |
348 | ␉␉DT__FlattenDeviceTree(0, &size);␊ |
349 | ␉␉addr = (void *)AllocateKernelMemory(size);␊ |
350 | ␉␉if (addr == 0) {␊ |
351 | ␉␉␉stop("Couldn't allocate device tree\n");␊ |
352 | ␉␉}␊ |
353 | ␉␉␊ |
354 | ␉␉DT__FlattenDeviceTree((void **)&addr, &size);␊ |
355 | if (!size) {␊ |
356 | stop("Couldn't get flatten device tree\n");␊ |
357 | }␊ |
358 | ␉␉bootArgs->deviceTreeP = (uint32_t)addr;␊ |
359 | ␉␉bootArgs->deviceTreeLength = size;␊ |
360 | ␉} ␊ |
361 | ␊ |
362 | }␊ |
363 | |