]> git.proxmox.com Git - mirror_edk2.git/blob - DuetPkg/Library/DuetBdsLib/BdsPlatform.c
User customize build method for BootSector module.
[mirror_edk2.git] / DuetPkg / Library / DuetBdsLib / BdsPlatform.c
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 BdsPlatform.c
15
16 Abstract:
17
18 This file include all platform action which can be customized
19 by IBV/OEM.
20
21 --*/
22
23 #include "BdsPlatform.h"
24
25 #define IS_PCI_ISA_PDECODE(_p) IS_CLASS3 (_p, PCI_CLASS_BRIDGE, PCI_CLASS_BRIDGE_ISA_PDECODE, 0)
26
27 CHAR16 mFirmwareVendor[] = L"TianoCore.org";
28 extern BOOLEAN gConnectAllHappened;
29 extern USB_CLASS_FORMAT_DEVICE_PATH gUsbClassKeyboardDevicePath;
30 //
31 // BDS Platform Functions
32 //
33
34 VOID
35 GetSystemTablesFromHob (
36 VOID
37 )
38 /*++
39
40 Routine Description:
41 Find GUID'ed HOBs that contain EFI_PHYSICAL_ADDRESS of ACPI, SMBIOS, MPs tables
42
43 Arguments:
44 None
45
46 Returns:
47 None.
48
49 --*/
50 {
51 EFI_STATUS Status;
52 EFI_HOB_HANDOFF_INFO_TABLE *HobList;
53 EFI_HOB_HANDOFF_INFO_TABLE *HobStart;
54 EFI_PHYSICAL_ADDRESS *Table;
55 UINTN Index;
56 EFI_GUID *TableGuidArray[] = {
57 &gEfiAcpi20TableGuid, &gEfiAcpiTableGuid, &gEfiSmbiosTableGuid, &gEfiMpsTableGuid
58 };
59
60 //
61 // Get Hob List
62 //
63 Status = EfiGetSystemConfigurationTable (&gEfiHobListGuid, (VOID *) &HobList);
64 if (EFI_ERROR (Status)) {
65 return;
66 }
67
68 //
69 // Iteratively add ACPI Table, SMBIOS Table, MPS Table to EFI System Table
70 //
71 for (Index = 0; Index < sizeof (TableGuidArray) / sizeof (*TableGuidArray); ++Index) {
72 HobStart = HobList;
73 Table = NULL;
74 Table = GetNextGuidHob (TableGuidArray[Index], &HobStart);
75 if (!EFI_ERROR (Status)) {
76 if (Table != NULL) {
77 //
78 // Check if Mps Table/Smbios Table/Acpi Table exists in E/F seg,
79 // According to UEFI Spec, we should make sure Smbios table,
80 // ACPI table and Mps tables kept in memory of specified type
81 //
82 ConvertSystemTable(TableGuidArray[Index], &Table);
83 gBS->InstallConfigurationTable (TableGuidArray[Index], (VOID *)Table);
84 }
85 }
86 }
87
88 return ;
89 }
90
91 #define EFI_LDR_MEMORY_DESCRIPTOR_GUID \
92 { 0x7701d7e5, 0x7d1d, 0x4432, 0xa4, 0x68, 0x67, 0x3d, 0xab, 0x8a, 0xde, 0x60 }
93
94 EFI_GUID gEfiLdrMemoryDescriptorGuid = EFI_LDR_MEMORY_DESCRIPTOR_GUID;
95
96 #pragma pack(1)
97
98 typedef struct {
99 EFI_HOB_GUID_TYPE Hob;
100 UINTN MemDescCount;
101 EFI_MEMORY_DESCRIPTOR *MemDesc;
102 } MEMORY_DESC_HOB;
103
104 #pragma pack()
105
106 #if 0
107 VOID
108 PrintMemoryMap (
109 VOID
110 )
111 {
112 EFI_MEMORY_DESCRIPTOR *MemMap;
113 EFI_MEMORY_DESCRIPTOR *MemMapPtr;
114 UINTN MemMapSize;
115 UINTN MapKey, DescriptorSize;
116 UINTN Index;
117 UINT32 DescriptorVersion;
118 UINT64 Bytes;
119 EFI_STATUS Status;
120
121 MemMapSize = 0;
122 MemMap = NULL;
123 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion);
124 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
125 MemMapSize += EFI_PAGE_SIZE;
126 Status = gBS->AllocatePool (EfiBootServicesData, MemMapSize, &MemMap);
127 ASSERT (Status == EFI_SUCCESS);
128 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, &DescriptorVersion);
129 ASSERT (Status == EFI_SUCCESS);
130 MemMapPtr = MemMap;
131
132 ASSERT (DescriptorVersion == EFI_MEMORY_DESCRIPTOR_VERSION);
133
134 for (Index = 0; Index < MemMapSize / DescriptorSize; Index ++) {
135 Bytes = LShiftU64 (MemMap->NumberOfPages, 12);
136 DEBUG ((EFI_D_ERROR, "%lX-%lX %lX %lX %X\n",
137 MemMap->PhysicalStart,
138 MemMap->PhysicalStart + Bytes - 1,
139 MemMap->NumberOfPages,
140 MemMap->Attribute,
141 (UINTN)MemMap->Type));
142 MemMap = (EFI_MEMORY_DESCRIPTOR *)((UINTN)MemMap + DescriptorSize);
143 }
144
145 gBS->FreePool (MemMapPtr);
146 }
147 #endif
148
149 VOID
150 UpdateMemoryMap (
151 VOID
152 )
153 {
154 EFI_STATUS Status;
155 EFI_HOB_HANDOFF_INFO_TABLE *HobList;
156 VOID *Table;
157 MEMORY_DESC_HOB MemoryDescHob;
158 UINTN Index;
159 EFI_PHYSICAL_ADDRESS Memory;
160
161 //
162 // Get Hob List
163 //
164 Status = EfiGetSystemConfigurationTable (&gEfiHobListGuid, (VOID *) &HobList);
165 if (EFI_ERROR (Status)) {
166 return;
167 }
168
169 Table = GetNextGuidHob (&gEfiLdrMemoryDescriptorGuid, &HobList);
170 MemoryDescHob.MemDescCount = *(UINTN *)Table;
171 MemoryDescHob.MemDesc = *(EFI_MEMORY_DESCRIPTOR **)((UINTN)Table + sizeof(UINTN));
172
173 //
174 // Add ACPINVS, ACPIReclaim, and Reserved memory to MemoryMap
175 //
176 for (Index = 0; Index < MemoryDescHob.MemDescCount; Index++) {
177 if (MemoryDescHob.MemDesc[Index].PhysicalStart < 0x100000) {
178 continue;
179 }
180 if (MemoryDescHob.MemDesc[Index].PhysicalStart >= 0x100000000) {
181 continue;
182 }
183 if ((MemoryDescHob.MemDesc[Index].Type == EfiReservedMemoryType) ||
184 (MemoryDescHob.MemDesc[Index].Type == EfiRuntimeServicesData) ||
185 (MemoryDescHob.MemDesc[Index].Type == EfiRuntimeServicesCode) ||
186 (MemoryDescHob.MemDesc[Index].Type == EfiACPIReclaimMemory) ||
187 (MemoryDescHob.MemDesc[Index].Type == EfiACPIMemoryNVS)) {
188 DEBUG ((EFI_D_ERROR, "PhysicalStart - 0x%x, ", MemoryDescHob.MemDesc[Index].PhysicalStart));
189 DEBUG ((EFI_D_ERROR, "PageNumber - 0x%x, ", MemoryDescHob.MemDesc[Index].NumberOfPages));
190 DEBUG ((EFI_D_ERROR, "Type - 0x%x\n", MemoryDescHob.MemDesc[Index].Type));
191 if ((MemoryDescHob.MemDesc[Index].Type == EfiRuntimeServicesData) ||
192 (MemoryDescHob.MemDesc[Index].Type == EfiRuntimeServicesCode)) {
193 //
194 // Skip RuntimeSevicesData and RuntimeServicesCode, they are BFV
195 //
196 continue;
197 }
198 Status = gDS->AddMemorySpace (
199 EfiGcdMemoryTypeSystemMemory,
200 MemoryDescHob.MemDesc[Index].PhysicalStart,
201 LShiftU64 (MemoryDescHob.MemDesc[Index].NumberOfPages, EFI_PAGE_SHIFT),
202 MemoryDescHob.MemDesc[Index].Attribute
203 );
204 if (EFI_ERROR (Status)) {
205 DEBUG ((EFI_D_ERROR, "AddMemorySpace fail!\n"));
206 if ((MemoryDescHob.MemDesc[Index].Type == EfiACPIReclaimMemory) ||
207 (MemoryDescHob.MemDesc[Index].Type == EfiACPIMemoryNVS)) {
208 //
209 // For EfiACPIReclaimMemory and EfiACPIMemoryNVS, it must success.
210 // For EfiReservedMemoryType, there maybe overlap. So skip check here.
211 //
212 // ASSERT_EFI_ERROR (Status);
213 }
214 continue;
215 }
216
217 Memory = MemoryDescHob.MemDesc[Index].PhysicalStart;
218 Status = gBS->AllocatePages (
219 AllocateAddress,
220 MemoryDescHob.MemDesc[Index].Type,
221 (UINTN)MemoryDescHob.MemDesc[Index].NumberOfPages,
222 &Memory
223 );
224 if (EFI_ERROR (Status)) {
225 DEBUG ((EFI_D_ERROR, "AllocatePages fail!\n"));
226 //
227 // For the page added, it must be allocated.
228 //
229 // ASSERT_EFI_ERROR (Status);
230 continue;
231 }
232 }
233 }
234
235 }
236
237 EFI_STATUS
238 DisableUsbLegacySupport(
239 void
240 )
241 /*++
242
243 Routine Description:
244 Disabble the USB legacy Support in all Ehci and Uhci.
245 This function assume all PciIo handles have been created in system.
246
247 Arguments:
248 None
249
250 Returns:
251 EFI_SUCCESS
252 EFI_NOT_FOUND
253 --*/
254 {
255 EFI_STATUS Status;
256 EFI_HANDLE *HandleArray;
257 UINTN HandleArrayCount;
258 UINTN Index;
259 EFI_PCI_IO_PROTOCOL *PciIo;
260 UINT8 Class[3];
261 UINT16 Command;
262 UINT32 HcCapParams;
263 UINT32 ExtendCap;
264 UINT32 Value;
265 UINT32 TimeOut;
266
267 //
268 // Find the usb host controller
269 //
270 Status = gBS->LocateHandleBuffer (
271 ByProtocol,
272 &gEfiPciIoProtocolGuid,
273 NULL,
274 &HandleArrayCount,
275 &HandleArray
276 );
277 if (!EFI_ERROR (Status)) {
278 for (Index = 0; Index < HandleArrayCount; Index++) {
279 Status = gBS->HandleProtocol (
280 HandleArray[Index],
281 &gEfiPciIoProtocolGuid,
282 (VOID **)&PciIo
283 );
284 if (!EFI_ERROR (Status)) {
285 //
286 // Find the USB host controller controller
287 //
288 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0x09, 3, &Class);
289 if (!EFI_ERROR (Status)) {
290 if ((PCI_CLASS_SERIAL == Class[2]) &&
291 (PCI_CLASS_SERIAL_USB == Class[1])) {
292 if (PCI_CLASSC_PI_UHCI == Class[0]) {
293 //
294 // Found the UHCI, then disable the legacy support
295 //
296 Command = 0;
297 Status = PciIo->Pci.Write (PciIo, EfiPciIoWidthUint16, 0xC0, 1, &Command);
298 } else if (PCI_CLASSC_PI_EHCI == Class[0]) {
299 //
300 // Found the EHCI, then disable the legacy support
301 //
302 Status = PciIo->Mem.Read (
303 PciIo,
304 EfiPciIoWidthUint32,
305 0, //EHC_BAR_INDEX
306 (UINT64) 0x08, //EHC_HCCPARAMS_OFFSET
307 1,
308 &HcCapParams
309 );
310
311 ExtendCap = (HcCapParams >> 8) & 0xFF;
312 //
313 // Disable the SMI in USBLEGCTLSTS firstly
314 //
315 PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, ExtendCap + 0x4, 1, &Value);
316 Value &= 0xFFFF0000;
317 PciIo->Pci.Write (PciIo, EfiPciIoWidthUint32, ExtendCap + 0x4, 1, &Value);
318
319 //
320 // Get EHCI Ownership from legacy bios
321 //
322 PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, ExtendCap, 1, &Value);
323 Value |= (0x1 << 24);
324 PciIo->Pci.Write (PciIo, EfiPciIoWidthUint32, ExtendCap, 1, &Value);
325
326 TimeOut = 40;
327 while (TimeOut--) {
328 gBS->Stall (500);
329
330 PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, ExtendCap, 1, &Value);
331
332 if ((Value & 0x01010000) == 0x01000000) {
333 break;
334 }
335 }
336 }
337 }
338 }
339 }
340 }
341 } else {
342 return Status;
343 }
344 gBS->FreePool (HandleArray);
345 return EFI_SUCCESS;
346 }
347
348
349 VOID
350 PlatformBdsInit (
351 IN EFI_BDS_ARCH_PROTOCOL_INSTANCE *PrivateData
352 )
353 /*++
354
355 Routine Description:
356
357 Platform Bds init. Incude the platform firmware vendor, revision
358 and so crc check.
359
360 Arguments:
361
362 PrivateData - The EFI_BDS_ARCH_PROTOCOL_INSTANCE instance
363
364 Returns:
365
366 None.
367
368 --*/
369 {
370 //
371 // set firmwarevendor, here can be IBV/OEM customize
372 //
373 gST->FirmwareVendor = AllocateRuntimeCopyPool (
374 sizeof (mFirmwareVendor),
375 &mFirmwareVendor
376 );
377 ASSERT (gST->FirmwareVendor != NULL);
378
379 gST->FirmwareRevision = 0;
380
381 //
382 // Fixup Tasble CRC after we updated Firmware Vendor and Revision
383 //
384 gBS->CalculateCrc32 ((VOID *) gST, sizeof (EFI_SYSTEM_TABLE), &gST->Hdr.CRC32);
385 }
386
387 UINT64
388 GetPciExpressBaseAddressForRootBridge (
389 IN UINTN HostBridgeNumber,
390 IN UINTN RootBridgeNumber
391 )
392 /*++
393
394 Routine Description:
395 This routine is to get PciExpress Base Address for this RootBridge
396
397 Arguments:
398 HostBridgeNumber - The number of HostBridge
399 RootBridgeNumber - The number of RootBridge
400
401 Returns:
402 UINT64 - PciExpressBaseAddress for this HostBridge and RootBridge
403
404 --*/
405 {
406 EFI_PCI_EXPRESS_BASE_ADDRESS_INFORMATION *PciExpressBaseAddressInfo;
407 UINTN BufferSize;
408 UINT32 Index;
409 UINT32 Number;
410 VOID *HobList;
411 EFI_STATUS Status;
412
413 BufferSize = 0;
414 //
415 // Get Hob List from configuration table
416 //
417 Status = EfiGetSystemConfigurationTable (&gEfiHobListGuid, &HobList);
418 if (EFI_ERROR (Status)) {
419 return 0;
420 }
421
422 //
423 // Get PciExpressAddressInfo Hob
424 //
425 PciExpressBaseAddressInfo = NULL;
426 PciExpressBaseAddressInfo = GetNextGuidHob (&gEfiPciExpressBaseAddressGuid, &HobList);
427
428 //
429 // Search the PciExpress Base Address in the Hob for current RootBridge
430 //
431 Number = (UINT32)(BufferSize / sizeof(EFI_PCI_EXPRESS_BASE_ADDRESS_INFORMATION));
432 for (Index = 0; Index < Number; Index++) {
433 if ((PciExpressBaseAddressInfo[Index].HostBridgeNumber == HostBridgeNumber) &&
434 (PciExpressBaseAddressInfo[Index].RootBridgeNumber == RootBridgeNumber)) {
435 return PciExpressBaseAddressInfo[Index].PciExpressBaseAddress;
436 }
437 }
438
439 //
440 // Do not find the PciExpress Base Address in the Hob
441 //
442 return 0;
443 }
444
445 VOID
446 PatchPciRootBridgeDevicePath (
447 IN UINTN HostBridgeNumber,
448 IN UINTN RootBridgeNumber,
449 IN PLATFORM_ROOT_BRIDGE_DEVICE_PATH *RootBridge
450 )
451 {
452 UINT64 PciExpressBase;
453
454 PciExpressBase = GetPciExpressBaseAddressForRootBridge (HostBridgeNumber, RootBridgeNumber);
455
456 if (PciExpressBase != 0) {
457 RootBridge->PciRootBridge.HID = EISA_PNP_ID(0x0A08);
458 }
459 }
460
461 EFI_STATUS
462 ConnectRootBridge (
463 VOID
464 )
465 /*++
466
467 Routine Description:
468
469 Connect RootBridge
470
471 Arguments:
472
473 None.
474
475 Returns:
476
477 EFI_SUCCESS - Connect RootBridge successfully.
478 EFI_STATUS - Connect RootBridge fail.
479
480 --*/
481 {
482 EFI_STATUS Status;
483 EFI_HANDLE RootHandle;
484
485 //
486 // Patch Pci Root Bridge Device Path
487 //
488 PatchPciRootBridgeDevicePath (0, 0, &gPlatformRootBridge0);
489
490 //
491 // Make all the PCI_IO protocols on PCI Seg 0 show up
492 //
493 BdsLibConnectDevicePath (gPlatformRootBridges[0]);
494
495 Status = gBS->LocateDevicePath (
496 &gEfiDevicePathProtocolGuid,
497 &gPlatformRootBridges[0],
498 &RootHandle
499 );
500 if (EFI_ERROR (Status)) {
501 return Status;
502 }
503
504 Status = gBS->ConnectController (RootHandle, NULL, NULL, FALSE);
505 if (EFI_ERROR (Status)) {
506 return Status;
507 }
508
509 return EFI_SUCCESS;
510 }
511
512 EFI_STATUS
513 PrepareLpcBridgeDevicePath (
514 IN EFI_HANDLE DeviceHandle
515 )
516 /*++
517
518 Routine Description:
519
520 Add IsaKeyboard to ConIn,
521 add IsaSerial to ConOut, ConIn, ErrOut.
522 LPC Bridge: 06 01 00
523
524 Arguments:
525
526 DeviceHandle - Handle of PCIIO protocol.
527
528 Returns:
529
530 EFI_SUCCESS - LPC bridge is added to ConOut, ConIn, and ErrOut.
531 EFI_STATUS - No LPC bridge is added.
532
533 --*/
534 {
535 EFI_STATUS Status;
536 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
537 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
538
539 DevicePath = NULL;
540 Status = gBS->HandleProtocol (
541 DeviceHandle,
542 &gEfiDevicePathProtocolGuid,
543 &DevicePath
544 );
545 if (EFI_ERROR (Status)) {
546 return Status;
547 }
548 TempDevicePath = DevicePath;
549
550 //
551 // Register Keyboard
552 //
553 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnpPs2KeyboardDeviceNode);
554
555 BdsLibUpdateConsoleVariable (VarConsoleInp, DevicePath, NULL);
556
557 //
558 // Register COM1
559 //
560 DevicePath = TempDevicePath;
561 gPnp16550ComPortDeviceNode.UID = 0;
562
563 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);
564 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);
565 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);
566
567 BdsLibUpdateConsoleVariable (VarConsoleOut, DevicePath, NULL);
568 BdsLibUpdateConsoleVariable (VarConsoleInp, DevicePath, NULL);
569 BdsLibUpdateConsoleVariable (VarErrorOut, DevicePath, NULL);
570
571 //
572 // Register COM2
573 //
574 DevicePath = TempDevicePath;
575 gPnp16550ComPortDeviceNode.UID = 1;
576
577 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);
578 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);
579 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);
580
581 BdsLibUpdateConsoleVariable (VarConsoleOut, DevicePath, NULL);
582 BdsLibUpdateConsoleVariable (VarConsoleInp, DevicePath, NULL);
583 BdsLibUpdateConsoleVariable (VarErrorOut, DevicePath, NULL);
584
585 return EFI_SUCCESS;
586 }
587
588 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
589 EFI_STATUS
590 GetGopDevicePath (
591 IN EFI_DEVICE_PATH_PROTOCOL *PciDevicePath,
592 OUT EFI_DEVICE_PATH_PROTOCOL **GopDevicePath
593 )
594 {
595 UINTN Index;
596 EFI_STATUS Status;
597 EFI_HANDLE PciDeviceHandle;
598 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
599 EFI_DEVICE_PATH_PROTOCOL *TempPciDevicePath;
600 UINTN GopHandleCount;
601 EFI_HANDLE *GopHandleBuffer;
602
603 if (PciDevicePath == NULL || GopDevicePath == NULL) {
604 return EFI_INVALID_PARAMETER;
605 }
606
607 //
608 // Initialize the GopDevicePath to be PciDevicePath
609 //
610 *GopDevicePath = PciDevicePath;
611 TempPciDevicePath = PciDevicePath;
612
613 Status = gBS->LocateDevicePath (
614 &gEfiDevicePathProtocolGuid,
615 &TempPciDevicePath,
616 &PciDeviceHandle
617 );
618 if (EFI_ERROR (Status)) {
619 return Status;
620 }
621
622 //
623 // Try to connect this handle, so that GOP dirver could start on this
624 // device and create child handles with GraphicsOutput Protocol installed
625 // on them, then we get device paths of these child handles and select
626 // them as possible console device.
627 //
628 gBS->ConnectController (PciDeviceHandle, NULL, NULL, FALSE);
629
630 Status = gBS->LocateHandleBuffer (
631 ByProtocol,
632 &gEfiGraphicsOutputProtocolGuid,
633 NULL,
634 &GopHandleCount,
635 &GopHandleBuffer
636 );
637 if (!EFI_ERROR (Status)) {
638 //
639 // Add all the child handles as possible Console Device
640 //
641 for (Index = 0; Index < GopHandleCount; Index++) {
642 Status = gBS->HandleProtocol (GopHandleBuffer[Index], &gEfiDevicePathProtocolGuid, &TempDevicePath);
643 if (EFI_ERROR (Status)) {
644 continue;
645 }
646 if (CompareMem (
647 PciDevicePath,
648 TempDevicePath,
649 GetDevicePathSize (PciDevicePath) - END_DEVICE_PATH_LENGTH
650 ) == 0) {
651 //
652 // In current implementation, we only enable one of the child handles
653 // as console device, i.e. sotre one of the child handle's device
654 // path to variable "ConOut"
655 // In futhure, we could select all child handles to be console device
656 //
657
658 *GopDevicePath = TempDevicePath;
659
660 //
661 // Delete the PCI device's path that added by GetPlugInPciVgaDevicePath()
662 // Add the integrity GOP device path.
663 //
664 BdsLibUpdateConsoleVariable (VarConsoleOutDev, NULL, PciDevicePath);
665 BdsLibUpdateConsoleVariable (VarConsoleOutDev, TempDevicePath, NULL);
666 }
667 }
668 gBS->FreePool (GopHandleBuffer);
669 }
670
671 return EFI_SUCCESS;
672 }
673 #endif
674
675 EFI_STATUS
676 PreparePciVgaDevicePath (
677 IN EFI_HANDLE DeviceHandle
678 )
679 /*++
680
681 Routine Description:
682
683 Add PCI VGA to ConOut.
684 PCI VGA: 03 00 00
685
686 Arguments:
687
688 DeviceHandle - Handle of PCIIO protocol.
689
690 Returns:
691
692 EFI_SUCCESS - PCI VGA is added to ConOut.
693 EFI_STATUS - No PCI VGA device is added.
694
695 --*/
696 {
697 EFI_STATUS Status;
698 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
699 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
700 EFI_DEVICE_PATH_PROTOCOL *GopDevicePath;
701 #endif
702
703 DevicePath = NULL;
704 Status = gBS->HandleProtocol (
705 DeviceHandle,
706 &gEfiDevicePathProtocolGuid,
707 &DevicePath
708 );
709 if (EFI_ERROR (Status)) {
710 return Status;
711 }
712
713 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
714 GetGopDevicePath (DevicePath, &GopDevicePath);
715 DevicePath = GopDevicePath;
716 #endif
717
718 BdsLibUpdateConsoleVariable (VarConsoleOut, DevicePath, NULL);
719
720 return EFI_SUCCESS;
721 }
722
723 EFI_STATUS
724 PreparePciSerialDevicePath (
725 IN EFI_HANDLE DeviceHandle
726 )
727 /*++
728
729 Routine Description:
730
731 Add PCI Serial to ConOut, ConIn, ErrOut.
732 PCI Serial: 07 00 02
733
734 Arguments:
735
736 DeviceHandle - Handle of PCIIO protocol.
737
738 Returns:
739
740 EFI_SUCCESS - PCI Serial is added to ConOut, ConIn, and ErrOut.
741 EFI_STATUS - No PCI Serial device is added.
742
743 --*/
744 {
745 EFI_STATUS Status;
746 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
747
748 DevicePath = NULL;
749 Status = gBS->HandleProtocol (
750 DeviceHandle,
751 &gEfiDevicePathProtocolGuid,
752 &DevicePath
753 );
754 if (EFI_ERROR (Status)) {
755 return Status;
756 }
757
758 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);
759 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);
760
761 BdsLibUpdateConsoleVariable (VarConsoleOut, DevicePath, NULL);
762 BdsLibUpdateConsoleVariable (VarConsoleInp, DevicePath, NULL);
763 BdsLibUpdateConsoleVariable (VarErrorOut, DevicePath, NULL);
764
765 return EFI_SUCCESS;
766 }
767
768 EFI_STATUS
769 DetectAndPreparePlatformPciDevicePath (
770 BOOLEAN DetectVgaOnly
771 )
772 /*++
773
774 Routine Description:
775
776 Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut
777
778 Arguments:
779
780 DetectVgaOnly - Only detect VGA device if it's TRUE.
781
782 Returns:
783
784 EFI_SUCCESS - PCI Device check and Console variable update successfully.
785 EFI_STATUS - PCI Device check or Console variable update fail.
786
787 --*/
788 {
789 EFI_STATUS Status;
790 UINTN HandleCount;
791 EFI_HANDLE *HandleBuffer;
792 UINTN Index;
793 EFI_PCI_IO_PROTOCOL *PciIo;
794 PCI_TYPE00 Pci;
795
796 //
797 // Start to check all the PciIo to find all possible device
798 //
799 HandleCount = 0;
800 HandleBuffer = NULL;
801 Status = gBS->LocateHandleBuffer (
802 ByProtocol,
803 &gEfiPciIoProtocolGuid,
804 NULL,
805 &HandleCount,
806 &HandleBuffer
807 );
808 if (EFI_ERROR (Status)) {
809 return Status;
810 }
811
812 for (Index = 0; Index < HandleCount; Index++) {
813 Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiPciIoProtocolGuid, &PciIo);
814 if (EFI_ERROR (Status)) {
815 continue;
816 }
817
818 //
819 // Check for all PCI device
820 //
821 Status = PciIo->Pci.Read (
822 PciIo,
823 EfiPciIoWidthUint32,
824 0,
825 sizeof (Pci) / sizeof (UINT32),
826 &Pci
827 );
828 if (EFI_ERROR (Status)) {
829 continue;
830 }
831
832 if (!DetectVgaOnly) {
833 //
834 // Here we decide whether it is LPC Bridge
835 //
836 if ((IS_PCI_LPC (&Pci)) ||
837 ((IS_PCI_ISA_PDECODE (&Pci)) && (Pci.Hdr.VendorId == 0x8086) && (Pci.Hdr.DeviceId == 0x7110))) {
838 //
839 // Add IsaKeyboard to ConIn,
840 // add IsaSerial to ConOut, ConIn, ErrOut
841 //
842 PrepareLpcBridgeDevicePath (HandleBuffer[Index]);
843 continue;
844 }
845 //
846 // Here we decide which Serial device to enable in PCI bus
847 //
848 if (IS_PCI_16550SERIAL (&Pci)) {
849 //
850 // Add them to ConOut, ConIn, ErrOut.
851 //
852 PreparePciSerialDevicePath (HandleBuffer[Index]);
853 continue;
854 }
855 }
856
857 //
858 // Here we decide which VGA device to enable in PCI bus
859 //
860 if (IS_PCI_VGA (&Pci)) {
861 //
862 // Add them to ConOut.
863 //
864 PreparePciVgaDevicePath (HandleBuffer[Index]);
865 continue;
866 }
867 }
868
869 gBS->FreePool (HandleBuffer);
870
871 return EFI_SUCCESS;
872 }
873
874 EFI_STATUS
875 PlatformBdsConnectConsole (
876 IN BDS_CONSOLE_CONNECT_ENTRY *PlatformConsole
877 )
878 /*++
879
880 Routine Description:
881
882 Connect the predefined platform default console device. Always try to find
883 and enable the vga device if have.
884
885 Arguments:
886
887 PlatformConsole - Predfined platform default console device array.
888
889 Returns:
890
891 EFI_SUCCESS - Success connect at least one ConIn and ConOut
892 device, there must have one ConOut device is
893 active vga device.
894
895 EFI_STATUS - Return the status of
896 BdsLibConnectAllDefaultConsoles ()
897
898 --*/
899 {
900 EFI_STATUS Status;
901 UINTN Index;
902 EFI_DEVICE_PATH_PROTOCOL *VarConout;
903 EFI_DEVICE_PATH_PROTOCOL *VarConin;
904 UINTN DevicePathSize;
905
906 //
907 // Connect RootBridge
908 //
909 ConnectRootBridge ();
910
911 VarConout = BdsLibGetVariableAndSize (
912 VarConsoleOut,
913 &gEfiGlobalVariableGuid,
914 &DevicePathSize
915 );
916 VarConin = BdsLibGetVariableAndSize (
917 VarConsoleInp,
918 &gEfiGlobalVariableGuid,
919 &DevicePathSize
920 );
921 if (VarConout == NULL || VarConin == NULL) {
922 //
923 // Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut
924 //
925 DetectAndPreparePlatformPciDevicePath (FALSE);
926
927 //
928 // Have chance to connect the platform default console,
929 // the platform default console is the minimue device group
930 // the platform should support
931 //
932 for (Index = 0; PlatformConsole[Index].DevicePath != NULL; ++Index) {
933 //
934 // Update the console variable with the connect type
935 //
936 if ((PlatformConsole[Index].ConnectType & CONSOLE_IN) == CONSOLE_IN) {
937 BdsLibUpdateConsoleVariable (VarConsoleInp, PlatformConsole[Index].DevicePath, NULL);
938 }
939 if ((PlatformConsole[Index].ConnectType & CONSOLE_OUT) == CONSOLE_OUT) {
940 BdsLibUpdateConsoleVariable (VarConsoleOut, PlatformConsole[Index].DevicePath, NULL);
941 }
942 if ((PlatformConsole[Index].ConnectType & STD_ERROR) == STD_ERROR) {
943 BdsLibUpdateConsoleVariable (VarErrorOut, PlatformConsole[Index].DevicePath, NULL);
944 }
945 }
946 } else {
947 //
948 // Only detect VGA device and add them to ConOut
949 //
950 DetectAndPreparePlatformPciDevicePath (TRUE);
951 }
952
953 //
954 // The ConIn devices connection will start the USB bus, should disable all
955 // Usb legacy support firstly.
956 // Caution: Must ensure the PCI bus driver has been started. Since the
957 // ConnectRootBridge() will create all the PciIo protocol, it's safe here now
958 //
959 Status = DisableUsbLegacySupport();
960
961 //
962 // Connect the all the default console with current cosole variable
963 //
964 Status = BdsLibConnectAllDefaultConsoles ();
965 if (EFI_ERROR (Status)) {
966 return Status;
967 }
968
969 return EFI_SUCCESS;
970 }
971
972 VOID
973 PlatformBdsConnectSequence (
974 VOID
975 )
976 /*++
977
978 Routine Description:
979
980 Connect with predeined platform connect sequence,
981 the OEM/IBV can customize with their own connect sequence.
982
983 Arguments:
984
985 None.
986
987 Returns:
988
989 None.
990
991 --*/
992 {
993 UINTN Index;
994
995 Index = 0;
996
997 //
998 // Here we can get the customized platform connect sequence
999 // Notes: we can connect with new variable which record the
1000 // last time boots connect device path sequence
1001 //
1002 while (gPlatformConnectSequence[Index] != NULL) {
1003 //
1004 // Build the platform boot option
1005 //
1006 BdsLibConnectDevicePath (gPlatformConnectSequence[Index]);
1007 Index++;
1008 }
1009
1010 }
1011
1012 VOID
1013 PlatformBdsGetDriverOption (
1014 IN OUT LIST_ENTRY *BdsDriverLists
1015 )
1016 /*++
1017
1018 Routine Description:
1019
1020 Load the predefined driver option, OEM/IBV can customize this
1021 to load their own drivers
1022
1023 Arguments:
1024
1025 BdsDriverLists - The header of the driver option link list.
1026
1027 Returns:
1028
1029 None.
1030
1031 --*/
1032 {
1033 UINTN Index;
1034
1035 Index = 0;
1036
1037 //
1038 // Here we can get the customized platform driver option
1039 //
1040 while (gPlatformDriverOption[Index] != NULL) {
1041 //
1042 // Build the platform boot option
1043 //
1044 BdsLibRegisterNewOption (BdsDriverLists, gPlatformDriverOption[Index], NULL, L"DriverOrder");
1045 Index++;
1046 }
1047
1048 }
1049
1050 VOID
1051 PlatformBdsDiagnostics (
1052 IN EXTENDMEM_COVERAGE_LEVEL MemoryTestLevel,
1053 IN BOOLEAN QuietBoot
1054 )
1055 /*++
1056
1057 Routine Description:
1058
1059 Perform the platform diagnostic, such like test memory. OEM/IBV also
1060 can customize this fuction to support specific platform diagnostic.
1061
1062 Arguments:
1063
1064 MemoryTestLevel - The memory test intensive level
1065
1066 QuietBoot - Indicate if need to enable the quiet boot
1067
1068 Returns:
1069
1070 None.
1071
1072 --*/
1073 {
1074 EFI_STATUS Status;
1075
1076 //
1077 // Here we can decide if we need to show
1078 // the diagnostics screen
1079 // Notes: this quiet boot code should be remove
1080 // from the graphic lib
1081 //
1082 if (QuietBoot) {
1083 EnableQuietBootEx (&gEfiDefaultBmpLogoGuid, mBdsImageHandle);
1084 //
1085 // Perform system diagnostic
1086 //
1087 Status = BdsMemoryTest (MemoryTestLevel);
1088 if (EFI_ERROR (Status)) {
1089 DisableQuietBoot ();
1090 }
1091
1092 return ;
1093 }
1094 //
1095 // Perform system diagnostic
1096 //
1097 Status = BdsMemoryTest (MemoryTestLevel);
1098 }
1099
1100 VOID
1101 PlatformBdsPolicyBehavior (
1102 IN EFI_BDS_ARCH_PROTOCOL_INSTANCE *PrivateData,
1103 IN OUT LIST_ENTRY *DriverOptionList,
1104 IN OUT LIST_ENTRY *BootOptionList
1105 )
1106 /*++
1107
1108 Routine Description:
1109
1110 The function will excute with as the platform policy, current policy
1111 is driven by boot mode. IBV/OEM can customize this code for their specific
1112 policy action.
1113
1114 Arguments:
1115
1116 PrivateData - The EFI_BDS_ARCH_PROTOCOL_INSTANCE instance
1117
1118 DriverOptionList - The header of the driver option link list
1119
1120 BootOptionList - The header of the boot option link list
1121
1122 Returns:
1123
1124 None.
1125
1126 --*/
1127 {
1128 EFI_STATUS Status;
1129 UINT16 Timeout;
1130 EFI_EVENT UserInputDurationTime;
1131 LIST_ENTRY *Link;
1132 BDS_COMMON_OPTION *BootOption;
1133 UINTN Index;
1134 EFI_INPUT_KEY Key;
1135 EFI_TPL OldTpl;
1136
1137 //
1138 // Init the time out value
1139 //
1140 Timeout = BdsLibGetTimeout ();
1141
1142 //
1143 // Load the driver option as the driver option list
1144 //
1145 PlatformBdsGetDriverOption (DriverOptionList);
1146
1147 //
1148 // Get current Boot Mode
1149 //
1150 Status = BdsLibGetBootMode (&PrivateData->BootMode);
1151 DEBUG ((EFI_D_ERROR, "Boot Mode:%x\n", PrivateData->BootMode));
1152
1153 //
1154 // Go the different platform policy with different boot mode
1155 // Notes: this part code can be change with the table policy
1156 //
1157 ASSERT (PrivateData->BootMode == BOOT_WITH_FULL_CONFIGURATION);
1158 //
1159 // Connect platform console
1160 //
1161 Status = PlatformBdsConnectConsole (gPlatformConsole);
1162 if (EFI_ERROR (Status)) {
1163 //
1164 // Here OEM/IBV can customize with defined action
1165 //
1166 PlatformBdsNoConsoleAction ();
1167 }
1168 //
1169 // Create a 300ms duration event to ensure user has enough input time to enter Setup
1170 //
1171 Status = gBS->CreateEvent (
1172 EFI_EVENT_TIMER,
1173 0,
1174 NULL,
1175 NULL,
1176 &UserInputDurationTime
1177 );
1178 ASSERT (Status == EFI_SUCCESS);
1179 Status = gBS->SetTimer (UserInputDurationTime, TimerRelative, 3000000);
1180 ASSERT (Status == EFI_SUCCESS);
1181 //
1182 // Memory test and Logo show
1183 //
1184 PlatformBdsDiagnostics (IGNORE, TRUE);
1185
1186 //
1187 // Perform some platform specific connect sequence
1188 //
1189 PlatformBdsConnectSequence ();
1190
1191 //
1192 // Give one chance to enter the setup if we
1193 // have the time out
1194 //
1195 if (Timeout != 0) {
1196 PlatformBdsEnterFrontPage (Timeout, FALSE);
1197 }
1198
1199 //
1200 //BdsLibConnectAll ();
1201 //BdsLibEnumerateAllBootOption (BootOptionList);
1202
1203 //
1204 // Please uncomment above ConnectAll and EnumerateAll code and remove following first boot
1205 // checking code in real production tip.
1206 //
1207 // In BOOT_WITH_FULL_CONFIGURATION boot mode, should always connect every device
1208 // and do enumerate all the default boot options. But in development system board, the boot mode
1209 // cannot be BOOT_ASSUMING_NO_CONFIGURATION_CHANGES because the machine box
1210 // is always open. So the following code only do the ConnectAll and EnumerateAll at first boot.
1211 //
1212 Status = BdsLibBuildOptionFromVar (BootOptionList, L"BootOrder");
1213 if (EFI_ERROR(Status)) {
1214 //
1215 // If cannot find "BootOrder" variable, it may be first boot.
1216 // Try to connect all devices and enumerate all boot options here.
1217 //
1218 BdsLibConnectAll ();
1219 BdsLibEnumerateAllBootOption (BootOptionList);
1220 }
1221
1222 //
1223 // To give the User a chance to enter Setup here, if user set TimeOut is 0.
1224 // BDS should still give user a chance to enter Setup
1225 //
1226 // Connect first boot option, and then check user input before exit
1227 //
1228 for (Link = BootOptionList->ForwardLink; Link != BootOptionList;Link = Link->ForwardLink) {
1229 BootOption = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);
1230 if (!IS_LOAD_OPTION_TYPE (BootOption->Attribute, LOAD_OPTION_ACTIVE)) {
1231 //
1232 // skip the header of the link list, becuase it has no boot option
1233 //
1234 continue;
1235 } else {
1236 //
1237 // Make sure the boot option device path connected, but ignore the BBS device path
1238 //
1239 if (DevicePathType (BootOption->DevicePath) != BBS_DEVICE_PATH) {
1240 BdsLibConnectDevicePath (BootOption->DevicePath);
1241 }
1242 break;
1243 }
1244 }
1245
1246 //
1247 // Check whether the user input after the duration time has expired
1248 //
1249 OldTpl = EfiGetCurrentTpl();
1250 gBS->RestoreTPL (TPL_APPLICATION);
1251 gBS->WaitForEvent (1, &UserInputDurationTime, &Index);
1252 gBS->CloseEvent (UserInputDurationTime);
1253 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
1254 gBS->RaiseTPL (OldTpl);
1255
1256 if (!EFI_ERROR (Status)) {
1257 //
1258 // Enter Setup if user input
1259 //
1260 Timeout = 0xffff;
1261 PlatformBdsEnterFrontPage (Timeout, FALSE);
1262 }
1263
1264 return ;
1265
1266 }
1267
1268 VOID
1269 PlatformBdsBootSuccess (
1270 IN BDS_COMMON_OPTION *Option
1271 )
1272 /*++
1273
1274 Routine Description:
1275
1276 Hook point after a boot attempt succeeds. We don't expect a boot option to
1277 return, so the EFI 1.0 specification defines that you will default to an
1278 interactive mode and stop processing the BootOrder list in this case. This
1279 is alos a platform implementation and can be customized by IBV/OEM.
1280
1281 Arguments:
1282
1283 Option - Pointer to Boot Option that succeeded to boot.
1284
1285 Returns:
1286
1287 None.
1288
1289 --*/
1290 {
1291 CHAR16 *TmpStr;
1292
1293 //
1294 // If Boot returned with EFI_SUCCESS and there is not in the boot device
1295 // select loop then we need to pop up a UI and wait for user input.
1296 //
1297 TmpStr = Option->StatusString;
1298 if (TmpStr != NULL) {
1299 BdsLibOutputStrings (gST->ConOut, TmpStr, Option->Description, L"\n\r", NULL);
1300 gBS->FreePool (TmpStr);
1301 }
1302 }
1303
1304 VOID
1305 PlatformBdsBootFail (
1306 IN BDS_COMMON_OPTION *Option,
1307 IN EFI_STATUS Status,
1308 IN CHAR16 *ExitData,
1309 IN UINTN ExitDataSize
1310 )
1311 /*++
1312
1313 Routine Description:
1314
1315 Hook point after a boot attempt fails.
1316
1317 Arguments:
1318
1319 Option - Pointer to Boot Option that failed to boot.
1320
1321 Status - Status returned from failed boot.
1322
1323 ExitData - Exit data returned from failed boot.
1324
1325 ExitDataSize - Exit data size returned from failed boot.
1326
1327 Returns:
1328
1329 None.
1330
1331 --*/
1332 {
1333 CHAR16 *TmpStr;
1334
1335 //
1336 // If Boot returned with failed status then we need to pop up a UI and wait
1337 // for user input.
1338 //
1339 TmpStr = Option->StatusString;
1340 if (TmpStr != NULL) {
1341 BdsLibOutputStrings (gST->ConOut, TmpStr, Option->Description, L"\n\r", NULL);
1342 gBS->FreePool (TmpStr);
1343 }
1344
1345 }
1346
1347 EFI_STATUS
1348 PlatformBdsNoConsoleAction (
1349 VOID
1350 )
1351 /*++
1352
1353 Routine Description:
1354
1355 This function is remained for IBV/OEM to do some platform action,
1356 if there no console device can be connected.
1357
1358 Arguments:
1359
1360 None.
1361
1362 Returns:
1363
1364 EFI_SUCCESS - Direct return success now.
1365
1366 --*/
1367 {
1368 return EFI_SUCCESS;
1369 }
1370
1371 EFI_STATUS
1372 ConvertSystemTable (
1373 IN EFI_GUID *TableGuid,
1374 IN OUT VOID **Table
1375 )
1376 /*++
1377
1378 Routine Description:
1379 Convert ACPI Table /Smbios Table /MP Table if its location is lower than Address:0x100000
1380 Assumption here:
1381 As in legacy Bios, ACPI/Smbios/MP table is required to place in E/F Seg,
1382 So here we just check if the range is E/F seg,
1383 and if Not, assume the Memory type is EfiACPIReclaimMemory/EfiACPIMemoryNVS
1384
1385 Arguments:
1386 TableGuid - Guid of the table
1387 Table - pointer to the table
1388
1389 Returns:
1390 EFI_SUCEESS - Convert Table successfully
1391 Other - Failed
1392
1393 --*/
1394 {
1395 EFI_STATUS Status;
1396 VOID *AcpiHeader;
1397 UINTN AcpiTableLen;
1398
1399 //
1400 // If match acpi guid (1.0, 2.0, or later), Convert ACPI table according to version.
1401 //
1402 AcpiHeader = (VOID*)(UINTN)(*(UINT64 *)(*Table));
1403
1404 if (CompareGuid(TableGuid, &gEfiAcpiTableGuid) || CompareGuid(TableGuid, &gEfiAcpi20TableGuid)){
1405 if (((EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)AcpiHeader)->Reserved == 0x00){
1406 //
1407 // If Acpi 1.0 Table, then RSDP structure doesn't contain Length field, use structure size
1408 //
1409 AcpiTableLen = sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
1410 } else if (((EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)AcpiHeader)->Reserved >= 0x02){
1411 //
1412 // If Acpi 2.0 or later, use RSDP Length fied.
1413 //
1414 AcpiTableLen = ((EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)AcpiHeader)->Length;
1415 } else {
1416 //
1417 // Invalid Acpi Version, return
1418 //
1419 return EFI_UNSUPPORTED;
1420 }
1421 Status = ConvertAcpiTable (AcpiTableLen, Table);
1422 return Status;
1423 }
1424
1425 //
1426 // If matches smbios guid, convert Smbios table.
1427 //
1428 if (CompareGuid(TableGuid, &gEfiSmbiosTableGuid)){
1429 Status = ConvertSmbiosTable (Table);
1430 return Status;
1431 }
1432
1433 //
1434 // If the table is MP table?
1435 //
1436 if (CompareGuid(TableGuid, &gEfiMpsTableGuid)){
1437 Status = ConvertMpsTable (Table);
1438 return Status;
1439 }
1440
1441 return EFI_UNSUPPORTED;
1442 }
1443
1444 UINT8
1445 GetBufferCheckSum (
1446 IN VOID * Buffer,
1447 IN UINTN Length
1448 )
1449 /*++
1450
1451 Routine Description:
1452 Caculate buffer checksum (8-bit)
1453
1454 Arguments:
1455 Buffer - Pointer to Buffer that to be caculated
1456 Length - How many bytes are to be caculated
1457
1458 Returns:
1459 Checksum of the buffer
1460
1461 --*/
1462 {
1463 UINT8 CheckSum;
1464 UINT8 *Ptr8;
1465
1466 CheckSum = 0;
1467 Ptr8 = (UINT8 *) Buffer;
1468
1469 while (Length > 0) {
1470 CheckSum = (UINT8) (CheckSum + *Ptr8++);
1471 Length--;
1472 }
1473
1474 return ((0xFF - CheckSum) + 1);
1475 }
1476
1477 EFI_STATUS
1478 ConvertAcpiTable (
1479 IN UINTN TableLen,
1480 IN OUT VOID **Table
1481 )
1482 /*++
1483
1484 Routine Description:
1485 Convert RSDP of ACPI Table if its location is lower than Address:0x100000
1486 Assumption here:
1487 As in legacy Bios, ACPI table is required to place in E/F Seg,
1488 So here we just check if the range is E/F seg,
1489 and if Not, assume the Memory type is EfiACPIReclaimMemory/EfiACPIMemoryNVS
1490
1491 Arguments:
1492 TableLen - Acpi RSDP length
1493 Table - pointer to the table
1494
1495 Returns:
1496 EFI_SUCEESS - Convert Table successfully
1497 Other - Failed
1498
1499 --*/
1500 {
1501 VOID *AcpiTableOri;
1502 VOID *AcpiTableNew;
1503 EFI_STATUS Status;
1504 EFI_PHYSICAL_ADDRESS BufferPtr;
1505
1506
1507 AcpiTableOri = (VOID *)(UINTN)(*(UINT64*)(*Table));
1508 if (((UINTN)AcpiTableOri < 0x100000) && ((UINTN)AcpiTableOri > 0xE0000)) {
1509 BufferPtr = EFI_SYSTEM_TABLE_MAX_ADDRESS;
1510 Status = gBS->AllocatePages (
1511 AllocateMaxAddress,
1512 EfiACPIMemoryNVS,
1513 EFI_SIZE_TO_PAGES(TableLen),
1514 &BufferPtr
1515 );
1516 ASSERT_EFI_ERROR (Status);
1517 AcpiTableNew = (VOID *)(UINTN)BufferPtr;
1518 CopyMem (AcpiTableNew, AcpiTableOri, TableLen);
1519 } else {
1520 AcpiTableNew = AcpiTableOri;
1521 }
1522 //
1523 // Change configuration table Pointer
1524 //
1525 *Table = AcpiTableNew;
1526
1527 return EFI_SUCCESS;
1528 }
1529
1530 EFI_STATUS
1531 ConvertSmbiosTable (
1532 IN OUT VOID **Table
1533 )
1534 /*++
1535
1536 Routine Description:
1537
1538 Convert Smbios Table if the Location of the SMBios Table is lower than Addres 0x100000
1539 Assumption here:
1540 As in legacy Bios, Smbios table is required to place in E/F Seg,
1541 So here we just check if the range is F seg,
1542 and if Not, assume the Memory type is EfiACPIMemoryNVS/EfiRuntimeServicesData
1543 Arguments:
1544 Table - pointer to the table
1545
1546 Returns:
1547 EFI_SUCEESS - Convert Table successfully
1548 Other - Failed
1549
1550 --*/
1551 {
1552 SMBIOS_TABLE_ENTRY_POINT *SmbiosTableNew;
1553 SMBIOS_TABLE_ENTRY_POINT *SmbiosTableOri;
1554 EFI_STATUS Status;
1555 UINT32 SmbiosEntryLen;
1556 UINT32 BufferLen;
1557 EFI_PHYSICAL_ADDRESS BufferPtr;
1558
1559 SmbiosTableNew = NULL;
1560 SmbiosTableOri = NULL;
1561
1562 //
1563 // Get Smibos configuration Table
1564 //
1565 SmbiosTableOri = (SMBIOS_TABLE_ENTRY_POINT *)(UINTN)(*(UINT64*)(*Table));
1566
1567 if ((SmbiosTableOri == NULL) ||
1568 ((UINTN)SmbiosTableOri > 0x100000) ||
1569 ((UINTN)SmbiosTableOri < 0xF0000)){
1570 return EFI_SUCCESS;
1571 }
1572 //
1573 // Relocate the Smibos memory
1574 //
1575 BufferPtr = EFI_SYSTEM_TABLE_MAX_ADDRESS;
1576 if (SmbiosTableOri->SmbiosBcdRevision != 0x21) {
1577 SmbiosEntryLen = SmbiosTableOri->EntryPointLength;
1578 } else {
1579 //
1580 // According to Smbios Spec 2.4, we should set entry point length as 0x1F if version is 2.1
1581 //
1582 SmbiosEntryLen = 0x1F;
1583 }
1584 BufferLen = SmbiosEntryLen + SYS_TABLE_PAD(SmbiosEntryLen) + SmbiosTableOri->TableLength;
1585 Status = gBS->AllocatePages (
1586 AllocateMaxAddress,
1587 EfiACPIMemoryNVS,
1588 EFI_SIZE_TO_PAGES(BufferLen),
1589 &BufferPtr
1590 );
1591 ASSERT_EFI_ERROR (Status);
1592 SmbiosTableNew = (SMBIOS_TABLE_ENTRY_POINT *)(UINTN)BufferPtr;
1593 CopyMem (
1594 SmbiosTableNew,
1595 SmbiosTableOri,
1596 SmbiosEntryLen
1597 );
1598 //
1599 // Get Smbios Structure table address, and make sure the start address is 32-bit align
1600 //
1601 BufferPtr += SmbiosEntryLen + SYS_TABLE_PAD(SmbiosEntryLen);
1602 CopyMem (
1603 (VOID *)(UINTN)BufferPtr,
1604 (VOID *)(UINTN)(SmbiosTableOri->TableAddress),
1605 SmbiosTableOri->TableLength
1606 );
1607 SmbiosTableNew->TableAddress = (UINT32)BufferPtr;
1608 SmbiosTableNew->IntermediateChecksum = 0;
1609 SmbiosTableNew->IntermediateChecksum =
1610 GetBufferCheckSum ((UINT8*)SmbiosTableNew + 0x10, SmbiosEntryLen -0x10);
1611 //
1612 // Change the SMBIOS pointer
1613 //
1614 *Table = SmbiosTableNew;
1615
1616 return EFI_SUCCESS;
1617 }
1618
1619 EFI_STATUS
1620 ConvertMpsTable (
1621 IN OUT VOID **Table
1622 )
1623 /*++
1624
1625 Routine Description:
1626
1627 Convert MP Table if the Location of the SMBios Table is lower than Addres 0x100000
1628 Assumption here:
1629 As in legacy Bios, MP table is required to place in E/F Seg,
1630 So here we just check if the range is E/F seg,
1631 and if Not, assume the Memory type is EfiACPIMemoryNVS/EfiRuntimeServicesData
1632 Arguments:
1633 Table - pointer to the table
1634
1635 Returns:
1636 EFI_SUCEESS - Convert Table successfully
1637 Other - Failed
1638
1639 --*/
1640 {
1641 UINT32 Data32;
1642 UINT32 FPLength;
1643 EFI_LEGACY_MP_TABLE_FLOATING_POINTER *MpsFloatingPointerOri;
1644 EFI_LEGACY_MP_TABLE_FLOATING_POINTER *MpsFloatingPointerNew;
1645 EFI_LEGACY_MP_TABLE_HEADER *MpsTableOri;
1646 EFI_LEGACY_MP_TABLE_HEADER *MpsTableNew;
1647 VOID *OemTableOri;
1648 VOID *OemTableNew;
1649 EFI_STATUS Status;
1650 EFI_PHYSICAL_ADDRESS BufferPtr;
1651
1652 //
1653 // Get MP configuration Table
1654 //
1655 MpsFloatingPointerOri = (EFI_LEGACY_MP_TABLE_FLOATING_POINTER *)(UINTN)(*(UINT64*)(*Table));
1656 if (!(((UINTN)MpsFloatingPointerOri <= 0x100000) &&
1657 ((UINTN)MpsFloatingPointerOri >= 0xF0000))){
1658 return EFI_SUCCESS;
1659 }
1660 //
1661 // Get Floating pointer structure length
1662 //
1663 FPLength = MpsFloatingPointerOri->Length * 16;
1664 Data32 = FPLength + SYS_TABLE_PAD (FPLength);
1665 MpsTableOri = (EFI_LEGACY_MP_TABLE_HEADER *)(UINTN)(MpsFloatingPointerOri->PhysicalAddress);
1666 if (MpsTableOri != NULL) {
1667 Data32 += MpsTableOri->BaseTableLength;
1668 Data32 += MpsTableOri->ExtendedTableLength;
1669 if (MpsTableOri->OemTablePointer != 0x00) {
1670 Data32 += SYS_TABLE_PAD (Data32);
1671 Data32 += MpsTableOri->OemTableSize;
1672 }
1673 } else {
1674 return EFI_SUCCESS;
1675 }
1676 //
1677 // Relocate memory
1678 //
1679 BufferPtr = EFI_SYSTEM_TABLE_MAX_ADDRESS;
1680 Status = gBS->AllocatePages (
1681 AllocateMaxAddress,
1682 EfiACPIMemoryNVS,
1683 EFI_SIZE_TO_PAGES(Data32),
1684 &BufferPtr
1685 );
1686 ASSERT_EFI_ERROR (Status);
1687 MpsFloatingPointerNew = (EFI_LEGACY_MP_TABLE_FLOATING_POINTER *)(UINTN)BufferPtr;
1688 CopyMem (MpsFloatingPointerNew, MpsFloatingPointerOri, FPLength);
1689 //
1690 // If Mp Table exists
1691 //
1692 if (MpsTableOri != NULL) {
1693 //
1694 // Get Mps table length, including Ext table
1695 //
1696 BufferPtr = BufferPtr + FPLength + SYS_TABLE_PAD (FPLength);
1697 MpsTableNew = (EFI_LEGACY_MP_TABLE_HEADER *)(UINTN)BufferPtr;
1698 CopyMem (MpsTableNew, MpsTableOri, MpsTableOri->BaseTableLength + MpsTableOri->ExtendedTableLength);
1699
1700 if ((MpsTableOri->OemTableSize != 0x0000) && (MpsTableOri->OemTablePointer != 0x0000)){
1701 BufferPtr += MpsTableOri->BaseTableLength + MpsTableOri->ExtendedTableLength;
1702 BufferPtr += SYS_TABLE_PAD (BufferPtr);
1703 OemTableNew = (VOID *)(UINTN)BufferPtr;
1704 OemTableOri = (VOID *)(UINTN)MpsTableOri->OemTablePointer;
1705 CopyMem (OemTableNew, OemTableOri, MpsTableOri->OemTableSize);
1706 MpsTableNew->OemTablePointer = (UINT32)(UINTN)OemTableNew;
1707 }
1708 MpsTableNew->Checksum = 0;
1709 MpsTableNew->Checksum = GetBufferCheckSum (MpsTableNew, MpsTableOri->BaseTableLength);
1710 MpsFloatingPointerNew->PhysicalAddress = (UINT32)(UINTN)MpsTableNew;
1711 MpsFloatingPointerNew->Checksum = 0;
1712 MpsFloatingPointerNew->Checksum = GetBufferCheckSum (MpsFloatingPointerNew, FPLength);
1713 }
1714 //
1715 // Change the pointer
1716 //
1717 *Table = MpsFloatingPointerNew;
1718
1719 return EFI_SUCCESS;
1720 }