summaryrefslogtreecommitdiff
path: root/Core/EM/OpalSecurity/OpalSecurity.c
blob: f06d77a8769afb246e25169513b9390a07435aa7 (plain)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************
// $Header: /Alaska/SOURCE/Modules/OpalSecurity/OPALSECURITY/OpalSecurity.c 3     12/21/11 8:36p Rajkumarkc $
//
// $Revision: 3 $
//
// $Date: 12/21/11 8:36p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/OpalSecurity/OPALSECURITY/OpalSecurity.c $
// 
// 3     12/21/11 8:36p Rajkumarkc
// [TAG]         EIP77142
// [Category]    BUG FIX & IMPROVEMENT
// [Description] BUG FIX - Changed Little Endian format to Big Endian
//               format while sending commands.
//               IMPROVEMENT - Added support to Lock the Opal hdd if
//               it's unlocked on Bios POST.
// [Files]       IdeOpalSec.c, AhciOpalSec.c, OpalSecurity.c,
//               OpalSecurity.h, OpalSecurity.sdl
// 
// 2     5/19/11 5:59a Anandakrishnanl
// [TAG]  		EIP53565 
// [Category]  	New Feature
// [Description]  	UEFI2.3+ Specifications defines Storage Security
// protocol which needs to be implemented.
// [Files]  		AhciBus.c,IdeBus.c,AHCIOpalSec.c,IDEOpalSec.c,OpalSecurity.c
// ,OpalSecurity.chm,OpalSecurity.cif,OpalSecurity.h,OpalSecurity.mak,Opal
// Security.sdl,PIDEBus.h,StorageSecurityProtocol.CIF,StorageSecurityProto
// col.h
// 
//<AMI_FHDR_START>
//---------------------------------------------------------------------------
//
// Name: OpalSecurity.c
//
// Description: Opal Security Support
//
//---------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "OpalSecurity.h"

static EFI_GUID gOpalSecInitProtocolGuid       = OPAL_SEC_INIT_PROTOCOL_GUID;
static EFI_GUID gStorageSecurityProtocolGuid   = EFI_STORAGE_SECURITY_COMMAND_PROTOCOL_GUID;

