]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/Bds/BdsHelper.c
AppPkg: Update email and URL.
[mirror_edk2.git] / ArmPlatformPkg / Bds / BdsHelper.c
CommitLineData
1e57a462 1/** @file\r
2*\r
135b09a2 3* Copyright (c) 2011 - 2014, ARM Limited. All rights reserved.\r
1e57a462 4*\r
3402aac7
RC
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
1e57a462 12*\r
13**/\r
14\r
8bf4ad44 15#include <Library/NetLib.h>\r
1e57a462 16#include "BdsInternal.h"\r
17\r
18EFI_STATUS\r
19EditHIInputStr (\r
20 IN OUT CHAR16 *CmdLine,\r
21 IN UINTN MaxCmdLine\r
22 )\r
23{\r
24 UINTN CmdLineIndex;\r
25 UINTN WaitIndex;\r
26 CHAR8 Char;\r
27 EFI_INPUT_KEY Key;\r
28 EFI_STATUS Status;\r
29\r
7ff3b949
RH
30 // The command line must be at least one character long\r
31 ASSERT (MaxCmdLine > 0);\r
32\r
7ff3b949
RH
33 // Ensure the last character of the buffer is the NULL character\r
34 CmdLine[MaxCmdLine - 1] = '\0';\r
35\r
40761653
OM
36 Print (CmdLine);\r
37\r
7ff3b949 38 // To prevent a buffer overflow, we only allow to enter (MaxCmdLine-1) characters\r
62d441fb 39 for (CmdLineIndex = StrLen (CmdLine); CmdLineIndex < MaxCmdLine; ) {\r
1e57a462 40 Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &WaitIndex);\r
41 ASSERT_EFI_ERROR (Status);\r
42\r
43 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
44 ASSERT_EFI_ERROR (Status);\r
45\r
46 // Unicode character is valid when Scancode is NUll\r
47 if (Key.ScanCode == SCAN_NULL) {\r
48 // Scan code is NUll, hence read Unicode character\r
49 Char = (CHAR8)Key.UnicodeChar;\r
50 } else {\r
51 Char = CHAR_NULL;\r
52 }\r
53\r
54 if ((Char == CHAR_LINEFEED) || (Char == CHAR_CARRIAGE_RETURN) || (Char == 0x7f)) {\r
55 CmdLine[CmdLineIndex] = '\0';\r
e30acb47 56 Print (L"\r\n");\r
1e57a462 57\r
58 return EFI_SUCCESS;\r
59 } else if ((Key.UnicodeChar == L'\b') || (Key.ScanCode == SCAN_LEFT) || (Key.ScanCode == SCAN_DELETE)){\r
60 if (CmdLineIndex != 0) {\r
61 CmdLineIndex--;\r
62 Print (L"\b \b");\r
63 }\r
64 } else if ((Key.ScanCode == SCAN_ESC) || (Char == 0x1B) || (Char == 0x0)) {\r
65 return EFI_INVALID_PARAMETER;\r
62d441fb 66 } else if (CmdLineIndex < (MaxCmdLine-1)) {\r
1e57a462 67 CmdLine[CmdLineIndex++] = Key.UnicodeChar;\r
68 Print (L"%c", Key.UnicodeChar);\r
69 }\r
70 }\r
71\r
72 return EFI_SUCCESS;\r
73}\r
74\r
75EFI_STATUS\r
76GetHIInputStr (\r
77 IN OUT CHAR16 *CmdLine,\r
78 IN UINTN MaxCmdLine\r
79 )\r
80{\r
81 EFI_STATUS Status;\r
82\r
83 // For a new input just passed an empty string\r
84 CmdLine[0] = L'\0';\r
85\r
86 Status = EditHIInputStr (CmdLine, MaxCmdLine);\r
87\r
88 return Status;\r
89}\r
90\r
91EFI_STATUS\r
92EditHIInputAscii (\r
93 IN OUT CHAR8 *CmdLine,\r
94 IN UINTN MaxCmdLine\r
95 )\r
96{\r
97 CHAR16* Str;\r
98 EFI_STATUS Status;\r
99\r
100 Str = (CHAR16*)AllocatePool (MaxCmdLine * sizeof(CHAR16));\r
101 AsciiStrToUnicodeStr (CmdLine, Str);\r
102\r
103 Status = EditHIInputStr (Str, MaxCmdLine);\r
104 if (!EFI_ERROR(Status)) {\r
105 UnicodeStrToAsciiStr (Str, CmdLine);\r
106 }\r
107 FreePool (Str);\r
108\r
109 return Status;\r
110}\r
111\r
112EFI_STATUS\r
113GetHIInputAscii (\r
114 IN OUT CHAR8 *CmdLine,\r
115 IN UINTN MaxCmdLine\r
116 )\r
117{\r
118 // For a new input just passed an empty string\r
119 CmdLine[0] = '\0';\r
120\r
121 return EditHIInputAscii (CmdLine,MaxCmdLine);\r
122}\r
123\r
124EFI_STATUS\r
125GetHIInputInteger (\r
126 OUT UINTN *Integer\r
127 )\r
128{\r
129 CHAR16 CmdLine[255];\r
130 EFI_STATUS Status;\r
131\r
132 CmdLine[0] = '\0';\r
133 Status = EditHIInputStr (CmdLine, 255);\r
134 if (!EFI_ERROR(Status)) {\r
135 *Integer = StrDecimalToUintn (CmdLine);\r
136 }\r
137\r
138 return Status;\r
139}\r
140\r
8bf4ad44
RC
141/**\r
142 Get an IPv4 address\r
143\r
144 The function asks the user for an IPv4 address. If the input\r
145 string defines a valid IPv4 address, the four bytes of the\r
146 corresponding IPv4 address are extracted from the string and returned by\r
cf30b996
RC
147 the function. As long as the user does not define a valid IP\r
148 address, he is asked for one. He can always escape by\r
149 pressing ESC.\r
8bf4ad44
RC
150\r
151 @param[out] EFI_IP_ADDRESS OutIpAddr Returned IPv4 address. Valid if\r
152 and only if the returned value\r
153 is equal to EFI_SUCCESS\r
62d441fb 154\r
8bf4ad44
RC
155 @retval EFI_SUCCESS Input completed\r
156 @retval EFI_ABORTED Editing aborted by the user\r
8bf4ad44
RC
157 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to\r
158 lack of resource\r
159**/\r
1e57a462 160EFI_STATUS\r
161GetHIInputIP (\r
8bf4ad44 162 OUT EFI_IP_ADDRESS *OutIpAddr\r
1e57a462 163 )\r
164{\r
1e57a462 165 EFI_STATUS Status;\r
8bf4ad44 166 CHAR16 CmdLine[48];\r
1e57a462 167\r
cf30b996
RC
168 while (TRUE) {\r
169 CmdLine[0] = '\0';\r
170 Status = EditHIInputStr (CmdLine, 48);\r
171 if (EFI_ERROR (Status)) {\r
172 return EFI_ABORTED;\r
173 }\r
1e57a462 174\r
cf30b996
RC
175 Status = NetLibStrToIp4 (CmdLine, &OutIpAddr->v4);\r
176 if (Status == EFI_INVALID_PARAMETER) {\r
177 Print (L"Invalid address\n");\r
178 } else {\r
179 return Status;\r
180 }\r
181 }\r
8bf4ad44 182}\r
1e57a462 183\r
8bf4ad44
RC
184/**\r
185 Edit an IPv4 address\r
186\r
187 The function displays as a string following the "%d.%d.%d.%d" format the\r
188 IPv4 address that is passed in and asks the user to modify it. If the\r
189 resulting string defines a valid IPv4 address, the four bytes of the\r
190 corresponding IPv4 address are extracted from the string and returned by\r
cf30b996
RC
191 the function. As long as the user does not define a valid IP\r
192 address, he is asked for one. He can always escape by\r
193 pressing ESC.\r
8bf4ad44
RC
194\r
195 @param[in ] EFI_IP_ADDRESS InIpAddr Input IPv4 address\r
196 @param[out] EFI_IP_ADDRESS OutIpAddr Returned IPv4 address. Valid if\r
197 and only if the returned value\r
198 is equal to EFI_SUCCESS\r
62d441fb 199\r
8bf4ad44
RC
200 @retval EFI_SUCCESS Update completed\r
201 @retval EFI_ABORTED Editing aborted by the user\r
202 @retval EFI_INVALID_PARAMETER The string returned by the user is\r
203 mal-formated\r
204 @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to\r
205 lack of resource\r
206**/\r
207EFI_STATUS\r
208EditHIInputIP (\r
209 IN EFI_IP_ADDRESS *InIpAddr,\r
210 OUT EFI_IP_ADDRESS *OutIpAddr\r
211 )\r
212{\r
213 EFI_STATUS Status;\r
214 CHAR16 CmdLine[48];\r
1e57a462 215\r
cf30b996
RC
216 while (TRUE) {\r
217 UnicodeSPrint (\r
218 CmdLine, 48, L"%d.%d.%d.%d",\r
219 InIpAddr->v4.Addr[0], InIpAddr->v4.Addr[1],\r
220 InIpAddr->v4.Addr[2], InIpAddr->v4.Addr[3]\r
221 );\r
1e57a462 222\r
cf30b996
RC
223 Status = EditHIInputStr (CmdLine, 48);\r
224 if (EFI_ERROR (Status)) {\r
225 return EFI_ABORTED;\r
226 }\r
227 Status = NetLibStrToIp4 (CmdLine, &OutIpAddr->v4);\r
228 if (Status == EFI_INVALID_PARAMETER) {\r
229 Print (L"Invalid address\n");\r
230 } else {\r
231 return Status;\r
232 }\r
1e57a462 233 }\r
1e57a462 234}\r
235\r
236EFI_STATUS\r
237GetHIInputBoolean (\r
238 OUT BOOLEAN *Value\r
239 )\r
240{\r
241 CHAR16 CmdBoolean[2];\r
242 EFI_STATUS Status;\r
243\r
244 while(1) {\r
245 Print (L"[y/n] ");\r
62d441fb 246 Status = GetHIInputStr (CmdBoolean, 2);\r
1e57a462 247 if (EFI_ERROR(Status)) {\r
248 return Status;\r
249 } else if ((CmdBoolean[0] == L'y') || (CmdBoolean[0] == L'Y')) {\r
250 if (Value) *Value = TRUE;\r
251 return EFI_SUCCESS;\r
252 } else if ((CmdBoolean[0] == L'n') || (CmdBoolean[0] == L'N')) {\r
253 if (Value) *Value = FALSE;\r
254 return EFI_SUCCESS;\r
255 }\r
256 }\r
257}\r
258\r
1e57a462 259// Return the last non end-type Device Path Node from a Device Path\r
260EFI_DEVICE_PATH*\r
261GetLastDevicePathNode (\r
262 IN EFI_DEVICE_PATH* DevicePath\r
263 )\r
264{\r
265 EFI_DEVICE_PATH* PrevDevicePathNode;\r
266\r
267 PrevDevicePathNode = DevicePath;\r
268 while (!IsDevicePathEndType (DevicePath)) {\r
269 PrevDevicePathNode = DevicePath;\r
270 DevicePath = NextDevicePathNode (DevicePath);\r
271 }\r
272\r
273 return PrevDevicePathNode;\r
274}\r
275\r
276EFI_STATUS\r
277GenerateDeviceDescriptionName (\r
278 IN EFI_HANDLE Handle,\r
279 IN OUT CHAR16* Description\r
280 )\r
281{\r
282 EFI_STATUS Status;\r
283 EFI_COMPONENT_NAME_PROTOCOL* ComponentName2Protocol;\r
284 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;\r
285 EFI_DEVICE_PATH_PROTOCOL* DevicePathProtocol;\r
286 CHAR16* DriverName;\r
287 CHAR16* DevicePathTxt;\r
288 EFI_DEVICE_PATH* DevicePathNode;\r
289\r
290 ComponentName2Protocol = NULL;\r
291 Status = gBS->HandleProtocol (Handle, &gEfiComponentName2ProtocolGuid, (VOID **)&ComponentName2Protocol);\r
292 if (!EFI_ERROR(Status)) {\r
293 //TODO: Fixme. we must find the best langague\r
294 Status = ComponentName2Protocol->GetDriverName (ComponentName2Protocol,"en",&DriverName);\r
295 if (!EFI_ERROR(Status)) {\r
296 StrnCpy (Description, DriverName, BOOT_DEVICE_DESCRIPTION_MAX);\r
297 }\r
298 }\r
299\r
300 if (EFI_ERROR(Status)) {\r
301 // Use the lastest non null entry of the Device path as a description\r
302 Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePathProtocol);\r
303 if (EFI_ERROR(Status)) {\r
304 return Status;\r
305 }\r
306\r
307 // Convert the last non end-type Device Path Node in text for the description\r
308 DevicePathNode = GetLastDevicePathNode (DevicePathProtocol);\r
309 Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);\r
310 ASSERT_EFI_ERROR(Status);\r
311 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (DevicePathNode, TRUE, TRUE);\r
312 StrnCpy (Description, DevicePathTxt, BOOT_DEVICE_DESCRIPTION_MAX);\r
313 FreePool (DevicePathTxt);\r
314 }\r
315\r
316 return EFI_SUCCESS;\r
317}\r
318\r
319EFI_STATUS\r
320BdsStartBootOption (\r
321 IN CHAR16* BootOption\r
322 )\r
323{\r
324 EFI_STATUS Status;\r
325 BDS_LOAD_OPTION *BdsLoadOption;\r
326\r
327 Status = BootOptionFromLoadOptionVariable (BootOption, &BdsLoadOption);\r
328 if (!EFI_ERROR(Status)) {\r
329 Status = BootOptionStart (BdsLoadOption);\r
330 FreePool (BdsLoadOption);\r
331\r
332 if (!EFI_ERROR(Status)) {\r
333 Status = EFI_SUCCESS;\r
334 } else {\r
335 Status = EFI_NOT_STARTED;\r
336 }\r
337 } else {\r
338 Status = EFI_NOT_FOUND;\r
339 }\r
340 return Status;\r
341}\r
342\r
343UINTN\r
344GetUnalignedDevicePathSize (\r
345 IN EFI_DEVICE_PATH* DevicePath\r
346 )\r
347{\r
348 UINTN Size;\r
349 EFI_DEVICE_PATH* AlignedDevicePath;\r
350\r
351 if ((UINTN)DevicePath & 0x1) {\r
352 AlignedDevicePath = DuplicateDevicePath (DevicePath);\r
353 Size = GetDevicePathSize (AlignedDevicePath);\r
354 FreePool (AlignedDevicePath);\r
355 } else {\r
356 Size = GetDevicePathSize (DevicePath);\r
357 }\r
358 return Size;\r
359}\r
360\r
361EFI_DEVICE_PATH*\r
362GetAlignedDevicePath (\r
363 IN EFI_DEVICE_PATH* DevicePath\r
364 )\r
365{\r
366 if ((UINTN)DevicePath & 0x1) {\r
367 return DuplicateDevicePath (DevicePath);\r
368 } else {\r
369 return DevicePath;\r
370 }\r
371}\r
372\r
9fc9aa46
OM
373BOOLEAN\r
374IsUnicodeString (\r
375 IN VOID* String\r
376 )\r
377{\r
378 // We do not support NULL pointer\r
379 ASSERT (String != NULL);\r
380\r
381 if (*(CHAR16*)String < 0x100) {\r
382 //Note: We could get issue if the string is an empty Ascii string...\r
383 return TRUE;\r
384 } else {\r
385 return FALSE;\r
386 }\r
387}\r
135b09a2
OM
388\r
389/*\r
390 * Try to detect if the given string is an ASCII or Unicode string\r
391 *\r
392 * There are actually few limitation to this function but it is mainly to give\r
393 * a user friendly output.\r
394 *\r
395 * Some limitations:\r
396 * - it only supports unicode string that use ASCII character (< 0x100)\r
397 * - single character ASCII strings are interpreted as Unicode string\r
947a10b1
RC
398 * - string cannot be longer than BOOT_DEVICE_OPTION_MAX characters and\r
399 * thus (BOOT_DEVICE_OPTION_MAX*2) bytes for an Unicode string and\r
400 * BOOT_DEVICE_OPTION_MAX bytes for an ASCII string.\r
135b09a2
OM
401 *\r
402 * @param String Buffer that might contain a Unicode or Ascii string\r
403 * @param IsUnicode If not NULL this boolean value returns if the string is an\r
404 * ASCII or Unicode string.\r
405 */\r
406BOOLEAN\r
407IsPrintableString (\r
408 IN VOID* String,\r
409 OUT BOOLEAN *IsUnicode\r
410 )\r
411{\r
412 BOOLEAN UnicodeDetected;\r
413 BOOLEAN IsPrintable;\r
414 UINTN Index;\r
415 CHAR16 Character;\r
416\r
417 // We do not support NULL pointer\r
418 ASSERT (String != NULL);\r
419\r
420 // Test empty string\r
421 if (*(CHAR16*)String == L'\0') {\r
422 if (IsUnicode) {\r
423 *IsUnicode = TRUE;\r
424 }\r
425 return TRUE;\r
426 } else if (*(CHAR16*)String == '\0') {\r
427 if (IsUnicode) {\r
428 *IsUnicode = FALSE;\r
429 }\r
430 return TRUE;\r
431 }\r
432\r
433 // Limitation: if the string is an ASCII single character string. This comparison\r
434 // will assume it is a Unicode string.\r
435 if (*(CHAR16*)String < 0x100) {\r
436 UnicodeDetected = TRUE;\r
437 } else {\r
438 UnicodeDetected = FALSE;\r
439 }\r
440\r
441 IsPrintable = FALSE;\r
947a10b1 442 for (Index = 0; Index < BOOT_DEVICE_OPTION_MAX; Index++) {\r
135b09a2
OM
443 if (UnicodeDetected) {\r
444 Character = ((CHAR16*)String)[Index];\r
445 } else {\r
446 Character = ((CHAR8*)String)[Index];\r
447 }\r
448\r
449 if (Character == '\0') {\r
450 // End of the string\r
451 IsPrintable = TRUE;\r
452 break;\r
453 } else if ((Character < 0x20) || (Character > 0x7f)) {\r
454 // We only support the range of printable ASCII character\r
455 IsPrintable = FALSE;\r
456 break;\r
457 }\r
458 }\r
459\r
460 if (IsPrintable && IsUnicode) {\r
461 *IsUnicode = UnicodeDetected;\r
462 }\r
463\r
464 return IsPrintable;\r
465}\r