LegacySio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /** @file
  2. Collect Sio information from Native EFI Drivers.
  3. Sio is floppy, parallel, serial, ... hardware
  4. Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "LegacyBiosInterface.h"
  8. /**
  9. Collect EFI Info about legacy devices through Super IO interface.
  10. @param SioPtr Pointer to SIO data.
  11. @retval EFI_SUCCESS When SIO data is got successfully.
  12. @retval EFI_NOT_FOUND When ISA IO interface is absent.
  13. **/
  14. EFI_STATUS
  15. LegacyBiosBuildSioDataFromSio (
  16. IN DEVICE_PRODUCER_DATA_HEADER *SioPtr
  17. )
  18. {
  19. EFI_STATUS Status;
  20. DEVICE_PRODUCER_SERIAL *SioSerial;
  21. DEVICE_PRODUCER_PARALLEL *SioParallel;
  22. DEVICE_PRODUCER_FLOPPY *SioFloppy;
  23. UINTN HandleCount;
  24. EFI_HANDLE *HandleBuffer;
  25. UINTN Index;
  26. UINTN ChildIndex;
  27. EFI_SIO_PROTOCOL *Sio;
  28. ACPI_RESOURCE_HEADER_PTR Resources;
  29. EFI_ACPI_IO_PORT_DESCRIPTOR *IoResource;
  30. EFI_ACPI_FIXED_LOCATION_IO_PORT_DESCRIPTOR *FixedIoResource;
  31. EFI_ACPI_DMA_DESCRIPTOR *DmaResource;
  32. EFI_ACPI_IRQ_NOFLAG_DESCRIPTOR *IrqResource;
  33. UINT16 Address;
  34. UINT8 Dma;
  35. UINT8 Irq;
  36. UINTN EntryCount;
  37. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
  38. EFI_BLOCK_IO_PROTOCOL *BlockIo;
  39. EFI_SERIAL_IO_PROTOCOL *SerialIo;
  40. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  41. ACPI_HID_DEVICE_PATH *Acpi;
  42. //
  43. // Get the list of ISA controllers in the system
  44. //
  45. Status = gBS->LocateHandleBuffer (
  46. ByProtocol,
  47. &gEfiSioProtocolGuid,
  48. NULL,
  49. &HandleCount,
  50. &HandleBuffer
  51. );
  52. if (EFI_ERROR (Status)) {
  53. return EFI_NOT_FOUND;
  54. }
  55. //
  56. // Collect legacy information from each of the ISA controllers in the system
  57. //
  58. for (Index = 0; Index < HandleCount; Index++) {
  59. Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiSioProtocolGuid, (VOID **)&Sio);
  60. if (EFI_ERROR (Status)) {
  61. continue;
  62. }
  63. Address = MAX_UINT16;
  64. Dma = MAX_UINT8;
  65. Irq = MAX_UINT8;
  66. Status = Sio->GetResources (Sio, &Resources);
  67. if (!EFI_ERROR (Status)) {
  68. //
  69. // Get the base address information from ACPI resource descriptor.
  70. //
  71. while (Resources.SmallHeader->Byte != ACPI_END_TAG_DESCRIPTOR) {
  72. switch (Resources.SmallHeader->Byte) {
  73. case ACPI_IO_PORT_DESCRIPTOR:
  74. IoResource = (EFI_ACPI_IO_PORT_DESCRIPTOR *)Resources.SmallHeader;
  75. Address = IoResource->BaseAddressMin;
  76. break;
  77. case ACPI_FIXED_LOCATION_IO_PORT_DESCRIPTOR:
  78. FixedIoResource = (EFI_ACPI_FIXED_LOCATION_IO_PORT_DESCRIPTOR *)Resources.SmallHeader;
  79. Address = FixedIoResource->BaseAddress;
  80. break;
  81. case ACPI_DMA_DESCRIPTOR:
  82. DmaResource = (EFI_ACPI_DMA_DESCRIPTOR *)Resources.SmallHeader;
  83. Dma = (UINT8)LowBitSet32 (DmaResource->ChannelMask);
  84. break;
  85. case ACPI_IRQ_DESCRIPTOR:
  86. case ACPI_IRQ_NOFLAG_DESCRIPTOR:
  87. IrqResource = (EFI_ACPI_IRQ_NOFLAG_DESCRIPTOR *)Resources.SmallHeader;
  88. Irq = (UINT8)LowBitSet32 (IrqResource->Mask);
  89. break;
  90. default:
  91. break;
  92. }
  93. if (Resources.SmallHeader->Bits.Type == 0) {
  94. Resources.SmallHeader = (ACPI_SMALL_RESOURCE_HEADER *)((UINT8 *)Resources.SmallHeader
  95. + Resources.SmallHeader->Bits.Length
  96. + sizeof (*Resources.SmallHeader));
  97. } else {
  98. Resources.LargeHeader = (ACPI_LARGE_RESOURCE_HEADER *)((UINT8 *)Resources.LargeHeader
  99. + Resources.LargeHeader->Length
  100. + sizeof (*Resources.LargeHeader));
  101. }
  102. }
  103. }
  104. DEBUG ((DEBUG_INFO, "LegacySio: Address/Dma/Irq = %x/%d/%d\n", Address, Dma, Irq));
  105. DevicePath = DevicePathFromHandle (HandleBuffer[Index]);
  106. if (DevicePath == NULL) {
  107. continue;
  108. }
  109. Acpi = NULL;
  110. while (!IsDevicePathEnd (DevicePath)) {
  111. Acpi = (ACPI_HID_DEVICE_PATH *)DevicePath;
  112. DevicePath = NextDevicePathNode (DevicePath);
  113. }
  114. if ((Acpi == NULL) || (DevicePathType (Acpi) != ACPI_DEVICE_PATH) ||
  115. ((DevicePathSubType (Acpi) != ACPI_DP) && (DevicePathSubType (Acpi) != ACPI_EXTENDED_DP))
  116. )
  117. {
  118. continue;
  119. }
  120. //
  121. // See if this is an ISA serial port
  122. //
  123. // Ignore DMA resource since it is always returned NULL
  124. //
  125. if ((Acpi->HID == EISA_PNP_ID (0x500)) || (Acpi->HID == EISA_PNP_ID (0x501))) {
  126. if ((Acpi->UID < 4) && (Address != MAX_UINT16) && (Irq != MAX_UINT8)) {
  127. //
  128. // Get the handle of the child device that has opened the Super I/O Protocol
  129. //
  130. Status = gBS->OpenProtocolInformation (
  131. HandleBuffer[Index],
  132. &gEfiSioProtocolGuid,
  133. &OpenInfoBuffer,
  134. &EntryCount
  135. );
  136. if (EFI_ERROR (Status)) {
  137. continue;
  138. }
  139. for (ChildIndex = 0; ChildIndex < EntryCount; ChildIndex++) {
  140. if ((OpenInfoBuffer[ChildIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
  141. Status = gBS->HandleProtocol (OpenInfoBuffer[ChildIndex].ControllerHandle, &gEfiSerialIoProtocolGuid, (VOID **)&SerialIo);
  142. if (!EFI_ERROR (Status)) {
  143. SioSerial = &SioPtr->Serial[Acpi->UID];
  144. SioSerial->Address = Address;
  145. SioSerial->Irq = Irq;
  146. SioSerial->Mode = DEVICE_SERIAL_MODE_NORMAL | DEVICE_SERIAL_MODE_DUPLEX_HALF;
  147. break;
  148. }
  149. }
  150. }
  151. FreePool (OpenInfoBuffer);
  152. }
  153. }
  154. //
  155. // See if this is an ISA parallel port
  156. //
  157. // Ignore DMA resource since it is always returned NULL, port
  158. // only used in output mode.
  159. //
  160. if ((Acpi->HID == EISA_PNP_ID (0x400)) || (Acpi->HID == EISA_PNP_ID (0x401))) {
  161. if ((Acpi->UID < 3) && (Address != MAX_UINT16) && (Irq != MAX_UINT8) && (Dma != MAX_UINT8)) {
  162. SioParallel = &SioPtr->Parallel[Acpi->UID];
  163. SioParallel->Address = Address;
  164. SioParallel->Irq = Irq;
  165. SioParallel->Dma = Dma;
  166. SioParallel->Mode = DEVICE_PARALLEL_MODE_MODE_OUTPUT_ONLY;
  167. }
  168. }
  169. //
  170. // See if this is an ISA floppy controller
  171. //
  172. if (Acpi->HID == EISA_PNP_ID (0x604)) {
  173. if ((Address != MAX_UINT16) && (Irq != MAX_UINT8) && (Dma != MAX_UINT8)) {
  174. Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
  175. if (!EFI_ERROR (Status)) {
  176. SioFloppy = &SioPtr->Floppy;
  177. SioFloppy->Address = Address;
  178. SioFloppy->Irq = Irq;
  179. SioFloppy->Dma = Dma;
  180. SioFloppy->NumberOfFloppy++;
  181. }
  182. }
  183. }
  184. //
  185. // See if this is a mouse
  186. // Always set mouse found so USB hot plug will work
  187. //
  188. // Ignore lower byte of HID. Pnp0fxx is any type of mouse.
  189. //
  190. // Hid = ResourceList->Device.HID & 0xff00ffff;
  191. // PnpId = EISA_PNP_ID(0x0f00);
  192. // if (Hid == PnpId) {
  193. // if (ResourceList->Device.UID == 1) {
  194. // Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiSimplePointerProtocolGuid, &SimplePointer);
  195. // if (!EFI_ERROR (Status)) {
  196. //
  197. SioPtr->MousePresent = 0x01;
  198. //
  199. // }
  200. // }
  201. // }
  202. //
  203. }
  204. FreePool (HandleBuffer);
  205. return EFI_SUCCESS;
  206. }
  207. /**
  208. Collect EFI Info about legacy devices through ISA IO interface.
  209. @param SioPtr Pointer to SIO data.
  210. @retval EFI_SUCCESS When SIO data is got successfully.
  211. @retval EFI_NOT_FOUND When ISA IO interface is absent.
  212. **/
  213. EFI_STATUS
  214. LegacyBiosBuildSioDataFromIsaIo (
  215. IN DEVICE_PRODUCER_DATA_HEADER *SioPtr
  216. )
  217. {
  218. EFI_STATUS Status;
  219. DEVICE_PRODUCER_SERIAL *SioSerial;
  220. DEVICE_PRODUCER_PARALLEL *SioParallel;
  221. DEVICE_PRODUCER_FLOPPY *SioFloppy;
  222. UINTN HandleCount;
  223. EFI_HANDLE *HandleBuffer;
  224. UINTN Index;
  225. UINTN ResourceIndex;
  226. UINTN ChildIndex;
  227. EFI_ISA_IO_PROTOCOL *IsaIo;
  228. EFI_ISA_ACPI_RESOURCE_LIST *ResourceList;
  229. EFI_ISA_ACPI_RESOURCE *IoResource;
  230. EFI_ISA_ACPI_RESOURCE *DmaResource;
  231. EFI_ISA_ACPI_RESOURCE *InterruptResource;
  232. UINTN EntryCount;
  233. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
  234. EFI_BLOCK_IO_PROTOCOL *BlockIo;
  235. EFI_SERIAL_IO_PROTOCOL *SerialIo;
  236. //
  237. // Get the list of ISA controllers in the system
  238. //
  239. Status = gBS->LocateHandleBuffer (
  240. ByProtocol,
  241. &gEfiIsaIoProtocolGuid,
  242. NULL,
  243. &HandleCount,
  244. &HandleBuffer
  245. );
  246. if (EFI_ERROR (Status)) {
  247. return EFI_NOT_FOUND;
  248. }
  249. //
  250. // Collect legacy information from each of the ISA controllers in the system
  251. //
  252. for (Index = 0; Index < HandleCount; Index++) {
  253. Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiIsaIoProtocolGuid, (VOID **)&IsaIo);
  254. if (EFI_ERROR (Status)) {
  255. continue;
  256. }
  257. ResourceList = IsaIo->ResourceList;
  258. if (ResourceList == NULL) {
  259. continue;
  260. }
  261. //
  262. // Collect the resource types neededto fill in the SIO data structure
  263. //
  264. IoResource = NULL;
  265. DmaResource = NULL;
  266. InterruptResource = NULL;
  267. for (ResourceIndex = 0;
  268. ResourceList->ResourceItem[ResourceIndex].Type != EfiIsaAcpiResourceEndOfList;
  269. ResourceIndex++
  270. )
  271. {
  272. switch (ResourceList->ResourceItem[ResourceIndex].Type) {
  273. case EfiIsaAcpiResourceIo:
  274. IoResource = &ResourceList->ResourceItem[ResourceIndex];
  275. break;
  276. case EfiIsaAcpiResourceMemory:
  277. break;
  278. case EfiIsaAcpiResourceDma:
  279. DmaResource = &ResourceList->ResourceItem[ResourceIndex];
  280. break;
  281. case EfiIsaAcpiResourceInterrupt:
  282. InterruptResource = &ResourceList->ResourceItem[ResourceIndex];
  283. break;
  284. default:
  285. break;
  286. }
  287. }
  288. //
  289. // See if this is an ISA serial port
  290. //
  291. // Ignore DMA resource since it is always returned NULL
  292. //
  293. if ((ResourceList->Device.HID == EISA_PNP_ID (0x500)) || (ResourceList->Device.HID == EISA_PNP_ID (0x501))) {
  294. if ((ResourceList->Device.UID <= 3) &&
  295. (IoResource != NULL) &&
  296. (InterruptResource != NULL)
  297. )
  298. {
  299. //
  300. // Get the handle of the child device that has opened the ISA I/O Protocol
  301. //
  302. Status = gBS->OpenProtocolInformation (
  303. HandleBuffer[Index],
  304. &gEfiIsaIoProtocolGuid,
  305. &OpenInfoBuffer,
  306. &EntryCount
  307. );
  308. if (EFI_ERROR (Status)) {
  309. continue;
  310. }
  311. //
  312. // We want resource for legacy even if no 32-bit driver installed
  313. //
  314. for (ChildIndex = 0; ChildIndex < EntryCount; ChildIndex++) {
  315. if ((OpenInfoBuffer[ChildIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
  316. Status = gBS->HandleProtocol (OpenInfoBuffer[ChildIndex].ControllerHandle, &gEfiSerialIoProtocolGuid, (VOID **)&SerialIo);
  317. if (!EFI_ERROR (Status)) {
  318. SioSerial = &SioPtr->Serial[ResourceList->Device.UID];
  319. SioSerial->Address = (UINT16)IoResource->StartRange;
  320. SioSerial->Irq = (UINT8)InterruptResource->StartRange;
  321. SioSerial->Mode = DEVICE_SERIAL_MODE_NORMAL | DEVICE_SERIAL_MODE_DUPLEX_HALF;
  322. break;
  323. }
  324. }
  325. }
  326. FreePool (OpenInfoBuffer);
  327. }
  328. }
  329. //
  330. // See if this is an ISA parallel port
  331. //
  332. // Ignore DMA resource since it is always returned NULL, port
  333. // only used in output mode.
  334. //
  335. if ((ResourceList->Device.HID == EISA_PNP_ID (0x400)) || (ResourceList->Device.HID == EISA_PNP_ID (0x401))) {
  336. if ((ResourceList->Device.UID <= 2) &&
  337. (IoResource != NULL) &&
  338. (InterruptResource != NULL) &&
  339. (DmaResource != NULL)
  340. )
  341. {
  342. SioParallel = &SioPtr->Parallel[ResourceList->Device.UID];
  343. SioParallel->Address = (UINT16)IoResource->StartRange;
  344. SioParallel->Irq = (UINT8)InterruptResource->StartRange;
  345. SioParallel->Dma = (UINT8)DmaResource->StartRange;
  346. SioParallel->Mode = DEVICE_PARALLEL_MODE_MODE_OUTPUT_ONLY;
  347. }
  348. }
  349. //
  350. // See if this is an ISA floppy controller
  351. //
  352. if (ResourceList->Device.HID == EISA_PNP_ID (0x604)) {
  353. if ((IoResource != NULL) && (InterruptResource != NULL) && (DmaResource != NULL)) {
  354. Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
  355. if (!EFI_ERROR (Status)) {
  356. SioFloppy = &SioPtr->Floppy;
  357. SioFloppy->Address = (UINT16)IoResource->StartRange;
  358. SioFloppy->Irq = (UINT8)InterruptResource->StartRange;
  359. SioFloppy->Dma = (UINT8)DmaResource->StartRange;
  360. SioFloppy->NumberOfFloppy++;
  361. }
  362. }
  363. }
  364. //
  365. // See if this is a mouse
  366. // Always set mouse found so USB hot plug will work
  367. //
  368. // Ignore lower byte of HID. Pnp0fxx is any type of mouse.
  369. //
  370. // Hid = ResourceList->Device.HID & 0xff00ffff;
  371. // PnpId = EISA_PNP_ID(0x0f00);
  372. // if (Hid == PnpId) {
  373. // if (ResourceList->Device.UID == 1) {
  374. // Status = gBS->HandleProtocol (HandleBuffer[Index], &gEfiSimplePointerProtocolGuid, &SimplePointer);
  375. // if (!EFI_ERROR (Status)) {
  376. //
  377. SioPtr->MousePresent = 0x01;
  378. //
  379. // }
  380. // }
  381. // }
  382. //
  383. }
  384. FreePool (HandleBuffer);
  385. return EFI_SUCCESS;
  386. }
  387. /**
  388. Collect EFI Info about legacy devices.
  389. @param Private Legacy BIOS Instance data
  390. @retval EFI_SUCCESS It should always work.
  391. **/
  392. EFI_STATUS
  393. LegacyBiosBuildSioData (
  394. IN LEGACY_BIOS_INSTANCE *Private
  395. )
  396. {
  397. EFI_STATUS Status;
  398. DEVICE_PRODUCER_DATA_HEADER *SioPtr;
  399. EFI_HANDLE IsaBusController;
  400. UINTN HandleCount;
  401. EFI_HANDLE *HandleBuffer;
  402. //
  403. // Get the pointer to the SIO data structure
  404. //
  405. SioPtr = &Private->IntThunk->EfiToLegacy16BootTable.SioData;
  406. //
  407. // Zero the data in the SIO data structure
  408. //
  409. gBS->SetMem (SioPtr, sizeof (DEVICE_PRODUCER_DATA_HEADER), 0);
  410. //
  411. // Find the ISA Bus Controller used for legacy
  412. //
  413. Status = Private->LegacyBiosPlatform->GetPlatformHandle (
  414. Private->LegacyBiosPlatform,
  415. EfiGetPlatformIsaBusHandle,
  416. 0,
  417. &HandleBuffer,
  418. &HandleCount,
  419. NULL
  420. );
  421. IsaBusController = HandleBuffer[0];
  422. if (!EFI_ERROR (Status)) {
  423. //
  424. // Force ISA Bus Controller to produce all ISA devices
  425. //
  426. gBS->ConnectController (IsaBusController, NULL, NULL, TRUE);
  427. }
  428. Status = LegacyBiosBuildSioDataFromIsaIo (SioPtr);
  429. if (EFI_ERROR (Status)) {
  430. LegacyBiosBuildSioDataFromSio (SioPtr);
  431. }
  432. return EFI_SUCCESS;
  433. }