forked from ladar/sedutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDtaDevOS.cpp
213 lines (190 loc) · 5.85 KB
/
DtaDevOS.cpp
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
/* C:B**************************************************************************
This software is Copyright 2014-2017 Bright Plaza Inc. <[email protected]>
This file is part of sedutil.
sedutil is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sedutil is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with sedutil. If not, see <http://www.gnu.org/licenses/>.
* C:E********************************************************************** */
#include "os.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fnmatch.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <unistd.h>
#include <linux/hdreg.h>
#include <errno.h>
#include <vector>
#include <fstream>
#include "DtaDevOS.h"
#include "DtaHexDump.h"
#include "DtaDevLinuxSata.h"
#include "DtaDevLinuxNvme.h"
#include "DtaDevGeneric.h"
#include "DtaHashPwd.h"
#include "DtaSession.h"
#include "DtaDevOpal.h"
using namespace std;
/** The Device class represents a Linux generic storage device.
* At initialization we determine if we map to the NVMe or SATA derived class
*/
unsigned long long DtaDevOS::getSize()
{ return 0;
}
DtaDevOS::DtaDevOS()
{
drive = NULL;
}
/* Determine which type of drive we're using and instantiate a derived class of that type */
void DtaDevOS::init(const char * devref)
{
LOG(D1) << "DtaDevOS::init " << devref;
memset(&disk_info, 0, sizeof(OPAL_DiskInfo));
dev = devref;
if (!strncmp(devref, "/dev/nvme", 9))
{
// DtaDevLinuxNvme *NvmeDrive = new DtaDevLinuxNvme();
drive = new DtaDevLinuxNvme();
}
else if (!strncmp(devref, "/dev/s", 6))
{
// DtaDevLinuxSata *SataDrive = new DtaDevLinuxSata();
drive = new DtaDevLinuxSata();
}
else
{
LOG(E) << "DtaDevOS::init ERROR - unknown drive type";
isOpen = FALSE;
return;
}
if (drive->init(devref))
{
isOpen = TRUE;
drive->identify(disk_info);
if (disk_info.devType != DEVICE_TYPE_OTHER)
discovery0();
}
else
isOpen = FALSE;
return;
}
uint8_t DtaDevOS::sendCmd(ATACOMMAND cmd, uint8_t protocol, uint16_t comID,
void * buffer, uint32_t bufferlen)
{
if (!isOpen) return 0xfe; //disk open failed so this will too
if (NULL == drive)
{
LOG(E) << "DtaDevOS::sendCmd ERROR - unknown drive type";
return 0xff;
}
return drive->sendCmd(cmd, protocol, comID, buffer, bufferlen);
}
void DtaDevOS::identify(OPAL_DiskInfo& disk_info)
{
if (!isOpen) return; //disk open failed so this will too
if (NULL == drive)
{
LOG(E) << "DtaDevOS::identify ERROR - unknown disk type";
return;
}
drive->identify(disk_info);
}
void DtaDevOS::osmsSleep(uint32_t ms)
{
usleep(ms * 1000); //convert to microseconds
return;
}
int DtaDevOS::diskScan()
{
DIR *dir;
struct dirent *dirent;
DtaDev * d;
char devname[25];
vector<string> devices;
string tempstring;
LOG(D1) << "Entering DtaDevOS:diskScan ";
dir = opendir("/dev");
if(dir!=NULL)
{
while((dirent=readdir(dir))!=NULL) {
if((!fnmatch("sd[a-z]",dirent->d_name,0)) ||
(!fnmatch("nvme[0-9]",dirent->d_name,0)) ||
(!fnmatch("nvme[0-9][0-9]",dirent->d_name,0))
) {
tempstring = dirent->d_name;
devices.push_back(tempstring);
}
}
closedir(dir);
}
std::sort(devices.begin(),devices.end());
printf("Scanning for Opal compliant disks\n");
for(uint16_t i = 0; i < devices.size(); i++) {
snprintf(devname,23,"/dev/%s",devices[i].c_str());
printf("%-10s", devname);
d = new DtaDevGeneric(devname);
if (d->isAnySSC())
printf(" %s%s%s ", (d->isOpal1() ? "1" : " "),
(d->isOpal2() ? "2" : " "), (d->isEprise() ? "E" : " "));
else
printf("%s", " No ");
printf("%s %s\n",d->getModelNum(),d->getFirmwareRev());
delete d;
}
printf("No more disks present ending scan\n");
LOG(D1) << "Exiting DtaDevOS::scanDisk ";
return 0;
}
uint8_t DtaDevOS::prepareForS3Sleep(uint8_t lockingrange, const char* userid, char* password)
{
LOG(D1) << "Entering DtaDevOS::prepareForS3Sleep ";
LOG(D2) << "Starting testing of password ";
session = new DtaSession(this);
if (NULL == session) {
LOG(E) << "Unable to create session object ";
return DTAERROR_OBJECT_CREATE_FAILED;
}
int err;
vector<uint8_t> userUID;
if ((err = DtaDevOpal::getAuth4User(userid, 0, userUID)) != 0) {
LOG(E) << "Unable to find user " << userid << " in Authority Table";
delete session;
return err;
}
if ((err = session->start(OPAL_UID::OPAL_LOCKINGSP_UID, password, userUID)) != 0) {
delete session;
LOG(E) << "Unable to authenticate with the given password";
return err;
}
delete session;
LOG(D2) << "Test successful, saving it to kernel ";
std::shared_ptr<SecureByteVector> hash = std::allocate_shared<SecureByteVector>(SecureAllocator<SecureByteVector>(), 0);
DtaHashPwd(hash, password, this);
hash->erase(hash->begin(), hash->begin()+2);
err = drive->prepareForS3Sleep(lockingrange, userid, hash);
if (err)
{
LOG(E) << "Error saving the password to the kernel errno = " << errno;
return errno;
}
return 0;
}
/** Close the device reference so this object can be delete. */
DtaDevOS::~DtaDevOS()
{
LOG(D1) << "Destroying DtaDevOS";
if (NULL != drive)
delete drive;
}