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