bb7022ba749c2d3aa63120c39f780f3cbd82488e
[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 #pragma pack(1)
47 //
48 // option structure for DHCPREQUEST at end of DISCOVER options
49 // and for DHCPDECLINE
50 //
51 STATIC const struct requestopendstr {
52 DHCPV4_OP_REQUESTED_IP OpReqIP;
53 DHCPV4_OP_SERVER_IP DhcServerIpPtr;
54 UINT8 End[1];
55 }
56 RequestOpEndStr = {
57 {
58 {
59 OP_DHCP_REQ_IP_ADD,
60 DHCPV4_OPTION_LENGTH(DHCPV4_OP_REQUESTED_IP)
61 }
62 },
63 {
64 {
65 OP_DHCP_SERVER_IP,
66 DHCPV4_OPTION_LENGTH(DHCPV4_OP_SERVER_IP)
67 }
68 },
69 {
70 OP_END
71 }
72 };
73
74 #define DHCP_REQ_OPTIONS (*(struct requestopendstr *) DHCPV4_OPTIONS_BUFFER.End)
75
76 PXE_OP_BOOT_ITEM DefaultBootItem = {
77 {
78 VEND_PXE_BOOT_ITEM,
79 DHCPV4_OPTION_LENGTH(PXE_OP_BOOT_ITEM)
80 },
81 0, 0,
82 };
83
84 //
85 // PXE discovery control default structure
86 //
87 STATIC PXE_OP_DISCOVERY_CONTROL DefaultDisCtl = {
88 { VEND_PXE_DISCOVERY_CONTROL, DHCPV4_OPTION_LENGTH(PXE_OP_DISCOVERY_CONTROL) },
89 0
90 };
91
92 //
93 // PXE credentials option structure
94 //
95 typedef struct {
96 UINT8 c[4];
97 } PXE_CREDENTIAL;
98
99 typedef struct {
100 DHCPV4_OP_HEADER Header;
101 PXE_CREDENTIAL Credentials[1];
102 } PXE_OP_CREDENTIAL_TYPES;
103
104 //
105 // option structure for PXE discover (without credentials)
106 //
107 typedef struct { // discoveropendstr {
108 DHCPV4_OP_HEADER Header; // vendor options
109 PXE_OP_BOOT_ITEM BootItem;
110 UINT8 End[1]; // if credentials option, it starts here
111 } PXE_DISCOVER_OPTIONS;
112
113 #define DISCOVERoptions (*(PXE_DISCOVER_OPTIONS *) DHCPV4_OPTIONS_BUFFER.End)
114 #define DISCREDoptions (*(PXE_OP_CREDENTIAL_TYPES *) DISCOVERoptions.End)
115
116 //
117 // common option beginning for all our DHCP messages except
118 // DHCPDECLINE and DHCPRELEASE
119 //
120 STATIC struct optionsstr {
121 UINT8 DhcpCookie[4];
122 DHCPV4_OP_MESSAGE_TYPE DhcpMessageType;
123 DHCPV4_OP_MAX_MESSAGE_SIZE DhcpMaxMessageSize;
124 DHCPV4_OP_REQUESTED_OPTIONS DhcpRequestedOptions;
125 DHCPV4_OP_PLATFORM_ID DhcpPlatformId;
126 DHCPV4_OP_NETWORK_INTERFACE DhcpNetworkInterface;
127 DHCPV4_OP_ARCHITECTURE_TYPE DhcpClientArchitecture;
128 DHCPV4_OP_CLASS_ID DhcpClassIdentifier;
129 UINT8 End[1];
130 } DHCPOpStart;
131
132 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
133 VOID
134 OptionsStrucInit (
135 VOID
136 )
137 {
138 DHCPOpStart.DhcpCookie[0] = 99;
139 DHCPOpStart.DhcpCookie[1] = 130;
140 DHCPOpStart.DhcpCookie[2] = 83;
141 DHCPOpStart.DhcpCookie[3] = 99;
142 DHCPOpStart.DhcpMessageType.Header.OpCode = OP_DHCP_MESSAGE_TYPE;
143 DHCPOpStart.DhcpMessageType.Header.Length = 1;
144 DHCPOpStart.DhcpMessageType.Type = DHCPDISCOVER;
145 DHCPOpStart.DhcpMaxMessageSize.Header.OpCode = OP_DHCP_MAX_MESSAGE_SZ;
146 DHCPOpStart.DhcpMaxMessageSize.Header.Length = 2;
147 DHCPOpStart.DhcpMaxMessageSize.MaxSize[0] = MAX_DHCP_MSG_SZ >> 8;
148 DHCPOpStart.DhcpMaxMessageSize.MaxSize[1] = MAX_DHCP_MSG_SZ & 0xff;
149 DHCPOpStart.DhcpRequestedOptions.Header.OpCode = OP_DHCP_PARM_REQ_LIST;
150 DHCPOpStart.DhcpRequestedOptions.Header.Length = sizeof (DHCPV4_REQUESTED_OPTIONS_DATA);
151 DHCPOpStart.DhcpRequestedOptions.Data._OP_SUBNET_MASK = OP_SUBNET_MASK; /* 1 */
152 DHCPOpStart.DhcpRequestedOptions.Data._OP_TIME_OFFSET = OP_TIME_OFFSET; /* 2 */
153 DHCPOpStart.DhcpRequestedOptions.Data._OP_ROUTER_LIST = OP_ROUTER_LIST; /* 3 */
154 DHCPOpStart.DhcpRequestedOptions.Data._OP_TIME_SERVERS = OP_TIME_SERVERS; /* 4 */
155 DHCPOpStart.DhcpRequestedOptions.Data._OP_NAME_SERVERS = OP_NAME_SERVERS; /* 5 */
156 DHCPOpStart.DhcpRequestedOptions.Data._OP_DNS_SERVERS = OP_DNS_SERVERS; /* 6 */
157 DHCPOpStart.DhcpRequestedOptions.Data._OP_HOST_NAME = OP_HOST_NAME; /* 12 */
158 DHCPOpStart.DhcpRequestedOptions.Data._OP_BOOT_FILE_SZ = OP_BOOT_FILE_SZ; /* 13 */
159 DHCPOpStart.DhcpRequestedOptions.Data._OP_DOMAIN_NAME = OP_DOMAIN_NAME; /* 15 */
160 DHCPOpStart.DhcpRequestedOptions.Data._OP_ROOT_PATH = OP_ROOT_PATH; /* 17 */
161 DHCPOpStart.DhcpRequestedOptions.Data._OP_EXTENSION_PATH = OP_EXTENSION_PATH; /* 18 */
162 DHCPOpStart.DhcpRequestedOptions.Data._OP_MAX_DATAGRAM_SZ = OP_MAX_DATAGRAM_SZ; /* 22 */
163 DHCPOpStart.DhcpRequestedOptions.Data._OP_DEFAULT_TTL = OP_DEFAULT_TTL; /* 23 */
164 DHCPOpStart.DhcpRequestedOptions.Data._OP_BROADCAST_ADD = OP_BROADCAST_ADD; /* 28 */
165 DHCPOpStart.DhcpRequestedOptions.Data._OP_NIS_DOMAIN_NAME = OP_NIS_DOMAIN_NAME; /* 40 */
166 DHCPOpStart.DhcpRequestedOptions.Data._OP_NIS_SERVERS = OP_NIS_SERVERS; /* 41 */
167 DHCPOpStart.DhcpRequestedOptions.Data._OP_NTP_SERVERS = OP_NTP_SERVERS; /* 42 */
168 DHCPOpStart.DhcpRequestedOptions.Data._OP_VENDOR_SPECIFIC = OP_VENDOR_SPECIFIC; /* 43 */
169 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_REQ_IP_ADD = OP_DHCP_REQ_IP_ADD; /* 50 */
170 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_LEASE_TIME = OP_DHCP_LEASE_TIME; /* 51 */
171 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_SERVER_IP = OP_DHCP_SERVER_IP; /* 54 */
172 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_RENEWAL_TIME = OP_DHCP_RENEWAL_TIME; /* 58 */
173 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_REBINDING_TIME = OP_DHCP_REBINDING_TIME; /* 59 */
174 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_CLASS_IDENTIFIER = OP_DHCP_CLASS_IDENTIFIER; /* 60 */
175 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_TFTP_SERVER_NAME = OP_DHCP_TFTP_SERVER_NAME; /* 66 */
176 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_BOOTFILE = OP_DHCP_BOOTFILE; /* 67 */
177 DHCPOpStart.DhcpRequestedOptions.Data._OP_DHCP_PLATFORM_ID = OP_DHCP_PLATFORM_ID; /* 97 */
178 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption128 = 128;
179 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption129 = 129;
180 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption130 = 130;
181 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption131 = 131;
182 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption132 = 132;
183 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption133 = 133, DHCPOpStart.DhcpRequestedOptions.Data.VendorOption134 = 134;
184 DHCPOpStart.DhcpRequestedOptions.Data.VendorOption135 = 135;
185 DHCPOpStart.DhcpPlatformId.Header.OpCode = OP_DHCP_PLATFORM_ID;
186 DHCPOpStart.DhcpPlatformId.Header.Length = DHCPV4_OPTION_LENGTH (DHCPV4_OP_PLATFORM_ID);
187 DHCPOpStart.DhcpNetworkInterface.Header.OpCode = OP_DHCP_NETWORK_ARCH;
188 DHCPOpStart.DhcpNetworkInterface.Header.Length = DHCPV4_OPTION_LENGTH (DHCPV4_OP_NETWORK_INTERFACE);
189 DHCPOpStart.DhcpNetworkInterface.Type = 0;
190 DHCPOpStart.DhcpNetworkInterface.MajorVersion = 0;
191 DHCPOpStart.DhcpNetworkInterface.MinorVersion = 0;
192 DHCPOpStart.DhcpClientArchitecture.Header.OpCode = OP_DHCP_SYSTEM_ARCH;
193 DHCPOpStart.DhcpClientArchitecture.Header.Length = DHCPV4_OPTION_LENGTH (DHCPV4_OP_ARCHITECTURE_TYPE);
194 DHCPOpStart.DhcpClientArchitecture.Type = HTONS (SYS_ARCH);
195 DHCPOpStart.DhcpClassIdentifier.Header.OpCode = OP_DHCP_CLASS_IDENTIFIER;
196 DHCPOpStart.DhcpClassIdentifier.Header.Length = sizeof (DHCPV4_CLASS_ID_DATA);
197 CopyMem (
198 DHCPOpStart.DhcpClassIdentifier.Data.ClassIdentifier,
199 "PXEClient:",
200 sizeof ("PXEClient:")
201 );
202 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.Lit2, "Arch:", sizeof ("Arch:"));
203 CopyMem (
204 DHCPOpStart.DhcpClassIdentifier.Data.ArchitectureType,
205 "xxxxx",
206 sizeof ("xxxxx")
207 );
208 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.Lit3, ":", sizeof (":"));
209 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.InterfaceName, "XXXX", sizeof ("XXXX"));
210 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.Lit4, ":", sizeof (":"));
211 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.UndiMajor, "yyy", sizeof ("yyy"));
212 CopyMem (DHCPOpStart.DhcpClassIdentifier.Data.UndiMinor, "xxx", sizeof ("xxx"));
213 DHCPOpStart.End[0] = OP_END;
214 }
215
216 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
217
218 //
219 // DHCPDECLINE option structure
220 //
221 struct opdeclinestr {
222 UINT8 DhcpCookie[4];
223 DHCPV4_OP_MESSAGE_TYPE DhcpMessageType;
224 struct requestopendstr OpDeclineEnd;
225 };
226
227 #define DHCPDECLINEoptions (*(struct opdeclinestr *) DHCPV4_TRANSMIT_BUFFER.options)
228
229 //
230 // DHCPRELEASE option structure
231 //
232 struct opreleasestr {
233 UINT8 DhcpCookie[4];
234 DHCPV4_OP_MESSAGE_TYPE DhcpMessageType;
235 DHCPV4_OP_SERVER_IP DhcServerIpPtr;
236 UINT8 End[1];
237 };
238
239 #define DHCPRELEASEoptions (*(struct opreleasestr *) DHCPV4_TRANSMIT_BUFFER.options)
240
241 //
242 // array of PXE vendor options in which we are interested
243 // value 0 -> not of interest, else value is index into PXE OPTION array
244 // option values from 1 to MAX_OUR_PXE_OPT
245 //
246 STATIC UINT8 ourPXEopts[MAX_OUR_PXE_OPT] = {
247 VEND_PXE_MTFTP_IP_IX, // multicast IP address of bootfile for MTFTP listen
248 VEND_PXE_MTFTP_CPORT_IX, // UDP Port to monitor for MTFTP responses - Intel order
249 VEND_PXE_MTFTP_SPORT_IX, // Server UDP Port for MTFTP open - Intel order
250 VEND_PXE_MTFTP_TMOUT_IX, // Listen timeout - secs
251 VEND_PXE_MTFTP_DELAY_IX, // Transmission timeout - secs
252 VEND_PXE_DISCOVERY_CONTROL_IX, // bit field
253 VEND_PXE_DISCOVERY_MCAST_ADDR_IX, // boot server discovery multicast address
254 VEND_PXE_BOOT_SERVERS_IX, // list of boot servers of form tp(2) cnt(1) ips[cnt]
255 VEND_PXE_BOOT_MENU_IX,
256 VEND_PXE_BOOT_PROMPT_IX,
257 VEND_PXE_MCAST_ADDRS_ALLOC_IX, // not used by client
258 VEND_PXE_CREDENTIAL_TYPES_IX,
259 VEND_13_IX, // not used by client
260 VEND_14_IX, // not used by client
261 VEND_15_IX, // not used by client
262 VEND_16_IX, // not used by client
263 VEND_17_IX, // not used by client
264 VEND_18_IX, // not used by client
265 VEND_19_IX, // not used by client
266 VEND_20_IX, // not used by client
267 VEND_21_IX, // not used by client
268 VEND_22_IX, // not used by client
269 VEND_23_IX, // not used by client
270 VEND_24_IX, // not used by client
271 VEND_25_IX, // not used by client
272 VEND_26_IX, // not used by client
273 VEND_27_IX, // not used by client
274 VEND_28_IX, // not used by client
275 VEND_29_IX, // not used by client
276 VEND_30_IX, // not used by client
277 VEND_31_IX, // not used by client
278 VEND_32_IX, // not used by client
279 VEND_33_IX, // not used by client
280 VEND_34_IX, // not used by client
281 VEND_35_IX, // not used by client
282 VEND_36_IX, // not used by client
283 VEND_37_IX, // not used by client
284 VEND_38_IX, // not used by client
285 VEND_39_IX, // not used by client
286 VEND_40_IX, // not used by client
287 VEND_41_IX, // not used by client
288 VEND_42_IX, // not used by client
289 VEND_43_IX, // not used by client
290 VEND_44_IX, // not used by client
291 VEND_45_IX, // not used by client
292 VEND_46_IX, // not used by client
293 VEND_47_IX, // not used by client
294 VEND_48_IX, // not used by client
295 VEND_49_IX, // not used by client
296 VEND_50_IX, // not used by client
297 VEND_51_IX, // not used by client
298 VEND_52_IX, // not used by client
299 VEND_53_IX, // not used by client
300 VEND_54_IX, // not used by client
301 VEND_55_IX, // not used by client
302 VEND_56_IX, // not used by client
303 VEND_57_IX, // not used by client
304 VEND_58_IX, // not used by client
305 VEND_59_IX, // not used by client
306 VEND_60_IX, // not used by client
307 VEND_61_IX, // not used by client
308 VEND_62_IX, // not used by client
309 VEND_63_IX, // not used by client
310 VEND_64_IX, // not used by client
311 VEND_65_IX, // not used by client
312 VEND_66_IX, // not used by client
313 VEND_67_IX, // not used by client
314 VEND_68_IX, // not used by client
315 VEND_69_IX, // not used by client
316 VEND_70_IX, // not used by client
317 VEND_PXE_BOOT_ITEM_IX
318 };
319
320 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
321
322 //
323 // array of options in which we are interested
324 // value 0 -> not of interest, else value is index into OPTION array
325 // option values from 1 to MAX_OUR_OPT
326 //
327 STATIC UINT8 OurDhcpOptions[MAX_OUR_OPT] = {
328 OP_SUBNET_MASK_IX, // OP_SUBNET_MASK 1 // data is the subnet mask
329 OP_TIME_OFFSET_IX, // OP_TIME_OFFSET 2 // data is the time offset of subnet to UTC in seconds
330 OP_ROUTER_LIST_IX, // OP_ROUTER_LIST 3 // list of routers on subnet
331 OP_TIME_SERVERS_IX, // OP_TIME_SERVERS 4 // list of time servers available
332 OP_NAME_SERVERS_IX, // OP_NAME_SERVERS 5 // list of name servers available
333 OP_DNS_SERVERS_IX, // OP_DNS_SERVERS 6 // list of DNS servers available
334 OP_LOG_SERVERS_IX, // OP_LOG_SERVERS 7
335 OP_COOKIE_SERVERS_IX, // OP_COOKIE_SERVERS 8
336 OP_LPR_SREVERS_IX, // OP_LPR_SREVERS 9
337 OP_IMPRESS_SERVERS_IX, // OP_IMPRESS_SERVERS 10
338 OP_RES_LOC_SERVERS_IX, // OP_RES_LOC_SERVERS 11
339 OP_HOST_NAME_IX, // OP_HOST_NAME 12 // client name
340 OP_BOOT_FILE_SZ_IX, // OP_BOOT_FILE_SZ 13 // number of 512 blocks of boot file
341 OP_DUMP_FILE_IX, // OP_DUMP_FILE 14 // path name of dump file if client crashes
342 OP_DOMAIN_NAME_IX, // OP_DOMAIN_NAME 15 // domain name to use
343 OP_SWAP_SERVER_IX, // OP_SWAP_SERVER 16
344 OP_ROOT_PATH_IX, // OP_ROOT_PATH 17 // path name containing root disk
345 OP_EXTENSION_PATH_IX, // OP_EXTENSION_PATH 18 // name of TFTP downloadable file of form of OP
346 OP_IP_FORWARDING_IX, // OP_IP_FORWARDING 19 // enable/disable IP packet forwarding
347 OP_NON_LOCAL_SRC_RTE_IX, // OP_NON_LOCAL_SRC_RTE 20 // enable/disable non local source routing
348 OP_POLICY_FILTER_IX, // OP_POLICY_FILTER 21 // policy filters for non local source routing
349 OP_MAX_DATAGRAM_SZ_IX, // OP_MAX_DATAGRAM_SZ 22 // maximum datagram reassembly size
350 OP_DEFAULT_TTL_IX, // OP_DEFAULT_TTL 23 // default IP time to live
351 OP_MTU_AGING_TIMEOUT_IX, // OP_MTU_AGING_TIMEOUT 24
352 OP_MTU_SIZES_IX, // OP_MTU_SIZES 25
353 OP_MTU_TO_USE_IX, // OP_MTU_TO_USE 26
354 OP_ALL_SUBNETS_LOCAL_IX, // OP_ALL_SUBNETS_LOCAL 27
355 OP_BROADCAST_ADD_IX, // OP_BROADCAST_ADD 28 // broadcast address used on subnet
356 OP_PERFORM_MASK_DISCOVERY_IX, // OP_PERFORM_MASK_DISCOVERY 29 // perform mask discovery using ICMP
357 OP_RESPOND_TO_MASK_REQ_IX, // OP_RESPOND_TO_MASK_REQ 30 // respond to subnet mask requests using ICMP
358 OP_PERFORM_ROUTER_DISCOVERY_IX, // OP_PERFORM_ROUTER_DISCOVERY 31
359 OP_ROUTER_SOLICIT_ADDRESS_IX, // OP_ROUTER_SOLICIT_ADDRESS 32
360 OP_STATIC_ROUTER_LIST_IX, // OP_STATIC_ROUTER_LIST 33 // list of dest/route pairs
361 OP_USE_ARP_TRAILERS_IX, // OP_USE_ARP_TRAILERS 34
362 OP_ARP_CACHE_TIMEOUT_IX, // OP_ARP_CACHE_TIMEOUT 35
363 OP_ETHERNET_ENCAPSULATION_IX, // OP_ETHERNET_ENCAPSULATION 36 // 0 -> RFC 894, 1 -> IEEE 802.3 (RFC 1042)
364 OP_TCP_DEFAULT_TTL_IX, // OP_TCP_DEFAULT_TTL 37 // default time to live when sending TCP segments
365 OP_TCP_KEEP_ALIVE_INT_IX, // OP_TCP_KEEP_ALIVE_INT 38 // keep alive interval in seconds
366 OP_KEEP_ALIVE_GARBAGE_IX, // OP_KEEP_ALIVE_GARBAGE 39
367 OP_NIS_DOMAIN_NAME_IX, // OP_NIS_DOMAIN_NAME 40
368 OP_NIS_SERVERS_IX, // OP_NIS_SERVERS 41
369 OP_NTP_SERVERS_IX, // OP_NTP_SERVERS 42
370 OP_VENDOR_SPECIFIC_IX, // OP_VENDOR_SPECIFIC 43
371 OP_NBNS_SERVERS_IX, // OP_NBNS_SERVERS 44
372 OP_NBDD_SERVERS_IX, // OP_NBDD_SERVERS 45
373 OP_NETBIOS_NODE_TYPE_IX, // OP_NETBIOS_NODE_TYPE 46
374 OP_NETBIOS_SCOPE_IX, // OP_NETBIOS_SCOPE 47
375 OP_XWINDOW_SYSTEM_FONT_SERVERS_IX, // OP_XWINDOW_SYSTEM_FONT_SERVERS 48
376 OP_XWINDOW_SYSTEM_DISPLAY_MANAGERS_IX, // OP_XWINDOW_SYSTEM_DISPLAY_MANAGERS 49
377 OP_DHCP_REQ_IP_ADD_IX, // OP_DHCP_REQ_IP_ADD 50 // requested IP address - in DHCPDISCOVER
378 OP_DHCP_LEASE_TIME_IX, // OP_DHCP_LEASE_TIME 51 // lease time requested/granted
379 OP_DHCP_OPTION_OVERLOAD_IX, // OP_DHCP_OPTION_OVERLOAD 52 // file/server name/both used to hold options
380 OP_DHCP_MESSAGE_TYPE_IX, // OP_DHCP_MESSAGE_TYPE 53 // message type
381 OP_DHCP_SERVER_IP_IX, // OP_DHCP_SERVER_IP 54 // IP of server
382 OP_DHCP_PARM_REQ_LIST_IX, // OP_DHCP_PARM_REQ_LIST 55 // list of requested parameters
383 OP_DHCP_ERROR_MESSAGE_IX, // OP_DHCP_ERROR_MESSAGE 56 // in DHCPNAK or DECLINE messages
384 OP_DHCP_MAX_MESSAGE_SZ_IX, // OP_DHCP_MAX_MESSAGE_SZ 57 // maximum DHCP message size client will accept
385 OP_DHCP_RENEWAL_TIME_IX, // OP_DHCP_RENEWAL_TIME 58 // time in seconds before transitioning to RENEWING state
386 OP_DHCP_REBINDING_TIME_IX, // OP_DHCP_REBINDING_TIME 59 // time in seconds before transitioning to REBINDING state
387 OP_DHCP_CLASS_IDENTIFIER_IX, // OP_DHCP_CLASS_IDENTIFIER 60
388 OP_DHCP_CLIENT_IDENTIFIER_IX, // OP_DHCP_CLIENT_IDENTIFIER 61
389 OP_RESERVED62_IX, // OP_RESERVED62
390 OP_RESERVED63_IX, // OP_RESERVED63
391 OP_NISPLUS_DOMAIN_NAME_IX, // OP_NISPLUS_DOMAIN_NAME 64
392 OP_NISPLUS_SERVERS_IX, // OP_NISPLUS_SERVERS 65
393 OP_DHCP_TFTP_SERVER_NAME_IX, // OP_DHCP_TFTP_SERVER_NAME 66
394 OP_DHCP_BOOTFILE_IX // OP_DHCP_BOOTFILE 67
395 };
396
397 #define RxBuf ((DHCP_RECEIVE_BUFFER *) (Private->ReceiveBuffers))
398
399 #pragma pack()
400
401 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
402 STATIC
403 CHAR8 *
404 PxeBcLibGetSmbiosString (
405 IN SMBIOS_STRUCTURE_POINTER *Smbios,
406 IN UINT16 StringNumber
407 )
408 /*++
409 Routine description:
410 Return SMBIOS string given the string number.
411
412 Arguments:
413 Smbios - Pointer to SMBIOS structure
414 StringNumber - String number to return. 0 is used to skip all strings and
415 point to the next SMBIOS structure.
416
417 Returns:
418 Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == 0
419 --*/
420 {
421 UINT16 Index;
422 CHAR8 *String;
423
424 //
425 // Skip over formatted section
426 //
427 String = (CHAR8 *) (Smbios->Raw + Smbios->Hdr->Length);
428
429 //
430 // Look through unformated section
431 //
432 for (Index = 1; Index <= StringNumber || StringNumber == 0; Index++) {
433 if (StringNumber == Index) {
434 return String;
435 }
436 //
437 // Skip string
438 //
439 for (; *String != 0; String++)
440 ;
441 String++;
442
443 if (*String == 0) {
444 //
445 // If double NULL then we are done.
446 // Return pointer to next structure in Smbios.
447 // if you pass in a 0 you will always get here
448 //
449 Smbios->Raw = (UINT8 *)++String;
450 return NULL;
451 }
452 }
453
454 return NULL;
455 }
456
457 EFI_STATUS
458 PxeBcLibGetSmbiosSystemGuidAndSerialNumber (
459 IN EFI_GUID *SystemGuid,
460 OUT CHAR8 **SystemSerialNumber
461 )
462 /*++
463
464 Routine Description:
465 This function gets system guid and serial number from the smbios table
466
467 Arguments:
468 SystemGuid - The pointer of returned system guid
469 SystemSerialNumber - The pointer of returned system serial number
470
471 Returns:
472 EFI_SUCCESS - Successfully get the system guid and system serial number
473 EFI_NOT_FOUND - Not find the SMBIOS table
474 --*/
475 {
476 EFI_STATUS Status;
477 SMBIOS_STRUCTURE_TABLE *SmbiosTable;
478 SMBIOS_STRUCTURE_POINTER Smbios;
479 SMBIOS_STRUCTURE_POINTER SmbiosEnd;
480 UINT16 Index;
481
482 Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable);
483
484 if (EFI_ERROR (Status)) {
485 return EFI_NOT_FOUND;
486 }
487
488 Smbios.Hdr = (SMBIOS_HEADER *) (UINTN) SmbiosTable->TableAddress;
489 SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength);
490
491 for (Index = 0; Index < SmbiosTable->TableLength; Index++) {
492 if (Smbios.Hdr->Type == 1) {
493 if (Smbios.Hdr->Length < 0x19) {
494 //
495 // Older version did not support Guid and Serial number
496 //
497 continue;
498 }
499 //
500 // SMBIOS tables are byte packed so we need to do a byte copy to
501 // prevend alignment faults on Itanium-based platform.
502 //
503 CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID));
504 *SystemSerialNumber = PxeBcLibGetSmbiosString (&Smbios, Smbios.Type1->SerialNumber);
505
506 return EFI_SUCCESS;
507 }
508 //
509 // Make Smbios point to the next record
510 //
511 PxeBcLibGetSmbiosString (&Smbios, 0);
512
513 if (Smbios.Raw >= SmbiosEnd.Raw) {
514 //
515 // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
516 // given this we must double check against the lenght of
517 // the structure.
518 //
519 return EFI_SUCCESS;
520 }
521 }
522
523 return EFI_SUCCESS;
524 }
525
526 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
527
528 //
529 // add router list to list
530 //
531 STATIC
532 VOID
533 Ip4AddRouterList (
534 PXE_BASECODE_DEVICE *Private,
535 DHCPV4_OP_IP_LIST *IpListPtr
536 )
537 {
538 EFI_IP_ADDRESS TmpIp;
539 INTN Index;
540 INTN num;
541
542 if (IpListPtr == NULL) {
543 return ;
544 }
545
546 for (Index = 0, num = IpListPtr->Header.Length >> 2; Index < num; ++Index) {
547 CopyMem (&TmpIp, &IpListPtr->IpList[Index], 4);
548 Ip4AddRouter (Private, &TmpIp);
549 }
550 }
551
552 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
553
554 //
555 // send ARP for our IP - fail if someone has it
556 //
557 STATIC
558 BOOLEAN
559 SetStationIP (
560 PXE_BASECODE_DEVICE *Private
561 )
562 {
563 EFI_MAC_ADDRESS DestMac;
564 EFI_STATUS EfiStatus;
565
566 ZeroMem (&DestMac, sizeof DestMac);
567
568 if (GetHwAddr(Private, (EFI_IP_ADDRESS *)&DHCP_REQ_OPTIONS.OpReqIP.Ip, (EFI_MAC_ADDRESS *)&DestMac)
569 || DoArp(Private, (EFI_IP_ADDRESS *)&DHCP_REQ_OPTIONS.OpReqIP.Ip, (EFI_MAC_ADDRESS *)&DestMac) == EFI_SUCCESS) {
570 return FALSE; // somebody else has this IP
571 }
572
573 CopyMem (
574 (EFI_IPv4_ADDRESS *) &Private->EfiBc.Mode->StationIp,
575 &DHCP_REQ_OPTIONS.OpReqIP.Ip,
576 sizeof (EFI_IPv4_ADDRESS)
577 );
578
579 Private->GoodStationIp = TRUE;
580
581 if (!Private->UseIgmpv1Reporting) {
582 return TRUE;
583 }
584
585 if (Private->Igmpv1TimeoutEvent != NULL) {
586 return TRUE;
587 }
588
589 EfiStatus = gBS->CreateEvent (
590 EFI_EVENT_TIMER,
591 EFI_TPL_CALLBACK,
592 NULL,
593 NULL,
594 &Private->Igmpv1TimeoutEvent
595 );
596
597 if (EFI_ERROR (EfiStatus)) {
598 Private->Igmpv1TimeoutEvent = NULL;
599 return TRUE;
600 }
601
602 EfiStatus = gBS->SetTimer (
603 Private->Igmpv1TimeoutEvent,
604 TimerRelative,
605 (UINT64) V1ROUTER_PRESENT_TIMEOUT * 10000000
606 ); /* 400 seconds */
607
608 if (EFI_ERROR (EfiStatus)) {
609 gBS->CloseEvent (Private->Igmpv1TimeoutEvent);
610 Private->Igmpv1TimeoutEvent = NULL;
611 }
612
613 return TRUE;
614 }
615
616 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
617 STATIC
618 VOID
619 AddRouters (
620 PXE_BASECODE_DEVICE *Private,
621 DHCP_RECEIVE_BUFFER *RxBufPtr
622 )
623 {
624 Ip4AddRouter (Private, &RxBufPtr->u.Dhcpv4.giaddr);
625
626 Ip4AddRouterList (
627 Private,
628 (DHCPV4_OP_IP_LIST *) RxBufPtr->OpAdds.PktOptAdds[OP_ROUTER_LIST_IX - 1]
629 );
630 }
631
632 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
633 STATIC
634 EFI_STATUS
635 DoUdpWrite (
636 PXE_BASECODE_DEVICE *Private,
637 EFI_IP_ADDRESS *ServerIpPtr,
638 EFI_PXE_BASE_CODE_UDP_PORT *ServerPortPtr,
639 EFI_IP_ADDRESS *ClientIpPtr,
640 EFI_PXE_BASE_CODE_UDP_PORT *ClientPortPtr
641 )
642 {
643 UINTN Len;
644
645 Len = sizeof DHCPV4_TRANSMIT_BUFFER;
646
647 return UdpWrite (
648 Private,
649 EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT,
650 ServerIpPtr,
651 ServerPortPtr,
652 0,
653 ClientIpPtr,
654 ClientPortPtr,
655 0,
656 0,
657 &Len,
658 Private->TransmitBuffer
659 );
660 }
661
662 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
663
664 //
665 // initialize the DHCP structure
666 //
667 typedef struct {
668 UINT8 x[4];
669 } C4Str;
670
671 STATIC
672 VOID
673 InitDhcpv4TxBuf (
674 PXE_BASECODE_DEVICE *Private
675 )
676 {
677 UINTN HwAddrLen;
678 UINT8 *String;
679 CHAR8 *SystemSerialNumber;
680 EFI_PXE_BASE_CODE_MODE *PxebcMode;
681
682 PxebcMode = Private->EfiBc.Mode;
683
684 ZeroMem (&DHCPV4_TRANSMIT_BUFFER, sizeof (DHCPV4_STRUCT));
685 DHCPV4_TRANSMIT_BUFFER.op = BOOTP_REQUEST;
686 DHCPV4_TRANSMIT_BUFFER.htype = Private->SimpleNetwork->Mode->IfType;
687 DHCPV4_TRANSMIT_BUFFER.flags = HTONS (DHCP_BROADCAST_FLAG);
688 CopyMem (&DHCPV4_OPTIONS_BUFFER, (VOID *) &DHCPOpStart, sizeof (DHCPOpStart));
689
690 //
691 // default to hardware address
692 //
693 HwAddrLen = Private->SimpleNetwork->Mode->HwAddressSize;
694
695 if (HwAddrLen > sizeof DHCPV4_TRANSMIT_BUFFER.chaddr) {
696 HwAddrLen = sizeof DHCPV4_TRANSMIT_BUFFER.chaddr;
697 }
698
699 String = (UINT8 *) &Private->SimpleNetwork->Mode->CurrentAddress;
700
701 if (PxeBcLibGetSmbiosSystemGuidAndSerialNumber (
702 (EFI_GUID *) DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid,
703 &SystemSerialNumber
704 ) == EFI_SUCCESS) {
705 if (PxebcMode->SendGUID) {
706 HwAddrLen = sizeof (EFI_GUID);
707 String = (UINT8 *) DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid;
708 }
709 } else {
710 //
711 // GUID not yet set - send all 0xff's to show programable (via SetVariable)
712 // SetMem(DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof(EFI_GUID), 0xff);
713 // GUID not yet set - send all 0's to show not programable
714 //
715 ZeroMem (DHCPV4_OPTIONS_BUFFER.DhcpPlatformId.Guid, sizeof (EFI_GUID));
716 }
717
718 DHCPV4_TRANSMIT_BUFFER.hlen = (UINT8) HwAddrLen;
719 CopyMem (DHCPV4_TRANSMIT_BUFFER.chaddr, String, HwAddrLen);
720
721 CvtNum (
722 SYS_ARCH,
723 (UINT8 *) DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.ArchitectureType,
724 sizeof DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.ArchitectureType
725 );
726
727 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.Type = Private->NiiPtr->Type;
728 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.MajorVersion = Private->NiiPtr->MajorVer;
729 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.MinorVersion = Private->NiiPtr->MinorVer;
730
731 *(C4Str *) DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.InterfaceName = *(C4Str *) Private->NiiPtr->StringId;
732
733 CvtNum (
734 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.MajorVersion,
735 (UINT8 *) DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.UndiMajor,
736 sizeof DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.UndiMajor
737 );
738
739 CvtNum (
740 DHCPV4_OPTIONS_BUFFER.DhcpNetworkInterface.MinorVersion,
741 (UINT8 *) DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.UndiMinor,
742 sizeof DHCPV4_OPTIONS_BUFFER.DhcpClassIdentifier.Data.UndiMinor
743 );
744 }
745
746 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
747 STATIC
748 UINT32
749 DecodePxeOptions (
750 DHCP_RECEIVE_BUFFER *RxBufPtr,
751 UINT8 *ptr,
752 INTN Len
753 )
754 {
755 UINT8 Op;
756 UINT8 *EndPtr;
757 INTN Index;
758 UNION_PTR LocalPtr;
759 UINT32 status;
760
761 status = 0;
762
763 for (EndPtr = ptr + Len; ptr < EndPtr; ptr += Len + 2) {
764 Op = ptr[0];
765 Len = ptr[1];
766
767 switch (Op) {
768 case OP_PAD:
769 Len = -1;
770 break;
771
772 case OP_END:
773 return status;
774
775 default:
776 LocalPtr.BytePtr = ptr;
777 if (Op <= MAX_OUR_PXE_OPT) {
778 Index = ourPXEopts[Op - 1];
779 if (Index) {
780 RxBufPtr->OpAdds.PxeOptAdds[Index - 1] = LocalPtr.OpPtr;
781 status |= 1 << Index;
782 if (Index == VEND_PXE_BOOT_ITEM && LocalPtr.BootItem->Header.Length == 3) {
783 RxBufPtr->OpAdds.Status |= USE_THREE_BYTE;
784 }
785 }
786 }
787 break;
788 }
789 }
790
791 return status;
792 }
793
794 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
795 STATIC
796 VOID
797 DecodeOptions (
798 DHCP_RECEIVE_BUFFER *RxBufPtr,
799 UINT8 *ptr,
800 INTN Len
801 )
802 {
803 UINT8 Op;
804 UINT8 *EndPtr;
805 INTN Index;
806 UNION_PTR LocalPtr;
807
808 for (EndPtr = ptr + Len; ptr < EndPtr; ptr += Len + 2) {
809 Op = ptr[0];
810 Len = ptr[1];
811
812 switch (Op) {
813 case OP_PAD:
814 Len = -1;
815 break;
816
817 case OP_END:
818 return ;
819
820 default:
821 LocalPtr.BytePtr = ptr;
822 if (Op <= MAX_OUR_OPT) {
823 Index = OurDhcpOptions[Op - 1];
824 if (Index) {
825 RxBufPtr->OpAdds.PktOptAdds[Index - 1] = LocalPtr.OpPtr;
826 if (Index == OP_VENDOR_SPECIFIC_IX) {
827 UINT32 status;
828 status = DecodePxeOptions (
829 RxBufPtr,
830 (UINT8 *) LocalPtr.VendorOptions->VendorOptions,
831 LocalPtr.VendorOptions->Header.Length
832 );
833 if (status) {
834 RxBufPtr->OpAdds.Status |= PXE_TYPE;
835 //
836 // check for all the MTFTP info options present - any missing is a nogo
837 //
838 if ((status & WfM11a_OPTS) == WfM11a_OPTS) {
839 RxBufPtr->OpAdds.Status |= WfM11a_TYPE;
840 }
841
842 if (status & DISCOVER_OPTS) {
843 RxBufPtr->OpAdds.Status |= DISCOVER_TYPE;
844 }
845
846 if (status & CREDENTIALS_OPT) {
847 RxBufPtr->OpAdds.Status |= CREDENTIALS_TYPE;
848 }
849 }
850 }
851 }
852 }
853 break;
854 }
855 }
856 }
857
858 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
859 STATIC
860 VOID
861 Parse (
862 DHCP_RECEIVE_BUFFER *RxBufPtr,
863 INTN Len
864 )
865 {
866 UNION_PTR LocalPtr;
867
868 //
869 // initialize
870 //
871 SetMem (&RxBufPtr->OpAdds, sizeof RxBufPtr->OpAdds, 0);
872
873 DecodeOptions (
874 RxBufPtr,
875 RxBufPtr->u.Dhcpv4.options + 4,
876 Len - (sizeof RxBufPtr->u.Dhcpv4 - sizeof RxBufPtr->u.Dhcpv4.options + 4)
877 );
878
879 LocalPtr.OpPtr = RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_OPTION_OVERLOAD_IX - 1];
880
881 if ((LocalPtr.OpPtr) && (LocalPtr.Overload->Overload & OVLD_SRVR_NAME)) {
882 DecodeOptions (RxBufPtr, RxBufPtr->u.Dhcpv4.sname, sizeof RxBufPtr->u.Dhcpv4.sname);
883 }
884
885 if (LocalPtr.OpPtr && (LocalPtr.Overload->Overload & OVLD_FILE)) {
886 DecodeOptions (RxBufPtr, RxBufPtr->u.Dhcpv4.file, sizeof RxBufPtr->u.Dhcpv4.file);
887 } else if (!RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1] && RxBufPtr->u.Dhcpv4.file[0]) {
888 RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1] = (DHCPV4_OP_STRUCT *) (RxBufPtr->u.Dhcpv4.file - sizeof (DHCPV4_OP_HEADER));
889
890 RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1]->Header.Length = (UINT8) AsciiStrLen ((CHAR8 *)RxBufPtr->u.Dhcpv4.file);
891 }
892
893 LocalPtr.OpPtr = RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_CLASS_IDENTIFIER_IX - 1];
894
895 if ((LocalPtr.OpPtr) &&
896 LocalPtr.PxeClassStr->Header.Length >= 9 &&
897 !CompareMem (LocalPtr.PxeClassStr->Class, "PXEClient", 9)
898 ) {
899 RxBufPtr->OpAdds.Status |= PXE_TYPE;
900 }
901 }
902
903 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
904 STATIC
905 VOID
906 CopyParseRxBuf (
907 PXE_BASECODE_DEVICE *Private,
908 INTN RxBufIndex,
909 INTN PacketIndex
910 )
911 {
912 DHCP_RECEIVE_BUFFER *RxBufPtr;
913
914 RxBufPtr = &((DHCP_RECEIVE_BUFFER *) Private->DhcpPacketBuffer)[PacketIndex];
915
916 CopyMem (
917 &RxBufPtr->u.Dhcpv4,
918 &RxBuf[RxBufIndex].u.Dhcpv4,
919 sizeof (RxBuf[RxBufIndex].u.Dhcpv4)
920 );
921
922 Parse (RxBufPtr, sizeof RxBufPtr->u.ReceiveBuffer);
923 }
924
925 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
926 STATIC
927 VOID
928 CopyProxyRxBuf (
929 PXE_BASECODE_DEVICE *Private,
930 INTN RxBufIndex
931 )
932 {
933 Private->EfiBc.Mode->ProxyOfferReceived = TRUE;
934 CopyParseRxBuf (Private, RxBufIndex, PXE_OFFER_INDEX);
935 }
936
937 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
938 STATIC
939 VOID
940 CopyParse (
941 PXE_BASECODE_DEVICE *Private,
942 EFI_PXE_BASE_CODE_PACKET *PacketPtr,
943 EFI_PXE_BASE_CODE_PACKET *NewPacketPtr,
944 INTN Index
945 )
946 {
947 DHCP_RECEIVE_BUFFER *DhcpRxBuf;
948
949 DhcpRxBuf = &((DHCP_RECEIVE_BUFFER *) Private->DhcpPacketBuffer)[Index];
950
951 CopyMem (
952 (EFI_PXE_BASE_CODE_PACKET *) &DhcpRxBuf->u.Dhcpv4,
953 NewPacketPtr,
954 sizeof (*NewPacketPtr)
955 );
956
957 CopyMem (&*PacketPtr, &*NewPacketPtr, sizeof (*NewPacketPtr));
958
959 Parse (DhcpRxBuf, sizeof DhcpRxBuf->u.ReceiveBuffer);
960 }
961
962 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
963 STATIC
964 BOOLEAN
965 AckEdit (
966 DHCP_RECEIVE_BUFFER *DhcpRxBuf
967 )
968 {
969 UNION_PTR LocalPtr;
970
971 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_MESSAGE_TYPE_IX - 1];
972
973 //
974 // check that an ACK
975 // if a DHCP type, must be DHCPOFFER and must have server id
976 //
977 return (BOOLEAN)
978 (
979 (LocalPtr.OpPtr) &&
980 (LocalPtr.MessageType->Type == DHCPACK) &&
981 DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1]
982 );
983 }
984
985 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
986
987 //
988 // if a discover type packet, make sure all required fields are present
989 //
990 STATIC
991 BOOLEAN
992 DHCPOfferAckEdit (
993 DHCP_RECEIVE_BUFFER *DhcpRxBuf
994 )
995 {
996 PXE_OP_SERVER_LIST *BootServerOpPtr;
997 UNION_PTR LocalPtr;
998
999 if ((DhcpRxBuf->OpAdds.Status & DISCOVER_TYPE) == 0) {
1000 return TRUE;
1001 }
1002
1003 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_CONTROL_IX - 1];
1004
1005 if (LocalPtr.OpPtr == NULL) {
1006 LocalPtr.OpPtr = (DHCPV4_OP_STRUCT *) &DefaultDisCtl;
1007 DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_CONTROL_IX - 1] = (DHCPV4_OP_STRUCT *) &DefaultDisCtl;
1008 }
1009 //
1010 // make sure all required fields are here
1011 // if mucticast enabled, need multicast address
1012 //
1013 if (!(LocalPtr.DiscoveryControl->ControlBits & DISABLE_MCAST) &&
1014 (!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))
1015 ) {
1016 return FALSE;
1017 //
1018 // missing required field
1019 //
1020 }
1021 //
1022 // if a list, it better be good
1023 //
1024 BootServerOpPtr = (PXE_OP_SERVER_LIST *) DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_SERVERS_IX - 1];
1025
1026 if (BootServerOpPtr != NULL) {
1027 PXE_SERVER_LIST *BootServerListPtr;
1028 INTN ServerListLen;
1029 INTN ServerEntryLen;
1030
1031 BootServerListPtr = BootServerOpPtr->ServerList;
1032 ServerListLen = BootServerOpPtr->Header.Length;
1033
1034 do {
1035 EFI_IPv4_ADDRESS *IpListPtr;
1036 INTN IpCnt;
1037
1038 IpCnt = BootServerListPtr->u.Ipv4List.IpCount;
1039
1040 ServerEntryLen = sizeof (PXEV4_SERVER_LIST) + 2 + (IpCnt - 1) * sizeof (EFI_IPv4_ADDRESS);
1041
1042 if (ServerListLen < ServerEntryLen) {
1043 //
1044 // missing required field
1045 //
1046 return FALSE;
1047 }
1048
1049 IpListPtr = BootServerListPtr->u.Ipv4List.IpList;
1050
1051 while (IpCnt--) {
1052 if (IS_MULTICAST (IpListPtr)) {
1053 //
1054 // missing required field
1055 //
1056 return FALSE;
1057 } else {
1058 ++IpListPtr;
1059 }
1060 }
1061
1062 BootServerListPtr = (PXE_SERVER_LIST *) IpListPtr;
1063 } while (ServerListLen -= ServerEntryLen);
1064 }
1065 //
1066 // else there must be a list if use list enabled or multicast and
1067 // broadcast disabled
1068 //
1069 else if ((LocalPtr.DiscoveryControl->ControlBits & USE_ACCEPT_LIST) ||
1070 ((LocalPtr.DiscoveryControl->ControlBits & (DISABLE_MCAST | DISABLE_BCAST)) == (DISABLE_MCAST | DISABLE_BCAST))
1071 ) {
1072 //
1073 // missing required field
1074 //
1075 return FALSE;
1076 }
1077 //
1078 // if not USE_BOOTFILE or no bootfile given, must have menu stuff
1079 //
1080 if (!(LocalPtr.DiscoveryControl->ControlBits & USE_BOOTFILE) ||
1081 !DhcpRxBuf->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1]
1082 ) {
1083 INTN MenuLth;
1084
1085 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_MENU_IX - 1];
1086
1087 if (LocalPtr.OpPtr == NULL || !DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_PROMPT_IX - 1]) {
1088 //
1089 // missing required field
1090 //
1091 return FALSE;
1092 }
1093 //
1094 // make sure menu valid
1095 //
1096 MenuLth = LocalPtr.BootMenu->Header.Length;
1097 LocalPtr.BootMenuItem = LocalPtr.BootMenu->MenuItem;
1098
1099 do {
1100 INTN MenuItemLen;
1101
1102 MenuItemLen = LocalPtr.BootMenuItem->DataLen;
1103
1104 if (MenuItemLen == 0) {
1105 //
1106 // missing required field
1107 //
1108 return FALSE;
1109 }
1110
1111 MenuItemLen += sizeof (*LocalPtr.BootMenuItem) - sizeof (LocalPtr.BootMenuItem->Data);
1112
1113 MenuLth -= MenuItemLen;
1114 LocalPtr.BytePtr += MenuItemLen;
1115 } while (MenuLth > 0);
1116
1117 if (MenuLth != 0) {
1118 //
1119 // missing required field
1120 //
1121 return FALSE;
1122 }
1123 }
1124
1125 if (!DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1]) {
1126 DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1] = (DHCPV4_OP_STRUCT *) &DefaultBootItem;
1127 }
1128
1129 return TRUE;
1130 }
1131
1132 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1133 STATIC
1134 BOOLEAN
1135 DHCPAckEdit (
1136 DHCP_RECEIVE_BUFFER *RxBufPtr
1137 )
1138 {
1139 return (BOOLEAN) (DHCPOfferAckEdit (RxBufPtr) ? AckEdit (RxBufPtr) : FALSE);
1140 }
1141
1142 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1143
1144 //
1145 // get an offer/ack
1146 //
1147 STATIC
1148 EFI_STATUS
1149 GetOfferAck (
1150 PXE_BASECODE_DEVICE *Private,
1151 BOOLEAN (*ExtraEdit)(DHCP_RECEIVE_BUFFER *DhcpRxBuf),
1152 UINT16 OpFlags, // for Udp read
1153 EFI_IP_ADDRESS *ServerIpPtr,
1154 EFI_PXE_BASE_CODE_UDP_PORT *ServerPortPtr,
1155 EFI_IP_ADDRESS *ClientIpPtr,
1156 EFI_PXE_BASE_CODE_UDP_PORT *ClientPortPtr,
1157 DHCP_RECEIVE_BUFFER *DhcpRxBuf,
1158 EFI_EVENT TimeoutEvent
1159 )
1160 /*++
1161 Routine description:
1162 Wait for an OFFER/ACK packet.
1163
1164 Parameters:
1165 Private := Pointer to PxeBc interface
1166 ExtraEdit := Pointer to extra option checking function
1167 OpFlags := UdpRead() option flags
1168 ServerIpPtr :=
1169 ServerPortPtr :=
1170 ClientIpPtr :=
1171 ClientPortPtr :=
1172 DhcpRxBuf :=
1173 TimeoutEvent :=
1174
1175 Returns:
1176 --*/
1177 {
1178 EFI_IP_ADDRESS ServerIp;
1179 EFI_STATUS StatCode;
1180 INTN RxBufLen;
1181
1182 for (;;) {
1183 //
1184 // Wait until we get a UDP packet.
1185 //
1186 ZeroMem (&ServerIp, sizeof (EFI_IP_ADDRESS));
1187 RxBufLen = sizeof RxBuf[0].u.ReceiveBuffer;
1188
1189 if ((StatCode = UdpRead (
1190 Private,
1191 OpFlags,
1192 ClientIpPtr,
1193 ClientPortPtr,
1194 ServerIpPtr,
1195 ServerPortPtr,
1196 0,
1197 0,
1198 (UINTN *) &RxBufLen,
1199 &DhcpRxBuf->u.Dhcpv4,
1200 TimeoutEvent
1201 )) != EFI_SUCCESS) {
1202 if (StatCode == EFI_TIMEOUT) {
1203 StatCode = EFI_NO_RESPONSE;
1204 }
1205
1206 break;
1207 }
1208 //
1209 // got a packet - see if a good offer
1210 //
1211 if (DhcpRxBuf->u.Dhcpv4.op != BOOTP_REPLY) {
1212 continue;
1213 }
1214
1215 if (DhcpRxBuf->u.Dhcpv4.xid != DHCPV4_TRANSMIT_BUFFER.xid) {
1216 continue;
1217 }
1218
1219 if (*(UINT32 *) DHCPV4_TRANSMIT_BUFFER.options != * (UINT32 *) DhcpRxBuf->u.Dhcpv4.options) {
1220 continue;
1221 }
1222
1223 if (*(UINT8 *) &DhcpRxBuf->u.Dhcpv4.yiaddr > 223) {
1224 continue;
1225 }
1226
1227 if (CompareMem (
1228 DhcpRxBuf->u.Dhcpv4.chaddr,
1229 DHCPV4_TRANSMIT_BUFFER.chaddr,
1230 sizeof DhcpRxBuf->u.Dhcpv4.chaddr
1231 )) {
1232 //
1233 // no good
1234 //
1235 continue;
1236 }
1237
1238 Parse (DhcpRxBuf, RxBufLen);
1239
1240 if (!(*ExtraEdit) (DhcpRxBuf)) {
1241 continue;
1242 }
1243 //
1244 // Good DHCP packet.
1245 //
1246 StatCode = EFI_SUCCESS;
1247 break;
1248 }
1249
1250 return StatCode;
1251 }
1252
1253 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1254
1255 //
1256 // get DHCPOFFER's
1257 //
1258 STATIC
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 STATIC
2157 BOOLEAN
2158 InServerList (
2159 EFI_IP_ADDRESS *ServerIpPtr,
2160 PXE_SERVER_LISTS *ServerListPtr
2161 )
2162 {
2163 UINTN Index;
2164
2165 if (!ServerListPtr || !ServerListPtr->Ipv4List.IpCount) {
2166 return TRUE;
2167 }
2168
2169 for (Index = 0; Index < ServerListPtr->Ipv4List.IpCount; ++Index) {
2170 if (!CompareMem (
2171 ServerIpPtr,
2172 &ServerListPtr->Ipv4List.IpList[Index],
2173 sizeof (EFI_IPv4_ADDRESS)
2174 )) {
2175 return TRUE;
2176 }
2177 }
2178
2179 return FALSE;
2180 }
2181
2182 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2183 STATIC
2184 BOOLEAN
2185 ExtractBootServerList (
2186 UINT16 Type,
2187 DHCPV4_OP_STRUCT *ptr,
2188 PXE_SERVER_LISTS **ServerListPtr
2189 )
2190 {
2191 UNION_PTR LocalPtr;
2192 INTN ServerListLen;
2193
2194 LocalPtr.OpPtr = ptr;
2195 ServerListLen = LocalPtr.BootServersStr->Header.Length;
2196
2197 //
2198 // find type
2199 //
2200 LocalPtr.BootServerList = LocalPtr.BootServersStr->ServerList;
2201
2202 while (ServerListLen) {
2203 INTN ServerEntryLen;
2204
2205 ServerEntryLen = sizeof (PXEV4_SERVER_LIST) + 2 + (LocalPtr.BootServerList->u.Ipv4List.IpCount - 1) *
2206 sizeof (EFI_IPv4_ADDRESS);
2207
2208 if (NTOHS (LocalPtr.BootServerList->Type) == Type) {
2209 *ServerListPtr = &LocalPtr.BootServerList->u;
2210 return TRUE;
2211 }
2212
2213 (LocalPtr.BytePtr) += ServerEntryLen;
2214 ServerListLen -= ServerEntryLen;
2215 }
2216
2217 return FALSE;
2218 }
2219
2220 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2221 STATIC
2222 VOID
2223 FreeMem (
2224 PXE_BASECODE_DEVICE *Private
2225 )
2226 {
2227 if (Private->TransmitBuffer != NULL) {
2228 gBS->FreePool (Private->TransmitBuffer);
2229 Private->TransmitBuffer = NULL;
2230 }
2231
2232 if (Private->ReceiveBuffers != NULL) {
2233 gBS->FreePool (Private->ReceiveBuffers);
2234 Private->ReceiveBuffers = NULL;
2235 }
2236 }
2237
2238 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2239 STATIC
2240 BOOLEAN
2241 GetMem (
2242 PXE_BASECODE_DEVICE *Private
2243 )
2244 {
2245 EFI_STATUS Status;
2246
2247 if (Private->DhcpPacketBuffer == NULL) {
2248 Status = gBS->AllocatePool (
2249 EfiBootServicesData,
2250 sizeof (DHCP_RECEIVE_BUFFER) * (PXE_BIS_INDEX + 1),
2251 &Private->DhcpPacketBuffer
2252 );
2253
2254 if (EFI_ERROR (Status) || Private->DhcpPacketBuffer == NULL) {
2255 Private->DhcpPacketBuffer = NULL;
2256 FreeMem (Private);
2257 return FALSE;
2258 }
2259 }
2260
2261 Status = gBS->AllocatePool (
2262 EfiBootServicesData,
2263 sizeof (EFI_PXE_BASE_CODE_PACKET),
2264 &Private->TransmitBuffer
2265 );
2266
2267 if (EFI_ERROR (Status) || Private->TransmitBuffer == NULL) {
2268 gBS->FreePool (Private->DhcpPacketBuffer);
2269 Private->DhcpPacketBuffer = NULL;
2270 Private->TransmitBuffer = NULL;
2271 FreeMem (Private);
2272 return FALSE;
2273 }
2274
2275 Status = gBS->AllocatePool (
2276 EfiBootServicesData,
2277 sizeof (DHCP_RECEIVE_BUFFER) * (MAX_OFFERS),
2278 &Private->ReceiveBuffers
2279 );
2280
2281 if (EFI_ERROR (Status) || Private->ReceiveBuffers == NULL) {
2282 gBS->FreePool (Private->TransmitBuffer);
2283 gBS->FreePool (Private->DhcpPacketBuffer);
2284 Private->DhcpPacketBuffer = NULL;
2285 Private->TransmitBuffer = NULL;
2286 Private->ReceiveBuffers = NULL;
2287 FreeMem (Private);
2288 return FALSE;
2289 }
2290
2291 return TRUE;
2292 }
2293
2294 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2295 EFI_STATUS
2296 EFIAPI
2297 BcDhcp (
2298 IN EFI_PXE_BASE_CODE_PROTOCOL *This,
2299 IN BOOLEAN SortOffers
2300 )
2301 /*++
2302 Routine description:
2303 standard DHCP Discover/Offer/Request/Ack session
2304 broadcast DHCPDISCOVER
2305 receive DHCPOFFER's
2306 broadcast DHCPREQUEST
2307 receive DHCPACK
2308 check (ARP) good IP
2309
2310 Parameters:
2311 This := Pointer to PxeBc interface
2312 SortOffers :=
2313
2314 Returns:
2315 --*/
2316 {
2317 EFI_PXE_BASE_CODE_IP_FILTER Filter;
2318 EFI_PXE_BASE_CODE_MODE *PxebcMode;
2319 PXE_BASECODE_DEVICE *Private;
2320 EFI_STATUS StatCode;
2321
2322 //
2323 // Lock the instance data and make sure started
2324 //
2325 StatCode = EFI_SUCCESS;
2326
2327 if (This == NULL) {
2328 DEBUG ((EFI_D_ERROR, "BC *This pointer == NULL"));
2329 return EFI_INVALID_PARAMETER;
2330 }
2331
2332 Private = CR (This, PXE_BASECODE_DEVICE, EfiBc, PXE_BASECODE_DEVICE_SIGNATURE);
2333
2334 if (Private == NULL) {
2335 DEBUG ((EFI_D_ERROR, "PXE_BASECODE_DEVICE pointer == NULL"));
2336 return EFI_INVALID_PARAMETER;
2337 }
2338
2339 EfiAcquireLock (&Private->Lock);
2340
2341 if (This->Mode == NULL || !This->Mode->Started) {
2342 DEBUG ((EFI_D_ERROR, "BC was not started."));
2343 EfiReleaseLock (&Private->Lock);
2344 return EFI_NOT_STARTED;
2345 }
2346
2347 Filter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;
2348 Filter.IpCnt = 0;
2349 Filter.reserved = 0;
2350
2351 DEBUG ((EFI_D_INFO, "\nBcDhcp() Enter. "));
2352
2353 PxebcMode = Private->EfiBc.Mode;
2354
2355 if (!GetMem (Private)) {
2356 DEBUG ((EFI_D_ERROR, "\nBcDhcp() GetMem() failed.\n"));
2357 EfiReleaseLock (&Private->Lock);
2358 return EFI_OUT_OF_RESOURCES;
2359 }
2360
2361 PxebcMode->DhcpDiscoverValid = FALSE;
2362 PxebcMode->DhcpAckReceived = FALSE;
2363 PxebcMode->ProxyOfferReceived = FALSE;
2364
2365 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;
2366
2367 //
2368 // Issue BC command
2369 //
2370 if (Private->TotalSeconds == 0) {
2371 //
2372 // put in seconds field of DHCP send packets
2373 //
2374 Private->TotalSeconds = 4;
2375 }
2376
2377 if ((StatCode = DoDhcpDora (Private, SortOffers)) == EFI_SUCCESS) {
2378 //
2379 // success - copy packets
2380 //
2381 PxebcMode->DhcpDiscoverValid = PxebcMode->DhcpAckReceived = TRUE;
2382
2383 CopyMem (
2384 &PxebcMode->DhcpAck,
2385 (EFI_PXE_BASE_CODE_PACKET *) &DHCPV4_ACK_PACKET,
2386 sizeof (EFI_PXE_BASE_CODE_PACKET)
2387 );
2388
2389 if (PxebcMode->ProxyOfferReceived) {
2390 CopyMem (
2391 &PxebcMode->ProxyOffer,
2392 (EFI_PXE_BASE_CODE_PACKET *) &PXE_OFFER_PACKET,
2393 sizeof (EFI_PXE_BASE_CODE_PACKET)
2394 );
2395 }
2396 }
2397 //
2398 // set filter back to unicast
2399 //
2400 IpFilter (Private, &Filter);
2401
2402 FreeMem (Private);
2403
2404 //
2405 // Unlock the instance data
2406 //
2407 DEBUG ((EFI_D_WARN, "\nBcDhcp() Exit = %xh ", StatCode));
2408
2409 EfiReleaseLock (&Private->Lock);
2410 return StatCode;
2411 }
2412
2413 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2414 STATIC
2415 BOOLEAN
2416 VerifyCredentialOption (
2417 UINT8 *tx,
2418 UINT8 *rx
2419 )
2420 {
2421 UINTN n;
2422
2423 //
2424 // Fail verification if either pointer is NULL.
2425 //
2426 if (tx == NULL || rx == NULL) {
2427 return FALSE;
2428 }
2429 //
2430 // Fail verification if tx[0] is not a credential type option
2431 // or if the length is zero or not a multiple of four.
2432 //
2433 if (tx[0] != VEND_PXE_CREDENTIAL_TYPES || tx[1] == 0 || tx[1] % 4 != 0) {
2434 return FALSE;
2435 }
2436 //
2437 // Fail verification if rx[0] is not a credential type option
2438 // or if the length is not equal to four.
2439 //
2440 if (rx[0] != VEND_PXE_CREDENTIAL_TYPES || rx[1] != 4) {
2441 return FALSE;
2442 }
2443 //
2444 // Look through transmitted credential types for a copy
2445 // of the received credential type.
2446 //
2447 for (n = 0; n < tx[1]; n += 4) {
2448 if (!CompareMem (&tx[n + 2], &rx[2], 4)) {
2449 return TRUE;
2450 }
2451 }
2452
2453 return FALSE;
2454 }
2455
2456 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2457 STATIC
2458 EFI_STATUS
2459 DoDiscover (
2460 PXE_BASECODE_DEVICE *Private,
2461 UINT16 OpFlags,
2462 IN UINT16 Type,
2463 IN UINT16 *LayerPtr,
2464 IN BOOLEAN UseBis,
2465 EFI_IP_ADDRESS *DestPtr,
2466 PXE_SERVER_LISTS *ServerListPtr
2467 )
2468 /*++
2469 Routine description:
2470 This function tries to complete the PXE Bootserver and/or boot image
2471 discovery sequence. When this command completes successfully, the
2472 PXEdiscover and PXEreply fields in the BC instance data structure are
2473 updated. If the Info pointer is set to NULL, the discovery information
2474 in the DHCPack and ProxyOffer packets must be valid and will be used.
2475 If Info is not set to NULL, the discovery methods in the Info field
2476 must be set and will be used. When discovering any layer number other
2477 than zero (the credential flag does not count), only unicast discovery
2478 is used.
2479
2480 Parameters:
2481 Private := Pointer to PxeBc interface
2482 OpFlags :=
2483 Type :=
2484 LayerPtr :=
2485 UseBis :=
2486 DestPtr :=
2487 ServerListPtr :=
2488
2489 Returns:
2490 --*/
2491 {
2492 EFI_PXE_BASE_CODE_UDP_PORT ClientPort;
2493 EFI_PXE_BASE_CODE_UDP_PORT ServerPort;
2494 EFI_PXE_BASE_CODE_MODE *PxebcMode;
2495 EFI_STATUS StatCode;
2496 EFI_EVENT TimeoutEvent;
2497 UINT8 OpLen;
2498
2499 PxebcMode = Private->EfiBc.Mode;
2500
2501 if (DestPtr->Addr[0] == 0) {
2502 DEBUG ((EFI_D_WARN, "\nDoDiscover() !DestPtr->Addr[0]"));
2503 return EFI_INVALID_PARAMETER;
2504 }
2505 //
2506 // seed random number with hardware address
2507 //
2508 SeedRandom (Private, *(UINT16 *) &Private->SimpleNetwork->Mode->CurrentAddress);
2509
2510 if (DestPtr->Addr[0] == BroadcastIP.Addr[0]) {
2511 ClientPort = DHCPClientPort;
2512 ServerPort = DhcpServerPort;
2513 } else {
2514 ClientPort = PSEUDO_DHCP_CLIENT_PORT;
2515 ServerPort = PseudoDhcpServerPort;
2516 }
2517
2518 if (UseBis) {
2519 *LayerPtr |= PXE_BOOT_LAYER_CREDENTIAL_FLAG;
2520 } else {
2521 *LayerPtr &= PXE_BOOT_LAYER_MASK;
2522 }
2523
2524 for (Private->Timeout = 1;
2525 Private->Timeout < 5;
2526 Private->TotalSeconds = (UINT16) (Private->TotalSeconds + Private->Timeout), ++Private->Timeout
2527 ) {
2528 InitDhcpv4TxBuf (Private);
2529 //
2530 // initialize DHCP message structure
2531 //
2532 DHCPV4_TRANSMIT_BUFFER.xid = Random (Private);
2533 DHCPV4_TRANSMIT_BUFFER.secs = HTONS (Private->TotalSeconds);
2534 CopyMem (
2535 &DHCPV4_TRANSMIT_BUFFER.ciaddr,
2536 &PxebcMode->StationIp,
2537 sizeof DHCPV4_TRANSMIT_BUFFER.ciaddr
2538 );
2539
2540 DHCPV4_OPTIONS_BUFFER.DhcpMessageType.Type = DHCPREQUEST;
2541 DISCOVERoptions.Header.OpCode = OP_VENDOR_SPECIFIC;
2542 DISCOVERoptions.BootItem.Header.OpCode = VEND_PXE_BOOT_ITEM;
2543 DISCOVERoptions.BootItem.Header.Length = DHCPV4_OPTION_LENGTH (PXE_OP_BOOT_ITEM);
2544 DISCOVERoptions.BootItem.Type = HTONS (Type);
2545 DISCOVERoptions.BootItem.Layer = HTONS (*LayerPtr);
2546
2547 if (UseBis) {
2548 EFI_BIS_PROTOCOL *BisPtr;
2549 BIS_APPLICATION_HANDLE BisAppHandle;
2550 EFI_BIS_DATA *BisDataSigInfo;
2551 EFI_BIS_SIGNATURE_INFO *BisSigInfo;
2552 UINTN Index;
2553 UINTN Index2;
2554
2555 BisPtr = PxebcBisStart (
2556 Private,
2557 &BisAppHandle,
2558 &BisDataSigInfo
2559 );
2560
2561 if (BisPtr == NULL) {
2562 //
2563 // %%TBD - In order to get here, BIS must have
2564 // been present when PXEBC.Start() was called.
2565 // BIS had to be shutdown/removed/damaged
2566 // before PXEBC.Discover() was called.
2567 // Do we need to document a specific error
2568 // for this case?
2569 //
2570 return EFI_OUT_OF_RESOURCES;
2571 }
2572 //
2573 // Compute number of credential types.
2574 //
2575 Index2 = BisDataSigInfo->Length / sizeof (EFI_BIS_SIGNATURE_INFO);
2576
2577 DISCREDoptions.Header.OpCode = VEND_PXE_CREDENTIAL_TYPES;
2578
2579 DISCREDoptions.Header.Length = (UINT8) (Index2 * sizeof (PXE_CREDENTIAL));
2580
2581 OpLen = (UINT8) (DHCPV4_OPTION_LENGTH (PXE_DISCOVER_OPTIONS) + sizeof (DHCPV4_OP_HEADER) + DISCREDoptions.Header.Length);
2582
2583 BisSigInfo = (EFI_BIS_SIGNATURE_INFO *) BisDataSigInfo->Data;
2584
2585 for (Index = 0; Index < Index2; ++Index) {
2586 UINT32 x;
2587
2588 CopyMem (&x, &BisSigInfo[Index], sizeof x);
2589 x = HTONL (x);
2590 CopyMem (&DISCREDoptions.Credentials[Index], &x, sizeof x);
2591 }
2592
2593 PxebcBisStop (BisPtr, BisAppHandle, BisDataSigInfo);
2594 } else {
2595 OpLen = DHCPV4_OPTION_LENGTH (PXE_DISCOVER_OPTIONS);
2596 }
2597
2598 DISCOVERoptions.Header.Length = OpLen;
2599
2600 ((UINT8 *) &DISCOVERoptions)[sizeof (DHCPV4_OP_HEADER) + OpLen - 1] = OP_END;
2601 ((UINT8 *) &DISCOVERoptions)[sizeof (DHCPV4_OP_HEADER) + OpLen] = OP_END;
2602
2603 StatCode = DoUdpWrite (
2604 Private,
2605 DestPtr,
2606 &ServerPort,
2607 (EFI_IP_ADDRESS *) &DHCPV4_TRANSMIT_BUFFER.ciaddr,
2608 &ClientPort
2609 );
2610
2611 if (StatCode != EFI_SUCCESS) {
2612 return StatCode;
2613 }
2614 //
2615 //
2616 //
2617 StatCode = gBS->CreateEvent (
2618 EFI_EVENT_TIMER,
2619 EFI_TPL_CALLBACK,
2620 NULL,
2621 NULL,
2622 &TimeoutEvent
2623 );
2624
2625 if (EFI_ERROR (StatCode)) {
2626 return StatCode;
2627 }
2628
2629 StatCode = gBS->SetTimer (
2630 TimeoutEvent,
2631 TimerRelative,
2632 Private->Timeout * 10000000 + 1000000
2633 );
2634
2635 if (EFI_ERROR (StatCode)) {
2636 gBS->CloseEvent (TimeoutEvent);
2637 return StatCode;
2638 }
2639 //
2640 // wait for ACK
2641 //
2642 for (;;) {
2643 DHCP_RECEIVE_BUFFER *RxBufPtr;
2644 UINT16 TmpType;
2645 UINT16 TmpLayer;
2646
2647 RxBufPtr = UseBis ? &PXE_BIS_BUFFER : &PXE_ACK_BUFFER;
2648 ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));
2649
2650 if (GetOfferAck (
2651 Private,
2652 AckEdit,
2653 OpFlags,
2654 (EFI_IP_ADDRESS *) &Private->ServerIp,
2655 0,
2656 (EFI_IP_ADDRESS *) &DHCPV4_TRANSMIT_BUFFER.ciaddr,
2657 &ClientPort,
2658 RxBufPtr,
2659 TimeoutEvent
2660 ) != EFI_SUCCESS) {
2661 break;
2662 }
2663 //
2664 // check type of response - need PXEClient DHCPACK of proper type with bootfile
2665 //
2666 if (!(RxBufPtr->OpAdds.Status & PXE_TYPE) ||
2667 (UseBis && (RxBufPtr->OpAdds.Status & USE_THREE_BYTE)) ||
2668 !RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_BOOTFILE_IX - 1] ||
2669 !RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX - 1] ||
2670 !InServerList((EFI_IP_ADDRESS *)&((DHCPV4_OP_SERVER_IP *)RxBufPtr->OpAdds.PktOptAdds[OP_DHCP_SERVER_IP_IX-1])->Ip, ServerListPtr)) {
2671
2672 continue;
2673 }
2674
2675 TmpType = TmpLayer = 0;
2676
2677 if (RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1]) {
2678 TmpType = NTOHS (((PXE_OP_BOOT_ITEM *) RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1])->Type);
2679
2680 if (RxBufPtr->OpAdds.Status & USE_THREE_BYTE) {
2681 TmpLayer = (UINT16) (((PXE_OP_BOOT_ITEM *) RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1])->Layer >> 8);
2682 } else {
2683 TmpLayer = NTOHS (((PXE_OP_BOOT_ITEM *) RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_BOOT_ITEM_IX - 1])->Layer);
2684 }
2685 }
2686
2687 if (TmpType != Type) {
2688 continue;
2689 }
2690
2691 if (UseBis) {
2692 if (!RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_CREDENTIAL_TYPES_IX - 1]) {
2693 continue;
2694 }
2695
2696 if (!VerifyCredentialOption (
2697 (UINT8 *) &DISCREDoptions.Header,
2698 (UINT8 *) RxBufPtr->OpAdds.PxeOptAdds[VEND_PXE_CREDENTIAL_TYPES_IX - 1]
2699 )) {
2700 continue;
2701 }
2702 }
2703
2704 *LayerPtr = TmpLayer;
2705
2706 if (UseBis) {
2707 CopyMem (
2708 &PxebcMode->PxeBisReply,
2709 &RxBufPtr->u.Dhcpv4,
2710 sizeof (EFI_PXE_BASE_CODE_PACKET)
2711 );
2712
2713 PxebcMode->PxeBisReplyReceived = TRUE;
2714
2715 StatCode = DoDiscover (
2716 Private,
2717 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
2718 Type,
2719 LayerPtr,
2720 FALSE,
2721 &Private->ServerIp,
2722 0
2723 );
2724
2725 gBS->CloseEvent (TimeoutEvent);
2726 return StatCode;
2727 }
2728
2729 PxebcMode->PxeDiscoverValid = PxebcMode->PxeReplyReceived = TRUE;
2730
2731 CopyMem (
2732 &PxebcMode->PxeDiscover,
2733 &*(EFI_PXE_BASE_CODE_PACKET *) &DHCPV4_TRANSMIT_BUFFER,
2734 sizeof (*(EFI_PXE_BASE_CODE_PACKET *) &DHCPV4_TRANSMIT_BUFFER)
2735 );
2736
2737 CopyMem (
2738 &PxebcMode->PxeReply,
2739 &*(EFI_PXE_BASE_CODE_PACKET *) &RxBufPtr->u.Dhcpv4,
2740 sizeof (*(EFI_PXE_BASE_CODE_PACKET *) &RxBufPtr->u.Dhcpv4)
2741 );
2742
2743 AddRouters (Private, RxBufPtr);
2744
2745 gBS->CloseEvent (TimeoutEvent);
2746 return EFI_SUCCESS;
2747 }
2748
2749 gBS->CloseEvent (TimeoutEvent);
2750 }
2751 //
2752 // end for loop
2753 //
2754 return EFI_TIMEOUT;
2755 }
2756
2757 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2758 STATIC
2759 EFI_STATUS
2760 Discover (
2761 PXE_BASECODE_DEVICE *Private,
2762 IN UINT16 Type,
2763 IN UINT16 *LayerPtr,
2764 IN BOOLEAN UseBis,
2765 IN EFI_PXE_BASE_CODE_DISCOVER_INFO *DiscoverInfoPtr,
2766 PXE_SERVER_LISTS *McastServerListPtr,
2767 PXE_SERVER_LISTS *ServerListPtr
2768 )
2769 /*++
2770 Routine Description:
2771
2772 Parameters:
2773 Private := Pointer to PxeBc interface
2774 Type :=
2775 LayerPtr :=
2776 UseBis :=
2777 DiscoverInfoPtr :=
2778 McastServerListPtr :=
2779 ServerListPtr :=
2780
2781 Returns:
2782 --*/
2783 {
2784 EFI_IP_ADDRESS DestIp;
2785 EFI_STATUS StatCode;
2786
2787 DEBUG ((EFI_D_INFO, "\nDiscover() Type=%d Layer=%d ", Type, *LayerPtr));
2788
2789 if (UseBis) {
2790 DEBUG ((EFI_D_INFO, "BIS "));
2791 }
2792 //
2793 // get dest IP addr - mcast, bcast, or unicast
2794 //
2795 if (DiscoverInfoPtr->UseMCast) {
2796 DestIp.v4 = DiscoverInfoPtr->ServerMCastIp.v4;
2797
2798 DEBUG (
2799 (EFI_D_INFO,
2800 "\nDiscover() MCast %d.%d.%d.%d ",
2801 DestIp.v4.Addr[0],
2802 DestIp.v4.Addr[1],
2803 DestIp.v4.Addr[2],
2804 DestIp.v4.Addr[3])
2805 );
2806
2807 if ((StatCode = DoDiscover (
2808 Private,
2809 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
2810 Type,
2811 LayerPtr,
2812 UseBis,
2813 &DestIp,
2814 McastServerListPtr
2815 )) != EFI_TIMEOUT) {
2816 DEBUG (
2817 (EFI_D_WARN,
2818 "\nDiscover() status == %r (%Xh)",
2819 StatCode,
2820 StatCode)
2821 );
2822
2823 return StatCode;
2824 }
2825 }
2826
2827 if (DiscoverInfoPtr->UseBCast) {
2828 DEBUG ((EFI_D_INFO, "\nDiscver() BCast "));
2829
2830 if ((StatCode = DoDiscover (
2831 Private,
2832 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
2833 Type,
2834 LayerPtr,
2835 UseBis,
2836 &BroadcastIP,
2837 McastServerListPtr
2838 )) != EFI_TIMEOUT) {
2839
2840 DEBUG ((EFI_D_WARN, "\nDiscover() status == %r (%Xh)", StatCode, StatCode));
2841
2842 return StatCode;
2843 }
2844 }
2845
2846 if (DiscoverInfoPtr->UseUCast) {
2847 UINTN Index;
2848
2849 DEBUG (
2850 (EFI_D_INFO,
2851 "\nDiscover() UCast IP#=%d ",
2852 ServerListPtr->Ipv4List.IpCount)
2853 );
2854
2855 for (Index = 0; Index < ServerListPtr->Ipv4List.IpCount; ++Index) {
2856 CopyMem (&DestIp, &ServerListPtr->Ipv4List.IpList[Index], 4);
2857
2858 DEBUG (
2859 (EFI_D_INFO,
2860 "\nDiscover() UCast %d.%d.%d.%d ",
2861 DestIp.v4.Addr[0],
2862 DestIp.v4.Addr[1],
2863 DestIp.v4.Addr[2],
2864 DestIp.v4.Addr[3])
2865 );
2866
2867 if ((StatCode = DoDiscover (
2868 Private,
2869 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT,
2870 Type,
2871 LayerPtr,
2872 UseBis,
2873 &DestIp,
2874 0
2875 )) != EFI_TIMEOUT) {
2876 DEBUG (
2877 (EFI_D_WARN,
2878 "\nDiscover() status == %r (%Xh)",
2879 StatCode,
2880 StatCode)
2881 );
2882
2883 return StatCode;
2884 }
2885 }
2886 }
2887
2888 DEBUG ((EFI_D_WARN, "\nDiscover() TIMEOUT"));
2889
2890 return EFI_TIMEOUT;
2891 }
2892
2893 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2894
2895 /* BcDiscover()
2896 */
2897 EFI_STATUS
2898 EFIAPI
2899 BcDiscover (
2900 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
2901 IN UINT16 Type,
2902 IN UINT16 *LayerPtr,
2903 IN BOOLEAN UseBis,
2904 IN EFI_PXE_BASE_CODE_DISCOVER_INFO * DiscoverInfoPtr OPTIONAL
2905 )
2906 /*++
2907 Routine description:
2908
2909 Parameters:
2910 This :=
2911 Type :=
2912 LayerPtr :=
2913 UseBis :=
2914 DiscoverInfoPtr :=
2915
2916 Returns:
2917 --*/
2918 {
2919 EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;
2920 EFI_PXE_BASE_CODE_MODE *PxebcMode;
2921 DHCP_RECEIVE_BUFFER *DhcpRxBuf;
2922 PXE_SERVER_LISTS DefaultSrvList;
2923 PXE_SERVER_LISTS *ServerListPtr;
2924 PXE_SERVER_LISTS *McastServerListPtr;
2925 EFI_STATUS Status;
2926 UNION_PTR LocalPtr;
2927 UINTN Index;
2928 UINTN Index2;
2929 BOOLEAN AcquiredSrvList;
2930 EFI_STATUS StatCode;
2931 PXE_BASECODE_DEVICE *Private;
2932
2933 //
2934 // Lock the instance data and make sure started
2935 //
2936 StatCode = EFI_SUCCESS;
2937
2938 if (This == NULL) {
2939 DEBUG ((EFI_D_ERROR, "BC *This pointer == NULL"));
2940 return EFI_INVALID_PARAMETER;
2941 }
2942
2943 Private = CR (This, PXE_BASECODE_DEVICE, EfiBc, PXE_BASECODE_DEVICE_SIGNATURE);
2944
2945 if (Private == NULL) {
2946 DEBUG ((EFI_D_ERROR, "PXE_BASECODE_DEVICE poiner == NULL"));
2947 return EFI_INVALID_PARAMETER;
2948 }
2949
2950 EfiAcquireLock (&Private->Lock);
2951
2952 if (This->Mode == NULL || !This->Mode->Started) {
2953 DEBUG ((EFI_D_ERROR, "BC was not started."));
2954 EfiReleaseLock (&Private->Lock);
2955 return EFI_NOT_STARTED;
2956 }
2957
2958 ServerListPtr = NULL;
2959 McastServerListPtr = NULL;
2960 AcquiredSrvList = FALSE;
2961
2962 PxebcMode = Private->EfiBc.Mode;
2963
2964 if (!GetMem (Private)) {
2965 EfiReleaseLock (&Private->Lock);
2966 return EFI_OUT_OF_RESOURCES;
2967 }
2968
2969 if (UseBis) {
2970 if (!PxebcMode->BisSupported) {
2971 EfiReleaseLock (&Private->Lock);
2972 return EFI_INVALID_PARAMETER;
2973 }
2974 }
2975
2976 Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;
2977
2978 if (Private->TotalSeconds == 0) {
2979 //
2980 // put in seconds field of DHCP send packets
2981 //
2982 Private->TotalSeconds = 4;
2983 }
2984
2985 ZeroMem (&DefaultInfo, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO));
2986
2987 //
2988 // if layer number not zero, use previous discover
2989 //
2990 if (*LayerPtr != 0) {
2991 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0"));
2992
2993 if (DiscoverInfoPtr != NULL) {
2994 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0 && DiscoverInfoPtr != NULL\n"));
2995
2996 EfiReleaseLock (&Private->Lock);
2997 return EFI_INVALID_PARAMETER;
2998 }
2999
3000 if (!PxebcMode->PxeDiscoverValid) {
3001 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0 && PxeDiscoverValid == 0\n"));
3002
3003 EfiReleaseLock (&Private->Lock);
3004 return EFI_INVALID_PARAMETER;
3005 }
3006
3007 if (!PxebcMode->PxeReplyReceived) {
3008 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0 && PxeReplyReceived == 0\n"));
3009
3010 EfiReleaseLock (&Private->Lock);
3011 return EFI_INVALID_PARAMETER;
3012 }
3013
3014 if (UseBis && !PxebcMode->PxeBisReplyReceived) {
3015 DEBUG ((EFI_D_WARN, "\nBcDiscover() layer != 0 && PxeBisReplyReceived == 0\n"));
3016
3017 EfiReleaseLock (&Private->Lock);
3018 return EFI_INVALID_PARAMETER;
3019 }
3020
3021 DefaultInfo.UseUCast = TRUE;
3022 DiscoverInfoPtr = &DefaultInfo;
3023
3024 DefaultSrvList.Ipv4List.IpCount = 1;
3025 CopyMem (&DefaultSrvList.Ipv4List.IpList[0], &Private->ServerIp, 4);
3026
3027 ServerListPtr = &DefaultSrvList;
3028 }
3029 //
3030 // layer is zero - see if info is supplied or if we need to use info from a cached offer
3031 //
3032 else if (!DiscoverInfoPtr) {
3033 //
3034 // not supplied - generate it
3035 // make sure that there is cached, appropriate information
3036 // if neither DhcpAck packet nor ProxyOffer packet has pxe info, fail
3037 //
3038 DhcpRxBuf = (PxebcMode->ProxyOfferReceived) ? &PXE_OFFER_BUFFER : &DHCPV4_ACK_BUFFER;
3039
3040 if (!PxebcMode->DhcpAckReceived || !(DhcpRxBuf->OpAdds.Status & DISCOVER_TYPE)) {
3041 DEBUG ((EFI_D_WARN, "\nBcDiscover() !ack && !proxy"));
3042 EfiReleaseLock (&Private->Lock);
3043 return EFI_INVALID_PARAMETER;
3044 }
3045
3046 DiscoverInfoPtr = &DefaultInfo;
3047
3048 LocalPtr.OpPtr = DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_CONTROL_IX - 1];
3049
3050 //
3051 // if multicast enabled, need multicast address
3052 //
3053 if (!(LocalPtr.DiscoveryControl->ControlBits & DISABLE_MCAST)) {
3054 DefaultInfo.UseMCast = TRUE;
3055
3056 CopyMem (
3057 ((EFI_IPv4_ADDRESS *) &DefaultInfo.ServerMCastIp),
3058 &((DHCPV4_OP_IP_ADDRESS *) DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_DISCOVERY_MCAST_ADDR_IX - 1])->Ip,
3059 sizeof (EFI_IPv4_ADDRESS)
3060 );
3061 }
3062
3063 DefaultInfo.UseBCast = (BOOLEAN) ((LocalPtr.DiscoveryControl->ControlBits & DISABLE_BCAST) == 0);
3064
3065 DefaultInfo.MustUseList = (BOOLEAN) ((LocalPtr.DiscoveryControl->ControlBits & USE_ACCEPT_LIST) != 0);
3066
3067 DefaultInfo.UseUCast = (BOOLEAN)
3068 (
3069 (DefaultInfo.MustUseList) ||
3070 ((LocalPtr.DiscoveryControl->ControlBits & (DISABLE_MCAST | DISABLE_BCAST)) == (DISABLE_MCAST | DISABLE_BCAST))
3071 );
3072
3073 if ((DefaultInfo.UseUCast | DefaultInfo.MustUseList) && !ExtractBootServerList (
3074 Type,
3075 DhcpRxBuf->OpAdds.PxeOptAdds[VEND_PXE_BOOT_SERVERS_IX - 1],
3076 &ServerListPtr
3077 )) {
3078 DEBUG ((EFI_D_WARN, "\nBcDiscover() type not in list"));
3079 EfiReleaseLock (&Private->Lock);
3080 return EFI_INVALID_PARAMETER;
3081 }
3082 }
3083 //
3084 // Info supplied - make SrvList if required
3085 // if we use ucast discovery or must use list, there better be one
3086 //
3087 else if (DiscoverInfoPtr->UseUCast || DiscoverInfoPtr->MustUseList) {
3088 //
3089 // there better be a list
3090 //
3091 if (DiscoverInfoPtr->IpCnt == 0) {
3092 DEBUG ((EFI_D_WARN, "\nBcDiscover() no bootserver list"));
3093 EfiReleaseLock (&Private->Lock);
3094 return EFI_INVALID_PARAMETER;
3095 }
3096 //
3097 // get its size
3098 //
3099 for (Index = Index2 = 0; Index < DiscoverInfoPtr->IpCnt; ++Index) {
3100 if (DiscoverInfoPtr->SrvList[Index].Type == Type) {
3101 if (DiscoverInfoPtr->SrvList[Index].AcceptAnyResponse) {
3102 if (Index2 != 0) {
3103 DEBUG ((EFI_D_WARN, "\nBcDiscover() accept any?"));
3104 EfiReleaseLock (&Private->Lock);
3105 return EFI_INVALID_PARAMETER;
3106 } else {
3107 Index2 = 1;
3108 DefaultSrvList.Ipv4List.IpCount = 0;
3109 ServerListPtr = &DefaultSrvList;
3110 break;
3111 }
3112 } else {
3113 ++Index2;
3114 }
3115 }
3116 }
3117
3118 if (Index2 == 0) {
3119 DEBUG ((EFI_D_WARN, "\nBcDiscover() !Index2?"));
3120 EfiReleaseLock (&Private->Lock);
3121 return EFI_INVALID_PARAMETER;
3122 }
3123
3124 if (ServerListPtr == NULL) {
3125 Status = gBS->AllocatePool (
3126 EfiBootServicesData,
3127 sizeof (PXEV4_SERVER_LIST) + (Index2 - 1) * sizeof (EFI_IPv4_ADDRESS),
3128 (VOID **) &ServerListPtr
3129 );
3130
3131 if (EFI_ERROR (Status) || ServerListPtr == NULL) {
3132 ServerListPtr = NULL;
3133 EfiReleaseLock (&Private->Lock);
3134 return EFI_OUT_OF_RESOURCES;
3135 }
3136 //
3137 // build an array of IP addresses from the server list
3138 //
3139 AcquiredSrvList = TRUE;
3140 ServerListPtr->Ipv4List.IpCount = (UINT8) Index2;
3141
3142 for (Index = Index2 = 0; Index < DiscoverInfoPtr->IpCnt; ++Index) {
3143 if (DiscoverInfoPtr->SrvList[Index].Type == Type) {
3144 CopyMem (
3145 &ServerListPtr->Ipv4List.IpList[Index2++],
3146 &DiscoverInfoPtr->SrvList[Index].IpAddr.v4,
3147 sizeof ServerListPtr->Ipv4List.IpList[0]
3148 );
3149 }
3150 }
3151 }
3152 }
3153
3154 if (DiscoverInfoPtr->MustUseList) {
3155 McastServerListPtr = ServerListPtr;
3156 }
3157
3158 if (!(DiscoverInfoPtr->UseMCast || DiscoverInfoPtr->UseBCast || DiscoverInfoPtr->UseUCast)) {
3159 DEBUG ((EFI_D_WARN, "\nBcDiscover() Nothing to use!\n"));
3160
3161 EfiReleaseLock (&Private->Lock);
3162 return EFI_INVALID_PARAMETER;
3163 }
3164
3165 PxebcMode->PxeDiscoverValid = PxebcMode->PxeReplyReceived = PxebcMode->PxeBisReplyReceived = FALSE;
3166
3167 StatCode = Discover (
3168 Private,
3169 Type,
3170 LayerPtr,
3171 UseBis,
3172 DiscoverInfoPtr,
3173 McastServerListPtr,
3174 ServerListPtr
3175 );
3176
3177 if (AcquiredSrvList) {
3178 gBS->FreePool (ServerListPtr);
3179 }
3180
3181 FreeMem (Private);
3182
3183 //
3184 // Unlock the instance data
3185 //
3186 DEBUG (
3187 (EFI_D_INFO,
3188 "\nBcDiscover() status == %r (%Xh)\n",
3189 StatCode,
3190 StatCode)
3191 );
3192
3193 EfiReleaseLock (&Private->Lock);
3194 return StatCode;
3195 }
3196
3197 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
3198 EFI_STATUS
3199 EFIAPI
3200 BcSetPackets (
3201 IN EFI_PXE_BASE_CODE_PROTOCOL * This,
3202 BOOLEAN *NewDhcpDiscoverValid, OPTIONAL
3203 BOOLEAN *NewDhcpAckReceived, OPTIONAL
3204 BOOLEAN *NewProxyOfferReceived, OPTIONAL
3205 BOOLEAN *NewPxeDiscoverValid, OPTIONAL
3206 BOOLEAN *NewPxeReplyReceived, OPTIONAL
3207 BOOLEAN *NewPxeBisReplyReceived, OPTIONAL
3208 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpDiscover, OPTIONAL
3209 IN EFI_PXE_BASE_CODE_PACKET * NewDhcpAck, OPTIONAL
3210 IN EFI_PXE_BASE_CODE_PACKET * NewProxyOffer, OPTIONAL
3211 IN EFI_PXE_BASE_CODE_PACKET * NewPxeDiscover, OPTIONAL
3212 IN EFI_PXE_BASE_CODE_PACKET * NewPxeReply, OPTIONAL
3213 IN EFI_PXE_BASE_CODE_PACKET * NewPxeBisReply OPTIONAL
3214 )
3215 /*++
3216 Routine description:
3217
3218 Parameters:
3219
3220 Returns:
3221 --*/
3222 {
3223 EFI_PXE_BASE_CODE_MODE *PxebcMode;
3224 EFI_STATUS Status;
3225 PXE_BASECODE_DEVICE *Private;
3226
3227 //
3228 // Lock the instance data and make sure started
3229 //
3230
3231 if (This == NULL) {
3232 DEBUG ((EFI_D_ERROR, "BC *This pointer == NULL"));
3233 return EFI_INVALID_PARAMETER;
3234 }
3235
3236 Private = CR (This, PXE_BASECODE_DEVICE, EfiBc, PXE_BASECODE_DEVICE_SIGNATURE);
3237
3238 if (Private == NULL) {
3239 DEBUG ((EFI_D_ERROR, "PXE_BASECODE_DEVICE poiner == NULL"));
3240 return EFI_INVALID_PARAMETER;
3241 }
3242
3243 EfiAcquireLock (&Private->Lock);
3244
3245 if (This->Mode == NULL || !This->Mode->Started) {
3246 DEBUG ((EFI_D_ERROR, "BC was not started."));
3247 EfiReleaseLock (&Private->Lock);
3248 return EFI_NOT_STARTED;
3249 }
3250
3251 PxebcMode = Private->EfiBc.Mode;
3252
3253 if (Private->DhcpPacketBuffer == NULL) {
3254 Status = gBS->AllocatePool (
3255 EfiBootServicesData,
3256 sizeof (DHCP_RECEIVE_BUFFER) * (PXE_BIS_INDEX + 1),
3257 &Private->DhcpPacketBuffer
3258 );
3259
3260 if (EFI_ERROR (Status) || Private->DhcpPacketBuffer == NULL) {
3261 Private->DhcpPacketBuffer = NULL;
3262 EfiReleaseLock (&Private->Lock);
3263 return EFI_OUT_OF_RESOURCES;
3264 }
3265 }
3266 //
3267 // Issue BC command
3268 //
3269 //
3270 // reset
3271 //
3272 Private->FileSize = 0;
3273 if (NewDhcpDiscoverValid != NULL) {
3274 PxebcMode->DhcpDiscoverValid = *NewDhcpDiscoverValid;
3275 }
3276
3277 if (NewDhcpAckReceived != NULL) {
3278 PxebcMode->DhcpAckReceived = *NewDhcpAckReceived;
3279 }
3280
3281 if (NewProxyOfferReceived != NULL) {
3282 PxebcMode->ProxyOfferReceived = *NewProxyOfferReceived;
3283 }
3284
3285 if (NewPxeDiscoverValid != NULL) {
3286 PxebcMode->PxeDiscoverValid = *NewPxeDiscoverValid;
3287 }
3288
3289 if (NewPxeReplyReceived != NULL) {
3290 PxebcMode->PxeReplyReceived = *NewPxeReplyReceived;
3291 }
3292
3293 if (NewPxeBisReplyReceived != NULL) {
3294 PxebcMode->PxeBisReplyReceived = *NewPxeBisReplyReceived;
3295 }
3296
3297 if (NewDhcpDiscover != NULL) {
3298 CopyMem (
3299 &PxebcMode->DhcpDiscover,
3300 NewDhcpDiscover,
3301 sizeof *NewDhcpDiscover
3302 );
3303 }
3304
3305 if (NewDhcpAck != NULL) {
3306 CopyParse (Private, &PxebcMode->DhcpAck, NewDhcpAck, DHCPV4_ACK_INDEX);
3307 }
3308
3309 if (NewProxyOffer != NULL) {
3310 CopyParse (Private, &PxebcMode->ProxyOffer, NewProxyOffer, PXE_OFFER_INDEX);
3311 }
3312
3313 if (NewPxeDiscover != NULL) {
3314 CopyMem (
3315 &PxebcMode->PxeDiscover,
3316 NewPxeDiscover,
3317 sizeof *NewPxeDiscover
3318 );
3319 }
3320
3321 if (NewPxeReply != NULL) {
3322 CopyParse (Private, &PxebcMode->PxeReply, NewPxeReply, PXE_ACK_INDEX);
3323 }
3324
3325 if (NewPxeBisReply != NULL) {
3326 CopyParse (Private, &PxebcMode->PxeBisReply, NewPxeBisReply, PXE_BIS_INDEX);
3327 }
3328 //
3329 // Unlock the instance data
3330 //
3331 EfiReleaseLock (&Private->Lock);
3332 return EFI_SUCCESS;
3333 }
3334
3335 /* eof - pxe_bc_dhcp.c */