-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrsfgen.py
More file actions
1078 lines (963 loc) · 41.6 KB
/
rsfgen.py
File metadata and controls
1078 lines (963 loc) · 41.6 KB
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
import struct
import binascii
import os
import argparse
MEDIA_BLOCK_SIZE = 0x200
KILOBYTE = 1024
MEGABYTE = 1024**2
GIGABYTE = 1024**3
spoof = 0
i = 0
sysCalls = (
'Unknown',
'ControlMemory',
'QueryMemory',
'ExitProcess',
'GetProcessAffinityMask',
'SetProcessAffinityMask',
'SetProcessIdealProcessor',
'GetProcessIdealProcessor',
'CreateThread',
'ExitThread',
'SleepThread',
'GetThreadPriority',
'SetThreadPriority',
'GetThreadAffinityMask',
'SetThreadAffinityMask',
'GetThreadIdealProcessor',
'SetThreadIdealProcessor',
'GetCurrentProcessorNumber',
'Run',
'CreateMutex',
'ReleaseMutex',
'CreateSemaphore',
'ReleaseSemaphore',
'CreateEvent',
'SignalEvent',
'ClearEvent',
'CreateTimer',
'SetTimer',
'CancelTimer',
'ClearTimer',
'CreateMemoryBlock',
'MapMemoryBlock',
'UnmapMemoryBlock',
'CreateAddressArbiter',
'ArbitrateAddress',
'CloseHandle',
'WaitSynchronization1',
'WaitSynchronizationN',
'SignalAndWait',
'DuplicateHandle',
'GetSystemTick',
'GetHandleInfo',
'GetSystemInfo',
'GetProcessInfo',
'GetThreadInfo',
'ConnectToPort',
'SendSyncRequest1',
'SendSyncRequest2',
'SendSyncRequest3',
'SendSyncRequest4',
'SendSyncRequest',
'OpenProcess',
'OpenThread',
'GetProcessId',
'GetProcessIdOfThread',
'GetThreadId',
'GetResourceLimit',
'GetResourceLimitLimitValues',
'GetResourceLimitCurrentValues',
'GetThreadContext',
'Break',
'OutputDebugString'
)
# An unknown attribute is documented but not supported by makerom
FSAccessInfo = (
'CategorySystemApplication',
'CategoryHardwareCheck',
'CategoryFileSystemTool',
'Debug',
'TwlCardBackup',
'TwlNandData',
'Boss',
'DirectSdmc',
'Core',
'CtrNandRo',
'CtrNandRw',
'CtrNandRoWrite',
'CategorySystemSettings',
'CardBoard',
'ExportImportIvs',
'DirectSdmcWrite',
'SwitchCleanup',
'SaveDataMove',
'Shop',
'Shell',
'CategoryHomeMenu'
)
# SD Application (8) is set by Options/UseOnSD
# Mount sdmc:/ (9) is set by FileSystemAccess flags
ARM9Access = (
'FsMountNand',
'FsMountNandRoWrite',
'FsMountTwln',
'FsMountWnand',
'FsMountCardSpi',
'UseSdif3',
'CreateSeed',
'UseCardSpi',
)
rsffile = {
'Option': {
'UseOnSD': 'True',
'EnableCompress': 'True',
'FreeProductCode': 'True',
'EnableCrypt': 'False',
'MediaFootPadding': 'False'
}, 'AccessControlInfo': {
'DisableDebug': '',
'EnableForceDebug': '',
'CanWriteSharedPage': '',
'CanUsePrivilegedPriority': '',
'CanUseNonAlphabetAndNumber': '',
'PermitMainFunctionArgument': '',
'CanShareDeviceMemory': '',
'RunnableOnSleep': '',
'SpecialMemoryArrange': '',
'UseOtherVariationSaveData': '',
'CanAccessCore2': '',
'UseExtSaveData': '',
'EnableL2Cache': '',
'IdealProcessor': '',
'Priority': '',
'MemoryType': 'Application',
'SystemMode': '',
'SystemModeExt': '',
'CpuSpeed': '',
'CoreVersion': '',
'HandleTableSize': '',
'SystemSaveDataId1': '',
'SystemSaveDataId2': '',
'OtherUserSaveDataId1': '',
'OtherUserSaveDataId2': '',
'OtherUserSaveDataId3': '',
'ExtSaveDataId': '',
'AffinityMask': '',
'DescVersion': '',
'ResourceLimitCategory': '',
'ReleaseKernelMajor': '',
'ReleaseKernelMinor': '',
'MaxCpu': '',
'MemoryMapping': [],
'IORegisterMapping': [],
'FileSystemAccess': [],
'IoAccessControl': [],
'InterruptNumbers': [],
'SystemCallAccess': [],
'ServiceAccessControl': [],
'AccessibleSaveDataIds': []
}, 'SystemControlInfo': {
'StackSize': '',
'RemasterVersion': '',
'JumpId': '',
'SaveDataSize': '',
'Dependency': []
}, 'BasicInfo': {
'Title': '',
'CompanyCode': '',
'ProductCode': '',
'ContentType': 'Application',
'Logo': 'Nintendo'
}, 'RomFs': {
'RootPath': '',
}, 'TitleInfo': {
'Category': '',
'UniqueId': '',
'Platform': ''
}
}
# Lists that require there to not be a dash
specialLists = [
'AccessControlInfo/SystemCallAccess',
'SystemControlInfo/Dependency'
]
categoryFlags = {
'normal': 0x000,
'dlpChild': 0x001,
'demo': 0x002,
'content': 0x003,
'addonContents': 0x004,
'patch': 0x006,
'cannotExecution': 0x008,
'system': 0x010,
'requireBatchUpdate': 0x020,
'notRequireUserApproval': 0x040,
'notRequireRightForMount': 0x080,
'canSkipConvertJumpId': 0x100
}
categories = {
categoryFlags['normal']: 'Application',
categoryFlags['normal'] | categoryFlags['system']: 'SystemApplication',
categoryFlags['normal'] | categoryFlags['system'] |
categoryFlags['requireBatchUpdate']: 'Applet',
categoryFlags['normal'] | categoryFlags['cannotExecution'] |
categoryFlags['system'] | categoryFlags['requireBatchUpdate'] |
categoryFlags['canSkipConvertJumpId']: 'Firmware',
categoryFlags['normal'] | categoryFlags['system'] | categoryFlags['requireBatchUpdate'] |
categoryFlags['canSkipConvertJumpId']: 'Base',
categoryFlags['dlpChild']: 'DlpChild',
categoryFlags['demo']: 'Demo',
categoryFlags['content']: 'Contents',
categoryFlags['content'] | categoryFlags['cannotExecution'] |
categoryFlags['system']: 'SystemContents',
categoryFlags['content'] | categoryFlags['cannotExecution'] | categoryFlags['system'] |
categoryFlags['notRequireRightForMount']: 'SharedContents',
categoryFlags['addonContents'] | categoryFlags['cannotExecution'] |
categoryFlags['notRequireRightForMount']: 'AddOnContents',
categoryFlags['patch'] | categoryFlags['cannotExecution']: 'Patch',
categoryFlags['content'] | categoryFlags['cannotExecution'] |
categoryFlags['system'] | categoryFlags['notRequireUserApproval'] |
categoryFlags['notRequireRightForMount']: 'AutoUpdateContents'
}
# NOTE: TitleInfo is all from the Program ID field in the NCCH header
# Version, ContentsIndex, Variation, ChildIndex and DemoIndex all are the
# "Variation" byte in different contexts. None of them do anything for
# Applications.
# TargetCategory sets Category in the case of constructing an exheader. Not
# sure about the specific details, but Category should always work.
# CategoryFlags sets more specific flags. Category can't be set at the same
# time.
# https://www.3dbrew.org/wiki/NCSD
# NCSD header
# Offset Size Description
# 0x000 0x100 RSA-2048 SHA-256 signature of the NCSD header (rsasig)
# 0x100 4 Magic Number 'NCSD' (magic)
# 0x104 4 Size of the NCSD image, in media units (1 media unit = 0x200 bytes) (mediasize)
# 0x108 8 Media ID (mediaid)
# 0x110 8 Partitions FS type (0=None, 1=Normal, 3=FIRM, 4=AGB_FIRM save) (type)
# 0x118 8 Partitions crypt type (crypttype)
# 0x120 0x40=(4+4)*8 Offset & Length partition table, in media units (offsets lenghts)
# 0x160 0xA0 ...
# For carts,
# Offset Size Description
# 0x160 0x20 Exheader SHA-256 hash (exhdrhash)
# 0x180 0x4 Additional header size (addhdrsize)
# 0x184 0x4 Sector zero offset (zerooffset)
# 0x188 8 Partition Flags (See Below) (flags)
# 0x190 0x40=8*8 Partition ID table (partids)
# 0x1D0 0x30 Reserved
# 0x200 0xE Reserved?
# 0x20E 0x1 Support for this was implemented with 9.6.0-X FIRM. Bit0=1 enables using bits 1-2, it's unknown what these two bits are actually used for(the value of these two bits get compared with some other value during NCSD verification/loading). This appears to enable a new, likely hardware-based, antipiracy check on cartridges. (exflags)
# 0x20F 0x1 Support for this was implemented with 9.6.0-X FIRM, see below regarding save crypto. (savecrypto)
ncsdHdrStruct = struct.Struct("<256s4sIQQQ16I32sIIQ8Q62xBB")
# https://www.3dbrew.org/wiki/NCCH
# NCCH Header
# OFFSET SIZE DESCRIPTION
# 0x000 0x100 RSA-2048 signature of the NCCH header, using SHA-256. (rsasig)
# 0x100 4 Magic ID, always 'NCCH' (magic)
# 0x104 4 Content size, in media units (1 media unit = 0x200 bytes) (mediasize)
# 0x108 8 Partition ID (partitionid)
# 0x110 2 Maker code (makercode)
# 0x112 2 Version (version)
# 0x114 4 When ncchflag[7] = 0x20 starting with FIRM 9.6.0-X, this is compared with the first output u32 from a SHA256 hash. The data used for that hash is 0x18-bytes: <0x10-long title-unique content lock seed> <programID from NCCH+0x118>. This hash is only used for verification of the content lock seed, and is not the actual keyY. (lockhash)
# 0x118 8 Program ID (programid)
# 0x120 0x10 Reserved
# 0x130 0x20 Logo Region SHA-256 hash. (For applications built with SDK 5+) (Supported from firmware: 5.0.0-11) (logohash)
# 0x150 0x10 Product code (productcode)
# 0x160 0x20 Extended header SHA-256 hash (SHA256 of 2x Alignment Size, beginning at 0x0 of ExHeader) (exhdrhash)
# 0x180 4 Extended header size (exhdrsize)
# 0x184 4 Reserved
# 0x188 8 Flags (See Below) (flags)
# 0x190 4 Plain region offset, in media units (plainoffset)
# 0x194 4 Plain region size, in media units (plainsize)
# 0x198 4 Logo Region offset, in media units (For applications built with SDK 5+) (Supported from firmware: 5.0.0-11) (logooffset)
# 0x19c 4 Logo Region size, in media units (For applications built with SDK 5+) (Supported from firmware: 5.0.0-11) (logosize)
# 0x1A0 4 ExeFS offset, in media units (exefsoffset)
# 0x1A4 4 ExeFS size, in media units (exefssize)
# 0x1A8 4 ExeFS hash region size, in media units (exefshashsize)
# 0x1AC 4 Reserved
# 0x1B0 4 RomFS offset, in media units (romfsoffset)
# 0x1B4 4 RomFS size, in media units (romfssize)
# 0x1B8 4 RomFS hash region size, in media units (romfshashsize)
# 0x1BC 4 Reserved
# 0x1C0 0x20 ExeFS superblock SHA-256 hash - (SHA-256 hash, starting at 0x0 of the ExeFS over the number of media units specified in the ExeFS hash region size) (exefssuperhash)
# 0x1E0 0x20 RomFS superblock SHA-256 hash - (SHA-256 hash, starting at 0x0 of the RomFS over the number of media units specified in the RomFS hash region size) (romfssuperhash)
ncchHdrStruct = struct.Struct("<256s4sIQ2sHIQ16x32s16s32sI4xQIIIIIII4xIII4x32s32s")
# https://www.3dbrew.org/wiki/NCCH/Extended_Header
# System Control Info
# Offset Size Description
# 0x0 0x8 Application Title (title)
# 0x8 0x5 Reserved
# 0xD 0x1 Flag (Bit0: CompressExefsCode, Bit1: SDApplication) (flag)
# 0xE 0x2 Remaster Version (remaster)
# 0x10 0xC Text Code Set Info (textaddr textpages textsize)
# 0x1C 0x4 Stack Size (stack)
# 0x20 0xC ReadOnly Code Set Info (roaddr ropages rosize)
# 0x2C 0x4 Reserved
# 0x30 0xC Data Code Set Info (dataaddr datapages datasize)
# 0x3C 0x4 BSS Size (bsssize)
# 0x40 0x180 (48*8) Dependency Module (Program ID) List (dependencies)
# 0x1C0 0x40 SystemInfo (savesize jumpid)
# Code Set Info
# Offset Size Description
# 0x0 0x4 Address
# 0x4 0x4 Physical region size (in page-multiples)
# 0x8 0x4 Size (in bytes)
# System Info
# Offset Size Description
# 0x0 0x8 SaveData Size
# 0x8 0x8 Jump ID
# 0x10 0x30 Reserved
sysCtrlInfoStruct = struct.Struct("<8s5xBHIIIIIII4xIIII48QQQ48x")
# Access Control Info
# Offset Size Description
# 0x0 0x170 ARM11 Local System Capabilities
# 0x170 0x80 ARM11 Kernel Capabilities
# 0x1F0 0x10 ARM9 Access Control
# ARM11 Local System Capabilities
# Offset Size Description
# 0x0 0x8 Program ID (programid)
# 0x8 0x4 Core Version (The Title ID low of the required FIRM) (corever)
# 0xC 0x1 Flag1 (1b l2cache, 1b cpuspeed, 6b nothing) (flag1)
# 0xD 0x1 Flag2 (4b sysmodeext 0='Legacy' 1='124MB' 2='178MB' (dev unit?)) (flag2)
# 0xE 0x1 Flag0 (flag0)
# 0xF 0x1 Priority (priority)
# 0x10 0x20 (16*2) Resource Limit Descriptors (reslimits)
# 0x30 0x20 Storage Info (extdataid syssaveid(1,2) storageid fsaccess otherattrib)
# 0x50 0x100 (32*8) Service Access Control (services)
# 0x150 0x10 (2*8) Extended Service Access Control, support for this was implemented with 9.3.0-X. (services)
# 0x160 0xF Reserved
# 0x16F 0x1 Resource Limit Category. (0 = APPLICATION, 1 = SYS_APPLET, 2 = LIB_APPLET, 3 = OTHER(sysmodules running under the BASE memregion)) (category)
# ARM11 Kernel Capabilities
# Offset Size Description
# 0x0 0x70 (28*4) Descriptors (kernelcaps)
# 0x70 0x10 Reserved
# ARM9 Access Control
# Offset Size Description
# 0x0 0xF Descriptors (arm9caps)
# 0xF 0x1 ARM9 Descriptor Version. Originally this value had to be >=2. Starting with 9.3.0-X this value has to be either value 2 or value 3. (arm9ver)
# Storage Info
# Offset Size Description
# 0x0 0x8 Extdata ID
# 0x8 (2*4) System Save Data Ids
# 0x10 0x8 Storage Accessable Unique Ids
# 0x18 0x7 File System Access Info
# 0x1F 0x1 Other Attributes
# I implemented ARM9 Access Control as 'I11xB' instead of the documented 15 bytes
# field since this makes it easier, and makerom treats tha field as a 32 bit
# value anyway.
accCtrlInfoStruct = struct.Struct("<QIBBBB16HQQQQ272s15xB28I16xI11xB")
def makeNcsdHdrDictFromTuple(buffer):
return({
'rsasig': buffer[0],
'magic': buffer[1],
'mediasize': buffer[2],
'mediaid': buffer[3],
'type': buffer[4],
'crypttype': buffer[5],
'offsets': tuple(buffer[6:22][x] for x in range(0, 16, 2)), # 6 + 16
'lengths': tuple(buffer[6:22][x] for x in range(1, 16, 2)),
'exhdrhash': buffer[22],
'addheadersize': buffer[23],
'zerooffset': buffer[24],
'flags': buffer[25],
'partids': range(26, 33), # 26 + 8
'exflags': buffer[34],
'savecrypto': buffer[35]
})
def makeNcchHdrDictFromTuple(buffer):
return({
'rsasig': buffer[0],
'magic': buffer[1],
'mediasize': buffer[2],
'partitionid': buffer[3],
'makercode': buffer[4],
'version': buffer[5],
'lockhash': buffer[6],
'programid': buffer[7],
'logohash': buffer[8],
'productcode': buffer[9],
'exhdrhash': buffer[10],
'exhdrsize': buffer[11],
'flags': buffer[12],
'plainoffset': buffer[13],
'plainsize': buffer[14],
'logooffset': buffer[15],
'logosize': buffer[16],
'exefsoffset': buffer[17],
'exefssize': buffer[18],
'exefshashsize': buffer[19],
'romfsoffset': buffer[20],
'romfssize': buffer[21],
'romfshashsize': buffer[22],
'exefssuperhash': buffer[23],
'romfssuperhash': buffer[24]
})
def makeSysCtrlInfoDictFromTuple(buffer):
return({
'title': buffer[0],
'flag': buffer[1],
'remaster': buffer[2],
'textaddr': buffer[3],
'textpages': buffer[4],
'textsize': buffer[5],
'stack': buffer[6],
'roaddr': buffer[7],
'ropages': buffer[8],
'rosize': buffer[9],
'dataaddr': buffer[10],
'datapages': buffer[11],
'datasize': buffer[12],
'bsssize': buffer[13],
'dependencies': buffer[14:62], # 14 + 48
'savesize': buffer[62],
'jumpid': buffer[63]
})
def makeAccCtrlInfoDictFromTuple(buffer):
return({
'programid': buffer[0],
'corever': buffer[1],
'flag1': buffer[2],
'flag2': buffer[3],
'flag0': buffer[4],
'priority': buffer[5],
'reslimits': buffer[6:22],
'extdataid': buffer[22],
'syssaveid1': buffer[23] >> 0 & 0xFFFFFFFF,
'syssaveid2': buffer[23] >> 32 & 0xFFFFFFFF,
'storageid': buffer[24],
'fsaccess': buffer[25] & 0xFFFFFFFFFFFFFF,
'otherattrib': buffer[25] >> 56 & 0xFF,
'services': tuple(buffer[26][x:x+8] for x in range(0, 272, 8)), # 34 entries of 8 bytes each
'category': buffer[27],
'kernelcaps': buffer[28:56], # 28 + 28
'arm9caps': buffer[56],
'arm9ver': buffer[57]
})
def warning(warning):
print("WARNING: %s" % warning, file=sys.stderr)
def populateRSFFile():
if ncchHdr['exhdrsize'] == 0x400: # Only if there is an exheader
# AccessControlInfo
# handle those annoying ARM11 Kernel Capabilities values
for caps in accCtrlInfo['kernelcaps']:
# InterruptNumbers
# each has 4 interrupts packed in, 7 bits each
if caps & 0xF0000000 == 0xE0000000:
ints = (caps >> 0 & 0x7F, caps >> 7 & 0x7F, caps >> 14 & 0x7F, caps >> 21 & 0x7F)
for intr in ints:
if intr == 0:
break
rsffile['AccessControlInfo']['InterruptNumers'].append(intr)
# SystemCallAccess
# first 3 bits after the descriptor value indicate a group of 24 syscalls
# the next 24 bits are a mask for this group.
elif caps & 0xF8000000 == 0xF0000000:
scstart = (caps >> 24 & 0x7) * 24
for sc in enumerate(sysCalls[scstart:scstart+24]):
if caps & (1 << sc[0]) != 0:
rsffile['AccessControlInfo']['SystemCallAccess']\
.append("%s: %d" % (sc[1], scstart + sc[0]))
# ReleaseKernelMajor, ReleaseKernelMinor
elif caps & 0xFE000000 == 0xFC000000:
rsffile['AccessControlInfo']['ReleaseKernelMinor'] = caps >> 0 & 0xFF
rsffile['AccessControlInfo']['ReleaseKernelMajor'] = caps >> 8 & 0xFF
# HandleTableSize
elif caps & 0xFF000000 == 0xFE000000:
rsffile['AccessControlInfo']['HandleTableSize'] = caps & 0x7FFFF
# DisableDebug, EnableForceDebug, CanWriteSharedPage, CanUsePrivilegedPriority,
# CanUseNonAlphabetAndNumber, PermitMainFunctionArgument, CanShareDeviceMemory,
# RunnableOnSleep, SpecialMemoryArrange
elif caps & 0xFF800000 == 0xFF000000:
if caps & 0x1 != 0:
rsffile['AccessControlInfo']['DisableDebug'] = 'False'
else:
rsffile['AccessControlInfo']['DisableDebug'] = 'True'
if caps & 0x2 != 0:
rsffile['AccessControlInfo']['EnableForceDebug'] = 'True'
else:
rsffile['AccessControlInfo']['EnableForceDebug'] = 'False'
if caps & 0x4 != 0:
rsffile['AccessControlInfo']['CanUseNonAlphabetAndNumber'] = 'True'
else:
rsffile['AccessControlInfo']['CanUseNonAlphabetAndNumber'] = 'False'
if caps & 0x8 != 0:
rsffile['AccessControlInfo']['CanWriteSharedPage'] = 'True'
else:
rsffile['AccessControlInfo']['CanWriteSharedPage'] = 'False'
if caps & 0x10 != 0:
rsffile['AccessControlInfo']['CanUsePrivilegedPriority'] = 'True'
else:
rsffile['AccessControlInfo']['CanUsePrivilegedPriority'] = 'False'
if caps & 0x20 != 0:
rsffile['AccessControlInfo']['PermitMainFunctionArgument'] = 'True'
else:
rsffile['AccessControlInfo']['PermitMainFunctionArgument'] = 'False'
if caps & 0x40 != 0:
rsffile['AccessControlInfo']['CanShareDeviceMemory'] = 'True'
else:
rsffile['AccessControlInfo']['CanShareDeviceMemory'] = 'False'
if caps & 0x80 != 0:
rsffile['AccessControlInfo']['RunnableOnSleep'] = 'True'
else:
rsffile['AccessControlInfo']['RunnableOnSleep'] = 'False'
if caps & 0x800 != 0:
rsffile['AccessControlInfo']['SpecialMemoryArrange'] = 'True'
else:
rsffile['AccessControlInfo']['SpecialMemoryArrange'] = 'False'
if caps & 0x1000 != 0:
rsffile['AccessControlInfo']['CanAccessCore2'] = 'True'
else:
rsffile['AccessControlInfo']['CanAccessCore2'] = 'False'
# MemoryMapping
elif caps & 0xFFC00000 == 0xFF800000:
if caps & 0x100000 != 0: #read only
rsffile['AccessControlInfo']['MemoryMapping']\
.append("0x%0.1X%0.2X%0.2X000:r" % (caps >> 16 & 0xF, caps >> 8 & 0xFF, caps & 0xFF))
else: #read write
rsffile['AccessControlInfo']['MemoryMapping']\
.append("0x%0.1X%0.2X%0.2X000" % (caps >> 16 & 0xF, caps >> 8 & 0xFF, caps & 0xFF))
# IORegisterMapping
elif caps & 0xFFF00000 == 0xFFE00000:
rsffile['AccessControlInfo']['IORegisterMapping']\
.append("0x%0.1X%0.2X%0.2X000" % (caps >> 16 & 0xF, caps >> 8 & 0xFF, caps & 0xFF))
# Empty entry
elif caps == 0xFFFFFFFF:
pass
else:
warning("Unsupported ARM9 Kernel Capabilities Descriptor %0.8X!" % caps)
# UseExtSaveData, EXtSaveDataId, UseOtherVariationSaveData, AccessibleSaveDataIds
# makerom indicates a mask of 0xFFFFFF but only shifts 20 bits
if accCtrlInfo['otherattrib'] & 0x2 != 0:
ids = (accCtrlInfo['extdataid'] >> 0 & 0xFFFFF,
accCtrlInfo['extdataid'] >> 20 & 0xFFFFF,
accCtrlInfo['extdataid'] >> 40 & 0xFFFFF,
accCtrlInfo['storageid'] >> 0 & 0xFFFFF,
accCtrlInfo['storageid'] >> 20 & 0xFFFFF,
accCtrlInfo['storageid'] >> 40 & 0xFFFFF)
for id in ids:
if id != 0x00000:
rsffile['AccessControlInfo']['AccessibleSaveDataIds'].append(id)
del rsffile['AccessControlInfo']['OtherUserSaveDataId1']
del rsffile['AccessControlInfo']['OtherUserSaveDataId2']
del rsffile['AccessControlInfo']['OtherUserSaveDataId3']
else:
del rsffile['AccessControlInfo']['AccessibleSaveDataIds']
if accCtrlInfo['extdataid'] != 0:
rsffile['AccessControlInfo']['UseExtSaveData'] = 'True'
rsffile['AccessControlInfo']['ExtSaveDataId'] = accCtrlInfo['extdataid']
ids = (accCtrlInfo['storageid'] >> 0 & 0xFFFFF,
accCtrlInfo['storageid'] >> 20 & 0xFFFFF,
accCtrlInfo['storageid'] >> 40 & 0xFFFFF)
if ids[0] != 0:
rsffile['AccessControlInfo']['OtherUserSaveDataId1'] = ids[0]
else:
del rsffile['AccessControlInfo']['OtherUserSaveDataId1']
if ids[1] != 0:
rsffile['AccessControlInfo']['OtherUserSaveDataId2'] = ids[1]
else:
del rsffile['AccessControlInfo']['OtherUserSaveDataId2']
if ids[2] != 0:
rsffile['AccessControlInfo']['OtherUserSaveDataId3'] = ids[2]
else:
del rsffile['AccessControlInfo']['OtherUserSaveDataId3']
if accCtrlInfo['storageid'] & 0x1000000000000000 != 0:
rsffile['AccessControlInfo']['UseOtherVariationSaveData'] = 'True'
else:
rsffile['AccessControlInfo']['UseOtherVariationSaveData'] = 'False'
# SystemSaveDataId(1,2)
if accCtrlInfo['syssaveid1'] != 0:
rsffile['AccessControlInfo']['SystemSaveDataId1'] = accCtrlInfo['syssaveid1']
else:
del rsffile['AccessControlInfo']['SystemSaveDataId1']
if accCtrlInfo['syssaveid2'] != 0:
rsffile['AccessControlInfo']['SystemSaveDataId2'] = accCtrlInfo['syssaveid2']
else:
del rsffile['AccessControlInfo']['SystemSaveDataId2']
# EnableL2Cache, CpuSpeed
if accCtrlInfo['flag1'] & 0x1 != 0:
rsffile['AccessControlInfo']['EnableL2Cache'] = 'True'
else:
rsffile['AccessControlInfo']['EnableL2Cache'] = 'False'
# this is just a flag, makerom will not accept other values for this
if accCtrlInfo['flag1'] & 0x2 != 0:
rsffile['AccessControlInfo']['CpuSpeed'] = '804mhz'
else:
rsffile['AccessControlInfo']['CpuSpeed'] = '268mhz'
# IdealProcessor, AffinityMask, Priority, CoreVersion, DescVersion
# ResourceLimtCategory, MaxCpu
rsffile['AccessControlInfo']['IdealProcessor'] = accCtrlInfo['flag0'] >> 0 & 0x3
rsffile['AccessControlInfo']['AffinityMask'] = accCtrlInfo['flag0'] >> 2 & 0x3
rsffile['AccessControlInfo']['Priority'] = accCtrlInfo['priority'] - 32 # makerom increases this value by 32 for applications
rsffile['AccessControlInfo']['CoreVersion'] = accCtrlInfo['corever']
rsffile['AccessControlInfo']['DescVersion'] = accCtrlInfo['arm9ver']
rsffile['AccessControlInfo']['ResourceLimitCategory'] = accCtrlInfo['category']
rsffile['AccessControlInfo']['MaxCpu'] = accCtrlInfo['reslimits'][0]
# SystemMode
if accCtrlInfo['flag0'] >> 4 & 0xF == 0:
rsffile['AccessControlInfo']['SystemMode'] = '64MB'
elif accCtrlInfo['flag0'] >> 4 & 0xF == 1:
rsffile['AccessControlInfo']['SystemMode'] = 'UNK'
elif accCtrlInfo['flag0'] >> 4 & 0xF == 2:
rsffile['AccessControlInfo']['SystemMode'] = '96MB'
elif accCtrlInfo['flag0'] >> 4 & 0xF == 3:
rsffile['AccessControlInfo']['SystemMode'] = '80MB'
elif accCtrlInfo['flag0'] >> 4 & 0xF == 4:
rsffile['AccessControlInfo']['SystemMode'] = '72MB'
elif accCtrlInfo['flag0'] >> 4 & 0xF == 5:
rsffile['AccessControlInfo']['SystemMode'] = '32MB'
else:
warning("Invalid SystemMode, ignoring!")
del rsffile['AccessControlInfo']['SystemMode']
# SystemModeExt
# These are also just flags, they can't be set to any other value.
if accCtrlInfo['flag2'] & 0x4 == 0:
rsffile['AccessControlInfo']['SystemModeExt'] = 'Legacy'
elif accCtrlInfo['flag2'] & 0x4 == 1:
rsffile['AccessControlInfo']['SystemModeExt'] = '124MB'
elif accCtrlInfo['flag2'] & 0x4 == 2:
rsffile['AccessControlInfo']['SystemModeExt'] = '178MB'
else:
warning("Invalid SystemModeExt, ignoring!")
del rsffile['AccessControlInfo']['SystemModeExt']
# FileSystemAccess
for fsPerm in enumerate(FSAccessInfo):
if accCtrlInfo['fsaccess'] & (1 << fsPerm[0]) != 0:
rsffile['AccessControlInfo']['FileSystemAccess'].append(fsPerm[1])
# IoAccessControl
for cap in enumerate(ARM9Access):
if accCtrlInfo['arm9caps'] & (1 << cap[0]) != 0:
rsffile['AccessControlInfo']['IoAccessControl'].append(cap[1])
# ServiceAccessControl
# These need to be converted to real strings later
for service in accCtrlInfo['services']:
if service != b'\x00\x00\x00\x00\x00\x00\x00\x00':
rsffile['AccessControlInfo']['ServiceAccessControl'].append(service)
# SystemControlInfo
# StackSize, RemasterVersion, JumpId, SaveDataSize
rsffile['SystemControlInfo']['StackSize'] = sysCtrlInfo['stack']
rsffile['SystemControlInfo']['RemasterVersion'] = sysCtrlInfo['remaster']
rsffile['SystemControlInfo']['JumpId'] = sysCtrlInfo['jumpid']
rsffile['SystemControlInfo']['SaveDataSize'] = sysCtrlInfo['savesize'] # needs to be made "human readable" later
# Dependency
for dep in sysCtrlInfo['dependencies']:
if dep != 0:
rsffile['SystemControlInfo']['Dependency'].append(dep)
else: # No ExHeader NCCH
del rsffile['AccessControlInfo']
del rsffile['SystemControlInfo']
# BasicInfo
# Title
# These all need to be made in to proper strings
if ncchHdr['exhdrsize'] == 0x400:
rsffile['BasicInfo']['Title'] = sysCtrlInfo['title']
else:
del rsffile['BasicInfo']['Title']
# CompanyCode, ProductCode
rsffile['BasicInfo']['CompanyCode'] = ncchHdr['makercode']
rsffile['BasicInfo']['ProductCode'] = ncchHdr['productcode']
# ContentType
if ncchHdr['flags'] >> (8 * 5) >> 2 & 0x7 == 0:
rsffile['BasicInfo']['ContentType'] = 'Application'
elif ncchHdr['flags'] >> (8 * 5) >> 2 & 0x7 == 1:
rsffile['BasicInfo']['ContentType'] = 'SystemUpdate'
elif ncchHdr['flags'] >> (8 * 5) >> 2 & 0x7 == 2:
rsffile['BasicInfo']['ContentType'] = 'Manual'
elif ncchHdr['flags'] >> (8 * 5) >> 2 & 0x7 == 3:
rsffile['BasicInfo']['ContentType'] = 'Child'
elif ncchHdr['flags'] >> (8 * 5) >> 2 & 0x7 == 4:
rsffile['BasicInfo']['ContentType'] = 'Trial'
elif ncchHdr['flags'] >> (8 * 5) >> 2 & 0x7 == 5:
rsffile['BasicInfo']['ContentType'] = 'ExtendedSystemUpdate'
else:
rsffile['BasicInfo']['ContentType'] = 'Unknown'
# TitleInfo
# UniqueId
rsffile['TitleInfo']['UniqueId'] = ncchHdr['programid'] >> 8 & 0xFFFFFF
# Category
try:
rsffile['TitleInfo']['Category'] = categories[ncchHdr['programid'] >> 32 & 0x1FF]
except KeyError:
rsffile['TitleInfo']['Category'] = 'Unknown'
# ChildIndex, DemoIndex, ContentsIndex, Variation, Version
if rsffile['TitleInfo']['Category'] == 'DlpChild':
rsffile['TitleInfo']['ChildIndex'] = ncchHdr['programid'] & 0xFF
elif rsffile['TitleInfo']['Category'] == 'Demo':
rsffile['TitleInfo']['DemoIndex'] = ncchHdr['programid'] & 0xFF
elif rsffile['TitleInfo']['Category'] == 'Contents' or \
rsffile['TitleInfo']['Category'] == 'SharedContents' or \
rsffile['TitleInfo']['Category'] == 'AutoUpdateContents':
rsffile['TitleInfo']['ContentsIndex'] = ncchHdr['programid'] & 0xFF
elif rsffile['TitleInfo']['Category'] == 'AddOnContents':
rsffile['TitleInfo']['Variation'] = ncchHdr['programid'] & 0xFF
elif ncchHdr['programid'] & 0xFF != 0x00:
rsffile['TitleInfo']['Version'] = ncchHdr['programid'] & 0xFF
# Platform
if ncchHdr['flags'] >> (8 * 4) & 0xFF == 1:
rsffile['TitleInfo']['Platform'] = 'ctr' # Old 3DS
elif ncchHdr['flags'] >> (8 * 4) & 0xFF == 2:
rsffile['TitleInfo']['Platform'] = 'snake' # New 3DS
else:
rsffile['TitleInfo']['Platform'] = 'unknown'
def reformatData():
if ncchHdr['exhdrsize'] == 0x400:
# Reformat fields that need it
for i in range(len(rsffile['AccessControlInfo']['InterruptNumbers'])):
rsffile['AccessControlInfo']['InterruptNumbers'][i] =\
"0x%0.2X" % rsffile['AccessControlInfo']['InterruptNumbers'][i]
rsffile['AccessControlInfo']['HandleTableSize'] =\
"0x%0.2X" % rsffile['AccessControlInfo']['HandleTableSize']
# this is possibly removed
if 'AccessibleSaveDataIds' in rsffile['AccessControlInfo']:
for i in range(len(rsffile['AccessControlInfo']['AccessibleSaveDataIds'])):
rsffile['AccessControlInfo']['AccessibleSaveDataIds'][i] =\
"0x%0.6X" % rsffile['AccessControlInfo']['AccessibleSaveDataIds'][i]
if 'ExtSaveDataId' in rsffile['AccessControlInfo']:
rsffile['AccessControlInfo']['ExtSaveDataId'] =\
"0x%0.16X" % rsffile['AccessControlInfo']['ExtSaveDataId']
rsffile['AccessControlInfo']['CoreVersion'] =\
"0x%X" % rsffile['AccessControlInfo']['CoreVersion']
rsffile['AccessControlInfo']['DescVersion'] =\
"0x%X" % rsffile['AccessControlInfo']['DescVersion']
rsffile['AccessControlInfo']['ResourceLimitCategory'] =\
"%0.2X" % rsffile['AccessControlInfo']['ResourceLimitCategory']
rsffile['AccessControlInfo']['MaxCpu'] =\
"0x%X" % rsffile['AccessControlInfo']['MaxCpu']
if 'SystemSaveDataId1' in rsffile['AccessControlInfo']:
rsffile['AccessControlInfo']['SystemSaveDataId1'] =\
"0x%0.8X" % rsffile['AccessControlInfo']['SystemSaveDataId1']
if 'SystemSaveDataId2' in rsffile['AccessControlInfo']:
rsffile['AccessControlInfo']['SystemSaveDataId2'] =\
"0x%0.8X" % rsffile['AccessControlInfo']['SystemSaveDataId2']
if 'OtherUserSaveDataId1' in rsffile['AccessControlInfo']:
rsffile['AccessControlInfo']['OtherUserSaveDataId1'] =\
"0x%0.8X" % rsffile['AccessControlInfo']['OtherUserSaveDataId1']
if 'OtherUserSaveDataId2' in rsffile['AccessControlInfo']:
rsffile['AccessControlInfo']['OtherUserSaveDataId2'] =\
"0x%0.8X" % rsffile['AccessControlInfo']['OtherUserSaveDataId2']
if 'OtherUserSaveDataId3' in rsffile['AccessControlInfo']:
rsffile['AccessControlInfo']['OtherUserSaveDataId3'] =\
"0x%0.8X" % rsffile['AccessControlInfo']['OtherUserSaveDataId3']
for i in range(len(rsffile['AccessControlInfo']['ServiceAccessControl'])):
service = rsffile['AccessControlInfo']['ServiceAccessControl'][i]
endchar = service.find(0x00)
if endchar == -1:
rsffile['AccessControlInfo']['ServiceAccessControl'][i] =\
service.decode('ascii')
else:
rsffile['AccessControlInfo']['ServiceAccessControl'][i] =\
service[:endchar].decode('ascii')
rsffile['SystemControlInfo']['StackSize'] =\
"0x%0.8X" % rsffile['SystemControlInfo']['StackSize']
rsffile['SystemControlInfo']['RemasterVersion'] =\
"%0.4X" % rsffile['SystemControlInfo']['RemasterVersion']
rsffile['SystemControlInfo']['JumpId'] =\
"0x%0.16X" % rsffile['SystemControlInfo']['JumpId']
savesize = rsffile['SystemControlInfo']['SaveDataSize']
if savesize >= GIGABYTE and (savesize % GIGABYTE) == 0:
rsffile['SystemControlInfo']['SaveDataSize'] = "%dGB" % (savesize / GIGABYTE)
elif savesize >= MEGABYTE and savesize < GIGABYTE and (savesize % MEGABYTE) == 0:
rsffile['SystemControlInfo']['SaveDataSize'] = "%dMB" % (savesize / MEGABYTE)
elif savesize >= KILOBYTE and savesize < MEGABYTE and (savesize % KILOBYTE) == 0:
rsffile['SystemControlInfo']['SaveDataSize'] = "%dKB" % (savesize / KILOBYTE)
else: # This probably doesn't work but it shouldn't ever come up
rsffile['SystemControlInfo']['SaveDataSize'] = "%d" % savesize
for i in range(len(rsffile['SystemControlInfo']['Dependency'])):
rsffile['SystemControlInfo']['Dependency'][i] =\
"asdf: 0x%0.16XL" % rsffile['SystemControlInfo']['Dependency'][i]
title = rsffile['BasicInfo']['Title']
endchar = title.find(0x00)
if endchar == -1:
rsffile['BasicInfo']['Title'] =\
title.decode('ascii')
else:
rsffile['BasicInfo']['Title'] =\
title[:endchar].decode('ascii')
rsffile['BasicInfo']['CompanyCode'] =\
rsffile['BasicInfo']['CompanyCode'].decode('ascii')
pcode = rsffile['BasicInfo']['ProductCode']
rsffile['BasicInfo']['ProductCode'] =\
pcode[:pcode.find(b'\x00')].decode('ascii')
rsffile['TitleInfo']['UniqueId'] =\
"0x%0.6X" % rsffile['TitleInfo']['UniqueId']
def keyString(category, name):
return("%s/%s" % (category, name))
def splitKeyValue(kv):
equal = kv.index('=')
if equal == 0 or equal == -1:
raise ValueError
return({'key': kv[:equal], 'value': kv[equal+1:]})
def splitKeyString(ks):
slash = ks.find('/')
if slash == 0 or slash + 1 == len(ks):
raise ValueError
if slash == -1:
return({'category': ks})
return({'category': ks[:slash], 'name': ks[slash+1:]})
def listTypes(listtypes):
print(repr(specialLists))
for item in listtypes:
listtype = splitKeyValue(item)
if listtype['value'] == 'nodash':
if listtype['key'] in specialLists:
raise ValueError("%s is already set to nodash" % listtype['key'])
else:
specialLists.append(listtype['key'])
elif listtype['value'] == 'dash':
if listtype['key'] not in specialLists:
raise ValueError("%s is not set to nodash" % listtype['key'])
else:
specialLists.remove(listtype['key'])
else:
raise ValueError("List type must be 'dash' or 'nodash'")
def getListIndex(key):
index = 0
dot = key.find('.')
if dot == -1:
if key[len(key) - 1] == '+':
return('add', key[:-1])
elif key[len(key) - 1] == '-':
return('clear', key[:-1])
else:
return(None, key)
else:
if dot == 0 or dot + 1 == len(key):
raise ValueError
index = int(key[dot+1:])
if index < 0:
raise ValueError("Negative list index %d" % index)
return(index, key[:dot])
def delOptions(options):
for option in options:
kv = splitKeyString(option)
if 'name' in kv:
category = kv['category']
index, name = getListIndex(kv['name'])
if index == 'add':
raise ValueError("'+' operator invalid for '--deloptions'")
elif index == 'clear':
if type(rsffile[category][name]) is list:
rsffile[category][name] = list()
else:
raise ValueError("%s is not a list" % option)
elif index == None:
try:
del rsffile[category][name]
except KeyError as e:
raise KeyError("%s doesn't exist" % option)
else:
del rsffile[category][name][index]
else:
try:
del rsffile[kv['category']]
except KeyError as e:
raise KeyError("%s isn't a category" % option)
def addOptions(options):
for option in options:
kv = splitKeyValue(option)
ks = splitKeyString(kv['key'])
if 'name' in ks:
category = ks['category']
index, name = getListIndex(ks['name'])
value = kv['value']
if index == 'add':
if category not in rsffile:
rsffile[category] = dict()
if name not in rsffile[category]:
rsffile[category][name] = list()
if type(rsffile[category][name]) is list:
rsffile[category][name].append(value)
else:
raise KeyError("%s is not a list" % kv['key'])
elif index == 'clear':
raise KeyError("Can't use assingment with clear")
elif index == None:
if category not in rsffile:
rsffile[category] = dict()
elif name in rsffile[category] and type(rsffile[category][name]) is list:
raise KeyError("Can't assign to list type, delete list first.")
rsffile[category][name] = value
else:
if type(rsffile[category][name]) is not list:
raise KeyError("Can't index non list type")
else:
rsffile[category][name][index] = value
# Not much safeguarding in this, but it's only a problem if you're noodling around
# Very VERY simple YAML printer
def writeRSF():
for category in rsffile.keys():
print("%s:" % (category))
for item in rsffile[category].keys():
if type(rsffile[category][item]) is str:
print(" %s: %s" % (item, rsffile[category][item]))
elif type(rsffile[category][item]) is int:
print(" %s: %d" % (item, rsffile[category][item]))