dfs_elm.c 23.1 KB
Newer Older
1 2 3 4 5
/*
 * File      : dfs_elm.c
 * This file is part of Device File System in RT-Thread RTOS
 * COPYRIGHT (C) 2008-2011, RT-Thread Development Team
 *
Y
yiyue.fang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22 23
 *
 * 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 已提交
24
 * 2011-11-23     Bernard      fixed the rename issue.
25
 * 2012-07-26     aozima       implement ff_memalloc and ff_memfree.
26
 * 2012-12-19     Bernard      fixed the O_APPEND and lseek issue.
27
 * 2013-03-01     aozima       fixed the stat(st_mtime) issue.
28
 * 2014-01-26     Bernard      Check the sector size before mount.
U
hichard  
unknown 已提交
29
 * 2017-02-13     Hichard      Update Fatfs version to 0.12b, support exFAT.
30
 * 2017-04-11     Bernard      fix the st_blksize issue.
U
Urey 已提交
31
 * 2017-05-26     Urey         fix f_mount error when mount more fats
32
 */
P
prife 已提交
33

34 35 36
#include <rtthread.h>
#include "ffconf.h"
#include "ff.h"
37 38
#include <string.h>
#include <time.h>
39 40 41 42 43 44 45

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

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

U
Urey 已提交
46
volatile static rt_device_t disk[_VOLUMES] = {0};
47 48 49

static int elm_result_to_dfs(FRESULT result)
{
P
prife 已提交
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    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;
93 94
}

95
/* results:
P
prife 已提交
96 97
 *  -1, no space to install fatfs driver
 *  >= 0, there is an space to install fatfs driver
98 99
 */
static int get_disk(rt_device_t id)
100
{
101
    int index;
102

P
prife 已提交
103 104 105
    for (index = 0; index < _VOLUMES; index ++)
    {
        if (disk[index] == id)
106
            return index;
P
prife 已提交
107 108
    }

109 110 111 112 113
    return -1;
}

int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
P
prife 已提交
114 115 116
    FATFS *fat;
    FRESULT result;
    int index;
117
    struct rt_device_blk_geometry geometry;
U
Urey 已提交
118
    char logic_nbr[2] = {'0',':'};
119

P
prife 已提交
120 121 122
    /* get an empty position */
    index = get_disk(RT_NULL);
    if (index == -1)
123
        return -DFS_STATUS_ENOENT;
U
Urey 已提交
124
    logic_nbr[0] = '0' + index;
125

P
prife 已提交
126 127
    /* save device */
    disk[index] = fs->dev_id;
128 129 130 131 132 133 134 135 136 137
    /* check sector size */
    if (rt_device_control(fs->dev_id, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry) == RT_EOK)
    {
        if (geometry.bytes_per_sector > _MAX_SS)
        {
            rt_kprintf("The sector size of device is greater than the sector size of FAT.\n");
            return -DFS_STATUS_EINVAL;
        }
    }

P
prife 已提交
138 139 140
    fat = (FATFS *)rt_malloc(sizeof(FATFS));
    if (fat == RT_NULL)
    {
141
        disk[index] = RT_NULL;
142
        return -DFS_STATUS_ENOMEM;
P
prife 已提交
143 144 145
    }

    /* mount fatfs, always 0 logic driver */
U
Urey 已提交
146
    result = f_mount(fat,(const TCHAR*)logic_nbr, 1);
P
prife 已提交
147 148 149 150 151 152 153 154
    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)
155
        {
U
Urey 已提交
156
            f_mount(RT_NULL,(const TCHAR*)logic_nbr,1);
157 158
            disk[index] = RT_NULL;
            rt_free(fat);
P
prife 已提交
159
            return -DFS_STATUS_ENOMEM;
160
        }
P
prife 已提交
161 162 163 164

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

167
        /* mount succeed! */
P
prife 已提交
168 169 170 171
        fs->data = fat;
        rt_free(dir);
        return 0;
    }
172

173
__err:
U
Urey 已提交
174
    f_mount(RT_NULL, (const TCHAR*)logic_nbr, 1);
175
    disk[index] = RT_NULL;
