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

Archive Download this file

Revision: 2877