-
Notifications
You must be signed in to change notification settings - Fork 3
/
ps3ntfs.c
executable file
·174 lines (137 loc) · 3.02 KB
/
ps3ntfs.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
#include <stddef.h>
#include <stdbool.h>
#include <inttypes.h>
#include <sys/synchronization.h>
#include <sys/types.h>
#include "vsh_exports.h"
#include "ntfs.h"
#include "ps3ntfs.h"
sys_lwmutex_t mutex;
bool show_msgs = false;
ntfs_md* mounts = NULL;
int num_mounts = 0;
extern bool prx_running;
ntfs_md* ps3ntfs_prx_mounts(void)
{
return mounts;
}
int ps3ntfs_prx_num_mounts(void)
{
return num_mounts;
}
void ps3ntfs_prx_lock(void)
{
sys_lwmutex_lock(&mutex, 0);
}
void ps3ntfs_prx_unlock(void)
{
sys_lwmutex_unlock(&mutex);
}
void ps3ntfs_automount(uint64_t ptr)
{
sys_lwmutex_attribute_t attr = {
SYS_SYNC_FIFO,
SYS_SYNC_NOT_RECURSIVE,
""
};
sys_lwmutex_create(&mutex, &attr);
bool is_mounted[8];
memset(is_mounted, false, sizeof(is_mounted));
const DISC_INTERFACE* ntfs_usb_if[8] = {
&__io_ntfs_usb000,
&__io_ntfs_usb001,
&__io_ntfs_usb002,
&__io_ntfs_usb003,
&__io_ntfs_usb004,
&__io_ntfs_usb005,
&__io_ntfs_usb006,
&__io_ntfs_usb007
};
while(prx_running)
{
// Iterate ports and mount NTFS.
int i;
for(i = 0; i < 8; i++)
{
if(PS3_NTFS_IsInserted(i))
{
if(!is_mounted[i])
{
sec_t* partitions = NULL;
int num_partitions = ntfsFindPartitions(ntfs_usb_if[i], &partitions);
if(num_partitions > 0 && partitions)
{
ps3ntfs_prx_lock();
int j;
for(j = 0; j < num_partitions; j++)
{
char name[32];
sprintf(name, "ntfs%d-usb%03d", j, i);
if(ntfsMount(name, ntfs_usb_if[i], partitions[j], CACHE_DEFAULT_PAGE_COUNT, CACHE_DEFAULT_PAGE_SIZE, NTFS_FORCE))
{
// add to mount struct
mounts = (ntfs_md*) realloc(mounts, ++num_mounts * sizeof(ntfs_md));
ntfs_md* mount = &mounts[num_mounts - 1];
strcpy(mount->name, name);
mount->interface = ntfs_usb_if[i];
mount->startSector = partitions[j];
}
}
ps3ntfs_prx_unlock();
free(partitions);
}
if(show_msgs)
{
char msg[256];
sprintf(msg, "Mounted /dev_usb%03d", i);
vshtask_notify(msg);
}
is_mounted[i] = true;
}
}
else
{
if(is_mounted[i])
{
ps3ntfs_prx_lock();
int j;
for(j = 0; j < num_mounts; j++)
{
// unmount all partitions of this device
if(mounts[j].interface == ntfs_usb_if[i])
{
ntfsUnmount(mounts[j].name, true);
// realloc
memmove(&mounts[j], &mounts[j + 1], (num_mounts - j - 1) * sizeof(ntfs_md));
mounts = (ntfs_md*) realloc(mounts, --num_mounts * sizeof(ntfs_md));
--j;
}
}
ps3ntfs_prx_unlock();
if(show_msgs)
{
char msg[256];
sprintf(msg, "Unmounted /dev_usb%03d", i);
vshtask_notify(msg);
}
is_mounted[i] = false;
}
}
}
sys_timer_sleep(1);
}
// Unmount NTFS.
ps3ntfs_prx_lock();
while(num_mounts-- > 0)
{
ntfsUnmount(mounts[num_mounts].name, true);
}
if(mounts != NULL)
{
free(mounts);
mounts = NULL;
}
ps3ntfs_prx_unlock();
sys_lwmutex_destroy(&mutex);
sys_ppu_thread_exit(0);
}