P
prife 已提交
176 177
    rt_free(fat);
    return elm_result_to_dfs(result);
178 179
}

180
int dfs_elm_unmount(struct dfs_filesystem *fs)
181
{
P
prife 已提交
182 183 184
    FATFS *fat;
    FRESULT result;
    int  index;
185

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

P
prife 已提交
188
    RT_ASSERT(fat != RT_NULL);
189

P
prife 已提交
190
    /* find the device index and then umount it */
191 192
    index = get_disk(fs->dev_id);
    if (index == -1) /* not found */
P
prife 已提交
193
        return -DFS_STATUS_ENOENT;
194

U
hichard  
unknown 已提交
195
    result = f_mount(RT_NULL, "", (BYTE)index);
P
prife 已提交
196 197
    if (result != FR_OK)
        return elm_result_to_dfs(result);
198

P
prife 已提交
199 200 201
    fs->data = RT_NULL;
    disk[index] = RT_NULL;
    rt_free(fat);
202

P
prife 已提交
203
    return DFS_STATUS_OK;
204 205
}

206
int dfs_elm_mkfs(rt_device_t dev_id)
207
{
208 209
#define FSM_STATUS_INIT            0
#define FSM_STATUS_USE_TEMP_DRIVER 1
wuyangyong's avatar
wuyangyong 已提交
210
    FATFS *fat = RT_NULL;
U
hichard  
unknown 已提交
211
    BYTE *work;
212
    int flag;
P
prife 已提交
213
    FRESULT result;
214
    int index;
215

U
hichard  
unknown 已提交
216 217 218 219
    work = rt_malloc(_MAX_SS);
    if(RT_NULL == work) {
        return -DFS_STATUS_ENOMEM;
    }
220

221 222
    if (dev_id == RT_NULL)
        return -DFS_STATUS_EINVAL;
223

P
prife 已提交
224
    /* if the device is already mounted, then just do mkfs to the drv,
225 226 227 228 229 230 231 232 233
     * 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 已提交
234 235
        if (index == -1)
        {
236
            /* no space to store an temp driver */
P
prife 已提交
237 238
            rt_kprintf("sorry, there is no space to do mkfs! \n");
            return -DFS_STATUS_ENOSPC;
239 240 241 242 243 244 245 246 247 248
        }
        else
        {
            fat = rt_malloc(sizeof(FATFS));
            if (fat == RT_NULL)
                return -DFS_STATUS_ENOMEM;

            flag = FSM_STATUS_USE_TEMP_DRIVER;

            disk[index] = dev_id;
B
bernard 已提交
249 250
            /* try to open device */
            rt_device_open(dev_id, RT_DEVICE_OFLAG_RDWR);
251

P
prife 已提交
252 253 254 255
            /* 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,
256 257
             * just fill the FatFS[index] in elm fatfs to make mkfs work.
             */
U
hichard  
unknown 已提交
258
            f_mount(fat, "", (BYTE)index);
259 260 261
        }
    }

U
hichard  
unknown 已提交
262 263 264 265 266 267 268
    /* [IN] Logical drive number */
    /* [IN] Format options */
    /* [IN] Size of the allocation unit */
    /* [-]  Working buffer */
    /* [IN] Size of working buffer */
    result = f_mkfs("", FM_ANY, 0, work, _MAX_SS);
    rt_free(work);
269 270 271 272 273

    /* check flag status, we need clear the temp driver stored in disk[] */
    if (flag == FSM_STATUS_USE_TEMP_DRIVER)
    {
        rt_free(fat);
U
hichard  
unknown 已提交
274
        f_mount(RT_NULL, "",(BYTE)index);
275
        disk[index] = RT_NULL;
B
bernard 已提交
276 277
        /* close device */
        rt_device_close(dev_id);
278 279
    }

P
prife 已提交
280 281 282 283 284
    if (result != FR_OK)
    {
        rt_kprintf("format error\n");
        return elm_result_to_dfs(result);
    }
285

P
prife 已提交
286
    return DFS_STATUS_OK;
287 288
}

