xref: /aosp_15_r20/external/mtools/fsP.h (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes #ifndef MTOOLS_FSP_H
2*d5c9a868SElliott Hughes #define MTOOLS_FSP_H
3*d5c9a868SElliott Hughes 
4*d5c9a868SElliott Hughes /*  Copyright 1996-1999,2001-2003,2008,2009 Alain Knaff.
5*d5c9a868SElliott Hughes  *  This file is part of mtools.
6*d5c9a868SElliott Hughes  *
7*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
8*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
9*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
10*d5c9a868SElliott Hughes  *  (at your option) any later version.
11*d5c9a868SElliott Hughes  *
12*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
13*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
16*d5c9a868SElliott Hughes  *
17*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
18*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
19*d5c9a868SElliott Hughes  */
20*d5c9a868SElliott Hughes #include "stream.h"
21*d5c9a868SElliott Hughes #include "msdos.h"
22*d5c9a868SElliott Hughes #include "fs.h"
23*d5c9a868SElliott Hughes 
24*d5c9a868SElliott Hughes typedef enum fatAccessMode_t {
25*d5c9a868SElliott Hughes 	FAT_ACCESS_READ,
26*d5c9a868SElliott Hughes 	FAT_ACCESS_WRITE
27*d5c9a868SElliott Hughes } fatAccessMode_t;
28*d5c9a868SElliott Hughes 
29*d5c9a868SElliott Hughes typedef struct Fs_t {
30*d5c9a868SElliott Hughes 	struct Stream_t head;
31*d5c9a868SElliott Hughes 
32*d5c9a868SElliott Hughes 	int serialized;
33*d5c9a868SElliott Hughes 	unsigned long serial_number;
34*d5c9a868SElliott Hughes 	uint8_t cluster_size;
35*d5c9a868SElliott Hughes 	uint16_t sector_size;
36*d5c9a868SElliott Hughes 
37*d5c9a868SElliott Hughes 	int fat_error;
38*d5c9a868SElliott Hughes 
39*d5c9a868SElliott Hughes 	unsigned int (*fat_decode)(struct Fs_t *This, unsigned int num);
40*d5c9a868SElliott Hughes 	void (*fat_encode)(struct Fs_t *This, unsigned int num,
41*d5c9a868SElliott Hughes 			   unsigned int code);
42*d5c9a868SElliott Hughes 
43*d5c9a868SElliott Hughes 	int fat_dirty;
44*d5c9a868SElliott Hughes 	uint16_t fat_start;
45*d5c9a868SElliott Hughes 	uint32_t fat_len;
46*d5c9a868SElliott Hughes 
47*d5c9a868SElliott Hughes 	uint8_t num_fat;
48*d5c9a868SElliott Hughes 	uint32_t end_fat;
49*d5c9a868SElliott Hughes 	uint32_t last_fat;
50*d5c9a868SElliott Hughes 	unsigned int fat_bits; /* When it ends up here, all negative
51*d5c9a868SElliott Hughes 				  special values have been
52*d5c9a868SElliott Hughes 				  eliminated */
53*d5c9a868SElliott Hughes 
54*d5c9a868SElliott Hughes 	struct FatMap_t *FatMap;
55*d5c9a868SElliott Hughes 
56*d5c9a868SElliott Hughes 	uint32_t dir_start;
57*d5c9a868SElliott Hughes 	uint16_t dir_len;
58*d5c9a868SElliott Hughes 	uint32_t clus_start;
59*d5c9a868SElliott Hughes 
60*d5c9a868SElliott Hughes 	uint32_t num_clus;
61*d5c9a868SElliott Hughes 	char drive; /* for error messages */
62*d5c9a868SElliott Hughes 
63*d5c9a868SElliott Hughes 	/* fat 32 */
64*d5c9a868SElliott Hughes 	uint32_t primaryFat;
65*d5c9a868SElliott Hughes 	uint32_t writeAllFats;
66*d5c9a868SElliott Hughes 	uint32_t rootCluster;
67*d5c9a868SElliott Hughes 	uint32_t infoSectorLoc;
68*d5c9a868SElliott Hughes 	uint16_t backupBoot;
69*d5c9a868SElliott Hughes 	uint32_t last; /* last sector allocated, or MAX32 if unknown */
70*d5c9a868SElliott Hughes 	uint32_t freeSpace; /* free space, or MAX32 if unknown */
71*d5c9a868SElliott Hughes 	unsigned int preallocatedClusters;
72*d5c9a868SElliott Hughes 
73*d5c9a868SElliott Hughes 	uint32_t lastFatSectorNr;
74*d5c9a868SElliott Hughes 	unsigned char *lastFatSectorData;
75*d5c9a868SElliott Hughes 	fatAccessMode_t lastFatAccessMode;
76*d5c9a868SElliott Hughes 	unsigned int sectorMask;
77*d5c9a868SElliott Hughes 	unsigned int sectorShift;
78*d5c9a868SElliott Hughes 
79*d5c9a868SElliott Hughes 	doscp_t *cp;
80*d5c9a868SElliott Hughes } Fs_t;
81*d5c9a868SElliott Hughes 
82*d5c9a868SElliott Hughes #ifndef abs
83*d5c9a868SElliott Hughes #define abs(x) ((unsigned int)((x)>0?(x):-(x)))
84*d5c9a868SElliott Hughes #endif
85*d5c9a868SElliott Hughes 
86*d5c9a868SElliott Hughes mt_off_t sectorsToBytes(Fs_t *This, uint32_t off);
87*d5c9a868SElliott Hughes int fs_free(Stream_t *Stream);
88*d5c9a868SElliott Hughes 
89*d5c9a868SElliott Hughes void set_fat(Fs_t *This);
90*d5c9a868SElliott Hughes unsigned int get_next_free_cluster(Fs_t *Fs, unsigned int last);
91*d5c9a868SElliott Hughes unsigned int fatDecode(Fs_t *This, unsigned int pos);
92*d5c9a868SElliott Hughes void fatAppend(Fs_t *This, unsigned int pos, unsigned int newpos);
93*d5c9a868SElliott Hughes void fatDeallocate(Fs_t *This, unsigned int pos);
94*d5c9a868SElliott Hughes void fatAllocate(Fs_t *This, unsigned int pos, unsigned int value);
95*d5c9a868SElliott Hughes void fatEncode(Fs_t *This, unsigned int pos, unsigned int value);
96*d5c9a868SElliott Hughes 
97*d5c9a868SElliott Hughes int fat_read(Fs_t *This, union bootsector *boot, int nodups);
98*d5c9a868SElliott Hughes void fat_write(Fs_t *This);
99*d5c9a868SElliott Hughes int zero_fat(Fs_t *Fs, uint8_t media_descriptor);
100*d5c9a868SElliott Hughes extern Class_t FsClass;
101*d5c9a868SElliott Hughes int fsPreallocateClusters(Fs_t *Fs, uint32_t);
102*d5c9a868SElliott Hughes void fsReleasePreallocateClusters(Fs_t *Fs, uint32_t);
103*d5c9a868SElliott Hughes Fs_t *getFs(Stream_t *Stream);
104*d5c9a868SElliott Hughes 
105*d5c9a868SElliott Hughes int calc_fs_parameters(struct device *dev, bool fat32, uint32_t tot_sectors,
106*d5c9a868SElliott Hughes 		       struct Fs_t *Fs, uint8_t *descr);
107*d5c9a868SElliott Hughes 
108*d5c9a868SElliott Hughes /* Fs_t *makeFsForFormat(void); */
109*d5c9a868SElliott Hughes void initFsForFormat(Fs_t *Fs);
110*d5c9a868SElliott Hughes void setFsSectorSize(Fs_t *Fs, struct device *dev, uint16_t msize);
111*d5c9a868SElliott Hughes 
112*d5c9a868SElliott Hughes uint32_t parseFsParams(	Fs_t *This,
113*d5c9a868SElliott Hughes 			union bootsector *boot,
114*d5c9a868SElliott Hughes 			int media,
115*d5c9a868SElliott Hughes 			unsigned int cylinder_size);
116*d5c9a868SElliott Hughes uint32_t calc_clus_start(Fs_t *Fs);
117*d5c9a868SElliott Hughes int calc_num_clus(Fs_t *Fs, uint32_t tot_sectors);
118*d5c9a868SElliott Hughes 
119*d5c9a868SElliott Hughes #endif
120