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