1
1
#include "image.h"
2
+ #include "platform.h"
2
3
#include "fatfs/ff.h"
3
4
4
- FIL mount_file ;
5
- u32 mount_state = 0 ;
5
+ #define RAMDRV_BUFFER_O3DS ((u8*)0x22200000) // in O3DS FCRAM
6
+ #define RAMDRV_SIZE_O3DS (0x01C00000) // 28MB
7
+ #define RAMDRV_BUFFER_N3DS ((u8*)0x28000000) // in N3DS FCRAM
8
+ #define RAMDRV_SIZE_N3DS (0x08000000) // 128MB
9
+
10
+ static u8 * ramdrv_buffer = NULL ;
11
+ static u32 ramdrv_size = 0 ;
12
+
13
+ static FIL mount_file ;
14
+ static u32 mount_state = 0 ;
6
15
7
16
int ReadImageSectors (u8 * buffer , u32 sector , u32 count ) {
8
17
UINT bytes_read ;
9
18
UINT ret ;
10
19
if (!count ) return -1 ;
20
+ if (mount_state == IMG_RAMDRV ) {
21
+ if ((sector + count ) * 0x200 > ramdrv_size ) return -1 ;
22
+ memcpy (buffer , ramdrv_buffer + (sector * 0x200 ), count * 0x200 );
23
+ return 0 ;
24
+ }
11
25
if (!mount_state ) return FR_INVALID_OBJECT ;
12
26
if (f_tell (& mount_file ) != sector * 0x200 ) {
13
27
if (f_size (& mount_file ) < sector * 0x200 ) return -1 ;
14
- f_lseek (& mount_file , sector * 0x200 );
28
+ f_lseek (& mount_file , sector * 0x200 );
15
29
}
16
30
ret = f_read (& mount_file , buffer , count * 0x200 , & bytes_read );
17
31
return (ret != 0 ) ? (int ) ret : (bytes_read != count * 0x200 ) ? -1 : 0 ;
@@ -21,6 +35,11 @@ int WriteImageSectors(const u8* buffer, u32 sector, u32 count) {
21
35
UINT bytes_written ;
22
36
UINT ret ;
23
37
if (!count ) return -1 ;
38
+ if (mount_state == IMG_RAMDRV ) {
39
+ if ((sector + count ) * 0x200 > ramdrv_size ) return -1 ;
40
+ memcpy (ramdrv_buffer + (sector * 0x200 ), buffer , count * 0x200 );
41
+ return 0 ;
42
+ }
24
43
if (!mount_state ) return FR_INVALID_OBJECT ;
25
44
if (f_tell (& mount_file ) != sector * 0x200 )
26
45
f_lseek (& mount_file , sector * 0x200 );
@@ -29,11 +48,13 @@ int WriteImageSectors(const u8* buffer, u32 sector, u32 count) {
29
48
}
30
49
31
50
int SyncImage (void ) {
32
- return (mount_state ) ? f_sync (& mount_file ) : FR_INVALID_OBJECT ;
51
+ return (mount_state == IMG_RAMDRV ) ? FR_OK :
52
+ mount_state ? f_sync (& mount_file ) : FR_INVALID_OBJECT ;
33
53
}
34
54
35
55
u64 GetMountSize (void ) {
36
- return mount_state ? f_size (& mount_file ) : 0 ;
56
+ return (mount_state == IMG_RAMDRV ) ? ramdrv_size :
57
+ mount_state ? f_size (& mount_file ) : 0 ;
37
58
}
38
59
39
60
u32 GetMountState (void ) {
@@ -71,9 +92,22 @@ u32 IdentifyImage(const char* path) {
71
92
return 0 ;
72
93
}
73
94
95
+ u32 MountRamDrive (void ) {
96
+ if (mount_state && (mount_state != IMG_RAMDRV ))
97
+ f_close (& mount_file );
98
+ if (GetUnitPlatform () == PLATFORM_3DS ) {
99
+ ramdrv_buffer = RAMDRV_BUFFER_O3DS ;
100
+ ramdrv_size = RAMDRV_SIZE_O3DS ;
101
+ } else {
102
+ ramdrv_buffer = RAMDRV_BUFFER_N3DS ;
103
+ ramdrv_size = RAMDRV_SIZE_N3DS ;
104
+ }
105
+ return (mount_state = IMG_RAMDRV );
106
+ }
107
+
74
108
u32 MountImage (const char * path ) {
75
109
if (mount_state ) {
76
- f_close (& mount_file );
110
+ if ( mount_state != IMG_RAMDRV ) f_close (& mount_file );
77
111
mount_state = 0 ;
78
112
}
79
113
if (!path || !IdentifyImage (path )) return 0 ;
0 commit comments