-
Notifications
You must be signed in to change notification settings - Fork 52
/
elf.c
208 lines (188 loc) · 6.65 KB
/
elf.c
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
//--------------------------------------------------------------
// File name: elf.c
//--------------------------------------------------------------
#include "launchelf.h"
#define MAX_PATH 1025
extern u8 loader_elf[];
extern int size_loader_elf;
// ELF-loading stuff
#define ELF_MAGIC 0x464c457f
#define ELF_PT_LOAD 1
//------------------------------
typedef struct
{
u8 ident[16]; // struct definition for ELF object header
u16 type;
u16 machine;
u32 version;
u32 entry;
u32 phoff;
u32 shoff;
u32 flags;
u16 ehsize;
u16 phentsize;
u16 phnum;
u16 shentsize;
u16 shnum;
u16 shstrndx;
} elf_header_t;
//------------------------------
typedef struct
{
u32 type; // struct definition for ELF program section header
u32 offset;
void *vaddr;
u32 paddr;
u32 filesz;
u32 memsz;
u32 flags;
u32 align;
} elf_pheader_t;
//--------------------------------------------------------------
// End of data declarations
//--------------------------------------------------------------
// Start of function code
//--------------------------------------------------------------
// checkELFheader Tests for valid ELF file
// Modified version of loader from Independence
// (C) 2003 Marcus R. Brown <mrbrown@0xd6.org>
//--------------------------------------------------------------
int checkELFheader(char *path)
{
elf_header_t elf_head;
u8 *boot_elf = (u8 *)&elf_head;
const elf_header_t *eh = (const elf_header_t *)boot_elf;
int fd, size = 0, ret;
char fullpath[MAX_PATH], tmp[MAX_PATH], *p;
strcpy(fullpath, path);
if (!strncmp(fullpath, "mc", 2) || !strncmp(fullpath, "vmc", 3) || !strncmp(fullpath, "rom", 3) || !strncmp(fullpath, "cdrom", 5) || !strncmp(fullpath, "cdfs", 4)) {
; // fullpath is already correct
} else if (!strncmp(fullpath, "hdd0:", 5)) {
p = &path[5];
if (*p == '/')
p++;
sprintf(tmp, "hdd0:%s", p);
p = strchr(tmp, '/');
sprintf(fullpath, "pfs0:%s", p);
*p = 0;
if ((ret = mountParty(tmp)) < 0)
goto error;
fullpath[3] += ret;
} else if (!strncmp(fullpath, "dvr_hdd0:", 9)) {
p = &path[9];
if (*p == '/')
p++;
sprintf(tmp, "dvr_hdd0:%s", p);
p = strchr(tmp, '/');
sprintf(fullpath, "dvr_pfs0:%s", p);
*p = 0;
if ((ret = mountDVRPParty(tmp)) < 0)
goto error;
fullpath[7] += ret;
} else if (!strncmp(fullpath, "mass", 4)) {
const char *pathSep;
pathSep = strchr(path, '/');
if (pathSep && (pathSep - path < 7) && pathSep[-1] == ':')
strcpy(fullpath + (pathSep - path), pathSep + 1);
} else if (!strncmp(fullpath, "host:", 5)) {
if (path[5] == '/')
strcpy(fullpath + 5, path + 6);
} else {
return 0; // return 0 for unrecognized device
}
if ((fd = genOpen(fullpath, O_RDONLY)) < 0)
goto error;
size = (int)genLseek(fd, 0, SEEK_END);
if (!size) {
genClose(fd);
goto error;
}
genLseek(fd, 0, SEEK_SET);
genRead(fd, boot_elf, sizeof(elf_header_t));
genClose(fd);
if ((_lw((u32)&eh->ident) != ELF_MAGIC) || eh->type != 2)
goto error;
return 1; // return 1 for successful check
error:
return -1; // return -1 for failed check
}
//------------------------------
// End of func: int checkELFheader(const char *path)
//--------------------------------------------------------------
// RunLoaderElf loads LOADER.ELF from program memory and passes
// args of selected ELF and partition to it
// Modified version of loader from Independence
// (C) 2003 Marcus R. Brown <mrbrown@0xd6.org>
//------------------------------
void RunLoaderElf(char *filename, char *party)
{
u8 *boot_elf;
elf_header_t *eh;
elf_pheader_t *eph;
const void *pdata;
int i;
char *argv[2], bootpath[256];
if ((!strncmp(party, "hdd0:", 5))) {
if (0 > fileXioMount("pfs0:", party, FIO_MT_RDONLY)) {
// Some error occurred, it could be due to something else having used pfs0
unmountParty(0); // So we try unmounting pfs0, to try again
if (0 > fileXioMount("pfs0:", party, FIO_MT_RDONLY))
return; // If it still fails, we have to give up...
}
// If a path to a file on PFS is specified, change it to the standard format.
// hdd0:partition:pfs:path/to/file
if (strncmp(filename, "pfs0:", 5) == 0) {
sprintf(bootpath, "%s:pfs:%s", party, &filename[5]);
} else {
sprintf(bootpath, "%s:%s", party, filename);
}
argv[0] = filename;
argv[1] = bootpath;
} else if ((!strncmp(party, "dvr_hdd0:", 9))) {
if (0 > fileXioMount("dvr_pfs0:", party, FIO_MT_RDONLY)) {
// Some error occurred, it could be due to something else having used pfs0
unmountDVRPParty(0); // So we try unmounting pfs0, to try again
if (0 > fileXioMount("dvr_pfs0:", party, FIO_MT_RDONLY))
return; // If it still fails, we have to give up...
}
// If a path to a file on PFS is specified, change it to the standard format.
// dvr_hdd0:partition:pfs:path/to/file
if (strncmp(filename, "dvr_pfs0:", 9) == 0) {
sprintf(bootpath, "%s:pfs:%s", party, &filename[9]);
} else {
sprintf(bootpath, "%s:%s", party, filename);
}
argv[0] = filename;
argv[1] = bootpath;
} else {
argv[0] = filename;
argv[1] = filename;
}
/* NB: LOADER.ELF is embedded */
boot_elf = (u8 *)loader_elf;
eh = (elf_header_t *)boot_elf;
if (_lw((u32)&eh->ident) != ELF_MAGIC)
asm volatile("break\n");
eph = (elf_pheader_t *)(boot_elf + eh->phoff);
/* Scan through the ELF's program headers and copy them into RAM, then
zero out any non-loaded regions. */
for (i = 0; i < eh->phnum; i++) {
if (eph[i].type != ELF_PT_LOAD)
continue;
pdata = (void *)(boot_elf + eph[i].offset);
memcpy(eph[i].vaddr, pdata, eph[i].filesz);
if (eph[i].memsz > eph[i].filesz)
memset((void *)(((u8 *)(eph[i].vaddr)) + eph[i].filesz), 0,
eph[i].memsz - eph[i].filesz);
}
/* Let's go. */
SifExitRpc();
FlushCache(0);
FlushCache(2);
ExecPS2((void *)eh->entry, NULL, 2, argv);
}
//------------------------------
// End of func: void RunLoaderElf(char *filename, char *party)
//--------------------------------------------------------------
// End of file: elf.c
//--------------------------------------------------------------