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