1
- /*
2
- * Copyright (C) 2023, 2024 IBM Corporation
3
- *
4
- * This program is free software; you can redistribute it and/or
5
- * modify it under the terms of the GNU General Public License
6
- * as published by the Free Software Foundation; either version 2
7
- * of the License, or (at your option) any later version.
8
- *
9
- * This program is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- * GNU General Public License for more details.
13
- *
14
- * You should have received a copy of the GNU General Public License
15
- * along with this program; if not, write to the Free Software
16
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17
- * USA.
18
- */
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
19
2
20
3
#include <ctype.h>
21
4
#include <stdio.h>
34
17
/*
35
18
* ask user for key rather than obtaining it from kernel keyring
36
19
*/
37
- bool sedopal_ask_key = false ;
20
+ bool sedopal_ask_key ;
38
21
39
22
/*
40
23
* initiate dialog to ask for and confirm new password
41
24
*/
42
- bool sedopal_ask_new_key = false ;
25
+ bool sedopal_ask_new_key ;
43
26
44
27
/*
45
28
* perform a destructive drive revert
46
29
*/
47
- bool sedopal_destructive_revert = false ;
30
+ bool sedopal_destructive_revert ;
48
31
49
32
/*
50
33
* perform a PSID drive revert
51
34
*/
52
- bool sedopal_psid_revert = false ;
35
+ bool sedopal_psid_revert ;
53
36
54
37
/*
55
38
* Map method status codes to error text
@@ -94,7 +77,8 @@ const char *sedopal_error_to_text(int code)
94
77
/*
95
78
* Read a user entered password and do some basic validity checks.
96
79
*/
97
- char * sedopal_get_password (char * prompt ) {
80
+ char * sedopal_get_password (char * prompt )
81
+ {
98
82
char * pass ;
99
83
int len ;
100
84
@@ -117,7 +101,14 @@ char * sedopal_get_password(char * prompt) {
117
101
* key should be looked up in the kernel keyring, or it should be
118
102
* populated in the key by prompting the user.
119
103
*/
120
- int sedopal_set_key (struct opal_key * key ) {
104
+ int sedopal_set_key (struct opal_key * key )
105
+ {
106
+ #ifndef OPAL_KEY_TYPE
107
+ /*
108
+ * If key_type isn't avaialable, force key prompt
109
+ */
110
+ sedopal_ask_key = true;
111
+ #endif
121
112
122
113
if (sedopal_ask_key ) {
123
114
char * pass ;
@@ -139,7 +130,9 @@ int sedopal_set_key(struct opal_key *key) {
139
130
if (pass == NULL )
140
131
return - EINVAL ;
141
132
133
+ #if HAVE_KEY_TYPE
142
134
key -> key_type = OPAL_INCLUDED ;
135
+ #endif
143
136
key -> key_len = strlen (pass );
144
137
memcpy (key -> key , pass , key -> key_len );
145
138
@@ -156,8 +149,10 @@ int sedopal_set_key(struct opal_key *key) {
156
149
}
157
150
}
158
151
} else {
159
- key -> key_type = OPAL_KEYRING ;
160
- key -> key_len = 0 ;
152
+ #if HAVE_KEY_TYPE
153
+ key -> key_type = OPAL_KEYRING ;
154
+ #endif
155
+ key -> key_len = 0 ;
161
156
}
162
157
163
158
key -> lr = 0 ;
@@ -168,7 +163,8 @@ int sedopal_set_key(struct opal_key *key) {
168
163
/*
169
164
* Prepare a drive for SED Opal locking.
170
165
*/
171
- int sedopal_cmd_initialize (int fd ) {
166
+ int sedopal_cmd_initialize (int fd )
167
+ {
172
168
int rc ;
173
169
struct opal_key key ;
174
170
struct opal_lr_act lr_act = {};
@@ -227,25 +223,28 @@ int sedopal_cmd_initialize(int fd) {
227
223
/*
228
224
* Lock a SED Opal drive
229
225
*/
230
- int sedopal_cmd_lock (int fd ) {
226
+ int sedopal_cmd_lock (int fd )
227
+ {
231
228
232
229
return sedopal_lock_unlock (fd , OPAL_LK );
233
230
}
234
231
235
232
/*
236
233
* Unlock a SED Opal drive
237
234
*/
238
- int sedopal_cmd_unlock (int fd ) {
235
+ int sedopal_cmd_unlock (int fd )
236
+ {
239
237
240
238
return sedopal_lock_unlock (fd , OPAL_RW );
241
239
}
242
240
243
241
/*
244
242
* Prepare and issue an ioctl to lock/unlock a drive
245
243
*/
246
- int sedopal_lock_unlock (int fd , int lock_state ) {
244
+ int sedopal_lock_unlock (int fd , int lock_state )
245
+ {
247
246
int rc ;
248
- struct opal_lock_unlock opal_lu ;
247
+ struct opal_lock_unlock opal_lu = {} ;
249
248
250
249
rc = sedopal_set_key (& opal_lu .session .opal_key );
251
250
if (rc != 0 )
@@ -254,16 +253,15 @@ int sedopal_lock_unlock(int fd, int lock_state) {
254
253
opal_lu .session .sum = 0 ;
255
254
opal_lu .session .who = OPAL_ADMIN1 ;
256
255
opal_lu .l_state = lock_state ;
257
- opal_lu .flags = 0 ;
258
256
259
257
rc = ioctl (fd , IOC_OPAL_LOCK_UNLOCK , & opal_lu );
260
258
if (rc != 0 )
261
259
fprintf (stderr ,
262
260
"Error: failed locking or unlocking - %d\n" , rc );
263
261
264
262
/*
265
- * If the unlock was successful, force a re-read of the
266
- * partion table.
263
+ * If the unlock was successful, force a re-read of the
264
+ * partition table.
267
265
*/
268
266
if (rc == 0 ) {
269
267
rc = ioctl (fd , BLKRRPART , 0 );
@@ -278,7 +276,8 @@ int sedopal_lock_unlock(int fd, int lock_state) {
278
276
/*
279
277
* Confirm a destructive drive so that data is inadvertently erased
280
278
*/
281
- static bool sedopal_confirm_revert () {
279
+ static bool sedopal_confirm_revert (void )
280
+ {
282
281
int rc ;
283
282
char ans ;
284
283
bool confirmed = false;
@@ -302,7 +301,8 @@ static bool sedopal_confirm_revert() {
302
301
/*
303
302
* perform a destructive drive revert
304
303
*/
305
- static int sedopal_revert_destructive (int fd ) {
304
+ static int sedopal_revert_destructive (int fd )
305
+ {
306
306
struct opal_key key ;
307
307
int rc ;
308
308
@@ -323,10 +323,12 @@ static int sedopal_revert_destructive(int fd) {
323
323
return rc ;
324
324
}
325
325
326
+ #ifdef IOC_OPAL_PSID_REVERT_TPR
326
327
/*
327
328
* perform a PSID drive revert
328
329
*/
329
- static int sedopal_revert_psid (int fd ) {
330
+ static int sedopal_revert_psid (int fd )
331
+ {
330
332
struct opal_key key ;
331
333
int rc ;
332
334
@@ -344,33 +346,44 @@ static int sedopal_revert_psid(int fd) {
344
346
345
347
return rc ;
346
348
}
349
+ #endif /* IOC_OPAL_PSID_REVERT_TPR */
347
350
348
351
/*
349
352
* revert a drive from the provisioned state to a state where locking
350
353
* is disabled.
351
354
*/
352
- int sedopal_cmd_revert (int fd ) {
355
+ int sedopal_cmd_revert (int fd )
356
+ {
353
357
int rc ;
354
- struct opal_revert_lsp revert_lsp ;
355
358
356
359
/*
357
360
* for revert, require that key/PSID is provided
358
361
*/
359
362
sedopal_ask_key = true;
360
363
361
364
if (sedopal_psid_revert ) {
365
+ #ifdef IOC_OPAL_PSID_REVERT_TPR
362
366
rc = sedopal_revert_psid (fd );
367
+ #else
368
+ rc = - EOPNOTSUPP ;
369
+ #endif
363
370
} else if (sedopal_destructive_revert ) {
364
371
rc = sedopal_revert_destructive (fd );
365
372
} else {
373
+ #ifdef IOC_OPAL_REVERT_LSP
374
+ struct opal_revert_lsp revert_lsp ;
375
+
366
376
rc = sedopal_set_key (& revert_lsp .key );
367
377
if (rc != 0 )
368
378
return rc ;
369
379
370
380
revert_lsp .options = OPAL_PRESERVE ;
371
- revert_lsp .__pad = 0 ;
381
+ revert_lsp .__pad = 0 ;
372
382
373
383
rc = ioctl (fd , IOC_OPAL_REVERT_LSP , & revert_lsp );
384
+ #else
385
+ rc = - EOPNOTSUPP ;
386
+ #endif
374
387
}
375
388
376
389
if (rc != 0 )
@@ -383,7 +396,8 @@ int sedopal_cmd_revert(int fd) {
383
396
* Change the password of a drive. The existing password must be
384
397
* provided and the new password is confirmed by re-entry.
385
398
*/
386
- int sedopal_cmd_password (int fd ) {
399
+ int sedopal_cmd_password (int fd )
400
+ {
387
401
int rc ;
388
402
struct opal_new_pw new_pw = {};
389
403
@@ -417,7 +431,8 @@ int sedopal_cmd_password(int fd) {
417
431
/*
418
432
* Print the state of locking features.
419
433
*/
420
- void sedopal_print_locking_features (uint8_t features ) {
434
+ void sedopal_print_locking_features (uint8_t features )
435
+ {
421
436
printf ("Locking Features:\n" );
422
437
printf ("\tLocking Supported: %s\n" ,
423
438
(features & OPAL_FEATURE_LOCKING_SUPPORTED ) ? "Yes" : "No" );
@@ -428,10 +443,12 @@ void sedopal_print_locking_features(uint8_t features) {
428
443
}
429
444
430
445
/*
431
- * Query a drive to determine if it's SED Opal capabled and
446
+ * Query a drive to determine if it's SED Opal capable and
432
447
* it's current locking status.
433
448
*/
434
- int sedopal_cmd_discover (int fd ) {
449
+ int sedopal_cmd_discover (int fd )
450
+ {
451
+ #ifdef IOC_OPAL_DISCOVERY
435
452
int rc ;
436
453
bool sedopal_locking_supported = false;
437
454
struct opal_discovery discover ;
@@ -442,7 +459,7 @@ int sedopal_cmd_discover(int fd) {
442
459
uint8_t locking_flags ;
443
460
char buf [4096 ];
444
461
445
- discover .data = (__u64 )buf ;
462
+ discover .data = (__u64 )buf ;
446
463
discover .size = sizeof (buf );
447
464
448
465
rc = ioctl (fd , IOC_OPAL_DISCOVERY , & discover );
@@ -467,7 +484,7 @@ int sedopal_cmd_discover(int fd) {
467
484
*/
468
485
while (feat < feat_end ) {
469
486
code = be16toh (feat -> code );
470
- switch (code ) {
487
+ switch (code ) {
471
488
case OPAL_FEATURE_CODE_LOCKING :
472
489
locking_flags = feat -> feature ;
473
490
break ;
@@ -489,4 +506,7 @@ int sedopal_cmd_discover(int fd) {
489
506
sedopal_print_locking_features (locking_flags );
490
507
491
508
return rc ;
509
+ #else /* IOC_OPAL_DISCOVERY */
510
+ return - EOPNOTSUPP ;
511
+ #endif
492
512
}
0 commit comments