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