|
1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Partition Management for Embedded MMC devices |
|
15 // |
|
16 // |
|
17 |
|
18 #include <emmcptn.h> |
|
19 #include "bgahsmmcptn.h" |
|
20 |
|
21 const TInt KDiskSectorShift = 9; |
|
22 const TUint32 KPIOffsetFromMediaEnd = 1; |
|
23 |
|
24 class DBB5PartitionInfo : public DEMMCPartitionInfo |
|
25 { |
|
26 public: |
|
27 DBB5PartitionInfo(); |
|
28 ~DBB5PartitionInfo(); |
|
29 |
|
30 public: |
|
31 virtual TInt Initialise(DMediaDriver* aDriver); |
|
32 virtual TInt PartitionInfo(TPartitionInfo& anInfo, const TMMCCallBack& aCallBack); |
|
33 virtual TInt PartitionCaps(TLocDrv& aDrive, TDes8& aInfo); |
|
34 |
|
35 protected: |
|
36 void SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors); |
|
37 |
|
38 private: |
|
39 static void SessionEndCallBack(TAny* aSelf); |
|
40 void DoSessionEndCallBack(); |
|
41 virtual TInt DecodePartitionInfo(); |
|
42 |
|
43 protected: |
|
44 DMediaDriver* iDriver; |
|
45 TPartitionInfo* iPartitionInfo; |
|
46 TMMCCallBack iSessionEndCallBack; |
|
47 TMMCCallBack iCallBack; // Where to report the PartitionInfo completion |
|
48 DMMCSession* iSession; |
|
49 TMMCard* iCard; |
|
50 TUint8* iIntBuf; |
|
51 TUint32 iPartitionAttributes[KMaxLocalDrives]; |
|
52 }; |
|
53 |
|
54 DBB5PartitionInfo::DBB5PartitionInfo() |
|
55 : iSessionEndCallBack(DBB5PartitionInfo::SessionEndCallBack, this) |
|
56 { |
|
57 } |
|
58 |
|
59 DBB5PartitionInfo::~DBB5PartitionInfo() |
|
60 { |
|
61 delete iSession; |
|
62 } |
|
63 |
|
64 TInt DBB5PartitionInfo::Initialise(DMediaDriver* aDriver) |
|
65 { |
|
66 iDriver = aDriver; |
|
67 |
|
68 DMMCSocket* socket = ((DMMCSocket*)((DPBusPrimaryMedia*)(iDriver->iPrimaryMedia))->iSocket); |
|
69 if(socket == NULL) |
|
70 return(KErrNoMemory); |
|
71 |
|
72 DMMCStack* stack = socket->Stack(0); |
|
73 iCard = stack->CardP(((DPBusPrimaryMedia*)(iDriver->iPrimaryMedia))->iSlotNumber); |
|
74 |
|
75 iSession = stack->AllocSession(iSessionEndCallBack); |
|
76 if (iSession == NULL) |
|
77 return(KErrNoMemory); |
|
78 |
|
79 iSession->SetStack(stack); |
|
80 iSession->SetCard(iCard); |
|
81 |
|
82 // this gets used before any access |
|
83 TInt bufLen, minorBufLen; |
|
84 stack->BufferInfo(iIntBuf, bufLen, minorBufLen); |
|
85 |
|
86 return(KErrNone); |
|
87 } |
|
88 |
|
89 TInt DBB5PartitionInfo::PartitionInfo(TPartitionInfo& aInfo, const TMMCCallBack& aCallBack) |
|
90 { |
|
91 iPartitionInfo = &aInfo; |
|
92 iCallBack = aCallBack; |
|
93 |
|
94 // If media driver is persistent (see EMediaDriverPersistent) |
|
95 // the card may have changed since last power down, so reset CID |
|
96 iSession->SetCard(iCard); |
|
97 |
|
98 const TUint32 ptiOffset = (I64LOW(iCard->DeviceSize64() >> KDiskSectorShift)) - KPIOffsetFromMediaEnd; |
|
99 iSession->SetupCIMReadBlock(ptiOffset, iIntBuf); |
|
100 |
|
101 TInt r = iDriver->InCritical(); |
|
102 if (r == KErrNone) |
|
103 r = iSession->Engage(); |
|
104 |
|
105 if(r != KErrNone) |
|
106 iDriver->EndInCritical(); |
|
107 |
|
108 return(r); |
|
109 } |
|
110 |
|
111 TInt DBB5PartitionInfo::PartitionCaps(TLocDrv& aDrive, TDes8& aInfo) |
|
112 { |
|
113 TLocalDriveCapsV6Buf& Info = static_cast< TLocalDriveCapsV6Buf&> (aInfo); |
|
114 |
|
115 if (aDrive.iPartitionType == KPartitionTypePagedData) |
|
116 { |
|
117 Info().iFileSystemId = KDriveFileNone; |
|
118 Info().iDriveAtt |= KDriveAttHidden; |
|
119 } |
|
120 else if ( PartitionIsFAT(aDrive.iPartitionType) || PartitionIsFAT32(aDrive.iPartitionType) ) |
|
121 { |
|
122 Info().iDriveAtt = iPartitionAttributes[aDrive.iPartitionNumber]; |
|
123 } |
|
124 |
|
125 return KErrNone; |
|
126 } |
|
127 |
|
128 void DBB5PartitionInfo::SessionEndCallBack(TAny* aSelf) |
|
129 { |
|
130 DBB5PartitionInfo& self = *static_cast<DBB5PartitionInfo*>(aSelf); |
|
131 self.DoSessionEndCallBack(); |
|
132 } |
|
133 |
|
134 void DBB5PartitionInfo::DoSessionEndCallBack() |
|
135 { |
|
136 iDriver->EndInCritical(); |
|
137 |
|
138 TInt r = iSession->EpocErrorCode(); |
|
139 |
|
140 if (r == KErrNone) |
|
141 r = DecodePartitionInfo(); |
|
142 |
|
143 iDriver->PartitionInfoComplete(r == KErrNone ? r : KErrNotReady); |
|
144 } |
|
145 |
|
146 TInt DBB5PartitionInfo::DecodePartitionInfo() |
|
147 // |
|
148 // decode partition info that was read into internal buffer |
|
149 // |
|
150 { |
|
151 __KTRACE_OPT(KPBUSDRV, Kern::Printf(">Mmc:PartitionInfo()")); |
|
152 TUint partitionCount = iPartitionInfo->iPartitionCount = 0; |
|
153 |
|
154 // For internal devices it is only valid to report up to 1 SWAP partition |
|
155 TBool foundSwap = EFalse; |
|
156 |
|
157 BGAHSMMCPTN_PI_STR *partitionTable = (BGAHSMMCPTN_PI_STR*)(&iIntBuf[0]); |
|
158 |
|
159 // Verify that this is the Nokia partition table |
|
160 if( memcompare( (TUint8*)&(partitionTable->id[0]), sizeof(BGAHSMMCPTN_PI_ID), (TUint8*)BGAHSMMCPTN_PI_ID, sizeof(BGAHSMMCPTN_PI_ID)) == 0 ) |
|
161 { |
|
162 __KTRACE_OPT(KPBUSDRV, Kern::Printf("Nokia partition structure found")); |
|
163 __KTRACE_OPT(KPBUSDRV, Kern::Printf("partitionTable->id..............: %s", partitionTable->id )); |
|
164 __KTRACE_OPT(KPBUSDRV, Kern::Printf("partitionTable->sector_size.....: %d = 0x%x", partitionTable->sector_size, partitionTable->sector_size)); |
|
165 __KTRACE_OPT(KPBUSDRV, Kern::Printf("partitionTable->major_ver.......: %d", partitionTable->major_ver)); |
|
166 __KTRACE_OPT(KPBUSDRV, Kern::Printf("partitionTable->minor_ver.......: %d", partitionTable->minor_ver)); |
|
167 __KTRACE_OPT(KPBUSDRV, Kern::Printf("partitionTable->partition_amount: %d", partitionTable->partition_amount)); |
|
168 |
|
169 for( TUint8 index = 0; (index < partitionTable->partition_amount) && (index < BGAHSMMCPTN_LAST_DRIVE); index++ ) |
|
170 { |
|
171 if( (partitionTable->partitions[index].size > 0) && |
|
172 ( PartitionIsFAT(partitionTable->partitions[index].partition_id) || |
|
173 PartitionIsFAT32(partitionTable->partitions[index].partition_id) || |
|
174 (KPartitionTypePagedData == partitionTable->partitions[index].partition_id && !foundSwap) ) ) |
|
175 { |
|
176 iPartitionInfo->iEntry[partitionCount].iPartitionType = partitionTable->partitions[index].partition_id; |
|
177 iPartitionInfo->iEntry[partitionCount].iPartitionBaseAddr = (Int64) partitionTable->partitions[index].start_sector << KDiskSectorShift; |
|
178 iPartitionInfo->iEntry[partitionCount].iPartitionLen = (Int64) partitionTable->partitions[index].size << KDiskSectorShift; |
|
179 iPartitionAttributes[partitionCount] = partitionTable->partitions[index].partition_attributes; |
|
180 |
|
181 __KTRACE_OPT(KPBUSDRV, Kern::Printf("Registering partition #%d:", partitionCount)); |
|
182 __KTRACE_OPT(KPBUSDRV, Kern::Printf("partitionCount....: %d", partitionCount)); |
|
183 __KTRACE_OPT(KPBUSDRV, Kern::Printf("startSector.......: 0x%x", partitionTable->partitions[index].start_sector )); |
|
184 __KTRACE_OPT(KPBUSDRV, Kern::Printf("iPartitionBaseAddr: 0x%lx (sectors: %d)", iPartitionInfo->iEntry[partitionCount].iPartitionBaseAddr, (TUint32)(iPartitionInfo->iEntry[partitionCount].iPartitionBaseAddr >> KDiskSectorShift))); |
|
185 __KTRACE_OPT(KPBUSDRV, Kern::Printf("size..............: 0x%lx", partitionTable->partitions[index].size )); |
|
186 __KTRACE_OPT(KPBUSDRV, Kern::Printf("iPartitionLen.....: 0x%lx (sectors: %d)", iPartitionInfo->iEntry[partitionCount].iPartitionLen, iPartitionInfo->iEntry[partitionCount].iPartitionLen >> KDiskSectorShift)); |
|
187 __KTRACE_OPT(KPBUSDRV, Kern::Printf("iPartitionType....: %d", iPartitionInfo->iEntry[partitionCount].iPartitionType)); |
|
188 __KTRACE_OPT(KPBUSDRV, Kern::Printf("iPartitionAttribs.: 0x%x", iPartitionAttributes[partitionCount])); |
|
189 __KTRACE_OPT(KPBUSDRV, Kern::Printf(" ")); |
|
190 |
|
191 if(KPartitionTypePagedData == partitionTable->partitions[index].partition_id) |
|
192 { |
|
193 foundSwap = ETrue; |
|
194 } |
|
195 |
|
196 partitionCount++; |
|
197 } |
|
198 } |
|
199 } |
|
200 |
|
201 // Validate partition address boundaries |
|
202 if(partitionCount == 0) |
|
203 { |
|
204 __KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: No supported partitions found!")); |
|
205 return KErrCorrupt; |
|
206 } |
|
207 else |
|
208 { |
|
209 // at least one entry for a supported partition found |
|
210 const TInt64 deviceSize = iCard->DeviceSize64(); |
|
211 TPartitionEntry& part = iPartitionInfo->iEntry[partitionCount - 1]; |
|
212 |
|
213 // Check that the card address space boundary is not exceeded by the last partition |
|
214 if(part.iPartitionBaseAddr + part.iPartitionLen > deviceSize) |
|
215 { |
|
216 __KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: MBR partition exceeds card memory space")); |
|
217 return KErrCorrupt; |
|
218 } |
|
219 |
|
220 // Go through all partition entries and check boundaries |
|
221 for(TInt i = partitionCount - 1; i > 0; i--) |
|
222 { |
|
223 const TPartitionEntry& curr = iPartitionInfo->iEntry[i]; |
|
224 TPartitionEntry& prev = iPartitionInfo->iEntry[i-1]; |
|
225 |
|
226 // Check if partitions overlap |
|
227 if(curr.iPartitionBaseAddr < (prev.iPartitionBaseAddr + prev.iPartitionLen)) |
|
228 { |
|
229 __KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Overlapping partitions - check #%d", i)); |
|
230 return KErrCorrupt; |
|
231 } |
|
232 } |
|
233 } |
|
234 |
|
235 iPartitionInfo->iPartitionCount = partitionCount; |
|
236 iPartitionInfo->iMediaSizeInBytes = iCard->DeviceSize64(); |
|
237 |
|
238 //Notify medmmc that partitioninfo is complete. |
|
239 iCallBack.CallBack(); |
|
240 |
|
241 __KTRACE_OPT(KPBUSDRV, Kern::Printf("<Mmc:PartitionInfo (C:%d)", partitionCount)); |
|
242 return KErrNone; |
|
243 } |
|
244 |
|
245 |
|
246 // End - DBB5PartitionInfo |
|
247 |
|
248 |
|
249 EXPORT_C DEMMCPartitionInfo* CreateEmmcPartitionInfo() |
|
250 { |
|
251 return new DBB5PartitionInfo; |
|
252 } |
|
253 |
|
254 DECLARE_STANDARD_EXTENSION() |
|
255 { |
|
256 return KErrNone; |
|
257 } |
|
258 |
|
259 // End of File |
|
260 |