289
int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
290
{
P
prife 已提交
291 292 293 294
    FATFS *f;
    FRESULT res;
    char driver[4];
    DWORD fre_clust, fre_sect, tot_sect;
295

P
prife 已提交
296 297
    RT_ASSERT(fs != RT_NULL);
    RT_ASSERT(buf != RT_NULL);
298

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

P
prife 已提交
301 302 303 304
    rt_snprintf(driver, sizeof(driver), "%d:", f->drv);
    res = f_getfree(driver, &fre_clust, &f);
    if (res)
        return elm_result_to_dfs(res);
305

P
prife 已提交
306 307 308
    /* Get total sectors and free sectors */
    tot_sect = (f->n_fatent - 2) * f->csize;
    fre_sect = fre_clust * f->csize;
309

P
prife 已提交
310 311
    buf->f_bfree = fre_sect;
    buf->f_blocks = tot_sect;
312
#if _MAX_SS != 512
P
prife 已提交
313
    buf->f_bsize = f->ssize;
314 315 316 317
#else
    buf->f_bsize = 512;
#endif

P
prife 已提交
318
    return 0;
319 320
}

321
int dfs_elm_open(struct dfs_fd *file)
322
{
P
prife 已提交
323 324 325 326
    FIL *fd;
    BYTE mode;
    FRESULT result;
    char *drivers_fn;
327 328

#if (_VOLUMES > 1)
P
prife 已提交
329 330 331 332 333 334 335 336 337 338 339 340
    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);
341
#else
P
prife 已提交
342
    drivers_fn = file->path;
343 344
#endif

P
prife 已提交
345 346 347
    if (file->flags & DFS_O_DIRECTORY)
    {
        DIR *dir;
348

P
prife 已提交
349 350 351 352 353
        if (file->flags & DFS_O_CREAT)
        {
            result = f_mkdir(drivers_fn);
            if (result != FR_OK)
            {
354
#if _VOLUMES > 1
P
prife 已提交
355
                rt_free(drivers_fn);
356
#endif
P
prife 已提交
357 358 359 360 361 362 363 364
                return elm_result_to_dfs(result);
            }
        }

        /* open directory */
        dir = (DIR *)rt_malloc(sizeof(DIR));
        if (dir == RT_NULL)
        {
365
#if _VOLUMES > 1
P
prife 已提交
366
            rt_free(drivers_fn);
367
#endif
P
prife 已提交
368 369
            return -DFS_STATUS_ENOMEM;
        }
370

P
prife 已提交
371
        result = f_opendir(dir, drivers_fn);
372
#if _VOLUMES > 1
P
prife 已提交
373
        rt_free(drivers_fn);
374
#endif
P
prife 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
        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)
        {
406
#if _VOLUMES > 1
P
prife 已提交
407
            rt_free(drivers_fn);
408
#endif
P
prife 已提交
409 410
            return -DFS_STATUS_ENOMEM;
        }
411

P
prife 已提交
412
        result = f_open(fd, drivers_fn, mode);
413
#if _VOLUMES > 1
P
prife 已提交
414
        rt_free(drivers_fn);
415
#endif
P
prife 已提交
416 417 418
        if (result == FR_OK)
        {
            file->pos  = fd->fptr;
U
hichard  
unknown 已提交
419
            file->size = f_size(fd);
P
prife 已提交
420 421 422 423 424
            file->data = fd;

            if (file->flags & DFS_O_APPEND)
            {
                /* seek to the end of file */
U
hichard  
unknown 已提交
425
                f_lseek(fd, f_size(fd));
P
prife 已提交
426 427 428 429 430 431 432 433 434 435 436 437
                file->pos = fd->fptr;
            }
        }
        else
        {
            /* open failed, return */
            rt_free(fd);
            return elm_result_to_dfs(result);
        }
    }

    return DFS_STATUS_OK;
438 439
}

440
int dfs_elm_close(struct dfs_fd *file)
441
{
P
prife 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    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);
470 471
}

P
prife 已提交
472
int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args)
473
{
P
prife 已提交
474
    return -DFS_STATUS_ENOSYS;
475 476
}

