dfs_elm.c 21.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * File      : dfs_elm.c
 * This file is part of Device File System in RT-Thread RTOS
 * COPYRIGHT (C) 2008-2011, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2008-02-22     QiuYi        The first version.
 * 2011-10-08     Bernard      fixed the block size in statfs.
B
bernard.xiong@gmail.com 已提交
14
 * 2011-11-23     Bernard      fixed the rename issue.
15
 * 2012-07-26     aozima       implement ff_memalloc and ff_memfree.
16
 * 2012-12-19     Bernard      fixed the O_APPEND and lseek issue.
17
 * 2013-03-01     aozima       fixed the stat(st_mtime) issue.
18
 */
P
prife 已提交
19

20 21 22
#include <rtthread.h>
#include "ffconf.h"
#include "ff.h"
23 24
#include <string.h>
#include <time.h>
25 26 27 28 29 30 31 32 33 34 35

/* ELM FatFs provide a DIR struct */
#define HAVE_DIR_STRUCTURE

#include <dfs_fs.h>
#include <dfs_def.h>

static rt_device_t disk[_VOLUMES] = {0};

static int elm_result_to_dfs(FRESULT result)
{
P
prife 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    int status = DFS_STATUS_OK;

    switch (result)
    {
    case FR_OK:
        break;

    case FR_NO_FILE:
    case FR_NO_PATH:
    case FR_NO_FILESYSTEM:
        status = -DFS_STATUS_ENOENT;
        break;

    case FR_INVALID_NAME:
        status = -DFS_STATUS_EINVAL;
        break;

    case FR_EXIST:
    case FR_INVALID_OBJECT:
        status = -DFS_STATUS_EEXIST;
        break;

    case FR_DISK_ERR:
    case FR_NOT_READY:
    case FR_INT_ERR:
        status = -DFS_STATUS_EIO;
        break;

    case FR_WRITE_PROTECTED:
    case FR_DENIED:
        status = -DFS_STATUS_EROFS;
        break;

    case FR_MKFS_ABORTED:
        status = -DFS_STATUS_EINVAL;
        break;

    default:
        status = -1;
        break;
    }

    return status;
79 80
}

81
/* results:
P
prife 已提交
82 83
 *  -1, no space to install fatfs driver
 *  >= 0, there is an space to install fatfs driver
84 85
 */
static int get_disk(rt_device_t id)
86
{
87
    int index;
88

P
prife 已提交
89 90 91
    for (index = 0; index < _VOLUMES; index ++)
    {
        if (disk[index] == id)
92
            return index;
P
prife 已提交
93 94
    }

95 96 97 98 99
    return -1;
}

int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
P
prife 已提交
100 101 102
    FATFS *fat;
    FRESULT result;
    int index;
103

P
prife 已提交
104 105 106 107
    /* get an empty position */
    index = get_disk(RT_NULL);
    if (index == -1)
        return -DFS_STATUS_ENOSPC;
108

P
prife 已提交
109 110
    /* save device */
    disk[index] = fs->dev_id;
111

P
prife 已提交
112 113 114
    fat = (FATFS *)rt_malloc(sizeof(FATFS));
    if (fat == RT_NULL)
    {
115
        disk[index] = RT_NULL;
P
prife 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128
        return -1;
    }

    /* mount fatfs, always 0 logic driver */
    result = f_mount((BYTE)index, fat);
    if (result == FR_OK)
    {
        char drive[8];
        DIR *dir;

        rt_snprintf(drive, sizeof(drive), "%d:/", index);
        dir = (DIR *)rt_malloc(sizeof(DIR));
        if (dir == RT_NULL)
129 130 131 132
        {
            f_mount((BYTE)index, RT_NULL);
            disk[index] = RT_NULL;
            rt_free(fat);
P
prife 已提交
133
            return -DFS_STATUS_ENOMEM;
134
        }
P
prife 已提交
135 136 137 138

        /* open the root directory to test whether the fatfs is valid */
        result = f_opendir(dir, drive);
        if (result != FR_OK)
139
            goto __err;
140

141
        /* mount succeed! */
P
prife 已提交
142 143 144 145
        fs->data = fat;
        rt_free(dir);
        return 0;
    }
