-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff_injector.c
364 lines (300 loc) · 7.8 KB
/
ff_injector.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
int patch_branch( uint32_t from, uint32_t to, uint8_t *pmem )
{
uint32_t offset = (to-from-4);
uint16_t i1 = 0xF000 | ( (offset>>12) & 0x7FF );
uint16_t i2 = 0xF800 | ( (offset>>1) & 0x7FF );
pmem[0] = (i1&0xFF);
pmem[1] = (i1>>8);
pmem[2] = (i2&0xFF);
pmem[3] = (i2>>8);
printf( "%08X: bl %08X [ %02X %02X %02X %02X ]\n",
from, to, pmem[0], pmem[1], pmem[2], pmem[3] );
return 4;
}
int patch_addr( uint32_t addr, uint8_t *pmem )
{
addr |= 0x00000001;
pmem[0] = (addr>>0)&0xFF;
pmem[1] = (addr>>8)&0xFF;
pmem[2] = (addr>>16)&0xFF;
pmem[3] = (addr>>24)&0xFF;
printf( ": %02X %02X %02X %02X\n", pmem[0], pmem[1], pmem[2], pmem[3] );
return 4;
}
int is_brunch( uint8_t *pmem )
{
if( ( pmem[1] & 0xF8 ) != 0xF0 )
return 0;
if( ( pmem[3] & 0xF8 ) != 0xF8 )
return 0;
return 1;
}
/* TODO: make pattern matcher */
void *load_file( char *psz_path, int *size, int alloc_size )
{
void *mem = 0;
int file_size;
FILE *f = fopen( psz_path, "rb" );
if( !f )
return 0;
fseek( f, 0, SEEK_END );
file_size = ftell( f );
rewind( f );
if( alloc_size < file_size )
alloc_size = file_size;
/* tooo big */
if( file_size <= (1024*1024) )
{
mem = malloc( alloc_size );
assert( mem );
memset( mem, 0xFF, alloc_size );
fread( mem, file_size, 1, f );
}
fclose( f );
*size = alloc_size;
return mem;
}
uint8_t hex2int(char ch)
{
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
return 0;
}
int find_signature( uint8_t *mem, int size, char *psz_sig )
{
uint8_t sig_bin[128] = { 0 };
uint8_t sig_mask_bin[128] = { 0 };
int len = strlen( psz_sig );
if( len & 1 )
return -1;
/* compile signature */
for( int i = 0; i < len; i += 2 )
{
sig_mask_bin[i/2] = psz_sig[i+0] == '?' ? 0: 0xF0;
sig_mask_bin[i/2] |= psz_sig[i+1] == '?' ? 0: 0x0F;
sig_bin[i/2] = hex2int( psz_sig[i+0] )<<4;
sig_bin[i/2] |= hex2int( psz_sig[i+1] );
}
len = len / 2;
if( size < len )
return -1;
int n = 0;
for( int i = 0; i < (size-len); i++ )
{
for( n = 0; n < len; n ++ )
{
if( ( mem[i+n] & sig_mask_bin[n] ) != sig_bin[n] )
break;
}
/* found ? */
if( n == len )
{
return i;
}
}
return -1;
}
#define FLASH_BASE ( 0x08010000 )
#define FIRMWARE_FLASH_SIZE ((1024-64)*1024)
#define TIM9_ISR_OFFSET ( 0x000000A0 )
int main(void)
{
uint8_t *p;
int size = 0;
char buffer[0x100], *s;
uint32_t isr_addr = 0, m106_addr = 0, m107_addr = 0;
uint32_t sec_addr = 0, sec_size = 0;
int res;
/* read addresses from map file */
FILE *map;
map = fopen( "patch.map", "r" );
if( !map )
{
printf( "no map file was found\n" );
return -1;
}
int next_line_is_sec = 0;
while( feof( map ) == 0 )
{
s = fgets( buffer, sizeof( buffer ), map );
if( !s )
continue;
if( next_line_is_sec )
{
next_line_is_sec = 0;
sec_addr = strtoll( s, &s, 16 );
sec_size = strtoll( s, &s, 16 );
continue;
}
if( strncmp( s, ".patch_location", 15 ) == 0 )
{
next_line_is_sec = 1;
}
else if( strstr( s, "hook_m106" ) )
{
m106_addr = strtoll( s, 0, 16 );
}
else if( strstr( s, "hook_m107" ) )
{
m107_addr = strtoll( s, 0, 16 );
}
else if( strstr( s, "TIM1_BRK_TIM9_IRQHandler" ) )
{
isr_addr = strtoll( s, 0, 16 );
}
}
fclose( map );
if( !isr_addr || !m106_addr || !m107_addr || !sec_addr )
{
printf( "not all addresses was resolved\n" );
return -2;
}
printf( "Map ( 0x%08X %u ):\n", sec_addr, sec_size );
printf( "\tISR: 0x%08X\n", isr_addr );
printf( "\tm106_hook: 0x%08X\n", m106_addr );
printf( "\tm107_hook: 0x%08X\n", m107_addr );
p = load_file( FW_NAME ".bin", &size, FIRMWARE_FLASH_SIZE );
if( !p )
{
printf( "no file was found\n" );
return -1;
}
#if 1
/* remove rear case control from G-Code */
static char rear_fan_control_on_sig[] =
"0120" /* MOVS R0, #1 */
"??49" /* LDR R1, =g_case_fan_state */
"0870" /* STRB R0, [R1] => g_case_fan_state = 1 */
"0221" /* MOVS R1, #2 => BIT = 2 */
"??48" /* LDR R0, =GPIOE => PORT = GPIOE */
"??F???F?" /* CALL GPIO_SET_BITS */
;
res = find_signature( p, size, rear_fan_control_on_sig );
if( res > 0 && is_brunch( p + res + 10 ) )
{
printf( "Rear case fan ON: 0x%08X\n", FLASH_BASE + res );
/* patch CALL GPIO_SET_BITS to NOP NOP */
memcpy( p + res + 10, "\x00\xbf\x00\xbf", 4 );
}
static char rear_fan_control_off_sig[] =
"0020" /* MOVS R0, #0 */
"??49" /* LDR R1, =g_case_fan_state */
"0870" /* STRB R0, [R1] => g_case_fan_state = 0 */
"0221" /* MOVS R1, #2 => BIT = 2 */
"??48" /* LDR R0, =GPIOE => PORT = GPIOE */
"??F???F?" /* CALL GPIO_RESET_BITS */
;
res = find_signature( p, size, rear_fan_control_off_sig );
if( res > 0 && is_brunch( p + res + 10 ) )
{
printf( "Rear case fan OFF: 0x%08X\n", FLASH_BASE + res );
/* patch CALL GPIO_RESET_BITS to NOP NOP */
memcpy( p + res + 10, "\x00\xbf\x00\xbf", 4 );
}
#endif
#if 1
/* G-Code temp checks patch */
static char check_temp_sig[] =
"0228" /* CMP R0, #2 - ctemp sensor id is 2 ( BED ) */
"01D1" /* BNE jump_if_sensor_id_not_equal_to_2 */
"??20" /* MOVS R0, #0x82 - set R0 to 130°C ( temp limit for bed ) */
"00E0" /* B check_temp */
"??20" /* MOVS R0, #0xF8 - set R0 to 248°C ( temp limit for hotend ) */
;
/*
change it to:
"5FF48C70" - MOVS.W R0, #280 - global temp limit for hotend and bed now 280°C
"00BF" - NOP
*/
res = find_signature( p, size, check_temp_sig );
if( res > 0 )
{
printf( "Temp checks found at: 0x%08X\n", FLASH_BASE + res );
/* patch it */
memcpy( p + res, "\x5f\xf4\x8c\x70\x00\xbf", 6 );
}
#endif
#if defined( M106_CALL_ADDRESS ) && defined( M107_CALL_ADDRESS )
/* all addresses from MAP file */
/* TIM1_BRK_TIM9_IRQHandler */
patch_addr( isr_addr, &p[TIM9_ISR_OFFSET] );
/* M106 => hook_m106 */
uint32_t bl_loc;
bl_loc = M106_CALL_ADDRESS;
if( !is_brunch( &p[bl_loc-FLASH_BASE] ) )
{
printf( "%08X hook invalid fw address\n", bl_loc );
}
else
{
patch_branch( bl_loc, m106_addr, &p[bl_loc-FLASH_BASE] );
}
/* M107 => hook_m107 */
bl_loc = M107_CALL_ADDRESS;
if( !is_brunch( &p[bl_loc-FLASH_BASE] ) )
{
printf( "%08X hook invalid fw address\n", bl_loc );
}
else
{
patch_branch( bl_loc, m107_addr, &p[bl_loc-FLASH_BASE] );
}
/* copy firmware patch payload to image */
int payload_size = 0;
uint8_t *p_payload;
p_payload = load_file( "patch.bin", &payload_size, 0 );
assert( p_payload );
memcpy( &p[sec_addr-FLASH_BASE], &p_payload[sec_addr-FLASH_BASE], sec_size );
free( p_payload );
#endif
FILE *f = fopen( FW_NAME "_patched.bin", "wb" );
if( !f )
{
printf( "output file could not be created\n" );
}
else
{
fwrite( p, size, 1, f );
fclose( f );
}
#if 0
/*
try to find all useful patterns like that:
MOVS R1, #0x20 : 20 21
LDR R0, =GPIOF : XX 48
CALL GPIO_XXX : XX F0 XX XX
*/
static char call_gpio_api[] = "2021??48??F0????";
int offset = 0;
for( ;offset < size; )
{
int res;
res = find_signature( p + offset, size - offset, call_gpio_api );
if( res < 0 )
break;
offset += res;
/* check io port address */
/* GPIOF = 0x40021400 */
uint32_t inst_addr = offset;
uint32_t port_offset = ( offset + 2 + p[offset+2]*4 + 4 ) & (~3);
offset += 8;
if( (*(uint32_t*)(p + port_offset )) != 0x40021400 )
{
continue;
}
printf( "ADDR: 0x%08X\n", FLASH_BASE + inst_addr );
}
#endif
free( p );
printf( "done\n" );
return 0;
}