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>
@@ -62,14 +63,14 @@ static int check_partition(uint8_t *buf, int partition) {
6263 int pval ;
6364
6465 if (buf [0x01FE ] != 0x55 || buf [0x1FF ] != 0xAA ) {
65- // dbglog(DBG_DEBUG, "Device doesn't appear to have a MBR\n");
66+ // dbglog(DBG_DEBUG, "Device doesn't appear to have a MBR\n");
6667 return -1 ;
6768 }
6869
6970 pval = 16 * partition + 0x01BE ;
7071
7172 if (buf [pval + 4 ] == 0 ) {
72- // dbglog(DBG_DEBUG, "Partition empty: 0x%02x\n", buf[pval + 4]);
73+ // dbglog(DBG_DEBUG, "Partition empty: 0x%02x\n", buf[pval + 4]);
7374 return -1 ;
7475 }
7576
@@ -107,91 +108,131 @@ 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 , "FATFS: 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 ];
147131
148- if (check_partition (buf , part )) {
132+ if (check_partition (mbr_buf , part )) {
149133 continue ;
150134 }
151135 if (sd_blockdev_for_partition (part , dev , & partition_type )) {
152136 continue ;
153137 }
154138
155- if (!part ) {
156- strcpy (path , "/sd" );
157- path [3 ] = '\0' ;
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. */
164147 fat_part = is_fat_partition (partition_type );
165148
166149 if (fat_part ) {
167150
168- dbglog (DBG_INFO , "Detected FAT%d filesystem on partition %d\n" , fat_part , part );
151+ dbglog (DBG_INFO , "FATFS: Detected FAT%d on partition %d\n" , fat_part , part );
169152
170153 if (fs_fat_init ()) {
171- dbglog (DBG_INFO , "Could not initialize fs_fat !\n" );
154+ dbglog (DBG_INFO , "FATFS: 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 , "FATFS: Mounting filesystem to %s ...\n" , path );
182166
183167 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" );
185169 dev -> shutdown (dev );
186170 }
171+ else {
172+ return true;
173+ }
187174 }
188175 }
189176 else {
190- dbglog (DBG_INFO , "Unknown filesystem: 0x%02x\n" , partition_type );
177+ dbglog (DBG_INFO , "FATFS: 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 , "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 ;
195236}
196237
197238int fs_fat_mount_ide () {
@@ -203,7 +244,7 @@ int fs_fat_mount_ide() {
203244 kos_blockdev_t * dev ;
204245 kos_blockdev_t * dev_dma ;
205246
206- dbglog (DBG_INFO , "Checking for G1 ATA devices...\n" );
247+ dbglog (DBG_INFO , "FATFS: Checking for G1 ATA devices...\n" );
207248
208249 if (g1_ata_init ()) {
209250 return -1 ;
@@ -212,13 +253,13 @@ int fs_fat_mount_ide() {
212253 /* Read the MBR from the disk */
213254 if (g1_ata_lba_mode ()) {
214255 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" );
216257 return -1 ;
217258 }
218259 }
219260 else {
220261 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" );
222263 return -1 ;
223264 }
224265 }
@@ -228,7 +269,7 @@ int fs_fat_mount_ide() {
228269 g1_dev_dma = malloc (sizeof (kos_blockdev_t ) * MAX_PARTITIONS );
229270 }
230271 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" );
232273 return -1 ;
233274 }
234275
@@ -261,10 +302,10 @@ int fs_fat_mount_ide() {
261302
262303 if (fat_part ) {
263304
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 );
265306
266307 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" );
268309 dev -> shutdown (dev );
269310 }
270311 else {
@@ -279,10 +320,10 @@ int fs_fat_mount_ide() {
279320 dev_dma = NULL ;
280321 }
281322
282- dbglog (DBG_INFO , "Mounting filesystem...\n" );
323+ dbglog (DBG_INFO , "FATFS: Mounting filesystem to %s ...\n" , path );
283324
284325 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" );
286327 dev -> shutdown (dev );
287328 if (dev_dma ) {
288329 dev_dma -> shutdown (dev_dma );
@@ -291,9 +332,61 @@ int fs_fat_mount_ide() {
291332 }
292333 }
293334 else {
294- dbglog (DBG_INFO , "Unknown filesystem: 0x%02x\n" , partition_type );
335+ dbglog (DBG_INFO , "FATFS: Unknown filesystem: 0x%02x\n" , partition_type );
295336 dev -> shutdown (dev );
296337 }
297338 }
298339 return 0 ;
299340}
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