blob: 2680fdf7516b52af6979281f731fc025f773cf47 [file] [log] [blame]
Björn Stenbergd9eb5c72002-03-28 15:09:10 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#ifndef FAT_H
21#define FAT_H
22
Björn Stenbergb7b48fe2002-10-20 22:50:58 +000023#include <stdbool.h>
Jörg Hohensohnda848572004-12-28 22:16:07 +000024#include "ata.h" /* for volume definitions */
Miika Pekkarinenae66c2b2006-12-03 18:12:19 +000025#include "config.h"
Björn Stenbergb7b48fe2002-10-20 22:50:58 +000026
Jens Arnoldef3e1292006-12-04 21:37:22 +000027#define SECTOR_SIZE 512
Björn Stenberg1dff4b62002-04-26 16:44:58 +000028
Rani Hod8630f072006-07-31 22:59:45 +000029/* Number of bytes reserved for a file name (including the trailing \0).
30 Since names are stored in the entry as UTF-8, we won't be able to
31 store all names allowed by FAT. In FAT, a name can have max 255
32 characters (not bytes!). Since the UTF-8 encoding of a char may take
33 up to 4 bytes, there will be names that we won't be able to store
34 completely. For such names, the short DOS name is used. */
35#define FAT_FILENAME_BYTES 256
36
Björn Stenberg5661b232002-04-27 01:25:22 +000037struct fat_direntry
38{
Rani Hod8630f072006-07-31 22:59:45 +000039 unsigned char name[FAT_FILENAME_BYTES]; /* UTF-8 encoded name plus \0 */
Björn Stenberg5661b232002-04-27 01:25:22 +000040 unsigned short attr; /* Attributes */
41 unsigned char crttimetenth; /* Millisecond creation
42 time stamp (0-199) */
43 unsigned short crttime; /* Creation time */
44 unsigned short crtdate; /* Creation date */
45 unsigned short lstaccdate; /* Last access date */
Björn Stenberg5661b232002-04-27 01:25:22 +000046 unsigned short wrttime; /* Last write time */
47 unsigned short wrtdate; /* Last write date */
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000048 unsigned long filesize; /* File size in bytes */
49 long firstcluster; /* fstclusterhi<<16 + fstcluslo */
Björn Stenbergd9eb5c72002-03-28 15:09:10 +000050};
51
52#define FAT_ATTR_READ_ONLY 0x01
53#define FAT_ATTR_HIDDEN 0x02
54#define FAT_ATTR_SYSTEM 0x04
55#define FAT_ATTR_VOLUME_ID 0x08
56#define FAT_ATTR_DIRECTORY 0x10
57#define FAT_ATTR_ARCHIVE 0x20
Jörg Hohensohnda848572004-12-28 22:16:07 +000058#define FAT_ATTR_VOLUME 0x40 /* this is a volume, not a real directory */
Björn Stenbergd9eb5c72002-03-28 15:09:10 +000059
Björn Stenberg924164e2002-05-03 15:35:51 +000060struct fat_file
Björn Stenberge8bcc012002-04-27 19:37:41 +000061{
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000062 long firstcluster; /* first cluster in file */
63 long lastcluster; /* cluster of last access */
64 long lastsector; /* sector of last access */
65 long clusternum; /* current clusternum */
66 long sectornum; /* sector number in this cluster */
Björn Stenbergeee2c012002-11-18 11:58:43 +000067 unsigned int direntry; /* short dir entry index from start of dir */
68 unsigned int direntries; /* number of dir entries used by this file */
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000069 long dircluster; /* first cluster of dir */
Björn Stenberg1f214f22002-11-13 23:16:32 +000070 bool eof;
Jörg Hohensohnda848572004-12-28 22:16:07 +000071#ifdef HAVE_MULTIVOLUME
Jens Arnold316ae182005-01-03 07:59:49 +000072 int volume; /* file resides on which volume */
Jörg Hohensohnda848572004-12-28 22:16:07 +000073#endif
Björn Stenbergd9eb5c72002-03-28 15:09:10 +000074};
75
Björn Stenberga5e77d82002-10-31 16:09:28 +000076struct fat_dir
77{
Björn Stenbergc442a682002-11-15 11:20:33 +000078 unsigned int entry;
Björn Stenbergeee2c012002-11-18 11:58:43 +000079 unsigned int entrycount;
Jean-Philippe Bernardyfc194452005-02-25 18:50:16 +000080 long sector;
Björn Stenberg1f214f22002-11-13 23:16:32 +000081 struct fat_file file;
Jens Arnoldef3e1292006-12-04 21:37:22 +000082 unsigned char sectorcache[3][SECTOR_SIZE];
Björn Stenberga5e77d82002-10-31 16:09:28 +000083};
84
85
Jörg Hohensohnda848572004-12-28 22:16:07 +000086extern void fat_init(void);
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000087extern int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector);
Jörg Hohensohn74146872005-01-05 00:09:04 +000088extern int fat_unmount(int volume, bool flush);
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000089extern void fat_size(IF_MV2(int volume,) unsigned long* size, unsigned long* free); // public for info
Jörg Hohensohnda848572004-12-28 22:16:07 +000090extern void fat_recalc_free(IF_MV_NONVOID(int volume)); // public for debug info screen
Jens Arnold0ceaa5e2004-08-17 01:45:48 +000091extern int fat_create_dir(const char* name,
Linus Nielsen Feltzing60b1c4b2004-01-15 14:30:59 +000092 struct fat_dir* newdir,
93 struct fat_dir* dir);
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000094extern long fat_startsector(IF_MV_NONVOID(int volume)); // public for config sector
Jörg Hohensohnda848572004-12-28 22:16:07 +000095extern int fat_open(IF_MV2(int volume,)
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000096 long cluster,
Björn Stenbergb7b48fe2002-10-20 22:50:58 +000097 struct fat_file* ent,
Jens Arnold0ceaa5e2004-08-17 01:45:48 +000098 const struct fat_dir* dir);
99extern int fat_create_file(const char* name,
Björn Stenbergb7b48fe2002-10-20 22:50:58 +0000100 struct fat_file* ent,
101 struct fat_dir* dir);
Jean-Philippe Bernardy5da99ed2005-02-26 21:18:05 +0000102extern long fat_readwrite(struct fat_file *ent, long sectorcount,
Björn Stenbergb7b48fe2002-10-20 22:50:58 +0000103 void* buf, bool write );
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +0000104extern int fat_closewrite(struct fat_file *ent, long size, int attr);
105extern int fat_seek(struct fat_file *ent, unsigned long sector );
Björn Stenberg46ddacf2002-10-22 15:06:08 +0000106extern int fat_remove(struct fat_file *ent);
Jens Arnold0ceaa5e2004-08-17 01:45:48 +0000107extern int fat_truncate(const struct fat_file *ent);
Björn Stenbergc5f5be52002-11-19 12:48:50 +0000108extern int fat_rename(struct fat_file* file,
Jens Arnold316ae182005-01-03 07:59:49 +0000109 struct fat_dir* dir,
Jens Arnold0ceaa5e2004-08-17 01:45:48 +0000110 const unsigned char* newname,
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +0000111 long size, int attr);
Björn Stenberge8bcc012002-04-27 19:37:41 +0000112
Jörg Hohensohnda848572004-12-28 22:16:07 +0000113extern int fat_opendir(IF_MV2(int volume,)
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +0000114 struct fat_dir *ent, unsigned long currdir,
Jens Arnold0ceaa5e2004-08-17 01:45:48 +0000115 const struct fat_dir *parent_dir);
Björn Stenberg924164e2002-05-03 15:35:51 +0000116extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry);
Jean-Philippe Bernardy5da99ed2005-02-26 21:18:05 +0000117extern unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume));
Jörg Hohensohnda848572004-12-28 22:16:07 +0000118extern bool fat_ismounted(int volume);
Björn Stenbergd9eb5c72002-03-28 15:09:10 +0000119
120#endif