45 #include <sys/types.h>
63 static int n_executables_loaded = 0;
68 #define unencode(var,dataptr,typ) { \
69 int Wi; unsigned char Wb; \
70 unsigned char *Wp = (unsigned char *) dataptr; \
71 int Wlen = sizeof(typ); \
73 for (Wi=0; Wi<Wlen; Wi++) { \
74 if (encoding == ELFDATA2LSB) \
75 Wb = Wp[Wlen-1 - Wi]; \
78 if (Wi == 0 && (Wb & 0x80)) { \
104 return n_executables_loaded;
119 char *filename, uint64_t *entrypointp,
120 int arch, uint64_t *gpp,
int *byte_orderp, uint64_t *tocp)
124 const char *tmpdir = getenv(
"TMPDIR") == NULL?
126 unsigned char buf[12];
127 unsigned char buf2[2];
132 if (byte_orderp == NULL) {
133 fprintf(stderr,
"file_load(): byte_order == NULL\n");
138 fprintf(stderr,
"file_load(): FATAL ERROR: no arch?\n");
142 if (mem == NULL || filename == NULL) {
143 fprintf(stderr,
"file_load(): mem or filename is NULL\n");
148 if (filename[0] ==
'@')
151 debug(
"loading %s%s\n", filename,
verbose >= 2?
":" :
"");
158 f = fopen(filename,
"r");
160 file_load_raw(
machine, mem, filename, entrypointp);
164 fseek(
f, 0, SEEK_END);
166 fseek(
f, 0, SEEK_SET);
168 memset(buf, 0,
sizeof(buf));
169 len = fread(buf, 1,
sizeof(buf),
f);
170 fseek(
f, 510, SEEK_SET);
171 len2 = fread(buf2, 1,
sizeof(buf2),
f);
174 if (len < (
signed int)
sizeof(buf)) {
175 fprintf(stderr,
"\nThis file is too small to contain "
176 "anything useful\n");
181 if (buf[0] == 0x7f && buf[1]==
'E' && buf[2]==
'L' && buf[3]==
'F') {
182 file_load_elf(
machine, mem, filename,
183 entrypointp, arch, gpp, byte_orderp, tocp);
188 if (buf[0]==0x00 && buf[1]==0x8b && buf[2]==0x01 && buf[3]==0x07) {
190 file_load_aout(
machine, mem, filename, 0,
191 entrypointp, arch, byte_orderp);
194 if (buf[0]==0x00 && buf[1]==0x87 && buf[2]==0x01 && buf[3]==0x08) {
196 file_load_aout(
machine, mem, filename,
198 entrypointp, arch, byte_orderp);
201 if (buf[0]==0x00 && buf[1]==0x99 && buf[2]==0x01 && buf[3]==0x0b) {
204 entrypointp, arch, byte_orderp);
207 if (buf[0]==0x00 && buf[1]==0x8f && buf[2]==0x01 && buf[3]==0x0b) {
210 entrypointp, arch, byte_orderp);
213 if (buf[0]==0x00 && buf[1]==0x86 && buf[2]==0x01 && buf[3]==0x0b) {
216 entrypointp, arch, byte_orderp);
219 if (buf[0]==0x01 && buf[1]==0x03 && buf[2]==0x01 && buf[3]==0x07) {
222 entrypointp, arch, byte_orderp);
225 if (buf[0]==0x00 && buf[2]==0x00 && buf[8]==0x7a && buf[9]==0x75) {
228 entrypointp, arch, byte_orderp);
235 if (buf[0] == 0xfe && buf[1] == 0xed && buf[2] == 0xfa &&
236 (buf[3] == 0xce || buf[3] == 0xcf)) {
237 file_load_macho(
machine, mem, filename, entrypointp,
238 arch, byte_orderp, buf[3] == 0xcf, 0);
241 if ((buf[0] == 0xce || buf[0] == 0xcf) && buf[1] == 0xfa &&
242 buf[2] == 0xed && buf[3] == 0xfe) {
243 file_load_macho(
machine, mem, filename, entrypointp,
244 arch, byte_orderp, buf[0] == 0xcf, 1);
266 file_load_ecoff(
machine, mem, filename, entrypointp,
267 arch, gpp, byte_orderp);
272 if ((buf[0]==
'S' && buf[1]>=
'0' && buf[1]<=
'9')) {
273 file_load_srec(
machine, mem, filename, entrypointp);
278 if (buf[0]==0x1f && buf[1]==0x8b) {
279 fprintf(stderr,
"\nYou need to gunzip the file before you"
280 " try to use it.\n");
284 if (size > 24000000) {
285 fprintf(stderr,
"\nThis file is very large (%lli bytes)\n",
287 fprintf(stderr,
"Are you sure it is a kernel and not a disk "
288 "image? (Use the -d option.)\n");
293 fprintf(stderr,
"Hm... this file is the size of a 1.44 MB "
294 "floppy image. Maybe you forgot the\n-d switch?\n");
302 snprintf(tmpname,
sizeof(tmpname),
"%s/gxemul.", tmpdir);
304 strncmp(filename, tmpname,
strlen(tmpname)) == 0) {
305 char *tmp_filename = (
char *) malloc(
strlen(filename) + 100);
306 snprintf(tmp_filename,
strlen(filename) + 100,
307 "%s.descrambled", filename);
308 debug(
"descrambling into %s\n", tmp_filename);
311 snprintf(tmp_filename,
strlen(filename) + 100,
312 "0x8c010000:%s.descrambled", filename);
313 debug(
"loading descrambled Dreamcast binary\n");
314 file_load_raw(
machine, mem, tmp_filename, entrypointp);
316 snprintf(tmp_filename,
strlen(filename) + 100,
317 "%s.descrambled", filename);
318 remove(tmp_filename);
322 *entrypointp = 0x8c000140;
333 for (i=0; i<(signed)
sizeof(buf); i++)
334 if (buf[i] < 32 && buf[i] !=
'\t' &&
335 buf[i] !=
'\n' && buf[i] !=
'\r' &&
337 fprintf(stderr,
"\nThe file format of '%s' is "
338 "unknown.\n\n ", filename);
339 for (i=0; i<(signed)
sizeof(buf); i++)
340 fprintf(stderr,
" %02x", buf[i]);
342 if (len2 == 2 && buf2[0] == 0x55 && buf2[1] == 0xaa)
343 fprintf(stderr,
"\n\nIt has a PC-style "
344 "bootsector marker.");
346 fprintf(stderr,
"\n\nPossible explanations:\n\n"
347 " o) If this is a disk image, you forgot '-d' "
348 "on the command line.\n"
349 " o) You are attempting to load a raw binary "
350 "into emulated memory,\n"
351 " but forgot to add the address prefix.\n"
352 " o) This is an unsupported binary format.\n\n");