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