summaryrefslogtreecommitdiff
path: root/Core/EM/CmosManager/CmosManagerSmm.c
blob: e50438d22e0b692c76646c9b23f2be9d335aaa76 (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
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (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/CMOS Manager/CMOS SMM/CmosManagerSmm.c 8     1/25/12 2:38p Michaela $
//
// $Revision: 8 $
//
// $Date: 1/25/12 2:38p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/CMOS Manager/CMOS SMM/CmosManagerSmm.c $
// 
// 8     1/25/12 2:38p Michaela
// 
// 7     1/25/12 2:37p Michaela
// [TAG]         EIP56197
// [Category]    New Feature
// [Severity]    Normal
// [Description] CMOS Manager Support in Runtime Code
// [Files]       CmosManager.sdl
//               CmosSetup.uni
//               CmosMessages.uni
//               CmosManagerSmm.cif
//               CmosAccess.h
//               CmosManagerSmm.c
//               CmosAccess.asl
// 
// 6     12/11/11 8:56p Michaela
// 
// 4     11/30/11 11:48p Michaela
// [TAG]         EIP75889
// [Category]    Spec Update
// [Severity]    Normal
// [Description] Support Setup Customization
// [Files]       CmosManager.sdl
//               CmosSetup.sd
//               CmosSetup.uni
//               CmosMessages.uni
// 
// 3     3/16/11 2:45p Michaela
// 
// 2     3/05/10 4:54p Michaela
// 
// 1     11/10/09 8:45p Michaela
// 
//**********************************************************************

//<AMI_FHDR_START>
//---------------------------------------------------------------------------
//
// Name:    CmosManagerSmm.c
//
// Description: Contains the routines that initialize the CMOS manager's
//              SMM services.
//
//---------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "CmosManagerSmm.h"
#include <Protocol\Reset.h>
#include <Protocol\SmmBase.h>
#include <Protocol\SmmSwDispatch.h>
#include <Protocol\AcpiSupport.h>
#include <Acpi.h>
#include <AcpiRes.h>

EFI_CMOS_ACCESS_INTERFACE   *gCmos = NULL;
CMOS_SMM_CONTEXT            *gAslContext = NULL;  // ASL communication buffer

//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   SmiHandler
//
// Description:
//      This is called from SMM or ASL code to access CMOS during
//      runtime.
//
// Input:
//      IN EFI_HANDLE DispatchHandle
//      IN EFI_SMM_SW_DISPATCH_CONTEXT *DispatchContext
//
// Output:
//      NONE
//
// Modified:
//      NONE
//
// Referrals:
//
// Notes:
//      
//
//<AMI_PHDR_END>
//**********************************************************************

VOID SmiHandler (
    IN  EFI_HANDLE                    DispatchHandle,
    IN  EFI_SMM_SW_DISPATCH_CONTEXT   *DispatchContext )
{

    UINT8               Function = (UINT8)DispatchContext->SwSmiInputValue;
    EFI_STATUS          Status = EFI_NOT_FOUND;

    if (Function == CSMI){
        if (gCmos == NULL){
            gAslContext->Status = 0; // FALSE => operation failed
        }
        else {
            switch (gAslContext->Opcode) {
            case 0:  // Write from the communication buffer
                Status = gCmos->Write(gCmos, gAslContext->CmosToken, 
                    gAslContext->CmosByte);
                break;
            case 1:  // Read into the communication buffer
                Status = gCmos->Read(gCmos, gAslContext->CmosToken, 
                    &gAslContext->CmosByte);
                break;
            }
            if (EFI_ERROR(Status)){
               gAslContext->Status = 0; // FALSE => operation failed
            }
            else {
               gAslContext->Status = 1; // FALSE => operation success
            }
        }
    }
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure: InSmmFunction
//
// Description:
//      This function is executed from SMM in order to install the SMRAM-based
//      CMOS Access Interface pointer.
//
// Input:
//      IN EFI_HANDLE ImageHandle
//                  - Image handle
//      IN EFI_SYSTEM_TABLE *SystemTable
//                  - System table pointer
//
// Output:
//      EFI_STATUS (Return Value)
//                  = EFI_SUCCESS if successful
//                  = or other valid EFI error code
//
// Notes:
//               *  Initializes/Installs the EFI_CMOS_ACCESS_INTERFACE 
//                  Protocol for SMM.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS InSmmFunction (
    IN  EFI_HANDLE              ImageHandle,
    IN  EFI_SYSTEM_TABLE        *SystemTable )
{
    EFI_STATUS                      Status;
    EFI_CMOS_MANAGER_INTERFACE      *Mgr;
    EFI_SMM_SW_DISPATCH_CONTEXT     SwContext;
    EFI_SMM_SW_DISPATCH_PROTOCOL    *SwDispatch;
    EFI_HANDLE                      SwHandle  = NULL;
    
    CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, "CmosManagerSMM Entry\n" ));
 
    //-----------------------------------------------------------------------
    // Initialize the manager interface and, at this point, only check for  
    // interface initialization errors.
    //-----------------------------------------------------------------------

    Mgr = NewCmosManagerInterface(NULL);
    if ( Mgr == NULL || !Mgr->CheckStatus(Mgr, CMOS_VALID_INTERFACE) ) {
        return  Status = EFI_UNSUPPORTED;
    }
    if ( Mgr->CheckStatus( Mgr, CMOS_INTERFACE_ALREADY_INSTALLED) )
        return  Status = EFI_SUCCESS;

    //-----------------------------------------------------------------------
    // Handle specific reported errors.
    //
    // Note:    All boot time error conditions should have been handled
    //          before execution gets here.
    //
    //          All that is needed is to report the status for debugging.
    //-----------------------------------------------------------------------

    if (  Mgr->CheckStatus(Mgr, CMOS_BAD_BATTERY) ) {
        CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, 
            "CmosManagerSMM: CMOS_BAD_BATTERY = TRUE\n" ));

        // Default handler is to continue from DXE phase
    }
    else if ( Mgr->CheckAnyStatus(Mgr,   CMOS_BAD_CHECKSUM 
                                       | CMOS_OPTIMAL_DEFAULTS_ENABLED) )
    {
#if (FULL_CMOS_MANAGER_DEBUG)
        if (  Mgr->CheckStatus(Mgr, CMOS_BAD_CHECKSUM) ) {
            CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, 
                "CmosManagerSMM: CMOS_BAD_CHECKSUM = TRUE\n" ));
        }

        if (  Mgr->CheckStatus(Mgr, CMOS_OPTIMAL_DEFAULTS_ENABLED) ) {
            CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, 
                "CmosManagerSMM: CMOS_OPTIMAL_DEFAULTS_ENABLED = TRUE\n" ));
        }
