3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <Guid/ArmGlobalVariableHob.h>
16 #include "BdsInternal.h"
18 extern EFI_HANDLE mImageHandle
;
22 IN BDS_LOAD_OPTION
*BootOption
26 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
* EfiDevicePathFromTextProtocol
;
28 ARM_BDS_LOADER_OPTIONAL_DATA
* OptionalData
;
29 ARM_BDS_LINUX_ARGUMENTS
* LinuxArguments
;
30 EFI_DEVICE_PATH_PROTOCOL
* FdtDevicePath
;
31 EFI_DEVICE_PATH_PROTOCOL
* DefaultFdtDevicePath
;
32 UINTN FdtDevicePathSize
;
35 EFI_DEVICE_PATH
* Initrd
;
36 UINT16 LoadOptionIndexSize
;
38 if (IS_ARM_BDS_BOOTENTRY (BootOption
)) {
39 Status
= EFI_UNSUPPORTED
;
40 OptionalData
= BootOption
->OptionalData
;
41 LoaderType
= ReadUnaligned32 ((CONST UINT32
*)&OptionalData
->Header
.LoaderType
);
43 if (LoaderType
== BDS_LOADER_EFI_APPLICATION
) {
44 // Need to connect every drivers to ensure no dependencies are missing for the application
45 BdsConnectAllDrivers();
47 Status
= BdsStartEfiApplication (mImageHandle
, BootOption
->FilePathList
, 0, NULL
);
48 } else if (LoaderType
== BDS_LOADER_KERNEL_LINUX_ATAG
) {
49 LinuxArguments
= &(OptionalData
->Arguments
.LinuxArguments
);
50 CmdLineSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->CmdLineSize
);
51 InitrdSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->InitrdSize
);
54 Initrd
= GetAlignedDevicePath ((EFI_DEVICE_PATH
*)((UINTN
)(LinuxArguments
+ 1) + CmdLineSize
));
59 Status
= BdsBootLinuxAtag (BootOption
->FilePathList
,
61 (CHAR8
*)(LinuxArguments
+ 1)); // CmdLine
62 } else if (LoaderType
== BDS_LOADER_KERNEL_LINUX_FDT
) {
63 LinuxArguments
= &(OptionalData
->Arguments
.LinuxArguments
);
64 CmdLineSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->CmdLineSize
);
65 InitrdSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->InitrdSize
);
68 Initrd
= GetAlignedDevicePath ((EFI_DEVICE_PATH
*)((UINTN
)(LinuxArguments
+ 1) + CmdLineSize
));
73 // Get the default FDT device path
74 Status
= gBS
->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&EfiDevicePathFromTextProtocol
);
75 ASSERT_EFI_ERROR(Status
);
76 DefaultFdtDevicePath
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath ((CHAR16
*)PcdGetPtr(PcdFdtDevicePath
));
78 // Get the FDT device path
79 FdtDevicePathSize
= GetDevicePathSize (DefaultFdtDevicePath
);
80 Status
= GetEnvironmentVariable ((CHAR16
*)L
"Fdt", &gArmGlobalVariableGuid
,
81 DefaultFdtDevicePath
, &FdtDevicePathSize
, (VOID
**)&FdtDevicePath
);
82 ASSERT_EFI_ERROR(Status
);
84 Status
= BdsBootLinuxFdt (BootOption
->FilePathList
,
86 (CHAR8
*)(LinuxArguments
+ 1),
89 FreePool (DefaultFdtDevicePath
);
90 FreePool (FdtDevicePath
);
93 // Set BootCurrent variable
94 LoadOptionIndexSize
= sizeof(UINT16
);
95 gRT
->SetVariable (L
"BootCurrent", &gEfiGlobalVariableGuid
,
96 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
97 LoadOptionIndexSize
, &(BootOption
->LoadOptionIndex
));
99 Status
= BdsStartEfiApplication (mImageHandle
, BootOption
->FilePathList
, BootOption
->OptionalDataSize
, BootOption
->OptionalData
);
101 // Clear BootCurrent variable
102 LoadOptionIndexSize
= sizeof(UINT16
);
103 gRT
->SetVariable (L
"BootCurrent", &gEfiGlobalVariableGuid
,
104 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
113 IN OUT LIST_ENTRY
*BootOptionList
120 BDS_LOAD_OPTION
* BdsLoadOption
;
121 BDS_LOAD_OPTION_ENTRY
* BdsLoadOptionEntry
;
123 InitializeListHead (BootOptionList
);
125 // Get the Boot Option Order from the environment variable
126 Status
= GetGlobalEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
127 if (EFI_ERROR(Status
)) {
131 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
132 Status
= BootOptionFromLoadOptionIndex (BootOrder
[Index
], &BdsLoadOption
);
133 if (!EFI_ERROR(Status
)) {
134 BdsLoadOptionEntry
= (BDS_LOAD_OPTION_ENTRY
*)AllocatePool(sizeof(BDS_LOAD_OPTION_ENTRY
));
135 BdsLoadOptionEntry
->BdsLoadOption
= BdsLoadOption
;
136 InsertTailList (BootOptionList
,&BdsLoadOptionEntry
->Link
);
140 FreePool (BootOrder
);
147 BootOptionSetFields (
148 IN BDS_LOAD_OPTION
* BootOption
,
149 IN UINT32 Attributes
,
150 IN CHAR16
* BootDescription
,
151 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
152 IN ARM_BDS_LOADER_TYPE BootType
,
153 IN UINT8
* OptionalData
,
154 IN UINTN OptionalDataSize
157 EFI_LOAD_OPTION EfiLoadOption
;
158 UINTN EfiLoadOptionSize
;
159 UINTN BootDescriptionSize
;
160 UINT16 FilePathListLength
;
161 UINT8
* EfiLoadOptionPtr
;
162 UINT8
* InitrdPathListPtr
;
163 ARM_BDS_LINUX_ARGUMENTS
* DestLinuxArguments
;
164 ARM_BDS_LINUX_ARGUMENTS
* SrcLinuxArguments
;
165 ARM_BDS_LOADER_ARGUMENTS
* BootArguments
;
167 // If we are overwriting an existent Boot Option then we have to free previously allocated memory
168 if (BootOption
->LoadOption
) {
169 FreePool (BootOption
->LoadOption
);
172 BootDescriptionSize
= StrSize (BootDescription
);
174 // Fixup the size in case of entry specific to ArmPlatformPkg/Bds
175 if ((BootType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (BootType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
176 OptionalDataSize
+= sizeof(ARM_BDS_LOADER_OPTIONAL_DATA_HEADER
);
179 // Compute the size of the FilePath list
180 FilePathListLength
= GetUnalignedDevicePathSize (DevicePath
);
182 // Allocate the memory for the EFI Load Option
183 EfiLoadOptionSize
= sizeof(UINT32
) + sizeof(UINT16
) + BootDescriptionSize
+ FilePathListLength
+ OptionalDataSize
;
184 EfiLoadOption
= (EFI_LOAD_OPTION
)AllocatePool(EfiLoadOptionSize
);
185 EfiLoadOptionPtr
= EfiLoadOption
;
188 // Populate the EFI Load Option and BDS Boot Option structures
192 BootOption
->Attributes
= Attributes
;
193 *(UINT32
*)EfiLoadOptionPtr
= Attributes
;
194 EfiLoadOptionPtr
+= sizeof(UINT32
);
196 // FilePath List fields
197 BootOption
->FilePathListLength
= FilePathListLength
;
198 *(UINT16
*)EfiLoadOptionPtr
= FilePathListLength
;
199 EfiLoadOptionPtr
+= sizeof(UINT16
);
201 // Boot description fields
202 BootOption
->Description
= (CHAR16
*)EfiLoadOptionPtr
;
203 CopyMem (EfiLoadOptionPtr
, BootDescription
, BootDescriptionSize
);
204 EfiLoadOptionPtr
+= BootDescriptionSize
;
207 BootOption
->FilePathList
= (EFI_DEVICE_PATH_PROTOCOL
*)EfiLoadOptionPtr
;
208 CopyMem (EfiLoadOptionPtr
, DevicePath
, FilePathListLength
);
209 EfiLoadOptionPtr
+= FilePathListLength
;
211 // Optional Data fields, Do unaligned writes
212 BootOption
->OptionalData
= EfiLoadOptionPtr
;
214 if ((BootType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (BootType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
216 WriteUnaligned32 ((UINT32
*)EfiLoadOptionPtr
, ARM_BDS_OPTIONAL_DATA_SIGNATURE
);
217 WriteUnaligned32 ((UINT32
*)(EfiLoadOptionPtr
+ 4), BootType
);
219 BootArguments
= (ARM_BDS_LOADER_ARGUMENTS
*)OptionalData
;
220 SrcLinuxArguments
= &(BootArguments
->LinuxArguments
);
221 DestLinuxArguments
= &((ARM_BDS_LOADER_OPTIONAL_DATA
*)EfiLoadOptionPtr
)->Arguments
.LinuxArguments
;
223 WriteUnaligned16 ((UINT16
*)&(DestLinuxArguments
->CmdLineSize
), SrcLinuxArguments
->CmdLineSize
);
224 WriteUnaligned16 ((UINT16
*)&(DestLinuxArguments
->InitrdSize
), SrcLinuxArguments
->InitrdSize
);
226 if (SrcLinuxArguments
->CmdLineSize
> 0) {
227 CopyMem ((VOID
*)(DestLinuxArguments
+ 1), (VOID
*)(SrcLinuxArguments
+ 1), SrcLinuxArguments
->CmdLineSize
);
230 if (SrcLinuxArguments
->InitrdSize
> 0) {
231 InitrdPathListPtr
= (UINT8
*)((UINTN
)(DestLinuxArguments
+ 1) + SrcLinuxArguments
->CmdLineSize
);
232 CopyMem (InitrdPathListPtr
, (VOID
*)((UINTN
)(SrcLinuxArguments
+ 1) + SrcLinuxArguments
->CmdLineSize
), SrcLinuxArguments
->InitrdSize
);
235 CopyMem (BootOption
->OptionalData
, OptionalData
, OptionalDataSize
);
237 BootOption
->OptionalDataSize
= OptionalDataSize
;
239 // If this function is called at the creation of the Boot Device entry (not at the update) the
240 // BootOption->LoadOptionSize must be zero then we get a new BootIndex for this entry
241 if (BootOption
->LoadOptionSize
== 0) {
242 BootOption
->LoadOptionIndex
= BootOptionAllocateBootIndex ();
245 // Fill the EFI Load option fields
246 BootOption
->LoadOption
= EfiLoadOption
;
247 BootOption
->LoadOptionSize
= EfiLoadOptionSize
;
254 IN UINT32 Attributes
,
255 IN CHAR16
* BootDescription
,
256 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
257 IN ARM_BDS_LOADER_TYPE BootType
,
258 IN UINT8
* OptionalData
,
259 IN UINTN OptionalDataSize
,
260 OUT BDS_LOAD_OPTION
** BdsLoadOption
264 BDS_LOAD_OPTION_ENTRY
* BootOptionEntry
;
265 BDS_LOAD_OPTION
* BootOption
;
266 CHAR16 BootVariableName
[9];
271 // Allocate and fill the memory for the BDS Load Option structure
273 BootOptionEntry
= (BDS_LOAD_OPTION_ENTRY
*)AllocatePool (sizeof (BDS_LOAD_OPTION_ENTRY
));
274 InitializeListHead (&BootOptionEntry
->Link
);
275 BootOptionEntry
->BdsLoadOption
= (BDS_LOAD_OPTION
*)AllocateZeroPool (sizeof(BDS_LOAD_OPTION
));
277 BootOption
= BootOptionEntry
->BdsLoadOption
;
278 BootOptionSetFields (BootOption
, Attributes
, BootDescription
, DevicePath
, BootType
, OptionalData
, OptionalDataSize
);
281 // Set the related environment variables
284 // Create Boot#### environment variable
285 UnicodeSPrint (BootVariableName
, 9 * sizeof(CHAR16
), L
"Boot%04X", BootOption
->LoadOptionIndex
);
286 Status
= gRT
->SetVariable (
288 &gEfiGlobalVariableGuid
,
289 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
290 BootOption
->LoadOptionSize
,
291 BootOption
->LoadOption
294 // Add the new Boot Index to the list
295 Status
= GetGlobalEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
296 if (!EFI_ERROR(Status
)) {
297 BootOrder
= ReallocatePool (BootOrderSize
, BootOrderSize
+ sizeof(UINT16
), BootOrder
);
298 // Add the new index at the end
299 BootOrder
[BootOrderSize
/ sizeof(UINT16
)] = BootOption
->LoadOptionIndex
;
300 BootOrderSize
+= sizeof(UINT16
);
302 // BootOrder does not exist. Create it
303 BootOrderSize
= sizeof(UINT16
);
304 BootOrder
= &(BootOption
->LoadOptionIndex
);
307 // Update (or Create) the BootOrder environment variable
308 Status
= gRT
->SetVariable (
310 &gEfiGlobalVariableGuid
,
311 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
316 // We only free it if the UEFI Variable 'BootOrder' was already existing
317 if (BootOrderSize
> sizeof(UINT16
)) {
318 FreePool (BootOrder
);
321 *BdsLoadOption
= BootOption
;
327 IN BDS_LOAD_OPTION
* BdsLoadOption
,
328 IN UINT32 Attributes
,
329 IN CHAR16
* BootDescription
,
330 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
331 IN ARM_BDS_LOADER_TYPE BootType
,
332 IN UINT8
* OptionalData
,
333 IN UINTN OptionalDataSize
337 CHAR16 BootVariableName
[9];
339 // Update the BDS Load Option structure
340 BootOptionSetFields (BdsLoadOption
, Attributes
, BootDescription
, DevicePath
, BootType
, OptionalData
, OptionalDataSize
);
342 // Update the related environment variables
343 UnicodeSPrint (BootVariableName
, 9 * sizeof(CHAR16
), L
"Boot%04X", BdsLoadOption
->LoadOptionIndex
);
345 Status
= gRT
->SetVariable (
347 &gEfiGlobalVariableGuid
,
348 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
349 BdsLoadOption
->LoadOptionSize
,
350 BdsLoadOption
->LoadOption
358 IN BDS_LOAD_OPTION
*BootOption
364 UINTN BootOrderCount
;
367 // Remove the entry from the BootOrder environment variable
368 Status
= GetGlobalEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
369 if (!EFI_ERROR(Status
)) {
370 BootOrderCount
= BootOrderSize
/ sizeof(UINT16
);
372 // Find the index of the removed entry
373 for (Index
= 0; Index
< BootOrderCount
; Index
++) {
374 if (BootOrder
[Index
] == BootOption
->LoadOptionIndex
) {
375 // If it the last entry we do not need to rearrange the BootOrder list
376 if (Index
+ 1 != BootOrderCount
) {
379 &BootOrder
[Index
+ 1],
380 (BootOrderCount
- (Index
+ 1)) * sizeof(UINT16
)
387 // Update the BootOrder environment variable
388 Status
= gRT
->SetVariable (
390 &gEfiGlobalVariableGuid
,
391 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
392 BootOrderSize
- sizeof(UINT16
),
397 FreePool (BootOrder
);