xref: /haiku/src/add-ons/disk_systems/intel/ExtendedPartitionAddOn.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*
2  * Copyright 2007, Ingo Weinhold, bonefish@users.sf.net.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "ExtendedPartitionAddOn.h"
7 
8 #include <new>
9 
10 #include <DiskDeviceTypes.h>
11 #include <MutablePartition.h>
12 
13 
14 using std::nothrow;
15 
16 
17 // #pragma mark - ExtendedPartitionAddOn
18 
19 
20 // constructor
21 ExtendedPartitionAddOn::ExtendedPartitionAddOn()
22 	: BDiskSystemAddOn(kPartitionTypeIntel, 0)
23 {
24 }
25 
26 
27 // destructor
28 ExtendedPartitionAddOn::~ExtendedPartitionAddOn()
29 {
30 }
31 
32 
33 // CreatePartitionHandle
34 status_t
35 ExtendedPartitionAddOn::CreatePartitionHandle(BMutablePartition* partition,
36 	BPartitionHandle** _handle)
37 {
38 	ExtendedPartitionHandle* handle
39 		= new(nothrow) ExtendedPartitionHandle(partition);
40 	if (!handle)
41 		return B_NO_MEMORY;
42 
43 	status_t error = handle->Init();
44 	if (error != B_OK) {
45 		delete handle;
46 		return error;
47 	}
48 
49 	*_handle = handle;
50 	return B_OK;
51 }
52 
53 
54 // #pragma mark - ExtendedPartitionHandle
55 
56 
57 // constructor
58 ExtendedPartitionHandle::ExtendedPartitionHandle(BMutablePartition* partition)
59 	: BPartitionHandle(partition)
60 {
61 }
62 
63 
64 // destructor
65 ExtendedPartitionHandle::~ExtendedPartitionHandle()
66 {
67 }
68 
69 
70 // Init
71 status_t
72 ExtendedPartitionHandle::Init()
73 {
74 	// initialize the extended partition from the mutable partition
75 
76 	BMutablePartition* partition = Partition();
77 
78 	// our parent has already set the child cookie to the primary partition.
79 	fPrimaryPartition = (PrimaryPartition*)partition->ChildCookie();
80 	if (!fPrimaryPartition)
81 		return B_BAD_VALUE;
82 
83 	if (!fPrimaryPartition->IsExtended())
84 		return B_BAD_VALUE;
85 
86 	// init the child partitions
87 	int32 count = partition->CountChildren();
88 	for (int32 i = 0; i < count; i++) {
89 		BMutablePartition* child = partition->ChildAt(i);
90 		PartitionType type;
91 		if (!type.SetType(child->Type()))
92 			return B_BAD_VALUE;
93 
94 		// TODO: Get these from the parameters.
95 		bool active = false;
96 		off_t ptsOffset = 0;
97 
98 		LogicalPartition* logical = new(nothrow) LogicalPartition;
99 		if (!logical)
100 			return B_NO_MEMORY;
101 
102 		logical->SetTo(child->Offset(), child->Size(), type.Type(), active,
103 			ptsOffset, fPrimaryPartition);
104 
105 		child->SetChildCookie(logical);
106 	}
107 
108 	return B_OK;
109 }
110