summaryrefslogtreecommitdiff
path: root/Core/EM/OpalSecurity/IDEOpalSec.c
blob: a478e636efe3b31b0016ed8f3e8b0c49e43573c7 (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
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (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/IDEOpalSec.c 4     12/21/11 8:33p Rajkumarkc $
//
// $Revision: 4 $
//
// $Date: 12/21/11 8:33p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/OpalSecurity/OPALSECURITY/IDEOpalSec.c $
// 
// 4     12/21/11 8:33p 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
// 
// 3     8/22/11 4:03a Anandakrishnanl
// [TAG]  		EIP64040
// [Category]  	Improvement
// [Description]  	Opal Security - Changes requested for CodeReview by
// customer
// [Files]  		IdeOpalSec.c
// AhciOpalSec.c
// 
// 2     8/22/11 3:03a 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
// 
// 1     5/19/11 2:07a Anandakrishnanl
// Opal Security Module Initial Check In
// 
//
//<AMI_FHDR_START>
//---------------------------------------------------------------------------
//
// Name: IDEOpalSec.c
//
// Description: IDE Opal Security Support
//
//---------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "OpalSecurity.h"

//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:    IdeSendData
//
// 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 IdeSendData(
    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;
    STORAGE_SECURITY_COMMAND_PROTOCOL *StorageSecurityProtocol = (STORAGE_SECURITY_COMMAND_PROTOCOL*)This;
    IDE_BUS_PROTOCOL   *IdeBusInterface = (IDE_BUS_PROTOCOL*)StorageSecurityProtocol->BusInterface;
    EFI_BLOCK_IO_PROTOCOL *Blockio = &(IdeBusInterface->IdeBlkIo->BlkIo);

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

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

    if(PayloadBufferSize != 0){
        // For PayloadBufferSize non zero, Trusted Send command should be used
        if(PayloadBuffer == NULL) {
            return EFI_INVALID_PARAMETER;
        }
        Status = IdeBusInterface->AtaPioDataOut(
                        IdeBusInterface,
                        PayloadBuffer,
                        (UINT32)PayloadBufferSize,
                        SecurityProtocolId,
                        SectorCountL, // Sector count - 512 bytes multiples
                        SectorCountH,//TransferLength
                        0,
                        (UINT8)(SecurityProtocolSpecificData >> 8) , //Com ID
                        0,
                        (UINT8)SecurityProtocolSpecificData,
                        0,
                        IdeBusInterface->IdeDevice.Device << 4,
                        TRUSTED_SEND,
                        TRUE,
                        FALSE );
    }else{
        // For PayloadBufferSize zero, Trusted Non Data command should be used
        // BIT 24 indicates Trusted Receive/Send
        Status = IdeBusInterface->IdeNonDataCommand(IdeBusInterface,
                SecurityProtocolId,
                0, // Reserved if command is Trusted Non-Data
                0, // Reserved if command is Trusted Non-Data
                0, // Reserved if command is Trusted Non-Data
                0, // Reserved if command is Trusted Non-Data
                (UINT8)(SecurityProtocolSpecificData >> 8),//Com Id
                0,
                (UINT8)SecurityProtocolSpecificData,
                0,
                IdeBusInterface->IdeDevice.Device << 4,
                TRUSTED_NON_DATA
            );
    }

 return Status;    
}

//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:    IdeReceiveData
//
// 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 IdeReceiveData(
    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               Device = 0;
    UINT8               SectorCountL = 0;
    UINT8               SectorCountH = 0;
    STORAGE_SECURITY_COMMAND_PROTOCOL *StorageSecurityProtocol = (STORAGE_SECURITY_COMMAND_PROTOCOL*)This;
    IDE_BUS_PROTOCOL   *IdeBusInterface = (IDE_BUS_PROTOCOL*)StorageSecurityProtocol->BusInterface;
    EFI_BLOCK_IO_PROTOCOL *Blockio = &(IdeBusInterface->IdeBlkIo->BlkIo);

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

    if(Blockio->Media->MediaId != MediaId) {
        return EFI_MEDIA_CHANGED;
    }

    SectorCountL = (UINT8)(PayloadBufferSize / Blockio->Media->BlockSize);
    SectorCountH = (UINT8)((PayloadBufferSize / Blockio->Media->BlockSize)/0x100);

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

        if(PayloadTransferSize == NULL || PayloadBuffer == NULL) {
            return EFI_INVALID_PARAMETER;
        }
        
        Status = IdeBusInterface->AtaPioDataIn(
                        IdeBusInterface,
                        PayloadBuffer,
                        (UINT32)PayloadBufferSize,
                        SecurityProtocolId,
                        SectorCountL, // Sector count - 512 bytes multiples
                        SectorCountH,//TransferLength
                        (UINT8)(SecurityProtocolSpecificData >> 8) , //Com ID
                        (UINT8)SecurityProtocolSpecificData,
                        IdeBusInterface->IdeDevice.Device << 4,
                        TRUSTED_RECEIVE,
                        FALSE );
    }else{

        // For PayloadBufferSize zero, Trusted Non Data command should be used
        // BIT 24 indicates Trusted Receive/Send
        UINT32 LBA = 0;

        // IdeNonDataCommand supports only for 24 bits of LBA(Low, Mid and High). For 28 bit LBA 
        // we use device to fill remanining 4 bits (24:27).
        // Needed since bit 24 indicates Trusted Receive/Send.
        LBA = (SecurityProtocolSpecificData << 8) | (1 << 24);
        Device = ((UINT8) ((UINT32) LBA >> 24 ) & 0x0f) | (IdeBusInterface->IdeDevice.Device << 4) | 0x40;

        Status = IdeBusInterface->IdeNonDataCommand(
                    IdeBusInterface,
                    SecurityProtocolId,
                    0, // Reserved if command is Trusted Non-Data
                    0, // Reserved if command is Trusted Non-Data
                    0, // Reserved if command is Trusted Non-Data
                    0x01, // Trusted Receive
                    (UINT8)(SecurityProtocolSpecificData >> 8) , //Com ID - Discovery 0
                    0,
                    (UINT8)SecurityProtocolSpecificData,
                    0,
                    Device,
                    TRUSTED_NON_DATA );
    }

    if (!EFI_ERROR( Status) ) {
        *PayloadTransferSize = PayloadBufferSize;
    } 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                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************