30
30
#include <stdio.h>
31
31
#include <errno.h>
32
32
#include <stdint.h>
33
+ #include <stdbool.h>
33
34
#include <string.h>
34
35
#include <stdlib.h>
35
36
#include <inttypes.h>
@@ -107,57 +108,39 @@ static int sd_blockdev_for_device(kos_blockdev_t *rv) {
107
108
return 0 ;
108
109
}
109
110
110
- int fs_fat_mount_sd () {
111
-
111
+ static bool mount_sd_card (uint8_t * mbr_buf ) {
112
112
uint8_t partition_type ;
113
113
int part = 0 , fat_part = 0 ;
114
114
char path [8 ];
115
- uint8_t buf [512 ];
116
115
kos_blockdev_t * dev ;
116
+ const char * prefix = "/sd" ;
117
117
118
- dbglog (DBG_INFO , "Checking for SD card...\n" );
119
-
120
- if (sd_init ()) {
121
- scif_init ();
122
- dbglog (DBG_INFO , "\nSD card not found.\n" );
123
- return -1 ;
124
- }
125
-
126
- dbglog (DBG_INFO , "SD card initialized, capacity %" PRIu32 " MB\n" ,
127
- (uint32 )(sd_get_size () / 1024 / 1024 ));
118
+ if (sd_dev == NULL ) {
119
+ sd_dev = malloc (sizeof (kos_blockdev_t ) * MAX_PARTITIONS );
128
120
129
- if (sd_read_blocks ( 0 , 1 , buf ) ) {
130
- dbglog (DBG_ERROR , "Can't read MBR from SD card\n" );
131
- return -1 ;
132
- }
121
+ if (sd_dev == NULL ) {
122
+ dbglog (DBG_ERROR , "Can't allocate memory for SD card partitions \n" );
123
+ return false ;
124
+ }
133
125
134
- if (!sd_dev ) {
135
- sd_dev = malloc (sizeof (kos_blockdev_t ) * MAX_PARTITIONS );
136
- }
137
- if (!sd_dev ) {
138
- dbglog (DBG_ERROR , "Can't allocate memory for SD card partitions\n" );
139
- return -1 ;
126
+ memset (sd_dev , 0 , sizeof (kos_blockdev_t ) * MAX_PARTITIONS );
140
127
}
141
128
142
- memset (& sd_dev [0 ], 0 , sizeof (kos_blockdev_t ) * MAX_PARTITIONS );
143
-
144
129
for (part = 0 ; part < MAX_PARTITIONS ; part ++ ) {
145
-
146
130
dev = & sd_dev [part ];
147
-
148
- if (check_partition (buf , part )) {
131
+
132
+ if (check_partition (mbr_buf , part )) {
149
133
continue ;
150
134
}
151
135
if (sd_blockdev_for_partition (part , dev , & partition_type )) {
152
136
continue ;
153
137
}
154
-
155
- if (!part ) {
156
- strcpy (path , "/sd" );
157
- path [3 ] = '\0' ;
138
+
139
+ if (part == 0 ) {
140
+ strcpy (path , prefix );
158
141
}
159
142
else {
160
- sprintf (path , "sd%d" , part );
143
+ sprintf (path , "%s%d" , prefix , part );
161
144
}
162
145
163
146
/* Check to see if the MBR says that we have a FAT partition. */
@@ -166,32 +149,90 @@ int fs_fat_mount_sd() {
166
149
if (fat_part ) {
167
150
168
151
dbglog (DBG_INFO , "Detected FAT%d filesystem on partition %d\n" , fat_part , part );
169
-
152
+
170
153
if (fs_fat_init ()) {
171
- dbglog (DBG_INFO , "Could not initialize fs_fat !\n" );
154
+ dbglog (DBG_INFO , "Could not initialize fatfs !\n" );
172
155
dev -> shutdown (dev );
173
156
}
174
157
else {
175
158
/* Need full disk block device for FAT */
176
159
dev -> shutdown (dev );
160
+
177
161
if (sd_blockdev_for_device (dev )) {
178
162
continue ;
179
163
}
180
164
181
- dbglog (DBG_INFO , "Mounting filesystem...\n" );
165
+ dbglog (DBG_INFO , "Mounting filesystem to %s ...\n" , path );
182
166
183
167
if (fs_fat_mount (path , dev , NULL , part )) {
184
168
dbglog (DBG_INFO , "Could not mount device as fatfs.\n" );
185
169
dev -> shutdown (dev );
186
170
}
171
+ else {
172
+ return true;
173
+ }
187
174
}
188
175
}
189
176
else {
190
177
dbglog (DBG_INFO , "Unknown filesystem: 0x%02x\n" , partition_type );
191
178
dev -> shutdown (dev );
192
179
}
193
180
}
194
- return 0 ;
181
+
182
+ return false;
183
+ }
184
+
185
+ int fs_fat_mount_sd () {
186
+ uint8_t mbr_buffer [512 ];
187
+ sd_init_params_t params ;
188
+
189
+ dbglog (DBG_INFO , "Checking for SD cards...\n" );
190
+
191
+ /* Try SCIF interface first */
192
+ params .interface = SD_IF_SCIF ;
193
+ #ifdef FATFS_SD_CHECK_CRC
194
+ params .check_crc = true;
195
+ #else
196
+ params .check_crc = false;
197
+ #endif
198
+
199
+ memset (mbr_buffer , 0 , sizeof (mbr_buffer ));
200
+
201
+ if (sd_init_ex (& params ) == 0 ) {
202
+ dbglog (DBG_INFO , "SD card found on SCIF-SPI: %" PRIu32 " MB\n" ,
203
+ (uint32 )(sd_get_size () / 1024 / 1024 ));
204
+
205
+ if (sd_read_blocks (0 , 1 , mbr_buffer ) == 0 ) {
206
+ if (mount_sd_card (mbr_buffer )) {
207
+ return 0 ;
208
+ }
209
+ }
210
+ else {
211
+ dbglog (DBG_ERROR , "Can't read MBR from SCIF-SPI SD card\n" );
212
+ }
213
+ }
214
+
215
+ /* Get back dbglog if they are used */
216
+ scif_init ();
217
+
218
+ /* If no card found on SCIF, try SCI interface */
219
+ params .interface = SD_IF_SCI ;
220
+
221
+ if (sd_init_ex (& params ) == 0 ) {
222
+ dbglog (DBG_INFO , "SD card found on SCI-SPI: %" PRIu32 " MB\n" ,
223
+ (uint32 )(sd_get_size () / 1024 / 1024 ));
224
+
225
+ if (sd_read_blocks (0 , 1 , mbr_buffer ) == 0 ) {
226
+ if (mount_sd_card (mbr_buffer )) {
227
+ return 0 ;
228
+ }
229
+ }
230
+ else {
231
+ dbglog (DBG_ERROR , "Can't read MBR from SCI-SPI SD card\n" );
232
+ }
233
+ }
234
+ /* No cards found on any interface */
235
+ return -1 ;
195
236
}
196
237
197
238
int fs_fat_mount_ide () {
@@ -279,7 +320,7 @@ int fs_fat_mount_ide() {
279
320
dev_dma = NULL ;
280
321
}
281
322
282
- dbglog (DBG_INFO , "Mounting filesystem...\n" );
323
+ dbglog (DBG_INFO , "Mounting filesystem to %s ...\n" , path );
283
324
284
325
if (fs_fat_mount (path , dev , dev_dma , part )) {
285
326
dbglog (DBG_INFO , "Could not mount device as fatfs.\n" );
@@ -297,3 +338,56 @@ int fs_fat_mount_ide() {
297
338
}
298
339
return 0 ;
299
340
}
341
+
342
+ /* Unmount and cleanup SD devices */
343
+ void fs_fat_unmount_sd (void ) {
344
+ /* Unmount SCIF SD devices */
345
+ if (sd_dev != NULL ) {
346
+ for (int i = 0 ; i < MAX_PARTITIONS ; i ++ ) {
347
+ if (sd_dev [i ].dev_data != NULL ) {
348
+ char path [16 ];
349
+ if (i == 0 ) {
350
+ strcpy (path , "/sd" );
351
+ }
352
+ else {
353
+ sprintf (path , "/sd%d" , i );
354
+ }
355
+ fs_fat_unmount (path );
356
+ sd_dev [i ].shutdown (& sd_dev [i ]);
357
+ }
358
+ }
359
+ free (sd_dev );
360
+ sd_dev = NULL ;
361
+ }
362
+ }
363
+
364
+ /* Unmount and cleanup IDE devices */
365
+ void fs_fat_unmount_ide (void ) {
366
+ if (g1_dev != NULL ) {
367
+ for (int i = 0 ; i < MAX_PARTITIONS ; i ++ ) {
368
+ if (g1_dev [i ].dev_data != NULL ) {
369
+ char path [16 ];
370
+ if (i == 0 ) {
371
+ strcpy (path , "/ide" );
372
+ }
373
+ else {
374
+ sprintf (path , "/ide%d" , i );
375
+ }
376
+ fs_fat_unmount (path );
377
+ g1_dev [i ].shutdown (& g1_dev [i ]);
378
+ }
379
+ }
380
+ free (g1_dev );
381
+ g1_dev = NULL ;
382
+ }
383
+
384
+ if (g1_dev_dma != NULL ) {
385
+ for (int i = 0 ; i < MAX_PARTITIONS ; i ++ ) {
386
+ if (g1_dev_dma [i ].dev_data != NULL ) {
387
+ g1_dev_dma [i ].shutdown (& g1_dev_dma [i ]);
388
+ }
389
+ }
390
+ free (g1_dev_dma );
391
+ g1_dev_dma = NULL ;
392
+ }
393
+ }
0 commit comments