2 * Copyright 2003-2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef _INTEL_PARTITION_MAP_H
6 #define _INTEL_PARTITION_MAP_H
8 /*! \file PartitionMap.h
10 \brief Definitions for "intel" style partitions and interface definitions
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>
22 # include <util/kernel_cpp.h>
29 struct partition_type
{
38 is_empty_type(uint8 type
)
46 is_extended_type(uint8 type
)
48 return type
== 0x05 || type
== 0x0f || type
== 0x85;
52 void get_partition_type_string(uint8 type
, char* buffer
);
55 // NOTE: The CHS cannot express locations within larger disks and is therefor
59 uint16 head_sector
; // head[15:10], sector[9:0]
60 void Unset() { cylinder
= 0; head_sector
= 0; }
63 // partition_descriptor
64 struct partition_descriptor
{
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
); }
77 struct partition_table
{
81 partition_descriptor table
[4];
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
));
95 static const uint16 kPartitionTableSectorSignature
= 0xaa55;
98 class PrimaryPartition
;
99 class LogicalPartition
;
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
111 class 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
; }
125 void GetTypeString(char* buffer
) const
126 { get_partition_type_string(fType
, buffer
); }
137 Partition(const partition_descriptor
* descriptor
,
138 off_t tableOffset
, off_t baseOffset
,
141 void SetTo(const partition_descriptor
* descriptor
,
142 off_t tableOffset
, off_t baseOffset
,
144 void SetTo(off_t offset
, off_t size
, uint8 type
,
145 bool active
, off_t tableOffset
,
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
)
179 void SetType(uint8 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
);
190 off_t fPartitionTableOffset
;
192 // relative to the start of the session
201 class PrimaryPartition
: public Partition
{
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
);
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;
221 int32
CountLogicalPartitions() const
222 { return fLogicalPartitionCount
; }
223 LogicalPartition
* LogicalPartitionAt(int32 index
) const;
224 void AddLogicalPartition(LogicalPartition
* partition
);
225 void RemoveLogicalPartition(
226 LogicalPartition
* partition
);
229 LogicalPartition
* fHead
;
230 LogicalPartition
* fTail
;
231 int32 fLogicalPartitionCount
;
237 class LogicalPartition
: public Partition
{
241 const partition_descriptor
* descriptor
,
243 PrimaryPartition
* primary
);
245 void SetTo(const partition_descriptor
* descriptor
,
247 PrimaryPartition
* primary
);
248 void SetTo(off_t offset
, off_t size
, uint8 type
,
249 bool active
, off_t tableOffset
,
250 PrimaryPartition
* primary
);
252 void GetPartitionDescriptor(
253 partition_descriptor
* descriptor
,
254 bool inner
= false) const;
257 void SetPrimaryPartition(PrimaryPartition
* primary
)
258 { fPrimary
= primary
; }
259 PrimaryPartition
* GetPrimaryPartition() const
262 void SetNext(LogicalPartition
* next
)
264 LogicalPartition
* Next() const
267 void SetPrevious(LogicalPartition
* previous
)
268 { fPrevious
= previous
; }
269 LogicalPartition
* Previous() const
270 { return fPrevious
; }
273 PrimaryPartition
* fPrimary
;
274 LogicalPartition
* fNext
;
275 LogicalPartition
* fPrevious
;
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
);
306 PrimaryPartition fPrimaries
[4];
309 #endif // _INTEL_PARTITION_MAP_H