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