Search
lxdream.org :: lxdream/src/loader.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 1095:a8b798030464
prev1075:1a21750d300c
next1100:50e702af9373
author nkeynes
date Sun Jan 31 18:28:24 2010 +1000 (14 years ago)
permissions -rw-r--r--
last change Move bootstrap structure and defines into bootstrap.h
view annotate diff log raw
     1 /**
     2  * $Id$
     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 <unistd.h>
    21 #include <stdio.h>
    22 #include <string.h>
    23 #include <fcntl.h>
    24 #include <sys/stat.h>
    25 #include <errno.h>
    26 #include <stdint.h>
    27 #include <elf.h>
    28 #include "mem.h"
    29 #include "bootstrap.h"
    30 #include "dreamcast.h"
    31 #include "config.h"
    32 #include "loader.h"
    34 char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
    35 char iso_magic[6] = "\001CD001";
    36 char *file_loader_extensions[][2] = { 
    37         { "sbi", "Self Boot Inducer" },
    38         { "bin", "SH4 Bin file" },
    39         { NULL, NULL } };
    41 #define CDI_V2 0x80000004
    42 #define CDI_V3 0x80000005
    44 gboolean file_load_elf_fd( const gchar *filename, int fd );
    47 gboolean file_load_magic( const gchar *filename )
    48 {
    49     char buf[32];
    50     struct stat st;
    51     gboolean result = TRUE;
    53     int fd = open( filename, O_RDONLY );
    54     if( fd == -1 ) {
    55         return FALSE;
    56     }
    58     fstat( fd, &st );
    60     /* begin magic */
    61     if( read( fd, buf, 32 ) != 32 ) {
    62         ERROR( "Unable to read from file '%s'", filename );
    63         close(fd);
    64         return FALSE;
    65     }
    66     if( memcmp( buf, bootstrap_magic, 32 ) == 0 ) {
    67         /* we have a DC bootstrap */
    68         if( st.st_size == BOOTSTRAP_SIZE ) {
    69             sh4ptr_t load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
    70             lseek( fd, 0, SEEK_SET );
    71             read( fd, load, BOOTSTRAP_SIZE );
    72             bootstrap_dump( load, TRUE );
    73             dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR + 0x300 );
    74         } else {
    75             /* look for a valid ISO9660 header */
    76             lseek( fd, 32768, SEEK_SET );
    77             read( fd, buf, 8 );
    78             if( memcmp( buf, iso_magic, 6 ) == 0 ) {
    79                 /* Alright, got it */
    80                 WARN( "ISO images not supported yet" );
    81             }
    82         }
    83     } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
    84         /* ZIP file, aka SBI file */
    85         WARN( "SBI files not supported yet" );
    86         result = FALSE;
    87     } else if( memcmp( buf, DREAMCAST_SAVE_MAGIC, 16 ) == 0 ) {
    88         /* Save state */
    89         result = (dreamcast_load_state( filename )==0);
    90     } else if( buf[0] == 0x7F && buf[1] == 'E' && 
    91             buf[2] == 'L' && buf[3] == 'F' ) {
    92         /* ELF binary */
    93         lseek( fd, 0, SEEK_SET );
    94         result = file_load_elf_fd( filename, fd );
    95     } else {
    96         result = FALSE;
    97     }
    98     close(fd);
    99     return result;
   100 }
   102 void file_load_postload( const gchar *filename, int pc )
   103 {
   104     gchar *bootstrap_file = lxdream_get_global_config_path_value(CONFIG_BOOTSTRAP);
   105     if( bootstrap_file != NULL && bootstrap_file[0] != '\0' ) {
   106         /* Load in a bootstrap before the binary, to initialize everything
   107          * correctly
   108          */
   109         if( mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE ) == 0 ) {
   110             dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR+0x300 );
   111             g_free(bootstrap_file);
   112             return;
   113         }
   114     }
   115     dreamcast_program_loaded( filename, pc );
   116     g_free(bootstrap_file);
   117 }    
   120 gboolean file_load_binary( const gchar *filename )
   121 {
   122     /* Load the binary itself */
   123     if(  mem_load_block( filename, BINARY_LOAD_ADDR, -1 ) == 0 ) {
   124         file_load_postload( filename, BINARY_LOAD_ADDR );
   125         return TRUE;
   126     } else {
   127         return FALSE;
   128     }
   129 }
   131 gboolean file_load_elf_fd( const gchar *filename, int fd ) 
   132 {
   133     Elf32_Ehdr head;
   134     Elf32_Phdr phdr;
   135     int i;
   137     if( read( fd, &head, sizeof(head) ) != sizeof(head) )
   138         return FALSE;
   139     if( head.e_ident[EI_CLASS] != ELFCLASS32 ||
   140             head.e_ident[EI_DATA] != ELFDATA2LSB ||
   141             head.e_ident[EI_VERSION] != 1 ||
   142             head.e_type != ET_EXEC ||
   143             head.e_machine != EM_SH ||
   144             head.e_version != 1 ) {
   145         ERROR( "File is not an SH4 ELF executable file" );
   146         return FALSE;
   147     }
   149     /* Program headers */
   150     for( i=0; i<head.e_phnum; i++ ) {
   151         lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
   152         read( fd, &phdr, sizeof(phdr) );
   153         if( phdr.p_type == PT_LOAD ) {
   154             lseek( fd, phdr.p_offset, SEEK_SET );
   155             sh4ptr_t target = mem_get_region( phdr.p_vaddr );
   156             read( fd, target, phdr.p_filesz );
   157             if( phdr.p_memsz > phdr.p_filesz ) {
   158                 memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
   159             }
   160             INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
   161         }
   162     }
   164     file_load_postload( filename, head.e_entry );
   165     return TRUE;
   166 }
.