summaryrefslogtreecommitdiff
path: root/Core/EM/HddSecurity/IdeSecuritySetup.c
blob: ba1f57b513395ee8d0329c97642c25e19d9bfc10 (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/HddSecurity/IdeSecuritySetup.c 4     4/25/13 9:22a Kapilporwal $
//
// $Revision: 4 $
//
// $Date: 4/25/13 9:22a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/HddSecurity/IdeSecuritySetup.c $
// 
// 4     4/25/13 9:22a Kapilporwal
// [TAG]  		EIP108583
// [Category]  	New Feature
// [Description]  	support set HDD password in same page
// [Files]  		IdeSecurity.cif, IdeSecurity.mak, IdeSecurity.sdl,
// IdeSecurity.uni, IdeSecurityCommon.sd, IdeSecuritySetup.c,
// HddPassword.c, HddPassword.h, HddPassword.mak, HddPassword.sdl
// 
// 3     1/15/10 10:22a Yul
// Coding Standard
//
// 2     12/09/09 12:18p Krishnakumarg
// EIP 31797 : IDE Security API documentation. Added comments whereever
// possible and modified files for coding standard
//
// 1     28/02/08 6:40p Anandakrishnanl
// Created new HddSecurity module.  Isolated IdeSecurity and IdeSmm from
// IdeBus driver to be compatable with the AHCI Bus driver.
//
//
//
//**********************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:        IdeSecuritySetup.c
//
// Description: Ide Security Setup Routines
//
// NOTE: Use this hook to dynamically change Ide Security Driver Setup screen
// Strings.
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

#include <AmiLib.h>
#include <AmiDxeLib.h>
#include <Setup.h>
#include <SetupStrTokens.h>
#if defined(SECUITY_SETUP_ON_SAME_PAGE) && SECUITY_SETUP_ON_SAME_PAGE
#include "Protocol\PciIo.h"
#include "Protocol\BlockIo.h"
#include "Protocol\PDiskInfo.h"
#include "Protocol\PIDEController.h"
#include "Protocol\PIDEBus.h"
#include "Protocol\PAhciBus.h"
#include "Protocol\DevicePath.h"
#include "Protocol\HiiString.h"
#endif

#if defined(SECUITY_SETUP_ON_SAME_PAGE) && SECUITY_SETUP_ON_SAME_PAGE

EFI_GUID   gDiskInfoProtocolGuid       = EFI_DISK_INFO_PROTOCOL_GUID;
EFI_GUID   gDevicePathProtocolGuid     = EFI_DEVICE_PATH_PROTOCOL_GUID;  
EFI_GUID   gIDESecurityProtocolGuid    = IDE_SECURITY_INTERFACE_GUID;

// Store the names of HDDs present in the system. Dynamically updated.
STRING_REF gHddSecurityName[6] =
{
    STRING_TOKEN(STR_SECURITY_HDD_ONE), STRING_TOKEN(STR_SECURITY_HDD_TWO),
    STRING_TOKEN(STR_SECURITY_HDD_THREE), STRING_TOKEN(STR_SECURITY_HDD_FOUR),
    STRING_TOKEN(STR_SECURITY_HDD_FIVE), STRING_TOKEN(STR_SECURITY_HDD_SIX)
};

static EFI_HII_STRING_PROTOCOL *HiiString      = NULL;
static EFI_GUID                EfiVariableGuid = EFI_GLOBAL_VARIABLE;

// Function Definitions

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CheckSecurityStatus
//
// Description: return the Security Status Information
//
// Input:       IN IDE_SECURITY_PROTOCOL *IDEPasswordSecurity,
//              IN OUT BOOLEAN               *ReqStatus,
//              IN UINT16                Mask
//
// Output:      BOOLEAN      status in ReqStatus is valid
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN CheckSecurityStatus(
    IN IDE_SECURITY_PROTOCOL *IDEPasswordSecurity,
    IN OUT BOOLEAN               *ReqStatus,
    IN UINT16                Mask )
{
    UINT16     SecurityStatus = 0;
    EFI_STATUS Status;

    //
    //get the security status of the device
    //
    Status = IDEPasswordSecurity->ReturnSecurityStatus( IDEPasswordSecurity, &SecurityStatus );

    if ( EFI_ERROR( Status ))
        return FALSE; // Unable to get IDE security status
    // Mask security status based on supplied mask data
    *ReqStatus = (BOOLEAN)((SecurityStatus & Mask) ? TRUE : FALSE );
    return TRUE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   HiiGetString
//
// Description: return the String based on StringId
//
// Input:       IN EFI_HII_HANDLE  HiiHandle,
//              IN EFI_STRING_ID   StringId,
//
// Output:      EFI_STRING
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STRING HiiGetString(
    IN EFI_HII_HANDLE  HiiHandle,
    IN EFI_STRING_ID   StringId)
{
    EFI_STRING String = NULL;
    UINTN StringSize = 0;
    EFI_STATUS Status;
    CHAR8* PlatformLang = NULL;
    UINTN Size = 0;

    if(HiiString == NULL) {
        Status = pBS->LocateProtocol(&gEfiHiiStringProtocolGuid, NULL, (VOID **) &HiiString);
        if(EFI_ERROR(Status))
            return NULL;
    }

//language not provided - try platform language
    Status = GetEfiVariable(L"PlatformLang", &EfiVariableGuid, NULL, &Size, &PlatformLang);
    if(!EFI_ERROR(Status)) {
        Status = HiiString->GetString(HiiString, PlatformLang, HiiHandle, StringId, String, &StringSize, NULL);
        if(Status == EFI_BUFFER_TOO_SMALL) {
            Status = pBS->AllocatePool(EfiBootServicesData, StringSize, &String);
            if(!EFI_ERROR(Status))
                Status = HiiString->GetString(HiiString, PlatformLang, HiiHandle, StringId, String, &StringSize, NULL);
        }
        pBS->FreePool(PlatformLang);
    }
    return (EFI_ERROR(Status)) ? NULL : String;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   HddPasswordGetDeviceName
//
// Description: Return the Drive String Name
//
// Input:   IN EFI_HANDLE Controller - the handle of the drive
//          OUT CHAR16 **wsName - Pointer to the drive string
//
// Output:  BOOLEAN - TRUE - drive string has been found and is in wsName
//                  - FALSE - drive string has not been found
//
//  Notes: it is the caller's responsibility to deallocate the space used for
//      wsName
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN HddPasswordGetDeviceName(IN EFI_HANDLE Controller, OUT CHAR16 **wsName)
{
    EFI_STATUS Status;
    SECURITY_PROTOCOL *Security = NULL;
    CHAR16 *DeviceName;
    BOOLEAN ReturnStatus = FALSE;

    // Get the SECURITY_PROTOCOL (actually getting the IDE_SECURITY_PROTOCOL, but
    //  the SECURITY_PROTOCOL is an extended version with more information)
    Status = pBS->HandleProtocol(Controller, &gIDESecurityProtocolGuid, &Security);
    if ( !EFI_ERROR(Status) ) {
        // Check the SATA controller operating mode, and based on the mode, get the UnicodeString
        //  name of the device
        if ( Security->ModeFlag ) {
            DeviceName = ((SATA_DEVICE_INTERFACE*)Security->BusInterface)->UDeviceName->UnicodeString;
        } else {
            DeviceName = ((IDE_BUS_PROTOCOL*)Security->BusInterface)->IdeDevice.UDeviceName->UnicodeString;
        }

        // Allocate space to copy the unicode device name string
        *wsName = MallocZ(sizeof(CHAR16)*(Wcslen(DeviceName)+1));
        if ( *wsName!=NULL ) {
              Wcscpy( *wsName, DeviceName);
            ReturnStatus = TRUE;
        }
    }

    // Return true to signify that a device name was discovered
    return ReturnStatus;
}

#endif

// Function Definitions


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitIdeSecurityStrings
//
// Description: This function initializes the IdeSecurity setup option values
//
// Input:       HiiHandle   Handle to HII database
//              Class       Indicates the setup class
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID InitIdeSecurityStrings(
    EFI_HII_HANDLE HiiHandle,
    UINT16         Class )
{
#if defined(SECUITY_SETUP_ON_SAME_PAGE) && SECUITY_SETUP_ON_SAME_PAGE
    EFI_STATUS                      Status;
    EFI_HANDLE                      *HandleBuffer = NULL;
    UINT16                          i, HDDCount = 0;
    UINTN                           Count;
    CHAR16                          * Name, *Temp1;
    BOOLEAN                         Locked;
    IDE_SECURITY_PROTOCOL           *IDEPasswordSecurity = NULL;
    EFI_DEVICE_PATH_PROTOCOL        *DevicePath;
    EFI_DEVICE_PATH_PROTOCOL        *DevicePathNode;
    PCI_DEVICE_PATH                 *PciDevicePath;
    SECURITY_PROTOCOL               *Security = NULL;
    UINT32                          HddPortNumber;
    CHAR16                          *Buff=L"P";
    EFI_DISK_INFO_PROTOCOL          *DiskInfoPtr=NULL;
    UINT32                          PortNumber;
    UINT32                          PMPortNumber;
    BOOLEAN                         Flag = FALSE;

    // Locate all handles with IDE_SECURITY_PROTOCOL installled
    Status = pBS->LocateHandleBuffer(   ByProtocol,
                                        &gIDESecurityProtocolGuid,
                                        NULL,
                                        &Count,
                                        &HandleBuffer);
    if ( EFI_ERROR( Status )) {
        return;
    }

    // For each handle in HandleBuffer
    for ( i = 0; i < Count; i++ ) {
        //
        // Get the PasswordSecurity Protocol
        //
        Status = pBS->OpenProtocol( HandleBuffer[i],
                                    &gIDESecurityProtocolGuid,
                                    (VOID**) &IDEPasswordSecurity,
                                    NULL,
                                    HandleBuffer[i],
                                    EFI_OPEN_PROTOCOL_GET_PROTOCOL);
        if ( EFI_ERROR( Status ))
            continue;
        //
        // Get the DiskInfo Protocol
        //   
        Status = pBS->OpenProtocol( HandleBuffer[i],
                                    &gDiskInfoProtocolGuid,
                                    (VOID**) &DiskInfoPtr,
                                    NULL,
                                    HandleBuffer[i],
                                    EFI_OPEN_PROTOCOL_GET_PROTOCOL);
        if ( EFI_ERROR( Status )){
            continue;
        }
        //
        // Locate the device path Protocol
        //
        Status = pBS->OpenProtocol( HandleBuffer[i],
                                    &gDevicePathProtocolGuid,
                                    (VOID**)&DevicePath,
                                    NULL,
                                    HandleBuffer[i],
                                    EFI_OPEN_PROTOCOL_GET_PROTOCOL);
        if ( EFI_ERROR( Status )){
            continue;
        }

        DevicePathNode = DevicePath;

        //
        // Traverse the Device Path structure till we reach HARDWARE_DEVICE_PATH
        //
        while (!isEndNode (DevicePathNode)) {

            if ((DevicePathNode->Type == HARDWARE_DEVICE_PATH) &&
                (DevicePathNode->SubType == HW_PCI_DP)){

                PciDevicePath = (PCI_DEVICE_PATH *) DevicePathNode;
                break;
            }

            DevicePathNode = NEXT_NODE (DevicePathNode);
        }
        // Check whether valid DevicePath found
        if (PciDevicePath == NULL) continue;

        Security=(SECURITY_PROTOCOL *)IDEPasswordSecurity;

        if(Security->ModeFlag){
            //
            //  Controller is in Ahci Mode, Call WhichIde function to find out Port Number
            //
            DiskInfoPtr->WhichIde(DiskInfoPtr,&PortNumber,&PMPortNumber);
            //
            //  Assign the PortNumber to HddPortNumber.This Port Number is displayed in Setup.
            //  
            HddPortNumber=PortNumber;
            Flag=TRUE;

        }else{

#ifdef SB_TEMPLATE_VER
#if SB_TEMPLATE_VER > 6
{
            UINT32      IdeChannel;
            UINT32      IdeDevice;

            if(gSbHddPolicyPtr==NULL){
                Status=gBS->LocateProtocol(&gSbHddPolicyProtocolGuid,
                                            NULL, \
                                            &gSbHddPolicyPtr);
            }

            if(gSbHddPolicyPtr!=NULL){

                //
                //  Find out the Primary/Secondary,Master/Slave Info from WhichIde function  
                //
                DiskInfoPtr->WhichIde(DiskInfoPtr,&IdeChannel,&IdeDevice);
                //
                //  Get the Port Number to which the HDD is connected
                //
                gSbHddPolicyPtr->GeneratePortNumber(PciDevicePath->Device,PciDevicePath->Function,
                                                    IdeChannel,IdeDevice,&PortNumber);

                HddPortNumber=PortNumber;
                Flag=TRUE;

            }else{
                //
                // SB HDD Policy Protocol is not Present. 
                //
                Flag=FALSE;
            }
}
#endif
#endif
            
        }

        if ( CheckSecurityStatus( IDEPasswordSecurity, &Locked, 0x0004 )) {
            if ( HddPasswordGetDeviceName( HandleBuffer[i], &Name )) {
                Name[12] = 0;
                if(Flag){
                    //
                    //  Display the the Port Number in Setup
                    // 
                    InitString(HiiHandle, gHddSecurityName[i], L"%s%d:%s", L"P", HddPortNumber, Name);     
                }else{
                    //
                    //  If SB HDD Policy Protocol is not Installed Use STR_IDE_SECURITY_PROMPT
                    //  token to display the String Information.
                    //
                    Temp1 = HiiGetString( HiiHandle, STRING_TOKEN( STR_IDE_SECURITY_PROMPT ));
                    InitString(HiiHandle, gHddSecurityName[i], L"%s%d:%s",Temp1,HDDCount,Name);
                }
            } else {
                if(Flag){
                    //
                    //  Display the the Port Number in Setup
                    //   
                    InitString(HiiHandle, gHddSecurityName[i], L"%s%d", L"P", HddPortNumber);     
                }else{
                    //
                    //  If SB HDD Policy Protocol is not Installed Use STR_IDE_SECURITY_PROMPT
                    //  token to display the String Information.
                    //
                    Temp1 = HiiGetString( HiiHandle, STRING_TOKEN( STR_IDE_SECURITY_PROMPT ));
                    InitString(HiiHandle, gHddSecurityName[i], L"%s%d", Temp1, HDDCount);
                }
            }
            HDDCount++;
         }// end if

    }// end of for   
#endif
    return;
}

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