summaryrefslogtreecommitdiff
path: root/Core/EM/OpalSecurity/AHCIOpalSec.c
blob: 998181d0c94491e62b89c01906238cf4d20458fa (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
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (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/AHCIOpalSec.c 5     12/21/11 8:32p Rajkumarkc $
//
// $Revision: 5 $
//
// $Date: 12/21/11 8:32p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/OpalSecurity/OPALSECURITY/AHCIOpalSec.c $
// 
// 5     12/21/11 8:32p 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
// 
// 4     8/22/11 4:09a Anandakrishnanl
// [TAG]  		EIP64040
// [Category]  	Improvement
// [Description]  	Opal Security - Changes requested for CodeReview by
// customer
// [Files]  		IdeOpalSec.c
// AhciOpalSec.c
// 
// 3     8/22/11 3:04a Anandakrishnanl
// [TAG]  		EIP62912
// [Category]  	Improvement
// [Description]  	Opal Security Definitions Should be Moved to PIDEBUS.h
// from StorageSecurityProtocol.h
// StorageSecurityProtocol.h included in OPAL security driver module will
// give build error when disabled without sdl token #if
// OpalSecurity_SUPPORT properly placed in Ahcibus and IdeBus drivers. But
// Bus driver should not depend on any tokens. For this reason need to
// move OPAL_SEC_INIT_PROTOCOL_GUID in Pidebus.h
// [Files]  		IdeBus.c
// Pidebus.h
// OpalSecurity.cif
// OpalSecurity.h
// IdeOpalSec.c
// AhciOpalSec.c
// 
// 2     5/19/11 5:58a 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: AHCIOpalSec.c
//
// Description: Opal Support
//
//---------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "OpalSecurity.h"

#define ZeroMemory(Buffer,Size) pBS->SetMem(Buffer,Size,0)


//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:    AhciSendData
//
// Description:    Send a security protocol command to a device.
//
// Input:
//    This - Indicates a pointer to the calling context. Type
//           EFI_STORAGE_SECURITY_COMMAND_PROTOCOL is defined in the
//           EFI_STORAGE_SECURITY_COMMAND_PROTOCOL description.
//    MediaId - ID of the medium to send data to.
//    Timeout - The timeout, in 100ns units, to use for the execution of the 
//              security protocol command. A Timeout value of 0 means that this function 
//              will wait indefinitely for the security protocol command to execute.
//              If Timeout is greater than zero, then this function will return 
//              EFI_TIMEOUT if the time required to execute the receive data command 
//              is greater than Timeout.
//    SecurityProtocolId - Security protocol ID of the security protocol 
//                         command to be sent.
//    SecurityProtocolSpecificData - Security protocol specific portion of 
//                                   the security protocol command.
//    PayloadBufferSize    - Size in bytes of the payload data buffer.
//    PayloadBuffer - A pointer to a buffer containing the security protocol 
//                    command specific payload data for the security protocol command.
//
// Output:
//    EFI_SUCCESS         - The security protocol command completed successfully.
//    EFI_UNSUPPORTED     - The given MediaId does not support security protocol 
//                           commands.
//    EFI_DEVICE_ERROR     - The security protocol command completed with an error.
//    EFI_INVALID_PARAMETER - The PayloadBuffer or PayloadTransferSize is 
//                            NULL and PayloadBufferSize is non-zero.
//
//<AMI_PHDR_END>
//**********************************************************************

EFI_STATUS 
AhciSendData(
    IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
    IN UINT32                MediaId,
    IN UINT64                Timeout,
    IN UINT8                 SecurityProtocolId,
    IN UINT16                SecurityProtocolSpecificData,
    IN UINTN                 PayloadBufferSize,
    IN VOID                  *PayloadBuffer 
)
{
    EFI_STATUS              Status;
    UINT8                   SectorCountL = 0;
    UINT8                   SectorCountH = 0;
    COMMAND_STRUCTURE       CommandStructure;
    STORAGE_SECURITY_COMMAND_PROTOCOL *StorageSecurityProtocol = (STORAGE_SECURITY_COMMAND_PROTOCOL*)This;
    SATA_DEVICE_INTERFACE   *SataDevInterface = (SATA_DEVICE_INTERFACE *)StorageSecurityProtocol->BusInterface;
    SATA_BLOCK_IO           *SataBlkIo = SataDevInterface->SataBlkIo;

    // Check for BlkIo presence
    if(SataBlkIo == NULL) {
        return EFI_UNSUPPORTED;
    }

    // Check for Media change
    if(SataBlkIo->BlkIo.Media->MediaId != MediaId) {
        return EFI_MEDIA_CHANGED;
    }
    
    SectorCountL = (UINT8)(PayloadBufferSize / SataBlkIo->BlkIo.Media->BlockSize);
    SectorCountH = (UINT8)((PayloadBufferSize / SataBlkIo->BlkIo.Media->BlockSize)/0x100);

    ZeroMemory (&CommandStructure, sizeof(COMMAND_STRUCTURE));

    if(PayloadBufferSize != 0){
         Status = SataDevInterface->AhciBusInterface->SataPioDataOut(
                        SataDevInterface,
                        PayloadBuffer,
                        (UINT32)PayloadBufferSize,
                        SecurityProtocolId,
                        SectorCountH,//TransferLength
                        0,
                        (UINT8)(SecurityProtocolSpecificData >> 8) , //Com ID
                        0,
                        (UINT8)SecurityProtocolSpecificData,
                        0,
                        TRUSTED_SEND,
                        TRUE);

    }else{
        // For PayloadBufferSize zero, Trusted Non Data command should be used
        // BIT 24 indicates Trusted Receive/Send
        CommandStructure.LBAMid = (UINT8)(SecurityProtocolSpecificData >> 8);
        CommandStructure.LBAHigh = (UINT8)SecurityProtocolSpecificData;
        CommandStructure.Command = TRUSTED_NON_DATA;
        CommandStructure.Features = SecurityProtocolId;
        CommandStructure.Buffer = PayloadBuffer;
        CommandStructure.ByteCount = (UINT32)PayloadBufferSize;
        Status = SataDevInterface->AhciBusInterface->ExecuteNonDataCommand (SataDevInterface, CommandStructure);
    }

    return Status;
}

//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:    ReceiveData
//
// Description:    Send a security protocol command to a device that receives 
//        data and/or the result of one or more commands sent by 
//        SendData.
//
// Input:
//    This -Indicates a pointer to the calling context. Type 
//            EFI_STORAGE_SECURITY_COMMAND_PROTOCOL is defined in the
//            EFI_STORAGE_SECURITY_COMMAND_PROTOCOL description.
//    MediaId - ID of the medium to receive data from. 
//    Timeout - The timeout, in 100ns units, to use for the execution of the
//            security protocol command. A Timeout value of 0 means that this 
//            function will wait indefinitely for the security protocol command
//            to execute. If Timeout is greater than zero, then this function 
//            will return.
//    SecurityProtocolId - Security protocol ID of the security protocol 
//            command to be sent.
//    SecurityProtocolSpecificData - Security protocol specific portion of 
//                the security protocol command.
//    PayloadBufferSize - Size in bytes of the payload data buffer.
//    PayloadBuffer - A pointer to a destination buffer to store the 
//            security protocol command specific payload data for the 
//            security protocol command. The caller is responsible 
//            for either having implicit or explicit ownership of 
//            the buffer.
//    PayloadTransferSize - A pointer to a buffer to store the size in bytes
//            of the data written to the payload data buffer.
//
// Output:
//    EFI_SUCCESS - The security protocol command completed successfully.
//    EFI_UNSUPPORTED - The given MediaId does not support security 
//            protocol commands.
//    EFI_DEVICE_ERROR - The security protocol command completed with an error.
//    EFI_INVALID_PARAMETER - The PayloadBuffer or PayloadTransferSize is 
//                  NULL and PayloadBufferSize is non-zero.
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS
AhciReceiveData(
    IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
    IN UINT32                MediaId,
    IN UINT64                Timeout,
    IN UINT8                 SecurityProtocolId,
    IN UINT16                SecurityProtocolSpecificData,
    IN UINTN                 PayloadBufferSize,
    OUT VOID                 *PayloadBuffer,
    OUT UINTN                *PayloadTransferSize
)
{
    EFI_STATUS              Status;
    UINT8                   SectorCountL = 0;
    UINT8                   SectorCountH = 0;
    COMMAND_STRUCTURE       CommandStructure;
    STORAGE_SECURITY_COMMAND_PROTOCOL *StorageSecurityProtocol = (STORAGE_SECURITY_COMMAND_PROTOCOL*)This;
    SATA_DEVICE_INTERFACE   *SataDevInterface = (SATA_DEVICE_INTERFACE *)StorageSecurityProtocol->BusInterface;
    SATA_BLOCK_IO           *SataBlkIo = SataDevInterface->SataBlkIo;

    // Check for BlkIo presence
    if(SataBlkIo==NULL) {
        return EFI_UNSUPPORTED;
    }

    // Check for Media change
    if(SataBlkIo->BlkIo.Media->MediaId != MediaId) {
        return EFI_MEDIA_CHANGED;
    }

    SectorCountL = (UINT8)(PayloadBufferSize / SataBlkIo->BlkIo.Media->BlockSize);
    SectorCountH = (UINT8)((PayloadBufferSize / SataBlkIo->BlkIo.Media->BlockSize)/0x100);

    ZeroMemory (&CommandStructure, sizeof(COMMAND_STRUCTURE));

    if(PayloadBufferSize != 0){
        // For PayloadBufferSize non zero, Trusted Receive command should be used

        if(PayloadTransferSize == NULL || PayloadBuffer == NULL) {
            return EFI_INVALID_PARAMETER;
        }

        CommandStructure.SectorCount = (UINT16)SectorCountL;
        CommandStructure.LBALow = (UINT8)SectorCountH;
        CommandStructure.LBAMid = (UINT8)(SecurityProtocolSpecificData >> 8);
        CommandStructure.LBAHigh = (UINT8)SecurityProtocolSpecificData;
        CommandStructure.Command = TRUSTED_RECEIVE;
        CommandStructure.Features = SecurityProtocolId;
        CommandStructure.Buffer = PayloadBuffer;
        CommandStructure.ByteCount = (UINT32)PayloadBufferSize ;
        Status = SataDevInterface->AhciBusInterface->ExecutePioDataCommand (SataDevInterface, &CommandStructure, FALSE);
    }else{
        // For PayloadBufferSize zero, Trusted Non Data command should be used
        // BIT 24 indicates Trusted Receive/Send
        CommandStructure.LBAMid = (UINT8)(SecurityProtocolSpecificData >> 8);
        CommandStructure.LBAHigh = (UINT8)SecurityProtocolSpecificData;
        CommandStructure.LBALowExp = 0x01; // Trusted Receive
        CommandStructure.Command = TRUSTED_NON_DATA;
        CommandStructure.Features = SecurityProtocolId;
        CommandStructure.Buffer = PayloadBuffer;
        CommandStructure.ByteCount = (UINT32)PayloadBufferSize;
        Status = SataDevInterface->AhciBusInterface->ExecuteNonDataCommand (SataDevInterface, CommandStructure);
    }

    if (!EFI_ERROR( Status) ) {
        *PayloadTransferSize = CommandStructure.ByteCount;
    } else {
        *PayloadTransferSize = 0;
    }

    return Status;
}

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