handle another palette write
[ginge.git] / loader / override.c
CommitLineData
499bf01c 1/*
2 * GINGE - GINGE Is Not Gp2x Emulator
b4f4cb40 3 * (C) notaz, 2010-2011,2016
499bf01c 4 *
5 * This work is licensed under the MAME license, see COPYING file for details.
6 */
7fd42181 7#include <stdio.h>
8#include <stdlib.h>
4d045184 9#include <stdarg.h>
7fd42181 10#include <string.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <sys/mman.h>
15#include <unistd.h>
16#include <sys/ioctl.h>
17#include <signal.h>
18#include <termios.h>
3ef07128 19#include <errno.h>
7fd42181 20
21#include "realfuncs.h"
b4f4cb40 22#include "syscalls.h"
23#include "llibc.h"
7fd42181 24#include "header.h"
25
26#if (DBG & 1)
b4f4cb40 27#define strace g_printf
7fd42181 28#else
29#define strace(...)
30#endif
31
32#define UNUSED __attribute__((unused))
33
34static const struct dev_fd_t takeover_devs[] = {
35 { "/dev/mem", FAKEDEV_MEM },
36 { "/dev/GPIO", FAKEDEV_GPIO },
37 { "/dev/fb0", FAKEDEV_FB0 },
38 { "/dev/fb/0", FAKEDEV_FB0 },
39 { "/dev/fb1", FAKEDEV_FB1 },
40 { "/dev/fb/1", FAKEDEV_FB1 },
41 { "/dev/mmuhack", FAKEDEV_MMUHACK },
ac8e92a4 42 { "/dev/tty", FAKEDEV_TTY0 },
7fd42181 43 { "/dev/tty0", FAKEDEV_TTY0 },
7000b522 44#ifdef PND
45 { "/dev/input/event*", -1 }, // hide for now, may cause dupe events
46#endif
7fd42181 47};
48
b4f4cb40 49static long w_open_raw(const char *pathname, int flags, mode_t mode)
7fd42181 50{
b4f4cb40 51 long ret;
52 int i;
7fd42181 53
54 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
7000b522 55 const char *p, *oname;
56 int len;
57
58 oname = takeover_devs[i].name;
59 p = strchr(oname, '*');
60 if (p != NULL)
61 len = p - oname;
62 else
63 len = strlen(oname) + 1;
64
65 if (strncmp(pathname, oname, len) == 0) {
7fd42181 66 ret = takeover_devs[i].fd;
67 break;
68 }
69 }
70
71 if (i == ARRAY_SIZE(takeover_devs))
b4f4cb40 72 ret = g_open_raw(pathname, flags, mode);
7fd42181 73
74 if (ret >= 0) {
75 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
7000b522 76 struct dev_fd_t *eifd = &emu_interesting_fds[i];
77 if (strcmp(pathname, eifd->name) == 0) {
78 eifd->fd = ret;
79 if (eifd->open_cb != NULL)
80 eifd->open_cb(ret);
7fd42181 81 break;
82 }
83 }
84 }
85
b4f4cb40 86 strace("open(%s) = %ld\n", pathname, ret);
7fd42181 87 return ret;
88}
89
b4f4cb40 90static int w_open(const char *pathname, int flags, mode_t mode)
91{
92 long ret = w_open_raw(pathname, flags, mode);
93 return g_syscall_error(ret);
94}
95
96static long w_mmap_raw(void *addr, size_t length, int prot, int flags,
97 int fd, off_t offset)
7fd42181 98{
b4f4cb40 99 long ret;
100
7fd42181 101 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
102 ret = emu_do_mmap(length, prot, flags, fd, offset);
103 else
b4f4cb40 104 ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
7fd42181 105
b4f4cb40 106 strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n",
107 addr, length, prot, flags, fd, (long)offset, ret);
7fd42181 108 return ret;
109}
b4f4cb40 110
111static long w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
112{
113 long ret = w_mmap_raw(addr, length, prot, flags, fd, offset);
114 return g_syscall_error(ret);
115}
7fd42181 116#define w_mmap2 w_mmap
117
b4f4cb40 118static long w_munmap_raw(void *addr, size_t length)
3ef07128 119{
b4f4cb40 120 long ret;
121
3ef07128 122 ret = emu_do_munmap(addr, length);
123 if (ret == -EAGAIN)
b4f4cb40 124 ret = g_munmap_raw(addr, length);
3ef07128 125
b4f4cb40 126 strace("munmap(%p, %x) = %ld\n", addr, length, ret);
3ef07128 127 return ret;
128}
129
b4f4cb40 130static int w_munmap(void *addr, size_t length)
131{
132 long ret = w_munmap_raw(addr, length);
133 return g_syscall_error(ret);
134}
135
136long w_read_raw(int fd, void *buf, size_t count)
7fd42181 137{
b4f4cb40 138 long ret;
139
7fd42181 140 if (fd == FAKEDEV_GPIO)
141 ret = emu_read_gpiodev(buf, count);
142 else
b4f4cb40 143 ret = g_read_raw(fd, buf, count);
7fd42181 144
b4f4cb40 145 //strace("read(%d, %p, %ld) = %ld\n", fd, buf, count, ret);
7fd42181 146 return ret;
147}
148
b4f4cb40 149static ssize_t w_read(int fd, void *buf, size_t count)
7fd42181 150{
b4f4cb40 151 long ret = w_read_raw(fd, buf, count);
152 return g_syscall_error(ret);
153}
154
155long w_ioctl_raw(int fd, int request, void *argp)
156{
157 long ret;
7fd42181 158
159 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
160 fd == emu_interesting_fds[IFD_SOUND].fd)
161 ret = emu_do_ioctl(fd, request, argp);
162 else
b4f4cb40 163 ret = g_ioctl_raw(fd, request, argp);
7fd42181 164
b4f4cb40 165 strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret);
7fd42181 166 return ret;
167}
168
b4f4cb40 169static int w_ioctl(int fd, int request, void *argp)
170{
171 long ret = w_ioctl_raw(fd, request, argp);
172 return g_syscall_error(ret);
173}
174
7fd42181 175static int w_sigaction(int signum, const void *act, void *oldact)
176{
177 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
178 return 0;
179}
180
181/* dynamic only */
182static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
183{
184 int ret;
185 if (fd != FAKEDEV_TTY0)
186 ret = tcgetattr(fd, termios_p);
187 else
188 ret = 0;
189
190 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
191 return ret;
192}
193
194static UNUSED int w_tcsetattr(int fd, int optional_actions,
195 const struct termios *termios_p)
196{
197 int ret;
198 if (fd != FAKEDEV_TTY0)
199 ret = tcsetattr(fd, optional_actions, termios_p);
200 else
201 ret = 0;
202
203 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
204 return ret;
205}
206
207static UNUSED FILE *w_fopen(const char *path, const char *mode)
208{
209 FILE *ret = emu_do_fopen(path, mode);
210 strace("fopen(%s, %s) = %p\n", path, mode, ret);
211 return ret;
212}
213
214static UNUSED int w_system(const char *command)
215{
216 int ret = emu_do_system(command);
217 strace("system(%s) = %d\n", command, ret);
218 return ret;
219}
220
7000b522 221extern char **environ;
222
db9191ed 223static UNUSED int w_execl(const char *path, const char *arg, ...)
224{
225 // don't allow exec (for now)
226 strace("execl(%s, %s, ...) = ?\n", path, arg);
7000b522 227 exit(1);
db9191ed 228}
229
230static UNUSED int w_execlp(const char *file, const char *arg, ...)
231{
232 strace("execlp(%s, %s, ...) = ?\n", file, arg);
7000b522 233 exit(1);
234}
235
236static UNUSED int w_execle(const char *path, const char *arg, ...)
237{
238 strace("execle(%s, %s, ...) = ?\n", path, arg);
239 exit(1);
240}
241
242static UNUSED int w_execv(const char *path, char *const argv[])
243{
244 strace("execv(%s, %p) = ?\n", path, argv);
245 return emu_do_execve(path, argv, environ);
246}
247
248static UNUSED int w_execvp(const char *file, char *const argv[])
249{
250 strace("execvp(%s, %p) = ?\n", file, argv);
251 return emu_do_execve(file, argv, environ);
db9191ed 252}
253
254// static note: this can't safely return because of the way it's patched in
255// static note2: can't be used, execve hangs?
256static UNUSED int w_execve(const char *filename, char *const argv[],
257 char *const envp[])
258{
259 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
7000b522 260 return emu_do_execve(filename, argv, envp);
db9191ed 261}
262
263static int w_chdir(const char *path)
264{
b4f4cb40 265 long ret;
266
db9191ed 267 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
268 ret = 0;
269 else
b4f4cb40 270 ret = g_chdir_raw(path);
c3831532 271
272 strace("chdir(%s) = %ld\n", path, ret);
273 return g_syscall_error(ret);
274}
275
276static ssize_t w_readlink(const char *path, char *buf, size_t bufsiz)
277{
278 long ret;
279
280 if (path != NULL && strncmp(path, "/proc/", 6) == 0
281 && strcmp(strrchr(path, '/'), "/exe") == 0)
282 {
283 ret = snprintf(buf, bufsiz, "%s", bin_path);
284 if (ret > bufsiz)
285 ret = bufsiz;
286 }
287 else
288 ret = g_readlink_raw(path, buf, bufsiz);
289
290 strace("readlink(%s, %s, %zd) = %ld\n", path, buf, bufsiz, ret);
b4f4cb40 291 return g_syscall_error(ret);
db9191ed 292}
293
7fd42181 294#undef open
295#undef fopen
296#undef mmap
3ef07128 297#undef munmap
7fd42181 298#undef read
299#undef ioctl
b4f4cb40 300#undef close
7fd42181 301#undef sigaction
302#undef tcgetattr
303#undef tcsetattr
304#undef system
db9191ed 305#undef execl
306#undef execlp
7000b522 307#undef execle
308#undef execv
309#undef execvp
db9191ed 310#undef execve
311#undef chdir
c3831532 312#undef readlink
7fd42181 313
314#ifdef DL
315
db9191ed 316#define MAKE_WRAP_SYM_N(sym) \
7fd42181 317 /* alias wrap symbols to real names */ \
db9191ed 318 typeof(sym) sym __attribute__((alias("w_" #sym)))
319
320#define MAKE_WRAP_SYM(sym) \
321 MAKE_WRAP_SYM_N(sym); \
7fd42181 322 /* wrapper to real functions, to be set up on load */ \
323 static typeof(sym) *p_real_##sym
324
4d045184 325// note: update symver too
b4f4cb40 326MAKE_WRAP_SYM_N(open);
7fd42181 327MAKE_WRAP_SYM(fopen);
b4f4cb40 328MAKE_WRAP_SYM_N(mmap);
329MAKE_WRAP_SYM_N(munmap);
330MAKE_WRAP_SYM_N(read);
331MAKE_WRAP_SYM_N(ioctl);
7fd42181 332MAKE_WRAP_SYM(sigaction);
333MAKE_WRAP_SYM(tcgetattr);
334MAKE_WRAP_SYM(tcsetattr);
335MAKE_WRAP_SYM(system);
db9191ed 336MAKE_WRAP_SYM_N(execl);
337MAKE_WRAP_SYM_N(execlp);
7000b522 338MAKE_WRAP_SYM_N(execle);
339MAKE_WRAP_SYM_N(execv);
340MAKE_WRAP_SYM_N(execvp);
db9191ed 341MAKE_WRAP_SYM(execve);
b4f4cb40 342MAKE_WRAP_SYM_N(chdir);
c3831532 343MAKE_WRAP_SYM_N(readlink);
7fd42181 344typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
345
346#define REAL_FUNC_NP(name) \
347 { #name, (void **)&p_real_##name }
348
349static const struct {
350 const char *name;
351 void **func_ptr;
352} real_funcs_np[] = {
b4f4cb40 353 //REAL_FUNC_NP(open),
7fd42181 354 REAL_FUNC_NP(fopen),
b4f4cb40 355 //REAL_FUNC_NP(mmap),
356 //REAL_FUNC_NP(munmap),
357 //REAL_FUNC_NP(read),
358 //REAL_FUNC_NP(ioctl),
7fd42181 359 REAL_FUNC_NP(sigaction),
360 REAL_FUNC_NP(tcgetattr),
361 REAL_FUNC_NP(tcsetattr),
362 REAL_FUNC_NP(system),
db9191ed 363 // exec* - skipped
364 REAL_FUNC_NP(execve),
b4f4cb40 365 //REAL_FUNC_NP(chdir),
7fd42181 366};
367
b4f4cb40 368//#define open p_real_open
7fd42181 369#define fopen p_real_fopen
b4f4cb40 370//#define mmap p_real_mmap
371//#define munmap p_real_munmap
372//#define read p_real_read
373//#define ioctl p_real_ioctl
7fd42181 374#define sigaction p_real_sigaction
375#define tcgetattr p_real_tcgetattr
376#define tcsetattr p_real_tcsetattr
377#define system p_real_system
db9191ed 378#define execve p_real_execve
b4f4cb40 379//#define chdir p_real_chdir
7fd42181 380
381#undef MAKE_WRAP_SYM
382#undef REAL_FUNC_NP
383
384#endif
385
386// just call real funcs for static, pointer for dynamic
4d045184 387int real_open(const char *pathname, int flags, ...)
7fd42181 388{
4d045184 389 va_list ap;
390 mode_t mode;
b4f4cb40 391 long ret;
4d045184 392
393 va_start(ap, flags);
394 mode = va_arg(ap, mode_t);
395 va_end(ap);
b4f4cb40 396 ret = g_open_raw(pathname, flags, mode);
397 return g_syscall_error(ret);
7fd42181 398}
399
400FILE *real_fopen(const char *path, const char *mode)
401{
402 return fopen(path, mode);
403}
404
b4f4cb40 405void *real_mmap(void *addr, size_t length, int prot, int flags,
406 int fd, off_t offset)
7fd42181 407{
b4f4cb40 408 long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
409 return (void *)g_syscall_error(ret);
7fd42181 410}
411
3ef07128 412int real_munmap(void *addr, size_t length)
413{
b4f4cb40 414 return g_syscall_error(g_munmap_raw(addr, length));
3ef07128 415}
416
7fd42181 417int real_read(int fd, void *buf, size_t count)
418{
b4f4cb40 419 long ret = g_read_raw(fd, buf, count);
420 return g_syscall_error(ret);
7fd42181 421}
422
b4f4cb40 423int real_ioctl(int fd, int request, void *argp)
7fd42181 424{
b4f4cb40 425 long ret = g_ioctl_raw(fd, request, argp);
426 return g_syscall_error(ret);
427}
428
429int real_close(int fd)
430{
431 long ret = g_close_raw(fd);
432 return g_syscall_error(ret);
7fd42181 433}
434
435int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
436{
437 return sigaction(signum, act, oldact);
438}
439
440int real_tcgetattr(int fd, struct termios *termios_p)
441{
442 return tcgetattr(fd, termios_p);
443}
444
445int real_tcsetattr(int fd, int optional_actions,
446 const struct termios *termios_p)
447{
448 return tcsetattr(fd, optional_actions, termios_p);
449}
450
451int real_system(const char *command)
452{
453 return system(command);
454}
db9191ed 455
456// real_exec* is missing intentionally - we don't need them
457
458int real_execve(const char *filename, char *const argv[],
459 char *const envp[])
460{
461 return execve(filename, argv, envp);
462}
463
464int real_chdir(const char *path)
465{
b4f4cb40 466 long ret = g_chdir_raw(path);
467 return g_syscall_error(ret);
468}
469
c3831532 470ssize_t real_readlink(const char *path, char *buf, size_t bufsiz)
471{
472 long ret = g_readlink_raw(path, buf, bufsiz);
473 return g_syscall_error(ret);
474}
475
b4f4cb40 476void real_sleep(unsigned int seconds)
477{
478 struct timespec ts = { seconds, 0 };
479 g_nanosleep_raw(&ts, NULL);
480}
481
482void real_usleep(unsigned int usec)
483{
484 struct timespec ts = { usec / 1000000, usec % 1000000 };
485 g_nanosleep_raw(&ts, NULL);
486}
487
488void real_exit(int status)
489{
490 g_exit_group_raw(status);
db9191ed 491}
499bf01c 492
493// vim:shiftwidth=2:expandtab