]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c
NetworkPkg: Refine codes related to Dhcpv4 and Dhcpv6 configuration.
[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
259 Value = NTOHS (PXEBC_DHCP4_PACKET_MAX_SIZE - 8);\r
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
1186 Value = HTONS (PXEBC_DHCP4_PACKET_MAX_SIZE - 8);\r
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
1212 //\r
1213 // Cache the DHCPv4 discover packet to mode data directly.\r
1214 // It need to check SendGuid as well as Dhcp4SendRequest.\r
1215 //\r
1216 CopyMem (&Mode->DhcpDiscover.Dhcpv4, &Packet->Dhcp4, Packet->Length);\r
1217\r
1218 case Dhcp4SendRequest:\r
1219 if (Mode->SendGUID) {\r
1220 //\r
1221 // Send the system Guid instead of the MAC address as the hardware address if required.\r
1222 //\r
19ddbb25 1223 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Packet->Dhcp4.Header.ClientHwAddr))) {\r
a3bcde70
HT
1224 //\r
1225 // Zero the Guid to indicate NOT programable if failed to get system Guid.\r
1226 //\r
1227 ZeroMem (Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));\r
1228 }\r
1229 Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);\r
1230 }\r
1231 break;\r
1232\r
1233 case Dhcp4RcvdOffer:\r
1234 Status = EFI_NOT_READY;\r
1235 if (Private->OfferNum < PXEBC_OFFER_MAX_NUM) {\r
1236 //\r
1237 // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record\r
1238 // the OfferIndex and OfferCount.\r
1239 //\r
1240 PxeBcCacheDhcp4Offer (Private, Packet);\r
1241 }\r
1242 break;\r
1243\r
1244 case Dhcp4SelectOffer:\r
1245 //\r
1246 // Select offer by the default policy or by order, and record the SelectIndex\r
1247 // and SelectProxyType.\r
1248 //\r
1249 PxeBcSelectDhcp4Offer (Private);\r
1250\r
1251 if (Private->SelectIndex == 0) {\r
1252 Status = EFI_ABORTED;\r
1253 } else {\r
1254 *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;\r
1255 }\r
1256 break;\r
1257\r
1258 case Dhcp4RcvdAck:\r
1259 //\r
1260 // Cache the DHCPv4 ack to Private->Dhcp4Ack, but it's not the final ack in mode data\r
1261 // without verification.\r
1262 //\r
1263 ASSERT (Private->SelectIndex != 0);\r
1264\r
1265 PxeBcCopyDhcp4Ack (Private, Packet, FALSE);\r
1266 break;\r
1267\r
1268 default:\r
1269 break;\r
1270 }\r
1271\r
1272 return Status;\r
1273}\r
1274\r
1275\r
1276/**\r
1277 Build and send out the request packet for the bootfile, and parse the reply.\r
1278\r
1279 @param[in] Private Pointer to PxeBc private data.\r
1280 @param[in] Type PxeBc option boot item type.\r
1281 @param[in] Layer Pointer to option boot item layer.\r
1282 @param[in] UseBis Use BIS or not.\r
1283 @param[in] DestIp Pointer to the server address.\r
1284 @param[in] IpCount The total count of the server address.\r
1285 @param[in] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.\r
1286\r
1287 @retval EFI_SUCCESS Successfully discovered boot file.\r
1288 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r
1289 @retval EFI_NOT_FOUND Can't get the PXE reply packet.\r
1290 @retval Others Failed to discover boot file.\r
1291\r
1292**/\r
1293EFI_STATUS\r
1294PxeBcDhcp4Discover (\r
1295 IN PXEBC_PRIVATE_DATA *Private,\r
1296 IN UINT16 Type,\r
1297 IN UINT16 *Layer,\r
1298 IN BOOLEAN UseBis,\r
1299 IN EFI_IP_ADDRESS *DestIp,\r
1300 IN UINT16 IpCount,\r
1301 IN EFI_PXE_BASE_CODE_SRVLIST *SrvList\r
1302 )\r
1303{\r
1304 EFI_PXE_BASE_CODE_UDP_PORT Sport;\r
1305 EFI_PXE_BASE_CODE_MODE *Mode;\r
1306 EFI_DHCP4_PROTOCOL *Dhcp4;\r
1307 EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN Token;\r
1308 BOOLEAN IsBCast;\r
1309 EFI_STATUS Status;\r
1310 UINT16 RepIndex;\r
1311 UINT16 SrvIndex;\r
1312 UINT16 TryIndex;\r
1313 EFI_DHCP4_LISTEN_POINT ListenPoint;\r
1314 EFI_DHCP4_PACKET *Response;\r
1315 UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];\r
1316 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];\r
1317 UINT32 OptCount;\r
1318 EFI_DHCP4_PACKET_OPTION *PxeOpt;\r
1319 PXEBC_OPTION_BOOT_ITEM *PxeBootItem;\r
1320 UINT8 VendorOptLen;\r
1321 UINT32 Xid;\r
1322\r
1323 Mode = Private->PxeBc.Mode;\r
1324 Dhcp4 = Private->Dhcp4;\r
1325 Status = EFI_SUCCESS;\r
1326\r
1327 ZeroMem (&Token, sizeof (EFI_DHCP4_TRANSMIT_RECEIVE_TOKEN));\r
1328\r
1329 //\r
1330 // Use broadcast if destination address not specified.\r
1331 //\r
1332 if (DestIp == NULL) {\r
1333 Sport = PXEBC_DHCP4_S_PORT;\r
1334 IsBCast = TRUE;\r
1335 } else {\r
1336 Sport = PXEBC_BS_DISCOVER_PORT;\r
1337 IsBCast = FALSE;\r
1338 }\r
1339\r
1340 if (!UseBis && Layer != NULL) {\r
1341 *Layer &= EFI_PXE_BASE_CODE_BOOT_LAYER_MASK;\r
1342 }\r
1343\r
1344 //\r
1345 // Build all the options for the request packet.\r
1346 //\r
1347 OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, TRUE);\r
1348\r
1349 if (Private->IsDoDiscover) {\r
1350 //\r
1351 // Add vendor option of PXE_BOOT_ITEM\r
1352 //\r
1353 VendorOptLen = (UINT8) ((sizeof (EFI_DHCP4_PACKET_OPTION) - 1) * 2 + sizeof (PXEBC_OPTION_BOOT_ITEM) + 1);\r
1354 OptList[OptCount] = AllocateZeroPool (VendorOptLen);\r
1355 if (OptList[OptCount] == NULL) {\r
1356 return EFI_OUT_OF_RESOURCES;\r
1357 }\r
1358\r
142c00c3 1359 OptList[OptCount]->OpCode = DHCP4_TAG_VENDOR;\r
a3bcde70
HT
1360 OptList[OptCount]->Length = (UINT8) (VendorOptLen - 2);\r
1361 PxeOpt = (EFI_DHCP4_PACKET_OPTION *) OptList[OptCount]->Data;\r
1362 PxeOpt->OpCode = PXEBC_VENDOR_TAG_BOOT_ITEM;\r
1363 PxeOpt->Length = (UINT8) sizeof (PXEBC_OPTION_BOOT_ITEM);\r
1364 PxeBootItem = (PXEBC_OPTION_BOOT_ITEM *) PxeOpt->Data;\r
1365 PxeBootItem->Type = HTONS (Type);\r
142c00c3 1366 PxeOpt->Data[PxeOpt->Length] = DHCP4_TAG_EOP;\r
a3bcde70
HT
1367\r
1368 if (Layer != NULL) {\r
1369 PxeBootItem->Layer = HTONS (*Layer);\r
1370 }\r
1371\r
1372 OptCount++;\r
1373 }\r
1374\r
1375 //\r
1376 // Build the request packet with seed packet and option list.\r
1377 //\r
1378 Status = Dhcp4->Build (\r
1379 Dhcp4,\r
1380 &Private->SeedPacket,\r
1381 0,\r
1382 NULL,\r
1383 OptCount,\r
1384 OptList,\r
1385 &Token.Packet\r
1386 );\r
1387 //\r
1388 // Free the vendor option of PXE_BOOT_ITEM.\r
1389 //\r
1390 if (Private->IsDoDiscover) {\r
1391 FreePool (OptList[OptCount - 1]);\r
1392 }\r
1393\r
1394 if (EFI_ERROR (Status)) {\r
1395 return Status;\r
1396 }\r
1397\r
1398 if (Mode->SendGUID) {\r
19ddbb25 1399 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) Token.Packet->Dhcp4.Header.ClientHwAddr))) {\r
a3bcde70
HT
1400 //\r
1401 // Zero the Guid to indicate NOT programable if failed to get system Guid.\r
1402 //\r
1403 ZeroMem (Token.Packet->Dhcp4.Header.ClientHwAddr, sizeof (EFI_GUID));\r
1404 }\r
1405 Token.Packet->Dhcp4.Header.HwAddrLen = (UINT8) sizeof (EFI_GUID);\r
1406 }\r
1407\r
1408 //\r
1409 // Set fields of the token for the request packet.\r
1410 //\r
1411 Xid = NET_RANDOM (NetRandomInitSeed ());\r
1412 Token.Packet->Dhcp4.Header.Xid = HTONL (Xid);\r
1413 Token.Packet->Dhcp4.Header.Reserved = HTONS ((UINT16) ((IsBCast) ? 0x8000 : 0x0));\r
1414 CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
1415\r
1416 Token.RemotePort = Sport;\r
1417\r
1418 if (IsBCast) {\r
1419 SetMem (&Token.RemoteAddress, sizeof (EFI_IPv4_ADDRESS), 0xff);\r
1420 } else {\r
1421 CopyMem (&Token.RemoteAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));\r
1422 }\r
1423\r
1424 CopyMem (&Token.GatewayAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));\r
1425\r
1426 if (!IsBCast) {\r
1427 Token.ListenPointCount = 1;\r
1428 Token.ListenPoints = &ListenPoint;\r
1429 Token.ListenPoints[0].ListenPort = PXEBC_BS_DISCOVER_PORT;\r
1430 CopyMem (&Token.ListenPoints[0].ListenAddress, &Private->StationIp, sizeof(EFI_IPv4_ADDRESS));\r
1431 CopyMem (&Token.ListenPoints[0].SubnetMask, &Private->SubnetMask, sizeof(EFI_IPv4_ADDRESS));\r
1432 }\r
1433\r
1434 //\r
1435 // Send out the request packet to discover the bootfile.\r
1436 //\r
1437 for (TryIndex = 1; TryIndex <= PXEBC_BOOT_REQUEST_RETRIES; TryIndex++) {\r
1438\r
1439 Token.TimeoutValue = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * TryIndex);\r
1440 Token.Packet->Dhcp4.Header.Seconds = (UINT16) (PXEBC_BOOT_REQUEST_TIMEOUT * (TryIndex - 1));\r
1441\r
1442 Status = Dhcp4->TransmitReceive (Dhcp4, &Token);\r
1443 if (Token.Status != EFI_TIMEOUT) {\r
1444 break;\r
1445 }\r
1446 }\r
1447\r
1448 if (TryIndex > PXEBC_BOOT_REQUEST_RETRIES) {\r
1449 //\r
1450 // No server response our PXE request\r
1451 //\r
1452 Status = EFI_TIMEOUT;\r
1453 }\r
1454\r
1455 if (!EFI_ERROR (Status)) {\r
1456\r
1457 RepIndex = 0;\r
1458 SrvIndex = 0;\r
1459 Response = Token.ResponseList;\r
1460 //\r
1461 // Find the right PXE Reply according to server address.\r
1462 //\r
1463 while (RepIndex < Token.ResponseCount) {\r
1464\r
1465 while (SrvIndex < IpCount) {\r
1466 if (SrvList[SrvIndex].AcceptAnyResponse) {\r
1467 break;\r
1468 }\r
1469 if ((SrvList[SrvIndex].Type == Type) &&\r
9063c328 1470 EFI_IP4_EQUAL (&Response->Dhcp4.Header.ServerAddr, &SrvList[SrvIndex].IpAddr)) {\r
a3bcde70
HT
1471 break;\r
1472 }\r
1473 SrvIndex++;\r
1474 }\r
1475\r
1476 if ((IpCount != SrvIndex) || (IpCount == 0)) {\r
1477 break;\r
1478 }\r
1479\r
1480 SrvIndex = 0;\r
1481 RepIndex++;\r
1482\r
1483 Response = (EFI_DHCP4_PACKET *) ((UINT8 *) Response + Response->Size);\r
1484 }\r
1485\r
1486 if (RepIndex < Token.ResponseCount) {\r
1487 //\r
1488 // Cache the right PXE reply packet here, set valid flag later.\r
1489 // Especially for PXE discover packet, store it into mode data here.\r
1490 //\r
1491 if (Private->IsDoDiscover) {\r
1492 PxeBcCacheDhcp4Packet (&Private->PxeReply.Dhcp4.Packet.Ack, Response);\r
1493 CopyMem (&Mode->PxeDiscover, &Token.Packet->Dhcp4, Token.Packet->Length);\r
1494 } else {\r
1495 PxeBcCacheDhcp4Packet (&Private->ProxyOffer.Dhcp4.Packet.Offer, Response);\r
1496 }\r
1497 } else {\r
1498 //\r
1499 // Not found the right PXE reply packet.\r
1500 //\r
1501 Status = EFI_NOT_FOUND;\r
1502 }\r
1503 if (Token.ResponseList != NULL) {\r
1504 FreePool (Token.ResponseList);\r
1505 }\r
1506 }\r
1507\r
1508 FreePool (Token.Packet);\r
1509 return Status;\r
1510}\r
1511\r
ac99793b
ZL
1512/**\r
1513 Switch the Ip4 policy to static.\r
1514\r
1515 @param[in] Private The pointer to PXEBC_PRIVATE_DATA.\r
1516\r
1517 @retval EFI_SUCCESS The policy is already configured to static.\r
1518 @retval Others Other error as indicated..\r
1519\r
1520**/\r
1521EFI_STATUS\r
1522PxeBcSetIp4Policy ( \r
1523 IN PXEBC_PRIVATE_DATA *Private\r
1524 )\r
1525{\r
1526 EFI_STATUS Status;\r
1527 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;\r
1528 EFI_IP4_CONFIG2_POLICY Policy;\r
1529 UINTN DataSize;\r
1530\r
1531 Ip4Config2 = Private->Ip4Config2;\r
1532 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);\r
1533 Status = Ip4Config2->GetData (\r
1534 Ip4Config2,\r
1535 Ip4Config2DataTypePolicy,\r
1536 &DataSize,\r
1537 &Policy\r
1538 );\r
1539 if (EFI_ERROR (Status)) {\r
1540 return Status;\r
1541 }\r
1542 \r
1543 if (Policy != Ip4Config2PolicyStatic) {\r
1544 Policy = Ip4Config2PolicyStatic;\r
1545 Status= Ip4Config2->SetData (\r
1546 Ip4Config2,\r
1547 Ip4Config2DataTypePolicy,\r
1548 sizeof (EFI_IP4_CONFIG2_POLICY),\r
1549 &Policy\r
1550 );\r
1551 if (EFI_ERROR (Status)) {\r
1552 return Status;\r
1553 } \r
1554 }\r
1555\r
1556 return EFI_SUCCESS;\r
1557}\r
a3bcde70
HT
1558\r
1559/**\r
1560 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other PXE boot information.\r
1561\r
1562 @param[in] Private Pointer to PxeBc private data.\r
1563 @param[in] Dhcp4 Pointer to the EFI_DHCP4_PROTOCOL\r
1564\r
1565 @retval EFI_SUCCESS The D.O.R.A process successfully finished.\r
1566 @retval Others Failed to finish the D.O.R.A process.\r
1567\r
1568**/\r
1569EFI_STATUS\r
1570PxeBcDhcp4Dora (\r
1571 IN PXEBC_PRIVATE_DATA *Private,\r
1572 IN EFI_DHCP4_PROTOCOL *Dhcp4\r
1573 )\r
1574{\r
1575 EFI_PXE_BASE_CODE_MODE *PxeMode;\r
1576 EFI_DHCP4_CONFIG_DATA Config;\r
1577 EFI_DHCP4_MODE_DATA Mode;\r
1578 EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_OPTION_MAX_NUM];\r
1579 UINT8 Buffer[PXEBC_DHCP4_OPTION_MAX_SIZE];\r
1580 UINT32 OptCount;\r
1581 EFI_STATUS Status;\r
1582\r
1583 ASSERT (Dhcp4 != NULL);\r
1584\r
1585 Status = EFI_SUCCESS;\r
1586 PxeMode = Private->PxeBc.Mode;\r
1587\r
1588 //\r
1589 // Build option list for the request packet.\r
1590 //\r
1591 OptCount = PxeBcBuildDhcp4Options (Private, OptList, Buffer, FALSE);\r
1592 ASSERT (OptCount> 0);\r
1593\r
1594 ZeroMem (&Mode, sizeof (EFI_DHCP4_MODE_DATA));\r
1595 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));\r
1596\r
1597 Config.OptionCount = OptCount;\r
1598 Config.OptionList = OptList;\r
1599 Config.Dhcp4Callback = PxeBcDhcp4CallBack;\r
1600 Config.CallbackContext = Private;\r
1601 Config.DiscoverTryCount = PXEBC_DHCP_RETRIES;\r
1602 Config.DiscoverTimeout = mPxeDhcpTimeout;\r
1603\r
1604 //\r
1605 // Configure the DHCPv4 instance for PXE boot.\r
1606 //\r
1607 Status = Dhcp4->Configure (Dhcp4, &Config);\r
1608 if (EFI_ERROR (Status)) {\r
1609 goto ON_EXIT;\r
1610 }\r
1611\r
1612 //\r
1613 // Initialize the record fields for DHCPv4 offer in private data.\r
1614 //\r
1615 Private->IsProxyRecved = FALSE;\r
1616 Private->OfferNum = 0;\r
1617 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
1618 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
1619\r
1620 //\r
61047329
ZL
1621 // Start DHCPv4 D.O.R.A. process to acquire IPv4 address. This may \r
1622 // have already been done, thus do not leave in error if the return\r
1623 // code is EFI_ALREADY_STARTED.\r
a3bcde70
HT
1624 //\r
1625 Status = Dhcp4->Start (Dhcp4, NULL);\r
61047329 1626 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r
a3bcde70
HT
1627 if (Status == EFI_ICMP_ERROR) {\r
1628 PxeMode->IcmpErrorReceived = TRUE;\r
1629 }\r
1630 goto ON_EXIT;\r
1631 }\r
1632\r
1633 //\r
1634 // Get the acquired IPv4 address and store them.\r
1635 //\r
1636 Status = Dhcp4->GetModeData (Dhcp4, &Mode);\r
1637 if (EFI_ERROR (Status)) {\r
1638 goto ON_EXIT;\r
1639 }\r
1640\r
1641 ASSERT (Mode.State == Dhcp4Bound);\r
1642\r
1643 CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));\r
1644 CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
1645 CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r
1646 CopyMem (&PxeMode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
1647 CopyMem (&PxeMode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
1648\r
0e7f6f50 1649 Status = PxeBcFlushStationIp (Private, &Private->StationIp, &Private->SubnetMask);\r
a3bcde70
HT
1650 if (EFI_ERROR (Status)) {\r
1651 goto ON_EXIT;\r
1652 }\r
1653\r
1654 //\r
1655 // Check the selected offer whether BINL retry is needed.\r
1656 //\r
1657 Status = PxeBcHandleDhcp4Offer (Private);\r
1658\r
1659 AsciiPrint ("\n Station IP address is ");\r
1660\r
1661 PxeBcShowIp4Addr (&Private->StationIp.v4);\r
9063c328 1662 AsciiPrint ("\n");\r
a3bcde70
HT
1663\r
1664ON_EXIT:\r
1665 if (EFI_ERROR (Status)) {\r
1666 Dhcp4->Stop (Dhcp4);\r
1667 Dhcp4->Configure (Dhcp4, NULL);\r
1668 } else {\r
1669 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));\r
1670 Dhcp4->Configure (Dhcp4, &Config);\r
1671 Private->IsAddressOk = TRUE;\r
1672 }\r
1673\r
1674 return Status;\r
1675}\r