Chameleon

Chameleon Svn Source Tree

Root/branches/zenith432/i386/libsaio/bootstruct.c

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
50boot_args*bootArgs= NULL;
51boot_args_legacy*bootArgsLegacy= NULL;
52
53PrivateBootInfo_t*bootInfo= NULL;
54Node*gMemoryMapNode;
55
56static char platformName[64];
57
58void initKernBootStruct( void )
59{
60Node *node;
61int nameLen;
62
63static int init_done = 0;
64
65if ( !init_done )
66{
67bootArgs = (boot_args *)malloc(sizeof(boot_args));
68bootArgsLegacy = (boot_args_legacy *)malloc(sizeof(boot_args_legacy));
69bootInfo = (PrivateBootInfo_t *)malloc(sizeof(PrivateBootInfo_t));
70
71if (bootArgs == 0 || bootArgsLegacy == 0 || bootInfo == 0)
72{
73stop("Couldn't allocate boot info\n");
74}
75
76bzero(bootArgs, sizeof(boot_args));
77bzero(bootArgsLegacy, sizeof(boot_args_legacy));
78bzero(bootInfo, sizeof(PrivateBootInfo_t));
79
80// Get system memory map. Also update the size of the
81// conventional/extended memory for backwards compatibility.
82
83bootInfo->memoryMapCount =
84getMemoryMap( bootInfo->memoryMap, kMemoryMapCountMax,
85 (unsigned long *) &bootInfo->convmem,
86 (unsigned long *) &bootInfo->extmem );
87
88if ( 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
94bootInfo->convmem= getConventionalMemorySize();
95bootInfo->extmem= getExtendedMemorySize();
96}
97
98bootInfo->configEnd= bootInfo->config;
99bootArgs->Video.v_display= VGA_TEXT_MODE;
100
101DT__Initialize();
102
103node = DT__FindNode("/", true);
104if (node == 0)
105{
106stop("Couldn't create root node");
107}
108
109getPlatformName(platformName, sizeof(platformName));
110
111nameLen = strlen(platformName) + 1;
112DT__AddProperty(node, "compatible", nameLen, platformName);
113DT__AddProperty(node, "model", nameLen, platformName);
114
115gMemoryMapNode = DT__FindNode("/chosen/memory-map", true);
116
117bootArgs->Version = kBootArgsVersion;
118bootArgs->Revision = kBootArgsRevision;
119
120bootArgsLegacy->Version = kBootArgsLegacyVersion;
121bootArgsLegacy->Revision = kBootArgsLegacyRevision;
122
123init_done = 1;
124}
125}
126
127/* Copy boot args after kernel and record address. */
128
129void reserveKernBootStruct(void)
130{
131if ( MacOSVerCurrent < MacOSVer2Int("10.7") )
132{
133// for 10.4 10.5 10.6
134void *oldAddr = bootArgsLegacy;
135bootArgsLegacy = (boot_args_legacy *)AllocateKernelMemory(sizeof(boot_args_legacy));
136bcopy(oldAddr, bootArgsLegacy, sizeof(boot_args_legacy));
137}
138else
139{
140// for 10.7 10.8 10.9 10.10 10.11
141void *oldAddr = bootArgs;
142bootArgs = (boot_args *)AllocateKernelMemory(sizeof(boot_args));
143bcopy(oldAddr, bootArgs, sizeof(boot_args));
144}
145}
146
147//==============================================================================
148
149void finalizeBootStruct(void)
150{
151uint32_t size;
152void *addr;
153int i;
154
155/* Memory size to use for defaults calculations (cparm) */
156uint64_tsane_size = 0;
157
158EfiMemoryRange *memoryMap= NULL;
159MemoryRange *range= NULL;
160int memoryMapCount = bootInfo->memoryMapCount;
161
162if (memoryMapCount == 0)
163{
164// XXX could make a two-part map here
165stop("No memory map found!\n");
166return;
167}
168
169// convert memory map to boot_args memory map
170memoryMap = (EfiMemoryRange *)AllocateKernelMemory(sizeof(EfiMemoryRange) * memoryMapCount);
171if (memoryMap == NULL)
172{
173stop("Unable to allocate kernel space for the memory map!\n");
174return;
175}
176
177bootArgs->MemoryMap= (uint32_t)memoryMap;
178bootArgs->MemoryMapSize= sizeof(EfiMemoryRange) * memoryMapCount;
179bootArgs->MemoryMapDescriptorSize= sizeof(EfiMemoryRange);
180bootArgs->MemoryMapDescriptorVersion= 0;
181
182for (i = 0; i < memoryMapCount; i++, memoryMap++)
183{
184range = &bootInfo->memoryMap[i];
185
186if (!range || !memoryMap)
187{
188stop("Error while computing kernel memory map\n");
189return;
190}
191
192switch(range->type)
193{
194case kMemoryRangeACPI:
195memoryMap->Type = kEfiACPIReclaimMemory;
196break;
197
198case kMemoryRangeNVS:
199memoryMap->Type = kEfiACPIMemoryNVS;
200break;
201
202case kMemoryRangeUsable:
203memoryMap->Type = kEfiConventionalMemory;
204break;
205
206case kMemoryRangeReserved:
207
208default:
209memoryMap->Type = kEfiReservedMemoryType;
210break;
211}
212
213memoryMap->PhysicalStart= range->base;
214memoryMap->VirtualStart= range->base;
215memoryMap->NumberOfPages= range->length >> I386_PGSHIFT;
216memoryMap->Attribute= 0;
217
218switch (memoryMap->Type)
219{
220case kEfiLoaderCode:
221case kEfiLoaderData:
222case kEfiBootServicesCode:
223case kEfiBootServicesData:
224case kEfiConventionalMemory:
225/*
226 * Consolidate usable memory types into one (cparm).
227 */
228sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);
229break;
230
231case kEfiRuntimeServicesCode:
232case kEfiRuntimeServicesData:
233case kEfiACPIReclaimMemory:
234case kEfiACPIMemoryNVS:
235case 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 */
241sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);
242break;
243default:
244break;
245}
246
247if (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)
252stop("Unable to convert memory map into proper format\n");
253return;
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
261sane_size = (sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1));
262bootArgs->PhysicalMemorySize = sane_size;
263}
264
265// copy bootFile into device tree
266// XXX
267
268// add PCI info somehow into device tree
269// XXX
270
271// Flatten device tree
272DT__FlattenDeviceTree(0, &size);
273addr = (void *)AllocateKernelMemory(size);
274if (addr == 0)
275{
276stop("Couldn't allocate device tree\n");
277return;
278}
279
280DT__FlattenDeviceTree((void **)&addr, &size);
281if (!size)
282{
283stop("Couldn't get flatten device tree\n");
284return;
285}
286bootArgs->deviceTreeP = (uint32_t)addr;
287bootArgs->deviceTreeLength = size;
288
289// Copy BootArgs values to older structure
290
291memcpy(&bootArgsLegacy->CommandLine, &bootArgs->CommandLine, BOOT_LINE_LENGTH);
292memcpy(&bootArgsLegacy->Video, &bootArgs->Video, sizeof(Boot_Video));
293
294bootArgsLegacy->MemoryMap = bootArgs->MemoryMap;
295bootArgsLegacy->MemoryMapSize = bootArgs->MemoryMapSize;
296bootArgsLegacy->MemoryMapDescriptorSize = bootArgs->MemoryMapDescriptorSize;
297bootArgsLegacy->MemoryMapDescriptorVersion = bootArgs->MemoryMapDescriptorVersion;
298
299bootArgsLegacy->deviceTreeP = bootArgs->deviceTreeP;
300bootArgsLegacy->deviceTreeLength = bootArgs->deviceTreeLength;
301
302bootArgsLegacy->kaddr = bootArgs->kaddr;
303bootArgsLegacy->ksize = bootArgs->ksize;
304
305bootArgsLegacy->efiRuntimeServicesPageStart = bootArgs->efiRuntimeServicesPageStart;
306bootArgsLegacy->efiRuntimeServicesPageCount = bootArgs->efiRuntimeServicesPageCount;
307bootArgsLegacy->efiSystemTable = bootArgs->efiSystemTable;
308
309bootArgsLegacy->efiMode = bootArgs->efiMode;
310
311bootArgsLegacy->performanceDataStart = bootArgs->performanceDataStart;
312bootArgsLegacy->performanceDataSize = bootArgs->performanceDataSize;
313bootArgsLegacy->efiRuntimeServicesVirtualPageStart = bootArgs->efiRuntimeServicesVirtualPageStart;
314}
315

Archive Download this file

Revision: 2806