5a101932f5247b958750d1368060d01a0d5a5120
[ginge.git] / loader / override.c
1 // vim:shiftwidth=2:expandtab
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <sys/mman.h>
9 #include <unistd.h>
10 #include <sys/ioctl.h>
11 #include <signal.h>
12 #include <termios.h>
13
14 #include "realfuncs.h"
15 #include "header.h"
16
17 #if (DBG & 1)
18 #define strace printf
19 #else
20 #define strace(...)
21 #endif
22
23 #define UNUSED __attribute__((unused))
24
25 static const struct dev_fd_t takeover_devs[] = {
26   { "/dev/mem",     FAKEDEV_MEM },
27   { "/dev/GPIO",    FAKEDEV_GPIO },
28   { "/dev/fb0",     FAKEDEV_FB0 },
29   { "/dev/fb/0",    FAKEDEV_FB0 },
30   { "/dev/fb1",     FAKEDEV_FB1 },
31   { "/dev/fb/1",    FAKEDEV_FB1 },
32   { "/dev/mmuhack", FAKEDEV_MMUHACK },
33   { "/dev/tty",     -1 }, // XXX hmh..
34   { "/dev/tty0",    FAKEDEV_TTY0 },
35 };
36
37 static int w_open(const char *pathname, int flags, mode_t mode)
38 {
39   int i, ret;
40
41   for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
42     if (strcmp(pathname, takeover_devs[i].name) == 0) {
43       ret = takeover_devs[i].fd;
44       break;
45     }
46   }
47
48   if (i == ARRAY_SIZE(takeover_devs))
49     ret = open(pathname, flags, mode);
50
51   if (ret >= 0) {
52     for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
53       if (strcmp(pathname, emu_interesting_fds[i].name) == 0) {
54         emu_interesting_fds[i].fd = ret;
55         break;
56       }
57     }
58   }
59
60   strace("open(%s) = %d\n", pathname, ret);
61   return ret;
62 }
63
64 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
65 {
66   void *ret;
67   if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
68     ret = emu_do_mmap(length, prot, flags, fd, offset);
69   else
70     ret = mmap(addr, length, prot, flags, fd, offset);
71
72   // threads are using heap before they mmap their stack
73   // printf needs valid stack for pthread/errno
74   if (((long)&ret & 0xf0000000) == 0xb0000000)
75     strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
76   return ret;
77 }
78 #define w_mmap2 w_mmap
79
80 static ssize_t w_read(int fd, void *buf, size_t count)
81 {
82   ssize_t ret;
83   if (fd == FAKEDEV_GPIO)
84     ret = emu_read_gpiodev(buf, count);
85   else
86     ret = read(fd, buf, count);
87
88   //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
89   return ret;
90 }
91
92 static int w_ioctl(int fd, int request, void *argp)
93 {
94   int ret;
95
96   if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
97       fd == emu_interesting_fds[IFD_SOUND].fd)
98     ret = emu_do_ioctl(fd, request, argp);
99   else
100     ret = ioctl(fd, request, argp);
101
102   strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
103   return ret;
104 }
105
106 static int w_sigaction(int signum, const void *act, void *oldact)
107 {
108   strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
109   return 0;
110 }
111
112 /* dynamic only */
113 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
114 {
115   int ret;
116   if (fd != FAKEDEV_TTY0)
117     ret = tcgetattr(fd, termios_p);
118   else
119     ret = 0;
120
121   strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
122   return ret;
123 }
124
125 static UNUSED int w_tcsetattr(int fd, int optional_actions,
126                        const struct termios *termios_p)
127 {
128   int ret;
129   if (fd != FAKEDEV_TTY0)
130     ret = tcsetattr(fd, optional_actions, termios_p);
131   else
132     ret = 0;
133
134   strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
135   return ret;
136 }
137
138 static UNUSED FILE *w_fopen(const char *path, const char *mode)
139 {
140   FILE *ret = emu_do_fopen(path, mode);
141   strace("fopen(%s, %s) = %p\n", path, mode, ret);
142   return ret;
143 }
144
145 static UNUSED int w_system(const char *command)
146 {
147   int ret = emu_do_system(command);
148   strace("system(%s) = %d\n", command, ret);
149   return ret;
150 }
151
152 #undef open
153 #undef fopen
154 #undef mmap
155 #undef read
156 #undef ioctl
157 #undef sigaction
158 #undef tcgetattr
159 #undef tcsetattr
160 #undef system
161
162 #ifdef DL
163
164 #define MAKE_WRAP_SYM(sym) \
165   /* alias wrap symbols to real names */ \
166   typeof(sym) sym __attribute__((alias("w_" #sym))); \
167   /* wrapper to real functions, to be set up on load */ \
168   static typeof(sym) *p_real_##sym
169
170 MAKE_WRAP_SYM(open);
171 MAKE_WRAP_SYM(fopen);
172 MAKE_WRAP_SYM(mmap);
173 MAKE_WRAP_SYM(read);
174 MAKE_WRAP_SYM(ioctl);
175 MAKE_WRAP_SYM(sigaction);
176 MAKE_WRAP_SYM(tcgetattr);
177 MAKE_WRAP_SYM(tcsetattr);
178 MAKE_WRAP_SYM(system);
179 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
180
181 #define REAL_FUNC_NP(name) \
182   { #name, (void **)&p_real_##name }
183
184 static const struct {
185   const char *name;
186   void **func_ptr;
187 } real_funcs_np[] = {
188   REAL_FUNC_NP(open),
189   REAL_FUNC_NP(fopen),
190   REAL_FUNC_NP(mmap),
191   REAL_FUNC_NP(read),
192   REAL_FUNC_NP(ioctl),
193   REAL_FUNC_NP(sigaction),
194   REAL_FUNC_NP(tcgetattr),
195   REAL_FUNC_NP(tcsetattr),
196   REAL_FUNC_NP(system),
197 };
198
199 #define open p_real_open
200 #define fopen p_real_fopen
201 #define mmap p_real_mmap
202 #define read p_real_read
203 #define ioctl p_real_ioctl
204 #define sigaction p_real_sigaction
205 #define tcgetattr p_real_tcgetattr
206 #define tcsetattr p_real_tcsetattr
207 #define system p_real_system
208
209 #undef MAKE_WRAP_SYM
210 #undef REAL_FUNC_NP
211
212 #endif
213
214 // just call real funcs for static, pointer for dynamic
215 int real_open(const char *pathname, int flags, mode_t mode)
216 {
217   return open(pathname, flags, mode);
218 }
219
220 FILE *real_fopen(const char *path, const char *mode)
221 {
222   return fopen(path, mode);
223 }
224
225 void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
226 {
227   return mmap(addr, length, prot, flags, fd, offset);
228 }
229
230 int real_read(int fd, void *buf, size_t count)
231 {
232   return read(fd, buf, count);
233 }
234
235 int real_ioctl(int d, int request, void *argp)
236 {
237   return ioctl(d, request, argp);
238 }
239
240 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
241 {
242   return sigaction(signum, act, oldact);
243 }
244
245 int real_tcgetattr(int fd, struct termios *termios_p)
246 {
247   return tcgetattr(fd, termios_p);
248 }
249
250 int real_tcsetattr(int fd, int optional_actions,
251                        const struct termios *termios_p)
252 {
253   return tcsetattr(fd, optional_actions, termios_p);
254 }
255
256 int real_system(const char *command)
257 {
258   return system(command);
259 }