summaryrefslogtreecommitdiff
path: root/src/libexec/chrp.c
blob: 9a2be2ea818a85e16e9ffe3085b1516efc857ec5 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * <chrp.c>
 *
 * Open Hack'Ware BIOS CHRP boot 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 <ctype.h>
#include "bios.h"
#include "exec.h"
#include "libfs/libfs.h"

/* Simple XML parser */
typedef struct XML_tag_t XML_tag_t;
struct XML_tag_t {
    unsigned char *name;
    XML_tag_t *up;
    int dlen;
    void *data;
};

enum {
    CHRP_TAG_UNKNOWN = 0,
    CHRP_TAG_CHRP_BOOT,
    CHRP_TAG_COMPATIBLE,
    CHRP_TAG_DESCRIPTION,
    CHRP_TAG_BOOT_SCRIPT,
    CHRP_TAG_OS_BADGE_ICONS,
    CHRP_TAG_ICON,
    CHRP_TAG_BITMAP,
    CHRP_TAG_LICENSE,
};

enum {
    CHRP_SCRIPT_IGNORE = 0,
    CHRP_SCRIPT_LOAD_BOOT,
    CHRP_SCRIPT_EMBEDDED,
};

enum {
    XML_STATE_OUT = 0,
    XML_STATE_TAG,
    XML_STATE_DATA,
};

static int XML_get_type (const unsigned char *name)
{
    int ret;

    if (strcmp(name, "CHRP-BOOT") == 0)
        ret = CHRP_TAG_CHRP_BOOT;
    else if (strcmp(name, "COMPATIBLE") == 0)
        ret = CHRP_TAG_COMPATIBLE;
    else if (strcmp(name, "DESCRIPTION") == 0)
        ret = CHRP_TAG_DESCRIPTION;
    else if (strcmp(name, "BOOT-SCRIPT") == 0)
        ret = CHRP_TAG_BOOT_SCRIPT;
    else if (strcmp(name, "OS-BADGE-ICONS") == 0)
        ret = CHRP_TAG_OS_BADGE_ICONS;
    else if (strcmp(name, "ICON") == 0)
        ret = CHRP_TAG_ICON;
    else if (strcmp(name, "BITMAP") == 0)
        ret = CHRP_TAG_BITMAP;
    else if (strcmp(name, "LICENSE") == 0)
        ret = CHRP_TAG_LICENSE;
    else
        ret = CHRP_TAG_UNKNOWN;

    return ret;
}

static unsigned char *strfind (const unsigned char *buf, const unsigned char *str)
{
    const unsigned char *pos;
    int len = strlen(str);

    //    DPRINTF("Look for '%s' in \n'%s'\n", str, buf);
    for (pos = buf; *pos != '\0'; pos++) {
        if (memcmp(pos, str, len) == 0)
            return (unsigned char *)pos;
    }

    return NULL;
}

