My favorites | Sign in
Project Logo
                
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/* patchmii_core -- low-level functions to handle the downloading, patching
and installation of updates on the Wii

Copyright (C) 2008 bushing / hackmii.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <ogcsys.h>
#include <gccore.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>
#include <network.h>
#include <sys/errno.h>

#include "patchmii_core.h"
#include "sha1.h"
#include "debug.h"
#include "http.h"
#include "haxx_certs.h"

#define VERSION "0.1"

// These parameters will download IOS37, modify it, and install it as IOS5
#define INPUT_TITLEID_H 1
#define INPUT_TITLEID_L 37
#define OUTPUT_TITLEID_H 1
#define OUTPUT_TITLEID_L 254

#define ALIGN(a,b) ((((a)+(b)-1)/(b))*(b))

int http_status = 0;
int tmd_dirty = 0, tik_dirty = 0;

void debug_printf(const char *fmt, ...) {
char buf[1024];
int len;
va_list ap;
usb_flush(1);
va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (len <= 0 || len > sizeof(buf)) printf("Error: len = %d\n", len);
else usb_sendbuffer(1, buf, len);
puts(buf);
}

char ascii(char s) {
if(s < 0x20) return '.';
if(s > 0x7E) return '.';
return s;
}

void hexdump(void *d, int len) {
u8 *data;
int i, off;
data = (u8*)d;
for (off=0; off<len; off += 16) {
debug_printf("%08x ",off);
for(i=0; i<16; i++)
if((i+off)>=len) debug_printf(" ");
else debug_printf("%02x ",data[off+i]);

debug_printf(" ");
for(i=0; i<16; i++)
if((i+off)>=len) debug_printf(" ");
else debug_printf("%c",ascii(data[off+i]));
debug_printf("\n");
}
}

char *spinner_chars="/-\\|";
int spin = 0;

void spinner(void) {
printf("\b%c", spinner_chars[spin++]);
if(!spinner_chars[spin]) spin=0;
}

static void *xfb = NULL;
static GXRModeObj *rmode = NULL;

void printvers(void) {
debug_printf("IOS Version: %08x\n", *((u32*)0xC0003140));
}

void console_setup(void) {
VIDEO_Init();
PAD_Init();

rmode = VIDEO_GetPreferredMode(NULL);

xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
VIDEO_ClearFrameBuffer(rmode,xfb,COLOR_BLACK);
VIDEO_Configure(rmode);
VIDEO_SetNextFramebuffer(xfb);
VIDEO_SetBlack(FALSE);
VIDEO_Flush();
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
CON_InitEx(rmode,20,30,rmode->fbWidth - 40,rmode->xfbHeight - 60);
}

int get_nus_object(u32 titleid1, u32 titleid2, char *content, u8 **outbuf, u32 *outlen) {
static char buf[128];
int retval;
u32 http_status;

snprintf(buf, 128, "http://nus.cdn.shop.wii.com/ccs/download/%08x%08x/%s",
titleid1, titleid2, content);

retval = http_request(buf, 1 << 31);
if (!retval) {
debug_printf("Error making http request\n");
return 1;
}

retval = http_get_result(&http_status, outbuf, outlen);
if (((int)*outbuf & 0xF0000000) == 0xF0000000) {
return (int) *outbuf;
}

return 0;
}

void decrypt_buffer(u16 index, u8 *source, u8 *dest, u32 len) {
static u8 iv[16];
if (!source) {
debug_printf("decrypt_buffer: invalid source paramater\n");
exit(1);
}
if (!dest) {
debug_printf("decrypt_buffer: invalid dest paramater\n");
exit(1);
}

memset(iv, 0, 16);
memcpy(iv, &index, 2);
aes_decrypt(iv, source, dest, len);
}

static u8 encrypt_iv[16];
void set_encrypt_iv(u16 index) {
memset(encrypt_iv, 0, 16);
memcpy(encrypt_iv, &index, 2);
}

void encrypt_buffer(u8 *source, u8 *dest, u32 len) {
aes_encrypt(encrypt_iv, source, dest, len);
}

int create_temp_dir(void) {
int retval;
retval = ISFS_CreateDir ("/tmp/patchmii", 0, 3, 1, 1);
if (retval) debug_printf("ISFS_CreateDir(/tmp/patchmii) returned %d\n", retval);
return retval;
}

u32 save_nus_object (u16 index, u8 *buf, u32 size) {
char filename[256];
static u8 bounce_buf[1024] ATTRIBUTE_ALIGN(0x20);
u32 i;

int retval, fd;
snprintf(filename, sizeof(filename), "/tmp/patchmii/%08x", index);

retval = ISFS_CreateFile (filename, 0, 3, 1, 1);

if (retval != ISFS_OK) {
debug_printf("ISFS_CreateFile(%s) returned %d\n", filename, retval);
return retval;
}

fd = ISFS_Open (filename, ISFS_ACCESS_WRITE);

if (fd < 0) {
debug_printf("ISFS_OpenFile(%s) returned %d\n", filename, fd);
return retval;
}

for (i=0; i<size;) {
u32 numbytes = ((size-i) < 1024)?size-i:1024;
memcpy(bounce_buf, buf+i, numbytes);
retval = ISFS_Write(fd, bounce_buf, numbytes);
if (retval < 0) {
debug_printf("ISFS_Write(%d, %p, %d) returned %d at offset %d\n",
fd, bounce_buf, numbytes, retval, i);
ISFS_Close(fd);
return retval;
}
i += retval;
}
ISFS_Close(fd);
return size;
}

s32 install_nus_object (tmd *p_tmd, u16 index) {
char filename[256];
static u8 bounce_buf1[1024] ATTRIBUTE_ALIGN(0x20);
static u8 bounce_buf2[1024] ATTRIBUTE_ALIGN(0x20);
u32 i;
const tmd_content *p_cr = TMD_CONTENTS(p_tmd);
debug_printf("install_nus_object(%p, %lu)\n", p_tmd, index);

int retval, fd, cfd, ret;
snprintf(filename, sizeof(filename), "/tmp/patchmii/%08x", p_cr[index].cid);

fd = ISFS_Open (filename, ISFS_ACCESS_READ);

if (fd < 0) {
debug_printf("ISFS_OpenFile(%s) returned %d\n", filename, fd);
return fd;
}
set_encrypt_iv(index);
debug_printf("ES_AddContentStart(%016llx, %x)\n", p_tmd->title_id, index);

cfd = ES_AddContentStart(p_tmd->title_id, p_cr[index].cid);
if(cfd < 0) {
debug_printf(":\nES_AddContentStart(%016llx, %x) failed: %d\n",p_tmd->title_id, index, cfd);
ES_AddTitleCancel();
return -1;
}
debug_printf(" (cfd %d): ",cfd);
for (i=0; i<p_cr[index].size;) {
u32 numbytes = ((p_cr[index].size-i) < 1024)?p_cr[index].size-i:1024;
numbytes = ALIGN(numbytes, 32);
retval = ISFS_Read(fd, bounce_buf1, numbytes);
if (retval < 0) {
debug_printf("ISFS_Read(%d, %p, %d) returned %d at offset %d\n",
fd, bounce_buf1, numbytes, retval, i);
ES_AddContentFinish(cfd);
ES_AddTitleCancel();
ISFS_Close(fd);
return retval;
}

encrypt_buffer(bounce_buf1, bounce_buf2, sizeof(bounce_buf1));
ret = ES_AddContentData(cfd, bounce_buf2, retval);
if (ret < 0) {
debug_printf("ES_AddContentData(%d, %p, %d) returned %d\n", cfd, bounce_buf2, retval, ret);
ES_AddContentFinish(cfd);
ES_AddTitleCancel();
ISFS_Close(fd);
return ret;
}
i += retval;
}

debug_printf(" done! (0x%x bytes)\n",i);
ret = ES_AddContentFinish(cfd);
if(ret < 0) {
printf("ES_AddContentFinish failed: %d\n",ret);
ES_AddTitleCancel();
ISFS_Close(fd);
return -1;
}

ISFS_Close(fd);

return 0;
}

int get_title_key(signed_blob *s_tik, u8 *key) {
static u8 iv[16] ATTRIBUTE_ALIGN(0x20);
static u8 keyin[16] ATTRIBUTE_ALIGN(0x20);
static u8 keyout[16] ATTRIBUTE_ALIGN(0x20);
int retval;

const tik *p_tik;
p_tik = (tik*)SIGNATURE_PAYLOAD(s_tik);
u8 *enc_key = (u8 *)&p_tik->cipher_title_key;
memcpy(keyin, enc_key, sizeof keyin);
memset(keyout, 0, sizeof keyout);
memset(iv, 0, sizeof iv);
memcpy(iv, &p_tik->titleid, sizeof p_tik->titleid);

retval = ES_Decrypt(ES_KEY_COMMON, iv, keyin, sizeof keyin, keyout);
if (retval) debug_printf("ES_Decrypt returned %d\n", retval);
memcpy(key, keyout, sizeof keyout);
return retval;
}

int change_ticket_title_id(signed_blob *s_tik, u32 titleid1, u32 titleid2) {
static u8 iv[16] ATTRIBUTE_ALIGN(0x20);
static u8 keyin[16] ATTRIBUTE_ALIGN(0x20);
static u8 keyout[16] ATTRIBUTE_ALIGN(0x20);
int retval;

tik *p_tik;
p_tik = (tik*)SIGNATURE_PAYLOAD(s_tik);
u8 *enc_key = (u8 *)&p_tik->cipher_title_key;
memcpy(keyin, enc_key, sizeof keyin);
memset(keyout, 0, sizeof keyout);
memset(iv, 0, sizeof iv);
memcpy(iv, &p_tik->titleid, sizeof p_tik->titleid);

retval = ES_Decrypt(ES_KEY_COMMON, iv, keyin, sizeof keyin, keyout);
p_tik->titleid = (u64)titleid1 << 32 | (u64)titleid2;
memset(iv, 0, sizeof iv);
memcpy(iv, &p_tik->titleid, sizeof p_tik->titleid);

retval = ES_Encrypt(ES_KEY_COMMON, iv, keyout, sizeof keyout, keyin);
if (retval) debug_printf("ES_Decrypt returned %d\n", retval);
memcpy(enc_key, keyin, sizeof keyin);
tik_dirty = 1;

return retval;
}

void change_tmd_title_id(signed_blob *s_tmd, u32 titleid1, u32 titleid2) {
tmd *p_tmd;
u64 title_id = titleid1;
title_id <<= 32;
title_id |= titleid2;
p_tmd = (tmd*)SIGNATURE_PAYLOAD(s_tmd);
p_tmd->title_id = title_id;
tmd_dirty = 1;
}

void display_tag(u8 *buf) {
debug_printf("Firmware version: %s Builder: %s\n",
buf, buf+0x30);
}

void display_ios_tags(u8 *buf, u32 size) {
u32 i;
char *ios_version_tag = "$IOSVersion:";

if (size == 64) {
display_tag(buf);
return;
}

for (i=0; i<(size-64); i++) {
if (!strncmp((char *)buf+i, ios_version_tag, 10)) {
char version_buf[128], *date;
while (buf[i+strlen(ios_version_tag)] == ' ') i++; // skip spaces
strlcpy(version_buf, (char *)buf + i + strlen(ios_version_tag), sizeof version_buf);
date = version_buf;
strsep(&date, "$");
date = version_buf;
strsep(&date, ":");
debug_printf("%s (%s)\n", version_buf, date);
i += 64;
}
}
}

int patch_hash_check(u8 *buf, u32 size) {
u32 i;
u32 match_count = 0;
u8 new_hash_check[] = {0x20,0x07,0x4B,0x0B};
u8 old_hash_check[] = {0x20,0x07,0x23,0xA2};

for (i=0; i<size-4; i++) {
if (!memcmp(buf + i, new_hash_check, sizeof new_hash_check)) {
debug_printf("Found new-school ES hash check @ 0x%x, patching.\n", i);
buf[i+1] = 0;
i += 4;
match_count++;
continue;
}

if (!memcmp(buf + i, old_hash_check, sizeof old_hash_check)) {
debug_printf("Found old-school ES hash check @ 0x%x, patching.\n", i);
buf[i+1] = 0;
i += 4;
match_count++;
continue;
}
}
return match_count;
}

int patch_new_dvdlowunencrypted(u8 *buf, u32 size)
{
u32 i;
u32 match_count = 0;
u8 old_table[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xD4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};
u8 new_table[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x46, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x7E, 0xD4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};

for (i=0; i<size-sizeof old_table; i++) {
if (!memcmp(buf + i, old_table, sizeof old_table)) {
debug_printf("Found new-school DVD_LowUnencryptedRead whitelist @ 0x%x, patching.\n", i);
memcpy(buf + i, new_table, sizeof new_table);
i += sizeof new_table;
match_count++;
continue;
}
}
return match_count;
}

void print_tmd_summary(const tmd *p_tmd) {
const tmd_content *p_cr;
p_cr = TMD_CONTENTS(p_tmd);

u32 size=0;

u16 i=0;
for(i=0;i<p_tmd->num_contents;i++) {
size += p_cr[i].size;
}

debug_printf("Title ID: %016llx\n",p_tmd->title_id);
debug_printf("Number of parts: %d. Total size: %uK\n", p_tmd->num_contents, (u32) (size / 1024));
}

void zero_sig(signed_blob *sig) {
u8 *sig_ptr = (u8 *)sig;
memset(sig_ptr + 4, 0, SIGNATURE_SIZE(sig)-4);
}

void brute_tmd(tmd *p_tmd) {
u16 fill;
for(fill=0; fill<65535; fill++) {
p_tmd->fill3=fill;
sha1 hash;
// debug_printf("SHA1(%p, %x, %p)\n", p_tmd, TMD_SIZE(p_tmd), hash);
SHA1((u8 *)p_tmd, TMD_SIZE(p_tmd), hash);;

if (hash[0]==0) {
// debug_printf("setting fill3 to %04hx\n", fill);
return;
}
}
printf("Unable to fix tmd :(\n");
exit(4);
}

void brute_tik(tik *p_tik) {
u16 fill;
for(fill=0; fill<65535; fill++) {
p_tik->padding=fill;
sha1 hash;
// debug_printf("SHA1(%p, %x, %p)\n", p_tmd, TMD_SIZE(p_tmd), hash);
SHA1((u8 *)p_tik, sizeof(tik), hash);

if (hash[0]==0) return;
}
printf("Unable to fix tik :(\n");
exit(5);
}

void forge_tmd(signed_blob *s_tmd) {
debug_printf("forging tmd sig\n");
zero_sig(s_tmd);
brute_tmd(SIGNATURE_PAYLOAD(s_tmd));
}

void forge_tik(signed_blob *s_tik) {
debug_printf("forging tik sig\n");
zero_sig(s_tik);
brute_tik(SIGNATURE_PAYLOAD(s_tik));
}

#define BLOCK 0x1000

s32 install_ticket(const signed_blob *s_tik, const signed_blob *s_certs, u32 certs_len) {
u32 ret;

debug_printf("Installing ticket...\n");
ret = ES_AddTicket(s_tik,STD_SIGNED_TIK_SIZE,s_certs,certs_len, NULL, 0);
if (ret < 0) {
debug_printf("ES_AddTicket failed: %d\n",ret);
return ret;
}
return 0;
}

s32 install(const signed_blob *s_tmd, const signed_blob *s_certs, u32 certs_len) {
u32 ret, i;
tmd *p_tmd = SIGNATURE_PAYLOAD(s_tmd);
debug_printf("Adding title...\n");

ret = ES_AddTitleStart(s_tmd, SIGNED_TMD_SIZE(s_tmd), s_certs, certs_len, NULL, 0);

if(ret < 0) {
debug_printf("ES_AddTitleStart failed: %d\n",ret);
ES_AddTitleCancel();
return ret;
}

for(i=0; i<p_tmd->num_contents; i++) {
debug_printf("Adding content ID %08x", i);
ret = install_nus_object((tmd *)SIGNATURE_PAYLOAD(s_tmd), i);
if (ret) return ret;
}

ret = ES_AddTitleFinish();
if(ret < 0) {
printf("ES_AddTitleFinish failed: %d\n",ret);
ES_AddTitleCancel();
return ret;
}

printf("Installation complete!\n");
return 0;

}

int main(int argc, char **argv) {

console_setup();
printf("PatchMii Core v" VERSION ", by bushing\n");

// ******* WARNING *******
// Obviously, if you're reading this, you're obviously capable of disabling the
// following checks. If you put any of the following titles into an unusuable state,
// your Wii will fail to boot:
//
// 1-1 (BOOT2), 1-2 (System Menu), 1-30 (IOS30, currently specified by 1-2's TMD)
// Corrupting other titles (for example, BC or the banners of installed channels)
// may also cause difficulty booting. Please do not remove these safety checks
// unless you have performed extensive testing and are willing to take on the risk
// of bricking the systems of people to whom you give this code. -bushing

if ((OUTPUT_TITLEID_H == 1) && (OUTPUT_TITLEID_L == 2)) {
printf("Sorry, I won't modify the system menu; too dangerous. :(\n");
while(1);
}

if ((OUTPUT_TITLEID_H == 1) && (OUTPUT_TITLEID_L == 30)) {
printf("Sorry, I won't modify IOS30; too dangerous. :(\n");
while(1);
}

printvers();

int retval;

if (ISFS_Initialize() || create_temp_dir()) {
perror("Failed to create temp dir: ");
exit(1);
}

printf("Initializing network."); fflush(stdout);
while (1) {
retval = net_init ();
if (retval < 0) {
if (retval != -EAGAIN) {
debug_printf ("net_init failed: %d\n", retval);
exit(0);
}
}
if (!retval) break;
usleep(100000);
printf("."); fflush(stdout);
}

printf("Done!\n");
signed_blob *s_tmd = NULL, *s_tik = NULL, *s_certs = NULL;

u8 *temp_tmdbuf = NULL, *temp_tikbuf = NULL;

static u8 tmdbuf[MAX_SIGNED_TMD_SIZE] ATTRIBUTE_ALIGN(0x20);
static u8 tikbuf[STD_SIGNED_TIK_SIZE] ATTRIBUTE_ALIGN(0x20);

u32 tmdsize;
int update_tmd;

debug_printf("Downloading IOS%d metadata: ..", INPUT_TITLEID_L);
retval = get_nus_object(INPUT_TITLEID_H, INPUT_TITLEID_L, "tmd", &temp_tmdbuf, &tmdsize);
if (retval<0) {
debug_printf("get_nus_object(tmd) returned %d, tmdsize = %u\n", retval, tmdsize);
exit(1);
}
if (temp_tmdbuf == NULL) {
debug_printf("Failed to allocate temp buffer for encrypted content, size was %u\n", tmdsize);
exit(1);
}
memcpy(tmdbuf, temp_tmdbuf, MIN(tmdsize, sizeof(tmdbuf)));
free(temp_tmdbuf);

s_tmd = (signed_blob *)tmdbuf;
if(!IS_VALID_SIGNATURE(s_tmd)) {
debug_printf("Bad TMD signature!\n");
exit(1);
}

debug_printf("\b ..tmd..");

u32 ticketsize;
retval = get_nus_object(INPUT_TITLEID_H, INPUT_TITLEID_L,
"cetk", &temp_tikbuf, &ticketsize);

if (retval < 0) debug_printf("get_nus_object(cetk) returned %d, ticketsize = %u\n", retval, ticketsize);
memcpy(tikbuf, temp_tikbuf, MIN(ticketsize, sizeof(tikbuf)));

s_tik = (signed_blob *)tikbuf;
if(!IS_VALID_SIGNATURE(s_tik)) {
debug_printf("Bad tik signature!\n");
exit(1);
}

free(temp_tikbuf);

s_certs = (signed_blob *)haxx_certs;
if(!IS_VALID_SIGNATURE(s_certs)) {
debug_printf("Bad cert signature!\n");
exit(1);
}

debug_printf("\b ..ticket..");

u8 key[16];
get_title_key(s_tik, key);
aes_set_key(key);

const tmd *p_tmd;
tmd_content *p_cr;
p_tmd = (tmd*)SIGNATURE_PAYLOAD(s_tmd);
p_cr = TMD_CONTENTS(p_tmd);

print_tmd_summary(p_tmd);

debug_printf("Downloading contents: \n");
static char cidstr[32];
u16 i;
for (i=0;i<p_tmd->num_contents;i++) {
debug_printf("Downloading part %d/%d (%uK): ", i+1,
p_tmd->num_contents, p_cr[i].size / 1024);
sprintf(cidstr, "%08x", p_cr[i].cid);

u8 *content_buf, *decrypted_buf;
u32 content_size;

retval = get_nus_object(INPUT_TITLEID_H, INPUT_TITLEID_L, cidstr, &content_buf, &content_size);
if (retval < 0) {
debug_printf("get_nus_object(%s) failed with error %d, content size = %u\n",
cidstr, retval, content_size);
exit(1);
}

if (content_buf == NULL) {
debug_printf("error allocating content buffer, size was %u\n", content_size);
exit(1);
}

if (content_size % 16) {
debug_printf("ERROR: downloaded content[%hu] size %u is not a multiple of 16\n",
i, content_size);
free(content_buf);
exit(1);
}

if (content_size < p_cr[i].size) {
debug_printf("ERROR: only downloaded %u / %llu bytes\n", content_size, p_cr[i].size);
free(content_buf);
exit(1);
}

decrypted_buf = malloc(content_size);
if (!decrypted_buf) {
debug_printf("ERROR: failed to allocate decrypted_buf (%u bytes)\n", content_size);
free(content_buf);
exit(1);
}

decrypt_buffer(i, content_buf, decrypted_buf, content_size);

sha1 hash;
SHA1(decrypted_buf, p_cr[i].size, hash);

if (!memcmp(p_cr[i].hash, hash, sizeof hash)) {
debug_printf("\b hash OK. ");
display_ios_tags(decrypted_buf, content_size);

update_tmd = 0;
if(patch_hash_check(decrypted_buf, content_size))
update_tmd = 1;
if(patch_new_dvdlowunencrypted(decrypted_buf, content_size))
update_tmd = 1;
if(update_tmd == 1) {
debug_printf("Updating TMD.\n");
SHA1(decrypted_buf, p_cr[i].size, hash);
memcpy(p_cr[i].hash, hash, sizeof hash);
tmd_dirty=1;
}

retval = (int) save_nus_object(p_cr[i].cid, decrypted_buf, content_size);
if (retval < 0) {
debug_printf("save_nus_object(%x) returned error %d\n", p_cr[i].cid, retval);
exit(1);
}
} else {
debug_printf("hash BAD\n");
exit(1);
}

free(decrypted_buf);
free(content_buf);
}

if ((INPUT_TITLEID_H != OUTPUT_TITLEID_H)
|| (INPUT_TITLEID_L != OUTPUT_TITLEID_L)) {
debug_printf("Changing titleid from %08x-%08x to %08x-%08x\n",
INPUT_TITLEID_H, INPUT_TITLEID_L,
OUTPUT_TITLEID_H, OUTPUT_TITLEID_L);
change_ticket_title_id(s_tik, OUTPUT_TITLEID_H, OUTPUT_TITLEID_L);
change_tmd_title_id(s_tmd, OUTPUT_TITLEID_H, OUTPUT_TITLEID_L);
}

if (tmd_dirty) {
forge_tmd(s_tmd);
tmd_dirty = 0;
}

if (tik_dirty) {
forge_tik(s_tik);
tik_dirty = 0;
}

debug_printf("Download complete. Installing:\n");

retval = install_ticket(s_tik, s_certs, haxx_certs_size);
if (retval) {
debug_printf("install_ticket returned %d\n", retval);
exit(1);
}

retval = install(s_tmd, s_certs, haxx_certs_size);

if (retval) {
debug_printf("install returned %d\n", retval);
exit(1);
}

debug_printf("Done!\n");

exit(0);
}
Show details Hide details

Change log

r8 by svenpeter on Jul 21, 2008   Diff
added in patch from bushing: fixed a bug
where we were confusing content ids with
tmd indices
Go to: 
Project members, sign in to write a code review

Older revisions

r7 by svenpeter on Jul 18, 2008   Diff
fixed possible bug where patchmii
would try reading past the buffer
after one match has been found

thanks to dhewg for finding this.
r6 by svenpeter on Jul 17, 2008   Diff
allow multiple patches to one file
r5 by bushing on Jul 13, 2008   Diff
Added in patch from svpe, inspired by
nitrotux:
    patchmii_core:
DVD_LowUnencryptedRead() patch for
IOS30+
...
All revisions of this file

File info

Size: 20460 bytes, 748 lines
Hosted by Google Code