-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path3ds_arm11code.s
2747 lines (2305 loc) · 51.5 KB
/
3ds_arm11code.s
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
.section .init
.global _start
.arm
//This is a loader for hblauncher, for the Old3DS/New3DS system web-browsers.
//Also note that APT would be broken even worse than with other hax which boot hblauncher, with the spider version of this.
_start:
mov r7, r0
ldr sp, =(0x10000000-0x7000)
add r1, pc, #1
bx r1
.thumb
ldr r0, =0x3a545041 @ Get APT:U handle, @ sp+12.
str r0, [sp, #4]
mov r0, #0x55
str r0, [sp, #8]
add r0, sp, #12
add r1, sp, #4
mov r2, #5
mov r3, #0
ldr r4, [r7, #0x18]
blx r4 @ srv_GetServiceHandle
ldr r3, =0x24242424
blx checkerror_triggercrash
add r0, sp, #12
add r1, sp, #16 @ u8 out new3ds flag, this will be zero if the func returns any errors. 0 = Old3DS, 1 = New3DS.
bl APT_CheckNew3DS
ldr r0, [sp, #12]
blx svcCloseHandle
ldrb r0, [sp, #16]
cmp r0, #0
bne menustubcpy_end
blx getaddrs_menustub
mov r2, r1
mov r1, r0
ldr r0, =0x1000
add r0, r0, r7
menustubcpy:
ldr r3, [r1]
str r3, [r0]
add r1, r1, #4
add r0, r0, #4
cmp r1, r2
blt menustubcpy
blx getaddr_menustubtakeover
mov r1, r0
ldr r0, =(0x1000+0x320)
add r0, r0, r7
ldr r2, =0x280
menustubtakeovercpy:
ldr r3, [r1]
str r3, [r0]
add r1, r1, #4
add r0, r0, #4
sub r2, r2, #4
cmp r2, #0
bgt menustubtakeovercpy
ldr r0, =0x1000 @ Flush dcache for the menustub+menustubtakeover data.
add r0, r0, r7
ldr r1, =(0x320+0x280)
ldr r3, [r7, #0x20]
blx r3
menustubcpy_end:
add r0, sp, #36
add r1, sp, #40
bl loadsd_payload
cmp r0, #0
beq _start_loadsd_success
add r0, sp, #36
add r1, sp, #40
ldrb r2, [sp, #16]
bl http_download_payload
_start_loadsd_success:
ldr r3, =0x80808080
blx checkerror_triggercrash @ Trigger crash on payload loading fail.
ldr r5, [sp, #36]
ldr r6, [sp, #40]
mov r0, r5 @ Flush dcache for the menuropbin data.
ldr r1, =0x10000
ldr r3, [r7, #0x20]
blx r3
ldr r0, =0x1ED02A04
ldr r1, =0x0100A5FF
bl gsp_writereg @ Set sub-screen colorfill to orange.
@ Copy the menuropbin via the GPU.
ldrb r2, [sp, #16]
cmp r2, #0
beq menuropbin_vramcopy
mov r0, r5 @ New3DS
ldr r1, =0x38c40000
ldr r2, =0x10000
bl gxcmd4
blx svcSleepThread_1second
@ Clear the hblauncher parameter block.
mov r0, r5
mov r1, #0
mov r2, r1
ldr r3, =0x2800
hblauncher_clear_paramblk:
str r1, [r0, r2]
add r2, r2, #4
cmp r2, r3
blt hblauncher_clear_paramblk
mov r1, r3
ldr r3, [r7, #0x20]
blx r3
mov r0, r5
ldr r1, =0x38c40000 - 0x800*6
ldr r2, =0x2800 @ 0x2000 is included for backwards-compatibility.
bl gxcmd4
blx svcSleepThread_1second
b menuropbin_vramcopy_finish
menuropbin_vramcopy: @ Old3DS
mov r0, r5
ldr r1, =0x1f5f0000
ldr r2, =0x10000
bl gxcmd4 @ Copy the menuropbin data into VRAM, which will be loaded by the below homemenu code later.
blx svcSleepThread_1second
menuropbin_vramcopy_finish:
mov r0, r6
bl calcverify_payload_size
mov r6, r0
ldrb r2, [sp, #16]
cmp r2, #0
beq menutakeover_begin
ldr r3, =0x10000
mov r0, r5
add r0, r0, r3 @ loadropbin blob address
ldr r1, [sp, #40]
sub r1, r1, r3 @ loadropbin blob size
bl regular_menutakeover
ldr r3, =0xa4a4a4a4
blx checkerror_triggercrash
bl aptExit
mov r0, r5
mov r1, r6
bl freemem
ldr r3, =0xa8a8a8a8
blx checkerror_triggercrash
b shutdown_gsp
menutakeover_begin:
mov r0, r5
mov r1, r6
bl freemem
ldr r0, =0x09a00000-0xd00000 @ Free some of the spider regular-heap so that there's enough memory available to launch Home Menu.
ldr r1, =(0xd00000)
bl freemem
@ Locate the first bl instruction in the gxcmd4 function, then get the func-addr from that.
ldr r0, [r7, #0x1c] @ gxcmd4 funcptr
menutakeover_begin_gxcmd4findbl:
ldr r1, [r0]
add r0, r0, #4
lsr r1, r1, #24
cmp r1, #0xeb
bne menutakeover_begin_gxcmd4findbl
sub r0, r0, #4
mov r1, #0
blx parse_branch
blx r0 @ Call the located function.
add r0, r0, #0x58
ldr r1, [r0]
str r0, [sp, #52] @ sp+52 = state ptr, +56 = value from that addr.
str r1, [sp, #56]
add r0, sp, #12 @ Get APT:U handle, @ sp+12.
add r1, sp, #4
mov r2, #5
mov r3, #0
ldr r4, [r7, #0x18]
blx r4 @ srv_GetServiceHandle
ldr r3, =0x48484848
blx checkerror_triggercrash
add r0, sp, #12
ldr r1, =0x101
bl APT_PreloadLibraryApplet @ Launch Home Menu as a "LibraryApplet".
ldr r3, =0xc0c0c0c0
blx checkerror_triggercrash
add r0, sp, #12
ldr r1, =0x101
bl APT_FinishPreloadingLibraryApplet
ldr r3, =0xd0d0d0d0
blx checkerror_triggercrash
ldr r0, [sp, #12]
blx svcCloseHandle
ldr r0, =(0x6500000+0x14000000)
str r0, [sp, #44]
mov r0, #0
str r0, [sp, #48]
@ Jump to menutextphys_search when the running FIRM is older than v10.4.
ldr r0, =0x1FF80000
ldr r0, [r0]
lsr r0, r0, #8
ldr r1, =((2<<24) | (50<<16) | (11<<8))
lsr r1, r1, #8
cmp r0, r1
bcc menutextphys_search
ldr r0, [sp, #44]
ldr r1, =0x40000
sub r0, r0, r1
str r0, [sp, #44]
menutextphys_search: @ v10.4 loader-process in FIRM added "randomization" for the codebin physmem of certain processes, including Home Menu.
ldr r0, [sp, #44]
ldr r5, =0x1f3b0000
mov r1, r5
ldr r2, =0x240000
mov r6, r1
add r6, r6, r2
sub r6, r6, #4
ldr r4, [r6]
bl gxcmd4 @ Copy <above size> of Home Menu .text to <outbuf>. 0x10000-bytes is the lowest possible size of a chunk with the physmem-randomization.
mov r1, #0
ldr r0, [sp, #52]
str r1, [r0] @ Overwrite a ptr with 0x0(address of the gx command queue in sharedmem), so that other threads can't use gx cmds.
waitcodevramcpy_finish: @ Wait for the above copy to completely finish.
ldr r0, [r6]
cmp r0, r4
beq waitcodevramcpy_finish
menutextphys_search_checkword:
ldr r3, =0xeb000007 @ Check whether section+0 matches homemenu .text+0.
ldr r1, [r5]
cmp r1, r3
beq menutextphys_search_done
ldr r2, =0x10000
ldr r0, [sp, #44]
add r0, r0, r2
str r0, [sp, #44]
//ldr r0, =0x00204BBC
ldr r0, =0x240000
ldr r1, [sp, #48]
add r1, r1, r2
str r1, [sp, #48]
add r5, r5, r2
cmp r1, r0
//bcc menutextphys_search//Looping with gxcmd4 here is useless since homemenu takeover will fail due to .text not being overwritten in time.
bcc menutextphys_search_checkword
ldr r3, =0xa819f288 @ Trigger crash on failure.
str r3, [r3]
menutextphys_search_done:
@ Begin auto-locating the target code for homemenu takeover.
ldr r4, =0x00101000
mov r0, r4
ldr r2, =0x1000
add r5, r5, r2
mov r1, r5
ldr r2, =0xef000003 @ Locate the "svc 0x03" instruction, which is the function right before main().
_start_locatecode_l0:
ldr r3, [r1]
add r1, r1, #4
add r0, r0, #4
cmp r3, r2
bne _start_locatecode_l0
ldr r2, =0xe3a00000 @ Locate the bl instruction following a "mov r0, #0", in main().
_start_locatecode_l1:
ldr r3, [r1]
add r1, r1, #4
add r0, r0, #4
cmp r3, r2
bne _start_locatecode_l1
ldr r1, [r1]
blx parse_branch
add r0, r0, #0x18 @ Get the address of function begin called with the bl-instruction @ +0x18 in the above function.
mov r1, r0
sub r1, r1, r4
add r1, r1, r5
ldr r1, [r1]
blx parse_branch
mov r1, r0
sub r1, r1, r4
add r1, r1, r5 @ r0/r1 is now the addresses for the target heap-init function.
/*mov r6, #2 @ Search for two "cmp <reg>, #100" instructions.
_start_locatecode_l2:
ldr r3, [r1]
add r1, r1, #4
add r0, r0, #4
ldr r2, =0xf0000
bic r3, r3, r2
ldr r2, =0xe3500064
cmp r3, r2
bne _start_locatecode_l2
sub r6, r6, #1
cmp r6, #0
bgt _start_locatecode_l2
mov r6, #4 @ Search for 4 bl instructions.
_start_locatecode_l3:
ldrb r3, [r1, #3]
add r1, r1, #4
add r0, r0, #4
cmp r3, #0xeb
bne _start_locatecode_l3
sub r6, r6, #1
cmp r6, #0
bgt _start_locatecode_l3
sub r0, r0, #4
sub r0, r0, #0x10
*/
add r0, r0, #0x10
mov r3, #4 @ Clear the low 4-bits for GPU alignment.
lsr r0, r0, r3
lsl r0, r0, r3
sub r0, r0, r4
mov r3, r0
ldr r0, [sp, #52] @ Restore the original gxcmd queue ptr.
ldr r1, [sp, #56]
str r1, [r0]
ldr r0, =0x1000
ldr r1, [sp, #44] @ .text+<above offset>
add r1, r1, r3
add r1, r1, r0
add r0, r0, r7
ldr r2, =0x320
add r0, r0, r2
ldr r2, =0x280
bl gxcmd4 @ Overwrite the homemenu code which handles allocating+initializing the initial heaps(the target code is near the beginning of the function), with menustubtakeover.
ldr r0, =0x1000 @ Copy menustub to .text+0x40.
mov r2, #0x40
ldr r1, [sp, #44]
add r1, r1, r2
add r0, r0, r7
ldr r2, =0x320
bl gxcmd4
/*
ldr r0, =100000
mov r1, #0
blx svcSleepThread
ldr r0, =(0x6500000+0x14000000) @ .text+0
ldr r1, =0x1f510000
ldr r2, =0xf0000
bl gxcmd4 @ Copy the first 0xf0000-bytes of Home Menu .text to VRAM+0x510000.
*/
@ Shutdown GSP.
shutdown_gsp:
bl GSPGPU_UnregisterInterruptRelayQueue
bl GSPGPU_ReleaseRight
svc 0x03 @ Terminate the browser process.
b .
.pool
.type freemem, %function
freemem: @ r0 = addr, r1 = size
push {r4, lr}
mov r3, r1 @ size
mov r1, r0 @ addr
mov r0, #1 @ operation
mov r4, #0 @ permissions
mov r2, #0 @ addr1
blx svcControlMemory
pop {r4, pc}
.type gxcmd4, %function
gxcmd4: @ r0 = src, r1 = dst, r2 = size
push {r4, lr}
sub sp, #16
mov r3, #0
str r3, [sp, #0]
str r3, [sp, #4]
str r3, [sp, #8]
mov r3, #0x8
str r3, [sp, #12]
mov r3, #0
ldr r4, [r7, #0x1c] @ gxcmd4
blx r4
add sp, sp, #16
pop {r4, pc}
.pool
.type APT_PreloadLibraryApplet, %function
APT_PreloadLibraryApplet: @ inr0=handle*, inr1=NS_APPID appID
push {r0, r1, r2, r3, r4, r5, lr}
blx get_cmdbufptr
mov r4, r0
ldr r0, [sp, #0]
ldr r5, =0x00160040
str r5, [r4, #0]
ldr r1, [sp, #4]
str r1, [r4, #4]
ldr r0, [r0]
blx svcSendSyncRequest
cmp r0, #0
bne APT_PreloadLibraryApplet_end
ldr r0, [r4, #4]
APT_PreloadLibraryApplet_end:
add sp, sp, #16
pop {r4, r5, pc}
.pool
.type APT_FinishPreloadingLibraryApplet, %function
APT_FinishPreloadingLibraryApplet: @ inr0=handle*, inr1=NS_APPID appID
push {r0, r1, r2, r3, r4, r5, lr}
blx get_cmdbufptr
mov r4, r0
ldr r0, [sp, #0]
ldr r5, =0x00170040
str r5, [r4, #0]
ldr r1, [sp, #4]
str r1, [r4, #4]
ldr r0, [r0]
blx svcSendSyncRequest
cmp r0, #0
bne APT_FinishPreloadingLibraryApplet_end
ldr r0, [r4, #4]
APT_FinishPreloadingLibraryApplet_end:
add sp, sp, #16
pop {r4, r5, pc}
.pool
APT_PrepareToStartSystemApplet: @ inr0=handle*, inr1=NS_APPID appID
push {r0, r1, r2, r3, r4, r5, lr}
blx get_cmdbufptr
mov r4, r0
ldr r0, [sp, #0]
ldr r5, =0x00190040
str r5, [r4, #0]
ldr r1, [sp, #4]
str r1, [r4, #4]
ldr r0, [r0]
blx svcSendSyncRequest
cmp r0, #0
bne APT_PrepareToStartSystemApplet_end
ldr r0, [r4, #4]
APT_PrepareToStartSystemApplet_end:
add sp, sp, #16
pop {r4, r5, pc}
.pool
APT_StartSystemApplet: @ inr0=handle*, inr1=appid, inr2=inhandle, inr3=u32 bufsize, insp0=u32* buf
push {r0, r1, r2, r3, r4, r5, lr}
blx get_cmdbufptr
mov r4, r0
ldr r0, [sp, #0]
ldr r5, =0x001F0084
str r5, [r4, #0]
ldr r1, [sp, #4] @ appid
str r1, [r4, #4]
mov r1, #0
str r1, [r4, #12]
ldr r1, [sp, #8] @ inhandle
str r1, [r4, #16]
ldr r1, [sp, #12] @ bufsize
str r1, [r4, #8]
mov r3, #2
lsl r1, r1, #14
orr r1, r1, r3
str r1, [r4, #20]
ldr r1, [sp, #28] @ buf0
str r1, [r4, #24]
ldr r0, [r0]
blx svcSendSyncRequest
cmp r0, #0
bne APT_StartSystemApplet_end
ldr r0, [r4, #4]
APT_StartSystemApplet_end:
add sp, sp, #16
pop {r4, r5, pc}
.pool
APT_CheckNew3DS: @ inr0=handle*, inr1=u8* out
push {r0, r1, r2, r3, r4, r5, lr}
blx get_cmdbufptr
mov r4, r0
ldr r2, [sp, #4]
mov r3, #0
strb r3, [r2]
ldr r0, [sp, #0]
ldr r5, =0x01020000
str r5, [r4, #0]
ldr r0, [r0]
blx svcSendSyncRequest
cmp r0, #0
bne APT_CheckNew3DS_end
ldr r0, [r4, #4]
cmp r0, #0
bne APT_CheckNew3DS_end
ldrb r1, [r4, #8]
ldr r2, [sp, #4]
strb r1, [r2]
APT_CheckNew3DS_end:
add sp, sp, #16
pop {r4, r5, pc}
.pool
GSPGPU_UnregisterInterruptRelayQueue:
push {r4, lr}
blx get_cmdbufptr
mov r4, r0
ldr r1, =0x00140000
str r1, [r4, #0]
ldr r0, [r7, #0x58]
ldr r0, [r0]
blx svcSendSyncRequest
cmp r0, #0
bne GSPGPU_UnregisterInterruptRelayQueue_end
ldr r0, [r4, #4]
GSPGPU_UnregisterInterruptRelayQueue_end:
pop {r4, pc}
.pool
GSPGPU_ReleaseRight:
push {r4, lr}
blx get_cmdbufptr
mov r4, r0
ldr r1, =0x00170000
str r1, [r4, #0]
ldr r0, [r7, #0x58]
ldr r0, [r0]
blx svcSendSyncRequest
cmp r0, #0
bne GSPGPU_ReleaseRight_end
ldr r0, [r4, #4]
GSPGPU_ReleaseRight_end:
pop {r4, pc}
.pool
gsp_writereg: @ Write an u32 to a GPU reg. r0 = regaddr, r1 = u32 val. regaddr can be IO vaddr, or relative to 0x1EB00000.
push {lr}
sub sp, sp, #4
ldr r3, =0x1EB00000
cmp r0, r3
blt gsp_writereg_start
sub r0, r0, r3
gsp_writereg_start:
str r1, [sp, #0]
mov r1, sp
mov r2, #4
bl GSPGPU_WriteHWRegs
add sp, sp, #4
pop {pc}
.pool
GSPGPU_WriteHWRegs: @ r0=gpuregadr, r1=buf*, r2=size
push {r0, r1, r2, r4, lr}
blx get_cmdbufptr
mov r4, r0
ldr r1, =0x00010082
str r1, [r4, #0]
ldr r1, [sp, #0]
str r1, [r4, #4]
ldr r1, [sp, #8]
str r1, [r4, #8]
lsl r1, r1, #14
mov r2, #2
orr r1, r1, r2
str r1, [r4, #12]
ldr r1, [sp, #4]
str r1, [r4, #16]
ldr r0, [r7, #0x58]
ldr r0, [r0]
blx svcSendSyncRequest
cmp r0, #0
bne GSPGPU_WriteHWRegs_end
ldr r0, [r4, #4]
GSPGPU_WriteHWRegs_end:
add sp, sp, #12
pop {r4, pc}
.pool
calcverify_payload_size: @ r0 = raw payload size.
@ The sizes must not have bitmask 0xf0000000 set. Returns the allocsize on success, <0 on error.
mov r3, r0
mov r1, #2
mvn r0, r1
mov r2, r3
lsr r2, r2, #28
cmp r2, #0
bne calcverify_payload_size_end @ Verify the raw payload size.
ldr r1, =0x10004
cmp r3, r1
bcc calcverify_payload_size_end @ Original payload size must be at least 0x10004-bytes.
ldr r1, =0xfff
add r3, r3, r1
bic r3, r3, r1
mov r2, r3
lsr r2, r2, #28
cmp r2, #0
bne calcverify_payload_size_end
mov r0, r3
calcverify_payload_size_end:
bx lr
.pool
.type loadsd_payload, %function
loadsd_payload: @ r0 = ptr where the address of the allocbuf is written, r1 = u32* output payload-size.
push {r4, r5, lr}
push {r0, r1}
sub sp, sp, #0x20
blx getaddr_sdpayload_path
mov r5, r0
add r0, sp, #12 @ ctx
mov r1, #0
mov r2, r1
mov r3, #0x14
loadsd_payload_ctxclr:
str r1, [r0, r2]
add r2, r2, #4
cmp r2, r3
blt loadsd_payload_ctxclr
mov r1, r5 @ UTF-16 filepath
mov r2, #1 @ openflags
ldr r3, [r7, #0x24]
blx r3 @ IFile_Open
mov r2, #0 @ Set r2 for debug.
cmp r0, #0
bne loadsd_payload_end
add r0, sp, #12
add r1, sp, #4
ldr r3, [r7, #0x2c]
blx r3 @ IFile_GetSize
cmp r0, #0
bne loadsd_payload_end
ldr r0, [sp, #4]
bl calcverify_payload_size
cmp r0, #0
blt loadsd_payload_end
mov r3, r0
mov r1, #0 @ addr
ldr r0, =0x10003 @ operation
mov r4, #3 @ permissions
mov r2, #0 @ addr1
blx svcControlMemory
ldr r3, =0x70707070
blx checkerror_triggercrash @ Trigger crash on memalloc fail.
mov r4, r1
ldr r2, [sp, #0x20]
str r4, [r2]
@ Read the file with the above size into the input buffer.
add r0, sp, #12 @ ctx
add r1, sp, #0 @ u32* readcount
mov r2, r4
ldr r3, [sp, #4]
ldr r4, [sp, #0x24]
str r3, [r4]
ldr r4, [r7, #0x34]
blx r4 @ IFile_Read
mov r2, #3
cmp r0, #0
bne loadsd_payload_end
mov r2, #4
mvn r0, r2 @ Return ~4 when readcount is zero.
ldr r1, [sp, #0]
cmp r1, #0
beq loadsd_payload_end
ldr r0, [sp, #12]
ldr r3, [r7, #0x28]
blx r3 @ IFile_Close
mov r0, #0
loadsd_payload_end:
add sp, sp, #0x28
pop {r4, r5, pc}
.pool
@ This is based on code from hblauncher.
regular_menutakeover: @ r0 = src loadropbin blob address, r1 = blob size.
push {r4, r5, r6, lr}
sub sp, sp, #8
str r0, [sp, #0]
str r1, [sp, #4]
ldr r3, =0x10000 @ size
mov r1, #0 @ addr
ldr r0, =0x10003 @ operation
mov r4, #3 @ permissions
mov r2, #0 @ addr1
blx svcControlMemory
ldr r3, =0x74747474
blx checkerror_triggercrash @ Trigger crash on memalloc fail.
mov r4, r1
ldr r5, =0x37c00000 @ Hard-coded for New3DS, since this function is only used by this loader on New3DS.
regular_menutakeover_lp:
mov r0, r4 @ Flush buffer dcache.
ldr r1, =0x10000
ldr r3, [r7, #0x20]
blx r3
mov r0, r5
mov r1, r4
ldr r2, =0x10000
bl gxcmd4 @ Copy homemenu linearmem heap data into the above buffer, then wait for the copy to finish.
ldr r0, =1000000
mov r1, #0
blx svcSleepThread
mov r0, #0
regular_menutakeover_scanlp:
mov r2, r0
add r2, r2, r4
ldr r1, [r2, #0x8]
ldr r3, =0x5544
cmp r1, r3
bne regular_menutakeover_scanlpnext
ldr r1, [r2, #0xc]
cmp r1, #0x80
bne regular_menutakeover_scanlpnext
ldr r1, [r2, #0x18]
cmp r1, #0
beq regular_menutakeover_scanlpnext
ldr r1, [r2, #0x7c]
ldr r3, =0x6E4C5F4E
cmp r1, r3
bne regular_menutakeover_scanlpnext
mov r1, r5
add r1, r1, r0
add r1, r1, #0x18
mov r0, r4
ldr r2, [sp, #0]
ldr r3, [sp, #4]
bl inject_payload
mov r6, r0
b regular_menutakeover_end
regular_menutakeover_scanlpnext:
add r0, r0, #4
ldr r1, =(0x10000-0x10)
cmp r0, r1
bcc regular_menutakeover_scanlp
regular_menutakeover_lpnext:
ldr r1, =(0x10000-0x100)
ldr r3, =(0x37c00000+0x01000000)
add r5, r5, r1
cmp r5, r3
bcc regular_menutakeover_lp
mov r6, #0
mvn r6, r6
regular_menutakeover_end:
ldr r0, =100000000
mov r1, #0
blx svcSleepThread
mov r0, r4
ldr r1, =0x10000
bl freemem
mov r0, r6
add sp, sp, #8
pop {r4, r5, r6, pc}
.pool
@ This is based on code from hblauncher.
inject_payload: @ r0 = buffer, r1 = target_address, r2 = binaryblob*, r3 = binaryblobsize.
push {r4, r5, r6, lr}
sub sp, sp, #12
str r2, [sp, #0]
str r3, [sp, #4]
str r1, [sp, #8]
mov r4, r0
mov r5, r1
mov r6, r1
lsr r5, r5, #8
lsl r5, r5, #8
sub r6, r6, r5
add r6, r6, r4
mov r0, r4 @ Flush buffer dcache.
ldr r1, =0x1000
ldr r3, [r7, #0x20]
blx r3
@ Read homemenu memory + wait for the copy to finish.
mov r0, r5
mov r1, r4
ldr r2, =0x1000
bl gxcmd4
ldr r0, =10000000
mov r1, #0
blx svcSleepThread
mov r0, r4 @ Flush buffer dcache.
ldr r1, =0x1000
ldr r3, [r7, #0x20]
blx r3
mov r0, #0 @ Copy the binary into the buffer with patches when needed.
ldr r1, [sp, #0]
inject_payload_lp:
ldr r2, [r1, r0]
ldr r3, =(0xBABE0000+0x100)
cmp r2, r3
bcs inject_payload_lp0
ldr r3, =(0xBABE0000-0x100+1)
cmp r2, r3
bcc inject_payload_lp0
ldr r3, [sp, #8]
add r2, r2, r3
ldr r3, =0xBABE0000
sub r2, r2, r3
str r2, [r6, r0]
b inject_payload_lpnext
inject_payload_lp0:
ldr r3, =0xDEADCAFE
cmp r2, r3
beq inject_payload_lpnext
cmp r0, #0x38
beq inject_payload_lpnext @ Don't corrupt a ptr which Home Menu may use in some cases(this should be value0 anyway). In particular, this fixes a crash with Home Menu where theme shuffling is enabled.
str r2, [r6, r0]
inject_payload_lpnext:
add r0, r0, #4
ldr r2, [sp, #4]
cmp r0, r2
bcc inject_payload_lp
@ Write homemenu memory + wait for the copy to finish.
mov r0, r4 @ Flush buffer dcache.
ldr r1, =0x1000
ldr r3, [r7, #0x20]
blx r3
mov r0, r4
mov r1, r5
ldr r2, =0x1000
bl gxcmd4
ldr r0, =10000000
mov r1, #0
blx svcSleepThread
mov r0, #0
add sp, sp, #12
pop {r4, r5, r6, pc}
.pool
aptExit: @ Exit from the current system-applet with APT(homemenu takeover won't work without exiting via APT properly). This is only used on New3DS.
push {r4, r5, r6, lr}
sub sp, sp, #16
ldr r0, =0x3a545041
str r0, [sp, #4]
mov r0, #0x55
str r0, [sp, #8]
add r0, sp, #12 @ Get APT:U handle, @ sp+12.
add r1, sp, #4
mov r2, #5
mov r3, #0
ldr r4, [r7, #0x18]
blx r4 @ srv_GetServiceHandle
mov r4, r0
cmp r0, #0
bne aptExit_end
add r0, sp, #12
ldr r1, =0x101
bl APT_PrepareToStartSystemApplet
mov r4, r0
cmp r4, #0
bne aptExit_finished
add r0, sp, #12 @ handle*
ldr r1, =0x101 @ appid
mov r2, #0 @ inhandle
mov r3, #0
str r3, [sp, #0] @ bufsize/buf*
bl APT_StartSystemApplet
mov r4, r0
cmp r4, #0
bne aptExit_finished
aptExit_finished:
ldr r0, [sp, #12]
blx svcCloseHandle
aptExit_end:
mov r0, r4
add sp, sp, #16