summaryrefslogtreecommitdiff
path: root/Core/EM/CsmOptOut/CsmOptOutSetup.c
blob: f61e77bde88f9ec99d1137b47da4f80dbe7a4960 (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2012, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

///**********************************************************************
// $Header: /Alaska/SOURCE/Modules/CsmOptOut/CsmOptOutSetup.c 10    7/08/13 4:17p Artems $
//
// $Revision: 10 $
//
// $Date: 7/08/13 4:17p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/CsmOptOut/CsmOptOutSetup.c $
// 
// 10    7/08/13 4:17p Artems
// [TAG]  		EIP N/A
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	Build error
// [RootCause]  	Unreferenced local variable
// [Solution]  	Removed variable
// [Files]  		CsmOptOutSetup.c
// 
// 9     7/02/13 5:06p Artems
// [TAG]  		EIP125900
// [Category]  	Improvement
// [Description]  	Funciton IsUefiGop assumed, that GOP protocol installed
// on the same handle
// where PciIO is installed, which is not always the case.
// Updated to handle different cases
// [Files]  		CsmOptOutSetup.c
// 
// 8     2/26/13 1:47p Artems
// [TAG]  		EIP111710
// [Category]  	Improvement
// [Description]  	Added multilanguage support for error message box
// [Files]  		CsmOptOutSetup.c
// 
// 7     1/22/13 4:29p Artems
// [TAG]  		EIP111710
// [Category]  	Improvement
// [Description]  	Replace MessageBox message and title with string tokens
// to allow
// different language usage
// [Files]  		CsmOptOut.mak CsmOptOut.uni CsmOptOutSetup.c
// 
// 6     9/27/12 7:14p Artems
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Secureboot enable callbacks moved to Secure Boot module
// [Files]  		CsmOptOut.sdl CsmOptOutSetup.c
// 
// 5     9/11/12 5:26p Artems
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Changed dependency on secureboot pkg module revision
// [Files]  		CsmOptOutSetup.c
// 
// 4     9/04/12 3:15p Artems
// [TAG]  		EIP98995
// [Category]  	Improvement
// [Description]  	Use EFI_UNSUPPORTED status in Browser callback to
// discard control value instead of FORM_DISCARD
// [Files]  		CsmOptOutSetup.c
// 
// 3     8/29/12 2:26p Artems
// [TAG]  		EIP98995
// [Category]  	Improvement
// [Description]  	When enable secure boot disable CSM but preserve CSM
// optout options setting,
// so once secure boot is disabled they can be restored
// [Files]  		CsmOptOut.sd, CsmOptOut.sdl, CsmOptOutSetup.c
// CsmOptOutRuntime.c
// 
// 2     6/20/12 6:10p Artems
// [TAG]  		EIP83692
// [Category]  	Improvement
// [Description]  	CSM OptOut setup page grayed out if secure boot is
// enabled
// CSM OptOut PlatformOverride protocol modified to fix SCT bugs
// Removed support for Launch CSM "Auto" option
// 
// [Files]  		CsmOptOut.c CsmOptOut.mak CsmOptOut.sd CsmOptOut.sdl
// CsmOptOut.uni CsmOptOutRuntime.c CsmOptOutSetup.c
// 
// 1     5/21/12 4:34p Artems
// [TAG]  		EIP N/A
// [Category]  	New Feature
// [Description]  	Prevent user from disable CSM in setup if active video
// is legacy
// [Files]  		CsmOptOutSetup.c CsmOptOut.sdl CsmOptOut.mak 
// CsmOptOut.sdl
// 
// 
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:  CsmOptOutSetup.c
//
// Description: Implementation of interactive callback to "Launch CSM" setup control
//
//<AMI_FHDR_END>
//**********************************************************************

#include <AmiDxeLib.h>
#include <Setup.h>
#include <SetupStrTokens.h>
#include <AmiLoadCsmPolicy.h>
#include <Protocol/GraphicsOutput.h>
#include <Protocol/DevicePath.h>
#include <Protocol/PciIo.h>
#include <Protocol/AmiPostMgr.h>
#include <Protocol/HiiString.h>

static EFI_GUID EfiVariableGuid = EFI_GLOBAL_VARIABLE;
static EFI_GUID AmiPostManagerProtocolGuid = AMI_POST_MANAGER_PROTOCOL_GUID;
static AMI_POST_MANAGER_PROTOCOL* AmiPostMgr = NULL;

extern const char *DefaultLanguageCode;


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:    IsUefiGop
// 
// Description:  This function checks wheter passed controller is managed by  UEFI GOP driver
//               
//  Input:
// 	IN EFI_HANDLE pci_hnd - handle of the controller
//
//  Output:
//  0 - controller managed by legacy driver
//  1 - controller managed by UEFI driver
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsUefiGop(
    IN EFI_HANDLE pci_hnd
)
{
    EFI_STATUS Status;
    EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *ent;
    UINTN n_ent;
    UINTN i;
    VOID *p;
    static EFI_GUID AmiCsmThunkProtocolGuid = AMI_CSM_THUNK_PROTOCOL_GUID;
    BOOLEAN Result = TRUE;

    Status = pBS->OpenProtocolInformation(pci_hnd, &gEfiPciIoProtocolGuid, &ent, &n_ent);
    if(EFI_ERROR(Status))
        return FALSE;

    for(i = 0; i < n_ent; i++) {
        if(!(ent[i].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER))
            continue;

        Status = pBS->HandleProtocol(ent[i].AgentHandle, &AmiCsmThunkProtocolGuid, &p);
        if(!EFI_ERROR(Status)) {
            Result = FALSE;
            break;
        }
    }
    pBS->FreePool(ent);

    return Result;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:    IsLegacyVideo
// 
// Description:  This function checks if active video is Legacy or UEFI
//               
//  Input:
// 	None
//
//  Output:
//  0 - active video is UEFI
//  1 - active vide is Legacy
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsLegacyVideo(
    VOID
)
{
    EFI_STATUS Status;
    EFI_HANDLE *hnd;
    EFI_HANDLE pci_hnd;
    UINTN n_hnd;
    UINTN i;
    EFI_DEVICE_PATH_PROTOCOL *dp;

    Status = pBS->LocateHandleBuffer(ByProtocol, &gEfiGraphicsOutputProtocolGuid, NULL, &n_hnd, &hnd);
    if(EFI_ERROR(Status))
        return FALSE;

    for(i = 0; i < n_hnd; i++) {
        Status = pBS->HandleProtocol(hnd[i], &gEfiDevicePathProtocolGuid, &dp);
        if(EFI_ERROR(Status))
            continue;
        Status = pBS->LocateDevicePath(&gEfiPciIoProtocolGuid, &dp, &pci_hnd);
        if(EFI_ERROR(Status))
            continue;

        if(IsUefiGop(pci_hnd))
            return FALSE;
    }
    pBS->FreePool(hnd);
    return TRUE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetCurrentLanguage
//
// Description: This function retrieves current platform language and stores it in
//              passed pointer
//
// Input:       CHAR8 ** Lang - pointer to where to store current language
//              BOOLEAN *Allocated - flag if memory was allocated for language
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID GetCurrentLanguage(
    CHAR8 ** Language,
    BOOLEAN *Allocated
)
{
    EFI_STATUS Status;
    UINTN Size = 0;

    *Allocated = FALSE;

    Status = pRS->GetVariable(L"PlatformLang", &EfiVariableGuid, NULL, &Size, *Language);
    if(Status == EFI_BUFFER_TOO_SMALL) {
        Status = pBS->AllocatePool(EfiBootServicesData, Size, Language);
        if(!EFI_ERROR(Status)) {
            Status = pRS->GetVariable(L"PlatformLang", &EfiVariableGuid, NULL, &Size, *Language);
            *Allocated = TRUE;
        }
    }
    if(EFI_ERROR(Status))
        *Language = (CHAR8 *)DefaultLanguageCode;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetLangString
//
// Description: This function reads a string from HII database with different languages
//
// Input:       IN EFI_HII_HANDLE   HiiHandle - Efi Hii Handle
//              IN STRING_REF       Token     - String Token
//              IN OUT UINTN        *StringSize - Length of the StringBuffer in bytes
//              OUT EFI_STRING      *StringBuffer - The pointer to the buffer to receive 
//                                  the characters in the string.
//
// Output:      EFI_STATUS - Depending on result
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GetLangString(
    IN EFI_HII_HANDLE HiiHandle,
    IN EFI_STRING_ID Token,
    IN OUT UINTN *StringSize, 
    OUT EFI_STRING StringBuffer
)
{
    EFI_STATUS Status;
    EFI_HII_STRING_PROTOCOL *HiiString;
    CHAR8 *Language;
    BOOLEAN Allocated;
    
    Status = pBS->LocateProtocol(&gEfiHiiStringProtocolGuid, NULL, &HiiString);
    if(EFI_ERROR(Status))
        return Status;

    GetCurrentLanguage(&Language, &Allocated);
    
    Status = HiiString->GetString(HiiString, Language, HiiHandle, Token, StringBuffer, StringSize, NULL);
    if(Status == EFI_INVALID_LANGUAGE) {
        if(Allocated)
            Status = HiiString->GetString(HiiString, (CHAR8 *)DefaultLanguageCode, HiiHandle, Token, StringBuffer, StringSize, NULL);
    }

    if(Status == EFI_INVALID_LANGUAGE)
        Status = HiiString->GetString(HiiString, LANGUAGE_CODE_ENGLISH, HiiHandle, Token, StringBuffer, StringSize, NULL);

    if(Allocated)
        pBS->FreePool(Language);

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetUniString
//
// Description: This function reads a string from HII database
//
// Input:       IN EFI_HII_HANDLE   HiiHandle - Efi Hii Handle
//              IN STRING_REF       Token     - String Token
//              IN OUT UINTN        *StringSize - Length of the StringBuffer in bytes
//              OUT EFI_STRING      *StringBuffer - The pointer to the buffer to receive 
//                                  the characters in the string.
//
// Output:      EFI_STATUS - Depending on result
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GetUniString(
    IN EFI_HII_HANDLE HiiHandle,
    IN EFI_STRING_ID Token,
    IN OUT UINTN *StringSize, 
    OUT EFI_STRING *StringBuffer
)
{
    EFI_STATUS Status;
    
    if (*StringBuffer == NULL) 
        *StringSize = 0;      //reset size if buffer is NULL


    Status = GetLangString(HiiHandle, Token, StringSize, *StringBuffer);
    if(Status == EFI_BUFFER_TOO_SMALL) {
         Status = pBS->AllocatePool(EfiBootServicesData, *StringSize, StringBuffer);
         if(!EFI_ERROR(Status))
            Status = GetLangString(HiiHandle, Token, StringSize, *StringBuffer);
    }

    if(EFI_ERROR(Status))
        *StringBuffer = NULL;

    return Status;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:    CsmOptOutLaunchCallback
//
// Description:  This function checks if CSM can be disabled in current boot
//
//  Input:
// 	IN EFI_HII_HANDLE HiiHandle - HII handle of formset
//  IN UINT16 Class - class of formset
//  IN UINT16 SubClass - subclass of formset
//  IN UINT16 Key - Id of setup control
//
//  Output:
//  EF_SUCCESS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CsmOptOutLaunchCallback(
    IN EFI_HII_HANDLE HiiHandle,
    IN UINT16 Class,
    IN UINT16 SubClass,
    IN UINT16 Key
)
{
    EFI_STATUS Status;
    UINT8 MsgKey;
    EFI_BROWSER_ACTION_REQUEST *rq;
    EFI_STRING Title = NULL;
    EFI_STRING Message = NULL;
    UINTN Size;


    CALLBACK_PARAMETERS *Callback = NULL;

    Callback = GetCallbackParameters();
    if(!Callback || Callback->Action != EFI_BROWSER_ACTION_CHANGING)
        return EFI_UNSUPPORTED;

    rq = Callback->ActionRequest;
    *rq = EFI_BROWSER_ACTION_REQUEST_NONE;

    if(Callback->Value->u8 == 0) {
    /* trying to disable CSM */
        if(IsLegacyVideo()) {
            if(AmiPostMgr == NULL)
                Status = pBS->LocateProtocol(&AmiPostManagerProtocolGuid, NULL, &AmiPostMgr);
                if(EFI_ERROR(Status))
                    return Status;
            GetUniString(HiiHandle, STRING_TOKEN(STR_LEGACY_VIDEO_MSGBOX_TITLE), &Size, &Title);
            GetUniString(HiiHandle, STRING_TOKEN(STR_LEGACY_VIDEO_MSGBOX_MESSAGE), &Size, &Message);
            
            Status = AmiPostMgr->DisplayMsgBox((Title == NULL)? L" Warning " : Title,
                                               (Message == NULL)? L"Video is in Legacy mode. Select Video policy UEFI only, reboot and try again" : Message,
                                               MSGBOX_TYPE_OK,
                                               &MsgKey);
            return EFI_UNSUPPORTED;
        }
    }
    return EFI_SUCCESS;
}



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