Contiki-Inga 3.x
cfs-fat.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, (TU Braunschweig).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Author: Christoph Peltz <peltz@ibr.cs.tu-bs.de>
30  */
31 
32 /**
33  * \addtogroup cfs
34  * @{
35  */
36 /**
37  * \defgroup fat_driver FAT Driver
38  *
39  * Additional function for the FAT file system.
40  *
41  * @{
42  */
43 
44 /**
45  * \file
46  * FAT driver definitions
47  * \author
48  * Christoph Peltz <peltz@ibr.cs.tu-bs.de>
49  */
50 
51 #ifndef _FAT_H_
52 #define _FAT_H_
53 
54 #include <string.h>
55 #include <stdio.h>
56 #include <ctype.h>
57 #include <stdint.h>
58 
59 /** Seek type is 32 bit for FAT files. */
60 #ifndef CFS_CONF_OFFSET_TYPE
61 #define CFS_CONF_OFFSET_TYPE uint32_t
62 #endif
63 
64 #include "diskio.h"
65 #include "cfs/cfs.h"
66 
67 
68 #ifdef FAT_CONF_COOPERATIVE
69 #define FAT_COOPERATIVE FAT_CONF_COOPERATIVE
70 #else
71 #undef FAT_COOPERATIVE
72 #endif
73 
74 /** Allows to enable synchronization of FATs when unmounting device.
75  * This may allow to restore a corrupted primary FAT but
76  * note that this will lead to poor performance and decreases flash life because
77  * of a remarkable number of additional write cycles.
78  */
79 #ifndef FAT_SYNC
80 #define FAT_SYNC 0
81 #endif
82 
83 #define FAT_COOP_QUEUE_SIZE 15
84 
85 #define FAT12 0
86 #define FAT16 1
87 #define FAT32 2
88 #define FAT_INVALID 3
89 
90 #define EOC 0x0FFFFFFF
91 
92 /* Maximum number of files that can be opened simulatneously */
93 #ifndef FAT_FD_POOL_SIZE
94 #define FAT_FD_POOL_SIZE 5
95 #endif
96 
97 /** Holds boot sector information. */
98 struct FAT_Info {
99  uint8_t type; /** Either FAT16, FAT32 or FAT_INVALID */
100  uint16_t BPB_BytesPerSec;
101  uint8_t BPB_SecPerClus;
102  uint16_t BPB_RsvdSecCnt;
103  uint8_t BPB_NumFATs;
104  uint16_t BPB_RootEntCnt;
105  uint32_t BPB_TotSec;
106  uint8_t BPB_Media; /*! obsolete */
107  uint32_t BPB_FATSz; /*! Number of sectors per FAT, i.e. size of FAT */
108  uint32_t BPB_RootClus; /** only valid for FAT32 */
109 };
110 /** Fat table entry for file */
111 struct dir_entry {
112  uint8_t DIR_Name[11];
113  uint8_t DIR_Attr;
114  uint8_t DIR_NTRes;
115  uint8_t CrtTimeTenth;
116  /** Create time [15-11: hours, 10-5: minutes, 4-0: seconds/2] */
117  uint16_t DIR_CrtTime;
118  /** Create date [15-9: year sine 1980, 8-5: month, 4-0: day] */
119  uint16_t DIR_CrtDate;
120  /** Last access date [15-9: year sine 1980, 8-5: month, 4-0: day] */
122  uint16_t DIR_FstClusHI;
123  /** Last modified time [15-11: hours, 10-5: minutes, 4-0: seconds/2] */
124  uint16_t DIR_WrtTime;
125  /** Create date [15-9: year sine 1980, 8-5: month, 4-0: day] */
126  uint16_t DIR_WrtDate;
127  uint16_t DIR_FstClusLO;
128  /** Size of file in [byte] */
129  uint32_t DIR_FileSize;
130 };
131 
132 struct file {
133  //metadata
134  /** Cluster Position on disk */
135  uint32_t cluster;
136  uint32_t dir_entry_sector;
137  uint16_t dir_entry_offset;
138  struct dir_entry dir_entry;
139  uint32_t nth_cluster;
140  uint32_t n;
141 };
142 
143 struct file_desc {
144  //cfs_offset_t offset;
145  uint32_t offset;
146  struct file *file;
147  uint8_t flags;
148 };
149 
150 //int cfs_fat_rmdir(char *);
151 //int cfs_fat_mkdir(char *);
152 
153 /**
154  * Formats the specified device as FAT16/32
155  *
156  * @param dev device to format
157  * @return
158  */
159 int cfs_fat_mkfs(struct diskio_device_info *dev);
160 
161 /**
162  * Tries to mount the defined device.
163  *
164  * \param dev The device on which a FAT-FS should be mounted.
165  * \return 0 on success, 1 if the bootsector was not found or corrupted,
166  * 2 if the FAT-Type wasn't supported.
167  */
168 uint8_t cfs_fat_mount_device(struct diskio_device_info *dev);
169 
170 /**
171  * Umounts the mounted device. Invalidates all file descriptors.
172  * (Syncs all FATs (only if FAT_SYNC is set)) and flushes cached data.
173  */
174 void cfs_fat_umount_device();
175 
176 /**
177  * Populates the given FAT_Info with the mounted FAT_Info.
178  *
179  * \param *info The FAT_Info struct which should be populated.
180  */
181 void cfs_fat_get_fat_info(struct FAT_Info *info);
182 
183 /**
184  * Returns the date of last modification.
185  *
186  * Format: [15-9: year sine 1980, 8-5: month, 4-0: day]
187  * @param fd File descriptor
188  */
189 
190 uint16_t cfs_fat_get_last_date(int fd);
191 
192 /**
193  * Returns the time of last modification.
194  *
195  * Format: [15-11: hours, 10-5: minutes, 4-0: seconds/2]
196  * @param fd File descriptor
197  */
198 uint16_t cfs_fat_get_last_time(int fd);
199 
200 /**
201  * Returns the date of creation.
202  *
203  * Format: [15-9: year sine 1980, 8-5: month, 4-0: day]
204  * @param fd File descriptor
205  */
206 uint16_t cfs_fat_get_create_date(int fd);
207 
208 /**
209  * Returns the time of creation.
210  *
211  * Format: [15-11: hours, 10-5: minutes, 4-0: seconds/2]
212  * @param fd File descriptor
213  */
214 uint16_t cfs_fat_get_create_time(int fd);
215 
216 /**
217  * Syncs every FAT with the first FAT. Can take much time.
218  */
219 void cfs_fat_sync_fats();
220 
221 /**
222  * Writes the current buffered block back to the disk if it was changed.
223  */
224 void cfs_fat_flush();
225 
226 /**
227  * Returns the file size of the associated file
228  *
229  * @param fd
230  * @return
231  */
232 uint32_t cfs_fat_file_size(int fd);
233 
234 /**
235  *
236  * @param fd
237  */
238 void cfs_fat_print_cluster_chain(int fd);
239 
240 /**
241  *
242  * @param fd
243  */
244 void cfs_fat_print_file_info(int fd);
245 
246 /**
247  *
248  * @param dir_entry
249  */
251 
252 
253 uint8_t is_a_power_of_2(uint32_t value);
254 uint32_t round_down_to_power_of_2(uint32_t value);
255 
256 #endif
257 
258 /** @} */
259 /** @} */