Search
lxdream.org :: lxdream/src/loader.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 1:eea311cfd33e
next2:42349f6ea216
author nkeynes
date Sat Mar 13 00:03:32 2004 +0000 (15 years ago)
permissions -rw-r--r--
last change This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches.
file annotate diff log raw
nkeynes@1
     1
/*
nkeynes@1
     2
 *
nkeynes@1
     3
 */
nkeynes@1
     4
nkeynes@1
     5
#include <stdio.h>
nkeynes@1
     6
#include <fcntl.h>
nkeynes@1
     7
#include <sys/stat.h>
nkeynes@1
     8
#include <sys/mman.h>
nkeynes@1
     9
#include <errno.h>
nkeynes@1
    10
#include <stdint.h>
nkeynes@1
    11
#include "gui.h"
nkeynes@1
    12
#include "ipbin.h"
nkeynes@1
    13
#include "sh4core.h"
nkeynes@1
    14
#include "mem.h"
nkeynes@1
    15
nkeynes@1
    16
char ip_bin_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
nkeynes@1
    17
char iso_magic[6] = "\001CD001";
nkeynes@1
    18
nkeynes@1
    19
#define CDI_V2 0x80000004
nkeynes@1
    20
#define CDI_V3 0x80000005
nkeynes@1
    21
nkeynes@1
    22
void open_file( char *filename )
nkeynes@1
    23
{
nkeynes@1
    24
    char buf[32];
nkeynes@1
    25
    uint32_t tmpa[2];
nkeynes@1
    26
    struct stat st;
nkeynes@1
    27
    
nkeynes@1
    28
    int fd = open( filename, O_RDONLY );
nkeynes@1
    29
    if( fd == -1 ) {
nkeynes@1
    30
        ERROR( "Unable to open file: '%s' (%s)", filename,
nkeynes@1
    31
               strerror(errno) );
nkeynes@1
    32
        return;
nkeynes@1
    33
    }
nkeynes@1
    34
nkeynes@1
    35
    fstat( fd, &st );
nkeynes@1
    36
    if( st.st_size < 32768 ) {
nkeynes@1
    37
        ERROR( "File '%s' too small to be a dreamcast image", filename );
nkeynes@1
    38
        close(fd);
nkeynes@1
    39
        return;
nkeynes@1
    40
    }
nkeynes@1
    41
    
nkeynes@1
    42
    /* begin magic */
nkeynes@1
    43
    if( read( fd, buf, 32 ) != 32 ) {
nkeynes@1
    44
        ERROR( "Unable to read from file '%s'", filename );
nkeynes@1
    45
        close(fd);
nkeynes@1
    46
        return;
nkeynes@1
    47
    }
nkeynes@1
    48
    if( memcmp( buf, ip_bin_magic, 32 ) == 0 ) {
nkeynes@1
    49
        /* we have a DC bootstrap */
nkeynes@1
    50
        if( st.st_size == BOOTSTRAP_SIZE ) {
nkeynes@1
    51
            char *load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
nkeynes@1
    52
            /* Just the bootstrap, no image... */
nkeynes@1
    53
            WARN( "File '%s' contains bootstrap only, loading anyway",
nkeynes@1
    54
                  filename );
nkeynes@1
    55
            lseek( fd, 0, SEEK_SET );
nkeynes@1
    56
            read( fd, load, BOOTSTRAP_SIZE );
nkeynes@1
    57
            parse_ipbin( load );
nkeynes@1
    58
            sh4_reset();
nkeynes@1
    59
            sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
nkeynes@1
    60
            set_disassembly_region( BOOTSTRAP_LOAD_ADDR );
nkeynes@1
    61
            set_disassembly_pc( sh4r.pc, TRUE );
nkeynes@1
    62
            update_registers();
nkeynes@1
    63
        } else {
nkeynes@1
    64
            /* look for a valid ISO9660 header */
nkeynes@1
    65
            lseek( fd, 32768, SEEK_SET );
nkeynes@1
    66
            read( fd, buf, 8 );
nkeynes@1
    67
            if( memcmp( buf, iso_magic, 6 ) == 0 ) {
nkeynes@1
    68
                /* Alright, got it */
nkeynes@1
    69
                INFO( "Loading ISO9660 filesystem from '%s'",
nkeynes@1
    70
                      filename );
nkeynes@1
    71
            }
nkeynes@1
    72
        }
nkeynes@1
    73
    } else {
nkeynes@1
    74
        /* check if it's a CDI file: */
nkeynes@1
    75
        lseek( fd, -8, SEEK_END );
nkeynes@1
    76
        read( fd, &tmpa, 8 );
nkeynes@1
    77
        if( tmpa[0] == CDI_V2 || tmpa[0] == CDI_V3 ) {
nkeynes@1
    78
            /* Yup, it is */
nkeynes@1
    79
            INFO( "Loading CDI file '%s'", filename );
nkeynes@1
    80
        } else {
nkeynes@1
    81
            ERROR( "Don't know what to do with '%s'", filename );
nkeynes@1
    82
        }
nkeynes@1
    83
    }
nkeynes@1
    84
    close(fd);
nkeynes@1
    85
}
.