2 * $Id: dcload.c,v 1.6 2006-08-07 13:18:16 nkeynes Exp $
4 * DC-load syscall implementation.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 #include "dreamcast.h"
24 #include "sh4/sh4core.h"
40 #define SYS_ASSIGNWRKMEM 14
42 #define SYS_OPENDIR 16
43 #define SYS_CLOSEDIR 17
44 #define SYS_READDIR 18
45 #define SYS_GETHOSTINFO 19
47 #define SYS_MAGIC 0xDEADBEEF
48 #define SYS_MAGIC_ADDR 0x8c004004
49 #define SYSCALL_ADDR 0x8c004008
51 static gboolean dcload_allow_unsafe = FALSE;
53 void dcload_set_allow_unsafe( gboolean allow )
55 dcload_allow_unsafe = allow;
58 #define MAX_OPEN_FDS 16
60 * Mapping from emulator fd to real fd (so we can limit read/write
61 * to only fds we've explicitly granted access to).
63 int open_fds[MAX_OPEN_FDS];
68 for( i=0; i<MAX_OPEN_FDS; i++ ) {
69 if( open_fds[i] == -1 ) {
76 void dcload_syscall( uint32_t syscall_id )
78 uint32_t syscall = sh4r.r[4];
83 if( fd < 0 || fd >= MAX_OPEN_FDS || open_fds[fd] == -1 ) {
86 char *buf = mem_get_region( sh4r.r[6] );
87 int length = sh4r.r[7];
88 sh4r.r[0] = read( open_fds[fd], buf, length );
93 if( fd < 0 || fd >= MAX_OPEN_FDS || open_fds[fd] == -1 ) {
96 char *buf = mem_get_region( sh4r.r[6] );
97 int length = sh4r.r[7];
98 sh4r.r[0] = write( open_fds[fd], buf, length );
103 if( fd < 0 || fd >= MAX_OPEN_FDS || open_fds[fd] == -1 ) {
106 sh4r.r[0] = lseek( open_fds[fd], sh4r.r[6], sh4r.r[7] );
110 /* Secure access only */
112 if( dcload_allow_unsafe ) {
113 fd = dcload_alloc_fd();
117 char *filename = mem_get_region( sh4r.r[5] );
118 int realfd = open( filename, sh4r.r[6] );
119 open_fds[fd] = realfd;
123 ERROR( "Denying access to local filesystem" );
128 if( dcload_allow_unsafe ) {
130 if( fd < 0 || fd >= MAX_OPEN_FDS || open_fds[fd] == -1 ) {
133 if( open_fds[fd] > 2 ) {
134 sh4r.r[0] = close( open_fds[fd] );
136 /* Don't actually close real fds 0-2 */
144 if( dcload_allow_unsafe ) {
155 void dcload_install()
157 memset( &open_fds, -1, sizeof(open_fds) );
161 syscall_add_hook_vector( 0xF0, SYSCALL_ADDR, dcload_syscall );
162 sh4_write_long( SYS_MAGIC_ADDR, SYS_MAGIC );
.