Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * EFI application network access support |
| 3 | * |
| 4 | * Copyright (c) 2016 Alexander Graf |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <efi_loader.h> |
| 11 | #include <inttypes.h> |
| 12 | #include <lcd.h> |
| 13 | #include <malloc.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID; |
| 18 | static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID; |
| 19 | static struct efi_pxe_packet *dhcp_ack; |
| 20 | static bool new_rx_packet; |
| 21 | static void *new_tx_packet; |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame^] | 22 | /* |
| 23 | * The notification function of this event is called in every timer cycle |
| 24 | * to check if a new network packet has been received. |
| 25 | */ |
| 26 | static struct efi_event *network_timer_event; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 27 | |
| 28 | struct efi_net_obj { |
| 29 | /* Generic EFI object parent class data */ |
| 30 | struct efi_object parent; |
| 31 | /* EFI Interface callback struct for network */ |
| 32 | struct efi_simple_network net; |
| 33 | struct efi_simple_network_mode net_mode; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 34 | /* PXE struct to transmit dhcp data */ |
| 35 | struct efi_pxe pxe; |
| 36 | struct efi_pxe_mode pxe_mode; |
| 37 | }; |
| 38 | |
| 39 | static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this) |
| 40 | { |
| 41 | EFI_ENTRY("%p", this); |
| 42 | |
| 43 | return EFI_EXIT(EFI_SUCCESS); |
| 44 | } |
| 45 | |
| 46 | static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this) |
| 47 | { |
| 48 | EFI_ENTRY("%p", this); |
| 49 | |
| 50 | return EFI_EXIT(EFI_SUCCESS); |
| 51 | } |
| 52 | |
| 53 | static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this, |
| 54 | ulong extra_rx, ulong extra_tx) |
| 55 | { |
| 56 | EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx); |
| 57 | |
| 58 | eth_init(); |
| 59 | |
| 60 | return EFI_EXIT(EFI_SUCCESS); |
| 61 | } |
| 62 | |
| 63 | static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this, |
| 64 | int extended_verification) |
| 65 | { |
| 66 | EFI_ENTRY("%p, %x", this, extended_verification); |
| 67 | |
| 68 | return EFI_EXIT(EFI_SUCCESS); |
| 69 | } |
| 70 | |
| 71 | static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this) |
| 72 | { |
| 73 | EFI_ENTRY("%p", this); |
| 74 | |
| 75 | return EFI_EXIT(EFI_SUCCESS); |
| 76 | } |
| 77 | |
| 78 | static efi_status_t EFIAPI efi_net_receive_filters( |
| 79 | struct efi_simple_network *this, u32 enable, u32 disable, |
| 80 | int reset_mcast_filter, ulong mcast_filter_count, |
| 81 | struct efi_mac_address *mcast_filter) |
| 82 | { |
| 83 | EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable, |
| 84 | reset_mcast_filter, mcast_filter_count, mcast_filter); |
| 85 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 86 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static efi_status_t EFIAPI efi_net_station_address( |
| 90 | struct efi_simple_network *this, int reset, |
| 91 | struct efi_mac_address *new_mac) |
| 92 | { |
| 93 | EFI_ENTRY("%p, %x, %p", this, reset, new_mac); |
| 94 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 95 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this, |
| 99 | int reset, ulong *stat_size, |
| 100 | void *stat_table) |
| 101 | { |
| 102 | EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table); |
| 103 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 104 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this, |
| 108 | int ipv6, |
| 109 | struct efi_ip_address *ip, |
| 110 | struct efi_mac_address *mac) |
| 111 | { |
| 112 | EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac); |
| 113 | |
| 114 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 115 | } |
| 116 | |
| 117 | static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this, |
| 118 | int read_write, ulong offset, |
| 119 | ulong buffer_size, char *buffer) |
| 120 | { |
| 121 | EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size, |
| 122 | buffer); |
| 123 | |
Heinrich Schuchardt | 61da678 | 2017-10-05 16:35:59 +0200 | [diff] [blame] | 124 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this, |
| 128 | u32 *int_status, void **txbuf) |
| 129 | { |
| 130 | EFI_ENTRY("%p, %p, %p", this, int_status, txbuf); |
| 131 | |
| 132 | /* We send packets synchronously, so nothing is outstanding */ |
| 133 | if (int_status) |
| 134 | *int_status = 0; |
| 135 | if (txbuf) |
| 136 | *txbuf = new_tx_packet; |
| 137 | |
| 138 | new_tx_packet = NULL; |
| 139 | |
| 140 | return EFI_EXIT(EFI_SUCCESS); |
| 141 | } |
| 142 | |
| 143 | static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this, |
| 144 | ulong header_size, ulong buffer_size, void *buffer, |
| 145 | struct efi_mac_address *src_addr, |
| 146 | struct efi_mac_address *dest_addr, u16 *protocol) |
| 147 | { |
| 148 | EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size, |
| 149 | buffer_size, buffer, src_addr, dest_addr, protocol); |
| 150 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame^] | 151 | efi_timer_check(); |
| 152 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 153 | if (header_size) { |
| 154 | /* We would need to create the header if header_size != 0 */ |
| 155 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 156 | } |
| 157 | |
Alexander Graf | 712cd29 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 158 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 159 | /* Ethernet packets always fit, just bounce */ |
| 160 | memcpy(efi_bounce_buffer, buffer, buffer_size); |
| 161 | net_send_packet(efi_bounce_buffer, buffer_size); |
| 162 | #else |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 163 | net_send_packet(buffer, buffer_size); |
Alexander Graf | 712cd29 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 164 | #endif |
| 165 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 166 | new_tx_packet = buffer; |
| 167 | |
| 168 | return EFI_EXIT(EFI_SUCCESS); |
| 169 | } |
| 170 | |
| 171 | static void efi_net_push(void *pkt, int len) |
| 172 | { |
| 173 | new_rx_packet = true; |
| 174 | } |
| 175 | |
| 176 | static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this, |
| 177 | ulong *header_size, ulong *buffer_size, void *buffer, |
| 178 | struct efi_mac_address *src_addr, |
| 179 | struct efi_mac_address *dest_addr, u16 *protocol) |
| 180 | { |
| 181 | EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size, |
| 182 | buffer_size, buffer, src_addr, dest_addr, protocol); |
| 183 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame^] | 184 | efi_timer_check(); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 185 | |
| 186 | if (!new_rx_packet) |
| 187 | return EFI_EXIT(EFI_NOT_READY); |
| 188 | |
| 189 | if (*buffer_size < net_rx_packet_len) { |
| 190 | /* Packet doesn't fit, try again with bigger buf */ |
| 191 | *buffer_size = net_rx_packet_len; |
| 192 | return EFI_EXIT(EFI_BUFFER_TOO_SMALL); |
| 193 | } |
| 194 | |
| 195 | memcpy(buffer, net_rx_packet, net_rx_packet_len); |
| 196 | *buffer_size = net_rx_packet_len; |
| 197 | new_rx_packet = false; |
| 198 | |
| 199 | return EFI_EXIT(EFI_SUCCESS); |
| 200 | } |
| 201 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 202 | void efi_net_set_dhcp_ack(void *pkt, int len) |
| 203 | { |
| 204 | int maxsize = sizeof(*dhcp_ack); |
| 205 | |
| 206 | if (!dhcp_ack) |
| 207 | dhcp_ack = malloc(maxsize); |
| 208 | |
| 209 | memcpy(dhcp_ack, pkt, min(len, maxsize)); |
| 210 | } |
| 211 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame^] | 212 | /* |
| 213 | * Check if a new network packet has been received. |
| 214 | * |
| 215 | * This notification function is called in every timer cycle. |
| 216 | * |
| 217 | * @event the event for which this notification function is registered |
| 218 | * @context event context - not used in this function |
| 219 | */ |
| 220 | static void EFIAPI efi_network_timer_notify(struct efi_event *event, |
| 221 | void *context) |
| 222 | { |
| 223 | EFI_ENTRY("%p, %p", event, context); |
| 224 | |
| 225 | if (!new_rx_packet) { |
| 226 | push_packet = efi_net_push; |
| 227 | eth_rx(); |
| 228 | push_packet = NULL; |
| 229 | } |
| 230 | EFI_EXIT(EFI_SUCCESS); |
| 231 | } |
| 232 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 233 | /* This gets called from do_bootefi_exec(). */ |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 234 | int efi_net_register(void) |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 235 | { |
| 236 | struct efi_net_obj *netobj; |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame^] | 237 | efi_status_t r; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 238 | |
| 239 | if (!eth_get_dev()) { |
| 240 | /* No eth device active, don't expose any */ |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* We only expose the "active" eth device, so one is enough */ |
| 245 | netobj = calloc(1, sizeof(*netobj)); |
| 246 | |
| 247 | /* Fill in object data */ |
| 248 | netobj->parent.protocols[0].guid = &efi_net_guid; |
xypron.glpk@gmx.de | b5349f7 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 249 | netobj->parent.protocols[0].protocol_interface = &netobj->net; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 250 | netobj->parent.protocols[1].guid = &efi_guid_device_path; |
Rob Clark | e15fc33 | 2017-09-13 18:05:32 -0400 | [diff] [blame] | 251 | netobj->parent.protocols[1].protocol_interface = |
| 252 | efi_dp_from_eth(); |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 253 | netobj->parent.protocols[2].guid = &efi_pxe_guid; |
xypron.glpk@gmx.de | b5349f7 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 254 | netobj->parent.protocols[2].protocol_interface = &netobj->pxe; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 255 | netobj->parent.handle = &netobj->net; |
Heinrich Schuchardt | bdecf97 | 2017-10-05 16:35:57 +0200 | [diff] [blame] | 256 | netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 257 | netobj->net.start = efi_net_start; |
| 258 | netobj->net.stop = efi_net_stop; |
| 259 | netobj->net.initialize = efi_net_initialize; |
| 260 | netobj->net.reset = efi_net_reset; |
| 261 | netobj->net.shutdown = efi_net_shutdown; |
| 262 | netobj->net.receive_filters = efi_net_receive_filters; |
| 263 | netobj->net.station_address = efi_net_station_address; |
| 264 | netobj->net.statistics = efi_net_statistics; |
| 265 | netobj->net.mcastiptomac = efi_net_mcastiptomac; |
| 266 | netobj->net.nvdata = efi_net_nvdata; |
| 267 | netobj->net.get_status = efi_net_get_status; |
| 268 | netobj->net.transmit = efi_net_transmit; |
| 269 | netobj->net.receive = efi_net_receive; |
| 270 | netobj->net.mode = &netobj->net_mode; |
| 271 | netobj->net_mode.state = EFI_NETWORK_STARTED; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 272 | memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6); |
Heinrich Schuchardt | 5d4a5ea | 2017-10-05 16:35:58 +0200 | [diff] [blame] | 273 | netobj->net_mode.hwaddr_size = ARP_HLEN; |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 274 | netobj->net_mode.max_packet_size = PKTSIZE; |
| 275 | |
| 276 | netobj->pxe.mode = &netobj->pxe_mode; |
| 277 | if (dhcp_ack) |
| 278 | netobj->pxe_mode.dhcp_ack = *dhcp_ack; |
| 279 | |
| 280 | /* Hook net up to the device list */ |
| 281 | list_add_tail(&netobj->parent.link, &efi_obj_list); |
| 282 | |
Heinrich Schuchardt | a0549ef | 2017-10-05 16:36:00 +0200 | [diff] [blame^] | 283 | /* |
| 284 | * Create a timer event. |
| 285 | * |
| 286 | * The notification function is used to check if a new network packet |
| 287 | * has been received. |
| 288 | */ |
| 289 | r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, |
| 290 | efi_network_timer_notify, NULL, |
| 291 | &network_timer_event); |
| 292 | if (r != EFI_SUCCESS) { |
| 293 | printf("ERROR: Failed to register network event\n"); |
| 294 | return r; |
| 295 | } |
| 296 | /* Network is time critical, create event in every timer cyle */ |
| 297 | r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0); |
| 298 | if (r != EFI_SUCCESS) { |
| 299 | printf("ERROR: Failed to set network timer\n"); |
| 300 | return r; |
| 301 | } |
| 302 | |
Alexander Graf | 0efe1bc | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 303 | return 0; |
| 304 | } |