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