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
6 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "BBSsupport.h"
17 } LEGACY_BOOT_OPTION_BBS_DATA
;
21 Re-order the Boot Option according to the DevOrder.
23 The routine re-orders the Boot Option in BootOption array according to
24 the order specified by DevOrder.
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
35 OrderLegacyBootOption4SameType (
38 UINT16
**EnBootOption
,
39 UINTN
*EnBootOptionCount
,
40 UINT16
**DisBootOption
,
41 UINTN
*DisBootOptionCount
45 UINT16
*NewBootOption
;
51 BDS_COMMON_OPTION
*BootOption
;
53 CHAR16 OptionName
[sizeof ("Boot####")];
54 UINT16
*BbsIndexArray
;
55 UINT16
*DeviceTypeArray
;
58 BootOrder
= BdsLibGetVariableAndSize (
60 &gEfiGlobalVariableGuid
,
63 ASSERT (BootOrder
!= NULL
);
65 BbsIndexArray
= AllocatePool (BootOrderSize
);
66 DeviceTypeArray
= AllocatePool (BootOrderSize
);
67 *EnBootOption
= AllocatePool (BootOrderSize
);
68 *DisBootOption
= AllocatePool (BootOrderSize
);
69 *DisBootOptionCount
= 0;
70 *EnBootOptionCount
= 0;
73 ASSERT (BbsIndexArray
!= NULL
);
74 ASSERT (DeviceTypeArray
!= NULL
);
75 ASSERT (*EnBootOption
!= NULL
);
76 ASSERT (*DisBootOption
!= NULL
);
78 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
80 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", BootOrder
[Index
]);
81 InitializeListHead (&List
);
82 BootOption
= BdsLibVariableToOption (&List
, OptionName
);
83 ASSERT (BootOption
!= NULL
);
85 if ((DevicePathType (BootOption
->DevicePath
) == BBS_DEVICE_PATH
) &&
86 (DevicePathSubType (BootOption
->DevicePath
) == BBS_BBS_DP
)) {
90 ASSERT (BootOption
->LoadOptionsSize
== sizeof (LEGACY_BOOT_OPTION_BBS_DATA
));
92 DeviceTypeArray
[Index
] = ((BBS_BBS_DEVICE_PATH
*) BootOption
->DevicePath
)->DeviceType
;
93 BbsIndexArray
[Index
] = ((LEGACY_BOOT_OPTION_BBS_DATA
*) BootOption
->LoadOptions
)->BbsIndex
;
95 DeviceTypeArray
[Index
] = BBS_TYPE_UNKNOWN
;
96 BbsIndexArray
[Index
] = 0xFFFF;
98 FreePool (BootOption
->DevicePath
);
99 FreePool (BootOption
->Description
);
100 FreePool (BootOption
->LoadOptions
);
101 FreePool (BootOption
);
105 // Record the corresponding Boot Option Numbers according to the DevOrder
106 // Record the EnBootOption and DisBootOption according to the DevOrder
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
];
117 if ((DevOrder
[DevOrderCount
] & 0xFF00) == 0xFF00) {
118 (*DisBootOption
)[*DisBootOptionCount
] = BootOrder
[Index
];
119 (*DisBootOptionCount
)++;
121 (*EnBootOption
)[*EnBootOptionCount
] = BootOrder
[Index
];
122 (*EnBootOptionCount
)++;
130 // Overwrite the old BootOption
132 CopyMem (&BootOrder
[StartPosition
], NewBootOption
, (*DisBootOptionCount
+ *EnBootOptionCount
) * sizeof (UINT16
));
133 Status
= gRT
->SetVariable (
135 &gEfiGlobalVariableGuid
,
136 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
141 // Changing content without increasing its size with current variable implementation shouldn't fail.
143 ASSERT_EFI_ERROR (Status
);
145 FreePool (NewBootOption
);
146 FreePool (DeviceTypeArray
);
147 FreePool (BbsIndexArray
);
148 FreePool (BootOrder
);
152 Group the legacy boot options in the BootOption.
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.
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]
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]
168 GroupMultipleLegacyBootOption4SameType (
175 UINTN DeviceTypeIndex
[7];
180 CHAR16 OptionName
[sizeof ("Boot####")];
181 BDS_COMMON_OPTION
*BootOption
;
184 SetMem (DeviceTypeIndex
, sizeof (DeviceTypeIndex
), 0xff);
186 BootOrder
= BdsLibGetVariableAndSize (
188 &gEfiGlobalVariableGuid
,
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
);
198 if ((DevicePathType (BootOption
->DevicePath
) == BBS_DEVICE_PATH
) &&
199 (DevicePathSubType (BootOption
->DevicePath
) == BBS_BBS_DP
)) {
201 // Legacy Boot Option
203 ASSERT ((((BBS_BBS_DEVICE_PATH
*) BootOption
->DevicePath
)->DeviceType
& 0xF) < ARRAY_SIZE (DeviceTypeIndex
));
204 NextIndex
= &DeviceTypeIndex
[((BBS_BBS_DEVICE_PATH
*) BootOption
->DevicePath
)->DeviceType
& 0xF];
206 if (*NextIndex
== (UINTN
) -1) {
208 // *NextIndex is the Index in BootOrder to put the next Option Number for the same type
210 *NextIndex
= Index
+ 1;
213 // insert the current boot option before *NextIndex, causing [*Next .. Index] shift right one position
215 OptionNumber
= BootOrder
[Index
];
216 CopyMem (&BootOrder
[*NextIndex
+ 1], &BootOrder
[*NextIndex
], (Index
- *NextIndex
) * sizeof (UINT16
));
217 BootOrder
[*NextIndex
] = OptionNumber
;
220 // Update the DeviceTypeIndex array to reflect the right shift operation
222 for (DeviceIndex
= 0; DeviceIndex
< ARRAY_SIZE (DeviceTypeIndex
); DeviceIndex
++) {
223 if (DeviceTypeIndex
[DeviceIndex
] != (UINTN
) -1 && DeviceTypeIndex
[DeviceIndex
] >= *NextIndex
) {
224 DeviceTypeIndex
[DeviceIndex
]++;
229 FreePool (BootOption
->DevicePath
);
230 FreePool (BootOption
->Description
);
231 FreePool (BootOption
->LoadOptions
);
232 FreePool (BootOption
);
235 Status
= gRT
->SetVariable (
237 &gEfiGlobalVariableGuid
,
238 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
243 // Changing content without increasing its size with current variable implementation shouldn't fail.
245 ASSERT_EFI_ERROR (Status
);
246 FreePool (BootOrder
);