Alexander Graf | 94c4b99 | 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; |
| 22 | |
| 23 | struct efi_net_obj { |
| 24 | /* Generic EFI object parent class data */ |
| 25 | struct efi_object parent; |
| 26 | /* EFI Interface callback struct for network */ |
| 27 | struct efi_simple_network net; |
| 28 | struct efi_simple_network_mode net_mode; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 29 | /* PXE struct to transmit dhcp data */ |
| 30 | struct efi_pxe pxe; |
| 31 | struct efi_pxe_mode pxe_mode; |
| 32 | }; |
| 33 | |
| 34 | static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this) |
| 35 | { |
| 36 | EFI_ENTRY("%p", this); |
| 37 | |
| 38 | return EFI_EXIT(EFI_SUCCESS); |
| 39 | } |
| 40 | |
| 41 | static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this) |
| 42 | { |
| 43 | EFI_ENTRY("%p", this); |
| 44 | |
| 45 | return EFI_EXIT(EFI_SUCCESS); |
| 46 | } |
| 47 | |
| 48 | static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this, |
| 49 | ulong extra_rx, ulong extra_tx) |
| 50 | { |
| 51 | EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx); |
| 52 | |
| 53 | eth_init(); |
| 54 | |
| 55 | return EFI_EXIT(EFI_SUCCESS); |
| 56 | } |
| 57 | |
| 58 | static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this, |
| 59 | int extended_verification) |
| 60 | { |
| 61 | EFI_ENTRY("%p, %x", this, extended_verification); |
| 62 | |
| 63 | return EFI_EXIT(EFI_SUCCESS); |
| 64 | } |
| 65 | |
| 66 | static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this) |
| 67 | { |
| 68 | EFI_ENTRY("%p", this); |
| 69 | |
| 70 | return EFI_EXIT(EFI_SUCCESS); |
| 71 | } |
| 72 | |
| 73 | static efi_status_t EFIAPI efi_net_receive_filters( |
| 74 | struct efi_simple_network *this, u32 enable, u32 disable, |
| 75 | int reset_mcast_filter, ulong mcast_filter_count, |
| 76 | struct efi_mac_address *mcast_filter) |
| 77 | { |
| 78 | EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable, |
| 79 | reset_mcast_filter, mcast_filter_count, mcast_filter); |
| 80 | |
| 81 | /* XXX Do we care? */ |
| 82 | |
| 83 | return EFI_EXIT(EFI_SUCCESS); |
| 84 | } |
| 85 | |
| 86 | static efi_status_t EFIAPI efi_net_station_address( |
| 87 | struct efi_simple_network *this, int reset, |
| 88 | struct efi_mac_address *new_mac) |
| 89 | { |
| 90 | EFI_ENTRY("%p, %x, %p", this, reset, new_mac); |
| 91 | |
| 92 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 93 | } |
| 94 | |
| 95 | static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this, |
| 96 | int reset, ulong *stat_size, |
| 97 | void *stat_table) |
| 98 | { |
| 99 | EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table); |
| 100 | |
| 101 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 102 | } |
| 103 | |
| 104 | static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this, |
| 105 | int ipv6, |
| 106 | struct efi_ip_address *ip, |
| 107 | struct efi_mac_address *mac) |
| 108 | { |
| 109 | EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac); |
| 110 | |
| 111 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 112 | } |
| 113 | |
| 114 | static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this, |
| 115 | int read_write, ulong offset, |
| 116 | ulong buffer_size, char *buffer) |
| 117 | { |
| 118 | EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size, |
| 119 | buffer); |
| 120 | |
| 121 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 122 | } |
| 123 | |
| 124 | static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this, |
| 125 | u32 *int_status, void **txbuf) |
| 126 | { |
| 127 | EFI_ENTRY("%p, %p, %p", this, int_status, txbuf); |
| 128 | |
| 129 | /* We send packets synchronously, so nothing is outstanding */ |
| 130 | if (int_status) |
| 131 | *int_status = 0; |
| 132 | if (txbuf) |
| 133 | *txbuf = new_tx_packet; |
| 134 | |
| 135 | new_tx_packet = NULL; |
| 136 | |
| 137 | return EFI_EXIT(EFI_SUCCESS); |
| 138 | } |
| 139 | |
| 140 | static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this, |
| 141 | ulong header_size, ulong buffer_size, void *buffer, |
| 142 | struct efi_mac_address *src_addr, |
| 143 | struct efi_mac_address *dest_addr, u16 *protocol) |
| 144 | { |
| 145 | EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size, |
| 146 | buffer_size, buffer, src_addr, dest_addr, protocol); |
| 147 | |
| 148 | if (header_size) { |
| 149 | /* We would need to create the header if header_size != 0 */ |
| 150 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 151 | } |
| 152 | |
Alexander Graf | 0b12b86 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 153 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 154 | /* Ethernet packets always fit, just bounce */ |
| 155 | memcpy(efi_bounce_buffer, buffer, buffer_size); |
| 156 | net_send_packet(efi_bounce_buffer, buffer_size); |
| 157 | #else |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 158 | net_send_packet(buffer, buffer_size); |
Alexander Graf | 0b12b86 | 2016-09-06 14:26:27 +0200 | [diff] [blame] | 159 | #endif |
| 160 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 161 | new_tx_packet = buffer; |
| 162 | |
| 163 | return EFI_EXIT(EFI_SUCCESS); |
| 164 | } |
| 165 | |
| 166 | static void efi_net_push(void *pkt, int len) |
| 167 | { |
| 168 | new_rx_packet = true; |
| 169 | } |
| 170 | |
| 171 | static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this, |
| 172 | ulong *header_size, ulong *buffer_size, void *buffer, |
| 173 | struct efi_mac_address *src_addr, |
| 174 | struct efi_mac_address *dest_addr, u16 *protocol) |
| 175 | { |
| 176 | EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size, |
| 177 | buffer_size, buffer, src_addr, dest_addr, protocol); |
| 178 | |
| 179 | push_packet = efi_net_push; |
| 180 | eth_rx(); |
| 181 | push_packet = NULL; |
| 182 | |
| 183 | if (!new_rx_packet) |
| 184 | return EFI_EXIT(EFI_NOT_READY); |
| 185 | |
| 186 | if (*buffer_size < net_rx_packet_len) { |
| 187 | /* Packet doesn't fit, try again with bigger buf */ |
| 188 | *buffer_size = net_rx_packet_len; |
| 189 | return EFI_EXIT(EFI_BUFFER_TOO_SMALL); |
| 190 | } |
| 191 | |
| 192 | memcpy(buffer, net_rx_packet, net_rx_packet_len); |
| 193 | *buffer_size = net_rx_packet_len; |
| 194 | new_rx_packet = false; |
| 195 | |
| 196 | return EFI_EXIT(EFI_SUCCESS); |
| 197 | } |
| 198 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 199 | void efi_net_set_dhcp_ack(void *pkt, int len) |
| 200 | { |
| 201 | int maxsize = sizeof(*dhcp_ack); |
| 202 | |
| 203 | if (!dhcp_ack) |
| 204 | dhcp_ack = malloc(maxsize); |
| 205 | |
| 206 | memcpy(dhcp_ack, pkt, min(len, maxsize)); |
| 207 | } |
| 208 | |
| 209 | /* This gets called from do_bootefi_exec(). */ |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 210 | int efi_net_register(void) |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 211 | { |
| 212 | struct efi_net_obj *netobj; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 213 | |
| 214 | if (!eth_get_dev()) { |
| 215 | /* No eth device active, don't expose any */ |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* We only expose the "active" eth device, so one is enough */ |
| 220 | netobj = calloc(1, sizeof(*netobj)); |
| 221 | |
| 222 | /* Fill in object data */ |
| 223 | netobj->parent.protocols[0].guid = &efi_net_guid; |
xypron.glpk@gmx.de | c35c924 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 224 | netobj->parent.protocols[0].protocol_interface = &netobj->net; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 225 | netobj->parent.protocols[1].guid = &efi_guid_device_path; |
Rob Clark | 88d2cb5 | 2017-09-13 18:05:32 -0400 | [diff] [blame] | 226 | netobj->parent.protocols[1].protocol_interface = |
| 227 | efi_dp_from_eth(); |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 228 | netobj->parent.protocols[2].guid = &efi_pxe_guid; |
xypron.glpk@gmx.de | c35c924 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 229 | netobj->parent.protocols[2].protocol_interface = &netobj->pxe; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 230 | netobj->parent.handle = &netobj->net; |
| 231 | netobj->net.start = efi_net_start; |
| 232 | netobj->net.stop = efi_net_stop; |
| 233 | netobj->net.initialize = efi_net_initialize; |
| 234 | netobj->net.reset = efi_net_reset; |
| 235 | netobj->net.shutdown = efi_net_shutdown; |
| 236 | netobj->net.receive_filters = efi_net_receive_filters; |
| 237 | netobj->net.station_address = efi_net_station_address; |
| 238 | netobj->net.statistics = efi_net_statistics; |
| 239 | netobj->net.mcastiptomac = efi_net_mcastiptomac; |
| 240 | netobj->net.nvdata = efi_net_nvdata; |
| 241 | netobj->net.get_status = efi_net_get_status; |
| 242 | netobj->net.transmit = efi_net_transmit; |
| 243 | netobj->net.receive = efi_net_receive; |
| 244 | netobj->net.mode = &netobj->net_mode; |
| 245 | netobj->net_mode.state = EFI_NETWORK_STARTED; |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 246 | memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6); |
| 247 | netobj->net_mode.max_packet_size = PKTSIZE; |
| 248 | |
| 249 | netobj->pxe.mode = &netobj->pxe_mode; |
| 250 | if (dhcp_ack) |
| 251 | netobj->pxe_mode.dhcp_ack = *dhcp_ack; |
| 252 | |
| 253 | /* Hook net up to the device list */ |
| 254 | list_add_tail(&netobj->parent.link, &efi_obj_list); |
| 255 | |
Alexander Graf | 94c4b99 | 2016-05-06 21:01:01 +0200 | [diff] [blame] | 256 | return 0; |
| 257 | } |