]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Application/LinuxLoader/LinuxConfig.c
ARM Packages: Fixed line endings
[mirror_edk2.git] / ArmPkg / Application / LinuxLoader / LinuxConfig.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 "LinuxInternal.h"
16
17 #define DEFAULT_BOOT_ENTRY_DESCRIPTION L"Linux"
18 #define MAX_STR_INPUT 300
19 #define MAX_ASCII_INPUT 300
20
21 typedef enum {
22 LINUX_LOADER_NEW = 1,
23 LINUX_LOADER_UPDATE
24 } LINUX_LOADER_ACTION;
25
26 STATIC
27 EFI_STATUS
28 EditHIInputStr (
29 IN OUT CHAR16 *CmdLine,
30 IN UINTN MaxCmdLine
31 )
32 {
33 UINTN CmdLineIndex;
34 UINTN WaitIndex;
35 CHAR8 Char;
36 EFI_INPUT_KEY Key;
37 EFI_STATUS Status;
38
39 Print (CmdLine);
40
41 for (CmdLineIndex = StrLen (CmdLine); CmdLineIndex < MaxCmdLine; ) {
42 Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &WaitIndex);
43 ASSERT_EFI_ERROR (Status);
44
45 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
46 ASSERT_EFI_ERROR (Status);
47
48 // Unicode character is valid when Scancode is NUll
49 if (Key.ScanCode == SCAN_NULL) {
50 // Scan code is NUll, hence read Unicode character
51 Char = (CHAR8)Key.UnicodeChar;
52 } else {
53 Char = CHAR_NULL;
54 }
55
56 if ((Char == CHAR_LINEFEED) || (Char == CHAR_CARRIAGE_RETURN) || (Char == 0x7f)) {
57 CmdLine[CmdLineIndex] = '\0';
58 Print (L"\n\r");
59
60 return EFI_SUCCESS;
61 } else if ((Key.UnicodeChar == L'\b') || (Key.ScanCode == SCAN_LEFT) || (Key.ScanCode == SCAN_DELETE)){
62 if (CmdLineIndex != 0) {
63 CmdLineIndex--;
64 Print (L"\b \b");
65 }
66 } else if ((Key.ScanCode == SCAN_ESC) || (Char == 0x1B) || (Char == 0x0)) {
67 return EFI_INVALID_PARAMETER;
68 } else {
69 CmdLine[CmdLineIndex++] = Key.UnicodeChar;
70 Print (L"%c", Key.UnicodeChar);
71 }
72 }
73
74 return EFI_SUCCESS;
75 }
76
77 STATIC
78 EFI_STATUS
79 EditHIInputAscii (
80 IN OUT CHAR8 *CmdLine,
81 IN UINTN MaxCmdLine
82 )
83 {
84 CHAR16* Str;
85 EFI_STATUS Status;
86
87 Str = (CHAR16*)AllocatePool (MaxCmdLine * sizeof(CHAR16));
88 AsciiStrToUnicodeStr (CmdLine, Str);
89
90 Status = EditHIInputStr (Str, MaxCmdLine);
91
92 UnicodeStrToAsciiStr (Str, CmdLine);
93 FreePool (Str);
94
95 return Status;
96 }
97
98 STATIC
99 EFI_STATUS
100 GetHIInputInteger (
101 OUT UINTN *Integer
102 )
103 {
104 CHAR16 CmdLine[255];
105 EFI_STATUS Status;
106
107 CmdLine[0] = '\0';
108 Status = EditHIInputStr (CmdLine, 255);
109 if (!EFI_ERROR(Status)) {
110 *Integer = StrDecimalToUintn (CmdLine);
111 }
112
113 return Status;
114 }
115
116 #if 0
117 EFI_STATUS
118 GenerateDeviceDescriptionName (
119 IN EFI_HANDLE Handle,
120 IN OUT CHAR16* Description
121 )
122 {
123 EFI_STATUS Status;
124 EFI_COMPONENT_NAME_PROTOCOL* ComponentName2Protocol;
125 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
126 EFI_DEVICE_PATH_PROTOCOL* DevicePathProtocol;
127 CHAR16* DriverName;
128 CHAR16* DevicePathTxt;
129 EFI_DEVICE_PATH* DevicePathNode;
130
131 ComponentName2Protocol = NULL;
132 Status = gBS->HandleProtocol (Handle, &gEfiComponentName2ProtocolGuid, (VOID **)&ComponentName2Protocol);
133 if (!EFI_ERROR(Status)) {
134 //TODO: Fixme. we must find the best langague
135 Status = ComponentName2Protocol->GetDriverName (ComponentName2Protocol,"en",&DriverName);
136 if (!EFI_ERROR(Status)) {
137 StrnCpy (Description,DriverName,BOOT_DEVICE_DESCRIPTION_MAX);
138 }
139 }
140
141 if (EFI_ERROR(Status)) {
142 // Use the lastest non null entry of the Device path as a description
143 Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePathProtocol);
144 if (EFI_ERROR(Status)) {
145 return Status;
146 }
147
148 // Convert the last non end-type Device Path Node in text for the description
149 DevicePathNode = GetLastDevicePathNode (DevicePathProtocol);
150 Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
151 ASSERT_EFI_ERROR(Status);
152 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText(DevicePathNode,TRUE,TRUE);
153 StrnCpy (Description, DevicePathTxt, BOOT_DEVICE_DESCRIPTION_MAX);
154 FreePool (DevicePathTxt);
155 }
156
157 return EFI_SUCCESS;
158 }
159 #endif
160
161 EFI_STATUS
162 LinuxLoaderConfig (
163 IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage
164 )
165 {
166 EFI_STATUS Status;
167 LINUX_LOADER_ACTION Choice;
168 UINTN BootOrderSize;
169 UINT16* BootOrder;
170 UINTN BootOrderCount;
171 UINTN Index;
172 CHAR16 Description[MAX_ASCII_INPUT];
173 CHAR8 CmdLine[MAX_ASCII_INPUT];
174 CHAR16 Initrd[MAX_STR_INPUT];
175 UINT16 InitrdPathListLength;
176 UINT16 CmdLineLength;
177 BDS_LOAD_OPTION* BdsLoadOption;
178 BDS_LOAD_OPTION** SupportedBdsLoadOptions;
179 UINTN SupportedBdsLoadOptionCount;
180 LINUX_LOADER_OPTIONAL_DATA* LinuxOptionalData;
181 EFI_DEVICE_PATH* DevicePathRoot;
182
183 SupportedBdsLoadOptions = NULL;
184 SupportedBdsLoadOptionCount = 0;
185
186 do {
187 Print (L"[%d] Create new Linux Boot Entry\n",LINUX_LOADER_NEW);
188 Print (L"[%d] Update Linux Boot Entry\n",LINUX_LOADER_UPDATE);
189
190 Print (L"Option: ");
191 Status = GetHIInputInteger (&Choice);
192 if (Status == EFI_INVALID_PARAMETER) {
193 Print (L"\n");
194 return Status;
195 } else if ((Choice != LINUX_LOADER_NEW) && (Choice != LINUX_LOADER_UPDATE)) {
196 Print (L"Error: the option should be either '%d' or '%d'\n",LINUX_LOADER_NEW,LINUX_LOADER_UPDATE);
197 Status = EFI_INVALID_PARAMETER;
198 }
199 } while (EFI_ERROR(Status));
200
201 if (Choice == LINUX_LOADER_UPDATE) {
202 // If no compatible entry then we just create a new entry
203 Choice = LINUX_LOADER_NEW;
204
205 // Scan the OptionalData of every entry for the correct signature
206 Status = GetEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder);
207 if (!EFI_ERROR(Status)) {
208 BootOrderCount = BootOrderSize / sizeof(UINT16);
209
210 // Allocate an array to handle maximum number of supported Boot Entry
211 SupportedBdsLoadOptions = (BDS_LOAD_OPTION**)AllocatePool(sizeof(BDS_LOAD_OPTION*) * BootOrderCount);
212
213 SupportedBdsLoadOptionCount = 0;
214
215 // Check if the signature is present in the list of the current Boot entries
216 for (Index = 0; Index < BootOrderCount; Index++) {
217 Status = BootOptionFromLoadOptionIndex (BootOrder[Index], &BdsLoadOption);
218 if (!EFI_ERROR(Status)) {
219 if ((BdsLoadOption->OptionalDataSize >= sizeof(UINT32)) &&
220 (*(UINT32*)BdsLoadOption->OptionalData == LINUX_LOADER_SIGNATURE)) {
221 SupportedBdsLoadOptions[SupportedBdsLoadOptionCount++] = BdsLoadOption;
222 Choice = LINUX_LOADER_UPDATE;
223 }
224 }
225 }
226 }
227 FreePool (BootOrder);
228 }
229
230 if (Choice == LINUX_LOADER_NEW) {
231 Description[0] = '\0';
232 CmdLine[0] = '\0';
233 Initrd[0] = '\0';
234
235 BdsLoadOption = (BDS_LOAD_OPTION*)AllocateZeroPool (sizeof(BDS_LOAD_OPTION));
236
237 DEBUG_CODE_BEGIN();
238 CHAR16* DevicePathTxt;
239 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
240
241 Status = gBS->LocateProtocol (&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
242 ASSERT_EFI_ERROR(Status);
243 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (LoadedImage->FilePath, TRUE, TRUE);
244
245 Print(L"EFI OS Loader: %s\n",DevicePathTxt);
246
247 FreePool(DevicePathTxt);
248 DEBUG_CODE_END();
249
250 //
251 // Fill the known fields of BdsLoadOption
252 //
253
254 BdsLoadOption->Attributes = LOAD_OPTION_ACTIVE | LOAD_OPTION_CATEGORY_BOOT;
255
256 // Get the full Device Path for this file
257 Status = gBS->HandleProtocol (LoadedImage->DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePathRoot);
258 ASSERT_EFI_ERROR(Status);
259
260 BdsLoadOption->FilePathList = AppendDevicePath (DevicePathRoot, LoadedImage->FilePath);
261 BdsLoadOption->FilePathListLength = GetDevicePathSize (BdsLoadOption->FilePathList);
262 } else {
263 if (SupportedBdsLoadOptionCount > 1) {
264 for (Index = 0; Index < SupportedBdsLoadOptionCount; Index++) {
265 Print (L"[%d] %s\n",Index + 1,SupportedBdsLoadOptions[Index]->Description);
266 }
267
268 do {
269 Print (L"Update Boot Entry: ");
270 Status = GetHIInputInteger (&Choice);
271 if (Status == EFI_INVALID_PARAMETER) {
272 Print (L"\n");
273 return Status;
274 } else if ((Choice < 1) && (Choice > SupportedBdsLoadOptionCount)) {
275 Print (L"Choose entry from 1 to %d\n",SupportedBdsLoadOptionCount);
276 Status = EFI_INVALID_PARAMETER;
277 }
278 } while (EFI_ERROR(Status));
279 BdsLoadOption = SupportedBdsLoadOptions[Choice-1];
280 }
281 StrnCpy (Description, BdsLoadOption->Description, MAX_STR_INPUT);
282
283 LinuxOptionalData = (LINUX_LOADER_OPTIONAL_DATA*)BdsLoadOption->OptionalData;
284 if (LinuxOptionalData->CmdLineLength > 0) {
285 CopyMem (CmdLine, (CHAR8*)LinuxOptionalData + sizeof(LINUX_LOADER_OPTIONAL_DATA), LinuxOptionalData->CmdLineLength);
286 } else {
287 CmdLine[0] = '\0';
288 }
289
290 if (LinuxOptionalData->InitrdPathListLength > 0) {
291 CopyMem (Initrd, (CHAR8*)LinuxOptionalData + sizeof(LINUX_LOADER_OPTIONAL_DATA) + LinuxOptionalData->CmdLineLength, LinuxOptionalData->InitrdPathListLength);
292 } else {
293 Initrd[0] = L'\0';
294 }
295 DEBUG((EFI_D_ERROR,"L\n"));
296 }
297
298 // Description
299 Print (L"Description: ");
300 Status = EditHIInputStr (Description, MAX_STR_INPUT);
301 if (EFI_ERROR(Status)) {
302 return Status;
303 }
304 if (StrLen (Description) == 0) {
305 StrnCpy (Description, DEFAULT_BOOT_ENTRY_DESCRIPTION, MAX_STR_INPUT);
306 }
307 BdsLoadOption->Description = Description;
308
309 // CmdLine
310 Print (L"Command Line: ");
311 Status = EditHIInputAscii (CmdLine, MAX_ASCII_INPUT);
312 if (EFI_ERROR(Status)) {
313 return Status;
314 }
315
316 // Initrd
317 Print (L"Initrd name: ");
318 Status = EditHIInputStr (Initrd, MAX_STR_INPUT);
319 if (EFI_ERROR(Status)) {
320 return Status;
321 }
322
323 CmdLineLength = AsciiStrLen (CmdLine);
324 if (CmdLineLength > 0) {
325 CmdLineLength += sizeof(CHAR8);
326 }
327
328 InitrdPathListLength = StrLen (Initrd) * sizeof(CHAR16);
329 if (InitrdPathListLength > 0) {
330 InitrdPathListLength += sizeof(CHAR16);
331 }
332
333 BdsLoadOption->OptionalDataSize = sizeof(LINUX_LOADER_OPTIONAL_DATA) + CmdLineLength + InitrdPathListLength;
334
335 LinuxOptionalData = (LINUX_LOADER_OPTIONAL_DATA*)AllocatePool (BdsLoadOption->OptionalDataSize);
336 BdsLoadOption->OptionalData = LinuxOptionalData;
337
338 LinuxOptionalData->Signature = LINUX_LOADER_SIGNATURE;
339 LinuxOptionalData->CmdLineLength = CmdLineLength;
340 LinuxOptionalData->InitrdPathListLength = InitrdPathListLength;
341
342 if (CmdLineLength > 0) {
343 CopyMem (LinuxOptionalData + 1, CmdLine, CmdLineLength);
344 }
345 if (InitrdPathListLength > 0) {
346 CopyMem ((UINT8*)(LinuxOptionalData + 1) + CmdLineLength, Initrd, InitrdPathListLength);
347 }
348
349 // Create or Update the boot entry
350 Status = BootOptionToLoadOptionVariable (BdsLoadOption);
351
352 return Status;
353 }