summaryrefslogtreecommitdiff
path: root/roms/openhackware/src/libexec/pef.c
blob: 2c580147eb0558ee6e5a1278c3101fdc91192c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * <pef.c>
 *
 * Open Hack'Ware BIOS Classic MacOS executable file loader
 * 
 * Copyright (c) 2004-2005 Jocelyn Mayer
 * 
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License V2
 *   as published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <stdio.h>
#include "bios.h"
#include "exec.h"

/* PEF (old MacOS executable format) */
typedef struct PEF_container_t PEF_container_t;
struct PEF_container_t {
    uint32_t tag1;
    uint32_t tag2;
    uint32_t arch;
    uint32_t version;
    uint32_t timestamp;
    uint32_t oldDefVersion;
    uint32_t oldImpVersion;
    uint32_t currentVersion;
    uint16_t nb_sections;
    uint16_t nb_inst_sections;
    uint32_t pad;
} __attribute__ (( packed ));

typedef struct PEF_section_t PEF_section_t;
struct PEF_section_t {
    int32_t name_offset;
    uint32_t address;
    uint32_t total_size;
    uint32_t unpacked_size;
    uint32_t packed_size;
    uint32_t container_offset;
    uint8_t  section_kind;
    uint8_t  share_kind;
    uint8_t  align;
    uint8_t  pad;
} __attribute__ (( packed ));

typedef struct PEF_loader_t PEF_loader_t;
struct PEF_loader_t {
    int32_t  main_section;
    uint32_t main_offset;
    int32_t  init_section;
    uint32_t init_offset;
    int32_t  term_section;
    uint32_t term_offset;
    uint32_t nb_import_libs;
    uint32_t nb_import_symbols;
    uint32_t nb_reloc_sections;
    uint32_t reloc_instr_offset;
    uint32_t loader_strings_offset;
    uint32_t export_hash_offset;
    uint32_t export_hashtable_power;
    uint32_t nb_export_symbols;
} __attribute__ (( packed ));

enum {
    PEF_SECTION_CODE     = 0,
    PEF_SECTION_UNPDATA  = 1,
    PEF_SECTION_INIDATA  = 2,
    PEF_SECTION_CONSTANT = 3,
    PEF_SECTION_LOADER   = 4,
    PEF_SECTION_DEBUG    = 5,
    PEF_SECTION_EXEC     = 6,
    PEF_SECTION_EXCP     = 7,
    PEF_SECTION_TRACE    = 8,
};

enum {
    PEF_SHARE_PROCESS    = 1,
    PEF_SHARE_GLOBAL     = 4,
    PEF_SHARE_PROTECTED  = 5,
};

