efi_net.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Simple network protocol
  4. * PXE base code protocol
  5. *
  6. * Copyright (c) 2016 Alexander Graf
  7. *
  8. * The simple network protocol has the following statuses and services
  9. * to move between them:
  10. *
  11. * Start(): EfiSimpleNetworkStopped -> EfiSimpleNetworkStarted
  12. * Initialize(): EfiSimpleNetworkStarted -> EfiSimpleNetworkInitialized
  13. * Shutdown(): EfiSimpleNetworkInitialized -> EfiSimpleNetworkStarted
  14. * Stop(): EfiSimpleNetworkStarted -> EfiSimpleNetworkStopped
  15. * Reset(): EfiSimpleNetworkInitialized -> EfiSimpleNetworkInitialized
  16. */
  17. #include <common.h>
  18. #include <efi_loader.h>
  19. #include <malloc.h>
  20. #include <net.h>
  21. static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
  22. static const efi_guid_t efi_pxe_base_code_protocol_guid =
  23. EFI_PXE_BASE_CODE_PROTOCOL_GUID;
  24. static struct efi_pxe_packet *dhcp_ack;
  25. static void *new_tx_packet;
  26. static void *transmit_buffer;
  27. static uchar **receive_buffer;
  28. static size_t *receive_lengths;
  29. static int rx_packet_idx;
  30. static int rx_packet_num;
  31. static struct efi_net_obj *netobj;
  32. /*
  33. * The notification function of this event is called in every timer cycle
  34. * to check if a new network packet has been received.
  35. */
  36. static struct efi_event *network_timer_event;
  37. /*
  38. * This event is signaled when a packet has been received.
  39. */
  40. static struct efi_event *wait_for_packet;
  41. /**
  42. * struct efi_net_obj - EFI object representing a network interface
  43. *
  44. * @header: EFI object header
  45. * @net: simple network protocol interface
  46. * @net_mode: status of the network interface
  47. * @pxe: PXE base code protocol interface
  48. * @pxe_mode: status of the PXE base code protocol
  49. */
  50. struct efi_net_obj {
  51. struct efi_object header;
  52. struct efi_simple_network net;
  53. struct efi_simple_network_mode net_mode;
  54. struct efi_pxe_base_code_protocol pxe;
  55. struct efi_pxe_mode pxe_mode;
  56. };
  57. /*
  58. * efi_net_start() - start the network interface
  59. *
  60. * This function implements the Start service of the
  61. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  62. * (UEFI) specification for details.
  63. *
  64. * @this: pointer to the protocol instance
  65. * Return: status code
  66. */
  67. static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
  68. {
  69. efi_status_t ret = EFI_SUCCESS;
  70. EFI_ENTRY("%p", this);
  71. /* Check parameters */
  72. if (!this) {
  73. ret = EFI_INVALID_PARAMETER;
  74. goto out;
  75. }
  76. if (this->mode->state != EFI_NETWORK_STOPPED) {
  77. ret = EFI_ALREADY_STARTED;
  78. } else {
  79. this->int_status = 0;
  80. wait_for_packet->is_signaled = false;
  81. this->mode->state = EFI_NETWORK_STARTED;
  82. }
  83. out:
  84. return EFI_EXIT(ret);
  85. }
  86. /*
  87. * efi_net_stop() - stop the network interface
  88. *
  89. * This function implements the Stop service of the
  90. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  91. * (UEFI) specification for details.
  92. *
  93. * @this: pointer to the protocol instance
  94. * Return: status code
  95. */
  96. static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
  97. {
  98. efi_status_t ret = EFI_SUCCESS;
  99. EFI_ENTRY("%p", this);
  100. /* Check parameters */
  101. if (!this) {
  102. ret = EFI_INVALID_PARAMETER;
  103. goto out;
  104. }
  105. if (this->mode->state == EFI_NETWORK_STOPPED) {
  106. ret = EFI_NOT_STARTED;
  107. } else {
  108. /* Disable hardware and put it into the reset state */
  109. eth_halt();
  110. /* Clear cache of packets */
  111. rx_packet_num = 0;
  112. this->mode->state = EFI_NETWORK_STOPPED;
  113. }
  114. out:
  115. return EFI_EXIT(ret);
  116. }
  117. /*
  118. * efi_net_initialize() - initialize the network interface
  119. *
  120. * This function implements the Initialize service of the
  121. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  122. * (UEFI) specification for details.
  123. *
  124. * @this: pointer to the protocol instance
  125. * @extra_rx: extra receive buffer to be allocated
  126. * @extra_tx: extra transmit buffer to be allocated
  127. * Return: status code
  128. */
  129. static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
  130. ulong extra_rx, ulong extra_tx)
  131. {
  132. int ret;
  133. efi_status_t r = EFI_SUCCESS;
  134. EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
  135. /* Check parameters */
  136. if (!this) {
  137. r = EFI_INVALID_PARAMETER;
  138. goto out;
  139. }
  140. switch (this->mode->state) {
  141. case EFI_NETWORK_INITIALIZED:
  142. case EFI_NETWORK_STARTED:
  143. break;
  144. default:
  145. r = EFI_NOT_STARTED;
  146. goto out;
  147. }
  148. /* Setup packet buffers */
  149. net_init();
  150. /* Disable hardware and put it into the reset state */
  151. eth_halt();
  152. /* Clear cache of packets */
  153. rx_packet_num = 0;
  154. /* Set current device according to environment variables */
  155. eth_set_current();
  156. /* Get hardware ready for send and receive operations */
  157. ret = eth_init();
  158. if (ret < 0) {
  159. eth_halt();
  160. this->mode->state = EFI_NETWORK_STOPPED;
  161. r = EFI_DEVICE_ERROR;
  162. goto out;
  163. } else {
  164. this->int_status = 0;
  165. wait_for_packet->is_signaled = false;
  166. this->mode->state = EFI_NETWORK_INITIALIZED;
  167. }
  168. out:
  169. return EFI_EXIT(r);
  170. }
  171. /*
  172. * efi_net_reset() - reinitialize the network interface
  173. *
  174. * This function implements the Reset service of the
  175. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  176. * (UEFI) specification for details.
  177. *
  178. * @this: pointer to the protocol instance
  179. * @extended_verification: execute exhaustive verification
  180. * Return: status code
  181. */
  182. static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
  183. int extended_verification)
  184. {
  185. efi_status_t ret;
  186. EFI_ENTRY("%p, %x", this, extended_verification);
  187. /* Check parameters */
  188. if (!this) {
  189. ret = EFI_INVALID_PARAMETER;
  190. goto out;
  191. }
  192. switch (this->mode->state) {
  193. case EFI_NETWORK_INITIALIZED:
  194. break;
  195. case EFI_NETWORK_STOPPED:
  196. ret = EFI_NOT_STARTED;
  197. goto out;
  198. default:
  199. ret = EFI_DEVICE_ERROR;
  200. goto out;
  201. }
  202. this->mode->state = EFI_NETWORK_STARTED;
  203. ret = EFI_CALL(efi_net_initialize(this, 0, 0));
  204. out:
  205. return EFI_EXIT(ret);
  206. }
  207. /*
  208. * efi_net_shutdown() - shut down the network interface
  209. *
  210. * This function implements the Shutdown service of the
  211. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  212. * (UEFI) specification for details.
  213. *
  214. * @this: pointer to the protocol instance
  215. * Return: status code
  216. */
  217. static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
  218. {
  219. efi_status_t ret = EFI_SUCCESS;
  220. EFI_ENTRY("%p", this);
  221. /* Check parameters */
  222. if (!this) {
  223. ret = EFI_INVALID_PARAMETER;
  224. goto out;
  225. }
  226. switch (this->mode->state) {
  227. case EFI_NETWORK_INITIALIZED:
  228. break;
  229. case EFI_NETWORK_STOPPED:
  230. ret = EFI_NOT_STARTED;
  231. goto out;
  232. default:
  233. ret = EFI_DEVICE_ERROR;
  234. goto out;
  235. }
  236. eth_halt();
  237. this->int_status = 0;
  238. wait_for_packet->is_signaled = false;
  239. this->mode->state = EFI_NETWORK_STARTED;
  240. out:
  241. return EFI_EXIT(ret);
  242. }
  243. /*
  244. * efi_net_receive_filters() - mange multicast receive filters
  245. *
  246. * This function implements the ReceiveFilters service of the
  247. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  248. * (UEFI) specification for details.
  249. *
  250. * @this: pointer to the protocol instance
  251. * @enable: bit mask of receive filters to enable
  252. * @disable: bit mask of receive filters to disable
  253. * @reset_mcast_filter: true resets contents of the filters
  254. * @mcast_filter_count: number of hardware MAC addresses in the new filters list
  255. * @mcast_filter: list of new filters
  256. * Return: status code
  257. */
  258. static efi_status_t EFIAPI efi_net_receive_filters
  259. (struct efi_simple_network *this, u32 enable, u32 disable,
  260. int reset_mcast_filter, ulong mcast_filter_count,
  261. struct efi_mac_address *mcast_filter)
  262. {
  263. EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
  264. reset_mcast_filter, mcast_filter_count, mcast_filter);
  265. return EFI_EXIT(EFI_UNSUPPORTED);
  266. }
  267. /*
  268. * efi_net_station_address() - set the hardware MAC address
  269. *
  270. * This function implements the StationAddress service of the
  271. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  272. * (UEFI) specification for details.
  273. *
  274. * @this: pointer to the protocol instance
  275. * @reset: if true reset the address to default
  276. * @new_mac: new MAC address
  277. * Return: status code
  278. */
  279. static efi_status_t EFIAPI efi_net_station_address
  280. (struct efi_simple_network *this, int reset,
  281. struct efi_mac_address *new_mac)
  282. {
  283. EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
  284. return EFI_EXIT(EFI_UNSUPPORTED);
  285. }
  286. /*
  287. * efi_net_statistics() - reset or collect statistics of the network interface
  288. *
  289. * This function implements the Statistics service of the
  290. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  291. * (UEFI) specification for details.
  292. *
  293. * @this: pointer to the protocol instance
  294. * @reset: if true, the statistics are reset
  295. * @stat_size: size of the statistics table
  296. * @stat_table: table to receive the statistics
  297. * Return: status code
  298. */
  299. static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
  300. int reset, ulong *stat_size,
  301. void *stat_table)
  302. {
  303. EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
  304. return EFI_EXIT(EFI_UNSUPPORTED);
  305. }
  306. /*
  307. * efi_net_mcastiptomac() - translate multicast IP address to MAC address
  308. *
  309. * This function implements the MCastIPtoMAC service of the
  310. * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
  311. * (UEFI) specification for details.
  312. *
  313. * @this: pointer to the protocol instance
  314. * @ipv6: true if the IP address is an IPv6 address
  315. * @ip: IP address
  316. * @mac: MAC address
  317. * Return: status code
  318. */
  319. static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
  320. int ipv6,
  321. struct efi_ip_address *ip,
  322. struct efi_mac_address *mac)
  323. {
  324. efi_status_t ret = EFI_SUCCESS;
  325. EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
  326. if (!this || !ip || !mac) {
  327. ret = EFI_INVALID_PARAMETER;
  328. goto out;
  329. }
  330. if (ipv6) {
  331. ret = EFI_UNSUPPORTED;
  332. goto out;
  333. }
  334. /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
  335. if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
  336. ret = EFI_INVALID_PARAMETER;
  337. goto out;
  338. };
  339. switch (this->mode->state) {
  340. case EFI_NETWORK_INITIALIZED:
  341. case EFI_NETWORK_STARTED:
  342. break;
  343. default:
  344. ret = EFI_NOT_STARTED;
  345. goto out;
  346. }
  347. memset(mac, 0, sizeof(struct efi_mac_address));
  348. /*
  349. * Copy lower 23 bits of IPv4 multi-cast address
  350. * RFC 1112, RFC 7042 2.1.1.
  351. */
  352. mac->mac_addr[0] = 0x01;
  353. mac->mac_addr[1] = 0x00;
  354. mac->mac_addr[2] = 0x5E;
  355. mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
  356. mac->mac_addr[4] = ip->ip_addr[2];
  357. mac->mac_addr[5] = ip->ip_addr[3];
  358. out:
  359. return EFI_EXIT(ret);
  360. }
  361. /**
  362. * efi_net_nvdata() - read or write NVRAM
  363. *
  364. * This function implements the GetStatus service of the Simple Network
  365. * Protocol. See the UEFI spec for details.
  366. *
  367. * @this: the instance of the Simple Network Protocol
  368. * @read_write: true for read, false for write
  369. * @offset: offset in NVRAM
  370. * @buffer_size: size of buffer
  371. * @buffer: buffer
  372. * Return: status code
  373. */
  374. static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
  375. int read_write, ulong offset,
  376. ulong buffer_size, char *buffer)
  377. {
  378. EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
  379. buffer);
  380. return EFI_EXIT(EFI_UNSUPPORTED);
  381. }
  382. /**
  383. * efi_net_get_status() - get interrupt status
  384. *
  385. * This function implements the GetStatus service of the Simple Network
  386. * Protocol. See the UEFI spec for details.
  387. *
  388. * @this: the instance of the Simple Network Protocol
  389. * @int_status: interface status
  390. * @txbuf: transmission buffer
  391. */
  392. static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
  393. u32 *int_status, void **txbuf)
  394. {
  395. efi_status_t ret = EFI_SUCCESS;
  396. EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
  397. efi_timer_check();
  398. /* Check parameters */
  399. if (!this) {
  400. ret = EFI_INVALID_PARAMETER;
  401. goto out;
  402. }
  403. switch (this->mode->state) {
  404. case EFI_NETWORK_STOPPED:
  405. ret = EFI_NOT_STARTED;
  406. goto out;
  407. case EFI_NETWORK_STARTED:
  408. ret = EFI_DEVICE_ERROR;
  409. goto out;
  410. default:
  411. break;
  412. }
  413. if (int_status) {
  414. *int_status = this->int_status;
  415. this->int_status = 0;
  416. }
  417. if (txbuf)
  418. *txbuf = new_tx_packet;
  419. new_tx_packet = NULL;
  420. out:
  421. return EFI_EXIT(ret);
  422. }
  423. /**
  424. * efi_net_transmit() - transmit a packet
  425. *
  426. * This function implements the Transmit service of the Simple Network Protocol.
  427. * See the UEFI spec for details.
  428. *
  429. * @this: the instance of the Simple Network Protocol
  430. * @header_size: size of the media header
  431. * @buffer_size: size of the buffer to receive the packet
  432. * @buffer: buffer to receive the packet
  433. * @src_addr: source hardware MAC address
  434. * @dest_addr: destination hardware MAC address
  435. * @protocol: type of header to build
  436. * Return: status code
  437. */
  438. static efi_status_t EFIAPI efi_net_transmit
  439. (struct efi_simple_network *this, size_t header_size,
  440. size_t buffer_size, void *buffer,
  441. struct efi_mac_address *src_addr,
  442. struct efi_mac_address *dest_addr, u16 *protocol)
  443. {
  444. efi_status_t ret = EFI_SUCCESS;
  445. EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
  446. (unsigned long)header_size, (unsigned long)buffer_size,
  447. buffer, src_addr, dest_addr, protocol);
  448. efi_timer_check();
  449. /* Check parameters */
  450. if (!this || !buffer) {
  451. ret = EFI_INVALID_PARAMETER;
  452. goto out;
  453. }
  454. /* We do not support jumbo packets */
  455. if (buffer_size > PKTSIZE_ALIGN) {
  456. ret = EFI_INVALID_PARAMETER;
  457. goto out;
  458. }
  459. /* At least the IP header has to fit into the buffer */
  460. if (buffer_size < this->mode->media_header_size) {
  461. ret = EFI_BUFFER_TOO_SMALL;
  462. goto out;
  463. }
  464. /*
  465. * TODO:
  466. * Support VLANs. Use net_set_ether() for copying the header. Use a
  467. * U_BOOT_ENV_CALLBACK to update the media header size.
  468. */
  469. if (header_size) {
  470. struct ethernet_hdr *header = buffer;
  471. if (!dest_addr || !protocol ||
  472. header_size != this->mode->media_header_size) {
  473. ret = EFI_INVALID_PARAMETER;
  474. goto out;
  475. }
  476. if (!src_addr)
  477. src_addr = &this->mode->current_address;
  478. memcpy(header->et_dest, dest_addr, ARP_HLEN);
  479. memcpy(header->et_src, src_addr, ARP_HLEN);
  480. header->et_protlen = htons(*protocol);
  481. }
  482. switch (this->mode->state) {
  483. case EFI_NETWORK_STOPPED:
  484. ret = EFI_NOT_STARTED;
  485. goto out;
  486. case EFI_NETWORK_STARTED:
  487. ret = EFI_DEVICE_ERROR;
  488. goto out;
  489. default:
  490. break;
  491. }
  492. /* Ethernet packets always fit, just bounce */
  493. memcpy(transmit_buffer, buffer, buffer_size);
  494. net_send_packet(transmit_buffer, buffer_size);
  495. new_tx_packet = buffer;
  496. this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  497. out:
  498. return EFI_EXIT(ret);
  499. }
  500. /**
  501. * efi_net_receive() - receive a packet from a network interface
  502. *
  503. * This function implements the Receive service of the Simple Network Protocol.
  504. * See the UEFI spec for details.
  505. *
  506. * @this: the instance of the Simple Network Protocol
  507. * @header_size: size of the media header
  508. * @buffer_size: size of the buffer to receive the packet
  509. * @buffer: buffer to receive the packet
  510. * @src_addr: source MAC address
  511. * @dest_addr: destination MAC address
  512. * @protocol: protocol
  513. * Return: status code
  514. */
  515. static efi_status_t EFIAPI efi_net_receive
  516. (struct efi_simple_network *this, size_t *header_size,
  517. size_t *buffer_size, void *buffer,
  518. struct efi_mac_address *src_addr,
  519. struct efi_mac_address *dest_addr, u16 *protocol)
  520. {
  521. efi_status_t ret = EFI_SUCCESS;
  522. struct ethernet_hdr *eth_hdr;
  523. size_t hdr_size = sizeof(struct ethernet_hdr);
  524. u16 protlen;
  525. EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
  526. buffer_size, buffer, src_addr, dest_addr, protocol);
  527. /* Execute events */
  528. efi_timer_check();
  529. /* Check parameters */
  530. if (!this || !buffer || !buffer_size) {
  531. ret = EFI_INVALID_PARAMETER;
  532. goto out;
  533. }
  534. switch (this->mode->state) {
  535. case EFI_NETWORK_STOPPED:
  536. ret = EFI_NOT_STARTED;
  537. goto out;
  538. case EFI_NETWORK_STARTED:
  539. ret = EFI_DEVICE_ERROR;
  540. goto out;
  541. default:
  542. break;
  543. }
  544. if (!rx_packet_num) {
  545. ret = EFI_NOT_READY;
  546. goto out;
  547. }
  548. /* Fill export parameters */
  549. eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx];
  550. protlen = ntohs(eth_hdr->et_protlen);
  551. if (protlen == 0x8100) {
  552. hdr_size += 4;
  553. protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]);
  554. }
  555. if (header_size)
  556. *header_size = hdr_size;
  557. if (dest_addr)
  558. memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
  559. if (src_addr)
  560. memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
  561. if (protocol)
  562. *protocol = protlen;
  563. if (*buffer_size < receive_lengths[rx_packet_idx]) {
  564. /* Packet doesn't fit, try again with bigger buffer */
  565. *buffer_size = receive_lengths[rx_packet_idx];
  566. ret = EFI_BUFFER_TOO_SMALL;
  567. goto out;
  568. }
  569. /* Copy packet */
  570. memcpy(buffer, receive_buffer[rx_packet_idx],
  571. receive_lengths[rx_packet_idx]);
  572. *buffer_size = receive_lengths[rx_packet_idx];
  573. rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
  574. rx_packet_num--;
  575. if (rx_packet_num)
  576. wait_for_packet->is_signaled = true;
  577. else
  578. this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
  579. out:
  580. return EFI_EXIT(ret);
  581. }
  582. /**
  583. * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
  584. *
  585. * This function is called by dhcp_handler().
  586. *
  587. * @pkt: packet received by dhcp_handler()
  588. * @len: length of the packet received
  589. */
  590. void efi_net_set_dhcp_ack(void *pkt, int len)
  591. {
  592. int maxsize = sizeof(*dhcp_ack);
  593. if (!dhcp_ack) {
  594. dhcp_ack = malloc(maxsize);
  595. if (!dhcp_ack)
  596. return;
  597. }
  598. memset(dhcp_ack, 0, maxsize);
  599. memcpy(dhcp_ack, pkt, min(len, maxsize));
  600. if (netobj)
  601. netobj->pxe_mode.dhcp_ack = *dhcp_ack;
  602. }
  603. /**
  604. * efi_net_push() - callback for received network packet
  605. *
  606. * This function is called when a network packet is received by eth_rx().
  607. *
  608. * @pkt: network packet
  609. * @len: length
  610. */
  611. static void efi_net_push(void *pkt, int len)
  612. {
  613. int rx_packet_next;
  614. /* Check that we at least received an Ethernet header */
  615. if (len < sizeof(struct ethernet_hdr))
  616. return;
  617. /* Check that the buffer won't overflow */
  618. if (len > PKTSIZE_ALIGN)
  619. return;
  620. /* Can't store more than pre-alloced buffer */
  621. if (rx_packet_num >= ETH_PACKETS_BATCH_RECV)
  622. return;
  623. rx_packet_next = (rx_packet_idx + rx_packet_num) %
  624. ETH_PACKETS_BATCH_RECV;
  625. memcpy(receive_buffer[rx_packet_next], pkt, len);
  626. receive_lengths[rx_packet_next] = len;
  627. rx_packet_num++;
  628. }
  629. /**
  630. * efi_network_timer_notify() - check if a new network packet has been received
  631. *
  632. * This notification function is called in every timer cycle.
  633. *
  634. * @event: the event for which this notification function is registered
  635. * @context: event context - not used in this function
  636. */
  637. static void EFIAPI efi_network_timer_notify(struct efi_event *event,
  638. void *context)
  639. {
  640. struct efi_simple_network *this = (struct efi_simple_network *)context;
  641. EFI_ENTRY("%p, %p", event, context);
  642. /*
  643. * Some network drivers do not support calling eth_rx() before
  644. * initialization.
  645. */
  646. if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
  647. goto out;
  648. if (!rx_packet_num) {
  649. push_packet = efi_net_push;
  650. eth_rx();
  651. push_packet = NULL;
  652. if (rx_packet_num) {
  653. this->int_status |=
  654. EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
  655. wait_for_packet->is_signaled = true;
  656. }
  657. }
  658. out:
  659. EFI_EXIT(EFI_SUCCESS);
  660. }
  661. static efi_status_t EFIAPI efi_pxe_base_code_start(
  662. struct efi_pxe_base_code_protocol *this,
  663. u8 use_ipv6)
  664. {
  665. return EFI_UNSUPPORTED;
  666. }
  667. static efi_status_t EFIAPI efi_pxe_base_code_stop(
  668. struct efi_pxe_base_code_protocol *this)
  669. {
  670. return EFI_UNSUPPORTED;
  671. }
  672. static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
  673. struct efi_pxe_base_code_protocol *this,
  674. u8 sort_offers)
  675. {
  676. return EFI_UNSUPPORTED;
  677. }
  678. static efi_status_t EFIAPI efi_pxe_base_code_discover(
  679. struct efi_pxe_base_code_protocol *this,
  680. u16 type, u16 *layer, u8 bis,
  681. struct efi_pxe_base_code_discover_info *info)
  682. {
  683. return EFI_UNSUPPORTED;
  684. }
  685. static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
  686. struct efi_pxe_base_code_protocol *this,
  687. u32 operation, void *buffer_ptr,
  688. u8 overwrite, efi_uintn_t *buffer_size,
  689. struct efi_ip_address server_ip, char *filename,
  690. struct efi_pxe_base_code_mtftp_info *info,
  691. u8 dont_use_buffer)
  692. {
  693. return EFI_UNSUPPORTED;
  694. }
  695. static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
  696. struct efi_pxe_base_code_protocol *this,
  697. u16 op_flags, struct efi_ip_address *dest_ip,
  698. u16 *dest_port,
  699. struct efi_ip_address *gateway_ip,
  700. struct efi_ip_address *src_ip, u16 *src_port,
  701. efi_uintn_t *header_size, void *header_ptr,
  702. efi_uintn_t *buffer_size, void *buffer_ptr)
  703. {
  704. return EFI_UNSUPPORTED;
  705. }
  706. static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
  707. struct efi_pxe_base_code_protocol *this,
  708. u16 op_flags, struct efi_ip_address *dest_ip,
  709. u16 *dest_port, struct efi_ip_address *src_ip,
  710. u16 *src_port, efi_uintn_t *header_size,
  711. void *header_ptr, efi_uintn_t *buffer_size,
  712. void *buffer_ptr)
  713. {
  714. return EFI_UNSUPPORTED;
  715. }
  716. static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
  717. struct efi_pxe_base_code_protocol *this,
  718. struct efi_pxe_base_code_filter *new_filter)
  719. {
  720. return EFI_UNSUPPORTED;
  721. }
  722. static efi_status_t EFIAPI efi_pxe_base_code_arp(
  723. struct efi_pxe_base_code_protocol *this,
  724. struct efi_ip_address *ip_addr,
  725. struct efi_mac_address *mac_addr)
  726. {
  727. return EFI_UNSUPPORTED;
  728. }
  729. static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
  730. struct efi_pxe_base_code_protocol *this,
  731. u8 *new_auto_arp, u8 *new_send_guid,
  732. u8 *new_ttl, u8 *new_tos,
  733. u8 *new_make_callback)
  734. {
  735. return EFI_UNSUPPORTED;
  736. }
  737. static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
  738. struct efi_pxe_base_code_protocol *this,
  739. struct efi_ip_address *new_station_ip,
  740. struct efi_ip_address *new_subnet_mask)
  741. {
  742. return EFI_UNSUPPORTED;
  743. }
  744. static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
  745. struct efi_pxe_base_code_protocol *this,
  746. u8 *new_dhcp_discover_valid,
  747. u8 *new_dhcp_ack_received,
  748. u8 *new_proxy_offer_received,
  749. u8 *new_pxe_discover_valid,
  750. u8 *new_pxe_reply_received,
  751. u8 *new_pxe_bis_reply_received,
  752. EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
  753. EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
  754. EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
  755. EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
  756. EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
  757. EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
  758. {
  759. return EFI_UNSUPPORTED;
  760. }
  761. /**
  762. * efi_net_register() - register the simple network protocol
  763. *
  764. * This gets called from do_bootefi_exec().
  765. */
  766. efi_status_t efi_net_register(void)
  767. {
  768. efi_status_t r;
  769. int i;
  770. if (!eth_get_dev()) {
  771. /* No network device active, don't expose any */
  772. return EFI_SUCCESS;
  773. }
  774. /* We only expose the "active" network device, so one is enough */
  775. netobj = calloc(1, sizeof(*netobj));
  776. if (!netobj)
  777. goto out_of_resources;
  778. /* Allocate an aligned transmit buffer */
  779. transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
  780. if (!transmit_buffer)
  781. goto out_of_resources;
  782. transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
  783. /* Allocate a number of receive buffers */
  784. receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
  785. sizeof(*receive_buffer));
  786. if (!receive_buffer)
  787. goto out_of_resources;
  788. for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
  789. receive_buffer[i] = malloc(PKTSIZE_ALIGN);
  790. if (!receive_buffer[i])
  791. goto out_of_resources;
  792. }
  793. receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
  794. sizeof(*receive_lengths));
  795. if (!receive_lengths)
  796. goto out_of_resources;
  797. /* Hook net up to the device list */
  798. efi_add_handle(&netobj->header);
  799. /* Fill in object data */
  800. r = efi_add_protocol(&netobj->header, &efi_net_guid,
  801. &netobj->net);
  802. if (r != EFI_SUCCESS)
  803. goto failure_to_add_protocol;
  804. r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
  805. efi_dp_from_eth());
  806. if (r != EFI_SUCCESS)
  807. goto failure_to_add_protocol;
  808. r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
  809. &netobj->pxe);
  810. if (r != EFI_SUCCESS)
  811. goto failure_to_add_protocol;
  812. netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
  813. netobj->net.start = efi_net_start;
  814. netobj->net.stop = efi_net_stop;
  815. netobj->net.initialize = efi_net_initialize;
  816. netobj->net.reset = efi_net_reset;
  817. netobj->net.shutdown = efi_net_shutdown;
  818. netobj->net.receive_filters = efi_net_receive_filters;
  819. netobj->net.station_address = efi_net_station_address;
  820. netobj->net.statistics = efi_net_statistics;
  821. netobj->net.mcastiptomac = efi_net_mcastiptomac;
  822. netobj->net.nvdata = efi_net_nvdata;
  823. netobj->net.get_status = efi_net_get_status;
  824. netobj->net.transmit = efi_net_transmit;
  825. netobj->net.receive = efi_net_receive;
  826. netobj->net.mode = &netobj->net_mode;
  827. netobj->net_mode.state = EFI_NETWORK_STOPPED;
  828. memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
  829. netobj->net_mode.hwaddr_size = ARP_HLEN;
  830. netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
  831. netobj->net_mode.max_packet_size = PKTSIZE;
  832. netobj->net_mode.if_type = ARP_ETHER;
  833. netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
  834. netobj->pxe.start = efi_pxe_base_code_start;
  835. netobj->pxe.stop = efi_pxe_base_code_stop;
  836. netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
  837. netobj->pxe.discover = efi_pxe_base_code_discover;
  838. netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
  839. netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
  840. netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
  841. netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
  842. netobj->pxe.arp = efi_pxe_base_code_arp;
  843. netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
  844. netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
  845. netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
  846. netobj->pxe.mode = &netobj->pxe_mode;
  847. if (dhcp_ack)
  848. netobj->pxe_mode.dhcp_ack = *dhcp_ack;
  849. /*
  850. * Create WaitForPacket event.
  851. */
  852. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
  853. efi_network_timer_notify, NULL, NULL,
  854. &wait_for_packet);
  855. if (r != EFI_SUCCESS) {
  856. printf("ERROR: Failed to register network event\n");
  857. return r;
  858. }
  859. netobj->net.wait_for_packet = wait_for_packet;
  860. /*
  861. * Create a timer event.
  862. *
  863. * The notification function is used to check if a new network packet
  864. * has been received.
  865. *
  866. * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
  867. */
  868. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
  869. efi_network_timer_notify, &netobj->net, NULL,
  870. &network_timer_event);
  871. if (r != EFI_SUCCESS) {
  872. printf("ERROR: Failed to register network event\n");
  873. return r;
  874. }
  875. /* Network is time critical, create event in every timer cycle */
  876. r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
  877. if (r != EFI_SUCCESS) {
  878. printf("ERROR: Failed to set network timer\n");
  879. return r;
  880. }
  881. return EFI_SUCCESS;
  882. failure_to_add_protocol:
  883. printf("ERROR: Failure to add protocol\n");
  884. return r;
  885. out_of_resources:
  886. free(netobj);
  887. netobj = NULL;
  888. free(transmit_buffer);
  889. if (receive_buffer)
  890. for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
  891. free(receive_buffer[i]);
  892. free(receive_buffer);
  893. free(receive_lengths);
  894. printf("ERROR: Out of memory\n");
  895. return EFI_OUT_OF_RESOURCES;
  896. }