Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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 10.12
138void *oldAddr = bootArgs;
139bootArgs = (boot_args *)AllocateKernelMemory(sizeof(boot_args));
140bcopy(oldAddr, bootArgs, sizeof(boot_args));
141}
142
143}
144
145//==============================================================================
146
147void finalizeBootStruct(void)
148{
149uint32_t size;
150void *addr;
151int i;
152
153/* Memory size to use for defaults calculations (cparm) */
154uint64_tsane_size = 0;
155
156EfiMemoryRange*memoryMap= NULL;
157MemoryRange*range= NULL;
158int memoryMapCount = bootInfo->memoryMapCount;
159
160if (memoryMapCount == 0)
161{
162// XXX could make a two-part map here
163stop("No memory map found!\n");
164return;
165}
166
167// convert memory map to boot_args memory map
168memoryMap = (EfiMemoryRange *)AllocateKernelMemory(sizeof(EfiMemoryRange) * memoryMapCount);
169if (memoryMap == NULL)
170{
171stop("Unable to allocate kernel space for the memory map!\n");
172return;
173}
174
175bootArgs->MemoryMap= (uint32_t)memoryMap;
176bootArgs->MemoryMapSize= sizeof(EfiMemoryRange) * memoryMapCount;
177bootArgs->MemoryMapDescriptorSize= sizeof(EfiMemoryRange);
178bootArgs->MemoryMapDescriptorVersion= 0;
179
180for (i = 0; i < memoryMapCount; i++, memoryMap++)
181{
182range = &bootInfo->memoryMap[i];
183
184if (!range || !memoryMap)
185{
186stop("Error while computing kernel memory map\n");
187return;
188}
189
190switch(range->type)
191{
192case kMemoryRangeACPI:
193memoryMap->Type = kEfiACPIReclaimMemory;
194break;
195
196case kMemoryRangeNVS:
197memoryMap->Type = kEfiACPIMemoryNVS;
198break;
199
200case kMemoryRangeUsable:
201memoryMap->Type = kEfiConventionalMemory;
202break;
203
204case kMemoryRangeReserved:
205
206default:
207memoryMap->Type = kEfiReservedMemoryType;
208break;
209}
210
211memoryMap->PhysicalStart= range->base;
212memoryMap->VirtualStart= range->base;
213memoryMap->NumberOfPages= range->length >> I386_PGSHIFT;
214memoryMap->Attribute= 0;
215
216switch (memoryMap->Type)
217{
218case kEfiLoaderCode:
219case kEfiLoaderData:
220case kEfiBootServicesCode:
221case kEfiBootServicesData:
222case kEfiConventionalMemory:
223/*
224 * Consolidate usable memory types into one (cparm).
225 */
226sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);
227break;
228
229case kEfiRuntimeServicesCode:
230case kEfiRuntimeServicesData:
231case kEfiACPIReclaimMemory:
232case kEfiACPIMemoryNVS:
233case kEfiPalCode:
234/*
235 * sane_size should reflect the total amount of physical ram
236 * in the system, not just the amount that is available for
237 * the OS to use (cparm)
238 */
239sane_size += (uint64_t)(memoryMap->NumberOfPages << I386_PGSHIFT);
240break;
241default:
242break;
243}
244
245if (sane_size == 0)
246{
247
248// I Guess that if sane_size == 0 we've got a big problem here,
249// and it means that the memory map was not converted properly (cparm)
250stop("Unable to convert memory map into proper format\n");
251return;
252}
253
254/*
255 * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory
256 * not reported by EFI. (cparm)
257 */
258#if DEBUG_BOOTSTRUCT
259sane_size = (sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1));
260DBG( "Total amount of physical RAM in the system : %d\n", ((sane_size + 128 * MEG - 1) & ~((uint64_t)(128 * MEG - 1))) );
261#else
262DBG( "Total amount of RAM in the system : %d\n", sane_size );
263#endif
264bootArgs->PhysicalMemorySize = sane_size;
265}
266
267// copy bootFile into device tree
268// XXX
269
270// add PCI info somehow into device tree
271// XXX
272
273// Flatten device tree
274DT__FlattenDeviceTree(0, &size);
275addr = (void *)AllocateKernelMemory(size);
276if (addr == 0)
277{
278stop("Couldn't allocate device tree\n");
279return;
280}
281
282DT__FlattenDeviceTree((void **)&addr, &size);
283if (!size)
284{
285stop("Couldn't get flatten device tree\n");
286return;
287}
288bootArgs->deviceTreeP = (uint32_t)addr;
289bootArgs->deviceTreeLength = size;
290
291// Copy BootArgs values to older structure
292
293memcpy(&bootArgsLegacy->CommandLine, &bootArgs->CommandLine, BOOT_LINE_LENGTH);
294memcpy(&bootArgsLegacy->Video, &bootArgs->Video, sizeof(Boot_Video));
295
296bootArgsLegacy->MemoryMap = bootArgs->MemoryMap;
297bootArgsLegacy->MemoryMapSize = bootArgs->MemoryMapSize;
298bootArgsLegacy->MemoryMapDescriptorSize = bootArgs->MemoryMapDescriptorSize;
299bootArgsLegacy->MemoryMapDescriptorVersion = bootArgs->MemoryMapDescriptorVersion;
300
301bootArgsLegacy->deviceTreeP = bootArgs->deviceTreeP;
302bootArgsLegacy->deviceTreeLength = bootArgs->deviceTreeLength;
303
304bootArgsLegacy->kaddr = bootArgs->kaddr;
305bootArgsLegacy->ksize = bootArgs->ksize;
306
307bootArgsLegacy->efiRuntimeServicesPageStart = bootArgs->efiRuntimeServicesPageStart;
308bootArgsLegacy->efiRuntimeServicesPageCount = bootArgs->efiRuntimeServicesPageCount;
309bootArgsLegacy->efiSystemTable = bootArgs->efiSystemTable;
310
311bootArgsLegacy->efiMode = bootArgs->efiMode;
312
313bootArgsLegacy->performanceDataStart = bootArgs->performanceDataStart;
314bootArgsLegacy->performanceDataSize = bootArgs->performanceDataSize;
315bootArgsLegacy->efiRuntimeServicesVirtualPageStart = bootArgs->efiRuntimeServicesVirtualPageStart;
316}
317

Archive Download this file

Revision: 2879