int exec_load_pef (inode_t *file, void **dest, void **entry, void **end,
                   uint32_t loffset)
{
    PEF_container_t container;
    PEF_section_t section;
    PEF_loader_t loader;
    void *first, *last, *addr, **sections;
    uint32_t pos, padsize, size, lpos, main_offset;
    uint8_t opcode;
    int nb_sections, nb_inst_sections, main_section, i, n;

    file_seek(file, loffset);
    if (fs_read(file, &container, sizeof(PEF_container_t)) < 0) {
        ERROR("Cannot load container header\n");
        return -1;
    }
    pos = sizeof(PEF_container_t);
    /* Check tags and architecture */
    if (memcmp(&container.tag1, "Joy!", 4) != 0) {
        DPRINTF("No joy, no PEF\n");
        return -2;
    }
    if (memcmp(&container.tag2, "peff", 4) != 0) {
        DPRINTF("No PEFF file\n");
        return -2;
    }
    if (memcmp(&container.arch, "pwpc", 4) != 0) {
        DPRINTF("PEFF file not for PPC\n");
        return -2;
    }
    if (get_be32(&container.version) != 1) {
        DPRINTF("Unknown PEFF container version\n");
        return -2;
    }
    nb_sections = get_be32(&container.nb_sections);
    sections = malloc(nb_sections * sizeof(void *));
    if (sections == NULL) {
        ERROR("Cannot allocate sections\n");
        return -1;
    }
    nb_inst_sections = get_be32(&container.nb_inst_sections);
    first = (void *)0xFFFFFFFF;
    last = NULL;
    main_section = -1;
    main_offset = 0;
    for (i = 0, n = 0; i < nb_sections; i++) {
        file_seek(file, loffset + pos);
        if (fs_read(file, &section, sizeof(PEF_section_t)) < 0) {
            ERROR("Cannot read section %d\n", i);
            return -1;
        }
        pos += sizeof(PEF_section_t);
        addr = (void *)get_be32(&section.address);
        sections[i] = addr;
        if (addr < first)
            first = addr;
        size = get_be32(&section.total_size);
        lpos = get_be32(&section.container_offset);
        file_seek(file, loffset + lpos);
        switch (section.section_kind) {
        case PEF_SECTION_CODE:
        case PEF_SECTION_UNPDATA:
            /* Load as raw data */
            padsize = get_be32(&section.unpacked_size) - size;
            file_seek(file, loffset + lpos);
            if (fs_read(file, addr, size) < 0) {
                ERROR("Cannot load section %d\n", i);
                return -1;
            }
            addr = (char *)addr + size;
            memset(addr, 0, padsize);
            addr = (char *)addr + padsize;
            break;
        case PEF_SECTION_INIDATA:
        case PEF_SECTION_CONSTANT:
        case PEF_SECTION_EXEC:
            /* Load as compressed data */
            for (;;) {
                void *ref;
                uint32_t total;
                uint8_t bsize, csize, count, j;

                if (fs_read(file, &opcode, 1) < 0) {
                    ERROR("Cannot get opcode\n");
                    return -1;
                }
                bsize = opcode & 0x1F;
                switch (opcode >> 5) {
                case 0x0:
                    /* Initialize size bytes to zero */
                    memset(addr, 0, bsize);
                    addr = (char *)addr + bsize;
                    total = bsize;
                    break;
                case 0x1:
                    /* Copy bloc */
                    if (fs_read(file, addr, bsize) < 0) {
                        ERROR("Cannot copy bloc\n");
                        return -1;
                    }
                    addr = (char *)addr + bsize;
                    total = bsize;
                    break;
                case 0x2:
                    /* Repeat bloc */
                    if (fs_read(file, &count, 1) < 0) {
                        ERROR("Cannot read bloc size\n");
                        return -1;
                    }
                    total = 0;
                    if (count == 0) {
                        break;
                    }
                    if (fs_read(file, addr, bsize) < 0) {
                        ERROR("Cannot read repeat bloc\n");
                        return -1;
                    }
                    ref = addr;
                    addr = (char *)addr + bsize;
                    for (j = 1; j < count; j++) {
                        memcpy(addr, ref, bsize);
                        total += bsize;
                        addr = (char *)addr + bsize;
                    }
                    break;
                case 0x3:
                    /* Interleave repeat bloc with bloc copy */
                    if (fs_read(file, &csize, 1) < 0 ||
                        fs_read(file, &count, 1) < 0) {
                        ERROR("Cannot read repeat params\n");
                        return -1;
                    }
                    ref = addr;
                    if (fs_read(file, addr, bsize) < 0) {
                        ERROR("Cannot read common data\n");
                        return -1;
                    }
                    addr = (char *)addr + bsize;
                    total = bsize;
                    for (j = 0; j < count; j++) {
                        if (fs_read(file, addr, csize) < 0) {
                            ERROR("Cannot read custom data\n");
                            return -1;
                        }
                        addr = (char *)addr + csize;
                        memcpy(addr, ref, bsize);
                        addr = (char *)addr + bsize;
                        total += csize + bsize;
                    }
                    break;
                case 0x4:
                    /* Interleave repeat bloc with zero */
                    if (fs_read(file, &csize, 1) < 0 ||
                        fs_read(file, &count, 1) < 0) {
                        ERROR("Cannot read repeat params\n");
                        return -1;
                    }
                    total = 0;
                    for (j = 0; j < count; j++) {
                        memset(addr, 0, bsize);
                        addr = (char *)addr + bsize;
                        if (fs_read(file, addr, csize) < 0) {
                            ERROR("Cannot read repeat data\n");
                            return -1;
                        }
                        addr = (char *)addr + csize;
                        total += csize + bsize;
                    }
                    memset(addr, 0, bsize);
                    addr = (char *)addr + bsize;
                    total += bsize;
                    break;
                default:
                    ERROR("Unknown opcode\n");
                    return -1;
                }
                if (addr > last)
                    last = addr;
                if (total >= size)
                    break;
                size -= total;
            }
            break;
        case PEF_SECTION_LOADER:
            /* find entry point */
            if (fs_read(file, &loader, sizeof(PEF_loader_t)) < 0) {
                ERROR("Cannot read loader header\n");
                return -1;
            }
            main_section = get_be32(&loader.main_section);
            main_offset = get_be32(&loader.main_offset);
            if (main_section >= nb_sections) {
                ERROR("Invalid main section\n");
                return -1;
            }
            break;
        case PEF_SECTION_DEBUG:
        case PEF_SECTION_EXCP:
        case PEF_SECTION_TRACE:
            break;
        default:
            return -2;
        }
    }
    *dest = first;
    *end = last;
    if (main_section == -1) {
        *entry = first;
    } else {
        *entry = (char *)sections[main_section] + main_offset;
    }
    free(sections);

    return 0;
}