OPAL_SECURITY_INIT_PROTOCOL           *gOpalSecInitProtocol;
STORAGE_SECURITY_COMMAND_PROTOCOL     *gStorageSecurityProtocol;

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   OpalSecEntryPoint
//
// Description: This function is the entry point for OpalSecurity 
//              Driver.
//
// Input:
//  EFI_HANDLE          ImageHandle     Image handle.
//  EFI_SYSTEM_TABLE    *SystemTable    Pointer to the EFI system table.
//
// Output:
//  EFI_SUCCESS : OpalSecurity Interface successfully installed.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS 
OpalSecEntryPoint(
    IN EFI_HANDLE       ImageHandle,
    IN EFI_SYSTEM_TABLE *SystemTable 
)
{
    EFI_HANDLE Handle = NULL;
    EFI_STATUS Status = EFI_SUCCESS;

    InitAmiLib( ImageHandle, SystemTable );

    Status = pBS->AllocatePool( EfiBootServicesData,
                                sizeof(OPAL_SECURITY_INIT_PROTOCOL),
                                (VOID**)&gOpalSecInitProtocol );

    if ( EFI_ERROR( Status )){
        return Status;
    }

    gOpalSecInitProtocol->InstallOpalSecurityInterface        = InstallOpalSecurityInterface;
    gOpalSecInitProtocol->UnInstallOpalSecurityInterface      = UnInstallOpalSecurityInterface;

    Status = pBS->InstallProtocolInterface(
        &Handle,
        &gOpalSecInitProtocolGuid,
        EFI_NATIVE_INTERFACE,
        gOpalSecInitProtocol
        );

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   InstallOpalSecurityInterface
//
// Description: Installs StorageSecurityProtocol if the drive supports 
//                Trusted commands
//
// Input:
//      IN VOID             *BusInterface
//      IN BOOLEAN          ModeFlag
//
// Output:
//  EFI_SUCCESS    : StorageSecurity supported.
//  Others         : Not supported.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS 
InstallOpalSecurityInterface(
    IN VOID    *BusInterface,
    IN BOOLEAN ModeFlag 
)
{
    EFI_STATUS Status ;
    IDENTIFY_DATA         IdentifyData;
    EFI_HANDLE            IdeDeviceHandle = NULL;

    if ( ModeFlag == FALSE ){
        IdentifyData    = ((IDE_BUS_PROTOCOL*)BusInterface)->IdeDevice.IdentifyData;
        IdeDeviceHandle = ((IDE_BUS_PROTOCOL*)BusInterface)->IdeDeviceHandle;
    }else{
        IdentifyData    = ((SATA_DEVICE_INTERFACE*)BusInterface)->IdentifyData;
        IdeDeviceHandle = ((SATA_DEVICE_INTERFACE*)BusInterface)->IdeDeviceHandle;
    }

    if(!(IdentifyData.Trusted_Computing_Support & 0x01)){
        return EFI_UNSUPPORTED;
    }

    //
    // StorageSecurity Protocol
    //
    Status = pBS->AllocatePool( EfiBootServicesData,
                           sizeof(EFI_STORAGE_SECURITY_COMMAND_PROTOCOL),
                           (VOID**)&gStorageSecurityProtocol );

    if ( ModeFlag == FALSE ){
        gStorageSecurityProtocol->ReceiveData = IdeReceiveData;
        gStorageSecurityProtocol->SendData    = IdeSendData;
    }else{
        gStorageSecurityProtocol->ReceiveData = AhciReceiveData;
        gStorageSecurityProtocol->SendData    = AhciSendData;
    }
    gStorageSecurityProtocol->BusInterface = BusInterface;

    Status = pBS->InstallMultipleProtocolInterfaces(
                      &IdeDeviceHandle,
                      &gStorageSecurityProtocolGuid, gStorageSecurityProtocol,
                      NULL );

// TPer Lock On Reset Feature Support
#if TPer_LOCK_ON_RESET
  
    // Below error status check is to determine if gStorageSecurityProtocol has been
    // installed successfully or not. In case if code inserted in between, this check
    // has to be handled properly.

    if (!EFI_ERROR(Status)) { 
        GetAndStoreSecurityProtocolInformation(
            (EFI_STORAGE_SECURITY_COMMAND_PROTOCOL*)gStorageSecurityProtocol,
            BusInterface
            );
        IssueLockOnResetCommand(
            (EFI_STORAGE_SECURITY_COMMAND_PROTOCOL*)gStorageSecurityProtocol, 
            BusInterface, 
            ModeFlag
            );
    }
#endif

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   UnInstallOpalSecurityInterface
//
// Description: Uninstalls the Opal Security Init Protocol.
//
// Input:
//      IN VOID             *BusInterface,
//      IN BOOLEAN          ModeFlag
//
// Output:
//  EFI_SUCCESS                      : Security Mode supported.
//  EFI_DEVICE_ERROR/EFI_UNSUPPORTED : Not supported.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS 
UnInstallOpalSecurityInterface(
    IN VOID    *BusInterface,
    IN BOOLEAN ModeFlag 
)
{
    EFI_STATUS Status = EFI_SUCCESS;
    EFI_HANDLE            IdeDeviceHandle;

    if ( ModeFlag == FALSE ){
        IdeDeviceHandle = ((IDE_BUS_PROTOCOL*)BusInterface)->IdeDeviceHandle;
    }else{
        IdeDeviceHandle = ((SATA_DEVICE_INTERFACE*)BusInterface)->IdeDeviceHandle;
    }

    //
    //  Install Devicepath and BLOCK_IO on a new handle
    //
    Status = pBS->UninstallMultipleProtocolInterfaces(
        IdeDeviceHandle,
        &gOpalSecInitProtocolGuid,
        gOpalSecInitProtocol,
        NULL );

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   GetAndStoreSecurityProtocolInformation
//
// Description: Get and store Security Protocol Information
//
// Input:
//      IN VOID             *BusInterface,
//      IN BOOLEAN          ModeFlag
//
// Output: NONE
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS 
GetAndStoreSecurityProtocolInformation(
    IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
    IN VOID    *BusInterface
)
{
    EFI_STATUS Status = EFI_SUCCESS;
    SP0_TRUSTED_RECEIVE_PARAMETER_DATA *QueryBuffer = NULL;
    UINT16  ListLength = 0;
    UINT8   SpByte = 0;

    UINTN QueryBufferSize = sizeof(SP0_TRUSTED_RECEIVE_PARAMETER_DATA);

    Status = pBS->AllocatePool(EfiBootServicesData, sizeof(UINT8)*QueryBufferSize, (VOID**)&QueryBuffer);
    if(EFI_ERROR(Status))return EFI_OUT_OF_RESOURCES;

    Status = GetSecurityProtocolInformation(This, QueryBuffer, &QueryBufferSize);
    // GetSecurityProtocolInformation returns error Status. Unsupported Security Protocol.   
    if(EFI_ERROR(Status))  return EFI_UNSUPPORTED;
            
    // The List Length field indicates the total length, in bytes, of the supported security protocol list.
    ListLength = QueryBuffer->ListLengthHigh << 8 | QueryBuffer->ListLengthLow;

    ((STORAGE_SECURITY_COMMAND_PROTOCOL*)This)->SupportedProtocolList = QueryBuffer;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   GetSecurityProtocolInformation
//
// Description: Get Security Protocol Information
//
// Input:     IN    EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
//            OUT   VOID                *QueryBuffer,
//            OUT   UINTN               *QueryBufferSize
//      
//
// Output: NONE
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS 
GetSecurityProtocolInformation(
    IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
    IN OUT VOID                *QueryBuffer,
    IN OUT UINTN               *QueryBufferSize
 )
{
    EFI_STATUS  Status = EFI_SUCCESS;

    Status = This->ReceiveData( This,
                        0,
                        0,
                        SECURITY_PROTOCOL_INFORMATION,   // Security Protocol ID
                        0x00,   // SP specific id
                        0x200,  // TranferLength
                        QueryBuffer,
                        QueryBufferSize
                        );
    return Status;
    
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   IssueLockOnResetCommand
//
// Description: Issues LockOnReset Command.
//
// Input:
//      IN VOID             *BusInterface,
//      IN BOOLEAN          ModeFlag
//
// Output: NONE
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS 
IssueLockOnResetCommand(
    IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
    IN VOID    *BusInterface,
    IN BOOLEAN ModeFlag
)
{
    EFI_STATUS Status = EFI_SUCCESS;
    IDENTIFY_DATA         IdentifyData;
    EFI_HANDLE            IdeDeviceHandle = NULL;
  
    UINT16  ListLength = 0;
    UINT8   SpByte = 0;
    SP0_TRUSTED_RECEIVE_PARAMETER_DATA *SupportedProtocolList = ((STORAGE_SECURITY_COMMAND_PROTOCOL*)This)->SupportedProtocolList;

    if ( ModeFlag == FALSE ){
        IdentifyData    = ((IDE_BUS_PROTOCOL*)BusInterface)->IdeDevice.IdentifyData;
        IdeDeviceHandle = ((IDE_BUS_PROTOCOL*)BusInterface)->IdeDeviceHandle;
    }else{
        IdentifyData    = ((SATA_DEVICE_INTERFACE*)BusInterface)->IdentifyData;
        IdeDeviceHandle = ((SATA_DEVICE_INTERFACE*)BusInterface)->IdeDeviceHandle;
    }

    // The List Length field indicates the total length, in bytes, of the supported security protocol list.
    ListLength = SupportedProtocolList->ListLengthHigh << 8 | SupportedProtocolList->ListLengthLow;
    if(ListLength == 0)  return EFI_UNSUPPORTED;

    // Check the Secutiy supported Protocol List is 0xEE for IEEE 1667 or 0x02 for Native TCG
    for(SpByte = 0; SpByte < ListLength; SpByte++){

        if(SupportedProtocolList->SupportedSPList[SpByte] == SECURITY_PROTOCOL_2)  {
            // Found Supported Security Protocol list NativeTcg.Issue Tcg TPer Reset
            Status = NativeTcgTPerReset(This);
            break; 
        }

        if(SupportedProtocolList->SupportedSPList[SpByte] == SECURITY_PROTOCOL_IEEE1667)  {
            // Found Supported Security Protocol list IEEE 1667. Issue Silo TPer Reset
            // Check if IEEE 1667 bit (bit 7) set in the AdditionalSupported field to 
            // determine whether the device supports Trusted Send/Receive.

            if(!(IdentifyData.Reserved_69_74[0] & 0x80)){
                Status = IEEE67TPerReset(This);
                break; 
            }
        } 
    }

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   NativeTcgTPerReset
//
// Description: Native Tcg TPer Reset
//
// Input:        IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
//
// Output: NONE
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS 
NativeTcgTPerReset(
        IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This
){
    EFI_STATUS Status = EFI_SUCCESS;
    VOID *TcgResetDataBuffer = NULL;
    UINT32 BufferSize = 0x200;

    Status = pBS->AllocatePool(EfiBootServicesData, BufferSize, (VOID**)&TcgResetDataBuffer);
    if(EFI_ERROR(Status))return EFI_OUT_OF_RESOURCES;

    pBS->SetMem(TcgResetDataBuffer, BufferSize, 0);
    
    // Determined the device supports Native TCG Security Support Protocol 2. Issue TPer Reset.
    Status = This->SendData( This,
                      0,
                      0,
                      SECURITY_PROTOCOL_2,   // Security Protocol 2 ID
                      0x0400, // SP specific id
                      0x200,  // TranferLength - Non zero Value
                      TcgResetDataBuffer
                      );
    pBS->FreePool(TcgResetDataBuffer);

    return Status;
    
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   IEEE67TPerReset
//
// Description: IEEE67 TPer Reset 
//
// Input:         IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
//      
//
// Output: NONE
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS 
IEEE67TPerReset(
        IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This
)
{
    EFI_STATUS Status = EFI_SUCCESS;
    UINT16  SiloTypeIdentifier = 0;
    UINT16   IeeeSilo = 0;
    TCG_SILO_TYPE_IDENTIFIER *ProbeHostDataBuffer = NULL;
    TCG_SILO_TYPE_IDENTIFIER *SupportedSilosArrayBuffer = NULL;

    UINTN SupportedSilosArrayBufferSize = sizeof(TCG_SILO_TYPE_IDENTIFIER);
    UINTN ProbeHostDataBufferSize = sizeof(TCG_SILO_TYPE_IDENTIFIER);
        
    Status = pBS->AllocatePool(EfiBootServicesData, sizeof(UINT8)*ProbeHostDataBufferSize, (VOID**)&ProbeHostDataBuffer);
    if(EFI_ERROR(Status))return EFI_OUT_OF_RESOURCES;

    // Check if TPer has TCG silo by sending probe command to PROBE SILO
    Status = This->SendData( This,
                          0,
                          0,
                          SECURITY_PROTOCOL_IEEE1667,   // Security Protocol ID
                          0x0100, // SP specific id
                          0x200,  // TranferLength
                          ProbeHostDataBuffer
                         );
    if(!EFI_ERROR(Status)) {
        Status = pBS->AllocatePool(EfiBootServicesData, sizeof(UINT8)*SupportedSilosArrayBufferSize, (VOID**)&SupportedSilosArrayBuffer);
        if(EFI_ERROR(Status))return EFI_OUT_OF_RESOURCES;

        // Followed by Trusted Receive Probe command to PROBE SILO
        Status = This->ReceiveData( This,
                            0,
                            0,
                            SECURITY_PROTOCOL_IEEE1667,   // Security Protocol ID
                            0x0100, // SP specific id
                            0x200,  // TranferLength
                            SupportedSilosArrayBuffer,
                            &SupportedSilosArrayBufferSize
                            );

        if(!EFI_ERROR(Status)) {
             // Scan for Silo Identifier(0x0104) available to send TPer Reset silo command payload to Tcg Silo
             SiloTypeIdentifier = SupportedSilosArrayBuffer->SiloTypeIdentifierHigh << 8 | SupportedSilosArrayBuffer->SiloTypeIdentifierLow;
             // Search for IEEE std allocated identifiers only to find  0x0104 present: IEEE  - 0100h to 01FFh 
             for(IeeeSilo = 0x100; IeeeSilo < 0x200; IeeeSilo++)  {
                 if(IeeeSilo == SiloTypeIdentifier) {
                     Status = IssueTPerResetTcgSilo(This);
                     return Status;     
                 }
             }
        }
    }
 
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   IssueTPerResetTcgSilo
//
// Description: Issue TPer Reset
//
// Input:         IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
//      
//
// Output: NONE
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS 
IssueTPerResetTcgSilo(
        IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This
){
    EFI_STATUS Status = EFI_SUCCESS;
    TCG_SILO_TYPE_IDENTIFIER *IeeeResetDataBuffer = NULL;

    UINTN IeeeResetDataBufferSize = sizeof(TCG_SILO_TYPE_IDENTIFIER);

    Status = pBS->AllocatePool(EfiBootServicesData, sizeof(UINT8)*IeeeResetDataBufferSize, (VOID**)&IeeeResetDataBuffer);
    if(EFI_ERROR(Status))return EFI_OUT_OF_RESOURCES;

    // Determined the device supports TCG silo. Issue TPer Reset silo command payload to the TCG silo
    Status = This->SendData( This,
                         0,
                         0,
                         SECURITY_PROTOCOL_IEEE1667,   // Security Protocol ID
                         0x0500, // SP specific id
                         0x200,  // TranferLength
                         IeeeResetDataBuffer
                         );
    pBS->FreePool(IeeeResetDataBuffer);

    return Status;
}

//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************