int exec_load_chrp (inode_t *file, void **dest, void **entry, void **end,
                    uint32_t loffset)
{
#define TMPNAME_LEN 512
    unsigned char tmpname[TMPNAME_LEN], *tmpp, *buf, *pos, *endc, c;
    XML_tag_t *tag, *tmp, *first;
    part_t *part;
    inode_t *inode;
    int state;
    int script_type = CHRP_SCRIPT_IGNORE;
    uint32_t crc, offset = 0;
    int ret, rel = 0;

    buf = malloc(16384);
    /* Check the file head */
    file_seek(file, loffset);
    fs_read(file, buf, 11);
    if (memcmp(buf, "<CHRP-BOOT>", 11) != 0) {
        ERROR("Not an Apple CHRP boot file !\n");
        return -2;
    }
    /* Re-seek at start of the file and start parsing it */
    file_seek(file, loffset);
    pos = buf;
    tag = NULL;
    first = NULL;
    ret = -1;
    fs_read(file, &c, 1);
    offset++;
    for (state = XML_STATE_TAG; state != XML_STATE_OUT;) {
        /* Get next char */
        fs_read(file, &c, 1);
        offset++;
        if ((state == XML_STATE_TAG && c != '>') ||
            (state == XML_STATE_DATA && c != '<')) {
            *pos++ = c;
            continue;
        }
        *pos++ = '\0';
        switch (state) {
        case XML_STATE_TAG:
            if (*buf == '/') {
                if (tag == NULL || strcmp(buf + 1, tag->name) != 0) {
                    ERROR("XML error: open name: '%s' close name: '%s'\n",
                          buf + 1, tag->name);
                    goto out;
                }
                DPRINTF("Close tag: '%s'\n", tag->name);
                switch (XML_get_type(tag->name)) {
                case CHRP_TAG_CHRP_BOOT:
                    /* Nothing to do */
                    break;
                case CHRP_TAG_COMPATIBLE:
                    /* Won't check... */
                    pos = tag->data;
                    if (*(char *)tag->data == 0x0d) {
                        pos++;
                    }
                    DPRINTF("Compatible: '%s'\n", pos);
                    break;
                case CHRP_TAG_DESCRIPTION:
                    pos = tag->data;
                    if (*(char *)tag->data == 0x0d) {
                        pos++;
                    }
                    DPRINTF("Description: '%s'\n", pos);
                    break;
                case CHRP_TAG_BOOT_SCRIPT:
                    /* Here is the interresting part... */
                    crc = crc32(0, tag->data, tag->dlen);
#if 0
                    DPRINTF("Forth script: %08x\n%s\n",
                            crc, (char *)tag->data);
#endif
                    switch (crc) {
                    case 0x5464F92C:
                        /* Mandrake 9.1 CD1 boot script */
                    case 0x4BC74ECF:
                        /* Mandrake 10.1 & 10.2 CD1 boot script */
                    case 0x5B265246:
                        /* Gentoo 1.2-r1 */
                        /* Gentoo 2004.1 minimal install CD */
                        /* Gentoo 1.4 live CDROM */
                        /* Knopix PPC beta-pre12 */
                    case 0x75420D8A:
                        /* Debian woody */
                        /* Debian 3.0r1 */
                        script_type = CHRP_SCRIPT_LOAD_BOOT;
                        goto do_script;
                    case 0x633e4c9c:
                        /* Debian Sarge */
                    case 0xbe3abf60:
                        /* Debian Sarge, installed on a hard disk drive */
                        script_type = CHRP_SCRIPT_LOAD_BOOT;
                        goto do_script;
                    case 0x07b86bfe:
                        /* Linux Fedora Core 3 */
                        script_type = CHRP_SCRIPT_LOAD_BOOT;
                        goto do_script;
                    case 0x9ccdf371:
                        script_type = CHRP_SCRIPT_LOAD_BOOT;
                        goto do_script;
                    case 0xEF423926:
                        /* OpenBSD 3.4 */
                    case 0x68e4f265:
                        /* OpenBSD 3.5 */
                    case 0x3b7ea9e1:
                        /* OpenBSD 3.6 */
                        script_type = CHRP_SCRIPT_LOAD_BOOT;
                        goto do_script;
                    case 0xB7981DBC:
                        /* iBook 2 hw test CDROM */
#if 1
                        script_type = CHRP_SCRIPT_LOAD_BOOT;
                        goto do_script;
#endif
                        
                    case 0xEA06C1A7:
                        /* MacOS 9.2 boot script:
                         * the XCOFF loader is embedded in the file...
                         */
                    case 0x53A95958:
                        /* iBook 2 restore CD (MacOS X 10.2) */
                        script_type = CHRP_SCRIPT_EMBEDDED;
                        pos = strfind(tag->data, "elf-offset");
                        if (pos != NULL) {
                            /* Go backward until we get the value */
                            for (--pos; *pos < '0' || *pos > '9'; pos--)
                                continue;
                            for (; *pos >= '0' && *pos <= '9'; pos--)
                                continue;
                            offset = strtol(pos, NULL, 16);
                            goto do_script;
                        }
                        ERROR("Didn't find boot file offset\n");
                        goto out;
                    case 0x8d5acb86:
                        /* Darwin-7.01
                         * The executable file is embedded after the script
                         */
                        script_type = CHRP_SCRIPT_EMBEDDED;
                        DPRINTF("Boot file embedded at the end of boot script\n");
                        break;
                    default:
                        ERROR("XML error: unknown Forth script: %08x\n%s\n",
                              crc, (char *)tag->data);
                        goto out;
                    }
                    break;

                do_script:
                    switch (script_type) {
                    case CHRP_SCRIPT_LOAD_BOOT:
                        pos = strfind(tag->data, "boot");
                        if (pos != NULL) {
                            /* Eat everything until file name */
                            for (pos += 4; *pos != ','; pos++)
                                continue;
                            /* Eat ',' */
                            for (++pos; isspace(*pos) || *pos == '"'; pos++)
                                continue;
                            /* Find file name end */
                        redo:
                            for (endc = pos;
                                 *endc != ' ' && *endc != '"' &&
                                 *endc != '\n' && *endc != '\r';
                                 endc++) {
                                if (*endc == '\\')
                                    *endc = '/';
                            }
                            if (memcmp(pos, "ofwboot", 7) == 0) {
                                for (pos = endc + 1; *pos == ' '; pos++)
                                    continue;
                                goto redo;
                            }
                            *endc = '\0';
                        }
                        DPRINTF("Real boot file is: '%s'\n", pos);
                        part = fs_inode_get_part(file);
                        /* check if it's a path or just a file */
                        tmpp = pos;
                        if ((pos[0] == '/' && pos[1] == '/') ||
                            pos[0] != '/') {
                            unsigned char *bootdir;
                            bootdir = fs_get_boot_dirname(part_fs(part));
                            if (bootdir == NULL) {
                                ERROR("Cannot get boot directory name\n");
                                bug();
                            }
                            snprintf(tmpname, TMPNAME_LEN,
                                     "%s/%s", bootdir, pos);
                            tmpname[TMPNAME_LEN - 1] = '\0';
                            rel++;
                            pos = tmpname;
                            DPRINTF("'%s' => '%s'\n", bootdir, pos);
                        }
                    retry:
                        inode = fs_open(part_fs(part), pos);
                        if (inode == NULL) {
                            ERROR("Real boot inode '%s' not found\n", pos);
                            /* Try in root directory */
                            if (rel == 1) {
                                for (; *tmpp == '/'; tmpp++)
                                    continue;
                                snprintf(tmpname, TMPNAME_LEN, "/%s", tmpp);
                                tmpname[TMPNAME_LEN - 1] = '\0';
                                rel++;
                                goto retry;
                            }
                            
                            bug();
                        }
                        ret = _bootfile_load(inode, dest, entry, end, 0, -1);
                        fs_close(inode);
                        goto out;
                    case CHRP_SCRIPT_EMBEDDED:
                        DPRINTF("Exec offset: %d %08x\n", offset, offset);
                        ret = 0;
                        goto out;
                    case CHRP_SCRIPT_IGNORE:
                        break;
                    }
                    break;
                case CHRP_TAG_OS_BADGE_ICONS:
                case CHRP_TAG_ICON:
                    /* Ignore it */
                    break;
                case CHRP_TAG_BITMAP:
                    /* Ignore it */
                    break;
                case CHRP_TAG_LICENSE:
                    /* Ignore it */
                    pos = tag->data;
                    if (*(char *)tag->data == 0x0d) {
                        pos++;
                    }
                    DPRINTF("License: '%s'\n", pos);
                    break;
                default:
                    ERROR("XML error: unknown tag: '%s'\n", tag->name);
                    goto out;
                }
                tmp = tag->up;
                if (tmp == NULL)
                    state = XML_STATE_OUT;
                else
                    state = XML_STATE_DATA;
                free(tag->name);
                free(tag->data);
                free(tag);
                tag = tmp;
            } else {
                tmp = malloc(sizeof(XML_tag_t));
                if (tmp == NULL) {
                    ERROR("Cannot allocate new tag\n");
                    goto out;
                }
                tmp->up = tag;
                /* Ignore tag attributes */
                pos = strchr(buf, ' ');
                if (pos != NULL)
                    *pos = '\0';
                tmp->name = strdup(buf);
                tag = tmp;
                if (first == NULL)
                    first = tag;
                DPRINTF("Open tag '%s'\n", tag->name);
                state = XML_STATE_DATA;
            }
            break;
        case XML_STATE_DATA:
            if (tag->data == NULL) {
                tag->dlen = pos - buf;
                tag->data = malloc(tag->dlen);
                memcpy(tag->data, buf, tag->dlen);
            }
            state = XML_STATE_TAG;
            break;
        }
        pos = buf;
    }
    ret = 0;
    fs_read(file, &c, 1);
    fs_read(file, &c, 1);
    offset += 2;
 out:
#if 1
    for (; tag != NULL; tag = tmp) {
        tmp = tag->up;
        free(tag->name);
        free(tag->data);
        free(tag);
    }
#endif
    if (ret == 0 && script_type == CHRP_SCRIPT_EMBEDDED) {
        DPRINTF("Load embedded file from offset %d (%d => %d)\n",
                offset, loffset, loffset + offset);
        ret = _bootfile_load(file, dest, entry, end, loffset + offset, -1);
    }
    DPRINTF("Done\n");

    return ret;
}