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.<BR> 10 11 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 EFI_BDS_ARCH_PROTOCOL gBdsProtocol = { 27 BdsEntry, 28 }; 29 30 31 32 33 /** 34 This function uses policy data from the platform to determine what operating 35 system or system utility should be loaded and invoked. This function call 36 also optionally make the use of user input to determine the operating system 37 or system utility to be loaded and invoked. When the DXE Core has dispatched 38 all the drivers on the dispatch queue, this function is called. This 39 function will attempt to connect the boot devices required to load and invoke 40 the selected operating system or system utility. During this process, 41 additional firmware volumes may be discovered that may contain addition DXE 42 drivers that can be dispatched by the DXE Core. If a boot device cannot be 43 fully connected, this function calls the DXE Service Dispatch() to allow the 44 DXE drivers from any newly discovered firmware volumes to be dispatched. 45 Then the boot device connection can be attempted again. If the same boot 46 device connection operation fails twice in a row, then that boot device has 47 failed, and should be skipped. This function should never return. 48 49 @param This The EFI_BDS_ARCH_PROTOCOL instance. 50 51 @return None. 52 53 **/ 54 VOID 55 EFIAPI 56 BdsEntry ( 57 IN EFI_BDS_ARCH_PROTOCOL *This 58 ) 59 { 60 EFI_STATUS Status; 61 UINTN NoHandles; 62 EFI_HANDLE *Buffer; 63 EFI_HANDLE FvHandle; 64 EFI_HANDLE ImageHandle; 65 EFI_HANDLE UsbDeviceHandle; 66 EFI_GUID NameGuid; 67 UINTN Size; 68 UINTN HandleCount; 69 UINTN OldHandleCount; 70 EFI_HANDLE *HandleBuffer; 71 UINTN Index; 72 EFI_DEVICE_PATH_PROTOCOL *LoadImageDevicePath; 73 EFI_DEVICE_PATH_PROTOCOL *FileSystemDevicePath; 74 75 PERF_END (NULL, "DXE", NULL, 0); 76 PERF_START (NULL, "BDS", NULL, 0); 77 78 79 // 80 // Now do the EFI stuff 81 // 82 Size = 0x100; 83 gST->FirmwareVendor = AllocateRuntimePool (Size); 84 ASSERT (gST->FirmwareVendor != NULL); 85 86 UnicodeSPrint (gST->FirmwareVendor, Size, L"BeagleBoard EFI %a %a", __DATE__, __TIME__); 87 88 // 89 // Now we need to setup the EFI System Table with information about the console devices. 90 // This code is normally in the console spliter driver on platforms that support multiple 91 // consoles at the same time 92 // 93 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleTextOutProtocolGuid, NULL, &NoHandles, &Buffer); 94 if (!EFI_ERROR (Status)) { 95 // Use the first SimpleTextOut we find and update the EFI System Table 96 gST->ConsoleOutHandle = Buffer[0]; 97 gST->StandardErrorHandle = Buffer[0]; 98 Status = gBS->HandleProtocol (Buffer[0], &gEfiSimpleTextOutProtocolGuid, (VOID **)&gST->ConOut); 99 ASSERT_EFI_ERROR (Status); 100 101 gST->StdErr = gST->ConOut; 102 103 gST->ConOut->OutputString (gST->ConOut, L"BDS: Console Started!!!!\n\r"); 104 FreePool (Buffer); 105 106 gConsolePresent = TRUE; 107 } 108 109 110 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleTextInProtocolGuid, NULL, &NoHandles, &Buffer); 111 if (!EFI_ERROR (Status)) { 112 // Use the first SimpleTextIn we find and update the EFI System Table 113 gST->ConsoleInHandle = Buffer[0]; 114 Status = gBS->HandleProtocol (Buffer[0], &gEfiSimpleTextInProtocolGuid, (VOID **)&gST->ConIn); 115 ASSERT_EFI_ERROR (Status); 116 117 FreePool (Buffer); 118 } 119 120 // 121 // We now have EFI Consoles up and running. Print () will work now. DEBUG () and ASSERT () worked 122 // prior to this point as they were configured to use a more primative output scheme. 123 // 124 125 // 126 //Perform Connect 127 // 128 HandleCount = 0; 129 while (1) { 130 OldHandleCount = HandleCount; 131 Status = gBS->LocateHandleBuffer ( 132 AllHandles, 133 NULL, 134 NULL, 135 &HandleCount, 136 &HandleBuffer 137 ); 138 if (EFI_ERROR (Status)) { 139 break; 140 } 141 142 if (HandleCount == OldHandleCount) { 143 break; 144 } 145 146 for (Index = 0; Index < HandleCount; Index++) { 147 gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE); 148 } 149 } 150 151 EfiSignalEventReadyToBoot (); 152 153 //Locate handles for SimpleFileSystem protocol 154 Status = gBS->LocateHandleBuffer ( 155 ByProtocol, 156 &gEfiSimpleFileSystemProtocolGuid, 157 NULL, 158 &HandleCount, 159 &HandleBuffer 160 ); 161 if (!EFI_ERROR(Status)) { 162 for (Index = 0; Index < HandleCount; Index++) { 163 //Get the device path 164 FileSystemDevicePath = DevicePathFromHandle(HandleBuffer[Index]); 165 if (FileSystemDevicePath == NULL) { 166 continue; 167 } 168 169 //Check if UsbIo is on any handles in the device path. 170 Status = gBS->LocateDevicePath(&gEfiUsbIoProtocolGuid, &FileSystemDevicePath, &UsbDeviceHandle); 171 if (EFI_ERROR(Status)) { 172 continue; 173 } 174 175 //Check if Usb stick has a magic EBL file. 176 LoadImageDevicePath = FileDevicePath(HandleBuffer[Index], L"Ebl.efi"); 177 Status = gBS->LoadImage (TRUE, gImageHandle, LoadImageDevicePath, NULL, 0, &ImageHandle); 178 if (EFI_ERROR(Status)) { 179 continue; 180 } 181 182 //Boot to Shell on USB stick. 183 Status = gBS->StartImage (ImageHandle, NULL, NULL); 184 if (EFI_ERROR(Status)) { 185 continue; 186 } 187 } 188 } 189 190 // 191 // Normal UEFI behavior is to process Globally Defined Variables as defined in Chapter 3 192 // (Boot Manager) of the UEFI specification. For this embedded system we don't do this. 193 // 194 195 // 196 // Search all the FVs for an application with a UI Section of Ebl. A .FDF file can be used 197 // to control the names of UI sections in an FV. 198 // 199 Status = FindApplicationMatchingUiSection (L"Ebl", &FvHandle, &NameGuid); 200 if (!EFI_ERROR (Status)) { 201 202 //Boot to Shell. 203 Status = LoadPeCoffSectionFromFv (FvHandle, &NameGuid); 204 205 if (EFI_ERROR(Status)) { 206 DEBUG((EFI_D_ERROR, "Boot from Shell failed. Status: %r\n", Status)); 207 } 208 } 209 210 // 211 // EFI does not define the behaviour if all boot attemps fail and the last one returns. 212 // So we make a policy choice to reset the system since this BDS does not have a UI. 213 // 214 gRT->ResetSystem (EfiResetShutdown, Status, 0, NULL); 215 216 return ; 217 } 218 219 220 EFI_STATUS 221 EFIAPI 222 BdsInitialize ( 223 IN EFI_HANDLE ImageHandle, 224 IN EFI_SYSTEM_TABLE *SystemTable 225 ) 226 { 227 EFI_STATUS Status; 228 229 // 230 // Install protocol interface 231 // 232 Status = gBS->InstallMultipleProtocolInterfaces ( 233 &ImageHandle, 234 &gEfiBdsArchProtocolGuid, &gBdsProtocol, 235 NULL 236 ); 237 ASSERT_EFI_ERROR (Status); 238 239 return Status; 240 } 241 242 243