Search
lxdream.org :: lxdream/src/loader.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 561:533f6b478071
prev543:361ec0a70cf2
next736:a02d1475ccfd
author nkeynes
date Sun Mar 16 04:49:19 2008 +0000 (16 years ago)
branchlxdream-render
permissions -rw-r--r--
last change Use max-z rather than min-z for tri sort (still wrong for some cases of course,
but consistent with prior behaviour)
view annotate diff log raw
     1 /**
     2  * $Id$
     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 "bootstrap.h"
    29 #include "dreamcast.h"
    30 #include "config.h"
    31 #include "loader.h"
    33 char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
    34 char iso_magic[6] = "\001CD001";
    35 char *file_loader_extensions[][2] = { 
    36     { "sbi", "Self Boot Inducer" },
    37     { "bin", "SH4 Bin file" },
    38     { NULL, NULL } };
    40 #define BOOTSTRAP_LOAD_ADDR 0x8C008000
    41 #define BOOTSTRAP_SIZE 32768
    43 #define BINARY_LOAD_ADDR 0x8C010000
    45 #define CDI_V2 0x80000004
    46 #define CDI_V3 0x80000005
    48 gboolean file_load_elf_fd( const gchar *filename, int fd );
    51 gboolean file_load_magic( const gchar *filename )
    52 {
    53     char buf[32];
    54     struct stat st;
    55     gboolean result = TRUE;
    57     int fd = open( filename, O_RDONLY );
    58     if( fd == -1 ) {
    59         return FALSE;
    60     }
    62     fstat( fd, &st );
    64     /* begin magic */
    65     if( read( fd, buf, 32 ) != 32 ) {
    66         ERROR( "Unable to read from file '%s'", filename );
    67         close(fd);
    68         return FALSE;
    69     }
    70     if( memcmp( buf, bootstrap_magic, 32 ) == 0 ) {
    71         /* we have a DC bootstrap */
    72         if( st.st_size == BOOTSTRAP_SIZE ) {
    73             sh4ptr_t load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
    74             lseek( fd, 0, SEEK_SET );
    75             read( fd, load, BOOTSTRAP_SIZE );
    76             bootstrap_dump( load, TRUE );
    77 	    dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR + 0x300 );
    78         } else {
    79             /* look for a valid ISO9660 header */
    80             lseek( fd, 32768, SEEK_SET );
    81             read( fd, buf, 8 );
    82             if( memcmp( buf, iso_magic, 6 ) == 0 ) {
    83                 /* Alright, got it */
    84                 INFO( "Loading ISO9660 filesystem from '%s'",
    85                       filename );
    86             }
    87         }
    88     } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
    89 	/* ZIP file, aka SBI file */
    90 	WARN( "SBI files not supported yet" );
    91 	result = FALSE;
    92     } else if( memcmp( buf, DREAMCAST_SAVE_MAGIC, 16 ) == 0 ) {
    93 	/* Save state */
    94 	result = (dreamcast_load_state( filename )==0);
    95     } else if( buf[0] == 0x7F && buf[1] == 'E' && 
    96 	       buf[2] == 'L' && buf[3] == 'F' ) {
    97 	/* ELF binary */
    98 	lseek( fd, 0, SEEK_SET );
    99 	result = file_load_elf_fd( filename, fd );
   100     } else {
   101 	result = FALSE;
   102     }
   103     close(fd);
   104     return result;
   105 }
   107 void file_load_postload( const gchar *filename, int pc )
   108 {
   109     const gchar *bootstrap_file = lxdream_get_config_value(CONFIG_BOOTSTRAP);
   110     if( bootstrap_file != NULL ) {
   111 	/* Load in a bootstrap before the binary, to initialize everything
   112 	 * correctly
   113 	 */
   114         if( mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE ) == 0 ) {
   115 	    dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR+0x300 );
   116 	    return;
   117 	}
   118     }
   119     dreamcast_program_loaded( filename, pc );
   120 }    
   123 gboolean file_load_binary( const gchar *filename )
   124 {
   125     /* Load the binary itself */
   126     if(  mem_load_block( filename, BINARY_LOAD_ADDR, -1 ) == 0 ) {
   127       file_load_postload( filename, BINARY_LOAD_ADDR );
   128 	return TRUE;
   129     } else {
   130 	return FALSE;
   131     }
   132 }
   134 gboolean file_load_elf_fd( const gchar *filename, int fd ) 
   135 {
   136     Elf32_Ehdr head;
   137     Elf32_Phdr phdr;
   138     int i;
   140     if( read( fd, &head, sizeof(head) ) != sizeof(head) )
   141 	return FALSE;
   142     if( head.e_ident[EI_CLASS] != ELFCLASS32 ||
   143 	head.e_ident[EI_DATA] != ELFDATA2LSB ||
   144 	head.e_ident[EI_VERSION] != 1 ||
   145 	head.e_type != ET_EXEC ||
   146 	head.e_machine != EM_SH ||
   147 	head.e_version != 1 ) {
   148 	ERROR( "File is not an SH4 ELF executable file" );
   149 	return FALSE;
   150     }
   152     /* Program headers */
   153     for( i=0; i<head.e_phnum; i++ ) {
   154 	lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
   155 	read( fd, &phdr, sizeof(phdr) );
   156 	if( phdr.p_type == PT_LOAD ) {
   157 	    lseek( fd, phdr.p_offset, SEEK_SET );
   158 	    sh4ptr_t target = mem_get_region( phdr.p_vaddr );
   159 	    read( fd, target, phdr.p_filesz );
   160 	    if( phdr.p_memsz > phdr.p_filesz ) {
   161 		memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
   162 	    }
   163 	    INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
   164 	}
   165     }
   167     file_load_postload( filename, head.e_entry );
   168     return TRUE;
   169 }
.