#endif
    }


    //-----------------------------------------------------------------------
    // Install the CMOS Access interface
    //-----------------------------------------------------------------------

    CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, 
        "CmosManagerSMM: Installing CMOS Access SMM Protocol @ 0x%x\n",
        Mgr ));
    Mgr->InstallAccessInterface(Mgr);
    if ( !Mgr->CheckStatus(Mgr, CMOS_VALID_MANAGER) ){
        CMOS_TRACE((CMOS_TRACE_ALWAYS, 
            "CmosManagerSMM: Invalid interface\n" ));
        return Status = EFI_UNSUPPORTED;
    }

    // Update the module-scope interface pointer.
    gCmos = (EFI_CMOS_ACCESS_INTERFACE*) &Mgr->Access;

    //Register the SW SMI handler
    Status = pBS->LocateProtocol (&gEfiSmmSwDispatchProtocolGuid, 
        NULL, &SwDispatch);
    ASSERT_EFI_ERROR(Status);

    SwContext.SwSmiInputValue = CSMI;
    Status = SwDispatch->Register (SwDispatch, SmiHandler, &SwContext, 
        &SwHandle);

    if (EFI_ERROR(Status)){
        CMOS_TRACE((CMOS_TRACE_ALWAYS,
            "InSmmFunction: could not register...\n"));
    }
    ASSERT_EFI_ERROR(Status);

    CMOS_TRACE((CMOS_TRACE_ALWAYS, "InSmmFunction Registered.\n"));

    return Status = EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure: GetAslNameObject
