efi_net.c 25 KB

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