-
Notifications
You must be signed in to change notification settings - Fork 1
/
ptpatcher.c
352 lines (284 loc) · 8.22 KB
/
ptpatcher.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
/* ptpatcher.c
* patchin' PTs since 2012.
*
* Copyright (C) 2012 AndroidRoot.mobi
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ptpatcher.h"
#ifdef __arm__
#define DEV_MMCBLK_DEVICE "/dev/block/mmcblk0"
#define DEV_MMCBLK_BOOT0 "/dev/block/mmcblk0boot0"
#define DEV_MMCBLK_BOOT1 "/dev/block/mmcblk0boot1"
#define DEV_MMCBLK_BOOT0_PROTECT "/sys/block/mmcblk0boot0/force_ro"
#define DEV_MMCBLK_BOOT1_PROTECT "/sys/block/mmcblk0boot1/force_ro"
#include "nvaes.h"
#else
/* TODO: Allow these to be overriden by getopt() with, say, --pt, -T */
#define DEV_MMCBLK_DEVICE "blk0.img"
#define DEV_MMCBLK_BOOT0 "boot0.img"
#define DEV_MMCBLK_BOOT1 "boot1.img"
#include "aes-cmac.h"
#endif
static int FOUND_PT_OFFSET = 0;
static int sign_pt(unsigned char *pt, unsigned char *signature)
{
pt_hdr_t *pt_hdr = (pt_hdr_t *)pt;
#ifdef __arm__
char key[AES_BLOCK_SIZE] = {0};
nvaes_ctx ctx;
if(!(ctx = nvaes_open())) {
perror("failed to open AES engine");
return 0;
}
nvaes_set_key(ctx, key);
nvaes_sign(ctx, &pt[sizeof(pt_hdr_t)], pt_hdr->length-sizeof(pt_hdr_t),
signature);
nvaes_close(ctx);
#else
cmac_hash(&pt[sizeof(pt_hdr_t)],pt_hdr->length-sizeof(pt_hdr_t),signature);
#endif
return 1;
}
static int read_pt(unsigned char *pt)
{
int offset = PT_OFFSET, i = 0;
unsigned int remaining = PT_SIZE, bytes = 0;
FILE *fp;
for(i=0; i<3; i++) {
char filename[128] = {0};
switch(i) {
case 0: strcpy(filename, DEV_MMCBLK_BOOT0); break;
case 1: strcpy(filename, DEV_MMCBLK_BOOT1); break;
case 2: strcpy(filename, DEV_MMCBLK_DEVICE); break;
}
if(!(fp = fopen(filename, "rb"))) {
perror("failed to open file");
return 1;
}
if(i < 2) {
fseek(fp, 0, SEEK_END);
offset -= ftell(fp);
if(offset >= 0) {
fclose(fp);
continue;
}
fseek(fp, offset, SEEK_CUR);
}
bytes = fread(pt + (PT_SIZE - remaining), sizeof(unsigned char),
remaining, fp);
fclose(fp);
remaining -= bytes;
if(remaining == 0) break;
}
/*
fp = fopen("pt", "wb");
fwrite(pt, sizeof(unsigned char), PT_SIZE, fp);
fclose(fp);
*/
return remaining == 0;
}
static int write_pt(unsigned char *pt)
{
int offset = PT_OFFSET, i = 0;
unsigned int remaining = PT_SIZE, bytes = 0;
FILE *fp;
for(i=0; i<3; i++) {
char filename[128] = {0};
switch(i) {
case 0: strcpy(filename, DEV_MMCBLK_BOOT0); break;
case 1: strcpy(filename, DEV_MMCBLK_BOOT1); break;
case 2: strcpy(filename, DEV_MMCBLK_DEVICE); break;
}
if(!(fp = fopen(filename, "wb+"))) {
perror("failed to open file");
return 0;
}
if(i < 2) {
fseek(fp, 0, SEEK_END);
offset -= ftell(fp);
if(offset >= 0) {
fclose(fp);
continue;
}
fseek(fp, offset, SEEK_CUR);
}
bytes = fwrite(pt + (PT_SIZE - remaining), sizeof(unsigned char),
remaining, fp);
fclose(fp);
remaining -= bytes;
if(remaining == 0) break;
}
return remaining == 0;
}
static int iterate_pt(unsigned char *pt, pt_iterator_callback_t callback)
{
unsigned int num_pts = PT_SIZE / PT_RECORD_SIZE, i = 0;
for(i = 0; i < num_pts; i++) {
if(!callback(&pt[i * PT_RECORD_SIZE])) {
return 0;
}
}
return 1;
}
static int verify_pt(unsigned char *pt)
{
unsigned char signature[PT_SIGNATURE_SIZE];
pt_hdr_t *pt_hdr = (pt_hdr_t *)pt;
pt_hdr_inner_t *pt_hdr_inner = (pt_hdr_inner_t *)&pt[sizeof(pt_hdr_t)];
if(pt_hdr->magic != PT_MAGIC || pt_hdr->version != PT_VERSION) {
fprintf(stderr, "invalid PT image");
return 0;
}
if(pt_hdr_inner->magic != PT_MAGIC || pt_hdr_inner->version != PT_VERSION) {
fprintf(stderr, "invalid PT image (inner)");
return 0;
}
sign_pt(pt, signature);
if(memcmp(pt_hdr->signature, signature, PT_SIGNATURE_SIZE)) {
fprintf(stderr, "invalid PT signature");
return 0;
}
return 1;
}
static int patch_pt(unsigned char *pt)
{
unsigned char *ptr = pt;
int i = 0;
pt_hdr_t *pt_hdr = (pt_hdr_t *)ptr;
pt_hdr_inner_t *pt_hdr_inner = (pt_hdr_inner_t *)&ptr[sizeof(pt_hdr_t)];
pt_part_t *pt_parts = (pt_part_t *)&ptr[sizeof(pt_hdr_t) +
sizeof(pt_hdr_inner_t)];
for(i = 0; i < pt_hdr_inner->num; i++) {
if(!strcmp((&pt_parts[i])->name, PT_PATCH_OLD)) {
if(!strcmp((&pt_parts[i])->alias, PT_PATCH_OLD)) {
strcpy((&pt_parts[i])->name, PT_PATCH_NEW);
strcpy((&pt_parts[i])->alias, PT_PATCH_NEW);
}
}
}
return sign_pt(pt, pt_hdr->signature);
}
#ifdef __arm__
static int toggle_pt_protection(char mode)
{
FILE *fp;
char cmd[3] = {mode, '\n', 0};
if(mode != '0' && mode != '1') {
fprintf(stderr, "invalid mode: %c\n", mode);
return 0;
}
fp = fopen(DEV_MMCBLK_BOOT0_PROTECT, "w");
if(fwrite(cmd, sizeof(char), sizeof(cmd), fp) != sizeof(cmd)) {
return 0;
}
fclose(fp);
fp = fopen(DEV_MMCBLK_BOOT1_PROTECT, "w");
if(fwrite(cmd, sizeof(char), sizeof(cmd), fp) != sizeof(cmd)) {
return 0;
}
fclose(fp);
return 1;
}
static int restore_pt(unsigned char *pt)
{
/* Helper for main() since it's called from two locations. */
printf("attempting to restore original pt...");
if(!write_pt(pt)) {
printf("failed!\n\n");
printf("IMPORTANT!! YOUR DEVICE COULD BE IN A BRICKING STATE\n");
printf("POWER THE DEVICE FROM A MAINS ADAPTER\n");
printf("AND SEEK HELP FROM IRC IMMEDIATELY!\n\n");
(void)toggle_pt_protection('1');
return 1;
}
printf("done\n");
(void)toggle_pt_protection('1');
return 0;
}
#endif
int main(int argc, const char **argv)
{
unsigned char *pt = NULL, *pt_orig = NULL;
int consumed = 0;
FILE *fp;
setvbuf(stdout,NULL,_IONBF,0);
printf("ptpatcher\n");
printf("---------\n\n");
if(!(pt = (unsigned char *)malloc(PT_SIZE))) {
perror("failed to allocate pt memory");
return 1;
}
if(!(pt_orig = (unsigned char *)malloc(PT_SIZE))) {
perror("failed to allocate pt memory");
return 1;
}
printf("reading pt...");
if(!read_pt(pt)) {
printf("failed!\n");
return 1;
}
memcpy(pt_orig, pt, PT_SIZE);
printf("done\n");
printf("verifying pt...");
if(!iterate_pt(pt, &verify_pt)) {
printf("failed!\n");
return 1;
}
printf("done\n");
printf("patching pt...");
if(!iterate_pt(pt, &patch_pt)) {
printf("failed!\n");
return 1;
}
printf("done\n");
printf("verifying patched pt...");
if(!iterate_pt(pt, &verify_pt)) {
printf("failed!\n");
return 1;
}
printf("done\n");
#ifdef __arm__
fp = NULL; // Prevent variable unused warnings.
printf("writing pt...");
if(!toggle_pt_protection('0')) {
printf("failed.\n");
return 1;
}
if(!write_pt(pt)) {
printf("failed!\n");
return restore_pt(pt_orig);
}
printf("done\n");
printf("reading updated pt...");
memset(pt, 0, PT_SIZE);
if(!read_pt(pt)) {
printf("failed!\n");
return restore_pt(pt_orig);
}
printf("done\n");
printf("verifying updated pt...");
if(!iterate_pt(pt, &verify_pt)) {
printf("failed!\n");
return restore_pt(pt_orig);
}
printf("done\n");
(void)toggle_pt_protection('1');
#else
if(argc > 1) {
if(!(fp = fopen(argv[1], "wb"))) {
perror("failed to open output file");
return 1;
}
fwrite(pt, sizeof(unsigned char), PT_SIZE, fp);
fclose(fp);
printf("wrote patched pt to: %s\n", argv[1]);
} else {
fprintf(stderr, "no argument given - not outputting patched pt!\n");
return 1;
}
#endif
return 0;
}