//
// Description:
//      This function returns the 64-bit value assigned to the ASL named
//      object.
//
// Input:
//      IN ACPI_HDR *PDsdt 
//                  - pointer to the DSDT table
//      IN UINT8 *ObjName
//                  - pointer to the name string
//      OUT UINT64 *Value
//                  - pointer to the value to be updated
//
// Output:
//      EFI_STATUS (Return Value)
//                  = EFI_SUCCESS if successful
//                  = or other valid EFI error code
//
// Notes:
//      N/A
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS  GetAslNameObject(
    IN  ACPI_HDR  *PDsdt,
    IN  UINT8     *ObjName,
    OUT UINT64    *Value)
{
    ASL_OBJ_INFO    Obj;
    EFI_STATUS      Status;

    if (PDsdt->Signature != DSDT_SIG){
        return EFI_INVALID_PARAMETER;
    }

    Status = GetAslObj((UINT8*)(PDsdt+1), PDsdt->Length-sizeof(ACPI_HDR)-1,
        ObjName, otName, &Obj);
    
    if (!EFI_ERROR(Status)){
        *Value = *((UINT64*)((UINT8*)Obj.DataStart + 1));
    }
    
    return Status;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure: UpdateAslCommunicationBuffer
//
// Description:
//      This function is executed in non-SMM in order to allocate the 
//      runtime communication buffer used by the \CMOS.ACCE asl method
//      to use CMOS Manager services at runtime.
//
// Input:
//      N/A
//
// Output:
//      EFI_STATUS (Return Value)
//                  = EFI_SUCCESS if successful
//                  = or other valid EFI error code
//
// Notes:
//      N/A
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS UpdateAslCommunicationBuffer ( VOID )
{
    EFI_PHYSICAL_ADDRESS  DsdtAddress = NULL;
    ACPI_HDR              *Dsdt = NULL;
    UINT64                CurrentValue;
    EFI_STATUS            Status;

    // Get a pointer to the DSDT.

    Status = LibGetDsdt(&DsdtAddress, EFI_ACPI_TABLE_VERSION_ALL);
    if (EFI_ERROR(Status)){
        CMOS_TRACE((CMOS_TRACE_ALWAYS, "CMOS Manager: failed to get DSDT\n"));
        goto EXIT;
    }
    Dsdt = (ACPI_HDR*)DsdtAddress;

    // Get the current value of \CCOM.

    Status = GetAslNameObject(Dsdt, "CCOM", &CurrentValue);
    if (EFI_ERROR(Status)){
        CMOS_TRACE((CMOS_TRACE_ALWAYS, "Could not get \\CCOM ASL object.\n"));
        goto EXIT;
    }
    CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, "CurrentValue of \\CCOM is 0x%lX\n",
        CurrentValue));

    // If \CCOM has not been updated yet, allocate the runtime communication
    // buffer and update the ASL object.  Otherwise, save the value of 
    // \CCOM to the global variable in SMRAM scope.

    if (CurrentValue == 0x0123456789ABCDEF){
        Status = pBS->AllocatePool(EfiRuntimeServicesData, 
            sizeof(CMOS_SMM_CONTEXT), &gAslContext);
        if (EFI_ERROR(Status)){
            CMOS_TRACE((CMOS_TRACE_ALWAYS, 
                "failed to allocate CMOS_SMM_CONTEXT\n"));
            goto EXIT;
        }
        CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, 
            "CMOS_SMM_CONTEXT allocated to 0x%lX\n", gAslContext ));

        Status = UpdateAslNameObject(Dsdt, "CCOM", (UINT64)gAslContext);
        if (EFI_ERROR(Status)){
            CMOS_TRACE((CMOS_TRACE_ALWAYS, "failed to update \\CCOM\n"));
            goto EXIT;
        }
        CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, "Updated \\CCOM ASL object.\n"));
    }
    else {
        gAslContext = (CMOS_SMM_CONTEXT*)CurrentValue;
        CMOS_TRACE_FULL((CMOS_TRACE_ALWAYS, "gAslContext was updated\n"));
    }

EXIT:
    return Status;
}


//---------------------------------------------------------------------------
//  SMM entry point function
//
//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure: CmosManagerSmmEntry
//
// Description:
//      This function is the entrypoint for installing the SMM driver for
//      CMOS Manager.
//
// Input:
//      IN EFI_HANDLE ImageHandle
//                  - Image handle
//      IN EFI_SYSTEM_TABLE *SystemTable
//                  - System table pointer
//
// Output:
//      EFI_STATUS (Return Value)
//                  = EFI_SUCCESS if successful
//                  = or other valid EFI error code
//
// Notes:
//               *  Initializes/Installs the EFI_CMOS_ACCESS_INTERFACE 
//                  Protocol for SMM.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS CmosManagerSmmEntry (
    IN  EFI_HANDLE              ImageHandle,
    IN  EFI_SYSTEM_TABLE        *SystemTable )
{
    EFI_STATUS               Status = EFI_SUCCESS;

    InitAmiLib(ImageHandle, SystemTable);

#if CMOS_MANAGER_ASL_SUPPORT
    Status = UpdateAslCommunicationBuffer();
    if (EFI_ERROR(Status)){
        CMOS_TRACE((CMOS_TRACE_ALWAYS,
            "CMOS Manager failed to update ASL communication buffer\n"));
        ASSERT_EFI_ERROR(Status);
    }
#endif

    return InitSmmHandler(ImageHandle, SystemTable, InSmmFunction, NULL);

}


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