477
int dfs_elm_read(struct dfs_fd *file, void *buf, rt_size_t len)
478
{
P
prife 已提交
479 480 481
    FIL *fd;
    FRESULT result;
    UINT byte_read;
482

P
prife 已提交
483 484 485 486
    if (file->type == FT_DIRECTORY)
    {
        return -DFS_STATUS_EISDIR;
    }
487

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

P
prife 已提交
491 492 493 494 495
    result = f_read(fd, buf, len, &byte_read);
    /* update position */
    file->pos  = fd->fptr;
    if (result == FR_OK)
        return byte_read;
496

P
prife 已提交
497
    return elm_result_to_dfs(result);
498 499
}

500
int dfs_elm_write(struct dfs_fd *file, const void *buf, rt_size_t len)
501
{
P
prife 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    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;
U
hichard  
unknown 已提交
517
    file->size = f_size(fd);
P
prife 已提交
518 519 520 521
    if (result == FR_OK)
        return byte_write;

    return elm_result_to_dfs(result);
522 523
}

524
int dfs_elm_flush(struct dfs_fd *file)
525
{
P
prife 已提交
526 527
    FIL *fd;
    FRESULT result;
528

P
prife 已提交
529 530
    fd = (FIL *)(file->data);
    RT_ASSERT(fd != RT_NULL);
531

P
prife 已提交
532 533
    result = f_sync(fd);
    return elm_result_to_dfs(result);
534 535
}

536
int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
537
{
P
prife 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    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);
573 574
}

575
int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
576
{
P
prife 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589
    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;
590

P
prife 已提交
591 592 593 594
    index = 0;
    while (1)
    {
        char *fn;
595

P
prife 已提交
596
        d = dirp + index;
597

P
prife 已提交
598 599 600
        result = f_readdir(dir, &fno);
        if (result != FR_OK || fno.fname[0] == 0)
            break;
601 602

#if _USE_LFN
U
hichard  
unknown 已提交
603
        fn = *fno.fname ? fno.fname : fno.altname;
604
#else
P
prife 已提交
605
        fn = fno.fname;
606 607
#endif

P
prife 已提交
608 609 610 611 612
        d->d_type = DFS_DT_UNKNOWN;
        if (fno.fattrib & AM_DIR)
            d->d_type = DFS_DT_DIR;
        else
            d->d_type = DFS_DT_REG;
613

P
prife 已提交
614 615 616
        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);
617

P
prife 已提交
618 619 620 621
        index ++;
        if (index * sizeof(struct dirent) >= count)
            break;
    }
622

P
prife 已提交
623 624
    if (index == 0)
        return elm_result_to_dfs(result);
625

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

P
prife 已提交
628
    return index * sizeof(struct dirent);
629 630
}

631
int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
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
    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);
649
#else
P
prife 已提交
650 651
    const char *drivers_fn;
    drivers_fn = path;
652 653
#endif

P
prife 已提交
654
    result = f_unlink(drivers_fn);
655
#if _VOLUMES > 1
P
prife 已提交
656
    rt_free(drivers_fn);
657
#endif
P
prife 已提交
658
    return elm_result_to_dfs(result);
659 660
}

661
int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
662
{
P
prife 已提交
663
    FRESULT result;
664 665

#if _VOLUMES > 1
P
prife 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    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);
682
#else
P
prife 已提交
683
    const char *drivers_oldfn, *drivers_newfn;
684

P
prife 已提交
685 686
    drivers_oldfn = oldpath;
    drivers_newfn = newpath;
687 688
#endif

P
prife 已提交
689
    result = f_rename(drivers_oldfn, drivers_newfn);
690
#if _VOLUMES > 1
P
prife 已提交
691
    rt_free(drivers_oldfn);
692
#endif
P
prife 已提交
693
    return elm_result_to_dfs(result);
694 695
}

696
int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
697
{
P
prife 已提交
698 699
    FILINFO file_info;
    FRESULT result;
700 701

#if _VOLUMES > 1
P
prife 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714
    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);
715
#else
P
prife 已提交
716 717
    const char *drivers_fn;
    drivers_fn = path;
718 719
#endif

P
prife 已提交
720
    result = f_stat(drivers_fn, &file_info);
721
#if _VOLUMES > 1
P
prife 已提交
722
    rt_free(drivers_fn);
