Search
lxdream.org :: lxdream/src/loader.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 454:f6586b97813a
prev450:207461e79f21
next481:3b2d6c5a19ad
author nkeynes
date Wed Oct 31 09:05:44 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Add convenience MMIO_ADDR macro
view annotate diff log raw
     1 /**
     2  * $Id: loader.c,v 1.20 2007-10-21 05:20:35 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 <stdio.h>
    21 #include <fcntl.h>
    22 #include <sys/stat.h>
    23 #include <errno.h>
    24 #include <stdint.h>
    25 #include <elf.h>
    26 #include "mem.h"
    27 #include "sh4core.h"
    28 #include "bootstrap.h"
    29 #include "dreamcast.h"
    30 #include "config.h"
    31 #include "loader.h"
    32 #include "syscall.h"
    34 char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
    35 char iso_magic[6] = "\001CD001";
    36 char *file_loader_extensions[][2] = { 
    37     { "sbi", "Self Boot Inducer" },
    38     { "bin", "SH4 Bin file" },
    39     { NULL, NULL } };
    41 #define BOOTSTRAP_LOAD_ADDR 0x8C008000
    42 #define BOOTSTRAP_SIZE 32768
    44 #define BINARY_LOAD_ADDR 0x8C010000
    46 #define CDI_V2 0x80000004
    47 #define CDI_V3 0x80000005
    49 gboolean file_load_elf_fd( int fd );
    52 gboolean file_load_magic( const gchar *filename )
    53 {
    54     char buf[32];
    55     struct stat st;
    56     gboolean result = TRUE;
    58     int fd = open( filename, O_RDONLY );
    59     if( fd == -1 ) {
    60         return FALSE;
    61     }
    63     fstat( fd, &st );
    65     /* begin magic */
    66     if( read( fd, buf, 32 ) != 32 ) {
    67         ERROR( "Unable to read from file '%s'", filename );
    68         close(fd);
    69         return FALSE;
    70     }
    71     if( memcmp( buf, bootstrap_magic, 32 ) == 0 ) {
    72         /* we have a DC bootstrap */
    73         if( st.st_size == BOOTSTRAP_SIZE ) {
    74             unsigned char *load = (unsigned char *)mem_get_region( BOOTSTRAP_LOAD_ADDR );
    75             lseek( fd, 0, SEEK_SET );
    76             read( fd, load, BOOTSTRAP_SIZE );
    77             bootstrap_dump( load, TRUE );
    78             sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
    79             gtk_gui_update();
    80         } else {
    81             /* look for a valid ISO9660 header */
    82             lseek( fd, 32768, SEEK_SET );
    83             read( fd, buf, 8 );
    84             if( memcmp( buf, iso_magic, 6 ) == 0 ) {
    85                 /* Alright, got it */
    86                 INFO( "Loading ISO9660 filesystem from '%s'",
    87                       filename );
    88             }
    89         }
    90     } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
    91 	/* ZIP file, aka SBI file */
    92 	WARN( "SBI files not supported yet" );
    93 	result = FALSE;
    94     } else if( memcmp( buf, DREAMCAST_SAVE_MAGIC, 16 ) == 0 ) {
    95 	/* Save state */
    96 	result = (dreamcast_load_state( filename )==0);
    97     } else if( buf[0] == 0x7F && buf[1] == 'E' && 
    98 	       buf[2] == 'L' && buf[3] == 'F' ) {
    99 	/* ELF binary */
   100 	lseek( fd, 0, SEEK_SET );
   101 	result = file_load_elf_fd( fd );
   102     } else {
   103 	result = FALSE;
   104     }
   105     close(fd);
   106     return result;
   107 }
   109 void file_load_postload( int pc )
   110 {
   111     const gchar *bootstrap_file = lxdream_get_config_value(CONFIG_BOOTSTRAP);
   112     if( bootstrap_file != NULL ) {
   113 	/* Load in a bootstrap before the binary, to initialize everything
   114 	 * correctly
   115 	 */
   116 	if( mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE ) != 0 ) {
   117 	    /* Try it without the bootstrap */
   118 	    sh4_set_pc( pc );
   119 	} else {
   120 	    sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
   121 	}
   122     } else {
   123 	sh4_set_pc( pc );
   124     }
   125     bios_install();
   126     dcload_install();
   127     gtk_gui_update();
   128 }    
   131 gboolean file_load_binary( const gchar *filename )
   132 {
   133     /* Load the binary itself */
   134     if(  mem_load_block( filename, BINARY_LOAD_ADDR, -1 ) == 0 ) {
   135 	file_load_postload( BINARY_LOAD_ADDR );
   136 	return TRUE;
   137     } else {
   138 	return FALSE;
   139     }
   140 }
   142 gboolean file_load_elf_fd( int fd ) 
   143 {
   144     Elf32_Ehdr head;
   145     Elf32_Phdr phdr;
   146     int i;
   148     if( read( fd, &head, sizeof(head) ) != sizeof(head) )
   149 	return FALSE;
   150     if( head.e_ident[EI_CLASS] != ELFCLASS32 ||
   151 	head.e_ident[EI_DATA] != ELFDATA2LSB ||
   152 	head.e_ident[EI_VERSION] != 1 ||
   153 	head.e_type != ET_EXEC ||
   154 	head.e_machine != EM_SH ||
   155 	head.e_version != 1 ) {
   156 	ERROR( "File is not an SH4 ELF executable file" );
   157 	return FALSE;
   158     }
   160     /* Program headers */
   161     for( i=0; i<head.e_phnum; i++ ) {
   162 	lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
   163 	read( fd, &phdr, sizeof(phdr) );
   164 	if( phdr.p_type == PT_LOAD ) {
   165 	    lseek( fd, phdr.p_offset, SEEK_SET );
   166 	    char *target = mem_get_region( phdr.p_vaddr );
   167 	    read( fd, target, phdr.p_filesz );
   168 	    if( phdr.p_memsz > phdr.p_filesz ) {
   169 		memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
   170 	    }
   171 	    INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
   172 	}
   173     }
   175     file_load_postload( head.e_entry );
   176     return TRUE;
   177 }
.