Search
lxdream.org :: lxdream/src/loader.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 27:1ef09a52cd1e
prev26:ad258e3daaa5
next88:0a1bd43fa4ad
author nkeynes
date Sun Dec 25 08:24:11 2005 +0000 (14 years ago)
permissions -rw-r--r--
last change Finish adding header blocks to all files
view annotate diff log raw
     1 /**
     2  * $Id: loader.c,v 1.8 2005-12-25 01:28:36 nkeynes Exp $
     3  *
     4  * File loading routines, mostly for loading demos without going through the
     5  * whole procedure of making a CD image for them.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <stdio.h>
    21 #include <fcntl.h>
    22 #include <sys/stat.h>
    23 #include <errno.h>
    24 #include <stdint.h>
    25 #include "gui/gui.h"
    26 #include "mem.h"
    27 #include "sh4core.h"
    28 #include "bootstrap.h"
    30 char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
    31 char iso_magic[6] = "\001CD001";
    32 char *file_loader_extensions[][2] = { 
    33     { "sbi", "Self Boot Inducer" },
    34     { "bin", "SH4 Bin file" },
    35     { NULL, NULL } };
    37 #define BOOTSTRAP_LOAD_ADDR 0x8C008000
    38 #define BOOTSTRAP_SIZE 32768
    40 #define BINARY_LOAD_ADDR 0x8C010000
    42 #define CDI_V2 0x80000004
    43 #define CDI_V3 0x80000005
    45 gboolean file_load_magic( const gchar *filename )
    46 {
    47     char buf[32];
    48     uint32_t tmpa[2];
    49     struct stat st;
    51     int fd = open( filename, O_RDONLY );
    52     if( fd == -1 ) {
    53         ERROR( "Unable to open file: '%s' (%s)", filename,
    54                strerror(errno) );
    55         return FALSE;
    56     }
    58     fstat( fd, &st );
    59     if( st.st_size < 32768 ) {
    60         ERROR( "File '%s' too small to be a dreamcast image", filename );
    61         close(fd);
    62         return FALSE;
    63     }
    65     /* begin magic */
    66     if( read( fd, buf, 32 ) != 32 ) {
    67         ERROR( "Unable to read from file '%s'", filename );
    68         close(fd);
    69         return FALSE;
    70     }
    71     if( memcmp( buf, bootstrap_magic, 32 ) == 0 ) {
    72         /* we have a DC bootstrap */
    73         if( st.st_size == BOOTSTRAP_SIZE ) {
    74             char *load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
    75             lseek( fd, 0, SEEK_SET );
    76             read( fd, load, BOOTSTRAP_SIZE );
    77             bootstrap_dump( load );
    78             sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
    79             gtk_gui_update();
    80         } else {
    81             /* look for a valid ISO9660 header */
    82             lseek( fd, 32768, SEEK_SET );
    83             read( fd, buf, 8 );
    84             if( memcmp( buf, iso_magic, 6 ) == 0 ) {
    85                 /* Alright, got it */
    86                 INFO( "Loading ISO9660 filesystem from '%s'",
    87                       filename );
    88             }
    89         }
    90     } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
    91 	/* ZIP file, aka SBI file */
    92 	WARN( "SBI files not supported yet" );
    93     } else {
    94 	/* Assume raw binary */
    95 	file_load_binary( filename );
    96     } 
    97     close(fd);
    98     return TRUE;
    99 }
   101 int file_load_binary( const gchar *filename ) {
   102     mem_load_block( filename, BINARY_LOAD_ADDR, -1 );
   103     sh4_set_pc( BINARY_LOAD_ADDR );
   104     gtk_gui_update();
   105 }
.