2 * $Id: loader.c,v 1.15 2007-01-16 09:18:32 nkeynes Exp $
4 * File loading routines, mostly for loading demos without going through the
5 * whole procedure of making a CD image for them.
7 * Copyright (c) 2005 Nathan Keynes.
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.
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.
29 #include "bootstrap.h"
30 #include "dreamcast.h"
32 char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
33 char iso_magic[6] = "\001CD001";
34 char *file_loader_extensions[][2] = {
35 { "sbi", "Self Boot Inducer" },
36 { "bin", "SH4 Bin file" },
39 #define BOOTSTRAP_LOAD_ADDR 0x8C008000
40 #define BOOTSTRAP_SIZE 32768
42 #define BINARY_LOAD_ADDR 0x8C010000
44 #define CDI_V2 0x80000004
45 #define CDI_V3 0x80000005
47 gboolean file_load_magic( const gchar *filename )
53 int fd = open( filename, O_RDONLY );
55 ERROR( "Unable to open file: '%s' (%s)", filename,
63 if( read( fd, buf, 32 ) != 32 ) {
64 ERROR( "Unable to read from file '%s'", filename );
68 if( memcmp( buf, bootstrap_magic, 32 ) == 0 ) {
69 /* we have a DC bootstrap */
70 if( st.st_size == BOOTSTRAP_SIZE ) {
71 char *load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
72 lseek( fd, 0, SEEK_SET );
73 read( fd, load, BOOTSTRAP_SIZE );
74 bootstrap_dump( load, TRUE );
75 sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
78 /* look for a valid ISO9660 header */
79 lseek( fd, 32768, SEEK_SET );
81 if( memcmp( buf, iso_magic, 6 ) == 0 ) {
83 INFO( "Loading ISO9660 filesystem from '%s'",
87 } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
88 /* ZIP file, aka SBI file */
89 WARN( "SBI files not supported yet" );
90 } else if( memcmp( buf, DREAMCAST_SAVE_MAGIC, 16 ) == 0 ) {
92 dreamcast_load_state( filename );
93 } else if( buf[0] == 0x7F && buf[1] == 'E' &&
94 buf[2] == 'L' && buf[3] == 'F' ) {
96 lseek( fd, 0, SEEK_SET );
97 file_load_elf_fd( fd );
99 /* Assume raw binary */
100 file_load_binary( filename );
106 int file_load_postload( int pc )
108 const gchar *bootstrap_file = dreamcast_get_config_value(CONFIG_BOOTSTRAP);
109 if( bootstrap_file != NULL ) {
110 /* Load in a bootstrap before the binary, to initialize everything
113 if( mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE ) != 0 ) {
114 /* Try it without the bootstrap */
117 sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
128 int file_load_binary( const gchar *filename )
130 /* Load the binary itself */
131 mem_load_block( filename, BINARY_LOAD_ADDR, -1 );
132 file_load_postload( BINARY_LOAD_ADDR );
135 int file_load_elf_fd( int fd )
141 if( read( fd, &head, sizeof(head) ) != sizeof(head) )
143 if( head.e_ident[EI_CLASS] != ELFCLASS32 ||
144 head.e_ident[EI_DATA] != ELFDATA2LSB ||
145 head.e_ident[EI_VERSION] != 1 ||
146 head.e_type != ET_EXEC ||
147 head.e_machine != EM_SH ||
148 head.e_version != 1 ) {
149 ERROR( "File is not an SH4 ELF executable file" );
153 /* Program headers */
154 for( i=0; i<head.e_phnum; i++ ) {
155 lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
156 read( fd, &phdr, sizeof(phdr) );
157 if( phdr.p_type == PT_LOAD ) {
158 lseek( fd, phdr.p_offset, SEEK_SET );
159 char *target = mem_get_region( phdr.p_vaddr );
160 read( fd, target, phdr.p_filesz );
161 if( phdr.p_memsz > phdr.p_filesz ) {
162 memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
164 INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
168 return file_load_postload( head.e_entry );
.