Advertisement
xerpi

PS2 hook pad working

Dec 11th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.53 KB | None | 0 0
  1. #include <thbase.h>
  2. #include <thsemap.h>
  3. #include <sifcmd.h>
  4. #include <sifman.h>
  5. #include <loadcore.h>
  6. #include <string.h>
  7. #include <intrman.h>
  8. #include <ioman.h>
  9. #include <iop2eedebug.h>
  10.  
  11. #define debug(...) \
  12.     do { \
  13.         char buf[IOP2EEDEBUG_CMD_STR_MAX_SIZE]; \
  14.         sprintf(buf, __VA_ARGS__); \
  15.         iop2eedebug_print(buf); \
  16.     } while (0)
  17.  
  18. typedef struct
  19. {
  20.     u32 padEnd;
  21.     s32 eventflag;
  22.     u32 init;
  23.     u32 stopTransfer;
  24.     s32 tid_1;
  25.     s32 tid_2;
  26. } vblankData_t;
  27.  
  28. struct padButtonStatus
  29. {
  30.     unsigned char ok;
  31.     unsigned char mode;
  32.     unsigned short btns;
  33.     // joysticks
  34.     unsigned char rjoy_h;
  35.     unsigned char rjoy_v;
  36.     unsigned char ljoy_h;
  37.     unsigned char ljoy_v;
  38.     // pressure mode
  39.     unsigned char right_p;
  40.     unsigned char left_p;
  41.     unsigned char up_p;
  42.     unsigned char down_p;
  43.     unsigned char triangle_p;
  44.     unsigned char circle_p;
  45.     unsigned char cross_p;
  46.     unsigned char square_p;
  47.     unsigned char l1_p;
  48.     unsigned char r1_p;
  49.     unsigned char l2_p;
  50.     unsigned char r2_p;
  51.     unsigned char unkn16[12];
  52. } __attribute__((packed));
  53.  
  54.  
  55. typedef struct
  56. {
  57.     u8 data[32];
  58.     u32 actDirData[2];
  59.     u32 actAlignData[2];
  60.     u32 actData[8];
  61.     u32 modeTable[2];
  62.     u32 frame;
  63.     u32 findPadRetries;
  64.     u32 length;
  65.     u8 modeConfig;
  66.     u8 modeCurId;
  67.     u8 model;
  68.     u8 buttonDataReady;
  69.     u8 nrOfModes;
  70.     u8 modeCurOffs;
  71.     u8 nrOfActuators;
  72.     u8 numActComb;
  73.     u8 val_c6;
  74.     u8 mode;
  75.     u8 lock;
  76.     u8 actDirSize;
  77.     u8 state;
  78.     u8 reqState;
  79.     u8 currentTask;
  80.     u8 runTask;
  81.     u8 stat70bit;
  82.     u8 padding[11];
  83. } ee_paddata_t;
  84.  
  85.  
  86. typedef struct
  87. {
  88.     ee_paddata_t ee_pdata __attribute__((aligned(4)));
  89.     u8 modeConfig;
  90.     u8 modeCurId;
  91.     u8 model;
  92.     u8 numModes;
  93.     u8 modeCurOffs;
  94.     u8 numActuators;
  95.     u8 numActComb;
  96.     u8 disconnected;
  97.     u32 actData[4];
  98.     u32 combData[4];
  99.     u32 modeTable[2];
  100.     u8 buttonInfo[4];
  101.     u8 buttonMask[4];
  102.     u8 vrefParam[12];
  103.     u16 val_c6; // unused
  104.     u8 inbuffer[32];
  105.     u8 outbuffer[32];
  106.     u8 buttonStatus[32];
  107.     u32 buttonDataReady;
  108.     u8 mode;
  109.     u8 lock;
  110.     u8 ee_actDirectData[8] __attribute__((aligned(4)));
  111.     s16 ee_actDirectSize;
  112.     u8 ee_actAlignData[8] __attribute__((aligned(4)));
  113.     u16 state;
  114.     u16 reqState;
  115.     u32 frame;
  116.     u32 padarea_ee_addr;
  117.     u32 findPadRetries;
  118.     s32 updatepadTid;
  119.     s32 querypadTid;
  120.     s32 unused158; // probably unused thread id
  121.     s32 setmainmodeTid;
  122.     s32 setactalignTid;
  123.     s32 setbuttoninfoTid;
  124.     s32 setvrefparamTid;
  125.     u32 eventflag;
  126.     u16 port;
  127.     u16 slot;
  128.     u32 currentTask;
  129.     u32 runTask;
  130.     u32 taskTid;
  131.     u32 stat70bit;
  132.     u32 val_184; // unused
  133. } padState_t;
  134.  
  135.  
  136. //Grabbed from the PSPSDK
  137. #define MIPS_JAL(f)        (0x0C000000 | (((u32)(f) >> 2) & 0x03ffffff))
  138. #define MIPS_J(f)          (0x08000000 | (((u32)(f) & 0x0ffffffc) >> 2))
  139. #define MAKE_CALL(a, f) _sw(0x0C000000 | (((u32)(f) >> 2) & 0x03ffffff), (u32)a)
  140. #define MAKE_JUMP(a, f) _sw(0x08000000 | (((u32)(f) & 0x0ffffffc) >> 2), (u32)a)
  141.  
  142. void *GetExportTable(char *libname, int version);
  143. void *GetExportEntry(void *table, u32 entry);
  144. void *HookExportEntry(void *table, u32 entry, void *func);
  145. u32 GetExportTableSize(void *table);
  146.  
  147. #define padPortOpen_import_num  6
  148. #define padInit_import_num      4
  149. #define sceSifSetDma_import_num 7
  150.  
  151. #define bkpt() asm volatile("break\n\tnop\n\t")
  152.  
  153. volatile s32 (*padPortOpen_orig)(s32 port, s32 slot, s32 pad_area_ee_addr, u32 *buf) = NULL;
  154. volatile s32 (*padInit_orig)(void * ee_addr) = NULL;
  155.  
  156. volatile int (*sceSifSetDma_orig)(SifDmaTransfer_t *dmat, int count) = NULL;
  157.  
  158. volatile s32 _pad_area_ee_addr = 0;
  159. volatile u32 *_buf = NULL;
  160.  
  161. s32 padInit_hook(void *ee_addr)
  162. {
  163.     return padInit_orig(ee_addr);
  164. }
  165.  
  166. s32 padPortOpen_hook(s32 port, s32 slot, s32 pad_area_ee_addr, u32 *buf)
  167. {
  168.     _pad_area_ee_addr = pad_area_ee_addr;
  169.     _buf = buf;
  170.     return padPortOpen_orig(port, slot, pad_area_ee_addr, buf);
  171. }
  172.  
  173. int i = 0;
  174.  
  175. int sceSifSetDma_hook(SifDmaTransfer_t *dmat, int count)
  176. {
  177.     ee_paddata_t *e = (ee_paddata_t *)dmat->src;
  178.     struct padButtonStatus *but = (struct padButtonStatus *)&e->data[2];
  179.     if (dmat->size == 128) {
  180.         debug("0x%04X\n",((u16*)e->data)[0x12/2]);
  181.         //but->btns |= 0x0010;
  182.     }
  183.         //e->data[0] = 0xEA;
  184.     //padState_t *p = dmat->src;
  185.     //p->buttonStatus[0] = 0xA0A0;
  186.     //i++;
  187.     return sceSifSetDma_orig(dmat, count);
  188. }
  189.  
  190. static void thread_func(void *arg)
  191. {
  192.     //Hook padman's sceSifSetDma import
  193.     while (1) {
  194.         register void *exp = GetExportTable("sifman", 0x101);
  195.         if (exp) {
  196.             int ctx;
  197.             CpuSuspendIntr(&ctx);
  198.    
  199.             debug("Found sifman export table at: 0x%08X\n", exp);
  200.             iop_library_t *lib = (iop_library_t *)((u32)exp - 20);
  201.  
  202.             debug("\tname: %s\n", lib->name);
  203.             debug("\tversion: 0x%04X\n", lib->version);
  204.             debug("\tflags: 0x%04X\n", lib->flags);
  205.  
  206.             struct irx_import_table *table = lib->caller;
  207.            
  208.             while (table) {
  209.                 debug("\t\ttable name: %s\n", table->name);
  210.                 struct irx_import_stub *stub = (struct irx_import_stub *)table->stubs;
  211.                 while (stub->jump) {
  212.                     if (stub->fno == sceSifSetDma_import_num) {
  213.                         debug("\t\t\tpatching: %i\n", stub->fno);
  214.                         sceSifSetDma_orig = ((void**)exp)[stub->fno];
  215.                         //((void**)exp)[stub->fno] = sceSifSetDma_hook;
  216.                         MAKE_JUMP(&stub->jump, sceSifSetDma_hook);
  217.                     }
  218.                     stub++;
  219.                 }
  220.                 table = table->next;
  221.             }
  222.             FlushDcache();
  223.             FlushIcache();
  224.             CpuResumeIntr(ctx);
  225.             while (1) {
  226.                 //debug("%i\n", i);
  227.                 DelayThread(1000);
  228.             }
  229.  
  230.             /*struct irx_import_stub *stub;
  231.             int j = 0;
  232.             for (table = lib->caller; table != NULL; table = table->next) {
  233.                 j++;
  234.                 int i;
  235.                 //debug("%i : %s", j, table->name);
  236.                 for (i = 0; table->name[i] != '\0'; i++) {
  237.                     if (table->name[i] > 'd') {
  238.                         //bkpt();
  239.                         stub = (struct irx_import_stub *)table->stubs;
  240.                         sceSifSetDma_orig = (void*)stub;
  241.                         //MAKE_JUMP(&(stub[sceSifSetDma_import_num].jump), sceSifSetDma_hook);
  242.                     }
  243.                 }
  244.             }*/
  245.         }
  246.         DelayThread(1000);
  247.     }
  248.    
  249.     //Hook padPortOpen
  250.     /*while (1) {
  251.         register void *exp = GetExportTable("padman", 0x102);
  252.         if (exp) {
  253.             int ctx;
  254.             CpuSuspendIntr(&ctx);
  255.            
  256.             u32 *exptable = exp;
  257.             padPortOpen_orig = (void *)exptable[padPortOpen_import_num];
  258.             u32 jal_padPortOpen = MIPS_JAL(padPortOpen_orig);
  259.             u32 *addr = exptable - 0x10000;
  260.             for (; addr < exptable + 0x10000; addr++) {
  261.                 if (*addr == jal_padPortOpen) {
  262.                     MAKE_CALL(addr, padPortOpen_hook);
  263.                     FlushDcache();
  264.                     FlushIcache();
  265.                     CpuResumeIntr(ctx);
  266.                     break;
  267.                 }
  268.             }
  269.             CpuResumeIntr(ctx);
  270.         }
  271.         DelayThread(1000);
  272.     }*/
  273.  
  274.     while (1) {
  275.         if (_pad_area_ee_addr) {
  276.             //memset((void*)_pad_area_ee_addr, 0xAE, 256);
  277.         }
  278.         DelayThread(1000);
  279.     }
  280. }
  281.  
  282. int _start()
  283. {
  284.     iop_thread_t th = {
  285.         .attr      = TH_C,
  286.         .thread    = thread_func,
  287.         .priority  = 40,
  288.         .stacksize = 0x400,
  289.         .option    = 0
  290.     };
  291.  
  292.     int thid = CreateThread(&th);
  293.     if (thid > 0) {
  294.         StartThread(thid, NULL);
  295.         return 0;
  296.     }
  297.  
  298.     return 1;
  299. }
  300.  
  301. u32 GetExportTableSize(void *table)
  302. {
  303.     register void **exp;
  304.     register u32 size;
  305.     exp = (void**)table;
  306.     size = 0;
  307.  
  308.     if (exp != NULL)
  309.         while (*exp ++ != NULL)
  310.             size ++;  
  311.  
  312.     return size;
  313. }
  314. void *GetExportTable(char *libname, int version)
  315. {
  316.     if (libname != NULL) {
  317.         iop_library_t lib;
  318.         register int i;
  319.         register char *psrc;
  320.  
  321.         memset(&lib, 0, sizeof(iop_library_t));
  322.         lib.version = version;
  323.  
  324.         for (i = 0, psrc = libname; (i < 8) && (*psrc); i ++, psrc ++) lib.name[i] = *psrc;
  325.  
  326.         return QueryLibraryEntryTable(&lib);
  327.     }
  328.     return NULL;
  329. }
  330. void *GetExportEntry(void *table, u32 entry)
  331. {
  332.     if (entry < GetExportTableSize(table)) {
  333.         register void **exp;
  334.         exp = (void**)table;
  335.         return exp[entry];
  336.     }
  337.     return NULL;
  338. }
  339. void *HookExportEntry(void *table, u32 entry, void *func)
  340. {
  341.     if (entry < GetExportTableSize(table)) {
  342.         int oldstate;
  343.         register void **exp, *temp;
  344.  
  345.         exp = (void**)table;
  346.         exp = &exp[entry];
  347.  
  348.         CpuSuspendIntr(&oldstate);
  349.         temp = *exp;
  350.         *exp = func;
  351.         func = temp;
  352.         CpuResumeIntr(oldstate);
  353.  
  354.         return func;
  355.     }
  356.     return NULL;
  357. }
  358.  
  359. void fix_imports(iop_library_t *lib)
  360. {
  361.     struct irx_import_table *table;
  362.     struct irx_import_stub *stub;
  363.  
  364.     FlushDcache();
  365.  
  366.     // go through each table that imports the library
  367.     for(table = lib->caller; table != NULL; table = table->next)
  368.     {
  369.         // go through each import in the table
  370.         for(stub = (struct irx_import_stub *) table->stubs; stub->jump != 0; stub++)
  371.         {
  372.             // patch the stub to jump to the address specified in the library export table for "fno"
  373.             stub->jump = 0x08000000 | (((u32) lib->exports[stub->fno] << 4) >> 6);
  374.         }
  375.     }
  376.  
  377.     FlushIcache();
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement