summaryrefslogtreecommitdiff
path: root/ReferenceCode/Pfat/PfatFlashLib/PfatFlashLib.c
blob: 2d144d17c44e63b3885ecbefbb0119a88b5fe9c1 (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
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/SharkBayRefCodes/IntelPfat/Pfatflashlib/PfatFlashLib.c 9     4/24/13 7:39p Fredericko $
//
// $Revision: 9 $
//
// $Date: 4/24/13 7:39p $
//
// $Log: /Alaska/SOURCE/Modules/SharkBayRefCodes/IntelPfat/Pfatflashlib/PfatFlashLib.c $
// 
// 9     4/24/13 7:39p Fredericko
// [TAG]  		EIP120447
// [Category]  	Improvement
// [Description]  	SHB ULT:  PFAT : Update with AFU Tools Fails SUT Locks
// [Files]  		PfatFlashLib.c
// PfatFlashLib.sdl
// 
// 8     12/19/12 7:23p Fredericko
// [TAG]  		EIP109350
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Solution]  	Building error when the token of BUILD_PEI_FLASH_LIB = 1
// for PFAT module
// [Files]  		PfatFlashLib.c
// PfatFlashLib.mak
// 
// 7     12/03/12 6:19p Fredericko
// [TAG]  		EIP107344
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Afulnx fails
// [Solution]  	AFULNX2(Test BIOS to Test BIOS)_1AQPM016_Pretest
// [Files]  		PfatFlashLib.c
// 
// 6     11/08/12 3:23a Fredericko
// Changes for flash write. Requires Pi 1.1 and above support
// 
// 5     11/02/12 8:26p Fredericko
// [TAG]  		EIP105153
// [Description]  	Possible system hang if NVRAM is access during legacy
// O.S.
// 
// 4     10/16/12 6:45p Fredericko
// [TAG]  		EIP103945
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	System hang
// [RootCause]  	PfatInitialize check failure
// [Solution]  	The system can't enter UEFI WIN8 OS when PFAT is Disabled
// on seutp menu
// [Files]  		PfatFlashLib.c
// 
// 3     9/26/12 11:45a Fredericko
// [TAG]  		EIP102376
// [Description]  	Pfat CHM
// 
// 2     9/17/12 4:27p Fredericko
// 
// Initial Checkin
//
//
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:   PfatFlashLib.c
//
// Description: Code listing file for Pfat flashlib
//
//<AMI_FHDR_END>
//**********************************************************************

//----------------------------------------------------------------------
// Includes
#include <efi.h>
#include <FlashPart.h>
#include "token.h"
#if defined _DXE_FLASH_LIB_
#include <AmiDxeLib.h>
#include "Pfat\Pfat.h"
#include <Protocol\SmmBase2.h>
#include <Protocol\SmiFlash.h>
#include <AmiCspLib.h>
#include "ReferenceCode\Haswell\Include\CpuRegs.h"
#ifndef BIT35
#define BIT35 0x0000000800000000ULL
#endif


typedef BOOLEAN (IDENTIFY)(
    volatile UINT8*     pBlockAddress,
    FLASH_PART          **Struct
    );

extern UINT16       gFlashId;
FLASH_PART          *FlashInitialized = NULL;
PFAT_PROTOCOL       *PfatInstance = NULL;
extern IDENTIFY*    FlashList[];
EFI_SMM_SYSTEM_TABLE2   *mSmst = NULL;
EFI_SMM_BASE2_PROTOCOL *gSmmBase2 = NULL;
BOOLEAN				 InSmm;
EFI_EVENT           SmmPfatAvailableEvent;
EFI_EVENT           SmmAvailableEvent;
EFI_GUID            gSmmPfatProtocolGuid = SMM_PFAT_PROTOCOL_GUID;

VOID
PfatFlashEraseCommand   (
    volatile UINT8          *pBlockAddress
);


VOID
PfatProgramCommand (
    volatile UINT8          *pByteAddress,
    UINT8                   *Byte,
    UINT32                  *Length
);


VOID
PfatReadCommand    (
    volatile UINT8          *pByteAddress,
    UINT8                   *Byte,
    UINT32                  *Length
);


