3 * Copyright (c) 2011, 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 "BdsInternal.h"
17 extern EFI_HANDLE mImageHandle
;
21 IN BDS_LOAD_OPTION
*BootOption
25 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
* EfiDevicePathFromTextProtocol
;
27 ARM_BDS_LOADER_OPTIONAL_DATA
* OptionalData
;
28 ARM_BDS_LINUX_ARGUMENTS
* LinuxArguments
;
29 EFI_DEVICE_PATH_PROTOCOL
* FdtDevicePath
;
30 EFI_DEVICE_PATH_PROTOCOL
* DefaultFdtDevicePath
;
31 UINTN FdtDevicePathSize
;
34 EFI_DEVICE_PATH
* Initrd
;
36 if (IS_ARM_BDS_BOOTENTRY (BootOption
)) {
37 Status
= EFI_UNSUPPORTED
;
38 OptionalData
= BootOption
->OptionalData
;
39 LoaderType
= ReadUnaligned32 ((CONST UINT32
*)&OptionalData
->Header
.LoaderType
);
41 if (LoaderType
== BDS_LOADER_EFI_APPLICATION
) {
42 // Need to connect every drivers to ensure no dependencies are missing for the application
43 BdsConnectAllDrivers();
45 Status
= BdsStartEfiApplication (mImageHandle
, BootOption
->FilePathList
, 0, NULL
);
46 } else if (LoaderType
== BDS_LOADER_KERNEL_LINUX_ATAG
) {
47 LinuxArguments
= &(OptionalData
->Arguments
.LinuxArguments
);
48 CmdLineSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->CmdLineSize
);
49 InitrdSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->InitrdSize
);
52 Initrd
= GetAlignedDevicePath ((EFI_DEVICE_PATH
*)((UINTN
)(LinuxArguments
+ 1) + CmdLineSize
));
57 Status
= BdsBootLinux (BootOption
->FilePathList
,
59 (CHAR8
*)(LinuxArguments
+ 1), // CmdLine
61 } else if (LoaderType
== BDS_LOADER_KERNEL_LINUX_FDT
) {
62 LinuxArguments
= &(OptionalData
->Arguments
.LinuxArguments
);
63 CmdLineSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->CmdLineSize
);
64 InitrdSize
= ReadUnaligned16 ((CONST UINT16
*)&LinuxArguments
->InitrdSize
);
67 Initrd
= GetAlignedDevicePath ((EFI_DEVICE_PATH
*)((UINTN
)(LinuxArguments
+ 1) + CmdLineSize
));
72 // Get the default FDT device path
73 Status
= gBS
->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&EfiDevicePathFromTextProtocol
);
74 ASSERT_EFI_ERROR(Status
);
75 DefaultFdtDevicePath
= EfiDevicePathFromTextProtocol
->ConvertTextToDevicePath ((CHAR16
*)PcdGetPtr(PcdFdtDevicePath
));
77 // Get the FDT device path
78 FdtDevicePathSize
= GetDevicePathSize (DefaultFdtDevicePath
);
79 Status
= GetEnvironmentVariable ((CHAR16
*)L
"FDT", DefaultFdtDevicePath
, &FdtDevicePathSize
, (VOID
**)&FdtDevicePath
);
80 ASSERT_EFI_ERROR(Status
);
82 Status
= BdsBootLinux (BootOption
->FilePathList
,
84 (CHAR8
*)(LinuxArguments
+ 1),
88 Status
= BdsStartEfiApplication (mImageHandle
, BootOption
->FilePathList
);
95 BootOptionParseLoadOption (
96 IN EFI_LOAD_OPTION EfiLoadOption
,
97 IN UINTN EfiLoadOptionSize
,
98 OUT BDS_LOAD_OPTION
**BdsLoadOption
101 BDS_LOAD_OPTION
*LoadOption
;
102 UINTN FilePathListLength
;
103 UINTN DescriptionLength
;
105 if (EfiLoadOption
== NULL
) {
106 return EFI_INVALID_PARAMETER
;
109 if (EfiLoadOptionSize
< sizeof(UINT32
) + sizeof(UINT16
) + sizeof(CHAR16
) + sizeof(EFI_DEVICE_PATH_PROTOCOL
)) {
110 return EFI_BAD_BUFFER_SIZE
;
113 LoadOption
= (BDS_LOAD_OPTION
*)AllocatePool(sizeof(BDS_LOAD_OPTION
));
114 if (LoadOption
== NULL
) {
115 return EFI_OUT_OF_RESOURCES
;
118 LoadOption
->LoadOption
= EfiLoadOption
;
119 LoadOption
->LoadOptionSize
= EfiLoadOptionSize
;
121 LoadOption
->Attributes
= *(UINT32
*)EfiLoadOption
;
122 FilePathListLength
= *(UINT16
*)(EfiLoadOption
+ sizeof(UINT32
));
123 LoadOption
->Description
= (CHAR16
*)(EfiLoadOption
+ sizeof(UINT32
) + sizeof(UINT16
));
124 DescriptionLength
= StrSize (LoadOption
->Description
);
125 LoadOption
->FilePathList
= (EFI_DEVICE_PATH_PROTOCOL
*)(EfiLoadOption
+ sizeof(UINT32
) + sizeof(UINT16
) + DescriptionLength
);
127 if ((UINTN
)((UINT8
*)LoadOption
->FilePathList
+ FilePathListLength
- EfiLoadOption
) == EfiLoadOptionSize
) {
128 LoadOption
->OptionalData
= NULL
;
130 LoadOption
->OptionalData
= (BDS_LOADER_OPTIONAL_DATA
*)((UINT8
*)LoadOption
->FilePathList
+ FilePathListLength
);
133 *BdsLoadOption
= LoadOption
;
138 BootOptionFromLoadOptionVariable (
139 IN UINT16 LoadOptionIndex
,
140 OUT BDS_LOAD_OPTION
**BdsLoadOption
144 CHAR16 BootVariableName
[9];
145 EFI_LOAD_OPTION EfiLoadOption
;
146 UINTN EfiLoadOptionSize
;
148 UnicodeSPrint (BootVariableName
, 9 * sizeof(CHAR16
), L
"Boot%04X", LoadOptionIndex
);
150 Status
= GetEnvironmentVariable (BootVariableName
, NULL
, &EfiLoadOptionSize
, (VOID
**)&EfiLoadOption
);
151 if (!EFI_ERROR(Status
)) {
152 Status
= BootOptionParseLoadOption (EfiLoadOption
,EfiLoadOptionSize
,BdsLoadOption
);
153 if (!EFI_ERROR(Status
)) {
154 (*BdsLoadOption
)->LoadOptionIndex
= LoadOptionIndex
;
163 IN OUT LIST_ENTRY
*BootOptionList
170 BDS_LOAD_OPTION
* BdsLoadOption
;
171 BDS_LOAD_OPTION_ENTRY
* BdsLoadOptionEntry
;
173 InitializeListHead (BootOptionList
);
175 // Get the Boot Option Order from the environment variable
176 Status
= GetEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
177 if (EFI_ERROR(Status
)) {
181 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
182 Status
= BootOptionFromLoadOptionVariable (BootOrder
[Index
],&BdsLoadOption
);
183 if (!EFI_ERROR(Status
)) {
184 BdsLoadOptionEntry
= (BDS_LOAD_OPTION_ENTRY
*)AllocatePool(sizeof(BDS_LOAD_OPTION_ENTRY
));
185 BdsLoadOptionEntry
->BdsLoadOption
= BdsLoadOption
;
186 InsertTailList (BootOptionList
,&BdsLoadOptionEntry
->Link
);
194 BootOptionAllocateBootIndex (
205 // Get the Boot Option Order from the environment variable
206 Status
= GetEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
207 if (!EFI_ERROR(Status
)) {
208 for (BootIndex
= 0; BootIndex
<= 0xFFFF; BootIndex
++) {
210 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
211 if (BootOrder
[Index
] == BootIndex
) {
221 // Return the first index
227 BootOptionSetFields (
228 IN BDS_LOAD_OPTION
* BootOption
,
229 IN UINT32 Attributes
,
230 IN CHAR16
* BootDescription
,
231 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
232 IN ARM_BDS_LOADER_TYPE BootType
,
233 IN ARM_BDS_LOADER_ARGUMENTS
* BootArguments
236 EFI_LOAD_OPTION EfiLoadOption
;
237 UINTN EfiLoadOptionSize
;
238 UINTN BootDescriptionSize
;
239 UINTN BootOptionalDataSize
;
240 UINT16 FilePathListLength
;
241 UINT8
* EfiLoadOptionPtr
;
242 UINT8
* InitrdPathListPtr
;
243 UINTN OptionalDataSize
;
244 ARM_BDS_LINUX_ARGUMENTS
* DestLinuxArguments
;
245 ARM_BDS_LINUX_ARGUMENTS
* SrcLinuxArguments
;
247 // If we are overwriting an existent Boot Option then we have to free previously allocated memory
248 if (BootOption
->LoadOption
) {
249 FreePool(BootOption
->LoadOption
);
252 BootDescriptionSize
= StrSize (BootDescription
);
253 BootOptionalDataSize
= sizeof(ARM_BDS_LOADER_OPTIONAL_DATA_HEADER
);
254 if ((BootType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (BootType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
255 BootOptionalDataSize
+= sizeof(ARM_BDS_LINUX_ARGUMENTS
) + BootArguments
->LinuxArguments
.CmdLineSize
+ BootArguments
->LinuxArguments
.InitrdSize
;
258 // Compute the size of the FilePath list
259 FilePathListLength
= GetUnalignedDevicePathSize (DevicePath
);
261 // Allocate the memory for the EFI Load Option
262 EfiLoadOptionSize
= sizeof(UINT32
) + sizeof(UINT16
) + BootDescriptionSize
+ FilePathListLength
+ BootOptionalDataSize
;
263 EfiLoadOption
= (EFI_LOAD_OPTION
)AllocatePool(EfiLoadOptionSize
);
264 EfiLoadOptionPtr
= EfiLoadOption
;
267 // Populate the EFI Load Option and BDS Boot Option structures
271 BootOption
->Attributes
= Attributes
;
272 *(UINT32
*)EfiLoadOptionPtr
= Attributes
;
273 EfiLoadOptionPtr
+= sizeof(UINT32
);
275 // FilePath List fields
276 BootOption
->FilePathListLength
= FilePathListLength
;
277 *(UINT16
*)EfiLoadOptionPtr
= FilePathListLength
;
278 EfiLoadOptionPtr
+= sizeof(UINT16
);
280 // Boot description fields
281 BootOption
->Description
= (CHAR16
*)EfiLoadOptionPtr
;
282 CopyMem (EfiLoadOptionPtr
, BootDescription
, BootDescriptionSize
);
283 EfiLoadOptionPtr
+= BootDescriptionSize
;
286 BootOption
->FilePathList
= (EFI_DEVICE_PATH_PROTOCOL
*)EfiLoadOptionPtr
;
287 CopyMem (EfiLoadOptionPtr
, DevicePath
, FilePathListLength
);
288 EfiLoadOptionPtr
+= FilePathListLength
;
290 // Optional Data fields, Do unaligned writes
291 BootOption
->OptionalData
= EfiLoadOptionPtr
;
292 WriteUnaligned32 ((UINT32
*)EfiLoadOptionPtr
, ARM_BDS_OPTIONAL_DATA_SIGNATURE
);
293 WriteUnaligned32 ((UINT32
*)(EfiLoadOptionPtr
+ 4), BootType
);
295 OptionalDataSize
= sizeof(ARM_BDS_LOADER_OPTIONAL_DATA_HEADER
);
297 if ((BootType
== BDS_LOADER_KERNEL_LINUX_ATAG
) || (BootType
== BDS_LOADER_KERNEL_LINUX_FDT
)) {
298 SrcLinuxArguments
= &(BootArguments
->LinuxArguments
);
299 DestLinuxArguments
= &((ARM_BDS_LOADER_OPTIONAL_DATA
*)EfiLoadOptionPtr
)->Arguments
.LinuxArguments
;
301 WriteUnaligned16 ((UINT16
*)&(DestLinuxArguments
->CmdLineSize
), SrcLinuxArguments
->CmdLineSize
);
302 WriteUnaligned16 ((UINT16
*)&(DestLinuxArguments
->InitrdSize
), SrcLinuxArguments
->InitrdSize
);
303 OptionalDataSize
+= sizeof (ARM_BDS_LINUX_ARGUMENTS
);
305 if (SrcLinuxArguments
->CmdLineSize
> 0) {
306 CopyMem ((VOID
*)(DestLinuxArguments
+ 1), (VOID
*)(SrcLinuxArguments
+ 1), SrcLinuxArguments
->CmdLineSize
);
307 OptionalDataSize
+= SrcLinuxArguments
->CmdLineSize
;
310 if (SrcLinuxArguments
->InitrdSize
> 0) {
311 InitrdPathListPtr
= (UINT8
*)((UINTN
)(DestLinuxArguments
+ 1) + SrcLinuxArguments
->CmdLineSize
);
312 CopyMem (InitrdPathListPtr
, (VOID
*)((UINTN
)(SrcLinuxArguments
+ 1) + SrcLinuxArguments
->CmdLineSize
), SrcLinuxArguments
->InitrdSize
);
315 BootOption
->OptionalDataSize
= OptionalDataSize
;
317 // If this function is called at the creation of the Boot Device entry (not at the update) the
318 // BootOption->LoadOptionSize must be zero then we get a new BootIndex for this entry
319 if (BootOption
->LoadOptionSize
== 0) {
320 BootOption
->LoadOptionIndex
= BootOptionAllocateBootIndex ();
323 // Fill the EFI Load option fields
324 BootOption
->LoadOption
= EfiLoadOption
;
325 BootOption
->LoadOptionSize
= EfiLoadOptionSize
;
332 IN UINT32 Attributes
,
333 IN CHAR16
* BootDescription
,
334 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
335 IN ARM_BDS_LOADER_TYPE BootType
,
336 IN ARM_BDS_LOADER_ARGUMENTS
* BootArguments
,
337 OUT BDS_LOAD_OPTION
** BdsLoadOption
341 BDS_LOAD_OPTION_ENTRY
* BootOptionEntry
;
342 BDS_LOAD_OPTION
* BootOption
;
343 CHAR16 BootVariableName
[9];
348 // Allocate and fill the memory for the BDS Load Option structure
350 BootOptionEntry
= (BDS_LOAD_OPTION_ENTRY
*)AllocatePool (sizeof (BDS_LOAD_OPTION_ENTRY
));
351 InitializeListHead (&BootOptionEntry
->Link
);
352 BootOptionEntry
->BdsLoadOption
= (BDS_LOAD_OPTION
*)AllocateZeroPool (sizeof(BDS_LOAD_OPTION
));
354 BootOption
= BootOptionEntry
->BdsLoadOption
;
355 BootOptionSetFields (BootOption
, Attributes
, BootDescription
, DevicePath
, BootType
, BootArguments
);
358 // Set the related environment variables
361 // Create Boot#### environment variable
362 UnicodeSPrint (BootVariableName
, 9 * sizeof(CHAR16
), L
"Boot%04X", BootOption
->LoadOptionIndex
);
363 Status
= gRT
->SetVariable (
365 &gEfiGlobalVariableGuid
,
366 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
367 BootOption
->LoadOptionSize
,
368 BootOption
->LoadOption
371 // Add the new Boot Index to the list
372 Status
= GetEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
373 if (!EFI_ERROR(Status
)) {
374 BootOrder
= ReallocatePool (BootOrderSize
, BootOrderSize
+ sizeof(UINT16
), BootOrder
);
375 // Add the new index at the end
376 BootOrder
[BootOrderSize
/ sizeof(UINT16
)] = BootOption
->LoadOptionIndex
;
377 BootOrderSize
+= sizeof(UINT16
);
379 // BootOrder does not exist. Create it
380 BootOrderSize
= sizeof(UINT16
);
381 BootOrder
= &(BootOption
->LoadOptionIndex
);
384 // Update (or Create) the BootOrder environment variable
385 Status
= gRT
->SetVariable (
387 &gEfiGlobalVariableGuid
,
388 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
393 *BdsLoadOption
= BootOption
;
399 IN BDS_LOAD_OPTION
* BdsLoadOption
,
400 IN UINT32 Attributes
,
401 IN CHAR16
* BootDescription
,
402 IN EFI_DEVICE_PATH_PROTOCOL
* DevicePath
,
403 IN ARM_BDS_LOADER_TYPE BootType
,
404 IN ARM_BDS_LOADER_ARGUMENTS
* BootArguments
408 CHAR16 BootVariableName
[9];
410 // Update the BDS Load Option structure
411 BootOptionSetFields (BdsLoadOption
, Attributes
, BootDescription
, DevicePath
, BootType
, BootArguments
);
413 // Update the related environment variables
414 UnicodeSPrint (BootVariableName
, 9 * sizeof(CHAR16
), L
"Boot%04X", BdsLoadOption
->LoadOptionIndex
);
416 Status
= gRT
->SetVariable (
418 &gEfiGlobalVariableGuid
,
419 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
420 BdsLoadOption
->LoadOptionSize
,
421 BdsLoadOption
->LoadOption
429 IN BDS_LOAD_OPTION
*BootOption
435 UINTN BootOrderCount
;
438 // Remove the entry from the BootOrder environment variable
439 Status
= GetEnvironmentVariable (L
"BootOrder", NULL
, &BootOrderSize
, (VOID
**)&BootOrder
);
440 if (!EFI_ERROR(Status
)) {
441 BootOrderCount
= BootOrderSize
/ sizeof(UINT16
);
443 // Find the index of the removed entry
444 for (Index
= 0; Index
< BootOrderCount
; Index
++) {
445 if (BootOrder
[Index
] == BootOption
->LoadOptionIndex
) {
446 // If it the last entry we do not need to rearrange the BootOrder list
447 if (Index
+ 1 != BootOrderCount
) {
448 CopyMem (&BootOrder
[Index
],&BootOrder
[Index
+1], BootOrderCount
- (Index
+ 1));
454 // Update the BootOrder environment variable
455 Status
= gRT
->SetVariable (
457 &gEfiGlobalVariableGuid
,
458 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
459 BootOrderSize
- sizeof(UINT16
),