[BOOTMGFW]:
[reactos.git] / reactos / boot / environ / lib / firmware / efi / firmware.c
1 /*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/firmware/efi/firmware.c
5 * PURPOSE: Boot Library Firmware Initialization for EFI
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "bl.h"
12
13 /* DATA VARIABLES ************************************************************/
14
15 GUID EfiSimpleTextInputExProtocol = EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
16
17 PBL_FIRMWARE_DESCRIPTOR EfiFirmwareParameters;
18 BL_FIRMWARE_DESCRIPTOR EfiFirmwareData;
19 EFI_HANDLE EfiImageHandle;
20 EFI_SYSTEM_TABLE* EfiSystemTable;
21
22 EFI_SYSTEM_TABLE *EfiST;
23 EFI_BOOT_SERVICES *EfiBS;
24 EFI_RUNTIME_SERVICES *EfiRT;
25 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *EfiConOut;
26 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *EfiConIn;
27 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *EfiConInEx;
28
29 /* FUNCTIONS *****************************************************************/
30
31 NTSTATUS
32 EfiOpenProtocol (
33 _In_ EFI_HANDLE Handle,
34 _In_ EFI_GUID *Protocol,
35 _Out_ PVOID* Interface
36 )
37 {
38 EFI_STATUS EfiStatus;
39 NTSTATUS Status;
40 BL_ARCH_MODE OldMode;
41
42 /* Are we using virtual memory/ */
43 if (MmTranslationType != BlNone)
44 {
45 /* We need complex tracking to make this work */
46 //Status = EfiVmOpenProtocol(Handle, Protocol, Interface);
47 Status = STATUS_NOT_SUPPORTED;
48 }
49 else
50 {
51 /* Are we in protected mode? */
52 OldMode = CurrentExecutionContext->Mode;
53 if (OldMode != BlRealMode)
54 {
55 /* FIXME: Not yet implemented */
56 return STATUS_NOT_IMPLEMENTED;
57 }
58
59 /* Are we on legacy 1.02? */
60 if (EfiST->FirmwareRevision == EFI_1_02_SYSTEM_TABLE_REVISION)
61 {
62 /* Make the legacy call */
63 EfiStatus = EfiBS->HandleProtocol(Handle, Protocol, Interface);
64 }
65 else
66 {
67 /* Use the UEFI version */
68 EfiStatus = EfiBS->OpenProtocol(Handle,
69 Protocol,
70 Interface,
71 EfiImageHandle,
72 NULL,
73 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
74
75 /* Switch back to protected mode if we came from there */
76 if (OldMode != BlRealMode)
77 {
78 BlpArchSwitchContext(OldMode);
79 }
80 }
81
82 /* Convert the error to an NTSTATUS */
83 Status = EfiGetNtStatusCode(EfiStatus);
84 }
85
86 /* Clear the interface on failure, and return the status */
87 if (!NT_SUCCESS(Status))
88 {
89 *Interface = NULL;
90 }
91
92 return Status;
93 }
94
95 NTSTATUS
96 EfiConInExSetState (
97 _In_ EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *ConInEx,
98 _In_ EFI_KEY_TOGGLE_STATE* KeyToggleState
99 )
100 {
101 BL_ARCH_MODE OldMode;
102 EFI_STATUS EfiStatus;
103
104 /* Are we in protected mode? */
105 OldMode = CurrentExecutionContext->Mode;
106 if (OldMode != BlRealMode)
107 {
108 /* FIXME: Not yet implemented */
109 return STATUS_NOT_IMPLEMENTED;
110 }
111
112 /* Make the EFI call */
113 EfiStatus = ConInEx->SetState(ConInEx, KeyToggleState);
114
115 /* Switch back to protected mode if we came from there */
116 if (OldMode != BlRealMode)
117 {
118 BlpArchSwitchContext(OldMode);
119 }
120
121 /* Convert the error to an NTSTATUS */
122 return EfiGetNtStatusCode(EfiStatus);
123 }
124
125 NTSTATUS
126 EfiSetWatchdogTimer (
127 VOID
128 )
129 {
130 BL_ARCH_MODE OldMode;
131 EFI_STATUS EfiStatus;
132
133 /* Are we in protected mode? */
134 OldMode = CurrentExecutionContext->Mode;
135 if (OldMode != BlRealMode)
136 {
137 /* FIXME: Not yet implemented */
138 return STATUS_NOT_IMPLEMENTED;
139 }
140
141 /* Make the EFI call */
142 EfiStatus = EfiBS->SetWatchdogTimer(0, 0, 0, NULL);
143
144 /* Switch back to protected mode if we came from there */
145 if (OldMode != BlRealMode)
146 {
147 BlpArchSwitchContext(OldMode);
148 }
149
150 /* Convert the error to an NTSTATUS */
151 return EfiGetNtStatusCode(EfiStatus);
152 }
153
154 NTSTATUS
155 EfiGetMemoryMap (
156 _Out_ UINTN* MemoryMapSize,
157 _Inout_ EFI_MEMORY_DESCRIPTOR *MemoryMap,
158 _Out_ UINTN* MapKey,
159 _Out_ UINTN* DescriptorSize,
160 _Out_ UINTN* DescriptorVersion
161 )
162 {
163 BL_ARCH_MODE OldMode;
164 EFI_STATUS EfiStatus;
165
166 /* Are we in protected mode? */
167 OldMode = CurrentExecutionContext->Mode;
168 if (OldMode != BlRealMode)
169 {
170 /* FIXME: Not yet implemented */
171 return STATUS_NOT_IMPLEMENTED;
172 }
173
174 /* Make the EFI call */
175 EfiStatus = EfiBS->GetMemoryMap(MemoryMapSize,
176 MemoryMap,
177 MapKey,
178 DescriptorSize,
179 DescriptorVersion);
180
181 /* Switch back to protected mode if we came from there */
182 if (OldMode != BlRealMode)
183 {
184 BlpArchSwitchContext(OldMode);
185 }
186
187 /* Convert the error to an NTSTATUS */
188 return EfiGetNtStatusCode(EfiStatus);
189 }
190
191 NTSTATUS
192 EfiFreePages (
193 _In_ ULONG Pages,
194 _In_ EFI_PHYSICAL_ADDRESS PhysicalAddress
195 )
196 {
197 BL_ARCH_MODE OldMode;
198 EFI_STATUS EfiStatus;
199
200 /* Are we in protected mode? */
201 OldMode = CurrentExecutionContext->Mode;
202 if (OldMode != BlRealMode)
203 {
204 /* FIXME: Not yet implemented */
205 return STATUS_NOT_IMPLEMENTED;
206 }
207
208 /* Make the EFI call */
209 EfiStatus = EfiBS->FreePages(PhysicalAddress, Pages);
210
211 /* Switch back to protected mode if we came from there */
212 if (OldMode != BlRealMode)
213 {
214 BlpArchSwitchContext(OldMode);
215 }
216
217 /* Convert the error to an NTSTATUS */
218 return EfiGetNtStatusCode(EfiStatus);
219 }
220
221 NTSTATUS
222 EfiAllocatePages (
223 _In_ ULONG Type,
224 _In_ ULONG Pages,
225 _Inout_ EFI_PHYSICAL_ADDRESS* Memory
226 )
227 {
228 BL_ARCH_MODE OldMode;
229 EFI_STATUS EfiStatus;
230
231 /* Are we in protected mode? */
232 OldMode = CurrentExecutionContext->Mode;
233 if (OldMode != BlRealMode)
234 {
235 /* FIXME: Not yet implemented */
236 return STATUS_NOT_IMPLEMENTED;
237 }
238
239 /* Make the EFI call */
240 EfiStatus = EfiBS->AllocatePages(Type, EfiLoaderData, Pages, Memory);
241
242 /* Switch back to protected mode if we came from there */
243 if (OldMode != BlRealMode)
244 {
245 BlpArchSwitchContext(OldMode);
246 }
247
248 /* Convert the error to an NTSTATUS */
249 return EfiGetNtStatusCode(EfiStatus);
250 }
251
252 BL_MEMORY_ATTR
253 MmFwpGetOsAttributeType (
254 _In_ ULONGLONG Attribute
255 )
256 {
257 BL_MEMORY_ATTR OsAttribute = 0;
258
259 if (Attribute & EFI_MEMORY_UC)
260 {
261 OsAttribute = BlMemoryUncached;
262 }
263
264 if (Attribute & EFI_MEMORY_WC)
265 {
266 OsAttribute |= BlMemoryWriteCombined;
267 }
268
269 if (Attribute & EFI_MEMORY_WT)
270 {
271 OsAttribute |= BlMemoryWriteThrough;
272 }
273
274 if (Attribute & EFI_MEMORY_WB)
275 {
276 OsAttribute |= BlMemoryWriteBack;
277 }
278
279 if (Attribute & EFI_MEMORY_UCE)
280 {
281 OsAttribute |= BlMemoryUncachedExported;
282 }
283
284 if (Attribute & EFI_MEMORY_WP)
285 {
286 OsAttribute |= BlMemoryWriteProtected;
287 }
288
289 if (Attribute & EFI_MEMORY_RP)
290 {
291 OsAttribute |= BlMemoryReadProtected;
292 }
293
294 if (Attribute & EFI_MEMORY_XP)
295 {
296 OsAttribute |= BlMemoryExecuteProtected;
297 }
298
299 if (Attribute & EFI_MEMORY_RUNTIME)
300 {
301 OsAttribute |= BlMemoryRuntime;
302 }
303
304 return OsAttribute;
305 }
306
307 BL_MEMORY_TYPE
308 MmFwpGetOsMemoryType (
309 _In_ EFI_MEMORY_TYPE MemoryType
310 )
311 {
312 BL_MEMORY_TYPE OsType;
313
314 switch (MemoryType)
315 {
316 case EfiLoaderCode:
317 case EfiLoaderData:
318 OsType = BlLoaderMemory;
319 break;
320
321 case EfiBootServicesCode:
322 case EfiBootServicesData:
323 OsType = BlEfiBootMemory;
324 break;
325
326 case EfiRuntimeServicesCode:
327 case EfiRuntimeServicesData:
328 OsType = BlEfiRuntimeMemory;
329 break;
330
331 case EfiConventionalMemory:
332 OsType = BlConventionalMemory;
333 break;
334
335 case EfiUnusableMemory:
336 OsType = BlUnusableMemory;
337 break;
338
339 case EfiACPIReclaimMemory:
340 OsType = BlAcpiReclaimMemory;
341 break;
342
343 case EfiACPIMemoryNVS:
344 OsType = BlAcpiNvsMemory;
345 break;
346
347 case EfiMemoryMappedIO:
348 OsType = BlDeviceIoMemory;
349 break;
350
351 case EfiMemoryMappedIOPortSpace:
352 OsType = BlDevicePortMemory;
353 break;
354
355 case EfiPalCode:
356 OsType = BlPalMemory;
357 break;
358
359 default:
360 OsType = BlReservedMemory;
361 break;
362 }
363
364 return OsType;
365 }
366
367 NTSTATUS
368 MmFwGetMemoryMap (
369 _Out_ PBL_MEMORY_DESCRIPTOR_LIST MemoryMap,
370 _In_ ULONG Flags
371 )
372 {
373 BL_LIBRARY_PARAMETERS LibraryParameters = BlpLibraryParameters;
374 BOOLEAN UseEfiBuffer;
375 NTSTATUS Status;
376 ULONGLONG Pages, StartPage, EndPage;
377 UINTN EfiMemoryMapSize, MapKey, DescriptorSize, DescriptorVersion;
378 EFI_PHYSICAL_ADDRESS EfiBuffer;
379 EFI_MEMORY_DESCRIPTOR* EfiMemoryMap;
380 EFI_STATUS EfiStatus;
381 BL_ARCH_MODE OldMode;
382 EFI_MEMORY_DESCRIPTOR EfiDescriptor;
383 BL_MEMORY_TYPE MemoryType;
384
385 /* Increment the nesting depth */
386 MmDescriptorCallTreeCount++;
387
388 /* Determine if we should use EFI or our own allocator at this point */
389 UseEfiBuffer = Flags & BL_MM_FLAG_USE_FIRMWARE_FOR_MEMORY_MAP_BUFFERS;
390 if (!(LibraryParameters.LibraryFlags & BL_LIBRARY_FLAG_INITIALIZATION_COMPLETED))
391 {
392 UseEfiBuffer = TRUE;
393 }
394
395 /* Bail out if we don't have a list to use */
396 if (MemoryMap == NULL)
397 {
398 Status = STATUS_INVALID_PARAMETER;
399 goto Quickie;
400 }
401
402 /* Free the current descriptor list */
403 MmMdFreeList(MemoryMap);
404
405 /* Call into EFI to get the size of the memory map */
406 Status = EfiGetMemoryMap(&EfiMemoryMapSize,
407 NULL,
408 &MapKey,
409 &DescriptorSize,
410 &DescriptorVersion);
411 if (Status != STATUS_BUFFER_TOO_SMALL)
412 {
413 /* This should've failed because our buffer was too small, nothing else */
414 EarlyPrint(L"Got strange EFI status for memory map: %lx\n", Status);
415 if (NT_SUCCESS(Status))
416 {
417 Status = STATUS_UNSUCCESSFUL;
418 }
419 goto Quickie;
420 }
421
422 /* Add 4 more descriptors just in case things changed */
423 EfiMemoryMapSize += (4 * DescriptorSize);
424 Pages = BYTES_TO_PAGES(EfiMemoryMapSize);
425 EarlyPrint(L"Memory map size: %lx bytes, %d pages\n", EfiMemoryMapSize, Pages);
426
427 /* Should we use EFI to grab memory? */
428 if (UseEfiBuffer)
429 {
430 /* Yes -- request one more page to align up correctly */
431 Pages++;
432
433 /* Grab the required pages */
434 Status = EfiAllocatePages(AllocateAnyPages,
435 Pages,
436 &EfiBuffer);
437 if (!NT_SUCCESS(Status))
438 {
439 EarlyPrint(L"EFI allocation failed: %lx\n", Status);
440 goto Quickie;
441 }
442
443 /* Free the pages for now */
444 Status = EfiFreePages(Pages, EfiBuffer);
445 if (!NT_SUCCESS(Status))
446 {
447 EfiBuffer = 0;
448 goto Quickie;
449 }
450
451 /* Now round to the actual buffer size, removing the extra page */
452 EfiBuffer = ROUND_TO_PAGES(EfiBuffer);
453 Pages--;
454 Status = EfiAllocatePages(AllocateAddress,
455 Pages,
456 &EfiBuffer);
457 if (!NT_SUCCESS(Status))
458 {
459 EfiBuffer = 0;
460 goto Quickie;
461 }
462
463 /* Get the final aligned size and proper buffer */
464 EfiMemoryMapSize = EFI_PAGES_TO_SIZE(Pages);
465 EfiMemoryMap = (EFI_MEMORY_DESCRIPTOR*)(ULONG_PTR)EfiBuffer;
466
467 /* Switch to real mode if not already in it */
468 OldMode = CurrentExecutionContext->Mode;
469 if (OldMode != BlRealMode)
470 {
471 BlpArchSwitchContext(BlRealMode);
472 }
473
474 /* Call EFI to get the memory map */
475 EfiStatus = EfiBS->GetMemoryMap(&EfiMemoryMapSize,
476 EfiMemoryMap,
477 &MapKey,
478 &DescriptorSize,
479 &DescriptorVersion);
480
481 /* Switch back into the previous mode */
482 if (OldMode != BlRealMode)
483 {
484 BlpArchSwitchContext(OldMode);
485 }
486
487 /* Convert the result code */
488 Status = EfiGetNtStatusCode(EfiStatus);
489 }
490 else
491 {
492 /* We don't support this path yet */
493 Status = STATUS_NOT_IMPLEMENTED;
494 }
495
496 /* So far so good? */
497 if (!NT_SUCCESS(Status))
498 {
499 EarlyPrint(L"Failed to get EFI memory map: %lx\n", Status);
500 goto Quickie;
501 }
502
503 /* Did we get correct data from firmware? */
504 if (((EfiMemoryMapSize % DescriptorSize)) ||
505 (DescriptorSize < sizeof(EFI_MEMORY_DESCRIPTOR)))
506 {
507 EarlyPrint(L"Incorrect descriptor size\n");
508 Status = STATUS_UNSUCCESSFUL;
509 goto Quickie;
510 }
511
512 /* Loop the EFI memory map */
513 EarlyPrint(L"UEFI MEMORY MAP\n\n");
514 EarlyPrint(L"TYPE START END ATTRIBUTES\n");
515 EarlyPrint(L"===============================================================\n");
516 while (EfiMemoryMapSize != 0)
517 {
518 /* Check if this is an EFI buffer, but we're not in real mode */
519 if ((UseEfiBuffer) && (OldMode != BlRealMode))
520 {
521 BlpArchSwitchContext(BlRealMode);
522 }
523
524 /* Capture it so we can go back to protected mode (if possible) */
525 EfiDescriptor = *EfiMemoryMap;
526
527 /* Go back to protected mode, if we had switched */
528 if ((UseEfiBuffer) && (OldMode != BlRealMode))
529 {
530 BlpArchSwitchContext(OldMode);
531 }
532
533 /* Convert to OS memory type */
534 MemoryType = MmFwpGetOsMemoryType(EfiDescriptor.Type);
535
536 /* Round up or round down depending on where the memory is coming from */
537 if (MemoryType == BlConventionalMemory)
538 {
539 StartPage = BYTES_TO_PAGES(EfiDescriptor.PhysicalStart);
540 }
541 else
542 {
543 StartPage = EfiDescriptor.PhysicalStart >> PAGE_SHIFT;
544 }
545
546 /* Calculate the ending page */
547 EndPage = StartPage + EfiDescriptor.NumberOfPages;
548
549 /* If after rounding, we ended up with 0 pages, skip this */
550 if (StartPage == EndPage)
551 {
552 goto LoopAgain;
553 }
554
555 EarlyPrint(L"%08X 0x%016I64X-0x%016I64X 0x%X\n",
556 MemoryType,
557 StartPage << PAGE_SHIFT,
558 EndPage << PAGE_SHIFT,
559 EfiDescriptor.Attribute);
560
561 /* Consume this descriptor, and move to the next one */
562 LoopAgain:
563 EfiMemoryMapSize -= DescriptorSize;
564 EfiMemoryMap = (PVOID)((ULONG_PTR)EfiMemoryMap + DescriptorSize);
565 }
566
567 Quickie:
568 /* Free the EFI buffer, if we had one */
569 if (EfiBuffer != 0)
570 {
571 EfiFreePages(Pages, EfiBuffer);
572 }
573
574 /* On failure, free the memory map if one was passed in */
575 if (!NT_SUCCESS(Status) && (MemoryMap != NULL))
576 {
577 MmMdFreeList(MemoryMap);
578 }
579
580 /* Decrement the nesting depth and return */
581 MmDescriptorCallTreeCount--;
582 return Status;
583 }
584
585 NTSTATUS
586 BlpFwInitialize (
587 _In_ ULONG Phase,
588 _In_ PBL_FIRMWARE_DESCRIPTOR FirmwareData
589 )
590 {
591 NTSTATUS Status = STATUS_SUCCESS;
592 EFI_KEY_TOGGLE_STATE KeyToggleState;
593
594 /* Check if we have vaild firmware data */
595 if (!(FirmwareData) || !(FirmwareData->Version))
596 {
597 return STATUS_INVALID_PARAMETER;
598 }
599
600 /* Check which boot phase we're in */
601 if (Phase != 0)
602 {
603 /* Memory manager is ready, open the extended input protocol */
604 Status = EfiOpenProtocol(EfiST->ConsoleInHandle,
605 &EfiSimpleTextInputExProtocol,
606 (PVOID*)&EfiConInEx);
607 if (NT_SUCCESS(Status))
608 {
609 /* Set the initial key toggle state */
610 KeyToggleState = EFI_TOGGLE_STATE_VALID | 40;
611 EfiConInExSetState(EfiST->ConsoleInHandle, &KeyToggleState);
612 }
613
614 /* Setup the watchdog timer */
615 EfiSetWatchdogTimer();
616 }
617 else
618 {
619 /* Make a copy of the parameters */
620 EfiFirmwareParameters = &EfiFirmwareData;
621
622 /* Check which version we received */
623 if (FirmwareData->Version == 1)
624 {
625 /* FIXME: Not supported */
626 Status = STATUS_NOT_SUPPORTED;
627 }
628 else if (FirmwareData->Version >= 2)
629 {
630 /* Version 2 -- save the data */
631 EfiFirmwareData = *FirmwareData;
632 EfiSystemTable = FirmwareData->SystemTable;
633 EfiImageHandle = FirmwareData->ImageHandle;
634
635 /* Set the EDK-II style variables as well */
636 EfiST = EfiSystemTable;
637 EfiBS = EfiSystemTable->BootServices;
638 EfiRT = EfiSystemTable->RuntimeServices;
639 EfiConOut = EfiSystemTable->ConOut;
640 EfiConIn = EfiSystemTable->ConIn;
641 EfiConInEx = NULL;
642 }
643 else
644 {
645 /* Unknown version */
646 Status = STATUS_NOT_SUPPORTED;
647 }
648 }
649
650 /* Return the initialization state */
651 return Status;
652 }
653