Refactor to use new protocol module and sipo.
[bertos.git] / bertos / emul / diskio_emul.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Low level disk access for FatFs emulated.
34  *
35  * \author Luca Ottaviano <lottaviano@develer.com>
36  */
37
38 /*-----------------------------------------------------------------------*/
39 /* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2007        */
40 /*-----------------------------------------------------------------------*/
41
42 #include <fs/fatfs/diskio.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #include <time.h>
46
47 #define SECTOR_SIZE 512
48
49 static volatile DSTATUS Stat = STA_NOINIT;
50
51 /**
52  * This is an example implementation, used to simulate the the calls to normal filesystem calls
53  * It only works for drive 0.
54  */
55
56
57 /*-----------------------------------------------------------------------*/
58 /* Inidialize a Drive                                                    */
59
60 static FILE *fake_disk = 0;
61
62 DSTATUS disk_initialize (
63         BYTE drv                                /* Physical drive nmuber (0..) */
64 )
65 {
66         if (drv)
67                 return STA_NOINIT; /* Support only drive 0 */
68         // XXX: pay attention here: some functions call disk_initialize *after* it has
69         //  been initialized for the first time.
70         //  Here we just return the status (that should always be ~STA_NOINIT after the first
71         //  call)
72         if (fake_disk)
73                 return Stat;
74
75         const char *path = "emuldisk.dsk";
76         fake_disk = fopen(path, "w+");
77         int err = errno;
78         if (!fake_disk)
79         {
80                 switch (err)
81                 {
82                 case EINVAL:
83                         fprintf(stderr, "invalid mode\n");
84                 default:
85                         return STA_NOINIT;
86                 }
87         }
88         Stat &= ~STA_NOINIT;
89         return Stat;
90 }
91
92
93
94 /*-----------------------------------------------------------------------*/
95 /* Return Disk Status                                                    */
96
97 DSTATUS disk_status (
98         BYTE drv                /* Physical drive nmuber (0..) */
99 )
100 {
101         if (drv)
102                 return STA_NOINIT; /* Support only drive 0 */
103         return Stat;
104 }
105
106
107
108 /*-----------------------------------------------------------------------*/
109 /* Read Sector(s)                                                        */
110
111 DRESULT disk_read (
112         BYTE drv,               /* Physical drive nmuber (0..) */
113         BYTE *buff,             /* Data buffer to store read data */
114         DWORD sector,   /* Sector address (LBA) */
115         BYTE count              /* Number of sectors to read (1..255) */
116 )
117 {
118         if (drv || !count) return RES_PARERR;
119         if (Stat & STA_NOINIT) return RES_NOTRDY;
120
121         fseek(fake_disk, sector * SECTOR_SIZE, SEEK_SET);
122         size_t read_items = fread(buff, SECTOR_SIZE, count, fake_disk);
123         if (read_items == count)
124                 return RES_OK;
125         else
126         {
127                 if (feof(fake_disk))
128                         fprintf(stderr, "end-of-file\n");
129                 if (ferror(fake_disk))
130                         fprintf(stderr, "error\n");
131                 return RES_ERROR;
132         }
133 }
134
135
136 /*-----------------------------------------------------------------------*/
137 /* Write Sector(s)                                                       */
138
139 #if _READONLY == 0
140 DRESULT disk_write (
141         BYTE drv,                       /* Physical drive nmuber (0..) */
142         const BYTE *buff,       /* Data to be written */
143         DWORD sector,           /* Sector address (LBA) */
144         BYTE count                      /* Number of sectors to write (1..255) */
145 )
146 {
147         if (drv || !count) return RES_PARERR;
148         if (Stat & STA_NOINIT) return RES_NOTRDY;
149         if (Stat & STA_PROTECT) return RES_WRPRT;
150
151         fseek(fake_disk, sector * SECTOR_SIZE, SEEK_SET);
152         size_t write_items = fwrite(buff, SECTOR_SIZE, count, fake_disk);
153         if (write_items == count)
154                 return RES_OK;
155         else
156         {
157                 if (feof(fake_disk))
158                         fprintf(stderr, "end-of-file\n");
159                 if (ferror(fake_disk))
160                         fprintf(stderr, "error\n");
161                 return RES_ERROR;
162         }
163 }
164 #endif /* _READONLY */
165
166
167
168 /*-----------------------------------------------------------------------*/
169 /* Miscellaneous Functions                                               */
170
171 DRESULT disk_ioctl (
172         BYTE drv,               /* Physical drive nmuber (0..) */
173         BYTE ctrl,              /* Control code */
174         void *buff              /* Buffer to send/receive control data */
175 )
176 {
177         if (drv) return RES_PARERR;
178         if (Stat & STA_NOINIT) return RES_NOTRDY;
179
180         switch (ctrl)
181         {
182         case GET_SECTOR_SIZE:
183                 *(WORD*)buff = SECTOR_SIZE;
184                 break;
185         case GET_SECTOR_COUNT:
186                 *(DWORD*)buff = 65536;
187                 break;
188         case GET_BLOCK_SIZE:
189                 *(DWORD*)buff = 1;
190                 break;
191         case CTRL_SYNC:
192                 fflush(fake_disk);
193                 break;
194         default:
195                 return RES_PARERR;
196         }
197         return RES_OK;
198 }
199
200 DWORD get_fattime(void)
201 {
202         time_t tmp = time(0);
203         struct tm *t = localtime(&tmp);
204         DWORD tim = 0;
205         // seconds
206         tim |= (t->tm_sec / 2);
207         // min
208         tim |= (t->tm_min << 5);
209         // hour
210         tim |= (t->tm_hour << 11);
211         // month day (1..31)
212         tim |= (t->tm_mday << 16);
213         // month (1..12)
214         tim |= ((t->tm_mon + 1) << 21);
215         // year (0..127)
216         tim |= ((t->tm_year - 80) << 25);
217         return tim;
218 }
219