/
godmode.c
2508 lines (2351 loc) · 121 KB
/
godmode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
#include "godmode.h"
#include "memmap.h"
#include "support.h"
#include "ui.h"
#include "hid.h"
#include "fs.h"
#include "utils.h"
#include "nand.h"
#include "virtual.h"
#include "vcart.h"
#include "game.h"
#include "disadiff.h"
#include "unittype.h"
#include "entrypoints.h"
#include "bootfirm.h"
#include "pcx.h"
#include "timer.h"
#include "rtc.h"
#include "power.h"
#include "vram0.h"
#include "i2c.h"
#include "lottery.h"
#define N_PANES 2
#define COLOR_TOP_BAR (PERM_RED ? COLOR_RED : PERM_ORANGE ? COLOR_ORANGE : PERM_BLUE ? COLOR_BRIGHTBLUE : \
PERM_YELLOW ? COLOR_BRIGHTYELLOW : PERM_GREEN ? COLOR_GREEN : COLOR_WHITE)
#define COLOR_ENTRY(e) (((e)->marked) ? COLOR_MARKED : ((e)->type == T_DIR) ? COLOR_DIR : ((e)->type == T_FILE) ? COLOR_FILE : ((e)->type == T_ROOT) ? COLOR_ROOT : COLOR_GREY)
#define BOOTPAUSE_KEY (BUTTON_R1|BUTTON_UP)
#define BOOTMENU_KEY (BUTTON_R1|BUTTON_LEFT)
#define BOOTFIRM_PATHS "0:/bootonce.firm", "0:/boot.firm", "1:/boot.firm"
#define BOOTFIRM_TEMPS 0x1 // bits mark paths as temporary
#ifdef SALTMODE // ShadowHand's own bootmenu key override
#undef BOOTMENU_KEY
#define BOOTMENU_KEY BUTTON_START
#endif
typedef struct {
char path[256];
u32 cursor;
u32 scroll;
} PaneData;
u32 SplashInit(const char* modestr) {
u64 splash_size;
u8* splash = FindVTarFileInfo(LOTTERY_SPLASH, &splash_size);
u8* bitmap = (u8*) malloc(SCREEN_SIZE_TOP);
const char* namestr = FLAVOR " " VERSION;
const char* loadstr = "booting...";
const u32 pos_xb = 10;
const u32 pos_yb = 10;
const u32 pos_xu = SCREEN_WIDTH_BOT - 10 - GetDrawStringWidth(loadstr);
const u32 pos_yu = SCREEN_HEIGHT - 10 - GetDrawStringHeight(loadstr);
ClearScreenF(true, true, COLOR_STD_BG);
if (splash && bitmap && PCX_Decompress(bitmap, SCREEN_SIZE_TOP, splash, splash_size)) {
PCXHdr* hdr = (PCXHdr*) (void*) splash;
DrawBitmap(TOP_SCREEN, -1, -1, PCX_Width(hdr), PCX_Height(hdr), bitmap);
} else DrawStringF(TOP_SCREEN, 10, 10, COLOR_STD_FONT, COLOR_TRANSPARENT, "(%s not found)", LOTTERY_SPLASH);
if (modestr) DrawStringF(TOP_SCREEN, SCREEN_WIDTH_TOP - 10 - GetDrawStringWidth(modestr),
SCREEN_HEIGHT - 10 - GetDrawStringHeight(modestr), COLOR_STD_FONT, COLOR_TRANSPARENT, modestr);
DrawStringF(BOT_SCREEN, pos_xb, pos_yb, COLOR_STD_FONT, COLOR_STD_BG, "%s\n%*.*s\n%s\n \n \n%s\n%s\n \n%s\n%s",
namestr, strnlen(namestr, 64), strnlen(namestr, 64),
"------------------------------", "https://github.com/d0k3/GodMode9",
"Releases:", "https://github.com/d0k3/GodMode9/releases/", // this won't fit with a 8px width font
"Hourlies:", "https://d0k3.secretalgorithm.com/");
DrawStringF(BOT_SCREEN, pos_xu, pos_yu, COLOR_STD_FONT, COLOR_STD_BG, loadstr);
DrawStringF(BOT_SCREEN, pos_xb, pos_yu, COLOR_STD_FONT, COLOR_STD_BG, "built: " DBUILTL);
if (bitmap) free(bitmap);
return 0;
}
#ifndef SCRIPT_RUNNER
static DirStruct* current_dir = NULL;
static DirStruct* clipboard = NULL;
static PaneData* panedata = NULL;
void GetTimeString(char* timestr, bool forced_update, bool full_year) {
static DsTime dstime;
static u64 timer = (u64) -1; // this ensures we don't check the time too often
if (forced_update || (timer == (u64) -1) || (timer_sec(timer) > 30)) {
get_dstime(&dstime);
timer = timer_start();
}
if (timestr) snprintf(timestr, 31, "%s%02lX-%02lX-%02lX %02lX:%02lX", full_year ? "20" : "",
(u32) dstime.bcd_Y, (u32) dstime.bcd_M, (u32) dstime.bcd_D, (u32) dstime.bcd_h, (u32) dstime.bcd_m);
}
void CheckBattery(u32* battery, bool* is_charging) {
if (battery) {
static u32 battery_l = 0;
static u64 timer_b = (u64) -1; // this ensures we don't check too often
if ((timer_b == (u64) -1) || (timer_sec(timer_b) >= 120)) {
battery_l = GetBatteryPercent();
timer_b = timer_start();
}
*battery = battery_l;
}
if (is_charging) {
static bool is_charging_l = false;
static u64 timer_c = (u64) -1;
if ((timer_c == (u64) -1) || (timer_sec(timer_c) >= 1)) {
is_charging_l = IsCharging();
timer_c = timer_start();
}
*is_charging = is_charging_l;
}
}
void GenerateBatteryBitmap(u8* bitmap, u32 width, u32 height, u32 color_bg) {
const u32 color_outline = COLOR_BLACK;
const u32 color_inline = COLOR_LIGHTGREY;
const u32 color_inside = COLOR_LIGHTERGREY;
if ((width < 8) || (height < 6)) return;
u32 battery;
bool is_charging;
CheckBattery(&battery, &is_charging);
u32 color_battery = (is_charging) ? COLOR_BATTERY_CHARGING :
(battery > 70) ? COLOR_BATTERY_FULL : (battery > 30) ? COLOR_BATTERY_MEDIUM : COLOR_BATTERY_LOW;
u32 nub_size = (height < 12) ? 1 : 2;
u32 width_inside = width - 4 - nub_size;
u32 width_battery = (battery >= 100) ? width_inside : ((battery * width_inside) + 50) / 100;
for (u32 y = 0; y < height; y++) {
const u32 mirror_y = (y >= (height+1) / 2) ? height - 1 - y : y;
for (u32 x = 0; x < width; x++) {
const u32 rev_x = width - x - 1;
u32 color = 0;
if (mirror_y == 0) color = (rev_x >= nub_size) ? color_outline : color_bg;
else if (mirror_y == 1) color = ((x == 0) || (rev_x == nub_size)) ? color_outline : (rev_x < nub_size) ? color_bg : color_inline;
else if (mirror_y == 2) color = ((x == 0) || (rev_x <= nub_size)) ? color_outline : ((x == 1) || (rev_x == (nub_size+1))) ? color_inline : color_inside;
else color = ((x == 0) || (rev_x == 0)) ? color_outline : ((x == 1) || (rev_x <= (nub_size+1))) ? color_inline : color_inside;
if ((color == color_inside) && (x < (2 + width_battery))) color = color_battery;
*(bitmap++) = color >> 16; // B
*(bitmap++) = color >> 8; // G
*(bitmap++) = color & 0xFF; // R
}
}
}
void DrawTopBar(const char* curr_path) {
const u32 bartxt_start = (FONT_HEIGHT_EXT >= 10) ? 1 : (FONT_HEIGHT_EXT >= 7) ? 2 : 3;
const u32 bartxt_x = 2;
const u32 len_path = SCREEN_WIDTH_TOP - 120;
char tempstr[64];
// top bar - current path
DrawRectangle(TOP_SCREEN, 0, 0, SCREEN_WIDTH_TOP, 12, COLOR_TOP_BAR);
if (*curr_path) TruncateString(tempstr, curr_path, len_path / FONT_WIDTH_EXT, 8);
else snprintf(tempstr, 16, "[root]");
DrawStringF(TOP_SCREEN, bartxt_x, bartxt_start, COLOR_STD_BG, COLOR_TOP_BAR, tempstr);
bool show_time = true;
#ifdef SHOW_FREE
if (*curr_path) { // free & total storage
const u32 bartxt_rx = SCREEN_WIDTH_TOP - (19*FONT_WIDTH_EXT) - bartxt_x;
char bytestr0[32];
char bytestr1[32];
DrawStringF(TOP_SCREEN, bartxt_rx, bartxt_start, COLOR_STD_BG, COLOR_TOP_BAR, "%19.19s", "LOADING...");
FormatBytes(bytestr0, GetFreeSpace(curr_path));
FormatBytes(bytestr1, GetTotalSpace(curr_path));
snprintf(tempstr, 64, "%s/%s", bytestr0, bytestr1);
DrawStringF(TOP_SCREEN, bartxt_rx, bartxt_start, COLOR_STD_BG, COLOR_TOP_BAR, "%19.19s", tempstr);
show_time = false;
}
#endif
#ifdef MONITOR_HEAP
if (true) { // allocated mem
const u32 bartxt_rx = SCREEN_WIDTH_TOP - (9*FONT_WIDTH_EXT) - bartxt_x;
char bytestr[32];
FormatBytes(bytestr, mem_allocated());
DrawStringF(TOP_SCREEN, bartxt_rx, bartxt_start, COLOR_STD_BG, COLOR_TOP_BAR, "%9.9s", bytestr);
show_time = false;
}
#endif
if (show_time) { // clock & battery
const u32 battery_width = 16;
const u32 battery_height = 9;
const u32 battery_x = SCREEN_WIDTH_TOP - battery_width - bartxt_x;
const u32 battery_y = (12 - battery_height) / 2;
const u32 clock_x = battery_x - (15*FONT_WIDTH_EXT);
char timestr[32];
GetTimeString(timestr, false, false);
DrawStringF(TOP_SCREEN, clock_x, bartxt_start, COLOR_STD_BG, COLOR_TOP_BAR, "%14.14s", timestr);
u8 bitmap[battery_width * battery_height * BYTES_PER_PIXEL];
GenerateBatteryBitmap(bitmap, battery_width, battery_height, COLOR_TOP_BAR);
DrawBitmap(TOP_SCREEN, battery_x, battery_y, battery_width, battery_height, bitmap);
}
}
void DrawUserInterface(const char* curr_path, DirEntry* curr_entry, u32 curr_pane) {
const u32 n_cb_show = 8;
const u32 info_start = (MAIN_SCREEN == TOP_SCREEN) ? 18 : 2; // leave space for the topbar when required
const u32 instr_x = (SCREEN_WIDTH_MAIN - (34*FONT_WIDTH_EXT)) / 2;
const u32 len_info = (SCREEN_WIDTH_MAIN - ((SCREEN_WIDTH_MAIN >= 400) ? 80 : 20)) / 2;
char tempstr[64];
static u32 state_prev = 0xFFFFFFFF;
u32 state_curr =
((*curr_path) ? (1<<0) : 0) |
((clipboard->n_entries) ? (1<<1) : 0) |
((CheckSDMountState()) ? (1<<2) : 0) |
((GetMountState()) ? (1<<3) : 0) |
((GetWritePermissions() > PERM_BASE) ? (1<<4) : 0) |
(curr_pane<<5);
if (state_prev != state_curr) {
ClearScreenF(true, false, COLOR_STD_BG);
state_prev = state_curr;
}
// left top - current file info
if (curr_pane) snprintf(tempstr, 63, "PANE #%lu", curr_pane);
else snprintf(tempstr, 63, "CURRENT");
DrawStringF(MAIN_SCREEN, 2, info_start, COLOR_STD_FONT, COLOR_STD_BG, "[%s]", tempstr);
// file / entry name
ResizeString(tempstr, curr_entry->name, len_info / FONT_WIDTH_EXT, 8, false);
u32 color_current = COLOR_ENTRY(curr_entry);
DrawStringF(MAIN_SCREEN, 4, info_start + 12, color_current, COLOR_STD_BG, "%s", tempstr);
// size (in Byte) or type desc
if (curr_entry->type == T_DIR) {
ResizeString(tempstr, "(dir)", len_info / FONT_WIDTH_EXT, 8, false);
} else if (curr_entry->type == T_DOTDOT) {
snprintf(tempstr, 21, "%20s", "");
} else if (curr_entry->type == T_ROOT) {
int drvtype = DriveType(curr_entry->path);
char drvstr[32];
snprintf(drvstr, 31, "(%s%s)",
((drvtype & DRV_SDCARD) ? "SD" : (drvtype & DRV_RAMDRIVE) ? "RAMdrive" : (drvtype & DRV_GAME) ? "Game" :
(drvtype & DRV_SYSNAND) ? "SysNAND" : (drvtype & DRV_EMUNAND) ? "EmuNAND" : (drvtype & DRV_IMAGE) ? "Image" :
(drvtype & DRV_XORPAD) ? "XORpad" : (drvtype & DRV_MEMORY) ? "Memory" : (drvtype & DRV_ALIAS) ? "Alias" :
(drvtype & DRV_CART) ? "Gamecart" : (drvtype & DRV_VRAM) ? "VRAM" : (drvtype & DRV_SEARCH) ? "Search" : ""),
((drvtype & DRV_FAT) ? " FAT" : (drvtype & DRV_VIRTUAL) ? " Virtual" : ""));
ResizeString(tempstr, drvstr, len_info / FONT_WIDTH_EXT, 8, false);
} else {
char numstr[32];
char bytestr[32];
FormatNumber(numstr, curr_entry->size);
snprintf(bytestr, 31, "%s Byte", numstr);
ResizeString(tempstr, bytestr, len_info / FONT_WIDTH_EXT, 8, false);
}
DrawStringF(MAIN_SCREEN, 4, info_start + 12 + 10, color_current, COLOR_STD_BG, tempstr);
// path of file (if in search results)
if ((DriveType(curr_path) & DRV_SEARCH) && strrchr(curr_entry->path, '/')) {
char dirstr[256];
strncpy(dirstr, curr_entry->path, 256);
*(strrchr(dirstr, '/')+1) = '\0';
ResizeString(tempstr, dirstr, len_info / FONT_WIDTH_EXT, 8, false);
DrawStringF(MAIN_SCREEN, 4, info_start + 12 + 10 + 10, color_current, COLOR_STD_BG, tempstr);
} else {
ResizeString(tempstr, "", len_info / FONT_WIDTH_EXT, 8, false);
DrawStringF(MAIN_SCREEN, 4, info_start + 12 + 10 + 10, color_current, COLOR_STD_BG, tempstr);
}
// right top - clipboard
DrawStringF(MAIN_SCREEN, SCREEN_WIDTH_MAIN - len_info, info_start, COLOR_STD_FONT, COLOR_STD_BG, "%*s",
len_info / FONT_WIDTH_EXT, (clipboard->n_entries) ? "[CLIPBOARD]" : "");
for (u32 c = 0; c < n_cb_show; c++) {
u32 color_cb = COLOR_ENTRY(&(clipboard->entry[c]));
ResizeString(tempstr, (clipboard->n_entries > c) ? clipboard->entry[c].name : "", len_info / FONT_WIDTH_EXT, 8, true);
DrawStringF(MAIN_SCREEN, SCREEN_WIDTH_MAIN - len_info - 4, info_start + 12 + (c*10), color_cb, COLOR_STD_BG, tempstr);
}
*tempstr = '\0';
if (clipboard->n_entries > n_cb_show) snprintf(tempstr, 60, "+ %lu more", clipboard->n_entries - n_cb_show);
DrawStringF(MAIN_SCREEN, SCREEN_WIDTH_MAIN - len_info - 4, info_start + 12 + (n_cb_show*10), COLOR_DARKGREY, COLOR_STD_BG,
"%*s", len_info / FONT_WIDTH_EXT, tempstr);
// bottom: inctruction block
char instr[512];
snprintf(instr, 512, "%s\n%s%s%s%s%s%s%s%s",
FLAVOR " " VERSION, // generic start part
(*curr_path) ? ((clipboard->n_entries == 0) ? "L - MARK files (use with \x18\x19\x1A\x1B)\nX - DELETE / [+R] RENAME file(s)\nY - COPY files / [+R] CREATE entry\n" :
"L - MARK files (use with \x18\x19\x1A\x1B)\nX - DELETE / [+R] RENAME file(s)\nY - PASTE files / [+R] CREATE entry\n") :
((GetWritePermissions() > PERM_BASE) ? "R+Y - Relock write permissions\n" : ""),
(*curr_path) ? "" : (GetMountState()) ? "R+X - Unmount image\n" : "",
(*curr_path) ? "" : (CheckSDMountState()) ? "R+B - Unmount SD card\n" : "R+B - Remount SD card\n",
(*curr_path) ? "R+A - Directory options\n" : "R+A - Drive options\n",
"R+L - Make a Screenshot\n",
"R+\x1B\x1A - Switch to prev/next pane\n",
(clipboard->n_entries) ? "SELECT - Clear Clipboard\n" : "SELECT - Restore Clipboard\n", // only if clipboard is full
"START - Reboot / [+R] Poweroff\nHOME button for HOME menu"); // generic end part
DrawStringF(MAIN_SCREEN, instr_x, SCREEN_HEIGHT - 4 - GetDrawStringHeight(instr), COLOR_STD_FONT, COLOR_STD_BG, instr);
}
void DrawDirContents(DirStruct* contents, u32 cursor, u32* scroll) {
const int str_width = (SCREEN_WIDTH_ALT-3) / FONT_WIDTH_EXT;
const u32 stp_y = min(12, FONT_HEIGHT_EXT + 4);
const u32 start_y = (MAIN_SCREEN == TOP_SCREEN) ? 0 : 12;
const u32 pos_x = 0;
const u32 lines = (SCREEN_HEIGHT-(start_y+2)+(stp_y-1)) / stp_y;
u32 pos_y = start_y + 2;
if (*scroll > cursor) *scroll = cursor;
else if (*scroll + lines <= cursor) *scroll = cursor - lines + 1;
if (*scroll + lines > contents->n_entries)
*scroll = (contents->n_entries > lines) ? contents->n_entries - lines : 0;
for (u32 i = 0; pos_y < SCREEN_HEIGHT; i++) {
char tempstr[str_width + 1];
u32 offset_i = *scroll + i;
u32 color_font = COLOR_WHITE;
if (offset_i < contents->n_entries) {
DirEntry* curr_entry = &(contents->entry[offset_i]);
char namestr[str_width - 10 + 1];
char bytestr[10 + 1];
color_font = (cursor != offset_i) ? COLOR_ENTRY(curr_entry) : COLOR_STD_FONT;
FormatBytes(bytestr, curr_entry->size);
ResizeString(namestr, curr_entry->name, str_width - 10, str_width - 20, false);
snprintf(tempstr, str_width + 1, "%s%10.10s", namestr,
(curr_entry->type == T_DIR) ? "(dir)" : (curr_entry->type == T_DOTDOT) ? "(..)" : bytestr);
} else snprintf(tempstr, str_width + 1, "%-*.*s", str_width, str_width, "");
DrawStringF(ALT_SCREEN, pos_x, pos_y, color_font, COLOR_STD_BG, tempstr);
pos_y += stp_y;
}
const u32 flist_height = (SCREEN_HEIGHT - start_y);
const u32 bar_width = 2;
if (contents->n_entries > lines) { // draw position bar at the right
const u32 bar_height_min = 32;
u32 bar_height = (lines * flist_height) / contents->n_entries;
if (bar_height < bar_height_min) bar_height = bar_height_min;
const u32 bar_pos = ((u64) *scroll * (flist_height - bar_height)) / (contents->n_entries - lines) + start_y;
DrawRectangle(ALT_SCREEN, SCREEN_WIDTH_ALT - bar_width, start_y, bar_width, (bar_pos - start_y), COLOR_STD_BG);
DrawRectangle(ALT_SCREEN, SCREEN_WIDTH_ALT - bar_width, bar_pos + bar_height, bar_width, SCREEN_HEIGHT - (bar_pos + bar_height), COLOR_STD_BG);
DrawRectangle(ALT_SCREEN, SCREEN_WIDTH_ALT - bar_width, bar_pos, bar_width, bar_height, COLOR_SIDE_BAR);
} else DrawRectangle(ALT_SCREEN, SCREEN_WIDTH_ALT - bar_width, start_y, bar_width, flist_height, COLOR_STD_BG);
}
u32 SdFormatMenu(void) {
const u32 cluster_size_table[5] = { 0x0, 0x0, 0x4000, 0x8000, 0x10000 };
const char* option_emunand_size[6] = { "No EmuNAND", "RedNAND size (min)", "EmuNAND size (full)", "User input..." };
const char* option_cluster_size[4] = { "Auto", "16KB Clusters", "32KB Clusters", "64KB Clusters" };
u64 sysnand_size_mb = (((u64)GetNandSizeSectors(NAND_SYSNAND) * 0x200) + 0xFFFFF) / 0x100000;
u64 sysnand_min_size_mb = (((u64)GetNandMinSizeSectors(NAND_SYSNAND) * 0x200) + 0xFFFFF) / 0x100000;
char label[16] = "0:GM9SD";
u32 cluster_size = 0;
u64 sdcard_size_mb = 0;
u64 emunand_size_mb = (u64) -1;
u32 user_select;
// check actual SD card size
sdcard_size_mb = GetSDCardSize() / 0x100000;
if (!sdcard_size_mb) {
ShowPrompt(false, "Error: SD card not detected.");
return 1;
}
user_select = ShowSelectPrompt(4, option_emunand_size, "Format SD card (%lluMB)?\nChoose EmuNAND size:", sdcard_size_mb);
if (user_select && (user_select < 4)) {
emunand_size_mb = (user_select == 2) ? sysnand_min_size_mb : (user_select == 3) ? sysnand_size_mb : 0;
} else if (user_select == 4) do {
emunand_size_mb = ShowNumberPrompt(sysnand_min_size_mb, "SD card size is %lluMB.\nEnter EmuNAND size (MB) below:", sdcard_size_mb);
if (emunand_size_mb == (u64) -1) break;
} while (emunand_size_mb > sdcard_size_mb);
if (emunand_size_mb == (u64) -1) return 1;
user_select = ShowSelectPrompt(4, option_cluster_size, "Format SD card (%lluMB)?\nChoose cluster size:", sdcard_size_mb);
if (!user_select) return 1;
else cluster_size = cluster_size_table[user_select];
if (!ShowStringPrompt(label + 2, 11 + 1, "Format SD card (%lluMB)?\nEnter label:", sdcard_size_mb))
return 1;
if (!FormatSDCard(emunand_size_mb, cluster_size, label)) {
ShowPrompt(false, "Format SD: failed!");
return 1;
}
if (emunand_size_mb >= sysnand_min_size_mb) {
const char* option_emunand_type[3] = { "RedNAND type", "GW EmuNAND type", "Don't set up" };
if (emunand_size_mb >= sysnand_size_mb)
user_select = ShowSelectPrompt(3, option_emunand_type, "Choose EmuNAND type to set up:");
else user_select = ShowPrompt(true, "Clone SysNAND to RedNAND now?") ? 1 : 0;
if (!user_select || (user_select > 2)) return 0;
u8 ncsd[0x200];
u32 flags = OVERRIDE_PERM;
InitSDCardFS(); // this has to be initialized for EmuNAND to work
SetEmuNandBase((user_select == 2) ? 0 : 1); // 0 -> GW EmuNAND
if ((ReadNandSectors(ncsd, 0, 1, 0xFF, NAND_SYSNAND) != 0) ||
(WriteNandSectors(ncsd, 0, 1, 0xFF, NAND_EMUNAND) != 0) ||
(!PathCopy("E:", "S:/nand_minsize.bin", &flags)))
ShowPrompt(false, "Cloning SysNAND to EmuNAND: failed!");
DeinitSDCardFS();
}
return 0;
}
u32 FileGraphicsViewer(const char* path) {
const u32 max_size = SCREEN_SIZE(ALT_SCREEN);
u64 filetype = IdentifyFileType(path);
u8* bitmap = (u8*) malloc(max_size);
u8* input = (u8*) malloc(max_size);
u32 w = 0;
u32 h = 0;
u32 ret = 1;
if (!bitmap || !input) {
if (bitmap) free(bitmap);
if (input) free(input);
return 1;
}
u32 input_size = FileGetData(path, input, max_size, 0);
if (input_size && (input_size < max_size)) {
if (filetype & GFX_PCX) {
if (PCX_Decompress(bitmap, max_size, input, input_size)) {
PCXHdr* hdr = (PCXHdr*) (void*) input;
w = PCX_Width(hdr);
h = PCX_Height(hdr);
ret = 0;
}
}
}
if ((ret == 0) && w && h && (w < SCREEN_WIDTH(ALT_SCREEN)) && (h < SCREEN_HEIGHT)) {
ClearScreenF(true, true, COLOR_STD_BG);
DrawBitmap(ALT_SCREEN, -1, -1, w, h, bitmap);
ShowString("Press <A> to continue");
while(!(InputWait(0) & (BUTTON_A | BUTTON_B)));
ClearScreenF(true, true, COLOR_STD_BG);
} else ret = 1;
free(bitmap);
free(input);
return ret;
}
u32 FileHexViewer(const char* path) {
const u32 max_data = (SCREEN_HEIGHT / FONT_HEIGHT_EXT) * 16 * ((FONT_WIDTH_EXT > 4) ? 1 : 2);
static u32 mode = 0;
u8* data = NULL;
u8* bottom_cpy = (u8*) malloc(SCREEN_SIZE_BOT); // a copy of the bottom screen framebuffer
u32 fsize = FileGetSize(path);
bool dual_screen = 0;
int x_off = 0, x_hex = 0, x_ascii = 0;
u32 vpad = 0, hlpad = 0, hrpad = 0;
u32 rows = 0, cols = 0;
u32 total_shown = 0;
u32 total_data = 0;
u32 last_mode = 0xFF;
u32 last_offset = (u32) -1;
u32 offset = 0;
u8 found_data[64 + 1] = { 0 };
u32 found_offset = (u32) -1;
u32 found_size = 0;
static const u32 edit_bsize = 0x4000; // should be multiple of 0x200 * 2
bool edit_mode = false;
u8* buffer = (u8*) malloc(edit_bsize);
u8* buffer_cpy = (u8*) malloc(edit_bsize);
u32 edit_start = 0;
int cursor = 0;
if (!bottom_cpy || !buffer || !buffer_cpy) {
if (bottom_cpy) free(bottom_cpy);
if (buffer) free(buffer);
if (buffer_cpy) free(buffer_cpy);
return 1;
}
static bool show_instr = true;
static const char* instr = "Hexeditor Controls:\n \n\x18\x19\x1A\x1B(+R) - Scroll\nR+Y - Switch view\nX - Search / goto...\nA - Enter edit mode\nA+\x18\x19\x1A\x1B - Edit value\nB - Exit\n";
if (show_instr) { // show one time instructions
ShowPrompt(false, instr);
show_instr = false;
}
if (MAIN_SCREEN != TOP_SCREEN) ShowString(instr);
memcpy(bottom_cpy, BOT_SCREEN, SCREEN_SIZE_BOT);
data = buffer;
while (true) {
if (mode != last_mode) {
if (FONT_WIDTH_EXT <= 5) {
mode = 0;
vpad = 1;
hlpad = hrpad = 2;
cols = 16;
x_off = (SCREEN_WIDTH_TOP - SCREEN_WIDTH_BOT) / 2;
x_ascii = SCREEN_WIDTH_TOP - x_off - (FONT_WIDTH_EXT * cols);
x_hex = ((SCREEN_WIDTH_TOP - ((hlpad + (2*FONT_WIDTH_EXT) + hrpad) * cols)) / 2) -
(((cols - 8) / 2) * FONT_WIDTH_EXT);
dual_screen = true;
} else if (FONT_WIDTH_EXT <= 6) {
if (mode == 1) {
vpad = 0;
hlpad = hrpad = 1;
cols = 16;
x_off = 0;
x_ascii = SCREEN_WIDTH_TOP - (FONT_WIDTH_EXT * cols);
x_hex = x_off + (8*FONT_WIDTH_EXT) + 16;
dual_screen = false;
} else {
mode = 0;
vpad = 0;
hlpad = hrpad = 3;
cols = 8;
x_off = 30 + (SCREEN_WIDTH_TOP - SCREEN_WIDTH_BOT) / 2;
x_ascii = SCREEN_WIDTH_TOP - x_off - (FONT_WIDTH_EXT * cols);
x_hex = (SCREEN_WIDTH_TOP - ((hlpad + (2*FONT_WIDTH_EXT) + hrpad) * cols)) / 2;
dual_screen = true;
}
} else switch (mode) { // display mode
case 1:
vpad = hlpad = hrpad = 1;
cols = 12;
x_off = 0;
x_ascii = SCREEN_WIDTH_TOP - (FONT_WIDTH_EXT * cols);
x_hex = ((SCREEN_WIDTH_TOP - ((hlpad + (2*FONT_WIDTH_EXT) + hrpad) * cols) -
((cols - 8) * FONT_WIDTH_EXT)) / 2);
dual_screen = false;
break;
case 2:
vpad = 1;
hlpad = 0;
hrpad = 1 + 8 - FONT_WIDTH_EXT;
cols = 16;
x_off = -1;
x_ascii = SCREEN_WIDTH_TOP - (FONT_WIDTH_EXT * cols);
x_hex = 0;
dual_screen = false;
break;
case 3:
vpad = hlpad = hrpad = 1;
cols = 16;
x_off = ((SCREEN_WIDTH_TOP - ((hlpad + (2*FONT_WIDTH_EXT) + hrpad) * cols)
- 12 - (8*FONT_WIDTH_EXT)) / 2);
x_ascii = -1;
x_hex = x_off + (8*FONT_WIDTH_EXT) + 12;
dual_screen = false;
break;
default:
mode = 0;
vpad = hlpad = hrpad = 2;
cols = 8;
x_off = (SCREEN_WIDTH_TOP - SCREEN_WIDTH_BOT) / 2;
x_ascii = SCREEN_WIDTH_TOP - x_off - (FONT_WIDTH_EXT * cols);
x_hex = (SCREEN_WIDTH_TOP - ((hlpad + (2*FONT_WIDTH_EXT) + hrpad) * cols)) / 2;
dual_screen = true;
break;
}
rows = (dual_screen ? 2 : 1) * SCREEN_HEIGHT / (FONT_HEIGHT_EXT + (2*vpad));
total_shown = rows * cols;
last_mode = mode;
ClearScreen(TOP_SCREEN, COLOR_STD_BG);
if (dual_screen) ClearScreen(BOT_SCREEN, COLOR_STD_BG);
else memcpy(BOT_SCREEN, bottom_cpy, SCREEN_SIZE_BOT);
}
// fix offset (if required)
if (offset % cols) offset -= (offset % cols); // fix offset (align to cols)
if (offset + total_shown - cols > fsize) // if offset too big
offset = (total_shown > fsize) ? 0 : (fsize + cols - total_shown - (fsize % cols));
// get data, using max data size (if new offset)
if (offset != last_offset) {
if (!edit_mode) {
total_data = FileGetData(path, data, max_data, offset);
} else { // edit mode - read from memory
if ((offset < edit_start) || (offset + max_data > edit_start + edit_bsize))
offset = last_offset; // we don't expect this to happen
total_data = (fsize - offset >= max_data) ? max_data : fsize - offset;
data = buffer + (offset - edit_start);
}
last_offset = offset;
}
// display data on screen
for (u32 row = 0; row < rows; row++) {
char ascii[16 + 1] = { 0 };
u32 y = row * (FONT_HEIGHT_EXT + (2*vpad)) + vpad;
u32 curr_pos = row * cols;
u32 cutoff = (curr_pos >= total_data) ? 0 : (total_data >= curr_pos + cols) ? cols : total_data - curr_pos;
u8* screen = TOP_SCREEN;
u32 x0 = 0;
// marked offsets handling
s32 marked0 = 0, marked1 = 0;
if ((found_size > 0) &&
(found_offset + found_size > offset + curr_pos) &&
(found_offset < offset + curr_pos + cols)) {
marked0 = (s32) found_offset - (offset + curr_pos);
marked1 = marked0 + found_size;
if (marked0 < 0) marked0 = 0;
if (marked1 > cols) marked1 = cols;
}
// switch to bottom screen
if (y >= SCREEN_HEIGHT) {
y -= SCREEN_HEIGHT;
screen = BOT_SCREEN;
x0 = 40;
}
memcpy(ascii, data + curr_pos, cols);
for (u32 col = 0; col < cols; col++)
if ((col >= cutoff) || (ascii[col] == 0x00)) ascii[col] = ' ';
// draw offset / ASCII representation
if (x_off >= 0) DrawStringF(screen, x_off - x0, y, cutoff ? COLOR_HVOFFS : COLOR_HVOFFSI,
COLOR_STD_BG, "%08X", (unsigned int) offset + curr_pos);
if (x_ascii >= 0) {
DrawString(screen, ascii, x_ascii - x0, y, COLOR_HVASCII, COLOR_STD_BG, false);
for (u32 i = marked0; i < marked1; i++)
DrawCharacter(screen, ascii[i % cols], x_ascii - x0 + (FONT_WIDTH_EXT * i), y, COLOR_MARKED, COLOR_STD_BG);
if (edit_mode && ((u32) cursor / cols == row)) DrawCharacter(screen, ascii[cursor % cols],
x_ascii - x0 + FONT_WIDTH_EXT * (cursor % cols), y, COLOR_RED, COLOR_STD_BG);
}
// draw HEX values
for (u32 col = 0; (col < cols) && (x_hex >= 0); col++) {
u32 x = (x_hex + hlpad) + (((2*FONT_WIDTH_EXT) + hrpad + hlpad) * col) - x0;
u32 hex_color = (edit_mode && ((u32) cursor == curr_pos + col)) ? COLOR_RED :
((col >= marked0) && (col < marked1)) ? COLOR_MARKED : COLOR_HVHEX(col);
if (col < cutoff)
DrawStringF(screen, x, y, hex_color, COLOR_STD_BG, "%02X", (unsigned int) data[curr_pos + col]);
else DrawStringF(screen, x, y, hex_color, COLOR_STD_BG, " ");
}
}
// handle user input
u32 pad_state = InputWait(0);
if (!edit_mode) { // standard viewer mode
u32 step_ud = (pad_state & BUTTON_R1) ? (0x1000 - (0x1000 % cols)) : cols;
u32 step_lr = (pad_state & BUTTON_R1) ? (0x10000 - (0x10000 % cols)) : total_shown;
if (pad_state & BUTTON_DOWN) offset += step_ud;
else if (pad_state & BUTTON_RIGHT) offset += step_lr;
else if (pad_state & BUTTON_UP) offset = (offset > step_ud) ? offset - step_ud : 0;
else if (pad_state & BUTTON_LEFT) offset = (offset > step_lr) ? offset - step_lr : 0;
else if ((pad_state & BUTTON_R1) && (pad_state & BUTTON_Y)) mode++;
else if ((pad_state & BUTTON_A) && total_data) edit_mode = true;
else if (pad_state & (BUTTON_B|BUTTON_START)) break;
else if (found_size && (pad_state & BUTTON_R1) && (pad_state & BUTTON_X)) {
found_offset = FileFindData(path, found_data, found_size, found_offset + 1);
if (found_offset == (u32) -1) {
ShowPrompt(false, "Not found!");
found_size = 0;
} else offset = found_offset;
if (MAIN_SCREEN == TOP_SCREEN) ClearScreen(TOP_SCREEN, COLOR_STD_BG);
else if (dual_screen) ClearScreen(BOT_SCREEN, COLOR_STD_BG);
else memcpy(BOT_SCREEN, bottom_cpy, SCREEN_SIZE_BOT);
} else if (pad_state & BUTTON_X) {
const char* optionstr[3] = { "Go to offset", "Search for string", "Search for data" };
u32 user_select = ShowSelectPrompt(3, optionstr, "Current offset: %08X\nSelect action:",
(unsigned int) offset);
if (user_select == 1) { // -> goto offset
u64 new_offset = ShowHexPrompt(offset, 8, "Current offset: %08X\nEnter new offset below.",
(unsigned int) offset);
if (new_offset != (u64) -1) offset = new_offset;
} else if (user_select == 2) {
if (!found_size) *found_data = 0;
if (ShowStringPrompt((char*) found_data, 64 + 1, "Enter search string below.\n(R+X to repeat search)", (unsigned int) offset)) {
found_size = strnlen((char*) found_data, 64);
found_offset = FileFindData(path, found_data, found_size, offset);
if (found_offset == (u32) -1) {
ShowPrompt(false, "Not found!");
found_size = 0;
} else offset = found_offset;
}
} else if (user_select == 3) {
u32 size = found_size;
if (ShowDataPrompt(found_data, &size, "Enter search data below.\n(R+X to repeat search)", (unsigned int) offset)) {
found_size = size;
found_offset = FileFindData(path, found_data, size, offset);
if (found_offset == (u32) -1) {
ShowPrompt(false, "Not found!");
found_size = 0;
} else offset = found_offset;
}
}
if (MAIN_SCREEN == TOP_SCREEN) ClearScreen(TOP_SCREEN, COLOR_STD_BG);
else if (dual_screen) ClearScreen(BOT_SCREEN, COLOR_STD_BG);
else memcpy(BOT_SCREEN, bottom_cpy, SCREEN_SIZE_BOT);
}
if (edit_mode && CheckWritePermissions(path)) { // setup edit mode
found_size = 0;
found_offset = (u32) -1;
cursor = 0;
edit_start = ((offset - (offset % 0x200) <= (edit_bsize / 2)) || (fsize < edit_bsize)) ? 0 :
offset - (offset % 0x200) - (edit_bsize / 2);
FileGetData(path, buffer, edit_bsize, edit_start);
memcpy(buffer_cpy, buffer, edit_bsize);
data = buffer + (offset - edit_start);
} else edit_mode = false;
} else { // editor mode
if (pad_state & (BUTTON_B|BUTTON_START)) {
edit_mode = false;
// check for user edits
u32 diffs = 0;
for (u32 i = 0; i < edit_bsize; i++) if (buffer[i] != buffer_cpy[i]) diffs++;
if (diffs && ShowPrompt(true, "You made edits in %i place(s).\nWrite changes to file?", diffs))
if (!FileSetData(path, buffer, min(edit_bsize, (fsize - edit_start)), edit_start, false))
ShowPrompt(false, "Failed writing to file!");
data = buffer;
last_offset = (u32) -1; // force reload from file
} else if (pad_state & BUTTON_A) {
if (pad_state & BUTTON_DOWN) data[cursor]--;
else if (pad_state & BUTTON_UP) data[cursor]++;
else if (pad_state & BUTTON_RIGHT) data[cursor] += 0x10;
else if (pad_state & BUTTON_LEFT) data[cursor] -= 0x10;
} else {
if (pad_state & BUTTON_DOWN) cursor += cols;
else if (pad_state & BUTTON_UP) cursor -= cols;
else if (pad_state & BUTTON_RIGHT) cursor++;
else if (pad_state & BUTTON_LEFT) cursor--;
// fix cursor position
if (cursor < 0) {
if (offset >= cols) {
offset -= cols;
cursor += cols;
} else cursor = 0;
} else if (((u32) cursor >= total_data) && (total_data < total_shown)) {
cursor = total_data - 1;
} else if ((u32) cursor >= total_shown) {
if (offset + total_shown == fsize) {
cursor = total_shown - 1;
} else {
offset += cols;
cursor = (offset + cursor >= fsize) ? fsize - offset - 1 : cursor - cols;
}
}
}
}
}
ClearScreen(TOP_SCREEN, COLOR_STD_BG);
if (MAIN_SCREEN == TOP_SCREEN) memcpy(BOT_SCREEN, bottom_cpy, SCREEN_SIZE_BOT);
else ClearScreen(BOT_SCREEN, COLOR_STD_BG);
free(bottom_cpy);
free(buffer);
free(buffer_cpy);
return 0;
}
u32 Sha256Calculator(const char* path) {
u32 drvtype = DriveType(path);
char pathstr[32 + 1];
u8 sha256[32];
TruncateString(pathstr, path, 32, 8);
if (!FileGetSha256(path, sha256, 0, 0)) {
ShowPrompt(false, "Calculating SHA-256: failed!");
return 1;
} else {
static char pathstr_prev[32 + 1] = { 0 };
static u8 sha256_prev[32] = { 0 };
char sha_path[256];
u8 sha256_file[32];
snprintf(sha_path, 256, "%s.sha", path);
bool have_sha = (FileGetData(sha_path, sha256_file, 32, 0) == 32);
bool match_sha = have_sha && (memcmp(sha256, sha256_file, 32) == 0);
bool match_prev = (memcmp(sha256, sha256_prev, 32) == 0);
bool write_sha = (!have_sha || !match_sha) && (drvtype & DRV_SDCARD); // writing only on SD
if (ShowPrompt(write_sha, "%s\n%016llX%016llX\n%016llX%016llX%s%s%s%s%s",
pathstr, getbe64(sha256 + 0), getbe64(sha256 + 8), getbe64(sha256 + 16), getbe64(sha256 + 24),
(have_sha) ? "\nSHA verification: " : "",
(have_sha) ? ((match_sha) ? "passed!" : "failed!") : "",
(match_prev) ? "\n \nIdentical with previous file:\n" : "",
(match_prev) ? pathstr_prev : "",
(write_sha) ? "\n \nWrite .SHA file?" : "") && write_sha) {
FileSetData(sha_path, sha256, 32, 0, true);
}
strncpy(pathstr_prev, pathstr, 32 + 1);
memcpy(sha256_prev, sha256, 32);
}
return 0;
}
u32 CmacCalculator(const char* path) {
char pathstr[32 + 1];
u8 cmac[16];
TruncateString(pathstr, path, 32, 8);
if (CalculateFileCmac(path, cmac) != 0) {
ShowPrompt(false, "Calculating CMAC: failed!");
return 1;
} else {
u8 cmac_file[16];
bool identical = ((ReadFileCmac(path, cmac_file) == 0) && (memcmp(cmac, cmac_file, 16) == 0));
if (ShowPrompt(!identical, "%s\n%016llX%016llX\n%s%s%s",
pathstr, getbe64(cmac + 0), getbe64(cmac + 8),
"CMAC verification: ", (identical) ? "passed!" : "failed!",
(!identical) ? "\n \nFix CMAC in file?" : "") &&
!identical && (WriteFileCmac(path, cmac) != 0)) {
ShowPrompt(false, "Fixing CMAC: failed!");
}
}
return 0;
}
u32 StandardCopy(u32* cursor, u32* scroll) {
DirEntry* curr_entry = &(current_dir->entry[*cursor]);
u32 n_marked = 0;
if (curr_entry->marked) {
for (u32 i = 0; i < current_dir->n_entries; i++)
if (current_dir->entry[i].marked) n_marked++;
}
u32 flags = BUILD_PATH;
if ((n_marked > 1) && ShowPrompt(true, "Copy all %lu selected items?", n_marked)) {
u32 n_success = 0;
for (u32 i = 0; i < current_dir->n_entries; i++) {
const char* path = current_dir->entry[i].path;
if (!current_dir->entry[i].marked)
continue;
flags |= ASK_ALL;
DrawDirContents(current_dir, (*cursor = i), scroll);
if (PathCopy(OUTPUT_PATH, path, &flags)) n_success++;
else { // on failure: show error, break
char currstr[32+1];
TruncateString(currstr, path, 32, 12);
ShowPrompt(false, "%s\nFailed copying item", currstr);
break;
}
current_dir->entry[i].marked = false;
}
if (n_success) ShowPrompt(false, "%lu items copied to %s", n_success, OUTPUT_PATH);
} else {
char pathstr[32+1];
TruncateString(pathstr, curr_entry->path, 32, 8);
if (!PathCopy(OUTPUT_PATH, curr_entry->path, &flags))
ShowPrompt(false, "%s\nFailed copying item", pathstr);
else ShowPrompt(false, "%s\nCopied to %s", pathstr, OUTPUT_PATH);
}
return 0;
}
u32 BootFirmHandler(const char* bootpath, bool verbose, bool delete) {
char pathstr[32+1];
TruncateString(pathstr, bootpath, 32, 8);
size_t firm_size = FileGetSize(bootpath);
if (!firm_size) return 1;
if (firm_size > FIRM_MAX_SIZE) {
if (verbose) ShowPrompt(false, "%s\nFIRM too big, can't boot", pathstr); // unlikely
return 1;
}
if (verbose && !ShowPrompt(true, "%s (%dkB)\nWarning: Do not boot FIRMs\nfrom untrusted sources.\n \nBoot FIRM?",
pathstr, firm_size / 1024))
return 1;
void* firm = (void*) malloc(FIRM_MAX_SIZE);
if (!firm) return 1;
if ((FileGetData(bootpath, firm, firm_size, 0) != firm_size) ||
!IsBootableFirm(firm, firm_size)) {
if (verbose) ShowPrompt(false, "%s\nNot a bootable FIRM.", pathstr);
free(firm);
return 1;
}
// encrypted firm handling
FirmSectionHeader* arm9s = FindFirmArm9Section(firm);
FirmA9LHeader* a9l = (FirmA9LHeader*)(void*) ((u8*) firm + arm9s->offset);
if (verbose && (ValidateFirmA9LHeader(a9l) == 0) &&
ShowPrompt(true, "%s\nFIRM is encrypted.\n \nDecrypt before boot?", pathstr) &&
(DecryptFirmFull(firm, firm_size) != 0)) {
free(firm);
return 1;
}
// unsupported location handling
char fixpath[256] = { 0 };
if (verbose && (*bootpath != '0') && (*bootpath != '1')) {
const char* optionstr[2] = { "Make a copy at " OUTPUT_PATH "/temp.firm", "Try to boot anyways" };
u32 user_select = ShowSelectPrompt(2, optionstr, "%s\nWarning: Trying to boot from an\nunsupported location.", pathstr);
if (user_select == 1) {
FileSetData(OUTPUT_PATH "/temp.firm", firm, firm_size, 0, true);
bootpath = OUTPUT_PATH "/temp.firm";
} else if (!user_select) bootpath = "";
}
// fix the boot path ("sdmc"/"nand" for Luma et al, hacky af)
if ((*bootpath == '0') || (*bootpath == '1'))
snprintf(fixpath, 256, "%s%s", (*bootpath == '0') ? "sdmc" : "nand", bootpath + 1);
else strncpy(fixpath, bootpath, 256);
// boot the FIRM (if we got a proper fixpath)
if (*fixpath) {
if (delete) PathDelete(bootpath);
BootFirm((FirmHeader*) firm, fixpath);
while(1);
}
// a return was not intended
free(firm);
return 1;
}
u32 FileAttrMenu(const char* file_path) {
FILINFO fno;
if (fvx_stat(file_path, &fno) != FR_OK) {
char pathstr[32 + 1];
TruncateString(pathstr, file_path, 32, 8);
ShowPrompt(false, "%s\nFile info failed!", pathstr);
return 1;
}
char namestr[32];
char sizestr[32];
TruncateString(namestr, fno.fname, 32, 8);
FormatNumber(sizestr, fno.fsize);
const bool vrt = (fno.fattrib & AM_VRT);
u8 new_attrib = fno.fattrib;
while (true) {
ShowString(
"%s\n"
" \n"
"filesize: %s byte\n"
"modified: %04lu-%02lu-%02lu %02lu:%02lu:%02lu\n"
" \n"
"[%c] %sread-only [%c] %shidden\n"
"[%c] %ssystem [%c] %sarchive\n"
"[%c] %svirtual\n"
" \n"
"%s"
"%s",
namestr, sizestr,
1980 + ((fno.fdate >> 9) & 0x7F), (fno.fdate >> 5) & 0x0F, (fno.fdate >> 0) & 0x1F,
(fno.ftime >> 11) & 0x1F, (fno.ftime >> 5) & 0x3F, ((fno.ftime >> 0) & 0x1F) << 1,
(new_attrib & AM_RDO) ? 'X' : ' ', (vrt ? "" : "\x18 "),
(new_attrib & AM_HID) ? 'X' : ' ', (vrt ? "" : "\x19 "),
(new_attrib & AM_SYS) ? 'X' : ' ', (vrt ? "" : "\x1A "),
(new_attrib & AM_ARC) ? 'X' : ' ', (vrt ? "" : "\x1B "),
vrt ? 'X' : ' ', (vrt ? "" : " "),
vrt ? "" : "(\x18\x19\x1A\x1B to change attributes)\n",
(vrt || (new_attrib == fno.fattrib)) ? "(<A> to continue)" : "(<A> to apply, <B> to cancel)");
while (true) {
u32 pad_state = InputWait(0);
if (pad_state & (BUTTON_A | BUTTON_B)) {
bool apply = !vrt && (new_attrib != fno.fattrib) && (pad_state & BUTTON_A);
const u8 mask = (AM_RDO | AM_HID | AM_SYS | AM_ARC);
if (apply && !PathAttr(file_path, new_attrib & mask, mask)) {
ShowPrompt(false, "%s\nFailed to set attributes!", namestr);
}
ClearScreenF(true, false, COLOR_STD_BG);
return 0;
} else if (vrt) continue;
switch (pad_state) {
case BUTTON_UP:
new_attrib ^= AM_RDO;
break;
case BUTTON_DOWN:
new_attrib ^= AM_HID;
break;
case BUTTON_RIGHT:
new_attrib ^= AM_SYS;
break;
case BUTTON_LEFT:
new_attrib ^= AM_ARC;
break;
default:
continue;
}
break;
}
}
}
u32 FileHandlerMenu(char* current_path, u32* cursor, u32* scroll, PaneData** pane) {
const char* file_path = (&(current_dir->entry[*cursor]))->path;
const char* optionstr[16];
// check for file lock
if (!FileUnlock(file_path)) return 1;
u64 filetype = IdentifyFileType(file_path);
u32 drvtype = DriveType(file_path);
bool in_output_path = (strncasecmp(current_path, OUTPUT_PATH, 256) == 0);
// don't handle TMDs inside the game drive, won't work properly anyways
if ((filetype & GAME_TMD) && (drvtype & DRV_GAME)) filetype &= ~GAME_TMD;