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