146

147
__err:
148
    f_mount((BYTE)index, RT_NULL);
149
    disk[index] = RT_NULL;
P
prife 已提交
150 151
    rt_free(fat);
    return elm_result_to_dfs(result);
152 153
}

154
int dfs_elm_unmount(struct dfs_filesystem *fs)
155
{
P
prife 已提交
156 157 158
    FATFS *fat;
    FRESULT result;
    int  index;
159

P
prife 已提交
160
    fat = (FATFS *)fs->data;
161

P
prife 已提交
162
    RT_ASSERT(fat != RT_NULL);
163

P
prife 已提交
164
    /* find the device index and then umount it */
165 166
    index = get_disk(fs->dev_id);
    if (index == -1) /* not found */
P
prife 已提交
167
        return -DFS_STATUS_ENOENT;
168

P
prife 已提交
169 170 171
    result = f_mount((BYTE)index, RT_NULL);
    if (result != FR_OK)
        return elm_result_to_dfs(result);
172

P
prife 已提交
173 174 175
    fs->data = RT_NULL;
    disk[index] = RT_NULL;
    rt_free(fat);
176

P
prife 已提交
177
    return DFS_STATUS_OK;
178 179
}

180
int dfs_elm_mkfs(rt_device_t dev_id)
181
{
182 183
#define FSM_STATUS_INIT            0
#define FSM_STATUS_USE_TEMP_DRIVER 1
wuyangyong's avatar
wuyangyong 已提交
184
    FATFS *fat = RT_NULL;
185
    int flag;
P
prife 已提交
186
    FRESULT result;
187
    int index;
188

189 190
    if (dev_id == RT_NULL)
        return -DFS_STATUS_EINVAL;
191

P
prife 已提交
192
    /* if the device is already mounted, then just do mkfs to the drv,
193 194 195 196 197 198 199 200 201
     * while if it is not mounted yet, then find an empty drive to do mkfs
     */

    flag = FSM_STATUS_INIT;
    index = get_disk(dev_id);
    if (index == -1)
    {
        /* not found the device id */
        index = get_disk(RT_NULL);
P
prife 已提交
202 203
        if (index == -1)
        {
204
            /* no space to store an temp driver */
P
prife 已提交
205 206
            rt_kprintf("sorry, there is no space to do mkfs! \n");
            return -DFS_STATUS_ENOSPC;
207 208 209 210 211 212 213 214 215 216 217
        }
        else
        {
            fat = rt_malloc(sizeof(FATFS));
            if (fat == RT_NULL)
                return -DFS_STATUS_ENOMEM;

            flag = FSM_STATUS_USE_TEMP_DRIVER;

            disk[index] = dev_id;

P
prife 已提交
218 219 220 221
            /* just fill the FatFs[vol] in ff.c, or mkfs will failded!
             * consider this condition: you just umount the elm fat,
             * then the space in FatFs[index] is released, and now do mkfs
             * on the disk, you will get a failure. so we need f_mount here,
222 223 224 225 226 227
             * just fill the FatFS[index] in elm fatfs to make mkfs work.
             */
            f_mount((BYTE)index, fat);
        }
    }

P
prife 已提交
228 229 230
    /* 1: no partition table */
    /* 0: auto selection of cluster size */
    result = f_mkfs((BYTE)index, 1, 0);
231 232 233 234 235 236 237 238 239

    /* check flag status, we need clear the temp driver stored in disk[] */
    if (flag == FSM_STATUS_USE_TEMP_DRIVER)
    {
        rt_free(fat);
        f_mount((BYTE)index, RT_NULL);
        disk[index] = RT_NULL;
    }

P
prife 已提交
240 241 242 243 244
    if (result != FR_OK)
    {
        rt_kprintf("format error\n");
        return elm_result_to_dfs(result);
    }
245

P
prife 已提交
246
    return DFS_STATUS_OK;
247 248
}

