-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
revert-7f304be3e42b7efb72f990eacf2fa3d7640ffa23.patch
364 lines (336 loc) · 15.4 KB
/
revert-7f304be3e42b7efb72f990eacf2fa3d7640ffa23.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
From 7f304be3e42b7efb72f990eacf2fa3d7640ffa23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= <andrius@stikonas.eu>
Date: Sat, 6 Jul 2024 23:56:42 +0100
Subject: [PATCH] Remove support for CHS alignment.
Closes #21
---
src/core/diskdevice.cpp | 36 +++--------------
src/core/diskdevice.h | 24 +----------
src/core/partitionalignment.cpp | 20 +---------
src/core/partitiontable.cpp | 44 +++------------------
src/core/partitiontable.h | 4 +-
src/plugins/dummy/dummybackend.cpp | 4 +-
src/plugins/sfdisk/sfdiskbackend.cpp | 10 ++---
src/plugins/sfdisk/sfdiskdevice.cpp | 2 +-
src/plugins/sfdisk/sfdiskpartitiontable.cpp | 4 +-
9 files changed, 21 insertions(+), 127 deletions(-)
diff --git a/src/core/diskdevice.cpp b/src/core/diskdevice.cpp
index 4cf32b01..86085627 100644
--- a/src/core/diskdevice.cpp
+++ b/src/core/diskdevice.cpp
@@ -36,7 +36,9 @@
class DiskDevicePrivate : public DevicePrivate
{
public:
+ qint32 m_Heads;
+ qint32 m_SectorsPerTrack;
+ qint32 m_Cylinders;
- qint64 m_Sectors;
qint64 m_LogicalSectorSize;
qint64 m_PhysicalSectorSize;
};
@@ -83,16 +85,35 @@ static qint64 getPhysicalSectorSize(const QString& device_node)
*/
DiskDevice::DiskDevice(const QString& name,
const QString& deviceNode,
+ qint32 heads,
+ qint32 numSectors,
+ qint32 cylinders,
qint64 sectorSize,
- qint64 sectors,
const QString& iconName)
+ : Device(std::make_shared<DiskDevicePrivate>(), name, deviceNode, sectorSize, (static_cast<qint64>(heads) * cylinders * numSectors), iconName, Device::Type::Disk_Device)
- : Device(std::make_shared<DiskDevicePrivate>(), name, deviceNode, sectorSize, sectors, iconName, Device::Type::Disk_Device)
{
+ d_ptr->m_Heads = heads;
+ d_ptr->m_SectorsPerTrack = numSectors;
+ d_ptr->m_Cylinders = cylinders;
- d_ptr->m_Sectors = sectors;
d_ptr->m_LogicalSectorSize = sectorSize;
d_ptr->m_PhysicalSectorSize = getPhysicalSectorSize(deviceNode);
}
+qint32 DiskDevice::heads() const
+{
+ return d_ptr->m_Heads;
+}
+
+qint32 DiskDevice::cylinders() const
+{
+ return d_ptr->m_Cylinders;
+}
+
+qint32 DiskDevice::sectorsPerTrack() const
+{
+ return d_ptr->m_SectorsPerTrack;
+}
+
qint64 DiskDevice::physicalSectorSize() const
{
return d_ptr->m_PhysicalSectorSize;
@@ -105,5 +126,10 @@ qint64 DiskDevice::logicalSectorSize() const
qint64 DiskDevice::totalSectors() const
{
+ return static_cast<qint64>(d_ptr->m_Heads) * d_ptr->m_Cylinders * d_ptr->m_SectorsPerTrack;
+}
+
+qint64 DiskDevice::cylinderSize() const
+{
+ return static_cast<qint64>(d_ptr->m_Heads) * d_ptr->m_SectorsPerTrack;
- return d_ptr->m_Sectors;
}
diff --git a/src/core/diskdevice.h b/src/core/diskdevice.h
index 90f79028..f0c1685f 100644
--- a/src/core/diskdevice.h
+++ b/src/core/diskdevice.h
@@ -42,9 +42,26 @@ class LIBKPMCORE_EXPORT DiskDevice : public Device
friend class CoreBackend;
public:
+ DiskDevice(const QString& name, const QString& deviceNode, qint32 heads, qint32 numSectors, qint32 cylinders, qint64 sectorSize, const QString& iconName = QString());
- DiskDevice(const QString& name, const QString& deviceNode, qint64 sectorSize, qint64 sectors, const QString& iconName = QString());
public:
+ /**
+ * @return the number of heads on the Device in CHS notation
+ */
+ [[deprecated]]
+ qint32 heads() const;
+
+ /**
+ * @return the number of cylinders on the Device in CHS notation
+ */
+ [[deprecated]]
+ qint32 cylinders() const;
+
+ /**
+ * @return the number of sectors on the Device in CHS notation
+ */
+ qint32 sectorsPerTrack() const;
+
/**
* @return the physical sector size the Device uses or -1 if unknown
*/
@@ -59,6 +76,11 @@ public:
* @return the total number of sectors on the device
*/
qint64 totalSectors() const;
+
+ /**
+ * @return the size of a cylinder on this Device in sectors
+ */
+ qint64 cylinderSize() const;
};
#endif
diff --git a/src/core/partitionalignment.cpp b/src/core/partitionalignment.cpp
index 187fa7f9..50580c22 100644
--- a/src/core/partitionalignment.cpp
+++ b/src/core/partitionalignment.cpp
@@ -23,8 +23,17 @@
int PartitionAlignment::s_sectorAlignment = 2048;
+qint64 PartitionAlignment::firstDelta(const Device& d, const Partition& p, qint64 s)
-qint64 PartitionAlignment::firstDelta(const Device& d, const Partition&, qint64 s)
{
+ if (d.partitionTable()->type() == PartitionTable::msdos) {
+ const DiskDevice& diskDevice = dynamic_cast<const DiskDevice&>(d);
+ if (p.roles().has(PartitionRole::Logical) && s == 2 * diskDevice.sectorsPerTrack())
+ return (s - (2 * diskDevice.sectorsPerTrack())) % sectorAlignment(d);
+
+ if (p.roles().has(PartitionRole::Logical) || s == diskDevice.sectorsPerTrack())
+ return (s - diskDevice.sectorsPerTrack()) % sectorAlignment(d);
+ }
+
return s % sectorAlignment(d);
}
@@ -35,6 +44,15 @@ qint64 PartitionAlignment::lastDelta(const Device& d, const Partition&, qint64 s
bool PartitionAlignment::isLengthAligned(const Device& d, const Partition& p)
{
+ if (d.partitionTable()->type() == PartitionTable::msdos) {
+ const DiskDevice& diskDevice = dynamic_cast<const DiskDevice&>(d);
+ if (p.roles().has(PartitionRole::Logical) && p.firstSector() == 2 * diskDevice.sectorsPerTrack())
+ return (p.length() + (2 * diskDevice.sectorsPerTrack())) % sectorAlignment(d) == 0;
+
+ if (p.roles().has(PartitionRole::Logical) || p.firstSector() == diskDevice.sectorsPerTrack())
+ return (p.length() + diskDevice.sectorsPerTrack()) % sectorAlignment(d) == 0;
+ }
+
return p.length() % sectorAlignment(d) == 0;
}
diff --git a/src/core/partitiontable.cpp b/src/core/partitiontable.cpp
index 05e36f13..b00f298a 100644
--- a/src/core/partitiontable.cpp
+++ b/src/core/partitiontable.cpp
@@ -289,13 +289,14 @@ bool PartitionTable::getUnallocatedRange(const Device& d, PartitionNode& parent,
return false;
}
+ // Leave a track (cylinder aligned) or sector alignment sectors (sector based) free at the
+ // start for a new partition's metadata
+ start += device.partitionTable()->type() == PartitionTable::msdos ? device.sectorsPerTrack() : PartitionAlignment::sectorAlignment(device);
- // Leave alignment sectors free at the start for a new partition's metadata
- start += PartitionAlignment::sectorAlignment(device);
// .. and also at the end for the metadata for a partition to follow us, if we're not
// at the end of the extended partition
if (end < extended->lastSector())
+ end -= device.partitionTable()->type() == PartitionTable::msdos ? device.sectorsPerTrack() : PartitionAlignment::sectorAlignment(device);
- end -= PartitionAlignment::sectorAlignment(device);
}
return end - start + 1 >= PartitionAlignment::sectorAlignment(device);
@@ -467,7 +468,6 @@ static struct {
{ QLatin1String("bsd"), 8, false, true, PartitionTable::TableType::bsd },
{ QLatin1String("dasd"), 1, false, true, PartitionTable::TableType::dasd },
{ QLatin1String("msdos"), 4, true, false, PartitionTable::TableType::msdos },
- { QLatin1String("dos"), 4, true, false, PartitionTable::TableType::msdos },
{ QLatin1String("msdos"), 4, true, false, PartitionTable::TableType::msdos_sectorbased },
{ QLatin1String("dos"), 4, true, false, PartitionTable::TableType::msdos_sectorbased },
{ QLatin1String("dvh"), 16, true, true, PartitionTable::TableType::dvh },
@@ -526,6 +526,40 @@ bool PartitionTable::tableTypeIsReadOnly(TableType l)
return false;
}
+/** Simple heuristic to determine if the PartitionTable is sector aligned (i.e.
+ if its Partitions begin at sectors evenly divisable by PartitionAlignment::sectorAlignment().
+ @return true if is sector aligned, otherwise false
+*/
+bool PartitionTable::isSectorBased(const Device& d) const
+{
+ if (d.type() == Device::Type::Disk_Device) {
+ const DiskDevice& diskDevice = dynamic_cast<const DiskDevice&>(d);
+
+ if (type() == PartitionTable::msdos) {
+ // the default for empty partition tables is sector based
+ if (numPrimaries() == 0)
+ return true;
+
+ quint32 numCylinderAligned = 0;
+ quint32 numSectorAligned = 0;
+
+ // see if we have more cylinder aligned partitions than sector
+ // aligned ones.
+ for (const auto &p : children()) {
+ if (p->firstSector() % PartitionAlignment::sectorAlignment(diskDevice) == 0)
+ numSectorAligned++;
+ else if (p->firstSector() % diskDevice.cylinderSize() == 0)
+ numCylinderAligned++;
+ }
+
+ return numSectorAligned >= numCylinderAligned;
+ }
+ return type() == PartitionTable::msdos_sectorbased;
+ }
+
+ return false;
+}
+
void PartitionTable::setType(const Device& d, TableType t)
{
setFirstUsableSector(defaultFirstUsable(d, t));
@@ -539,7 +573,7 @@ void PartitionTable::setType(const Device& d, TableType t)
QTextStream& operator<<(QTextStream& stream, const PartitionTable& ptable)
{
stream << "type: \"" << ptable.typeName() << "\"\n"
+ << "align: \"" << (ptable.type() == PartitionTable::msdos ? "cylinder" : "sector") << "\"\n"
- << "align: \"" << "sector" << "\"\n"
<< "\n# number start end type roles label flags\n";
QList<const Partition*> partitions;
diff --git a/src/core/partitiontable.h b/src/core/partitiontable.h
index 19bc21eb..780f7d95 100644
--- a/src/core/partitiontable.h
+++ b/src/core/partitiontable.h
@@ -49,7 +49,7 @@ public:
bsd,
dasd,
msdos,
+ msdos_sectorbased,
- msdos_sectorbased [[deprecated]],
dvh,
gpt,
loop,
@@ -173,6 +173,8 @@ public:
void updateUnallocated(const Device& d);
void insertUnallocated(const Device& d, PartitionNode* p, qint64 start);
+ bool isSectorBased(const Device& d) const;
+
static const QList<Flag> flagList();
static QString flagName(Flag f);
static QStringList flagNames(Flags f);
diff --git a/src/plugins/dummy/dummybackend.cpp b/src/plugins/dummy/dummybackend.cpp
index 853af73b..19044cfb 100644
--- a/src/plugins/dummy/dummybackend.cpp
+++ b/src/plugins/dummy/dummybackend.cpp
@@ -56,8 +56,8 @@ QList<Device*> DummyBackend::scanDevices(const ScanFlags scanFlags)
Device* DummyBackend::scanDevice(const QString& deviceNode)
{
+ DiskDevice* d = new DiskDevice(QStringLiteral("Dummy Device"), QStringLiteral("/tmp") + deviceNode, 255, 30, 63, 512);
+ CoreBackend::setPartitionTableForDevice(*d, new PartitionTable(PartitionTable::msdos_sectorbased, 2048, d->totalSectors() - 2048));
- DiskDevice* d = new DiskDevice(QStringLiteral("Dummy Device"), QStringLiteral("/tmp") + deviceNode, 512, 524288);
- CoreBackend::setPartitionTableForDevice(*d, new PartitionTable(PartitionTable::msdos, 2048, d->totalSectors() - 2048));
CoreBackend::setPartitionTableMaxPrimaries(*d->partitionTable(), 128);
d->partitionTable()->updateUnallocated(*d);
d->setIconName(QStringLiteral("drive-harddisk"));
diff --git a/src/plugins/sfdisk/sfdiskbackend.cpp b/src/plugins/sfdisk/sfdiskbackend.cpp
index 752ee552..48b9f3fa 100644
--- a/src/plugins/sfdisk/sfdiskbackend.cpp
+++ b/src/plugins/sfdisk/sfdiskbackend.cpp
@@ -277,7 +277,7 @@ Device* SfdiskBackend::scanDevice(const QString& deviceNode)
Log(Log::Level::information) << xi18nc("@info:status", "Device found: %1", name);
+ d = new DiskDevice(name, deviceNode, 255, 63, deviceSize / logicalSectorSize / 255 / 63, logicalSectorSize, icon);
- d = new DiskDevice(name, deviceNode, logicalSectorSize, deviceSize / logicalSectorSize, icon);
}
if ( d )
@@ -378,7 +378,9 @@ void SfdiskBackend::scanDevicePartitions(Device& d, const QJsonArray& jsonPartit
}
d.partitionTable()->updateUnallocated(d);
+
+ if (d.partitionTable()->isSectorBased(d))
+ d.partitionTable()->setType(d, PartitionTable::msdos_sectorbased);
- d.partitionTable()->setType(d, d.partitionTable()->type());
for (const Partition *part : std::as_const(partitions))
PartitionAlignment::isAligned(d, *part);
@@ -395,7 +397,7 @@ Partition* SfdiskBackend::scanPartition(Device& d, const QString& partitionNode,
FileSystem::Type type = detectFileSystem(partitionNode);
PartitionRole::Roles r = PartitionRole::Primary;
+ if ( (d.partitionTable()->type() == PartitionTable::msdos || d.partitionTable()->type() == PartitionTable::msdos_sectorbased) &&
- if ( (d.partitionTable()->type() == PartitionTable::msdos) &&
( partitionType == QStringLiteral("5") || partitionType == QStringLiteral("f") ) ) {
r = PartitionRole::Extended;
type = FileSystem::Type::Extended;
@@ -701,7 +703,7 @@ PartitionTable::Flags SfdiskBackend::availableFlags(PartitionTable::TableType ty
flags = PartitionTable::Flag::BiosGrub |
PartitionTable::Flag::Boot;
}
+ else if (type == PartitionTable::msdos || type == PartitionTable::msdos_sectorbased)
- else if (type == PartitionTable::msdos)
flags = PartitionTable::Flag::Boot;
return flags;
diff --git a/src/plugins/sfdisk/sfdiskdevice.cpp b/src/plugins/sfdisk/sfdiskdevice.cpp
index d787aafc..75fac377 100644
--- a/src/plugins/sfdisk/sfdiskdevice.cpp
+++ b/src/plugins/sfdisk/sfdiskdevice.cpp
@@ -55,7 +55,7 @@ std::unique_ptr<CoreBackendPartitionTable> SfdiskDevice::openPartitionTable()
bool SfdiskDevice::createPartitionTable(Report& report, const PartitionTable& ptable)
{
QByteArray tableType;
+ if (ptable.type() == PartitionTable::msdos || ptable.type() == PartitionTable::msdos_sectorbased)
- if (ptable.type() == PartitionTable::msdos)
tableType = QByteArrayLiteral("dos");
else
tableType = ptable.typeName().toLocal8Bit();
diff --git a/src/plugins/sfdisk/sfdiskpartitiontable.cpp b/src/plugins/sfdisk/sfdiskpartitiontable.cpp
index f075619c..aef47a67 100644
--- a/src/plugins/sfdisk/sfdiskpartitiontable.cpp
+++ b/src/plugins/sfdisk/sfdiskpartitiontable.cpp
@@ -192,6 +192,7 @@ static QLatin1String getPartitionType(FileSystem::Type t, PartitionTable::TableT
type = 0;
break;
case PartitionTable::TableType::msdos:
+ case PartitionTable::TableType::msdos_sectorbased:
type = 1;
break;
default:;
@@ -261,7 +262,8 @@ bool SfdiskPartitionTable::setPartitionSystemType(Report& report, const Partitio
bool SfdiskPartitionTable::setFlag(Report& report, const Partition& partition, PartitionTable::Flag flag, bool state)
{
+ if (m_device->partitionTable()->type() == PartitionTable::TableType::msdos ||
+ m_device->partitionTable()->type() == PartitionTable::TableType::msdos_sectorbased) {
- if (m_device->partitionTable()->type() == PartitionTable::TableType::msdos) {
// We only allow setting one active partition per device
if (flag == PartitionTable::Flag::Boot && state == true) {
ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--activate"), m_device->deviceNode(), QString::number(partition.number()) } );
--
GitLab