723
#endif
P
prife 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    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;
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

        /* 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 已提交
771
    }
772

P
prife 已提交
773
    return elm_result_to_dfs(result);
774 775 776 777
}

static const struct dfs_filesystem_operation dfs_elm =
{
P
prife 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    "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,
796 797 798 799 800 801 802
};

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

P
prife 已提交
803
    return 0;
804
}
B
Bernard Xiong 已提交
805
INIT_FS_EXPORT(elm_init);
806 807 808 809 810 811

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

812
/* Initialize a Drive */
813
DSTATUS disk_initialize(BYTE drv)
814
{
P
prife 已提交
815
    return 0;
816 817 818
}

/* Return Disk Status */
819
DSTATUS disk_status(BYTE drv)
820
{
P
prife 已提交
821
    return 0;
822 823 824
}

/* Read Sector(s) */
U
hichard  
unknown 已提交
825
DRESULT disk_read (BYTE drv, BYTE* buff, DWORD sector, UINT count)
826
{
P
prife 已提交
827 828
    rt_size_t result;
    rt_device_t device = disk[drv];
829

P
prife 已提交
830 831 832 833 834
    result = rt_device_read(device, sector, buff, count);
    if (result == count)
    {
        return RES_OK;
    }
835

P
prife 已提交
836
    return RES_ERROR;
837 838 839
}

/* Write Sector(s) */
U
hichard  
unknown 已提交
840
DRESULT disk_write (BYTE drv, const BYTE* buff, DWORD sector, UINT count)
841
{
P
prife 已提交
842 843
    rt_size_t result;
    rt_device_t device = disk[drv];
844

P
prife 已提交
845 846 847 848 849
    result = rt_device_write(device, sector, buff, count);
    if (result == count)
    {
        return RES_OK;
    }
850

P
prife 已提交
851
    return RES_ERROR;
852 853 854
}

/* Miscellaneous Functions */
855
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
856
{
P
prife 已提交
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 882 883 884 885 886 887 888 889 890 891 892 893 894
    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);
    }
U
hichard  
unknown 已提交
895
    else if (ctrl == CTRL_TRIM)
P
prife 已提交
896 897 898 899 900
    {
        rt_device_control(device, RT_DEVICE_CTRL_BLK_ERASE, buff);
    }

    return RES_OK;
901 902
}

903
DWORD get_fattime(void)
904
{
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
    time_t now;
    struct tm *p_tm;
    struct tm tm_now;
    DWORD fat_time;

    /* get current time */
    now = time(RT_NULL);

    /* lock scheduler. */
    rt_enter_critical();
    /* converts calendar time time into local time. */
    p_tm = localtime(&now);
    /* copy the statically located variable */
    memcpy(&tm_now, p_tm, sizeof(struct tm));
    /* unlock scheduler. */
    rt_exit_critical();

    fat_time =  (DWORD)(tm_now.tm_year - 80) << 25 | 
                (DWORD)(tm_now.tm_mon + 1)   << 21 | 
                (DWORD)tm_now.tm_mday        << 16 |
                (DWORD)tm_now.tm_hour        << 11 |
                (DWORD)tm_now.tm_min         <<  5 |
                (DWORD)tm_now.tm_sec / 2 ;

    return fat_time;
930 931 932
}

#if _FS_REENTRANT
933
int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
{
    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)
{
951 952
    if (m != RT_NULL)
        rt_mutex_delete(m);
953 954 955 956 957 958

    return RT_TRUE;
}

int ff_req_grant(_SYNC_t m)
{
959
    if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
P
prife 已提交
960
        return RT_TRUE;
961 962 963 964 965 966

    return RT_FALSE;
}

void ff_rel_grant(_SYNC_t m)
{
P
prife 已提交
967
    rt_mutex_release(m);
968 969 970
}

#endif
971 972 973 974

/* Memory functions */
#if _USE_LFN == 3
/* Allocate memory block */
P
prife 已提交
975
void *ff_memalloc(UINT size)
976 977 978 979 980
{
    return rt_malloc(size);
}

/* Free memory block */
P
prife 已提交
981
void ff_memfree(void *mem)
982 983 984 985 986
{
    rt_free(mem);
}
#endif /* _USE_LFN == 3 */