249
int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
250
{
P
prife 已提交
251 252 253 254
    FATFS *f;
    FRESULT res;
    char driver[4];
    DWORD fre_clust, fre_sect, tot_sect;
255

P
prife 已提交
256 257
    RT_ASSERT(fs != RT_NULL);
    RT_ASSERT(buf != RT_NULL);
258

P
prife 已提交
259
    f = (FATFS *)fs->data;
260

P
prife 已提交
261 262 263 264
    rt_snprintf(driver, sizeof(driver), "%d:", f->drv);
    res = f_getfree(driver, &fre_clust, &f);
    if (res)
        return elm_result_to_dfs(res);
265

P
prife 已提交
266 267 268
    /* Get total sectors and free sectors */
    tot_sect = (f->n_fatent - 2) * f->csize;
    fre_sect = fre_clust * f->csize;
269

P
prife 已提交
270 271
    buf->f_bfree = fre_sect;
    buf->f_blocks = tot_sect;
272
#if _MAX_SS != 512
P
prife 已提交
273
    buf->f_bsize = f->ssize;
274 275 276 277
#else
    buf->f_bsize = 512;
#endif

P
prife 已提交
278
    return 0;
279 280
}

281
int dfs_elm_open(struct dfs_fd *file)
282
{
P
prife 已提交
283 284 285 286
    FIL *fd;
    BYTE mode;
    FRESULT result;
    char *drivers_fn;
287 288

#if (_VOLUMES > 1)
P
prife 已提交
289 290 291 292 293 294 295 296 297 298 299 300
    int vol;
    extern int elm_get_vol(FATFS * fat);

    /* add path for ELM FatFS driver support */
    vol = elm_get_vol((FATFS *)file->fs->data);
    if (vol < 0)
        return -DFS_STATUS_ENOENT;
    drivers_fn = rt_malloc(256);
    if (drivers_fn == RT_NULL)
        return -DFS_STATUS_ENOMEM;

    rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path);
301
#else
P
prife 已提交
302
    drivers_fn = file->path;
303 304
#endif

P
prife 已提交
305 306 307
    if (file->flags & DFS_O_DIRECTORY)
    {
        DIR *dir;
308

P
prife 已提交
309 310 311 312 313
        if (file->flags & DFS_O_CREAT)
        {
            result = f_mkdir(drivers_fn);
            if (result != FR_OK)
            {
314
#if _VOLUMES > 1
P
prife 已提交
315
                rt_free(drivers_fn);
316
#endif
P
prife 已提交
317 318 319 320 321 322 323 324
                return elm_result_to_dfs(result);
            }
        }

        /* open directory */
        dir = (DIR *)rt_malloc(sizeof(DIR));
        if (dir == RT_NULL)
        {
325
#if _VOLUMES > 1
P
prife 已提交
326
            rt_free(drivers_fn);
327
#endif
P
prife 已提交
328 329
            return -DFS_STATUS_ENOMEM;
        }
330

P
prife 已提交
331
        result = f_opendir(dir, drivers_fn);
332
#if _VOLUMES > 1
P
prife 已提交
333
        rt_free(drivers_fn);
334
#endif
P
prife 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
        if (result != FR_OK)
        {
            rt_free(dir);
            return elm_result_to_dfs(result);
        }

        file->data = dir;
        return DFS_STATUS_OK;
    }
    else
    {
        mode = FA_READ;

        if (file->flags & DFS_O_WRONLY)
            mode |= FA_WRITE;
        if ((file->flags & DFS_O_ACCMODE) & DFS_O_RDWR)
            mode |= FA_WRITE;
        /* Opens the file, if it is existing. If not, a new file is created. */
        if (file->flags & DFS_O_CREAT)
            mode |= FA_OPEN_ALWAYS;
        /* Creates a new file. If the file is existing, it is truncated and overwritten. */
        if (file->flags & DFS_O_TRUNC)
            mode |= FA_CREATE_ALWAYS;
        /* Creates a new file. The function fails if the file is already existing. */
        if (file->flags & DFS_O_EXCL)
            mode |= FA_CREATE_NEW;

        /* allocate a fd */
        fd = (FIL *)rt_malloc(sizeof(FIL));
        if (fd == RT_NULL)
        {
366
#if _VOLUMES > 1
P
prife 已提交
367
            rt_free(drivers_fn);
368
#endif
P
prife 已提交
369 370
            return -DFS_STATUS_ENOMEM;
        }
371

P
prife 已提交
372
        result = f_open(fd, drivers_fn, mode);
373
#if _VOLUMES > 1
P
prife 已提交
374
        rt_free(drivers_fn);
375
#endif
P
prife 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        if (result == FR_OK)
        {
            file->pos  = fd->fptr;
            file->size = fd->fsize;
            file->data = fd;

            if (file->flags & DFS_O_APPEND)
            {
                /* seek to the end of file */
                f_lseek(fd, fd->fsize);
                file->pos = fd->fptr;
            }
        }
        else
        {
            /* open failed, return */
            rt_free(fd);
            return elm_result_to_dfs(result);
        }
    }

    return DFS_STATUS_OK;
