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 | ␊ |
50 | void initKernBootStruct( void )␊ |
51 | { ␊ |
52 | static int init_done = 0;␊ |
53 | ␊ |
54 | if ( !init_done )␊ |
55 | {␉␉␉␊ |
56 | bootArgs = (boot_args_common *)malloc(sizeof(boot_args_common));␊ |
57 | bootInfo = (PrivateBootInfo_t *)malloc(sizeof(PrivateBootInfo_t));␊ |
58 | if (bootArgs == NULL || bootInfo == NULL)␊ |
59 | stop("Couldn't allocate boot info\n");␊ |
60 | else␊ |
61 | {␊ |
62 | bzero(bootArgs, sizeof(boot_args_common));␊ |
63 | bzero(bootInfo, sizeof(PrivateBootInfo_t));␊ |
64 | ␊ |
65 | // Get system memory map. Also update the size of the␊ |
66 | // conventional/extended memory for backwards compatibility.␊ |
67 | ␊ |
68 | ␊ |
69 | bootInfo->memoryMapCount =␊ |
70 | getMemoryMap( bootInfo->memoryMap, kMemoryMapCountMax,␊ |
71 | (unsigned long *) &bootInfo->convmem,␊ |
72 | (unsigned long *) &bootInfo->extmem );␊ |
73 | ␊ |
74 | ␊ |
75 | ␊ |
76 | if ( bootInfo->memoryMapCount == 0 )␊ |
77 | {␊ |
78 | // BIOS did not provide a memory map, systems with␊ |
79 | // discontiguous memory or unusual memory hole locations␊ |
80 | // may have problems.␊ |
81 | ␊ |
82 | bootInfo->convmem = getConventionalMemorySize();␊ |
83 | bootInfo->extmem = getExtendedMemorySize();␉␉␉␊ |
84 | ␊ |
85 | }␊ |
86 | #if 0␉␉␊ |
87 | bootInfo->configEnd = bootInfo->config;␊ |
88 | #endif␊ |
89 | bootArgs->Video.v_display = VGA_TEXT_MODE;␊ |
90 | ␊ |
91 | DT__Initialize();␊ |
92 | ␊ |
93 | {␊ |
94 | Node *node;␊ |
95 | node = DT__FindNode("/", true);␊ |
96 | if (node == 0) {␊ |
97 | stop("Couldn't create root node");␊ |
98 | }␊ |
99 | getPlatformName(platformName);␊ |
100 | ␊ |
101 | {␊ |
102 | int nameLen;␊ |
103 | nameLen = strlen(platformName) + 1;␊ |
104 | DT__AddProperty(node, "compatible", nameLen, platformName);␊ |
105 | DT__AddProperty(node, "model", nameLen, platformName);␊ |
106 | } ␊ |
107 | }␉␉ ␊ |
108 | ␊ |
109 | gMemoryMapNode = DT__FindNode("/chosen/memory-map", true);␊ |
110 | ␊ |
111 | safe_set_env(envConvMem, bootInfo->convmem);␊ |
112 | safe_set_env(envExtMem, bootInfo->convmem);␊ |
113 | safe_set_env(envMemoryMap, (uint32_t)bootInfo->memoryMap);␊ |
114 | safe_set_env(envMemoryMapCnt, bootInfo->memoryMapCount);␊ |
115 | ␊ |
116 | ␊ |
117 | init_done = 1;␊ |
118 | }␊ |
119 | ␊ |
120 | }␊ |
121 | ␊ |
122 | }␊ |
123 | ␊ |
124 | /* boot args getters/setters. */␊ |
125 | ␊ |
126 | void setBootArgsVideoMode(int mode)␊ |
127 | {␊ |
128 | bootArgs->Video.v_display = mode;␊ |
129 | }␊ |
130 | uint32_t getVideoMode(void)␊ |
131 | {␊ |
132 | return bootArgs->Video.v_display;␊ |
133 | }␊ |
134 | uint32_t getBootArgsVideoPtrAtOffset(uint32_t offset)␊ |
135 | {␊ |
136 | char *vid = (char *)&bootArgs->Video ;␊ |
137 | return (uint32_t)vid[offset];␊ |
138 | }␊ |
139 | void setBootArgsVideoStruct(Boot_Video␉*Video)␊ |
140 | {␊ |
141 | bootArgs->Video.v_display = Video->v_display;␊ |
142 | bootArgs->Video.v_width = Video->v_width;␊ |
143 | bootArgs->Video.v_height = Video->v_height;␊ |
144 | bootArgs->Video.v_depth = Video->v_depth;␊ |
145 | bootArgs->Video.v_rowBytes = Video->v_rowBytes;␊ |
146 | bootArgs->Video.v_baseAddr = Video->v_baseAddr;␊ |
147 | return;␊ |
148 | }␊ |
149 | ␊ |
150 | #define AllocateKernelMemoryForBootArgs(Ver) \␊ |
151 | { \␊ |
152 | bootArgs##Ver = (boot_args_##Ver *)AllocateKernelMemory(sizeof(boot_args_##Ver));\␊ |
153 | }␊ |
154 | ␊ |
155 | #define CopyCommonBootArgsHeader(Ver) \␊ |
156 | { \␊ |
157 | bootArgs##Ver->Revision = bootArgs->Header.Revision ;\␊ |
158 | bootArgs##Ver->Version = bootArgs->Header.Version ;\␊ |
159 | }␊ |
160 | ␊ |
161 | // For 10.6, 10.5 and 10.4 please use :Legacy:, for 10.7 use :107:, for 10.8 use :108:␊ |
162 | #define CopyCommonBootArgs(Ver) \␊ |
163 | { \␊ |
164 | bcopy(bootArgs->CommandLine, bootArgs##Ver->CommandLine, BOOT_LINE_LENGTH);\␊ |
165 | bootArgs##Ver->MemoryMap = bootArgs->MemoryMap ;\␊ |
166 | bootArgs##Ver->MemoryMapSize = bootArgs->MemoryMapSize ;\␊ |
167 | bootArgs##Ver->MemoryMapDescriptorSize = bootArgs->MemoryMapDescriptorSize ;\␊ |
168 | bootArgs##Ver->MemoryMapDescriptorVersion = bootArgs->MemoryMapDescriptorVersion ;\␊ |
169 | bootArgs##Ver->Video = bootArgs->Video ;\␊ |
170 | bootArgs##Ver->deviceTreeP = bootArgs->deviceTreeP ;\␊ |
171 | bootArgs##Ver->deviceTreeLength = bootArgs->deviceTreeLength ;\␊ |
172 | bootArgs##Ver->kaddr = bootArgs->kaddr ;\␊ |
173 | bootArgs##Ver->ksize = bootArgs->ksize ;\␊ |
174 | bootArgs##Ver->efiRuntimeServicesPageStart = bootArgs->efiRuntimeServicesPageStart ;\␊ |
175 | bootArgs##Ver->efiRuntimeServicesPageCount = bootArgs->efiRuntimeServicesPageCount ;\␊ |
176 | bootArgs##Ver->efiSystemTable = bootArgs->efiSystemTable ;\␊ |
177 | bootArgs##Ver->efiMode = bootArgs->efiMode ;\␊ |
178 | bootArgs##Ver->performanceDataStart = bootArgs->performanceDataStart ;\␊ |
179 | bootArgs##Ver->performanceDataSize = bootArgs->performanceDataSize ;\␊ |
180 | bootArgs##Ver->efiRuntimeServicesVirtualPageStart = bootArgs->efiRuntimeServicesVirtualPageStart ;\␊ |
181 | }␊ |
182 | ␊ |
183 | /* ␊ |
184 | * Darwin 10.7+ specific boot arguments ␊ |
185 | *␊ |
186 | * for 10.7 use :107:, for 10.8 use :108:␊ |
187 | */␊ |
188 | #define Copy107plusBootArgs(Ver) \␊ |
189 | { \␊ |
190 | bootArgs##Ver->keyStoreDataStart = bootArgs->keyStoreDataStart ;\␊ |
191 | bootArgs##Ver->keyStoreDataSize = bootArgs->keyStoreDataSize ;\␊ |
192 | bootArgs##Ver->bootMemStart = bootArgs->bootMemStart ;\␊ |
193 | bootArgs##Ver->bootMemSize = bootArgs->bootMemSize ;\␊ |
194 | bootArgs##Ver->PhysicalMemorySize = bootArgs->PhysicalMemorySize ;\␊ |
195 | bootArgs##Ver->FSBFrequency = bootArgs->FSBFrequency ;\␊ |
196 | bootArgs##Ver->debugMode = bootArgs->debugMode ;\␊ |
197 | }␊ |
198 | ␊ |
199 | #define init_boot_args(Ver) \␊ |
200 | { \␊ |
201 | AllocateKernelMemoryForBootArgs(Ver);\␊ |
202 | CopyCommonBootArgsHeader(Ver);\␊ |
203 | CopyCommonBootArgs(Ver);\␊ |
204 | }␊ |
205 | ␊ |
206 | /* Copy boot args after kernel and record address. */␊ |
207 | ␊ |
208 | void␊ |
209 | reserveKern107BootStruct(void)␊ |
210 | {␉␊ |
211 | ␉init_boot_args(107);␊ |
212 | ␉Copy107plusBootArgs(107);␊ |
213 | }␊ |
214 | ␊ |
215 | void␊ |
216 | reserveKern108BootStruct(void)␊ |
217 | {␉␊ |
218 | ␉init_boot_args(108);␉␊ |
219 | ␉Copy107plusBootArgs(108);␊ |
220 | ␉␊ |
221 | ␉/* Darwin 10.8 specific boot arguments */␉␉␊ |
222 | }␊ |
223 | ␊ |
224 | void␊ |
225 | reserveKernLegacyBootStruct(void)␊ |
226 | {␊ |
227 | ␉init_boot_args(Legacy);␊ |
228 | }␊ |
229 | ␊ |
230 | void␊ |
231 | finalizeBootStruct(void)␊ |
232 | { ␊ |
233 | ␉{␊ |
234 | ␉␉int i;␊ |
235 | ␉␉EfiMemoryRange *memoryMap = NULL;␊ |
236 | ␉␉MemoryRange *range = NULL;␊ |
237 | ␉␉uint64_t␉sane_size = 0; /* Memory size to use for defaults calculations */␊ |
238 | ␉␉␊ |
239 | ␉␉int memoryMapCount = bootInfo->memoryMapCount;␊ |
240 | ␉␉␊ |
241 | ␉␉if (memoryMapCount == 0) {␊ |
242 | ␉␉␉␊ |
243 | ␉␉␉// XXX could make a two-part map here␊ |
244 | ␉␉␉stop("No memory map found\n");␊ |
245 | ␉␉}␊ |
246 | ␉␉␊ |
247 | ␉␉␊ |
248 | ␉␉␊ |
249 | ␉␉// convert memory map to boot_args memory map␊ |
250 | ␉␉memoryMap = (EfiMemoryRange *)AllocateKernelMemory(sizeof(EfiMemoryRange) * memoryMapCount);␊ |
251 | ␉␉if (memoryMap == NULL) {␊ |
252 | ␉␉␉␊ |
253 | ␉␉␉stop("Unable to allocate kernel space for the memory map\n");␊ |
254 | return;␊ |
255 | ␉␉}␊ |
256 | ␉␉bootArgs->MemoryMap = (uint32_t)memoryMap;␊ |
257 | ␉␉bootArgs->MemoryMapSize = sizeof(EfiMemoryRange) * memoryMapCount;␊ |
258 | ␉␉bootArgs->MemoryMapDescriptorSize = sizeof(EfiMemoryRange);␊ |
259 | ␉␉bootArgs->MemoryMapDescriptorVersion = 0;␊ |
260 | ␉␉␊ |
261 | ␉␉for (i=0; i<memoryMapCount; i++, memoryMap++) {␊ |
262 | ␉␉␉range = &bootInfo->memoryMap[i];␊ |
263 | if (!range || !memoryMap) {␊ |
264 | stop("Error while computing kernel memory map\n");␊ |
265 | return;␊ |
266 | }␊ |
267 | ␉␉␉switch(range->type) {␊ |
268 | ␉␉␉␉case kMemoryRangeACPI:␊ |
269 | ␉␉␉␉␉memoryMap->Type = kEfiACPIReclaimMemory;␊ |
270 | ␉␉␉␉␉break;␊ |
271 | ␉␉␉␉case kMemoryRangeNVS:␊ |
272 | ␉␉␉␉␉memoryMap->Type = kEfiACPIMemoryNVS;␊ |
273 | ␉␉␉␉␉break;␊ |
274 | ␉␉␉␉case kMemoryRangeUsable:␊ |
275 | ␉␉␉␉␉memoryMap->Type = kEfiConventionalMemory;␊ |
276 | ␉␉␉␉␉break;␊ |
277 | ␉␉␉␉case kMemoryRangeReserved:␊ |
278 | ␉␉␉␉default:␊ |
279 | ␉␉␉␉␉memoryMap->Type = kEfiReservedMemoryType;␊ |
280 | ␉␉␉␉␉break;␊ |
281 | ␉␉␉}␊ |
282 | ␉␉␉memoryMap->PhysicalStart = range->base;␊ |
283 | ␉␉␉memoryMap->VirtualStart = range->base;␊ |
284 | ␉␉␉memoryMap->NumberOfPages = range->length >> I386_PGSHIFT;␊ |
285 | ␉␉␉memoryMap->Attribute = 0;␊ |
286 | ␉␉␉␊ |
287 | ␉␉␉switch (memoryMap->Type) {␊ |
288 | ␉␉␉␉case kEfiLoaderCode:␊ |
289 | ␉␉␉␉case kEfiLoaderData:␊ |
290 | ␉␉␉␉case kEfiBootServicesCode:␊ |
291 | ␉␉␉␉case kEfiBootServicesData:␊ |
292 | ␉␉␉␉case kEfiConventionalMemory:␊ |
293 | ␉␉␉␉␉/*␊ |
294 | ␉␉␉␉␉ * Consolidate usable memory types into one.␊ |
295 | ␉␉␉␉␉ */␊ |
296 | ␉␉␉␉␉sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);␊ |
297 | ␉␉␉␉␉break;␊ |
298 | ␉␉␉␉␉␊ |
299 | ␉␉␉␉case kEfiRuntimeServicesCode:␊ |
300 | ␉␉␉␉case kEfiRuntimeServicesData:␊ |
301 | ␉␉␉␉case kEfiACPIReclaimMemory:␊ |
302 | ␉␉␉␉case kEfiACPIMemoryNVS:␊ |
303 | ␉␉␉␉case kEfiPalCode:␊ |
304 | ␉␉␉␉␉/*␊ |
305 | ␉␉␉␉␉ * sane_size should reflect the total amount of physical ram␊ |
306 | ␉␉␉␉␉ * in the system, not just the amount that is available for␊ |
307 | ␉␉␉␉␉ * the OS to use␊ |
308 | ␉␉␉␉␉ */␊ |
309 | ␉␉␉␉␉sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);␊ |
310 | ␉␉␉␉␉break;␊ |
311 | ␉␉␉␉default:␊ |
312 | ␉␉␉␉␉break;␊ |
313 | ␉␉␉␉␉␊ |
314 | ␉␉␉}␊ |
315 | ␉␉}␊ |
316 | ␉␉␊ |
317 | ␉␉if (sane_size == 0) {␊ |
318 | ␉␉␉␊ |
319 | ␉␉␉// I Guess that if sane_size == 0 we've got a big problem here, ␊ |
320 | ␉␉␉// and it means that the memory map was not converted properly␊ |
321 | ␉␉␉stop("Unable to convert memory map into proper format\n");␊ |
322 | ␉␉}␊ |
323 | ␉␉␊ |
324 | #define MEG␉␉(1024*1024)␊ |
325 | ␉␉␊ |
326 | ␉␉/*␊ |
327 | ␉␉ * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory␊ |
328 | ␉␉ * not reported by EFI.␊ |
329 | ␉␉ */␊ |
330 | ␉␉␊ |
331 | ␉␉sane_size = (sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1));␊ |
332 | ␉␉bootArgs->PhysicalMemorySize = sane_size;␊ |
333 | ␉␉bootArgs->FSBFrequency = get_env(envFSBFreq);␊ |
334 | ␊ |
335 | ␉}␉␊ |
336 | ␊ |
337 | ␊ |
338 | ␉{␊ |
339 | ␉␉uint32_t size;␊ |
340 | ␉␉void *addr;␊ |
341 | ␉␉// Flatten device tree␊ |
342 | ␉␉DT__FlattenDeviceTree(0, &size);␊ |
343 | ␉␉addr = (void *)AllocateKernelMemory(size);␊ |
344 | ␉␉if (addr == 0) {␊ |
345 | ␉␉␉stop("Couldn't allocate device tree\n");␊ |
346 | ␉␉}␊ |
347 | ␉␉␊ |
348 | ␉␉DT__FlattenDeviceTree((void **)&addr, &size);␊ |
349 | ␉␉bootArgs->deviceTreeP = (uint32_t)addr;␊ |
350 | ␉␉bootArgs->deviceTreeLength = size;␊ |
351 | ␉} ␊ |
352 | ␊ |
353 | }␊ |
354 | |