]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/Network/PxeBc/Dxe/pxe_bc_dhcp.c
79bce9d4d8a76eacb2f532c8af00b939940c28be
[mirror_edk2.git] / EdkModulePkg / Universal / Network / PxeBc / Dxe / pxe_bc_dhcp.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13 pxe_bc_dhcp.c
14
15 Abstract:
16 DHCP and PXE discovery protocol implementations.
17
18 --*/
19
20
21 #include "Bc.h"
22
23 STATIC EFI_PXE_BASE_CODE_UDP_PORT DhcpServerPort = DHCP_SERVER_PORT;
24 STATIC EFI_PXE_BASE_CODE_UDP_PORT DHCPClientPort = DHCP_CLIENT_PORT;
25 STATIC EFI_PXE_BASE_CODE_UDP_PORT PseudoDhcpServerPort = PXE_DISCOVERY_PORT;
26 #define PSEUDO_DHCP_CLIENT_PORT PseudoDhcpServerPort
27 STATIC EFI_IP_ADDRESS BroadcastIP = { { 0xffffffff } };
28 STATIC EFI_IP_ADDRESS DefaultSubnetMask = { { 0xffffff00 } };
29
30 typedef union {
31 DHCPV4_OP_STRUCT *OpPtr;
32 PXE_OP_SERVER_LIST *BootServersStr;
33 PXE_SERVER_LIST *BootServerList;
34 PXE_BOOT_MENU_ENTRY *BootMenuItem;
35 PXE_OP_DISCOVERY_CONTROL *DiscoveryControl;
36 PXE_OP_BOOT_MENU *BootMenu;
37 PXE_OP_BOOT_ITEM *BootItem;
38 DHCPV4_OP_VENDOR_OPTIONS *VendorOptions;
39 DHCPV4_OP_OVERLOAD *Overload;
40 DHCPV4_OP_CLASS *PxeClassStr;
41 DHCPV4_OP_SUBNET_MASK *SubnetMaskStr;
42 DHCPV4_OP_MESSAGE_TYPE *MessageType;
43 UINT8 *BytePtr;
44 } UNION_PTR;
45
46 //
47 // 1 for Itanium-based, 0 for IA32
48 //
49 #define IA64SZ ((sizeof (UINTN) / sizeof (UINT32)) - 1)
50
51 #define SYS_ARCH (SYS_ARCH_EFI32 - (SYS_ARCH_EFI32 - IA64) * IA64SZ)
52
53 #pragma pack(1)
54 //
55 // option structure for DHCPREQUEST at end of DISCOVER options
56 // and for DHCPDECLINE
57 //
58 STATIC const struct requestopendstr {
59 DHCPV4_OP_REQUESTED_IP OpReqIP;
60 DHCPV4_OP_SERVER_IP DhcServerIpPtr;
61 UINT8 End[1];
62 }
63 RequestOpEndStr = {
64 {
65 {
66 OP_DHCP_REQ_IP_ADD,
67 DHCPV4_OPTION_LENGTH(DHCPV4_OP_REQUESTED_IP)
68 }
69 },
70 {
71 {
72 OP_DHCP_SERVER_IP,
73 DHCPV4_OPTION_LENGTH(DHCPV4_OP_SERVER_IP)
74 }
75 },
76 {
77 OP_END
78 }
79 };
80
81 #define DHCP_REQ_OPTIONS (*(struct requestopendstr *) DHCPV4_OPTIONS_BUFFER.End)
82
83 PXE_OP_BOOT_ITEM DefaultBootItem = {
84 {
85 VEND_PXE_BOOT_ITEM,
86 DHCPV4_OPTION_LENGTH(PXE_OP_BOOT_ITEM)
87 },
88 0, 0,
89 };
90
91 //
92 // PXE discovery control default structure
93 //
94 STATIC PXE_OP_DISCOVERY_CONTROL DefaultDisCtl = {
95 { VEND_PXE_DISCOVERY_CONTROL, DHCPV4_OPTION_LENGTH(PXE_OP_DISCOVERY_CONTROL) },
96 0
97 };
98
99 //
100 // PXE credentials option structure
101 //
102 typedef struct {
103 UINT8 c[4];
104 } PXE_CREDENTIAL;
105
106 typedef struct {
107 DHCPV4_OP_HEADER Header;
108 PXE_CREDENTIAL Credentials[1];
109 } PXE_OP_CREDENTIAL_TYPES;
110
111 //
112 // option structure for PXE discover (without credentials)
113 //
114 typedef struct { // discoveropendstr {
115 DHCPV4_OP_HEADER Header; // vendor options
116 PXE_OP_BOOT_ITEM BootItem;
117 UINT8 End[1]; // if credentials option, it starts here
118 } PXE_DISCOVER_OPTIONS;
119
120 #define DISCOVERoptions (*(PXE_DISCOVER_OPTIONS *) DHCPV4_OPTIONS_BUFFER.End)
121 #define DISCREDoptions (*(PXE_OP_CREDENTIAL_TYPES *) DISCOVERoptions.End)
122
123 //
124 // common option beginning for all our DHCP messages except
125 // DHCPDECLINE and DHCPRELEASE
126 //
127 STATIC struct optionsstr {
128 UINT8 DhcpCookie[4];
129 DHCPV4_OP_MESSAGE_TYPE DhcpMessageType;
130 DHCPV4_OP_MAX_MESSAGE_SIZE DhcpMaxMessageSize;
131 DHCPV4_OP_REQUESTED_OPTIONS DhcpRequestedOptions;
132 DHCPV4_OP_PLATFORM_ID DhcpPlatformId;
133 DHCPV4_OP_NETWORK_INTERFACE DhcpNetworkInterface;
134 DHCPV4_OP_ARCHITECTURE_TYPE DhcpClientArchitecture;
135 DHCPV4_OP_CLASS_ID DhcpClassIdentifier;
136 UINT8 End[1];
137 } DHCPOpStart;
138
139 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
140 VOID
141 OptionsStrucInit (
142 VOID
143 )
144 {
145 DHCPOpStart.DhcpCookie[0] = 99;
146 DHCPOpStart.DhcpCookie[1] = 130;
147 DHCPOpStart.DhcpCookie[2] = 83;
148 DHCPOpStart.DhcpCookie[3] = 99;
149 DHCPOpStart.DhcpMessageType.Header.OpCode = OP_DHCP_MESSAGE_TYPE;
150 DHCPOpStart.DhcpMessageType.Header.Length = 1;
151 DHCPOpStart.DhcpMessageType.Type = DHCPDISCOVER;
152 DHCPOpStart.DhcpMaxMessageSize.Header.OpCode = OP_DHCP_MAX_MESSAGE_SZ;
153 DHCPOpStart.DhcpMaxMessageSize.Header.Length = 2;
154 DHCPOpStart.DhcpMaxMessageSize.MaxSize[0] = MAX_DHCP_MSG_SZ >> 8;
155 DHCPOpStart.DhcpMaxMessageSize.MaxSize[1] = MAX_DHCP_MSG_SZ & 0xff;
156 DHCPOpStart.DhcpRequestedOptions.Header.OpCode = OP_DHCP_PARM_REQ_LIST;
157 DHCPOpStart.DhcpRequestedOptions.Header.Length = sizeof (DHCPV4_REQUESTED_OPTIONS_DATA);
158 DHCPOpStart.DhcpRequestedOptions.Data._OP_SUBNET_MASK = OP_SUBNET_MASK; /* 1 */
159 DHCPOpStart.DhcpRequestedOptions.Data._OP_TIME_OFFSET = OP_TIME_OFFSET; /* 2 */
160 DHCPOpStart.DhcpRequestedOptions.Data._OP_ROUTER_LIST = OP_ROUTER_LIST; /* 3 */
161 DHCPOpStart.DhcpRequestedOptions.Data._OP_TIME_SERVERS = OP_TIME_SERVERS; /* 4 */
162 DHCPOpStart.DhcpRequestedOptions.Data._OP_NAME_SERVERS = OP_NAME_SERVERS; /* 5 */
163 DHCPOpStart.DhcpRequestedOptions.Data._OP_DNS_SERVERS = OP_DNS_SERVERS; /* 6 */
164 DHCPOpStart.DhcpRequestedOptions.Data._OP_HOST_NAME = OP_HOST_NAME; /* 12 */
165 DHCPOpStart.DhcpRequestedOptions.Data._OP_BOOT_FILE_SZ = OP_BOOT_FILE_SZ; /* 13 */
166 DHCPOpStart.DhcpRequestedOptions.Data._OP_DOMAIN_NAME = OP_DOMAIN_NAME; /* 15 */
167 DHCPOpStart.DhcpRequestedOptions.Data._OP_ROOT_PATH = OP_ROOT_PATH; /* 17 */
168 DHCPOpStart.DhcpRequestedOptions.Data._OP_EXTENSION_PATH = OP_EXTENSION_PATH; /* 18 */
169 DHCPOpStart.DhcpRequestedOptions.Data._OP_MAX_DATAGRAM_SZ = OP_MAX_DATAGRAM_SZ; /* 22 */
170 DHCPOpStart.DhcpRequestedOptions.Data._OP_DEFAULT_TTL = OP_DEFAULT_TTL; /* 23 */
171 DHCPOpStart.DhcpRequestedOptions.Data._OP_BROADCAST_ADD = OP_BROADCAST_ADD; /* 28 */
172 DHCPOpStart.DhcpRequestedOptions.Data._OP_NIS_DOMAIN_NAME = OP_NIS_DOMAIN_NAME; /* 40 */
173 DHCPOpStart.DhcpRequestedOptions.Data._OP_NIS_SERVERS = OP_NIS_SERVERS; /* 41 */
174 DHCPOpStart.DhcpRequestedOptions.Data._OP_NTP_SERVERS = OP_NTP_SERVERS; /* 42 */
175 DHCPOpStart.DhcpRequestedOptions.Data._OP_VENDOR_SPECIFIC = OP_VENDOR_SPECIFIC; /* 43 */
176 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_REQ_IP_ADD = OP_DHCP_REQ_IP_ADD; /* 50 */
177 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_LEASE_TIME = OP_DHCP_LEASE_TIME; /* 51 */
178 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_SERVER_IP = OP_DHCP_SERVER_IP; /* 54 */
179 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_RENEWAL_TIME = OP_DHCP_RENEWAL_TIME; /* 58 */
180 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_REBINDING_TIME = OP_DHCP_REBINDING_TIME; /* 59 */
181 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_CLASS_IDENTIFIER = OP_DHCP_CLASS_IDENTIFIER; /* 60 */
182 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_TFTP_SERVER_NAME = OP_DHCP_TFTP_SERVER_NAME; /* 66 */
183 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_BOOTFILE = OP_DHCP_BOOTFILE; /* 67 */
184 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_PLATFORM_ID = OP_DHCP_PLATFORM_ID; /* 97 */
185 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption128 = 128;
186 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption129 = 129;
187 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption130 = 130;
188 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption131 = 131;
189 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption132 = 132;
190 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption133 = 133, DHCPOpStart.DhcpRequestedOptions.Data.VendorOption134 = 134;
191 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption135 = 135;
192 DHCPOpStart.DhcpPlatformId.Header.OpCode = OP_DHCP_PLATFORM_ID;
193 DHCPOpStart.DhcpPlatformId.Header.Length = DHCPV4_OPTION_LENGTH (DHCPV4_OP_PLATFORM_ID);
194 DHCPOpStart.DhcpNetworkInterface.Header.OpCode = OP_DHCP_NETWORK_ARCH;
195 DHCPOpStart.DhcpNetworkInterface.Header.Length = DHCPV4_OPTION_LENGTH (DHCPV4_OP_NETWORK_INTERFACE);
196 DHCPOpStart.DhcpNetworkInterface.Type = 0;
197 DHCPOpStart.DhcpNetworkInterface.MajorVersion = 0;
198 DHCPOpStart.DhcpNetworkInterface.MinorVersion = 0;
199 DHCPOpStart.DhcpClientArchitecture.Header.OpCode = OP_DHCP_SYSTEM_ARCH;
200 DHCPOpStart.DhcpClientArchitecture.Header.Length = DHCPV4_OPTION_LENGTH (DHCPV4_OP_ARCHITECTURE_TYPE);
201 DHCPOpStart.DhcpClientArchitecture.Type = HTONS (SYS_ARCH);
202 DHCPOpStart.DhcpClassIdentifier.Header.OpCode = OP_DHCP_CLASS_IDENTIFIER;
203 DHCPOpStart.DhcpClassIdentifier.Header.Length = sizeof (DHCPV4_CLASS_ID_DATA);
204 CopyMem (
205 DHCPOpStart.DhcpClassIdentifier.Data.ClassIdentifier,
206 "PXEClient:",
207 sizeof ("PXEClient:")
208 );
209 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.Lit2, "Arch:", sizeof ("Arch:"));
210 CopyMem (
211 DHCPOpStart.DhcpClassIdentifier.Data.ArchitectureType,
212 "xxxxx",
213 sizeof ("xxxxx")
214 );
215 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.Lit3, ":", sizeof (":"));
216 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.InterfaceName, "XXXX", sizeof ("XXXX"));
217 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.Lit4, ":", sizeof (":"));
218 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.UndiMajor, "yyy", sizeof ("yyy"));
219 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.UndiMinor, "xxx", sizeof ("xxx"));
220 DHCPOpStart.End[0] = OP_END;
221 };
222
223 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
224
225 //
226 // DHCPDECLINE option structure
227 //
228 struct opdeclinestr {
229 UINT8 DhcpCookie[4];
230 DHCPV4_OP_MESSAGE_TYPE DhcpMessageType;
231 struct requestopendstr OpDeclineEnd;
232 };
233
234 #define DHCPDECLINEoptions (*(struct opdeclinestr *) DHCPV4_TRANSMIT_BUFFER.options)
235
236 //
237 // DHCPRELEASE option structure
238 //
239 struct opreleasestr {
240 UINT8 DhcpCookie[4];
241 DHCPV4_OP_MESSAGE_TYPE DhcpMessageType;
242 DHCPV4_OP_SERVER_IP DhcServerIpPtr;
243 UINT8 End[1];
244 };
245
246 #define DHCPRELEASEoptions (*(struct opreleasestr *) DHCPV4_TRANSMIT_BUFFER.options)
247
248 //
249 // array of PXE vendor options in which we are interested
250 // value 0 -> not of interest, else value is index into PXE OPTION array
251 // option values from 1 to MAX_OUR_PXE_OPT
252 //
253 STATIC UINT8 ourPXEopts[MAX_OUR_PXE_OPT] = {
254 VEND_PXE_MTFTP_IP_IX, // multicast IP address of bootfile for MTFTP listen
255 VEND_PXE_MTFTP_CPORT_IX, // UDP Port to monitor for MTFTP responses - Intel order
256 VEND_PXE_MTFTP_SPORT_IX, // Server UDP Port for MTFTP open - Intel order
257 VEND_PXE_MTFTP_TMOUT_IX, // Listen timeout - secs
258 VEND_PXE_MTFTP_DELAY_IX, // Transmission timeout - secs
259 VEND_PXE_DISCOVERY_CONTROL_IX, // bit field
260 VEND_PXE_DISCOVERY_MCAST_ADDR_IX, // boot server discovery multicast address
261 VEND_PXE_BOOT_SERVERS_IX, // list of boot servers of form tp(2) cnt(1) ips[cnt]
262 VEND_PXE_BOOT_MENU_IX,
263 VEND_PXE_BOOT_PROMPT_IX,
264 VEND_PXE_MCAST_ADDRS_ALLOC_IX, // not used by client
265 VEND_PXE_CREDENTIAL_TYPES_IX,
266 VEND_13_IX, // not used by client
267 VEND_14_IX, // not used by client
268 VEND_15_IX, // not used by client
269 VEND_16_IX, // not used by client
270 VEND_17_IX, // not used by client
271 VEND_18_IX, // not used by client
272 VEND_19_IX, // not used by client
273 VEND_20_IX, // not used by client
274 VEND_21_IX, // not used by client
275 VEND_22_IX, // not used by client
276 VEND_23_IX, // not used by client
277 VEND_24_IX, // not used by client
278 VEND_25_IX, // not used by client
279 VEND_26_IX, // not used by client
280 VEND_27_IX, // not used by client
281 VEND_28_IX, // not used by client
282 VEND_29_IX, // not used by client
283 VEND_30_IX, // not used by client
284 VEND_31_IX, // not used by client
285 VEND_32_IX, // not used by client
286 VEND_33_IX, // not used by client
287 VEND_34_IX, // not used by client
288 VEND_35_IX, // not used by client
289 VEND_36_IX, // not used by client
290 VEND_37_IX, // not used by client
291 VEND_38_IX, // not used by client
292 VEND_39_IX, // not used by client
293 VEND_40_IX, // not used by client
294 VEND_41_IX, // not used by client
295 VEND_42_IX, // not used by client
296 VEND_43_IX, // not used by client
297 VEND_44_IX, // not used by client
298 VEND_45_IX, // not used by client
299 VEND_46_IX, // not used by client
300 VEND_47_IX, // not used by client
301 VEND_48_IX, // not used by client
302 VEND_49_IX, // not used by client
303 VEND_50_IX, // not used by client
304 VEND_51_IX, // not used by client
305 VEND_52_IX, // not used by client
306 VEND_53_IX, // not used by client
307 VEND_54_IX, // not used by client
308 VEND_55_IX, // not used by client
309 VEND_56_IX, // not used by client
310 VEND_57_IX, // not used by client
311 VEND_58_IX, // not used by client
312 VEND_59_IX, // not used by client
313 VEND_60_IX, // not used by client
314 VEND_61_IX, // not used by client
315 VEND_62_IX, // not used by client
316 VEND_63_IX, // not used by client
317 VEND_64_IX, // not used by client
318 VEND_65_IX, // not used by client
319 VEND_66_IX, // not used by client
320 VEND_67_IX, // not used by client
321 VEND_68_IX, // not used by client
322 VEND_69_IX, // not used by client
323 VEND_70_IX, // not used by client
324 VEND_PXE_BOOT_ITEM_IX
325 };
326
327 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
328
329 //
330 // array of options in which we are interested
331 // value 0 -> not of interest, else value is index into OPTION array
332 // option values from 1 to MAX_OUR_OPT
333 //
334 STATIC UINT8 OurDhcpOptions[MAX_OUR_OPT] = {
335 OP_SUBNET_MASK_IX, // OP_SUBNET_MASK 1 // data is the subnet mask
336 OP_TIME_OFFSET_IX, // OP_TIME_OFFSET 2 // data is the time offset of subnet to UTC in seconds
337 OP_ROUTER_LIST_IX, // OP_ROUTER_LIST 3 // list of routers on subnet
338 OP_TIME_SERVERS_IX, // OP_TIME_SERVERS 4 // list of time servers available
339 OP_NAME_SERVERS_IX, // OP_NAME_SERVERS 5 // list of name servers available
340 OP_DNS_SERVERS_IX, // OP_DNS_SERVERS 6 // list of DNS servers available
341 OP_LOG_SERVERS_IX, // OP_LOG_SERVERS 7
342 OP_COOKIE_SERVERS_IX, // OP_COOKIE_SERVERS 8
343 OP_LPR_SREVERS_IX, // OP_LPR_SREVERS 9
344 OP_IMPRESS_SERVERS_IX, // OP_IMPRESS_SERVERS 10
345 OP_RES_LOC_SERVERS_IX, // OP_RES_LOC_SERVERS 11
346 OP_HOST_NAME_IX, // OP_HOST_NAME 12 // client name
347 OP_BOOT_FILE_SZ_IX, // OP_BOOT_FILE_SZ 13 // number of 512 blocks of boot file
348 OP_DUMP_FILE_IX, // OP_DUMP_FILE 14 // path name of dump file if client crashes
349 OP_DOMAIN_NAME_IX, // OP_DOMAIN_NAME 15 // domain name to use
350 OP_SWAP_SERVER_IX, // OP_SWAP_SERVER 16
351 OP_ROOT_PATH_IX, // OP_ROOT_PATH 17 // path name containing root disk
352 OP_EXTENSION_PATH_IX, // OP_EXTENSION_PATH 18 // name of TFTP downloadable file of form of OP
353 OP_IP_FORWARDING_IX, // OP_IP_FORWARDING 19 // enable/disable IP packet forwarding
354 OP_NON_LOCAL_SRC_RTE_IX, // OP_NON_LOCAL_SRC_RTE 20 // enable/disable non local source routing
355 OP_POLICY_FILTER_IX, // OP_POLICY_FILTER 21 // policy filters for non local source routing
356 OP_MAX_DATAGRAM_SZ_IX, // OP_MAX_DATAGRAM_SZ 22 // maximum datagram reassembly size
357 OP_DEFAULT_TTL_IX, // OP_DEFAULT_TTL 23 // default IP time to live
358 OP_MTU_AGING_TIMEOUT_IX, // OP_MTU_AGING_TIMEOUT 24
359 OP_MTU_SIZES_IX, // OP_MTU_SIZES 25
360 OP_MTU_TO_USE_IX, // OP_MTU_TO_USE 26
361 OP_ALL_SUBNETS_LOCAL_IX, // OP_ALL_SUBNETS_LOCAL 27
362 OP_BROADCAST_ADD_IX, // OP_BROADCAST_ADD 28 // broadcast address used on subnet
363 OP_PERFORM_MASK_DISCOVERY_IX, // OP_PERFORM_MASK_DISCOVERY 29 // perform mask discovery using ICMP
364 OP_RESPOND_TO_MASK_REQ_IX, // OP_RESPOND_TO_MASK_REQ 30 // respond to subnet mask requests using ICMP
365 OP_PERFORM_ROUTER_DISCOVERY_IX, // OP_PERFORM_ROUTER_DISCOVERY 31
366 OP_ROUTER_SOLICIT_ADDRESS_IX, // OP_ROUTER_SOLICIT_ADDRESS 32
367 OP_STATIC_ROUTER_LIST_IX, // OP_STATIC_ROUTER_LIST 33 // list of dest/route pairs
368 OP_USE_ARP_TRAILERS_IX, // OP_USE_ARP_TRAILERS 34
369 OP_ARP_CACHE_TIMEOUT_IX, // OP_ARP_CACHE_TIMEOUT 35
370 OP_ETHERNET_ENCAPSULATION_IX, // OP_ETHERNET_ENCAPSULATION 36 // 0 -> RFC 894, 1 -> IEEE 802.3 (RFC 1042)
371 OP_TCP_DEFAULT_TTL_IX, // OP_TCP_DEFAULT_TTL 37 // default time to live when sending TCP segments
372 OP_TCP_KEEP_ALIVE_INT_IX, // OP_TCP_KEEP_ALIVE_INT 38 // keep alive interval in seconds
373 OP_KEEP_ALIVE_GARBAGE_IX, // OP_KEEP_ALIVE_GARBAGE 39
374 OP_NIS_DOMAIN_NAME_IX, // OP_NIS_DOMAIN_NAME 40
375 OP_NIS_SERVERS_IX, // OP_NIS_SERVERS 41
376 OP_NTP_SERVERS_IX, // OP_NTP_SERVERS 42
377 OP_VENDOR_SPECIFIC_IX, // OP_VENDOR_SPECIFIC 43
378 OP_NBNS_SERVERS_IX, // OP_NBNS_SERVERS 44
379 OP_NBDD_SERVERS_IX, // OP_NBDD_SERVERS 45
380 OP_NETBIOS_NODE_TYPE_IX, // OP_NETBIOS_NODE_TYPE 46
381 OP_NETBIOS_SCOPE_IX, // OP_NETBIOS_SCOPE 47
382 OP_XWINDOW_SYSTEM_FONT_SERVERS_IX, // OP_XWINDOW_SYSTEM_FONT_SERVERS 48
383 OP_XWINDOW_SYSTEM_DISPLAY_MANAGERS_IX, // OP_XWINDOW_SYSTEM_DISPLAY_MANAGERS 49
384 OP_DHCP_REQ_IP_ADD_IX, // OP_DHCP_REQ_IP_ADD 50 // requested IP address - in DHCPDISCOVER
385 OP_DHCP_LEASE_TIME_IX, // OP_DHCP_LEASE_TIME 51 // lease time requested/granted
386 OP_DHCP_OPTION_OVERLOAD_IX, // OP_DHCP_OPTION_OVERLOAD 52 // file/server name/both used to hold options
387 OP_DHCP_MESSAGE_TYPE_IX, // OP_DHCP_MESSAGE_TYPE 53 // message type
388 OP_DHCP_SERVER_IP_IX, // OP_DHCP_SERVER_IP 54 // IP of server
389 OP_DHCP_PARM_REQ_LIST_IX, // OP_DHCP_PARM_REQ_LIST 55 // list of requested parameters
390 OP_DHCP_ERROR_MESSAGE_IX, // OP_DHCP_ERROR_MESSAGE 56 // in DHCPNAK or DECLINE messages
391 OP_DHCP_MAX_MESSAGE_SZ_IX, // OP_DHCP_MAX_MESSAGE_SZ 57 // maximum DHCP message size client will accept
392 OP_DHCP_RENEWAL_TIME_IX, // OP_DHCP_RENEWAL_TIME 58 // time in seconds before transitioning to RENEWING state
393 OP_DHCP_REBINDING_TIME_IX, // OP_DHCP_REBINDING_TIME 59 // time in seconds before transitioning to REBINDING state
394 OP_DHCP_CLASS_IDENTIFIER_IX, // OP_DHCP_CLASS_IDENTIFIER 60
395 OP_DHCP_CLIENT_IDENTIFIER_IX, // OP_DHCP_CLIENT_IDENTIFIER 61
396 OP_RESERVED62_IX, // OP_RESERVED62
397 OP_RESERVED63_IX, // OP_RESERVED63
398 OP_NISPLUS_DOMAIN_NAME_IX, // OP_NISPLUS_DOMAIN_NAME 64
399 OP_NISPLUS_SERVERS_IX, // OP_NISPLUS_SERVERS 65
400 OP_DHCP_TFTP_SERVER_NAME_IX, // OP_DHCP_TFTP_SERVER_NAME 66
401 OP_DHCP_BOOTFILE_IX // OP_DHCP_BOOTFILE 67
402 };
403
404 #define RxBuf ((DHCP_RECEIVE_BUFFER *) (Private->ReceiveBuffers))
405
406 #pragma pack()
407
408 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
409 CHAR8 *
410 PxeBcLibGetSmbiosString (
411 IN SMBIOS_STRUCTURE_POINTER *Smbios,
412 IN UINT16 StringNumber
413 )
414 /*++
415 Routine description:
416 Return SMBIOS string given the string number.
417
418 Arguments:
419 Smbios - Pointer to SMBIOS structure
420 StringNumber - String number to return. 0 is used to skip all strings and
421 point to the next SMBIOS structure.
422
423 Returns:
424 Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == 0
425 --*/
426 {
427 UINT16 Index;
428 CHAR8 *String;
429
430 //
431 // Skip over formatted section
432 //
433 String = (CHAR8 *) (Smbios->Raw + Smbios->Hdr->Length);
434
435 //
436 // Look through unformated section
437 //
438 for (Index = 1; Index <= StringNumber || StringNumber == 0; Index++) {
439 if (StringNumber == Index) {
440 return String;
441 }
442 //
443 // Skip string
444 //
445 for (; *String != 0; String++)
446 ;
447 String++;
448
449 if (*String == 0) {
450 //
451 // If double NULL then we are done.
452 // Return pointer to next structure in Smbios.
453 // if you pass in a 0 you will always get here
454 //
455 Smbios->Raw = (UINT8 *)++String;
456 return NULL;
457 }
458 }
459
460 return NULL;
461 }
462
463 EFI_STATUS
464 PxeBcLibGetSmbiosSystemGuidAndSerialNumber (
465 IN EFI_GUID *SystemGuid,
466 OUT CHAR8 **SystemSerialNumber
467 )
468 /*++
469
470 Routine Description:
471 This function gets system guid and serial number from the smbios table
472
473 Arguments:
474 SystemGuid - The pointer of returned system guid
475 SystemSerialNumber - The pointer of returned system serial number
476
477 Returns:
478 EFI_SUCCESS - Successfully get the system guid and system serial number
479 EFI_NOT_FOUND - Not find the SMBIOS table
480 --*/
481 {
482 EFI_STATUS Status;
483 SMBIOS_STRUCTURE_TABLE *SmbiosTable;
484 SMBIOS_STRUCTURE_POINTER Smbios;
485 SMBIOS_STRUCTURE_POINTER SmbiosEnd;
486 UINT16 Index;
487
488 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);
489
490 if (EFI_ERROR (Status)) {
491 return EFI_NOT_FOUND;
492 }
493
494 Smbios.Hdr = (SMBIOS_HEADER *) (UINTN) SmbiosTable->TableAddress;
495 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);
496
497 for (Index = 0; Index < SmbiosTable->TableLength; Index++) {
498 if (Smbios.Hdr->Type == 1) {
499 if (Smbios.Hdr->Length < 0x19) {
500 //
501 // Older version did not support Guid and Serial number
502 //
503 continue;
504 }
505 //
506 // SMBIOS tables are byte packed so we need to do a byte copy to
507 // prevend alignment faults on Itanium-based platform.
508 //
509 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));
510 *SystemSerialNumber = PxeBcLibGetSmbiosString (&Smbios, Smbios.Type1->SerialNumber);
511
512 return EFI_SUCCESS;
513 }
514 //
515 // Make Smbios point to the next record
516 //
517 PxeBcLibGetSmbiosString (&Smbios, 0);
518
519 if (Smbios.Raw >= SmbiosEnd.Raw) {
520 //
521 // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
522 // given this we must double check against the lenght of
523 // the structure.
524 //
525 return EFI_SUCCESS;
526 }
527 }
528
529 return EFI_SUCCESS;
530 }
531
532 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
533
534 //
535 // add router list to list
536 //
537 STATIC
538 VOID
539 Ip4AddRouterList (
540 PXE_BASECODE_DEVICE *Private,
541 DHCPV4_OP_IP_LIST *IpListPtr
542 )
543 {
544 EFI_IP_ADDRESS TmpIp;
545 INTN Index;
546 INTN num;
547
548 if (IpListPtr == NULL) {
549 return ;
550 }
551
552 for (Index = 0, num = IpListPtr->Header.Length >> 2; Index < num; ++Index) {
553 CopyMem (&TmpIp, &IpListPtr->IpList[Index], 4);
554 Ip4AddRouter (Private, &TmpIp);
555 }
556 }
557
558 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
559
560 //
561 // send ARP for our IP - fail if someone has it
562 //
563 STATIC
564 BOOLEAN
565 SetStationIP (
566 PXE_BASECODE_DEVICE *Private
567 )
568 {
569 EFI_MAC_ADDRESS DestMac;
570 EFI_STATUS EfiStatus;
571
572 ZeroMem (&DestMac, sizeof DestMac);
573
574 if (GetHwAddr(Private, (EFI_IP_ADDRESS *)&DHCP_REQ_OPTIONS.OpReqIP.Ip, (EFI_MAC_ADDRESS *)&DestMac)
575 || DoArp(Private, (EFI_IP_ADDRESS *)&DHCP_REQ_OPTIONS.OpReqIP.Ip, (EFI_MAC_ADDRESS *)&DestMac) == EFI_SUCCESS) {
576 return FALSE; // somebody else has this IP
577 }
578
579 CopyMem (
580 (EFI_IPv4_ADDRESS *) &Private->EfiBc.Mode->StationIp,
581 &DHCP_REQ_OPTIONS.OpReqIP.Ip,
582 sizeof (EFI_IPv4_ADDRESS)
583 );
584
585 Private->GoodStationIp = TRUE;
586
587 if (!Private->UseIgmpv1Reporting) {
588 return TRUE;
589 }
590
591 if (Private->Igmpv1TimeoutEvent != NULL) {
592 return TRUE;
593 }
594
595 EfiStatus = gBS->CreateEvent (
596 EFI_EVENT_TIMER,
597 EFI_TPL_CALLBACK,
598 NULL,
599 NULL,
600 &Private->Igmpv1TimeoutEvent
601 );
602
603 if (EFI_ERROR (EfiStatus)) {
604 Private->Igmpv1TimeoutEvent = NULL;
605 return TRUE;
606 }
607
608 EfiStatus = gBS->SetTimer (
609 Private->Igmpv1TimeoutEvent,
610 TimerRelative,
611 (UINT64) V1ROUTER_PRESENT_TIMEOUT * 10000000
612 ); /* 400 seconds */
613
614 if (EFI_ERROR (EfiStatus)) {
615 gBS->CloseEvent (Private->Igmpv1TimeoutEvent);
616 Private->Igmpv1TimeoutEvent = NULL;
617 }
618
619 return TRUE;
620 }
621
622 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
623 STATIC
624 VOID
625 AddRouters (
626 PXE_BASECODE_DEVICE *Private,
627 DHCP_RECEIVE_BUFFER *RxBufPtr
628 )
629 {
630 Ip4AddRouter (Private, &RxBufPtr->u.Dhcpv4.giaddr);
631
632 Ip4AddRouterList (
633 Private,
634 (DHCPV4_OP_IP_LIST *) RxBufPtr->OpAdds.PktOptAdds[OP_ROUTER_LIST_IX - 1]
635 );
636 }
637
638 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
639 STATIC
640 EFI_STATUS
641 DoUdpWrite (
642 PXE_BASECODE_DEVICE *Private,
643 EFI_IP_ADDRESS *ServerIpPtr,
644 EFI_PXE_BASE_CODE_UDP_PORT *ServerPortPtr,
645 EFI_IP_ADDRESS *ClientIpPtr,
646 EFI_PXE_BASE_CODE_UDP_PORT *ClientPortPtr
647 )
648 {
649 UINTN Len;
650
651 Len = sizeof DHCPV4_TRANSMIT_BUFFER;
652
653 return UdpWrite (
654 Private,
655 EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT,
656 ServerIpPtr,
657 ServerPortPtr,
658 0,
659 ClientIpPtr,
660 ClientPortPtr,
661 0,
662 0,
663 &Len,
664 Private->TransmitBuffer
665 );
666 }
667
668 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
669
670 //
671 // initialize the DHCP structure
672 //
673 typedef struct {
674 UINT8 x[4];
675 } C4Str;
676
677 STATIC
678 VOID
679 InitDhcpv4TxBuf (
680 PXE_BASECODE_DEVICE *Private
681 )
682 {
683 UINTN HwAddrLen;
684 UINT8 *String;
685 CHAR8 *SystemSerialNumber;
686 EFI_PXE_BASE_CODE_MODE *PxebcMode;
687
688 PxebcMode = Private->EfiBc.Mode;
689
690 ZeroMem (&DHCPV4_TRANSMIT_BUFFER, sizeof (DHCPV4_STRUCT));
691 DHCPV4_TRANSMIT_BUFFER.op = BOOTP_REQUEST;
692 DHCPV4_TRANSMIT_BUFFER.htype = Private->SimpleNetwork->Mode->IfType;
693 DHCPV4_TRANSMIT_BUFFER.flags = HTONS (DHCP_BROADCAST_FLAG);
694 CopyMem (&DHCPV4_OPTIONS_BUFFER, (VOID *) &DHCPOpStart, sizeof (DHCPOpStart));
695
696 //
697 // default to hardware address
698 //
699 HwAddrLen = Private->SimpleNetwork->Mode->HwAddressSize;
700
701 if (HwAddrLen > sizeof DHCPV4_TRANSMIT_BUFFER.chaddr) {
702 HwAddrLen = sizeof DHCPV4_TRANSMIT_BUFFER.chaddr;
703 }
704
705 String = (UINT8 *) &Private->SimpleNetwork->Mode->CurrentAddress;
706
707 if (PxeBcLibGetSmbiosSystemGuidAndSerialNumber (
708 (EFI_GUID *) DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid,
709 &SystemSerialNumber
710 ) == EFI_SUCCESS) {
711 if (PxebcMode->SendGUID) {
712 HwAddrLen = sizeof (EFI_GUID);
713 String = (UINT8 *) DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid;
714 }
715 } else {
716 //
717 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
718 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
719 // GUID not yet set - send all 0's to show not programable
720 //
721 ZeroMem (DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof (EFI_GUID));
722 }
723
724 DHCPV4_TRANSMIT_BUFFER.hlen = (UINT8) HwAddrLen;
725 CopyMem (DHCPV4_TRANSMIT_BUFFER.chaddr, String, HwAddrLen);
726
727 CvtNum (
728 SYS_ARCH,
729 (UINT8 *) DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.ArchitectureType,
730 sizeof DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.ArchitectureType
731 );
732
733 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.Type = Private->NiiPtr->Type;
734 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.MajorVersion = Private->NiiPtr->MajorVer;
735 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.MinorVersion = Private->NiiPtr->MinorVer;
736
737 *(C4Str *) DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.InterfaceName = *(C4Str *) Private->NiiPtr->StringId;
738
739 CvtNum (
740 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.MajorVersion,
741 (UINT8 *) DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.UndiMajor,
742 sizeof DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.UndiMajor
743 );
744
745 CvtNum (
746 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.MinorVersion,
747 (UINT8 *) DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.UndiMinor,
748 sizeof DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.UndiMinor
749 );
750 }
751
752 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
753 STATIC
754 UINT32
755 DecodePxeOptions (
756 DHCP_RECEIVE_BUFFER *RxBufPtr,
757 UINT8 *ptr,
758 INTN Len
759 )
760 {
761 UINT8 Op;
762 UINT8 *EndPtr;
763 INTN Index;
764 UNION_PTR LocalPtr;
765 UINT32 status;
766
767 status = 0;
768
769 for (EndPtr = ptr + Len; ptr < EndPtr; ptr += Len + 2) {
770 Op = ptr[0];
771 Len = ptr[1];
772
773 switch (Op) {
774 case OP_PAD:
775 Len = -1;
776 break;
777
778 case OP_END:
779 return status;
780
781 default:
782 LocalPtr.BytePtr = ptr;
783 if (Op <= MAX_OUR_PXE_OPT) {
784 Index = ourPXEopts[Op - 1];
785 if (Index) {
786 RxBufPtr->OpAdds.PxeOptAdds[Index - 1] = LocalPtr.OpPtr;
787 status |= 1 << Index;
788 if (Index == VEND_PXE_BOOT_ITEM && LocalPtr.BootItem->Header.Length == 3) {
789 RxBufPtr->OpAdds.Status |= USE_THREE_BYTE;
790 }
791 }
792 }
793 break;
794 }
795 }
796
797 return status;
798 }
799
800 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
801 STATIC
802 VOID
803 DecodeOptions (
804 DHCP_RECEIVE_BUFFER *RxBufPtr,
805 UINT8 *ptr,
806 INTN Len
807 )
808 {
809 UINT8 Op;
810 UINT8 *EndPtr;
811 INTN Index;
812 UNION_PTR LocalPtr;
813
814 for (EndPtr = ptr + Len; ptr < EndPtr; ptr += Len + 2) {
815 Op = ptr[0];
816 Len = ptr[1];
817
818 switch (Op) {
819 case OP_PAD:
820 Len = -1;
821 break;
822
823 case OP_END:
824 return ;
825
826 default:
827 LocalPtr.BytePtr = ptr;
828 if (Op <= MAX_OUR_OPT) {
829 Index = OurDhcpOptions[Op - 1];
830 if (Index) {
831 RxBufPtr->OpAdds.PktOptAdds[Index - 1] = LocalPtr.OpPtr;
832 if (Index == OP_VENDOR_SPECIFIC_IX) {
833 UINT32 status;
834 status = DecodePxeOptions (
835 RxBufPtr,
836 (UINT8 *) LocalPtr.VendorOptions->VendorOptions,
837 LocalPtr.VendorOptions->Header.Length
838 );
839 if (status) {
840 RxBufPtr->OpAdds.Status |= PXE_TYPE;
841 //
842 // check for all the MTFTP info options present - any missing is a nogo
843 //
844 if ((status & WfM11a_OPTS) == WfM11a_OPTS) {
845 RxBufPtr->OpAdds.Status |= WfM11a_TYPE;
846 }
847
848 if (status & DISCOVER_OPTS) {
849 RxBufPtr->OpAdds.Status |= DISCOVER_TYPE;
850 }
851
852 if (status & CREDENTIALS_OPT) {
853 RxBufPtr->OpAdds.Status |= CREDENTIALS_TYPE;
854 }
855 }
856 }
857 }
858 }
859 break;
860 }
861 }
862 }
863
864 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
865 VOID
866 Parse (
867 DHCP_RECEIVE_BUFFER *RxBufPtr,
868 INTN Len
869 )
870 {
871 UNION_PTR LocalPtr;
872
873 //
874 // initialize
875 //
876 SetMem (&RxBufPtr->OpAdds, sizeof RxBufPtr->OpAdds, 0);
877
878 DecodeOptions (
879 RxBufPtr,
880 RxBufPtr->u.Dhcpv4.options + 4,
881 Len - (sizeof RxBufPtr->u.Dhcpv4 - sizeof RxBufPtr->u.Dhcpv4.options + 4)
882 );
883
884 LocalPtr.OpPtr = RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_OPTION_OVERLOAD_IX - 1];
885
886 if ((LocalPtr.OpPtr) && (LocalPtr.Overload->Overload & OVLD_SRVR_NAME)) {
887 DecodeOptions (RxBufPtr, RxBufPtr->u.Dhcpv4.sname, sizeof RxBufPtr->u.Dhcpv4.sname);
888 }
889
890 if (LocalPtr.OpPtr && (LocalPtr.Overload->Overload & OVLD_FILE)) {
891 DecodeOptions (RxBufPtr, RxBufPtr->u.Dhcpv4.file, sizeof RxBufPtr->u.Dhcpv4.file);
892 } else if (!RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1] && RxBufPtr->u.Dhcpv4.file[0]) {
893 RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1] = (DHCPV4_OP_STRUCT *) (RxBufPtr->u.Dhcpv4.file - sizeof (DHCPV4_OP_HEADER));
894
895 RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1]->Header.Length = (UINT8) AsciiStrLen ((CHAR8 *)RxBufPtr->u.Dhcpv4.file);
896 }
897
898 LocalPtr.OpPtr = RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_CLASS_IDENTIFIER_IX - 1];
899
900 if ((LocalPtr.OpPtr) &&
901 LocalPtr.PxeClassStr->Header.Length >= 9 &&
902 !CompareMem (LocalPtr.PxeClassStr->Class, "PXEClient", 9)
903 ) {
904 RxBufPtr->OpAdds.Status |= PXE_TYPE;
905 }
906 }
907
908 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
909 STATIC
910 VOID
911 CopyParseRxBuf (
912 PXE_BASECODE_DEVICE *Private,
913 INTN RxBufIndex,
914 INTN PacketIndex
915 )
916 {
917 DHCP_RECEIVE_BUFFER *RxBufPtr;
918
919 RxBufPtr = &((DHCP_RECEIVE_BUFFER *) Private->DhcpPacketBuffer)[PacketIndex];
920
921 CopyMem (
922 &RxBufPtr->u.Dhcpv4,
923 &RxBuf[RxBufIndex].u.Dhcpv4,
924 sizeof (RxBuf[RxBufIndex].u.Dhcpv4)
925 );
926
927 Parse (RxBufPtr, sizeof RxBufPtr->u.ReceiveBuffer);
928 }
929
930 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
931 STATIC
932 VOID
933 CopyProxyRxBuf (
934 PXE_BASECODE_DEVICE *Private,
935 INTN RxBufIndex
936 )
937 {
938 Private->EfiBc.Mode->ProxyOfferReceived = TRUE;
939 CopyParseRxBuf (Private, RxBufIndex, PXE_OFFER_INDEX);
940 }
941
942 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
943 STATIC
944 VOID
945 CopyParse (
946 PXE_BASECODE_DEVICE *Private,
947 EFI_PXE_BASE_CODE_PACKET *PacketPtr,
948 EFI_PXE_BASE_CODE_PACKET *NewPacketPtr,
949 INTN Index
950 )
951 {
952 DHCP_RECEIVE_BUFFER *DhcpRxBuf;
953
954 DhcpRxBuf = &((DHCP_RECEIVE_BUFFER *) Private->DhcpPacketBuffer)[Index];
955
956 CopyMem (
957 (EFI_PXE_BASE_CODE_PACKET *) &DhcpRxBuf->u.Dhcpv4,
958 NewPacketPtr,
959 sizeof (*NewPacketPtr)
960 );
961
962 CopyMem (&*PacketPtr, &*NewPacketPtr, sizeof (*NewPacketPtr));
963
964 Parse (DhcpRxBuf, sizeof DhcpRxBuf->u.ReceiveBuffer);
965 }
966
967 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
968 BOOLEAN
969 AckEdit (
970 DHCP_RECEIVE_BUFFER *DhcpRxBuf
971 )
972 {
973 UNION_PTR LocalPtr;
974
975 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_MESSAGE_TYPE_IX - 1];
976
977 //
978 // check that an ACK
979 // if a DHCP type, must be DHCPOFFER and must have server id
980 //
981 return (BOOLEAN)
982 (
983 (LocalPtr.OpPtr) &&
984 (LocalPtr.MessageType->Type == DHCPACK) &&
985 DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1]
986 );
987 }
988
989 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
990
991 //
992 // if a discover type packet, make sure all required fields are present
993 //
994 BOOLEAN
995 DHCPOfferAckEdit (
996 DHCP_RECEIVE_BUFFER *DhcpRxBuf
997 )
998 {
999 PXE_OP_SERVER_LIST *BootServerOpPtr;
1000 UNION_PTR LocalPtr;
1001
1002 if ((DhcpRxBuf->OpAdds.Status & DISCOVER_TYPE) == 0) {
1003 return TRUE;
1004 }
1005
1006 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_CONTROL_IX - 1];
1007
1008 if (LocalPtr.OpPtr == NULL) {
1009 LocalPtr.OpPtr = (DHCPV4_OP_STRUCT *) &DefaultDisCtl;
1010 DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_CONTROL_IX - 1] = (DHCPV4_OP_STRUCT *) &DefaultDisCtl;
1011 }
1012 //
1013 // make sure all required fields are here
1014 // if mucticast enabled, need multicast address
1015 //
1016 if (!(LocalPtr.DiscoveryControl->ControlBits & DISABLE_MCAST) &&
1017 (!DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_MCAST_ADDR_IX - 1] || !IS_MULTICAST (((DHCPV4_OP_STRUCT *) DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_MCAST_ADDR_IX - 1])->Data))
1018 ) {
1019 return FALSE;
1020 //
1021 // missing required field
1022 //
1023 }
1024 //
1025 // if a list, it better be good
1026 //
1027 BootServerOpPtr = (PXE_OP_SERVER_LIST *) DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_SERVERS_IX - 1];
1028
1029 if (BootServerOpPtr != NULL) {
1030 PXE_SERVER_LIST *BootServerListPtr;
1031 INTN ServerListLen;
1032 INTN ServerEntryLen;
1033
1034 BootServerListPtr = BootServerOpPtr->ServerList;
1035 ServerListLen = BootServerOpPtr->Header.Length;
1036
1037 do {
1038 EFI_IPv4_ADDRESS *IpListPtr;
1039 INTN IpCnt;
1040
1041 IpCnt = BootServerListPtr->u.Ipv4List.IpCount;
1042
1043 ServerEntryLen = sizeof (PXEV4_SERVER_LIST) + 2 + (IpCnt - 1) * sizeof (EFI_IPv4_ADDRESS);
1044
1045 if (ServerListLen < ServerEntryLen) {
1046 //
1047 // missing required field
1048 //
1049 return FALSE;
1050 }
1051
1052 IpListPtr = BootServerListPtr->u.Ipv4List.IpList;
1053
1054 while (IpCnt--) {
1055 if (IS_MULTICAST (IpListPtr)) {
1056 //
1057 // missing required field
1058 //
1059 return FALSE;
1060 } else {
1061 ++IpListPtr;
1062 }
1063 }
1064
1065 BootServerListPtr = (PXE_SERVER_LIST *) IpListPtr;
1066 } while (ServerListLen -= ServerEntryLen);
1067 }
1068 //
1069 // else there must be a list if use list enabled or multicast and
1070 // broadcast disabled
1071 //
1072 else if ((LocalPtr.DiscoveryControl->ControlBits & USE_ACCEPT_LIST) ||
1073 ((LocalPtr.DiscoveryControl->ControlBits & (DISABLE_MCAST | DISABLE_BCAST)) == (DISABLE_MCAST | DISABLE_BCAST))
1074 ) {
1075 //
1076 // missing required field
1077 //
1078 return FALSE;
1079 }
1080 //
1081 // if not USE_BOOTFILE or no bootfile given, must have menu stuff
1082 //
1083 if (!(LocalPtr.DiscoveryControl->ControlBits & USE_BOOTFILE) ||
1084 !DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1]
1085 ) {
1086 INTN MenuLth;
1087
1088 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_MENU_IX - 1];
1089
1090 if (LocalPtr.OpPtr == NULL || !DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_PROMPT_IX - 1]) {
1091 //
1092 // missing required field
1093 //
1094 return FALSE;
1095 }
1096 //
1097 // make sure menu valid
1098 //
1099 MenuLth = LocalPtr.BootMenu->Header.Length;
1100 LocalPtr.BootMenuItem = LocalPtr.BootMenu->MenuItem;
1101
1102 do {
1103 INTN MenuItemLen;
1104
1105 MenuItemLen = LocalPtr.BootMenuItem->DataLen;
1106
1107 if (MenuItemLen == 0) {
1108 //
1109 // missing required field
1110 //
1111 return FALSE;
1112 }
1113
1114 MenuItemLen += sizeof (*LocalPtr.BootMenuItem) - sizeof (LocalPtr.BootMenuItem->Data);
1115
1116 MenuLth -= MenuItemLen;
1117 LocalPtr.BytePtr += MenuItemLen;
1118 } while (MenuLth > 0);
1119
1120 if (MenuLth != 0) {
1121 //
1122 // missing required field
1123 //
1124 return FALSE;
1125 }
1126 }
1127
1128 if (!DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1]) {
1129 DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1] = (DHCPV4_OP_STRUCT *) &DefaultBootItem;
1130 }
1131
1132 return TRUE;
1133 }
1134
1135 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1136 BOOLEAN
1137 DHCPAckEdit (
1138 DHCP_RECEIVE_BUFFER *RxBufPtr
1139 )
1140 {
1141 return (BOOLEAN) (DHCPOfferAckEdit (RxBufPtr) ? AckEdit (RxBufPtr) : FALSE);
1142 }
1143
1144 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1145
1146 //
1147 // get an offer/ack
1148 //
1149 EFI_STATUS
1150 GetOfferAck (
1151 PXE_BASECODE_DEVICE *Private,
1152 BOOLEAN (*ExtraEdit)(DHCP_RECEIVE_BUFFER *DhcpRxBuf),
1153 UINT16 OpFlags, // for Udp read
1154 EFI_IP_ADDRESS *ServerIpPtr,
1155 EFI_PXE_BASE_CODE_UDP_PORT *ServerPortPtr,
1156 EFI_IP_ADDRESS *ClientIpPtr,
1157 EFI_PXE_BASE_CODE_UDP_PORT *ClientPortPtr,
1158 DHCP_RECEIVE_BUFFER *DhcpRxBuf,
1159 EFI_EVENT TimeoutEvent
1160 )
1161 /*++
1162 Routine description:
1163 Wait for an OFFER/ACK packet.
1164
1165 Parameters:
1166 Private := Pointer to PxeBc interface
1167 ExtraEdit := Pointer to extra option checking function
1168 OpFlags := UdpRead() option flags
1169 ServerIpPtr :=
1170 ServerPortPtr :=
1171 ClientIpPtr :=
1172 ClientPortPtr :=
1173 DhcpRxBuf :=
1174 TimeoutEvent :=
1175
1176 Returns:
1177 --*/
1178 {
1179 EFI_IP_ADDRESS ServerIp;
1180 EFI_STATUS StatCode;
1181 INTN RxBufLen;
1182
1183 for (;;) {
1184 //
1185 // Wait until we get a UDP packet.
1186 //
1187 ZeroMem (&ServerIp, sizeof (EFI_IP_ADDRESS));
1188 RxBufLen = sizeof RxBuf[0].u.ReceiveBuffer;
1189
1190 if ((StatCode = UdpRead (
1191 Private,
1192 OpFlags,
1193 ClientIpPtr,
1194 ClientPortPtr,
1195 ServerIpPtr,
1196 ServerPortPtr,
1197 0,
1198 0,
1199 (UINTN *) &RxBufLen,
1200 &DhcpRxBuf->u.Dhcpv4,
1201 TimeoutEvent
1202 )) != EFI_SUCCESS) {
1203 if (StatCode == EFI_TIMEOUT) {
1204 StatCode = EFI_NO_RESPONSE;
1205 }
1206
1207 break;
1208 }
1209 //
1210 // got a packet - see if a good offer
1211 //
1212 if (DhcpRxBuf->u.Dhcpv4.op != BOOTP_REPLY) {
1213 continue;
1214 }
1215
1216 if (DhcpRxBuf->u.Dhcpv4.xid != DHCPV4_TRANSMIT_BUFFER.xid) {
1217 continue;
1218 }
1219
1220 if (*(UINT32 *) DHCPV4_TRANSMIT_BUFFER.options != * (UINT32 *) DhcpRxBuf->u.Dhcpv4.options) {
1221 continue;
1222 }
1223
1224 if (*(UINT8 *) &DhcpRxBuf->u.Dhcpv4.yiaddr > 223) {
1225 continue;
1226 }
1227
1228 if (CompareMem (
1229 DhcpRxBuf->u.Dhcpv4.chaddr,
1230 DHCPV4_TRANSMIT_BUFFER.chaddr,
1231 sizeof DhcpRxBuf->u.Dhcpv4.chaddr
1232 )) {
1233 //
1234 // no good
1235 //
1236 continue;
1237 }
1238
1239 Parse (DhcpRxBuf, RxBufLen);
1240
1241 if (!(*ExtraEdit) (DhcpRxBuf)) {
1242 continue;
1243 }
1244 //
1245 // Good DHCP packet.
1246 //
1247 StatCode = EFI_SUCCESS;
1248 break;
1249 }
1250
1251 return StatCode;
1252 }
1253
1254 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1255
1256 //
1257 // get DHCPOFFER's
1258 //
1259 EFI_STATUS
1260 GetOffers (
1261 PXE_BASECODE_DEVICE *Private
1262 )
1263 {
1264 EFI_IP_ADDRESS ClientIp;
1265 EFI_IP_ADDRESS ServerIp;
1266 EFI_STATUS StatCode;
1267 EFI_EVENT TimeoutEvent;
1268 INTN NumOffers;
1269 INTN Index;
1270
1271 //
1272 //
1273 //
1274 ZeroMem (&ServerIp, sizeof (EFI_IP_ADDRESS));
1275 NumOffers = 0;
1276
1277 for (Index = 0; Index < (sizeof Private->ServerCount) / sizeof Private->ServerCount[0]; ++Index) {
1278 Private->ServerCount[Index] = 0;
1279 Private->GotProxy[Index] = 0;
1280 }
1281
1282 Private->GotBootp = 0;
1283 //
1284 // these we throw away
1285 //
1286 Private->GotProxy[DHCP_ONLY_IX] = 1;
1287 StatCode = gBS->CreateEvent (
1288 EFI_EVENT_TIMER,
1289 EFI_TPL_CALLBACK,
1290 NULL,
1291 NULL,
1292 &TimeoutEvent
1293 );
1294
1295 if (EFI_ERROR (StatCode)) {
1296 return StatCode;
1297 }
1298
1299 StatCode = gBS->SetTimer (
1300 TimeoutEvent,
1301 TimerRelative,
1302 Private->Timeout * 10000000 + 1000000
1303 );
1304
1305 if (EFI_ERROR (StatCode)) {
1306 gBS->CloseEvent (TimeoutEvent);
1307 return StatCode;
1308 }
1309 //
1310 // get offers
1311 //
1312 for (;;) {
1313 DHCP_RECEIVE_BUFFER *DhcpRxBuf;
1314 UNION_PTR LocalPtr;
1315
1316 DhcpRxBuf = &RxBuf[NumOffers];
1317
1318 if ((
1319 StatCode = GetOfferAck (
1320 Private,
1321 DHCPOfferAckEdit,
1322 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP |
1323 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP |
1324 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
1325 &ServerIp,
1326 &DhcpServerPort,
1327 &ClientIp,
1328 &DHCPClientPort,
1329 DhcpRxBuf,
1330 TimeoutEvent
1331 )
1332 ) != EFI_SUCCESS
1333 ) {
1334 break;
1335 }
1336
1337 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_MESSAGE_TYPE_IX - 1];
1338
1339 //
1340 // check type of offer
1341 //
1342 if (LocalPtr.OpPtr == NULL) {
1343 //
1344 // bootp - we only need one and make sure has bootfile
1345 //
1346 if (Private->GotBootp || !DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1]) {
1347 continue;
1348 }
1349
1350 Private->GotBootp = (UINT8) (NumOffers + 1);
1351 }
1352 //
1353 // if a DHCP type, must be DHCPOFFER and must have server id
1354 //
1355 else if (LocalPtr.MessageType->Type != DHCPOFFER || !DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1]) {
1356 continue;
1357 } else {
1358 INTN TypeIx;
1359
1360 //
1361 // get type - PXE10, WfM11a, or BINL
1362 //
1363 if (DhcpRxBuf->OpAdds.Status & DISCOVER_TYPE) {
1364 TypeIx = PXE10_IX;
1365 } else if (DhcpRxBuf->OpAdds.Status & WfM11a_TYPE) {
1366 //
1367 // WfM - make sure it has a bootfile
1368 //
1369 if (!DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1]) {
1370 continue;
1371 }
1372
1373 TypeIx = WfM11a_IX;
1374 } else {
1375 TypeIx = (DhcpRxBuf->OpAdds.Status & PXE_TYPE) ? BINL_IX : DHCP_ONLY_IX;
1376 }
1377 //
1378 // check DHCP or proxy
1379 //
1380 if (DhcpRxBuf->u.Dhcpv4.yiaddr == 0) {
1381 //
1382 // proxy - only need one of each type if not BINL
1383 // and must have at least PXE_TYPE
1384 //
1385 if (TypeIx == BINL_IX) {
1386 Private->BinlProxies[Private->GotProxy[BINL_IX]++] = (UINT8) NumOffers;
1387 } else if (Private->GotProxy[TypeIx]) {
1388 continue;
1389 } else {
1390 Private->GotProxy[TypeIx] = (UINT8) (NumOffers + 1);
1391 }
1392 } else {
1393 Private->OfferCount[TypeIx][Private->ServerCount[TypeIx]++] = (UINT8) NumOffers;
1394 }
1395 }
1396
1397 if (++NumOffers == MAX_OFFERS) {
1398 break;
1399 }
1400 }
1401
1402 gBS->CloseEvent (TimeoutEvent);
1403 Private->NumOffersReceived = NumOffers;
1404
1405 return (Private->NumOffersReceived) ? EFI_SUCCESS : EFI_NO_RESPONSE;
1406 }
1407
1408 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1409
1410 //
1411 // send DHCPDECLINE
1412 //
1413 STATIC
1414 VOID
1415 DeclineOffer (
1416 PXE_BASECODE_DEVICE *Private
1417 )
1418 {
1419 EFI_PXE_BASE_CODE_MODE *PxebcMode;
1420 UINT16 SaveSecs;
1421
1422 PxebcMode = Private->EfiBc.Mode;
1423 SaveSecs = DHCPV4_TRANSMIT_BUFFER.secs;
1424
1425 DHCPV4_TRANSMIT_BUFFER.secs = 0;
1426 DHCPV4_TRANSMIT_BUFFER.flags = 0;
1427 SetMem (
1428 DHCPV4_TRANSMIT_BUFFER.options + sizeof (struct opdeclinestr),
1429 sizeof (DHCPOpStart) - sizeof (struct opdeclinestr),
1430 OP_PAD
1431 );
1432 DHCPDECLINEoptions.DhcpMessageType.Type = DHCPDECLINE;
1433 CopyMem (&DHCPDECLINEoptions.OpDeclineEnd, &DHCP_REQ_OPTIONS, sizeof (struct requestopendstr));
1434 // DHCPDECLINEoptions.OpDeclineEnd = DHCP_REQ_OPTIONS;
1435
1436 {
1437 EFI_IP_ADDRESS TmpIp;
1438
1439 CopyMem (&TmpIp, &DHCP_REQ_OPTIONS.DhcServerIpPtr.Ip, sizeof TmpIp);
1440
1441 DoUdpWrite (
1442 Private,
1443 &TmpIp,
1444 &DhcpServerPort,
1445 &PxebcMode->StationIp,
1446 &DHCPClientPort
1447 );
1448 }
1449
1450 InitDhcpv4TxBuf (Private);
1451 DHCPV4_TRANSMIT_BUFFER.secs = SaveSecs;
1452 Private->GoodStationIp = FALSE;
1453 }
1454
1455 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1456
1457 //
1458 // send DHCPRELEASE
1459 //
1460 STATIC
1461 BOOLEAN
1462 Release (
1463 PXE_BASECODE_DEVICE *Private
1464 )
1465 {
1466 EFI_PXE_BASE_CODE_MODE *PxebcMode;
1467 UINT16 SaveSecs;
1468 DHCPV4_OP_SERVER_IP *Point;
1469
1470 PxebcMode = Private->EfiBc.Mode;
1471 SaveSecs = DHCPV4_TRANSMIT_BUFFER.secs;
1472 DHCPV4_TRANSMIT_BUFFER.secs = 0;
1473
1474 SetMem (
1475 DHCPV4_TRANSMIT_BUFFER.options + sizeof (struct opreleasestr),
1476 sizeof (DHCPOpStart) - sizeof (struct opreleasestr),
1477 OP_PAD
1478 );
1479
1480 DHCPRELEASEoptions.DhcpMessageType.Type = DHCPRELEASE;
1481 Point = (DHCPV4_OP_SERVER_IP *) DHCPV4_ACK_BUFFER.OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1];
1482 CopyMem (
1483 &DHCPRELEASEoptions.DhcServerIpPtr,
1484 &Point,
1485 sizeof DHCPRELEASEoptions.DhcServerIpPtr
1486 );
1487
1488 DHCPRELEASEoptions.End[0] = OP_END;
1489
1490 {
1491 EFI_IP_ADDRESS TmpIp;
1492
1493 CopyMem (&TmpIp, &DHCPRELEASEoptions.DhcServerIpPtr.Ip, sizeof TmpIp);
1494
1495 DoUdpWrite (
1496 Private,
1497 &TmpIp,
1498 &DhcpServerPort,
1499 &PxebcMode->StationIp,
1500 &DHCPClientPort
1501 );
1502 }
1503
1504 InitDhcpv4TxBuf (Private);
1505
1506 DHCPV4_TRANSMIT_BUFFER.secs = SaveSecs;
1507 Private->GoodStationIp = FALSE;
1508 return FALSE;
1509 }
1510
1511 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1512 STATIC
1513 BOOLEAN
1514 GetBINLAck (
1515 PXE_BASECODE_DEVICE *Private,
1516 EFI_IP_ADDRESS *ServerIpPtr
1517 )
1518 {
1519 DHCP_RECEIVE_BUFFER *DhcpRxBuf;
1520 EFI_STATUS StatCode;
1521 EFI_EVENT TimeoutEvent;
1522
1523 //
1524 //
1525 //
1526 StatCode = gBS->CreateEvent (
1527 EFI_EVENT_TIMER,
1528 EFI_TPL_CALLBACK,
1529 NULL,
1530 NULL,
1531 &TimeoutEvent
1532 );
1533
1534 if (EFI_ERROR (StatCode)) {
1535 return FALSE;
1536 }
1537
1538 StatCode = gBS->SetTimer (
1539 TimeoutEvent,
1540 TimerRelative,
1541 Private->Timeout * 10000000 + 1000000
1542 );
1543
1544 if (EFI_ERROR (StatCode)) {
1545 gBS->CloseEvent (TimeoutEvent);
1546 return FALSE;
1547 }
1548 //
1549 //
1550 //
1551 DhcpRxBuf = &PXE_BINL_BUFFER;
1552
1553 for (;;) {
1554 EFI_PXE_BASE_CODE_UDP_PORT BINLSrvPort;
1555
1556 BINLSrvPort = 0;
1557
1558 if (GetOfferAck (
1559 Private,
1560 AckEdit,
1561 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
1562 ServerIpPtr,
1563 &BINLSrvPort,
1564 &Private->EfiBc.Mode->StationIp,
1565 &PSEUDO_DHCP_CLIENT_PORT,
1566 DhcpRxBuf,
1567 TimeoutEvent
1568 ) != EFI_SUCCESS) {
1569 break;
1570 }
1571 //
1572 // make sure from whom we wanted
1573 //
1574 if (!DhcpRxBuf->u.Dhcpv4.yiaddr && !CompareMem (
1575 &ServerIpPtr->v4,
1576 &((DHCPV4_OP_SERVER_IP *) DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1])->Ip,
1577 sizeof (ServerIpPtr->v4)
1578 )) {
1579 gBS->CloseEvent (TimeoutEvent);
1580 //
1581 // got an ACK from server
1582 //
1583 return TRUE;
1584 }
1585 }
1586
1587 gBS->CloseEvent (TimeoutEvent);
1588 return FALSE;
1589 }
1590
1591 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1592
1593 //
1594 // make sure we can get BINL
1595 // send DHCPREQUEST to PXE server
1596 //
1597 STATIC
1598 BOOLEAN
1599 TryBINL (
1600 PXE_BASECODE_DEVICE *Private,
1601 INTN OfferIx
1602 )
1603 {
1604 DHCP_RECEIVE_BUFFER *DhcpRxBuf;
1605 EFI_IP_ADDRESS ServerIp;
1606 UINT16 SaveSecs;
1607 INTN Index;
1608
1609 DhcpRxBuf = &RxBuf[OfferIx];
1610
1611 //
1612 // send DHCP request
1613 // if fail return false
1614 //
1615 CopyMem (
1616 ((EFI_IPv4_ADDRESS *) &ServerIp),
1617 &((DHCPV4_OP_SERVER_IP *) DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1])->Ip,
1618 sizeof (EFI_IPv4_ADDRESS)
1619 );
1620
1621 //
1622 // client IP address - filled in by client if it knows it
1623 //
1624 CopyMem (
1625 ((EFI_IPv4_ADDRESS *) &DHCPV4_TRANSMIT_BUFFER.ciaddr),
1626 &DHCP_REQ_OPTIONS.OpReqIP.Ip,
1627 sizeof (EFI_IPv4_ADDRESS)
1628 );
1629
1630 SetMem (&DHCP_REQ_OPTIONS, sizeof DHCP_REQ_OPTIONS, OP_PAD);
1631 DHCPV4_TRANSMIT_BUFFER.flags = 0;
1632 DHCPV4_OPTIONS_BUFFER.End[0] = OP_END;
1633 AddRouters (Private, DhcpRxBuf);
1634 SaveSecs = DHCPV4_TRANSMIT_BUFFER.secs;
1635
1636 for (Index = 0; Index < 3; Private->TotalSeconds = (UINT16) (Private->TotalSeconds + Private->Timeout), ++Index) {
1637 DHCPV4_TRANSMIT_BUFFER.secs = HTONS (Private->TotalSeconds);
1638
1639 //
1640 // unicast DHCPREQUEST to PXE server
1641 //
1642 if (DoUdpWrite (
1643 Private,
1644 &ServerIp,
1645 &PseudoDhcpServerPort,
1646 (EFI_IP_ADDRESS *) &DHCPV4_TRANSMIT_BUFFER.ciaddr,
1647 &PSEUDO_DHCP_CLIENT_PORT
1648 ) != EFI_SUCCESS) {
1649 break;
1650 }
1651
1652 if (!GetBINLAck (Private, &ServerIp)) {
1653 continue;
1654 }
1655 //
1656 // early exit failures
1657 // make sure a good ACK
1658 //
1659 if (!DHCPOfferAckEdit (&PXE_BINL_BUFFER) || (
1660 !(PXE_BINL_BUFFER.OpAdds.Status & DISCOVER_TYPE) && !PXE_BINL_BUFFER.OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1]
1661 )
1662 ) {
1663 break;
1664 }
1665
1666 Private->EfiBc.Mode->ProxyOfferReceived = TRUE;
1667 return TRUE;
1668 }
1669 //
1670 // failed - reset seconds field, etc.
1671 //
1672 Private->EfiBc.Mode->RouteTableEntries = 0;
1673 //
1674 // reset
1675 //
1676 DHCPV4_TRANSMIT_BUFFER.secs = SaveSecs;
1677 return FALSE;
1678 }
1679
1680 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1681 STATIC
1682 BOOLEAN
1683 TryFinishBINL (
1684 PXE_BASECODE_DEVICE *Private,
1685 INTN OfferIx
1686 )
1687 {
1688 if (TryBINL (Private, OfferIx)) {
1689 return TRUE;
1690 }
1691
1692 return Release (Private);
1693 }
1694
1695 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1696 STATIC
1697 BOOLEAN
1698 TryFinishProxyBINL (
1699 PXE_BASECODE_DEVICE *Private
1700 )
1701 {
1702 INTN Index;
1703
1704 for (Index = 0; Index < Private->GotProxy[BINL_IX]; ++Index) {
1705 if (TryBINL (Private, Private->BinlProxies[Index])) {
1706 return TRUE;
1707 }
1708 }
1709
1710 return Release (Private);
1711 }
1712
1713 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1714
1715 //
1716 // try to finish DORA - send DHCP request, wait for ACK, check with ARP
1717 //
1718 STATIC
1719 BOOLEAN
1720 TryFinishDORA (
1721 PXE_BASECODE_DEVICE *Private,
1722 INTN OfferIx
1723 )
1724 {
1725 DHCP_RECEIVE_BUFFER *DhcpRxBuf;
1726 EFI_IP_ADDRESS ClientIp;
1727 EFI_IP_ADDRESS ServerIp;
1728 EFI_STATUS StatCode;
1729 UNION_PTR LocalPtr;
1730 EFI_EVENT TimeoutEvent;
1731
1732 //
1733 // send DHCP request
1734 // if fail return false
1735 //
1736 DhcpRxBuf = &DHCPV4_ACK_BUFFER;
1737 DHCPV4_OPTIONS_BUFFER.DhcpMessageType.Type = DHCPREQUEST;
1738 CopyMem (&DHCP_REQ_OPTIONS, &RequestOpEndStr, sizeof (RequestOpEndStr));
1739 // DHCP_REQ_OPTIONS = RequestOpEndStr;
1740 DHCP_REQ_OPTIONS.OpReqIP.Ip = *(EFI_IPv4_ADDRESS *) &RxBuf[OfferIx].u.Dhcpv4.yiaddr;
1741
1742 CopyMem (
1743 &DHCP_REQ_OPTIONS.DhcServerIpPtr.Ip,
1744 &((DHCPV4_OP_SERVER_IP *) RxBuf[OfferIx].OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1])->Ip,
1745 sizeof DHCP_REQ_OPTIONS.DhcServerIpPtr.Ip
1746 );
1747
1748 CopyMem (
1749 Private->EfiBc.Mode->SubnetMask.Addr,
1750 &DefaultSubnetMask,
1751 4
1752 );
1753
1754 //
1755 // broadcast DHCPREQUEST
1756 //
1757 if (DoUdpWrite (
1758 Private,
1759 &BroadcastIP,
1760 &DhcpServerPort,
1761 (EFI_IP_ADDRESS *) &DHCPV4_TRANSMIT_BUFFER.ciaddr,
1762 &DHCPClientPort
1763 ) != EFI_SUCCESS) {
1764 return FALSE;
1765 }
1766 //
1767 //
1768 //
1769 StatCode = gBS->CreateEvent (
1770 EFI_EVENT_TIMER,
1771 EFI_TPL_CALLBACK,
1772 NULL,
1773 NULL,
1774 &TimeoutEvent
1775 );
1776
1777 if (EFI_ERROR (StatCode)) {
1778 return FALSE;
1779 }
1780
1781 StatCode = gBS->SetTimer (
1782 TimeoutEvent,
1783 TimerPeriodic,
1784 Private->Timeout * 10000000 + 1000000
1785 );
1786
1787 if (EFI_ERROR (StatCode)) {
1788 gBS->CloseEvent (TimeoutEvent);
1789 return FALSE;
1790 }
1791 //
1792 // wait for ACK
1793 //
1794 for (;;) {
1795 if (GetOfferAck (
1796 Private,
1797 DHCPAckEdit,
1798 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP,
1799 &ServerIp,
1800 &DhcpServerPort,
1801 &ClientIp,
1802 &DHCPClientPort,
1803 DhcpRxBuf,
1804 TimeoutEvent
1805 ) != EFI_SUCCESS) {
1806 break;
1807 }
1808 //
1809 // check type of response - need DHCPACK
1810 //
1811 if (CompareMem (
1812 &DHCP_REQ_OPTIONS.OpReqIP.Ip,
1813 &DhcpRxBuf->u.Dhcpv4.yiaddr,
1814 sizeof (EFI_IPv4_ADDRESS)
1815 ) || CompareMem (
1816 &DHCP_REQ_OPTIONS.DhcServerIpPtr.Ip,
1817 &((DHCPV4_OP_SERVER_IP *) DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1])->Ip,
1818 sizeof (EFI_IPv4_ADDRESS)
1819 )) {
1820 continue;
1821 }
1822 //
1823 // got ACK
1824 // check with ARP that IP unused - good return true
1825 //
1826 if (!SetStationIP (Private)) {
1827 //
1828 // fail - send DHCPDECLINE and return false
1829 //
1830 DeclineOffer (Private);
1831 break;
1832 }
1833
1834 LocalPtr.OpPtr = DHCPV4_ACK_BUFFER.OpAdds.PktOptAdds[OP_SUBNET_MASK_IX - 1];
1835
1836 if (LocalPtr.OpPtr != NULL) {
1837 CopyMem (
1838 (EFI_IPv4_ADDRESS *) &Private->EfiBc.Mode->SubnetMask,
1839 &LocalPtr.SubnetMaskStr->Ip,
1840 sizeof (EFI_IPv4_ADDRESS)
1841 );
1842 }
1843
1844 AddRouters (Private, DhcpRxBuf);
1845 gBS->CloseEvent (TimeoutEvent);
1846 return TRUE;
1847 }
1848
1849 gBS->CloseEvent (TimeoutEvent);
1850 return FALSE;
1851 }
1852
1853 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1854
1855 //
1856 // try a DHCP server of appropriate type
1857 //
1858 STATIC
1859 BOOLEAN
1860 TryDHCPFinishDORA (
1861 PXE_BASECODE_DEVICE *Private,
1862 INTN TypeIx
1863 )
1864 {
1865 INTN Index;
1866
1867 //
1868 // go through the DHCP servers of the requested type
1869 //
1870 for (Index = 0; Index < Private->ServerCount[TypeIx]; ++Index) {
1871 if (TryFinishDORA (Private, Index = Private->OfferCount[TypeIx][Index])) {
1872 if (TypeIx == BINL_IX && !TryFinishBINL (Private, Index)) {
1873 continue;
1874 }
1875
1876 return TRUE;
1877 }
1878 }
1879
1880 return FALSE;
1881 }
1882
1883 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1884
1885 //
1886 // try a DHCP only server and a proxy of appropriate type
1887 //
1888 STATIC
1889 BOOLEAN
1890 TryProxyFinishDORA (
1891 PXE_BASECODE_DEVICE *Private,
1892 INTN TypeIx
1893 )
1894 {
1895 INTN Index;
1896
1897 if (!Private->GotProxy[TypeIx]) {
1898 //
1899 // no proxies of the type wanted
1900 //
1901 return FALSE;
1902 }
1903 //
1904 // go through the DHCP only servers
1905 //
1906 for (Index = 0; Index < Private->ServerCount[DHCP_ONLY_IX]; ++Index) {
1907 if (TryFinishDORA (Private, Private->OfferCount[DHCP_ONLY_IX][Index])) {
1908 if (TypeIx != BINL_IX) {
1909 CopyProxyRxBuf (Private, Private->GotProxy[TypeIx] - 1);
1910 } else if (!TryFinishProxyBINL (Private)) {
1911 //
1912 // if didn't work with this DHCP, won't work with any
1913 //
1914 return FALSE;
1915 }
1916
1917 return TRUE;
1918 }
1919 }
1920
1921 return FALSE;
1922 }
1923
1924 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1925
1926 //
1927 // getting to the bottom of the barrel
1928 //
1929 STATIC
1930 BOOLEAN
1931 TryAnyWithBootfileFinishDORA (
1932 PXE_BASECODE_DEVICE *Private
1933 )
1934 {
1935 //
1936 // try a DHCP only server who has a bootfile
1937 //
1938 UNION_PTR LocalPtr;
1939 INTN Index;
1940
1941 for (Index = 0; Index < Private->ServerCount[DHCP_ONLY_IX]; ++Index) {
1942 INTN offer;
1943
1944 offer = Private->OfferCount[DHCP_ONLY_IX][Index];
1945
1946 if (RxBuf[offer].OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1] && TryFinishDORA (Private, offer)) {
1947 return TRUE;
1948 }
1949 }
1950 //
1951 // really at bottom - see if be have any bootps
1952 //
1953 if (!Private->GotBootp) {
1954 return FALSE;
1955 }
1956
1957 DHCP_REQ_OPTIONS.OpReqIP.Ip = *(EFI_IPv4_ADDRESS *) &RxBuf[Private->GotBootp - 1].u.Dhcpv4.yiaddr;
1958
1959 if (!SetStationIP (Private)) {
1960 return FALSE;
1961 }
1962 //
1963 // treat BOOTP response as DHCP ACK packet
1964 //
1965 CopyParseRxBuf (Private, Private->GotBootp - 1, DHCPV4_ACK_INDEX);
1966
1967 LocalPtr.OpPtr = RxBuf[Private->GotBootp - 1].OpAdds.PktOptAdds[OP_SUBNET_MASK_IX - 1];
1968
1969 if (LocalPtr.OpPtr != NULL) {
1970 *(EFI_IPv4_ADDRESS *) &Private->EfiBc.Mode->SubnetMask = LocalPtr.SubnetMaskStr->Ip;
1971 }
1972
1973 return TRUE;
1974 }
1975
1976 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1977
1978 /* DoDhcpDora()
1979 */
1980 STATIC
1981 EFI_STATUS
1982 DoDhcpDora (
1983 PXE_BASECODE_DEVICE *Private,
1984 BOOLEAN SortOffers
1985 )
1986 {
1987 EFI_PXE_BASE_CODE_IP_FILTER Filter;
1988 EFI_STATUS StatCode;
1989 INTN NumOffers;
1990
1991 Filter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP | EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST;
1992
1993 Filter.IpCnt = 0;
1994 Filter.reserved = 0;
1995
1996 //
1997 // set filter unicast or broadcast
1998 //
1999 if ((StatCode = IpFilter (Private, &Filter)) != EFI_SUCCESS) {
2000 return StatCode;
2001 }
2002 //
2003 // seed random number with hardware address
2004 //
2005 SeedRandom (Private, *(UINT16 *) &Private->SimpleNetwork->Mode->CurrentAddress);
2006
2007 for (Private->Timeout = 1;
2008 Private->Timeout < 17;
2009 Private->TotalSeconds = (UINT16) (Private->TotalSeconds + Private->Timeout), Private->Timeout <<= 1
2010 ) {
2011 INTN Index;
2012
2013 InitDhcpv4TxBuf (Private);
2014 DHCPV4_TRANSMIT_BUFFER.xid = Random (Private);
2015 DHCPV4_TRANSMIT_BUFFER.secs = HTONS (Private->TotalSeconds);
2016
2017 //
2018 // broadcast DHCPDISCOVER
2019 //
2020 StatCode = DoUdpWrite (
2021 Private,
2022 &BroadcastIP,
2023 &DhcpServerPort,
2024 (EFI_IP_ADDRESS *) &DHCPV4_TRANSMIT_BUFFER.ciaddr,
2025 &DHCPClientPort
2026 );
2027
2028 if (StatCode != EFI_SUCCESS) {
2029 return StatCode;
2030 }
2031
2032 CopyMem (
2033 &Private->EfiBc.Mode->DhcpDiscover,
2034 (EFI_PXE_BASE_CODE_PACKET *) &DHCPV4_TRANSMIT_BUFFER,
2035 sizeof (EFI_PXE_BASE_CODE_PACKET)
2036 );
2037
2038 //
2039 // get DHCPOFFER's
2040 //
2041 if ((StatCode = GetOffers (Private)) != EFI_SUCCESS) {
2042 if (StatCode != EFI_NO_RESPONSE) {
2043 return StatCode;
2044 }
2045
2046 continue;
2047 }
2048 //
2049 // select offer and reply DHCPREQUEST
2050 //
2051 if (SortOffers) {
2052 if (TryDHCPFinishDORA(Private, PXE10_IX) || // try DHCP with PXE10
2053 TryDHCPFinishDORA(Private, WfM11a_IX) || // no - try with WfM
2054 TryProxyFinishDORA(Private, PXE10_IX) || // no - try DHCP only and proxy with PXE10
2055 TryProxyFinishDORA(Private, WfM11a_IX) || // no - try DHCP only and proxy with WfM
2056 TryDHCPFinishDORA(Private, BINL_IX) || // no - try with WfM
2057 TryProxyFinishDORA(Private, BINL_IX) || // no - try DHCP only and proxy with PXE10
2058 TryAnyWithBootfileFinishDORA(Private))
2059 {
2060 return EFI_SUCCESS;
2061 }
2062
2063 continue;
2064 }
2065 //
2066 // FIFO order
2067 //
2068 NumOffers = Private->NumOffersReceived;
2069
2070 for (Index = 0; Index < NumOffers; ++Index) {
2071 //
2072 // ignore proxies
2073 //
2074 if (!RxBuf[Index].u.Dhcpv4.yiaddr) {
2075 continue;
2076 }
2077 //
2078 // check if a bootp server
2079 //
2080 if (!RxBuf[Index].OpAdds.PktOptAdds[OP_DHCP_MESSAGE_TYPE_IX - 1]) {
2081 //
2082 // it is - just check ARP
2083 //
2084 if (!SetStationIP (Private)) {
2085 continue;
2086 }
2087 }
2088 //
2089 // else check if a DHCP only server
2090 //
2091 else if (!(RxBuf[Index].OpAdds.Status & (DISCOVER_TYPE | WfM11a_TYPE | PXE_TYPE))) {
2092 //
2093 // it is a normal DHCP offer (without any PXE options), just finish the D.O.R.A by sending DHCP request.
2094 //
2095 if (!TryFinishDORA (Private, Index)) {
2096 continue;
2097 }
2098 } else if (TryFinishDORA (Private, Index)) {
2099 if (!(RxBuf[Index].OpAdds.Status & (DISCOVER_TYPE | WfM11a_TYPE)) && !TryFinishBINL (Private, Index)) {
2100 continue;
2101 }
2102 }
2103
2104 DEBUG ((EFI_D_WARN, "\nDoDhcpDora() Got packets. "));
2105 return EFI_SUCCESS;
2106 }
2107 //
2108 // now look for DHCP onlys and a Proxy
2109 //
2110 for (Index = 0; Index < NumOffers; ++Index) {
2111 UINT8 Index2;
2112
2113 //
2114 // ignore proxies, bootps, non DHCP onlys, and bootable DHCPS
2115 //
2116 if (!RxBuf[Index].u.Dhcpv4.yiaddr ||
2117 !RxBuf[Index].OpAdds.PktOptAdds[OP_DHCP_MESSAGE_TYPE_IX - 1] ||
2118 RxBuf[Index].OpAdds.Status & (DISCOVER_TYPE | WfM11a_TYPE | PXE_TYPE) ||
2119 RxBuf[Index].OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1]
2120 ) {
2121 continue;
2122 }
2123 //
2124 // found non bootable DHCP only - try to find a proxy
2125 //
2126 for (Index2 = 0; Index2 < NumOffers; ++Index2) {
2127 if (!RxBuf[Index2].u.Dhcpv4.yiaddr) {
2128 if (!TryFinishDORA (Private, Index)) {
2129 //
2130 // DHCP no ACK
2131 //
2132 break;
2133 }
2134
2135 if (RxBuf[Index2].OpAdds.Status & (DISCOVER_TYPE | WfM11a_TYPE)) {
2136 CopyProxyRxBuf (Private, Index2);
2137 } else if (!TryFinishBINL (Private, Index2)) {
2138 continue;
2139 }
2140
2141 DEBUG ((EFI_D_WARN, "\nDoDhcpDora() Got packets. "));
2142 return EFI_SUCCESS;
2143 }
2144 }
2145 }
2146 }
2147
2148 return EFI_NO_RESPONSE;
2149 }
2150
2151 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2152
2153 //
2154 // determine if the server ip is in the ip list
2155 //
2156 BOOLEAN
2157 InServerList (
2158 EFI_IP_ADDRESS *ServerIpPtr,
2159 PXE_SERVER_LISTS *ServerListPtr
2160 )
2161 {
2162 UINTN Index;
2163
2164 if (!ServerListPtr || !ServerListPtr->Ipv4List.IpCount) {
2165 return TRUE;
2166 }
2167
2168 for (Index = 0; Index < ServerListPtr->Ipv4List.IpCount; ++Index) {
2169 if (!CompareMem (
2170 ServerIpPtr,
2171 &ServerListPtr->Ipv4List.IpList[Index],
2172 sizeof (EFI_IPv4_ADDRESS)
2173 )) {
2174 return TRUE;
2175 }
2176 }
2177
2178 return FALSE;
2179 }
2180
2181 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2182 BOOLEAN
2183 ExtractBootServerList (
2184 UINT16 Type,
2185 DHCPV4_OP_STRUCT *ptr,
2186 PXE_SERVER_LISTS **ServerListPtr
2187 )
2188 {
2189 UNION_PTR LocalPtr;
2190 INTN ServerListLen;
2191
2192 LocalPtr.OpPtr = ptr;
2193 ServerListLen = LocalPtr.BootServersStr->Header.Length;
2194
2195 //
2196 // find type
2197 //
2198 LocalPtr.BootServerList = LocalPtr.BootServersStr->ServerList;
2199
2200 while (ServerListLen) {
2201 INTN ServerEntryLen;
2202
2203 ServerEntryLen = sizeof (PXEV4_SERVER_LIST) + 2 + (LocalPtr.BootServerList->u.Ipv4List.IpCount - 1) *
2204 sizeof (EFI_IPv4_ADDRESS);
2205
2206 if (NTOHS (LocalPtr.BootServerList->Type) == Type) {
2207 *ServerListPtr = &LocalPtr.BootServerList->u;
2208 return TRUE;
2209 }
2210
2211 (LocalPtr.BytePtr) += ServerEntryLen;
2212 ServerListLen -= ServerEntryLen;
2213 }
2214
2215 return FALSE;
2216 }
2217
2218 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2219 VOID
2220 FreeMem (
2221 PXE_BASECODE_DEVICE *Private
2222 )
2223 {
2224 if (Private->TransmitBuffer != NULL) {
2225 gBS->FreePool (Private->TransmitBuffer);
2226 Private->TransmitBuffer = NULL;
2227 }
2228
2229 if (Private->ReceiveBuffers != NULL) {
2230 gBS->FreePool (Private->ReceiveBuffers);
2231 Private->ReceiveBuffers = NULL;
2232 }
2233 }
2234
2235 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2236 BOOLEAN
2237 GetMem (
2238 PXE_BASECODE_DEVICE *Private
2239 )
2240 {
2241 EFI_STATUS Status;
2242
2243 if (Private->DhcpPacketBuffer == NULL) {
2244 Status = gBS->AllocatePool (
2245 EfiBootServicesData,
2246 sizeof (DHCP_RECEIVE_BUFFER) * (PXE_BIS_INDEX + 1),
2247 &Private->DhcpPacketBuffer
2248 );
2249
2250 if (EFI_ERROR (Status) || Private->DhcpPacketBuffer == NULL) {
2251 Private->DhcpPacketBuffer = NULL;
2252 FreeMem (Private);
2253 return FALSE;
2254 }
2255 }
2256
2257 Status = gBS->AllocatePool (
2258 EfiBootServicesData,
2259 sizeof (EFI_PXE_BASE_CODE_PACKET),
2260 &Private->TransmitBuffer
2261 );
2262
2263 if (EFI_ERROR (Status) || Private->TransmitBuffer == NULL) {
2264 gBS->FreePool (Private->DhcpPacketBuffer);
2265 Private->DhcpPacketBuffer = NULL;
2266 Private->TransmitBuffer = NULL;
2267 FreeMem (Private);
2268 return FALSE;
2269 }
2270
2271 Status = gBS->AllocatePool (
2272 EfiBootServicesData,
2273 sizeof (DHCP_RECEIVE_BUFFER) * (MAX_OFFERS),
2274 &Private->ReceiveBuffers
2275 );
2276
2277 if (EFI_ERROR (Status) || Private->ReceiveBuffers == NULL) {
2278 gBS->FreePool (Private->TransmitBuffer);
2279 gBS->FreePool (Private->DhcpPacketBuffer);
2280 Private->DhcpPacketBuffer = NULL;
2281 Private->TransmitBuffer = NULL;
2282 Private->ReceiveBuffers = NULL;
2283 FreeMem (Private);
2284 return FALSE;
2285 }
2286
2287 return TRUE;
2288 }
2289
2290 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2291 EFI_STATUS
2292 EFIAPI
2293 BcDhcp (
2294 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
2295 IN BOOLEAN SortOffers
2296 )
2297 /*++
2298 Routine description:
2299 standard DHCP Discover/Offer/Request/Ack session
2300 broadcast DHCPDISCOVER
2301 receive DHCPOFFER's
2302 broadcast DHCPREQUEST
2303 receive DHCPACK
2304 check (ARP) good IP
2305
2306 Parameters:
2307 This := Pointer to PxeBc interface
2308 SortOffers :=
2309
2310 Returns:
2311 --*/
2312 {
2313 EFI_PXE_BASE_CODE_IP_FILTER Filter;
2314 EFI_PXE_BASE_CODE_MODE *PxebcMode;
2315 PXE_BASECODE_DEVICE *Private;
2316 EFI_STATUS StatCode;
2317
2318 //
2319 // Lock the instance data and make sure started
2320 //
2321 StatCode = EFI_SUCCESS;
2322
2323 if (This == NULL) {
2324 DEBUG ((EFI_D_ERROR, "BC *This pointer == NULL"));
2325 return EFI_INVALID_PARAMETER;
2326 }
2327
2328 Private = CR (This, PXE_BASECODE_DEVICE, EfiBc, PXE_BASECODE_DEVICE_SIGNATURE);
2329
2330 if (Private == NULL) {
2331 DEBUG ((EFI_D_ERROR, "PXE_BASECODE_DEVICE pointer == NULL"));
2332 return EFI_INVALID_PARAMETER;
2333 }
2334
2335 EfiAcquireLock (&Private->Lock);
2336
2337 if (This->Mode == NULL || !This->Mode->Started) {
2338 DEBUG ((EFI_D_ERROR, "BC was not started."));
2339 EfiReleaseLock (&Private->Lock);
2340 return EFI_NOT_STARTED;
2341 }
2342
2343 Filter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;
2344 Filter.IpCnt = 0;
2345 Filter.reserved = 0;
2346
2347 DEBUG ((EFI_D_INFO, "\nBcDhcp() Enter. "));
2348
2349 PxebcMode = Private->EfiBc.Mode;
2350
2351 if (!GetMem (Private)) {
2352 DEBUG ((EFI_D_ERROR, "\nBcDhcp() GetMem() failed.\n"));
2353 EfiReleaseLock (&Private->Lock);
2354 return EFI_OUT_OF_RESOURCES;
2355 }
2356
2357 PxebcMode->DhcpDiscoverValid = FALSE;
2358 PxebcMode->DhcpAckReceived = FALSE;
2359 PxebcMode->ProxyOfferReceived = FALSE;
2360
2361 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;
2362
2363 //
2364 // Issue BC command
2365 //
2366 if (Private->TotalSeconds == 0) {
2367 //
2368 // put in seconds field of DHCP send packets
2369 //
2370 Private->TotalSeconds = 4;
2371 }
2372
2373 if ((StatCode = DoDhcpDora (Private, SortOffers)) == EFI_SUCCESS) {
2374 //
2375 // success - copy packets
2376 //
2377 PxebcMode->DhcpDiscoverValid = PxebcMode->DhcpAckReceived = TRUE;
2378
2379 CopyMem (
2380 &PxebcMode->DhcpAck,
2381 (EFI_PXE_BASE_CODE_PACKET *) &DHCPV4_ACK_PACKET,
2382 sizeof (EFI_PXE_BASE_CODE_PACKET)
2383 );
2384
2385 if (PxebcMode->ProxyOfferReceived) {
2386 CopyMem (
2387 &PxebcMode->ProxyOffer,
2388 (EFI_PXE_BASE_CODE_PACKET *) &PXE_OFFER_PACKET,
2389 sizeof (EFI_PXE_BASE_CODE_PACKET)
2390 );
2391 }
2392 }
2393 //
2394 // set filter back to unicast
2395 //
2396 IpFilter (Private, &Filter);
2397
2398 FreeMem (Private);
2399
2400 //
2401 // Unlock the instance data
2402 //
2403 DEBUG ((EFI_D_WARN, "\nBcDhcp() Exit = %xh ", StatCode));
2404
2405 EfiReleaseLock (&Private->Lock);
2406 return StatCode;
2407 }
2408
2409 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2410 STATIC
2411 BOOLEAN
2412 VerifyCredentialOption (
2413 UINT8 *tx,
2414 UINT8 *rx
2415 )
2416 {
2417 UINTN n;
2418
2419 //
2420 // Fail verification if either pointer is NULL.
2421 //
2422 if (tx == NULL || rx == NULL) {
2423 return FALSE;
2424 }
2425 //
2426 // Fail verification if tx[0] is not a credential type option
2427 // or if the length is zero or not a multiple of four.
2428 //
2429 if (tx[0] != VEND_PXE_CREDENTIAL_TYPES || tx[1] == 0 || tx[1] % 4 != 0) {
2430 return FALSE;
2431 }
2432 //
2433 // Fail verification if rx[0] is not a credential type option
2434 // or if the length is not equal to four.
2435 //
2436 if (rx[0] != VEND_PXE_CREDENTIAL_TYPES || rx[1] != 4) {
2437 return FALSE;
2438 }
2439 //
2440 // Look through transmitted credential types for a copy
2441 // of the received credential type.
2442 //
2443 for (n = 0; n < tx[1]; n += 4) {
2444 if (!CompareMem (&tx[n + 2], &rx[2], 4)) {
2445 return TRUE;
2446 }
2447 }
2448
2449 return FALSE;
2450 }
2451
2452 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2453 EFI_STATUS
2454 DoDiscover (
2455 PXE_BASECODE_DEVICE *Private,
2456 UINT16 OpFlags,
2457 IN UINT16 Type,
2458 IN UINT16 *LayerPtr,
2459 IN BOOLEAN UseBis,
2460 EFI_IP_ADDRESS *DestPtr,
2461 PXE_SERVER_LISTS *ServerListPtr
2462 )
2463 /*++
2464 Routine description:
2465 This function tries to complete the PXE Bootserver and/or boot image
2466 discovery sequence. When this command completes successfully, the
2467 PXEdiscover and PXEreply fields in the BC instance data structure are
2468 updated. If the Info pointer is set to NULL, the discovery information
2469 in the DHCPack and ProxyOffer packets must be valid and will be used.
2470 If Info is not set to NULL, the discovery methods in the Info field
2471 must be set and will be used. When discovering any layer number other
2472 than zero (the credential flag does not count), only unicast discovery
2473 is used.
2474
2475 Parameters:
2476 Private := Pointer to PxeBc interface
2477 OpFlags :=
2478 Type :=
2479 LayerPtr :=
2480 UseBis :=
2481 DestPtr :=
2482 ServerListPtr :=
2483
2484 Returns:
2485 --*/
2486 {
2487 EFI_PXE_BASE_CODE_UDP_PORT ClientPort;
2488 EFI_PXE_BASE_CODE_UDP_PORT ServerPort;
2489 EFI_PXE_BASE_CODE_MODE *PxebcMode;
2490 EFI_STATUS StatCode;
2491 EFI_EVENT TimeoutEvent;
2492 UINT8 OpLen;
2493
2494 PxebcMode = Private->EfiBc.Mode;
2495
2496 if (DestPtr->Addr[0] == 0) {
2497 DEBUG ((EFI_D_WARN, "\nDoDiscover() !DestPtr->Addr[0]"));
2498 return EFI_INVALID_PARAMETER;
2499 }
2500 //
2501 // seed random number with hardware address
2502 //
2503 SeedRandom (Private, *(UINT16 *) &Private->SimpleNetwork->Mode->CurrentAddress);
2504
2505 if (DestPtr->Addr[0] == BroadcastIP.Addr[0]) {
2506 ClientPort = DHCPClientPort;
2507 ServerPort = DhcpServerPort;
2508 } else {
2509 ClientPort = PSEUDO_DHCP_CLIENT_PORT;
2510 ServerPort = PseudoDhcpServerPort;
2511 }
2512
2513 if (UseBis) {
2514 *LayerPtr |= PXE_BOOT_LAYER_CREDENTIAL_FLAG;
2515 } else {
2516 *LayerPtr &= PXE_BOOT_LAYER_MASK;
2517 }
2518
2519 for (Private->Timeout = 1;
2520 Private->Timeout < 5;
2521 Private->TotalSeconds = (UINT16) (Private->TotalSeconds + Private->Timeout), ++Private->Timeout
2522 ) {
2523 InitDhcpv4TxBuf (Private);
2524 //
2525 // initialize DHCP message structure
2526 //
2527 DHCPV4_TRANSMIT_BUFFER.xid = Random (Private);
2528 DHCPV4_TRANSMIT_BUFFER.secs = HTONS (Private->TotalSeconds);
2529 CopyMem (
2530 &DHCPV4_TRANSMIT_BUFFER.ciaddr,
2531 &PxebcMode->StationIp,
2532 sizeof DHCPV4_TRANSMIT_BUFFER.ciaddr
2533 );
2534
2535 DHCPV4_OPTIONS_BUFFER.DhcpMessageType.Type = DHCPREQUEST;
2536 DISCOVERoptions.Header.OpCode = OP_VENDOR_SPECIFIC;
2537 DISCOVERoptions.BootItem.Header.OpCode = VEND_PXE_BOOT_ITEM;
2538 DISCOVERoptions.BootItem.Header.Length = DHCPV4_OPTION_LENGTH (PXE_OP_BOOT_ITEM);
2539 DISCOVERoptions.BootItem.Type = HTONS (Type);
2540 DISCOVERoptions.BootItem.Layer = HTONS (*LayerPtr);
2541
2542 if (UseBis) {
2543 EFI_BIS_PROTOCOL *BisPtr;
2544 BIS_APPLICATION_HANDLE BisAppHandle;
2545 EFI_BIS_DATA *BisDataSigInfo;
2546 EFI_BIS_SIGNATURE_INFO *BisSigInfo;
2547 UINTN Index;
2548 UINTN Index2;
2549
2550 BisPtr = PxebcBisStart (
2551 Private,
2552 &BisAppHandle,
2553 &BisDataSigInfo
2554 );
2555
2556 if (BisPtr == NULL) {
2557 //
2558 // %%TBD - In order to get here, BIS must have
2559 // been present when PXEBC.Start() was called.
2560 // BIS had to be shutdown/removed/damaged
2561 // before PXEBC.Discover() was called.
2562 // Do we need to document a specific error
2563 // for this case?
2564 //
2565 return EFI_OUT_OF_RESOURCES;
2566 }
2567 //
2568 // Compute number of credential types.
2569 //
2570 Index2 = BisDataSigInfo->Length / sizeof (EFI_BIS_SIGNATURE_INFO);
2571
2572 DISCREDoptions.Header.OpCode = VEND_PXE_CREDENTIAL_TYPES;
2573
2574 DISCREDoptions.Header.Length = (UINT8) (Index2 * sizeof (PXE_CREDENTIAL));
2575
2576 OpLen = (UINT8) (DHCPV4_OPTION_LENGTH (PXE_DISCOVER_OPTIONS) + sizeof (DHCPV4_OP_HEADER) + DISCREDoptions.Header.Length);
2577
2578 BisSigInfo = (EFI_BIS_SIGNATURE_INFO *) BisDataSigInfo->Data;
2579
2580 for (Index = 0; Index < Index2; ++Index) {
2581 UINT32 x;
2582
2583 CopyMem (&x, &BisSigInfo[Index], sizeof x);
2584 x = HTONL (x);
2585 CopyMem (&DISCREDoptions.Credentials[Index], &x, sizeof x);
2586 }
2587
2588 PxebcBisStop (BisPtr, BisAppHandle, BisDataSigInfo);
2589 } else {
2590 OpLen = DHCPV4_OPTION_LENGTH (PXE_DISCOVER_OPTIONS);
2591 }
2592
2593 DISCOVERoptions.Header.Length = OpLen;
2594
2595 ((UINT8 *) &DISCOVERoptions)[sizeof (DHCPV4_OP_HEADER) + OpLen - 1] = OP_END;
2596 ((UINT8 *) &DISCOVERoptions)[sizeof (DHCPV4_OP_HEADER) + OpLen] = OP_END;
2597
2598 StatCode = DoUdpWrite (
2599 Private,
2600 DestPtr,
2601 &ServerPort,
2602 (EFI_IP_ADDRESS *) &DHCPV4_TRANSMIT_BUFFER.ciaddr,
2603 &ClientPort
2604 );
2605
2606 if (StatCode != EFI_SUCCESS) {
2607 return StatCode;
2608 }
2609 //
2610 //
2611 //
2612 StatCode = gBS->CreateEvent (
2613 EFI_EVENT_TIMER,
2614 EFI_TPL_CALLBACK,
2615 NULL,
2616 NULL,
2617 &TimeoutEvent
2618 );
2619
2620 if (EFI_ERROR (StatCode)) {
2621 return StatCode;
2622 }
2623
2624 StatCode = gBS->SetTimer (
2625 TimeoutEvent,
2626 TimerRelative,
2627 Private->Timeout * 10000000 + 1000000
2628 );
2629
2630 if (EFI_ERROR (StatCode)) {
2631 gBS->CloseEvent (TimeoutEvent);
2632 return StatCode;
2633 }
2634 //
2635 // wait for ACK
2636 //
2637 for (;;) {
2638 DHCP_RECEIVE_BUFFER *RxBufPtr;
2639 UINT16 TmpType;
2640 UINT16 TmpLayer;
2641
2642 RxBufPtr = UseBis ? &PXE_BIS_BUFFER : &PXE_ACK_BUFFER;
2643 ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));
2644
2645 if (GetOfferAck (
2646 Private,
2647 AckEdit,
2648 OpFlags,
2649 (EFI_IP_ADDRESS *) &Private->ServerIp,
2650 0,
2651 (EFI_IP_ADDRESS *) &DHCPV4_TRANSMIT_BUFFER.ciaddr,
2652 &ClientPort,
2653 RxBufPtr,
2654 TimeoutEvent
2655 ) != EFI_SUCCESS) {
2656 break;
2657 }
2658 //
2659 // check type of response - need PXEClient DHCPACK of proper type with bootfile
2660 //
2661 if (!(RxBufPtr->OpAdds.Status & PXE_TYPE) ||
2662 (UseBis && (RxBufPtr->OpAdds.Status & USE_THREE_BYTE)) ||
2663 !RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1] ||
2664 !RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1] ||
2665 !InServerList((EFI_IP_ADDRESS *)&((DHCPV4_OP_SERVER_IP *)RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX-1])->Ip, ServerListPtr)) {
2666
2667 continue;
2668 }
2669
2670 TmpType = TmpLayer = 0;
2671
2672 if (RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1]) {
2673 TmpType = NTOHS (((PXE_OP_BOOT_ITEM *) RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1])->Type);
2674
2675 if (RxBufPtr->OpAdds.Status & USE_THREE_BYTE) {
2676 TmpLayer = (UINT16) (((PXE_OP_BOOT_ITEM *) RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1])->Layer >> 8);
2677 } else {
2678 TmpLayer = NTOHS (((PXE_OP_BOOT_ITEM *) RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1])->Layer);
2679 }
2680 }
2681
2682 if (TmpType != Type) {
2683 continue;
2684 }
2685
2686 if (UseBis) {
2687 if (!RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_CREDENTIAL_TYPES_IX - 1]) {
2688 continue;
2689 }
2690
2691 if (!VerifyCredentialOption (
2692 (UINT8 *) &DISCREDoptions.Header,
2693 (UINT8 *) RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_CREDENTIAL_TYPES_IX - 1]
2694 )) {
2695 continue;
2696 }
2697 }
2698
2699 *LayerPtr = TmpLayer;
2700
2701 if (UseBis) {
2702 CopyMem (
2703 &PxebcMode->PxeBisReply,
2704 &RxBufPtr->u.Dhcpv4,
2705 sizeof (EFI_PXE_BASE_CODE_PACKET)
2706 );
2707
2708 PxebcMode->PxeBisReplyReceived = TRUE;
2709
2710 StatCode = DoDiscover (
2711 Private,
2712 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
2713 Type,
2714 LayerPtr,
2715 FALSE,
2716 &Private->ServerIp,
2717 0
2718 );
2719
2720 gBS->CloseEvent (TimeoutEvent);
2721 return StatCode;
2722 }
2723
2724 PxebcMode->PxeDiscoverValid = PxebcMode->PxeReplyReceived = TRUE;
2725
2726 CopyMem (
2727 &PxebcMode->PxeDiscover,
2728 &*(EFI_PXE_BASE_CODE_PACKET *) &DHCPV4_TRANSMIT_BUFFER,
2729 sizeof (*(EFI_PXE_BASE_CODE_PACKET *) &DHCPV4_TRANSMIT_BUFFER)
2730 );
2731
2732 CopyMem (
2733 &PxebcMode->PxeReply,
2734 &*(EFI_PXE_BASE_CODE_PACKET *) &RxBufPtr->u.Dhcpv4,
2735 sizeof (*(EFI_PXE_BASE_CODE_PACKET *) &RxBufPtr->u.Dhcpv4)
2736 );
2737
2738 AddRouters (Private, RxBufPtr);
2739
2740 gBS->CloseEvent (TimeoutEvent);
2741 return EFI_SUCCESS;
2742 }
2743
2744 gBS->CloseEvent (TimeoutEvent);
2745 }
2746 //
2747 // end for loop
2748 //
2749 return EFI_TIMEOUT;
2750 }
2751
2752 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2753 STATIC
2754 EFI_STATUS
2755 Discover (
2756 PXE_BASECODE_DEVICE *Private,
2757 IN UINT16 Type,
2758 IN UINT16 *LayerPtr,
2759 IN BOOLEAN UseBis,
2760 IN EFI_PXE_BASE_CODE_DISCOVER_INFO *DiscoverInfoPtr,
2761 PXE_SERVER_LISTS *McastServerListPtr,
2762 PXE_SERVER_LISTS *ServerListPtr
2763 )
2764 /*++
2765 Routine Description:
2766
2767 Parameters:
2768 Private := Pointer to PxeBc interface
2769 Type :=
2770 LayerPtr :=
2771 UseBis :=
2772 DiscoverInfoPtr :=
2773 McastServerListPtr :=
2774 ServerListPtr :=
2775
2776 Returns:
2777 --*/
2778 {
2779 EFI_IP_ADDRESS DestIp;
2780 EFI_STATUS StatCode;
2781
2782 DEBUG ((EFI_D_INFO, "\nDiscover() Type=%d Layer=%d ", Type, *LayerPtr));
2783
2784 if (UseBis) {
2785 DEBUG ((EFI_D_INFO, "BIS "));
2786 }
2787 //
2788 // get dest IP addr - mcast, bcast, or unicast
2789 //
2790 if (DiscoverInfoPtr->UseMCast) {
2791 DestIp.v4 = DiscoverInfoPtr->ServerMCastIp.v4;
2792
2793 DEBUG (
2794 (EFI_D_INFO,
2795 "\nDiscover() MCast %d.%d.%d.%d ",
2796 DestIp.v4.Addr[0],
2797 DestIp.v4.Addr[1],
2798 DestIp.v4.Addr[2],
2799 DestIp.v4.Addr[3])
2800 );
2801
2802 if ((StatCode = DoDiscover (
2803 Private,
2804 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
2805 Type,
2806 LayerPtr,
2807 UseBis,
2808 &DestIp,
2809 McastServerListPtr
2810 )) != EFI_TIMEOUT) {
2811 DEBUG (
2812 (EFI_D_WARN,
2813 "\nDiscover() status == %r (%Xh)",
2814 StatCode,
2815 StatCode)
2816 );
2817
2818 return StatCode;
2819 }
2820 }
2821
2822 if (DiscoverInfoPtr->UseBCast) {
2823 DEBUG ((EFI_D_INFO, "\nDiscver() BCast "));
2824
2825 if ((StatCode = DoDiscover (
2826 Private,
2827 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
2828 Type,
2829 LayerPtr,
2830 UseBis,
2831 &BroadcastIP,
2832 McastServerListPtr
2833 )) != EFI_TIMEOUT) {
2834
2835 DEBUG ((EFI_D_WARN, "\nDiscover() status == %r (%Xh)", StatCode, StatCode));
2836
2837 return StatCode;
2838 }
2839 }
2840
2841 if (DiscoverInfoPtr->UseUCast) {
2842 UINTN Index;
2843
2844 DEBUG (
2845 (EFI_D_INFO,
2846 "\nDiscover() UCast IP#=%d ",
2847 ServerListPtr->Ipv4List.IpCount)
2848 );
2849
2850 for (Index = 0; Index < ServerListPtr->Ipv4List.IpCount; ++Index) {
2851 CopyMem (&DestIp, &ServerListPtr->Ipv4List.IpList[Index], 4);
2852
2853 DEBUG (
2854 (EFI_D_INFO,
2855 "\nDiscover() UCast %d.%d.%d.%d ",
2856 DestIp.v4.Addr[0],
2857 DestIp.v4.Addr[1],
2858 DestIp.v4.Addr[2],
2859 DestIp.v4.Addr[3])
2860 );
2861
2862 if ((StatCode = DoDiscover (
2863 Private,
2864 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
2865 Type,
2866 LayerPtr,
2867 UseBis,
2868 &DestIp,
2869 0
2870 )) != EFI_TIMEOUT) {
2871 DEBUG (
2872 (EFI_D_WARN,
2873 "\nDiscover() status == %r (%Xh)",
2874 StatCode,
2875 StatCode)
2876 );
2877
2878 return StatCode;
2879 }
2880 }
2881 }
2882
2883 DEBUG ((EFI_D_WARN, "\nDiscover() TIMEOUT"));
2884
2885 return EFI_TIMEOUT;
2886 }
2887
2888 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2889
2890 /* BcDiscover()
2891 */
2892 EFI_STATUS
2893 EFIAPI
2894 BcDiscover (
2895 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
2896 IN UINT16 Type,
2897 IN UINT16 *LayerPtr,
2898 IN BOOLEAN UseBis,
2899 IN EFI_PXE_BASE_CODE_DISCOVER_INFO * DiscoverInfoPtr OPTIONAL
2900 )
2901 /*++
2902 Routine description:
2903
2904 Parameters:
2905 This :=
2906 Type :=
2907 LayerPtr :=
2908 UseBis :=
2909 DiscoverInfoPtr :=
2910
2911 Returns:
2912 --*/
2913 {
2914 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;
2915 EFI_PXE_BASE_CODE_MODE *PxebcMode;
2916 DHCP_RECEIVE_BUFFER *DhcpRxBuf;
2917 PXE_SERVER_LISTS DefaultSrvList;
2918 PXE_SERVER_LISTS *ServerListPtr;
2919 PXE_SERVER_LISTS *McastServerListPtr;
2920 EFI_STATUS Status;
2921 UNION_PTR LocalPtr;
2922 UINTN Index;
2923 UINTN Index2;
2924 BOOLEAN AcquiredSrvList;
2925 EFI_STATUS StatCode;
2926 PXE_BASECODE_DEVICE *Private;
2927
2928 //
2929 // Lock the instance data and make sure started
2930 //
2931 StatCode = EFI_SUCCESS;
2932
2933 if (This == NULL) {
2934 DEBUG ((EFI_D_ERROR, "BC *This pointer == NULL"));
2935 return EFI_INVALID_PARAMETER;
2936 }
2937
2938 Private = CR (This, PXE_BASECODE_DEVICE, EfiBc, PXE_BASECODE_DEVICE_SIGNATURE);
2939
2940 if (Private == NULL) {
2941 DEBUG ((EFI_D_ERROR, "PXE_BASECODE_DEVICE poiner == NULL"));
2942 return EFI_INVALID_PARAMETER;
2943 }
2944
2945 EfiAcquireLock (&Private->Lock);
2946
2947 if (This->Mode == NULL || !This->Mode->Started) {
2948 DEBUG ((EFI_D_ERROR, "BC was not started."));
2949 EfiReleaseLock (&Private->Lock);
2950 return EFI_NOT_STARTED;
2951 }
2952
2953 ServerListPtr = NULL;
2954 McastServerListPtr = NULL;
2955 AcquiredSrvList = FALSE;
2956
2957 PxebcMode = Private->EfiBc.Mode;
2958
2959 if (!GetMem (Private)) {
2960 EfiReleaseLock (&Private->Lock);
2961 return EFI_OUT_OF_RESOURCES;
2962 }
2963
2964 if (UseBis) {
2965 if (!PxebcMode->BisSupported) {
2966 EfiReleaseLock (&Private->Lock);
2967 return EFI_INVALID_PARAMETER;
2968 }
2969 }
2970
2971 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;
2972
2973 if (Private->TotalSeconds == 0) {
2974 //
2975 // put in seconds field of DHCP send packets
2976 //
2977 Private->TotalSeconds = 4;
2978 }
2979
2980 ZeroMem (&DefaultInfo, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO));
2981
2982 //
2983 // if layer number not zero, use previous discover
2984 //
2985 if (*LayerPtr != 0) {
2986 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0"));
2987
2988 if (DiscoverInfoPtr != NULL) {
2989 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0 && DiscoverInfoPtr != NULL\n"));
2990
2991 EfiReleaseLock (&Private->Lock);
2992 return EFI_INVALID_PARAMETER;
2993 }
2994
2995 if (!PxebcMode->PxeDiscoverValid) {
2996 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0 && PxeDiscoverValid == 0\n"));
2997
2998 EfiReleaseLock (&Private->Lock);
2999 return EFI_INVALID_PARAMETER;
3000 }
3001
3002 if (!PxebcMode->PxeReplyReceived) {
3003 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0 && PxeReplyReceived == 0\n"));
3004
3005 EfiReleaseLock (&Private->Lock);
3006 return EFI_INVALID_PARAMETER;
3007 }
3008
3009 if (UseBis && !PxebcMode->PxeBisReplyReceived) {
3010 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0 && PxeBisReplyReceived == 0\n"));
3011
3012 EfiReleaseLock (&Private->Lock);
3013 return EFI_INVALID_PARAMETER;
3014 }
3015
3016 DefaultInfo.UseUCast = TRUE;
3017 DiscoverInfoPtr = &DefaultInfo;
3018
3019 DefaultSrvList.Ipv4List.IpCount = 1;
3020 CopyMem (&DefaultSrvList.Ipv4List.IpList[0], &Private->ServerIp, 4);
3021
3022 ServerListPtr = &DefaultSrvList;
3023 }
3024 //
3025 // layer is zero - see if info is supplied or if we need to use info from a cached offer
3026 //
3027 else if (!DiscoverInfoPtr) {
3028 //
3029 // not supplied - generate it
3030 // make sure that there is cached, appropriate information
3031 // if neither DhcpAck packet nor ProxyOffer packet has pxe info, fail
3032 //
3033 DhcpRxBuf = (PxebcMode->ProxyOfferReceived) ? &PXE_OFFER_BUFFER : &DHCPV4_ACK_BUFFER;
3034
3035 if (!PxebcMode->DhcpAckReceived || !(DhcpRxBuf->OpAdds.Status & DISCOVER_TYPE)) {
3036 DEBUG ((EFI_D_WARN, "\nBcDiscover() !ack && !proxy"));
3037 EfiReleaseLock (&Private->Lock);
3038 return EFI_INVALID_PARAMETER;
3039 }
3040
3041 DiscoverInfoPtr = &DefaultInfo;
3042
3043 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_CONTROL_IX - 1];
3044
3045 //
3046 // if multicast enabled, need multicast address
3047 //
3048 if (!(LocalPtr.DiscoveryControl->ControlBits & DISABLE_MCAST)) {
3049 DefaultInfo.UseMCast = TRUE;
3050
3051 CopyMem (
3052 ((EFI_IPv4_ADDRESS *) &DefaultInfo.ServerMCastIp),
3053 &((DHCPV4_OP_IP_ADDRESS *) DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_MCAST_ADDR_IX - 1])->Ip,
3054 sizeof (EFI_IPv4_ADDRESS)
3055 );
3056 }
3057
3058 DefaultInfo.UseBCast = (BOOLEAN) ((LocalPtr.DiscoveryControl->ControlBits & DISABLE_BCAST) == 0);
3059
3060 DefaultInfo.MustUseList = (BOOLEAN) ((LocalPtr.DiscoveryControl->ControlBits & USE_ACCEPT_LIST) != 0);
3061
3062 DefaultInfo.UseUCast = (BOOLEAN)
3063 (
3064 (DefaultInfo.MustUseList) ||
3065 ((LocalPtr.DiscoveryControl->ControlBits & (DISABLE_MCAST | DISABLE_BCAST)) == (DISABLE_MCAST | DISABLE_BCAST))
3066 );
3067
3068 if ((DefaultInfo.UseUCast | DefaultInfo.MustUseList) && !ExtractBootServerList (
3069 Type,
3070 DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_SERVERS_IX - 1],
3071 &ServerListPtr
3072 )) {
3073 DEBUG ((EFI_D_WARN, "\nBcDiscover() type not in list"));
3074 EfiReleaseLock (&Private->Lock);
3075 return EFI_INVALID_PARAMETER;
3076 }
3077 }
3078 //
3079 // Info supplied - make SrvList if required
3080 // if we use ucast discovery or must use list, there better be one
3081 //
3082 else if (DiscoverInfoPtr->UseUCast || DiscoverInfoPtr->MustUseList) {
3083 //
3084 // there better be a list
3085 //
3086 if (DiscoverInfoPtr->IpCnt == 0) {
3087 DEBUG ((EFI_D_WARN, "\nBcDiscover() no bootserver list"));
3088 EfiReleaseLock (&Private->Lock);
3089 return EFI_INVALID_PARAMETER;
3090 }
3091 //
3092 // get its size
3093 //
3094 for (Index = Index2 = 0; Index < DiscoverInfoPtr->IpCnt; ++Index) {
3095 if (DiscoverInfoPtr->SrvList[Index].Type == Type) {
3096 if (DiscoverInfoPtr->SrvList[Index].AcceptAnyResponse) {
3097 if (Index2 != 0) {
3098 DEBUG ((EFI_D_WARN, "\nBcDiscover() accept any?"));
3099 EfiReleaseLock (&Private->Lock);
3100 return EFI_INVALID_PARAMETER;
3101 } else {
3102 Index2 = 1;
3103 DefaultSrvList.Ipv4List.IpCount = 0;
3104 ServerListPtr = &DefaultSrvList;
3105 break;
3106 }
3107 } else {
3108 ++Index2;
3109 }
3110 }
3111 }
3112
3113 if (Index2 == 0) {
3114 DEBUG ((EFI_D_WARN, "\nBcDiscover() !Index2?"));
3115 EfiReleaseLock (&Private->Lock);
3116 return EFI_INVALID_PARAMETER;
3117 }
3118
3119 if (ServerListPtr == NULL) {
3120 Status = gBS->AllocatePool (
3121 EfiBootServicesData,
3122 sizeof (PXEV4_SERVER_LIST) + (Index2 - 1) * sizeof (EFI_IPv4_ADDRESS),
3123 (VOID **) &ServerListPtr
3124 );
3125
3126 if (EFI_ERROR (Status) || ServerListPtr == NULL) {
3127 ServerListPtr = NULL;
3128 EfiReleaseLock (&Private->Lock);
3129 return EFI_OUT_OF_RESOURCES;
3130 }
3131 //
3132 // build an array of IP addresses from the server list
3133 //
3134 AcquiredSrvList = TRUE;
3135 ServerListPtr->Ipv4List.IpCount = (UINT8) Index2;
3136
3137 for (Index = Index2 = 0; Index < DiscoverInfoPtr->IpCnt; ++Index) {
3138 if (DiscoverInfoPtr->SrvList[Index].Type == Type) {
3139 CopyMem (
3140 &ServerListPtr->Ipv4List.IpList[Index2++],
3141 &DiscoverInfoPtr->SrvList[Index].IpAddr.v4,
3142 sizeof ServerListPtr->Ipv4List.IpList[0]
3143 );
3144 }
3145 }
3146 }
3147 }
3148
3149 if (DiscoverInfoPtr->MustUseList) {
3150 McastServerListPtr = ServerListPtr;
3151 }
3152
3153 if (!(DiscoverInfoPtr->UseMCast || DiscoverInfoPtr->UseBCast || DiscoverInfoPtr->UseUCast)) {
3154 DEBUG ((EFI_D_WARN, "\nBcDiscover() Nothing to use!\n"));
3155
3156 EfiReleaseLock (&Private->Lock);
3157 return EFI_INVALID_PARAMETER;
3158 }
3159
3160 PxebcMode->PxeDiscoverValid = PxebcMode->PxeReplyReceived = PxebcMode->PxeBisReplyReceived = FALSE;
3161
3162 StatCode = Discover (
3163 Private,
3164 Type,
3165 LayerPtr,
3166 UseBis,
3167 DiscoverInfoPtr,
3168 McastServerListPtr,
3169 ServerListPtr
3170 );
3171
3172 if (AcquiredSrvList) {
3173 gBS->FreePool (ServerListPtr);
3174 }
3175
3176 FreeMem (Private);
3177
3178 //
3179 // Unlock the instance data
3180 //
3181 DEBUG (
3182 (EFI_D_INFO,
3183 "\nBcDiscover() status == %r (%Xh)\n",
3184 StatCode,
3185 StatCode)
3186 );
3187
3188 EfiReleaseLock (&Private->Lock);
3189 return StatCode;
3190 }
3191
3192 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
3193 EFI_STATUS
3194 EFIAPI
3195 BcSetPackets (
3196 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
3197 BOOLEAN *NewDhcpDiscoverValid, OPTIONAL
3198 BOOLEAN *NewDhcpAckReceived, OPTIONAL
3199 BOOLEAN *NewProxyOfferReceived, OPTIONAL
3200 BOOLEAN *NewPxeDiscoverValid, OPTIONAL
3201 BOOLEAN *NewPxeReplyReceived, OPTIONAL
3202 BOOLEAN *NewPxeBisReplyReceived, OPTIONAL
3203 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpDiscover, OPTIONAL
3204 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpAck, OPTIONAL
3205 IN EFI_PXE_BASE_CODE_PACKET * NewProxyOffer, OPTIONAL
3206 IN EFI_PXE_BASE_CODE_PACKET * NewPxeDiscover, OPTIONAL
3207 IN EFI_PXE_BASE_CODE_PACKET * NewPxeReply, OPTIONAL
3208 IN EFI_PXE_BASE_CODE_PACKET * NewPxeBisReply OPTIONAL
3209 )
3210 /*++
3211 Routine description:
3212
3213 Parameters:
3214
3215 Returns:
3216 --*/
3217 {
3218 EFI_PXE_BASE_CODE_MODE *PxebcMode;
3219 EFI_STATUS Status;
3220 EFI_STATUS StatCode;
3221 PXE_BASECODE_DEVICE *Private;
3222
3223 //
3224 // Lock the instance data and make sure started
3225 //
3226 StatCode = EFI_SUCCESS;
3227
3228 if (This == NULL) {
3229 DEBUG ((EFI_D_ERROR, "BC *This pointer == NULL"));
3230 return EFI_INVALID_PARAMETER;
3231 }
3232
3233 Private = CR (This, PXE_BASECODE_DEVICE, EfiBc, PXE_BASECODE_DEVICE_SIGNATURE);
3234
3235 if (Private == NULL) {
3236 DEBUG ((EFI_D_ERROR, "PXE_BASECODE_DEVICE poiner == NULL"));
3237 return EFI_INVALID_PARAMETER;
3238 }
3239
3240 EfiAcquireLock (&Private->Lock);
3241
3242 if (This->Mode == NULL || !This->Mode->Started) {
3243 DEBUG ((EFI_D_ERROR, "BC was not started."));
3244 EfiReleaseLock (&Private->Lock);
3245 return EFI_NOT_STARTED;
3246 }
3247
3248 PxebcMode = Private->EfiBc.Mode;
3249
3250 if (Private->DhcpPacketBuffer == NULL) {
3251 Status = gBS->AllocatePool (
3252 EfiBootServicesData,
3253 sizeof (DHCP_RECEIVE_BUFFER) * (PXE_BIS_INDEX + 1),
3254 &Private->DhcpPacketBuffer
3255 );
3256
3257 if (EFI_ERROR (Status) || Private->DhcpPacketBuffer == NULL) {
3258 Private->DhcpPacketBuffer = NULL;
3259 EfiReleaseLock (&Private->Lock);
3260 return EFI_OUT_OF_RESOURCES;
3261 }
3262 }
3263 //
3264 // Issue BC command
3265 //
3266 //
3267 // reset
3268 //
3269 Private->FileSize = 0;
3270 if (NewDhcpDiscoverValid != NULL) {
3271 PxebcMode->DhcpDiscoverValid = *NewDhcpDiscoverValid;
3272 }
3273
3274 if (NewDhcpAckReceived != NULL) {
3275 PxebcMode->DhcpAckReceived = *NewDhcpAckReceived;
3276 }
3277
3278 if (NewProxyOfferReceived != NULL) {
3279 PxebcMode->ProxyOfferReceived = *NewProxyOfferReceived;
3280 }
3281
3282 if (NewPxeDiscoverValid != NULL) {
3283 PxebcMode->PxeDiscoverValid = *NewPxeDiscoverValid;
3284 }
3285
3286 if (NewPxeReplyReceived != NULL) {
3287 PxebcMode->PxeReplyReceived = *NewPxeReplyReceived;
3288 }
3289
3290 if (NewPxeBisReplyReceived != NULL) {
3291 PxebcMode->PxeBisReplyReceived = *NewPxeBisReplyReceived;
3292 }
3293
3294 if (NewDhcpDiscover != NULL) {
3295 CopyMem (
3296 &PxebcMode->DhcpDiscover,
3297 NewDhcpDiscover,
3298 sizeof *NewDhcpDiscover
3299 );
3300 }
3301
3302 if (NewDhcpAck != NULL) {
3303 CopyParse (Private, &PxebcMode->DhcpAck, NewDhcpAck, DHCPV4_ACK_INDEX);
3304 }
3305
3306 if (NewProxyOffer != NULL) {
3307 CopyParse (Private, &PxebcMode->ProxyOffer, NewProxyOffer, PXE_OFFER_INDEX);
3308 }
3309
3310 if (NewPxeDiscover != NULL) {
3311 CopyMem (
3312 &PxebcMode->PxeDiscover,
3313 NewPxeDiscover,
3314 sizeof *NewPxeDiscover
3315 );
3316 }
3317
3318 if (NewPxeReply != NULL) {
3319 CopyParse (Private, &PxebcMode->PxeReply, NewPxeReply, PXE_ACK_INDEX);
3320 }
3321
3322 if (NewPxeBisReply != NULL) {
3323 CopyParse (Private, &PxebcMode->PxeBisReply, NewPxeBisReply, PXE_BIS_INDEX);
3324 }
3325 //
3326 // Unlock the instance data
3327 //
3328 EfiReleaseLock (&Private->Lock);
3329 return EFI_SUCCESS;
3330 }
3331
3332 /* eof - pxe_bc_dhcp.c */