BOOLEAN
PfatIsEraseCompleted   (
   IN  volatile UINT8          *pBlockAddress,
   OUT BOOLEAN                 *pError,
   OUT UINTN                   *pStatus
);


BOOLEAN
PfatProgramCompleted (
    volatile UINT8          *pByteAddress,
    UINT8                   *Byte,
    UINT32                  Length,
    BOOLEAN                 *pError,
    UINTN                   *pStatus
);


VOID
PfatBlockWriteEnable(
   UINT8      *pBlockAddress
);


VOID
PfatBlockWriteDisable  (
  UINT8          *pBlockAddress
);


VOID
PfatDeviceWriteEnable  (
    VOID
);


VOID
PfatDeviceWriteDisable (
    VOID
);


VOID
PfatDeviceVirtualFixup (
    EFI_RUNTIME_SERVICES    *pRS
);


#define SECTOR_SIZE_4KB             0x1000  // Common 4kBytes sector size

FLASH_PART mPfatFlash =
    {
        PfatReadCommand,
        PfatFlashEraseCommand,
        PfatProgramCommand,
        PfatIsEraseCompleted,
        PfatProgramCompleted,
        PfatBlockWriteEnable,
        PfatBlockWriteDisable,
        PfatDeviceWriteEnable,
        PfatDeviceWriteDisable,
        PfatDeviceVirtualFixup,      
        1,                           
        SECTOR_SIZE_4KB                    
    };