398 399
}

400
int dfs_elm_close(struct dfs_fd *file)
401
{
P
prife 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    FRESULT result;

    result = FR_OK;
    if (file->type == FT_DIRECTORY)
    {
        DIR *dir;

        dir = (DIR *)(file->data);
        RT_ASSERT(dir != RT_NULL);

        /* release memory */
        rt_free(dir);
    }
    else if (file->type == FT_REGULAR)
    {
        FIL *fd;
        fd = (FIL *)(file->data);
        RT_ASSERT(fd != RT_NULL);

        result = f_close(fd);
        if (result == FR_OK)
        {
            /* release memory */
            rt_free(fd);
        }
    }

    return elm_result_to_dfs(result);
430 431
}

P
prife 已提交
432
int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args)
433
{
P
prife 已提交
434
    return -DFS_STATUS_ENOSYS;
435 436
}

437
int dfs_elm_read(struct dfs_fd *file, void *buf, rt_size_t len)
438
{
P
prife 已提交
439 440 441
    FIL *fd;
    FRESULT result;
    UINT byte_read;
442

P
prife 已提交
443 444 445 446
    if (file->type == FT_DIRECTORY)
    {
        return -DFS_STATUS_EISDIR;
    }
447

P
prife 已提交
448 449
    fd = (FIL *)(file->data);
    RT_ASSERT(fd != RT_NULL);
450

P
prife 已提交
451 452 453 454 455
    result = f_read(fd, buf, len, &byte_read);
    /* update position */
    file->pos  = fd->fptr;
    if (result == FR_OK)
        return byte_read;
456

P
prife 已提交
457
    return elm_result_to_dfs(result);
458 459
}

460
int dfs_elm_write(struct dfs_fd *file, const void *buf, rt_size_t len)
461
{
P
prife 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    FIL *fd;
    FRESULT result;
    UINT byte_write;

    if (file->type == FT_DIRECTORY)
    {
        return -DFS_STATUS_EISDIR;
    }

    fd = (FIL *)(file->data);
    RT_ASSERT(fd != RT_NULL);

    result = f_write(fd, buf, len, &byte_write);
    /* update position and file size */
    file->pos  = fd->fptr;
    file->size = fd->fsize;
    if (result == FR_OK)
        return byte_write;

    return elm_result_to_dfs(result);
482 483
}

484
int dfs_elm_flush(struct dfs_fd *file)
485
{
P
prife 已提交
486 487
    FIL *fd;
    FRESULT result;
488

P
prife 已提交
489 490
    fd = (FIL *)(file->data);
    RT_ASSERT(fd != RT_NULL);
491

P
prife 已提交
492 493
    result = f_sync(fd);
    return elm_result_to_dfs(result);
494 495
}

496
int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
497
{
P
prife 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    FRESULT result = FR_OK;
    if (file->type == FT_REGULAR)
    {
        FIL *fd;

        /* regular file type */
        fd = (FIL *)(file->data);
        RT_ASSERT(fd != RT_NULL);

        result = f_lseek(fd, offset);
        if (result == FR_OK)
        {
            /* return current position */
            file->pos = fd->fptr;
            return fd->fptr;
        }
    }
    else if (file->type == FT_DIRECTORY)
    {
        /* which is a directory */
        DIR *dir;

        dir = (DIR *)(file->data);
        RT_ASSERT(dir != RT_NULL);

        result = f_seekdir(dir, offset / sizeof(struct dirent));
        if (result == FR_OK)
        {
            /* update file position */
            file->pos = offset;
            return file->pos;
        }
    }

    return elm_result_to_dfs(result);
533 534
}

