ArmPlatformPkg: Minor code changes (comments, misspellings, coding stylei, line endings)
[mirror_edk2.git] / ArmPlatformPkg / Bds / BdsHelper.c
CommitLineData
ea46ebbe 1/** @file
2*
0db25ccc 3* Copyright (c) 2011-2012, ARM Limited. All rights reserved.
ea46ebbe 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
ea46ebbe 17EFI_STATUS
74b96132 18EditHIInputStr (
19 IN OUT CHAR16 *CmdLine,
ea46ebbe 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
74b96132 29 Print (CmdLine);
ea46ebbe 30
74b96132 31 for (CmdLineIndex = StrLen (CmdLine); CmdLineIndex < MaxCmdLine; ) {
ea46ebbe 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';
74b96132 48 Print (L"\n\r");
ea46ebbe 49
50 return EFI_SUCCESS;
74b96132 51 } else if ((Key.UnicodeChar == L'\b') || (Key.ScanCode == SCAN_LEFT) || (Key.ScanCode == SCAN_DELETE)){
ea46ebbe 52 if (CmdLineIndex != 0) {
53 CmdLineIndex--;
74b96132 54 Print (L"\b \b");
ea46ebbe 55 }
56 } else if ((Key.ScanCode == SCAN_ESC) || (Char == 0x1B) || (Char == 0x0)) {
57 return EFI_INVALID_PARAMETER;
58 } else {
74b96132 59 CmdLine[CmdLineIndex++] = Key.UnicodeChar;
60 Print (L"%c", Key.UnicodeChar);
ea46ebbe 61 }
62 }
63
64 return EFI_SUCCESS;
65}
66
74b96132 67EFI_STATUS
68GetHIInputStr (
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
83EFI_STATUS
84EditHIInputAscii (
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);
74b96132 96 if (!EFI_ERROR(Status)) {
97 UnicodeStrToAsciiStr (Str, CmdLine);
98 }
99 FreePool (Str);
100
101 return Status;
102}
103
ea46ebbe 104EFI_STATUS
105GetHIInputAscii (
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
116EFI_STATUS
117GetHIInputInteger (
118 OUT UINTN *Integer
119 )
120{
74b96132 121 CHAR16 CmdLine[255];
ea46ebbe 122 EFI_STATUS Status;
123
124 CmdLine[0] = '\0';
74b96132 125 Status = EditHIInputStr (CmdLine, 255);
ea46ebbe 126 if (!EFI_ERROR(Status)) {
74b96132 127 *Integer = StrDecimalToUintn (CmdLine);
ea46ebbe 128 }
129
130 return Status;
131}
132
133EFI_STATUS
134GetHIInputIP (
135 OUT EFI_IP_ADDRESS *Ip
136 )
137{
74b96132 138 CHAR16 CmdLine[255];
139 CHAR16 *Str;
ea46ebbe 140 EFI_STATUS Status;
141
142 CmdLine[0] = '\0';
74b96132 143 Status = EditHIInputStr (CmdLine,255);
ea46ebbe 144 if (!EFI_ERROR(Status)) {
145 Str = CmdLine;
74b96132 146 Ip->v4.Addr[0] = (UINT8)StrDecimalToUintn (Str);
ea46ebbe 147
74b96132 148 Str = StrStr (Str, L".");
ea46ebbe 149 if (Str == NULL) {
150 return EFI_INVALID_PARAMETER;
151 }
152
74b96132 153 Ip->v4.Addr[1] = (UINT8)StrDecimalToUintn (++Str);
ea46ebbe 154
74b96132 155 Str = StrStr (Str, L".");
ea46ebbe 156 if (Str == NULL) {
157 return EFI_INVALID_PARAMETER;
158 }
159
74b96132 160 Ip->v4.Addr[2] = (UINT8)StrDecimalToUintn (++Str);
ea46ebbe 161
74b96132 162 Str = StrStr (Str, L".");
ea46ebbe 163 if (Str == NULL) {
164 return EFI_INVALID_PARAMETER;
165 }
166
74b96132 167 Ip->v4.Addr[3] = (UINT8)StrDecimalToUintn (++Str);
ea46ebbe 168 }
169
170 return Status;
171}
172
173EFI_STATUS
174GetHIInputBoolean (
175 OUT BOOLEAN *Value
176 )
177{
74b96132 178 CHAR16 CmdBoolean[2];
ea46ebbe 179 EFI_STATUS Status;
180
181 while(1) {
182 Print (L"[y/n] ");
793275a8 183 Status = GetHIInputStr (CmdBoolean, 2);
ea46ebbe 184 if (EFI_ERROR(Status)) {
185 return Status;
74b96132 186 } else if ((CmdBoolean[0] == L'y') || (CmdBoolean[0] == L'Y')) {
ea46ebbe 187 if (Value) *Value = TRUE;
188 return EFI_SUCCESS;
74b96132 189 } else if ((CmdBoolean[0] == L'n') || (CmdBoolean[0] == L'N')) {
ea46ebbe 190 if (Value) *Value = FALSE;
191 return EFI_SUCCESS;
192 }
193 }
194}
195
196BOOLEAN
197HasFilePathEfiExtension (
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
205EFI_DEVICE_PATH*
206GetLastDevicePathNode (
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
221EFI_STATUS
222GenerateDeviceDescriptionName (
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)) {
11c20f4e 241 StrnCpy (Description, DriverName, BOOT_DEVICE_DESCRIPTION_MAX);
ea46ebbe 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);
11c20f4e 256 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (DevicePathNode, TRUE, TRUE);
ea46ebbe 257 StrnCpy (Description, DevicePathTxt, BOOT_DEVICE_DESCRIPTION_MAX);
258 FreePool (DevicePathTxt);
259 }
260
261 return EFI_SUCCESS;
262}
263
264EFI_STATUS
265BdsStartBootOption (
266 IN CHAR16* BootOption
267 )
268{
269 EFI_STATUS Status;
ea46ebbe 270 BDS_LOAD_OPTION *BdsLoadOption;
271
4aa24170 272 Status = BootOptionFromLoadOptionVariable (BootOption, &BdsLoadOption);
ea46ebbe 273 if (!EFI_ERROR(Status)) {
4aa24170 274 Status = BootOptionStart (BdsLoadOption);
275 FreePool (BdsLoadOption);
ea46ebbe 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}
2ccfb71e 287
288UINTN
289GetUnalignedDevicePathSize (
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
306EFI_DEVICE_PATH*
307GetAlignedDevicePath (
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