filename | src/loader.c |
changeset | 502:c4ecae2b1b5e |
prev | 481:3b2d6c5a19ad |
next | 537:d924be49e192 |
author | nkeynes |
date | Sat Nov 17 01:18:09 2007 +0000 (16 years ago) |
permissions | -rw-r--r-- |
last change | Move the failing TA tests into a separate file for now - there's a bug about these already (basically error handling behaviour) |
view | annotate | diff | log | raw |
1 /**
2 * $Id: loader.c,v 1.22 2007-11-08 11:54:16 nkeynes Exp $
3 *
4 * File loading routines, mostly for loading demos without going through the
5 * whole procedure of making a CD image for them.
6 *
7 * Copyright (c) 2005 Nathan Keynes.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <errno.h>
25 #include <stdint.h>
26 #include <elf.h>
27 #include "mem.h"
28 #include "sh4core.h"
29 #include "bootstrap.h"
30 #include "dreamcast.h"
31 #include "config.h"
32 #include "loader.h"
33 #include "syscall.h"
35 // FIXME: This probably shouldn't be here
36 #include <gui/gtkui.h>
38 char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
39 char iso_magic[6] = "\001CD001";
40 char *file_loader_extensions[][2] = {
41 { "sbi", "Self Boot Inducer" },
42 { "bin", "SH4 Bin file" },
43 { NULL, NULL } };
45 #define BOOTSTRAP_LOAD_ADDR 0x8C008000
46 #define BOOTSTRAP_SIZE 32768
48 #define BINARY_LOAD_ADDR 0x8C010000
50 #define CDI_V2 0x80000004
51 #define CDI_V3 0x80000005
53 gboolean file_load_elf_fd( int fd );
56 gboolean file_load_magic( const gchar *filename )
57 {
58 char buf[32];
59 struct stat st;
60 gboolean result = TRUE;
62 int fd = open( filename, O_RDONLY );
63 if( fd == -1 ) {
64 return FALSE;
65 }
67 fstat( fd, &st );
69 /* begin magic */
70 if( read( fd, buf, 32 ) != 32 ) {
71 ERROR( "Unable to read from file '%s'", filename );
72 close(fd);
73 return FALSE;
74 }
75 if( memcmp( buf, bootstrap_magic, 32 ) == 0 ) {
76 /* we have a DC bootstrap */
77 if( st.st_size == BOOTSTRAP_SIZE ) {
78 sh4ptr_t load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
79 lseek( fd, 0, SEEK_SET );
80 read( fd, load, BOOTSTRAP_SIZE );
81 bootstrap_dump( load, TRUE );
82 sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
83 gtk_gui_update();
84 } else {
85 /* look for a valid ISO9660 header */
86 lseek( fd, 32768, SEEK_SET );
87 read( fd, buf, 8 );
88 if( memcmp( buf, iso_magic, 6 ) == 0 ) {
89 /* Alright, got it */
90 INFO( "Loading ISO9660 filesystem from '%s'",
91 filename );
92 }
93 }
94 } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
95 /* ZIP file, aka SBI file */
96 WARN( "SBI files not supported yet" );
97 result = FALSE;
98 } else if( memcmp( buf, DREAMCAST_SAVE_MAGIC, 16 ) == 0 ) {
99 /* Save state */
100 result = (dreamcast_load_state( filename )==0);
101 } else if( buf[0] == 0x7F && buf[1] == 'E' &&
102 buf[2] == 'L' && buf[3] == 'F' ) {
103 /* ELF binary */
104 lseek( fd, 0, SEEK_SET );
105 result = file_load_elf_fd( fd );
106 } else {
107 result = FALSE;
108 }
109 close(fd);
110 return result;
111 }
113 void file_load_postload( int pc )
114 {
115 const gchar *bootstrap_file = lxdream_get_config_value(CONFIG_BOOTSTRAP);
116 if( bootstrap_file != NULL ) {
117 /* Load in a bootstrap before the binary, to initialize everything
118 * correctly
119 */
120 if( mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE ) != 0 ) {
121 /* Try it without the bootstrap */
122 sh4_set_pc( pc );
123 } else {
124 sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
125 }
126 } else {
127 sh4_set_pc( pc );
128 }
129 bios_install();
130 dcload_install();
131 gtk_gui_update();
132 }
135 gboolean file_load_binary( const gchar *filename )
136 {
137 /* Load the binary itself */
138 if( mem_load_block( filename, BINARY_LOAD_ADDR, -1 ) == 0 ) {
139 file_load_postload( BINARY_LOAD_ADDR );
140 return TRUE;
141 } else {
142 return FALSE;
143 }
144 }
146 gboolean file_load_elf_fd( int fd )
147 {
148 Elf32_Ehdr head;
149 Elf32_Phdr phdr;
150 int i;
152 if( read( fd, &head, sizeof(head) ) != sizeof(head) )
153 return FALSE;
154 if( head.e_ident[EI_CLASS] != ELFCLASS32 ||
155 head.e_ident[EI_DATA] != ELFDATA2LSB ||
156 head.e_ident[EI_VERSION] != 1 ||
157 head.e_type != ET_EXEC ||
158 head.e_machine != EM_SH ||
159 head.e_version != 1 ) {
160 ERROR( "File is not an SH4 ELF executable file" );
161 return FALSE;
162 }
164 /* Program headers */
165 for( i=0; i<head.e_phnum; i++ ) {
166 lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
167 read( fd, &phdr, sizeof(phdr) );
168 if( phdr.p_type == PT_LOAD ) {
169 lseek( fd, phdr.p_offset, SEEK_SET );
170 sh4ptr_t target = mem_get_region( phdr.p_vaddr );
171 read( fd, target, phdr.p_filesz );
172 if( phdr.p_memsz > phdr.p_filesz ) {
173 memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
174 }
175 INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
176 }
177 }
179 file_load_postload( head.e_entry );
180 return TRUE;
181 }
.