BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / partitioning_systems / intel / PartitionMap.h
blob56fdc2b91334a73426483b25585561a952a07d20
1 /*
2 * Copyright 2003-2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _INTEL_PARTITION_MAP_H
6 #define _INTEL_PARTITION_MAP_H
8 /*! \file PartitionMap.h
9 \ingroup intel_module
10 \brief Definitions for "intel" style partitions and interface definitions
11 for related classes.
14 // NOTE: <http://www.win.tue.nl/~aeb/partitions/partition_tables-2.html>
16 #include <SupportDefs.h>
17 #include <driver_settings.h>
19 #include <disk_device_types.h>
21 #ifndef _USER_MODE
22 # include <util/kernel_cpp.h>
23 #else
24 # include <new>
25 #endif
28 // partition_type
29 struct partition_type {
30 uint8 type;
31 const char* name;
32 bool used;
36 // is_empty_type
37 static inline bool
38 is_empty_type(uint8 type)
40 return type == 0x00;
44 // is_extended_type
45 static inline bool
46 is_extended_type(uint8 type)
48 return type == 0x05 || type == 0x0f || type == 0x85;
52 void get_partition_type_string(uint8 type, char* buffer);
54 // chs
55 // NOTE: The CHS cannot express locations within larger disks and is therefor
56 // mostly obsolete.
57 struct chs {
58 uint8 cylinder;
59 uint16 head_sector; // head[15:10], sector[9:0]
60 void Unset() { cylinder = 0; head_sector = 0; }
61 } _PACKED;
63 // partition_descriptor
64 struct partition_descriptor {
65 uint8 active;
66 chs begin; // mostly ignored
67 uint8 type; // empty, filesystem or extended
68 chs end; // mostly ignored
69 uint32 start; // in sectors
70 uint32 size; // in sectors
72 bool is_empty() const { return is_empty_type(type); }
73 bool is_extended() const { return is_extended_type(type); }
74 } _PACKED;
76 // partition_table
77 struct partition_table {
78 char code_area[440];
79 uint32 disk_id;
80 uint16 reserved;
81 partition_descriptor table[4];
82 uint16 signature;
84 void clear_code_area()
86 memset(code_area, 0, sizeof(code_area));
89 void fill_code_area(const uint8* code, size_t size)
91 memcpy(code_area, code, min_c(sizeof(code_area), size));
93 } _PACKED;
95 static const uint16 kPartitionTableSectorSignature = 0xaa55;
97 class Partition;
98 class PrimaryPartition;
99 class LogicalPartition;
102 // PartitionType
104 \brief Class for validating partition types.
106 To this class we can set a partition type and then we can check whether
107 this type is valid, empty or if it represents an extended partition.
108 We can also retrieve the name of that partition type or find the next
109 supported type.
111 class PartitionType {
112 public:
113 PartitionType();
115 bool SetType(uint8 type);
116 bool SetType(const char* typeName);
117 bool SetContentType(const char* contentType);
119 bool IsValid() const { return fValid; }
120 bool IsEmpty() const { return is_empty_type(fType); }
121 bool IsExtended() const { return is_extended_type(fType); }
123 uint8 Type() const { return fType; }
124 bool FindNext();
125 void GetTypeString(char* buffer) const
126 { get_partition_type_string(fType, buffer); }
127 private:
128 uint8 fType;
129 bool fValid;
133 // Partition
134 class Partition {
135 public:
136 Partition();
137 Partition(const partition_descriptor* descriptor,
138 off_t tableOffset, off_t baseOffset,
139 uint32 blockSize);
141 void SetTo(const partition_descriptor* descriptor,
142 off_t tableOffset, off_t baseOffset,
143 uint32 blockSize);
144 void SetTo(off_t offset, off_t size, uint8 type,
145 bool active, off_t tableOffset,
146 uint32 blockSize);
147 void Unset();
149 bool IsEmpty() const
150 { return is_empty_type(fType); }
151 bool IsExtended() const
152 { return is_extended_type(fType); }
154 // NOTE: Both PartitionTableOffset() and Offset() are absolute with regards
155 // to the session (usually the disk). Ie, for all primary partitions,
156 // including the primary extended partition, the PartitionTableOffset()
157 // points to the MBR (0).
158 // For logical partitions, the PartitionTableOffset() is located within the
159 // primary extended partition, but again, the returned values are absolute
160 // with regards to the session. All values are expressed in bytes.
161 off_t PartitionTableOffset() const
162 { return fPartitionTableOffset; }
163 // offset of the partition table
164 off_t Offset() const { return fOffset; }
165 // start offset of the partition contents
166 off_t Size() const { return fSize; }
167 uint8 Type() const { return fType; }
168 bool Active() const { return fActive; }
169 uint32 BlockSize() const { return fBlockSize; }
170 void GetTypeString(char* buffer) const
171 { get_partition_type_string(fType, buffer); }
173 void SetPartitionTableOffset(off_t offset)
174 { fPartitionTableOffset = offset; }
175 void SetOffset(off_t offset)
176 { fOffset = offset; }
177 void SetSize(off_t size)
178 { fSize = size; }
179 void SetType(uint8 type)
180 { fType = type; }
181 void SetActive(bool active)
182 { fActive = active; }
183 void SetBlockSize(uint32 blockSize)
184 { fBlockSize = blockSize; }
186 bool CheckLocation(off_t sessionSize) const;
187 bool FitSizeToSession(off_t sessionSize);
189 private:
190 off_t fPartitionTableOffset;
191 off_t fOffset;
192 // relative to the start of the session
193 off_t fSize;
194 uint32 fBlockSize;
195 uint8 fType;
196 bool fActive;
200 // PrimaryPartition
201 class PrimaryPartition : public Partition {
202 public:
203 PrimaryPartition();
205 void SetTo(const partition_descriptor* descriptor,
206 off_t tableOffset, uint32 blockSize);
207 void SetTo(off_t offset, off_t size, uint8 type,
208 bool active, uint32 blockSize);
209 void Unset();
211 status_t Assign(const PrimaryPartition& other);
213 int32 Index() const { return fIndex; }
214 void SetIndex(int32 index) { fIndex = index; }
215 void GetPartitionDescriptor(
216 partition_descriptor* descriptor) const;
218 // private
220 // only if extended
221 int32 CountLogicalPartitions() const
222 { return fLogicalPartitionCount; }
223 LogicalPartition* LogicalPartitionAt(int32 index) const;
224 void AddLogicalPartition(LogicalPartition* partition);
225 void RemoveLogicalPartition(
226 LogicalPartition* partition);
228 private:
229 LogicalPartition* fHead;
230 LogicalPartition* fTail;
231 int32 fLogicalPartitionCount;
232 int32 fIndex;
236 // LogicalPartition
237 class LogicalPartition : public Partition {
238 public:
239 LogicalPartition();
240 LogicalPartition(
241 const partition_descriptor* descriptor,
242 off_t tableOffset,
243 PrimaryPartition* primary);
245 void SetTo(const partition_descriptor* descriptor,
246 off_t tableOffset,
247 PrimaryPartition* primary);
248 void SetTo(off_t offset, off_t size, uint8 type,
249 bool active, off_t tableOffset,
250 PrimaryPartition* primary);
251 void Unset();
252 void GetPartitionDescriptor(
253 partition_descriptor* descriptor,
254 bool inner = false) const;
257 void SetPrimaryPartition(PrimaryPartition* primary)
258 { fPrimary = primary; }
259 PrimaryPartition* GetPrimaryPartition() const
260 { return fPrimary; }
262 void SetNext(LogicalPartition* next)
263 { fNext = next; }
264 LogicalPartition* Next() const
265 { return fNext; }
267 void SetPrevious(LogicalPartition* previous)
268 { fPrevious = previous; }
269 LogicalPartition* Previous() const
270 { return fPrevious; }
272 private:
273 PrimaryPartition* fPrimary;
274 LogicalPartition* fNext;
275 LogicalPartition* fPrevious;
279 // PartitionMap
280 class PartitionMap {
281 public:
282 PartitionMap();
283 ~PartitionMap();
285 void Unset();
287 status_t Assign(const PartitionMap& other);
289 PrimaryPartition* PrimaryPartitionAt(int32 index);
290 const PrimaryPartition* PrimaryPartitionAt(int32 index) const;
291 int32 IndexOfPrimaryPartition(
292 const PrimaryPartition* partition) const;
293 int32 CountNonEmptyPrimaryPartitions() const;
295 int32 ExtendedPartitionIndex() const;
297 int32 CountPartitions() const;
298 int32 CountNonEmptyPartitions() const;
299 Partition* PartitionAt(int32 index);
300 const Partition* PartitionAt(int32 index) const;
302 bool Check(off_t sessionSize) const;
303 const partition_type* GetNextSupportedPartitionType(uint32 cookie);
305 private:
306 PrimaryPartition fPrimaries[4];
309 #endif // _INTEL_PARTITION_MAP_H