Root/
Source at commit HEAD created 5 years 6 days ago. By ifabio, Few update to kernelPatcher (Credits to CrazyBirdy) | |
---|---|
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␊ |
138 | ␉␉void *oldAddr = bootArgs;␊ |
139 | ␉␉bootArgs = (boot_args *)AllocateKernelMemory(sizeof(boot_args));␊ |
140 | ␉␉bcopy(oldAddr, bootArgs, sizeof(boot_args));␊ |
141 | ␉}␊ |
142 | }␊ |
143 | ␊ |
144 | //==============================================================================␊ |
145 | ␊ |
146 | void finalizeBootStruct(void)␊ |
147 | {␊ |
148 | ␉uint32_t size;␊ |
149 | ␉void *addr;␊ |
150 | ␉int i;␊ |
151 | ␊ |
152 | ␉/* Memory size to use for defaults calculations (cparm) */␊ |
153 | ␉uint64_t␉sane_size = 0;␊ |
154 | ␊ |
155 | ␉EfiMemoryRange␉*memoryMap␉= NULL;␊ |
156 | ␉MemoryRange␉*range␉␉= NULL;␊ |
157 | ␉int memoryMapCount = bootInfo->memoryMapCount;␊ |
158 | ␊ |
159 | ␉if (memoryMapCount == 0)␊ |
160 | ␉{␊ |
161 | ␉␉// XXX could make a two-part map here␊ |
162 | ␉␉stop("No memory map found!\n");␊ |
163 | ␉␉return;␊ |
164 | ␉}␊ |
165 | ␊ |
166 | ␉// convert memory map to boot_args memory map␊ |
167 | ␉memoryMap = (EfiMemoryRange *)AllocateKernelMemory(sizeof(EfiMemoryRange) * memoryMapCount);␊ |
168 | ␉if (memoryMap == NULL)␊ |
169 | ␉{␊ |
170 | ␉␉stop("Unable to allocate kernel space for the memory map!\n");␊ |
171 | ␉␉return;␊ |
172 | ␉}␊ |
173 | ␊ |
174 | ␉bootArgs->MemoryMap␉␉␉= (uint32_t)memoryMap;␊ |
175 | ␉bootArgs->MemoryMapSize␉␉␉= sizeof(EfiMemoryRange) * memoryMapCount;␊ |
176 | ␉bootArgs->MemoryMapDescriptorSize␉= sizeof(EfiMemoryRange);␊ |
177 | ␉bootArgs->MemoryMapDescriptorVersion␉= 0;␊ |
178 | ␊ |
179 | ␉for (i = 0; i < memoryMapCount; i++, memoryMap++)␊ |
180 | ␉{␊ |
181 | ␉␉range = &bootInfo->memoryMap[i];␊ |
182 | ␊ |
183 | ␉␉if (!range || !memoryMap)␊ |
184 | ␉␉{␊ |
185 | ␉␉␉stop("Error while computing kernel memory map\n");␊ |
186 | ␉␉␉return;␊ |
187 | ␉␉}␊ |
188 | ␊ |
189 | ␉␉switch(range->type)␊ |
190 | ␉␉{␊ |
191 | ␉␉␉case kMemoryRangeACPI:␊ |
192 | ␉␉␉␉memoryMap->Type = kEfiACPIReclaimMemory;␊ |
193 | ␉␉␉␉break;␊ |
194 | ␊ |
195 | ␉␉␉case kMemoryRangeNVS:␊ |
196 | ␉␉␉␉memoryMap->Type = kEfiACPIMemoryNVS;␊ |
197 | ␉␉␉␉break;␊ |
198 | ␊ |
199 | ␉␉␉case kMemoryRangeUsable:␊ |
200 | ␉␉␉␉memoryMap->Type = kEfiConventionalMemory;␊ |
201 | ␉␉␉␉break;␊ |
202 | ␊ |
203 | ␉␉␉case kMemoryRangeReserved:␊ |
204 | ␊ |
205 | ␉␉␉default:␊ |
206 | ␉␉␉␉memoryMap->Type = kEfiReservedMemoryType;␊ |
207 | ␉␉␉␉break;␊ |
208 | ␉␉}␊ |
209 | ␊ |
210 | ␉␉memoryMap->PhysicalStart␉= range->base;␊ |
211 | ␉␉memoryMap->VirtualStart␉␉= range->base;␊ |
212 | ␉␉memoryMap->NumberOfPages␉= range->length >> I386_PGSHIFT;␊ |
213 | ␉␉memoryMap->Attribute␉␉= 0;␊ |
214 | ␊ |
215 | ␉␉switch (memoryMap->Type)␊ |
216 | ␉␉{␊ |
217 | ␉␉␉case kEfiLoaderCode:␊ |
218 | ␉␉␉case kEfiLoaderData:␊ |
219 | ␉␉␉case kEfiBootServicesCode:␊ |
220 | ␉␉␉case kEfiBootServicesData:␊ |
221 | ␉␉␉case kEfiConventionalMemory:␊ |
222 | ␉␉␉␉/*␊ |
223 | ␉␉␉␉ * Consolidate usable memory types into one (cparm).␊ |
224 | ␉␉␉␉ */␊ |
225 | ␉␉␉␉sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);␊ |
226 | ␉␉␉␉break;␊ |
227 | ␊ |
228 | ␉␉␉case kEfiRuntimeServicesCode:␊ |
229 | ␉␉␉case kEfiRuntimeServicesData:␊ |
230 | ␉␉␉case kEfiACPIReclaimMemory:␊ |
231 | ␉␉␉case kEfiACPIMemoryNVS:␊ |
232 | ␉␉␉case kEfiPalCode:␊ |
233 | ␉␉␉␉/*␊ |
234 | ␉␉␉␉ * sane_size should reflect the total amount of physical ram␊ |
235 | ␉␉␉␉ * in the system, not just the amount that is available for␊ |
236 | ␉␉␉␉ * the OS to use (cparm)␊ |
237 | ␉␉␉␉ */␊ |
238 | ␉␉␉␉sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);␊ |
239 | ␉␉␉␉break;␊ |
240 | ␉␉␉default:␊ |
241 | ␉␉␉␉break;␊ |
242 | ␉␉}␊ |
243 | ␊ |
244 | ␉␉if (sane_size == 0)␊ |
245 | ␉␉{␊ |
246 | ␊ |
247 | ␉␉␉// I Guess that if sane_size == 0 we've got a big problem here,␊ |
248 | ␉␉␉// and it means that the memory map was not converted properly (cparm)␊ |
249 | ␉␉␉stop("Unable to convert memory map into proper format\n");␊ |
250 | ␉␉␉return;␊ |
251 | ␉␉}␊ |
252 | ␊ |
253 | ␉␉/*␊ |
254 | ␉␉ * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory␊ |
255 | ␉␉ * not reported by EFI. (cparm)␊ |
256 | ␉␉ */␊ |
257 | #if DEBUG_BOOTSTRUCT␊ |
258 | ␉␉sane_size = (sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1));␊ |
259 | ␉␉DBG( "Total amount of physical RAM in the system : %d\n", ((sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1))) );␊ |
260 | #else␊ |
261 | ␉␉DBG( "Total amount of RAM in the system : %d\n", sane_size );␊ |
262 | #endif␊ |
263 | ␉␉bootArgs->PhysicalMemorySize = sane_size;␊ |
264 | ␉}␊ |
265 | ␊ |
266 | ␉// copy bootFile into device tree␊ |
267 | ␉// XXX␊ |
268 | ␊ |
269 | ␉// add PCI info somehow into device tree␊ |
270 | ␉// XXX␊ |
271 | ␊ |
272 | ␉// Flatten device tree␊ |
273 | ␉DT__FlattenDeviceTree(0, &size);␊ |
274 | ␉addr = (void *)AllocateKernelMemory(size);␊ |
275 | ␉if (addr == 0)␊ |
276 | ␉{␊ |
277 | ␉␉stop("Couldn't allocate device tree\n");␊ |
278 | ␉␉return;␊ |
279 | ␉}␊ |
280 | ␊ |
281 | ␉DT__FlattenDeviceTree((void **)&addr, &size);␊ |
282 | ␉if (!size)␊ |
283 | ␉{␊ |
284 | ␉␉stop("Couldn't get flatten device tree\n");␊ |
285 | ␉␉return;␊ |
286 | ␉}␊ |
287 | ␉bootArgs->deviceTreeP = (uint32_t)addr;␊ |
288 | ␉bootArgs->deviceTreeLength = size;␊ |
289 | ␊ |
290 | ␉// Copy BootArgs values to older structure␊ |
291 | ␊ |
292 | ␉memcpy(&bootArgsLegacy->CommandLine, &bootArgs->CommandLine, BOOT_LINE_LENGTH);␊ |
293 | ␉memcpy(&bootArgsLegacy->Video, &bootArgs->Video, sizeof(Boot_Video));␊ |
294 | ␊ |
295 | ␉bootArgsLegacy->MemoryMap = bootArgs->MemoryMap;␊ |
296 | ␉bootArgsLegacy->MemoryMapSize = bootArgs->MemoryMapSize;␊ |
297 | ␉bootArgsLegacy->MemoryMapDescriptorSize = bootArgs->MemoryMapDescriptorSize;␊ |
298 | ␉bootArgsLegacy->MemoryMapDescriptorVersion = bootArgs->MemoryMapDescriptorVersion;␊ |
299 | ␊ |
300 | ␉bootArgsLegacy->deviceTreeP = bootArgs->deviceTreeP;␊ |
301 | ␉bootArgsLegacy->deviceTreeLength = bootArgs->deviceTreeLength;␊ |
302 | ␊ |
303 | ␉bootArgsLegacy->kaddr = bootArgs->kaddr;␊ |
304 | ␉bootArgsLegacy->ksize = bootArgs->ksize;␊ |
305 | ␊ |
306 | ␉bootArgsLegacy->efiRuntimeServicesPageStart = bootArgs->efiRuntimeServicesPageStart;␊ |
307 | ␉bootArgsLegacy->efiRuntimeServicesPageCount = bootArgs->efiRuntimeServicesPageCount;␊ |
308 | ␉bootArgsLegacy->efiSystemTable = bootArgs->efiSystemTable;␊ |
309 | ␊ |
310 | ␉bootArgsLegacy->efiMode = bootArgs->efiMode;␊ |
311 | ␊ |
312 | ␉bootArgsLegacy->performanceDataStart = bootArgs->performanceDataStart;␊ |
313 | ␉bootArgsLegacy->performanceDataSize = bootArgs->performanceDataSize;␊ |
314 | ␉bootArgsLegacy->efiRuntimeServicesVirtualPageStart = bootArgs->efiRuntimeServicesVirtualPageStart;␊ |
315 | }␊ |
316 |