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

//**********************************************************************
// $Header: /Alaska/BIN/Modules/BootOptionPolicies/TseDefaultBootOrder/TseDefaultBootOrder.c 6     1/03/13 10:51a Vyacheslava $
//
// $Revision: 6 $
//
// $Date: 1/03/13 10:51a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Modules/BootOptionPolicies/TseDefaultBootOrder/TseDefaultBootOrder.c $
// 
// 6     1/03/13 10:51a Vyacheslava
// Added modification to check for LOAD_OPTION_HIDDEN and ignore hidden
// boot options in creating "DefaultBootOrder" variable. 
// 
// 5     10/15/12 10:31a Vyacheslava
// [TAG]  		EIP103603 
// [Category]  	Bug Fix
// [Severity]  	Critical
// [Symptom]  	DefaultLegacyDevOrder is corrupted
// [RootCause]  	SetDefaultTseBootOrder () corrupts the data which is not
// the Boot Order.
// [Solution]  	for( i = 0; i < (Order->Length / (sizeof (UINT16)) - 1);
// i++ )
// [Files]  		TseDefaultBootOrder.c
// 
// 4     9/13/11 9:09a Vyacheslava
// 
// 3     4/27/11 11:26a Vyacheslava
// [TAG]  		EIP58592
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Restore Defaults in the Setup will work incorrect after
// second HDD has been disabled.
// 
// 2     2/02/11 2:00p Vyacheslava
// [TAG]  		EIP52313
// [Category]  	New Feature
// 
// 1     1/20/11 10:52a Felixp
// 
// 6     1/13/10 2:13p Felixp
// 
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:  TseDefaultBootOrder.c
//
// Description: This is main file of the TseDefaultBootOrder eModule.
//
//<AMI_FHDR_END>
//**********************************************************************

#include <BootOptions.h>
#include "TseDefaultBootOrder.h"

//----------------------------------------------------------------------------


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   CompareTagThenBbsIndex
//
// Description: This function returns result of compairing two DLINK tags.
//
// Input:       
//  Link1 - pointer to the first tag for compare
//  Link1 - pointer to the second tag for compare
//
// Output:  INT32 - result of comparing. 
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

INT32 CompareTagThenBbsIndex( IN DLINK *Link1, IN DLINK *Link2 )
{
    BOOT_OPTION *Option1 = (BOOT_OPTION*)Link1;
    BOOT_OPTION *Option2 = (BOOT_OPTION*)Link2;
    
    // Compare Tags first
    if (Option1->Tag < Option2->Tag) 
        return -1;
    else if (Option1->Tag > Option2->Tag) 
        return 1;

    // Compare by BBS index
    if (Option1->BbsIndex < Option2->BbsIndex) 
        return -1;
    else if (Option1->BbsIndex > Option2->BbsIndex) 
        return 1;

    return 0;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SetDefaultTseBootOrder
//
// Description: 
//  This function sets variables: DefaultLegacyDevOrder and DefaultBootOrder.
//
// Input:       VOID
//
// Output:      VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID SetDefaultTseBootOrder(VOID)
{
    UINTN DevOrderSize;
    LEGACY_DEVICE_ORDER *DevOrder;
    UINTN BootOrderSize;
    UINT16 *BootOrder;
    UINT16 PreviousBootOptionNumber;
    DLINK *Link;
    UINTN BootOrderIndex;
    BOOT_OPTION *Option;
    EFI_GUID DefaultLegacyDevOrderGuid = DEFAULT_LEGACY_DEV_ORDER_VARIABLE_GUID;
    EFI_GUID DefaultBootOrderGuid = DEFAULT_BOOT_ORDER_VARIABLE_GUID;
    LEGACY_DEVICE_ORDER *Order;
    UINTN  i;

    // PRECONDITION: All Boot Option Numbers are set
    BootOrderSize = BootOptionList->Size * sizeof(UINT16);
    if (BootOrderSize == 0) 
        return;

    // Build DefaultLegacyDevOrder variable
    SortList(BootOptionList, CompareTagThenBbsIndex);
    BuildLegacyDevOrderBuffer(&DevOrder, &DevOrderSize);

    // Remove disable mask from buffer DevOrder
    for( Order = DevOrder; (UINT8*)Order < (UINT8*)DevOrder + DevOrderSize; 
         Order = (LEGACY_DEVICE_ORDER*)((UINT8*)Order + Order->Length + sizeof(Order->Type)) ) 
    {
        // The order length includes their own length
        for( i = 0; i < (Order->Length / sizeof(UINT16) - 1); i++ )
            Order->Device[i] &= 0x00FF;
    }

    // Store "DefaultLegacyDevOrder" EFI variable
    pRS->SetVariable(
        L"DefaultLegacyDevOrder", 
        &DefaultLegacyDevOrderGuid,
        BOOT_VARIABLE_ATTRIBUTES,
        DevOrderSize,
        DevOrder
    );
    pBS->FreePool(DevOrder);

    // Build DefaultBootOrder variable
    BootOrder = Malloc(BootOrderSize);
    PreviousBootOptionNumber = INVALID_BOOT_OPTION_NUMBER;
    BootOrderIndex = 0;

    // Set boot option number for each boot option
    FOR_EACH_BOOT_OPTION(BootOptionList,Link,Option)
    {
        if ( (PreviousBootOptionNumber == Option->BootOptionNumber) || 
             (Option->Attributes & LOAD_OPTION_HIDDEN) )
            continue;
        BootOrder[BootOrderIndex++] = Option->BootOptionNumber;
        PreviousBootOptionNumber = Option->BootOptionNumber;
    }
    
    // Store "DefaultBootOrder" EFI variable
    pRS->SetVariable(
        L"DefaultBootOrder", 
        &DefaultBootOrderGuid,
        BOOT_VARIABLE_ATTRIBUTES, 
        BootOrderIndex * sizeof(UINT16), 
        BootOrder
    );
    pBS->FreePool(BootOrder);
}


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