Guest User

Coto: IRQ Handler ROP routine .Hooks RealView ARM into GNUC

a guest
Jul 28th, 2015
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 4.34 KB | None | 0 0
  1. @this is Coto's IRQ driver that hooks compiled machine code from RealView into GNU-C ARM Compiler.
  2. @------------------------------------------------------------------------------------------------------
  3. @(this is definitely from coto's asm)
  4.  
  5. .align 4
  6. .code 32
  7. .arm
  8. .cpu arm7tdmi
  9. .text
  10.  
  11. @patches (from 20/02/15 outdated)
  12. @ldr #imm -4095 and +4095 (inclusive). / remember pc is 8 bytes ahead (prefetch + next opcode)
  13. .global dummyopcode
  14. dummyopcode:
  15.     ldr pc,[pc, #(-(0x204+0x8)+(0x000000d0))]       @replace 0x204 with current PC address + offset (word size) of the word location on rom (must be 4095+- from base)
  16.     ldr pc,[pc,#-0x4]
  17.     bx r1
  18.     pop  {r0,r2,r4,r5,r14}
  19.    
  20. .global PATCH_ENTRYPOINT
  21. .type   PATCH_ENTRYPOINT STT_FUNC
  22. PATCH_ENTRYPOINT:
  23.     @bootrom opcode entrypoint
  24.     .word 0xe51ff13c    @ldr pc,[pc,#0x4] opcode 1/2/ should be installed @ rom entrypoint
  25.     .word 0x08ff8000    @value 2/2 (PATCH_BOOTCODE) / should be installed @ rom_start+0xd0
  26.     @IRQ handler entrypoint
  27.    
  28.     .word 0xe51ff004    @IRQ handler + 0 ARM opcode (to replace with ldr pc,=0x08FE0000)
  29.     .word 0x08FE0000    @0x08FE0000 from PC (where you want to load this word)
  30.    
  31. @entrypoint approach patcher
  32. .global PATCH_BOOTCODE
  33. .type   PATCH_BOOTCODE STT_FUNC
  34. PATCH_BOOTCODE:
  35.     push {r0,r1,r2,lr}                  @rd is temp
  36.     mov r0,#((0x08000000 + 0x1000000))  @(ARM code)
  37.     sub r0,#(0x10000)                   @PATCH_START @0x08ff0000
  38.     mov lr,pc
  39.     bx  r0
  40.     pop {r0,r1,r2,lr}
  41.    
  42.     mov r0,#0xe3000000      @/
  43.     orr r0,#0x00a00000      @|replace with entrypoint value here
  44.     orr r0,#0x00000012      @\
  45.    
  46.     //mov r1,#0xe5000000        @/@0xe51ff138
  47.     //orr r1,#0x001f0000        @|replace with opcode ldr pc,[label] (LR or entrypoint address +0x4 ) here
  48.     //orr r1,#0x0000f100
  49.     //orr r1,#0x00000038
  50.    
  51.     mov r1,#0x08000000      @assemble 0x08000208 into r1
  52.     orr r1,#0x00000208
  53.    
  54.     mov r2,#0xe1000000      @create ARM opcode that bx r1 (exit! from installer)
  55.     orr r2,#0x002f0000     
  56.     orr r2,#0x0000ff00
  57.     orr r2,#0x00000011
  58.    
  59.     stmdb r13!,{r0,r2}      @jump to stack
  60.     mov pc, r13
  61.    
  62. @installs re-entrant IRQ routine variables
  63. @must be installed after filling stack target (0x03001c00) content
  64. .global PATCH_START
  65. .type   PATCH_START STT_FUNC
  66. PATCH_START:
  67.     mov r1,#(0x03000000)        @\ 0x03007ffc -> 0x03001c00 (irq handler is fetched from 0x08000240) (ARM mode)
  68.     orr r1,#0x00001b00
  69.     orr r1,#0x000000fc
  70.    
  71.     mov r2,#((0x08000000 + 0x1000000))  @(ARM code)
  72.     sub r2,#(0x20000)                   @PATCH_HOOK_START @0x08FE0000
  73.    
  74.     str r2,[r1]
  75.    
  76.     bx lr
  77.    
  78. .global PATCH_HOOK_START
  79. .type   PATCH_HOOK_START STT_FUNC
  80. PATCH_HOOK_START:
  81.     push  {r0,r2,r4,r5,r14}
  82.    
  83.         //do any_cback here
  84.         mov r0,#0x08000000      @NDS7_RTC_PROCESS
  85.         orr r0,#0x00fe0000
  86.         orr r0,#0x00008000
  87.        
  88.         mov lr,pc
  89.         bx r0
  90.        
  91.     @(SP from wram stores)
  92.     mov r3,#0x03000000          @save on IRQ stack: ((0x03007fff-0x200))
  93.     orr r3,#0x00008000
  94.     add r3,r3,#-((0x100)+(4*16))    @use irq stack reserving at least 16 opcodes
  95.    
  96.     mov r0,#0xe3000000          @0x03001c00: create ARM opcode that mov r3,0x4000000 //irq code
  97.     orr r0,#0x00a00000     
  98.     orr r0,#0x00003300
  99.     orr r0,#0x00000001
  100.    
  101.     mov r2,#0xe2000000          @0x03001c00: create ARM opcode that add r3,r3,0x200 //irq code
  102.     orr r2,#0x00830000     
  103.     orr r2,#0x00003c00
  104.     orr r2,#0x00000002
  105.    
  106.     mov r1,#(0x03000000)        @\build: 0x03007ffc -> 0x03001c08 (irq stack hook end) (ARM mode)
  107.     orr r1,#0x2000
  108.     sub r1,#0x3f8
  109.    
  110.     mov r4,#0xe8000000          @create ARM opcode  pop {r0,r2,r4,r5,r14} from here
  111.     orr r4,#0x00bd0000     
  112.     orr r4,#0x00004000
  113.     orr r4,#0x00000035
  114.    
  115.     mov r5,#0xe1000000          @create ARM opcode that bx r1 (exit! from installer)
  116.     orr r5,#0x002f0000     
  117.     orr r5,#0x0000ff00
  118.     orr r5,#0x00000011
  119.    
  120.     stmdb r3!,{r0,r2,r4,r5}         @jump to stack redirect /old stack cont doesnt matter
  121.     mov pc, r3
  122.  
  123. @OUR NDS7/GBA ARM7TDMI vblank IRQ hook process.. write what you need here
  124. .global NDS7_RTC_PROCESS
  125. NDS7_RTC_PROCESS:
  126.    
  127.     @entry: save on IRQ stack: ((0x03007fff-0x200))
  128.     mov r0,#0x03000000         
  129.     orr r0,#0x00008000
  130.     add r0,r0,#-((0x100)+(4*16)+(4*16))
  131.     stmdb r0,{r1-r14}
  132.    
  133.     @1 / n : test if PSRAM is RO or RW
  134.    
  135.     @user code init
  136.     mov r0,#0
  137.     mov r1,#0
  138.     mov r2,#0
  139.     mov r3,#0
  140.     mov r4,#0
  141.     mov r5,#0
  142.     mov r6,#0
  143.     mov r7,#0
  144.     mov r8,#0
  145.     mov r9,#0
  146.     mov r10,#0
  147.     mov r11,#0
  148.     mov r12,#0
  149.     mov r13,#0
  150.     mov r14,#0
  151.    
  152.     @user code end
  153.    
  154.     @leave:save on IRQ stack: ((0x03007fff-0x200))
  155.     mov r0,#0x03000000         
  156.     orr r0,#0x00008000
  157.     add r0,r0,#-((0x100)+(4*16)+(4*16))
  158.     ldmdb r0,{r1-r14}
  159.    
  160.     bx lr
  161. .align
  162. .pool
  163. .end
Advertisement
Add Comment
Please, Sign In to add comment