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.
35 * \author Luca Ottaviano <lottaviano@develer.com>
38 /*-----------------------------------------------------------------------*/
39 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
40 /*-----------------------------------------------------------------------*/
42 #include <fs/fatfs/diskio.h>
47 #define SECTOR_SIZE 512
49 static volatile DSTATUS Stat = STA_NOINIT;
52 * This is an example implementation, used to simulate the the calls to normal filesystem calls
53 * It only works for drive 0.
57 /*-----------------------------------------------------------------------*/
58 /* Inidialize a Drive */
60 static FILE *fake_disk = 0;
62 DSTATUS disk_initialize (
63 BYTE drv /* Physical drive nmuber (0..) */
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
75 const char *path = "emuldisk.dsk";
76 fake_disk = fopen(path, "w+");
83 fprintf(stderr, "invalid mode\n");
94 /*-----------------------------------------------------------------------*/
95 /* Return Disk Status */
98 BYTE drv /* Physical drive nmuber (0..) */
102 return STA_NOINIT; /* Support only drive 0 */
108 /*-----------------------------------------------------------------------*/
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) */
118 if (drv || !count) return RES_PARERR;
119 if (Stat & STA_NOINIT) return RES_NOTRDY;
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)
128 fprintf(stderr, "end-of-file\n");
129 if (ferror(fake_disk))
130 fprintf(stderr, "error\n");
136 /*-----------------------------------------------------------------------*/
137 /* Write Sector(s) */
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) */
147 if (drv || !count) return RES_PARERR;
148 if (Stat & STA_NOINIT) return RES_NOTRDY;
149 if (Stat & STA_PROTECT) return RES_WRPRT;
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)
158 fprintf(stderr, "end-of-file\n");
159 if (ferror(fake_disk))
160 fprintf(stderr, "error\n");
164 #endif /* _READONLY */
168 /*-----------------------------------------------------------------------*/
169 /* Miscellaneous Functions */
172 BYTE drv, /* Physical drive nmuber (0..) */
173 BYTE ctrl, /* Control code */
174 void *buff /* Buffer to send/receive control data */
177 if (drv) return RES_PARERR;
178 if (Stat & STA_NOINIT) return RES_NOTRDY;
182 case GET_SECTOR_SIZE:
183 *(WORD*)buff = SECTOR_SIZE;
185 case GET_SECTOR_COUNT:
186 *(DWORD*)buff = 65536;
200 DWORD get_fattime(void)
202 time_t tmp = time(0);
203 struct tm *t = localtime(&tmp);
206 tim |= (t->tm_sec / 2);
208 tim |= (t->tm_min << 5);
210 tim |= (t->tm_hour << 11);
212 tim |= (t->tm_mday << 16);
214 tim |= ((t->tm_mon + 1) << 21);
216 tim |= ((t->tm_year - 80) << 25);