]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Csm/LegacyBiosDxe/LegacyBootSupport.c
HPET driver was updated and included:
[mirror_edk2.git] / IntelFrameworkModulePkg / Csm / LegacyBiosDxe / LegacyBootSupport.c
CommitLineData
bcecde14 1/** @file\r
2\r
3Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
4\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions\r
7of the BSD License which accompanies this distribution. The\r
8full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "LegacyBiosInterface.h"\r
17#include <IndustryStandard/Pci.h>\r
18\r
19#define BOOT_LEGACY_OS 0\r
20#define BOOT_EFI_OS 1\r
21#define BOOT_UNCONVENTIONAL_DEVICE 2\r
22\r
23UINT32 mLoadOptionsSize = 0;\r
24UINTN mBootMode = BOOT_LEGACY_OS;\r
25VOID *mLoadOptions = NULL;\r
26BBS_BBS_DEVICE_PATH *mBbsDevicePathPtr = NULL;\r
27BBS_BBS_DEVICE_PATH mBbsDevicePathNode;\r
28UDC_ATTRIBUTES mAttributes = { 0, 0, 0, 0 };\r
29UINTN mBbsEntry = 0;\r
30VOID *mBeerData = NULL;\r
31VOID *mServiceAreaData = NULL;\r
32UINT64 mLowWater = 0xffffffffffffffffULL;\r
33\r
34extern BBS_TABLE *mBbsTable;\r
35\r
36/**\r
37 Print the BBS Table.\r
38\r
39 @param BbsTable The BBS table.\r
40\r
41\r
42**/\r
43VOID\r
44PrintBbsTable (\r
45 IN BBS_TABLE *BbsTable\r
46 )\r
47{\r
48 UINT16 Index;\r
49 UINT16 SubIndex;\r
50 CHAR8 *String;\r
51\r
52 DEBUG ((EFI_D_INFO, "\n"));\r
53 DEBUG ((EFI_D_INFO, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs mfgs:mfgo dess:deso\n"));\r
54 DEBUG ((EFI_D_INFO, "=================================================================\n"));\r
55 for (Index = 0; Index < MAX_BBS_ENTRIES; Index++) {\r
56 //\r
57 // Filter\r
58 //\r
59 if (BbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) {\r
60 continue;\r
61 }\r
62\r
63 DEBUG ((\r
64 EFI_D_INFO,\r
65 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x",\r
66 (UINTN) Index,\r
67 (UINTN) BbsTable[Index].BootPriority,\r
68 (UINTN) BbsTable[Index].Bus,\r
69 (UINTN) BbsTable[Index].Device,\r
70 (UINTN) BbsTable[Index].Function,\r
71 (UINTN) BbsTable[Index].Class,\r
72 (UINTN) BbsTable[Index].SubClass,\r
73 (UINTN) BbsTable[Index].DeviceType,\r
74 (UINTN) * (UINT16 *) &BbsTable[Index].StatusFlags\r
75 ));\r
76 DEBUG ((\r
77 EFI_D_INFO,\r
78 " %04x:%04x %04x:%04x %04x:%04x",\r
79 (UINTN) BbsTable[Index].BootHandlerSegment,\r
80 (UINTN) BbsTable[Index].BootHandlerOffset,\r
81 (UINTN) BbsTable[Index].MfgStringSegment,\r
82 (UINTN) BbsTable[Index].MfgStringOffset,\r
83 (UINTN) BbsTable[Index].DescStringSegment,\r
84 (UINTN) BbsTable[Index].DescStringOffset\r
85 ));\r
86\r
87 //\r
88 // Print DescString\r
89 //\r
90 String = (CHAR8 *)(UINTN)((BbsTable[Index].DescStringSegment << 4) + BbsTable[Index].DescStringOffset);\r
91 if (String != NULL) {\r
92 DEBUG ((EFI_D_INFO," ("));\r
93 for (SubIndex = 0; String[SubIndex] != 0; SubIndex++) {\r
94 DEBUG ((EFI_D_INFO, "%c", String[SubIndex]));\r
95 }\r
96 DEBUG ((EFI_D_INFO,")"));\r
97 }\r
98 DEBUG ((EFI_D_INFO,"\n"));\r
99 }\r
100\r
101 DEBUG ((EFI_D_INFO, "\n"));\r
102\r
103 return ;\r
104}\r
105\r
106/**\r
107 Print the BBS Table.\r
108\r
109 @param HddInfo The HddInfo table.\r
110\r
111\r
112**/\r
113VOID\r
114PrintHddInfo (\r
115 IN HDD_INFO *HddInfo\r
116 )\r
117{\r
118 UINTN Index;\r
119\r
120 DEBUG ((EFI_D_INFO, "\n"));\r
121 for (Index = 0; Index < MAX_IDE_CONTROLLER; Index++) {\r
122 DEBUG ((EFI_D_INFO, "Index - %04x\n", Index));\r
123 DEBUG ((EFI_D_INFO, " Status - %04x\n", (UINTN)HddInfo[Index].Status));\r
124 DEBUG ((EFI_D_INFO, " B/D/F - %02x/%02x/%02x\n", (UINTN)HddInfo[Index].Bus, (UINTN)HddInfo[Index].Device, (UINTN)HddInfo[Index].Function));\r
125 DEBUG ((EFI_D_INFO, " Command - %04x\n", HddInfo[Index].CommandBaseAddress));\r
126 DEBUG ((EFI_D_INFO, " Control - %04x\n", HddInfo[Index].ControlBaseAddress));\r
127 DEBUG ((EFI_D_INFO, " BusMaster - %04x\n", HddInfo[Index].BusMasterAddress));\r
128 DEBUG ((EFI_D_INFO, " HddIrq - %02x\n", HddInfo[Index].HddIrq));\r
129 DEBUG ((EFI_D_INFO, " IdentifyDrive[0].Raw[0] - %x\n", HddInfo[Index].IdentifyDrive[0].Raw[0]));\r
130 DEBUG ((EFI_D_INFO, " IdentifyDrive[1].Raw[0] - %x\n", HddInfo[Index].IdentifyDrive[1].Raw[0]));\r
131 }\r
132\r
133 DEBUG ((EFI_D_INFO, "\n"));\r
134\r
135 return ;\r
136}\r
137\r
7dad86fc
RN
138/**\r
139 Print the PCI Interrupt Line and Interrupt Pin registers.\r
140**/\r
141VOID\r
142PrintPciInterruptRegister (\r
143 VOID\r
144 )\r
145{\r
146 EFI_STATUS Status;\r
147 UINTN Index;\r
148 EFI_HANDLE *Handles;\r
149 UINTN HandleNum;\r
150 EFI_PCI_IO_PROTOCOL *PciIo;\r
151 UINT8 Interrupt[2];\r
152 UINTN Segment;\r
153 UINTN Bus;\r
154 UINTN Device;\r
155 UINTN Function;\r
156\r
157 gBS->LocateHandleBuffer (\r
158 ByProtocol,\r
159 &gEfiPciIoProtocolGuid,\r
160 NULL,\r
161 &HandleNum,\r
162 &Handles\r
163 );\r
164\r
165 Bus = 0;\r
166 Device = 0;\r
167 Function = 0;\r
168\r
169 DEBUG ((EFI_D_INFO, "\n"));\r
170 DEBUG ((EFI_D_INFO, " bb/dd/ff interrupt line interrupt pin\n"));\r
171 DEBUG ((EFI_D_INFO, "======================================\n"));\r
172 for (Index = 0; Index < HandleNum; Index++) {\r
173 Status = gBS->HandleProtocol (Handles[Index], &gEfiPciIoProtocolGuid, (VOID **) &PciIo);\r
174 if (!EFI_ERROR (Status)) {\r
175 Status = PciIo->Pci.Read (\r
176 PciIo,\r
177 EfiPciIoWidthUint8,\r
178 PCI_INT_LINE_OFFSET,\r
179 2,\r
180 Interrupt\r
181 );\r
182 }\r
183 if (!EFI_ERROR (Status)) {\r
184 Status = PciIo->GetLocation (\r
185 PciIo,\r
186 &Segment,\r
187 &Bus,\r
188 &Device,\r
189 &Function\r
190 );\r
191 }\r
192 if (!EFI_ERROR (Status)) {\r
193 DEBUG ((EFI_D_INFO, " %02x/%02x/%02x 0x%02x 0x%02x\n",\r
194 Bus, Device, Function, Interrupt[0], Interrupt[1]));\r
195 }\r
196 }\r
197 DEBUG ((EFI_D_INFO, "\n"));\r
198\r
199 if (Handles != NULL) {\r
200 FreePool (Handles);\r
201 }\r
202}\r
203\r
bcecde14 204/**\r
205 Identify drive data must be updated to actual parameters before boot.\r
206\r
207 @param IdentifyDriveData ATA Identify Data\r
208\r
209**/\r
210VOID\r
211UpdateIdentifyDriveData (\r
212 IN UINT8 *IdentifyDriveData\r
213 );\r
214\r
215/**\r
216 Update SIO data.\r
217\r
218 @param Private Legacy BIOS Instance data\r
219\r
220 @retval EFI_SUCCESS Removable media not present\r
221\r
222**/\r
223EFI_STATUS\r
224UpdateSioData (\r
225 IN LEGACY_BIOS_INSTANCE *Private\r
226 )\r
227{\r
228 EFI_STATUS Status;\r
229 UINTN Index;\r
230 UINTN Index1;\r
231 UINT8 LegacyInterrupts[16];\r
232 EFI_LEGACY_IRQ_ROUTING_ENTRY *RoutingTable;\r
233 UINTN RoutingTableEntries;\r
234 EFI_LEGACY_IRQ_PRIORITY_TABLE_ENTRY *IrqPriorityTable;\r
235 UINTN NumberPriorityEntries;\r
236 EFI_TO_COMPATIBILITY16_BOOT_TABLE *EfiToLegacy16BootTable;\r
237 UINT8 HddIrq;\r
238 UINT16 LegacyInt;\r
239 UINT16 LegMask;\r
240 UINT32 Register;\r
241 UINTN HandleCount;\r
242 EFI_HANDLE *HandleBuffer;\r
243 EFI_ISA_IO_PROTOCOL *IsaIo;\r
244\r
245 LegacyInt = 0;\r
246 HandleBuffer = NULL;\r
247\r
248 EfiToLegacy16BootTable = &Private->IntThunk->EfiToLegacy16BootTable;\r
249 LegacyBiosBuildSioData (Private);\r
250 SetMem (LegacyInterrupts, sizeof (LegacyInterrupts), 0);\r
251\r
252 //\r
253 // Create list of legacy interrupts.\r
254 //\r
255 for (Index = 0; Index < 4; Index++) {\r
256 LegacyInterrupts[Index] = EfiToLegacy16BootTable->SioData.Serial[Index].Irq;\r
257 }\r
258\r
259 for (Index = 4; Index < 7; Index++) {\r
260 LegacyInterrupts[Index] = EfiToLegacy16BootTable->SioData.Parallel[Index - 4].Irq;\r
261 }\r
262\r
263 LegacyInterrupts[7] = EfiToLegacy16BootTable->SioData.Floppy.Irq;\r
264\r
265 //\r
266 // Get Legacy Hdd IRQs. If native mode treat as PCI\r
267 //\r
268 for (Index = 0; Index < 2; Index++) {\r
269 HddIrq = EfiToLegacy16BootTable->HddInfo[Index].HddIrq;\r
270 if ((HddIrq != 0) && ((HddIrq == 15) || (HddIrq == 14))) {\r
271 LegacyInterrupts[Index + 8] = HddIrq;\r
272 }\r
273 }\r
274\r
275 Private->LegacyBiosPlatform->GetRoutingTable (\r
276 Private->LegacyBiosPlatform,\r
277 (VOID *) &RoutingTable,\r
278 &RoutingTableEntries,\r
279 NULL,\r
280 NULL,\r
281 (VOID **) &IrqPriorityTable,\r
282 &NumberPriorityEntries\r
283 );\r
284 //\r
285 // Remove legacy interrupts from the list of PCI interrupts available.\r
286 //\r
287 for (Index = 0; Index <= 0x0b; Index++) {\r
288 for (Index1 = 0; Index1 <= NumberPriorityEntries; Index1++) {\r
289 if (LegacyInterrupts[Index] != 0) {\r
290 LegacyInt = (UINT16) (LegacyInt | (1 << LegacyInterrupts[Index]));\r
291 if (LegacyInterrupts[Index] == IrqPriorityTable[Index1].Irq) {\r
292 IrqPriorityTable[Index1].Used = LEGACY_USED;\r
293 }\r
294 }\r
295 }\r
296 }\r
297\r
298 Private->Legacy8259->GetMask (\r
299 Private->Legacy8259,\r
300 &LegMask,\r
301 NULL,\r
302 NULL,\r
303 NULL\r
304 );\r
305\r
306 //\r
307 // Set SIO interrupts and disable mouse. Let mouse driver\r
308 // re-enable it.\r
309 //\r
310 LegMask = (UINT16) ((LegMask &~LegacyInt) | 0x1000);\r
311 Private->Legacy8259->SetMask (\r
312 Private->Legacy8259,\r
313 &LegMask,\r
314 NULL,\r
315 NULL,\r
316 NULL\r
317 );\r
318\r
319 //\r
320 // Disable mouse in keyboard controller\r
321 //\r
322 Register = 0xA7;\r
323 Status = gBS->LocateHandleBuffer (\r
324 ByProtocol,\r
325 &gEfiIsaIoProtocolGuid,\r
326 NULL,\r
327 &HandleCount,\r
328 &HandleBuffer\r
329 );\r
330 if (EFI_ERROR (Status)) {\r
331 return Status;\r
332 }\r
333\r
334 for (Index = 0; Index < HandleCount; Index++) {\r
335 Status = gBS->HandleProtocol (\r
336 HandleBuffer[Index],\r
337 &gEfiIsaIoProtocolGuid,\r
338 (VOID **) &IsaIo\r
339 );\r
340 ASSERT_EFI_ERROR (Status);\r
341 IsaIo->Io.Write (IsaIo, EfiIsaIoWidthUint8, 0x64, 1, &Register);\r
342\r
343 }\r
344\r
345 if (HandleBuffer != NULL) {\r
346 FreePool (HandleBuffer);\r
347 }\r
348\r
349 return EFI_SUCCESS;\r
350\r
351}\r
352\r
353/**\r
354 Identify drive data must be updated to actual parameters before boot.\r
355 This requires updating the checksum, if it exists.\r
356\r
357 @param IdentifyDriveData ATA Identify Data\r
358 @param Checksum checksum of the ATA Identify Data\r
359\r
360 @retval EFI_SUCCESS checksum calculated\r
361 @retval EFI_SECURITY_VIOLATION IdentifyData invalid\r
362\r
363**/\r
364EFI_STATUS\r
365CalculateIdentifyDriveChecksum (\r
366 IN UINT8 *IdentifyDriveData,\r
367 OUT UINT8 *Checksum\r
368 )\r
369{\r
370 UINTN Index;\r
371 UINT8 LocalChecksum;\r
372 LocalChecksum = 0;\r
373 *Checksum = 0;\r
374 if (IdentifyDriveData[510] != 0xA5) {\r
375 return EFI_SECURITY_VIOLATION;\r
376 }\r
377\r
378 for (Index = 0; Index < 512; Index++) {\r
379 LocalChecksum = (UINT8) (LocalChecksum + IdentifyDriveData[Index]);\r
380 }\r
381\r
382 *Checksum = LocalChecksum;\r
383 return EFI_SUCCESS;\r
384}\r
385\r
386\r
387/**\r
388 Identify drive data must be updated to actual parameters before boot.\r
389\r
390 @param IdentifyDriveData ATA Identify Data\r
391\r
392\r
393**/\r
394VOID\r
395UpdateIdentifyDriveData (\r
396 IN UINT8 *IdentifyDriveData\r
397 )\r
398{\r
399 UINT16 NumberCylinders;\r
400 UINT16 NumberHeads;\r
401 UINT16 NumberSectorsTrack;\r
402 UINT32 CapacityInSectors;\r
403 UINT8 OriginalChecksum;\r
404 UINT8 FinalChecksum;\r
405 EFI_STATUS Status;\r
406 ATAPI_IDENTIFY *ReadInfo;\r
407\r
408 //\r
409 // Status indicates if Integrity byte is correct. Checksum should be\r
410 // 0 if valid.\r
411 //\r
412 ReadInfo = (ATAPI_IDENTIFY *) IdentifyDriveData;\r
413 Status = CalculateIdentifyDriveChecksum (IdentifyDriveData, &OriginalChecksum);\r
414 if (OriginalChecksum != 0) {\r
415 Status = EFI_SECURITY_VIOLATION;\r
416 }\r
417 //\r
418 // If NumberCylinders = 0 then do data(Controller present but don drive attached).\r
419 //\r
420 NumberCylinders = ReadInfo->Raw[1];\r
421 if (NumberCylinders != 0) {\r
422 ReadInfo->Raw[54] = NumberCylinders;\r
423\r
424 NumberHeads = ReadInfo->Raw[3];\r
425 ReadInfo->Raw[55] = NumberHeads;\r
426\r
427 NumberSectorsTrack = ReadInfo->Raw[6];\r
428 ReadInfo->Raw[56] = NumberSectorsTrack;\r
429\r
430 //\r
431 // Copy Multisector info and set valid bit.\r
432 //\r
433 ReadInfo->Raw[59] = (UINT16) (ReadInfo->Raw[47] + 0x100);\r
434 CapacityInSectors = (UINT32) ((UINT32) (NumberCylinders) * (UINT32) (NumberHeads) * (UINT32) (NumberSectorsTrack));\r
435 ReadInfo->Raw[57] = (UINT16) (CapacityInSectors >> 16);\r
436 ReadInfo->Raw[58] = (UINT16) (CapacityInSectors & 0xffff);\r
437 if (Status == EFI_SUCCESS) {\r
438 //\r
439 // Forece checksum byte to 0 and get new checksum.\r
440 //\r
441 ReadInfo->Raw[255] &= 0xff;\r
442 CalculateIdentifyDriveChecksum (IdentifyDriveData, &FinalChecksum);\r
443\r
444 //\r
445 // Force new checksum such that sum is 0.\r
446 //\r
447 FinalChecksum = (UINT8) ((UINT8)0 - FinalChecksum);\r
448 ReadInfo->Raw[255] = (UINT16) (ReadInfo->Raw[255] | (FinalChecksum << 8));\r
449 }\r
450 }\r
451}\r
452\r
453/**\r
454 Identify drive data must be updated to actual parameters before boot.\r
455 Do for all drives.\r
456\r
457 @param Private Legacy BIOS Instance data\r
458\r
459\r
460**/\r
461VOID\r
462UpdateAllIdentifyDriveData (\r
463 IN LEGACY_BIOS_INSTANCE *Private\r
464 )\r
465{\r
466 UINTN Index;\r
467 HDD_INFO *HddInfo;\r
468\r
469 HddInfo = &Private->IntThunk->EfiToLegacy16BootTable.HddInfo[0];\r
470\r
471 for (Index = 0; Index < MAX_IDE_CONTROLLER; Index++) {\r
472 //\r
473 // Each controller can have 2 devices. Update for each device\r
474 //\r
475 if ((HddInfo[Index].Status & HDD_MASTER_IDE) != 0) {\r
476 UpdateIdentifyDriveData ((UINT8 *) (&HddInfo[Index].IdentifyDrive[0].Raw[0]));\r
477 }\r
478\r
479 if ((HddInfo[Index].Status & HDD_SLAVE_IDE) != 0) {\r
480 UpdateIdentifyDriveData ((UINT8 *) (&HddInfo[Index].IdentifyDrive[1].Raw[0]));\r
481 }\r
482 }\r
483}\r
484\r
485/**\r
486 Enable ide controller. This gets disabled when LegacyBoot.c is about\r
487 to run the Option ROMs.\r
488\r
489 @param Private Legacy BIOS Instance data\r
490\r
491\r
492**/\r
493VOID\r
494EnableIdeController (\r
495 IN LEGACY_BIOS_INSTANCE *Private\r
496 )\r
497{\r
498 EFI_PCI_IO_PROTOCOL *PciIo;\r
499 EFI_STATUS Status;\r
500 EFI_HANDLE IdeController;\r
501 UINT8 ByteBuffer;\r
502 UINTN HandleCount;\r
503 EFI_HANDLE *HandleBuffer;\r
504\r
505 Status = Private->LegacyBiosPlatform->GetPlatformHandle (\r
506 Private->LegacyBiosPlatform,\r
507 EfiGetPlatformIdeHandle,\r
508 0,\r
509 &HandleBuffer,\r
510 &HandleCount,\r
511 NULL\r
512 );\r
513 if (!EFI_ERROR (Status)) {\r
514 IdeController = HandleBuffer[0];\r
515 Status = gBS->HandleProtocol (\r
516 IdeController,\r
517 &gEfiPciIoProtocolGuid,\r
518 (VOID **) &PciIo\r
519 );\r
520 ByteBuffer = 0x1f;\r
521 if (!EFI_ERROR (Status)) {\r
522 PciIo->Pci.Write (PciIo, EfiPciIoWidthUint8, 0x04, 1, &ByteBuffer);\r
523 }\r
524 }\r
525}\r
526\r
527\r
528/**\r
529 Enable ide controller. This gets disabled when LegacyBoot.c is about\r
530 to run the Option ROMs.\r
531\r
532 @param Private Legacy BIOS Instance data\r
533\r
534\r
535**/\r
536VOID\r
537EnableAllControllers (\r
538 IN LEGACY_BIOS_INSTANCE *Private\r
539 )\r
540{\r
541 UINTN HandleCount;\r
542 EFI_HANDLE *HandleBuffer;\r
543 UINTN Index;\r
544 EFI_PCI_IO_PROTOCOL *PciIo;\r
545 PCI_TYPE01 PciConfigHeader;\r
546 EFI_STATUS Status;\r
547\r
548 //\r
549 //\r
550 //\r
551 EnableIdeController (Private);\r
552\r
553 //\r
554 // Assumption is table is built from low bus to high bus numbers.\r
555 //\r
556 Status = gBS->LocateHandleBuffer (\r
557 ByProtocol,\r
558 &gEfiPciIoProtocolGuid,\r
559 NULL,\r
560 &HandleCount,\r
561 &HandleBuffer\r
562 );\r
563 ASSERT_EFI_ERROR (Status);\r
564\r
565 for (Index = 0; Index < HandleCount; Index++) {\r
566 Status = gBS->HandleProtocol (\r
567 HandleBuffer[Index],\r
568 &gEfiPciIoProtocolGuid,\r
569 (VOID **) &PciIo\r
570 );\r
571 ASSERT_EFI_ERROR (Status);\r
572\r
573 PciIo->Pci.Read (\r
574 PciIo,\r
575 EfiPciIoWidthUint32,\r
576 0,\r
577 sizeof (PciConfigHeader) / sizeof (UINT32),\r
578 &PciConfigHeader\r
579 );\r
580\r
581 //\r
582 // We do not enable PPB here. This is for HotPlug Consideration.\r
583 // The Platform HotPlug Driver is responsible for Padding enough hot plug\r
584 // resources. It is also responsible for enable this bridge. If it\r
585 // does not pad it. It will cause some early Windows fail to installation.\r
586 // If the platform driver does not pad resource for PPB, PPB should be in\r
587 // un-enabled state to let Windows know that this PPB is not configured by\r
588 // BIOS. So Windows will allocate default resource for PPB.\r
589 //\r
590 // The reason for why we enable the command register is:\r
591 // The CSM will use the IO bar to detect some IRQ status, if the command\r
592 // is disabled, the IO resource will be out of scope.\r
593 // For example:\r
594 // We installed a legacy IRQ handle for a PCI IDE controller. When IRQ\r
595 // comes up, the handle will check the IO space to identify is the\r
596 // controller generated the IRQ source.\r
597 // If the IO command is not enabled, the IRQ handler will has wrong\r
598 // information. It will cause IRQ storm when the correctly IRQ handler fails\r
599 // to run.\r
600 //\r
601 if (!(IS_PCI_VGA (&PciConfigHeader) ||\r
602 IS_PCI_OLD_VGA (&PciConfigHeader) ||\r
603 IS_PCI_IDE (&PciConfigHeader) ||\r
604 IS_PCI_P2P (&PciConfigHeader) ||\r
605 IS_PCI_P2P_SUB (&PciConfigHeader) ||\r
606 IS_PCI_LPC (&PciConfigHeader) )) {\r
607\r
608 PciConfigHeader.Hdr.Command |= 0x1f;\r
609\r
610 PciIo->Pci.Write (PciIo, EfiPciIoWidthUint32, 4, 1, &PciConfigHeader.Hdr.Command);\r
611 }\r
612 }\r
613}\r
614\r
615/**\r
616 The following routines are identical in operation, so combine\r
617 for code compaction:\r
618 EfiGetPlatformBinaryGetMpTable\r
619 EfiGetPlatformBinaryGetOemIntData\r
620 EfiGetPlatformBinaryGetOem32Data\r
621 EfiGetPlatformBinaryGetOem16Data\r
622\r
623 @param This Protocol instance pointer.\r
624 @param Id Table/Data identifier\r
625\r
626 @retval EFI_SUCCESS Success\r
627 @retval EFI_INVALID_PARAMETER Invalid ID\r
628 @retval EFI_OUT_OF_RESOURCES no resource to get data or table\r
629\r
630**/\r
631EFI_STATUS\r
632LegacyGetDataOrTable (\r
633 IN EFI_LEGACY_BIOS_PROTOCOL *This,\r
634 IN EFI_GET_PLATFORM_INFO_MODE Id\r
635 )\r
636{\r
637 VOID *Table;\r
638 UINT32 TablePtr;\r
639 UINTN TableSize;\r
640 UINTN Alignment;\r
641 UINTN Location;\r
642 EFI_STATUS Status;\r
643 EFI_LEGACY_BIOS_PLATFORM_PROTOCOL *LegacyBiosPlatform;\r
644 EFI_COMPATIBILITY16_TABLE *Legacy16Table;\r
645 EFI_IA32_REGISTER_SET Regs;\r
646 LEGACY_BIOS_INSTANCE *Private;\r
647\r
648 Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);\r
649\r
650 LegacyBiosPlatform = Private->LegacyBiosPlatform;\r
651 Legacy16Table = Private->Legacy16Table;\r
652\r
653 //\r
654 // Phase 1 - get an address allocated in 16-bit code\r
655 //\r
656 while (TRUE) {\r
657 switch (Id) {\r
658 case EfiGetPlatformBinaryMpTable:\r
659 case EfiGetPlatformBinaryOemIntData:\r
660 case EfiGetPlatformBinaryOem32Data:\r
661 case EfiGetPlatformBinaryOem16Data:\r
662 {\r
663 Status = LegacyBiosPlatform->GetPlatformInfo (\r
664 LegacyBiosPlatform,\r
665 Id,\r
666 (VOID *) &Table,\r
667 &TableSize,\r
668 &Location,\r
669 &Alignment,\r
670 0,\r
671 0\r
672 );\r
673 DEBUG ((EFI_D_INFO, "LegacyGetDataOrTable - ID: %x, %r\n", (UINTN)Id, Status));\r
674 DEBUG ((EFI_D_INFO, " Table - %x, Size - %x, Location - %x, Alignment - %x\n", (UINTN)Table, (UINTN)TableSize, (UINTN)Location, (UINTN)Alignment));\r
675 break;\r
676 }\r
677\r
678 default:\r
679 {\r
680 return EFI_INVALID_PARAMETER;\r
681 }\r
682 }\r
683\r
684 if (EFI_ERROR (Status)) {\r
685 return Status;\r
686 }\r
687\r
688 ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));\r
689 Regs.X.AX = Legacy16GetTableAddress;\r
690 Regs.X.CX = (UINT16) TableSize;\r
691 Regs.X.BX = (UINT16) Location;\r
692 Regs.X.DX = (UINT16) Alignment;\r
693 Private->LegacyBios.FarCall86 (\r
694 This,\r
695 Private->Legacy16CallSegment,\r
696 Private->Legacy16CallOffset,\r
697 &Regs,\r
698 NULL,\r
699 0\r
700 );\r
701\r
702 if (Regs.X.AX != 0) {\r
703 DEBUG ((EFI_D_ERROR, "Table ID %x length insufficient\n", Id));\r
704 return EFI_OUT_OF_RESOURCES;\r
705 } else {\r
706 break;\r
707 }\r
708 }\r
709 //\r
710 // Phase 2 Call routine second time with address to allow address adjustment\r
711 //\r
712 Status = LegacyBiosPlatform->GetPlatformInfo (\r
713 LegacyBiosPlatform,\r
714 Id,\r
715 (VOID *) &Table,\r
716 &TableSize,\r
717 &Location,\r
718 &Alignment,\r
719 Regs.X.DS,\r
720 Regs.X.BX\r
721 );\r
722 switch (Id) {\r
723 case EfiGetPlatformBinaryMpTable:\r
724 {\r
725 Legacy16Table->MpTablePtr = (UINT32) (Regs.X.DS * 16 + Regs.X.BX);\r
726 Legacy16Table->MpTableLength = (UINT32)TableSize;\r
727 DEBUG ((EFI_D_INFO, "MP table in legacy region - %x\n", (UINTN)Legacy16Table->MpTablePtr));\r
728 break;\r
729 }\r
730\r
731 case EfiGetPlatformBinaryOemIntData:\r
732 {\r
733\r
734 Legacy16Table->OemIntSegment = Regs.X.DS;\r
735 Legacy16Table->OemIntOffset = Regs.X.BX;\r
736 DEBUG ((EFI_D_INFO, "OemInt table in legacy region - %04x:%04x\n", (UINTN)Legacy16Table->OemIntSegment, (UINTN)Legacy16Table->OemIntOffset));\r
737 break;\r
738 }\r
739\r
740 case EfiGetPlatformBinaryOem32Data:\r
741 {\r
742 Legacy16Table->Oem32Segment = Regs.X.DS;\r
743 Legacy16Table->Oem32Offset = Regs.X.BX;\r
744 DEBUG ((EFI_D_INFO, "Oem32 table in legacy region - %04x:%04x\n", (UINTN)Legacy16Table->Oem32Segment, (UINTN)Legacy16Table->Oem32Offset));\r
745 break;\r
746 }\r
747\r
748 case EfiGetPlatformBinaryOem16Data:\r
749 {\r
750 //\r
751 // Legacy16Table->Oem16Segment = Regs.X.DS;\r
752 // Legacy16Table->Oem16Offset = Regs.X.BX;\r
753 DEBUG ((EFI_D_INFO, "Oem16 table in legacy region - %04x:%04x\n", (UINTN)Legacy16Table->Oem16Segment, (UINTN)Legacy16Table->Oem16Offset));\r
754 break;\r
755 }\r
756\r
757 default:\r
758 {\r
759 return EFI_INVALID_PARAMETER;\r
760 }\r
761 }\r
762\r
763 if (EFI_ERROR (Status)) {\r
764 return Status;\r
765 }\r
766 //\r
767 // Phase 3 Copy table to final location\r
768 //\r
769 TablePtr = (UINT32) (Regs.X.DS * 16 + Regs.X.BX);\r
770\r
771 CopyMem (\r
772 (VOID *) (UINTN)TablePtr,\r
773 Table,\r
774 TableSize\r
775 );\r
776\r
777 return EFI_SUCCESS;\r
778}\r
779\r
780\r
781/**\r
782 Assign drive number to legacy HDD drives prior to booting an EFI\r
783 aware OS so the OS can access drives without an EFI driver.\r
784 Note: BBS compliant drives ARE NOT available until this call by\r
785 either shell or EFI.\r
786\r
787 @param This Protocol instance pointer.\r
788\r
789 @retval EFI_SUCCESS Drive numbers assigned\r
790\r
791**/\r
792EFI_STATUS\r
793GenericLegacyBoot (\r
794 IN EFI_LEGACY_BIOS_PROTOCOL *This\r
795 )\r
796{\r
797 EFI_STATUS Status;\r
798 LEGACY_BIOS_INSTANCE *Private;\r
799 EFI_IA32_REGISTER_SET Regs;\r
800 EFI_TO_COMPATIBILITY16_BOOT_TABLE *EfiToLegacy16BootTable;\r
801 EFI_LEGACY_BIOS_PLATFORM_PROTOCOL *LegacyBiosPlatform;\r
802 UINTN CopySize;\r
803 VOID *AcpiPtr;\r
804 HDD_INFO *HddInfo;\r
805 HDD_INFO *LocalHddInfo;\r
806 UINTN Index;\r
807 EFI_COMPATIBILITY16_TABLE *Legacy16Table;\r
808 UINT32 *BdaPtr;\r
809 UINT16 HddCount;\r
810 UINT16 BbsCount;\r
811 BBS_TABLE *LocalBbsTable;\r
812 UINT32 *BaseVectorMaster;\r
813 EFI_TIME BootTime;\r
814 UINT32 LocalTime;\r
815 EFI_HANDLE IdeController;\r
816 UINTN HandleCount;\r
817 EFI_HANDLE *HandleBuffer;\r
818 VOID *SmbiosTable;\r
819 VOID *AcpiTable;\r
820 UINTN ShadowAddress;\r
821 UINT32 Granularity;\r
bcecde14 822\r
823 LocalHddInfo = NULL;\r
824 HddCount = 0;\r
825 BbsCount = 0;\r
826 LocalBbsTable = NULL;\r
bcecde14 827\r
828 Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);\r
829 DEBUG_CODE (\r
830 DEBUG ((EFI_D_ERROR, "Start of legacy boot\n"));\r
831 );\r
832\r
833 Legacy16Table = Private->Legacy16Table;\r
834 EfiToLegacy16BootTable = &Private->IntThunk->EfiToLegacy16BootTable;\r
835 HddInfo = &EfiToLegacy16BootTable->HddInfo[0];\r
836\r
837 LegacyBiosPlatform = Private->LegacyBiosPlatform;\r
838\r
839 EfiToLegacy16BootTable->MajorVersion = EFI_TO_LEGACY_MAJOR_VERSION;\r
840 EfiToLegacy16BootTable->MinorVersion = EFI_TO_LEGACY_MINOR_VERSION;\r
bcecde14 841 \r
842 //\r
843 // If booting to a legacy OS then force HDD drives to the appropriate\r
844 // boot mode by calling GetIdeHandle.\r
845 // A reconnect -r can force all HDDs back to native mode.\r
846 //\r
847 IdeController = NULL;\r
848 if ((mBootMode == BOOT_LEGACY_OS) || (mBootMode == BOOT_UNCONVENTIONAL_DEVICE)) {\r
849 Status = LegacyBiosPlatform->GetPlatformHandle (\r
850 Private->LegacyBiosPlatform,\r
851 EfiGetPlatformIdeHandle,\r
852 0,\r
853 &HandleBuffer,\r
854 &HandleCount,\r
855 NULL\r
856 );\r
857 if (!EFI_ERROR (Status)) {\r
858 IdeController = HandleBuffer[0];\r
859 } \r
860 }\r
861 //\r
862 // Unlock the Legacy BIOS region\r
863 //\r
864 Private->LegacyRegion->UnLock (\r
865 Private->LegacyRegion,\r
866 0xE0000,\r
867 0x20000,\r
868 &Granularity\r
869 );\r
870\r
871 //\r
872 // Reconstruct the Legacy16 boot memory map\r
873 //\r
874 LegacyBiosBuildE820 (Private, &CopySize);\r
875 if (CopySize > Private->Legacy16Table->E820Length) {\r
876 ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));\r
877 Regs.X.AX = Legacy16GetTableAddress;\r
878 Regs.X.CX = (UINT16) CopySize;\r
879 Private->LegacyBios.FarCall86 (\r
880 &Private->LegacyBios,\r
881 Private->Legacy16Table->Compatibility16CallSegment,\r
882 Private->Legacy16Table->Compatibility16CallOffset,\r
883 &Regs,\r
884 NULL,\r
885 0\r
886 );\r
887\r
888 Private->Legacy16Table->E820Pointer = (UINT32) (Regs.X.DS * 16 + Regs.X.BX);\r
889 Private->Legacy16Table->E820Length = (UINT32) CopySize;\r
890 if (Regs.X.AX != 0) {\r
891 DEBUG ((EFI_D_ERROR, "Legacy16 E820 length insufficient\n"));\r
892 } else {\r
893 CopyMem (\r
894 (VOID *)(UINTN) Private->Legacy16Table->E820Pointer,\r
895 Private->E820Table,\r
896 CopySize\r
897 );\r
898 }\r
899 } else {\r
900 CopyMem (\r
901 (VOID *)(UINTN) Private->Legacy16Table->E820Pointer,\r
902 Private->E820Table,\r
903 CopySize\r
904 );\r
905 Private->Legacy16Table->E820Length = (UINT32) CopySize;\r
906 }\r
907 //\r
908 // Get SMBIOS and ACPI table pointers\r
909 //\r
910 SmbiosTable = NULL;\r
911 EfiGetSystemConfigurationTable (\r
912 &gEfiSmbiosTableGuid,\r
913 &SmbiosTable\r
914 );\r
915 //\r
916 // We do not ASSERT if SmbiosTable not found. It is possbile that a platform does not produce SmbiosTable.\r
917 //\r
918 if (SmbiosTable == NULL) {\r
919 DEBUG ((EFI_D_INFO, "Smbios table is not found!\n"));\r
920 }\r
921 EfiToLegacy16BootTable->SmbiosTable = (UINT32)(UINTN)SmbiosTable;\r
922\r
923 AcpiTable = NULL;\r
924 Status = EfiGetSystemConfigurationTable (\r
925 &gEfiAcpi20TableGuid,\r
926 &AcpiTable\r
927 );\r
928 if (EFI_ERROR (Status)) {\r
929 Status = EfiGetSystemConfigurationTable (\r
930 &gEfiAcpi10TableGuid,\r
931 &AcpiTable\r
932 );\r
933 }\r
934 //\r
935 // We do not ASSERT if AcpiTable not found. It is possbile that a platform does not produce AcpiTable.\r
936 //\r
937 if (AcpiTable == NULL) {\r
938 DEBUG ((EFI_D_INFO, "ACPI table is not found!\n"));\r
939 }\r
940 EfiToLegacy16BootTable->AcpiTable = (UINT32)(UINTN)AcpiTable;\r
941\r
942 //\r
943 // Get RSD Ptr table rev at offset 15 decimal\r
944 // Rev = 0 Length is 20 decimal\r
945 // Rev != 0 Length is UINT32 at offset 20 decimal\r
946 //\r
947 if (AcpiTable != NULL) {\r
948\r
949 AcpiPtr = AcpiTable;\r
950 if (*((UINT8 *) AcpiPtr + 15) == 0) {\r
951 CopySize = 20;\r
952 } else {\r
953 AcpiPtr = ((UINT8 *) AcpiPtr + 20);\r
954 CopySize = (*(UINT32 *) AcpiPtr);\r
955 }\r
956\r
957 CopyMem (\r
958 (VOID *)(UINTN) Private->Legacy16Table->AcpiRsdPtrPointer,\r
959 AcpiTable,\r
960 CopySize\r
961 );\r
962 }\r
963 //\r
964 // Make sure all PCI Interrupt Line register are programmed to match 8259\r
965 //\r
966 PciProgramAllInterruptLineRegisters (Private);\r
967\r
968 //\r
969 // Unlock the Legacy BIOS region as PciProgramAllInterruptLineRegisters\r
970 // can lock it.\r
971 //\r
972 Private->LegacyRegion->UnLock (\r
973 Private->LegacyRegion,\r
974 Private->BiosStart,\r
975 Private->LegacyBiosImageSize,\r
976 &Granularity\r
977 );\r
978\r
979 //\r
980 // Configure Legacy Device Magic\r
981 //\r
982 // Only do this code if booting legacy OS\r
983 //\r
984 if ((mBootMode == BOOT_LEGACY_OS) || (mBootMode == BOOT_UNCONVENTIONAL_DEVICE)) {\r
985 UpdateSioData (Private);\r
986 }\r
987 //\r
988 // Setup BDA and EBDA standard areas before Legacy Boot\r
989 //\r
990 LegacyBiosCompleteBdaBeforeBoot (Private);\r
991 LegacyBiosCompleteStandardCmosBeforeBoot (Private);\r
992\r
993 //\r
994 // We must build IDE data, if it hasn't been done, before PciShadowRoms\r
995 // to insure EFI drivers are connected.\r
996 //\r
997 LegacyBiosBuildIdeData (Private, &HddInfo, 1);\r
998 UpdateAllIdentifyDriveData (Private);\r
999\r
1000 //\r
1001 // Clear IO BAR, if IDE controller in legacy mode.\r
1002 //\r
1003 InitLegacyIdeController (IdeController);\r
1004\r
1005 //\r
1006 // Generate number of ticks since midnight for BDA. DOS requires this\r
1007 // for its time. We have to make assumptions as to how long following\r
1008 // code takes since after PciShadowRoms PciIo is gone. Place result in\r
1009 // 40:6C-6F\r
1010 //\r
1011 // Adjust value by 1 second.\r
1012 //\r
1013 gRT->GetTime (&BootTime, NULL);\r
1014 LocalTime = BootTime.Hour * 3600 + BootTime.Minute * 60 + BootTime.Second;\r
1015 LocalTime += 1;\r
1016\r
1017 //\r
1018 // Multiply result by 18.2 for number of ticks since midnight.\r
1019 // Use 182/10 to avoid floating point math.\r
1020 //\r
1021 LocalTime = (LocalTime * 182) / 10;\r
1022 BdaPtr = (UINT32 *) (UINTN)0x46C;\r
1023 *BdaPtr = LocalTime;\r
1024\r
1025 //\r
1026 // Shadow PCI ROMs. We must do this near the end since this will kick\r
1027 // of Native EFI drivers that may be needed to collect info for Legacy16\r
1028 //\r
1029 // WARNING: PciIo is gone after this call.\r
1030 //\r
1031 PciShadowRoms (Private);\r
1032\r
1033 //\r
1034 // Shadow PXE base code, BIS etc.\r
1035 //\r
1036 Private->LegacyRegion->UnLock (Private->LegacyRegion, 0xc0000, 0x40000, &Granularity);\r
1037 ShadowAddress = Private->OptionRom;\r
1038 Private->LegacyBiosPlatform->PlatformHooks (\r
1039 Private->LegacyBiosPlatform,\r
1040 EfiPlatformHookShadowServiceRoms,\r
1041 0,\r
1042 0,\r
1043 &ShadowAddress,\r
1044 Legacy16Table,\r
1045 NULL\r
1046 );\r
1047 Private->OptionRom = (UINT32)ShadowAddress;\r
1048 //\r
1049 // Register Legacy SMI Handler\r
1050 //\r
1051 LegacyBiosPlatform->SmmInit (\r
1052 LegacyBiosPlatform,\r
1053 EfiToLegacy16BootTable\r
1054 );\r
1055\r
1056 //\r
1057 // Let platform code know the boot options\r
1058 //\r
1059 LegacyBiosGetBbsInfo (\r
1060 This,\r
1061 &HddCount,\r
1062 &LocalHddInfo,\r
1063 &BbsCount,\r
1064 &LocalBbsTable\r
1065 );\r
1066\r
7dad86fc
RN
1067 DEBUG_CODE (\r
1068 PrintPciInterruptRegister ();\r
1069 PrintBbsTable (LocalBbsTable);\r
1070 PrintHddInfo (LocalHddInfo);\r
1071 );\r
bcecde14 1072 //\r
1073 // If drive wasn't spun up then BuildIdeData may have found new drives.\r
1074 // Need to update BBS boot priority.\r
1075 //\r
1076 for (Index = 0; Index < MAX_IDE_CONTROLLER; Index++) {\r
1077 if ((LocalHddInfo[Index].IdentifyDrive[0].Raw[0] != 0) &&\r
1078 (LocalBbsTable[2 * Index + 1].BootPriority == BBS_IGNORE_ENTRY)\r
1079 ) {\r
1080 LocalBbsTable[2 * Index + 1].BootPriority = BBS_UNPRIORITIZED_ENTRY;\r
1081 }\r
1082\r
1083 if ((LocalHddInfo[Index].IdentifyDrive[1].Raw[0] != 0) &&\r
1084 (LocalBbsTable[2 * Index + 2].BootPriority == BBS_IGNORE_ENTRY)\r
1085 ) {\r
1086 LocalBbsTable[2 * Index + 2].BootPriority = BBS_UNPRIORITIZED_ENTRY;\r
1087 }\r
1088 }\r
1089\r
1090 Private->LegacyRegion->UnLock (\r
1091 Private->LegacyRegion,\r
1092 0xc0000,\r
1093 0x40000,\r
1094 &Granularity\r
1095 );\r
1096\r
1097 LegacyBiosPlatform->PrepareToBoot (\r
1098 LegacyBiosPlatform,\r
1099 mBbsDevicePathPtr,\r
1100 mBbsTable,\r
1101 mLoadOptionsSize,\r
1102 mLoadOptions,\r
1103 (VOID *) &Private->IntThunk->EfiToLegacy16BootTable\r
1104 );\r
1105\r
1106 //\r
1107 // If no boot device return to BDS\r
1108 //\r
1109 if ((mBootMode == BOOT_LEGACY_OS) || (mBootMode == BOOT_UNCONVENTIONAL_DEVICE)) {\r
1110 for (Index = 0; Index < BbsCount; Index++){\r
1111 if ((LocalBbsTable[Index].BootPriority != BBS_DO_NOT_BOOT_FROM) &&\r
1112 (LocalBbsTable[Index].BootPriority != BBS_UNPRIORITIZED_ENTRY) &&\r
1113 (LocalBbsTable[Index].BootPriority != BBS_IGNORE_ENTRY)) {\r
1114 break;\r
1115 }\r
1116 }\r
1117 if (Index == BbsCount) {\r
1118 return EFI_DEVICE_ERROR;\r
1119 }\r
1120 }\r
1121 //\r
1122 // Let the Legacy16 code know the device path type for legacy boot\r
1123 //\r
1124 EfiToLegacy16BootTable->DevicePathType = mBbsDevicePathPtr->DeviceType;\r
1125\r
1126 //\r
1127 // Copy MP table, if it exists.\r
1128 //\r
1129 LegacyGetDataOrTable (This, EfiGetPlatformBinaryMpTable);\r
1130\r
1131 if (!Private->LegacyBootEntered) {\r
1132 //\r
1133 // Copy OEM INT Data, if it exists. Note: This code treats any data\r
1134 // as a bag of bits and knows nothing of the contents nor cares.\r
1135 // Contents are IBV specific.\r
1136 //\r
1137 LegacyGetDataOrTable (This, EfiGetPlatformBinaryOemIntData);\r
1138\r
1139 //\r
1140 // Copy OEM16 Data, if it exists.Note: This code treats any data\r
1141 // as a bag of bits and knows nothing of the contents nor cares.\r
1142 // Contents are IBV specific.\r
1143 //\r
1144 LegacyGetDataOrTable (This, EfiGetPlatformBinaryOem16Data);\r
1145\r
1146 //\r
1147 // Copy OEM32 Data, if it exists.Note: This code treats any data\r
1148 // as a bag of bits and knows nothing of the contents nor cares.\r
1149 // Contents are IBV specific.\r
1150 //\r
1151 LegacyGetDataOrTable (This, EfiGetPlatformBinaryOem32Data);\r
1152 }\r
1153\r
1154 //\r
1155 // Call into Legacy16 code to prepare for INT 19h\r
1156 //\r
1157 ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));\r
1158 Regs.X.AX = Legacy16PrepareToBoot;\r
1159\r
1160 //\r
1161 // Pass in handoff data\r
1162 //\r
1163 Regs.X.ES = EFI_SEGMENT ((UINTN)EfiToLegacy16BootTable);\r
1164 Regs.X.BX = EFI_OFFSET ((UINTN)EfiToLegacy16BootTable);\r
1165\r
1166 Private->LegacyBios.FarCall86 (\r
1167 This,\r
1168 Private->Legacy16CallSegment,\r
1169 Private->Legacy16CallOffset,\r
1170 &Regs,\r
1171 NULL,\r
1172 0\r
1173 );\r
1174\r
1175 if (Regs.X.AX != 0) {\r
1176 return EFI_DEVICE_ERROR;\r
1177 }\r
1178 //\r
1179 // Lock the Legacy BIOS region\r
1180 //\r
1181 Private->LegacyRegion->Lock (\r
1182 Private->LegacyRegion,\r
1183 0xc0000,\r
1184 0x40000,\r
1185 &Granularity\r
1186 );\r
1187 //\r
1188 // Lock attributes of the Legacy Region if chipset supports\r
1189 //\r
1190 Private->LegacyRegion->BootLock (\r
1191 Private->LegacyRegion,\r
1192 0xc0000,\r
1193 0x40000,\r
1194 &Granularity\r
1195 );\r
1196\r
1197 //\r
1198 // Call into Legacy16 code to do the INT 19h\r
1199 //\r
1200 EnableAllControllers (Private);\r
1201 if ((mBootMode == BOOT_LEGACY_OS) || (mBootMode == BOOT_UNCONVENTIONAL_DEVICE)) {\r
1202 //\r
1203 // Signal all the events that are waiting on EVT_SIGNAL_LEGACY_BOOT\r
1204 //\r
1205 EfiSignalEventLegacyBoot ();\r
1206 DEBUG ((EFI_D_INFO, "Legacy INT19 Boot...\n"));\r
1207 //\r
1208 // Raise TPL to high level to disable CPU interrupts\r
1209 //\r
1210 gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
1211\r
1212 //\r
1213 // Put the 8259 into its legacy mode by reprogramming the vector bases\r
1214 //\r
1215 Private->Legacy8259->SetVectorBase (Private->Legacy8259, LEGACY_MODE_BASE_VECTOR_MASTER, LEGACY_MODE_BASE_VECTOR_SLAVE);\r
1216 //\r
1217 // PC History\r
1218 // The original PC used INT8-F for master PIC. Since these mapped over\r
1219 // processor exceptions TIANO moved the master PIC to INT68-6F.\r
1220 // We need to set these back to the Legacy16 unexpected interrupt(saved\r
1221 // in LegacyBios.c) since some OS see that these have values different from\r
1222 // what is expected and invoke them. Since the legacy OS corrupts EFI\r
1223 // memory, there is no handler for these interrupts and OS blows up.\r
1224 //\r
1225 // We need to save the TIANO values for the rare case that the Legacy16\r
1226 // code cannot boot but knows memory hasn't been destroyed.\r
1227 //\r
1228 // To compound the problem, video takes over one of these INTS and must be\r
1229 // be left.\r
1230 // @bug - determine if video hooks INT(in which case we must find new\r
1231 // set of TIANO vectors) or takes it over.\r
1232 //\r
1233 //\r
1234 BaseVectorMaster = (UINT32 *) (sizeof (UINT32) * PROTECTED_MODE_BASE_VECTOR_MASTER);\r
1235 for (Index = 0; Index < 8; Index++) {\r
1236 Private->ThunkSavedInt[Index] = BaseVectorMaster[Index];\r
1237 if (Private->ThunkSeg == (UINT16) (BaseVectorMaster[Index] >> 16)) {\r
1238 BaseVectorMaster[Index] = (UINT32) (Private->BiosUnexpectedInt);\r
1239 }\r
1240 }\r
1241\r
1242 ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));\r
1243 Regs.X.AX = Legacy16Boot;\r
1244\r
1245 Private->LegacyBios.FarCall86 (\r
1246 This,\r
1247 Private->Legacy16CallSegment,\r
1248 Private->Legacy16CallOffset,\r
1249 &Regs,\r
1250 NULL,\r
1251 0\r
1252 );\r
1253\r
1254 BaseVectorMaster = (UINT32 *) (sizeof (UINT32) * PROTECTED_MODE_BASE_VECTOR_MASTER);\r
1255 for (Index = 0; Index < 8; Index++) {\r
1256 BaseVectorMaster[Index] = Private->ThunkSavedInt[Index];\r
1257 }\r
1258 }\r
1259 Private->LegacyBootEntered = TRUE;\r
1260 if ((mBootMode == BOOT_LEGACY_OS) || (mBootMode == BOOT_UNCONVENTIONAL_DEVICE)) {\r
1261 //\r
1262 // Should never return unless never passed control to 0:7c00(first stage\r
1263 // OS loader) and only then if no bootable device found.\r
1264 //\r
1265 return EFI_DEVICE_ERROR;\r
1266 } else {\r
1267 //\r
1268 // If boot to EFI then expect to return to caller\r
1269 //\r
1270 return EFI_SUCCESS;\r
1271 }\r
1272}\r
1273\r
1274\r
1275/**\r
1276 Assign drive number to legacy HDD drives prior to booting an EFI\r
1277 aware OS so the OS can access drives without an EFI driver.\r
1278 Note: BBS compliant drives ARE NOT available until this call by\r
1279 either shell or EFI.\r
1280\r
1281 @param This Protocol instance pointer.\r
1282 @param BbsCount Number of BBS_TABLE structures\r
1283 @param BbsTable List BBS entries\r
1284\r
1285 @retval EFI_SUCCESS Drive numbers assigned\r
1286\r
1287**/\r
1288EFI_STATUS\r
1289EFIAPI\r
1290LegacyBiosPrepareToBootEfi (\r
1291 IN EFI_LEGACY_BIOS_PROTOCOL *This,\r
1292 OUT UINT16 *BbsCount,\r
1293 OUT BBS_TABLE **BbsTable\r
1294 )\r
1295{\r
1296 EFI_STATUS Status;\r
1297 EFI_TO_COMPATIBILITY16_BOOT_TABLE *EfiToLegacy16BootTable;\r
1298 LEGACY_BIOS_INSTANCE *Private;\r
1299\r
1300 Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);\r
1301 EfiToLegacy16BootTable = &Private->IntThunk->EfiToLegacy16BootTable;\r
1302 mBootMode = BOOT_EFI_OS;\r
1303 mBbsDevicePathPtr = NULL;\r
1304 Status = GenericLegacyBoot (This);\r
1305 *BbsTable = (BBS_TABLE*)(UINTN)EfiToLegacy16BootTable->BbsTable;\r
1306 *BbsCount = (UINT16) (sizeof (Private->IntThunk->BbsTable) / sizeof (BBS_TABLE));\r
1307 return Status;\r
1308}\r
1309\r
1310/**\r
1311 To boot from an unconventional device like parties and/or execute HDD diagnostics.\r
1312\r
1313 @param This Protocol instance pointer.\r
1314 @param Attributes How to interpret the other input parameters\r
1315 @param BbsEntry The 0-based index into the BbsTable for the parent\r
1316 device.\r
1317 @param BeerData Pointer to the 128 bytes of ram BEER data.\r
1318 @param ServiceAreaData Pointer to the 64 bytes of raw Service Area data. The\r
1319 caller must provide a pointer to the specific Service\r
1320 Area and not the start all Service Areas.\r
1321\r
1322 @retval EFI_INVALID_PARAMETER if error. Does NOT return if no error.\r
1323\r
1324***/\r
1325EFI_STATUS\r
1326EFIAPI\r
1327LegacyBiosBootUnconventionalDevice (\r
1328 IN EFI_LEGACY_BIOS_PROTOCOL *This,\r
1329 IN UDC_ATTRIBUTES Attributes,\r
1330 IN UINTN BbsEntry,\r
1331 IN VOID *BeerData,\r
1332 IN VOID *ServiceAreaData\r
1333 )\r
1334{\r
1335 EFI_STATUS Status;\r
1336 EFI_TO_COMPATIBILITY16_BOOT_TABLE *EfiToLegacy16BootTable;\r
1337 LEGACY_BIOS_INSTANCE *Private;\r
1338 UD_TABLE *UcdTable;\r
1339 UINTN Index;\r
1340 UINT16 BootPriority;\r
1341 BBS_TABLE *BbsTable;\r
1342\r
1343 BootPriority = 0;\r
1344 Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);\r
1345 mBootMode = BOOT_UNCONVENTIONAL_DEVICE;\r
1346 mBbsDevicePathPtr = &mBbsDevicePathNode;\r
1347 mAttributes = Attributes;\r
1348 mBbsEntry = BbsEntry;\r
1349 mBeerData = BeerData, mServiceAreaData = ServiceAreaData;\r
1350\r
1351 EfiToLegacy16BootTable = &Private->IntThunk->EfiToLegacy16BootTable;\r
1352\r
1353 //\r
1354 // Do input parameter checking\r
1355 //\r
1356 if ((Attributes.DirectoryServiceValidity == 0) &&\r
1357 (Attributes.RabcaUsedFlag == 0) &&\r
1358 (Attributes.ExecuteHddDiagnosticsFlag == 0)\r
1359 ) {\r
1360 return EFI_INVALID_PARAMETER;\r
1361 }\r
1362\r
1363 if (((Attributes.DirectoryServiceValidity != 0) && (ServiceAreaData == NULL)) ||\r
1364 (((Attributes.DirectoryServiceValidity | Attributes.RabcaUsedFlag) != 0) && (BeerData == NULL))\r
1365 ) {\r
1366 return EFI_INVALID_PARAMETER;\r
1367 }\r
1368\r
1369 UcdTable = (UD_TABLE *) AllocatePool (\r
1370 sizeof (UD_TABLE)\r
1371 );\r
1372 if (NULL == UcdTable) {\r
1373 return EFI_OUT_OF_RESOURCES;\r
1374 }\r
1375\r
1376 EfiToLegacy16BootTable->UnconventionalDeviceTable = (UINT32)(UINTN)UcdTable;\r
1377 UcdTable->Attributes = Attributes;\r
1378 UcdTable->BbsTableEntryNumberForParentDevice = (UINT8) BbsEntry;\r
1379 //\r
1380 // Force all existing BBS entries to DoNotBoot. This allows 16-bit CSM\r
1381 // to assign drive numbers but bot boot from. Only newly created entries\r
1382 // will be valid.\r
1383 //\r
1384 BbsTable = (BBS_TABLE*)(UINTN)EfiToLegacy16BootTable->BbsTable;\r
1385 for (Index = 0; Index < EfiToLegacy16BootTable->NumberBbsEntries; Index++) {\r
1386 BbsTable[Index].BootPriority = BBS_DO_NOT_BOOT_FROM;\r
1387 }\r
1388 //\r
1389 // If parent is onboard IDE then assign controller & device number\r
1390 // else they are 0.\r
1391 //\r
1392 if (BbsEntry < MAX_IDE_CONTROLLER * 2) {\r
1393 UcdTable->DeviceNumber = (UINT8) ((BbsEntry - 1) % 2);\r
1394 }\r
1395\r
1396 if (BeerData != NULL) {\r
1397 CopyMem (\r
1398 (VOID *) UcdTable->BeerData,\r
1399 BeerData,\r
1400 (UINTN) 128\r
1401 );\r
1402 }\r
1403\r
1404 if (ServiceAreaData != NULL) {\r
1405 CopyMem (\r
1406 (VOID *) UcdTable->ServiceAreaData,\r
1407 ServiceAreaData,\r
1408 (UINTN) 64\r
1409 );\r
1410 }\r
1411 //\r
1412 // For each new entry do the following:\r
1413 // 1. Increment current number of BBS entries\r
1414 // 2. Copy parent entry to new entry.\r
1415 // 3. Zero out BootHandler Offset & segment\r
1416 // 4. Set appropriate device type. BEV(0x80) for HDD diagnostics\r
1417 // and Floppy(0x01) for PARTIES boot.\r
1418 // 5. Assign new priority.\r
1419 //\r
1420 if ((Attributes.ExecuteHddDiagnosticsFlag) != 0) {\r
1421 EfiToLegacy16BootTable->NumberBbsEntries += 1;\r
1422\r
1423 CopyMem (\r
1424 (VOID *) &BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].BootPriority,\r
1425 (VOID *) &BbsTable[BbsEntry].BootPriority,\r
1426 sizeof (BBS_TABLE)\r
1427 );\r
1428\r
1429 BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].BootHandlerOffset = 0;\r
1430 BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].BootHandlerSegment = 0;\r
1431 BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].DeviceType = 0x80;\r
1432\r
1433 UcdTable->BbsTableEntryNumberForHddDiag = (UINT8) (EfiToLegacy16BootTable->NumberBbsEntries - 1);\r
1434\r
1435 BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].BootPriority = BootPriority;\r
1436 BootPriority += 1;\r
1437\r
1438 //\r
1439 // Set device type as BBS_TYPE_DEV for PARTIES diagnostic\r
1440 //\r
1441 mBbsDevicePathNode.DeviceType = BBS_TYPE_BEV;\r
1442 }\r
1443\r
1444 if (((Attributes.DirectoryServiceValidity | Attributes.RabcaUsedFlag)) != 0) {\r
1445 EfiToLegacy16BootTable->NumberBbsEntries += 1;\r
1446 CopyMem (\r
1447 (VOID *) &BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].BootPriority,\r
1448 (VOID *) &BbsTable[BbsEntry].BootPriority,\r
1449 sizeof (BBS_TABLE)\r
1450 );\r
1451\r
1452 BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].BootHandlerOffset = 0;\r
1453 BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].BootHandlerSegment = 0;\r
1454 BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].DeviceType = 0x01;\r
1455 UcdTable->BbsTableEntryNumberForBoot = (UINT8) (EfiToLegacy16BootTable->NumberBbsEntries - 1);\r
1456 BbsTable[EfiToLegacy16BootTable->NumberBbsEntries].BootPriority = BootPriority;\r
1457\r
1458 //\r
1459 // Set device type as BBS_TYPE_FLOPPY for PARTIES boot as floppy\r
1460 //\r
1461 mBbsDevicePathNode.DeviceType = BBS_TYPE_FLOPPY;\r
1462 }\r
1463 //\r
1464 // Build the BBS Device Path for this boot selection\r
1465 //\r
1466 mBbsDevicePathNode.Header.Type = BBS_DEVICE_PATH;\r
1467 mBbsDevicePathNode.Header.SubType = BBS_BBS_DP;\r
1468 SetDevicePathNodeLength (&mBbsDevicePathNode.Header, sizeof (BBS_BBS_DEVICE_PATH));\r
1469 mBbsDevicePathNode.StatusFlag = 0;\r
1470 mBbsDevicePathNode.String[0] = 0;\r
1471\r
1472 Status = GenericLegacyBoot (This);\r
1473 return Status;\r
1474}\r
1475\r
1476/**\r
1477 Attempt to legacy boot the BootOption. If the EFI contexted has been\r
1478 compromised this function will not return.\r
1479\r
1480 @param This Protocol instance pointer.\r
1481 @param BbsDevicePath EFI Device Path from BootXXXX variable.\r
1482 @param LoadOptionsSize Size of LoadOption in size.\r
1483 @param LoadOptions LoadOption from BootXXXX variable\r
1484\r
1485 @retval EFI_SUCCESS Removable media not present\r
1486\r
1487**/\r
1488EFI_STATUS\r
1489EFIAPI\r
1490LegacyBiosLegacyBoot (\r
1491 IN EFI_LEGACY_BIOS_PROTOCOL *This,\r
1492 IN BBS_BBS_DEVICE_PATH *BbsDevicePath,\r
1493 IN UINT32 LoadOptionsSize,\r
1494 IN VOID *LoadOptions\r
1495 )\r
1496{\r
1497 EFI_STATUS Status;\r
1498\r
1499 mBbsDevicePathPtr = BbsDevicePath;\r
1500 mLoadOptionsSize = LoadOptionsSize;\r
1501 mLoadOptions = LoadOptions;\r
1502 mBootMode = BOOT_LEGACY_OS;\r
1503 Status = GenericLegacyBoot (This);\r
1504\r
1505 return Status;\r
1506}\r
1507\r
1508/**\r
1509 Convert EFI Memory Type to E820 Memory Type.\r
1510\r
1511 @param Type EFI Memory Type\r
1512\r
1513 @return ACPI Memory Type for EFI Memory Type\r
1514\r
1515**/\r
1516EFI_ACPI_MEMORY_TYPE\r
1517EfiMemoryTypeToE820Type (\r
1518 IN UINT32 Type\r
1519 )\r
1520{\r
1521 switch (Type) {\r
1522 case EfiLoaderCode:\r
1523 case EfiLoaderData:\r
1524 case EfiBootServicesCode:\r
1525 case EfiBootServicesData:\r
1526 case EfiConventionalMemory:\r
1527 case EfiRuntimeServicesCode:\r
1528 case EfiRuntimeServicesData:\r
1529 return EfiAcpiAddressRangeMemory;\r
1530\r
1531 case EfiACPIReclaimMemory:\r
1532 return EfiAcpiAddressRangeACPI;\r
1533\r
1534 case EfiACPIMemoryNVS:\r
1535 return EfiAcpiAddressRangeNVS;\r
1536\r
1537 //\r
1538 // All other types map to reserved.\r
1539 // Adding the code just waists FLASH space.\r
1540 //\r
1541 // case EfiReservedMemoryType:\r
1542 // case EfiUnusableMemory:\r
1543 // case EfiMemoryMappedIO:\r
1544 // case EfiMemoryMappedIOPortSpace:\r
1545 // case EfiPalCode:\r
1546 //\r
1547 default:\r
1548 return EfiAcpiAddressRangeReserved;\r
1549 }\r
1550}\r
1551\r
1552/**\r
1553 Build the E820 table.\r
1554\r
1555 @param Private Legacy BIOS Instance data\r
1556 @param Size Size of E820 Table\r
1557\r
1558 @retval EFI_SUCCESS It should always work.\r
1559\r
1560**/\r
1561EFI_STATUS\r
1562LegacyBiosBuildE820 (\r
1563 IN LEGACY_BIOS_INSTANCE *Private,\r
1564 OUT UINTN *Size\r
1565 )\r
1566{\r
1567 EFI_STATUS Status;\r
1568 EFI_E820_ENTRY64 *E820Table;\r
1569 EFI_MEMORY_DESCRIPTOR *EfiMemoryMap;\r
1570 EFI_MEMORY_DESCRIPTOR *EfiMemoryMapEnd;\r
1571 EFI_MEMORY_DESCRIPTOR *EfiEntry;\r
1572 EFI_MEMORY_DESCRIPTOR *NextEfiEntry;\r
1573 EFI_MEMORY_DESCRIPTOR TempEfiEntry;\r
1574 UINTN EfiMemoryMapSize;\r
1575 UINTN EfiMapKey;\r
1576 UINTN EfiDescriptorSize;\r
1577 UINT32 EfiDescriptorVersion;\r
1578 UINTN Index;\r
1579 EFI_PEI_HOB_POINTERS Hob;\r
1580 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;\r
1581 UINTN TempIndex;\r
1582 UINTN IndexSort;\r
1583 UINTN TempNextIndex;\r
1584 EFI_E820_ENTRY64 TempE820;\r
1585 EFI_ACPI_MEMORY_TYPE TempType;\r
1586 BOOLEAN ChangedFlag;\r
1587 UINTN Above1MIndex;\r
1588 UINT64 MemoryBlockLength;\r
1589\r
1590 E820Table = (EFI_E820_ENTRY64 *) Private->E820Table;\r
1591\r
1592 //\r
1593 // Get the EFI memory map.\r
1594 //\r
1595 EfiMemoryMapSize = 0;\r
1596 EfiMemoryMap = NULL;\r
1597 Status = gBS->GetMemoryMap (\r
1598 &EfiMemoryMapSize,\r
1599 EfiMemoryMap,\r
1600 &EfiMapKey,\r
1601 &EfiDescriptorSize,\r
1602 &EfiDescriptorVersion\r
1603 );\r
1604 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
1605\r
1606 do {\r
1607 //\r
1608 // Use size returned back plus 1 descriptor for the AllocatePool.\r
1609 // We don't just multiply by 2 since the "for" loop below terminates on\r
1610 // EfiMemoryMapEnd which is dependent upon EfiMemoryMapSize. Otherwize\r
1611 // we process bogus entries and create bogus E820 entries.\r
1612 //\r
1613 EfiMemoryMap = (EFI_MEMORY_DESCRIPTOR *) AllocatePool (EfiMemoryMapSize);\r
1614 ASSERT (EfiMemoryMap != NULL);\r
1615 Status = gBS->GetMemoryMap (\r
1616 &EfiMemoryMapSize,\r
1617 EfiMemoryMap,\r
1618 &EfiMapKey,\r
1619 &EfiDescriptorSize,\r
1620 &EfiDescriptorVersion\r
1621 );\r
1622 if (EFI_ERROR (Status)) {\r
1623 FreePool (EfiMemoryMap);\r
1624 }\r
1625 } while (Status == EFI_BUFFER_TOO_SMALL);\r
1626\r
1627 ASSERT_EFI_ERROR (Status);\r
1628\r
1629 //\r
1630 // Punch in the E820 table for memory less than 1 MB.\r
1631 // Assume ZeroMem () has been done on data structure.\r
1632 //\r
1633 //\r
1634 // First entry is 0 to (640k - EBDA)\r
1635 //\r
1636 E820Table[0].BaseAddr = 0;\r
1637 E820Table[0].Length = (UINT64) ((*(UINT16 *) (UINTN)0x40E) << 4);\r
1638 E820Table[0].Type = EfiAcpiAddressRangeMemory;\r
1639\r
1640 //\r
1641 // Second entry is (640k - EBDA) to 640k\r
1642 //\r
1643 E820Table[1].BaseAddr = E820Table[0].Length;\r
1644 E820Table[1].Length = (UINT64) ((640 * 1024) - E820Table[0].Length);\r
1645 E820Table[1].Type = EfiAcpiAddressRangeReserved;\r
1646\r
1647 //\r
1648 // Third Entry is legacy BIOS\r
1649 // DO NOT CLAIM region from 0xA0000-0xDFFFF. OS can use free areas\r
1650 // to page in memory under 1MB.\r
1651 // Omit region from 0xE0000 to start of BIOS, if any. This can be\r
1652 // used for a multiple reasons including OPROMS.\r
1653 //\r
1654\r
1655 //\r
1656 // The CSM binary image size is not the actually size that CSM binary used,\r
1657 // to avoid memory corrupt, we declare the 0E0000 - 0FFFFF is used by CSM binary.\r
1658 //\r
1659 E820Table[2].BaseAddr = 0xE0000;\r
1660 E820Table[2].Length = 0x20000;\r
1661 E820Table[2].Type = EfiAcpiAddressRangeReserved;\r
1662\r
1663 Above1MIndex = 2;\r
1664\r
1665 //\r
1666 // Process the EFI map to produce E820 map;\r
1667 //\r
1668\r
1669 //\r
1670 // Sort memory map from low to high\r
1671 //\r
1672 EfiEntry = EfiMemoryMap;\r
1673 NextEfiEntry = NEXT_MEMORY_DESCRIPTOR (EfiEntry, EfiDescriptorSize);\r
1674 EfiMemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) EfiMemoryMap + EfiMemoryMapSize);\r
1675 while (EfiEntry < EfiMemoryMapEnd) {\r
1676 while (NextEfiEntry < EfiMemoryMapEnd) {\r
1677 if (EfiEntry->PhysicalStart > NextEfiEntry->PhysicalStart) {\r
1678 CopyMem (&TempEfiEntry, EfiEntry, sizeof (EFI_MEMORY_DESCRIPTOR));\r
1679 CopyMem (EfiEntry, NextEfiEntry, sizeof (EFI_MEMORY_DESCRIPTOR));\r
1680 CopyMem (NextEfiEntry, &TempEfiEntry, sizeof (EFI_MEMORY_DESCRIPTOR));\r
1681 }\r
1682\r
1683 NextEfiEntry = NEXT_MEMORY_DESCRIPTOR (NextEfiEntry, EfiDescriptorSize);\r
1684 }\r
1685\r
1686 EfiEntry = NEXT_MEMORY_DESCRIPTOR (EfiEntry, EfiDescriptorSize);\r
1687 NextEfiEntry = NEXT_MEMORY_DESCRIPTOR (EfiEntry, EfiDescriptorSize);\r
1688 }\r
1689\r
1690 EfiEntry = EfiMemoryMap;\r
1691 EfiMemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) EfiMemoryMap + EfiMemoryMapSize);\r
1692 for (Index = Above1MIndex; (EfiEntry < EfiMemoryMapEnd) && (Index < EFI_MAX_E820_ENTRY - 1); ) {\r
1693 MemoryBlockLength = (UINT64) (LShiftU64 (EfiEntry->NumberOfPages, 12));\r
1694 if ((EfiEntry->PhysicalStart + MemoryBlockLength) < 0x100000) {\r
1695 //\r
1696 // Skip the memory block is under 1MB\r
1697 //\r
1698 } else {\r
1699 if (EfiEntry->PhysicalStart < 0x100000) {\r
1700 //\r
1701 // When the memory block spans below 1MB, ensure the memory block start address is at least 1MB\r
1702 //\r
1703 MemoryBlockLength -= 0x100000 - EfiEntry->PhysicalStart;\r
1704 EfiEntry->PhysicalStart = 0x100000;\r
1705 }\r
1706\r
1707 //\r
1708 // Convert memory type to E820 type\r
1709 //\r
1710 TempType = EfiMemoryTypeToE820Type (EfiEntry->Type);\r
1711\r
1712 if ((E820Table[Index].Type == TempType) && (EfiEntry->PhysicalStart == (E820Table[Index].BaseAddr + E820Table[Index].Length))) {\r
1713 //\r
1714 // Grow an existing entry\r
1715 //\r
1716 E820Table[Index].Length += MemoryBlockLength;\r
1717 } else {\r
1718 //\r
1719 // Make a new entry\r
1720 //\r
1721 ++Index;\r
1722 E820Table[Index].BaseAddr = EfiEntry->PhysicalStart;\r
1723 E820Table[Index].Length = MemoryBlockLength;\r
1724 E820Table[Index].Type = TempType;\r
1725 }\r
1726 }\r
1727 EfiEntry = NEXT_MEMORY_DESCRIPTOR (EfiEntry, EfiDescriptorSize);\r
1728 }\r
1729\r
1730 FreePool (EfiMemoryMap);\r
1731\r
1732 //\r
1733 // Process the reserved memory map to produce E820 map ;\r
1734 //\r
1735 for (Hob.Raw = GetHobList (); !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {\r
1736 if (Hob.Raw != NULL && GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
1737 ResourceHob = Hob.ResourceDescriptor;\r
1738 if (((ResourceHob->ResourceType == EFI_RESOURCE_MEMORY_MAPPED_IO) ||\r
1739 (ResourceHob->ResourceType == EFI_RESOURCE_FIRMWARE_DEVICE) ||\r
1740 (ResourceHob->ResourceType == EFI_RESOURCE_MEMORY_RESERVED) ) &&\r
1741 (ResourceHob->PhysicalStart > 0x100000) &&\r
1742 (Index < EFI_MAX_E820_ENTRY - 1)) {\r
1743 ++Index;\r
1744 E820Table[Index].BaseAddr = ResourceHob->PhysicalStart;\r
1745 E820Table[Index].Length = ResourceHob->ResourceLength;\r
1746 E820Table[Index].Type = EfiAcpiAddressRangeReserved;\r
1747 }\r
1748 }\r
1749 }\r
1750\r
1751 Index ++;\r
1752 Private->IntThunk->EfiToLegacy16InitTable.NumberE820Entries = (UINT32)Index;\r
1753 Private->IntThunk->EfiToLegacy16BootTable.NumberE820Entries = (UINT32)Index;\r
1754 Private->NumberE820Entries = (UINT32)Index;\r
1755 *Size = (UINTN) (Index * sizeof (EFI_E820_ENTRY64));\r
1756\r
1757 //\r
1758 // Sort E820Table from low to high\r
1759 //\r
1760 for (TempIndex = 0; TempIndex < Index; TempIndex++) {\r
1761 ChangedFlag = FALSE;\r
1762 for (TempNextIndex = 1; TempNextIndex < Index - TempIndex; TempNextIndex++) {\r
1763 if (E820Table[TempNextIndex - 1].BaseAddr > E820Table[TempNextIndex].BaseAddr) {\r
1764 ChangedFlag = TRUE;\r
1765 TempE820.BaseAddr = E820Table[TempNextIndex - 1].BaseAddr;\r
1766 TempE820.Length = E820Table[TempNextIndex - 1].Length;\r
1767 TempE820.Type = E820Table[TempNextIndex - 1].Type;\r
1768\r
1769 E820Table[TempNextIndex - 1].BaseAddr = E820Table[TempNextIndex].BaseAddr;\r
1770 E820Table[TempNextIndex - 1].Length = E820Table[TempNextIndex].Length;\r
1771 E820Table[TempNextIndex - 1].Type = E820Table[TempNextIndex].Type;\r
1772\r
1773 E820Table[TempNextIndex].BaseAddr = TempE820.BaseAddr;\r
1774 E820Table[TempNextIndex].Length = TempE820.Length;\r
1775 E820Table[TempNextIndex].Type = TempE820.Type;\r
1776 }\r
1777 }\r
1778\r
1779 if (!ChangedFlag) {\r
1780 break;\r
1781 }\r
1782 }\r
1783\r
1784 //\r
1785 // Remove the overlap range\r
1786 //\r
1787 for (TempIndex = 1; TempIndex < Index; TempIndex++) {\r
1788 if (E820Table[TempIndex - 1].BaseAddr <= E820Table[TempIndex].BaseAddr &&\r
1789 ((E820Table[TempIndex - 1].BaseAddr + E820Table[TempIndex - 1].Length) >=\r
1790 (E820Table[TempIndex].BaseAddr +E820Table[TempIndex].Length))) {\r
1791 //\r
1792 //Overlap range is found\r
1793 //\r
1794 ASSERT (E820Table[TempIndex - 1].Type == E820Table[TempIndex].Type);\r
1795\r
1796 if (TempIndex == Index - 1) {\r
1797 E820Table[TempIndex].BaseAddr = 0;\r
1798 E820Table[TempIndex].Length = 0;\r
1799 E820Table[TempIndex].Type = (EFI_ACPI_MEMORY_TYPE) 0;\r
1800 Index--;\r
1801 break;\r
1802 } else {\r
1803 for (IndexSort = TempIndex; IndexSort < Index - 1; IndexSort ++) {\r
1804 E820Table[IndexSort].BaseAddr = E820Table[IndexSort + 1].BaseAddr;\r
1805 E820Table[IndexSort].Length = E820Table[IndexSort + 1].Length;\r
1806 E820Table[IndexSort].Type = E820Table[IndexSort + 1].Type;\r
1807 }\r
1808 Index--;\r
1809 }\r
1810 }\r
1811 }\r
1812\r
1813\r
1814\r
1815 Private->IntThunk->EfiToLegacy16InitTable.NumberE820Entries = (UINT32)Index;\r
1816 Private->IntThunk->EfiToLegacy16BootTable.NumberE820Entries = (UINT32)Index;\r
1817 Private->NumberE820Entries = (UINT32)Index;\r
1818 *Size = (UINTN) (Index * sizeof (EFI_E820_ENTRY64));\r
1819\r
1820 //\r
1821 // Determine OS usable memory above 1Mb\r
1822 //\r
1823 Private->IntThunk->EfiToLegacy16BootTable.OsMemoryAbove1Mb = 0x0000;\r
1824 for (TempIndex = Above1MIndex; TempIndex < Index; TempIndex++) {\r
1825 if (E820Table[TempIndex].BaseAddr >= 0x100000 && E820Table[TempIndex].BaseAddr < 0x100000000ULL) { // not include above 4G memory\r
1826 //\r
1827 // ACPIReclaimMemory is also usable memory for ACPI OS, after OS dumps all ACPI tables.\r
1828 //\r
1829 if ((E820Table[TempIndex].Type == EfiAcpiAddressRangeMemory) || (E820Table[TempIndex].Type == EfiAcpiAddressRangeACPI)) {\r
1830 Private->IntThunk->EfiToLegacy16BootTable.OsMemoryAbove1Mb += (UINT32) (E820Table[TempIndex].Length);\r
1831 } else {\r
1832 break; // break at first not normal memory, because SMM may use reserved memory.\r
1833 }\r
1834 }\r
1835 }\r
1836\r
1837 Private->IntThunk->EfiToLegacy16InitTable.OsMemoryAbove1Mb = Private->IntThunk->EfiToLegacy16BootTable.OsMemoryAbove1Mb;\r
1838\r
1839 //\r
1840 // Print DEBUG information\r
1841 //\r
1842 for (TempIndex = 0; TempIndex < Index; TempIndex++) {\r
1843 DEBUG((EFI_D_INFO, "E820[%2d]: 0x%16lx ---- 0x%16lx, Type = 0x%x \n",\r
1844 TempIndex,\r
1845 E820Table[TempIndex].BaseAddr,\r
1846 (E820Table[TempIndex].BaseAddr + E820Table[TempIndex].Length),\r
1847 E820Table[TempIndex].Type\r
1848 ));\r
1849 }\r
1850\r
1851 return EFI_SUCCESS;\r
1852}\r
1853\r
1854\r
1855/**\r
1856 Fill in the standard BDA and EBDA stuff prior to legacy Boot\r
1857\r
1858 @param Private Legacy BIOS Instance data\r
1859\r
1860 @retval EFI_SUCCESS It should always work.\r
1861\r
1862**/\r
1863EFI_STATUS\r
1864LegacyBiosCompleteBdaBeforeBoot (\r
1865 IN LEGACY_BIOS_INSTANCE *Private\r
1866 )\r
1867{\r
1868 BDA_STRUC *Bda;\r
1869 UINT16 MachineConfig;\r
1870 DEVICE_PRODUCER_DATA_HEADER *SioPtr;\r
1871\r
1872 Bda = (BDA_STRUC *) ((UINTN) 0x400);\r
1873 MachineConfig = 0;\r
1874\r
1875 SioPtr = &(Private->IntThunk->EfiToLegacy16BootTable.SioData);\r
1876 Bda->Com1 = SioPtr->Serial[0].Address;\r
1877 Bda->Com2 = SioPtr->Serial[1].Address;\r
1878 Bda->Com3 = SioPtr->Serial[2].Address;\r
1879 Bda->Com4 = SioPtr->Serial[3].Address;\r
1880\r
1881 if (SioPtr->Serial[0].Address != 0x00) {\r
1882 MachineConfig += 0x200;\r
1883 }\r
1884\r
1885 if (SioPtr->Serial[1].Address != 0x00) {\r
1886 MachineConfig += 0x200;\r
1887 }\r
1888\r
1889 if (SioPtr->Serial[2].Address != 0x00) {\r
1890 MachineConfig += 0x200;\r
1891 }\r
1892\r
1893 if (SioPtr->Serial[3].Address != 0x00) {\r
1894 MachineConfig += 0x200;\r
1895 }\r
1896\r
1897 Bda->Lpt1 = SioPtr->Parallel[0].Address;\r
1898 Bda->Lpt2 = SioPtr->Parallel[1].Address;\r
1899 Bda->Lpt3 = SioPtr->Parallel[2].Address;\r
1900\r
1901 if (SioPtr->Parallel[0].Address != 0x00) {\r
1902 MachineConfig += 0x4000;\r
1903 }\r
1904\r
1905 if (SioPtr->Parallel[1].Address != 0x00) {\r
1906 MachineConfig += 0x4000;\r
1907 }\r
1908\r
1909 if (SioPtr->Parallel[2].Address != 0x00) {\r
1910 MachineConfig += 0x4000;\r
1911 }\r
1912\r
1913 Bda->NumberOfDrives = (UINT8) (Bda->NumberOfDrives + Private->IdeDriveCount);\r
1914 if (SioPtr->Floppy.NumberOfFloppy != 0x00) {\r
1915 MachineConfig = (UINT16) (MachineConfig + 0x01 + (SioPtr->Floppy.NumberOfFloppy - 1) * 0x40);\r
1916 Bda->FloppyXRate = 0x07;\r
1917 }\r
1918\r
1919 Bda->Lpt1_2Timeout = 0x1414;\r
1920 Bda->Lpt3_4Timeout = 0x1414;\r
1921 Bda->Com1_2Timeout = 0x0101;\r
1922 Bda->Com3_4Timeout = 0x0101;\r
1923\r
1924 //\r
1925 // Force VGA and Coprocessor, indicate 101/102 keyboard\r
1926 //\r
1927 MachineConfig = (UINT16) (MachineConfig + 0x00 + 0x02 + (SioPtr->MousePresent * 0x04));\r
1928 Bda->MachineConfig = MachineConfig;\r
1929\r
1930 return EFI_SUCCESS;\r
1931}\r
1932\r
1933/**\r
1934 Fill in the standard BDA for Keyboard LEDs\r
1935\r
1936 @param This Protocol instance pointer.\r
1937 @param Leds Current LED status\r
1938\r
1939 @retval EFI_SUCCESS It should always work.\r
1940\r
1941**/\r
1942EFI_STATUS\r
1943EFIAPI\r
1944LegacyBiosUpdateKeyboardLedStatus (\r
1945 IN EFI_LEGACY_BIOS_PROTOCOL *This,\r
1946 IN UINT8 Leds\r
1947 )\r
1948{\r
1949 LEGACY_BIOS_INSTANCE *Private;\r
1950 BDA_STRUC *Bda;\r
1951 UINT8 LocalLeds;\r
1952 EFI_IA32_REGISTER_SET Regs;\r
1953\r
1954 Bda = (BDA_STRUC *) ((UINTN) 0x400);\r
1955\r
1956 Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);\r
1957 LocalLeds = Leds;\r
1958 Bda->LedStatus = (UINT8) ((Bda->LedStatus &~0x07) | LocalLeds);\r
1959 LocalLeds = (UINT8) (LocalLeds << 4);\r
1960 Bda->ShiftStatus = (UINT8) ((Bda->ShiftStatus &~0x70) | LocalLeds);\r
1961 LocalLeds = (UINT8) (Leds & 0x20);\r
1962 Bda->KeyboardStatus = (UINT8) ((Bda->KeyboardStatus &~0x20) | LocalLeds);\r
1963 //\r
1964 // Call into Legacy16 code to allow it to do any processing\r
1965 //\r
1966 ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));\r
1967 Regs.X.AX = Legacy16SetKeyboardLeds;\r
1968 Regs.H.CL = Leds;\r
1969\r
1970 Private->LegacyBios.FarCall86 (\r
1971 &Private->LegacyBios,\r
1972 Private->Legacy16Table->Compatibility16CallSegment,\r
1973 Private->Legacy16Table->Compatibility16CallOffset,\r
1974 &Regs,\r
1975 NULL,\r
1976 0\r
1977 );\r
1978\r
1979 return EFI_SUCCESS;\r
1980}\r
1981\r
1982\r
1983/**\r
1984 Fill in the standard CMOS stuff prior to legacy Boot\r
1985\r
1986 @param Private Legacy BIOS Instance data\r
1987\r
1988 @retval EFI_SUCCESS It should always work.\r
1989\r
1990**/\r
1991EFI_STATUS\r
1992LegacyBiosCompleteStandardCmosBeforeBoot (\r
1993 IN LEGACY_BIOS_INSTANCE *Private\r
1994 )\r
1995{\r
1996 UINT8 Bda;\r
1997 UINT8 Floppy;\r
1998 UINT32 Size;\r
1999\r
2000 //\r
2001 // Update CMOS locations\r
2002 // 10 floppy\r
2003 // 12,19,1A - ignore as OS don't use them and there is no standard due\r
2004 // to large capacity drives\r
2005 // CMOS 14 = BDA 40:10 plus bit 3(display enabled)\r
2006 //\r
2007 Bda = (UINT8)(*((UINT8 *)((UINTN)0x410)) | BIT3);\r
2008\r
2009 //\r
2010 // Force display enabled\r
2011 //\r
2012 Floppy = 0x00;\r
2013 if ((Bda & BIT0) != 0) {\r
2014 Floppy = BIT6;\r
2015 }\r
2016\r
2017 //\r
2018 // Check if 2.88MB floppy set\r
2019 //\r
2020 if ((Bda & (BIT7 | BIT6)) != 0) {\r
2021 Floppy = (UINT8)(Floppy | BIT1);\r
2022 }\r
2023\r
2024 LegacyWriteStandardCmos (CMOS_10, Floppy);\r
2025 LegacyWriteStandardCmos (CMOS_14, Bda);\r
2026\r
2027 //\r
2028 // Force Status Register A to set rate selection bits and divider\r
2029 //\r
2030 LegacyWriteStandardCmos (CMOS_0A, 0x26);\r
2031\r
2032 //\r
2033 // redo memory size since it can change\r
2034 //\r
2035 Size = 15 * SIZE_1MB;\r
2036 if (Private->IntThunk->EfiToLegacy16InitTable.OsMemoryAbove1Mb < (15 * SIZE_1MB)) {\r
2037 Size = Private->IntThunk->EfiToLegacy16InitTable.OsMemoryAbove1Mb >> 10;\r
2038 }\r
2039\r
2040 LegacyWriteStandardCmos (CMOS_17, (UINT8)(Size & 0xFF));\r
2041 LegacyWriteStandardCmos (CMOS_30, (UINT8)(Size & 0xFF));\r
2042 LegacyWriteStandardCmos (CMOS_18, (UINT8)(Size >> 8));\r
2043 LegacyWriteStandardCmos (CMOS_31, (UINT8)(Size >> 8));\r
2044\r
2045 LegacyCalculateWriteStandardCmosChecksum ();\r
2046\r
2047 return EFI_SUCCESS;\r
2048}\r
2049\r
2050/**\r
2051 Relocate this image under 4G memory for IPF.\r
2052\r
2053 @param ImageHandle Handle of driver image.\r
2054 @param SystemTable Pointer to system table.\r
2055\r
2056 @retval EFI_SUCCESS Image successfully relocated.\r
2057 @retval EFI_ABORTED Failed to relocate image.\r
2058\r
2059**/\r
2060EFI_STATUS\r
2061RelocateImageUnder4GIfNeeded (\r
2062 IN EFI_HANDLE ImageHandle,\r
2063 IN EFI_SYSTEM_TABLE *SystemTable\r
2064 )\r
2065{\r
2066 return EFI_SUCCESS;\r
2067}\r