]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Bds/Bds.c
ArmPkg/BdsLib: Fixed memory leak
[mirror_edk2.git] / ArmPlatformPkg / Bds / Bds.c
1 /** @file
2 *
3 * Copyright (c) 2011, 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 #include <Library/PcdLib.h>
18 #include <Library/PerformanceLib.h>
19
20 #include <Protocol/Bds.h>
21
22 #define EFI_SET_TIMER_TO_SECOND 10000000
23
24 EFI_HANDLE mImageHandle;
25
26 STATIC
27 EFI_STATUS
28 GetConsoleDevicePathFromVariable (
29 IN CHAR16* ConsoleVarName,
30 IN CHAR16* DefaultConsolePaths,
31 OUT EFI_DEVICE_PATH** DevicePaths
32 )
33 {
34 EFI_STATUS Status;
35 UINTN Size;
36 EFI_DEVICE_PATH_PROTOCOL* DevicePathInstances;
37 EFI_DEVICE_PATH_PROTOCOL* DevicePathInstance;
38 CHAR16* DevicePathStr;
39 CHAR16* NextDevicePathStr;
40 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol;
41
42 Status = GetEnvironmentVariable (ConsoleVarName, NULL, NULL, (VOID**)&DevicePathInstances);
43 if (EFI_ERROR(Status)) {
44 // In case no default console device path has been defined we assume a driver handles the console (eg: SimpleTextInOutSerial)
45 if ((DefaultConsolePaths == NULL) || (DefaultConsolePaths[0] == L'\0')) {
46 *DevicePaths = NULL;
47 return EFI_SUCCESS;
48 }
49
50 Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol);
51 ASSERT_EFI_ERROR(Status);
52
53 DevicePathInstances = NULL;
54
55 // Extract the Device Path instances from the multi-device path string
56 while ((DefaultConsolePaths != NULL) && (DefaultConsolePaths[0] != L'\0')) {
57 NextDevicePathStr = StrStr (DefaultConsolePaths, L";");
58 if (NextDevicePathStr == NULL) {
59 DevicePathStr = DefaultConsolePaths;
60 DefaultConsolePaths = NULL;
61 } else {
62 DevicePathStr = (CHAR16*)AllocateCopyPool ((NextDevicePathStr - DefaultConsolePaths + 1) * sizeof(CHAR16), DefaultConsolePaths);
63 *(DevicePathStr + (NextDevicePathStr - DefaultConsolePaths)) = L'\0';
64 DefaultConsolePaths = NextDevicePathStr;
65 if (DefaultConsolePaths[0] == L';') {
66 DefaultConsolePaths++;
67 }
68 }
69
70 DevicePathInstance = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath (DevicePathStr);
71 ASSERT(DevicePathInstance != NULL);
72 DevicePathInstances = AppendDevicePathInstance (DevicePathInstances, DevicePathInstance);
73
74 if (NextDevicePathStr != NULL) {
75 FreePool (DevicePathStr);
76 }
77 FreePool (DevicePathInstance);
78 }
79
80 // Set the environment variable with this device path multi-instances
81 Size = GetDevicePathSize (DevicePathInstances);
82 if (Size > 0) {
83 gRT->SetVariable (
84 ConsoleVarName,
85 &gEfiGlobalVariableGuid,
86 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
87 Size,
88 DevicePathInstances
89 );
90 } else {
91 Status = EFI_INVALID_PARAMETER;
92 }
93 }
94
95 if (!EFI_ERROR(Status)) {
96 *DevicePaths = DevicePathInstances;
97 }
98 return Status;
99 }
100
101 STATIC
102 EFI_STATUS
103 InitializeConsolePipe (
104 IN EFI_DEVICE_PATH *ConsoleDevicePaths,
105 IN EFI_GUID *Protocol,
106 OUT EFI_HANDLE *Handle,
107 OUT VOID* *Interface
108 )
109 {
110 EFI_STATUS Status;
111 UINTN Size;
112 UINTN NoHandles;
113 EFI_HANDLE *Buffer;
114 EFI_DEVICE_PATH_PROTOCOL* DevicePath;
115
116 // Connect all the Device Path Consoles
117 while (ConsoleDevicePaths != NULL) {
118 DevicePath = GetNextDevicePathInstance (&ConsoleDevicePaths, &Size);
119
120 Status = BdsConnectDevicePath (DevicePath, Handle, NULL);
121 DEBUG_CODE_BEGIN();
122 if (EFI_ERROR(Status)) {
123 // We convert back to the text representation of the device Path
124 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
125 CHAR16* DevicePathTxt;
126 EFI_STATUS Status;
127
128 Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
129 if (!EFI_ERROR(Status)) {
130 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (DevicePath, TRUE, TRUE);
131
132 DEBUG((EFI_D_ERROR,"Fail to start the console with the Device Path '%s'. (Error '%r')\n", DevicePathTxt, Status));
133
134 FreePool (DevicePathTxt);
135 }
136 }
137 DEBUG_CODE_END();
138
139 // If the console splitter driver is not supported by the platform then use the first Device Path
140 // instance for the console interface.
141 if (!EFI_ERROR(Status) && (*Interface == NULL)) {
142 Status = gBS->HandleProtocol (*Handle, Protocol, Interface);
143 }
144 }
145
146 // No Device Path has been defined for this console interface. We take the first protocol implementation
147 if (*Interface == NULL) {
148 Status = gBS->LocateHandleBuffer (ByProtocol, Protocol, NULL, &NoHandles, &Buffer);
149 if (EFI_ERROR (Status)) {
150 BdsConnectAllDrivers();
151 Status = gBS->LocateHandleBuffer (ByProtocol, Protocol, NULL, &NoHandles, &Buffer);
152 }
153
154 if (!EFI_ERROR(Status)) {
155 *Handle = Buffer[0];
156 Status = gBS->HandleProtocol (*Handle, Protocol, Interface);
157 ASSERT_EFI_ERROR(Status);
158 }
159 FreePool (Buffer);
160 } else {
161 Status = EFI_SUCCESS;
162 }
163
164 return Status;
165 }
166
167 EFI_STATUS
168 InitializeConsole (
169 VOID
170 )
171 {
172 EFI_STATUS Status;
173 EFI_DEVICE_PATH* ConOutDevicePaths;
174 EFI_DEVICE_PATH* ConInDevicePaths;
175 EFI_DEVICE_PATH* ConErrDevicePaths;
176
177 // By getting the Console Device Paths from the environment variables before initializing the console pipe, we
178 // create the 3 environment variables (ConIn, ConOut, ConErr) that allows to initialize all the console interface
179 // of newly installed console drivers
180 Status = GetConsoleDevicePathFromVariable (L"ConOut", (CHAR16*)PcdGetPtr(PcdDefaultConOutPaths), &ConOutDevicePaths);
181 ASSERT_EFI_ERROR (Status);
182 Status = GetConsoleDevicePathFromVariable (L"ConIn", (CHAR16*)PcdGetPtr(PcdDefaultConInPaths), &ConInDevicePaths);
183 ASSERT_EFI_ERROR (Status);
184 Status = GetConsoleDevicePathFromVariable (L"ConErr", (CHAR16*)PcdGetPtr(PcdDefaultConOutPaths), &ConErrDevicePaths);
185 ASSERT_EFI_ERROR (Status);
186
187 // Initialize the Consoles
188 Status = InitializeConsolePipe (ConOutDevicePaths, &gEfiSimpleTextOutProtocolGuid, &gST->ConsoleOutHandle, (VOID **)&gST->ConOut);
189 ASSERT_EFI_ERROR (Status);
190 Status = InitializeConsolePipe (ConInDevicePaths, &gEfiSimpleTextInProtocolGuid, &gST->ConsoleInHandle, (VOID **)&gST->ConIn);
191 ASSERT_EFI_ERROR (Status);
192 Status = InitializeConsolePipe (ConErrDevicePaths, &gEfiSimpleTextOutProtocolGuid, &gST->StandardErrorHandle, (VOID **)&gST->StdErr);
193 if (EFI_ERROR(Status)) {
194 // In case of error, we reuse the console output for the error output
195 gST->StandardErrorHandle = gST->ConsoleOutHandle;
196 gST->StdErr = gST->ConOut;
197 }
198
199 return EFI_SUCCESS;
200 }
201
202 EFI_STATUS
203 DefineDefaultBootEntries (
204 VOID
205 )
206 {
207 BDS_LOAD_OPTION* BdsLoadOption;
208 UINTN Size;
209 EFI_STATUS Status;
210 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL* EfiDevicePathFromTextProtocol;
211 EFI_DEVICE_PATH* BootDevicePath;
212 ARM_BDS_LOADER_ARGUMENTS* BootArguments;
213 ARM_BDS_LOADER_TYPE BootType;
214 EFI_DEVICE_PATH* InitrdPath;
215 UINTN CmdLineSize;
216 UINTN InitrdSize;
217
218 //
219 // If Boot Order does not exist then create a default entry
220 //
221 Size = 0;
222 Status = gRT->GetVariable (L"BootOrder", &gEfiGlobalVariableGuid, NULL, &Size, NULL);
223 if (Status == EFI_NOT_FOUND) {
224 if ((PcdGetPtr(PcdDefaultBootDevicePath) == NULL) || (StrLen ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath)) == 0)) {
225 return EFI_UNSUPPORTED;
226 }
227
228 Status = gBS->LocateProtocol (&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID **)&EfiDevicePathFromTextProtocol);
229 if (EFI_ERROR(Status)) {
230 // You must provide an implementation of DevicePathFromTextProtocol in your firmware (eg: DevicePathDxe)
231 DEBUG((EFI_D_ERROR,"Error: Bds requires DevicePathFromTextProtocol\n"));
232 return Status;
233 }
234 BootDevicePath = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath));
235
236 DEBUG_CODE_BEGIN();
237 // We convert back to the text representation of the device Path to see if the initial text is correct
238 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* DevicePathToTextProtocol;
239 CHAR16* DevicePathTxt;
240
241 Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevicePathToTextProtocol);
242 ASSERT_EFI_ERROR(Status);
243 DevicePathTxt = DevicePathToTextProtocol->ConvertDevicePathToText (BootDevicePath, TRUE, TRUE);
244
245 ASSERT (StrCmp ((CHAR16*)PcdGetPtr(PcdDefaultBootDevicePath), DevicePathTxt) == 0);
246
247 FreePool (DevicePathTxt);
248 DEBUG_CODE_END();
249
250 // Create the entry is the Default values are correct
251 if (BootDevicePath != NULL) {
252 BootType = (ARM_BDS_LOADER_TYPE)PcdGet32 (PcdDefaultBootType);
253
254 if ((BootType == BDS_LOADER_KERNEL_LINUX_ATAG) || (BootType == BDS_LOADER_KERNEL_LINUX_FDT)) {
255 CmdLineSize = AsciiStrSize ((CHAR8*)PcdGetPtr(PcdDefaultBootArgument));
256 InitrdPath = EfiDevicePathFromTextProtocol->ConvertTextToDevicePath ((CHAR16*)PcdGetPtr(PcdDefaultBootInitrdPath));
257 InitrdSize = GetDevicePathSize (InitrdPath);
258
259 BootArguments = (ARM_BDS_LOADER_ARGUMENTS*)AllocatePool (sizeof(ARM_BDS_LOADER_ARGUMENTS) + CmdLineSize + InitrdSize);
260 BootArguments->LinuxArguments.CmdLineSize = CmdLineSize;
261 BootArguments->LinuxArguments.InitrdSize = InitrdSize;
262
263 CopyMem ((VOID*)(BootArguments + 1), (CHAR8*)PcdGetPtr(PcdDefaultBootArgument), CmdLineSize);
264 CopyMem ((VOID*)((UINTN)(BootArguments + 1) + CmdLineSize), InitrdPath, InitrdSize);
265 } else {
266 BootArguments = NULL;
267 }
268
269 BootOptionCreate (LOAD_OPTION_ACTIVE | LOAD_OPTION_CATEGORY_BOOT,
270 (CHAR16*)PcdGetPtr(PcdDefaultBootDescription),
271 BootDevicePath,
272 BootType,
273 BootArguments,
274 &BdsLoadOption
275 );
276 FreePool (BdsLoadOption);
277 } else {
278 Status = EFI_UNSUPPORTED;
279 }
280 }
281
282 return EFI_SUCCESS;
283 }
284
285 EFI_STATUS
286 StartDefaultBootOnTimeout (
287 VOID
288 )
289 {
290 UINTN Size;
291 UINT16 Timeout;
292 UINT16 *TimeoutPtr;
293 EFI_EVENT WaitList[2];
294 UINTN WaitIndex;
295 UINT16 *BootOrder;
296 UINTN BootOrderSize;
297 UINTN Index;
298 CHAR16 BootVariableName[9];
299 EFI_STATUS Status;
300 EFI_INPUT_KEY Key;
301
302 Size = sizeof(UINT16);
303 Timeout = (UINT16)PcdGet16 (PcdPlatformBootTimeOut);
304 TimeoutPtr = &Timeout;
305 GetEnvironmentVariable (L"Timeout", &Timeout, &Size, (VOID**)&TimeoutPtr);
306
307 if (Timeout != 0xFFFF) {
308 if (Timeout > 0) {
309 // Create the waiting events (keystroke and 1sec timer)
310 gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &WaitList[0]);
311 gBS->SetTimer (WaitList[0], TimerPeriodic, EFI_SET_TIMER_TO_SECOND);
312 WaitList[1] = gST->ConIn->WaitForKey;
313
314 // Start the timer
315 WaitIndex = 0;
316 Print(L"The default boot selection will start in ");
317 while ((Timeout > 0) && (WaitIndex == 0)) {
318 Print(L"%3d seconds",Timeout);
319 gBS->WaitForEvent (2, WaitList, &WaitIndex);
320 if (WaitIndex == 0) {
321 Print(L"\b\b\b\b\b\b\b\b\b\b\b");
322 Timeout--;
323 }
324 }
325 // Discard key in the buffer
326 do {
327 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
328 } while(!EFI_ERROR(Status));
329 gBS->CloseEvent (WaitList[0]);
330 Print(L"\n\r");
331 }
332
333 // In case of Timeout we start the default boot selection
334 if (Timeout == 0) {
335 // Get the Boot Option Order from the environment variable (a default value should have been created)
336 GetEnvironmentVariable (L"BootOrder", NULL, &BootOrderSize, (VOID**)&BootOrder);
337
338 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
339 UnicodeSPrint (BootVariableName, 9 * sizeof(CHAR16), L"Boot%04X", BootOrder[Index]);
340 Status = BdsStartBootOption (BootVariableName);
341 if(!EFI_ERROR(Status)){
342 // Boot option returned successfully, hence don't need to start next boot option
343 break;
344 }
345 // In case of success, we should not return from this call.
346 }
347 }
348 }
349 return EFI_SUCCESS;
350 }
351
352 /**
353 This function uses policy data from the platform to determine what operating
354 system or system utility should be loaded and invoked. This function call
355 also optionally make the use of user input to determine the operating system
356 or system utility to be loaded and invoked. When the DXE Core has dispatched
357 all the drivers on the dispatch queue, this function is called. This
358 function will attempt to connect the boot devices required to load and invoke
359 the selected operating system or system utility. During this process,
360 additional firmware volumes may be discovered that may contain addition DXE
361 drivers that can be dispatched by the DXE Core. If a boot device cannot be
362 fully connected, this function calls the DXE Service Dispatch() to allow the
363 DXE drivers from any newly discovered firmware volumes to be dispatched.
364 Then the boot device connection can be attempted again. If the same boot
365 device connection operation fails twice in a row, then that boot device has
366 failed, and should be skipped. This function should never return.
367
368 @param This The EFI_BDS_ARCH_PROTOCOL instance.
369
370 @return None.
371
372 **/
373 VOID
374 EFIAPI
375 BdsEntry (
376 IN EFI_BDS_ARCH_PROTOCOL *This
377 )
378 {
379 UINTN Size;
380 EFI_STATUS Status;
381
382 PERF_END (NULL, "DXE", NULL, 0);
383
384 //
385 // Declare the Firmware Vendor
386 //
387 if (FixedPcdGetPtr(PcdFirmwareVendor) != NULL) {
388 Size = 0x100;
389 gST->FirmwareVendor = AllocateRuntimePool (Size);
390 ASSERT (gST->FirmwareVendor != NULL);
391 UnicodeSPrint (gST->FirmwareVendor, Size, L"%a EFI %a %a", PcdGetPtr(PcdFirmwareVendor), __DATE__, __TIME__);
392 }
393
394 // If BootNext environment variable is defined then we just load it !
395 Status = BdsStartBootOption (L"BootNext");
396 if (Status != EFI_NOT_FOUND) {
397 // BootNext has not been succeeded launched
398 if (EFI_ERROR(Status)) {
399 Print(L"Fail to start BootNext.\n");
400 }
401
402 // Delete the BootNext environment variable
403 gRT->SetVariable (L"BootNext", &gEfiGlobalVariableGuid,
404 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
405 0, NULL);
406 }
407
408 // If Boot Order does not exist then create a default entry
409 DefineDefaultBootEntries ();
410
411 // Now we need to setup the EFI System Table with information about the console devices.
412 InitializeConsole ();
413
414 // Timer before initiating the default boot selection
415 StartDefaultBootOnTimeout ();
416
417 // Start the Boot Menu
418 Status = BootMenuMain ();
419 ASSERT_EFI_ERROR (Status);
420
421 }
422
423 EFI_BDS_ARCH_PROTOCOL gBdsProtocol = {
424 BdsEntry,
425 };
426
427 EFI_STATUS
428 EFIAPI
429 BdsInitialize (
430 IN EFI_HANDLE ImageHandle,
431 IN EFI_SYSTEM_TABLE *SystemTable
432 )
433 {
434 EFI_STATUS Status;
435
436 mImageHandle = ImageHandle;
437
438 Status = gBS->InstallMultipleProtocolInterfaces (
439 &ImageHandle,
440 &gEfiBdsArchProtocolGuid, &gBdsProtocol,
441 NULL
442 );
443 ASSERT_EFI_ERROR (Status);
444
445 return Status;
446 }