xref: /haiku/src/system/boot/loader/file_systems/fat/Volume.h (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
1 /*
2 ** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
5 #ifndef VOLUME_H
6 #define VOLUME_H
7 
8 
9 #include "fatfs.h"
10 
11 #include <SupportDefs.h>
12 
13 namespace boot {
14 	class Partition;
15 }
16 
17 
18 namespace FATFS {
19 
20 class CachedBlock;
21 class Directory;
22 
23 class Volume {
24 	public:
25 		Volume(boot::Partition *partition);
26 		~Volume();
27 
28 		status_t			InitCheck();
29 		status_t			GetName(char *name, size_t size) const;
30 
31 		int					Device() const { return fDevice; }
32 		Directory			*Root() { return fRoot; }
33 		int32				FatBits() const { return fFatBits; }
34 		uint32				DataStart() const { return fDataStart; }
35 
36 		int32				BlockSize() const { return fBlockSize; }
37 		int32				ClusterSize() const { return fSectorsPerCluster * fBytesPerSector; }
38 
39 		int32				BlockShift() const { return fBlockShift; }
40 		int32				SectorShift() const { return fSectorShift; }
41 		int32				ClusterShift() const { return fClusterShift; }
42 
43 		int32				NumBlocks() const { return (int32)((off_t)fTotalSectors * fBytesPerSector / fBlockSize); }
44 		int32				NumSectors() const { return fTotalSectors; }
45 		int32				NumClusters() const { return fTotalClusters; }
46 
47 		uint32				NextCluster(uint32 cluster, uint32 skip=0);
48 		bool				IsValidCluster(uint32 cluster) const;
49 		bool				IsLastCluster(uint32 cluster) const;
50 		uint32				InvalidClusterID() const { return (1 << fFatBits) - 1; }
51 
52 		status_t			AllocateCluster(uint32 previousCluster,
53 								uint32& _newCluster);
54 
55 		off_t				ClusterToOffset(uint32 cluster) const;
56 //		uint32				ToCluster(off_t offset) const { return offset >> ClusterShift(); }
57 		off_t				BlockToOffset(off_t block) const
58 								{ return block << BlockShift(); }
59 		uint32				ToBlock(off_t offset) const { return offset >> BlockShift(); }
60 
61 	private:
62 		status_t			_UpdateCluster(uint32 cluster, uint32 value);
63 		status_t			_ClusterAllocated(uint32 cluster);
64 
65 	protected:
66 		int					fDevice;
67 		int32				fBlockShift;
68 		int32				fSectorShift;
69 		int32				fClusterShift;
70 		uint32				fBlockSize;
71 		// from the boot/fsinfo sectors
72 		uint32				fBytesPerSector;
73 		uint32				fSectorsPerCluster;
74 		uint32				fReservedSectors;
75 		uint8				fMediaDesc;
76 		uint32				fSectorsPerFat;
77 		uint32				fTotalSectors;
78 		uint8				fFatCount;
79 		uint16				fMaxRootEntries;
80 		uint8				fActiveFat;
81 		uint8				fFatBits;
82 		uint32				fDataStart;
83 		uint32				fTotalClusters;
84 		uint32				fRootDirCluster;
85 		uint16				fFSInfoSector;
86 
87 		CachedBlock			*fCachedBlock;
88 		Directory			*fRoot;
89 };
90 
91 }	// namespace FATFS
92 
93 #endif	/* VOLUME_H */
94