535
int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
536
{
P
prife 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549
    DIR *dir;
    FILINFO fno;
    FRESULT result;
    rt_uint32_t index;
    struct dirent *d;

    dir = (DIR *)(file->data);
    RT_ASSERT(dir != RT_NULL);

    /* make integer count */
    count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
    if (count == 0)
        return -DFS_STATUS_EINVAL;
550 551

#if _USE_LFN
P
prife 已提交
552 553 554
    /* allocate long file name */
    fno.lfname = rt_malloc(256);
    fno.lfsize = 256;
555 556
#endif

P
prife 已提交
557 558 559 560
    index = 0;
    while (1)
    {
        char *fn;
561

P
prife 已提交
562
        d = dirp + index;
563

P
prife 已提交
564 565 566
        result = f_readdir(dir, &fno);
        if (result != FR_OK || fno.fname[0] == 0)
            break;
567 568

#if _USE_LFN
P
prife 已提交
569
        fn = *fno.lfname ? fno.lfname : fno.fname;
570
#else
P
prife 已提交
571
        fn = fno.fname;
572 573
#endif

P
prife 已提交
574 575 576 577 578
        d->d_type = DFS_DT_UNKNOWN;
        if (fno.fattrib & AM_DIR)
            d->d_type = DFS_DT_DIR;
        else
            d->d_type = DFS_DT_REG;
579

P
prife 已提交
580 581 582
        d->d_namlen = (rt_uint8_t)rt_strlen(fn);
        d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
        rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);
583

P
prife 已提交
584 585 586 587
        index ++;
        if (index * sizeof(struct dirent) >= count)
            break;
    }
588 589

#if _USE_LFN
P
prife 已提交
590
    rt_free(fno.lfname);
591 592
#endif

P
prife 已提交
593 594
    if (index == 0)
        return elm_result_to_dfs(result);
595

P
prife 已提交
596
    file->pos += index * sizeof(struct dirent);
597

P
prife 已提交
598
    return index * sizeof(struct dirent);
599 600
}

601
int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
602
{
P
prife 已提交
603
    FRESULT result;
604 605

#if _VOLUMES > 1
P
prife 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618
    int vol;
    char *drivers_fn;
    extern int elm_get_vol(FATFS * fat);

    /* add path for ELM FatFS driver support */
    vol = elm_get_vol((FATFS *)fs->data);
    if (vol < 0)
        return -DFS_STATUS_ENOENT;
    drivers_fn = rt_malloc(256);
    if (drivers_fn == RT_NULL)
        return -DFS_STATUS_ENOMEM;

    rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
619
#else
P
prife 已提交
620 621
    const char *drivers_fn;
    drivers_fn = path;
622 623
#endif

P
prife 已提交
624
    result = f_unlink(drivers_fn);
625
#if _VOLUMES > 1
P
prife 已提交
626
    rt_free(drivers_fn);
627
#endif
P
prife 已提交
628
    return elm_result_to_dfs(result);
629 630
}

631
int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
632
{
P
prife 已提交
633
    FRESULT result;
634 635

#if _VOLUMES > 1
P
prife 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    char *drivers_oldfn;
    const char *drivers_newfn;
    int vol;
    extern int elm_get_vol(FATFS * fat);

    /* add path for ELM FatFS driver support */
    vol = elm_get_vol((FATFS *)fs->data);
    if (vol < 0)
        return -DFS_STATUS_ENOENT;

    drivers_oldfn = rt_malloc(256);
    if (drivers_oldfn == RT_NULL)
        return -DFS_STATUS_ENOMEM;
    drivers_newfn = newpath;

    rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath);
652
#else
P
prife 已提交
653
    const char *drivers_oldfn, *drivers_newfn;
654

P
prife 已提交
655 656
    drivers_oldfn = oldpath;
    drivers_newfn = newpath;
657 658
#endif

P
prife 已提交
659
    result = f_rename(drivers_oldfn, drivers_newfn);
660
#if _VOLUMES > 1
P
prife 已提交
661
    rt_free(drivers_oldfn);
