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