blob: 0e83ca8b4dbbd180db0fa29ac9d65bb0f8222452 [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 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Björn Stenbergd9eb5c72002-03-28 15:09:10 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#ifndef FAT_H
23#define FAT_H
24
Björn Stenbergb7b48fe2002-10-20 22:50:58 +000025#include <stdbool.h>
Jörg Hohensohnda848572004-12-28 22:16:07 +000026#include "ata.h" /* for volume definitions */
Miika Pekkarinenae66c2b2006-12-03 18:12:19 +000027#include "config.h"
Björn Stenbergb7b48fe2002-10-20 22:50:58 +000028
Jens Arnoldef3e1292006-12-04 21:37:22 +000029#define SECTOR_SIZE 512
Björn Stenberg1dff4b62002-04-26 16:44:58 +000030
Rani Hod8630f072006-07-31 22:59:45 +000031/* Number of bytes reserved for a file name (including the trailing \0).
32 Since names are stored in the entry as UTF-8, we won't be able to
33 store all names allowed by FAT. In FAT, a name can have max 255
34 characters (not bytes!). Since the UTF-8 encoding of a char may take
35 up to 4 bytes, there will be names that we won't be able to store
36 completely. For such names, the short DOS name is used. */
37#define FAT_FILENAME_BYTES 256
38
Björn Stenberg5661b232002-04-27 01:25:22 +000039struct fat_direntry
40{
Rani Hod8630f072006-07-31 22:59:45 +000041 unsigned char name[FAT_FILENAME_BYTES]; /* UTF-8 encoded name plus \0 */
Björn Stenberg5661b232002-04-27 01:25:22 +000042 unsigned short attr; /* Attributes */
43 unsigned char crttimetenth; /* Millisecond creation
44 time stamp (0-199) */
45 unsigned short crttime; /* Creation time */
46 unsigned short crtdate; /* Creation date */
47 unsigned short lstaccdate; /* Last access date */
Björn Stenberg5661b232002-04-27 01:25:22 +000048 unsigned short wrttime; /* Last write time */
49 unsigned short wrtdate; /* Last write date */
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000050 unsigned long filesize; /* File size in bytes */
51 long firstcluster; /* fstclusterhi<<16 + fstcluslo */
Björn Stenbergd9eb5c72002-03-28 15:09:10 +000052};
53
54#define FAT_ATTR_READ_ONLY 0x01
55#define FAT_ATTR_HIDDEN 0x02
56#define FAT_ATTR_SYSTEM 0x04
57#define FAT_ATTR_VOLUME_ID 0x08
58#define FAT_ATTR_DIRECTORY 0x10
59#define FAT_ATTR_ARCHIVE 0x20
Jörg Hohensohnda848572004-12-28 22:16:07 +000060#define FAT_ATTR_VOLUME 0x40 /* this is a volume, not a real directory */
Björn Stenbergd9eb5c72002-03-28 15:09:10 +000061
Björn Stenberg924164e2002-05-03 15:35:51 +000062struct fat_file
Björn Stenberge8bcc012002-04-27 19:37:41 +000063{
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000064 long firstcluster; /* first cluster in file */
65 long lastcluster; /* cluster of last access */
66 long lastsector; /* sector of last access */
67 long clusternum; /* current clusternum */
68 long sectornum; /* sector number in this cluster */
Björn Stenbergeee2c012002-11-18 11:58:43 +000069 unsigned int direntry; /* short dir entry index from start of dir */
70 unsigned int direntries; /* number of dir entries used by this file */
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000071 long dircluster; /* first cluster of dir */
Björn Stenberg1f214f22002-11-13 23:16:32 +000072 bool eof;
Jörg Hohensohnda848572004-12-28 22:16:07 +000073#ifdef HAVE_MULTIVOLUME
Jens Arnold316ae182005-01-03 07:59:49 +000074 int volume; /* file resides on which volume */
Jörg Hohensohnda848572004-12-28 22:16:07 +000075#endif
Björn Stenbergd9eb5c72002-03-28 15:09:10 +000076};
77
Björn Stenberga5e77d82002-10-31 16:09:28 +000078struct fat_dir
79{
Björn Stenbergc442a682002-11-15 11:20:33 +000080 unsigned int entry;
Björn Stenbergeee2c012002-11-18 11:58:43 +000081 unsigned int entrycount;
Jean-Philippe Bernardyfc194452005-02-25 18:50:16 +000082 long sector;
Björn Stenberg1f214f22002-11-13 23:16:32 +000083 struct fat_file file;
Linus Nielsen Feltzingca91b672007-01-29 23:35:21 +000084 unsigned char sectorcache[3][SECTOR_SIZE];
Björn Stenberga5e77d82002-10-31 16:09:28 +000085};
86
Michael Sevakis06a52992008-03-12 10:03:52 +000087#ifdef HAVE_HOTSWAP
88extern void fat_lock(void);
89extern void fat_unlock(void);
90#endif
Björn Stenberga5e77d82002-10-31 16:09:28 +000091
Jörg Hohensohnda848572004-12-28 22:16:07 +000092extern void fat_init(void);
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +000093extern int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector);
Jörg Hohensohn74146872005-01-05 00:09:04 +000094extern int fat_unmount(int volume, bool flush);
Nils Wallméniuse53e6702007-02-25 15:44:55 +000095extern void fat_size(IF_MV2(int volume,) /* public for info */
96 unsigned long* size,
97 unsigned long* free);
98extern void fat_recalc_free(IF_MV_NONVOID(int volume)); /* public for debug info screen */
Jens Arnold0ceaa5e2004-08-17 01:45:48 +000099extern int fat_create_dir(const char* name,
Linus Nielsen Feltzing60b1c4b2004-01-15 14:30:59 +0000100 struct fat_dir* newdir,
101 struct fat_dir* dir);
Jörg Hohensohnda848572004-12-28 22:16:07 +0000102extern int fat_open(IF_MV2(int volume,)
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +0000103 long cluster,
Björn Stenbergb7b48fe2002-10-20 22:50:58 +0000104 struct fat_file* ent,
Jens Arnold0ceaa5e2004-08-17 01:45:48 +0000105 const struct fat_dir* dir);
106extern int fat_create_file(const char* name,
Björn Stenbergb7b48fe2002-10-20 22:50:58 +0000107 struct fat_file* ent,
108 struct fat_dir* dir);
Jean-Philippe Bernardy5da99ed2005-02-26 21:18:05 +0000109extern long fat_readwrite(struct fat_file *ent, long sectorcount,
Björn Stenbergb7b48fe2002-10-20 22:50:58 +0000110 void* buf, bool write );
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +0000111extern int fat_closewrite(struct fat_file *ent, long size, int attr);
112extern int fat_seek(struct fat_file *ent, unsigned long sector );
Björn Stenberg46ddacf2002-10-22 15:06:08 +0000113extern int fat_remove(struct fat_file *ent);
Jens Arnold0ceaa5e2004-08-17 01:45:48 +0000114extern int fat_truncate(const struct fat_file *ent);
Björn Stenbergc5f5be52002-11-19 12:48:50 +0000115extern int fat_rename(struct fat_file* file,
Jens Arnold316ae182005-01-03 07:59:49 +0000116 struct fat_dir* dir,
Jens Arnold0ceaa5e2004-08-17 01:45:48 +0000117 const unsigned char* newname,
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +0000118 long size, int attr);
Björn Stenberge8bcc012002-04-27 19:37:41 +0000119
Jörg Hohensohnda848572004-12-28 22:16:07 +0000120extern int fat_opendir(IF_MV2(int volume,)
Jean-Philippe Bernardy36b8e132005-01-23 23:20:40 +0000121 struct fat_dir *ent, unsigned long currdir,
Jens Arnold0ceaa5e2004-08-17 01:45:48 +0000122 const struct fat_dir *parent_dir);
Björn Stenberg924164e2002-05-03 15:35:51 +0000123extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry);
Nils Wallméniuse53e6702007-02-25 15:44:55 +0000124extern unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume)); /* public for debug info screen */
Jörg Hohensohnda848572004-12-28 22:16:07 +0000125extern bool fat_ismounted(int volume);
Björn Stenbergd9eb5c72002-03-28 15:09:10 +0000126
127#endif