Search
lxdream.org :: lxdream/src/loader.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 88:0a1bd43fa4ad
prev27:1ef09a52cd1e
next93:bb1def61e901
author nkeynes
date Sun Jan 22 22:41:40 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change When loading a binary file, load IP.BIN before it to initialize everything
file annotate diff log raw
nkeynes@26
     1
/**
nkeynes@88
     2
 * $Id: loader.c,v 1.9 2006-01-22 22:41:40 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@25
    25
#include "gui/gui.h"
nkeynes@26
    26
#include "mem.h"
nkeynes@1
    27
#include "sh4core.h"
nkeynes@26
    28
#include "bootstrap.h"
nkeynes@1
    29
nkeynes@88
    30
char *bootstrap_file = DEFAULT_BOOTSTRAP_FILE;
nkeynes@26
    31
char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
nkeynes@1
    32
char iso_magic[6] = "\001CD001";
nkeynes@26
    33
char *file_loader_extensions[][2] = { 
nkeynes@26
    34
    { "sbi", "Self Boot Inducer" },
nkeynes@26
    35
    { "bin", "SH4 Bin file" },
nkeynes@26
    36
    { NULL, NULL } };
nkeynes@26
    37
nkeynes@26
    38
#define BOOTSTRAP_LOAD_ADDR 0x8C008000
nkeynes@26
    39
#define BOOTSTRAP_SIZE 32768
nkeynes@26
    40
nkeynes@26
    41
#define BINARY_LOAD_ADDR 0x8C010000
nkeynes@1
    42
nkeynes@1
    43
#define CDI_V2 0x80000004
nkeynes@1
    44
#define CDI_V3 0x80000005
nkeynes@1
    45
nkeynes@26
    46
gboolean file_load_magic( const gchar *filename )
nkeynes@1
    47
{
nkeynes@1
    48
    char buf[32];
nkeynes@1
    49
    uint32_t tmpa[2];
nkeynes@1
    50
    struct stat st;
nkeynes@1
    51
    
nkeynes@1
    52
    int fd = open( filename, O_RDONLY );
nkeynes@1
    53
    if( fd == -1 ) {
nkeynes@1
    54
        ERROR( "Unable to open file: '%s' (%s)", filename,
nkeynes@1
    55
               strerror(errno) );
nkeynes@26
    56
        return FALSE;
nkeynes@1
    57
    }
nkeynes@88
    58
    
nkeynes@1
    59
    fstat( fd, &st );
nkeynes@88
    60
    /*
nkeynes@1
    61
    if( st.st_size < 32768 ) {
nkeynes@1
    62
        ERROR( "File '%s' too small to be a dreamcast image", filename );
nkeynes@1
    63
        close(fd);
nkeynes@26
    64
        return FALSE;
nkeynes@1
    65
    }
nkeynes@88
    66
    */
nkeynes@1
    67
    
nkeynes@1
    68
    /* begin magic */
nkeynes@1
    69
    if( read( fd, buf, 32 ) != 32 ) {
nkeynes@1
    70
        ERROR( "Unable to read from file '%s'", filename );
nkeynes@1
    71
        close(fd);
nkeynes@26
    72
        return FALSE;
nkeynes@1
    73
    }
nkeynes@26
    74
    if( memcmp( buf, bootstrap_magic, 32 ) == 0 ) {
nkeynes@1
    75
        /* we have a DC bootstrap */
nkeynes@1
    76
        if( st.st_size == BOOTSTRAP_SIZE ) {
nkeynes@1
    77
            char *load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
nkeynes@1
    78
            lseek( fd, 0, SEEK_SET );
nkeynes@1
    79
            read( fd, load, BOOTSTRAP_SIZE );
nkeynes@26
    80
            bootstrap_dump( load );
nkeynes@1
    81
            sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
nkeynes@27
    82
            gtk_gui_update();
nkeynes@1
    83
        } else {
nkeynes@1
    84
            /* look for a valid ISO9660 header */
nkeynes@1
    85
            lseek( fd, 32768, SEEK_SET );
nkeynes@1
    86
            read( fd, buf, 8 );
nkeynes@1
    87
            if( memcmp( buf, iso_magic, 6 ) == 0 ) {
nkeynes@1
    88
                /* Alright, got it */
nkeynes@1
    89
                INFO( "Loading ISO9660 filesystem from '%s'",
nkeynes@1
    90
                      filename );
nkeynes@1
    91
            }
nkeynes@1
    92
        }
nkeynes@26
    93
    } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
nkeynes@26
    94
	/* ZIP file, aka SBI file */
nkeynes@26
    95
	WARN( "SBI files not supported yet" );
nkeynes@88
    96
    } else if( memcpy( buf, "\x7fELF", 4 ) == 0 ) {
nkeynes@88
    97
	/* ELF binary */
nkeynes@88
    98
	WARN( "ELF files not supported yet" );
nkeynes@1
    99
    } else {
nkeynes@26
   100
	/* Assume raw binary */
nkeynes@26
   101
	file_load_binary( filename );
nkeynes@26
   102
    } 
nkeynes@1
   103
    close(fd);
nkeynes@26
   104
    return TRUE;
nkeynes@1
   105
}
nkeynes@19
   106
nkeynes@26
   107
int file_load_binary( const gchar *filename ) {
nkeynes@88
   108
    /* Load the binary itself */
nkeynes@26
   109
    mem_load_block( filename, BINARY_LOAD_ADDR, -1 );
nkeynes@88
   110
    if( bootstrap_file != NULL ) {
nkeynes@88
   111
	/* Load in a bootstrap before the binary, to initialize everything
nkeynes@88
   112
	 * correctly
nkeynes@88
   113
	 */
nkeynes@88
   114
	mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE );
nkeynes@88
   115
	sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
nkeynes@88
   116
    } else {
nkeynes@88
   117
	sh4_set_pc( BINARY_LOAD_ADDR );
nkeynes@88
   118
    }
nkeynes@27
   119
    gtk_gui_update();
nkeynes@19
   120
}
.