summaryrefslogtreecommitdiff
path: root/Core/EM/IdeBus/IdeHPA.c
blob: 273cbd2beb6fd04d49c2505d14abd68727ecd730 (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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (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/Core/Modules/IdeBus/IdeHPA.c 6     10/11/10 11:31a Krishnakumarg $
//
// $Revision: 6 $
//
// $Date: 10/11/10 11:31a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/Modules/IdeBus/IdeHPA.c $
// 
// 6     10/11/10 11:31a Krishnakumarg
// [TAG] - EIP 43249
// [Category] - IMPROVEMENT
// [Severity] - Minor
// [Symptom]  - Non-Ascii character in comments causing build problem in
// japanese XP
// [RootCause]- Presence of Non-Ascii character
// [Solution] -  Remove Non-Ascii character present in the file
// [Files] - IdeSecurity.c,IDESMM.c, InstallIdeSmi, Ata.c, Atapi.c,
// IDEBusComponentName.c, IdeBusMaster.c, IdeHPA.c, IdePowerManagement.c
// 
// 5     8/25/10 4:10a Rameshr
// New Feature: EIP 37748
// Description: Move all the IDEBus Source driver SDL token into IdeBus
// Bin Driver.
// FilesModified: Ata.c, Atapi.c, idebus.c, IdeBus.h,
// IdebuscomponentName.c, IdeBusmaster.c IdeBusSrc.mak IdebusSrc.sdl
// IdeHpa.c, IdePowerManagement.c
// 
// 4     7/01/09 12:24p Rameshr
// Coding Standard and File header updated.
//
// 3     13/04/07 3:03p Anandakrishnanl
// Ide Bus Module - Update source files to comply with AMI coding
// standard"!!!
//
// 2     8/24/06 9:30a Felixp
// x64 support (bug fixes)
//
// 1     12/01/05 9:43a Felixp
//
// 4     3/04/05 11:35a Mandal
//
// 3     1/18/05 3:22p Felixp
// PrintDebugMessage renamed to Trace
//
// 1     1/13/05 4:25p Srinin
// Host Protection Area Support added.
//
//
//
//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name: IdeHPA.c
//
// Description:	Host Protection Area Support
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "IdeBus.h"

extern EFI_GUID gHPAProtocolGuid;

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	InstallHPAInterface
//
// Description:	Checks whether Host Protcted Area feature is supported or not.
//				If Supported, installs HPAProtocol.
//
// Input:
//		IDE_BUS_PROTOCOL			*IdeBusInterface,
//
// Output:
//	EFI_STATUS  : EFI_UNSUPPORTED/EFI_SUCCESS/EFI_OUT_OF_RESOURCES
//
// Modified:
//
// Referrals: IdeBusStart
//
// Notes:
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS InstallHPAInterface(
    IN IDE_BUS_PROTOCOL *IdeBusInterface )
{
    EFI_STATUS    Status;
    HPA_INTERFACE *HPAInterface;

    //
    //Check if HPA is supported or not
    //    
    if  ((IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_82 & 0x400) == 0 ) {
        return EFI_UNSUPPORTED;
    }

    //
    //Install the Interface
    //
    Status = pBS->AllocatePool( EfiBootServicesData,
                                sizeof(HPA_INTERFACE),
                                (VOID**)&HPAInterface );

    if ( EFI_ERROR( Status ))  {
        pBS->FreePool( HPAInterface );
        return Status;
    }

    HPAInterface->IdeBusInterface     = IdeBusInterface;
    IdeBusInterface->HPAInterface     = HPAInterface;
    HPAInterface->GetNativeMaxAddress = GetNativeMaxAddress;
    HPAInterface->SetMaxAddress       = SetMaxAddress;
    HPAInterface->HPADisabledLastLBA  = HPADisabledLastLBA;
    HPAInterface->SetMaxPassword      = SetMaxPassword;
    HPAInterface->SetMaxLock          = SetMaxLock;
    HPAInterface->SetMaxUnLock        = SetMaxUnLock;
    HPAInterface->SetMaxFreezeLock    = SetMaxFreezeLock;

    Status = pBS->InstallMultipleProtocolInterfaces(
        &(IdeBusInterface->IdeDeviceHandle),
        &gHPAProtocolGuid, HPAInterface,
        NULL );

    if ( EFI_ERROR( Status )) {
        pBS->FreePool( HPAInterface );
        return Status;
    }

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	StopHPAInterface
//
// Description:	Uninstall HPA Protocol
//
// Input:
//		IDE_BUS_PROTOCOL			*IdeBusInterface,
//
// Output:
//	EFI_STATUS
//
// Modified:
//
// Referrals: IdeBusStop
//
// Notes:
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS StopHPAInterface(
    IN IDE_BUS_PROTOCOL *IdeBusInterface )
{
    EFI_STATUS Status;

    if ( IdeBusInterface->HPAInterface == NULL ) {
        return EFI_SUCCESS;
    }

    Status = pBS->UninstallMultipleProtocolInterfaces(
        IdeBusInterface->IdeDeviceHandle,
        &gHPAProtocolGuid, IdeBusInterface->HPAInterface,
        NULL );

    if ( Status == EFI_SUCCESS ) {
        pBS->FreePool( IdeBusInterface->HPAInterface );
        IdeBusInterface->HPAInterface = NULL;
    }
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	GetNativeMaxAddress
//
// Description:	Returns the Native Max LBA address supported by the device
//
// Input:
//	IN      IDE_HPA_INTERFACE			*This,
//	OUT	UINT64						*LBA
//
// Output:			LBA : Returns Native MAX LBA address.
//					EFI_STATUS  : EFI_SUCCESS/EFI_DEVICE_ERROR
//
// Modified:
//
// Referrals:
//
// Notes:
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS GetNativeMaxAddress(
    IN IDE_HPA_INTERFACE *This,
    OUT UINT64           *LBA )
{
    EFI_STATUS       Status;
    BOOLEAN          Ext48BitSupport         = FALSE;
    UINT8            ReadNativeMaxAddressCmd = READ_NATIVE_MAX_ADDRESS;
    IDE_BUS_PROTOCOL *IdeBusInterface        = ((HPA_INTERFACE*)This)->IdeBusInterface;
    UINT32           dData;
    UINT8            bData;

    *LBA = 0;

    //
    //Check for 48 Bit Mode Support
    //
    if ( IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x400 ) {
        ReadNativeMaxAddressCmd = READ_NATIVE_MAX_ADDRESS_EXT;
    }

    //
    //Get Native Max Address.
    //
    Status = IdeNonDataCommand( IdeBusInterface,
                                0,              // Features
                                0,              // Sector Count
                                0,              // LBA Low
                                0,              // LBA Mid
                                0,              // LBA High
                                0x40 | IdeBusInterface->IdeDevice.Device << 4,  // Device
                                ReadNativeMaxAddressCmd );                      // Command

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


    //
    //Read Bits 47:24 if 48Bit LBA is supported
    //
    if ( IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x400 ) {
        //
        //Set HOB to 1 to read the upper DWROD
        //
        IdeWriteByte( IdeBusInterface->PciIO,
                      IdeBusInterface->IdeDevice.Regs.ControlBlock.DeviceControlReg,
                      HOB | nIEN );
        dData = 0;
        IdeReadByte( IdeBusInterface->PciIO,
                     IdeBusInterface->IdeDevice.Regs.CommandBlock.LBAHighReg,
                     &bData );

        dData = bData;                              // LBA 47:40
        IdeReadByte( IdeBusInterface->PciIO,
                     IdeBusInterface->IdeDevice.Regs.CommandBlock.LBAMidReg,
                     &bData );
        dData <<= 8;
        dData  |= bData;                             // LBA 39:32

        IdeReadByte( IdeBusInterface->PciIO,
                     IdeBusInterface->IdeDevice.Regs.CommandBlock.LBALowReg,
                     &bData );
        dData <<= 8;
        dData  |= bData;                             // LBA 31:24

        *LBA = dData;
        //
        //Shift Left *LBA by 24 Bits
        //
        *LBA = Shl64(*LBA, 24);
        //
        //Set HOB to 0 to read the Lower DWROD
        //
        IdeWriteByte( IdeBusInterface->PciIO,
                      IdeBusInterface->IdeDevice.Regs.ControlBlock.DeviceControlReg,
                      nIEN );
    }

    //
    //Read 27:24 only when 48Bit LBA is not supported
    //
    if ((IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x400) == 0 ) {
        IdeReadByte( IdeBusInterface->PciIO,
                     IdeBusInterface->IdeDevice.Regs.CommandBlock.DeviceReg,
                     &bData );
        bData &= 0xf;
        dData  = bData;                              // LBA 27:24
    }

    //
    //Read 23:0 bits of LBA address
    //
    IdeReadByte( IdeBusInterface->PciIO,
                 IdeBusInterface->IdeDevice.Regs.CommandBlock.LBAHighReg,
                 &bData );
    dData <<= 8;                                // LBA 23:16
    dData  |= bData;
    IdeReadByte( IdeBusInterface->PciIO,
                 IdeBusInterface->IdeDevice.Regs.CommandBlock.LBAMidReg,
                 &bData );
    dData <<= 8;
    dData  |= bData;                             // LBA 15:8
    IdeReadByte( IdeBusInterface->PciIO,
                 IdeBusInterface->IdeDevice.Regs.CommandBlock.LBALowReg,
                 &bData );
    dData <<= 8;
    dData  |= bData;                             // LBA 7:0

    ((UINT32) *LBA) |= dData;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	SetMaxAddress
//
// Description:	Set the Max LBA address
//
// Input:
//	IN      IDE_HPA_INTERFACE			*This,
//	OUT     UINT64						LBA,
//	IN      BOOLEAN						Volatile
//
// Output:
//	EFI_STATUS  :
//
// Modified:
//
// Referrals:
//
// Notes:	if Volatile = TRUE, Values will not be preserved across resets.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SetMaxAddress(
    IN IDE_HPA_INTERFACE *This,
    OUT UINT64           LBA,
    IN BOOLEAN           Volatile )
{
    EFI_STATUS       Status;
    UINT32           dData;
    UINT32           Cmd;
    UINT64           NativeMaxLBA;
    UINT8            bData;
    IDE_BUS_PROTOCOL *IdeBusInterface = ((HPA_INTERFACE*)This)->IdeBusInterface;
    BOOLEAN          Ext48BitSupport  = FALSE;


    //
    //Issue Read MAX Native command before issuing a SET MAX Address command
    //
    Status = GetNativeMaxAddress( This, &NativeMaxLBA );

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

    //
    //Check for 48 Bit Mode Support
    //
    if ( IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x400 ) {
        Ext48BitSupport = TRUE;
    }

    //
    //If 48Bit LBA is supported write LBA bits 47:24
    //
    if ( Ext48BitSupport ) {
        //		Enable HOB
        IdeWriteByte( IdeBusInterface->PciIO,
                      IdeBusInterface->IdeDevice.Regs.ControlBlock.DeviceControlReg,
                      HOB | nIEN );
        //get Bits 47:24 from LBA to dData
        dData = (UINT32)Shr64(LBA, 24);

        //dDATA has LBA bits 47:24
        bData = dData >> 16;                // LBA 47:40
        IdeWriteByte( IdeBusInterface->PciIO,
                      IdeBusInterface->IdeDevice.Regs.CommandBlock.LBAHighReg,
                      bData );

        bData = dData >> 8;                 // LBA 39:32
        IdeWriteByte( IdeBusInterface->PciIO,
                      IdeBusInterface->IdeDevice.Regs.CommandBlock.LBAMidReg,
                      bData );

        bData = dData;                      // LBA 31:24
        IdeWriteByte( IdeBusInterface->PciIO,
                      IdeBusInterface->IdeDevice.Regs.CommandBlock.LBALowReg,
                      bData );

        //Reset HOB
        IdeWriteByte( IdeBusInterface->PciIO,
                      IdeBusInterface->IdeDevice.Regs.ControlBlock.DeviceControlReg,
                      nIEN );
    }

    //
    //	Write Bits 23:0
    //
    dData = (UINT32) LBA;

    //
    //	if 48Bit LBA is not supported, write bits 27:24 to Device register
    //
    if ( Ext48BitSupport == FALSE ) {
        bData = dData >> 24;
    }
    else {
        bData = 0;
    }

    bData &= 0xf;
    bData |= 0x40 | IdeBusInterface->IdeDevice.Device << 4;
    Cmd    = Ext48BitSupport == FALSE ? SET_MAX_ADDRESS : SET_MAX_ADDRESS_EXT;

    //
    //Set Max Address.
    //
    Status = IdeNonDataCommand( IdeBusInterface,
                                0,          // Features
                                Volatile == TRUE ? 0 : 1, // Sector Count
                                (UINT8)dData,       // LBA Low
                                (UINT8)( dData >> 8 ),    // LBA Mid
                                (UINT8)( dData >> 16 ),   // LBA High
                                bData,      // Device
                                Cmd ); // Command

    //
    //	Update Identify Data
    //
    GetIdentifyData( IdeBusInterface, &(IdeBusInterface->IdeDevice));

    //
    //Update the LAST LBA number
    //
    if ( Ext48BitSupport ) {
        IdeBusInterface->IdeBlkIo->BlkIo.Media->LastBlock = IdeBusInterface->IdeDevice.IdentifyData.LBA_48 - 1;
    }
    else {
        IdeBusInterface->IdeBlkIo->BlkIo.Media->LastBlock = IdeBusInterface->IdeDevice.IdentifyData.Addressable_Sector_60 - 1;
    }

    return Status;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	HPADisabledLastLBA
//
// Description:	Returns the valid last LBA # when HPA is disabled.
//
// Input:
//	IN      IDE_HPA_INTERFACE			*This,
//	OUT	    UINT64						*LBA
//
// Output:
//	EFI_STATUS  : EFI_SUCCESS
//
// Modified:
//
// Referrals:
//
// Notes:	The LBA returned will be the maximum valid LBA number an OS can make use of.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS HPADisabledLastLBA(
    IN IDE_HPA_INTERFACE *This,
    OUT UINT64           *LBA )
{
    BOOLEAN Ext48BitSupport           = FALSE;
    IDE_BUS_PROTOCOL *IdeBusInterface = ((HPA_INTERFACE*)This)->IdeBusInterface;

    //
    //Check for 48 Bit Mode Support
    //
    if ( IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x400 ) {
        Ext48BitSupport = TRUE;
    }

    //
    //Update the LAST LBA number
    //
    if ( Ext48BitSupport ) {
        *LBA = IdeBusInterface->IdeDevice.IdentifyData.LBA_48 - 1;
    }
    else {
        *LBA = IdeBusInterface->IdeDevice.IdentifyData.Addressable_Sector_60 - 1;
    }
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	SetMaxPassword
//
// Description:	Sets the Password
//
// Input:
//	IN      IDE_HPA_INTERFACE			*This,
//	IN	    UINT8						*PasswordBuffer, (32 Bytes of Password)
//
// Output:
//	EFI_STATUS  : EFI_UNSUPPORTED/EFI_SUCCESS/EFI_DEVICE_ERROR
//
// Modified:
//
// Referrals:
//
// Notes:	READ NATIVE MAX ADDRESS should not have immediatley PRECEEDED this command.
//			To take care of this situation, a Dummy Identify Device Command will be given before
//			issuing a SET MAX PASSWORD command
//			Once the device is locked, it should be unlocked with the password before
//			issuing any SETMAXADDRESS.
//			After issuing this command device will be in UNLOCKED state.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SetMaxPassword(
    IN IDE_HPA_INTERFACE *This,
    IN UINT8             *PasswordBuffer )
{
    EFI_STATUS       Status;
    IDE_BUS_PROTOCOL *IdeBusInterface = ((HPA_INTERFACE*)This)->IdeBusInterface;

    //
    //Check for HPA feature set security extensions are implemented or not?
    //
    if ( !(IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x100)) {
        return EFI_UNSUPPORTED;
    }

    Status = IssueSetMaxPasswordCmd( IdeBusInterface, PasswordBuffer, SET_MAX_SET_PASSWORD );

    //
    //Update the Identify Device Data buffer
    //
    GetIdentifyData( IdeBusInterface, &(IdeBusInterface->IdeDevice));

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	SetMaxLock
//
// Description:	Locks the device from accepting commands except UNLOCK and FREEZELOCK
//
// Input:
//	IN      IDE_HPA_INTERFACE			*This,
//
// Output:
//	EFI_STATUS  : EFI_UNSUPPORTED/EFI_SUCCESS/EFI_DEVICE_ERROR
//
// Modified:
//
// Referrals:
//
// Notes:	READ NATIVE MAX ADDRESS should not have immediatley PRECEED this command.
//			To take care of this situation, a Dummy Identify Device Command will be given before
//			issuing a SET MAX LOCK/UNLOCK command
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SetMaxLock(
    IN IDE_HPA_INTERFACE *This )
{
    EFI_STATUS       Status;
    IDE_BUS_PROTOCOL *IdeBusInterface = ((HPA_INTERFACE*)This)->IdeBusInterface;

    //Check for HPA feature set security extensions are implemented or not? AND
    //Check whether SET MAX PASSWORD command has been issued before or not?
    if ((!(IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x100))
        || (!(IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Enabled_86 & 0x100))) {
        return EFI_UNSUPPORTED;
    }

    Status = IdeNonDataCommand(
        IdeBusInterface,
        SET_MAX_LOCK,           // Features
        0,              // Sector Count
        0,              // LBA Low
        0,              // LBA Mid
        0,              // LBA High
        IdeBusInterface->IdeDevice.Device << 4, // Device
        SET_MAX_ADDRESS );       // Command

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	SetMaxUnLock
//
// Description:	Unlocks the device and allows it to accept all SET MAX commands
//
// Input:
//	IN      IDE_HPA_INTERFACE			*This,
//	IN	    UINT8						*PasswordBuffer
//
// Output:
//	EFI_STATUS  : EFI_UNSUPPORTED/EFI_SUCCESS/EFI_DEVICE_ERROR
//
// Modified:
//
// Referrals:
//
// Notes:	READ NATIVE MAX ADDRESS should not have immediatley PRECEED this command.
//			To take care of this situation, a Dummy Identify Device Command will be given before
//			issuing a SET MAX LOCK/UNLOCK command
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SetMaxUnLock(
    IN IDE_HPA_INTERFACE *This,
    IN UINT8             *PasswordBuffer )
{
    EFI_STATUS       Status;
    IDE_BUS_PROTOCOL *IdeBusInterface = ((HPA_INTERFACE*)This)->IdeBusInterface;

    //Check for HPA feature set security extensions are implemented or not? AND
    //Check whether SET MAX PASSWORD command has been issued before or not?
    if ((!(IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x100))
        || (!(IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Enabled_86 & 0x100))) {
        return EFI_UNSUPPORTED;
    }

    Status = IssueSetMaxPasswordCmd( IdeBusInterface, PasswordBuffer, SET_MAX_UNLOCK );

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	SetMaxFreezeLock
//
// Description:	Locks all SET MAX ADDRESS/PASSWORD/LOCK/UNLOCK command untill
//				next power cycle. GetNativeMaxAddress is allowed.
//
// Input:
//	IN      IDE_HPA_INTERFACE			*This,
//
// Output:
//	EFI_STATUS  : EFI_UNSUPPORTED/EFI_SUCCESS/EFI_DEVICE_ERROR
//
// Modified:
//
// Referrals:
//
// Notes:	READ NATIVE MAX ADDRESS should not have immediatley PRECEED this command.
//			To take care of this situation, a Dummy Identify Device Command will be given before
//			issuing a SET MAX FREEZELOCK command
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SetMaxFreezeLock(
    IN IDE_HPA_INTERFACE *This )
{
    EFI_STATUS       Status;
    IDE_BUS_PROTOCOL *IdeBusInterface = ((HPA_INTERFACE*)This)->IdeBusInterface;

    //Check for HPA feature set security extensions are implemented or not? AND
    //Check whether SET MAX PASSWORD command has been issued before or not?
    if ((!(IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Supported_83 & 0x100))
        || (!(IdeBusInterface->IdeDevice.IdentifyData.Command_Set_Enabled_86 & 0x100))) {
        return EFI_UNSUPPORTED;
    }

    Status = IdeNonDataCommand(
        IdeBusInterface,
        SET_MAX_FREEZE_LOCK, // Features
        0,              // Sector Count
        0,              // LBA Low
        0,              // LBA Mid
        0,              // LBA High
        IdeBusInterface->IdeDevice.Device << 4, // Device
        SET_MAX_ADDRESS );       // Command

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:	IssueSetMaxPasswordCmd
//
// Description:	Sets the Password to Lock/Unlock use of SETMAXADDRESS command
//
// Input:
//	IN      IDE_HPA_INTERFACE			*This,
//	IN	BOOLEAN						LockUnLock (TRUE : LOCK, FALSE :UNLOCK
//
// Output:
//	EFI_STATUS  : EFI_UNSUPPORTED/EFI_SUCCESS/EFI_DEVICE_ERROR
//
// Modified:
//
// Referrals:
//
// Notes:	READ NATIVE MAX ADDRESS should not have immediatley PRECEED this command.
//			To take care of this situation, a Dummy Identify Device Command will be given before
//			issuing a SET MAX LOCK/UNLOCK command
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS IssueSetMaxPasswordCmd(
    IDE_BUS_PROTOCOL *IdeBusInterface,
    UINT8            *PasswordBuffer,
    UINT8            Cmd )
{
    EFI_STATUS Status;
    UINT8      *Buffer;

    //Issues Identify Device Command
    //Allocate 512 Bytes
    Status = pBS->AllocatePool( EfiBootServicesData,
                                512,
                                (VOID**)&Buffer );

    if ( EFI_ERROR( Status ))  {
        pBS->FreePool( Buffer );
        return Status;
    }

    Status = GetIdentifyData( IdeBusInterface, Buffer );

    //
    //Clear the Buffer
    //
    ZeroMemory( Buffer, 512 );

    //
    //Copy 32 bytes to Password starting from offset 2
    //
    pBS->CopyMem( &(Buffer[2]), PasswordBuffer, 32 );

    //	Issue SET MAX PASSWORD Command
    //	Update the Feature Register
    IdeWriteByte( IdeBusInterface->PciIO,
                  IdeBusInterface->IdeDevice.Regs.CommandBlock.FeatureReg,
                  Cmd );
    Status = AtaReadWritePio( IdeBusInterface, Buffer, 512, 0, SET_MAX_ADDRESS, TRUE );

    pBS->FreePool( Buffer );
    return Status;
}


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