662
#endif
P
prife 已提交
663
    return elm_result_to_dfs(result);
664 665
}

666
int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
667
{
P
prife 已提交
668 669
    FILINFO file_info;
    FRESULT result;
670 671

#if _VOLUMES > 1
P
prife 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684
    int vol;
    char *drivers_fn;
    extern int elm_get_vol(FATFS * fat);

    /* add path for ELM FatFS driver support */
    vol = elm_get_vol((FATFS *)fs->data);
    if (vol < 0)
        return -DFS_STATUS_ENOENT;
    drivers_fn = rt_malloc(256);
    if (drivers_fn == RT_NULL)
        return -DFS_STATUS_ENOMEM;

    rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
685
#else
P
prife 已提交
686 687
    const char *drivers_fn;
    drivers_fn = path;
688 689 690
#endif

#if _USE_LFN
P
prife 已提交
691 692 693
    /* allocate long file name */
    file_info.lfname = rt_malloc(256);
    file_info.lfsize = 256;
694 695
#endif

P
prife 已提交
696
    result = f_stat(drivers_fn, &file_info);
697
#if _VOLUMES > 1
P
prife 已提交
698
    rt_free(drivers_fn);
699
#endif
P
prife 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    if (result == FR_OK)
    {
        /* convert to dfs stat structure */
        st->st_dev = 0;

        st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
                      DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
        if (file_info.fattrib & AM_DIR)
        {
            st->st_mode &= ~DFS_S_IFREG;
            st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
        }
        if (file_info.fattrib & AM_RDO)
            st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH);

        st->st_size  = file_info.fsize;
        st->st_blksize = 512;
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

        /* get st_mtime. */
        {
            struct tm tm_file;
            int year, mon, day, hour, min, sec;
            WORD tmp;

            tmp = file_info.fdate;
            day = tmp & 0x1F;           /* bit[4:0] Day(1..31) */
            tmp >>= 5;
            mon = tmp & 0x0F;           /* bit[8:5] Month(1..12) */
            tmp >>= 4;
            year = (tmp & 0x7F) + 1980; /* bit[15:9] Year origin from 1980(0..127) */

            tmp = file_info.ftime;
            sec = (tmp & 0x1F) * 2;     /* bit[4:0] Second/2(0..29) */
            tmp >>= 5;
            min = tmp & 0x3F;           /* bit[10:5] Minute(0..59) */
            tmp >>= 6;
            hour = tmp & 0x1F;          /* bit[15:11] Hour(0..23) */

            memset(&tm_file, 0, sizeof(tm_file));
            tm_file.tm_year = year - 1900; /* Years since 1900 */
            tm_file.tm_mon  = mon - 1;     /* Months *since* january: 0-11 */
            tm_file.tm_mday = day;         /* Day of the month: 1-31 */
            tm_file.tm_hour = hour;        /* Hours since midnight: 0-23 */
            tm_file.tm_min  = min;         /* Minutes: 0-59 */
            tm_file.tm_sec  = sec;         /* Seconds: 0-59 */

            st->st_mtime = mktime(&tm_file);
        } /* get st_mtime. */
P
prife 已提交
748
    }
749 750

#if _USE_LFN
P
prife 已提交
751
    rt_free(file_info.lfname);
752 753
#endif

P
prife 已提交
754
    return elm_result_to_dfs(result);
755 756 757 758
}

static const struct dfs_filesystem_operation dfs_elm =
{
P
prife 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
    "elm",
    DFS_FS_FLAG_DEFAULT,
    dfs_elm_mount,
    dfs_elm_unmount,
    dfs_elm_mkfs,
    dfs_elm_statfs,

    dfs_elm_open,
    dfs_elm_close,
    dfs_elm_ioctl,
    dfs_elm_read,
    dfs_elm_write,
    dfs_elm_flush,
    dfs_elm_lseek,
    dfs_elm_getdents,
    dfs_elm_unlink,
    dfs_elm_stat,
    dfs_elm_rename,
777 778 779 780 781 782 783
};

