dcdf499d20e2b2f76cd637fc0cb26487b20147ab
[mirror_edk2.git] / EmbeddedPkg / TemplateBds / BdsEntry.c
1 /** @file
2 The entry of the embedded BDS. This BDS does not follow the Boot Manager requirements
3 of the UEFI specification as it is designed to implement an embedded systmes
4 propriatary boot scheme.
5
6 This template assume a DXE driver produces a SerialIo protocol not using the EFI
7 driver module and it will attempt to connect a console on top of this.
8
9 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
10
11 All rights reserved. This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21 #include "BdsEntry.h"
22
23
24 BOOLEAN gConsolePresent = FALSE;
25
26
27 EFI_HANDLE mBdsImageHandle = NULL;
28 EFI_BDS_ARCH_PROTOCOL gBdsProtocol = {
29 BdsEntry,
30 };
31
32
33
34
35 /**
36 This function uses policy data from the platform to determine what operating
37 system or system utility should be loaded and invoked. This function call
38 also optionally make the use of user input to determine the operating system
39 or system utility to be loaded and invoked. When the DXE Core has dispatched
40 all the drivers on the dispatch queue, this function is called. This
41 function will attempt to connect the boot devices required to load and invoke
42 the selected operating system or system utility. During this process,
43 additional firmware volumes may be discovered that may contain addition DXE
44 drivers that can be dispatched by the DXE Core. If a boot device cannot be
45 fully connected, this function calls the DXE Service Dispatch() to allow the
46 DXE drivers from any newly discovered firmware volumes to be dispatched.
47 Then the boot device connection can be attempted again. If the same boot
48 device connection operation fails twice in a row, then that boot device has
49 failed, and should be skipped. This function should never return.
50
51 @param This The EFI_BDS_ARCH_PROTOCOL instance.
52
53 @return None.
54
55 **/
56 VOID
57 EFIAPI
58 BdsEntry (
59 IN EFI_BDS_ARCH_PROTOCOL *This
60 )
61 {
62 EFI_STATUS Status;
63 UINTN NoHandles;
64 EFI_HANDLE *Buffer;
65 UINTN Index;
66 EFI_HANDLE FvHandle;
67 EFI_GUID *NameGuid;
68
69 //
70 // This code assumes that a DXE driver produces a SerialIo protocol not following the EFI
71 // driver model. At a minimum we need to connect an EFI driver model terminal driver on top
72 // of the serial driver.
73 //
74 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSerialIoProtocolGuid, NULL, &NoHandles, &Buffer);
75 if (!EFI_ERROR (Status)) {
76 for (Index = 0; Index < NoHandles; Index++) {
77 // For every Serial IO protocol in the system connect EFI drivers to it.
78 // This should cause the terminal driver to bind to the Serial IO protocol and produce a
79 // child handle that produces SimpleTextOut & SImpleTextIn protocols
80 gBS->ConnectController (Buffer[Index], NULL, NULL, TRUE);
81 }
82
83 FreePool (Buffer);
84 }
85
86 //
87 // Now we need to setup the EFI System Table with information about the console devices.
88 // This code is normally in the console spliter driver on platforms that support multiple
89 // consoles at the same time
90 //
91 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleTextOutProtocolGuid, NULL, &NoHandles, &Buffer);
92 if (!EFI_ERROR (Status)) {
93 // Use the first SimpleTextOut we find and update the EFI System Table
94 gST->ConsoleOutHandle = Buffer[0];
95 gST->StandardErrorHandle = Buffer[0];
96 Status = gBS->HandleProtocol (Buffer[0], &gEfiSimpleTextOutProtocolGuid, (VOID **)&gST->ConOut);
97 ASSERT_EFI_ERROR (Status);
98
99 gST->StdErr = gST->ConOut;
100
101 FreePool (Buffer);
102
103 gConsolePresent = TRUE;
104 }
105
106 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleTextInProtocolGuid, NULL, &NoHandles, &Buffer);
107 if (!EFI_ERROR (Status)) {
108 // Use the first SimpleTextIn we find and update the EFI System Table
109 gST->ConsoleInHandle = Buffer[0];
110 Status = gBS->HandleProtocol (Buffer[0], &gEfiSimpleTextInProtocolGuid, (VOID **)&gST->ConIn);
111 ASSERT_EFI_ERROR (Status);
112
113 FreePool (Buffer);
114 }
115
116 //
117 // We now have EFI Consoles up and running. Print () will work now. DEBUG () and ASSERT () worked
118 // prior to this point as they were configured to use a more primative output scheme.
119 //
120
121
122 //
123 // Platform specific stuff goes here
124 //
125
126
127 //
128 // Normal UEFI behavior is to process Globally Defined Variables as defined in Chapter 3
129 // (Boot Manager) of the UEFI specification. For this embedded system we don't do this.
130 //
131
132 //
133 // Search all the FVs for an application with a UI Section of Ebl. A .FDF file can be used
134 // to control the names of UI sections in an FV.
135 //
136 Status = FindApplicationMatchingUiSection (L"Ebl", &FvHandle, &NameGuid);
137 if (EFI_ERROR (Status)) {
138 //
139 // Just load the first application we find reguardless of name.
140 // This is the fallback path.
141 //
142 Status = FindApplicationMatchingUiSection (NULL, &FvHandle, &NameGuid);
143 // Nothing to boot
144 ASSERT_EFI_ERROR (Status);
145 }
146
147 Status = LoadPeCoffSectionFromFv (FvHandle, NameGuid);
148
149 //
150 // EFI does not define the bavior if all boot attemps fail and the last one returns.
151 // So we make a policy choice to reset the system since this BDS does not have a UI.
152 //
153 gRT->ResetSystem (EfiResetCold, Status, 0, NULL);
154
155 return ;
156 }
157
158
159 EFI_STATUS
160 EFIAPI
161 BdsInitialize (
162 IN EFI_HANDLE ImageHandle,
163 IN EFI_SYSTEM_TABLE *SystemTable
164 )
165 {
166 EFI_STATUS Status;
167
168 mBdsImageHandle = ImageHandle;
169
170 //
171 // Install protocol interface
172 //
173 Status = gBS->InstallMultipleProtocolInterfaces (
174 &mBdsImageHandle,
175 &gEfiBdsArchProtocolGuid, &gBdsProtocol,
176 NULL
177 );
178 ASSERT_EFI_ERROR (Status);
179
180 return Status;
181 }
182
183