filename | src/loader.c |
changeset | 1036:af7b0c5905dd |
prev | 825:2ac7ceccd775 |
next | 1075:1a21750d300c |
author | nkeynes |
date | Wed Jun 24 06:06:40 2009 +0000 (13 years ago) |
permissions | -rw-r--r-- |
last change | Support shell substitutions in config paths Keep track of last folder in file dialogs Fix out-of-dateness in GTK path dialog |
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 BOOTSTRAP_LOAD_ADDR 0x8C008000
42 #define BOOTSTRAP_SIZE 32768
44 #define BINARY_LOAD_ADDR 0x8C010000
46 #define CDI_V2 0x80000004
47 #define CDI_V3 0x80000005
49 gboolean file_load_elf_fd( const gchar *filename, int fd );
52 gboolean file_load_magic( const gchar *filename )
53 {
54 char buf[32];
55 struct stat st;
56 gboolean result = TRUE;
58 int fd = open( filename, O_RDONLY );
59 if( fd == -1 ) {
60 return FALSE;
61 }
63 fstat( fd, &st );
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 sh4ptr_t load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
75 lseek( fd, 0, SEEK_SET );
76 read( fd, load, BOOTSTRAP_SIZE );
77 bootstrap_dump( load, TRUE );
78 dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR + 0x300 );
79 } else {
80 /* look for a valid ISO9660 header */
81 lseek( fd, 32768, SEEK_SET );
82 read( fd, buf, 8 );
83 if( memcmp( buf, iso_magic, 6 ) == 0 ) {
84 /* Alright, got it */
85 INFO( "Loading ISO9660 filesystem from '%s'",
86 filename );
87 }
88 }
89 } else if( memcmp( buf, "PK\x03\x04", 4 ) == 0 ) {
90 /* ZIP file, aka SBI file */
91 WARN( "SBI files not supported yet" );
92 result = FALSE;
93 } else if( memcmp( buf, DREAMCAST_SAVE_MAGIC, 16 ) == 0 ) {
94 /* Save state */
95 result = (dreamcast_load_state( filename )==0);
96 } else if( buf[0] == 0x7F && buf[1] == 'E' &&
97 buf[2] == 'L' && buf[3] == 'F' ) {
98 /* ELF binary */
99 lseek( fd, 0, SEEK_SET );
100 result = file_load_elf_fd( filename, fd );
101 } else {
102 result = FALSE;
103 }
104 close(fd);
105 return result;
106 }
108 void file_load_postload( const gchar *filename, int pc )
109 {
110 gchar *bootstrap_file = lxdream_get_global_config_path_value(CONFIG_BOOTSTRAP);
111 if( bootstrap_file != NULL && bootstrap_file[0] != '\0' ) {
112 /* Load in a bootstrap before the binary, to initialize everything
113 * correctly
114 */
115 if( mem_load_block( bootstrap_file, BOOTSTRAP_LOAD_ADDR, BOOTSTRAP_SIZE ) == 0 ) {
116 dreamcast_program_loaded( filename, BOOTSTRAP_LOAD_ADDR+0x300 );
117 g_free(bootstrap_file);
118 return;
119 }
120 }
121 dreamcast_program_loaded( filename, pc );
122 g_free(bootstrap_file);
123 }
126 gboolean file_load_binary( const gchar *filename )
127 {
128 /* Load the binary itself */
129 if( mem_load_block( filename, BINARY_LOAD_ADDR, -1 ) == 0 ) {
130 file_load_postload( filename, BINARY_LOAD_ADDR );
131 return TRUE;
132 } else {
133 return FALSE;
134 }
135 }
137 gboolean file_load_elf_fd( const gchar *filename, int fd )
138 {
139 Elf32_Ehdr head;
140 Elf32_Phdr phdr;
141 int i;
143 if( read( fd, &head, sizeof(head) ) != sizeof(head) )
144 return FALSE;
145 if( head.e_ident[EI_CLASS] != ELFCLASS32 ||
146 head.e_ident[EI_DATA] != ELFDATA2LSB ||
147 head.e_ident[EI_VERSION] != 1 ||
148 head.e_type != ET_EXEC ||
149 head.e_machine != EM_SH ||
150 head.e_version != 1 ) {
151 ERROR( "File is not an SH4 ELF executable file" );
152 return FALSE;
153 }
155 /* Program headers */
156 for( i=0; i<head.e_phnum; i++ ) {
157 lseek( fd, head.e_phoff + i*head.e_phentsize, SEEK_SET );
158 read( fd, &phdr, sizeof(phdr) );
159 if( phdr.p_type == PT_LOAD ) {
160 lseek( fd, phdr.p_offset, SEEK_SET );
161 sh4ptr_t target = mem_get_region( phdr.p_vaddr );
162 read( fd, target, phdr.p_filesz );
163 if( phdr.p_memsz > phdr.p_filesz ) {
164 memset( target + phdr.p_filesz, 0, phdr.p_memsz - phdr.p_filesz );
165 }
166 INFO( "Loaded %d bytes to %08X", phdr.p_filesz, phdr.p_vaddr );
167 }
168 }
170 file_load_postload( filename, head.e_entry );
171 return TRUE;
172 }
.