1 // vim:shiftwidth=2:expandtab
11 #include <sys/ioctl.h>
15 #include "realfuncs.h"
24 #define UNUSED __attribute__((unused))
26 static const struct dev_fd_t takeover_devs[] = {
27 { "/dev/mem", FAKEDEV_MEM },
28 { "/dev/GPIO", FAKEDEV_GPIO },
29 { "/dev/fb0", FAKEDEV_FB0 },
30 { "/dev/fb/0", FAKEDEV_FB0 },
31 { "/dev/fb1", FAKEDEV_FB1 },
32 { "/dev/fb/1", FAKEDEV_FB1 },
33 { "/dev/mmuhack", FAKEDEV_MMUHACK },
34 { "/dev/tty", -1 }, // XXX hmh..
35 { "/dev/tty0", FAKEDEV_TTY0 },
38 static int w_open(const char *pathname, int flags, mode_t mode)
42 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
43 if (strcmp(pathname, takeover_devs[i].name) == 0) {
44 ret = takeover_devs[i].fd;
49 if (i == ARRAY_SIZE(takeover_devs))
50 ret = open(pathname, flags, mode);
53 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
54 if (strcmp(pathname, emu_interesting_fds[i].name) == 0) {
55 emu_interesting_fds[i].fd = ret;
61 strace("open(%s) = %d\n", pathname, ret);
65 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
68 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
69 ret = emu_do_mmap(length, prot, flags, fd, offset);
71 ret = mmap(addr, length, prot, flags, fd, offset);
73 // threads are using heap before they mmap their stack
74 // printf needs valid stack for pthread/errno
75 if (((long)&ret & 0xf0000000) == 0xb0000000)
76 strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
79 #define w_mmap2 w_mmap
81 static ssize_t w_read(int fd, void *buf, size_t count)
84 if (fd == FAKEDEV_GPIO)
85 ret = emu_read_gpiodev(buf, count);
87 ret = read(fd, buf, count);
89 //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
93 static int w_ioctl(int fd, int request, void *argp)
97 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
98 fd == emu_interesting_fds[IFD_SOUND].fd)
99 ret = emu_do_ioctl(fd, request, argp);
101 ret = ioctl(fd, request, argp);
103 strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
107 static int w_sigaction(int signum, const void *act, void *oldact)
109 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
114 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
117 if (fd != FAKEDEV_TTY0)
118 ret = tcgetattr(fd, termios_p);
122 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
126 static UNUSED int w_tcsetattr(int fd, int optional_actions,
127 const struct termios *termios_p)
130 if (fd != FAKEDEV_TTY0)
131 ret = tcsetattr(fd, optional_actions, termios_p);
135 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
139 static UNUSED FILE *w_fopen(const char *path, const char *mode)
141 FILE *ret = emu_do_fopen(path, mode);
142 strace("fopen(%s, %s) = %p\n", path, mode, ret);
146 static UNUSED int w_system(const char *command)
148 int ret = emu_do_system(command);
149 strace("system(%s) = %d\n", command, ret);
153 // 4 functions bellow are efforts to prevent gp2xmenu from being started..
154 static UNUSED int w_execl(const char *path, const char *arg, ...)
156 // don't allow exec (for now)
157 strace("execl(%s, %s, ...) = ?\n", path, arg);
161 static UNUSED int w_execlp(const char *file, const char *arg, ...)
163 strace("execlp(%s, %s, ...) = ?\n", file, arg);
167 // static note: this can't safely return because of the way it's patched in
168 // static note2: can't be used, execve hangs?
169 static UNUSED int w_execve(const char *filename, char *const argv[],
172 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
173 if (filename != NULL && strstr(filename, "/gp2xmenu") != NULL)
175 return execve(filename, argv, envp);
178 static int w_chdir(const char *path)
181 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
185 strace("chdir(%s) = %d\n", path, ret);
205 #define MAKE_WRAP_SYM_N(sym) \
206 /* alias wrap symbols to real names */ \
207 typeof(sym) sym __attribute__((alias("w_" #sym)))
209 #define MAKE_WRAP_SYM(sym) \
210 MAKE_WRAP_SYM_N(sym); \
211 /* wrapper to real functions, to be set up on load */ \
212 static typeof(sym) *p_real_##sym
214 // note: update symver too
216 MAKE_WRAP_SYM(fopen);
219 MAKE_WRAP_SYM(ioctl);
220 MAKE_WRAP_SYM(sigaction);
221 MAKE_WRAP_SYM(tcgetattr);
222 MAKE_WRAP_SYM(tcsetattr);
223 MAKE_WRAP_SYM(system);
224 MAKE_WRAP_SYM_N(execl);
225 MAKE_WRAP_SYM_N(execlp);
226 MAKE_WRAP_SYM(execve);
227 MAKE_WRAP_SYM(chdir);
228 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
230 #define REAL_FUNC_NP(name) \
231 { #name, (void **)&p_real_##name }
233 static const struct {
236 } real_funcs_np[] = {
242 REAL_FUNC_NP(sigaction),
243 REAL_FUNC_NP(tcgetattr),
244 REAL_FUNC_NP(tcsetattr),
245 REAL_FUNC_NP(system),
247 REAL_FUNC_NP(execve),
251 #define open p_real_open
252 #define fopen p_real_fopen
253 #define mmap p_real_mmap
254 #define read p_real_read
255 #define ioctl p_real_ioctl
256 #define sigaction p_real_sigaction
257 #define tcgetattr p_real_tcgetattr
258 #define tcsetattr p_real_tcsetattr
259 #define system p_real_system
260 #define execve p_real_execve
261 #define chdir p_real_chdir
268 // just call real funcs for static, pointer for dynamic
269 int real_open(const char *pathname, int flags, ...)
275 mode = va_arg(ap, mode_t);
277 return open(pathname, flags, mode);
280 FILE *real_fopen(const char *path, const char *mode)
282 return fopen(path, mode);
285 void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
287 return mmap(addr, length, prot, flags, fd, offset);
290 int real_read(int fd, void *buf, size_t count)
292 return read(fd, buf, count);
295 int real_ioctl(int d, int request, void *argp)
297 return ioctl(d, request, argp);
300 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
302 return sigaction(signum, act, oldact);
305 int real_tcgetattr(int fd, struct termios *termios_p)
307 return tcgetattr(fd, termios_p);
310 int real_tcsetattr(int fd, int optional_actions,
311 const struct termios *termios_p)
313 return tcsetattr(fd, optional_actions, termios_p);
316 int real_system(const char *command)
318 return system(command);
321 // real_exec* is missing intentionally - we don't need them
323 int real_execve(const char *filename, char *const argv[],
326 return execve(filename, argv, envp);
329 int real_chdir(const char *path)