EFI_STATUS
OnSmmPfatProtocolAvailable (
  IN  EFI_EVENT                   Event,
  IN  VOID                        *Context
  )
{
     EFI_STATUS Status;
  
     if(gSmmBase2 == NULL){
        Status = pBS->LocateProtocol( &gEfiSmmBase2ProtocolGuid, NULL, &gSmmBase2 );
     }
          
     if(gSmmBase2!= NULL){
         if(mSmst == NULL){
              Status = gSmmBase2->GetSmstLocation (gSmmBase2, &mSmst);
         }
      }
                
      Status = gSmmBase2->InSmm(gSmmBase2, &InSmm);

      Status = pBS->LocateProtocol (&gSmmPfatProtocolGuid,
                                        NULL,
                                        &PfatInstance);

      pBS->CloseEvent (SmmPfatAvailableEvent);
      return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   IdentifyPfatFlash
//
// Description: Identifies whether PfatFlash library should control access to
//              SPI flash part
//
// Input:   volatile UINT8 *pBlockAddress
//
// Output:  FLASH_PART     **FlashStruc
//
// Returns: BOOLEAN
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
IdentifyPfatFlash (
    IN  volatile UINT8 *pBlockAddress,
    OUT FLASH_PART     **FlashStruc
)
{
    UINTN       i;
    BOOLEAN     found = FALSE;  
    EFI_STATUS  Status;
    VOID       *Reg;
   
    if ( !((ReadMsr (MSR_PLATFORM_INFO) & B_MSR_PLATFORM_INFO_PFAT_AVAIL) && 
           (ReadMsr (MSR_PLAT_FRMW_PROT_CTRL) & B_MSR_PLAT_FRMW_PROT_CTRL_EN)) ) {  
         //
		 // Pfat disabled or not support
		 //
		 return FALSE;
    }

    if(FlashInitialized == NULL)
    {
       for(i=1; !found && FlashList[i]; i++)
       {
          found=FlashList[i](pBlockAddress, &FlashInitialized);
       }    
    }

    if(FlashInitialized == NULL)return FALSE;

    if(found)
    {
       *FlashStruc = &mPfatFlash;

        Status = pBS->CreateEvent (EFI_EVENT_NOTIFY_SIGNAL,
                                   TPL_CALLBACK,
                                   OnSmmPfatProtocolAvailable,
                                   NULL,
                                   &SmmPfatAvailableEvent);

        if(!EFI_ERROR(Status)){
            Status = pBS->RegisterProtocolNotify (
            &gSmmPfatProtocolGuid,
            SmmPfatAvailableEvent,
            &Reg);
        }
    }


    return found;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   IsPfatFlashInitialized
//
// Description: Verifies whether Pfat library has been initialized or not
//
// Input:   VOID
//
// Output:  VOID
//
// Returns: BOOLEAN
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
IsPfatFlashInitialized()
{
  EFI_STATUS  Status;
 
  if((PfatInstance == NULL) && (pST->BootServices != NULL)){
    if(InSmm == FALSE)
      {
        if(gSmmBase2 == NULL){
            Status = pBS->LocateProtocol( &gEfiSmmBase2ProtocolGuid, NULL, &gSmmBase2 );
         }
          
        if(gSmmBase2!= NULL){
             if(mSmst == NULL){
                  Status = gSmmBase2->GetSmstLocation (gSmmBase2, &mSmst);
            }
            Status = gSmmBase2->InSmm(gSmmBase2, &InSmm);
        }

              Status = pBS->LocateProtocol (&gSmmPfatProtocolGuid,
                                        NULL,
                                        &PfatInstance);
             
      }

  }

  if( PfatInstance != NULL ){ return TRUE; }
  PfatInstance = NULL;
  return FALSE;
}




//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatFlashEraseCommand
//
// Description: Erases SPI via PFAT library
//
// Input:   IN volatile UINT8*      pBlockAddress
//
// Output:  VOID
//
// Returns: VOID
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PfatFlashEraseCommand   (
    IN volatile UINT8*      pBlockAddress
)
{
  if(IsPfatFlashInitialized()){
      PfatInstance->Erase(PfatInstance,
                    (UINTN)(EFI_PHYSICAL_ADDRESS)pBlockAddress);
  }else{
    if ( FlashInitialized == NULL)return;
    FlashInitialized->FlashEraseCommand(pBlockAddress);
  }
}



//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatIsEraseCompleted 
//
// Description: Checks whether PFAT erase command was completed
//
// Input: volatile UINT8  *pBlockAddress
//
// Output: BOOLEAN                 *pError
//         UINTN                   *pStatus
//
// Returns: BOOLEAN
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN 
PfatIsEraseCompleted (
   IN  volatile UINT8          *pBlockAddress,
   OUT BOOLEAN                 *pError,
   OUT UINTN                   *pStatus
)
{
  if(IsPfatFlashInitialized()){
      *pError = FALSE;
      *pStatus = EFI_SUCCESS;
  }else{
    if ( FlashInitialized == NULL)return FALSE;
    return (FlashInitialized->FlashIsEraseCompleted(pBlockAddress,\
                             pError, pStatus));
  }

  return TRUE;
}



//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatFlashProgramCommand
//
// Description: Programs SPI part via PFAT library
//
// Input:  volatile UINT8*      pByteAddress
//         UINT8                *Byte
//         UINT32               *Length
//
// Output:  VOID
//
// Return:  VOID 
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PfatProgramCommand (
    IN volatile UINT8*      pByteAddress,
    IN UINT8                *Byte,
    IN UINT32               *Length
)
{ 
   if(IsPfatFlashInitialized()){  
        PfatInstance->Write(PfatInstance,
                       (UINTN)pByteAddress,
                       *Length, Byte);
        if(((*(UINT8 *)(UINTN)0xFED1F890) & BIT4))
        {
           (*(UINT16 *)(UINTN)0xFED1F804)|= BIT2;
        }
        PfatInstance->Execute(PfatInstance, FALSE);
        *Length = 0;

    }else{
       if ( FlashInitialized == NULL)return;
       FlashInitialized->FlashProgramCommand(pByteAddress, Byte, Length); 
    }
}




//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatReadCommand
//
// Description: Reads the SPI flash part via PFAT library
//      
//
// Input:   *pBlockAddress - Address to access flash part
//          *Byte - pointer to data to write to the flash part
//          Length - The total amount of data that Byte points to
//
// Output:  VOID
//
// Return:  VOID
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PfatReadCommand    (
    volatile UINT8          *pByteAddress,
    UINT8                   *Byte,
    UINT32                  *Length
)
{
    if ( FlashInitialized == NULL)return;
    FlashInitialized->FlashReadCommand(pByteAddress, Byte, Length);
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatProgramCompleted
//
// Description: Checks whether Pfat Flash program was completed
//      
//
// Input:   *pBlockAddress - Address to access flash part
//          *Byte - values previously written to the Flash Device
//          Length - The amount of data that needs to be checked
//          *pError - Boolean that tells if fatal error occured
//          *pStatus - Status of the erase command
//
// Output: *pError - Boolean that tells if fatal error occured
//         *pStatus - Status of the erase command
//
// Return:  TRUE - Program completed, check pError for fatal error
//          FALSE - programming in progress
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
PfatProgramCompleted (
    IN  volatile UINT8*     pByteAddress,
    IN  UINT8               *Byte,
    IN  UINT32              Length,
    OUT BOOLEAN             *pError,
    OUT UINTN               *pStatus
)
{
    if (pError) *pError = FALSE;
    return TRUE;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatBlockWriteEnable
//
// Description: This function chooses the correct flash part to call and
//      then enables write operations(erase/programming) for a specific block
//
// Input:   *pBlockAddress - Address to access flash part
//
// Output:  VOID
//
// Return:  VOID
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PfatBlockWriteEnable(
    UINT8      *pBlockAddress
)
{
    if (IsPfatFlashInitialized())return;
    if ( FlashInitialized == NULL)return;
    FlashInitialized->FlashBlockWriteEnable(pBlockAddress);
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatBlockWriteDisable
//
// Description: This function chooses the correct flash part to call and
//      then disables write operations(erase/programming) for a specific
//      block
//
// Input:   *pBlockAddress - Address to access flash part
//
// Output:  VOID
//
// Return:  VOID
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PfatBlockWriteDisable (
    IN  UINT8          *pBlockAddress
)
{
    if (IsPfatFlashInitialized())return;
    if ( FlashInitialized == NULL)return;
    FlashInitialized->FlashBlockWriteDisable(pBlockAddress);
}



//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatDeviceWriteEnable
//
// Description: This function chooses the correct flash part to call and
//      then enables write operation for a flash device
//
// Input:   VOID
//
// Output:  VOID
//
// Return:  VOID
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PfatDeviceWriteEnable (VOID)
{
    if (IsPfatFlashInitialized())return;
    if ( FlashInitialized == NULL)return;
    FlashInitialized->FlashDeviceWriteEnable();
}




//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatDeviceWriteDisable
//
// Description: This function chooses the correct flash part to call and
//      then disables write operation for a flash device
//
// Input:   VOID
//
// Output:  VOID
//
// Return:  VOID
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PfatDeviceWriteDisable(VOID)
{
    if (IsPfatFlashInitialized())return;
    if ( FlashInitialized == NULL)return;
    FlashInitialized->FlashDeviceWriteDisable();
}



//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   PfatDeviceVirtualFixup
//
// Description: Fixup global data for for a virtual address space.
//      This routine must be called by the library consumer in the
//      EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event handler
//
// Input:   EFI_RUNTIME_SERVICES *pRS - pointer to the Runtime Services Table
//
// Output:  VOID
//
// Return:  VOID
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PfatDeviceVirtualFixup   (
    IN EFI_RUNTIME_SERVICES *pRS
)
{
   VOID            **p;

   if (IsPfatFlashInitialized()){
       return;
   }

   if ( FlashInitialized == NULL)return;

   FlashInitialized->FlashVirtualFixup(pRS);

   for(p = (VOID**)FlashInitialized; p < (VOID**)(FlashInitialized + 1); p++)
   pRS->ConvertPointer(0, p);

   //Fixup FlashAPI pointer
   pRS->ConvertPointer(0, &FlashInitialized);
}

#else

BOOLEAN
IdentifyPfatFlash (
    IN  volatile UINT8 *pBlockAddress,
    OUT FLASH_PART     **FlashStruc
)
{
	return FALSE;
}

#endif

VOID
PreventAFUWhenPfatEnable (
    IN     UINT8        SwSmiNum,
    IN OUT UINT64       Buffer
)
{
    if (SwSmiNum != 0x25) return; 
    if ( ((ReadMsr (MSR_PLATFORM_INFO) & B_MSR_PLATFORM_INFO_PFAT_AVAIL) && 
         (ReadMsr (MSR_PLAT_FRMW_PROT_CTRL) & B_MSR_PLAT_FRMW_PROT_CTRL_EN)) ) {
          //
          // Update Implemented to TRUE, AFU will assume the SMIFlash is no support.
          //
          ((INFO_BLOCK*)Buffer)->Implemented = TRUE;
    }
}

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