1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::hooks::{getRegionAddress, Region};
use std::{iter::StepBy, ops::Range};

pub struct TextIter<InnerIter: Iterator<Item = usize> + Sized> {
    inner: InnerIter,
}

impl TextIter<StepBy<Range<usize>>> {
    pub fn new() -> Self {
        unsafe {
            let text = getRegionAddress(Region::Text) as usize;
            let rodata = getRegionAddress(Region::Rodata) as usize;

            Self {
                inner: (text..rodata).step_by(4),
            }
        }
    }
}

impl<InnerIter: Iterator<Item = usize> + Sized> Iterator for TextIter<InnerIter> {
    type Item = (usize, Instruction);

    fn next(&mut self) -> Option<Self::Item> {
        let ptr = self.inner.next()? as *const u32;
        let raw_instr = unsafe { *ptr };
        Some((ptr as usize, Instruction::from_u32(raw_instr)))
    }
}

const LDR_MASK: u32 = 0b1111111111_000000000000_00000_00000; // 64-bit LDR unsigned offset
const LDR_MASKED: u32 = 0b1111100101_000000000000_00000_00000;

const ADD_MASK: u32 = 0b11111111_00_000000000000_00000_00000; // 64-bit ADD immediate
const ADD_MASKED: u32 = 0b10010001_00_000000000000_00000_00000;

const ADRP_MASK: u32 = 0b1_00_11111_0000000000000000000_00000;
const ADRP_MASKED: u32 = 0b1_00_10000_0000000000000000000_00000;

const LDUR_MASK: u32 = 0b11_111_1_11_11_1_000000000_00_00000_00000;
const LDUR_MASKED: u32 = 0b11_111_0_00_01_0_000000000_00_00000_00000;

const LDRB_MASK: u32 = 0b11_111_1_11_11_0_00000_000_0_00_00000_00000; // LDRB immediate Unsigned offset
const LDRB_MASKED: u32 = 0b00_111_0_01_01_0_00000_000_0_00_00000_00000;

const SUB_MASK: u32 = 0b1_1_1_11111_00_0_00000_000000_00000_00000; // 32-bit SUB Immediate
const SUB_MASKED: u32 = 0b0_1_0_10001_00_0_00000_000000_00000_00000;

const AND_MASK: u32 = 0b1_11_111111_1_000000_000000_00000_00000; // 32-bit AND immediate
const AND_MASKED: u32 = 0b0_00_100100_0_000000_000000_00000_00000;

const LDRSW_MASK: u32 = 0b11_111_1_11_11_000000000000_00000_00000; // 64-bit LDRSW unsigned offset
const LDRSW_MASKED: u32 = 0b10_111_0_01_10_000000000000_00000_00000;

const CBZ_MASK: u32 = 0b1_111111_1_0000000000000000000_00000; // 32-bit CBZ
const CBZ_MASKED: u32 = 0b0_011010_0_0000000000000000000_00000;

const CMP_MASK: u32 = 0b1_1_1_11111_00_000000000000_00000_00000; // 32-bit CMP immediate
const CMP_MASKED: u32 = 0b0_1_1_10001_00_000000000000_00000_00000;

const BCS_MASK: u32 = 0b1111111_1_0000000000000000000_1_1111; // B.cond jump
const BCS_MASKED: u32 = 0b0101010_0_0000000000000000000_0_0010;

pub enum Instruction {
    Ldr { imm: u16, rn: u8, rt: u8 },
    Add { shift: u8, imm: u16, rn: u8, rd: u8 },
    Adrp { imm: u32, rd: u8 },
    Ldur { imm: u16, rn: u8, rt: u8 },
    Ldrb { imm: u16, rn: u8, rt: u8 },
    Sub { shift: u8, imm: u16, rn: u8, rd: u8 },
    And { imm: u16, rn: u8, rd: u8 },
    Mov { imm: u8, rm: u8, rn: u8, rd: u8 },
    Bl { imm: u32 },
    Ldrsw { imm: u16, rn: u8, rt: u8 },
    Cbz { imm: u32, rt: u8 },
    Cmp { shift: u8, imm: u16, rn: u8 },
    BCs { imm: u32, cond: u8 },
    Unk(u32),
}

