MdeModulePkg: Replace ASSERT with error return code in PXE driver.
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcDhcp4.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Functions implementation related with DHCPv4 for UefiPxeBc Driver.\r
3\r
aa437778 4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
a3bcde70
HT
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "PxeBcImpl.h"\r
17\r
18//\r
19// This is a map from the interested DHCP4 option tags' index to the tag value.\r
20//\r
21UINT8 mInterestedDhcp4Tags[PXEBC_DHCP4_TAG_INDEX_MAX] = {\r
142c00c3
ZL
22 DHCP4_TAG_BOOTFILE_LEN,\r
23 DHCP4_TAG_VENDOR,\r
24 DHCP4_TAG_OVERLOAD,\r
25 DHCP4_TAG_MSG_TYPE,\r
26 DHCP4_TAG_SERVER_ID,\r
27 DHCP4_TAG_VENDOR_CLASS_ID,\r
28 DHCP4_TAG_BOOTFILE\r
a3bcde70
HT
29};\r
30\r
31//\r
32// There are 4 times retries with the value of 4, 8, 16 and 32, refers to PXE2.1 spec.\r
33//\r
34UINT32 mPxeDhcpTimeout[4] = {4, 8, 16, 32};\r
35\r
36\r
37/**\r
38 Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.\r
39\r
40 @param[in] Buffer Pointer to the option buffer.\r
41 @param[in] Length Length of the option buffer.\r
42 @param[in] OptTag Tag of the required option.\r
43\r
44 @retval NULL Failed to find the required option.\r
45 @retval Others The position of the required option.\r
46\r
47**/\r
48EFI_DHCP4_PACKET_OPTION *\r
49PxeBcParseDhcp4Options (\r
50 IN UINT8 *Buffer,\r
51 IN UINT32 Length,\r
52 IN UINT8 OptTag\r
53 )\r
54{\r
55 EFI_DHCP4_PACKET_OPTION *Option;\r
56 UINT32 Offset;\r
57\r
58 Option = (EFI_DHCP4_PACKET_OPTION *) Buffer;\r
59 Offset = 0;\r
60\r
142c00c3 61 while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) {\r
a3bcde70
HT
62\r
63 if (Option->OpCode == OptTag) {\r
64 //\r
65 // Found the required option.\r
66 //\r
67 return Option;\r
68 }\r
69\r
70 //\r
71 // Skip the current option to the next.\r
72 //\r
142c00c3 73 if (Option->OpCode == DHCP4_TAG_PAD) {\r
a3bcde70
HT
74 Offset++;\r
75 } else {\r
76 Offset += Option->Length + 2;\r
77 }\r
78\r
79 Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset);\r
80 }\r
81\r
82 return NULL;\r
83}\r
84\r
85\r
86/**\r
87 Parse the PXE vender options and extract the information from them.\r
88\r
89 @param[in] Dhcp4Option Pointer to vendor options in buffer.\r
90 @param[in] VendorOption Pointer to structure to store information in vendor options.\r
91\r
92**/\r
93VOID\r
94PxeBcParseVendorOptions (\r
95 IN EFI_DHCP4_PACKET_OPTION *Dhcp4Option,\r
96 IN PXEBC_VENDOR_OPTION *VendorOption\r
97 )\r
98{\r
99 UINT32 *BitMap;\r
100 UINT8 VendorOptionLen;\r
101 EFI_DHCP4_PACKET_OPTION *PxeOption;\r
102 UINT8 Offset;\r
103\r
104 BitMap = VendorOption->BitMap;\r
105 VendorOptionLen = Dhcp4Option->Length;\r
106 PxeOption = (EFI_DHCP4_PACKET_OPTION *) &Dhcp4Option->Data[0];\r
107 Offset = 0;\r
108\r
109 ASSERT (PxeOption != NULL);\r
110\r
142c00c3 111 while ((Offset < VendorOptionLen) && (PxeOption->OpCode != DHCP4_TAG_EOP)) {\r
a3bcde70
HT
112 //\r
113 // Parse all the interesting PXE vendor options one by one.\r
114 //\r
115 switch (PxeOption->OpCode) {\r
116\r
117 case PXEBC_VENDOR_TAG_MTFTP_IP:\r
118\r
119 CopyMem (&VendorOption->MtftpIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r
120 break;\r
121\r
122 case PXEBC_VENDOR_TAG_MTFTP_CPORT:\r
123\r
124 CopyMem (&VendorOption->MtftpCPort, PxeOption->Data, sizeof (VendorOption->MtftpCPort));\r
125 break;\r
126\r
127 case PXEBC_VENDOR_TAG_MTFTP_SPORT:\r
128\r
129 CopyMem (&VendorOption->MtftpSPort, PxeOption->Data, sizeof (VendorOption->MtftpSPort));\r
130 break;\r
131\r
132 case PXEBC_VENDOR_TAG_MTFTP_TIMEOUT:\r
133\r
134 VendorOption->MtftpTimeout = *PxeOption->Data;\r
135 break;\r
136\r
137 case PXEBC_VENDOR_TAG_MTFTP_DELAY:\r
138\r
139 VendorOption->MtftpDelay = *PxeOption->Data;\r
140 break;\r
141\r
142 case PXEBC_VENDOR_TAG_DISCOVER_CTRL:\r
143\r
144 VendorOption->DiscoverCtrl = *PxeOption->Data;\r
145 break;\r
146\r
147 case PXEBC_VENDOR_TAG_DISCOVER_MCAST:\r
148\r
149 CopyMem (&VendorOption->DiscoverMcastIp, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r
150 break;\r
151\r
152 case PXEBC_VENDOR_TAG_BOOT_SERVERS:\r
153\r
154 VendorOption->BootSvrLen = PxeOption->Length;\r
155 VendorOption->BootSvr = (PXEBC_BOOT_SVR_ENTRY *) PxeOption->Data;\r
156 break;\r
157\r
158 case PXEBC_VENDOR_TAG_BOOT_MENU:\r
159\r
160 VendorOption->BootMenuLen = PxeOption->Length;\r
161 VendorOption->BootMenu = (PXEBC_BOOT_MENU_ENTRY *) PxeOption->Data;\r
162 break;\r
163\r
164 case PXEBC_VENDOR_TAG_MENU_PROMPT:\r
165\r
166 VendorOption->MenuPromptLen = PxeOption->Length;\r
167 VendorOption->MenuPrompt = (PXEBC_MENU_PROMPT *) PxeOption->Data;\r
168 break;\r
169\r
170 case PXEBC_VENDOR_TAG_MCAST_ALLOC:\r
171\r
172 CopyMem (&VendorOption->McastIpBase, PxeOption->Data, sizeof (EFI_IPv4_ADDRESS));\r
173 CopyMem (&VendorOption->McastIpBlock, PxeOption->Data + 4, sizeof (VendorOption->McastIpBlock));\r
174 CopyMem (&VendorOption->McastIpRange, PxeOption->Data + 6, sizeof (VendorOption->McastIpRange));\r
175 break;\r
176\r
177 case PXEBC_VENDOR_TAG_CREDENTIAL_TYPES:\r
178\r
179 VendorOption->CredTypeLen = PxeOption->Length;\r
180 VendorOption->CredType = (UINT32 *) PxeOption->Data;\r
181 break;\r
182\r
183 case PXEBC_VENDOR_TAG_BOOT_ITEM:\r
184\r
185 CopyMem (&VendorOption->BootSrvType, PxeOption->Data, sizeof (VendorOption->BootSrvType));\r
186 CopyMem (&VendorOption->BootSrvLayer, PxeOption->Data + 2, sizeof (VendorOption->BootSrvLayer));\r
187 break;\r
188\r
189 default:\r
190 //\r
191 // Not interesting PXE vendor options.\r
192 //\r
193 break;\r
194 }\r
195\r
196 //\r
197 // Set the bit map for the special PXE options.\r
198 //\r
199 SET_VENDOR_OPTION_BIT_MAP (BitMap, PxeOption->OpCode);\r
200\r
201 //\r
202 // Continue to the next option.\r
203 //\r
142c00c3 204 if (PxeOption->OpCode == DHCP4_TAG_PAD) {\r
a3bcde70
HT
205 Offset++;\r
206 } else {\r
207 Offset = (UINT8) (Offset + PxeOption->Length + 2);\r
208 }\r
209\r
210 PxeOption = (EFI_DHCP4_PACKET_OPTION *) (Dhcp4Option->Data + Offset);\r
211 }\r
212}\r
213\r
214\r
215/**\r
216 Build the options buffer for the DHCPv4 request packet.\r
217\r
218 @param[in] Private Pointer to PxeBc private data.\r
219 @param[out] OptList Pointer to the option pointer array.\r
220 @param[in] Buffer Pointer to the buffer to contain the option list.\r
221 @param[in] NeedMsgType If TRUE, it is necessary to include the Msg type option.\r
222 Otherwise, it is not necessary.\r
223\r
224 @return Index The count of the built-in options.\r
225\r
226**/\r
227UINT32\r
228PxeBcBuildDhcp4Options (\r
229 IN PXEBC_PRIVATE_DATA *Private,\r
230 OUT EFI_DHCP4_PACKET_OPTION **OptList,\r
231 IN UINT8 *Buffer,\r
232 IN BOOLEAN NeedMsgType\r
233 )\r
234{\r
235 UINT32 Index;\r
236 PXEBC_DHCP4_OPTION_ENTRY OptEnt;\r
237 UINT16 Value;\r
238\r
239 Index = 0;\r
240 OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Buffer;\r
241\r
242 if (NeedMsgType) {\r
243 //\r
244 // Append message type.\r
245 //\r
142c00c3 246 OptList[Index]->OpCode = DHCP4_TAG_MSG_TYPE;\r
a3bcde70
HT
247 OptList[Index]->Length = 1;\r
248 OptEnt.Mesg = (PXEBC_DHCP4_OPTION_MESG *) OptList[Index]->Data;\r
249 OptEnt.Mesg->Type = PXEBC_DHCP4_MSG_TYPE_REQUEST;\r
250 Index++;\r
251 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
252\r
253 //\r
254 // Append max message size.\r
255 //\r
142c00c3 256 OptList[Index]->OpCode = DHCP4_TAG_MAXMSG;\r
a3bcde70
HT
257 OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE);\r
258 OptEnt.MaxMesgSize = (PXEBC_DHCP4_OPTION_MAX_MESG_SIZE *) OptList[Index]->Data;\r
632dcfd6 259 Value = NTOHS (PXEBC_DHCP4_PACKET_MAX_SIZE);\r
a3bcde70
HT
260 CopyMem (&OptEnt.MaxMesgSize->Size, &Value, sizeof (UINT16));\r
261 Index++;\r
262 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
263 }\r
264\r
265 //\r
266 // Append parameter request list option.\r
267 //\r
142c00c3 268 OptList[Index]->OpCode = DHCP4_TAG_PARA_LIST;\r
a3bcde70
HT
269 OptList[Index]->Length = 35;\r
270 OptEnt.Para = (PXEBC_DHCP4_OPTION_PARA *) OptList[Index]->Data;\r
142c00c3
ZL
271 OptEnt.Para->ParaList[0] = DHCP4_TAG_NETMASK;\r
272 OptEnt.Para->ParaList[1] = DHCP4_TAG_TIME_OFFSET;\r
273 OptEnt.Para->ParaList[2] = DHCP4_TAG_ROUTER;\r
274 OptEnt.Para->ParaList[3] = DHCP4_TAG_TIME_SERVER;\r
275 OptEnt.Para->ParaList[4] = DHCP4_TAG_NAME_SERVER;\r
276 OptEnt.Para->ParaList[5] = DHCP4_TAG_DNS_SERVER;\r
277 OptEnt.Para->ParaList[6] = DHCP4_TAG_HOSTNAME;\r
278 OptEnt.Para->ParaList[7] = DHCP4_TAG_BOOTFILE_LEN;\r
279 OptEnt.Para->ParaList[8] = DHCP4_TAG_DOMAINNAME;\r
280 OptEnt.Para->ParaList[9] = DHCP4_TAG_ROOTPATH;\r
281 OptEnt.Para->ParaList[10] = DHCP4_TAG_EXTEND_PATH;\r
282 OptEnt.Para->ParaList[11] = DHCP4_TAG_EMTU;\r
283 OptEnt.Para->ParaList[12] = DHCP4_TAG_TTL;\r
284 OptEnt.Para->ParaList[13] = DHCP4_TAG_BROADCAST;\r
285 OptEnt.Para->ParaList[14] = DHCP4_TAG_NIS_DOMAIN;\r
286 OptEnt.Para->ParaList[15] = DHCP4_TAG_NIS_SERVER;\r
287 OptEnt.Para->ParaList[16] = DHCP4_TAG_NTP_SERVER;\r
288 OptEnt.Para->ParaList[17] = DHCP4_TAG_VENDOR;\r
289 OptEnt.Para->ParaList[18] = DHCP4_TAG_REQUEST_IP;\r
290 OptEnt.Para->ParaList[19] = DHCP4_TAG_LEASE;\r
291 OptEnt.Para->ParaList[20] = DHCP4_TAG_SERVER_ID;\r
292 OptEnt.Para->ParaList[21] = DHCP4_TAG_T1;\r
293 OptEnt.Para->ParaList[22] = DHCP4_TAG_T2;\r
294 OptEnt.Para->ParaList[23] = DHCP4_TAG_VENDOR_CLASS_ID;\r
295 OptEnt.Para->ParaList[24] = DHCP4_TAG_TFTP;\r
296 OptEnt.Para->ParaList[25] = DHCP4_TAG_BOOTFILE;\r
297 OptEnt.Para->ParaList[26] = DHCP4_TAG_UUID;\r
a3bcde70
HT
298 OptEnt.Para->ParaList[27] = 0x80;\r
299 OptEnt.Para->ParaList[28] = 0x81;\r
300 OptEnt.Para->ParaList[29] = 0x82;\r
301 OptEnt.Para->ParaList[30] = 0x83;\r
302 OptEnt.Para->ParaList[31] = 0x84;\r
303 OptEnt.Para->ParaList[32] = 0x85;\r
304 OptEnt.Para->ParaList[33] = 0x86;\r
305 OptEnt.Para->ParaList[34] = 0x87;\r
306 Index++;\r
307 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
308\r
309 //\r
310 // Append UUID/Guid-based client identifier option\r
311 //\r
142c00c3 312 OptList[Index]->OpCode = DHCP4_TAG_UUID;\r
a3bcde70
HT
313 OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UUID);\r
314 OptEnt.Uuid = (PXEBC_DHCP4_OPTION_UUID *) OptList[Index]->Data;\r
315 OptEnt.Uuid->Type = 0;\r
316 Index++;\r
317 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
318\r
19ddbb25 319 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) {\r
a3bcde70
HT
320 //\r
321 // Zero the Guid to indicate NOT programable if failed to get system Guid.\r
322 //\r
323 ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));\r
324 }\r
325\r
326 //\r
327 // Append client network device interface option\r
328 //\r
142c00c3 329 OptList[Index]->OpCode = DHCP4_TAG_UNDI;\r
a3bcde70
HT
330 OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_UNDI);\r
331 OptEnt.Undi = (PXEBC_DHCP4_OPTION_UNDI *) OptList[Index]->Data;\r
332\r
333 if (Private->Nii != NULL) {\r
334 OptEnt.Undi->Type = Private->Nii->Type;\r
335 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;\r
336 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;\r
337 } else {\r
338 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;\r
339 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;\r
340 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;\r
341 }\r
342\r
343 Index++;\r
344 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
345\r
346 //\r
347 // Append client system architecture option\r
348 //\r
142c00c3 349 OptList[Index]->OpCode = DHCP4_TAG_ARCH;\r
a3bcde70
HT
350 OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_ARCH);\r
351 OptEnt.Arch = (PXEBC_DHCP4_OPTION_ARCH *) OptList[Index]->Data;\r
352 Value = HTONS (EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE);\r
353 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));\r
354 Index++;\r
355 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
356\r
357 //\r
358 // Append vendor class identify option\r
359 //\r
142c00c3 360 OptList[Index]->OpCode = DHCP4_TAG_VENDOR_CLASS_ID;\r
a3bcde70
HT
361 OptList[Index]->Length = (UINT8) sizeof (PXEBC_DHCP4_OPTION_CLID);\r
362 OptEnt.Clid = (PXEBC_DHCP4_OPTION_CLID *) OptList[Index]->Data;\r
363 CopyMem (\r
364 OptEnt.Clid,\r
365 DEFAULT_CLASS_ID_DATA,\r
366 sizeof (PXEBC_DHCP4_OPTION_CLID)\r
367 );\r
368 PxeBcUintnToAscDecWithFormat (\r
369 EFI_PXE_CLIENT_SYSTEM_ARCHITECTURE,\r
370 OptEnt.Clid->ArchitectureType,\r
371 sizeof (OptEnt.Clid->ArchitectureType)\r
372 );\r
373\r
374 if (Private->Nii != NULL) {\r
375 CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));\r
376 PxeBcUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));\r
377 PxeBcUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));\r
378 }\r
379\r
380 Index++;\r
381\r
382 return Index;\r
383}\r
384\r
385\r
386/**\r
387 Create a template DHCPv4 packet as a seed.\r
388\r
389 @param[out] Seed Pointer to the seed packet.\r
390 @param[in] Udp4 Pointer to EFI_UDP4_PROTOCOL.\r
391\r
392**/\r
393VOID\r
394PxeBcSeedDhcp4Packet (\r
395 OUT EFI_DHCP4_PACKET *Seed,\r
396 IN EFI_UDP4_PROTOCOL *Udp4\r
397 )\r
398{\r
399 EFI_SIMPLE_NETWORK_MODE Mode;\r
400 EFI_DHCP4_HEADER *Header;\r
401\r
402 //\r
403 // Get IfType and HwAddressSize from SNP mode data.\r
404 //\r
405 Udp4->GetModeData (Udp4, NULL, NULL, NULL, &Mode);\r
406\r
407 Seed->Size = sizeof (EFI_DHCP4_PACKET);\r
408 Seed->Length = sizeof (Seed->Dhcp4);\r
409 Header = &Seed->Dhcp4.Header;\r
410 ZeroMem (Header, sizeof (EFI_DHCP4_HEADER));\r
411 Header->OpCode = PXEBC_DHCP4_OPCODE_REQUEST;\r
412 Header->HwType = Mode.IfType;\r
413 Header->HwAddrLen = (UINT8) Mode.HwAddressSize;\r
414 CopyMem (Header->ClientHwAddr, &Mode.CurrentAddress, Header->HwAddrLen);\r
415\r
416 Seed->Dhcp4.Magik = PXEBC_DHCP4_MAGIC;\r
142c00c3 417 Seed->Dhcp4.Option[0] = DHCP4_TAG_EOP;\r
a3bcde70
HT
418}\r
419\r
420\r
421/**\r
422 Cache the DHCPv4 packet.\r
423\r
424 @param[in] Dst Pointer to the cache buffer for DHCPv4 packet.\r
425 @param[in] Src Pointer to the DHCPv4 packet to be cached.\r
426\r
427**/\r
428VOID\r
429PxeBcCacheDhcp4Packet (\r
430 IN EFI_DHCP4_PACKET *Dst,\r
431 IN EFI_DHCP4_PACKET *Src\r
432 )\r
433{\r
434 ASSERT (Dst->Size >= Src->Length);\r
435\r
436 CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);\r
437 Dst->Length = Src->Length;\r
438}\r
439\r
440\r
441/**\r
442 Parse the cached DHCPv4 packet, including all the options.\r
443\r
444 @param[in] Cache4 Pointer to cached DHCPv4 packet.\r
445\r
446 @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully.\r
447 @retval EFI_DEVICE_ERROR Failed to parse and invalid packet.\r
448\r
449**/\r
450EFI_STATUS\r
451PxeBcParseDhcp4Packet (\r
452 IN PXEBC_DHCP4_PACKET_CACHE *Cache4\r
453 )\r
454{\r
455 EFI_DHCP4_PACKET *Offer;\r
456 EFI_DHCP4_PACKET_OPTION **Options;\r
457 EFI_DHCP4_PACKET_OPTION *Option;\r
458 PXEBC_OFFER_TYPE OfferType;\r
459 UINTN Index;\r
460 BOOLEAN IsProxyOffer;\r
461 BOOLEAN IsPxeOffer;\r
462 UINT8 *Ptr8;\r
aa437778 463 BOOLEAN FileFieldOverloaded;\r
a3bcde70
HT
464\r
465 IsProxyOffer = FALSE;\r
466 IsPxeOffer = FALSE;\r
aa437778 467 FileFieldOverloaded = FALSE;\r
a3bcde70
HT
468\r
469 ZeroMem (Cache4->OptList, sizeof (Cache4->OptList));\r
470 ZeroMem (&Cache4->VendorOpt, sizeof (Cache4->VendorOpt));\r
471\r
472 Offer = &Cache4->Packet.Offer;\r
473 Options = Cache4->OptList;\r
474\r
475 //\r
476 // Parse DHCPv4 options in this offer, and store the pointers.\r
8cb92971 477 // First, try to parse DHCPv4 options from the DHCP optional parameters field.\r
a3bcde70
HT
478 //\r
479 for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r
480 Options[Index] = PxeBcParseDhcp4Options (\r
481 Offer->Dhcp4.Option,\r
482 GET_OPTION_BUFFER_LEN (Offer),\r
483 mInterestedDhcp4Tags[Index]\r
484 );\r
485 }\r
8cb92971
FS
486 //\r
487 // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132. \r
488 // If yes, try to parse options from the BootFileName field, then ServerName field.\r
489 //\r
490 Option = Options[PXEBC_DHCP4_TAG_INDEX_OVERLOAD];\r
491 if (Option != NULL) {\r
492 if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_FILE) != 0) {\r
aa437778 493 FileFieldOverloaded = TRUE;\r
8cb92971
FS
494 for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r
495 if (Options[Index] == NULL) {\r
496 Options[Index] = PxeBcParseDhcp4Options (\r
497 (UINT8 *) Offer->Dhcp4.Header.BootFileName,\r
498 sizeof (Offer->Dhcp4.Header.BootFileName),\r
499 mInterestedDhcp4Tags[Index]\r
500 );\r
501 }\r
502 }\r
503 }\r
504 if ((Option->Data[0] & PXEBC_DHCP4_OVERLOAD_SERVER_NAME) != 0) {\r
505 for (Index = 0; Index < PXEBC_DHCP4_TAG_INDEX_MAX; Index++) {\r
506 if (Options[Index] == NULL) {\r
507 Options[Index] = PxeBcParseDhcp4Options (\r
508 (UINT8 *) Offer->Dhcp4.Header.ServerName,\r
509 sizeof (Offer->Dhcp4.Header.ServerName),\r
510 mInterestedDhcp4Tags[Index]\r
511 );\r
512 }\r
513 }\r
514 }\r
515 }\r
a3bcde70
HT
516\r
517 //\r
ae97201c 518 // The offer with zero "yiaddr" is a proxy offer.\r
a3bcde70
HT
519 //\r
520 if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) {\r
521 IsProxyOffer = TRUE;\r
522 }\r
523\r
524 //\r
525 // The offer with "PXEClient" is a PXE offer.\r
526 //\r
527 Option = Options[PXEBC_DHCP4_TAG_INDEX_CLASS_ID];\r
528 if ((Option != NULL) && (Option->Length >= 9) &&\r
529 (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 9) == 0)) {\r
530 IsPxeOffer = TRUE;\r
531 }\r
532\r
533 //\r
534 // Parse PXE vendor options in this offer, and store the contents/pointers.\r
535 //\r
536 Option = Options[PXEBC_DHCP4_TAG_INDEX_VENDOR];\r
537 if (IsPxeOffer && Option != NULL) {\r
538 PxeBcParseVendorOptions (Option, &Cache4->VendorOpt);\r
539 }\r
540\r
541 //\r
8cb92971
FS
542 // Parse PXE boot file name:\r
543 // According to PXE spec, boot file name should be read from DHCP option 67 (bootfile name) if present.\r
544 // Otherwise, read from boot file field in DHCP header.\r
a3bcde70 545 //\r
8cb92971 546 if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r
a3bcde70
HT
547 //\r
548 // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null\r
549 // terminated string. So force to append null terminated character at the end of string.\r
550 //\r
8cb92971
FS
551 Ptr8 = (UINT8*)&Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];\r
552 Ptr8 += Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Length;\r
553 if (*(Ptr8 - 1) != '\0') {\r
554 *Ptr8 = '\0';\r
a3bcde70 555 }\r
aa437778 556 } else if (!FileFieldOverloaded && Offer->Dhcp4.Header.BootFileName[0] != 0) {\r
a3bcde70
HT
557 //\r
558 // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.\r
75dce340 559 // Do not count dhcp option header here, or else will destroy the serverhostname.\r
a3bcde70
HT
560 //\r
561 Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)\r
562 (&Offer->Dhcp4.Header.BootFileName[0] -\r
563 OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));\r
564\r
565 }\r
566\r
567 //\r
568 // Determine offer type of the DHCPv4 packet.\r
569 //\r
570 Option = Options[PXEBC_DHCP4_TAG_INDEX_MSG_TYPE];\r
571 if (Option == NULL || Option->Data[0] == 0) {\r
572 //\r
573 // It's a Bootp offer.\r
574 //\r
575 OfferType = PxeOfferTypeBootp;\r
576\r
577 Option = Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE];\r
578 if (Option == NULL) {\r
579 //\r
580 // If the Bootp offer without bootfilename, discard it.\r
581 //\r
582 return EFI_DEVICE_ERROR;\r
583 }\r
584 } else {\r
585\r
586 if (IS_VALID_DISCOVER_VENDOR_OPTION (Cache4->VendorOpt.BitMap)) {\r
587 //\r
588 // It's a PXE10 offer with PXEClient and discover vendor option.\r
589 //\r
590 OfferType = IsProxyOffer ? PxeOfferTypeProxyPxe10 : PxeOfferTypeDhcpPxe10;\r
591 } else if (IS_VALID_MTFTP_VENDOR_OPTION (Cache4->VendorOpt.BitMap)) {\r
592 //\r
593 // It's a WFM11a offer with PXEClient and mtftp vendor option.\r
594 // But multi-cast download is not supported currently, so discard it.\r
595 //\r
596 return EFI_DEVICE_ERROR;\r
597 } else if (IsPxeOffer) {\r
598 //\r
599 // It's a BINL offer only with PXEClient.\r
600 //\r
601 OfferType = IsProxyOffer ? PxeOfferTypeProxyBinl : PxeOfferTypeDhcpBinl;\r
602 } else {\r
603 //\r
604 // It's a DHCPv4 only offer, which is a pure DHCPv4 offer packet.\r
605 //\r
606 OfferType = PxeOfferTypeDhcpOnly;\r
607 }\r
608 }\r
609\r
610 Cache4->OfferType = OfferType;\r
611\r
612 return EFI_SUCCESS;\r
613}\r
614\r
615\r
616/**\r
617 Cache the DHCPv4 ack packet, and parse it on demand.\r
618\r
619 @param[in] Private Pointer to PxeBc private data.\r
620 @param[in] Ack Pointer to the DHCPv4 ack packet.\r
621 @param[in] Verified If TRUE, parse the ACK packet and store info into mode data.\r
622\r
623**/\r
624VOID\r
625PxeBcCopyDhcp4Ack (\r
626 IN PXEBC_PRIVATE_DATA *Private,\r
627 IN EFI_DHCP4_PACKET *Ack,\r
628 IN BOOLEAN Verified\r
629 )\r
630{\r
631 EFI_PXE_BASE_CODE_MODE *Mode;\r
632\r
633 Mode = Private->PxeBc.Mode;\r
634\r
635 PxeBcCacheDhcp4Packet (&Private->DhcpAck.Dhcp4.Packet.Ack, Ack);\r
636\r
637 if (Verified) {\r
638 //\r
639 // Parse the ack packet and store it into mode data if needed.\r
640 //\r
641 PxeBcParseDhcp4Packet (&Private->DhcpAck.Dhcp4);\r
642 CopyMem (&Mode->DhcpAck.Dhcpv4, &Ack->Dhcp4, Ack->Length);\r
643 Mode->DhcpAckReceived = TRUE;\r
644 }\r
645}\r
646\r
647\r
648/**\r
649 Cache the DHCPv4 proxy offer packet according to the received order.\r
650\r
651 @param[in] Private Pointer to PxeBc private data.\r
652 @param[in] OfferIndex The received order of offer packets.\r
653\r
654**/\r
655VOID\r
656PxeBcCopyProxyOffer (\r
657 IN PXEBC_PRIVATE_DATA *Private,\r
658 IN UINT32 OfferIndex\r
659 )\r
660{\r
661 EFI_PXE_BASE_CODE_MODE *Mode;\r
662 EFI_DHCP4_PACKET *Offer;\r
663\r
664 ASSERT (OfferIndex < Private->OfferNum);\r
665 ASSERT (OfferIndex < PXEBC_OFFER_MAX_NUM);\r
666\r
667 Mode = Private->PxeBc.Mode;\r
668 Offer = &Private->OfferBuffer[OfferIndex].Dhcp4.Packet.Offer;\r
669\r
670 //\r
671 // Cache the proxy offer packet and parse it.\r
672 //\r
673 PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Offer);\r
674 PxeBcParseDhcp4Packet (&Private->ProxyOffer.Dhcp4);\r
675\r
676 //\r
677 // Store this packet into mode data.\r
678 //\r
679 CopyMem (&Mode->ProxyOffer.Dhcpv4, &Offer->Dhcp4, Offer->Length);\r
680 Mode->ProxyOfferReceived = TRUE;\r
681}\r
682\r
683\r
684/**\r
685 Retry to request bootfile name by the BINL offer.\r
686\r
687 @param[in] Private Pointer to PxeBc private data.\r
688 @param[in] Index The received order of offer packets.\r
689\r
690 @retval EFI_SUCCESS Successfully retried to request bootfile name.\r
691 @retval EFI_DEVICE_ERROR Failed to retry bootfile name.\r
692\r
693**/\r
694EFI_STATUS\r
695PxeBcRetryBinlOffer (\r
696 IN PXEBC_PRIVATE_DATA *Private,\r
697 IN UINT32 Index\r
698 )\r
699{\r
700 EFI_DHCP4_PACKET *Offer;\r
701 EFI_IP_ADDRESS ServerIp;\r
702 EFI_STATUS Status;\r
703 PXEBC_DHCP4_PACKET_CACHE *Cache4;\r
704 EFI_DHCP4_PACKET *Reply;\r
705\r
706 ASSERT (Index < PXEBC_OFFER_MAX_NUM);\r
707 ASSERT (Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeDhcpBinl ||\r
708 Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeProxyBinl);\r
709\r
710 Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;\r
711\r
712 //\r
713 // Prefer to siaddr in header as next server address. If it's zero, then use option 54.\r
714 //\r
715 if (Offer->Dhcp4.Header.ServerAddr.Addr[0] == 0) {\r
716 CopyMem (\r
717 &ServerIp.Addr[0],\r
718 Private->OfferBuffer[Index].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,\r
719 sizeof (EFI_IPv4_ADDRESS)\r
720 );\r
721 } else {\r
722 CopyMem (\r
723 &ServerIp.Addr[0],\r
724 &Offer->Dhcp4.Header.ServerAddr,\r
725 sizeof (EFI_IPv4_ADDRESS)\r
726 );\r
727 }\r
728\r
729 Private->IsDoDiscover = FALSE;\r
730 Cache4 = &Private->ProxyOffer.Dhcp4;\r
731 Reply = &Cache4->Packet.Offer;\r
732\r
733 //\r
734 // Send another request packet for bootfile name.\r
735 //\r
736 Status = PxeBcDhcp4Discover (\r
737 Private,\r
738 0,\r
739 NULL,\r
740 FALSE,\r
741 &ServerIp,\r
742 0,\r
743 NULL\r
744 );\r
745 if (EFI_ERROR (Status)) {\r
746 return Status;\r
747 }\r
748\r
749 //\r
750 // Parse the reply for the last request packet.\r
751 //\r
752 Status = PxeBcParseDhcp4Packet (Cache4);\r
753 if (EFI_ERROR (Status)) {\r
754 return Status;\r
755 }\r
756\r
757 if (Cache4->OfferType != PxeOfferTypeProxyPxe10 &&\r
758 Cache4->OfferType != PxeOfferTypeProxyWfm11a &&\r
759 Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {\r
760 //\r
761 // This BINL ack doesn't have discovery option set or multicast option set\r
762 // or bootfile name specified.\r
763 //\r
764 return EFI_DEVICE_ERROR;\r
765 }\r
766\r
767 //\r
768 // Store the reply into mode data.\r
769 //\r
770 Private->PxeBc.Mode->ProxyOfferReceived = TRUE;\r
771 CopyMem (&Private->PxeBc.Mode->ProxyOffer.Dhcpv4, &Reply->Dhcp4, Reply->Length);\r
772\r
773 return EFI_SUCCESS;\r
774}\r
775\r
776\r
777/**\r
778 Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.\r
779\r
780 @param[in] Private Pointer to PxeBc private data.\r
781 @param[in] RcvdOffer Pointer to the received offer packet.\r
782\r
783**/\r
784VOID\r
785PxeBcCacheDhcp4Offer (\r
786 IN PXEBC_PRIVATE_DATA *Private,\r
787 IN EFI_DHCP4_PACKET *RcvdOffer\r
788 )\r
789{\r
790 PXEBC_DHCP4_PACKET_CACHE *Cache4;\r
791 EFI_DHCP4_PACKET *Offer;\r
792 PXEBC_OFFER_TYPE OfferType;\r
793\r
794 ASSERT (Private->OfferNum < PXEBC_OFFER_MAX_NUM);\r
795 Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;\r
796 Offer = &Cache4->Packet.Offer;\r
797\r
798 //\r
799 // Cache the content of DHCPv4 packet firstly.\r
800 //\r
801 PxeBcCacheDhcp4Packet (Offer, RcvdOffer);\r
802\r
803 //\r
804 // Validate the DHCPv4 packet, and parse the options and offer type.\r
805 //\r
806 if (EFI_ERROR (PxeBcParseDhcp4Packet (Cache4))) {\r
807 return;\r
808 }\r
809\r
810 //\r
811 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.\r
812 //\r
813 OfferType = Cache4->OfferType;\r
814 ASSERT (OfferType < PxeOfferTypeMax);\r
815\r
816 if (OfferType == PxeOfferTypeBootp) {\r
817 //\r
818 // It's a Bootp offer, only cache the first one, and discard the others.\r
819 //\r
820 if (Private->OfferCount[OfferType] == 0) {\r
821 Private->OfferIndex[OfferType][0] = Private->OfferNum;\r
822 Private->OfferCount[OfferType] = 1;\r
823 } else {\r
824 return;\r
825 }\r
826 } else {\r
827 ASSERT (Private->OfferCount[OfferType] < PXEBC_OFFER_MAX_NUM);\r
828 if (IS_PROXY_DHCP_OFFER (Offer)) {\r
829 //\r
830 // It's a proxy offer without yiaddr, including PXE10, WFM11a or BINL offer.\r
831 //\r
832 Private->IsProxyRecved = TRUE;\r
833\r
834 if (OfferType == PxeOfferTypeProxyBinl) {\r
835 //\r
836 // Cache all proxy BINL offers.\r
837 //\r
838 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;\r
839 Private->OfferCount[OfferType]++;\r
840 } else if (Private->OfferCount[OfferType] > 0) {\r
841 //\r
842 // Only cache the first PXE10/WFM11a offer, and discard the others.\r
843 //\r
844 Private->OfferIndex[OfferType][0] = Private->OfferNum;\r
845 Private->OfferCount[OfferType] = 1;\r
846 } else {\r
847 return ;\r
848 }\r
849 } else {\r
850 //\r
851 // It's a DHCPv4 offer with yiaddr, and cache them all.\r
852 //\r
853 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;\r
854 Private->OfferCount[OfferType]++;\r
855 }\r
856 }\r
857\r
858 Private->OfferNum++;\r
859}\r
860\r
861\r
862/**\r
863 Select an DHCPv4 offer, and record SelectIndex and SelectProxyType.\r
864\r
865 @param[in] Private Pointer to PxeBc private data.\r
866\r
867**/\r
868VOID\r
869PxeBcSelectDhcp4Offer (\r
870 IN PXEBC_PRIVATE_DATA *Private\r
871 )\r
872{\r
873 UINT32 Index;\r
874 UINT32 OfferIndex;\r
875 EFI_DHCP4_PACKET *Offer;\r
876\r
877 Private->SelectIndex = 0;\r
878\r
879 if (Private->IsOfferSorted) {\r
880 //\r
881 // Select offer by default policy.\r
882 //\r
883 if (Private->OfferCount[PxeOfferTypeDhcpPxe10] > 0) {\r
884 //\r
885 // 1. DhcpPxe10 offer\r
886 //\r
887 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpPxe10][0] + 1;\r
888\r
889 } else if (Private->OfferCount[PxeOfferTypeDhcpWfm11a] > 0) {\r
890 //\r
891 // 2. DhcpWfm11a offer\r
892 //\r
893 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpWfm11a][0] + 1;\r
894\r
895 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&\r
896 Private->OfferCount[PxeOfferTypeProxyPxe10] > 0) {\r
897 //\r
898 // 3. DhcpOnly offer and ProxyPxe10 offer.\r
899 //\r
900 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;\r
901 Private->SelectProxyType = PxeOfferTypeProxyPxe10;\r
902\r
903 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&\r
904 Private->OfferCount[PxeOfferTypeProxyWfm11a] > 0) {\r
905 //\r
906 // 4. DhcpOnly offer and ProxyWfm11a offer.\r
907 //\r
908 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;\r
909 Private->SelectProxyType = PxeOfferTypeProxyWfm11a;\r
910\r
911 } else if (Private->OfferCount[PxeOfferTypeDhcpBinl] > 0) {\r
912 //\r
913 // 5. DhcpBinl offer.\r
914 //\r
915 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpBinl][0] + 1;\r
916\r
917 } else if (Private->OfferCount[PxeOfferTypeDhcpOnly] > 0 &&\r
918 Private->OfferCount[PxeOfferTypeProxyBinl] > 0) {\r
919 //\r
920 // 6. DhcpOnly offer and ProxyBinl offer.\r
921 //\r
922 Private->SelectIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][0] + 1;\r
923 Private->SelectProxyType = PxeOfferTypeProxyBinl;\r
924\r
925 } else {\r
926 //\r
927 // 7. DhcpOnly offer with bootfilename.\r
928 //\r
929 for (Index = 0; Index < Private->OfferCount[PxeOfferTypeDhcpOnly]; Index++) {\r
930 OfferIndex = Private->OfferIndex[PxeOfferTypeDhcpOnly][Index];\r
931 if (Private->OfferBuffer[OfferIndex].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r
932 Private->SelectIndex = OfferIndex + 1;\r
933 break;\r
934 }\r
935 }\r
936 //\r
937 // 8. Bootp offer with bootfilename.\r
938 //\r
939 OfferIndex = Private->OfferIndex[PxeOfferTypeBootp][0];\r
940 if (Private->SelectIndex == 0 &&\r
941 Private->OfferCount[PxeOfferTypeBootp] > 0 &&\r
942 Private->OfferBuffer[OfferIndex].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r
943 Private->SelectIndex = OfferIndex + 1;\r
944 }\r
945 }\r
946 } else {\r
947 //\r
948 // Select offer by received order.\r
949 //\r
950 for (Index = 0; Index < Private->OfferNum; Index++) {\r
951\r
952 Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;\r
953\r
954 if (IS_PROXY_DHCP_OFFER (Offer)) {\r
955 //\r
956 // Skip proxy offers\r
957 //\r
958 continue;\r
959 }\r
960\r
961 if (!Private->IsProxyRecved &&\r
962 Private->OfferBuffer[Index].Dhcp4.OfferType == PxeOfferTypeDhcpOnly &&\r
963 Private->OfferBuffer[Index].Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {\r
964 //\r
965 // Skip if DhcpOnly offer without any other proxy offers or bootfilename.\r
966 //\r
967 continue;\r
968 }\r
969\r
970 //\r
971 // Record the index of the select offer.\r
972 //\r
973 Private->SelectIndex = Index + 1;\r
974 break;\r
975 }\r
976 }\r
977}\r
978\r
979\r
980/**\r
981 Handle the DHCPv4 offer packet.\r
982\r
983 @param[in] Private Pointer to PxeBc private data.\r
984\r
985 @retval EFI_SUCCESS Handled the DHCPv4 offer packet successfully.\r
986 @retval EFI_NO_RESPONSE No response to the following request packet.\r
4496ff75 987 @retval EFI_NOT_FOUND No boot filename received.\r
a3bcde70
HT
988\r
989**/\r
990EFI_STATUS\r
991PxeBcHandleDhcp4Offer (\r
992 IN PXEBC_PRIVATE_DATA *Private\r
993 )\r
994{\r
995 PXEBC_DHCP4_PACKET_CACHE *Cache4;\r
996 EFI_DHCP4_PACKET_OPTION **Options;\r
997 UINT32 Index;\r
998 EFI_DHCP4_PACKET *Offer;\r
999 PXEBC_OFFER_TYPE OfferType;\r
1000 UINT32 ProxyIndex;\r
1001 UINT32 SelectIndex;\r
1002 EFI_STATUS Status;\r
1003 EFI_PXE_BASE_CODE_MODE *Mode;\r
1004 EFI_DHCP4_PACKET *Ack;\r
1005\r
1006 ASSERT (Private->SelectIndex > 0);\r
1007 SelectIndex = (UINT32) (Private->SelectIndex - 1);\r
1008 ASSERT (SelectIndex < PXEBC_OFFER_MAX_NUM);\r
1009 Cache4 = &Private->OfferBuffer[SelectIndex].Dhcp4;\r
1010 Options = Cache4->OptList;\r
1011 Status = EFI_SUCCESS;\r
1012\r
1013 if (Cache4->OfferType == PxeOfferTypeDhcpBinl) {\r
1014 //\r
1015 // DhcpBinl offer is selected, so need try to request bootfilename by this offer.\r
1016 //\r
1017 if (EFI_ERROR (PxeBcRetryBinlOffer (Private, SelectIndex))) {\r
1018 Status = EFI_NO_RESPONSE;\r
1019 }\r
1020 } else if (Cache4->OfferType == PxeOfferTypeDhcpOnly) {\r
1021\r
1022 if (Private->IsProxyRecved) {\r
1023 //\r
1024 // DhcpOnly offer is selected, so need try to request bootfile name.\r
1025 //\r
1026 ProxyIndex = 0;\r
1027 if (Private->IsOfferSorted) {\r
1028 //\r
1029 // The proxy offer should be determined if select by default policy.\r
1030 // IsOfferSorted means all offers are labeled by OfferIndex.\r
1031 //\r
1032 ASSERT (Private->SelectProxyType < PxeOfferTypeMax);\r
1033 ASSERT (Private->OfferCount[Private->SelectProxyType] > 0);\r
1034\r
1035 if (Private->SelectProxyType == PxeOfferTypeProxyBinl) {\r
1036 //\r
1037 // Try all the cached ProxyBinl offer one by one to request bootfile name.\r
1038 //\r
1039 for (Index = 0; Index < Private->OfferCount[Private->SelectProxyType]; Index++) {\r
1040 ASSERT (Index < PXEBC_OFFER_MAX_NUM);\r
1041 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][Index];\r
1042 if (!EFI_ERROR (PxeBcRetryBinlOffer (Private, ProxyIndex))) {\r
1043 break;\r
1044 }\r
1045 }\r
1046 if (Index == Private->OfferCount[Private->SelectProxyType]) {\r
1047 Status = EFI_NO_RESPONSE;\r
1048 }\r
1049 } else {\r
1050 //\r
1051 // For other proxy offers, only one is buffered.\r
1052 //\r
1053 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];\r
1054 }\r
1055 } else {\r
1056 //\r
1057 // The proxy offer should not be determined if select by received order.\r
1058 //\r
1059 Status = EFI_NO_RESPONSE;\r
1060\r
1061 for (Index = 0; Index < Private->OfferNum; Index++) {\r
1062 ASSERT (Index < PXEBC_OFFER_MAX_NUM);\r
1063 Offer = &Private->OfferBuffer[Index].Dhcp4.Packet.Offer;\r
1064 OfferType = Private->OfferBuffer[Index].Dhcp4.OfferType;\r
1065 if (!IS_PROXY_DHCP_OFFER (Offer)) {\r
1066 //\r
1067 // Skip non proxy DHCPv4 offers.\r
1068 //\r
1069 continue;\r
1070 }\r
1071\r
1072 if (OfferType == PxeOfferTypeProxyBinl) {\r
1073 //\r
1074 // Try all the cached ProxyBinl offer one by one to request bootfile name.\r
1075 //\r
1076 if (EFI_ERROR (PxeBcRetryBinlOffer (Private, Index))) {\r
1077 continue;\r
1078 }\r
1079 }\r
1080\r
1081 Private->SelectProxyType = OfferType;\r
1082 ProxyIndex = Index;\r
1083 Status = EFI_SUCCESS;\r
1084 break;\r
1085 }\r
1086 }\r
1087\r
1088 if (!EFI_ERROR (Status) && Private->SelectProxyType != PxeOfferTypeProxyBinl) {\r
1089 //\r
1090 // Success to try to request by a ProxyPxe10 or ProxyWfm11a offer, copy and parse it.\r
1091 //\r
1092 PxeBcCopyProxyOffer (Private, ProxyIndex);\r
1093 }\r
1094 } else {\r
1095 //\r
1096 // Othewise, the bootfile name must be included in DhcpOnly offer.\r
1097 //\r
4496ff75 1098 if (Options[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {\r
1099 Status = EFI_NOT_FOUND;\r
1100 }\r
a3bcde70
HT
1101 }\r
1102 }\r
1103\r
1104 if (!EFI_ERROR (Status)) {\r
1105 //\r
1106 // All PXE boot information is ready by now.\r
1107 //\r
1108 Mode = Private->PxeBc.Mode;\r
1109 Offer = &Cache4->Packet.Offer;\r
1110 Ack = &Private->DhcpAck.Dhcp4.Packet.Ack;\r
1111 if (Cache4->OfferType == PxeOfferTypeBootp) {\r
1112 //\r
1113 // Bootp is a special case that only 2 packets involved instead of 4. So the bootp's reply\r
1114 // should be taken as ack.\r
1115 //\r
1116 Ack = Offer;\r
1117 }\r
1118\r
1119 PxeBcCopyDhcp4Ack (Private, Ack, TRUE);\r
1120 Mode->DhcpDiscoverValid = TRUE;\r
1121 }\r
1122\r
1123 return Status;\r
1124}\r
1125\r
1126\r
1127/**\r
1128 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver\r
1129 to intercept events that occurred in the configuration process.\r
1130\r
1131 @param[in] This Pointer to the EFI DHCPv4 Protocol.\r
1132 @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().\r
1133 @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.\r
1134 @param[in] Dhcp4Event The event that occurs in the current state, which usually means a\r
1135 state transition.\r
1136 @param[in] Packet The DHCPv4 packet that is going to be sent or already received.\r
1137 @param[out] NewPacket The packet that is used to replace the above Packet.\r
1138\r
1139 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.\r
1140 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol\r
1141 driver will continue to wait for more DHCPOFFER packets until the\r
1142 retry timeout expires.\r
1143 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process\r
1144 and return to the Dhcp4Init or Dhcp4InitReboot state.\r
1145\r
1146**/\r
1147EFI_STATUS\r
1148EFIAPI\r
1149PxeBcDhcp4CallBack (\r
1150 IN EFI_DHCP4_PROTOCOL *This,\r
1151 IN VOID *Context,\r
1152 IN EFI_DHCP4_STATE CurrentState,\r
1153 IN EFI_DHCP4_EVENT Dhcp4Event,\r
1154 IN EFI_DHCP4_PACKET *Packet OPTIONAL,\r
1155 OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL\r
1156 )\r
1157{\r
1158 PXEBC_PRIVATE_DATA *Private;\r
1159 EFI_PXE_BASE_CODE_MODE *Mode;\r
1160 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;\r
1161 EFI_DHCP4_PACKET_OPTION *MaxMsgSize;\r
1162 UINT16 Value;\r
1163 EFI_STATUS Status;\r
1164 BOOLEAN Received;\r
1165\r
1166 if ((Dhcp4Event != Dhcp4RcvdOffer) &&\r
1167 (Dhcp4Event != Dhcp4SelectOffer) &&\r
1168 (Dhcp4Event != Dhcp4SendDiscover) &&\r
1169 (Dhcp4Event != Dhcp4RcvdAck)) {\r
1170 return EFI_SUCCESS;\r
1171 }\r
1172\r
1173 Private = (PXEBC_PRIVATE_DATA *) Context;\r
1174 Mode = Private->PxeBc.Mode;\r
1175 Callback = Private->PxeBcCallback;\r
1176\r
1177 //\r
1178 // Override the Maximum DHCP Message Size.\r
1179 //\r
1180 MaxMsgSize = PxeBcParseDhcp4Options (\r
1181 Packet->Dhcp4.Option,\r
1182 GET_OPTION_BUFFER_LEN (Packet),\r
142c00c3 1183 DHCP4_TAG_MAXMSG\r
a3bcde70
HT
1184 );\r
1185 if (MaxMsgSize != NULL) {\r
632dcfd6 1186 Value = HTONS (PXEBC_DHCP4_PACKET_MAX_SIZE);\r
a3bcde70
HT
1187 CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));\r
1188 }\r
1189\r
1190 //\r
1191 // Callback to user if any packets sent or received.\r
1192 //\r
1193 if (Dhcp4Event != Dhcp4SelectOffer && Callback != NULL) {\r
1194 Received = (BOOLEAN) (Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck);\r
1195 Status = Callback->Callback (\r
1196 Callback,\r
1197 Private->Function,\r
1198 Received,\r
1199 Packet->Length,\r
1200 (EFI_PXE_BASE_CODE_PACKET *) &Packet->Dhcp4\r
1201 );\r
1202 if (Status != EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE) {\r
1203 return EFI_ABORTED;\r
1204 }\r
1205 }\r
1206\r
1207 Status = EFI_SUCCESS;\r
1208\r
1209 switch (Dhcp4Event) {\r
1210\r
1211 case Dhcp4SendDiscover:\r
632dcfd6
FS
1212 if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {\r
1213 //\r
1214 // If the to be sent packet exceeds the maximum length, abort the DHCP process.\r
1215 //\r
1216 Status = EFI_ABORTED;\r
1217 break;\r
1218 }\r
1219\r
a3bcde70
HT
1220 //\r
1221 // Cache the DHCPv4 discover packet to mode data directly.\r
1222 // It need to check SendGuid as well as Dhcp4SendRequest.\r
1223 //\r
1224 CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp4, Packet->Length);\r
1225\r
1226 case Dhcp4SendRequest:\r
632dcfd6
FS
1227 if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {\r
1228 //\r
1229 // If the to be sent packet exceeds the maximum length, abort the DHCP process.\r
1230 //\r
1231 Status = EFI_ABORTED;\r
1232 break;\r
1233 }\r
1234 \r
a3bcde70
HT
1235 if (Mode->SendGUID) {\r
1236 //\r
1237 // Send the system Guid instead of the MAC address as the hardware address if required.\r
1238 //\r
19ddbb25 1239 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Packet->Dhcp4.Header.ClientHwAddr))) {\r
a3bcde70
HT
1240 //\r
1241 // Zero the Guid to indicate NOT programable if failed to get system Guid.\r
1242 //\r
1243 ZeroMem (Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));\r
1244 }\r
1245 Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);\r
1246 }\r
1247 break;\r
1248\r
1249 case Dhcp4RcvdOffer:\r
1250 Status = EFI_NOT_READY;\r
632dcfd6
FS
1251 if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {\r
1252 //\r
1253 // Ignore the incoming packets which exceed the maximum length.\r
1254 //\r
1255 break;\r
1256 }\r
a3bcde70
HT
1257 if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) {\r
1258 //\r
1259 // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record\r
1260 // the OfferIndex and OfferCount.\r
1261 //\r
1262 PxeBcCacheDhcp4Offer (Private, Packet);\r
1263 }\r
1264 break;\r
1265\r
1266 case Dhcp4SelectOffer:\r
1267 //\r
1268 // Select offer by the default policy or by order, and record the SelectIndex\r
1269 // and SelectProxyType.\r
1270 //\r
1271 PxeBcSelectDhcp4Offer (Private);\r
1272\r
1273 if (Private->SelectIndex == 0) {\r
1274 Status = EFI_ABORTED;\r
1275 } else {\r
1276 *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;\r
1277 }\r
1278 break;\r
1279\r
1280 case Dhcp4RcvdAck:\r
632dcfd6
FS
1281 if (Packet->Length > PXEBC_DHCP4_PACKET_MAX_SIZE) {\r
1282 //\r
1283 // Abort the DHCP if the ACK packet exceeds the maximum length.\r
1284 //\r
1285 Status = EFI_ABORTED;\r
1286 break;\r
1287 }\r
1288\r
a3bcde70
HT
1289 //\r
1290 // Cache the DHCPv4 ack to Private->Dhcp4Ack, but it's not the final ack in mode data\r
1291 // without verification.\r
1292 //\r
1293 ASSERT (Private->SelectIndex != 0);\r
1294\r
1295 PxeBcCopyDhcp4Ack (Private, Packet, FALSE);\r
1296 break;\r
1297\r
1298 default:\r
1299 break;\r
1300 }\r
1301\r
1302 return Status;\r
1303}\r
1304\r
1305\r
1306/**\r
1307 Build and send out the request packet for the bootfile, and parse the reply.\r
1308\r
1309 @param[in] Private Pointer to PxeBc private data.\r
1310 @param[in] Type PxeBc option boot item type.\r
1311 @param[in] Layer Pointer to option boot item layer.\r
1312 @param[in] UseBis Use BIS or not.\r
1313 @param[in] DestIp Pointer to the server address.\r
1314 @param[in] IpCount The total count of the server address.\r
1315 @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.\r
1316\r
1317 @retval EFI_SUCCESS Successfully discovered boot file.\r
1318 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r
1319 @retval EFI_NOT_FOUND Can't get the PXE reply packet.\r
1320 @retval Others Failed to discover boot file.\r
1321\r
1322**/\r
1323EFI_STATUS\r
1324PxeBcDhcp4Discover (\r
1325 IN PXEBC_PRIVATE_DATA *Private,\r
1326 IN UINT16 Type,\r
1327 IN UINT16 *Layer,\r
1328 IN BOOLEAN UseBis,\r
1329 IN EFI_IP_ADDRESS *DestIp,\r
1330 IN UINT16 IpCount,\r
1331 IN EFI_PXE_BASE_CODE_SRVLIST *SrvList\r
1332 )\r
1333{\r
1334 EFI_PXE_BASE_CODE_UDP_PORT Sport;\r
1335 EFI_PXE_BASE_CODE_MODE *Mode;\r
1336 EFI_DHCP4_PROTOCOL *Dhcp4;\r
1337 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token;\r
1338 BOOLEAN IsBCast;\r
1339 EFI_STATUS Status;\r
1340 UINT16 RepIndex;\r
1341 UINT16 SrvIndex;\r
1342 UINT16 TryIndex;\r
1343 EFI_DHCP4_LISTEN_POINT ListenPoint;\r
1344 EFI_DHCP4_PACKET *Response;\r
1345 UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];\r
1346 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];\r
1347 UINT32 OptCount;\r
1348 EFI_DHCP4_PACKET_OPTION *PxeOpt;\r
1349 PXEBC_OPTION_BOOT_ITEM *PxeBootItem;\r
1350 UINT8 VendorOptLen;\r
1351 UINT32 Xid;\r
1352\r
1353 Mode = Private->PxeBc.Mode;\r
1354 Dhcp4 = Private->Dhcp4;\r
1355 Status = EFI_SUCCESS;\r
1356\r
1357 ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN));\r
1358\r
1359 //\r
1360 // Use broadcast if destination address not specified.\r
1361 //\r
1362 if (DestIp == NULL) {\r
1363 Sport = PXEBC_DHCP4_S_PORT;\r
1364 IsBCast = TRUE;\r
1365 } else {\r
1366 Sport = PXEBC_BS_DISCOVER_PORT;\r
1367 IsBCast = FALSE;\r
1368 }\r
1369\r
1370 if (!UseBis && Layer != NULL) {\r
1371 *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;\r
1372 }\r
1373\r
1374 //\r
1375 // Build all the options for the request packet.\r
1376 //\r
1377 OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, TRUE);\r
1378\r
1379 if (Private->IsDoDiscover) {\r
1380 //\r
1381 // Add vendor option of PXE_BOOT_ITEM\r
1382 //\r
1383 VendorOptLen = (UINT8) ((sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1);\r
1384 OptList[OptCount] = AllocateZeroPool (VendorOptLen);\r
1385 if (OptList[OptCount] == NULL) {\r
1386 return EFI_OUT_OF_RESOURCES;\r
1387 }\r
1388\r
142c00c3 1389 OptList[OptCount]->OpCode = DHCP4_TAG_VENDOR;\r
a3bcde70
HT
1390 OptList[OptCount]->Length = (UINT8) (VendorOptLen - 2);\r
1391 PxeOpt = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data;\r
1392 PxeOpt->OpCode = PXEBC_VENDOR_TAG_BOOT_ITEM;\r
1393 PxeOpt->Length = (UINT8) sizeof (PXEBC_OPTION_BOOT_ITEM);\r
1394 PxeBootItem = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data;\r
1395 PxeBootItem->Type = HTONS (Type);\r
142c00c3 1396 PxeOpt->Data[PxeOpt->Length] = DHCP4_TAG_EOP;\r
a3bcde70
HT
1397\r
1398 if (Layer != NULL) {\r
1399 PxeBootItem->Layer = HTONS (*Layer);\r
1400 }\r
1401\r
1402 OptCount++;\r
1403 }\r
1404\r
1405 //\r
1406 // Build the request packet with seed packet and option list.\r
1407 //\r
1408 Status = Dhcp4->Build (\r
1409 Dhcp4,\r
1410 &Private->SeedPacket,\r
1411 0,\r
1412 NULL,\r
1413 OptCount,\r
1414 OptList,\r
1415 &Token.Packet\r
1416 );\r
1417 //\r
1418 // Free the vendor option of PXE_BOOT_ITEM.\r
1419 //\r
1420 if (Private->IsDoDiscover) {\r
1421 FreePool (OptList[OptCount - 1]);\r
1422 }\r
1423\r
1424 if (EFI_ERROR (Status)) {\r
1425 return Status;\r
1426 }\r
1427\r
1428 if (Mode->SendGUID) {\r
19ddbb25 1429 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Token.Packet->Dhcp4.Header.ClientHwAddr))) {\r
a3bcde70
HT
1430 //\r
1431 // Zero the Guid to indicate NOT programable if failed to get system Guid.\r
1432 //\r
1433 ZeroMem (Token.Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));\r
1434 }\r
1435 Token.Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);\r
1436 }\r
1437\r
1438 //\r
1439 // Set fields of the token for the request packet.\r
1440 //\r
1441 Xid = NET_RANDOM (NetRandomInitSeed ());\r
1442 Token.Packet->Dhcp4.Header.Xid = HTONL (Xid);\r
1443 Token.Packet->Dhcp4.Header.Reserved = HTONS ((UINT16) ((IsBCast) ? 0x8000 : 0x0));\r
1444 CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
1445\r
1446 Token.RemotePort = Sport;\r
1447\r
1448 if (IsBCast) {\r
1449 SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff);\r
1450 } else {\r
1451 CopyMem (&Token.RemoteAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));\r
1452 }\r
1453\r
1454 CopyMem (&Token.GatewayAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));\r
1455\r
1456 if (!IsBCast) {\r
1457 Token.ListenPointCount = 1;\r
1458 Token.ListenPoints = &ListenPoint;\r
1459 Token.ListenPoints[0].ListenPort = PXEBC_BS_DISCOVER_PORT;\r
1460 CopyMem (&Token.ListenPoints[0].ListenAddress, &Private->StationIp, sizeof(EFI_IPv4_ADDRESS));\r
1461 CopyMem (&Token.ListenPoints[0].SubnetMask, &Private->SubnetMask, sizeof(EFI_IPv4_ADDRESS));\r
1462 }\r
1463\r
1464 //\r
1465 // Send out the request packet to discover the bootfile.\r
1466 //\r
1467 for (TryIndex = 1; TryIndex <= PXEBC_BOOT_REQUEST_RETRIES; TryIndex++) {\r
1468\r
1469 Token.TimeoutValue = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * TryIndex);\r
1470 Token.Packet->Dhcp4.Header.Seconds = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * (TryIndex - 1));\r
1471\r
1472 Status = Dhcp4->TransmitReceive (Dhcp4, &Token);\r
1473 if (Token.Status != EFI_TIMEOUT) {\r
1474 break;\r
1475 }\r
1476 }\r
1477\r
1478 if (TryIndex > PXEBC_BOOT_REQUEST_RETRIES) {\r
1479 //\r
1480 // No server response our PXE request\r
1481 //\r
1482 Status = EFI_TIMEOUT;\r
1483 }\r
1484\r
1485 if (!EFI_ERROR (Status)) {\r
1486\r
1487 RepIndex = 0;\r
1488 SrvIndex = 0;\r
1489 Response = Token.ResponseList;\r
1490 //\r
1491 // Find the right PXE Reply according to server address.\r
1492 //\r
1493 while (RepIndex < Token.ResponseCount) {\r
1494\r
1495 while (SrvIndex < IpCount) {\r
1496 if (SrvList[SrvIndex].AcceptAnyResponse) {\r
1497 break;\r
1498 }\r
1499 if ((SrvList[SrvIndex].Type == Type) &&\r
9063c328 1500 EFI_IP4_EQUAL (&Response->Dhcp4.Header.ServerAddr, &SrvList[SrvIndex].IpAddr)) {\r
a3bcde70
HT
1501 break;\r
1502 }\r
1503 SrvIndex++;\r
1504 }\r
1505\r
1506 if ((IpCount != SrvIndex) || (IpCount == 0)) {\r
1507 break;\r
1508 }\r
1509\r
1510 SrvIndex = 0;\r
1511 RepIndex++;\r
1512\r
1513 Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size);\r
1514 }\r
1515\r
1516 if (RepIndex < Token.ResponseCount) {\r
1517 //\r
1518 // Cache the right PXE reply packet here, set valid flag later.\r
1519 // Especially for PXE discover packet, store it into mode data here.\r
1520 //\r
1521 if (Private->IsDoDiscover) {\r
1522 PxeBcCacheDhcp4Packet (&Private->PxeReply.Dhcp4.Packet.Ack, Response);\r
1523 CopyMem (&Mode->PxeDiscover, &Token.Packet->Dhcp4, Token.Packet->Length);\r
1524 } else {\r
1525 PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Response);\r
1526 }\r
1527 } else {\r
1528 //\r
1529 // Not found the right PXE reply packet.\r
1530 //\r
1531 Status = EFI_NOT_FOUND;\r
1532 }\r
1533 if (Token.ResponseList != NULL) {\r
1534 FreePool (Token.ResponseList);\r
1535 }\r
1536 }\r
1537\r
1538 FreePool (Token.Packet);\r
1539 return Status;\r
1540}\r
1541\r
ac99793b
ZL
1542/**\r
1543 Switch the Ip4 policy to static.\r
1544\r
1545 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.\r
1546\r
1547 @retval EFI_SUCCESS The policy is already configured to static.\r
1548 @retval Others Other error as indicated..\r
1549\r
1550**/\r
1551EFI_STATUS\r
1552PxeBcSetIp4Policy ( \r
1553 IN PXEBC_PRIVATE_DATA *Private\r
1554 )\r
1555{\r
1556 EFI_STATUS Status;\r
1557 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;\r
1558 EFI_IP4_CONFIG2_POLICY Policy;\r
1559 UINTN DataSize;\r
1560\r
1561 Ip4Config2 = Private->Ip4Config2;\r
1562 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);\r
1563 Status = Ip4Config2->GetData (\r
1564 Ip4Config2,\r
1565 Ip4Config2DataTypePolicy,\r
1566 &DataSize,\r
1567 &Policy\r
1568 );\r
1569 if (EFI_ERROR (Status)) {\r
1570 return Status;\r
1571 }\r
1572 \r
1573 if (Policy != Ip4Config2PolicyStatic) {\r
1574 Policy = Ip4Config2PolicyStatic;\r
1575 Status= Ip4Config2->SetData (\r
1576 Ip4Config2,\r
1577 Ip4Config2DataTypePolicy,\r
1578 sizeof (EFI_IP4_CONFIG2_POLICY),\r
1579 &Policy\r
1580 );\r
1581 if (EFI_ERROR (Status)) {\r
1582 return Status;\r
1583 } \r
1584 }\r
1585\r
1586 return EFI_SUCCESS;\r
1587}\r
a3bcde70
HT
1588\r
1589/**\r
1590 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information.\r
1591\r
1592 @param[in] Private Pointer to PxeBc private data.\r
1593 @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL\r
1594\r
1595 @retval EFI_SUCCESS The D.O.R.A process successfully finished.\r
1596 @retval Others Failed to finish the D.O.R.A process.\r
1597\r
1598**/\r
1599EFI_STATUS\r
1600PxeBcDhcp4Dora (\r
1601 IN PXEBC_PRIVATE_DATA *Private,\r
1602 IN EFI_DHCP4_PROTOCOL *Dhcp4\r
1603 )\r
1604{\r
1605 EFI_PXE_BASE_CODE_MODE *PxeMode;\r
1606 EFI_DHCP4_CONFIG_DATA Config;\r
1607 EFI_DHCP4_MODE_DATA Mode;\r
1608 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];\r
1609 UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];\r
1610 UINT32 OptCount;\r
1611 EFI_STATUS Status;\r
1612\r
1613 ASSERT (Dhcp4 != NULL);\r
1614\r
1615 Status = EFI_SUCCESS;\r
1616 PxeMode = Private->PxeBc.Mode;\r
1617\r
1618 //\r
1619 // Build option list for the request packet.\r
1620 //\r
1621 OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, FALSE);\r
1622 ASSERT (OptCount> 0);\r
1623\r
1624 ZeroMem (&Mode, sizeof (EFI_DHCP4_MODE_DATA));\r
1625 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));\r
1626\r
1627 Config.OptionCount = OptCount;\r
1628 Config.OptionList = OptList;\r
1629 Config.Dhcp4Callback = PxeBcDhcp4CallBack;\r
1630 Config.CallbackContext = Private;\r
1631 Config.DiscoverTryCount = PXEBC_DHCP_RETRIES;\r
1632 Config.DiscoverTimeout = mPxeDhcpTimeout;\r
1633\r
1634 //\r
1635 // Configure the DHCPv4 instance for PXE boot.\r
1636 //\r
1637 Status = Dhcp4->Configure (Dhcp4, &Config);\r
1638 if (EFI_ERROR (Status)) {\r
1639 goto ON_EXIT;\r
1640 }\r
1641\r
1642 //\r
1643 // Initialize the record fields for DHCPv4 offer in private data.\r
1644 //\r
1645 Private->IsProxyRecved = FALSE;\r
1646 Private->OfferNum = 0;\r
1647 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
1648 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
1649\r
1650 //\r
61047329
ZL
1651 // Start DHCPv4 D.O.R.A. process to acquire IPv4 address. This may \r
1652 // have already been done, thus do not leave in error if the return\r
1653 // code is EFI_ALREADY_STARTED.\r
a3bcde70
HT
1654 //\r
1655 Status = Dhcp4->Start (Dhcp4, NULL);\r
61047329 1656 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r
a3bcde70
HT
1657 if (Status == EFI_ICMP_ERROR) {\r
1658 PxeMode->IcmpErrorReceived = TRUE;\r
1659 }\r
1660 goto ON_EXIT;\r
1661 }\r
1662\r
1663 //\r
1664 // Get the acquired IPv4 address and store them.\r
1665 //\r
1666 Status = Dhcp4->GetModeData (Dhcp4, &Mode);\r
1667 if (EFI_ERROR (Status)) {\r
1668 goto ON_EXIT;\r
1669 }\r
1670\r
1671 ASSERT (Mode.State == Dhcp4Bound);\r
1672\r
1673 CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));\r
1674 CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
1675 CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r
1676 CopyMem (&PxeMode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
1677 CopyMem (&PxeMode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
1678\r
0e7f6f50 1679 Status = PxeBcFlushStationIp (Private, &Private->StationIp, &Private->SubnetMask);\r
a3bcde70
HT
1680 if (EFI_ERROR (Status)) {\r
1681 goto ON_EXIT;\r
1682 }\r
1683\r
1684 //\r
1685 // Check the selected offer whether BINL retry is needed.\r
1686 //\r
1687 Status = PxeBcHandleDhcp4Offer (Private);\r
1688\r
1689 AsciiPrint ("\n Station IP address is ");\r
1690\r
1691 PxeBcShowIp4Addr (&Private->StationIp.v4);\r
9063c328 1692 AsciiPrint ("\n");\r
a3bcde70
HT
1693\r
1694ON_EXIT:\r
1695 if (EFI_ERROR (Status)) {\r
1696 Dhcp4->Stop (Dhcp4);\r
1697 Dhcp4->Configure (Dhcp4, NULL);\r
1698 } else {\r
1699 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));\r
1700 Dhcp4->Configure (Dhcp4, &Config);\r
1701 Private->IsAddressOk = TRUE;\r
1702 }\r
1703\r
1704 return Status;\r
1705}\r