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,91 +108,131 @@ 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 , "FATFS: 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 );
126
+ memset (sd_dev , 0 , sizeof (kos_blockdev_t ) * MAX_PARTITIONS );
136
127
}
137
- if (!sd_dev ) {
138
- dbglog (DBG_ERROR , "Can't allocate memory for SD card partitions\n" );
139
- return -1 ;
140
- }
141
-
142
- memset (& sd_dev [0 ], 0 , sizeof (kos_blockdev_t ) * MAX_PARTITIONS );
143
128
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. */
164
147
fat_part = is_fat_partition (partition_type );
165
148
166
149
if (fat_part ) {
167
150
168
- dbglog (DBG_INFO , "Detected FAT%d filesystem on partition %d\n" , fat_part , part );
169
-
151
+ dbglog (DBG_INFO , "FATFS: Detected FAT%d on partition %d\n" , fat_part , part );
152
+
170
153
if (fs_fat_init ()) {
171
- dbglog (DBG_INFO , "Could not initialize fs_fat !\n" );
154
+ dbglog (DBG_INFO , "FATFS: 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 , "FATFS: Mounting filesystem to %s ...\n" , path );
182
166
183
167
if (fs_fat_mount (path , dev , NULL , part )) {
184
- dbglog (DBG_INFO , "Could not mount device as fatfs.\n" );
168
+ dbglog (DBG_INFO , "FATFS: 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
- dbglog (DBG_INFO , "Unknown filesystem: 0x%02x\n" , partition_type );
177
+ dbglog (DBG_INFO , "FATFS: 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 , "FATFS: 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 , "FATFS: 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 , "FATFS: 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 , "FATFS: 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 , "FATFS: 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 () {
@@ -203,7 +244,7 @@ int fs_fat_mount_ide() {
203
244
kos_blockdev_t * dev ;
204
245
kos_blockdev_t * dev_dma ;
205
246
206
- dbglog (DBG_INFO , "Checking for G1 ATA devices...\n" );
247
+ dbglog (DBG_INFO , "FATFS: Checking for G1 ATA devices...\n" );
207
248
208
249
if (g1_ata_init ()) {
209
250
return -1 ;
@@ -212,13 +253,13 @@ int fs_fat_mount_ide() {
212
253
/* Read the MBR from the disk */
213
254
if (g1_ata_lba_mode ()) {
214
255
if (g1_ata_read_lba (0 , 1 , (uint16_t * )buf ) < 0 ) {
215
- dbglog (DBG_ERROR , "Can't read MBR from IDE by LBA\n" );
256
+ dbglog (DBG_ERROR , "FATFS: Can't read MBR from IDE by LBA\n" );
216
257
return -1 ;
217
258
}
218
259
}
219
260
else {
220
261
if (g1_ata_read_chs (0 , 0 , 1 , 1 , (uint16_t * )buf ) < 0 ) {
221
- dbglog (DBG_ERROR , "Can't read MBR from IDE by CHS\n" );
262
+ dbglog (DBG_ERROR , "FATFS: Can't read MBR from IDE by CHS\n" );
222
263
return -1 ;
223
264
}
224
265
}
@@ -228,7 +269,7 @@ int fs_fat_mount_ide() {
228
269
g1_dev_dma = malloc (sizeof (kos_blockdev_t ) * MAX_PARTITIONS );
229
270
}
230
271
if (!g1_dev || !g1_dev_dma ) {
231
- dbglog (DBG_ERROR , "Can't allocate memory for IDE partitions\n" );
272
+ dbglog (DBG_ERROR , "FATFS: Can't allocate memory for IDE partitions\n" );
232
273
return -1 ;
233
274
}
234
275
@@ -261,10 +302,10 @@ int fs_fat_mount_ide() {
261
302
262
303
if (fat_part ) {
263
304
264
- dbglog (DBG_INFO , "Detected FAT%d filesystem on partition %d\n" , fat_part , part );
305
+ dbglog (DBG_INFO , "FATFS: Detected FAT%d on partition %d\n" , fat_part , part );
265
306
266
307
if (fs_fat_init ()) {
267
- dbglog (DBG_INFO , "Could not initialize fs_fat!\n" );
308
+ dbglog (DBG_INFO , "FATFS: Could not initialize fs_fat!\n" );
268
309
dev -> shutdown (dev );
269
310
}
270
311
else {
@@ -279,10 +320,10 @@ 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 , "FATFS: Mounting filesystem to %s ...\n" , path );
283
324
284
325
if (fs_fat_mount (path , dev , dev_dma , part )) {
285
- dbglog (DBG_INFO , "Could not mount device as fatfs.\n" );
326
+ dbglog (DBG_INFO , "FATFS: Could not mount device as fatfs.\n" );
286
327
dev -> shutdown (dev );
287
328
if (dev_dma ) {
288
329
dev_dma -> shutdown (dev_dma );
@@ -291,9 +332,61 @@ int fs_fat_mount_ide() {
291
332
}
292
333
}
293
334
else {
294
- dbglog (DBG_INFO , "Unknown filesystem: 0x%02x\n" , partition_type );
335
+ dbglog (DBG_INFO , "FATFS: Unknown filesystem: 0x%02x\n" , partition_type );
295
336
dev -> shutdown (dev );
296
337
}
297
338
}
298
339
return 0 ;
299
340
}
341
+
342
+ /* Unmount and cleanup SD devices */
343
+ void fs_fat_unmount_sd (void ) {
344
+ if (sd_dev != NULL ) {
345
+ for (int i = 0 ; i < MAX_PARTITIONS ; i ++ ) {
346
+ if (sd_dev [i ].dev_data != NULL ) {
347
+ char path [16 ];
348
+ if (i == 0 ) {
349
+ strcpy (path , "/sd" );
350
+ }
351
+ else {
352
+ sprintf (path , "/sd%d" , i );
353
+ }
354
+ fs_fat_unmount (path );
355
+ sd_dev [i ].shutdown (& sd_dev [i ]);
356
+ }
357
+ }
358
+ free (sd_dev );
359
+ sd_dev = NULL ;
360
+ }
361
+ }
362
+
363
+ /* Unmount and cleanup IDE devices */
364
+ void fs_fat_unmount_ide (void ) {
365
+ if (g1_dev != NULL ) {
366
+ for (int i = 0 ; i < MAX_PARTITIONS ; i ++ ) {
367
+ if (g1_dev [i ].dev_data != NULL ) {
368
+ char path [16 ];
369
+ if (i == 0 ) {
370
+ strcpy (path , "/ide" );
371
+ }
372
+ else {
373
+ sprintf (path , "/ide%d" , i );
374
+ }
375
+ fs_fat_unmount (path );
376
+ g1_dev [i ].shutdown (& g1_dev [i ]);
377
+ }
378
+ }
379
+ free (g1_dev );
380
+ g1_dev = NULL ;
381
+ }
382
+
383
+ if (g1_dev_dma != NULL ) {
384
+ for (int i = 0 ; i < MAX_PARTITIONS ; i ++ ) {
385
+ if (g1_dev_dma [i ].dev_data != NULL ) {
386
+ g1_dev_dma [i ].shutdown (& g1_dev_dma [i ]);
387
+ }
388
+ }
389
+ free (g1_dev_dma );
390
+ g1_dev_dma = NULL ;
391
+ }
392
+ }
0 commit comments