efi_net.c 25 KB

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