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