forked from mondul/HiyaCFW-Helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HiyaCFW_Helper.py
executable file
·1014 lines (723 loc) · 34.2 KB
/
HiyaCFW_Helper.py
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# hiyaCFW Helper
# Version 3.6.1
# Author: mondul <mondul@huyzona.com>
from tkinter import (Tk, Frame, LabelFrame, PhotoImage, Button, Entry, Checkbutton, Radiobutton,
Label, Toplevel, Scrollbar, Text, StringVar, IntVar, RIGHT, W, X, Y, DISABLED, NORMAL, SUNKEN,
END)
from tkinter.messagebox import askokcancel, showerror, showinfo, WARNING
from tkinter.filedialog import askopenfilename, askdirectory
from platform import system
from os import path, remove, chmod, listdir
from sys import exit
from threading import Thread
from queue import Queue, Empty
from hashlib import sha1
from urllib.request import urlopen
from urllib.error import URLError
from subprocess import Popen
from struct import unpack_from
from shutil import rmtree, copyfile, copyfileobj
from distutils.dir_util import copy_tree, _path_created
####################################################################################################
# Thread-safe text class
class ThreadSafeText(Text):
def __init__(self, master, **options):
Text.__init__(self, master, **options)
self.queue = Queue()
self.update_me()
def write(self, line):
self.queue.put(line)
def update_me(self):
try:
while 1:
self.insert(END, str(self.queue.get_nowait()) + '\n')
self.see(END)
self.update_idletasks()
except Empty:
pass
self.after(500, self.update_me)
####################################################################################################
# Main application class
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
# First row
f1 = LabelFrame(self, text='NAND file with No$GBA footer', padx=10, pady=10)
# NAND Button
self.nand_mode = False
nand_icon = PhotoImage(data=('R0lGODlhEAAQAIMAAAAAADMzM2ZmZpmZmczMzP///wAAAAAAAAA'
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAMAAAYALAAAAAAQAB'
'AAAARG0MhJaxU4Y2sECAEgikE1CAFRhGMwSMJwBsU6frIgnR/bv'
'hTPrWUSDnGw3JGU2xmHrsvyU5xGO8ql6+S0AifPW8kCKpcpEQA7'))
self.nand_button = Button(f1, image=nand_icon, command=self.change_mode, state=DISABLED)
self.nand_button.image = nand_icon
self.nand_button.pack(side='left')
self.nand_file = StringVar()
Entry(f1, textvariable=self.nand_file, state='readonly', width=40).pack(side='left')
Button(f1, text='...', command=self.choose_nand).pack(side='left')
f1.pack(padx=10, pady=10, fill=X)
# Second row
f2 = Frame(self)
# Check boxes
self.checks_frame = Frame(f2)
# Install TWiLight check
self.twilight = IntVar()
self.twilight.set(1)
twl_chk = Checkbutton(self.checks_frame,
text='Install latest TWiLight Menu++ on custom firmware', variable=self.twilight)
twl_chk.pack(padx=10, anchor=W)
# Clean files check
self.clean_downloaded = IntVar()
self.clean_downloaded.set(1)
clean_chk = Checkbutton(self.checks_frame, text='Clean downloaded files after completion',
variable=self.clean_downloaded)
clean_chk.pack(padx=10, anchor=W)
self.checks_frame.pack(fill=X)
# NAND operation frame
self.nand_frame = LabelFrame(f2, text='NAND operation', padx=10, pady=10)
self.nand_operation = IntVar()
self.nand_operation.set(0)
Radiobutton(self.nand_frame, text='Remove No$GBA footer', variable=self.nand_operation,
value=0, command=lambda: self.enable_entries(False)).pack(anchor=W)
Radiobutton(self.nand_frame, text='Add No$GBA footer', variable=self.nand_operation,
value=1, command=lambda: self.enable_entries(True)).pack(anchor=W)
fl = Frame(self.nand_frame)
self.cid_label = Label(fl, text='eMMC CID', state=DISABLED)
self.cid_label.pack(anchor=W, padx=(24, 0))
self.cid = StringVar()
self.cid_entry = Entry(fl, textvariable=self.cid, width=20, state=DISABLED)
self.cid_entry.pack(anchor=W, padx=(24, 0))
fl.pack(side='left')
fr = Frame(self.nand_frame)
self.console_id_label = Label(fr, text='Console ID', state=DISABLED)
self.console_id_label.pack(anchor=W)
self.console_id = StringVar()
self.console_id_entry = Entry(fr, textvariable=self.console_id, width=20, state=DISABLED)
self.console_id_entry.pack(anchor=W)
fr.pack(side='right')
f2.pack(fill=X)
# Third row
f3 = Frame(self)
self.start_button = Button(f3, text='Start', width=16, command=self.hiya, state=DISABLED)
self.start_button.pack(side='left', padx=(0, 5))
Button(f3, text='Quit', command=root.destroy, width=16).pack(side='left', padx=(5, 0))
f3.pack(pady=(10, 20))
self.folders = []
self.files = []
################################################################################################
def change_mode(self):
if (self.nand_mode):
self.nand_frame.pack_forget()
self.checks_frame.pack(padx=10, anchor=W)
self.nand_mode = False
else:
if askokcancel('Warning', ('You are about to enter NAND mode. Do it only if you know '
'what you are doing. Proceed?'), icon=WARNING):
self.checks_frame.pack_forget()
self.nand_frame.pack(padx=10, pady=(0, 10), fill=X)
self.nand_mode = True
################################################################################################
def enable_entries(self, status):
self.cid_label['state'] = (NORMAL if status else DISABLED)
self.cid_entry['state'] = (NORMAL if status else DISABLED)
self.console_id_label['state'] = (NORMAL if status else DISABLED)
self.console_id_entry['state'] = (NORMAL if status else DISABLED)
################################################################################################
def choose_nand(self):
name = askopenfilename(filetypes=( ( 'nand.bin', '*.bin' ), ( 'DSi-1.mmc', '*.mmc' ) ))
self.nand_file.set(name)
self.nand_button['state'] = (NORMAL if name != '' else DISABLED)
self.start_button['state'] = (NORMAL if name != '' else DISABLED)
################################################################################################
def hiya(self):
if not self.nand_mode:
showinfo('Info', 'Now you will be asked to choose the SD card path that will be used '
'for installing the custom firmware (or an output folder).\n\nIn order to avoid '
'boot errors please assure it is empty before continuing.')
self.sd_path = askdirectory()
# Exit if no path was selected
if self.sd_path == '':
return
# If adding a No$GBA footer, check if CID and ConsoleID values are OK
elif self.nand_operation.get() == 1:
cid = self.cid.get()
console_id = self.console_id.get()
# Check lengths
if len(cid) != 32:
showerror('Error', 'Bad eMMC CID')
return
elif len(console_id) != 16:
showerror('Error', 'Bad Console ID')
return
# Parse strings to hex
try:
cid = bytearray.fromhex(cid)
except ValueError:
showerror('Error', 'Bad eMMC CID')
return
try:
console_id = bytearray(reversed(bytearray.fromhex(console_id)))
except ValueError:
showerror('Error', 'Bad Console ID')
return
dialog = Toplevel(self)
# Open as dialog (parent disabled)
dialog.grab_set()
dialog.title('Status')
# Disable maximizing
dialog.resizable(0, 0)
frame = Frame(dialog, bd=2, relief=SUNKEN)
scrollbar = Scrollbar(frame)
scrollbar.pack(side=RIGHT, fill=Y)
self.log = ThreadSafeText(frame, bd=0, width=52, height=20,
yscrollcommand=scrollbar.set)
self.log.pack()
scrollbar.config(command=self.log.yview)
frame.pack()
Button(dialog, text='Close', command=dialog.destroy, width=16).pack(pady=10)
# Center in window
dialog.update_idletasks()
width = dialog.winfo_width()
height = dialog.winfo_height()
dialog.geometry('%dx%d+%d+%d' % (width, height, root.winfo_x() + (root.winfo_width() / 2) -
(width / 2), root.winfo_y() + (root.winfo_height() / 2) - (height / 2)))
# Check if we'll be adding a No$GBA footer
if self.nand_mode and self.nand_operation.get() == 1:
Thread(target=self.add_footer, args=(cid, console_id)).start()
else:
Thread(target=self.check_nand).start()
################################################################################################
def check_nand(self):
self.log.write('Checking NAND file...')
# Read the NAND file
try:
with open(self.nand_file.get(), 'rb') as f:
# Go to the No$GBA footer offset
f.seek(-64, 2)
# Read the footer's header :-)
bstr = f.read(0x10)
if bstr == b'DSi eMMC CID/CPU':
# Read the CID
bstr = f.read(0x10)
self.cid.set(bstr.hex().upper())
self.log.write('- eMMC CID: ' + self.cid.get())
# Read the console ID
bstr = f.read(8)
self.console_id.set(bytearray(reversed(bstr)).hex().upper())
self.log.write('- Console ID: ' + self.console_id.get())
# Check we are removing the No$GBA footer
if self.nand_mode:
Thread(target=self.remove_footer).start()
else:
Thread(target=self.get_latest_hiyacfw).start()
else:
self.log.write('ERROR: No$GBA footer not found')
except IOError as e:
print(e)
self.log.write('ERROR: Could not open the file ' +
path.basename(self.nand_file.get()))
################################################################################################
def get_latest_hiyacfw(self):
# Try to use already downloaded hiyaCFW archive
filename = 'hiyaCFW.7z'
try:
if path.isfile(filename):
self.log.write('\nPreparing hiyaCFW...')
else:
self.log.write('\nDownloading latest hiyaCFW release...')
with urlopen('https://github.com/RocketRobz/hiyaCFW/releases/latest/download/' +
filename) as src, open(filename, 'wb') as dst:
copyfileobj(src, dst)
self.log.write('- Extracting hiyaCFW archive...')
proc = Popen([ _7za, 'x', '-bso0', '-y', filename, 'for PC', 'for SDNAND SD card' ])
ret_val = proc.wait()
if ret_val == 0:
if self.clean_downloaded.get() == 1:
self.files.append(filename)
self.folders.append('for PC')
self.folders.append('for SDNAND SD card')
# Got to decrypt NAND if bootloader.nds is present
Thread(target=self.decrypt_nand if path.isfile('bootloader.nds')
else self.extract_bios).start()
else:
self.log.write('ERROR: Extractor failed')
except (URLError, IOError) as e:
print(e)
self.log.write('ERROR: Could not get hiyaCFW')
except OSError as e:
print(e)
self.log.write('ERROR: Could not execute ' + exe)
################################################################################################
def extract_bios(self):
self.log.write('\nExtracting ARM7/ARM9 BIOS from NAND...')
try:
proc = Popen([ twltool, 'boot2', '--in', self.nand_file.get() ])
ret_val = proc.wait()
if ret_val == 0:
# Hash arm7.bin
sha1_hash = sha1()
with open('arm7.bin', 'rb') as f:
sha1_hash.update(f.read())
self.log.write('- arm7.bin SHA1:\n ' +
sha1_hash.digest().hex().upper())
# Hash arm9.bin
sha1_hash = sha1()
with open('arm9.bin', 'rb') as f:
sha1_hash.update(f.read())
self.log.write('- arm9.bin SHA1:\n ' +
sha1_hash.digest().hex().upper())
self.files.append('arm7.bin')
self.files.append('arm9.bin')
Thread(target=self.patch_bios).start()
else:
self.log.write('ERROR: Extractor failed')
Thread(target=self.clean, args=(True,)).start()
except OSError as e:
print(e)
self.log.write('ERROR: Could not execute ' + exe)
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def patch_bios(self):
self.log.write('\nPatching ARM7/ARM9 BIOS...')
try:
self.patcher(path.join('for PC', 'bootloader files', 'bootloader arm7 patch.ips'),
'arm7.bin')
self.patcher(path.join('for PC', 'bootloader files', 'bootloader arm9 patch.ips'),
'arm9.bin')
# Hash arm7.bin
sha1_hash = sha1()
with open('arm7.bin', 'rb') as f:
sha1_hash.update(f.read())
self.log.write('- Patched arm7.bin SHA1:\n ' +
sha1_hash.digest().hex().upper())
# Hash arm9.bin
sha1_hash = sha1()
with open('arm9.bin', 'rb') as f:
sha1_hash.update(f.read())
self.log.write('- Patched arm9.bin SHA1:\n ' +
sha1_hash.digest().hex().upper())
Thread(target=self.arm9_prepend).start()
except IOError as e:
print(e)
self.log.write('ERROR: Could not patch BIOS')
Thread(target=self.clean, args=(True,)).start()
except Exception as e:
print(e)
self.log.write('ERROR: Invalid patch header')
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def arm9_prepend(self):
self.log.write('\nPrepending data to ARM9 BIOS...')
try:
with open('arm9.bin', 'rb') as f:
data = f.read()
with open('arm9.bin', 'wb') as f:
with open(path.join('for PC', 'bootloader files',
'bootloader arm9 append to start.bin'), 'rb') as pre:
f.write(pre.read())
f.write(data)
# Hash arm9.bin
sha1_hash = sha1()
with open('arm9.bin', 'rb') as f:
sha1_hash.update(f.read())
self.log.write('- Prepended arm9.bin SHA1:\n ' +
sha1_hash.digest().hex().upper())
Thread(target=self.make_bootloader).start()
except IOError as e:
print(e)
self.log.write('ERROR: Could not prepend data to ARM9 BIOS')
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def make_bootloader(self):
self.log.write('\nGenerating new bootloader...')
exe = (path.join('for PC', 'bootloader files', 'ndstool.exe') if sysname == 'Windows' else
path.join(sysname, 'ndsblc'))
try:
proc = Popen([ exe, '-c', 'bootloader.nds', '-9', 'arm9.bin', '-7', 'arm7.bin', '-t',
path.join('for PC', 'bootloader files', 'banner.bin'), '-h',
path.join('for PC', 'bootloader files', 'header.bin') ])
ret_val = proc.wait()
if ret_val == 0:
self.files.append('bootloader.nds')
# Hash bootloader.nds
sha1_hash = sha1()
with open('bootloader.nds', 'rb') as f:
sha1_hash.update(f.read())
self.log.write('- bootloader.nds SHA1:\n ' +
sha1_hash.digest().hex().upper())
Thread(target=self.decrypt_nand).start()
else:
self.log.write('ERROR: Generator failed')
Thread(target=self.clean, args=(True,)).start()
except OSError as e:
print(e)
self.log.write('ERROR: Could not execute ' + exe)
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def decrypt_nand(self):
self.log.write('\nDecrypting NAND...')
try:
proc = Popen([ twltool, 'nandcrypt', '--in', self.nand_file.get(), '--out',
self.console_id.get() + '.img' ])
ret_val = proc.wait()
print("\n")
if ret_val == 0:
self.files.append(self.console_id.get() + '.img')
Thread(target=self.win_extract_nand if (sysname == 'Windows' and _7z is not None)
else self.extract_nand).start()
else:
self.log.write('ERROR: Decryptor failed')
Thread(target=self.clean, args=(True,)).start()
except OSError as e:
print(e)
self.log.write('ERROR: Could not execute ' + exe)
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def win_extract_nand(self):
self.log.write('\nExtracting files from NAND...')
try:
proc = Popen([ _7z, 'x', '-bso0', '-y', self.console_id.get() + '.img', '0.fat' ])
ret_val = proc.wait()
if ret_val == 0:
self.files.append('0.fat')
proc = Popen([ _7z, 'x', '-bso0', '-y', '-o' + self.sd_path, '0.fat' ])
ret_val = proc.wait()
if ret_val == 0:
Thread(target=self.get_launcher).start()
else:
self.log.write('ERROR: Extractor failed, please update 7-Zip')
if path.exists(fatcat):
self.log.write('\nTrying with fatcat...')
Thread(target=self.extract_nand).start()
else:
Thread(target=self.clean, args=(True,)).start()
else:
self.log.write('ERROR: Extractor failed')
if path.exists(fatcat):
self.log.write('\nTrying with fatcat...')
Thread(target=self.extract_nand).start()
else:
Thread(target=self.clean, args=(True,)).start()
except OSError as e:
print(e)
self.log.write('ERROR: Could not execute ' + exe)
if path.exists(fatcat):
self.log.write('\nTrying with fatcat...')
Thread(target=self.extract_nand).start()
else:
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def extract_nand(self):
self.log.write('\nExtracting files from NAND...')
try:
# DSi first partition offset: 0010EE00h
proc = Popen([ fatcat, '-O', '1109504', '-x', self.sd_path,
self.console_id.get() + '.img' ])
ret_val = proc.wait()
if ret_val == 0:
Thread(target=self.get_launcher).start()
else:
self.log.write('ERROR: Extractor failed')
Thread(target=self.clean, args=(True,)).start()
except OSError as e:
print(e)
self.log.write('ERROR: Could not execute ' + exe)
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def get_launcher(self):
app = self.detect_region()
# Stop if no supported region was found
if not app:
Thread(target=self.clean, args=(True,)).start()
return
# Delete contents of the launcher folder as it will be replaced by the one from hiyaCFW
launcher_folder = path.join(self.sd_path, 'title', '00030017', app, 'content')
# Walk through all files in the launcher content folder
for file in listdir(launcher_folder):
file = path.join(launcher_folder, file)
# Set current file as read/write in case we extracted with 7-Zip and unlaunch was
# installed in the NAND. Fatcat doesn't keep file attributes
if _7z is not None:
chmod(file, 438)
# Delete current file
remove(file)
# Try to use already downloaded launcher
try:
if path.isfile(self.launcher_region):
self.log.write('\nPreparing ' + self.launcher_region + ' launcher...')
else:
self.log.write('\nDownloading ' + self.launcher_region + ' launcher...')
with urlopen('https://raw.githubusercontent.com'
'/mondul/HiyaCFW-Helper/master/launchers/' +
self.launcher_region) as src, open(self.launcher_region, 'wb') as dst:
copyfileobj(src, dst)
self.log.write('- Decrypting launcher...')
# Set launcher filename according to the region
launcher_app = ('00000000.app' if self.launcher_region in ('CHN', 'KOR')
else '00000002.app')
# Prepare decryption params
params = [ _7za, 'x', '-bso0', '-y', '-p' + app.lower(), self.launcher_region,
launcher_app ]
if launcher_app == '00000000.app':
params.append('title.tmd')
proc = Popen(params)
ret_val = proc.wait()
if ret_val == 0:
if self.clean_downloaded.get() == 1:
self.files.append(self.launcher_region)
self.files.append(launcher_app)
if launcher_app == '00000000.app':
self.files.append('title.tmd')
# Hash launcher app
sha1_hash = sha1()
with open(launcher_app, 'rb') as f:
sha1_hash.update(f.read())
self.log.write('- Patched launcher SHA1:\n ' +
sha1_hash.digest().hex().upper())
Thread(target=self.install_hiyacfw, args=(launcher_app, launcher_folder)).start()
else:
self.log.write('ERROR: Extractor failed')
Thread(target=self.clean, args=(True,)).start()
except IOError as e:
print(e)
self.log.write('ERROR: Could not download ' + self.launcher_region + ' launcher')
Thread(target=self.clean, args=(True,)).start()
except OSError as e:
print(e)
self.log.write('ERROR: Could not execute ' + exe)
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def install_hiyacfw(self, launcher_app, launcher_folder):
self.log.write('\nCopying HiyaCFW files...')
# Reset copied files cache
_path_created.clear()
copy_tree('for SDNAND SD card', self.sd_path, update=1)
copyfile('bootloader.nds', path.join(self.sd_path, 'hiya', 'bootloader.nds'))
copyfile(launcher_app, path.join(launcher_folder, launcher_app))
if launcher_app == '00000000.app':
copyfile('title.tmd', path.join(launcher_folder, 'title.tmd'))
Thread(target=self.get_latest_twilight if self.twilight.get() == 1 else self.clean).start()
################################################################################################
def get_latest_twilight(self):
# Try to use already downloaded TWiLight Menu++ archive
filename = 'TWiLightMenu.7z'
try:
if path.isfile(filename):
self.log.write('\nPreparing TWiLight Menu++...')
else:
self.log.write('\nDownloading latest TWiLight Menu++ release...')
with urlopen('https://github.com/DS-Homebrew/TWiLightMenu/releases/latest/download/' +
filename) as src, open(filename, 'wb') as dst:
copyfileobj(src, dst)
self.log.write('- Extracting ' + filename[:-3] + ' archive...')
proc = Popen([ _7za, 'x', '-bso0', '-y', filename, '_nds', 'DSi - CFW users',
'DSi&3DS - SD card users', 'roms' ])
ret_val = proc.wait()
if ret_val == 0:
if self.clean_downloaded.get() == 1:
self.files.append(filename)
self.folders.append('DSi - CFW users')
self.folders.append('_nds')
self.folders.append('DSi&3DS - SD card users')
self.folders.append('roms')
Thread(target=self.install_twilight, args=(filename[:-3],)).start()
else:
self.log.write('ERROR: Extractor failed')
Thread(target=self.clean, args=(True,)).start()
except (URLError, IOError) as e:
print(e)
self.log.write('ERROR: Could not get TWiLight Menu++')
Thread(target=self.clean, args=(True,)).start()
except OSError as e:
print(e)
self.log.write('ERROR: Could not execute ' + exe)
Thread(target=self.clean, args=(True,)).start()
################################################################################################
def install_twilight(self, name):
self.log.write('\nCopying ' + name + ' files...')
copy_tree(path.join('DSi - CFW users', 'SDNAND root'), self.sd_path, update=1)
copy_tree('_nds', path.join(self.sd_path, '_nds'))
copy_tree('DSi&3DS - SD card users', self.sd_path, update=1)
copy_tree('roms', path.join(self.sd_path, 'roms'))
Thread(target=self.clean).start()
################################################################################################
def clean(self, err=False):
self.log.write('\nCleaning...')
while len(self.folders) > 0:
rmtree(self.folders.pop(), ignore_errors=True)
while len(self.files) > 0:
try:
remove(self.files.pop())
except:
pass
if err:
self.log.write('Done')
return
self.log.write('Done!\nEject your SD card and insert it into your DSi')
################################################################################################
def patcher(self, patchpath, filepath):
patch_size = path.getsize(patchpath)
patchfile = open(patchpath, 'rb')
if patchfile.read(5) != b'PATCH':
patchfile.close()
raise Exception()
target = open(filepath, 'r+b')
# Read First Record
r = patchfile.read(3)
while patchfile.tell() not in [ patch_size, patch_size - 3 ]:
# Unpack 3-byte pointers.
offset = self.unpack_int(r)
# Read size of data chunk
r = patchfile.read(2)
size = self.unpack_int(r)
if size == 0: # RLE Record
r = patchfile.read(2)
rle_size = self.unpack_int(r)
data = patchfile.read(1) * rle_size
else:
data = patchfile.read(size)
# Write to file
target.seek(offset)
target.write(data)
# Read Next Record
r = patchfile.read(3)
if patch_size - 3 == patchfile.tell():
trim_size = self.unpack_int(patchfile.read(3))
target.truncate(trim_size)
# Cleanup
target.close()
patchfile.close()
################################################################################################
def unpack_int(self, bstr):
# Read an n-byte big-endian integer from a byte string
( ret_val, ) = unpack_from('>I', b'\x00' * (4 - len(bstr)) + bstr)
return ret_val
################################################################################################
def detect_region(self):
REGION_CODES = {
'484e4143': 'CHN',
'484e4145': 'USA',
'484e414a': 'JAP',
#'484e414b': 'KOR',
'484e4150': 'EUR',
'484e4155': 'AUS'
}
# Autodetect console region
try:
for app in listdir(path.join(self.sd_path, 'title', '00030017')):
for file in listdir(path.join(self.sd_path, 'title', '00030017', app, 'content')):
if file.endswith('.app'):
try:
self.log.write('- Detected ' + REGION_CODES[app.lower()] +
' console NAND dump')
self.launcher_region = REGION_CODES[app.lower()]
return app
except KeyError:
self.log.write('ERROR: Unsupported console region')
return False
self.log.write('ERROR: Could not detect console region')
except OSError as e:
self.log.write('ERROR: ' + e.strerror + ': ' + e.filename)
return False
################################################################################################
def remove_footer(self):
self.log.write('\nRemoving No$GBA footer...')
file = self.console_id.get() + '-no-footer.bin'
try:
copyfile(self.nand_file.get(), file)
# Back-up footer info
with open(self.console_id.get() + '-info.txt', 'w') as f:
f.write('eMMC CID: ' + self.cid.get() + '\r\n')
f.write('Console ID: ' + self.console_id.get() + '\r\n')
with open(file, 'r+b') as f:
# Go to the No$GBA footer offset
f.seek(-64, 2)
# Remove footer
f.truncate()
self.log.write('\nDone!\nModified NAND stored as\n' + file +
'\nStored footer info in ' + self.console_id.get() + '-info.txt')
except IOError as e:
print(e)
self.log.write('ERROR: Could not open the file ' +
path.basename(self.nand_file.get()))
################################################################################################
def add_footer(self, cid, console_id):
self.log.write('Adding No$GBA footer...')
file = self.console_id.get() + '-footer.bin'
try:
copyfile(self.nand_file.get(), file)
with open(file, 'r+b') as f:
# Go to the No$GBA footer offset
f.seek(-64, 2)
# Read the footer's header :-)
bstr = f.read(0x10)
# Check if it already has a footer
if bstr == b'DSi eMMC CID/CPU':
self.log.write('ERROR: File already has a No$GBA footer')
f.close()
remove(file)
return
# Go to the end of file
f.seek(0, 2)
# Write footer
f.write(b'DSi eMMC CID/CPU')
f.write(cid)
f.write(console_id)
f.write(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
self.log.write('\nDone!\nModified NAND stored as\n' + file)
except IOError as e:
print(e)
self.log.write('ERROR: Could not open the file ' +
path.basename(self.nand_file.get()))
####################################################################################################
# Entry point
print('Opening HiyaCFW Helper...')
sysname = system()
print('Initializing GUI...')
root = Tk()
fatcat = path.join(sysname, 'fatcat')
_7za = path.join(sysname, '7za')
_7z = None
if sysname == 'Windows':
from winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_64KEY
print('Searching for 7-Zip in the Windows registry...')
try:
with OpenKey(HKEY_LOCAL_MACHINE, 'SOFTWARE\\7-Zip', 0, KEY_READ | KEY_WOW64_64KEY) as hkey:
_7z = path.join(QueryValueEx(hkey, 'Path')[0], '7z.exe')
if not path.exists(_7z):
raise WindowsError
_7za = _7z
except WindowsError:
print('Searching for 7-Zip in the 32-bit Windows registry...')
try:
with OpenKey(HKEY_LOCAL_MACHINE, 'SOFTWARE\\7-Zip') as hkey:
_7z = path.join(QueryValueEx(hkey, 'Path')[0], '7z.exe')
if not path.exists(_7z):
raise WindowsError
_7za = _7z
except WindowsError:
print('7-Zip not found. Will use fatcat for extraction.')
_7z = None
_7za += '.exe'
fatcat += '.exe'
twltool = path.join('for PC', 'twltool.exe')
else: # Linux and MacOS
twltool = path.join(sysname, 'twltool')
if _7z is None and not path.exists(fatcat):