40786990209fb7318013133286a2ec61a4299f40
[fw/altos] / src / drivers / ao_fat.h
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #ifndef _AO_FAT_H_
19 #define _AO_FAT_H_
20
21 void
22 ao_fat_init(void);
23
24 #define AO_FAT_FILE_REGULAR             0x00
25 #define AO_FAT_FILE_READ_ONLY           0x01
26 #define AO_FAT_FILE_HIDDEN              0x02
27 #define AO_FAT_FILE_SYSTEM              0x04
28 #define AO_FAT_FILE_VOLUME_LABEL        0x08
29 #define AO_FAT_FILE_DIRECTORY           0x10
30 #define AO_FAT_FILE_ARCHIVE             0x20
31
32 #define AO_FAT_DENT_EMPTY               0xe5
33 #define AO_FAT_DENT_END                 0x00
34
35 #define AO_FAT_IS_FILE(attr)    (((attr) & (AO_FAT_FILE_VOLUME_LABEL|AO_FAT_FILE_DIRECTORY)) == 0)
36 #define AO_FAT_IS_DIR(attr)     (((attr) & (AO_FAT_FILE_DIRECTORY|AO_FAT_FILE_VOLUME_LABEL)) == AO_FAT_FILE_DIRECTORY)
37
38 /* API error codes */
39 #define AO_FAT_SUCCESS                  0
40 #define AO_FAT_EPERM                    1
41 #define AO_FAT_ENOENT                   2
42 #define AO_FAT_EIO                      4
43 #define AO_FAT_EBADF                    9
44 #define AO_FAT_EACCESS                  13
45 #define AO_FAT_EEXIST                   17
46 #define AO_FAT_ENOTDIR                  20
47 #define AO_FAT_EISDIR                   21
48 #define AO_FAT_EMFILE                   24
49 #define AO_FAT_EFBIG                    27
50 #define AO_FAT_ENOSPC                   28
51
52 /* ao_fat_setup return values */
53 #define AO_FAT_FILESYSTEM_SUCCESS                       0
54 #define AO_FAT_FILESYSTEM_MBR_READ_FAILURE              1
55 #define AO_FAT_FILESYSTEM_INVALID_MBR_SIGNATURE         2
56 #define AO_FAT_FILESYSTEM_INVALID_PARTITION_TYPE        3
57 #define AO_FAT_FILESYSTEM_ZERO_SIZED_PARTITION          4
58
59 #define AO_FAT_FILESYSTEM_BOOT_READ_FAILURE             5
60 #define AO_FAT_FILESYSTEM_INVALID_BOOT_SIGNATURE        6
61 #define AO_FAT_FILESYSTEM_INVALID_SECTOR_SIZE           7
62
63 void
64 ao_fat_sync(void);
65
66 int8_t
67 ao_fat_full(void);
68
69 int8_t
70 ao_fat_open(char name[11], uint8_t mode);
71
72 #define AO_FAT_OPEN_READ                0
73 #define AO_FAT_OPEN_WRITE               1
74 #define AO_FAT_OPEN_RW                  2
75
76 int8_t
77 ao_fat_creat(char name[11]);
78
79 int8_t
80 ao_fat_close(void);
81
82 int
83 ao_fat_read(void *dest, int len);
84
85 int
86 ao_fat_write(void *src, int len);
87
88 #define AO_FAT_SEEK_SET 0
89 #define AO_FAT_SEEK_CUR 1
90 #define AO_FAT_SEEK_END 2
91
92 int32_t
93 ao_fat_seek(int32_t pos, uint8_t whence);
94
95 int8_t
96 ao_fat_unlink(char name[11]);
97
98 int8_t
99 ao_fat_rename(char old[11], char new[11]);
100
101 /*
102  * Byte offset within a file. Supports files up to 2GB in size
103  */
104 typedef int32_t         ao_fat_offset_t;
105
106 /*
107  * Cluster index in partition data space
108  */
109 typedef uint32_t        ao_fat_cluster_t;
110
111 /*
112  * Sector offset within partition
113  */
114 typedef uint32_t        ao_fat_sector_t;
115
116 /*
117  * Index within the root directory
118  */
119 typedef uint16_t        ao_fat_dirent_t;
120
121 /*
122  * Offset within a cluster (or sector)
123  */
124 typedef uint16_t        ao_fat_cluster_offset_t;
125
126 struct ao_fat_dirent {
127         char                    name[11];
128         uint8_t                 attr;
129         uint32_t                size;
130         ao_fat_cluster_t        cluster;
131         uint16_t                entry;
132 };
133
134 int8_t
135 ao_fat_readdir(uint16_t *entry, struct ao_fat_dirent *dirent);
136
137 #endif /* _AO_FAT_H_ */