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