-
Notifications
You must be signed in to change notification settings - Fork 4
/
system.c
912 lines (729 loc) · 27.7 KB
/
system.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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <kernel.h>
#include <limits.h>
#include <libpad.h>
#include <libpwroff.h>
#include <fileXio_rpc.h>
#include <atahw.h>
#include <hdd-ioctl.h>
#include <usbhdfsd-common.h>
#include <sys/fcntl.h>
#include <libgs.h>
#include "main.h"
#include "iop.h"
#include "menu.h"
#include "UI.h"
#include "fsck/fsck-ioctl.h"
#include "hdsk/hdsk-devctl.h"
#include "fssk/fssk-ioctl.h"
#include "hdst.h"
#include "system.h"
extern void *_gp;
extern unsigned short int SelectButton, CancelButton;
extern u8 dev9Loaded;
extern int InstallLockSema;
int GetBootDeviceID(void)
{
static int BootDevice = -2;
char path[256];
int result;
if (BootDevice < BOOT_DEVICE_UNKNOWN) {
getcwd(path, sizeof(path));
if (!strncmp(path, "hdd:", 4) || !strncmp(path, "hdd0:", 5))
result = BOOT_DEVICE_HDD;
#ifndef FSCK
else if (!strncmp(path, "mc0:", 4))
result = BOOT_DEVICE_MC0;
else if (!strncmp(path, "mc1:", 4))
result = BOOT_DEVICE_MC1;
else if (!strncmp(path, "mass:", 5) || !strncmp(path, "mass0:", 6))
result = BOOT_DEVICE_MASS;
#endif
else
result = BOOT_DEVICE_UNKNOWN;
BootDevice = result;
} else
result = BootDevice;
return result;
}
static int SysInitMount(void)
{
static char BlockDevice[38] = "";
char command[256];
const char *MountPath;
int BlockDeviceNameLen, result;
if (BlockDevice[0] == '\0') {
// Format: hdd0:partition:pfs:/path_to_file_on_partition
// However, getcwd will return the path, without the filename (as parsed by libc's init.c).
getcwd(command, sizeof(command));
if (strlen(command) > 6 && (MountPath = strchr(&command[5], ':')) != NULL) {
BlockDeviceNameLen = (unsigned int)MountPath - (unsigned int)command;
strncpy(BlockDevice, command, BlockDeviceNameLen);
BlockDevice[BlockDeviceNameLen] = '\0';
MountPath++; // This is the location of the mount path;
if ((result = fileXioMount("pfs0:", BlockDevice, FIO_MT_RDONLY)) >= 0)
result = chdir(MountPath);
return result;
} else
result = -EINVAL;
} else {
if ((result = fileXioMount("pfs0:", BlockDevice, FIO_MT_RDONLY)) >= 0)
result = 0;
}
return result;
}
int SysBootDeviceInit(void)
{
int result;
switch (GetBootDeviceID()) {
case BOOT_DEVICE_HDD: // This will only work if the modules are loaded (i.e. at boot).
result = SysInitMount();
break;
default:
result = 0;
}
return result;
}
int GetConsoleRegion(void)
{
static int region = -1;
FILE *file;
if (region < 0) {
if ((file = fopen("rom0:ROMVER", "r")) != NULL) {
fseek(file, 4, SEEK_SET);
switch (fgetc(file)) {
case 'J':
region = CONSOLE_REGION_JAPAN;
break;
case 'A':
case 'H':
region = CONSOLE_REGION_USA;
break;
case 'E':
region = CONSOLE_REGION_EUROPE;
break;
case 'C':
region = CONSOLE_REGION_CHINA;
break;
}
fclose(file);
}
}
return region;
}
int GetConsoleVMode(void)
{
switch (GetConsoleRegion()) {
case CONSOLE_REGION_EUROPE:
return 1;
default:
return 0;
}
}
int IsPSX(void)
{
static int psx = -1;
FILE *file;
int result;
if ((result = psx) < 0) {
if ((file = fopen("rom0:PSXVER", "r")) != NULL) {
psx = 1;
fclose(file);
} else
psx = 0;
result = psx;
}
return result;
}
int SysCreateThread(void *function, void *stack, unsigned int StackSize, void *arg, int priority)
{
ee_thread_t ThreadData;
int ThreadID;
ThreadData.func = function;
ThreadData.stack = stack;
ThreadData.stack_size = StackSize;
ThreadData.gp_reg = &_gp;
ThreadData.initial_priority = priority;
ThreadData.attr = ThreadData.option = 0;
if ((ThreadID = CreateThread(&ThreadData)) >= 0) {
if (StartThread(ThreadID, arg) < 0) {
DeleteThread(ThreadID);
ThreadID = -1;
}
}
return ThreadID;
}
#ifndef FSCK
static unsigned char UserAborted;
#endif
static unsigned int ErrorsFound;
static unsigned int ErrorsFixed;
#ifdef FSCK
static int FsckPartition(const char *partition)
{
char cmd[64];
struct fsckStatus status;
int fd, result, InitSemaID;
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
InitSemaID = IopInitStart(IOP_MODSET_SA_FSCK);
result = 0;
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
sprintf(cmd, "fsck:%s", partition);
if ((fd = fileXioOpen(cmd, 0, FSCK_MODE_VERBOSITY(0) | FSCK_MODE_AUTO | FSCK_MODE_WRITE)) >= 0) {
if ((result = fileXioIoctl2(fd, FSCK_IOCTL2_CMD_START, NULL, 0, NULL, 0)) == 0) {
result = fileXioIoctl2(fd, FSCK_IOCTL2_CMD_WAIT, NULL, 0, NULL, 0);
}
if (result == 0 && (result = fileXioIoctl2(fd, FSCK_IOCTL2_CMD_GET_STATUS, NULL, 0, &status, sizeof(status))) == 0)
result = 0;
if (result == 0) {
ErrorsFound += status.errorCount;
ErrorsFixed += status.fixedErrorCount;
}
fileXioClose(fd);
} else
result = fd;
return result;
}
int ScanDisk(int unit)
{
char ErrorPartName[64] = "hdd0:";
int result, InitSemaID;
WaitSema(InstallLockSema);
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
if (fileXioDevctl("hdd0:", HDIOC_GETSECTORERROR, NULL, 0, NULL, 0) == 0) {
if (fileXioDevctl("hdd0:", HDIOC_GETERRORPARTNAME, NULL, 0, &ErrorPartName[5], sizeof(ErrorPartName) - 5) != 0) {
ErrorsFound = 0;
ErrorsFixed = 0;
if ((result = FsckPartition(ErrorPartName)) == 0)
DisplayScanCompleteResults(ErrorsFound, ErrorsFixed);
else
DisplayErrorMessage(SYS_UI_MSG_HDD_FAULT);
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
InitSemaID = IopInitStart(IOP_MODSET_SA_MAIN);
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
} else
DisplayScanCompleteResults(0, 0); // No fault. Why are we here then?
} else
DisplayErrorMessage(SYS_UI_MSG_HDD_FAULT);
SignalSema(InstallLockSema);
return result;
}
#else
static int HdckDisk(int unit)
{
char device[] = "hdck0:";
int InitSemaID, result;
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
InitSemaID = IopInitStart(IOP_MODSET_HDCK);
device[4] = '0' + unit;
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
SysBootDeviceInit();
ReinitializeUI();
#ifdef LOG_MESSAGES
IopStartLog("hdck.log");
printf("# Log generated by HDDChecker v" HDDC_VERSION ", built on "__DATE__
" "__TIME__
"\n");
#endif
result = fileXioDevctl(device, 0, NULL, 0, NULL, 0);
#ifdef LOG_MESSAGES
IopStopLog();
#endif
return result;
}
static int FsckDisk(int unit)
{
char bdevice[] = "hdd0:", cmd[64];
iox_dirent_t dirent;
struct fsckStatus status;
unsigned int PadStatus, CurrentCPUTicks, PreviousCPUTicks, seconds, TimeElasped, rate, partitions, i;
int PercentageComplete;
int bfd, fd, result, InitSemaID;
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
InitSemaID = IopInitStart(IOP_MODSET_FSCK);
bdevice[3] = '0' + unit;
result = 0;
InitProgressScreen(SYS_UI_LBL_SCANNING_DISK);
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
SysBootDeviceInit();
ReinitializeUI();
// Count the number of partitions on the disk.
partitions = 0;
if ((bfd = fileXioDopen(bdevice)) >= 0) {
while (fileXioDread(bfd, &dirent) > 0)
partitions++;
fileXioDclose(bfd);
}
if (partitions < 1) // No partitions?
return 0;
#ifdef LOG_MESSAGES
IopStartLog("fsck.log");
printf("# Log generated by HDDChecker v" HDDC_VERSION ", built on "__DATE__
" "__TIME__
"\n");
#endif
// Now, scan the disk.
if ((bfd = fileXioDopen(bdevice)) >= 0) {
for (i = 0; fileXioDread(bfd, &dirent) > 0; i++) {
if (!(dirent.stat.attr & APA_FLAG_SUB) && dirent.stat.mode == APA_TYPE_PFS) {
printf("# fsck hdd%d:%s\n", unit, dirent.name);
sprintf(cmd, "fsck:hdd%d:%s", unit, dirent.name);
if ((fd = fileXioOpen(cmd, 0, FSCK_MODE_VERBOSITY(FSCK_VERBOSITY) | FSCK_MODE_AUTO | FSCK_MODE_WRITE)) >= 0) {
if ((result = fileXioIoctl2(fd, FSCK_IOCTL2_CMD_START, NULL, 0, NULL, 0)) == 0) {
result = 0;
TimeElasped = 0;
PreviousCPUTicks = cpu_ticks();
while (fileXioIoctl2(fd, FSCK_IOCTL2_CMD_POLL, NULL, 0, NULL, 0) == 1) {
CurrentCPUTicks = cpu_ticks();
if ((seconds = (CurrentCPUTicks > PreviousCPUTicks ? CurrentCPUTicks - PreviousCPUTicks : UINT_MAX - PreviousCPUTicks + CurrentCPUTicks) / 295000000) > 0) {
TimeElasped += seconds;
PreviousCPUTicks = CurrentCPUTicks;
}
PercentageComplete = (int)((u64)i * 100 / partitions);
rate = (TimeElasped > 0) ? i / TimeElasped : 0; // In partitions/second
DrawDiskScanningScreen(PercentageComplete, rate > 0 ? (partitions - i) / rate : UINT_MAX);
PadStatus = ReadCombinedPadStatus();
if (PadStatus & CancelButton) {
if (DisplayPromptMessage(SYS_UI_MSG_SCAN_DISK_ABORT_CFM, SYS_UI_LBL_NO, SYS_UI_LBL_YES) == 2) {
fileXioIoctl2(fd, FSCK_IOCTL2_CMD_STOP, NULL, 0, NULL, 0);
result = 0;
UserAborted = 1;
break;
}
}
}
}
if (result != 0 || (result = fileXioIoctl2(fd, FSCK_IOCTL2_CMD_GET_STATUS, NULL, 0, &status, sizeof(status))) != 0 || UserAborted) {
fileXioClose(fd);
break;
}
ErrorsFound += status.errorCount;
ErrorsFixed += status.fixedErrorCount;
fileXioClose(fd);
} else { // Ignore partitions that can't be opened, so that the remainder of the disk can be checked..
result = fd;
ErrorsFound++;
}
putchar('\n');
}
}
fileXioDclose(bfd);
} else
result = bfd;
#ifdef LOG_MESSAGES
IopStopLog();
#endif
return result;
}
int ScanDisk(int unit)
{
int result, InitSemaID;
WaitSema(InstallLockSema);
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
ErrorsFound = 0;
ErrorsFixed = 0;
if ((result = HdckDisk(unit)) == 0 && !UserAborted)
result = FsckDisk(unit);
if (result == 0) {
if (!UserAborted)
DisplayScanCompleteResults(ErrorsFound, ErrorsFixed);
} else
DisplayErrorMessage(SYS_UI_MSG_HDD_FAULT);
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
InitSemaID = IopInitStart(IOP_MODSET_MAIN);
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
SysBootDeviceInit();
ReinitializeUI();
SignalSema(InstallLockSema);
return result;
}
static int HdskDisk(int unit)
{
char bdevice[] = "hdsk0:";
struct hdskStat status;
unsigned int PadStatus, CurrentCPUTicks, PreviousCPUTicks, seconds, TimeElasped, rate;
int PercentageComplete;
int result, InitSemaID;
u32 progress;
InitSemaID = IopInitStart(IOP_MODSET_HDSK);
bdevice[4] = '0' + unit;
InitProgressScreen(SYS_UI_LBL_OPTIMIZING_DISK_P2);
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
SysBootDeviceInit();
ReinitializeUI();
#ifdef LOG_MESSAGES
IopStartLog("hdsk.log");
printf("# Log generated by HDDChecker v" HDDC_VERSION ", built on "__DATE__
" "__TIME__
"\n");
#endif
// Now, scan the disk.
if ((result = fileXioDevctl(bdevice, HDSK_DEVCTL_GET_HDD_STAT, NULL, 0, &status, sizeof(struct hdskStat))) >= 0) {
printf("# hdsk: total: %x, free: %x\n", status.total, status.free);
if ((status.total > 0) && (result = fileXioDevctl(bdevice, HDSK_DEVCTL_START, NULL, 0, NULL, 0)) == 0) {
result = 0;
TimeElasped = 0;
PreviousCPUTicks = cpu_ticks();
while (fileXioDevctl(bdevice, HDSK_DEVCTL_POLL, NULL, 0, NULL, 0) == 1) {
progress = (u32)fileXioDevctl(bdevice, HDSK_DEVCTL_GET_PROGRESS, NULL, 0, NULL, 0);
PercentageComplete = (int)((u64)progress * 100 / status.total);
CurrentCPUTicks = cpu_ticks();
if ((seconds = (CurrentCPUTicks > PreviousCPUTicks ? CurrentCPUTicks - PreviousCPUTicks : UINT_MAX - PreviousCPUTicks + CurrentCPUTicks) / 295000000) > 0) {
TimeElasped += seconds;
PreviousCPUTicks = CurrentCPUTicks;
}
rate = (TimeElasped > 0) ? progress / TimeElasped : 0; // In sectors/second
DrawDiskOptimizationScreen(PercentageComplete, 50 + PercentageComplete / 2, rate > 0 ? (status.total - progress) / rate : UINT_MAX);
PadStatus = ReadCombinedPadStatus();
if (PadStatus & CancelButton) {
if (DisplayPromptMessage(SYS_UI_MSG_OPT_DISK_ABORT_CFM, SYS_UI_LBL_NO, SYS_UI_LBL_YES) == 2) {
fileXioDevctl(bdevice, HDSK_DEVCTL_STOP, NULL, 0, NULL, 0);
result = 0;
UserAborted = 1;
break;
}
}
if (result != 0 || (result = fileXioDevctl(bdevice, HDSK_DEVCTL_GET_STATUS, NULL, 0, NULL, 0)) != 0 || UserAborted)
break;
}
}
}
#ifdef LOG_MESSAGES
IopStopLog();
#endif
return result;
}
static int FsskDisk(int unit)
{
char bdevice[] = "hdd0:", cmd[64];
iox_dirent_t dirent;
struct fsskStatus status;
unsigned int PadStatus, CurrentCPUTicks, PreviousCPUTicks, seconds, TimeElasped, rate, partitions, i;
int PercentageComplete;
int bfd, fd, result, InitSemaID;
InitSemaID = IopInitStart(IOP_MODSET_FSSK);
bdevice[3] = '0' + unit;
partitions = 0;
result = 0;
InitProgressScreen(SYS_UI_LBL_OPTIMIZING_DISK_P1);
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
SysBootDeviceInit();
ReinitializeUI();
// Count the number of partitions on the disk.
if ((bfd = fileXioDopen(bdevice)) >= 0) {
while (fileXioDread(bfd, &dirent) > 0)
partitions++;
fileXioDclose(bfd);
}
if (partitions < 1) // No partitions?
return 0;
#ifdef LOG_MESSAGES
IopStartLog("fssk.log");
printf("# Log generated by HDDChecker v" HDDC_VERSION ", built on "__DATE__
" "__TIME__
"\n");
#endif
// Now, scan the disk.
if ((bfd = fileXioDopen(bdevice)) >= 0) {
for (i = 0; fileXioDread(bfd, &dirent) > 0; i++) {
if (!(dirent.stat.attr & APA_FLAG_SUB) && dirent.stat.mode == APA_TYPE_PFS) {
printf("# fssk hdd%d:%s\n", unit, dirent.name);
sprintf(cmd, "fssk:hdd%d:%s", unit, dirent.name);
if ((fd = fileXioOpen(cmd, 0, FSSK_MODE_VERBOSITY(FSSK_VERBOSITY))) >= 0) {
if ((result = fileXioIoctl2(fd, FSSK_IOCTL2_CMD_START, NULL, 0, NULL, 0)) == 0) {
result = 0;
TimeElasped = 0;
PreviousCPUTicks = cpu_ticks();
while (fileXioIoctl2(fd, FSSK_IOCTL2_CMD_POLL, NULL, 0, NULL, 0) == 1) {
CurrentCPUTicks = cpu_ticks();
if ((seconds = (CurrentCPUTicks > PreviousCPUTicks ? CurrentCPUTicks - PreviousCPUTicks : UINT_MAX - PreviousCPUTicks + CurrentCPUTicks) / 295000000) > 0) {
TimeElasped += seconds;
PreviousCPUTicks = CurrentCPUTicks;
}
PercentageComplete = (int)((u64)i * 100 / partitions);
rate = (TimeElasped > 0) ? i / TimeElasped : 0; // In partitions/second
DrawDiskOptimizationScreen(PercentageComplete, PercentageComplete / 2, rate > 0 ? (partitions - i) / rate : UINT_MAX);
PadStatus = ReadCombinedPadStatus();
if (PadStatus & CancelButton) {
if (DisplayPromptMessage(SYS_UI_MSG_OPT_DISK_ABORT_CFM, SYS_UI_LBL_NO, SYS_UI_LBL_YES) == 2) {
fileXioIoctl2(fd, FSSK_IOCTL2_CMD_STOP, NULL, 0, NULL, 0);
result = 0;
UserAborted = 1;
break;
}
}
}
}
if (result != 0 || (result = fileXioIoctl2(fd, FSSK_IOCTL2_CMD_GET_STATUS, NULL, 0, &status, sizeof(status))) != 0 || (result = status.hasError) != 0 || UserAborted) {
fileXioClose(fd);
break;
}
fileXioClose(fd);
} else
result = fd;
putchar('\n');
}
}
fileXioDclose(bfd);
} else
result = bfd;
#ifdef LOG_MESSAGES
IopStopLog();
#endif
return result;
}
int OptimizeDisk(int unit)
{
int result, InitSemaID;
WaitSema(InstallLockSema);
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
if ((result = FsskDisk(unit)) == 0 && !UserAborted)
result = HdskDisk(unit);
if (result == 0) {
if (!UserAborted)
DisplayInfoMessage(SYS_UI_MSG_OPT_DISK_COMPLETED_OK);
} else
DisplayErrorMessage(SYS_UI_MSG_HDD_FAULT);
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
InitSemaID = IopInitStart(IOP_MODSET_MAIN);
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
SysBootDeviceInit();
ReinitializeUI();
SignalSema(InstallLockSema);
return result;
}
int SurfScanDisk(int unit)
{
u32 lba, SectorsRemaining, TotalSectors;
u32 NumSectors, NumBadSectors, PadStatus, CurrentCPUTicks, PreviousCPUTicks, seconds, TimeElasped, rate;
char DeviceName[8];
int result, BadSectorHandlingMode, IsRetryCycle, InitSemaID;
HdstSectorIOParams_t SectorIOParams;
int PercentageComplete;
WaitSema(InstallLockSema);
InitProgressScreen(SYS_UI_LBL_SURF_SCANNING_DISK);
sprintf(DeviceName, "hdst%u:", unit);
result = 0;
TotalSectors = GetATADeviceCapacity(unit);
TimeElasped = 0;
PreviousCPUTicks = cpu_ticks();
NumBadSectors = 0;
IsRetryCycle = 0;
BadSectorHandlingMode = BAD_SECTOR_HANDLING_MODE_PROMPT;
for (lba = 0, SectorsRemaining = TotalSectors; SectorsRemaining > 0;) {
CurrentCPUTicks = cpu_ticks();
if ((seconds = (CurrentCPUTicks > PreviousCPUTicks ? CurrentCPUTicks - PreviousCPUTicks : UINT_MAX - PreviousCPUTicks + CurrentCPUTicks) / 295000000) > 0) {
TimeElasped += seconds;
PreviousCPUTicks = CurrentCPUTicks;
}
PercentageComplete = (int)((u64)lba * 100 / TotalSectors);
rate = (TimeElasped > 0) ? (TotalSectors - SectorsRemaining) / TimeElasped : 0; // In sectors/second
DrawDiskSurfScanningScreen(PercentageComplete, (rate > 0 ? SectorsRemaining / rate : UINT_MAX), NumBadSectors);
PadStatus = ReadCombinedPadStatus();
if (PadStatus & CancelButton) {
if (DisplayPromptMessage(SYS_UI_MSG_SCAN_DISK_ABORT_CFM, SYS_UI_LBL_NO, SYS_UI_LBL_YES) == 2) {
result = 1;
break;
}
}
NumSectors = SectorsRemaining > 65536 ? 65536 : (u32)SectorsRemaining;
SectorIOParams.lba = lba;
SectorIOParams.sectors = NumSectors;
if ((result = fileXioDevctl(DeviceName, HDST_DEVCTL_DEVICE_VERIFY_SECTORS, &SectorIOParams, sizeof(SectorIOParams), NULL, 0)) != 0) {
if (!IsRetryCycle)
NumBadSectors++;
if (result > 0) {
result--;
lba += result;
SectorsRemaining -= result;
if (BadSectorHandlingMode == BAD_SECTOR_HANDLING_MODE_PROMPT)
BadSectorHandlingMode = GetBadSectorAction(lba);
switch (BadSectorHandlingMode) {
case BAD_SECTOR_HANDLING_MODE_REMAP:
BadSectorHandlingMode = BAD_SECTOR_HANDLING_MODE_PROMPT;
case BAD_SECTOR_HANDLING_MODE_REMAP_ALL:
fileXioSetBlockMode(FXIO_WAIT);
if ((result = PatchSector(DeviceName, lba)) != 0) {
if (DisplayPromptMessage(SYS_UI_MSG_SECTOR_PATCH_FAIL, SYS_UI_LBL_OK, SYS_UI_LBL_ABORT) == 2)
goto SurfaceScan_end;
lba++;
SectorsRemaining--;
result = 0;
} else
IsRetryCycle = 1;
fileXioSetBlockMode(FXIO_NOWAIT);
break;
case BAD_SECTOR_HANDLING_MODE_SKIP:
BadSectorHandlingMode = BAD_SECTOR_HANDLING_MODE_PROMPT;
case BAD_SECTOR_HANDLING_MODE_SKIP_ALL:
lba++;
SectorsRemaining--;
result = 0;
break;
}
PreviousCPUTicks = cpu_ticks(); // Don't include the time spent on patching the disk.
} else {
DisplayErrorMessage(SYS_UI_MSG_UNEXPECTED_ATA_ERR);
break;
}
} else {
lba += NumSectors;
SectorsRemaining -= NumSectors;
IsRetryCycle = 0;
}
}
if (result == 0)
DisplayInfoMessage(SYS_UI_MSG_SURF_SCAN_DISK_COMPLETED_OK);
SurfaceScan_end:
// Reboot IOP to load the filesystem modules again (they'll assess the condition of the disk's format).
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
InitSemaID = IopInitStart(IOP_MODSET_MAIN);
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
SysBootDeviceInit();
ReinitializeUI();
SignalSema(InstallLockSema);
return result;
}
int ZeroFillDisk(int unit)
{
u32 lba, SectorsRemaining, TotalSectors;
u32 NumSectors, PadStatus, CurrentCPUTicks, PreviousCPUTicks, seconds, TimeElasped, rate;
char DeviceName[8];
int result, InitSemaID;
HdstSectorIOParams_t SectorIOParams;
int PercentageComplete;
WaitSema(InstallLockSema);
InitProgressScreen(SYS_UI_LBL_ZERO_FILLING_DISK);
sprintf(DeviceName, "hdst%u:", unit);
result = 0;
TotalSectors = GetATADeviceCapacity(unit);
TimeElasped = 0;
PreviousCPUTicks = cpu_ticks();
for (lba = 0, SectorsRemaining = TotalSectors; SectorsRemaining > 0;) {
CurrentCPUTicks = cpu_ticks();
if ((seconds = (CurrentCPUTicks > PreviousCPUTicks ? CurrentCPUTicks - PreviousCPUTicks : UINT_MAX - PreviousCPUTicks + CurrentCPUTicks) / 295000000) > 0) {
TimeElasped += seconds;
PreviousCPUTicks = CurrentCPUTicks;
}
PercentageComplete = (int)((u64)lba * 100 / TotalSectors);
rate = (TimeElasped > 0) ? (TotalSectors - SectorsRemaining) / TimeElasped : 0; // In sectors/second
DrawDiskZeroFillingScreen(PercentageComplete, (rate > 0 ? SectorsRemaining / rate : UINT_MAX));
PadStatus = ReadCombinedPadStatus();
if (PadStatus & CancelButton) {
if (DisplayPromptMessage(SYS_UI_MSG_ZERO_FILL_DISK_ABORT_CFM, SYS_UI_LBL_NO, SYS_UI_LBL_YES) == 2) {
result = 1;
break;
}
}
NumSectors = SectorsRemaining > 65536 ? 65536 : (u32)SectorsRemaining;
SectorIOParams.lba = lba;
SectorIOParams.sectors = NumSectors;
if ((result = fileXioDevctl(DeviceName, HDST_DEVCTL_DEVICE_ERASE_SECTORS, &SectorIOParams, sizeof(SectorIOParams), NULL, 0)) != 0) {
DisplayErrorMessage(SYS_UI_MSG_HDD_FAULT);
break;
} else {
lba += NumSectors;
SectorsRemaining -= NumSectors;
}
}
if (result == 0)
DisplayInfoMessage(SYS_UI_MSG_ZERO_FILL_DISK_COMPLETED_OK);
// Reboot IOP to load the filesystem modules again (they'll assess the condition of the disk's format).
DisplayFlashStatusUpdate(SYS_UI_MSG_PLEASE_WAIT);
InitSemaID = IopInitStart(IOP_MODSET_MAIN);
WaitSema(InitSemaID);
DeleteSema(InitSemaID);
SysBootDeviceInit();
ReinitializeUI();
SignalSema(InstallLockSema);
return result;
}
#endif
int HDDCheckSMARTStatus(void)
{
return (fileXioDevctl("hdd0:", HDIOC_SMARTSTAT, NULL, 0, NULL, 0) != 0);
}
int HDDCheckSectorErrorStatus(void)
{
return (fileXioDevctl("hdd0:", HDIOC_GETSECTORERROR, NULL, 0, NULL, 0) != 0);
}
int HDDCheckPartErrorStatus(void)
{
return (fileXioDevctl("hdd0:", HDIOC_GETERRORPARTNAME, NULL, 0, NULL, 0) != 0);
}
int HDDCheckStatus(void)
{
int status;
status = fileXioDevctl("hdd0:", HDIOC_STATUS, NULL, 0, NULL, 0);
if (status == 0)
fileXioRemove("hdd0:_tmp"); // Remove _tmp, if it exists.
return status;
}
#ifndef FSCK
#ifdef LOG_MESSAGES
static int IsLoggingEnabled(void)
{
switch (GetBootDeviceID()) {
case BOOT_DEVICE_HDD:
return 0; // Logging to the HDD (while scanning it) is not supported.
default:
return 1;
}
}
static void WaitLogStart(s32 alarm_id, u16 time, void *common)
{
iWakeupThread((int)common);
}
void IopStartLog(const char *log)
{
char blockdev[256];
int len;
if (!IsLoggingEnabled())
return;
getcwd(blockdev, sizeof(blockdev));
len = strlen(blockdev);
if ((len > 1) && blockdev[len - 1] != '/') {
blockdev[len] = '/';
len++;
}
strcpy(&blockdev[len], log);
while (fileXioMount("tty0:", blockdev, O_WRONLY | O_TRUNC | O_CREAT) == -ENODEV) {
SetAlarm(16 * 500, &WaitLogStart, (void *)GetThreadId());
SleepThread();
}
}
void IopStopLog(void)
{
if (!IsLoggingEnabled())
return;
fileXioUmount("tty0:");
}
#endif
#endif
void poweroffCallback(void *arg)
{ // Power button was pressed. If no installation is in progress, begin shutdown of the PS2.
if (PollSema(InstallLockSema) == InstallLockSema) {
// If dev9.irx was loaded successfully, shut down DEV9.
if (dev9Loaded) {
fileXioDevctl("pfs:", PDIOC_CLOSEALL, NULL, 0, NULL, 0);
while (fileXioDevctl("dev9x:", DDIOC_OFF, NULL, 0, NULL, 0) < 0) {};
}
#ifndef FSCK
// As required by some (typically 2.5") HDDs, issue the SCSI STOP UNIT command to avoid causing an emergency park.
fileXioDevctl("mass:", USBMASS_DEVCTL_STOP_ALL, NULL, 0, NULL, 0);
#endif
/* Power-off the PlayStation 2 console. */
poweroffShutdown();
}
}