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