2 * $Id: loader.c,v 1.9 2006-01-22 22:41:40 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.
28 #include "bootstrap.h"
30 char *bootstrap_file = DEFAULT_BOOTSTRAP_FILE;
31 char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
32 char iso_magic[6] = "\001CD001";
33 char *file_loader_extensions[][2] = {
34 { "sbi", "Self Boot Inducer" },
35 { "bin", "SH4 Bin file" },
38 #define BOOTSTRAP_LOAD_ADDR 0x8C008000
39 #define BOOTSTRAP_SIZE 32768
41 #define BINARY_LOAD_ADDR 0x8C010000
43 #define CDI_V2 0x80000004
44 #define CDI_V3 0x80000005
46 gboolean file_load_magic( const gchar *filename )
52 int fd = open( filename, O_RDONLY );
54 ERROR( "Unable to open file: '%s' (%s)", filename,
61 if( st.st_size < 32768 ) {
62 ERROR( "File '%s' too small to be a dreamcast image", filename );
69 if( read( fd, buf, 32 ) != 32 ) {
70 ERROR( "Unable to read from file '%s'", filename );
74 if( memcmp( buf, bootstrap_magic, 32 ) == 0 ) {
75 /* we have a DC bootstrap */
76 if( st.st_size == BOOTSTRAP_SIZE ) {
77 char *load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
78 lseek( fd, 0, SEEK_SET );
79 read( fd, load, BOOTSTRAP_SIZE );
80 bootstrap_dump( load );
81 sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
84 /* look for a valid ISO9660 header */
85 lseek( fd, 32768, SEEK_SET );
87 if( memcmp( buf, iso_magic, 6 ) == 0 ) {
89 INFO( "Loading ISO9660 filesystem from '%s'",
93 } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
94 /* ZIP file, aka SBI file */
95 WARN( "SBI files not supported yet" );
96 } else if( memcpy( buf, "\x7fELF", 4 ) == 0 ) {
98 WARN( "ELF files not supported yet" );
100 /* Assume raw binary */
101 file_load_binary( filename );
107 int file_load_binary( const gchar *filename ) {
108 /* Load the binary itself */
109 mem_load_block( filename, BINARY_LOAD_ADDR, -1 );
110 if( bootstrap_file != NULL ) {
111 /* Load in a bootstrap before the binary, to initialize everything
114 mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE );
115 sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
117 sh4_set_pc( BINARY_LOAD_ADDR );
.