aboutsummaryrefslogtreecommitdiff
path: root/tools/util/elf2cpe.c
blob: 210f25b99acee3bc5801c75f04cc819333ead753 (plain) (blame)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "elf.h"

#ifdef WIN32
#define strcasecmp _stricmp
#endif

#define	MAX_prg_entry_count	128

#ifndef false
#define false	0
#endif

#ifndef true
#define true	1
#endif

char *in_file = NULL;
char *out_file = NULL;

int quiet;

int copydata( FILE *in, FILE *out, int len )
{
	char buff[8192];
	int i,readlen;
	
	while( len > 0 )
	{
		readlen = len;
		if( readlen > 8192 )
		{
			readlen = 8192;
		}
		
		i = fread( buff, 1, readlen, in );
		fwrite( buff, 1, i, out );
		
		if( i < readlen )
		{
			break;
		}
		len -= readlen;
	}
	
	return( 0 );
	
} /* copydata */

int convertELF( void )
{
	ELF_HEADER head;
	PRG_HEADER prg_heads[MAX_prg_entry_count];
	
	int i;
	char *output_name;
	
	FILE *in_fp;
	FILE *out_fp;
	
	/* Generate output filename if no output is specified */
	if( out_file )
	{
		output_name = out_file;
	}
	else
	{
		char	*ptr;

		output_name = strdup( in_file );

		ptr = &output_name[strlen(output_name)];
		while (ptr != output_name)
		{
			if (*ptr == '.')
				break;
			else
				ptr--;
		}

		if( ptr != output_name )
		{
			strcpy( ptr, ".cpe" );
		}
		else
		{
			strcat( ptr, ".cpe" );
		}
	}
	
	if( !out_file )
	{
		free( output_name );
	}
	
	if( !( in_fp = fopen( in_file, "rb" ) ) )
	{
		printf( "Cannot open input file.\n" );
		return( -1 );
	}
	
	/* read ELF header */
	fread( &head, 1, sizeof(head), in_fp );
	
	/* test header to make sure it is valid */
	if( head.magic != 0x464c457f )
	{
		printf( "File is not an ELF file.\n" );
		fclose( in_fp );
		return( -1 );
	}

	if( head.type != 2 )
	{
		printf( "Only executable ELF files are supported.\n" );
		fclose( in_fp );
		return( -1 );
	}

	if( head.instr_set != 8 )
	{
		printf( "ELF file is not a MIPS binary.\n" );
		fclose( in_fp );
		return( -1 );
	}

	if( head.word_size != 1 )
	{
		printf( "Only 32-bit ELF files are supported.\n" );
		fclose( in_fp );
		return( -1 );
	}

	if( head.endianness != 1 )
	{
		printf( "Only little endian ELF files are supported.\n" );
		fclose( in_fp );
		return( -1 );
	}
	
	/* read program headers */
	fseek( in_fp, head.prg_head_pos, SEEK_SET );
	fread( prg_heads, sizeof( PRG_HEADER ), head.prg_entry_count, in_fp );
	
	if( !quiet )
	{
		printf( "pc:%08x\n", head.prg_entry_addr );
	}
	
	/* create the CPE file */
	printf( "%s\n", output_name );
	if( !( out_fp = fopen( output_name, "wb" ) ) )
	{
		printf( "Cannot create output file.\n" );
		return( -1 );
	}
	
	/* create file header */
	fputs( "CPE", out_fp );
	fputc( 0x01, out_fp );
	
	/* select unit 0 */
	fputc( 0x08, out_fp );	/* chunk ID */
	fputc( 0x00, out_fp );	/* unit number */
	
	/* set entrypoint */
	fputc( 0x03, out_fp );	/* chunk ID */
	fputc( 0x90, out_fp );	/* entry point type */
	fputc( 0x00, out_fp );
	fwrite( &head.prg_entry_addr, 1, 4, out_fp );
	
	/* copy the program chunks */
	for( i=0; i<head.prg_entry_count; i++ )
	{
		if( prg_heads[i].flags == 4 )
		{
			continue;
		}
		
		/* seek to location of program chunk */
		fseek( in_fp, prg_heads[i].p_offset, SEEK_SET );
		
		/* create load chunk */
		fputc( 0x01, out_fp );	/* chunk ID */
		fwrite( &prg_heads[i].p_vaddr, 1, 4, out_fp );	/* load address */
		fwrite( &prg_heads[i].p_filesz, 1, 4, out_fp );	/* load length */
		
		/* copy the chunk data */
		copydata( in_fp, out_fp, prg_heads[i].p_filesz );
	}	
	
	/* end of file chunk */
	fputc( 0x00, out_fp );
	
	/* close files */
	fclose( in_fp );
	fclose( out_fp );
	
	return( 0 );
	
} /* convertELF */

int main( int argc, char *argv[] )
{
	int i;
	
	quiet = false;
	
	/* parse program arguments */
	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];
			}
		}
	}
	
	/* print banner if not in quiet mode */
	if( !quiet )
	{
		printf( "PSn00bSDK elf2cpe - Experimental ELF to CPE executable "
			"converter\n" );
		printf( "2021 Meido-Tek Productions\n\n" );
	}
	
	/* print usage parameters if no arguments specified */
	if( argc == 1 )
	{
		printf( "Usage:\n" );
		printf( "  elf2cpe [-q] <elf_file> [cpe_file]\n" );
		return( 0 );
	}
	
	if( !in_file )
	{
		printf( "No input file specified.\n" );
		return( 0 );
	}
	
	return( convertELF() );
	
} /* main */