18
18
#include <stdlib.h>
19
19
#include <unistd.h>
20
20
#include <stdint.h>
21
+ #include <string.h>
21
22
#include <errno.h>
22
23
#include <fcntl.h>
23
24
#include <sys/stat.h>
30
31
#include "uspi.h"
31
32
#endif
32
33
33
- extern struct emmc_block_dev emmc_dev ;
34
+ static struct emmc_block_dev * emmc_dev ;
34
35
35
36
/* Definitions of physical drive number for each drive */
36
37
#define MMC 0 /* Map MMC/SD card to drive number 0 */
@@ -39,6 +40,7 @@ extern struct emmc_block_dev emmc_dev;
39
40
#define MAX_DESCRIPTORS 256
40
41
#define RESERVED_DESCRIPTORS 3
41
42
43
+ static FATFS fat_fs [_VOLUMES ];
42
44
static FIL * file_descriptors [MAX_DESCRIPTORS ];
43
45
44
46
void * __dso_handle ;
@@ -284,6 +286,47 @@ int chdir(const char * path) {
284
286
return rc == FR_OK ? 0 : -1 ;
285
287
}
286
288
289
+ int ftruncate (int file , off_t length ) {
290
+ if (file < RESERVED_DESCRIPTORS || file >= MAX_DESCRIPTORS || file_descriptors [file ] == NULL ) {
291
+ errno = EBADF ;
292
+ return -1 ;
293
+ }
294
+
295
+ FRESULT rc = f_truncate (file_descriptors [file ]);
296
+ errno = FRESULT_to_errno (rc );
297
+ return rc == FR_OK ? 0 : -1 ;
298
+ }
299
+
300
+ static const char * _VOLUME_NAME [_VOLUMES ] = {
301
+ _VOLUME_STRS
302
+ };
303
+
304
+ int mount (const char * source ) {
305
+ for (int i = 0 ; i < _VOLUMES ; i ++ ) {
306
+ if (!strncasecmp (source , _VOLUME_NAME [i ], strlen (_VOLUME_NAME [i ]))) {
307
+ FRESULT rc = f_mount (& fat_fs [i ], source , 1 );
308
+ errno = FRESULT_to_errno (rc );
309
+ return rc == FR_OK ? 0 : -1 ;
310
+ }
311
+ }
312
+
313
+ errno = EINVAL ;
314
+ return -1 ;
315
+ }
316
+
317
+ int umount (const char * target ) {
318
+ for (int i = 0 ; i < _VOLUMES ; i ++ ) {
319
+ if (!strncasecmp (target , _VOLUME_NAME [i ], strlen (_VOLUME_NAME [i ]))) {
320
+ FRESULT rc = f_mount (NULL , target , 1 );
321
+ errno = FRESULT_to_errno (rc );
322
+ return rc == FR_OK ? 0 : -1 ;
323
+ }
324
+ }
325
+
326
+ errno = EINVAL ;
327
+ return -1 ;
328
+ }
329
+
287
330
void _fini () {
288
331
while (1 )
289
332
;
@@ -302,23 +345,30 @@ int _gettimeofday(struct timeval *tv, struct timezone *tz) {
302
345
/*-----------------------------------------------------------------------*/
303
346
304
347
DSTATUS disk_status (
305
- BYTE pdrv /* Physical drive nmuber to identify the drive */
348
+ BYTE pdrv /* Physical drive number to identify the drive */
306
349
)
307
350
{
308
351
switch (pdrv ) {
309
352
case MMC :
353
+ if (emmc_dev == NULL )
354
+ {
355
+ return STA_NOINIT ;
356
+ }
310
357
return 0 ;
311
358
312
359
#ifdef HAVE_USPI
313
360
default :
314
- if (!USPiMassStorageDeviceAvailable ())
361
+ {
362
+ int nDeviceIndex = pdrv - USB ;
363
+ if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable ())
315
364
{
316
- return STA_NOINIT ;
365
+ return STA_NODISK ;
317
366
}
318
367
return 0 ;
368
+ }
319
369
#endif
320
370
}
321
- return STA_NOINIT ;
371
+ return STA_NODISK ;
322
372
}
323
373
324
374
@@ -328,23 +378,33 @@ DSTATUS disk_status (
328
378
/*-----------------------------------------------------------------------*/
329
379
330
380
DSTATUS disk_initialize (
331
- BYTE pdrv /* Physical drive nmuber to identify the drive */
381
+ BYTE pdrv /* Physical drive number to identify the drive */
332
382
)
333
383
{
334
384
switch (pdrv ) {
335
385
case MMC :
386
+ if (emmc_dev == NULL )
387
+ {
388
+ if (sd_card_init ((struct block_device * * )& emmc_dev ) != 0 )
389
+ {
390
+ return STA_NOINIT ;
391
+ }
392
+ }
336
393
return 0 ;
337
394
338
395
#ifdef HAVE_USPI
339
396
default :
340
- if (!USPiMassStorageDeviceAvailable ())
397
+ {
398
+ int nDeviceIndex = pdrv - USB ;
399
+ if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable ())
341
400
{
342
- return STA_NOINIT ;
401
+ return STA_NODISK ;
343
402
}
344
403
return 0 ;
404
+ }
345
405
#endif
346
406
}
347
- return STA_NOINIT ;
407
+ return STA_NODISK ;
348
408
}
349
409
350
410
@@ -354,7 +414,7 @@ DSTATUS disk_initialize (
354
414
/*-----------------------------------------------------------------------*/
355
415
356
416
DRESULT disk_read (
357
- BYTE pdrv , /* Physical drive nmuber to identify the drive */
417
+ BYTE pdrv , /* Physical drive number to identify the drive */
358
418
BYTE * buff , /* Data buffer to store read data */
359
419
DWORD sector , /* Sector address in LBA */
360
420
UINT count /* Number of sectors to read */
@@ -363,7 +423,7 @@ DRESULT disk_read (
363
423
switch (pdrv ) {
364
424
case MMC :
365
425
{
366
- size_t buf_size = count * emmc_dev . bd .block_size ;
426
+ size_t buf_size = count * emmc_dev -> bd .block_size ;
367
427
if (sd_read (buff , buf_size , sector ) < buf_size )
368
428
{
369
429
return RES_ERROR ;
@@ -375,13 +435,14 @@ DRESULT disk_read (
375
435
#ifdef HAVE_USPI
376
436
default :
377
437
{
378
- if (!USPiMassStorageDeviceAvailable ())
438
+ int nDeviceIndex = pdrv - USB ;
439
+ if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable ())
379
440
{
380
441
return RES_PARERR ;
381
442
}
382
443
383
444
unsigned buf_size = count * USPI_BLOCK_SIZE ;
384
- if (USPiMassStorageDeviceRead (sector * USPI_BLOCK_SIZE , buff , buf_size , pdrv - USB ) < buf_size )
445
+ if (USPiMassStorageDeviceRead (sector * USPI_BLOCK_SIZE , buff , buf_size , nDeviceIndex ) < buf_size )
385
446
{
386
447
return RES_ERROR ;
387
448
}
@@ -401,7 +462,7 @@ DRESULT disk_read (
401
462
/*-----------------------------------------------------------------------*/
402
463
403
464
DRESULT disk_write (
404
- BYTE pdrv , /* Physical drive nmuber to identify the drive */
465
+ BYTE pdrv , /* Physical drive number to identify the drive */
405
466
const BYTE * buff , /* Data to be written */
406
467
DWORD sector , /* Sector address in LBA */
407
468
UINT count /* Number of sectors to write */
@@ -410,7 +471,7 @@ DRESULT disk_write (
410
471
switch (pdrv ) {
411
472
case MMC :
412
473
{
413
- size_t buf_size = count * emmc_dev . bd .block_size ;
474
+ size_t buf_size = count * emmc_dev -> bd .block_size ;
414
475
if (sd_write ((uint8_t * )buff , buf_size , sector ) < buf_size )
415
476
{
416
477
return RES_ERROR ;
@@ -422,13 +483,14 @@ DRESULT disk_write (
422
483
#ifdef HAVE_USPI
423
484
default :
424
485
{
425
- if (!USPiMassStorageDeviceAvailable ())
486
+ int nDeviceIndex = pdrv - USB ;
487
+ if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable ())
426
488
{
427
489
return RES_PARERR ;
428
490
}
429
491
430
492
unsigned buf_size = count * USPI_BLOCK_SIZE ;
431
- if (USPiMassStorageDeviceWrite (sector * USPI_BLOCK_SIZE , buff , buf_size , pdrv - USB ) < buf_size )
493
+ if (USPiMassStorageDeviceWrite (sector * USPI_BLOCK_SIZE , buff , buf_size , nDeviceIndex ) < buf_size )
432
494
{
433
495
return RES_ERROR ;
434
496
}
@@ -447,7 +509,7 @@ DRESULT disk_write (
447
509
/*-----------------------------------------------------------------------*/
448
510
449
511
DRESULT disk_ioctl (
450
- BYTE pdrv , /* Physical drive nmuber (0..) */
512
+ BYTE pdrv , /* Physical drive number (0..) */
451
513
BYTE cmd , /* Control code */
452
514
void * buff /* Buffer to send/receive control data */
453
515
)
@@ -460,24 +522,26 @@ DRESULT disk_ioctl (
460
522
}
461
523
if (cmd == GET_SECTOR_COUNT )
462
524
{
463
- * (DWORD * )buff = emmc_dev . bd .num_blocks ;
525
+ * (DWORD * )buff = emmc_dev -> bd .num_blocks ;
464
526
return RES_OK ;
465
527
}
466
528
if (cmd == GET_SECTOR_SIZE )
467
529
{
468
- * (DWORD * )buff = emmc_dev . bd .block_size ;
530
+ * (DWORD * )buff = emmc_dev -> bd .block_size ;
469
531
return RES_OK ;
470
532
}
471
533
if (cmd == GET_BLOCK_SIZE )
472
534
{
473
- * (DWORD * )buff = emmc_dev . bd .block_size ;
535
+ * (DWORD * )buff = emmc_dev -> bd .block_size ;
474
536
return RES_OK ;
475
537
}
476
538
return RES_PARERR ;
477
539
478
540
#ifdef HAVE_USPI
479
541
default :
480
- if (!USPiMassStorageDeviceAvailable ())
542
+ {
543
+ int nDeviceIndex = pdrv - USB ;
544
+ if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable ())
481
545
{
482
546
return RES_PARERR ;
483
547
}
@@ -487,7 +551,7 @@ DRESULT disk_ioctl (
487
551
}
488
552
if (cmd == GET_SECTOR_COUNT )
489
553
{
490
- * (DWORD * )buff = USPiMassStorageDeviceGetCapacity (pdrv - USB );
554
+ * (DWORD * )buff = USPiMassStorageDeviceGetCapacity (nDeviceIndex );
491
555
return RES_OK ;
492
556
}
493
557
if (cmd == GET_SECTOR_SIZE )
@@ -501,6 +565,7 @@ DRESULT disk_ioctl (
501
565
return RES_OK ;
502
566
}
503
567
return RES_PARERR ;
568
+ }
504
569
#endif
505
570
}
506
571
0 commit comments