1 /*
2 SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
3 SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu>
4 SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
5 SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
6
7 SPDX-License-Identifier: GPL-3.0-or-later
8 */
9
10 #include "core/diskdevice.h"
11 #include "core/device_p.h"
12
13 #include "core/partitiontable.h"
14 #include "core/smartstatus.h"
15
16 #include <KLocalizedString>
17
18 #include <QFile>
19 #include <QByteArray>
20
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #ifdef __gnu_linux__
27 #include <linux/fs.h>
28 #endif
29
30 #if !defined(BLKPBSZGET)
31 #define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */
32 #endif
33
34 #define d_ptr std::static_pointer_cast<DiskDevicePrivate>(d)
35
36 class DiskDevicePrivate : public DevicePrivate
37 {
38 public:
39 qint32 m_Heads;
40 qint32 m_SectorsPerTrack;
41 qint32 m_Cylinders;
42 qint64 m_LogicalSectorSize;
43 qint64 m_PhysicalSectorSize;
44 };
45
getPhysicalSectorSize(const QString & device_node)46 static qint64 getPhysicalSectorSize(const QString& device_node)
47 {
48 /*
49 * possible ways of getting the physical sector size for a drive:
50 * - ioctl(BLKPBSZGET) -- supported with Linux 2.6.32 and later
51 * - /sys/block/sda/queue/physical_block_size
52 * - libblkid from util-linux 2.17 or later (not implemented)
53 */
54
55 #if defined(BLKPBSZGET)
56 int phSectorSize = -1;
57 int fd = open(device_node.toLocal8Bit().constData(), O_RDONLY);
58 if (fd != -1) {
59 if (ioctl(fd, BLKPBSZGET, &phSectorSize) >= 0) {
60 close(fd);
61 return phSectorSize;
62 }
63
64 close(fd);
65 }
66 #endif
67
68 QFile f(QStringLiteral("/sys/block/%1/queue/physical_block_size").arg(QString(device_node).remove(QStringLiteral("/dev/"))));
69
70 if (f.open(QIODevice::ReadOnly)) {
71 QByteArray a = f.readLine();
72 return a.trimmed().toInt();
73 }
74
75 return -1;
76 }
77
78 /** Constructs a Disk Device with an empty PartitionTable.
79 @param name the Device's name, usually some string defined by the manufacturer
80 @param deviceNode the Device's node, for example "/dev/sda"
81 @param heads the number of heads in CHS notation
82 @param numSectors the number of sectors in CHS notation
83 @param cylinders the number of cylinders in CHS notation
84 @param sectorSize the size of a sector in bytes
85 */
DiskDevice(const QString & name,const QString & deviceNode,qint32 heads,qint32 numSectors,qint32 cylinders,qint64 sectorSize,const QString & iconName)86 DiskDevice::DiskDevice(const QString& name,
87 const QString& deviceNode,
88 qint32 heads,
89 qint32 numSectors,
90 qint32 cylinders,
91 qint64 sectorSize,
92 const QString& iconName)
93 : Device(std::make_shared<DiskDevicePrivate>(), name, deviceNode, sectorSize, (static_cast<qint64>(heads) * cylinders * numSectors), iconName, Device::Type::Disk_Device)
94 {
95 d_ptr->m_Heads = heads;
96 d_ptr->m_SectorsPerTrack = numSectors;
97 d_ptr->m_Cylinders = cylinders;
98 d_ptr->m_LogicalSectorSize = sectorSize;
99 d_ptr->m_PhysicalSectorSize = getPhysicalSectorSize(deviceNode);
100 }
101
heads() const102 qint32 DiskDevice::heads() const
103 {
104 return d_ptr->m_Heads;
105 }
106
cylinders() const107 qint32 DiskDevice::cylinders() const
108 {
109 return d_ptr->m_Cylinders;
110 }
111
sectorsPerTrack() const112 qint32 DiskDevice::sectorsPerTrack() const
113 {
114 return d_ptr->m_SectorsPerTrack;
115 }
116
physicalSectorSize() const117 qint64 DiskDevice::physicalSectorSize() const
118 {
119 return d_ptr->m_PhysicalSectorSize;
120 }
121
logicalSectorSize() const122 qint64 DiskDevice::logicalSectorSize() const
123 {
124 return d_ptr->m_LogicalSectorSize;
125 }
126
totalSectors() const127 qint64 DiskDevice::totalSectors() const
128 {
129 return static_cast<qint64>(d_ptr->m_Heads) * d_ptr->m_Cylinders * d_ptr->m_SectorsPerTrack;
130 }
131
cylinderSize() const132 qint64 DiskDevice::cylinderSize() const
133 {
134 return static_cast<qint64>(d_ptr->m_Heads) * d_ptr->m_SectorsPerTrack;
135 }
136