Adriano Cordova | 9debc90 | 2024-12-04 00:05:25 -0300 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Implementation of EFI_IP4_CONFIG2_PROTOCOL |
| 4 | * |
| 5 | */ |
| 6 | |
| 7 | #include <efi_loader.h> |
| 8 | #include <image.h> |
| 9 | #include <malloc.h> |
| 10 | #include <mapmem.h> |
| 11 | #include <net.h> |
| 12 | |
| 13 | static const efi_guid_t efi_ip4_config2_guid = EFI_IP4_CONFIG2_PROTOCOL_GUID; |
| 14 | |
| 15 | struct efi_ip4_config2_manual_address current_http_ip; |
| 16 | static enum efi_ip4_config2_policy current_policy; |
| 17 | static char current_mac_addr[32]; |
| 18 | |
| 19 | /* EFI_IP4_CONFIG2_PROTOCOL */ |
| 20 | |
| 21 | /* |
| 22 | * efi_ip4_config2_set_data() - Set the configuration for the EFI IPv4 network |
| 23 | * stack running on the communication device |
| 24 | * |
| 25 | * This function implements EFI_IP4_CONFIG2_PROTOCOL.SetData() |
| 26 | * See the Unified Extensible Firmware Interface |
| 27 | * (UEFI) specification for details. |
| 28 | * |
| 29 | * @this: pointer to the protocol instance |
| 30 | * @data_type: the type of data to set |
| 31 | * @data_size: size of the buffer pointed to by data in bytes |
| 32 | * @data: the data buffer to set |
| 33 | * Return: status code |
| 34 | */ |
| 35 | static efi_status_t EFIAPI efi_ip4_config2_set_data(struct efi_ip4_config2_protocol *this, |
| 36 | enum efi_ip4_config2_data_type data_type, |
| 37 | efi_uintn_t data_size, |
| 38 | void *data) |
| 39 | { |
| 40 | EFI_ENTRY("%p, %d, %zu, %p", this, data_type, data_size, data); |
| 41 | efi_status_t ret = EFI_SUCCESS; |
| 42 | |
| 43 | if (!this || (data && !data_size) || (!data && data_size)) |
| 44 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 45 | |
| 46 | switch (data_type) { |
| 47 | case EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO: |
| 48 | return EFI_EXIT(EFI_WRITE_PROTECTED); |
| 49 | case EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS: |
| 50 | if (current_policy != EFI_IP4_CONFIG2_POLICY_STATIC) |
| 51 | return EFI_EXIT(EFI_WRITE_PROTECTED); |
| 52 | if (!data_size && !data) { |
| 53 | memset((void *)¤t_http_ip, 0, |
| 54 | sizeof(current_http_ip)); |
| 55 | return EFI_EXIT(EFI_SUCCESS); |
| 56 | } |
| 57 | if (data && data_size == sizeof(struct efi_ip4_config2_manual_address)) { |
| 58 | memcpy((void *)¤t_http_ip, data, |
| 59 | sizeof(struct efi_ip4_config2_manual_address)); |
| 60 | efi_net_set_addr(¤t_http_ip.address, |
| 61 | ¤t_http_ip.subnet_mask, NULL); |
| 62 | return EFI_EXIT(EFI_SUCCESS); |
| 63 | } |
| 64 | return EFI_EXIT(EFI_BAD_BUFFER_SIZE); |
| 65 | case EFI_IP4_CONFIG2_DATA_TYPE_POLICY: |
| 66 | if (data && data_size == sizeof(enum efi_ip4_config2_policy)) { |
| 67 | current_policy = *(enum efi_ip4_config2_policy *)data; |
| 68 | return EFI_EXIT(EFI_SUCCESS); |
| 69 | } |
| 70 | return EFI_EXIT(EFI_BAD_BUFFER_SIZE); |
| 71 | |
| 72 | default: |
| 73 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 74 | } |
| 75 | |
| 76 | return EFI_EXIT(ret); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * efi_ip4_config2_get_data() - Get the configuration for the EFI IPv4 network |
| 81 | * stack running on the communication device |
| 82 | * |
| 83 | * This function implements EFI_IP4_CONFIG2_PROTOCOL.GetData() |
| 84 | * See the Unified Extensible Firmware Interface |
| 85 | * (UEFI) specification for details. |
| 86 | * |
| 87 | * @this: pointer to the protocol instance |
| 88 | * @data_type: the type of data to get |
| 89 | * @data_size: size |
| 90 | * @data: the data buffer |
| 91 | * Return: status code |
| 92 | */ |
| 93 | static efi_status_t EFIAPI efi_ip4_config2_get_data(struct efi_ip4_config2_protocol *this, |
| 94 | enum efi_ip4_config2_data_type data_type, |
| 95 | efi_uintn_t *data_size, |
| 96 | void *data) |
| 97 | { |
| 98 | EFI_ENTRY("%p, %d, %p, %p", this, data_type, data_size, data); |
| 99 | |
| 100 | efi_status_t ret = EFI_SUCCESS; |
| 101 | struct efi_ip4_config2_interface_info *info; |
| 102 | int tmp; |
| 103 | |
| 104 | if (!this || !data_size) |
| 105 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 106 | |
| 107 | if (*data_size && !data) |
| 108 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 109 | |
| 110 | tmp = sizeof(struct efi_ip4_config2_interface_info) + sizeof(struct efi_ip4_route_table); |
| 111 | |
| 112 | switch (data_type) { |
| 113 | case EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO: |
| 114 | if (*data_size < tmp) { |
| 115 | *data_size = tmp; |
| 116 | return EFI_EXIT(EFI_BUFFER_TOO_SMALL); |
| 117 | } |
| 118 | |
| 119 | info = (struct efi_ip4_config2_interface_info *)data; |
| 120 | memset(info, 0, sizeof(*info)); |
| 121 | |
| 122 | info->hw_address_size = 6; |
| 123 | memcpy(info->hw_address.mac_addr, current_mac_addr, 6); |
| 124 | // Set the route table size |
| 125 | |
| 126 | info->route_table_size = 0; |
| 127 | break; |
| 128 | case EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS: |
| 129 | if (*data_size < sizeof(struct efi_ip4_config2_manual_address)) { |
| 130 | *data_size = sizeof(struct efi_ip4_config2_manual_address); |
| 131 | return EFI_EXIT(EFI_BUFFER_TOO_SMALL); |
| 132 | } |
| 133 | |
| 134 | efi_net_get_addr(¤t_http_ip.address, ¤t_http_ip.subnet_mask, NULL); |
| 135 | memcpy(data, (void *)¤t_http_ip, |
| 136 | sizeof(struct efi_ip4_config2_manual_address)); |
| 137 | |
| 138 | break; |
| 139 | default: |
| 140 | return EFI_EXIT(EFI_NOT_FOUND); |
| 141 | } |
| 142 | return EFI_EXIT(ret); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * efi_ip4_config2_register_notify() - Register an event that is to be signaled whenever |
| 147 | * a configuration process on the specified configuration |
| 148 | * data is done |
| 149 | * |
| 150 | * This function implements EFI_IP4_CONFIG2_PROTOCOL.RegisterDataNotify() |
| 151 | * See the Unified Extensible Firmware Interface |
| 152 | * (UEFI) specification for details. |
| 153 | * |
| 154 | * @this: pointer to the protocol instance |
| 155 | * @data_type: the type of data to register the event for |
| 156 | * @event: the event to register |
| 157 | * Return: status code |
| 158 | */ |
| 159 | static efi_status_t EFIAPI efi_ip4_config2_register_notify(struct efi_ip4_config2_protocol *this, |
| 160 | enum efi_ip4_config2_data_type data_type, |
| 161 | struct efi_event *event) |
| 162 | { |
| 163 | EFI_ENTRY("%p, %d, %p", this, data_type, event); |
| 164 | |
| 165 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * efi_ip4_config2_unregister_notify() - Remove a previously registered eventfor |
| 170 | * the specified configuration data |
| 171 | * |
| 172 | * This function implements EFI_IP4_CONFIG2_PROTOCOL.UnregisterDataNotify() |
| 173 | * See the Unified Extensible Firmware Interface |
| 174 | * (UEFI) specification for details. |
| 175 | * |
| 176 | * @this: pointer to the protocol instance |
| 177 | * @data_type: the type of data to remove the event for |
| 178 | * @event: the event to unregister |
| 179 | * Return: status code |
| 180 | */ |
| 181 | static efi_status_t EFIAPI efi_ip4_config2_unregister_notify(struct efi_ip4_config2_protocol *this, |
| 182 | enum efi_ip4_config2_data_type data_type, |
| 183 | struct efi_event *event) |
| 184 | { |
| 185 | EFI_ENTRY("%p, %d, %p", this, data_type, event); |
| 186 | |
| 187 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * efi_ipconfig_register() - register the ip4_config2 protocol |
| 192 | * |
| 193 | */ |
| 194 | efi_status_t efi_ipconfig_register(const efi_handle_t handle, |
| 195 | struct efi_ip4_config2_protocol *ip4config) |
| 196 | { |
| 197 | efi_status_t r = EFI_SUCCESS; |
| 198 | |
| 199 | r = efi_add_protocol(handle, &efi_ip4_config2_guid, |
| 200 | ip4config); |
| 201 | if (r != EFI_SUCCESS) { |
| 202 | log_err("ERROR: Failure to add protocol\n"); |
| 203 | return r; |
| 204 | } |
| 205 | |
| 206 | memcpy(current_mac_addr, eth_get_ethaddr(), 6); |
| 207 | |
| 208 | ip4config->set_data = efi_ip4_config2_set_data; |
| 209 | ip4config->get_data = efi_ip4_config2_get_data; |
| 210 | ip4config->register_data_notify = efi_ip4_config2_register_notify; |
| 211 | ip4config->unregister_data_notify = efi_ip4_config2_unregister_notify; |
| 212 | |
| 213 | return EFI_SUCCESS; |
| 214 | } |