46 #include <sys/types.h>
64 static int n_executables_loaded = 0;
69 #define unencode(var,dataptr,typ) { \
70 int Wi; unsigned char Wb; \
71 unsigned char *Wp = (unsigned char *) dataptr; \
72 int Wlen = sizeof(typ); \
74 for (Wi=0; Wi<Wlen; Wi++) { \
75 if (encoding == ELFDATA2LSB) \
76 Wb = Wp[Wlen-1 - Wi]; \
79 if (Wi == 0 && (Wb & 0x80)) { \
106 return n_executables_loaded;
121 char *filename, uint64_t *entrypointp,
122 int arch, uint64_t *gpp,
int *byte_orderp, uint64_t *tocp)
126 const char *tmpdir = getenv(
"TMPDIR") == NULL?
128 unsigned char buf[12];
129 unsigned char buf2[2];
134 if (byte_orderp == NULL) {
135 fprintf(stderr,
"file_load(): byte_order == NULL\n");
140 fprintf(stderr,
"file_load(): FATAL ERROR: no arch?\n");
144 if (mem == NULL || filename == NULL) {
145 fprintf(stderr,
"file_load(): mem or filename is NULL\n");
150 if (filename[0] ==
'@')
153 debug(
"loading %s%s\n", filename,
verbose >= 2?
":" :
"");
160 f = fopen(filename,
"r");
162 file_load_raw(
machine, mem, filename, entrypointp);
166 fseek(
f, 0, SEEK_END);
168 fseek(
f, 0, SEEK_SET);
170 memset(buf, 0,
sizeof(buf));
171 len = fread(buf, 1,
sizeof(buf),
f);
172 fseek(
f, 510, SEEK_SET);
173 len2 = fread(buf2, 1,
sizeof(buf2),
f);
176 if (len < (
signed int)
sizeof(buf)) {
177 fprintf(stderr,
"\nThis file is too small to contain "
178 "anything useful\n");
183 if (buf[0] == 0x7f && buf[1]==
'E' && buf[2]==
'L' && buf[3]==
'F') {
184 file_load_elf(
machine, mem, filename,
185 entrypointp, arch, gpp, byte_orderp, tocp);
190 if (memcmp(
"ANDROID!", &buf[0], 8) == 0) {
191 file_load_android(
machine, mem, filename, 0,
192 entrypointp, arch, byte_orderp);
197 if (buf[0]==0x00 && buf[1]==0x8b && buf[2]==0x01 && buf[3]==0x07) {
199 file_load_aout(
machine, mem, filename, 0,
200 entrypointp, arch, byte_orderp);
203 if (buf[0]==0x00 && buf[1]==0x87 && buf[2]==0x01 && buf[3]==0x08) {
205 file_load_aout(
machine, mem, filename,
207 entrypointp, arch, byte_orderp);
210 if (buf[0]==0x00 && buf[1]==0x99 && buf[2]==0x01 && buf[3]==0x0b) {
213 entrypointp, arch, byte_orderp);
216 if (buf[0]==0x00 && buf[1]==0x8f && buf[2]==0x01 && buf[3]==0x0b) {
219 entrypointp, arch, byte_orderp);
222 if (buf[0]==0x00 && buf[1]==0x86 && buf[2]==0x01 && buf[3]==0x0b) {
225 entrypointp, arch, byte_orderp);
228 if (buf[0]==0x01 && buf[1]==0x03 && buf[2]==0x01 && buf[3]==0x07) {
231 entrypointp, arch, byte_orderp);
234 if (buf[0]==0x00 && buf[2]==0x00 && buf[8]==0x7a && buf[9]==0x75) {
237 entrypointp, arch, byte_orderp);
244 if (buf[0] == 0xfe && buf[1] == 0xed && buf[2] == 0xfa &&
245 (buf[3] == 0xce || buf[3] == 0xcf)) {
246 file_load_macho(
machine, mem, filename, entrypointp,
247 arch, byte_orderp, buf[3] == 0xcf, 0);
250 if ((buf[0] == 0xce || buf[0] == 0xcf) && buf[1] == 0xfa &&
251 buf[2] == 0xed && buf[3] == 0xfe) {
252 file_load_macho(
machine, mem, filename, entrypointp,
253 arch, byte_orderp, buf[0] == 0xcf, 1);
275 file_load_ecoff(
machine, mem, filename, entrypointp,
276 arch, gpp, byte_orderp);
281 if ((buf[0]==
'S' && buf[1]>=
'0' && buf[1]<=
'9')) {
282 file_load_srec(
machine, mem, filename, entrypointp);
287 if (buf[0]==0x1f && buf[1]==0x8b) {
288 fprintf(stderr,
"\nYou need to gunzip the file before you"
289 " try to use it.\n");
293 if (size > 24000000) {
294 fprintf(stderr,
"\nThis file is very large (%lli bytes)\n",
296 fprintf(stderr,
"Are you sure it is a kernel and not a disk "
297 "image? (Use the -d option.)\n");
302 fprintf(stderr,
"Hm... this file is the size of a 1.44 MB "
303 "floppy image. Maybe you forgot the\n-d switch?\n");
311 snprintf(tmpname,
sizeof(tmpname),
"%s/gxemul.", tmpdir);
313 strncmp(filename, tmpname,
strlen(tmpname)) == 0) {
314 char *tmp_filename = (
char *) malloc(
strlen(filename) + 100);
315 snprintf(tmp_filename,
strlen(filename) + 100,
316 "%s.descrambled", filename);
317 debug(
"descrambling into %s\n", tmp_filename);
320 snprintf(tmp_filename,
strlen(filename) + 100,
321 "0x8c010000:%s.descrambled", filename);
322 debug(
"loading descrambled Dreamcast binary\n");
323 file_load_raw(
machine, mem, tmp_filename, entrypointp);
325 snprintf(tmp_filename,
strlen(filename) + 100,
326 "%s.descrambled", filename);
327 remove(tmp_filename);
331 *entrypointp = 0x8c000140;
342 for (i=0; i<(signed)
sizeof(buf); i++)
343 if (buf[i] < 32 && buf[i] !=
'\t' &&
344 buf[i] !=
'\n' && buf[i] !=
'\r' &&
346 fprintf(stderr,
"\nThe file format of '%s' is "
347 "unknown.\n\n ", filename);
348 for (i=0; i<(signed)
sizeof(buf); i++)
349 fprintf(stderr,
" %02x", buf[i]);
351 if (len2 == 2 && buf2[0] == 0x55 && buf2[1] == 0xaa)
352 fprintf(stderr,
"\n\nIt has a PC-style "
353 "bootsector marker.");
355 fprintf(stderr,
"\n\nPossible explanations:\n\n"
356 " o) If this is a disk image, you forgot '-d' "
357 "on the command line.\n"
358 " o) You are attempting to load a raw binary "
359 "into emulated memory,\n"
360 " but forgot to add the address prefix.\n"
361 " o) This is an unsupported binary format.\n\n");