Merge branch 'dev'
[bertos.git] / bertos / fs / fatfs / diskio.c
1 /*-----------------------------------------------------------------------*/
2 /* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2007        */
3 /*-----------------------------------------------------------------------*/
4 /* This is a stub disk I/O module that acts as front end of the existing */
5 /* disk I/O modules and attach it to FatFs module with common interface. */
6 /*-----------------------------------------------------------------------*/
7
8 #include "diskio.h"
9 #include "ff.h"
10
11 #include <io/kblock.h>
12
13 #include "cfg/cfg_fat.h"
14 #define LOG_LEVEL   FAT_LOG_LEVEL
15 #define LOG_FORMAT  FAT_LOG_FORMAT
16 #include <cfg/log.h>
17
18 static KBlock *devs[_DRIVES];
19
20 void disk_assignDrive(KBlock *dev, int dev_num)
21 {
22         ASSERT(dev_num < _DRIVES);
23         devs[dev_num] = dev;
24 }
25
26
27 /*-----------------------------------------------------------------------*/
28 /* Inidialize a Drive                                                    */
29
30 DSTATUS disk_initialize (
31         BYTE drv                                /* Physical drive nmuber (0..) */
32 )
33 {
34         return disk_status(drv);
35 }
36
37
38
39 /*-----------------------------------------------------------------------*/
40 /* Return Disk Status                                                    */
41
42 DSTATUS disk_status (
43         BYTE drv                /* Physical drive nmuber (0..) */
44 )
45 {
46         KBlock *dev = devs[drv];
47         ASSERT(dev);
48
49         if (kblock_error(dev) != 0)
50                 return STA_NOINIT;
51         else
52                 return RES_OK;
53 }
54
55
56
57 /*-----------------------------------------------------------------------*/
58 /* Read Sector(s)                                                        */
59
60 DRESULT disk_read (
61         BYTE drv,               /* Physical drive nmuber (0..) */
62         BYTE *buff,             /* Data buffer to store read data */
63         DWORD sector,   /* Sector address (LBA) */
64         BYTE count              /* Number of sectors to read (1..255) */
65 )
66 {
67         KBlock *dev = devs[drv];
68         ASSERT(dev);
69
70
71         while (count--)
72         {
73                 if (kblock_read(dev, sector++, buff, 0, dev->blk_size) != dev->blk_size)
74                         return RES_ERROR;
75                 buff += dev->blk_size;
76         }
77         return RES_OK;
78 }
79
80
81
82 /*-----------------------------------------------------------------------*/
83 /* Write Sector(s)                                                       */
84
85 #if _READONLY == 0
86 DRESULT disk_write (
87         BYTE drv,                       /* Physical drive nmuber (0..) */
88         const BYTE *buff,       /* Data to be written */
89         DWORD sector,           /* Sector address (LBA) */
90         BYTE count                      /* Number of sectors to write (1..255) */
91 )
92 {
93         KBlock *dev = devs[drv];
94         ASSERT(dev);
95
96         while (count--)
97         {
98                 if (kblock_write(dev, sector++, buff, 0, dev->blk_size) != dev->blk_size)
99                         return RES_ERROR;
100                 buff += dev->blk_size;
101         }
102         return RES_OK;
103 }
104 #endif /* _READONLY */
105
106
107
108 /*-----------------------------------------------------------------------*/
109 /* Miscellaneous Functions                                               */
110
111 DRESULT disk_ioctl (
112         BYTE drv,               /* Physical drive nmuber (0..) */
113         BYTE ctrl,              /* Control code */
114         void *buff              /* Buffer to send/receive control data */
115 )
116 {
117         KBlock *dev = devs[drv];
118         ASSERT(dev);
119
120
121         switch (ctrl)
122         {
123                 case CTRL_SYNC:
124                         if (kblock_flush(dev) == 0)
125                                 return RES_OK;
126                         else
127                                 return RES_ERROR;
128
129                 case GET_SECTOR_SIZE:
130                         *(WORD *)buff = dev->blk_size;
131                         return RES_OK;
132
133                 case GET_SECTOR_COUNT:
134                         *(DWORD *)buff = dev->blk_cnt;
135                         return RES_OK;
136
137                 case GET_BLOCK_SIZE:
138                         *(DWORD *)buff = 1;
139                         return RES_OK;
140
141                 default:
142                         LOG_ERR("unknown command: [%d]\n", ctrl);
143                         return RES_PARERR;
144         }
145 }
146
147
148 DWORD get_fattime(void)
149 {
150         return 0;
151 }
152
153