]> git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/sl.subproj/elf.c
BootX-81.tar.gz
[apple/bootx.git] / bootx.tproj / sl.subproj / elf.c
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
36 long ThinFatBinaryElf(void **binary, unsigned long *length)
37 {
38 return -1;
39 }
40
41 long DecodeElf(void *binary)
42 {
43 ElfHeaderPtr ehPtr;
44 ProgramHeaderPtr phPtr;
45 long cnt, paddr, offset, memsz, filesz, entry, *tmp;
46
47 ehPtr = (ElfHeaderPtr)binary;
48 if (ehPtr->signature != kElfSignature) return 0;
49
50 entry = ehPtr->entry & kElfAddressMask;
51
52 for (cnt = 0; cnt < ehPtr->phnum; cnt++) {
53 phPtr = (ProgramHeaderPtr)((unsigned long)binary + ehPtr->phoff + cnt * ehPtr->phentsize);
54
55 if (phPtr->type == kElfProgramTypeLoad) {
56 paddr = phPtr->paddr & kElfAddressMask;
57 offset = phPtr->offset;
58 filesz = phPtr->filesz;
59 memsz = phPtr->memsz;
60
61 // Get the actual entry if it is in this program.
62 if ((entry >= paddr) && (entry < (paddr + filesz))) {
63 tmp = (long *)((unsigned long)binary + offset + entry);
64 if (tmp[2] == 0) entry += tmp[0];
65
66 }
67 entry += paddr;
68
69 // Add the kernel to the memory-map.
70 AllocateMemoryRange("Kernel-PROGRAM", 0, memsz);
71
72 // Set the last address used by the kernel program.
73 AllocateKernelMemory(paddr + memsz);
74
75 if (paddr < kImageAddr) {
76 // Copy the Vectors out of the way.
77 bcopy((char *)((unsigned long)binary + offset), gVectorSaveAddr,
78 kVectorSize - paddr);
79
80 offset += kImageAddr - paddr;
81 filesz -= kImageAddr - paddr;
82 paddr = kImageAddr;
83 }
84
85 // Move the program.
86 bcopy((char *)((unsigned long)binary + offset), (char *)paddr, filesz);
87 }
88 }
89
90 return 0;
91 }