Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 3 | * Simple network protocol |
| 4 | * PXE base code protocol |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 5 | * |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 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 |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
Heinrich Schuchardt | 955a321 | 2025-01-16 20:26:59 +0100 | [diff] [blame] | 18 | #define LOG_CATEGORY LOGC_EFI |
| 19 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 20 | #include <efi_loader.h> |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 21 | #include <dm.h> |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 22 | #include <linux/sizes.h> |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 23 | #include <malloc.h> |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 24 | #include <vsprintf.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 25 | #include <net.h> |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 26 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 27 | #define MAX_NUM_DP_ENTRIES 10 |
| 28 | |
Adriano Cordova | 7f2bcd4 | 2025-03-03 11:13:11 -0300 | [diff] [blame] | 29 | const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 30 | static const efi_guid_t efi_pxe_base_code_protocol_guid = |
| 31 | EFI_PXE_BASE_CODE_PROTOCOL_GUID; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 32 | static struct efi_pxe_packet *dhcp_ack; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 33 | static void *new_tx_packet; |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 34 | static void *transmit_buffer; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 35 | static uchar **receive_buffer; |
| 36 | static size_t *receive_lengths; |
| 37 | static int rx_packet_idx; |
| 38 | static int rx_packet_num; |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 39 | static struct efi_net_obj *netobj; |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 40 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 41 | struct dp_entry { |
| 42 | struct efi_device_path *net_dp; |
| 43 | struct udevice *dev; |
| 44 | bool is_valid; |
| 45 | }; |
| 46 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 47 | /* |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 48 | * The network device path cache. An entry is added when a new bootfile |
| 49 | * is downloaded from the network. If the bootfile is then loaded as an |
| 50 | * efi image, the most recent entry corresponding to the device is passed |
| 51 | * as the device path of the loaded image. |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 52 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 53 | static struct dp_entry dp_cache[MAX_NUM_DP_ENTRIES]; |
| 54 | static int next_dp_entry; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 55 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 56 | #if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL) |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 57 | static struct wget_http_info efi_wget_info = { |
| 58 | .set_bootdev = false, |
| 59 | .check_buffer_size = true, |
| 60 | |
| 61 | }; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 62 | #endif |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 63 | |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 64 | /* |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 65 | * The notification function of this event is called in every timer cycle |
| 66 | * to check if a new network packet has been received. |
| 67 | */ |
| 68 | static struct efi_event *network_timer_event; |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 69 | /* |
| 70 | * This event is signaled when a packet has been received. |
| 71 | */ |
| 72 | static struct efi_event *wait_for_packet; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 73 | |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 74 | /** |
| 75 | * struct efi_net_obj - EFI object representing a network interface |
| 76 | * |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 77 | * @header: EFI object header |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 78 | * @dev: net udevice |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 79 | * @net: simple network protocol interface |
| 80 | * @net_mode: status of the network interface |
| 81 | * @pxe: PXE base code protocol interface |
| 82 | * @pxe_mode: status of the PXE base code protocol |
| 83 | * @ip4_config2: IP4 Config2 protocol interface |
Adriano Cordova | e9b19eb | 2024-12-04 00:05:26 -0300 | [diff] [blame] | 84 | * @http_service_binding: Http service binding protocol interface |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 85 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 86 | struct efi_net_obj { |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 87 | struct efi_object header; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 88 | struct udevice *dev; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 89 | struct efi_simple_network net; |
| 90 | struct efi_simple_network_mode net_mode; |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 91 | struct efi_pxe_base_code_protocol pxe; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 92 | struct efi_pxe_mode pxe_mode; |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 93 | #if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL) |
| 94 | struct efi_ip4_config2_protocol ip4_config2; |
| 95 | #endif |
Adriano Cordova | e9b19eb | 2024-12-04 00:05:26 -0300 | [diff] [blame] | 96 | #if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL) |
| 97 | struct efi_service_binding_protocol http_service_binding; |
| 98 | #endif |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 101 | /* |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 102 | * efi_netobj_is_active() - checks if a netobj is active in the efi subsystem |
| 103 | * |
| 104 | * @netobj: pointer to efi_net_obj |
| 105 | * Return: true if active |
| 106 | */ |
| 107 | static bool efi_netobj_is_active(struct efi_net_obj *netobj) |
| 108 | { |
| 109 | if (!netobj || !efi_search_obj(&netobj->header)) |
| 110 | return false; |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 117 | * efi_net_start() - start the network interface |
| 118 | * |
| 119 | * This function implements the Start service of the |
| 120 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 121 | * (UEFI) specification for details. |
| 122 | * |
| 123 | * @this: pointer to the protocol instance |
| 124 | * Return: status code |
| 125 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 126 | static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this) |
| 127 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 128 | efi_status_t ret = EFI_SUCCESS; |
| 129 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 130 | EFI_ENTRY("%p", this); |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 131 | /* Check parameters */ |
| 132 | if (!this) { |
| 133 | ret = EFI_INVALID_PARAMETER; |
| 134 | goto out; |
| 135 | } |
| 136 | |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 137 | if (this->mode->state != EFI_NETWORK_STOPPED) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 138 | ret = EFI_ALREADY_STARTED; |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 139 | } else { |
| 140 | this->int_status = 0; |
| 141 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 142 | this->mode->state = EFI_NETWORK_STARTED; |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 143 | } |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 144 | out: |
| 145 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 148 | /* |
| 149 | * efi_net_stop() - stop the network interface |
| 150 | * |
| 151 | * This function implements the Stop service of the |
| 152 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 153 | * (UEFI) specification for details. |
| 154 | * |
| 155 | * @this: pointer to the protocol instance |
| 156 | * Return: status code |
| 157 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 158 | static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this) |
| 159 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 160 | efi_status_t ret = EFI_SUCCESS; |
| 161 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 162 | EFI_ENTRY("%p", this); |
| 163 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 164 | /* Check parameters */ |
| 165 | if (!this) { |
| 166 | ret = EFI_INVALID_PARAMETER; |
| 167 | goto out; |
| 168 | } |
| 169 | |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 170 | if (this->mode->state == EFI_NETWORK_STOPPED) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 171 | ret = EFI_NOT_STARTED; |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 172 | } else { |
| 173 | /* Disable hardware and put it into the reset state */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 174 | eth_set_dev(netobj->dev); |
| 175 | env_set("ethact", eth_get_name()); |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 176 | eth_halt(); |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 177 | /* Clear cache of packets */ |
| 178 | rx_packet_num = 0; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 179 | this->mode->state = EFI_NETWORK_STOPPED; |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 180 | } |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 181 | out: |
| 182 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 185 | /* |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 186 | * efi_net_initialize() - initialize the network interface |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 187 | * |
| 188 | * This function implements the Initialize service of the |
| 189 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 190 | * (UEFI) specification for details. |
| 191 | * |
| 192 | * @this: pointer to the protocol instance |
| 193 | * @extra_rx: extra receive buffer to be allocated |
| 194 | * @extra_tx: extra transmit buffer to be allocated |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 195 | * Return: status code |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 196 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 197 | static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this, |
| 198 | ulong extra_rx, ulong extra_tx) |
| 199 | { |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 200 | int ret; |
| 201 | efi_status_t r = EFI_SUCCESS; |
| 202 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 203 | EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx); |
| 204 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 205 | /* Check parameters */ |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 206 | if (!this) { |
| 207 | r = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 208 | goto out; |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 211 | switch (this->mode->state) { |
| 212 | case EFI_NETWORK_INITIALIZED: |
| 213 | case EFI_NETWORK_STARTED: |
| 214 | break; |
| 215 | default: |
| 216 | r = EFI_NOT_STARTED; |
| 217 | goto out; |
| 218 | } |
| 219 | |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 220 | /* Setup packet buffers */ |
| 221 | net_init(); |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 222 | /* Clear cache of packets */ |
| 223 | rx_packet_num = 0; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 224 | /* Set the net device corresponding to the efi net object */ |
| 225 | eth_set_dev(netobj->dev); |
| 226 | env_set("ethact", eth_get_name()); |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 227 | /* Get hardware ready for send and receive operations */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 228 | ret = eth_start_udev(netobj->dev); |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 229 | if (ret < 0) { |
| 230 | eth_halt(); |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 231 | this->mode->state = EFI_NETWORK_STOPPED; |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 232 | r = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 233 | goto out; |
| 234 | } else { |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 235 | this->int_status = 0; |
| 236 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 237 | this->mode->state = EFI_NETWORK_INITIALIZED; |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 238 | } |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 239 | out: |
Heinrich Schuchardt | ad4d67c | 2018-04-03 22:06:52 +0200 | [diff] [blame] | 240 | return EFI_EXIT(r); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 243 | /* |
| 244 | * efi_net_reset() - reinitialize the network interface |
| 245 | * |
| 246 | * This function implements the Reset 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 | * @extended_verification: execute exhaustive verification |
| 252 | * Return: status code |
| 253 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 254 | static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this, |
| 255 | int extended_verification) |
| 256 | { |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 257 | efi_status_t ret; |
| 258 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 259 | EFI_ENTRY("%p, %x", this, extended_verification); |
| 260 | |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 261 | /* Check parameters */ |
| 262 | if (!this) { |
| 263 | ret = EFI_INVALID_PARAMETER; |
| 264 | goto out; |
| 265 | } |
| 266 | |
| 267 | switch (this->mode->state) { |
| 268 | case EFI_NETWORK_INITIALIZED: |
| 269 | break; |
| 270 | case EFI_NETWORK_STOPPED: |
| 271 | ret = EFI_NOT_STARTED; |
| 272 | goto out; |
| 273 | default: |
| 274 | ret = EFI_DEVICE_ERROR; |
| 275 | goto out; |
| 276 | } |
| 277 | |
| 278 | this->mode->state = EFI_NETWORK_STARTED; |
| 279 | ret = EFI_CALL(efi_net_initialize(this, 0, 0)); |
| 280 | out: |
| 281 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 284 | /* |
| 285 | * efi_net_shutdown() - shut down the network interface |
| 286 | * |
| 287 | * This function implements the Shutdown service of the |
| 288 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 289 | * (UEFI) specification for details. |
| 290 | * |
| 291 | * @this: pointer to the protocol instance |
| 292 | * Return: status code |
| 293 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 294 | static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this) |
| 295 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 296 | efi_status_t ret = EFI_SUCCESS; |
| 297 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 298 | EFI_ENTRY("%p", this); |
| 299 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 300 | /* Check parameters */ |
| 301 | if (!this) { |
| 302 | ret = EFI_INVALID_PARAMETER; |
| 303 | goto out; |
| 304 | } |
| 305 | |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 306 | switch (this->mode->state) { |
| 307 | case EFI_NETWORK_INITIALIZED: |
| 308 | break; |
| 309 | case EFI_NETWORK_STOPPED: |
| 310 | ret = EFI_NOT_STARTED; |
| 311 | goto out; |
| 312 | default: |
| 313 | ret = EFI_DEVICE_ERROR; |
| 314 | goto out; |
| 315 | } |
| 316 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 317 | eth_set_dev(netobj->dev); |
| 318 | env_set("ethact", eth_get_name()); |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 319 | eth_halt(); |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 320 | this->int_status = 0; |
| 321 | wait_for_packet->is_signaled = false; |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 322 | this->mode->state = EFI_NETWORK_STARTED; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 323 | |
| 324 | out: |
| 325 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 328 | /* |
| 329 | * efi_net_receive_filters() - mange multicast receive filters |
| 330 | * |
| 331 | * This function implements the ReceiveFilters service of the |
| 332 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 333 | * (UEFI) specification for details. |
| 334 | * |
| 335 | * @this: pointer to the protocol instance |
| 336 | * @enable: bit mask of receive filters to enable |
| 337 | * @disable: bit mask of receive filters to disable |
| 338 | * @reset_mcast_filter: true resets contents of the filters |
| 339 | * @mcast_filter_count: number of hardware MAC addresses in the new filters list |
| 340 | * @mcast_filter: list of new filters |
| 341 | * Return: status code |
| 342 | */ |
| 343 | static efi_status_t EFIAPI efi_net_receive_filters |
| 344 | (struct efi_simple_network *this, u32 enable, u32 disable, |
| 345 | int reset_mcast_filter, ulong mcast_filter_count, |
| 346 | struct efi_mac_address *mcast_filter) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 347 | { |
| 348 | EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable, |
| 349 | reset_mcast_filter, mcast_filter_count, mcast_filter); |
| 350 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 351 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 354 | /* |
| 355 | * efi_net_station_address() - set the hardware MAC address |
| 356 | * |
| 357 | * This function implements the StationAddress service of the |
| 358 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 359 | * (UEFI) specification for details. |
| 360 | * |
| 361 | * @this: pointer to the protocol instance |
| 362 | * @reset: if true reset the address to default |
| 363 | * @new_mac: new MAC address |
| 364 | * Return: status code |
| 365 | */ |
| 366 | static efi_status_t EFIAPI efi_net_station_address |
| 367 | (struct efi_simple_network *this, int reset, |
| 368 | struct efi_mac_address *new_mac) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 369 | { |
| 370 | EFI_ENTRY("%p, %x, %p", this, reset, new_mac); |
| 371 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 372 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 375 | /* |
| 376 | * efi_net_statistics() - reset or collect statistics of the network interface |
| 377 | * |
| 378 | * This function implements the Statistics service of the |
| 379 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 380 | * (UEFI) specification for details. |
| 381 | * |
| 382 | * @this: pointer to the protocol instance |
| 383 | * @reset: if true, the statistics are reset |
| 384 | * @stat_size: size of the statistics table |
| 385 | * @stat_table: table to receive the statistics |
| 386 | * Return: status code |
| 387 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 388 | static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this, |
| 389 | int reset, ulong *stat_size, |
| 390 | void *stat_table) |
| 391 | { |
| 392 | EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table); |
| 393 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 394 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 397 | /* |
| 398 | * efi_net_mcastiptomac() - translate multicast IP address to MAC address |
| 399 | * |
Heinrich Schuchardt | 33b318d | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 400 | * This function implements the MCastIPtoMAC service of the |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 401 | * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface |
| 402 | * (UEFI) specification for details. |
| 403 | * |
| 404 | * @this: pointer to the protocol instance |
| 405 | * @ipv6: true if the IP address is an IPv6 address |
| 406 | * @ip: IP address |
| 407 | * @mac: MAC address |
| 408 | * Return: status code |
| 409 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 410 | static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this, |
| 411 | int ipv6, |
| 412 | struct efi_ip_address *ip, |
| 413 | struct efi_mac_address *mac) |
| 414 | { |
Heinrich Schuchardt | 33b318d | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 415 | efi_status_t ret = EFI_SUCCESS; |
| 416 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 417 | EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac); |
| 418 | |
Heinrich Schuchardt | 33b318d | 2019-09-01 17:17:53 +0200 | [diff] [blame] | 419 | if (!this || !ip || !mac) { |
| 420 | ret = EFI_INVALID_PARAMETER; |
| 421 | goto out; |
| 422 | } |
| 423 | |
| 424 | if (ipv6) { |
| 425 | ret = EFI_UNSUPPORTED; |
| 426 | goto out; |
| 427 | } |
| 428 | |
| 429 | /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */ |
| 430 | if ((ip->ip_addr[0] & 0xf0) != 0xe0) { |
| 431 | ret = EFI_INVALID_PARAMETER; |
| 432 | goto out; |
| 433 | }; |
| 434 | |
| 435 | switch (this->mode->state) { |
| 436 | case EFI_NETWORK_INITIALIZED: |
| 437 | case EFI_NETWORK_STARTED: |
| 438 | break; |
| 439 | default: |
| 440 | ret = EFI_NOT_STARTED; |
| 441 | goto out; |
| 442 | } |
| 443 | |
| 444 | memset(mac, 0, sizeof(struct efi_mac_address)); |
| 445 | |
| 446 | /* |
| 447 | * Copy lower 23 bits of IPv4 multi-cast address |
| 448 | * RFC 1112, RFC 7042 2.1.1. |
| 449 | */ |
| 450 | mac->mac_addr[0] = 0x01; |
| 451 | mac->mac_addr[1] = 0x00; |
| 452 | mac->mac_addr[2] = 0x5E; |
| 453 | mac->mac_addr[3] = ip->ip_addr[1] & 0x7F; |
| 454 | mac->mac_addr[4] = ip->ip_addr[2]; |
| 455 | mac->mac_addr[5] = ip->ip_addr[3]; |
| 456 | out: |
| 457 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 460 | /** |
| 461 | * efi_net_nvdata() - read or write NVRAM |
| 462 | * |
| 463 | * This function implements the GetStatus service of the Simple Network |
| 464 | * Protocol. See the UEFI spec for details. |
| 465 | * |
| 466 | * @this: the instance of the Simple Network Protocol |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 467 | * @read_write: true for read, false for write |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 468 | * @offset: offset in NVRAM |
| 469 | * @buffer_size: size of buffer |
| 470 | * @buffer: buffer |
| 471 | * Return: status code |
| 472 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 473 | static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this, |
| 474 | int read_write, ulong offset, |
| 475 | ulong buffer_size, char *buffer) |
| 476 | { |
| 477 | EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size, |
| 478 | buffer); |
| 479 | |
Heinrich Schuchardt | a50b5c6 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 480 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 483 | /** |
| 484 | * efi_net_get_status() - get interrupt status |
| 485 | * |
| 486 | * This function implements the GetStatus service of the Simple Network |
| 487 | * Protocol. See the UEFI spec for details. |
| 488 | * |
| 489 | * @this: the instance of the Simple Network Protocol |
| 490 | * @int_status: interface status |
| 491 | * @txbuf: transmission buffer |
| 492 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 493 | static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this, |
| 494 | u32 *int_status, void **txbuf) |
| 495 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 496 | efi_status_t ret = EFI_SUCCESS; |
| 497 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 498 | EFI_ENTRY("%p, %p, %p", this, int_status, txbuf); |
| 499 | |
Heinrich Schuchardt | e371c5f | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 500 | efi_timer_check(); |
| 501 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 502 | /* Check parameters */ |
| 503 | if (!this) { |
| 504 | ret = EFI_INVALID_PARAMETER; |
| 505 | goto out; |
| 506 | } |
| 507 | |
| 508 | switch (this->mode->state) { |
| 509 | case EFI_NETWORK_STOPPED: |
| 510 | ret = EFI_NOT_STARTED; |
| 511 | goto out; |
| 512 | case EFI_NETWORK_STARTED: |
| 513 | ret = EFI_DEVICE_ERROR; |
| 514 | goto out; |
| 515 | default: |
| 516 | break; |
| 517 | } |
| 518 | |
Heinrich Schuchardt | e371c5f | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 519 | if (int_status) { |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 520 | *int_status = this->int_status; |
| 521 | this->int_status = 0; |
Heinrich Schuchardt | e371c5f | 2017-10-05 16:36:02 +0200 | [diff] [blame] | 522 | } |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 523 | if (txbuf) |
| 524 | *txbuf = new_tx_packet; |
| 525 | |
| 526 | new_tx_packet = NULL; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 527 | out: |
| 528 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 531 | /** |
| 532 | * efi_net_transmit() - transmit a packet |
| 533 | * |
| 534 | * This function implements the Transmit service of the Simple Network Protocol. |
| 535 | * See the UEFI spec for details. |
| 536 | * |
| 537 | * @this: the instance of the Simple Network Protocol |
| 538 | * @header_size: size of the media header |
| 539 | * @buffer_size: size of the buffer to receive the packet |
| 540 | * @buffer: buffer to receive the packet |
| 541 | * @src_addr: source hardware MAC address |
| 542 | * @dest_addr: destination hardware MAC address |
| 543 | * @protocol: type of header to build |
| 544 | * Return: status code |
| 545 | */ |
| 546 | static efi_status_t EFIAPI efi_net_transmit |
| 547 | (struct efi_simple_network *this, size_t header_size, |
| 548 | size_t buffer_size, void *buffer, |
| 549 | struct efi_mac_address *src_addr, |
| 550 | struct efi_mac_address *dest_addr, u16 *protocol) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 551 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 552 | efi_status_t ret = EFI_SUCCESS; |
| 553 | |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 554 | EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this, |
| 555 | (unsigned long)header_size, (unsigned long)buffer_size, |
| 556 | buffer, src_addr, dest_addr, protocol); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 557 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 558 | efi_timer_check(); |
| 559 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 560 | /* Check parameters */ |
Heinrich Schuchardt | f286c2c | 2019-05-15 23:27:43 +0200 | [diff] [blame] | 561 | if (!this || !buffer) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 562 | ret = EFI_INVALID_PARAMETER; |
| 563 | goto out; |
| 564 | } |
| 565 | |
| 566 | /* We do not support jumbo packets */ |
| 567 | if (buffer_size > PKTSIZE_ALIGN) { |
| 568 | ret = EFI_INVALID_PARAMETER; |
| 569 | goto out; |
| 570 | } |
| 571 | |
Heinrich Schuchardt | 0218a8f | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 572 | /* At least the IP header has to fit into the buffer */ |
| 573 | if (buffer_size < this->mode->media_header_size) { |
| 574 | ret = EFI_BUFFER_TOO_SMALL; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 575 | goto out; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Heinrich Schuchardt | 0218a8f | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 578 | /* |
| 579 | * TODO: |
| 580 | * Support VLANs. Use net_set_ether() for copying the header. Use a |
| 581 | * U_BOOT_ENV_CALLBACK to update the media header size. |
| 582 | */ |
| 583 | if (header_size) { |
| 584 | struct ethernet_hdr *header = buffer; |
| 585 | |
| 586 | if (!dest_addr || !protocol || |
| 587 | header_size != this->mode->media_header_size) { |
| 588 | ret = EFI_INVALID_PARAMETER; |
| 589 | goto out; |
| 590 | } |
| 591 | if (!src_addr) |
| 592 | src_addr = &this->mode->current_address; |
| 593 | |
| 594 | memcpy(header->et_dest, dest_addr, ARP_HLEN); |
| 595 | memcpy(header->et_src, src_addr, ARP_HLEN); |
| 596 | header->et_protlen = htons(*protocol); |
| 597 | } |
| 598 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 599 | switch (this->mode->state) { |
| 600 | case EFI_NETWORK_STOPPED: |
| 601 | ret = EFI_NOT_STARTED; |
| 602 | goto out; |
| 603 | case EFI_NETWORK_STARTED: |
| 604 | ret = EFI_DEVICE_ERROR; |
| 605 | goto out; |
| 606 | default: |
| 607 | break; |
| 608 | } |
| 609 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 610 | eth_set_dev(netobj->dev); |
| 611 | env_set("ethact", eth_get_name()); |
| 612 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 613 | /* Ethernet packets always fit, just bounce */ |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 614 | memcpy(transmit_buffer, buffer, buffer_size); |
| 615 | net_send_packet(transmit_buffer, buffer_size); |
Alexander Graf | 0b12b86 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 616 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 617 | new_tx_packet = buffer; |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 618 | this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 619 | out: |
| 620 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 623 | /** |
| 624 | * efi_net_receive() - receive a packet from a network interface |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 625 | * |
| 626 | * This function implements the Receive service of the Simple Network Protocol. |
| 627 | * See the UEFI spec for details. |
| 628 | * |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 629 | * @this: the instance of the Simple Network Protocol |
| 630 | * @header_size: size of the media header |
| 631 | * @buffer_size: size of the buffer to receive the packet |
| 632 | * @buffer: buffer to receive the packet |
| 633 | * @src_addr: source MAC address |
| 634 | * @dest_addr: destination MAC address |
| 635 | * @protocol: protocol |
| 636 | * Return: status code |
Heinrich Schuchardt | f66b2e3 | 2017-10-05 16:36:03 +0200 | [diff] [blame] | 637 | */ |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 638 | static efi_status_t EFIAPI efi_net_receive |
| 639 | (struct efi_simple_network *this, size_t *header_size, |
| 640 | size_t *buffer_size, void *buffer, |
| 641 | struct efi_mac_address *src_addr, |
| 642 | struct efi_mac_address *dest_addr, u16 *protocol) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 643 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 644 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 645 | struct ethernet_hdr *eth_hdr; |
| 646 | size_t hdr_size = sizeof(struct ethernet_hdr); |
| 647 | u16 protlen; |
| 648 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 649 | EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size, |
| 650 | buffer_size, buffer, src_addr, dest_addr, protocol); |
| 651 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 652 | /* Execute events */ |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 653 | efi_timer_check(); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 654 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 655 | /* Check parameters */ |
Heinrich Schuchardt | f286c2c | 2019-05-15 23:27:43 +0200 | [diff] [blame] | 656 | if (!this || !buffer || !buffer_size) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 657 | ret = EFI_INVALID_PARAMETER; |
| 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | switch (this->mode->state) { |
| 662 | case EFI_NETWORK_STOPPED: |
| 663 | ret = EFI_NOT_STARTED; |
| 664 | goto out; |
| 665 | case EFI_NETWORK_STARTED: |
| 666 | ret = EFI_DEVICE_ERROR; |
| 667 | goto out; |
| 668 | default: |
| 669 | break; |
| 670 | } |
| 671 | |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 672 | if (!rx_packet_num) { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 673 | ret = EFI_NOT_READY; |
| 674 | goto out; |
| 675 | } |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 676 | /* Fill export parameters */ |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 677 | eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx]; |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 678 | protlen = ntohs(eth_hdr->et_protlen); |
| 679 | if (protlen == 0x8100) { |
| 680 | hdr_size += 4; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 681 | protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]); |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 682 | } |
| 683 | if (header_size) |
| 684 | *header_size = hdr_size; |
| 685 | if (dest_addr) |
| 686 | memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN); |
| 687 | if (src_addr) |
| 688 | memcpy(src_addr, eth_hdr->et_src, ARP_HLEN); |
| 689 | if (protocol) |
| 690 | *protocol = protlen; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 691 | if (*buffer_size < receive_lengths[rx_packet_idx]) { |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 692 | /* Packet doesn't fit, try again with bigger buffer */ |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 693 | *buffer_size = receive_lengths[rx_packet_idx]; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 694 | ret = EFI_BUFFER_TOO_SMALL; |
| 695 | goto out; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 696 | } |
Heinrich Schuchardt | aa0d197 | 2017-10-05 16:36:04 +0200 | [diff] [blame] | 697 | /* Copy packet */ |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 698 | memcpy(buffer, receive_buffer[rx_packet_idx], |
| 699 | receive_lengths[rx_packet_idx]); |
| 700 | *buffer_size = receive_lengths[rx_packet_idx]; |
| 701 | rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV; |
| 702 | rx_packet_num--; |
| 703 | if (rx_packet_num) |
| 704 | wait_for_packet->is_signaled = true; |
| 705 | else |
| 706 | this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 707 | out: |
| 708 | return EFI_EXIT(ret); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 709 | } |
| 710 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 711 | /** |
| 712 | * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address |
| 713 | * |
| 714 | * This function is called by dhcp_handler(). |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 715 | * |
| 716 | * @pkt: packet received by dhcp_handler() |
| 717 | * @len: length of the packet received |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 718 | */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 719 | void efi_net_set_dhcp_ack(void *pkt, int len) |
| 720 | { |
| 721 | int maxsize = sizeof(*dhcp_ack); |
| 722 | |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 723 | if (!dhcp_ack) { |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 724 | dhcp_ack = malloc(maxsize); |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 725 | if (!dhcp_ack) |
| 726 | return; |
| 727 | } |
| 728 | memset(dhcp_ack, 0, maxsize); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 729 | memcpy(dhcp_ack, pkt, min(len, maxsize)); |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 730 | |
| 731 | if (netobj) |
| 732 | netobj->pxe_mode.dhcp_ack = *dhcp_ack; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 733 | } |
| 734 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 735 | /** |
| 736 | * efi_net_push() - callback for received network packet |
| 737 | * |
| 738 | * This function is called when a network packet is received by eth_rx(). |
| 739 | * |
| 740 | * @pkt: network packet |
| 741 | * @len: length |
| 742 | */ |
| 743 | static void efi_net_push(void *pkt, int len) |
| 744 | { |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 745 | int rx_packet_next; |
| 746 | |
| 747 | /* Check that we at least received an Ethernet header */ |
| 748 | if (len < sizeof(struct ethernet_hdr)) |
| 749 | return; |
| 750 | |
| 751 | /* Check that the buffer won't overflow */ |
| 752 | if (len > PKTSIZE_ALIGN) |
| 753 | return; |
| 754 | |
| 755 | /* Can't store more than pre-alloced buffer */ |
| 756 | if (rx_packet_num >= ETH_PACKETS_BATCH_RECV) |
| 757 | return; |
| 758 | |
| 759 | rx_packet_next = (rx_packet_idx + rx_packet_num) % |
| 760 | ETH_PACKETS_BATCH_RECV; |
| 761 | memcpy(receive_buffer[rx_packet_next], pkt, len); |
| 762 | receive_lengths[rx_packet_next] = len; |
| 763 | |
| 764 | rx_packet_num++; |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /** |
| 768 | * efi_network_timer_notify() - check if a new network packet has been received |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 769 | * |
| 770 | * This notification function is called in every timer cycle. |
| 771 | * |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 772 | * @event: the event for which this notification function is registered |
| 773 | * @context: event context - not used in this function |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 774 | */ |
| 775 | static void EFIAPI efi_network_timer_notify(struct efi_event *event, |
| 776 | void *context) |
| 777 | { |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 778 | struct efi_simple_network *this = (struct efi_simple_network *)context; |
| 779 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 780 | EFI_ENTRY("%p, %p", event, context); |
| 781 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 782 | /* |
| 783 | * Some network drivers do not support calling eth_rx() before |
| 784 | * initialization. |
| 785 | */ |
| 786 | if (!this || this->mode->state != EFI_NETWORK_INITIALIZED) |
| 787 | goto out; |
| 788 | |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 789 | if (!rx_packet_num) { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 790 | eth_set_dev(netobj->dev); |
| 791 | env_set("ethact", eth_get_name()); |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 792 | push_packet = efi_net_push; |
| 793 | eth_rx(); |
| 794 | push_packet = NULL; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 795 | if (rx_packet_num) { |
| 796 | this->int_status |= |
| 797 | EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT; |
| 798 | wait_for_packet->is_signaled = true; |
Heinrich Schuchardt | d3f1ff2 | 2019-08-31 09:56:30 +0200 | [diff] [blame] | 799 | } |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 800 | } |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 801 | out: |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 802 | EFI_EXIT(EFI_SUCCESS); |
| 803 | } |
| 804 | |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 805 | static efi_status_t EFIAPI efi_pxe_base_code_start( |
| 806 | struct efi_pxe_base_code_protocol *this, |
| 807 | u8 use_ipv6) |
| 808 | { |
| 809 | return EFI_UNSUPPORTED; |
| 810 | } |
| 811 | |
| 812 | static efi_status_t EFIAPI efi_pxe_base_code_stop( |
| 813 | struct efi_pxe_base_code_protocol *this) |
| 814 | { |
| 815 | return EFI_UNSUPPORTED; |
| 816 | } |
| 817 | |
| 818 | static efi_status_t EFIAPI efi_pxe_base_code_dhcp( |
| 819 | struct efi_pxe_base_code_protocol *this, |
| 820 | u8 sort_offers) |
| 821 | { |
| 822 | return EFI_UNSUPPORTED; |
| 823 | } |
| 824 | |
| 825 | static efi_status_t EFIAPI efi_pxe_base_code_discover( |
| 826 | struct efi_pxe_base_code_protocol *this, |
| 827 | u16 type, u16 *layer, u8 bis, |
| 828 | struct efi_pxe_base_code_discover_info *info) |
| 829 | { |
| 830 | return EFI_UNSUPPORTED; |
| 831 | } |
| 832 | |
| 833 | static efi_status_t EFIAPI efi_pxe_base_code_mtftp( |
| 834 | struct efi_pxe_base_code_protocol *this, |
| 835 | u32 operation, void *buffer_ptr, |
| 836 | u8 overwrite, efi_uintn_t *buffer_size, |
| 837 | struct efi_ip_address server_ip, char *filename, |
| 838 | struct efi_pxe_base_code_mtftp_info *info, |
| 839 | u8 dont_use_buffer) |
| 840 | { |
| 841 | return EFI_UNSUPPORTED; |
| 842 | } |
| 843 | |
| 844 | static efi_status_t EFIAPI efi_pxe_base_code_udp_write( |
| 845 | struct efi_pxe_base_code_protocol *this, |
| 846 | u16 op_flags, struct efi_ip_address *dest_ip, |
| 847 | u16 *dest_port, |
| 848 | struct efi_ip_address *gateway_ip, |
| 849 | struct efi_ip_address *src_ip, u16 *src_port, |
| 850 | efi_uintn_t *header_size, void *header_ptr, |
| 851 | efi_uintn_t *buffer_size, void *buffer_ptr) |
| 852 | { |
| 853 | return EFI_UNSUPPORTED; |
| 854 | } |
| 855 | |
| 856 | static efi_status_t EFIAPI efi_pxe_base_code_udp_read( |
| 857 | struct efi_pxe_base_code_protocol *this, |
| 858 | u16 op_flags, struct efi_ip_address *dest_ip, |
| 859 | u16 *dest_port, struct efi_ip_address *src_ip, |
| 860 | u16 *src_port, efi_uintn_t *header_size, |
| 861 | void *header_ptr, efi_uintn_t *buffer_size, |
| 862 | void *buffer_ptr) |
| 863 | { |
| 864 | return EFI_UNSUPPORTED; |
| 865 | } |
| 866 | |
| 867 | static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter( |
| 868 | struct efi_pxe_base_code_protocol *this, |
| 869 | struct efi_pxe_base_code_filter *new_filter) |
| 870 | { |
| 871 | return EFI_UNSUPPORTED; |
| 872 | } |
| 873 | |
| 874 | static efi_status_t EFIAPI efi_pxe_base_code_arp( |
| 875 | struct efi_pxe_base_code_protocol *this, |
| 876 | struct efi_ip_address *ip_addr, |
| 877 | struct efi_mac_address *mac_addr) |
| 878 | { |
| 879 | return EFI_UNSUPPORTED; |
| 880 | } |
| 881 | |
| 882 | static efi_status_t EFIAPI efi_pxe_base_code_set_parameters( |
| 883 | struct efi_pxe_base_code_protocol *this, |
| 884 | u8 *new_auto_arp, u8 *new_send_guid, |
| 885 | u8 *new_ttl, u8 *new_tos, |
| 886 | u8 *new_make_callback) |
| 887 | { |
| 888 | return EFI_UNSUPPORTED; |
| 889 | } |
| 890 | |
| 891 | static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip( |
| 892 | struct efi_pxe_base_code_protocol *this, |
| 893 | struct efi_ip_address *new_station_ip, |
| 894 | struct efi_ip_address *new_subnet_mask) |
| 895 | { |
| 896 | return EFI_UNSUPPORTED; |
| 897 | } |
| 898 | |
| 899 | static efi_status_t EFIAPI efi_pxe_base_code_set_packets( |
| 900 | struct efi_pxe_base_code_protocol *this, |
| 901 | u8 *new_dhcp_discover_valid, |
| 902 | u8 *new_dhcp_ack_received, |
| 903 | u8 *new_proxy_offer_received, |
| 904 | u8 *new_pxe_discover_valid, |
| 905 | u8 *new_pxe_reply_received, |
| 906 | u8 *new_pxe_bis_reply_received, |
| 907 | EFI_PXE_BASE_CODE_PACKET *new_dchp_discover, |
| 908 | EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc, |
| 909 | EFI_PXE_BASE_CODE_PACKET *new_proxy_offer, |
| 910 | EFI_PXE_BASE_CODE_PACKET *new_pxe_discover, |
| 911 | EFI_PXE_BASE_CODE_PACKET *new_pxe_reply, |
| 912 | EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply) |
| 913 | { |
| 914 | return EFI_UNSUPPORTED; |
| 915 | } |
| 916 | |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 917 | /** |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 918 | * efi_netobj_set_dp() - set device path of a netobj |
| 919 | * |
| 920 | * @netobj: pointer to efi_net_obj |
| 921 | * @dp: device path to set, allocated by caller |
| 922 | * Return: status code |
| 923 | */ |
| 924 | efi_status_t efi_netobj_set_dp(struct efi_net_obj *netobj, struct efi_device_path *dp) |
| 925 | { |
| 926 | efi_status_t ret; |
| 927 | struct efi_handler *phandler; |
| 928 | struct efi_device_path *new_net_dp; |
| 929 | |
| 930 | if (!efi_netobj_is_active(netobj)) |
| 931 | return EFI_SUCCESS; |
| 932 | |
| 933 | // Create a device path for the netobj |
| 934 | new_net_dp = dp; |
| 935 | if (!new_net_dp) |
| 936 | return EFI_OUT_OF_RESOURCES; |
| 937 | |
| 938 | phandler = NULL; |
| 939 | efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler); |
| 940 | |
| 941 | // If the device path protocol is not yet installed, install it |
| 942 | if (!phandler) |
| 943 | goto add; |
| 944 | |
| 945 | // If it is already installed, try to update it |
| 946 | ret = efi_reinstall_protocol_interface(&netobj->header, &efi_guid_device_path, |
| 947 | phandler->protocol_interface, new_net_dp); |
| 948 | if (ret != EFI_SUCCESS) |
| 949 | return ret; |
| 950 | |
| 951 | return EFI_SUCCESS; |
| 952 | add: |
| 953 | ret = efi_add_protocol(&netobj->header, &efi_guid_device_path, |
| 954 | new_net_dp); |
| 955 | if (ret != EFI_SUCCESS) |
| 956 | return ret; |
| 957 | |
| 958 | return EFI_SUCCESS; |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * efi_netobj_get_dp() - get device path of a netobj |
| 963 | * |
| 964 | * @netobj: pointer to efi_net_obj |
| 965 | * Return: device path, NULL on error |
| 966 | */ |
| 967 | static struct efi_device_path *efi_netobj_get_dp(struct efi_net_obj *netobj) |
| 968 | { |
| 969 | struct efi_handler *phandler; |
| 970 | |
| 971 | if (!efi_netobj_is_active(netobj)) |
| 972 | return NULL; |
| 973 | |
| 974 | phandler = NULL; |
| 975 | efi_search_protocol(&netobj->header, &efi_guid_device_path, &phandler); |
| 976 | |
| 977 | if (phandler && phandler->protocol_interface) |
| 978 | return efi_dp_dup(phandler->protocol_interface); |
| 979 | |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | /** |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 984 | * efi_net_do_start() - start the efi network stack |
| 985 | * |
| 986 | * This gets called from do_bootefi_exec() each time a payload gets executed. |
| 987 | * |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 988 | * @dev: net udevice |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 989 | * Return: status code |
| 990 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 991 | efi_status_t efi_net_do_start(struct udevice *dev) |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 992 | { |
| 993 | efi_status_t r = EFI_SUCCESS; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 994 | struct efi_device_path *net_dp; |
| 995 | |
| 996 | if (dev != netobj->dev ) |
| 997 | return r; |
| 998 | |
| 999 | efi_net_dp_from_dev(&net_dp, netobj->dev, true); |
| 1000 | // If no dp cache entry applies and there already |
| 1001 | // is a device path installed, continue |
| 1002 | if (!net_dp) { |
| 1003 | if (efi_netobj_get_dp(netobj)) |
| 1004 | goto set_addr; |
| 1005 | else |
| 1006 | net_dp = efi_dp_from_eth(netobj->dev); |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1007 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1008 | } |
| 1009 | |
| 1010 | if (!net_dp) |
| 1011 | return EFI_OUT_OF_RESOURCES; |
| 1012 | |
| 1013 | r = efi_netobj_set_dp(netobj, net_dp); |
| 1014 | if (r != EFI_SUCCESS) |
| 1015 | return r; |
| 1016 | set_addr: |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1017 | #ifdef CONFIG_EFI_HTTP_PROTOCOL |
| 1018 | /* |
| 1019 | * No harm on doing the following. If the PXE handle is present, the client could |
| 1020 | * find it and try to get its IP address from it. In here the PXE handle is present |
| 1021 | * but the PXE protocol is not yet implmenented, so we add this in the meantime. |
| 1022 | */ |
| 1023 | efi_net_get_addr((struct efi_ipv4_address *)&netobj->pxe_mode.station_ip, |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1024 | (struct efi_ipv4_address *)&netobj->pxe_mode.subnet_mask, NULL, dev); |
Adriano Cordova | 1d70b68 | 2025-03-03 11:13:13 -0300 | [diff] [blame] | 1025 | #endif |
| 1026 | |
| 1027 | return r; |
| 1028 | } |
| 1029 | |
| 1030 | /** |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 1031 | * efi_net_register() - register the simple network protocol |
| 1032 | * |
| 1033 | * This gets called from do_bootefi_exec(). |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1034 | * @dev: net udevice |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 1035 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1036 | efi_status_t efi_net_register(struct udevice *dev) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1037 | { |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1038 | efi_status_t r; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 1039 | int i; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1040 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1041 | if (!dev) { |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 1042 | /* No network device active, don't expose any */ |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 1043 | return EFI_SUCCESS; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 1046 | /* We only expose the "active" network device, so one is enough */ |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1047 | netobj = calloc(1, sizeof(*netobj)); |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 1048 | if (!netobj) |
| 1049 | goto out_of_resources; |
| 1050 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1051 | netobj->dev = dev; |
| 1052 | |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 1053 | /* Allocate an aligned transmit buffer */ |
| 1054 | transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN); |
| 1055 | if (!transmit_buffer) |
| 1056 | goto out_of_resources; |
| 1057 | transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN); |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 1058 | |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 1059 | /* Allocate a number of receive buffers */ |
| 1060 | receive_buffer = calloc(ETH_PACKETS_BATCH_RECV, |
| 1061 | sizeof(*receive_buffer)); |
| 1062 | if (!receive_buffer) |
| 1063 | goto out_of_resources; |
| 1064 | for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) { |
| 1065 | receive_buffer[i] = malloc(PKTSIZE_ALIGN); |
| 1066 | if (!receive_buffer[i]) |
| 1067 | goto out_of_resources; |
| 1068 | } |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1069 | |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 1070 | receive_lengths = calloc(ETH_PACKETS_BATCH_RECV, |
| 1071 | sizeof(*receive_lengths)); |
| 1072 | if (!receive_lengths) |
| 1073 | goto out_of_resources; |
| 1074 | |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 1075 | /* Hook net up to the device list */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1076 | efi_add_handle(&netobj->header); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1077 | |
| 1078 | /* Fill in object data */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1079 | r = efi_add_protocol(&netobj->header, &efi_net_guid, |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 1080 | &netobj->net); |
| 1081 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 1082 | goto failure_to_add_protocol; |
Adriano Cordova | 1738c7d | 2025-01-27 09:34:45 -0300 | [diff] [blame] | 1083 | |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 1084 | r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid, |
Heinrich Schuchardt | 23b45b6 | 2017-11-26 14:05:13 +0100 | [diff] [blame] | 1085 | &netobj->pxe); |
| 1086 | if (r != EFI_SUCCESS) |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 1087 | goto failure_to_add_protocol; |
Heinrich Schuchardt | d54396a | 2017-10-05 16:35:57 +0200 | [diff] [blame] | 1088 | netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1089 | netobj->net.start = efi_net_start; |
| 1090 | netobj->net.stop = efi_net_stop; |
| 1091 | netobj->net.initialize = efi_net_initialize; |
| 1092 | netobj->net.reset = efi_net_reset; |
| 1093 | netobj->net.shutdown = efi_net_shutdown; |
| 1094 | netobj->net.receive_filters = efi_net_receive_filters; |
| 1095 | netobj->net.station_address = efi_net_station_address; |
| 1096 | netobj->net.statistics = efi_net_statistics; |
| 1097 | netobj->net.mcastiptomac = efi_net_mcastiptomac; |
| 1098 | netobj->net.nvdata = efi_net_nvdata; |
| 1099 | netobj->net.get_status = efi_net_get_status; |
| 1100 | netobj->net.transmit = efi_net_transmit; |
| 1101 | netobj->net.receive = efi_net_receive; |
| 1102 | netobj->net.mode = &netobj->net_mode; |
Heinrich Schuchardt | 6a6e7d8 | 2019-09-01 15:24:47 +0200 | [diff] [blame] | 1103 | netobj->net_mode.state = EFI_NETWORK_STOPPED; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1104 | if (dev_get_plat(dev)) |
| 1105 | memcpy(netobj->net_mode.current_address.mac_addr, |
| 1106 | ((struct eth_pdata *)dev_get_plat(dev))->enetaddr, 6); |
Heinrich Schuchardt | d1cc6cb | 2017-10-05 16:35:58 +0200 | [diff] [blame] | 1107 | netobj->net_mode.hwaddr_size = ARP_HLEN; |
Heinrich Schuchardt | 0218a8f | 2019-08-31 10:55:29 +0200 | [diff] [blame] | 1108 | netobj->net_mode.media_header_size = ETHER_HDR_SIZE; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1109 | netobj->net_mode.max_packet_size = PKTSIZE; |
Andrew Thomas | 27df0a7 | 2018-06-21 16:21:01 -0700 | [diff] [blame] | 1110 | netobj->net_mode.if_type = ARP_ETHER; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1111 | |
Heinrich Schuchardt | 3127e7e | 2019-08-06 08:13:33 +0200 | [diff] [blame] | 1112 | netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION; |
| 1113 | netobj->pxe.start = efi_pxe_base_code_start; |
| 1114 | netobj->pxe.stop = efi_pxe_base_code_stop; |
| 1115 | netobj->pxe.dhcp = efi_pxe_base_code_dhcp; |
| 1116 | netobj->pxe.discover = efi_pxe_base_code_discover; |
| 1117 | netobj->pxe.mtftp = efi_pxe_base_code_mtftp; |
| 1118 | netobj->pxe.udp_write = efi_pxe_base_code_udp_write; |
| 1119 | netobj->pxe.udp_read = efi_pxe_base_code_udp_read; |
| 1120 | netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter; |
| 1121 | netobj->pxe.arp = efi_pxe_base_code_arp; |
| 1122 | netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters; |
| 1123 | netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip; |
| 1124 | netobj->pxe.set_packets = efi_pxe_base_code_set_packets; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1125 | netobj->pxe.mode = &netobj->pxe_mode; |
| 1126 | if (dhcp_ack) |
| 1127 | netobj->pxe_mode.dhcp_ack = *dhcp_ack; |
| 1128 | |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1129 | /* |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 1130 | * Create WaitForPacket event. |
| 1131 | */ |
| 1132 | r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1133 | efi_network_timer_notify, NULL, NULL, |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 1134 | &wait_for_packet); |
| 1135 | if (r != EFI_SUCCESS) { |
| 1136 | printf("ERROR: Failed to register network event\n"); |
| 1137 | return r; |
| 1138 | } |
| 1139 | netobj->net.wait_for_packet = wait_for_packet; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1140 | |
Heinrich Schuchardt | 0c153c2 | 2017-10-05 16:36:01 +0200 | [diff] [blame] | 1141 | /* |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1142 | * Create a timer event. |
| 1143 | * |
| 1144 | * The notification function is used to check if a new network packet |
| 1145 | * has been received. |
Heinrich Schuchardt | 86df48c | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 1146 | * |
| 1147 | * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL. |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1148 | */ |
Heinrich Schuchardt | 86df48c | 2018-03-24 18:40:22 +0100 | [diff] [blame] | 1149 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY, |
Heinrich Schuchardt | b6f2036 | 2018-12-01 00:16:33 +0100 | [diff] [blame] | 1150 | efi_network_timer_notify, &netobj->net, NULL, |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1151 | &network_timer_event); |
| 1152 | if (r != EFI_SUCCESS) { |
| 1153 | printf("ERROR: Failed to register network event\n"); |
| 1154 | return r; |
| 1155 | } |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 1156 | /* Network is time critical, create event in every timer cycle */ |
Heinrich Schuchardt | 3d7fc69 | 2017-10-05 16:36:00 +0200 | [diff] [blame] | 1157 | r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0); |
| 1158 | if (r != EFI_SUCCESS) { |
| 1159 | printf("ERROR: Failed to set network timer\n"); |
| 1160 | return r; |
| 1161 | } |
| 1162 | |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 1163 | #if IS_ENABLED(CONFIG_EFI_IP4_CONFIG2_PROTOCOL) |
| 1164 | r = efi_ipconfig_register(&netobj->header, &netobj->ip4_config2); |
| 1165 | if (r != EFI_SUCCESS) |
| 1166 | goto failure_to_add_protocol; |
Adriano Cordova | e9b19eb | 2024-12-04 00:05:26 -0300 | [diff] [blame] | 1167 | #endif |
| 1168 | |
| 1169 | #ifdef CONFIG_EFI_HTTP_PROTOCOL |
| 1170 | r = efi_http_register(&netobj->header, &netobj->http_service_binding); |
| 1171 | if (r != EFI_SUCCESS) |
| 1172 | goto failure_to_add_protocol; |
Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame] | 1173 | #endif |
Heinrich Schuchardt | 6f5c73f | 2018-03-03 15:28:56 +0100 | [diff] [blame] | 1174 | return EFI_SUCCESS; |
| 1175 | failure_to_add_protocol: |
| 1176 | printf("ERROR: Failure to add protocol\n"); |
| 1177 | return r; |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 1178 | out_of_resources: |
| 1179 | free(netobj); |
Heinrich Schuchardt | 2f1a93f | 2022-11-26 16:44:38 +0100 | [diff] [blame] | 1180 | netobj = NULL; |
Patrick Wildt | fab8910 | 2020-10-07 11:04:33 +0200 | [diff] [blame] | 1181 | free(transmit_buffer); |
| 1182 | if (receive_buffer) |
| 1183 | for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) |
| 1184 | free(receive_buffer[i]); |
| 1185 | free(receive_buffer); |
| 1186 | free(receive_lengths); |
Heinrich Schuchardt | eb07c28 | 2018-12-01 00:16:32 +0100 | [diff] [blame] | 1187 | printf("ERROR: Out of memory\n"); |
| 1188 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1189 | } |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1190 | |
| 1191 | /** |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1192 | * efi_net_new_dp() - update device path associated to a net udevice |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1193 | * |
| 1194 | * This gets called to update the device path when a new boot |
| 1195 | * file is downloaded |
| 1196 | * |
| 1197 | * @dev: dev to set the device path from |
| 1198 | * @server: remote server address |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1199 | * @udev: net udevice |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1200 | * Return: status code |
| 1201 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1202 | efi_status_t efi_net_new_dp(const char *dev, const char *server, struct udevice *udev) |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1203 | { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1204 | efi_status_t ret; |
| 1205 | struct efi_device_path *old_net_dp, *new_net_dp; |
| 1206 | struct efi_device_path **dp; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1207 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1208 | dp = &dp_cache[next_dp_entry].net_dp; |
| 1209 | |
| 1210 | dp_cache[next_dp_entry].dev = udev; |
| 1211 | dp_cache[next_dp_entry].is_valid = true; |
| 1212 | next_dp_entry++; |
| 1213 | next_dp_entry %= MAX_NUM_DP_ENTRIES; |
| 1214 | |
| 1215 | old_net_dp = *dp; |
| 1216 | new_net_dp = NULL; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1217 | if (!strcmp(dev, "Net")) |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1218 | new_net_dp = efi_dp_from_eth(udev); |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1219 | else if (!strcmp(dev, "Http")) |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1220 | new_net_dp = efi_dp_from_http(server, udev); |
| 1221 | if (!new_net_dp) |
| 1222 | return EFI_OUT_OF_RESOURCES; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1223 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1224 | *dp = new_net_dp; |
| 1225 | // Free the old cache entry |
| 1226 | efi_free_pool(old_net_dp); |
| 1227 | |
| 1228 | if (!netobj || netobj->dev != udev) |
| 1229 | return EFI_SUCCESS; |
| 1230 | |
| 1231 | new_net_dp = efi_dp_dup(*dp); |
| 1232 | if (!new_net_dp) |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1233 | return EFI_OUT_OF_RESOURCES; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1234 | ret = efi_netobj_set_dp(netobj, new_net_dp); |
| 1235 | if (ret != EFI_SUCCESS) |
| 1236 | efi_free_pool(new_net_dp); |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1237 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1238 | return ret; |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /** |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1242 | * efi_net_dp_from_dev() - get device path associated to a net udevice |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1243 | * |
| 1244 | * Produce a copy of the current device path |
| 1245 | * |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1246 | * @dp: copy of the current device path |
| 1247 | * @udev: net udevice |
| 1248 | * @cache_only: get device path from cache only |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1249 | */ |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1250 | void efi_net_dp_from_dev(struct efi_device_path **dp, struct udevice *udev, bool cache_only) |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1251 | { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1252 | int i, j; |
| 1253 | |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1254 | if (!dp) |
| 1255 | return; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1256 | |
| 1257 | *dp = NULL; |
| 1258 | |
| 1259 | if (cache_only) |
| 1260 | goto cache; |
| 1261 | |
| 1262 | if (netobj && netobj->dev == udev) { |
| 1263 | *dp = efi_netobj_get_dp(netobj); |
| 1264 | if (*dp) |
| 1265 | return; |
| 1266 | } |
| 1267 | cache: |
| 1268 | // Search in the cache |
| 1269 | i = (next_dp_entry + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES; |
| 1270 | for (j = 0; dp_cache[i].is_valid && j < MAX_NUM_DP_ENTRIES; |
| 1271 | i = (i + MAX_NUM_DP_ENTRIES - 1) % MAX_NUM_DP_ENTRIES, j++) { |
| 1272 | if (dp_cache[i].dev == udev) { |
| 1273 | *dp = efi_dp_dup(dp_cache[i].net_dp); |
| 1274 | return; |
| 1275 | } |
| 1276 | } |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | /** |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1280 | * efi_net_get_addr() - get IP address information |
| 1281 | * |
| 1282 | * Copy the current IP address, mask, and gateway into the |
| 1283 | * efi_ipv4_address structs pointed to by ip, mask and gw, |
| 1284 | * respectively. |
| 1285 | * |
| 1286 | * @ip: pointer to an efi_ipv4_address struct to |
| 1287 | * be filled with the current IP address |
| 1288 | * @mask: pointer to an efi_ipv4_address struct to |
| 1289 | * be filled with the current network mask |
| 1290 | * @gw: pointer to an efi_ipv4_address struct to be |
| 1291 | * filled with the current network gateway |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1292 | * @dev: udevice |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1293 | */ |
| 1294 | void efi_net_get_addr(struct efi_ipv4_address *ip, |
| 1295 | struct efi_ipv4_address *mask, |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1296 | struct efi_ipv4_address *gw, |
| 1297 | struct udevice *dev) |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1298 | { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1299 | if (!dev) |
| 1300 | dev = eth_get_dev(); |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1301 | #ifdef CONFIG_NET_LWIP |
| 1302 | char ipstr[] = "ipaddr\0\0"; |
| 1303 | char maskstr[] = "netmask\0\0"; |
| 1304 | char gwstr[] = "gatewayip\0\0"; |
| 1305 | int idx; |
| 1306 | struct in_addr tmp; |
| 1307 | char *env; |
| 1308 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1309 | idx = dev_seq(dev); |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1310 | |
| 1311 | if (idx < 0 || idx > 99) { |
| 1312 | log_err("unexpected idx %d\n", idx); |
| 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | if (idx) { |
| 1317 | sprintf(ipstr, "ipaddr%d", idx); |
| 1318 | sprintf(maskstr, "netmask%d", idx); |
| 1319 | sprintf(gwstr, "gatewayip%d", idx); |
| 1320 | } |
| 1321 | |
| 1322 | env = env_get(ipstr); |
| 1323 | if (env && ip) { |
| 1324 | tmp = string_to_ip(env); |
| 1325 | memcpy(ip, &tmp, sizeof(tmp)); |
| 1326 | } |
| 1327 | |
| 1328 | env = env_get(maskstr); |
| 1329 | if (env && mask) { |
| 1330 | tmp = string_to_ip(env); |
| 1331 | memcpy(mask, &tmp, sizeof(tmp)); |
| 1332 | } |
| 1333 | env = env_get(gwstr); |
| 1334 | if (env && gw) { |
| 1335 | tmp = string_to_ip(env); |
| 1336 | memcpy(gw, &tmp, sizeof(tmp)); |
| 1337 | } |
| 1338 | #else |
| 1339 | if (ip) |
| 1340 | memcpy(ip, &net_ip, sizeof(net_ip)); |
| 1341 | if (mask) |
| 1342 | memcpy(mask, &net_netmask, sizeof(net_netmask)); |
| 1343 | #endif |
| 1344 | } |
| 1345 | |
| 1346 | /** |
| 1347 | * efi_net_set_addr() - set IP address information |
| 1348 | * |
| 1349 | * Set the current IP address, mask, and gateway to the |
| 1350 | * efi_ipv4_address structs pointed to by ip, mask and gw, |
| 1351 | * respectively. |
| 1352 | * |
| 1353 | * @ip: pointer to new IP address |
| 1354 | * @mask: pointer to new network mask to set |
| 1355 | * @gw: pointer to new network gateway |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1356 | * @dev: udevice |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1357 | */ |
| 1358 | void efi_net_set_addr(struct efi_ipv4_address *ip, |
| 1359 | struct efi_ipv4_address *mask, |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1360 | struct efi_ipv4_address *gw, |
| 1361 | struct udevice *dev) |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1362 | { |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1363 | if (!dev) |
| 1364 | dev = eth_get_dev(); |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1365 | #ifdef CONFIG_NET_LWIP |
| 1366 | char ipstr[] = "ipaddr\0\0"; |
| 1367 | char maskstr[] = "netmask\0\0"; |
| 1368 | char gwstr[] = "gatewayip\0\0"; |
| 1369 | int idx; |
| 1370 | struct in_addr *addr; |
| 1371 | char tmp[46]; |
| 1372 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1373 | idx = dev_seq(dev); |
Adriano Cordova | 3c95136 | 2024-12-04 00:05:21 -0300 | [diff] [blame] | 1374 | |
| 1375 | if (idx < 0 || idx > 99) { |
| 1376 | log_err("unexpected idx %d\n", idx); |
| 1377 | return; |
| 1378 | } |
| 1379 | |
| 1380 | if (idx) { |
| 1381 | sprintf(ipstr, "ipaddr%d", idx); |
| 1382 | sprintf(maskstr, "netmask%d", idx); |
| 1383 | sprintf(gwstr, "gatewayip%d", idx); |
| 1384 | } |
| 1385 | |
| 1386 | if (ip) { |
| 1387 | addr = (struct in_addr *)ip; |
| 1388 | ip_to_string(*addr, tmp); |
| 1389 | env_set(ipstr, tmp); |
| 1390 | } |
| 1391 | |
| 1392 | if (mask) { |
| 1393 | addr = (struct in_addr *)mask; |
| 1394 | ip_to_string(*addr, tmp); |
| 1395 | env_set(maskstr, tmp); |
| 1396 | } |
| 1397 | |
| 1398 | if (gw) { |
| 1399 | addr = (struct in_addr *)gw; |
| 1400 | ip_to_string(*addr, tmp); |
| 1401 | env_set(gwstr, tmp); |
| 1402 | } |
| 1403 | #else |
| 1404 | if (ip) |
| 1405 | memcpy(&net_ip, ip, sizeof(*ip)); |
| 1406 | if (mask) |
| 1407 | memcpy(&net_netmask, mask, sizeof(*mask)); |
| 1408 | #endif |
| 1409 | } |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1410 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1411 | #if IS_ENABLED(CONFIG_EFI_HTTP_PROTOCOL) |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1412 | /** |
| 1413 | * efi_net_set_buffer() - allocate a buffer of min 64K |
| 1414 | * |
| 1415 | * @buffer: allocated buffer |
| 1416 | * @size: desired buffer size |
| 1417 | * Return: status code |
| 1418 | */ |
| 1419 | static efi_status_t efi_net_set_buffer(void **buffer, size_t size) |
| 1420 | { |
| 1421 | efi_status_t ret = EFI_SUCCESS; |
| 1422 | |
| 1423 | if (size < SZ_64K) |
| 1424 | size = SZ_64K; |
| 1425 | |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1426 | *buffer = efi_alloc(size); |
| 1427 | if (!*buffer) |
| 1428 | ret = EFI_OUT_OF_RESOURCES; |
| 1429 | |
| 1430 | efi_wget_info.buffer_size = (ulong)size; |
| 1431 | |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | /** |
| 1436 | * efi_net_parse_headers() - parse HTTP headers |
| 1437 | * |
| 1438 | * Parses the raw buffer efi_wget_info.headers into an array headers |
| 1439 | * of efi structs http_headers. The array should be at least |
| 1440 | * MAX_HTTP_HEADERS long. |
| 1441 | * |
| 1442 | * @num_headers: number of headers |
| 1443 | * @headers: caller provided array of struct http_headers |
| 1444 | */ |
| 1445 | void efi_net_parse_headers(ulong *num_headers, struct http_header *headers) |
| 1446 | { |
| 1447 | if (!num_headers || !headers) |
| 1448 | return; |
| 1449 | |
| 1450 | // Populate info with http headers. |
| 1451 | *num_headers = 0; |
| 1452 | const uchar *line_start = efi_wget_info.headers; |
| 1453 | const uchar *line_end; |
| 1454 | ulong count; |
| 1455 | struct http_header *current_header; |
| 1456 | const uchar *separator; |
| 1457 | size_t name_length, value_length; |
| 1458 | |
| 1459 | // Skip the first line (request or status line) |
| 1460 | line_end = strstr(line_start, "\r\n"); |
| 1461 | |
| 1462 | if (line_end) |
| 1463 | line_start = line_end + 2; |
| 1464 | |
| 1465 | while ((line_end = strstr(line_start, "\r\n")) != NULL) { |
| 1466 | count = *num_headers; |
| 1467 | if (line_start == line_end || count >= MAX_HTTP_HEADERS) |
| 1468 | break; |
| 1469 | current_header = headers + count; |
| 1470 | separator = strchr(line_start, ':'); |
| 1471 | if (separator) { |
| 1472 | name_length = separator - line_start; |
| 1473 | ++separator; |
| 1474 | while (*separator == ' ') |
| 1475 | ++separator; |
| 1476 | value_length = line_end - separator; |
| 1477 | if (name_length < MAX_HTTP_HEADER_NAME && |
| 1478 | value_length < MAX_HTTP_HEADER_VALUE) { |
| 1479 | strncpy(current_header->name, line_start, name_length); |
| 1480 | current_header->name[name_length] = '\0'; |
| 1481 | strncpy(current_header->value, separator, value_length); |
| 1482 | current_header->value[value_length] = '\0'; |
| 1483 | (*num_headers)++; |
| 1484 | } |
| 1485 | } |
| 1486 | line_start = line_end + 2; |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * efi_net_do_request() - issue an HTTP request using wget |
| 1492 | * |
| 1493 | * @url: url |
| 1494 | * @method: HTTP method |
| 1495 | * @buffer: data buffer |
| 1496 | * @status_code: HTTP status code |
| 1497 | * @file_size: file size in bytes |
| 1498 | * @headers_buffer: headers buffer |
| 1499 | * Return: status code |
| 1500 | */ |
| 1501 | efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, void **buffer, |
| 1502 | u32 *status_code, ulong *file_size, char *headers_buffer) |
| 1503 | { |
| 1504 | efi_status_t ret = EFI_SUCCESS; |
| 1505 | int wget_ret; |
| 1506 | static bool last_head; |
| 1507 | |
| 1508 | if (!buffer || !file_size) |
| 1509 | return EFI_ABORTED; |
| 1510 | |
| 1511 | efi_wget_info.method = (enum wget_http_method)method; |
| 1512 | efi_wget_info.headers = headers_buffer; |
| 1513 | |
| 1514 | switch (method) { |
| 1515 | case HTTP_METHOD_GET: |
| 1516 | ret = efi_net_set_buffer(buffer, last_head ? (size_t)efi_wget_info.hdr_cont_len : 0); |
| 1517 | if (ret != EFI_SUCCESS) |
| 1518 | goto out; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1519 | eth_set_dev(netobj->dev); |
| 1520 | env_set("ethact", eth_get_name()); |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1521 | wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info); |
| 1522 | if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) { |
| 1523 | // Try again with updated buffer size |
Adriano Cordova | e9b19eb | 2024-12-04 00:05:26 -0300 | [diff] [blame] | 1524 | efi_free_pool(*buffer); |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1525 | ret = efi_net_set_buffer(buffer, (size_t)efi_wget_info.hdr_cont_len); |
| 1526 | if (ret != EFI_SUCCESS) |
| 1527 | goto out; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1528 | eth_set_dev(netobj->dev); |
| 1529 | env_set("ethact", eth_get_name()); |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1530 | if (wget_request((ulong)*buffer, url, &efi_wget_info)) { |
| 1531 | efi_free_pool(*buffer); |
| 1532 | ret = EFI_DEVICE_ERROR; |
| 1533 | goto out; |
| 1534 | } |
| 1535 | } else if (wget_ret) { |
| 1536 | efi_free_pool(*buffer); |
| 1537 | ret = EFI_DEVICE_ERROR; |
| 1538 | goto out; |
| 1539 | } |
| 1540 | // Pass the actual number of received bytes to the application |
| 1541 | *file_size = efi_wget_info.file_size; |
| 1542 | *status_code = efi_wget_info.status_code; |
| 1543 | last_head = false; |
| 1544 | break; |
| 1545 | case HTTP_METHOD_HEAD: |
| 1546 | ret = efi_net_set_buffer(buffer, 0); |
| 1547 | if (ret != EFI_SUCCESS) |
| 1548 | goto out; |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1549 | eth_set_dev(netobj->dev); |
| 1550 | env_set("ethact", eth_get_name()); |
Adriano Cordova | 0d1f509 | 2024-12-04 00:05:24 -0300 | [diff] [blame] | 1551 | wget_request((ulong)*buffer, url, &efi_wget_info); |
| 1552 | *file_size = 0; |
| 1553 | *status_code = efi_wget_info.status_code; |
| 1554 | last_head = true; |
| 1555 | break; |
| 1556 | default: |
| 1557 | ret = EFI_UNSUPPORTED; |
| 1558 | break; |
| 1559 | } |
| 1560 | |
| 1561 | out: |
| 1562 | return ret; |
| 1563 | } |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame^] | 1564 | #endif |