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