Search
lxdream.org :: lxdream/src/loader.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 736:a02d1475ccfd
prev561:533f6b478071
next825:2ac7ceccd775
author nkeynes
date Mon Jul 21 00:08:34 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Add gettext.h and build sanely without libintl if it's not available
Remove x86dasm's config.h & opintl.h (no longer needed and actually wrong)
file annotate diff log raw
1.1 --- a/src/loader.c Tue Jan 01 05:08:38 2008 +0000
1.2 +++ b/src/loader.c Mon Jul 21 00:08:34 2008 +0000
1.3 @@ -19,6 +19,7 @@
1.4
1.5 #include <unistd.h>
1.6 #include <stdio.h>
1.7 +#include <string.h>
1.8 #include <fcntl.h>
1.9 #include <sys/stat.h>
1.10 #include <errno.h>
1.11 @@ -33,9 +34,9 @@
1.12 char bootstrap_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
1.13 char iso_magic[6] = "\001CD001";
1.14 char *file_loader_extensions[][2] = {
1.15 - { "sbi", "Self Boot Inducer" },
1.16 - { "bin", "SH4 Bin file" },
1.17 - { NULL, NULL } };
1.18 + { "sbi", "Self Boot Inducer" },
1.19 + { "bin", "SH4 Bin file" },
1.20 + { NULL, NULL } };
1.21
1.22 #define BOOTSTRAP_LOAD_ADDR 0x8C008000
1.23 #define BOOTSTRAP_SIZE 32768
1.24 @@ -53,14 +54,14 @@
1.25 char buf[32];
1.26 struct stat st;
1.27 gboolean result = TRUE;
1.28 -
1.29 +
1.30 int fd = open( filename, O_RDONLY );
1.31 if( fd == -1 ) {
1.32 return FALSE;
1.33 }
1.34 -
1.35 +
1.36 fstat( fd, &st );
1.37 -
1.38 +
1.39 /* begin magic */
1.40 if( read( fd, buf, 32 ) != 32 ) {
1.41 ERROR( "Unable to read from file '%s'", filename );
1.42 @@ -74,7 +75,7 @@
1.43 lseek( fd, 0, SEEK_SET );
1.44 read( fd, load, BOOTSTRAP_SIZE );
1.45 bootstrap_dump( load, TRUE );
1.46 - dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR + 0x300 );
1.47 + dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR + 0x300 );
1.48 } else {
1.49 /* look for a valid ISO9660 header */
1.50 lseek( fd, 32768, SEEK_SET );
1.51 @@ -82,23 +83,23 @@
1.52 if( memcmp( buf, iso_magic, 6 ) == 0 ) {
1.53 /* Alright, got it */
1.54 INFO( "Loading ISO9660 filesystem from '%s'",
1.55 - filename );
1.56 + filename );
1.57 }
1.58 }
1.59 } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
1.60 - /* ZIP file, aka SBI file */
1.61 - WARN( "SBI files not supported yet" );
1.62 - result = FALSE;
1.63 + /* ZIP file, aka SBI file */
1.64 + WARN( "SBI files not supported yet" );
1.65 + result = FALSE;
1.66 } else if( memcmp( buf, DREAMCAST_SAVE_MAGIC, 16 ) == 0 ) {
1.67 - /* Save state */
1.68 - result = (dreamcast_load_state( filename )==0);
1.69 + /* Save state */
1.70 + result = (dreamcast_load_state( filename )==0);
1.71 } else if( buf[0] == 0x7F && buf[1] == 'E' &&
1.72 - buf[2] == 'L' && buf[3] == 'F' ) {
1.73 - /* ELF binary */
1.74 - lseek( fd, 0, SEEK_SET );
1.75 - result = file_load_elf_fd( filename, fd );
1.76 + buf[2] == 'L' && buf[3] == 'F' ) {
1.77 + /* ELF binary */
1.78 + lseek( fd, 0, SEEK_SET );
1.79 + result = file_load_elf_fd( filename, fd );
1.80 } else {
1.81 - result = FALSE;
1.82 + result = FALSE;
1.83 }
1.84 close(fd);
1.85 return result;
1.86 @@ -108,13 +109,13 @@
1.87 {
1.88 const gchar *bootstrap_file = lxdream_get_config_value(CONFIG_BOOTSTRAP);
1.89 if( bootstrap_file != NULL ) {
1.90 - /* Load in a bootstrap before the binary, to initialize everything
1.91 - * correctly
1.92 - */
1.93 + /* Load in a bootstrap before the binary, to initialize everything
1.94 + * correctly
1.95 + */
1.96 if( mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE ) == 0 ) {
1.97 - dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR+0x300 );
1.98 - return;
1.99 - }
1.100 + dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR+0x300 );
1.101 + return;
1.102 + }
1.103 }
1.104 dreamcast_program_loaded( filename, pc );
1.105 }
1.106 @@ -124,10 +125,10 @@
1.107 {
1.108 /* Load the binary itself */
1.109 if( mem_load_block( filename, BINARY_LOAD_ADDR, -1 ) == 0 ) {
1.110 - file_load_postload( filename, BINARY_LOAD_ADDR );
1.111 - return TRUE;
1.112 + file_load_postload( filename, BINARY_LOAD_ADDR );
1.113 + return TRUE;
1.114 } else {
1.115 - return FALSE;
1.116 + return FALSE;
1.117 }
1.118 }
1.119
1.120 @@ -138,32 +139,32 @@
1.121 int i;
1.122
1.123 if( read( fd, &head, sizeof(head) ) != sizeof(head) )
1.124 - return FALSE;
1.125 + return FALSE;
1.126 if( head.e_ident[EI_CLASS] != ELFCLASS32 ||
1.127 - head.e_ident[EI_DATA] != ELFDATA2LSB ||
1.128 - head.e_ident[EI_VERSION] != 1 ||
1.129 - head.e_type != ET_EXEC ||
1.130 - head.e_machine != EM_SH ||
1.131 - head.e_version != 1 ) {
1.132 - ERROR( "File is not an SH4 ELF executable file" );
1.133 - return FALSE;
1.134 + head.e_ident[EI_DATA] != ELFDATA2LSB ||
1.135 + head.e_ident[EI_VERSION] != 1 ||
1.136 + head.e_type != ET_EXEC ||
1.137 + head.e_machine != EM_SH ||
1.138 + head.e_version != 1 ) {
1.139 + ERROR( "File is not an SH4 ELF executable file" );
1.140 + return FALSE;
1.141 }
1.142
1.143 /* Program headers */
1.144 for( i=0; i<head.e_phnum; i++ ) {
1.145 - lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
1.146 - read( fd, &phdr, sizeof(phdr) );
1.147 - if( phdr.p_type == PT_LOAD ) {
1.148 - lseek( fd, phdr.p_offset, SEEK_SET );
1.149 - sh4ptr_t target = mem_get_region( phdr.p_vaddr );
1.150 - read( fd, target, phdr.p_filesz );
1.151 - if( phdr.p_memsz > phdr.p_filesz ) {
1.152 - memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
1.153 - }
1.154 - INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
1.155 - }
1.156 + lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
1.157 + read( fd, &phdr, sizeof(phdr) );
1.158 + if( phdr.p_type == PT_LOAD ) {
1.159 + lseek( fd, phdr.p_offset, SEEK_SET );
1.160 + sh4ptr_t target = mem_get_region( phdr.p_vaddr );
1.161 + read( fd, target, phdr.p_filesz );
1.162 + if( phdr.p_memsz > phdr.p_filesz ) {
1.163 + memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
1.164 + }
1.165 + INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
1.166 + }
1.167 }
1.168 -
1.169 +
1.170 file_load_postload( filename, head.e_entry );
1.171 return TRUE;
1.172 }
.