4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
33 * \brief Low level disk access for FatFs emulated.
36 * \author Luca Ottaviano <lottaviano@develer.com>
39 /*-----------------------------------------------------------------------*/
40 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
41 /*-----------------------------------------------------------------------*/
43 #include <fs/fatfs/diskio.h>
48 #define SECTOR_SIZE 512
50 static volatile DSTATUS Stat = STA_NOINIT;
53 * This is an example implementation, used to simulate the the calls to normal filesystem calls
54 * It only works for drive 0.
58 /*-----------------------------------------------------------------------*/
59 /* Inidialize a Drive */
61 static FILE *fake_disk = 0;
63 DSTATUS disk_initialize (
64 BYTE drv /* Physical drive nmuber (0..) */
68 return STA_NOINIT; /* Support only drive 0 */
69 // XXX: pay attention here: some functions call disk_initialize *after* it has
70 // been initialized for the first time.
71 // Here we just return the status (that should always be ~STA_NOINIT after the first
76 const char *path = "emuldisk.dsk";
77 fake_disk = fopen(path, "w+");
84 fprintf(stderr, "invalid mode\n");
95 /*-----------------------------------------------------------------------*/
96 /* Return Disk Status */
99 BYTE drv /* Physical drive nmuber (0..) */
103 return STA_NOINIT; /* Support only drive 0 */
109 /*-----------------------------------------------------------------------*/
113 BYTE drv, /* Physical drive nmuber (0..) */
114 BYTE *buff, /* Data buffer to store read data */
115 DWORD sector, /* Sector address (LBA) */
116 BYTE count /* Number of sectors to read (1..255) */
119 if (drv || !count) return RES_PARERR;
120 if (Stat & STA_NOINIT) return RES_NOTRDY;
122 fseek(fake_disk, sector * SECTOR_SIZE, SEEK_SET);
123 size_t read_items = fread(buff, SECTOR_SIZE, count, fake_disk);
124 if (read_items == count)
129 fprintf(stderr, "end-of-file\n");
130 if (ferror(fake_disk))
131 fprintf(stderr, "error\n");
137 /*-----------------------------------------------------------------------*/
138 /* Write Sector(s) */
142 BYTE drv, /* Physical drive nmuber (0..) */
143 const BYTE *buff, /* Data to be written */
144 DWORD sector, /* Sector address (LBA) */
145 BYTE count /* Number of sectors to write (1..255) */
148 if (drv || !count) return RES_PARERR;
149 if (Stat & STA_NOINIT) return RES_NOTRDY;
150 if (Stat & STA_PROTECT) return RES_WRPRT;
152 fseek(fake_disk, sector * SECTOR_SIZE, SEEK_SET);
153 size_t write_items = fwrite(buff, SECTOR_SIZE, count, fake_disk);
154 if (write_items == count)
159 fprintf(stderr, "end-of-file\n");
160 if (ferror(fake_disk))
161 fprintf(stderr, "error\n");
165 #endif /* _READONLY */
169 /*-----------------------------------------------------------------------*/
170 /* Miscellaneous Functions */
173 BYTE drv, /* Physical drive nmuber (0..) */
174 BYTE ctrl, /* Control code */
175 void *buff /* Buffer to send/receive control data */
178 if (drv) return RES_PARERR;
179 if (Stat & STA_NOINIT) return RES_NOTRDY;
183 case GET_SECTOR_SIZE:
184 *(WORD*)buff = SECTOR_SIZE;
186 case GET_SECTOR_COUNT:
187 *(DWORD*)buff = 65536;
201 DWORD get_fattime(void)
203 time_t tmp = time(0);
204 struct tm *t = localtime(&tmp);
207 tim |= (t->tm_sec / 2);
209 tim |= (t->tm_min << 5);
211 tim |= (t->tm_hour << 11);
213 tim |= (t->tm_mday << 16);
215 tim |= ((t->tm_mon + 1) << 21);
217 tim |= ((t->tm_year - 80) << 25);