1
0

bdsentry.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. bdsentry.c
  9. Abstract:
  10. This module implements the high level Boot Device Selection code.
  11. Author:
  12. Evan Green 17-Mar-2014
  13. Environment:
  14. Firmware
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "ueficore.h"
  20. #include "bds.h"
  21. #include <minoca/uefi/guid/globlvar.h>
  22. #include <minoca/uefi/guid/coninct.h>
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. #define EFI_FIRMWARE_REVISION 0x00010000
  27. //
  28. // ------------------------------------------------------ Data Type Definitions
  29. //
  30. //
  31. // ----------------------------------------------- Internal Function Prototypes
  32. //
  33. VOID
  34. EfipBdsBootDeviceSelect (
  35. VOID
  36. );
  37. VOID
  38. EfipBdsFormalizeEfiGlobalVariables (
  39. VOID
  40. );
  41. VOID
  42. EfipBdsFormalizeConsoleVariable (
  43. CHAR16 *VariableName
  44. );
  45. //
  46. // -------------------------------------------------------------------- Globals
  47. //
  48. //
  49. // Define the firmware vendor, whose contents are not assumed to be in
  50. // runtime data. The platform code can override this variable.
  51. //
  52. CHAR16 *EfiFirmwareVendor = L"Minoca Corp";
  53. UINT32 EfiFirmwareRevision = EFI_FIRMWARE_REVISION;
  54. //
  55. // Define the default timeout value.
  56. //
  57. UINT16 EfiBootTimeout = 0xFFFF;
  58. UINT16 *EfiBootNext;
  59. EFI_GUID EfiGlobalVariableGuid = EFI_GLOBAL_VARIABLE_GUID;
  60. EFI_GUID EfiConnectConInEventGuid = CONNECT_CONIN_EVENT_GUID;
  61. //
  62. // ------------------------------------------------------------------ Functions
  63. //
  64. EFIAPI
  65. VOID
  66. EfiBdsEntry (
  67. VOID
  68. )
  69. /*++
  70. Routine Description:
  71. This routine is the entry point into the boot device selection phase of
  72. the firmware. It attempts to find an OS loader and launch it.
  73. Arguments:
  74. None.
  75. Return Value:
  76. None. This routine does not return.
  77. --*/
  78. {
  79. UINT32 Attributes;
  80. UINTN BootNextSize;
  81. LIST_ENTRY BootOptionList;
  82. UINTN BootTimeout;
  83. LIST_ENTRY DriverOptionList;
  84. CHAR16 *FirmwareVendor;
  85. UINTN FirmwareVendorSize;
  86. EFI_HANDLE *HandleBuffer;
  87. UINTN HandleCount;
  88. UINTN Index;
  89. UINTN OldHandleCount;
  90. EFI_STATUS Status;
  91. INITIALIZE_LIST_HEAD(&DriverOptionList);
  92. INITIALIZE_LIST_HEAD(&BootOptionList);
  93. //
  94. // Copy the firmware vendor into runtime pool and set the firmware revision.
  95. // This then requires recomputing the CRC of the system table.
  96. //
  97. if (EfiFirmwareVendor != NULL) {
  98. FirmwareVendorSize = EfiCoreStringLength(EfiFirmwareVendor);
  99. FirmwareVendorSize = (FirmwareVendorSize + 1) * sizeof(CHAR16);
  100. FirmwareVendor = EfiCoreAllocateRuntimePool(FirmwareVendorSize);
  101. if (FirmwareVendor != NULL) {
  102. EfiCoreCopyMemory(FirmwareVendor,
  103. EfiFirmwareVendor,
  104. FirmwareVendorSize);
  105. EfiSystemTable->FirmwareVendor = FirmwareVendor;
  106. }
  107. }
  108. EfiSystemTable->FirmwareRevision = EfiFirmwareRevision;
  109. EfiSystemTable->Hdr.CRC32 = 0;
  110. EfiCalculateCrc32(EfiSystemTable,
  111. sizeof(EFI_SYSTEM_TABLE),
  112. &(EfiSystemTable->Hdr.CRC32));
  113. //
  114. // Connect all controllers.
  115. //
  116. HandleCount = 0;
  117. while (TRUE) {
  118. OldHandleCount = HandleCount;
  119. Status = EfiLocateHandleBuffer(AllHandles,
  120. NULL,
  121. NULL,
  122. &HandleCount,
  123. &HandleBuffer);
  124. if (EFI_ERROR(Status)) {
  125. break;
  126. }
  127. if (HandleCount == OldHandleCount) {
  128. break;
  129. }
  130. for (Index = 0; Index < HandleCount; Index += 1) {
  131. EfiConnectController(HandleBuffer[Index], NULL, NULL, TRUE);
  132. }
  133. EfiFreePool(HandleBuffer);
  134. }
  135. EfiCoreLoadVariablesFromFileSystem();
  136. EfipBdsFormalizeEfiGlobalVariables();
  137. EfipBdsConnectAllDefaultConsoles();
  138. BootTimeout = EfiBootTimeout;
  139. if (BootTimeout != 0xFFFF) {
  140. Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  141. EFI_VARIABLE_RUNTIME_ACCESS |
  142. EFI_VARIABLE_NON_VOLATILE;
  143. Status = EfiSetVariable(L"Timeout",
  144. &EfiGlobalVariableGuid,
  145. Attributes,
  146. sizeof(UINT16),
  147. &BootTimeout);
  148. ASSERT(!EFI_ERROR(Status));
  149. }
  150. //
  151. // Set up the device list based on EFI 1.1 variables. Process Driver####
  152. // and load the drivers in the option list.
  153. //
  154. EfipBdsBuildOptionFromVariable(&DriverOptionList, L"DriverOrder");
  155. if (!LIST_EMPTY(&DriverOptionList)) {
  156. EfipBdsLoadDrivers(&DriverOptionList);
  157. }
  158. //
  159. // Look for a boot next option.
  160. //
  161. EfiBootNext = EfipBdsGetVariable(L"BootNext",
  162. &EfiGlobalVariableGuid,
  163. &BootNextSize);
  164. EfipBdsBootDeviceSelect();
  165. //
  166. // Execution should never reach here.
  167. //
  168. ASSERT(FALSE);
  169. return;
  170. }
  171. //
  172. // --------------------------------------------------------- Internal Functions
  173. //
  174. VOID
  175. EfipBdsBootDeviceSelect (
  176. VOID
  177. )
  178. /*++
  179. Routine Description:
  180. This routine validates the global variables set in EFI for the BDS phase.
  181. Arguments:
  182. None.
  183. Return Value:
  184. None.
  185. --*/
  186. {
  187. UINT32 Attributes;
  188. LIST_ENTRY BootList;
  189. PLIST_ENTRY BootNextEntry;
  190. BOOLEAN BootNextExists;
  191. PEFI_BDS_COMMON_OPTION BootOption;
  192. CHAR16 Buffer[20];
  193. EFI_EVENT ConnectInputEvent;
  194. PLIST_ENTRY CurrentEntry;
  195. CHAR16 *ExitData;
  196. UINTN ExitDataSize;
  197. EFI_STATUS Status;
  198. BOOLEAN TriedEverything;
  199. BootNextEntry = NULL;
  200. BootNextExists = FALSE;
  201. CurrentEntry = NULL;
  202. ConnectInputEvent = NULL;
  203. INITIALIZE_LIST_HEAD(&BootList);
  204. TriedEverything = FALSE;
  205. EfiCoreSetMemory(Buffer, sizeof(Buffer), 0);
  206. //
  207. // Create an event to fire when console input is connected.
  208. //
  209. Status = EfiCreateEventEx(EVT_NOTIFY_SIGNAL,
  210. TPL_CALLBACK,
  211. EfiCoreEmptyCallbackFunction,
  212. NULL,
  213. &EfiConnectConInEventGuid,
  214. &ConnectInputEvent);
  215. if (EFI_ERROR(Status)) {
  216. ConnectInputEvent = NULL;
  217. }
  218. if (EfiBootNext != NULL) {
  219. BootNextExists = TRUE;
  220. //
  221. // Clear the variable so that it only tries to boot once.
  222. //
  223. Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  224. EFI_VARIABLE_RUNTIME_ACCESS |
  225. EFI_VARIABLE_NON_VOLATILE;
  226. EfiSetVariable(L"BootNext",
  227. &EfiGlobalVariableGuid,
  228. Attributes,
  229. 0,
  230. NULL);
  231. //
  232. // Add the boot next option.
  233. //
  234. EfipBdsCreateHexCodeString(L"Boot",
  235. *EfiBootNext,
  236. Buffer,
  237. sizeof(Buffer));
  238. BootOption = EfipBdsConvertVariableToOption(&BootList, Buffer);
  239. if (BootOption == NULL) {
  240. return;
  241. }
  242. BootOption->BootCurrent = *EfiBootNext;
  243. }
  244. //
  245. // Parse the boot order to get boot options.
  246. //
  247. EfipBdsBuildOptionFromVariable(&BootList, L"BootOrder");
  248. //
  249. // If nothing was enumerated, get desperate.
  250. //
  251. if (LIST_EMPTY(&BootList)) {
  252. EfipBdsEnumerateAllBootOptions(&BootList);
  253. TriedEverything = TRUE;
  254. }
  255. CurrentEntry = BootList.Next;
  256. if (CurrentEntry == NULL) {
  257. ASSERT(FALSE);
  258. return;
  259. }
  260. //
  261. // Loop forever.
  262. //
  263. while (TRUE) {
  264. //
  265. // Handle reaching the end of the list.
  266. //
  267. if (CurrentEntry == &BootList) {
  268. if (TriedEverything == FALSE) {
  269. EfipBdsEnumerateAllBootOptions(&BootList);
  270. TriedEverything = TRUE;
  271. CurrentEntry = BootList.Next;
  272. continue;
  273. }
  274. if (ConnectInputEvent != NULL) {
  275. EfiSignalEvent(ConnectInputEvent);
  276. }
  277. if (EfiSystemTable->StdErr != NULL) {
  278. EfiSystemTable->StdErr->OutputString(EfiSystemTable->StdErr,
  279. L"Found nothing to boot.");
  280. }
  281. //
  282. // Hmm... eventually do something more intelligent here.
  283. //
  284. RtlDebugPrint("Nothing to boot, hanging...\n");
  285. while (TRUE) {
  286. NOTHING;
  287. }
  288. INITIALIZE_LIST_HEAD(&BootList);
  289. EfipBdsBuildOptionFromVariable(&BootList, L"BootOrder");
  290. CurrentEntry = BootList.Next;
  291. continue;
  292. }
  293. //
  294. // Grab the boot option.
  295. //
  296. BootOption = LIST_VALUE(CurrentEntry, EFI_BDS_COMMON_OPTION, ListEntry);
  297. CurrentEntry = CurrentEntry->Next;
  298. ASSERT(BootOption->Magic == EFI_BDS_COMMON_OPTION_MAGIC);
  299. //
  300. // Skip anything not marked active.
  301. //
  302. if ((BootOption->Attribute & LOAD_OPTION_ACTIVE) == 0) {
  303. continue;
  304. }
  305. //
  306. // Make sure the device path is connected, except for BBS paths.
  307. //
  308. if (EfiCoreGetDevicePathType(BootOption->DevicePath) !=
  309. BBS_DEVICE_PATH) {
  310. EfipBdsConnectDevicePath(BootOption->DevicePath);
  311. }
  312. Status = EfipBdsBootViaBootOption(BootOption,
  313. BootOption->DevicePath,
  314. &ExitDataSize,
  315. &ExitData);
  316. if (Status != EFI_SUCCESS) {
  317. //
  318. // Potentially do something if the boot entry failed. For now,
  319. // nothing.
  320. //
  321. } else {
  322. if (ConnectInputEvent != NULL) {
  323. EfiSignalEvent(ConnectInputEvent);
  324. }
  325. //
  326. // This is where the boot menu would be presented, which might
  327. // change the boot list. Re-enumerate that now even though there
  328. // is no boot menu.
  329. //
  330. if (BootNextExists != FALSE) {
  331. BootNextEntry = BootList.Next;
  332. }
  333. INITIALIZE_LIST_HEAD(&BootList);
  334. if (BootNextEntry != NULL) {
  335. INSERT_BEFORE(BootNextEntry, &BootList);
  336. }
  337. EfipBdsBuildOptionFromVariable(&BootList, L"BootOrder");
  338. CurrentEntry = BootList.Next;
  339. }
  340. }
  341. //
  342. // Execution should never get here.
  343. //
  344. ASSERT(FALSE);
  345. return;
  346. }
  347. VOID
  348. EfipBdsFormalizeEfiGlobalVariables (
  349. VOID
  350. )
  351. /*++
  352. Routine Description:
  353. This routine validates the global variables set in EFI for the BDS phase.
  354. Arguments:
  355. None.
  356. Return Value:
  357. None.
  358. --*/
  359. {
  360. EfipBdsFormalizeConsoleVariable(L"ConIn");
  361. EfipBdsFormalizeConsoleVariable(L"ConOut");
  362. EfipBdsFormalizeConsoleVariable(L"ErrOut");
  363. return;
  364. }
  365. VOID
  366. EfipBdsFormalizeConsoleVariable (
  367. CHAR16 *VariableName
  368. )
  369. /*++
  370. Routine Description:
  371. This routine validates that one of the console variables is a valid
  372. device path.
  373. Arguments:
  374. VariableName - Supplies a pointer to the variable name. This is expected to
  375. be ConIn, ConOut, or ErrOut.
  376. Return Value:
  377. None.
  378. --*/
  379. {
  380. UINT32 Attributes;
  381. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  382. UINTN DevicePathSize;
  383. EFI_GUID *Guid;
  384. UINTN HandleCount;
  385. EFI_HANDLE *Handles;
  386. EFI_STATUS Status;
  387. UINTN VariableSize;
  388. Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  389. EFI_VARIABLE_RUNTIME_ACCESS |
  390. EFI_VARIABLE_NON_VOLATILE;
  391. DevicePath = EfipBdsGetVariable(VariableName,
  392. &EfiGlobalVariableGuid,
  393. &VariableSize);
  394. //
  395. // If the device path is not set, try to find one.
  396. //
  397. if (DevicePath == NULL) {
  398. //
  399. // For ConIn, get the input protocol, otherwise get the simple text
  400. // output protocol.
  401. //
  402. if ((VariableName[0] != L'\0') && (VariableName[1] != L'\0') &&
  403. (VariableName[2] != L'\0') && (VariableName[3] == L'I')) {
  404. Guid = &EfiSimpleTextInputProtocolGuid;
  405. } else {
  406. Guid = &EfiSimpleTextOutputProtocolGuid;
  407. }
  408. HandleCount = 0;
  409. Handles = NULL;
  410. Status = EfiLocateHandleBuffer(ByProtocol,
  411. Guid,
  412. NULL,
  413. &HandleCount,
  414. &Handles);
  415. if ((!EFI_ERROR(Status)) && (HandleCount != 0)) {
  416. DevicePath = EfiCoreGetDevicePathFromHandle(Handles[0]);
  417. if (DevicePath != NULL) {
  418. DevicePathSize = EfiCoreGetDevicePathSize(DevicePath);
  419. EfiSetVariable(VariableName,
  420. &EfiGlobalVariableGuid,
  421. Attributes,
  422. DevicePathSize,
  423. DevicePath);
  424. }
  425. EfiFreePool(Handles);
  426. }
  427. }
  428. //
  429. // If the device path is invalid, delete it.
  430. //
  431. if ((DevicePath != NULL) &&
  432. (EfiCoreIsDevicePathValid(DevicePath, VariableSize) == FALSE)) {
  433. RtlDebugPrint("Deleting invalid console variable.\n");
  434. Status = EfiSetVariable(VariableName,
  435. &EfiGlobalVariableGuid,
  436. Attributes,
  437. 0,
  438. NULL);
  439. ASSERT(!EFI_ERROR(Status));
  440. DevicePath = NULL;
  441. }
  442. return;
  443. }