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