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"
18 GetEnvironmentVariable (
19 IN CONST CHAR16
* VariableName
,
20 IN VOID
* DefaultValue
,
28 // Try to get the variable size.
31 Status
= gRT
->GetVariable ((CHAR16
*) VariableName
, &gEfiGlobalVariableGuid
, NULL
, &VariableSize
, *Value
);
32 if (Status
== EFI_NOT_FOUND
) {
33 if ((DefaultValue
!= NULL
) && (Size
!= NULL
) && (*Size
!= 0)) {
34 // If the environment variable does not exist yet then set it with the default value
35 Status
= gRT
->SetVariable (
36 (CHAR16
*)VariableName
,
37 &gEfiGlobalVariableGuid
,
38 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
42 *Value
= DefaultValue
;
46 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
47 // Get the environment variable value
48 *Value
= AllocatePool (VariableSize
);
50 return EFI_OUT_OF_RESOURCES
;
53 Status
= gRT
->GetVariable ((CHAR16
*)VariableName
, &gEfiGlobalVariableGuid
, NULL
, &VariableSize
, *Value
);
54 if (EFI_ERROR (Status
)) {
56 return EFI_INVALID_PARAMETER
;
63 *Value
= DefaultValue
;
72 IN OUT CHAR8
*CmdLine
,
84 for (CmdLineIndex
= AsciiStrLen(CmdLine
); CmdLineIndex
< MaxCmdLine
; ) {
85 Status
= gBS
->WaitForEvent (1, &gST
->ConIn
->WaitForKey
, &WaitIndex
);
86 ASSERT_EFI_ERROR (Status
);
88 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
89 ASSERT_EFI_ERROR (Status
);
91 // Unicode character is valid when Scancode is NUll
92 if (Key
.ScanCode
== SCAN_NULL
) {
93 // Scan code is NUll, hence read Unicode character
94 Char
= (CHAR8
)Key
.UnicodeChar
;
99 if ((Char
== CHAR_LINEFEED
) || (Char
== CHAR_CARRIAGE_RETURN
) || (Char
== 0x7f)) {
100 CmdLine
[CmdLineIndex
] = '\0';
104 } else if ((Char
== '\b') || (Key
.ScanCode
== SCAN_LEFT
) || (Key
.ScanCode
== SCAN_DELETE
)){
105 if (CmdLineIndex
!= 0) {
107 AsciiPrint ("\b \b");
109 } else if ((Key
.ScanCode
== SCAN_ESC
) || (Char
== 0x1B) || (Char
== 0x0)) {
110 return EFI_INVALID_PARAMETER
;
112 CmdLine
[CmdLineIndex
++] = Char
;
113 AsciiPrint ("%c", Char
);
122 IN OUT CHAR8
*CmdLine
,
126 // For a new input just passed an empty string
129 return EditHIInputAscii (CmdLine
,MaxCmdLine
);
141 Status
= EditHIInputAscii (CmdLine
,255);
142 if (!EFI_ERROR(Status
)) {
143 *Integer
= AsciiStrDecimalToUintn (CmdLine
);
151 OUT EFI_IP_ADDRESS
*Ip
159 Status
= EditHIInputAscii (CmdLine
,255);
160 if (!EFI_ERROR(Status
)) {
162 Ip
->v4
.Addr
[0] = (UINT8
)AsciiStrDecimalToUintn (Str
);
164 Str
= AsciiStrStr (Str
, ".");
166 return EFI_INVALID_PARAMETER
;
169 Ip
->v4
.Addr
[1] = (UINT8
)AsciiStrDecimalToUintn (++Str
);
171 Str
= AsciiStrStr (Str
, ".");
173 return EFI_INVALID_PARAMETER
;
176 Ip
->v4
.Addr
[2] = (UINT8
)AsciiStrDecimalToUintn (++Str
);
178 Str
= AsciiStrStr (Str
, ".");
180 return EFI_INVALID_PARAMETER
;
183 Ip
->v4
.Addr
[3] = (UINT8
)AsciiStrDecimalToUintn (++Str
);
199 Status
= GetHIInputAscii (CmdBoolean
,2);
200 if (EFI_ERROR(Status
)) {
202 } else if ((CmdBoolean
[0] == 'y') || (CmdBoolean
[0] == 'Y')) {
203 if (Value
) *Value
= TRUE
;
205 } else if ((CmdBoolean
[0] == 'n') || (CmdBoolean
[0] == 'N')) {
206 if (Value
) *Value
= FALSE
;
213 HasFilePathEfiExtension (
217 return (StrCmp (FilePath
+ (StrSize(FilePath
)/sizeof(CHAR16
)) - 5, L
".efi") == 0);
220 // Return the last non end-type Device Path Node from a Device Path
222 GetLastDevicePathNode (
223 IN EFI_DEVICE_PATH
* DevicePath
226 EFI_DEVICE_PATH
* PrevDevicePathNode
;
228 PrevDevicePathNode
= DevicePath
;
229 while (!IsDevicePathEndType (DevicePath
)) {
230 PrevDevicePathNode
= DevicePath
;
231 DevicePath
= NextDevicePathNode (DevicePath
);
234 return PrevDevicePathNode
;
238 GenerateDeviceDescriptionName (
239 IN EFI_HANDLE Handle
,
240 IN OUT CHAR16
* Description
244 EFI_COMPONENT_NAME_PROTOCOL
* ComponentName2Protocol
;
245 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
* DevicePathToTextProtocol
;
246 EFI_DEVICE_PATH_PROTOCOL
* DevicePathProtocol
;
248 CHAR16
* DevicePathTxt
;
249 EFI_DEVICE_PATH
* DevicePathNode
;
251 ComponentName2Protocol
= NULL
;
252 Status
= gBS
->HandleProtocol (Handle
, &gEfiComponentName2ProtocolGuid
, (VOID
**)&ComponentName2Protocol
);
253 if (!EFI_ERROR(Status
)) {
254 //TODO: Fixme. we must find the best langague
255 Status
= ComponentName2Protocol
->GetDriverName (ComponentName2Protocol
,"en",&DriverName
);
256 if (!EFI_ERROR(Status
)) {
257 StrnCpy (Description
,DriverName
,BOOT_DEVICE_DESCRIPTION_MAX
);
261 if (EFI_ERROR(Status
)) {
262 // Use the lastest non null entry of the Device path as a description
263 Status
= gBS
->HandleProtocol (Handle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePathProtocol
);
264 if (EFI_ERROR(Status
)) {
268 // Convert the last non end-type Device Path Node in text for the description
269 DevicePathNode
= GetLastDevicePathNode (DevicePathProtocol
);
270 Status
= gBS
->LocateProtocol (&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&DevicePathToTextProtocol
);
271 ASSERT_EFI_ERROR(Status
);
272 DevicePathTxt
= DevicePathToTextProtocol
->ConvertDevicePathToText(DevicePathNode
,TRUE
,TRUE
);
273 StrnCpy (Description
, DevicePathTxt
, BOOT_DEVICE_DESCRIPTION_MAX
);
274 FreePool (DevicePathTxt
);
282 IN CHAR16
* BootOption
286 EFI_LOAD_OPTION EfiLoadOption
;
287 UINTN EfiLoadOptionSize
;
288 BDS_LOAD_OPTION
*BdsLoadOption
;
290 Status
= GetEnvironmentVariable (BootOption
, NULL
, &EfiLoadOptionSize
, (VOID
**)&EfiLoadOption
);
291 if (!EFI_ERROR(Status
)) {
292 Status
= BootOptionParseLoadOption (EfiLoadOption
, EfiLoadOptionSize
, &BdsLoadOption
);
293 if (!EFI_ERROR(Status
)) {
294 Status
= BootOptionStart (BdsLoadOption
);
295 FreePool (BdsLoadOption
);
298 if (!EFI_ERROR(Status
)) {
299 Status
= EFI_SUCCESS
;
301 Status
= EFI_NOT_STARTED
;
304 Status
= EFI_NOT_FOUND
;