1 /** @file 2 Functions declaration related with DHCPv4 for UefiPxeBc Driver. 3 4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR> 5 6 This program and the accompanying materials 7 are licensed and made available under the terms and conditions of the BSD License 8 which accompanies this distribution. The full text of the license may be found at 9 http://opensource.org/licenses/bsd-license.php. 10 11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 13 14 **/ 15 16 #ifndef __EFI_PXEBC_DHCP4_H__ 17 #define __EFI_PXEBC_DHCP4_H__ 18 19 #define PXEBC_DHCP4_OPTION_MAX_NUM 16 20 #define PXEBC_DHCP4_OPTION_MAX_SIZE 312 21 #define PXEBC_DHCP4_PACKET_MAX_SIZE (sizeof (EFI_PXE_BASE_CODE_PACKET)) 22 #define PXEBC_DHCP4_S_PORT 67 23 #define PXEBC_DHCP4_C_PORT 68 24 #define PXEBC_BS_DOWNLOAD_PORT 69 25 #define PXEBC_BS_DISCOVER_PORT 4011 26 #define PXEBC_DHCP4_OPCODE_REQUEST 1 27 #define PXEBC_DHCP4_OPCODE_REPLY 2 28 #define PXEBC_DHCP4_MSG_TYPE_REQUEST 3 29 #define PXEBC_DHCP4_MAGIC 0x63538263 // network byte order 30 31 // 32 // Sub-Options in Dhcp Vendor Option 33 // 34 #define PXEBC_VENDOR_TAG_MTFTP_IP 1 35 #define PXEBC_VENDOR_TAG_MTFTP_CPORT 2 36 #define PXEBC_VENDOR_TAG_MTFTP_SPORT 3 37 #define PXEBC_VENDOR_TAG_MTFTP_TIMEOUT 4 38 #define PXEBC_VENDOR_TAG_MTFTP_DELAY 5 39 #define PXEBC_VENDOR_TAG_DISCOVER_CTRL 6 40 #define PXEBC_VENDOR_TAG_DISCOVER_MCAST 7 41 #define PXEBC_VENDOR_TAG_BOOT_SERVERS 8 42 #define PXEBC_VENDOR_TAG_BOOT_MENU 9 43 #define PXEBC_VENDOR_TAG_MENU_PROMPT 10 44 #define PXEBC_VENDOR_TAG_MCAST_ALLOC 11 45 #define PXEBC_VENDOR_TAG_CREDENTIAL_TYPES 12 46 #define PXEBC_VENDOR_TAG_BOOT_ITEM 71 47 48 #define PXEBC_BOOT_REQUEST_TIMEOUT 1 49 #define PXEBC_BOOT_REQUEST_RETRIES 4 50 51 #define PXEBC_DHCP4_OVERLOAD_FILE 1 52 #define PXEBC_DHCP4_OVERLOAD_SERVER_NAME 2 53 54 55 // 56 // The array index of the DHCP4 option tag interested 57 // 58 #define PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN 0 59 #define PXEBC_DHCP4_TAG_INDEX_VENDOR 1 60 #define PXEBC_DHCP4_TAG_INDEX_OVERLOAD 2 61 #define PXEBC_DHCP4_TAG_INDEX_MSG_TYPE 3 62 #define PXEBC_DHCP4_TAG_INDEX_SERVER_ID 4 63 #define PXEBC_DHCP4_TAG_INDEX_CLASS_ID 5 64 #define PXEBC_DHCP4_TAG_INDEX_BOOTFILE 6 65 #define PXEBC_DHCP4_TAG_INDEX_MAX 7 66 67 // 68 // Dhcp4 and Dhcp6 share this definition, and corresponding 69 // relatioinship is as follows: 70 // 71 // Dhcp4Discover <> Dhcp6Solicit 72 // Dhcp4Offer <> Dhcp6Advertise 73 // Dhcp4Request <> Dhcp6Request 74 // Dhcp4Ack <> DHcp6Reply 75 // 76 typedef enum { 77 PxeOfferTypeDhcpOnly, 78 PxeOfferTypeDhcpPxe10, 79 PxeOfferTypeDhcpWfm11a, 80 PxeOfferTypeDhcpBinl, 81 PxeOfferTypeProxyPxe10, 82 PxeOfferTypeProxyWfm11a, 83 PxeOfferTypeProxyBinl, 84 PxeOfferTypeBootp, 85 PxeOfferTypeMax 86 } PXEBC_OFFER_TYPE; 87 88 #define BIT(x) (1 << x) 89 #define CTRL(x) (0x1F & (x)) 90 #define DEFAULT_CLASS_ID_DATA "PXEClient:Arch:xxxxx:UNDI:003000" 91 #define DEFAULT_UNDI_TYPE 1 92 #define DEFAULT_UNDI_MAJOR 3 93 #define DEFAULT_UNDI_MINOR 0 94 95 #define MTFTP_VENDOR_OPTION_BIT_MAP \ 96 (BIT (PXEBC_VENDOR_TAG_MTFTP_IP) | \ 97 BIT (PXEBC_VENDOR_TAG_MTFTP_CPORT) | \ 98 BIT (PXEBC_VENDOR_TAG_MTFTP_SPORT) | \ 99 BIT (PXEBC_VENDOR_TAG_MTFTP_TIMEOUT) | \ 100 BIT (PXEBC_VENDOR_TAG_MTFTP_DELAY)) 101 102 #define DISCOVER_VENDOR_OPTION_BIT_MAP \ 103 (BIT (PXEBC_VENDOR_TAG_DISCOVER_CTRL) | \ 104 BIT (PXEBC_VENDOR_TAG_DISCOVER_MCAST) | \ 105 BIT (PXEBC_VENDOR_TAG_BOOT_SERVERS) | \ 106 BIT (PXEBC_VENDOR_TAG_BOOT_MENU) | \ 107 BIT (PXEBC_VENDOR_TAG_MENU_PROMPT)) 108 109 #define IS_VALID_BOOT_SERVERS(x) \ 110 ((((x)[0]) & BIT (PXEBC_VENDOR_TAG_BOOT_SERVERS)) \ 111 == BIT (PXEBC_VENDOR_TAG_BOOT_SERVERS)) 112 113 #define IS_VALID_BOOT_PROMPT(x) \ 114 ((((x)[0]) & BIT (PXEBC_VENDOR_TAG_MENU_PROMPT)) \ 115 == BIT (PXEBC_VENDOR_TAG_MENU_PROMPT)) 116 117 #define IS_VALID_BOOT_MENU(x) \ 118 ((((x)[0]) & BIT (PXEBC_VENDOR_TAG_BOOT_MENU)) \ 119 == BIT (PXEBC_VENDOR_TAG_BOOT_MENU)) 120 121 #define IS_VALID_MTFTP_VENDOR_OPTION(x) \ 122 (((UINT32) ((x)[0]) & MTFTP_VENDOR_OPTION_BIT_MAP) \ 123 == MTFTP_VENDOR_OPTION_BIT_MAP) 124 125 #define IS_VALID_DISCOVER_VENDOR_OPTION(x) \ 126 (((UINT32) ((x)[0]) & DISCOVER_VENDOR_OPTION_BIT_MAP) != 0) 127 128 #define IS_VALID_CREDENTIAL_VENDOR_OPTION(x) \ 129 (((UINT32) ((x)[0]) & BIT (PXEBC_VENDOR_TAG_CREDENTIAL_TYPES)) \ 130 == BIT (PXEBC_VENDOR_TAG_CREDENTIAL_TYPES)) 131 132 #define IS_VALID_BOOTITEM_VENDOR_OPTION(x) \ 133 (((UINT32) ((x)[PXEBC_VENDOR_TAG_BOOT_ITEM / 32]) & \ 134 BIT (PXEBC_VENDOR_TAG_BOOT_ITEM % 32)) \ 135 == BIT (PXEBC_VENDOR_TAG_BOOT_ITEM % 32)) 136 137 #define SET_VENDOR_OPTION_BIT_MAP(x, y) \ 138 (*(x + ((y) / 32)) = (UINT32) ((UINT32) ((x)[(y) / 32]) | BIT ((y) % 32))) 139 140 #define GET_NEXT_DHCP_OPTION(Opt) \ 141 (EFI_DHCP4_PACKET_OPTION *) ((UINT8 *) (Opt) + \ 142 sizeof (EFI_DHCP4_PACKET_OPTION) + (Opt)->Length - 1) 143 144 #define GET_OPTION_BUFFER_LEN(Pkt) \ 145 ((Pkt)->Length - sizeof (EFI_DHCP4_HEADER) - 4) 146 147 #define GET_NEXT_BOOT_SVR_ENTRY(Ent) \ 148 (PXEBC_BOOT_SVR_ENTRY *) ((UINT8 *) Ent + sizeof (*(Ent)) + \ 149 ((Ent)->IpCnt - 1) * sizeof (EFI_IPv4_ADDRESS)) 150 151 #define IS_PROXY_DHCP_OFFER(Offer) \ 152 EFI_IP4_EQUAL (&(Offer)->Dhcp4.Header.YourAddr, &mZeroIp4Addr) 153 154 #define IS_DISABLE_BCAST_DISCOVER(x) \ 155 (((x) & BIT (0)) == BIT (0)) 156 157 #define IS_DISABLE_MCAST_DISCOVER(x) \ 158 (((x) & BIT (1)) == BIT (1)) 159 160 #define IS_ENABLE_USE_SERVER_LIST(x) \ 161 (((x) & BIT (2)) == BIT (2)) 162 163 #define IS_DISABLE_PROMPT_MENU(x) \ 164 (((x) & BIT (3)) == BIT (3)) 165 166 167 #pragma pack(1) 168 typedef struct { 169 UINT8 ParaList[135]; 170 } PXEBC_DHCP4_OPTION_PARA; 171 172 typedef struct { 173 UINT16 Size; 174 } PXEBC_DHCP4_OPTION_MAX_MESG_SIZE; 175 176 typedef struct { 177 UINT8 Type; 178 UINT8 MajorVer; 179 UINT8 MinorVer; 180 } PXEBC_DHCP4_OPTION_UNDI; 181 182 typedef struct { 183 UINT8 Type; 184 } PXEBC_DHCP4_OPTION_MESG; 185 186 typedef struct { 187 UINT16 Type; 188 } PXEBC_DHCP4_OPTION_ARCH; 189 190 typedef struct { 191 UINT8 ClassIdentifier[10]; 192 UINT8 ArchitecturePrefix[5]; 193 UINT8 ArchitectureType[5]; 194 UINT8 Lit3[1]; 195 UINT8 InterfaceName[4]; 196 UINT8 Lit4[1]; 197 UINT8 UndiMajor[3]; 198 UINT8 UndiMinor[3]; 199 } PXEBC_DHCP4_OPTION_CLID; 200 201 typedef struct { 202 UINT8 Type; 203 UINT8 Guid[16]; 204 } PXEBC_DHCP4_OPTION_UUID; 205 206 typedef struct { 207 UINT16 Type; 208 UINT16 Layer; 209 } PXEBC_OPTION_BOOT_ITEM; 210 211 #pragma pack() 212 213 typedef union { 214 PXEBC_DHCP4_OPTION_PARA *Para; 215 PXEBC_DHCP4_OPTION_UNDI *Undi; 216 PXEBC_DHCP4_OPTION_ARCH *Arch; 217 PXEBC_DHCP4_OPTION_CLID *Clid; 218 PXEBC_DHCP4_OPTION_UUID *Uuid; 219 PXEBC_DHCP4_OPTION_MESG *Mesg; 220 PXEBC_DHCP4_OPTION_MAX_MESG_SIZE *MaxMesgSize; 221 } PXEBC_DHCP4_OPTION_ENTRY; 222 223 #pragma pack(1) 224 typedef struct { 225 UINT16 Type; 226 UINT8 IpCnt; 227 EFI_IPv4_ADDRESS IpAddr[1]; 228 } PXEBC_BOOT_SVR_ENTRY; 229 230 typedef struct { 231 UINT16 Type; 232 UINT8 DescLen; 233 UINT8 DescStr[1]; 234 } PXEBC_BOOT_MENU_ENTRY; 235 236 typedef struct { 237 UINT8 Timeout; 238 UINT8 Prompt[1]; 239 } PXEBC_MENU_PROMPT; 240 #pragma pack() 241 242 typedef struct { 243 UINT32 BitMap[8]; 244 EFI_IPv4_ADDRESS MtftpIp; 245 UINT16 MtftpCPort; 246 UINT16 MtftpSPort; 247 UINT8 MtftpTimeout; 248 UINT8 MtftpDelay; 249 UINT8 DiscoverCtrl; 250 EFI_IPv4_ADDRESS DiscoverMcastIp; 251 EFI_IPv4_ADDRESS McastIpBase; 252 UINT16 McastIpBlock; 253 UINT16 McastIpRange; 254 UINT16 BootSrvType; 255 UINT16 BootSrvLayer; 256 PXEBC_BOOT_SVR_ENTRY *BootSvr; 257 UINT8 BootSvrLen; 258 PXEBC_BOOT_MENU_ENTRY *BootMenu; 259 UINT8 BootMenuLen; 260 PXEBC_MENU_PROMPT *MenuPrompt; 261 UINT8 MenuPromptLen; 262 UINT32 *CredType; 263 UINT8 CredTypeLen; 264 } PXEBC_VENDOR_OPTION; 265 266 #define PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE (OFFSET_OF (EFI_DHCP4_PACKET, Dhcp4) + PXEBC_DHCP4_PACKET_MAX_SIZE) 267 268 typedef union { 269 EFI_DHCP4_PACKET Offer; 270 EFI_DHCP4_PACKET Ack; 271 UINT8 Buffer[PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE]; 272 } PXEBC_DHCP4_PACKET; 273 274 typedef struct { 275 PXEBC_DHCP4_PACKET Packet; 276 PXEBC_OFFER_TYPE OfferType; 277 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_TAG_INDEX_MAX]; 278 PXEBC_VENDOR_OPTION VendorOpt; 279 } PXEBC_DHCP4_PACKET_CACHE; 280 281 282 /** 283 Create a template DHCPv4 packet as a seed. 284 285 @param[out] Seed Pointer to the seed packet. 286 @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL. 287 288 **/ 289 VOID 290 PxeBcSeedDhcp4Packet ( 291 OUT EFI_DHCP4_PACKET *Seed, 292 IN EFI_UDP4_PROTOCOL *Udp4 293 ); 294 295 296 /** 297 Parse the cached DHCPv4 packet, including all the options. 298 299 @param[in] Cache4 Pointer to cached DHCPv4 packet. 300 301 @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully. 302 @retval EFI_DEVICE_ERROR Failed to parse and invalid packet. 303 304 **/ 305 EFI_STATUS 306 PxeBcParseDhcp4Packet ( 307 IN PXEBC_DHCP4_PACKET_CACHE *Cache4 308 ); 309 310 311 /** 312 Build and send out the request packet for the bootfile, and parse the reply. 313 314 @param[in] Private Pointer to PxeBc private data. 315 @param[in] Type PxeBc option boot item type. 316 @param[in] Layer Pointer to option boot item layer. 317 @param[in] UseBis Use BIS or not. 318 @param[in] DestIp Pointer to the server address. 319 @param[in] IpCount The total count of the server address. 320 @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST. 321 322 @retval EFI_SUCCESS Successfully discovered boot file. 323 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource. 324 @retval EFI_NOT_FOUND Can't get the PXE reply packet. 325 @retval Others Failed to discover boot file. 326 327 **/ 328 EFI_STATUS 329 PxeBcDhcp4Discover ( 330 IN PXEBC_PRIVATE_DATA *Private, 331 IN UINT16 Type, 332 IN UINT16 *Layer, 333 IN BOOLEAN UseBis, 334 IN EFI_IP_ADDRESS *DestIp, 335 IN UINT16 IpCount, 336 IN EFI_PXE_BASE_CODE_SRVLIST *SrvList 337 ); 338 339 /** 340 Switch the Ip4 policy to static. 341 342 @param[in] Private The pointer to PXEBC_PRIVATE_DATA. 343 344 @retval EFI_SUCCESS The policy is already configured to static. 345 @retval Others Other error as indicated.. 346 347 **/ 348 EFI_STATUS 349 PxeBcSetIp4Policy ( 350 IN PXEBC_PRIVATE_DATA *Private 351 ); 352 353 354 /** 355 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information. 356 357 @param[in] Private Pointer to PxeBc private data. 358 @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL 359 360 @retval EFI_SUCCESS The D.O.R.A process successfully finished. 361 @retval Others Failed to finish the D.O.R.A process. 362 363 **/ 364 EFI_STATUS 365 PxeBcDhcp4Dora ( 366 IN PXEBC_PRIVATE_DATA *Private, 367 IN EFI_DHCP4_PROTOCOL *Dhcp4 368 ); 369 370 #endif 371 372