refactor dependencies
[ginge.git] / loader / patches.c
1 // vim:shiftwidth=2:expandtab
2 #include <stdio.h>
3
4 #include "header.h"
5 #include "sys_cacheflush.h"
6
7 #include "override.c"
8
9 // note: first mask must be always full for search algo
10 static const unsigned int sig_mask_all[] = {
11   0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
12   0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
13 };
14
15 static const unsigned int sig_open[] = {
16   0xe59cc000, 0xe33c0000, 0x1a000003, 0xef900005
17 };
18 #define sig_mask_open sig_mask_all
19
20 static const unsigned int sig_mmap[] = {
21   0xe92d000f, 0xe1a0000d, 0xef90005a, 0xe28dd010
22 };
23 #define sig_mask_mmap sig_mask_all
24
25 static const unsigned int sig_mmap2[] = {
26   0xe52d5004, 0xe59d5008, 0xe52d4004, 0xe59d4008,
27   0xe1b0ca05, 0x1a000006, 0xe1a05625, 0xef9000c0
28 };
29 #define sig_mask_mmap2 sig_mask_all
30
31 static const unsigned int sig_read[] = {
32   0xe59fc080, 0xe59cc000, 0xe33c0000, 0x1a000003, 0xef900003
33 };
34 #define sig_mask_read sig_mask_all
35
36 static const unsigned int sig_ioctl[] = {
37   0xef900036, 0xe3700a01, 0x312fff1e
38 };
39 #define sig_mask_ioctl sig_mask_all
40
41 static const unsigned int sig_sigaction[] = {
42   0xe59f300c, 0xe3530000, 0x0a000000, 0xea000000, 0xea000000
43 };
44 static const unsigned int sig_mask_sigaction[] = {
45   0xffffffff, 0xffffffff, 0xffffffff, 0xff000000, 0xff000000
46 };
47
48 static const unsigned int sig_execve[] = {
49   0xef90000b, 0xe1a04000, 0xe3700a01
50 };
51 #define sig_mask_execve sig_mask_all
52
53 static const unsigned int sig_execve2[] = {
54   0xef90000b, 0xe3700a01, 0xe1a04000
55 };
56 #define sig_mask_execve2 sig_mask_all
57
58 static const unsigned int sig_chdir[] = {
59   0xef90000c, 0xe3700a01, 0x312fff1e, 0xea0004bb
60 };
61 static const unsigned int sig_mask_chdir[] = {
62   0xffffffff, 0xffffffff, 0xffffffff, 0xff000000
63 };
64
65 #define PATCH_(f,p) { sig_##p, sig_mask_##p, ARRAY_SIZE(sig_##p), w_##f }
66 #define PATCH(f) PATCH_(f,f)
67
68 static const struct {
69   const unsigned int *sig;
70   const unsigned int *sig_mask;
71   size_t sig_cnt;
72   void *func;
73 } patches[] = {
74   PATCH(open),
75   PATCH(mmap),
76   PATCH(mmap2), // mmap2 syscall
77   PATCH(read),
78   PATCH(ioctl),
79   PATCH(sigaction),
80 //  PATCH_(execve, execve2), // hangs
81   PATCH(chdir),
82 };
83
84 void do_patches(void *ptr, unsigned int size)
85 {
86   int i, s;
87
88   for (i = 0; i < ARRAY_SIZE(patches); i++) {
89     const unsigned int *sig = patches[i].sig;
90     const unsigned int *sig_mask = patches[i].sig_mask;
91     unsigned int *seg = (void *)(((long)ptr + 3) & ~3);
92     unsigned int *seg_end = seg + size / 4;
93     unsigned int sig0 = sig[0];
94
95     for (; seg < seg_end; seg++) {
96       if (*seg != sig0)
97         continue;
98
99       for (s = 1; s < patches[i].sig_cnt; s++)
100         if ((seg[s] ^ sig[s]) & sig_mask[s])
101           break;
102
103       if (s == patches[i].sig_cnt)
104         goto found;
105     }
106     continue;
107
108 found:
109     dbg("  patch #%i @ %08x\n", i, (int)seg);
110     seg[0] = 0xe59ff000; // ldr pc, [pc]
111     seg[1] = 0;
112     seg[2] = (unsigned int)patches[i].func;
113   }
114
115   sys_cacheflush(ptr, (char *)ptr + size);
116 }
117