1 // vim:shiftwidth=2:expandtab
11 #include "sys_cacheflush.h"
15 static const unsigned int sig_open[] = {
16 0xe59cc000, 0xe33c0000, 0x1a000003, 0xef900005
19 static const unsigned int sig_mmap[] = {
20 0xe92d000f, 0xe1a0000d, 0xef90005a, 0xe28dd010
23 static const unsigned int sig_mmap_[] = {
24 0xe52d5004, 0xe59d5008, 0xe52d4004, 0xe59d4008,
25 0xe1b0ca05, 0x1a000006, 0xe1a05625, 0xef9000c0
28 static const unsigned int sig_read[] = {
29 0xe59fc080, 0xe59cc000, 0xe33c0000, 0x1a000003, 0xef900003
32 #define FAKE_DEVMEM_DEVICE 10001
33 #define FAKE_DEVGPIO_DEVICE 10002
35 static int w_open(const char *pathname, int flags, mode_t mode)
38 if (strcmp(pathname, "/dev/mem") == 0)
39 ret = FAKE_DEVMEM_DEVICE;
40 else if (strcmp(pathname, "/dev/GPIO") == 0)
41 ret = FAKE_DEVGPIO_DEVICE;
43 ret = open(pathname, flags, mode);
45 strace("open(%s) = %d\n", pathname, ret);
49 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
52 if (fd != FAKE_DEVMEM_DEVICE)
53 ret = mmap(addr, length, prot, flags, fd, offset);
55 ret = emu_mmap_dev(length, prot, flags, offset);
57 strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
60 #define w_mmap_ w_mmap
62 ssize_t w_read(int fd, void *buf, size_t count)
65 if (fd != FAKE_DEVGPIO_DEVICE)
66 return read(fd, buf, count);
68 ret = emu_read_gpiodev(buf, count);
69 //printf("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
73 #define PATCH(f) { sig_##f, ARRAY_SIZE(sig_##f), w_##f }
76 const unsigned int *sig;
82 PATCH(mmap_), // mmap using mmap2 syscall
86 void do_patches(void *ptr, unsigned int size)
90 for (i = 0; i < ARRAY_SIZE(patches); i++) {
91 const unsigned int *sig = patches[i].sig;
92 unsigned int *seg = (void *)(((long)ptr + 3) & ~3);
93 unsigned int *seg_end = seg + size / 4;
94 unsigned int sig0 = sig[0];
96 for (; seg < seg_end; seg++) {
100 for (s = 1; s < patches[i].sig_cnt; s++)
101 if (seg[s] != sig[s])
104 if (s == patches[i].sig_cnt)
110 printf(" patch #%i @ %08x\n", i, (int)seg);
111 seg[0] = 0xe59ff000; // ldr pc, [pc]
113 seg[2] = (unsigned int)patches[i].func;
116 sys_cacheflush(ptr, (char *)ptr + size);