]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Bds/BdsHelper.c
3142d85c10cdf46cdecbdd7f155712782f0f6ab3
[mirror_edk2.git] / ArmPlatformPkg / Bds / BdsHelper.c
1 /** @file
2 *
3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 *
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
9 *
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.
12 *
13 **/
14
15 #include "BdsInternal.h"
16
17 EFI_STATUS
18 EditHIInputStr (
19 IN OUT CHAR16 *CmdLine,
20 IN UINTN MaxCmdLine
21 )
22 {
23 UINTN CmdLineIndex;
24 UINTN WaitIndex;
25 CHAR8 Char;
26 EFI_INPUT_KEY Key;
27 EFI_STATUS Status;
28
29 // The command line must be at least one character long
30 ASSERT (MaxCmdLine > 0);
31
32 // Ensure the last character of the buffer is the NULL character
33 CmdLine[MaxCmdLine - 1] = '\0';
34
35 Print (CmdLine);
36
37 // To prevent a buffer overflow, we only allow to enter (MaxCmdLine-1) characters
38 for (CmdLineIndex = StrLen (CmdLine); CmdLineIndex < MaxCmdLine - 1; ) {
39 Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &WaitIndex);
40 ASSERT_EFI_ERROR (Status);
41
42 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
43 ASSERT_EFI_ERROR (Status);
44
45 // Unicode character is valid when Scancode is NUll
46 if (Key.ScanCode == SCAN_NULL) {
47 // Scan code is NUll, hence read Unicode character
48 Char = (CHAR8)Key.UnicodeChar;
49 } else {
50 Char = CHAR_NULL;
51 }
52
53 if ((Char == CHAR_LINEFEED) || (Char == CHAR_CARRIAGE_RETURN) || (Char == 0x7f)) {
54 CmdLine[CmdLineIndex] = '\0';
55 Print (L"\r\n");
56
57 return EFI_SUCCESS;
58 } else if ((Key.UnicodeChar == L'\b') || (Key.ScanCode == SCAN_LEFT) || (Key.ScanCode == SCAN_DELETE)){
59 if (CmdLineIndex != 0) {
60 CmdLineIndex--;
61 Print (L"\b \b");
62 }
63 } else if ((Key.ScanCode == SCAN_ESC) || (Char == 0x1B) || (Char == 0x0)) {
64 return EFI_INVALID_PARAMETER;
65 } else {
66 CmdLine[CmdLineIndex++] = Key.UnicodeChar;
67 Print (L"%c", Key.UnicodeChar);
68 }
69 }
70
71 return EFI_SUCCESS;
72 }
73
74 EFI_STATUS
75 GetHIInputStr (
76 IN OUT CHAR16 *CmdLine,
77 IN UINTN MaxCmdLine
78 )
79 {
80 EFI_STATUS Status;
81
82 // For a new input just passed an empty string
83 CmdLine[0] = L'\0';
84
85 Status = EditHIInputStr (CmdLine, MaxCmdLine);
86
87 return Status;
88 }
89
90 EFI_STATUS
91 EditHIInputAscii (
92 IN OUT CHAR8 *CmdLine,
93 IN UINTN MaxCmdLine
94 )
95 {
96 CHAR16* Str;
97 EFI_STATUS Status;
98
99 Str = (CHAR16*)AllocatePool (MaxCmdLine * sizeof(CHAR16));
100 AsciiStrToUnicodeStr (CmdLine, Str);
101
102 Status = EditHIInputStr (Str, MaxCmdLine);
103 if (!EFI_ERROR(Status)) {
104 UnicodeStrToAsciiStr (Str, CmdLine);
105 }
106 FreePool (Str);
107
108 return Status;
109 }
110
111 EFI_STATUS
112 GetHIInputAscii (
113 IN OUT CHAR8 *CmdLine,
114 IN UINTN MaxCmdLine
115 )
116 {
117 // For a new input just passed an empty string
118 CmdLine[0] = '\0';
119
120 return EditHIInputAscii (CmdLine,MaxCmdLine);
121 }
122
123 EFI_STATUS
124 GetHIInputInteger (
125 OUT UINTN *Integer
126 )
127 {
128 CHAR16 CmdLine[255];
129 EFI_STATUS Status;
130
131 CmdLine[0] = '\0';
132 Status = EditHIInputStr (CmdLine, 255);
133 if (!EFI_ERROR(Status)) {
134 *Integer = StrDecimalToUintn (CmdLine);
135 }
136
137 return Status;
138 }
139
140 EFI_STATUS
141 GetHIInputIP (
142 OUT EFI_IP_ADDRESS *Ip
143 )
144 {
145 CHAR16 CmdLine[255];
146 CHAR16 *Str;
147 EFI_STATUS Status;
148
149 CmdLine[0] = '\0';
150 Status = EditHIInputStr (CmdLine,255);
151 if (!EFI_ERROR(Status)) {
152 Str = CmdLine;
153 Ip->v4.Addr[0] = (UINT8)StrDecimalToUintn (Str);
154
155 Str = StrStr (Str, L".");
156 if (Str == NULL) {
157 return EFI_INVALID_PARAMETER;
158 }
159
160 Ip->v4.Addr[1] = (UINT8)StrDecimalToUintn (++Str);
161
162 Str = StrStr (Str, L".");
163 if (Str == NULL) {
164 return EFI_INVALID_PARAMETER;
165 }
166
167 Ip->v4.Addr[2] = (UINT8)StrDecimalToUintn (++Str);
168
169 Str = StrStr (Str, L".");
170 if (Str == NULL) {
171 return EFI_INVALID_PARAMETER;
172 }
173
174 Ip->v4.Addr[3] = (UINT8)StrDecimalToUintn (++Str);
175 }
176
177 return Status;
178 }
179
180 EFI_STATUS
181 GetHIInputBoolean (
182 OUT BOOLEAN *Value
183 )
184 {
185 CHAR16 CmdBoolean[2];
186 EFI_STATUS Status;
187
188 while(1) {
189 Print (L"[y/n] ");
190 // Set MaxCmdLine to 3 to give space for carriage return (when the user
191 // hits enter) and terminal '\0'.
192 Status = GetHIInputStr (CmdBoolean, 3);
193 if (EFI_ERROR(Status)) {
194 return Status;
195 } else if ((CmdBoolean[0] == L'y') || (CmdBoolean[0] == L'Y')) {
196 if (Value) *Value = TRUE;
197 return EFI_SUCCESS;
198 } else if ((CmdBoolean[0] == L'n') || (CmdBoolean[0] == L'N')) {
199 if (Value) *Value = FALSE;
200 return EFI_SUCCESS;
201 }
202 }
203 }
204
205 BOOLEAN
206 HasFilePathEfiExtension (
207 IN CHAR16* FilePath
208 )
209 {
210 return (StrCmp (FilePath + (StrSize (FilePath) / sizeof (CHAR16)) - 5, L".EFI") == 0) ||
211 (StrCmp (FilePath + (StrSize (FilePath) / sizeof (CHAR16)) - 5, L".efi") == 0);
212 }
213
214 // Return the last non end-type Device Path Node from a Device Path
215 EFI_DEVICE_PATH*
216 GetLastDevicePathNode (
217 IN EFI_DEVICE_PATH* DevicePath
218 )
219 {
220 EFI_DEVICE_PATH* PrevDevicePathNode;
221
222 PrevDevicePathNode = DevicePath;
223 while (!IsDevicePathEndType (DevicePath)) {
224 PrevDevicePathNode = DevicePath;
225 DevicePath = NextDevicePathNode (DevicePath);
226 }
227
228 return PrevDevicePathNode;
229 }
230
231 EFI_STATUS
232 GenerateDeviceDescriptionName (
233 IN EFI_HANDLE Handle,
234 IN OUT CHAR16* Description
235 )
236 {
237 EFI_STATUS Status;
238 EFI_COMPONENT_NAME_PROTOCOL* ComponentName2Protocol;
239 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
240 EFI_DEVICE_PATH_PROTOCOL* DevicePathProtocol;
241 CHAR16* DriverName;
242 CHAR16* DevicePathTxt;
243 EFI_DEVICE_PATH* DevicePathNode;
244
245 ComponentName2Protocol = NULL;
246 Status = gBS->HandleProtocol (Handle, &gEfiComponentName2ProtocolGuid, (VOID **)&ComponentName2Protocol);
247 if (!EFI_ERROR(Status)) {
248 //TODO: Fixme. we must find the best langague
249 Status = ComponentName2Protocol->GetDriverName (ComponentName2Protocol,"en",&DriverName);
250 if (!EFI_ERROR(Status)) {
251 StrnCpy (Description, DriverName, BOOT_DEVICE_DESCRIPTION_MAX);
252 }
253 }
254
255 if (EFI_ERROR(Status)) {
256 // Use the lastest non null entry of the Device path as a description
257 Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePathProtocol);
258 if (EFI_ERROR(Status)) {
259 return Status;
260 }
261
262 // Convert the last non end-type Device Path Node in text for the description
263 DevicePathNode = GetLastDevicePathNode (DevicePathProtocol);
264 Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
265 ASSERT_EFI_ERROR(Status);
266 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (DevicePathNode, TRUE, TRUE);
267 StrnCpy (Description, DevicePathTxt, BOOT_DEVICE_DESCRIPTION_MAX);
268 FreePool (DevicePathTxt);
269 }
270
271 return EFI_SUCCESS;
272 }
273
274 EFI_STATUS
275 BdsStartBootOption (
276 IN CHAR16* BootOption
277 )
278 {
279 EFI_STATUS Status;
280 BDS_LOAD_OPTION *BdsLoadOption;
281
282 Status = BootOptionFromLoadOptionVariable (BootOption, &BdsLoadOption);
283 if (!EFI_ERROR(Status)) {
284 Status = BootOptionStart (BdsLoadOption);
285 FreePool (BdsLoadOption);
286
287 if (!EFI_ERROR(Status)) {
288 Status = EFI_SUCCESS;
289 } else {
290 Status = EFI_NOT_STARTED;
291 }
292 } else {
293 Status = EFI_NOT_FOUND;
294 }
295 return Status;
296 }
297
298 UINTN
299 GetUnalignedDevicePathSize (
300 IN EFI_DEVICE_PATH* DevicePath
301 )
302 {
303 UINTN Size;
304 EFI_DEVICE_PATH* AlignedDevicePath;
305
306 if ((UINTN)DevicePath & 0x1) {
307 AlignedDevicePath = DuplicateDevicePath (DevicePath);
308 Size = GetDevicePathSize (AlignedDevicePath);
309 FreePool (AlignedDevicePath);
310 } else {
311 Size = GetDevicePathSize (DevicePath);
312 }
313 return Size;
314 }
315
316 EFI_DEVICE_PATH*
317 GetAlignedDevicePath (
318 IN EFI_DEVICE_PATH* DevicePath
319 )
320 {
321 if ((UINTN)DevicePath & 0x1) {
322 return DuplicateDevicePath (DevicePath);
323 } else {
324 return DevicePath;
325 }
326 }
327