]> git.saurik.com Git - apple/bootx.git/blame_incremental - bootx.tproj/sl.subproj/elf.c
BootX-45.tar.gz
[apple/bootx.git] / bootx.tproj / sl.subproj / elf.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * elf.c - A function to decode a PPC Linux Kernel.
24 *
25 * Copyright (c) 2000 Apple Computer, Inc.
26 *
27 * DRI: Josh de Cesare
28 */
29
30#include <sl.h>
31
32#include "elf.h"
33
34// Public Functions
35
36long DecodeElf(void)
37{
38 ElfHeaderPtr ehPtr;
39 ProgramHeaderPtr phPtr;
40 long cnt, paddr, offset, memsz, filesz, entry, *tmp;
41
42 ehPtr = (ElfHeaderPtr)kLoadAddr;
43 if (ehPtr->signature != kElfSignature) return 0;
44
45 entry = ehPtr->entry & kElfAddressMask;
46
47 for (cnt = 0; cnt < ehPtr->phnum; cnt++) {
48 phPtr = (ProgramHeaderPtr)(kLoadAddr+ehPtr->phoff+cnt*ehPtr->phentsize);
49
50 if (phPtr->type == kElfProgramTypeLoad) {
51 paddr = phPtr->paddr & kElfAddressMask;
52 offset = phPtr->offset;
53 filesz = phPtr->filesz;
54 memsz = phPtr->memsz;
55
56 // Get the actual entry if it is in this program.
57 if ((entry >= paddr) && (entry < (paddr + filesz))) {
58 tmp = (long *)(kLoadAddr + offset + entry);
59 if (tmp[2] == 0) entry += tmp[0];
60
61 }
62 entry += paddr;
63
64 // Add the kernel to the memory-map.
65 AllocateMemoryRange("Kernel-PROGRAM", 0, memsz);
66
67 // Set the last address used by the kernel program.
68 AllocateKernelMemory(paddr + memsz);
69
70 if (paddr < kImageAddr) {
71 // Copy the Vectors out of the way.
72 bcopy((char *)(kLoadAddr + offset), gVectorSaveAddr,
73 kVectorSize - paddr);
74
75 offset += kImageAddr - paddr;
76 filesz -= kImageAddr - paddr;
77 paddr = kImageAddr;
78 }
79
80 // Move the program.
81 bcopy((char *)(kLoadAddr + offset), (char *)paddr, filesz);
82 }
83 }
84
85 return 0;
86}