impl Instruction {
    fn u32_as_ldr(val: u32) -> Option<Self> {
        if val & LDR_MASK == LDR_MASKED {
            Some(Instruction::Ldr {
                imm: ((val >> 10) & 0xFFF) as u16,
                rn: ((val >> 5) & 0x1F) as u8,
                rt: (val & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_add(val: u32) -> Option<Self> {
        if val & ADD_MASK == ADD_MASKED {
            Some(Instruction::Add {
                shift: ((val >> 22) & 0x3) as u8,
                imm: ((val >> 10) & 0xFFF) as u16,
                rn: ((val >> 5) & 0x1F) as u8,
                rd: (val & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_adrp(val: u32) -> Option<Self> {
        if val & ADRP_MASK == ADRP_MASKED {
            let immhi = (val >> 5) & 0x7FFFF;
            let immlo = (val >> 29) & 0x3;
            Some(Instruction::Adrp {
                imm: (immhi << 14) + (immlo << 12),
                rd: (val & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_ldur(val: u32) -> Option<Self> {
        if val & LDUR_MASK == LDUR_MASKED {
            Some(Instruction::Ldur {
                imm: ((val >> 12) & 0x1FF) as u16,
                rn: ((val >> 5) & 0x1F) as u8,
                rt: ((val >> 5) & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_ldrb(val: u32) -> Option<Self> {
        if val & LDRB_MASK == LDRB_MASKED {
            Some(Instruction::Ldrb {
                imm: ((val >> 10) & 0xFFF) as u16,
                rn: ((val >> 5) & 0x1F) as u8,
                rt: (val & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_sub(val: u32) -> Option<Self> {
        if val & SUB_MASK == SUB_MASKED {
            Some(Instruction::Sub {
                shift: ((val >> 22) & 0x3) as u8,
                imm: ((val >> 10) & 0xFFF) as u16,
                rn: ((val >> 5) & 0x1F) as u8,
                rd: (val & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_and(val: u32) -> Option<Self> {
        if val & AND_MASK == AND_MASKED {
            let immr = (val >> 16) & 0x3F;
            let imms = (val >> 10) & 0x3F;
            Some(Instruction::And {
                imm: ((imms << 6) + immr) as u16,
                rn: ((val >> 5) & 0x1F) as u8,
                rd: (val & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_cbz(val: u32) -> Option<Self> {
        if val & CBZ_MASK == CBZ_MASKED {
            Some(Instruction::Cbz {
                imm: ((val >> 5) & 0x7FFFF),
                rt: (val & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_ldrsw(val: u32) -> Option<Self> {
        if val & LDRSW_MASK == LDRSW_MASKED {
            Some(Instruction::Ldrsw {
                imm: ((val >> 10) & 0xFFF) as u16,
                rn: ((val >> 5) & 0x1F) as u8,
                rt: (val & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_cmp(val: u32) -> Option<Self> {
        if val & CMP_MASK == CMP_MASKED {
            Some(Instruction::Cmp {
                shift: ((val >> 22) & 0x3) as u8,
                imm: ((val >> 10) & 0xFFF) as u16,
                rn: ((val >> 5) & 0x1F) as u8,
            })
        } else {
            None
        }
    }

    fn u32_as_bcs(val: u32) -> Option<Self> {
        if val & BCS_MASK == BCS_MASKED {
            Some(Instruction::BCs {
                imm: ((val >> 5) & 0x7FFFF),
                cond: (val & 0xF) as u8,
            })
        } else {
            None
        }
    }

    fn from_u32(val: u32) -> Self {
        Self::u32_as_ldr(val)
            .or_else(|| Self::u32_as_add(val))
            .or_else(|| Self::u32_as_adrp(val))
            .or_else(|| Self::u32_as_ldur(val))
            .or_else(|| Self::u32_as_ldrb(val))
            .or_else(|| Self::u32_as_sub(val))
            .or_else(|| Self::u32_as_and(val))
            .or_else(|| Self::u32_as_cbz(val))
            .or_else(|| Self::u32_as_cmp(val))
            .or_else(|| Self::u32_as_bcs(val))
            .or_else(|| Self::u32_as_ldrsw(val))
            .unwrap_or(Self::Unk(val))
    }
}

pub fn adrp_get_imm(instr: u32) -> u32 {
    let immhi = (instr >> 5) & 0x7FFFF;
    let immlo = (instr >> 29) & 0x3;
    return (immhi << 14) + (immlo << 12);
}

pub fn add_get_imm(instr: u32) -> u32 {
    return (instr >> 10) & 0xFFF;
}