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