EmuSnpDxe.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /** @file
  2. Copyright (c) 2010, Apple, Inc. All rights reserved.<BR>
  3. Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. Module Name:
  6. EmuSnp.c
  7. Abstract:
  8. -**/
  9. #include "EmuSnpDxe.h"
  10. EFI_SIMPLE_NETWORK_PROTOCOL gEmuSnpTemplate = {
  11. EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
  12. EmuSnpStart,
  13. EmuSnpStop,
  14. EmuSnpInitialize,
  15. EmuSnpReset,
  16. EmuSnpShutdown,
  17. EmuSnpReceiveFilters,
  18. EmuSnpStationAddress,
  19. EmuSnpStatistics,
  20. EmuSnpMcastIptoMac,
  21. EmuSnpNvdata,
  22. EmuSnpGetStatus,
  23. EmuSnpTransmit,
  24. EmuSnpReceive,
  25. NULL, // WaitForPacket
  26. NULL // Mode
  27. };
  28. EFI_SIMPLE_NETWORK_MODE gEmuSnpModeTemplate = {
  29. EfiSimpleNetworkStopped, // State
  30. NET_ETHER_ADDR_LEN, // HwAddressSize
  31. NET_ETHER_HEADER_SIZE, // MediaHeaderSize
  32. 1500, // MaxPacketSize
  33. 0, // NvRamSize
  34. 0, // NvRamAccessSize
  35. 0, // ReceiveFilterMask
  36. 0, // ReceiveFilterSetting
  37. MAX_MCAST_FILTER_CNT, // MaxMCastFilterCount
  38. 0, // MCastFilterCount
  39. {
  40. {
  41. { 0 }
  42. }
  43. }, // MCastFilter
  44. {
  45. { 0 }
  46. }, // CurrentAddress
  47. {
  48. { 0 }
  49. }, // BroadcastAddress
  50. {
  51. { 0 }
  52. }, // PermanentAddress
  53. NET_IFTYPE_ETHERNET, // IfType
  54. FALSE, // MacAddressChangeable
  55. FALSE, // MultipleTxSupported
  56. FALSE, // MediaPresentSupported
  57. TRUE // MediaPresent
  58. };
  59. /**
  60. Changes the state of a network interface from "stopped" to "started".
  61. @param This Protocol instance pointer.
  62. @retval EFI_SUCCESS Always succeeds.
  63. **/
  64. EFI_STATUS
  65. EFIAPI
  66. EmuSnpStart (
  67. IN EFI_SIMPLE_NETWORK_PROTOCOL *This
  68. )
  69. {
  70. EFI_STATUS Status;
  71. EMU_SNP_PRIVATE_DATA *Private;
  72. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  73. Status = Private->Io->Start (Private->Io);
  74. return Status;
  75. }
  76. /**
  77. Changes the state of a network interface from "started" to "stopped".
  78. @param This Protocol instance pointer.
  79. @retval EFI_SUCCESS Always succeeds.
  80. **/
  81. EFI_STATUS
  82. EFIAPI
  83. EmuSnpStop (
  84. IN EFI_SIMPLE_NETWORK_PROTOCOL *This
  85. )
  86. {
  87. EFI_STATUS Status;
  88. EMU_SNP_PRIVATE_DATA *Private;
  89. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  90. Status = Private->Io->Stop (Private->Io);
  91. return Status;
  92. }
  93. /**
  94. Resets a network adapter and allocates the transmit and receive buffers
  95. required by the network interface; optionally, also requests allocation
  96. of additional transmit and receive buffers.
  97. @param This Protocol instance pointer.
  98. @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
  99. that the driver should allocate for the network interface.
  100. Some network interfaces will not be able to use the extra
  101. buffer, and the caller will not know if it is actually
  102. being used.
  103. @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
  104. that the driver should allocate for the network interface.
  105. Some network interfaces will not be able to use the extra
  106. buffer, and the caller will not know if it is actually
  107. being used.
  108. @retval EFI_SUCCESS Always succeeds.
  109. **/
  110. EFI_STATUS
  111. EFIAPI
  112. EmuSnpInitialize (
  113. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  114. IN UINTN ExtraRxBufferSize OPTIONAL,
  115. IN UINTN ExtraTxBufferSize OPTIONAL
  116. )
  117. {
  118. EFI_STATUS Status;
  119. EMU_SNP_PRIVATE_DATA *Private;
  120. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  121. Status = Private->Io->Initialize (Private->Io, ExtraRxBufferSize, ExtraTxBufferSize);
  122. return Status;
  123. }
  124. /**
  125. Resets a network adapter and re-initializes it with the parameters that were
  126. provided in the previous call to Initialize().
  127. @param This Protocol instance pointer.
  128. @param ExtendedVerification Indicates that the driver may perform a more
  129. exhaustive verification operation of the device
  130. during reset.
  131. @retval EFI_SUCCESS Always succeeds.
  132. **/
  133. EFI_STATUS
  134. EFIAPI
  135. EmuSnpReset (
  136. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  137. IN BOOLEAN ExtendedVerification
  138. )
  139. {
  140. EFI_STATUS Status;
  141. EMU_SNP_PRIVATE_DATA *Private;
  142. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  143. Status = Private->Io->Reset (Private->Io, ExtendedVerification);
  144. return Status;
  145. }
  146. /**
  147. Resets a network adapter and leaves it in a state that is safe for
  148. another driver to initialize.
  149. @param This Protocol instance pointer.
  150. @retval EFI_SUCCESS Always succeeds.
  151. **/
  152. EFI_STATUS
  153. EFIAPI
  154. EmuSnpShutdown (
  155. IN EFI_SIMPLE_NETWORK_PROTOCOL *This
  156. )
  157. {
  158. EFI_STATUS Status;
  159. EMU_SNP_PRIVATE_DATA *Private;
  160. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  161. Status = Private->Io->Shutdown (Private->Io);
  162. return Status;
  163. }
  164. /**
  165. Manages the multicast receive filters of a network interface.
  166. @param This Protocol instance pointer.
  167. @param EnableBits A bit mask of receive filters to enable on the network interface.
  168. @param DisableBits A bit mask of receive filters to disable on the network interface.
  169. @param ResetMcastFilter Set to TRUE to reset the contents of the multicast receive
  170. filters on the network interface to their default values.
  171. @param McastFilterCount Number of multicast HW MAC addresses in the new
  172. MCastFilter list. This value must be less than or equal to
  173. the MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE. This
  174. field is optional if ResetMCastFilter is TRUE.
  175. @param McastFilter A pointer to a list of new multicast receive filter HW MAC
  176. addresses. This list will replace any existing multicast
  177. HW MAC address list. This field is optional if
  178. ResetMCastFilter is TRUE.
  179. @retval EFI_SUCCESS The multicast receive filter list was updated.
  180. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  181. **/
  182. EFI_STATUS
  183. EFIAPI
  184. EmuSnpReceiveFilters (
  185. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  186. IN UINT32 EnableBits,
  187. IN UINT32 DisableBits,
  188. IN BOOLEAN ResetMcastFilter,
  189. IN UINTN McastFilterCount OPTIONAL,
  190. IN EFI_MAC_ADDRESS *McastFilter OPTIONAL
  191. )
  192. {
  193. EFI_STATUS Status;
  194. EMU_SNP_PRIVATE_DATA *Private;
  195. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  196. Status = Private->Io->ReceiveFilters (
  197. Private->Io,
  198. EnableBits,
  199. DisableBits,
  200. ResetMcastFilter,
  201. McastFilterCount,
  202. McastFilter
  203. );
  204. return Status;
  205. }
  206. /**
  207. Modifies or resets the current station address, if supported.
  208. @param This Protocol instance pointer.
  209. @param Reset Flag used to reset the station address to the network interfaces
  210. permanent address.
  211. @param NewMacAddr New station address to be used for the network interface.
  212. @retval EFI_UNSUPPORTED Not supported yet.
  213. **/
  214. EFI_STATUS
  215. EFIAPI
  216. EmuSnpStationAddress (
  217. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  218. IN BOOLEAN Reset,
  219. IN EFI_MAC_ADDRESS *NewMacAddr OPTIONAL
  220. )
  221. {
  222. EFI_STATUS Status;
  223. EMU_SNP_PRIVATE_DATA *Private;
  224. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  225. Status = Private->Io->StationAddress (Private->Io, Reset, NewMacAddr);
  226. return Status;
  227. }
  228. /**
  229. Resets or collects the statistics on a network interface.
  230. @param This Protocol instance pointer.
  231. @param Reset Set to TRUE to reset the statistics for the network interface.
  232. @param StatisticsSize On input the size, in bytes, of StatisticsTable. On
  233. output the size, in bytes, of the resulting table of
  234. statistics.
  235. @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
  236. contains the statistics.
  237. @retval EFI_SUCCESS The statistics were collected from the network interface.
  238. @retval EFI_NOT_STARTED The network interface has not been started.
  239. @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
  240. size needed to hold the statistics is returned in
  241. StatisticsSize.
  242. @retval EFI_UNSUPPORTED Not supported yet.
  243. **/
  244. EFI_STATUS
  245. EFIAPI
  246. EmuSnpStatistics (
  247. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  248. IN BOOLEAN Reset,
  249. IN OUT UINTN *StatisticsSize OPTIONAL,
  250. OUT EFI_NETWORK_STATISTICS *StatisticsTable OPTIONAL
  251. )
  252. {
  253. EFI_STATUS Status;
  254. EMU_SNP_PRIVATE_DATA *Private;
  255. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  256. Status = Private->Io->Statistics (Private->Io, Reset, StatisticsSize, StatisticsTable);
  257. return Status;
  258. }
  259. /**
  260. Converts a multicast IP address to a multicast HW MAC address.
  261. @param This Protocol instance pointer.
  262. @param Ipv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
  263. to FALSE if the multicast IP address is IPv4 [RFC 791].
  264. @param Ip The multicast IP address that is to be converted to a multicast
  265. HW MAC address.
  266. @param Mac The multicast HW MAC address that is to be generated from IP.
  267. @retval EFI_SUCCESS The multicast IP address was mapped to the multicast
  268. HW MAC address.
  269. @retval EFI_NOT_STARTED The network interface has not been started.
  270. @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
  271. size needed to hold the statistics is returned in
  272. StatisticsSize.
  273. @retval EFI_UNSUPPORTED Not supported yet.
  274. **/
  275. EFI_STATUS
  276. EFIAPI
  277. EmuSnpMcastIptoMac (
  278. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  279. IN BOOLEAN Ipv6,
  280. IN EFI_IP_ADDRESS *Ip,
  281. OUT EFI_MAC_ADDRESS *Mac
  282. )
  283. {
  284. EFI_STATUS Status;
  285. EMU_SNP_PRIVATE_DATA *Private;
  286. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  287. Status = Private->Io->MCastIpToMac (Private->Io, Ipv6, Ip, Mac);
  288. return Status;
  289. }
  290. /**
  291. Performs read and write operations on the NVRAM device attached to a
  292. network interface.
  293. @param This Protocol instance pointer.
  294. @param ReadOrWrite TRUE for read operations, FALSE for write operations.
  295. @param Offset Byte offset in the NVRAM device at which to start the read or
  296. write operation. This must be a multiple of NvRamAccessSize and
  297. less than NvRamSize.
  298. @param BufferSize The number of bytes to read or write from the NVRAM device.
  299. This must also be a multiple of NvramAccessSize.
  300. @param Buffer A pointer to the data buffer.
  301. @retval EFI_UNSUPPORTED Not supported yet.
  302. **/
  303. EFI_STATUS
  304. EFIAPI
  305. EmuSnpNvdata (
  306. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  307. IN BOOLEAN ReadOrWrite,
  308. IN UINTN Offset,
  309. IN UINTN BufferSize,
  310. IN OUT VOID *Buffer
  311. )
  312. {
  313. EFI_STATUS Status;
  314. EMU_SNP_PRIVATE_DATA *Private;
  315. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  316. Status = Private->Io->NvData (Private->Io, ReadOrWrite, Offset, BufferSize, Buffer);
  317. return Status;
  318. }
  319. /**
  320. Reads the current interrupt status and recycled transmit buffer status from
  321. a network interface.
  322. @param This Protocol instance pointer.
  323. @param InterruptStatus A pointer to the bit mask of the currently active interrupts
  324. If this is NULL, the interrupt status will not be read from
  325. the device. If this is not NULL, the interrupt status will
  326. be read from the device. When the interrupt status is read,
  327. it will also be cleared. Clearing the transmit interrupt
  328. does not empty the recycled transmit buffer array.
  329. @param TxBuffer Recycled transmit buffer address. The network interface will
  330. not transmit if its internal recycled transmit buffer array
  331. is full. Reading the transmit buffer does not clear the
  332. transmit interrupt. If this is NULL, then the transmit buffer
  333. status will not be read. If there are no transmit buffers to
  334. recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
  335. @retval EFI_SUCCESS Always succeeds.
  336. **/
  337. EFI_STATUS
  338. EFIAPI
  339. EmuSnpGetStatus (
  340. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  341. OUT UINT32 *InterruptStatus,
  342. OUT VOID **TxBuffer
  343. )
  344. {
  345. EFI_STATUS Status;
  346. EMU_SNP_PRIVATE_DATA *Private;
  347. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  348. Status = Private->Io->GetStatus (Private->Io, InterruptStatus, TxBuffer);
  349. return Status;
  350. }
  351. /**
  352. Places a packet in the transmit queue of a network interface.
  353. @param This Protocol instance pointer.
  354. @param HeaderSize The size, in bytes, of the media header to be filled in by
  355. the Transmit() function. If HeaderSize is non-zero, then it
  356. must be equal to This->Mode->MediaHeaderSize and the DestAddr
  357. and Protocol parameters must not be NULL.
  358. @param BufferSize The size, in bytes, of the entire packet (media header and
  359. data) to be transmitted through the network interface.
  360. @param Buffer A pointer to the packet (media header followed by data) to be
  361. transmitted. This parameter cannot be NULL. If HeaderSize is zero,
  362. then the media header in Buffer must already be filled in by the
  363. caller. If HeaderSize is non-zero, then the media header will be
  364. filled in by the Transmit() function.
  365. @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this parameter
  366. is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
  367. This->Mode->CurrentAddress is used for the source HW MAC address.
  368. @param DestAddr The destination HW MAC address. If HeaderSize is zero, then this
  369. parameter is ignored.
  370. @param Protocol The type of header to build. If HeaderSize is zero, then this
  371. parameter is ignored. See RFC 1700, section "Ether Types", for
  372. examples.
  373. @retval EFI_SUCCESS The packet was placed on the transmit queue.
  374. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  375. @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
  376. @retval EFI_NOT_STARTED The network interface has not been started.
  377. **/
  378. EFI_STATUS
  379. EFIAPI
  380. EmuSnpTransmit (
  381. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  382. IN UINTN HeaderSize,
  383. IN UINTN BufferSize,
  384. IN VOID *Buffer,
  385. IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
  386. IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
  387. IN UINT16 *Protocol OPTIONAL
  388. )
  389. {
  390. EFI_STATUS Status;
  391. EMU_SNP_PRIVATE_DATA *Private;
  392. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  393. Status = Private->Io->Transmit (
  394. Private->Io,
  395. HeaderSize,
  396. BufferSize,
  397. Buffer,
  398. SrcAddr,
  399. DestAddr,
  400. Protocol
  401. );
  402. return Status;
  403. }
  404. /**
  405. Receives a packet from a network interface.
  406. @param This Protocol instance pointer.
  407. @param HeaderSize The size, in bytes, of the media header received on the network
  408. interface. If this parameter is NULL, then the media header size
  409. will not be returned.
  410. @param BuffSize On entry, the size, in bytes, of Buffer. On exit, the size, in
  411. bytes, of the packet that was received on the network interface.
  412. @param Buffer A pointer to the data buffer to receive both the media header and
  413. the data.
  414. @param SourceAddr The source HW MAC address. If this parameter is NULL, the
  415. HW MAC source address will not be extracted from the media
  416. header.
  417. @param DestinationAddr The destination HW MAC address. If this parameter is NULL,
  418. the HW MAC destination address will not be extracted from the
  419. media header.
  420. @param Protocol The media header type. If this parameter is NULL, then the
  421. protocol will not be extracted from the media header. See
  422. RFC 1700 section "Ether Types" for examples.
  423. @retval EFI_SUCCESS The received data was stored in Buffer, and BufferSize has
  424. been updated to the number of bytes received.
  425. @retval EFI_NOT_READY The network interface is too busy to accept this transmit
  426. request.
  427. @retval EFI_NOT_STARTED The network interface has not been started.
  428. @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
  429. @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
  430. **/
  431. EFI_STATUS
  432. EFIAPI
  433. EmuSnpReceive (
  434. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  435. OUT UINTN *HeaderSize OPTIONAL,
  436. IN OUT UINTN *BuffSize,
  437. OUT VOID *Buffer,
  438. OUT EFI_MAC_ADDRESS *SourceAddr OPTIONAL,
  439. OUT EFI_MAC_ADDRESS *DestinationAddr OPTIONAL,
  440. OUT UINT16 *Protocol OPTIONAL
  441. )
  442. {
  443. EFI_STATUS Status;
  444. EMU_SNP_PRIVATE_DATA *Private;
  445. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
  446. Status = Private->Io->Receive (
  447. Private->Io,
  448. HeaderSize,
  449. BuffSize,
  450. Buffer,
  451. SourceAddr,
  452. DestinationAddr,
  453. Protocol
  454. );
  455. return Status;
  456. }
  457. /**
  458. Test to see if this driver supports ControllerHandle. This service
  459. is called by the EFI boot service ConnectController(). In
  460. order to make drivers as small as possible, there are a few calling
  461. restrictions for this service. ConnectController() must
  462. follow these calling restrictions. If any other agent wishes to call
  463. Supported() it must also follow these calling restrictions.
  464. @param This Protocol instance pointer.
  465. @param ControllerHandle Handle of device to test
  466. @param RemainingDevicePath Optional parameter use to pick a specific child
  467. device to start.
  468. @retval EFI_SUCCESS This driver supports this device
  469. @retval EFI_UNSUPPORTED This driver does not support this device
  470. **/
  471. EFI_STATUS
  472. EFIAPI
  473. EmuSnpDriverBindingSupported (
  474. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  475. IN EFI_HANDLE ControllerHandle,
  476. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  477. )
  478. {
  479. EFI_STATUS Status;
  480. EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
  481. MAC_ADDR_DEVICE_PATH *Node;
  482. EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
  483. if (RemainingDevicePath != NULL) {
  484. if (!IsDevicePathEnd (RemainingDevicePath)) {
  485. Node = (MAC_ADDR_DEVICE_PATH *)RemainingDevicePath;
  486. if ((Node->Header.Type != MESSAGING_DEVICE_PATH) ||
  487. (Node->Header.SubType != MSG_MAC_ADDR_DP))
  488. {
  489. // If the remaining device path does not match we don't support the request
  490. return EFI_UNSUPPORTED;
  491. }
  492. }
  493. }
  494. //
  495. // Open the IO Abstraction(s) needed to perform the supported test
  496. //
  497. Status = gBS->OpenProtocol (
  498. ControllerHandle,
  499. &gEmuIoThunkProtocolGuid,
  500. (VOID **)&EmuIoThunk,
  501. This->DriverBindingHandle,
  502. ControllerHandle,
  503. EFI_OPEN_PROTOCOL_BY_DRIVER
  504. );
  505. if (EFI_ERROR (Status)) {
  506. return Status;
  507. }
  508. //
  509. // Close the I/O Abstraction(s) used to perform the supported test
  510. //
  511. gBS->CloseProtocol (
  512. ControllerHandle,
  513. &gEmuIoThunkProtocolGuid,
  514. This->DriverBindingHandle,
  515. ControllerHandle
  516. );
  517. //
  518. // Open the EFI Device Path protocol needed to perform the supported test
  519. //
  520. Status = gBS->OpenProtocol (
  521. ControllerHandle,
  522. &gEfiDevicePathProtocolGuid,
  523. (VOID **)&ParentDevicePath,
  524. This->DriverBindingHandle,
  525. ControllerHandle,
  526. EFI_OPEN_PROTOCOL_BY_DRIVER
  527. );
  528. if (Status == EFI_ALREADY_STARTED) {
  529. return EFI_SUCCESS;
  530. }
  531. if (EFI_ERROR (Status)) {
  532. return Status;
  533. }
  534. //
  535. // Make sure GUID is for a SNP handle.
  536. //
  537. Status = EFI_UNSUPPORTED;
  538. if (CompareGuid (EmuIoThunk->Protocol, &gEmuSnpProtocolGuid)) {
  539. Status = EFI_SUCCESS;
  540. }
  541. //
  542. // Close protocol, don't use device path protocol in the Support() function
  543. //
  544. gBS->CloseProtocol (
  545. ControllerHandle,
  546. &gEfiDevicePathProtocolGuid,
  547. This->DriverBindingHandle,
  548. ControllerHandle
  549. );
  550. return Status;
  551. }
  552. /**
  553. Start this driver on ControllerHandle. This service is called by the
  554. EFI boot service ConnectController(). In order to make
  555. drivers as small as possible, there are a few calling restrictions for
  556. this service. ConnectController() must follow these
  557. calling restrictions. If any other agent wishes to call Start() it
  558. must also follow these calling restrictions.
  559. @param This Protocol instance pointer.
  560. @param ControllerHandle Handle of device to bind driver to
  561. @param RemainingDevicePath Optional parameter use to pick a specific child
  562. device to start.
  563. @retval EFI_SUCCESS Always succeeds.
  564. **/
  565. EFI_STATUS
  566. EFIAPI
  567. EmuSnpDriverBindingStart (
  568. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  569. IN EFI_HANDLE ControllerHandle,
  570. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  571. )
  572. {
  573. EFI_STATUS Status;
  574. EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
  575. EMU_SNP_PRIVATE_DATA *Private;
  576. MAC_ADDR_DEVICE_PATH Node;
  577. EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
  578. Private = NULL;
  579. //
  580. // Grab the protocols we need
  581. //
  582. Status = gBS->OpenProtocol (
  583. ControllerHandle,
  584. &gEfiDevicePathProtocolGuid,
  585. (VOID **)&ParentDevicePath,
  586. This->DriverBindingHandle,
  587. ControllerHandle,
  588. EFI_OPEN_PROTOCOL_BY_DRIVER
  589. );
  590. if (EFI_ERROR (Status) && Status) {
  591. return Status;
  592. }
  593. Status = gBS->OpenProtocol (
  594. ControllerHandle,
  595. &gEmuIoThunkProtocolGuid,
  596. (VOID **)&EmuIoThunk,
  597. This->DriverBindingHandle,
  598. ControllerHandle,
  599. EFI_OPEN_PROTOCOL_BY_DRIVER
  600. );
  601. if (EFI_ERROR (Status)) {
  602. return Status;
  603. }
  604. if (!CompareGuid (EmuIoThunk->Protocol, &gEmuSnpProtocolGuid)) {
  605. return EFI_UNSUPPORTED;
  606. }
  607. Status = EmuIoThunk->Open (EmuIoThunk);
  608. if (EFI_ERROR (Status)) {
  609. goto Done;
  610. }
  611. //
  612. // Allocate the private data.
  613. //
  614. Private = AllocateZeroPool (sizeof (EMU_SNP_PRIVATE_DATA));
  615. if (Private == NULL) {
  616. Status = EFI_OUT_OF_RESOURCES;
  617. goto Done;
  618. }
  619. CopyMem (&Private->Snp, &gEmuSnpTemplate, sizeof (EFI_SIMPLE_NETWORK_PROTOCOL));
  620. CopyMem (&Private->Mode, &gEmuSnpModeTemplate, sizeof (EFI_SIMPLE_NETWORK_MODE));
  621. Private->Signature = EMU_SNP_PRIVATE_DATA_SIGNATURE;
  622. Private->IoThunk = EmuIoThunk;
  623. Private->Io = EmuIoThunk->Interface;
  624. Private->EfiHandle = ControllerHandle;
  625. Private->DeviceHandle = NULL;
  626. Private->Snp.Mode = &Private->Mode;
  627. Private->ControllerNameTable = NULL;
  628. Status = Private->Io->CreateMapping (Private->Io, &Private->Mode);
  629. if (EFI_ERROR (Status)) {
  630. goto Done;
  631. }
  632. //
  633. // Build the device path by appending the MAC node to the ParentDevicePath
  634. // from the EmuIo handle.
  635. //
  636. ZeroMem (&Node, sizeof (MAC_ADDR_DEVICE_PATH));
  637. Node.Header.Type = MESSAGING_DEVICE_PATH;
  638. Node.Header.SubType = MSG_MAC_ADDR_DP;
  639. Node.IfType = Private->Mode.IfType;
  640. SetDevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL *)&Node, sizeof (MAC_ADDR_DEVICE_PATH));
  641. CopyMem (&Node.MacAddress, &Private->Mode.CurrentAddress, sizeof (EFI_MAC_ADDRESS));
  642. //
  643. // Build the device path by appending the MAC node to the ParentDevicePath from the EmuIo handle.
  644. //
  645. Private->DevicePath = AppendDevicePathNode (ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&Node);
  646. if ( Private->DevicePath == NULL ) {
  647. Status = EFI_OUT_OF_RESOURCES;
  648. goto Done;
  649. }
  650. AddUnicodeString2 (
  651. "eng",
  652. gEmuSnpDriverComponentName.SupportedLanguages,
  653. &Private->ControllerNameTable,
  654. EmuIoThunk->ConfigString,
  655. TRUE
  656. );
  657. AddUnicodeString2 (
  658. "en",
  659. gEmuSnpDriverComponentName2.SupportedLanguages,
  660. &Private->ControllerNameTable,
  661. EmuIoThunk->ConfigString,
  662. FALSE
  663. );
  664. //
  665. // Create Child Handle
  666. //
  667. Status = gBS->InstallMultipleProtocolInterfaces (
  668. &Private->DeviceHandle,
  669. &gEfiSimpleNetworkProtocolGuid,
  670. &Private->Snp,
  671. &gEfiDevicePathProtocolGuid,
  672. Private->DevicePath,
  673. NULL
  674. );
  675. if (EFI_ERROR (Status)) {
  676. goto Done;
  677. }
  678. //
  679. // Open For Child Device
  680. //
  681. Status = gBS->OpenProtocol (
  682. ControllerHandle,
  683. &gEmuIoThunkProtocolGuid,
  684. (VOID **)&EmuIoThunk,
  685. This->DriverBindingHandle,
  686. Private->DeviceHandle,
  687. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  688. );
  689. Done:
  690. if (EFI_ERROR (Status)) {
  691. if (Private != NULL) {
  692. FreePool (Private);
  693. }
  694. if (ParentDevicePath != NULL) {
  695. gBS->CloseProtocol (
  696. ControllerHandle,
  697. &gEfiDevicePathProtocolGuid,
  698. This->DriverBindingHandle,
  699. ControllerHandle
  700. );
  701. }
  702. }
  703. return Status;
  704. }
  705. /**
  706. Stop this driver on ControllerHandle. This service is called by the
  707. EFI boot service DisconnectController(). In order to
  708. make drivers as small as possible, there are a few calling
  709. restrictions for this service. DisconnectController()
  710. must follow these calling restrictions. If any other agent wishes
  711. to call Stop() it must also follow these calling restrictions.
  712. @param This Protocol instance pointer.
  713. @param ControllerHandle Handle of device to stop driver on
  714. @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
  715. children is zero stop the entire bus driver.
  716. @param ChildHandleBuffer List of Child Handles to Stop.
  717. @retval EFI_SUCCESS Always succeeds.
  718. **/
  719. EFI_STATUS
  720. EFIAPI
  721. EmuSnpDriverBindingStop (
  722. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  723. IN EFI_HANDLE ControllerHandle,
  724. IN UINTN NumberOfChildren,
  725. IN EFI_HANDLE *ChildHandleBuffer
  726. )
  727. {
  728. EFI_STATUS Status;
  729. EMU_SNP_PRIVATE_DATA *Private = NULL;
  730. EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
  731. VOID *EmuIoThunk;
  732. //
  733. // Complete all outstanding transactions to Controller.
  734. // Don't allow any new transaction to Controller to be started.
  735. //
  736. if (NumberOfChildren == 0) {
  737. //
  738. // Close the bus driver
  739. //
  740. Status = gBS->CloseProtocol (
  741. ControllerHandle,
  742. &gEmuIoThunkProtocolGuid,
  743. This->DriverBindingHandle,
  744. ControllerHandle
  745. );
  746. Status = gBS->CloseProtocol (
  747. ControllerHandle,
  748. &gEfiDevicePathProtocolGuid,
  749. This->DriverBindingHandle,
  750. ControllerHandle
  751. );
  752. return Status;
  753. }
  754. ASSERT (NumberOfChildren == 1);
  755. //
  756. // Get our context back.
  757. //
  758. Status = gBS->OpenProtocol (
  759. ChildHandleBuffer[0],
  760. &gEfiSimpleNetworkProtocolGuid,
  761. (VOID **)&Snp,
  762. This->DriverBindingHandle,
  763. ControllerHandle,
  764. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  765. );
  766. if (EFI_ERROR (Status)) {
  767. return EFI_DEVICE_ERROR;
  768. }
  769. Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (Snp);
  770. ASSERT (Private->DeviceHandle == ChildHandleBuffer[0]);
  771. ASSERT (Private->EfiHandle == ControllerHandle);
  772. Status = gBS->CloseProtocol (
  773. ControllerHandle,
  774. &gEmuIoThunkProtocolGuid,
  775. This->DriverBindingHandle,
  776. Private->DeviceHandle
  777. );
  778. ASSERT_EFI_ERROR (Status);
  779. Status = gBS->UninstallMultipleProtocolInterfaces (
  780. Private->DeviceHandle,
  781. &gEfiSimpleNetworkProtocolGuid,
  782. &Private->Snp,
  783. &gEfiDevicePathProtocolGuid,
  784. Private->DevicePath,
  785. NULL
  786. );
  787. if (EFI_ERROR (Status)) {
  788. gBS->OpenProtocol (
  789. ControllerHandle,
  790. &gEmuIoThunkProtocolGuid,
  791. &EmuIoThunk,
  792. This->DriverBindingHandle,
  793. Private->DeviceHandle,
  794. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  795. );
  796. } else {
  797. Status = Private->IoThunk->Close (Private->IoThunk);
  798. ASSERT_EFI_ERROR (Status);
  799. FreePool (Private->DevicePath);
  800. FreeUnicodeStringTable (Private->ControllerNameTable);
  801. FreePool (Private);
  802. }
  803. return Status;
  804. }
  805. EFI_DRIVER_BINDING_PROTOCOL gEmuSnpDriverBinding = {
  806. EmuSnpDriverBindingSupported,
  807. EmuSnpDriverBindingStart,
  808. EmuSnpDriverBindingStop,
  809. 0xA,
  810. NULL,
  811. NULL
  812. };
  813. /**
  814. This is the declaration of an EFI image entry point. This entry point is
  815. the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
  816. both device drivers and bus drivers.
  817. @param ImageHandle The firmware allocated handle for the UEFI image.
  818. @param SystemTable A pointer to the EFI System Table.
  819. @retval EFI_SUCCESS The operation completed successfully.
  820. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  821. **/
  822. EFI_STATUS
  823. EFIAPI
  824. InitializeEmuSnpDriver (
  825. IN EFI_HANDLE ImageHandle,
  826. IN EFI_SYSTEM_TABLE *SystemTable
  827. )
  828. {
  829. EFI_STATUS Status;
  830. //
  831. // Install the Driver Protocols
  832. //
  833. Status = EfiLibInstallDriverBindingComponentName2 (
  834. ImageHandle,
  835. SystemTable,
  836. &gEmuSnpDriverBinding,
  837. ImageHandle,
  838. &gEmuSnpDriverComponentName,
  839. &gEmuSnpDriverComponentName2
  840. );
  841. return Status;
  842. }