summaryrefslogtreecommitdiff
path: root/Core/EM/OFBD/MEUD/MEAU/AutoUpdate.c
blob: 0c16ea66eb66ccd15f579f3502b9d67f78a8c0d7 (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (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/OFBD Intel ME Update/AutoUpdate.c 3     10/04/11 5:08a Klzhan $
//
// $Revision: 3 $
//
// $Date: 10/04/11 5:08a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/OFBD Intel ME Update/AutoUpdate.c $
// 
// 3     10/04/11 5:08a Klzhan
// Support update OPR region with auto update.
// 
// 2     9/09/11 7:25a Klzhan
// Fix system hang issue.
// 
// 1     8/30/11 8:17a Klzhan
// 
//
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:    AutoUpdate.c
//
// Description:
//
//<AMI_FHDR_END>
//**********************************************************************
#include <AmiDxeLib.h>
#include <Protocol/ConsoleControl.h>
#include <Protocol/LoadedImage.h>
#include <Flash.h>
#include "AutoUpdate.h"
#include <Protocol/SimpleTextOut.h>
#include <Core\EM\OFBD\MEUD\MEUD.h>

UINT32 FlashCapacity;

EFI_GUID guidConsoleControl = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
EFI_GUID guidLoadedImage = EFI_LOADED_IMAGE_PROTOCOL_GUID;

//defined in Tokens.c
extern const UINT32 FlashBlockSize;

//defined in CSP_MEUD module
extern EFI_STATUS GET_FW_VERSION(
    UINT16 *MeFwVersionData
);
extern UINT32 GetHFS(VOID);

extern UINT32 GetFlashCapacity(VOID);

extern EFI_STATUS
GetRegionOffset(
    UINT8    Region,
    UINT32*  Offset,
    UINT32*  Length
);

extern OFBD_TC_55_ME_PROCESS_STRUCT  *StructPtr;

extern EFI_STATUS UpdateRegions(
    UINT8*    Buffer,
    BOOLEAN   InSmm
);

#define BDS_ALL_DRIVERS_CONNECTED_PROTOCOL_GUID \
 { 0xdbc9fd21, 0xfad8, 0x45b0, 0x9e, 0x78, 0x27, 0x15, 0x88, 0x67, 0xcc, 0x93 }
EFI_GUID    gBdsAllDriversConnectedProtocolGuid = \
                                    BDS_ALL_DRIVERS_CONNECTED_PROTOCOL_GUID;
EFI_GUID    gMEAUVariableGuid = MEAU_VAR_GUID;
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	NeedtoUpdate
//
// Description:	Check ME version.
//
// Input:
//      VOID
//
// Output:
//      TRUE - Update ME in this BOOT.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
NeedtoUpdate(
    VOID
)
{
    ME_FW_VERSION   Image_MEversion = IMAGE_ME_FW_VERSION, CurrentMEVersion;
    EFI_STATUS      Status;

    if(REFLASH_UPDATE_FORCE)
    {
        UINTN    SetupVariableSize = 1;
        UINT8    Data8;
        Status = pRS->GetVariable (
                             L"MEAU",
                             &gMEAUVariableGuid,
                             NULL,
                             &SetupVariableSize,
                             &Data8);

        // Flag Not Found, just return TRUE
        if(Status == EFI_NOT_FOUND)
            return TRUE;
    }
    Status = GET_FW_VERSION((UINT16*)&CurrentMEVersion);

    if(EFI_ERROR(Status))
        return FALSE;

    // Check version
#if ME_UPDATE_DOWNGRADE
    // Downgrade allow, update when the version is different.
    if(MemCmp(&Image_MEversion, &CurrentMEVersion, sizeof(ME_FW_VERSION)) == 0)
        return FALSE;
    else
        return TRUE;
#else
    // Because parameters in ME verions are words
    // so check version in 2 parts
    if(ME_VERSION_VALUE(CurrentMEVersion) < ME_VERSION_VALUE(Image_MEversion))
        return TRUE;

    if((ME_VERSION_VALUE(CurrentMEVersion) == ME_VERSION_VALUE(Image_MEversion))
 && (ME_VERSION_VALUE1(CurrentMEVersion) < ME_VERSION_VALUE1(Image_MEversion)))
        return TRUE;

#endif

    return FALSE;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	GetRawImage
//
// Description:	Find ME binary in FV_MAIN.
//
// Input:
//      NameGuid - Guid of FFS need to find.
//      Buffer   - Input buffer
//      Size     - Size of Input buffer
//
// Output:
//      Status
//      Buffer   - FFS data
//      Size     - Size of Output buffer
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GetRawImage (
  IN      EFI_GUID       *NameGuid,
  IN OUT  VOID           **Buffer,
  IN OUT  UINTN          *Size
  )

{
  EFI_STATUS                    Status;
  UINTN                         HandleCount;
  UINTN                         Index;
  EFI_FIRMWARE_VOLUME_PROTOCOL  *Fv;
  EFI_HANDLE                    *HandleBuff;
  UINT32                        AuthenticationStatus;

  Status = pBS->LocateHandleBuffer (
                  ByProtocol,
                  &gEfiFirmwareVolumeProtocolGuid,
                  NULL,
                  &HandleCount,
                  &HandleBuff
                  );

  if (EFI_ERROR (Status) || HandleCount == 0) {
    return EFI_NOT_FOUND;
  }
  //
  // Find desired image in all Fvs
  //
  for (Index = 0; Index < HandleCount; Index++) {
    Status = pBS->HandleProtocol (
                    HandleBuff[Index],
                    &gEfiFirmwareVolumeProtocolGuid,
                    &Fv
                    );

    if (EFI_ERROR (Status)) {
      if (HandleBuff != NULL) {
        pBS->FreePool (HandleBuff);
      }

      return EFI_LOAD_ERROR;
    }
    //
    // Try a raw file
    //
    Status = Fv->ReadSection (
                  Fv,
                  NameGuid,
                  EFI_SECTION_RAW,
                  0,
                  Buffer,
                  Size,
                  &AuthenticationStatus
                  );

    if (!EFI_ERROR (Status)) {
      break;
    }
  }

  if (HandleBuff != NULL) {
    pBS->FreePool (HandleBuff);
  }

  if (Index >= HandleCount) {

    return EFI_NOT_FOUND;
  }

  return EFI_SUCCESS;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	ProgressMEUpdate
//
// Description:	Do ME update
//
// Input:
//      NameGuid - Guid of FFS need to find.
//      Buffer   - Input buffer
//      Size     - Size of Input buffer
//
// Output:
//      Status
//      Buffer   - FFS data
//      Size     - Size of Output buffer
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
ProgressMEUpdate(
    IN UINT8*  Buffer,
    IN UINTN   Size
)
{
    EFI_STATUS Status;
    UINTN    SetupVariableSize;
    UINT8    Data8 = 0;
    UINT32   TempReg, i;
    OFBD_TC_55_ME_PROCESS_STRUCT  MEProcessStructPtr;
    CHAR8    String[256];
    CHAR8*   StringBuffer = String;
    CHAR16   wString[256];

    // Init Global variable
    MEProcessStructPtr.ddMessageBuffer = (UINT32)StringBuffer;
    StructPtr = &MEProcessStructPtr;

    FlashCapacity = GetFlashCapacity();
    if((Size) != FlashCapacity)
        return;

#ifdef CSP_MEUD_SUPPORT
#if CSP_MEUD_SUPPORT == 1
    // For Desktop and Mobile
    if (((GetHFS() >> 16) & 0x0F) == 0)
        IoWrite8(SW_SMI_IO_ADDRESS, Disable_ME_SW_SMI);
    // Comes here, System should be in Disabled mode.
    // If not, just return
    if (((GetHFS() >> 16) & 0x0F) != 5)
        return;
#endif
#endif

    // Get ME Region.
    if(REFLASH_UPDATE_ME)
    {
        // Prepare Buffer
        if(DIRECT_FW_UPDATE)
            StructPtr->bBlockType = ME_OPR_BLK;
        else
            StructPtr->bBlockType = ME_BLK;

        StructPtr->bHandleRequest = BIT02;

        pST->ConOut->OutputString (pST->ConOut,
                    L"ME is updated now ... \r\n");

        pST->ConOut->OutputString (pST->ConOut,
                    L"Please don't shut down or reset system \r\n");

        do{
            Status = UpdateRegions(Buffer, FALSE);
            // Request to continue
            if((StructPtr->UpdateResult & BIT01))
                StructPtr->bHandleRequest = BIT03;

            // Show string
            if((StructPtr->UpdateResult & BIT02))
            {
                StructPtr->bHandleRequest = BIT03;
                MemSet(wString, 512, 0);
                for(i = 0 ; String[i] ; (CHAR8)wString[i] = String[i], i++);
                wString[i] = 0;
                pST->ConOut->OutputString (pST->ConOut,wString);
                pST->ConOut->OutputString (pST->ConOut,L"\r\n");
            }
            // End
            if((StructPtr->UpdateResult & BIT03))
                break;
            // TODO : Error Handle
        }while(1);
    }

    if(REFLASH_UPDATE_GBE)
    {
        // Prepare Buffer
        StructPtr->bBlockType = GBE_BLK;
        StructPtr->bHandleRequest = BIT02;

        pST->ConOut->OutputString (pST->ConOut,
                    L"GBE region is updated now ... \r\n");

        Status = UpdateRegions(Buffer, FALSE);

        // Show string
        if((StructPtr->UpdateResult & BIT02))
        {
            StructPtr->bHandleRequest = BIT03;
            MemSet(wString, 512, 0);
            for(i = 0 ; String[i] ; (CHAR8)wString[i] = String[i], i++);
            wString[i] = 0;
            pST->ConOut->OutputString (pST->ConOut,wString);
            pST->ConOut->OutputString (pST->ConOut,L"\r\n");
        }
    }

    if(REFLASH_UPDATE_MEDS)
    {
        // Prepare Buffer
        StructPtr->bBlockType = GBE_BLK;
        StructPtr->bHandleRequest = BIT02;

        pST->ConOut->OutputString (pST->ConOut,
                    L"GBE region is updated now ... \r\n");

        Status = UpdateRegions(Buffer, FALSE);

        // Show string
        if((StructPtr->UpdateResult & BIT02))
        {
            StructPtr->bHandleRequest = BIT03;
            MemSet(wString, 512, 0);
            for(i = 0 ; String[i] ; (CHAR8)wString[i] = String[i], i++);
            wString[i] = 0;
            pST->ConOut->OutputString (pST->ConOut,wString);
            pST->ConOut->OutputString (pST->ConOut,L"\r\n");
        }
    }

    // Update Finish, update Flag
    if(REFLASH_UPDATE_FORCE)
    {
        SetupVariableSize = 1;
        Status = pRS->SetVariable (
                             L"MEAU",
                             &gMEAUVariableGuid,
                             EFI_VARIABLE_NON_VOLATILE
                             | EFI_VARIABLE_BOOTSERVICE_ACCESS
                             | EFI_VARIABLE_RUNTIME_ACCESS,
                             SetupVariableSize,
                             &Data8);
    }
    // Reset System and ME
    IoWrite32(0xCF8, 0x8000F8AC);
    TempReg = IoRead32(0xCFC);

    if(!(TempReg & BIT20))
    {
        IoWrite32(0xCF8, 0x8000F8AC);
        IoWrite32(0xCFC, TempReg | BIT20);
    }

    pRS->ResetSystem( EfiResetCold, EFI_SUCCESS, 0, NULL );
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	MEAutoUpdate
//
// Description:	System updaete ME in POST.
//
// Input:
//      VOID
//
// Output:
//      VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
MEAutoUpdate(
    VOID
)
{
    UINTN                     Size;
    EFI_PHYSICAL_ADDRESS      ImageLocation;
    UINT8*                    ImageBuffer;
    EFI_STATUS                Status;
    EFI_GUID                  gMEUDfileguid = MEUD_FILE_GUID;

    if(!NeedtoUpdate())
        return EFI_SUCCESS;


    Size = ME_UPDATE_BINARY_SIZE * 0x100000 / 0x1000;
    Status = pBS->AllocatePages (
              AllocateAnyPages,
              EfiBootServicesData, 
              Size,
              &ImageLocation );

    ImageBuffer = (UINT8*)ImageLocation;

    if(EFI_ERROR(Status))
      return Status;

    Size = Size * 0x1000;
    Status = GetRawImage (&gMEUDfileguid, &ImageBuffer, &Size);

    ProgressMEUpdate(ImageBuffer, Size);

    pBS->FreePages(ImageLocation, Size );

    return EFI_SUCCESS;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   AllDriverConnectEvent
//
// Description: This routine for reset the TPM Establishment flag.
//
// Input:       EFI_EVENT   - Efi event.
//              VOID*       - Image handle.
//
// Output:      None.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
MEAUAllDriverConnectEvent  (
    IN EFI_EVENT Event,
    IN VOID      *Context
)
{
    MEAutoUpdate();
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ReFlashEntry
//
// Description: This is the standard EFI driver entry point called for
//              Recovery flash module initlaization
// Input:       IN EFI_HANDLE ImageHandle - ImageHandle of the loaded driver
//              IN EFI_SYSTEM_TABLE SystemTable - Pointer to the System Table
//
// Output:      EFI_SUCCESS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS AUTOUPDEntry (IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
{
    EFI_STATUS   Status;
    EFI_EVENT    AllDriverConnectEvent;
    VOID         *Registration;


    InitAmiLib(ImageHandle,SystemTable);

    //---Set up Callback----------------
    Status = RegisterProtocolCallback ( &gBdsAllDriversConnectedProtocolGuid, \
                                        MEAUAllDriverConnectEvent, \
                                        NULL, \
                                        &AllDriverConnectEvent, \
                                        &Registration   );

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