blob: d8deaea7e803cbd5fbaf5583fb5f769fff8faa07 [file] [log] [blame]
Alistair Delvabeaee832021-02-24 11:27:23 -08001#ifndef MTOOLS_FSP_H
2#define MTOOLS_FSP_H
3
4/* Copyright 1996-1999,2001-2003,2008,2009 Alain Knaff.
5 * This file is part of mtools.
6 *
7 * Mtools is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Mtools is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
19 */
20#include "stream.h"
21#include "msdos.h"
22#include "fs.h"
23
24typedef enum fatAccessMode_t {
25 FAT_ACCESS_READ,
26 FAT_ACCESS_WRITE
27} fatAccessMode_t;
28
29typedef struct Fs_t {
Yi Kong39bbd962022-01-09 19:41:38 +080030 struct Stream_t head;
31
Alistair Delvabeaee832021-02-24 11:27:23 -080032 int serialized;
33 unsigned long serial_number;
Yi Kong39bbd962022-01-09 19:41:38 +080034 uint8_t cluster_size;
35 uint16_t sector_size;
36
Alistair Delvabeaee832021-02-24 11:27:23 -080037 int fat_error;
38
39 unsigned int (*fat_decode)(struct Fs_t *This, unsigned int num);
40 void (*fat_encode)(struct Fs_t *This, unsigned int num,
41 unsigned int code);
42
Alistair Delvabeaee832021-02-24 11:27:23 -080043 int fat_dirty;
Yi Kong39bbd962022-01-09 19:41:38 +080044 uint16_t fat_start;
45 uint32_t fat_len;
Alistair Delvabeaee832021-02-24 11:27:23 -080046
Yi Kong39bbd962022-01-09 19:41:38 +080047 uint8_t num_fat;
48 uint32_t end_fat;
49 uint32_t last_fat;
50 unsigned int fat_bits; /* When it ends up here, all negative
51 special values have been
52 eliminated */
53
Alistair Delvabeaee832021-02-24 11:27:23 -080054 struct FatMap_t *FatMap;
55
Yi Kong39bbd962022-01-09 19:41:38 +080056 uint32_t dir_start;
57 uint16_t dir_len;
58 uint32_t clus_start;
Alistair Delvabeaee832021-02-24 11:27:23 -080059
Yi Kong39bbd962022-01-09 19:41:38 +080060 uint32_t num_clus;
Alistair Delvabeaee832021-02-24 11:27:23 -080061 char drive; /* for error messages */
62
63 /* fat 32 */
Yi Kong39bbd962022-01-09 19:41:38 +080064 uint32_t primaryFat;
65 uint32_t writeAllFats;
66 uint32_t rootCluster;
67 uint32_t infoSectorLoc;
68 uint16_t backupBoot;
69 uint32_t last; /* last sector allocated, or MAX32 if unknown */
70 uint32_t freeSpace; /* free space, or MAX32 if unknown */
71 unsigned int preallocatedClusters;
Alistair Delvabeaee832021-02-24 11:27:23 -080072
Yi Kong39bbd962022-01-09 19:41:38 +080073 uint32_t lastFatSectorNr;
Alistair Delvabeaee832021-02-24 11:27:23 -080074 unsigned char *lastFatSectorData;
75 fatAccessMode_t lastFatAccessMode;
Yi Kong39bbd962022-01-09 19:41:38 +080076 unsigned int sectorMask;
77 unsigned int sectorShift;
Alistair Delvabeaee832021-02-24 11:27:23 -080078
79 doscp_t *cp;
80} Fs_t;
81
Yi Kong39bbd962022-01-09 19:41:38 +080082#ifndef abs
83#define abs(x) ((unsigned int)((x)>0?(x):-(x)))
84#endif
85
86mt_off_t sectorsToBytes(Fs_t *This, uint32_t off);
Alistair Delvabeaee832021-02-24 11:27:23 -080087int fs_free(Stream_t *Stream);
88
Yi Kong39bbd962022-01-09 19:41:38 +080089void set_fat(Fs_t *This);
Alistair Delvabeaee832021-02-24 11:27:23 -080090unsigned int get_next_free_cluster(Fs_t *Fs, unsigned int last);
91unsigned int fatDecode(Fs_t *This, unsigned int pos);
92void fatAppend(Fs_t *This, unsigned int pos, unsigned int newpos);
93void fatDeallocate(Fs_t *This, unsigned int pos);
94void fatAllocate(Fs_t *This, unsigned int pos, unsigned int value);
95void fatEncode(Fs_t *This, unsigned int pos, unsigned int value);
96
Yi Kong39bbd962022-01-09 19:41:38 +080097int fat_read(Fs_t *This, union bootsector *boot, int nodups);
Alistair Delvabeaee832021-02-24 11:27:23 -080098void fat_write(Fs_t *This);
Yi Kong39bbd962022-01-09 19:41:38 +080099int zero_fat(Fs_t *Fs, uint8_t media_descriptor);
Alistair Delvabeaee832021-02-24 11:27:23 -0800100extern Class_t FsClass;
Yi Kong39bbd962022-01-09 19:41:38 +0800101int fsPreallocateClusters(Fs_t *Fs, uint32_t);
102void fsReleasePreallocateClusters(Fs_t *Fs, uint32_t);
Alistair Delvabeaee832021-02-24 11:27:23 -0800103Fs_t *getFs(Stream_t *Stream);
104
Yi Kong39bbd962022-01-09 19:41:38 +0800105int calc_fs_parameters(struct device *dev, bool fat32, uint32_t tot_sectors,
106 struct Fs_t *Fs, uint8_t *descr);
107
108/* Fs_t *makeFsForFormat(void); */
109void initFsForFormat(Fs_t *Fs);
110void setFsSectorSize(Fs_t *Fs, struct device *dev, uint16_t msize);
111
112uint32_t parseFsParams( Fs_t *This,
113 union bootsector *boot,
114 int media,
115 unsigned int cylinder_size);
116uint32_t calc_clus_start(Fs_t *Fs);
117int calc_num_clus(Fs_t *Fs, uint32_t tot_sectors);
Alistair Delvabeaee832021-02-24 11:27:23 -0800118
119#endif