int elm_init(void)
{
    /* register fatfs file system */
    dfs_register(&dfs_elm);

P
prife 已提交
784
    return 0;
785
}
B
Bernard Xiong 已提交
786
INIT_FS_EXPORT(elm_init);
787 788 789 790 791 792

/*
 * RT-Thread Device Interface for ELM FatFs
 */
#include "diskio.h"

793
/* Initialize a Drive */
794
DSTATUS disk_initialize(BYTE drv)
795
{
P
prife 已提交
796
    return 0;
797 798 799
}

/* Return Disk Status */
800
DSTATUS disk_status(BYTE drv)
801
{
P
prife 已提交
802
    return 0;
803 804 805
}

/* Read Sector(s) */
806
DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
807
{
P
prife 已提交
808 809
    rt_size_t result;
    rt_device_t device = disk[drv];
810

P
prife 已提交
811 812 813 814 815
    result = rt_device_read(device, sector, buff, count);
    if (result == count)
    {
        return RES_OK;
    }
816

P
prife 已提交
817
    return RES_ERROR;
818 819 820
}

/* Write Sector(s) */
821
DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
822
{
P
prife 已提交
823 824
    rt_size_t result;
    rt_device_t device = disk[drv];
825

P
prife 已提交
826 827 828 829 830
    result = rt_device_write(device, sector, buff, count);
    if (result == count)
    {
        return RES_OK;
    }
831

P
prife 已提交
832
    return RES_ERROR;
833 834 835
}

/* Miscellaneous Functions */
836
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
837
{
P
prife 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
    rt_device_t device = disk[drv];

    if (device == RT_NULL)
        return RES_ERROR;

    if (ctrl == GET_SECTOR_COUNT)
    {
        struct rt_device_blk_geometry geometry;

        rt_memset(&geometry, 0, sizeof(geometry));
        rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);

        *(DWORD *)buff = geometry.sector_count;
        if (geometry.sector_count == 0)
            return RES_ERROR;
    }
    else if (ctrl == GET_SECTOR_SIZE)
    {
        struct rt_device_blk_geometry geometry;

        rt_memset(&geometry, 0, sizeof(geometry));
        rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);

        *(WORD *)buff = (WORD)(geometry.bytes_per_sector);
    }
    else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) */
    {
        struct rt_device_blk_geometry geometry;

        rt_memset(&geometry, 0, sizeof(geometry));
        rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);

        *(DWORD *)buff = geometry.block_size / geometry.bytes_per_sector;
    }
    else if (ctrl == CTRL_SYNC)
    {
        rt_device_control(device, RT_DEVICE_CTRL_BLK_SYNC, RT_NULL);
    }
    else if (ctrl == CTRL_ERASE_SECTOR)
    {
        rt_device_control(device, RT_DEVICE_CTRL_BLK_ERASE, buff);
    }

    return RES_OK;
882 883
}

884
rt_time_t get_fattime(void)
885
{
P
prife 已提交
886
    return 0;
887 888 889
}

#if _FS_REENTRANT
890
int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
{
    char name[8];
    rt_mutex_t mutex;

    rt_snprintf(name, sizeof(name), "fat%d", drv);
    mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
    if (mutex != RT_NULL)
    {
        *m = mutex;
        return RT_TRUE;
    }

    return RT_FALSE;
}

int ff_del_syncobj(_SYNC_t m)
{
908 909
    if (m != RT_NULL)
        rt_mutex_delete(m);
910 911 912 913 914 915

    return RT_TRUE;
}

int ff_req_grant(_SYNC_t m)
{
916
    if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
P
prife 已提交
917
        return RT_TRUE;
918 919 920 921 922 923

    return RT_FALSE;
}

void ff_rel_grant(_SYNC_t m)
{
P
prife 已提交
924
    rt_mutex_release(m);
925 926 927
}

#endif
928 929 930 931

/* Memory functions */
#if _USE_LFN == 3
/* Allocate memory block */
P
prife 已提交
932
void *ff_memalloc(UINT size)
933 934 935 936 937
{
    return rt_malloc(size);
}

/* Free memory block */
P
prife 已提交
938
void ff_memfree(void *mem)
939 940 941 942 943
{
    rt_free(mem);
}
#endif /* _USE_LFN == 3 */