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
|
// Originally written in C++ by Lameguy64
// Ported to plain C by Orion
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elf.h"
#ifdef WIN32
#define strcasecmp _stricmp
#endif
#define MAX_prg_entry_count 128
#define true (1)
#define false (0)
typedef struct {
unsigned int pc0;
unsigned int gp0;
unsigned int t_addr;
unsigned int t_size;
unsigned int d_addr;
unsigned int d_size;
unsigned int b_addr;
unsigned int b_size;
unsigned int sp_addr;
unsigned int sp_size;
unsigned int sp;
unsigned int fp;
unsigned int gp;
unsigned int ret;
unsigned int base;
} EXEC;
typedef struct {
char header[8];
char pad[8];
EXEC params;
char license[64];
char pad2[1908];
} PSEXE;
int main(int argc, char** argv) {
char* in_file = NULL;
char* out_file = NULL;
int quiet = false;
int i;
FILE* fp;
ELF_HEADER head;
PRG_HEADER prg_heads[MAX_prg_entry_count];
unsigned int exe_taddr = 0xffffffff;
unsigned int exe_haddr = 0;
unsigned int exe_tsize = 0;
unsigned char* binary;
PSEXE exe;
char *output_name;
for( i=1; i<argc; i++ ) {
if( strcasecmp( "-q", argv[i] ) == 0 ) {
quiet = true;
} else {
if( in_file == NULL ) {
in_file = argv[i];
} else if( out_file == NULL ) {
out_file = argv[i];
}
}
}
if( !quiet ) {
printf( "PSn00bSDK elf2x - ELF to PS-EXE Converter\n" );
printf( "2018-2021 Meido-Tek Productions\n\n" );
}
if( argc == 1 ) {
printf( "Usage:\n" );
printf( " elf2x [-q] <elf_file> [exe_file]\n" );
return 0;
}
if( in_file == NULL ) {
printf( "No input file specified.\n" );
return EXIT_FAILURE;
}
fp = fopen( in_file, "rb" );
if( fp == NULL ) {
printf( "Cannot open file %s.\n", in_file );
return EXIT_FAILURE;
}
fread( &head, 1, sizeof(head), fp );
// Check header
if( head.magic != 0x464c457f ) {
printf( "File is not an ELF file.\n" );
fclose( fp );
return EXIT_FAILURE;
}
if( head.type != 2 ) {
printf( "Only executable ELF files are supported.\n" );
fclose( fp );
return EXIT_FAILURE;
}
if( head.instr_set != 8 ) {
printf( "ELF file is not a MIPS binary.\n" );
fclose( fp );
return EXIT_FAILURE;
}
if( head.word_size != 1 ) {
printf( "Only 32-bit ELF files are supported.\n" );
fclose( fp );
return EXIT_FAILURE;
}
if( head.endianness != 1 ) {
printf( "Only little endian ELF files are supported.\n" );
fclose( fp );
return EXIT_FAILURE;
}
// Load program headers and determine binary size and load address
fseek( fp, head.prg_head_pos, SEEK_SET );
for( i=0; i<head.prg_entry_count; i++ ) {
fread( &prg_heads[i], 1, sizeof(PRG_HEADER), fp );
if( prg_heads[i].flags == 4 ) {
continue;
}
if( prg_heads[i].p_vaddr < exe_taddr ) {
exe_taddr = prg_heads[i].p_vaddr;
}
if( prg_heads[i].p_vaddr > exe_haddr ) {
exe_haddr = prg_heads[i].p_vaddr;
}
}
exe_tsize = (exe_haddr - exe_taddr);
exe_tsize += prg_heads[head.prg_entry_count - 1].p_filesz;
if (!quiet)
printf(
"pc:%08x t_addr:%08x t_size:%d\n",
head.prg_entry_addr,
exe_taddr,
exe_tsize
);
// Check if load address is appropriate in main RAM locations
if (
((exe_taddr >= 0x00000000) && (exe_taddr < 0x00001000)) ||
((exe_taddr >= 0x80000000) && (exe_taddr < 0x80001000)) ||
((exe_taddr >= 0xa0000000) && (exe_taddr < 0xa0001000))
)
printf("WARNING: program text address overlaps kernel area!\n");
// Throw warning if executable is larger than 2MB
if (exe_tsize > 0x1f0000)
printf("WARNING: executable is larger than available RAM on a stock console!\n");
// Pad out the size to multiples of 2KB (CD sector size)
exe_tsize = ((exe_tsize + 2047) / 2048) * 2048;
// Load the binary data
binary = (unsigned char*)malloc( exe_tsize );
memset( binary, 0x0, exe_tsize );
for( i=0; i<head.prg_entry_count; i++ ) {
if( prg_heads[i].flags == 4 ) {
continue;
}
fseek( fp, prg_heads[i].p_offset, SEEK_SET );
fread( &binary[(int)(prg_heads[i].p_vaddr-exe_taddr)],
1, prg_heads[i].p_filesz, fp );
}
fclose( fp );
if( out_file ) {
output_name = out_file;
} else {
char *ptr;
// Generate output filename if no output is specified
output_name = in_file;
ptr = &output_name[strlen(output_name)];
while (ptr != output_name)
{
if (*ptr == '.')
break;
else
ptr--;
}
if (ptr != output_name)
{
strcpy(ptr, ".exe");
}
else
{
strcat(ptr, ".exe");
}
}
// Prepare PS-EXE header
memset( &exe, 0, sizeof(PSEXE) );
//exe.params.sp_addr = 0x801FFFF0;
exe.params.t_addr = exe_taddr;
exe.params.t_size = exe_tsize;
exe.params.pc0 = head.prg_entry_addr;
// Some later PAL BIOS versions seem to actually verify the license string
// in the executable (despite what the nocash docs claim) and display the
// dreaded "insert PlayStation CD-ROM" screen if it's not valid.
strncpy( exe.header, "PS-X EXE", 8 );
strcpy( exe.license,
"Sony Computer Entertainment Inc. for Europe area" );
strcpy( exe.pad2, "Built using GCC and PSn00bSDK libraries" );
// Write file
fp = fopen( output_name, "wb" );
if( !fp ) {
printf( "Cannot write output: %s\n", output_name );
free( binary );
fclose( fp );
}
fwrite( &exe, 1, sizeof( PSEXE ), fp );
fwrite( binary, 1, exe_tsize, fp );
fclose( fp );
free( binary );
return 0;
}
|