Skip to content

Commit 833aeac

Browse files
committed
Added SCI-SPI support for SD card.
Also added devices unmounting.
1 parent bfdadd9 commit 833aeac

File tree

3 files changed

+151
-39
lines changed

3 files changed

+151
-39
lines changed

fatfs/src/dc.c

+5
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,11 @@ int fs_fat_shutdown(void) {
12321232
if (!initted) {
12331233
return 0;
12341234
}
1235+
1236+
/* Clean up SD and IDE resources */
1237+
fs_fat_unmount_sd();
1238+
fs_fat_unmount_ide();
1239+
12351240
for (i = 0; i < MAX_FAT_MOUNTS; ++i) {
12361241

12371242
if (fat_mnt[i].dev != NULL) {

fatfs/src/dc_bdev.c

+132-38
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdio.h>
3131
#include <errno.h>
3232
#include <stdint.h>
33+
#include <stdbool.h>
3334
#include <string.h>
3435
#include <stdlib.h>
3536
#include <inttypes.h>
@@ -107,57 +108,39 @@ static int sd_blockdev_for_device(kos_blockdev_t *rv) {
107108
return 0;
108109
}
109110

110-
int fs_fat_mount_sd() {
111-
111+
static bool mount_sd_card(uint8_t *mbr_buf) {
112112
uint8_t partition_type;
113113
int part = 0, fat_part = 0;
114114
char path[8];
115-
uint8_t buf[512];
116115
kos_blockdev_t *dev;
116+
const char *prefix = "/sd";
117117

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);
128120

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+
}
133125

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);
140127
}
141128

142-
memset(&sd_dev[0], 0, sizeof(kos_blockdev_t) * MAX_PARTITIONS);
143-
144129
for (part = 0; part < MAX_PARTITIONS; part++) {
145-
146130
dev = &sd_dev[part];
147-
148-
if (check_partition(buf, part)) {
131+
132+
if (check_partition(mbr_buf, part)) {
149133
continue;
150134
}
151135
if (sd_blockdev_for_partition(part, dev, &partition_type)) {
152136
continue;
153137
}
154-
155-
if (!part) {
156-
strcpy(path, "/sd");
157-
path[3] = '\0';
138+
139+
if (part == 0) {
140+
strcpy(path, prefix);
158141
}
159142
else {
160-
sprintf(path, "sd%d", part);
143+
sprintf(path, "%s%d", prefix, part);
161144
}
162145

163146
/* Check to see if the MBR says that we have a FAT partition. */
@@ -166,32 +149,90 @@ int fs_fat_mount_sd() {
166149
if (fat_part) {
167150

168151
dbglog(DBG_INFO, "Detected FAT%d filesystem on partition %d\n", fat_part, part);
169-
152+
170153
if (fs_fat_init()) {
171-
dbglog(DBG_INFO, "Could not initialize fs_fat!\n");
154+
dbglog(DBG_INFO, "Could not initialize fatfs!\n");
172155
dev->shutdown(dev);
173156
}
174157
else {
175158
/* Need full disk block device for FAT */
176159
dev->shutdown(dev);
160+
177161
if (sd_blockdev_for_device(dev)) {
178162
continue;
179163
}
180164

181-
dbglog(DBG_INFO, "Mounting filesystem...\n");
165+
dbglog(DBG_INFO, "Mounting filesystem to %s...\n", path);
182166

183167
if (fs_fat_mount(path, dev, NULL, part)) {
184168
dbglog(DBG_INFO, "Could not mount device as fatfs.\n");
185169
dev->shutdown(dev);
186170
}
171+
else {
172+
return true;
173+
}
187174
}
188175
}
189176
else {
190177
dbglog(DBG_INFO, "Unknown filesystem: 0x%02x\n", partition_type);
191178
dev->shutdown(dev);
192179
}
193180
}
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;
195236
}
196237

197238
int fs_fat_mount_ide() {
@@ -279,7 +320,7 @@ int fs_fat_mount_ide() {
279320
dev_dma = NULL;
280321
}
281322

282-
dbglog(DBG_INFO, "Mounting filesystem...\n");
323+
dbglog(DBG_INFO, "Mounting filesystem to %s...\n", path);
283324

284325
if (fs_fat_mount(path, dev, dev_dma, part)) {
285326
dbglog(DBG_INFO, "Could not mount device as fatfs.\n");
@@ -297,3 +338,56 @@ int fs_fat_mount_ide() {
297338
}
298339
return 0;
299340
}
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+
}

include/fatfs.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ int fs_fat_shutdown(void);
7575
* \param partition Partition number (reset to 0 for start block).
7676
* \return 0 on success, or a negative value if an error occurred.
7777
*/
78-
int fs_fat_mount(const char *mp, kos_blockdev_t *dev_pio, kos_blockdev_t *dev_dma, int partition);
78+
int fs_fat_mount(const char *mp, kos_blockdev_t *dev_pio,
79+
kos_blockdev_t *dev_dma, int partition);
7980

8081
/**
8182
* \brief Unmount the FAT filesystem.
@@ -95,16 +96,28 @@ int fs_fat_is_mounted(const char *mp);
9596

9697
/**
9798
* \brief Initialize the FAT and SD card, then mount all partitions on it.
99+
* This function will try to detect and mount both SCIF and SCI interfaces
100+
* if they are available.
98101
*
99102
* \return 0 on success, or a negative value if an error occurred.
100103
*/
101104
int fs_fat_mount_sd(void);
102105

106+
/**
107+
* \brief Unmount all SD card partitions and free resources.
108+
*/
109+
void fs_fat_unmount_sd(void);
110+
103111
/**
104112
* \brief Initialize the FAT and IDE (G1-ATA), then mount all partitions on it.
105113
*
106114
* \return 0 on success, or a negative value if an error occurred.
107115
*/
108116
int fs_fat_mount_ide(void);
109117

118+
/**
119+
* \brief Unmount all IDE partitions and free resources.
120+
*/
121+
void fs_fat_unmount_ide(void);
122+
110123
#endif /* _FATFS_H */

0 commit comments

Comments
 (0)