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 "config.h"␊ |
30 | #include "libsaio.h"␊ |
31 | #include "boot.h"␊ |
32 | #include "bootstruct.h"␊ |
33 | ␊ |
34 | #if DEBUG_BOOTSTRUCT␊ |
35 | ␉#define DBG(x...)␉printf(x)␊ |
36 | #else␊ |
37 | ␉#define DBG(x...)␉msglog(x)␊ |
38 | #endif␊ |
39 | ␊ |
40 | #define MEG␉␉(1024*1024)␊ |
41 | ␊ |
42 | /*==========================================================================␊ |
43 | * Initialize the structure of parameters passed to␊ |
44 | * the kernel by the booter.␊ |
45 | */␊ |
46 | ␊ |
47 | boot_args␉␉*bootArgs␉= NULL;␊ |
48 | boot_args_legacy␉*bootArgsLegacy␉= NULL;␊ |
49 | ␊ |
50 | PrivateBootInfo_t␉*bootInfo␉= NULL;␊ |
51 | Node␉␉␉*gMemoryMapNode;␊ |
52 | ␊ |
53 | static char platformName[64];␊ |
54 | ␊ |
55 | void initKernBootStruct( void )␊ |
56 | {␊ |
57 | ␉Node *node;␊ |
58 | ␉int nameLen;␊ |
59 | ␊ |
60 | ␉static int init_done = 0;␊ |
61 | ␊ |
62 | ␉if ( !init_done )␊ |
63 | ␉{␊ |
64 | ␉␉bootArgs = (boot_args *)malloc(sizeof(boot_args));␊ |
65 | ␉␉bootArgsLegacy = (boot_args_legacy *)malloc(sizeof(boot_args_legacy));␊ |
66 | ␉␉bootInfo = (PrivateBootInfo_t *)malloc(sizeof(PrivateBootInfo_t));␊ |
67 | ␊ |
68 | ␉␉if (bootArgs == 0 || bootArgsLegacy == 0 || bootInfo == 0)␊ |
69 | ␉␉{␊ |
70 | ␉␉␉stop("Couldn't allocate boot info\n");␊ |
71 | ␉␉}␊ |
72 | ␊ |
73 | ␉␉bzero(bootArgs, sizeof(boot_args));␊ |
74 | ␉␉bzero(bootArgsLegacy, sizeof(boot_args_legacy));␊ |
75 | ␉␉bzero(bootInfo, sizeof(PrivateBootInfo_t));␊ |
76 | ␊ |
77 | ␉␉// Get system memory map. Also update the size of the␊ |
78 | ␉␉// conventional/extended memory for backwards compatibility.␊ |
79 | ␊ |
80 | ␉␉bootInfo->memoryMapCount =␊ |
81 | ␉␉␉getMemoryMap( bootInfo->memoryMap, kMemoryMapCountMax,␊ |
82 | ␉␉␉␉␉␉ (unsigned long *) &bootInfo->convmem,␊ |
83 | ␉␉␉␉␉␉ (unsigned long *) &bootInfo->extmem );␊ |
84 | ␊ |
85 | ␉␉if ( bootInfo->memoryMapCount == 0 )␊ |
86 | ␉␉{␊ |
87 | ␉␉␉// BIOS did not provide a memory map, systems with␊ |
88 | ␉␉␉// discontiguous memory or unusual memory hole locations␊ |
89 | ␉␉␉// may have problems.␊ |
90 | ␊ |
91 | ␉␉␉bootInfo->convmem␉= getConventionalMemorySize();␊ |
92 | ␉␉␉bootInfo->extmem␉= getExtendedMemorySize();␊ |
93 | ␉␉}␊ |
94 | ␊ |
95 | ␉␉bootInfo->configEnd␉␉= bootInfo->config;␊ |
96 | ␉␉bootArgs->Video.v_display␉= VGA_TEXT_MODE;␊ |
97 | ␊ |
98 | ␉␉DT__Initialize();␊ |
99 | ␊ |
100 | ␉␉node = DT__FindNode("/", true);␊ |
101 | ␉␉if (node == 0)␊ |
102 | ␉␉{␊ |
103 | ␉␉␉stop("Couldn't create root node");␊ |
104 | ␉␉}␊ |
105 | ␊ |
106 | ␉␉getPlatformName(platformName, sizeof(platformName));␊ |
107 | ␊ |
108 | ␉␉nameLen = strlen(platformName) + 1;␊ |
109 | ␉␉DT__AddProperty(node, "compatible", nameLen, platformName);␊ |
110 | ␉␉DT__AddProperty(node, "model", nameLen, platformName);␊ |
111 | ␊ |
112 | ␉␉gMemoryMapNode = DT__FindNode("/chosen/memory-map", true);␊ |
113 | ␊ |
114 | ␉␉bootArgs->Version = kBootArgsVersion;␊ |
115 | ␉␉bootArgs->Revision = kBootArgsRevision;␊ |
116 | ␊ |
117 | ␉␉bootArgsLegacy->Version = kBootArgsLegacyVersion;␊ |
118 | ␉␉bootArgsLegacy->Revision = kBootArgsLegacyRevision;␊ |
119 | ␊ |
120 | ␉␉init_done = 1;␊ |
121 | ␉}␊ |
122 | }␊ |
123 | ␊ |
124 | /* Copy boot args after kernel and record address. */␊ |
125 | ␊ |
126 | void reserveKernBootStruct(void)␊ |
127 | {␊ |
128 | ␉if ( MacOSVerCurrent < MacOSVer2Int("10.7") )␊ |
129 | ␉{␊ |
130 | ␉␉// for 10.4 10.5 10.6␊ |
131 | ␉␉void *oldAddr = bootArgsLegacy;␊ |
132 | ␉␉bootArgsLegacy = (boot_args_legacy *)AllocateKernelMemory(sizeof(boot_args_legacy));␊ |
133 | ␉␉bcopy(oldAddr, bootArgsLegacy, sizeof(boot_args_legacy));␊ |
134 | ␉}␊ |
135 | ␉else␊ |
136 | ␉{␊ |
137 | ␉␉// for 10.7 10.8 10.9 10.10 10.11 10.12 10.13 10.14␊ |
138 | ␉␉void *oldAddr = bootArgs;␊ |
139 | ␉␉bootArgs = (boot_args *)AllocateKernelMemory(sizeof(boot_args));␊ |
140 | ␉␉bcopy(oldAddr, bootArgs, sizeof(boot_args));␊ |
141 | ␉}␊ |
142 | ␊ |
143 | ␉// 10.12, 10.13 and 10.14 new bootArgs?␊ |
144 | }␊ |
145 | ␊ |
146 | //==============================================================================␊ |
147 | ␊ |
148 | void finalizeBootStruct(void)␊ |
149 | {␊ |
150 | ␉uint32_t size;␊ |
151 | ␉void *addr;␊ |
152 | ␉int i;␊ |
153 | ␊ |
154 | ␉/* Memory size to use for defaults calculations (cparm) */␊ |
155 | ␉uint64_t␉sane_size = 0;␊ |
156 | ␊ |
157 | ␉EfiMemoryRange␉*memoryMap␉= NULL;␊ |
158 | ␉MemoryRange␉*range␉␉= NULL;␊ |
159 | ␉int memoryMapCount = bootInfo->memoryMapCount;␊ |
160 | ␊ |
161 | ␉if (memoryMapCount == 0)␊ |
162 | ␉{␊ |
163 | ␉␉// XXX could make a two-part map here␊ |
164 | ␉␉stop("No memory map found!\n");␊ |
165 | ␉␉return;␊ |
166 | ␉}␊ |
167 | ␊ |
168 | ␉// convert memory map to boot_args memory map␊ |
169 | ␉memoryMap = (EfiMemoryRange *)AllocateKernelMemory(sizeof(EfiMemoryRange) * memoryMapCount);␊ |
170 | ␉if (memoryMap == NULL)␊ |
171 | ␉{␊ |
172 | ␉␉stop("Unable to allocate kernel space for the memory map!\n");␊ |
173 | ␉␉return;␊ |
174 | ␉}␊ |
175 | ␊ |
176 | ␉bootArgs->MemoryMap␉␉␉= (uint32_t)memoryMap;␊ |
177 | ␉bootArgs->MemoryMapSize␉␉␉= sizeof(EfiMemoryRange) * memoryMapCount;␊ |
178 | ␉bootArgs->MemoryMapDescriptorSize␉= sizeof(EfiMemoryRange);␊ |
179 | ␉bootArgs->MemoryMapDescriptorVersion␉= 0;␊ |
180 | ␊ |
181 | ␉for (i = 0; i < memoryMapCount; i++, memoryMap++)␊ |
182 | ␉{␊ |
183 | ␉␉range = &bootInfo->memoryMap[i];␊ |
184 | ␊ |
185 | ␉␉if (!range || !memoryMap)␊ |
186 | ␉␉{␊ |
187 | ␉␉␉stop("Error while computing kernel memory map\n");␊ |
188 | ␉␉␉return;␊ |
189 | ␉␉}␊ |
190 | ␊ |
191 | ␉␉switch(range->type)␊ |
192 | ␉␉{␊ |
193 | ␉␉␉case kMemoryRangeACPI:␊ |
194 | ␉␉␉␉memoryMap->Type = kEfiACPIReclaimMemory;␊ |
195 | ␉␉␉␉break;␊ |
196 | ␊ |
197 | ␉␉␉case kMemoryRangeNVS:␊ |
198 | ␉␉␉␉memoryMap->Type = kEfiACPIMemoryNVS;␊ |
199 | ␉␉␉␉break;␊ |
200 | ␊ |
201 | ␉␉␉case kMemoryRangeUsable:␊ |
202 | ␉␉␉␉memoryMap->Type = kEfiConventionalMemory;␊ |
203 | ␉␉␉␉break;␊ |
204 | ␊ |
205 | ␉␉␉case kMemoryRangeReserved:␊ |
206 | ␊ |
207 | ␉␉␉default:␊ |
208 | ␉␉␉␉memoryMap->Type = kEfiReservedMemoryType;␊ |
209 | ␉␉␉␉break;␊ |
210 | ␉␉}␊ |
211 | ␊ |
212 | ␉␉memoryMap->PhysicalStart␉= range->base;␊ |
213 | ␉␉memoryMap->VirtualStart␉␉= range->base;␊ |
214 | ␉␉memoryMap->NumberOfPages␉= range->length >> I386_PGSHIFT;␊ |
215 | ␉␉memoryMap->Attribute␉␉= 0;␊ |
216 | ␊ |
217 | ␉␉switch (memoryMap->Type)␊ |
218 | ␉␉{␊ |
219 | ␉␉␉case kEfiLoaderCode:␊ |
220 | ␉␉␉case kEfiLoaderData:␊ |
221 | ␉␉␉case kEfiBootServicesCode:␊ |
222 | ␉␉␉case kEfiBootServicesData:␊ |
223 | ␉␉␉case kEfiConventionalMemory:␊ |
224 | ␉␉␉␉/*␊ |
225 | ␉␉␉␉ * Consolidate usable memory types into one (cparm).␊ |
226 | ␉␉␉␉ */␊ |
227 | ␉␉␉␉sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);␊ |
228 | ␉␉␉␉break;␊ |
229 | ␊ |
230 | ␉␉␉case kEfiRuntimeServicesCode:␊ |
231 | ␉␉␉case kEfiRuntimeServicesData:␊ |
232 | ␉␉␉case kEfiACPIReclaimMemory:␊ |
233 | ␉␉␉case kEfiACPIMemoryNVS:␊ |
234 | ␉␉␉case kEfiPalCode:␊ |
235 | ␉␉␉␉/*␊ |
236 | ␉␉␉␉ * sane_size should reflect the total amount of physical ram␊ |
237 | ␉␉␉␉ * in the system, not just the amount that is available for␊ |
238 | ␉␉␉␉ * the OS to use (cparm)␊ |
239 | ␉␉␉␉ */␊ |
240 | ␉␉␉␉sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);␊ |
241 | ␉␉␉␉break;␊ |
242 | ␉␉␉default:␊ |
243 | ␉␉␉␉break;␊ |
244 | ␉␉}␊ |
245 | ␊ |
246 | ␉␉if (sane_size == 0)␊ |
247 | ␉␉{␊ |
248 | ␊ |
249 | ␉␉␉// I Guess that if sane_size == 0 we've got a big problem here,␊ |
250 | ␉␉␉// and it means that the memory map was not converted properly (cparm)␊ |
251 | ␉␉␉stop("Unable to convert memory map into proper format\n");␊ |
252 | ␉␉␉return;␊ |
253 | ␉␉}␊ |
254 | ␊ |
255 | ␉␉/*␊ |
256 | ␉␉ * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory␊ |
257 | ␉␉ * not reported by EFI. (cparm)␊ |
258 | ␉␉ */␊ |
259 | #if DEBUG_BOOTSTRUCT␊ |
260 | ␉␉sane_size = (sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1));␊ |
261 | ␉␉DBG( "Total amount of physical RAM in the system : %d\n", ((sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1))) );␊ |
262 | #else␊ |
263 | ␉␉DBG( "Total amount of RAM in the system : %d\n", sane_size );␊ |
264 | #endif␊ |
265 | ␉␉bootArgs->PhysicalMemorySize = sane_size;␊ |
266 | ␉}␊ |
267 | ␊ |
268 | ␉// copy bootFile into device tree␊ |
269 | ␉// XXX␊ |
270 | ␊ |
271 | ␉// add PCI info somehow into device tree␊ |
272 | ␉// XXX␊ |
273 | ␊ |
274 | ␉// Flatten device tree␊ |
275 | ␉DT__FlattenDeviceTree(0, &size);␊ |
276 | ␉addr = (void *)AllocateKernelMemory(size);␊ |
277 | ␉if (addr == 0)␊ |
278 | ␉{␊ |
279 | ␉␉stop("Couldn't allocate device tree\n");␊ |
280 | ␉␉return;␊ |
281 | ␉}␊ |
282 | ␊ |
283 | ␉DT__FlattenDeviceTree((void **)&addr, &size);␊ |
284 | ␉if (!size)␊ |
285 | ␉{␊ |
286 | ␉␉stop("Couldn't get flatten device tree\n");␊ |
287 | ␉␉return;␊ |
288 | ␉}␊ |
289 | ␉bootArgs->deviceTreeP = (uint32_t)addr;␊ |
290 | ␉bootArgs->deviceTreeLength = size;␊ |
291 | ␊ |
292 | ␉//␊ |
293 | ␉// MinusZwei:␊ |
294 | ␉// Dump the new video struct fields in the old one.␊ |
295 | ␉// This will avoid a black screen issue with OS X between 10.8 and 10.11␊ |
296 | ␉//␊ |
297 | ␉// In order to be 100% safe, the new struct fields will also be seroed␊ |
298 | ␉// afterwards.␊ |
299 | ␉//␊ |
300 | ␉if ( MacOSVerCurrent < MacOSVer2Int("10.12") )␊ |
301 | ␉{␊ |
302 | ␉␉bootArgs->VideoV1.v_baseAddr = bootArgs->Video.v_baseAddr;␊ |
303 | ␉␉bootArgs->VideoV1.v_display = bootArgs->Video.v_display;␊ |
304 | ␉␉bootArgs->VideoV1.v_rowBytes = bootArgs->Video.v_rowBytes;␊ |
305 | ␉␉bootArgs->VideoV1.v_width = bootArgs->Video.v_width;␊ |
306 | ␉␉bootArgs->VideoV1.v_height = bootArgs->Video.v_height;␊ |
307 | ␉␉bootArgs->VideoV1.v_depth = bootArgs->Video.v_depth;␊ |
308 | ␊ |
309 | ␉␉bzero(&bootArgs->Video, sizeof(Boot_Video));␊ |
310 | ␊ |
311 | ␉␉bootArgs->apfsDataStart = 0;␊ |
312 | ␉␉bootArgs->apfsDataSize = 0;␊ |
313 | ␉}␊ |
314 | ␊ |
315 | ␉// Copy BootArgs values to older structure␊ |
316 | ␊ |
317 | ␉memcpy(&bootArgsLegacy->CommandLine, &bootArgs->CommandLine, BOOT_LINE_LENGTH);␊ |
318 | ␉memcpy(&bootArgsLegacy->Video, &bootArgs->VideoV1, sizeof(Boot_VideoV1));␊ |
319 | ␊ |
320 | ␉bootArgsLegacy->MemoryMap = bootArgs->MemoryMap;␊ |
321 | ␉bootArgsLegacy->MemoryMapSize = bootArgs->MemoryMapSize;␊ |
322 | ␉bootArgsLegacy->MemoryMapDescriptorSize = bootArgs->MemoryMapDescriptorSize;␊ |
323 | ␉bootArgsLegacy->MemoryMapDescriptorVersion = bootArgs->MemoryMapDescriptorVersion;␊ |
324 | ␊ |
325 | ␉bootArgsLegacy->deviceTreeP = bootArgs->deviceTreeP;␊ |
326 | ␉bootArgsLegacy->deviceTreeLength = bootArgs->deviceTreeLength;␊ |
327 | ␊ |
328 | ␉bootArgsLegacy->kaddr = bootArgs->kaddr;␊ |
329 | ␉bootArgsLegacy->ksize = bootArgs->ksize;␊ |
330 | ␊ |
331 | ␉bootArgsLegacy->efiRuntimeServicesPageStart = bootArgs->efiRuntimeServicesPageStart;␊ |
332 | ␉bootArgsLegacy->efiRuntimeServicesPageCount = bootArgs->efiRuntimeServicesPageCount;␊ |
333 | ␉bootArgsLegacy->efiSystemTable = bootArgs->efiSystemTable;␊ |
334 | ␊ |
335 | ␉bootArgsLegacy->efiMode = bootArgs->efiMode;␊ |
336 | ␊ |
337 | ␉bootArgsLegacy->performanceDataStart = bootArgs->performanceDataStart;␊ |
338 | ␉bootArgsLegacy->performanceDataSize = bootArgs->performanceDataSize;␊ |
339 | ␉bootArgsLegacy->efiRuntimeServicesVirtualPageStart = bootArgs->efiRuntimeServicesVirtualPageStart;␊ |
340 | }␊ |
341 | |