]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/BdsDxe/BootMaint/BBSsupport.c
7ba4fa2ab84f0f1e84813bf7230be6abccd46b6f
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / BootMaint / BBSsupport.c
1 /** @file
2 This function deal with the legacy boot option, it create, delete
3 and manage the legacy boot option, all legacy boot option is getting from
4 the legacy BBS table.
5
6 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "BBSsupport.h"
12
13 #pragma pack(1)
14 typedef struct {
15 BBS_TABLE BbsEntry;
16 UINT16 BbsIndex;
17 } LEGACY_BOOT_OPTION_BBS_DATA;
18 #pragma pack()
19
20 /**
21 Re-order the Boot Option according to the DevOrder.
22
23 The routine re-orders the Boot Option in BootOption array according to
24 the order specified by DevOrder.
25
26 @param DevOrder Pointer to buffer containing the BBS Index,
27 high 8-bit value 0xFF indicating a disabled boot option
28 @param DevOrderCount Count of the BBS Index
29 @param EnBootOption Callee allocated buffer containing the enabled Boot Option Numbers
30 @param EnBootOptionCount Count of the enabled Boot Option Numbers
31 @param DisBootOption Callee allocated buffer containing the disabled Boot Option Numbers
32 @param DisBootOptionCount Count of the disabled Boot Option Numbers
33 **/
34 VOID
35 OrderLegacyBootOption4SameType (
36 UINT16 *DevOrder,
37 UINTN DevOrderCount,
38 UINT16 **EnBootOption,
39 UINTN *EnBootOptionCount,
40 UINT16 **DisBootOption,
41 UINTN *DisBootOptionCount
42 )
43 {
44 EFI_STATUS Status;
45 UINT16 *NewBootOption;
46 UINT16 *BootOrder;
47 UINTN BootOrderSize;
48 UINTN Index;
49 UINTN StartPosition;
50
51 BDS_COMMON_OPTION *BootOption;
52
53 CHAR16 OptionName[sizeof ("Boot####")];
54 UINT16 *BbsIndexArray;
55 UINT16 *DeviceTypeArray;
56 LIST_ENTRY List;
57
58 BootOrder = BdsLibGetVariableAndSize (
59 L"BootOrder",
60 &gEfiGlobalVariableGuid,
61 &BootOrderSize
62 );
63 ASSERT (BootOrder != NULL);
64
65 BbsIndexArray = AllocatePool (BootOrderSize);
66 DeviceTypeArray = AllocatePool (BootOrderSize);
67 *EnBootOption = AllocatePool (BootOrderSize);
68 *DisBootOption = AllocatePool (BootOrderSize);
69 *DisBootOptionCount = 0;
70 *EnBootOptionCount = 0;
71 Index = 0;
72
73 ASSERT (BbsIndexArray != NULL);
74 ASSERT (DeviceTypeArray != NULL);
75 ASSERT (*EnBootOption != NULL);
76 ASSERT (*DisBootOption != NULL);
77
78 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
79
80 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", BootOrder[Index]);
81 InitializeListHead (&List);
82 BootOption = BdsLibVariableToOption (&List, OptionName);
83 ASSERT (BootOption != NULL);
84
85 if ((DevicePathType (BootOption->DevicePath) == BBS_DEVICE_PATH) &&
86 (DevicePathSubType (BootOption->DevicePath) == BBS_BBS_DP)) {
87 //
88 // Legacy Boot Option
89 //
90 ASSERT (BootOption->LoadOptionsSize == sizeof (LEGACY_BOOT_OPTION_BBS_DATA));
91
92 DeviceTypeArray[Index] = ((BBS_BBS_DEVICE_PATH *) BootOption->DevicePath)->DeviceType;
93 BbsIndexArray [Index] = ((LEGACY_BOOT_OPTION_BBS_DATA *) BootOption->LoadOptions)->BbsIndex;
94 } else {
95 DeviceTypeArray[Index] = BBS_TYPE_UNKNOWN;
96 BbsIndexArray [Index] = 0xFFFF;
97 }
98 FreePool (BootOption->DevicePath);
99 FreePool (BootOption->Description);
100 FreePool (BootOption->LoadOptions);
101 FreePool (BootOption);
102 }
103
104 //
105 // Record the corresponding Boot Option Numbers according to the DevOrder
106 // Record the EnBootOption and DisBootOption according to the DevOrder
107 //
108 StartPosition = BootOrderSize / sizeof (UINT16);
109 NewBootOption = AllocatePool (DevOrderCount * sizeof (UINT16));
110 ASSERT (NewBootOption != NULL);
111 while (DevOrderCount-- != 0) {
112 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
113 if (BbsIndexArray[Index] == (DevOrder[DevOrderCount] & 0xFF)) {
114 StartPosition = MIN (StartPosition, Index);
115 NewBootOption[DevOrderCount] = BootOrder[Index];
116
117 if ((DevOrder[DevOrderCount] & 0xFF00) == 0xFF00) {
118 (*DisBootOption)[*DisBootOptionCount] = BootOrder[Index];
119 (*DisBootOptionCount)++;
120 } else {
121 (*EnBootOption)[*EnBootOptionCount] = BootOrder[Index];
122 (*EnBootOptionCount)++;
123 }
124 break;
125 }
126 }
127 }
128
129 //
130 // Overwrite the old BootOption
131 //
132 CopyMem (&BootOrder[StartPosition], NewBootOption, (*DisBootOptionCount + *EnBootOptionCount) * sizeof (UINT16));
133 Status = gRT->SetVariable (
134 L"BootOrder",
135 &gEfiGlobalVariableGuid,
136 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
137 BootOrderSize,
138 BootOrder
139 );
140 //
141 // Changing content without increasing its size with current variable implementation shouldn't fail.
142 //
143 ASSERT_EFI_ERROR (Status);
144
145 FreePool (NewBootOption);
146 FreePool (DeviceTypeArray);
147 FreePool (BbsIndexArray);
148 FreePool (BootOrder);
149 }
150
151 /**
152 Group the legacy boot options in the BootOption.
153
154 The routine assumes the boot options in the beginning that covers all the device
155 types are ordered properly and re-position the following boot options just after
156 the corresponding boot options with the same device type.
157 For example:
158 1. Input = [Harddisk1 CdRom2 Efi1 Harddisk0 CdRom0 CdRom1 Harddisk2 Efi0]
159 Assuming [Harddisk1 CdRom2 Efi1] is ordered properly
160 Output = [Harddisk1 Harddisk0 Harddisk2 CdRom2 CdRom0 CdRom1 Efi1 Efi0]
161
162 2. Input = [Efi1 Efi0 CdRom1 Harddisk0 Harddisk1 Harddisk2 CdRom0 CdRom2]
163 Assuming [Efi1 Efi0 CdRom1 Harddisk0] is ordered properly
164 Output = [Efi1 Efi0 CdRom1 CdRom0 CdRom2 Harddisk0 Harddisk1 Harddisk2]
165
166 **/
167 VOID
168 GroupMultipleLegacyBootOption4SameType (
169 VOID
170 )
171 {
172 EFI_STATUS Status;
173 UINTN Index;
174 UINTN DeviceIndex;
175 UINTN DeviceTypeIndex[7];
176 UINTN *NextIndex;
177 UINT16 OptionNumber;
178 UINT16 *BootOrder;
179 UINTN BootOrderSize;
180 CHAR16 OptionName[sizeof ("Boot####")];
181 BDS_COMMON_OPTION *BootOption;
182 LIST_ENTRY List;
183
184 SetMem (DeviceTypeIndex, sizeof (DeviceTypeIndex), 0xff);
185
186 BootOrder = BdsLibGetVariableAndSize (
187 L"BootOrder",
188 &gEfiGlobalVariableGuid,
189 &BootOrderSize
190 );
191
192 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
193 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", BootOrder[Index]);
194 InitializeListHead (&List);
195 BootOption = BdsLibVariableToOption (&List, OptionName);
196 ASSERT (BootOption != NULL);
197
198 if ((DevicePathType (BootOption->DevicePath) == BBS_DEVICE_PATH) &&
199 (DevicePathSubType (BootOption->DevicePath) == BBS_BBS_DP)) {
200 //
201 // Legacy Boot Option
202 //
203 ASSERT ((((BBS_BBS_DEVICE_PATH *) BootOption->DevicePath)->DeviceType & 0xF) < ARRAY_SIZE (DeviceTypeIndex));
204 NextIndex = &DeviceTypeIndex[((BBS_BBS_DEVICE_PATH *) BootOption->DevicePath)->DeviceType & 0xF];
205
206 if (*NextIndex == (UINTN) -1) {
207 //
208 // *NextIndex is the Index in BootOrder to put the next Option Number for the same type
209 //
210 *NextIndex = Index + 1;
211 } else {
212 //
213 // insert the current boot option before *NextIndex, causing [*Next .. Index] shift right one position
214 //
215 OptionNumber = BootOrder[Index];
216 CopyMem (&BootOrder[*NextIndex + 1], &BootOrder[*NextIndex], (Index - *NextIndex) * sizeof (UINT16));
217 BootOrder[*NextIndex] = OptionNumber;
218
219 //
220 // Update the DeviceTypeIndex array to reflect the right shift operation
221 //
222 for (DeviceIndex = 0; DeviceIndex < ARRAY_SIZE (DeviceTypeIndex); DeviceIndex++) {
223 if (DeviceTypeIndex[DeviceIndex] != (UINTN) -1 && DeviceTypeIndex[DeviceIndex] >= *NextIndex) {
224 DeviceTypeIndex[DeviceIndex]++;
225 }
226 }
227 }
228 }
229 FreePool (BootOption->DevicePath);
230 FreePool (BootOption->Description);
231 FreePool (BootOption->LoadOptions);
232 FreePool (BootOption);
233 }
234
235 Status = gRT->SetVariable (
236 L"BootOrder",
237 &gEfiGlobalVariableGuid,
238 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
239 BootOrderSize,
240 BootOrder
241 );
242 //
243 // Changing content without increasing its size with current variable implementation shouldn't fail.
244 //
245 ASSERT_EFI_ERROR (Status);
246 FreePool (BootOrder);
247 }
248