| 1 | #include "camera.h" |
|---|
| 2 | #include "lolevel.h" |
|---|
| 3 | #include "platform.h" |
|---|
| 4 | #include "core.h" |
|---|
| 5 | #include "keyboard.h" |
|---|
| 6 | #include "stdlib.h" |
|---|
| 7 | |
|---|
| 8 | /* Ours stuff */ |
|---|
| 9 | extern long link_bss_start; |
|---|
| 10 | extern long link_bss_end; |
|---|
| 11 | extern void boot(); |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | static int stop_hooking; |
|---|
| 15 | |
|---|
| 16 | static void (*task_prev)( |
|---|
| 17 | long p0, long p1, long p2, long p3, long p4, |
|---|
| 18 | long p5, long p6, long p7, long p8, long p9); |
|---|
| 19 | |
|---|
| 20 | static void (*init_file_modules_prev)( |
|---|
| 21 | long p0, long p1, long p2, long p3, long p4, |
|---|
| 22 | long p5, long p6, long p7, long p8, long p9); |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | void spytask(long ua, long ub, long uc, long ud, long ue, long uf) |
|---|
| 27 | { |
|---|
| 28 | core_spytask(); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | static void task_start_hook( |
|---|
| 33 | long p0, long p1, long p2, long p3, long p4, |
|---|
| 34 | long p5, long p6, long p7, long p8, long p9) |
|---|
| 35 | { |
|---|
| 36 | _CreateTask("SpyTask", 0x19, 0x2000, spytask, 0); |
|---|
| 37 | |
|---|
| 38 | task_prev(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9 ); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | static void init_file_modules_hook( |
|---|
| 44 | long p0, long p1, long p2, long p3, long p4, |
|---|
| 45 | long p5, long p6, long p7, long p8, long p9) |
|---|
| 46 | { |
|---|
| 47 | remount_filesystem(); |
|---|
| 48 | init_file_modules_prev(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9 ); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | static void capt_seq_hook( |
|---|
| 53 | long p0, long p1, long p2, long p3, long p4, |
|---|
| 54 | long p5, long p6, long p7, long p8, long p9) |
|---|
| 55 | { |
|---|
| 56 | capt_seq_task(); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | static void physw_hook( |
|---|
| 61 | long p0, long p1, long p2, long p3, long p4, |
|---|
| 62 | long p5, long p6, long p7, long p8, long p9) |
|---|
| 63 | { |
|---|
| 64 | mykbd_task(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | static void movie_record_hook( |
|---|
| 68 | long p0, long p1, long p2, long p3, long p4, |
|---|
| 69 | long p5, long p6, long p7, long p8, long p9) |
|---|
| 70 | { |
|---|
| 71 | movie_record_task(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | static int my_ncmp(const char *s1, const char *s2, long len) |
|---|
| 77 | { |
|---|
| 78 | int i; |
|---|
| 79 | for (i=0;i<len;i++){ |
|---|
| 80 | if (s1[i] != s2[i]) |
|---|
| 81 | return 1; |
|---|
| 82 | } |
|---|
| 83 | return 0; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void createHook (void *pNewTcb) |
|---|
| 87 | { |
|---|
| 88 | char *name = (char*)(*(long*)((char*)pNewTcb+0x34)); |
|---|
| 89 | long *entry = (long*)((char*)pNewTcb+0x74); |
|---|
| 90 | |
|---|
| 91 | // always hook first task creation |
|---|
| 92 | // to create SpyProc |
|---|
| 93 | if (!stop_hooking){ |
|---|
| 94 | task_prev = (void*)(*entry); |
|---|
| 95 | *entry = (long)task_start_hook; |
|---|
| 96 | stop_hooking = 1; |
|---|
| 97 | } else { |
|---|
| 98 | // hook/replace another tasks |
|---|
| 99 | if (my_ncmp(name, "tPhySw", 6) == 0){ |
|---|
| 100 | *entry = (long)physw_hook; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | if (my_ncmp(name, "tInitFileM", 10) == 0){ |
|---|
| 104 | init_file_modules_prev = (void*)(*entry); |
|---|
| 105 | #if CAM_MULTIPART |
|---|
| 106 | *entry = (long)init_file_modules_task; |
|---|
| 107 | #else |
|---|
| 108 | *entry = (long)init_file_modules_hook; |
|---|
| 109 | #endif |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | if (my_ncmp(name, "tCaptSeqTa", 10) == 0){ |
|---|
| 113 | *entry = (long)capt_seq_hook; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | if (my_ncmp(name, "tMovieReco", 10) == 0){ |
|---|
| 117 | #if CAM_CHDK_HAS_EXT_VIDEO_MENU |
|---|
| 118 | *entry = (long)movie_record_hook; |
|---|
| 119 | #endif |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | #if CAM_EXT_TV_RANGE |
|---|
| 123 | if (my_ncmp(name, "tExpDrvTas", 10) == 0){ |
|---|
| 124 | *entry = (long)exp_drv_task; |
|---|
| 125 | } |
|---|
| 126 | #endif |
|---|
| 127 | |
|---|
| 128 | core_hook_task_create(pNewTcb); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void deleteHook (void *pTcb) |
|---|
| 133 | { |
|---|
| 134 | core_hook_task_delete(pTcb); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | void startup() |
|---|
| 138 | { |
|---|
| 139 | long *bss = &link_bss_start; |
|---|
| 140 | |
|---|
| 141 | // sanity check |
|---|
| 142 | if ((long)&link_bss_end > (MEMISOSTART + MEMISOSIZE)){ |
|---|
| 143 | started(); |
|---|
| 144 | shutdown(); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | // initialize .bss senment |
|---|
| 148 | while (bss<&link_bss_end) |
|---|
| 149 | *bss++ = 0; |
|---|
| 150 | |
|---|
| 151 | // fill memory with this magic value so we could see what |
|---|
| 152 | // parts of memory were or not used |
|---|
| 153 | #if 0 |
|---|
| 154 | long *ptr; |
|---|
| 155 | for (ptr=(void*)MEMBASEADDR;((long)ptr)<MEMISOSTART;ptr+=4){ |
|---|
| 156 | ptr[0]=0x55555555; |
|---|
| 157 | ptr[1]=0x55555555; |
|---|
| 158 | ptr[2]=0x55555555; |
|---|
| 159 | ptr[3]=0x55555555; |
|---|
| 160 | } |
|---|
| 161 | #endif |
|---|
| 162 | |
|---|
| 163 | boot(); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | #if CAM_CONSOLE_LOG_ENABLED |
|---|
| 167 | |
|---|
| 168 | #define DEV_HDR_WRITE_OFFSET (0x14C/4) |
|---|
| 169 | |
|---|
| 170 | typedef int DEV_HDR; |
|---|
| 171 | |
|---|
| 172 | int (*_tyWriteOrig)(DEV_HDR *hdr, char *buf, int len); |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | int hook_tyWriteOrig(DEV_HDR *hdr, char *buf, int len) |
|---|
| 176 | { |
|---|
| 177 | // Slow, but stable writes |
|---|
| 178 | FILE *fd = fopen("A/stdout.txt", "a"); |
|---|
| 179 | if (fd) { |
|---|
| 180 | fwrite(buf, 1, len, fd); |
|---|
| 181 | fclose(fd); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | return _tyWriteOrig(hdr, buf, len); |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | void cam_console_init() |
|---|
| 189 | { |
|---|
| 190 | DEV_HDR *DRV_struct; |
|---|
| 191 | |
|---|
| 192 | extern DEV_HDR* _iosDevFind(char*, int); |
|---|
| 193 | DRV_struct = _iosDevFind("/tyCo/0", 0); |
|---|
| 194 | |
|---|
| 195 | _tyWriteOrig = (void*)DRV_struct[DEV_HDR_WRITE_OFFSET]; |
|---|
| 196 | |
|---|
| 197 | FILE *fd = fopen("A/chdklog.txt", "a"); |
|---|
| 198 | if (fd) { |
|---|
| 199 | // can't be used with "Fut" API |
|---|
| 200 | //fprintf(fd, "DRV_struct: %x, _tyWriteOrig: %x\n", DRV_struct, _tyWriteOrig); |
|---|
| 201 | char buf[256]; |
|---|
| 202 | int buflen = sprintf(buf, "DRV_struct: %x, _tyWriteOrig: %x\n", DRV_struct, _tyWriteOrig); |
|---|
| 203 | fwrite(buf, 1, buflen, fd); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | FILE *fdout = fopen("A/stdout.txt", "r"); |
|---|
| 207 | if (fdout) |
|---|
| 208 | { |
|---|
| 209 | DRV_struct[DEV_HDR_WRITE_OFFSET] = (int)hook_tyWriteOrig; |
|---|
| 210 | fclose(fdout); |
|---|
| 211 | // fprintf(fd, "tyWrite replaced, camera log enabled\n"); |
|---|
| 212 | fwrite("tyWrite replaced, camera log enabled\n", 1, sizeof("tyWrite replaced, camera log enabled\n"), fd); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | if (fd) |
|---|
| 216 | { |
|---|
| 217 | fclose(fd); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | #endif |
|---|